static void test_cr_detect_compression_bad_suffix(void) { cr_CompressionType ret; GError *tmp_err = NULL; // Plain ret = cr_detect_compression(FILE_COMPRESSED_0_PLAIN_BAD_SUFFIX, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_NO_COMPRESSION); g_assert(!tmp_err); ret = cr_detect_compression(FILE_COMPRESSED_1_PLAIN_BAD_SUFFIX, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_NO_COMPRESSION); g_assert(!tmp_err); // Gz ret = cr_detect_compression(FILE_COMPRESSED_0_GZ_BAD_SUFFIX, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_GZ_COMPRESSION); g_assert(!tmp_err); ret = cr_detect_compression(FILE_COMPRESSED_1_GZ_BAD_SUFFIX, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_GZ_COMPRESSION); g_assert(!tmp_err); // Bz2 ret = cr_detect_compression(FILE_COMPRESSED_0_BZ2_BAD_SUFFIX, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_BZ2_COMPRESSION); g_assert(!tmp_err); ret = cr_detect_compression(FILE_COMPRESSED_1_BZ2_BAD_SUFFIX, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_BZ2_COMPRESSION); g_assert(!tmp_err); // Xz ret = cr_detect_compression(FILE_COMPRESSED_0_XZ_BAD_SUFFIX, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_XZ_COMPRESSION); g_assert(!tmp_err); ret = cr_detect_compression(FILE_COMPRESSED_1_XZ_BAD_SUFFIX, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_XZ_COMPRESSION); g_assert(!tmp_err); }
static void test_cr_error_handling(void) { GError *tmp_err = NULL; cr_CompressionType type; CR_FILE *f; type = cr_detect_compression("/filename/that/should/not/exists", &tmp_err); g_assert_cmpint(type, ==, CR_CW_UNKNOWN_COMPRESSION); g_assert(tmp_err); g_assert_cmpint(tmp_err->code, ==, CRE_NOFILE); g_error_free(tmp_err); tmp_err = NULL; type = cr_detect_compression("/", &tmp_err); g_assert_cmpint(type, ==, CR_CW_UNKNOWN_COMPRESSION); g_assert(tmp_err); g_assert_cmpint(tmp_err->code, ==, CRE_NOFILE); g_error_free(tmp_err); tmp_err = NULL; f = cr_open("/", CR_CW_MODE_READ, CR_CW_AUTO_DETECT_COMPRESSION, &tmp_err); g_assert(!f); g_assert(tmp_err); g_assert_cmpint(tmp_err->code, ==, CRE_NOFILE); g_error_free(tmp_err); tmp_err = NULL; // Opening dir for writing f = cr_open("/", CR_CW_MODE_WRITE, CR_CW_NO_COMPRESSION, &tmp_err); g_assert(!f); g_assert(tmp_err); g_assert_cmpint(tmp_err->code, ==, CRE_IO); g_error_free(tmp_err); tmp_err = NULL; f = cr_open("/", CR_CW_MODE_WRITE, CR_CW_GZ_COMPRESSION, &tmp_err); g_assert(!f); g_assert(tmp_err); g_assert_cmpint(tmp_err->code, ==, CRE_GZ); g_error_free(tmp_err); tmp_err = NULL; f = cr_open("/", CR_CW_MODE_WRITE, CR_CW_BZ2_COMPRESSION, &tmp_err); g_assert(!f); g_assert(tmp_err); g_assert_cmpint(tmp_err->code, ==, CRE_IO); g_error_free(tmp_err); tmp_err = NULL; f = cr_open("/", CR_CW_MODE_WRITE, CR_CW_XZ_COMPRESSION, &tmp_err); g_assert(!f); g_assert(tmp_err); g_assert_cmpint(tmp_err->code, ==, CRE_XZ); g_error_free(tmp_err); tmp_err = NULL; // Opening plain text file as compressed char buf[256]; int ret; // gzread can read compressed as well as uncompressed, so this test // is useful. f = cr_open(FILE_COMPRESSED_1_PLAIN, CR_CW_MODE_READ, CR_CW_GZ_COMPRESSION, &tmp_err); g_assert(f); ret = cr_read(f, buf, 256, &tmp_err); g_assert_cmpint(ret, ==, FILE_COMPRESSED_1_CONTENT_LEN); g_assert(!tmp_err); ret = cr_close(f, &tmp_err); g_assert_cmpint(ret, ==, CRE_OK); g_assert(!tmp_err); f = cr_open(FILE_COMPRESSED_1_PLAIN, CR_CW_MODE_READ, CR_CW_BZ2_COMPRESSION, &tmp_err); g_assert(f); ret = cr_read(f, buf, 256, &tmp_err); g_assert_cmpint(ret, ==, -1); g_assert(tmp_err); g_assert_cmpint(tmp_err->code, ==, CRE_BZ2); g_error_free(tmp_err); tmp_err = NULL; ret = cr_close(f, &tmp_err); g_assert_cmpint(ret, ==, CRE_OK); g_assert(!tmp_err); f = cr_open(FILE_COMPRESSED_1_PLAIN, CR_CW_MODE_READ, CR_CW_XZ_COMPRESSION, &tmp_err); g_assert(f); ret = cr_read(f, buf, 256, &tmp_err); g_assert_cmpint(ret, ==, -1); g_assert(tmp_err); g_assert_cmpint(tmp_err->code, ==, CRE_XZ); g_error_free(tmp_err); tmp_err = NULL; ret = cr_close(f, &tmp_err); g_assert_cmpint(ret, ==, CRE_OK); g_assert(!tmp_err); }
int cr_repomd_record_fill(cr_RepomdRecord *md, cr_ChecksumType checksum_type, GError **err) { const char *checksum_str; cr_ChecksumType checksum_t; gchar *path; GError *tmp_err = NULL; assert(md); assert(!err || *err == NULL); if (!(md->location_real) || !strlen(md->location_real)) { g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_BADARG, "Empty locations in repomd record object."); return CRE_BADARG; } path = md->location_real; checksum_str = cr_checksum_name_str(checksum_type); checksum_t = checksum_type; if (!g_file_test(path, G_FILE_TEST_IS_REGULAR)) { // File doesn't exists g_warning("%s: File %s doesn't exists", __func__, path); g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_NOFILE, "File %s doesn't exists or not a regular file", path); return CRE_NOFILE; } // Compute checksum of compressed file if (!md->checksum_type || !md->checksum) { gchar *chksum; chksum = cr_checksum_file(path, checksum_t, &tmp_err); if (tmp_err) { int code = tmp_err->code; g_propagate_prefixed_error(err, tmp_err, "Error while checksum calculation of %s:", path); return code; } md->checksum_type = g_string_chunk_insert(md->chunk, checksum_str); md->checksum = g_string_chunk_insert(md->chunk, chksum); g_free(chksum); } // Compute checksum of non compressed content and its size if (!md->checksum_open_type || !md->checksum_open || !md->size_open) { cr_CompressionType com_type = cr_detect_compression(path, &tmp_err); if (tmp_err) { int code = tmp_err->code; g_propagate_prefixed_error(err, tmp_err, "Cannot detect compression type of %s: ", path); return code; } if (com_type != CR_CW_UNKNOWN_COMPRESSION && com_type != CR_CW_NO_COMPRESSION) { // File compressed by supported algorithm contentStat *open_stat = NULL; open_stat = cr_get_compressed_content_stat(path, checksum_t, &tmp_err); if (tmp_err) { int code = tmp_err->code; g_propagate_prefixed_error(err, tmp_err, "Error while computing stat of compressed content of %s:", path); return code; } md->checksum_open_type = g_string_chunk_insert(md->chunk, checksum_str); md->checksum_open = g_string_chunk_insert(md->chunk, open_stat->checksum); if (!md->size_open) md->size_open = open_stat->size; g_free(open_stat->checksum); g_free(open_stat); } else { if (com_type != CR_CW_NO_COMPRESSION) { // Unknown compression g_warning("%s: File \"%s\" compressed by an unsupported type" " of compression", __func__, path); } md->checksum_open_type = NULL; md->checksum_open = NULL; md->size_open = -1; } } // Get timestamp and size of compressed file if (!md->timestamp || !md->size) { struct stat buf; if (!stat(path, &buf)) { if (!md->timestamp) { md->timestamp = buf.st_mtime; } if (!md->size) { md->size = buf.st_size; } } else { g_warning("%s: Stat on file \"%s\" failed", __func__, path); g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_STAT, "Stat() on %s failed: %s", path, strerror(errno)); return CRE_STAT; } } // Set db version if (!md->db_ver) md->db_ver = DEFAULT_DATABASE_VERSION; return CRE_OK; }