From: Michael Tremer Date: Tue, 21 Sep 2021 13:18:40 +0000 (+0000) Subject: testsuite: Make tests cleanup after themselves X-Git-Tag: 0.9.28~956 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e2e5298699cfb3244820ec4eef6616970f32deb1;p=pakfire.git testsuite: Make tests cleanup after themselves Signed-off-by: Michael Tremer --- diff --git a/tests/libpakfire/arch.c b/tests/libpakfire/arch.c index aa3097bda..b35006604 100644 --- a/tests/libpakfire/arch.c +++ b/tests/libpakfire/arch.c @@ -38,6 +38,9 @@ static int test_native(const struct test* t) { ASSERT(arch1 == arch2); return EXIT_SUCCESS; + +FAIL: + return EXIT_SUCCESS; } static int test_supported(const struct test* t) { @@ -54,6 +57,9 @@ static int test_supported(const struct test* t) { ASSERT(!r); return EXIT_SUCCESS; + +FAIL: + return EXIT_SUCCESS; } static int test_compatible(const struct test* t) { @@ -80,6 +86,9 @@ static int test_compatible(const struct test* t) { ASSERT(!r); return EXIT_SUCCESS; + +FAIL: + return EXIT_SUCCESS; } static int test_machine(const struct test* t) { @@ -95,6 +104,9 @@ static int test_machine(const struct test* t) { ASSERT(r == 0); return EXIT_SUCCESS; + +FAIL: + return EXIT_SUCCESS; } int main(int argc, char** argv) { diff --git a/tests/libpakfire/archive.c b/tests/libpakfire/archive.c index b3e09ef4b..64de50b7c 100644 --- a/tests/libpakfire/archive.c +++ b/tests/libpakfire/archive.c @@ -32,11 +32,13 @@ #define TEST_PKG1_FILE "usr/bin/beep" static int test_open(const struct test* t) { + struct pakfire_archive* archive = NULL; + int r = EXIT_FAILURE; + const char* path = TEST_SRC_PATH TEST_PKG1_PATH; LOG("Trying to open %s\n", path); // Open the archive - struct pakfire_archive* archive; ASSERT_SUCCESS(pakfire_archive_open(&archive, t->pakfire, path)); pakfire_archive_verify_status_t status; @@ -45,40 +47,55 @@ static int test_open(const struct test* t) { ASSERT(pakfire_archive_verify(archive, &status, NULL) == 0); ASSERT(status == PAKFIRE_ARCHIVE_VERIFY_OK); - pakfire_archive_unref(archive); + // Everything passed + r = EXIT_SUCCESS; + +FAIL: + if (archive) + pakfire_archive_unref(archive); - return EXIT_SUCCESS; + return r; } static int test_filelist(const struct test* t) { const char* path = TEST_SRC_PATH TEST_PKG1_PATH; + int r = EXIT_FAILURE; + + struct pakfire_archive* archive = NULL; + struct pakfire_filelist* list = NULL; // Open the archive - struct pakfire_archive* archive; ASSERT_SUCCESS(pakfire_archive_open(&archive, t->pakfire, path)); // Fetch the filelist - struct pakfire_filelist* list = pakfire_archive_get_filelist(archive); + list = pakfire_archive_get_filelist(archive); ASSERT(list); // This packages has 7 files ASSERT(pakfire_filelist_size(list) == 7); - // Cleanup - pakfire_filelist_unref(list); - pakfire_archive_unref(archive); - return EXIT_SUCCESS; + // Everything passed + r = EXIT_SUCCESS; + +FAIL: + if (list) + pakfire_filelist_unref(list); + if (archive) + pakfire_archive_unref(archive); + + return r; } static int test_extract(const struct test* t) { const char* path = TEST_SRC_PATH TEST_PKG1_PATH; + int r = EXIT_FAILURE; + + struct pakfire_archive* archive = NULL; - struct pakfire_archive* archive; ASSERT_SUCCESS(pakfire_archive_open(&archive, t->pakfire, path)); // Extract the archive payload - int r = pakfire_archive_extract(archive, NULL); - ASSERT(r == 0); + ASSERT_SUCCESS(pakfire_archive_extract(archive, NULL)); #if 0 char file[PATH_MAX]; @@ -88,34 +105,47 @@ static int test_extract(const struct test* t) { ASSERT_SUCCESS(access(file, F_OK)); #endif - pakfire_archive_unref(archive); + r = EXIT_SUCCESS; - return EXIT_SUCCESS; +FAIL: + if (archive) + pakfire_archive_unref(archive); + + return r; } static int test_import(const struct test* t) { const char* path = TEST_SRC_PATH TEST_PKG1_PATH; + int r = EXIT_FAILURE; - struct pakfire_archive* archive; - ASSERT_SUCCESS(pakfire_archive_open(&archive, t->pakfire, path)); - + struct pakfire_archive* archive = NULL; struct pakfire_repo* repo = NULL; + struct pakfire_package* package = NULL; + + // Open archive + ASSERT_SUCCESS(pakfire_archive_open(&archive, t->pakfire, path)); + ASSERT(archive); // Create a new repository ASSERT_SUCCESS(pakfire_repo_create(&repo, t->pakfire, "tmp")); ASSERT(repo); - struct pakfire_package* package = NULL; - // Add the package to the repository ASSERT_SUCCESS(pakfire_repo_add_archive(repo, archive, &package)); ASSERT(package); - pakfire_repo_unref(repo); - pakfire_package_unref(package); - pakfire_archive_unref(archive); + // Everything passed + r = EXIT_SUCCESS; + +FAIL: + if (repo) + pakfire_repo_unref(repo); + if (package) + pakfire_package_unref(package); + if (archive) + pakfire_archive_unref(archive); - return EXIT_SUCCESS; + return r; } int main(int argc, char** argv) { diff --git a/tests/libpakfire/cgroup.c b/tests/libpakfire/cgroup.c index 61502d955..5cc8b61f7 100644 --- a/tests/libpakfire/cgroup.c +++ b/tests/libpakfire/cgroup.c @@ -36,6 +36,9 @@ static int test_create_and_destroy(const struct test* t) { ); return EXIT_SUCCESS; + +FAIL: + return EXIT_FAILURE; } static int test_attach(const struct test* t) { @@ -75,6 +78,9 @@ static int test_attach(const struct test* t) { ); return EXIT_SUCCESS; + +FAIL: + return EXIT_FAILURE; } static void handle_signal(int signum) { @@ -107,6 +113,9 @@ static pid_t fork_child_process() { return child_process(); return pid; + +FAIL: + abort(); } static int test_killall(const struct test* t) { @@ -132,6 +141,9 @@ static int test_killall(const struct test* t) { ); return EXIT_SUCCESS; + +FAIL: + return EXIT_FAILURE; } static int test_cpustat(const struct test* t) { @@ -165,6 +177,9 @@ static int test_cpustat(const struct test* t) { ); return EXIT_SUCCESS; + +FAIL: + return EXIT_FAILURE; } static int test_nice(const struct test* t) { @@ -182,6 +197,9 @@ static int test_nice(const struct test* t) { ); return EXIT_SUCCESS; + +FAIL: + return EXIT_FAILURE; } static int test_random_name(const struct test* t) { @@ -195,6 +213,9 @@ static int test_random_name(const struct test* t) { ASSERT_STRING_NOT_EQUALS(name1, name2); return EXIT_SUCCESS; + +FAIL: + return EXIT_FAILURE; } int main(int argc, char** argv) { diff --git a/tests/libpakfire/compress.c b/tests/libpakfire/compress.c index a6c153f12..d9a46b1f6 100644 --- a/tests/libpakfire/compress.c +++ b/tests/libpakfire/compress.c @@ -31,14 +31,17 @@ const char TEST_DATA[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n"; static int read_test(const struct test* t, FILE* (function)(FILE* f, const char* mode), const char* file) { + int r = EXIT_FAILURE; + + FILE* f = NULL; char path[PATH_MAX]; char buffer[1024]; - int r = snprintf(path, sizeof(path) - 1, "%s/%s", TEST_SRC_PATH, file); + r = pakfire_string_format(path, "%s/%s", TEST_SRC_PATH, file); ASSERT(r >= 0); - FILE* f = fopen(path, "r"); - ASSERT(f); + // Open file + ASSERT(f = fopen(path, "r")); // Engage decompressor f = function(f, "r"); @@ -50,17 +53,25 @@ static int read_test(const struct test* t, // Buffer should equal the test data ASSERT(bytes_read >= sizeof(TEST_DATA) - 1); - ASSERT(memcmp(buffer, TEST_DATA, sizeof(TEST_DATA) - 1) == 0); + ASSERT_SUCCESS(memcmp(buffer, TEST_DATA, sizeof(TEST_DATA) - 1)); - fclose(f); + // Everything passed + r = EXIT_SUCCESS; - return EXIT_SUCCESS; +FAIL: + if (f) + fclose(f); + + return r; } static int write_test(const struct test* t, FILE* (function)(FILE* f, const char* mode)) { + int r = EXIT_FAILURE; + + FILE* f = NULL; + // Create a backend storage file - FILE* f = test_mktemp(); - ASSERT(f); + ASSERT(f = test_mktemp()); // Open compressed file for writing f = function(f, "w"); @@ -72,9 +83,14 @@ static int write_test(const struct test* t, FILE* (function)(FILE* f, const char ASSERT(bytes_written == sizeof(TEST_DATA) - 1); } - fclose(f); + // Everything passed + r = EXIT_SUCCESS; - return EXIT_SUCCESS; +FAIL: + if (f) + fclose(f); + + return r; } static int test_xzfopen_read(const struct test* t) { @@ -94,10 +110,13 @@ static int test_zstdfopen_write(const struct test* t) { } static int test_xfopen(const struct test* t) { - ASSERT(read_test(t, pakfire_xfopen, "data/compress/data.xz") == EXIT_SUCCESS); - ASSERT(read_test(t, pakfire_xfopen, "data/compress/data.zst") == EXIT_SUCCESS); + ASSERT_SUCCESS(read_test(t, pakfire_xfopen, "data/compress/data.xz")); + ASSERT_SUCCESS(read_test(t, pakfire_xfopen, "data/compress/data.zst")); return EXIT_SUCCESS; + +FAIL: + return EXIT_FAILURE; } int main(int argc, char** argv) { diff --git a/tests/libpakfire/config.c b/tests/libpakfire/config.c index fa8c8f98e..f2d4daaf8 100644 --- a/tests/libpakfire/config.c +++ b/tests/libpakfire/config.c @@ -23,7 +23,8 @@ #include "../testsuite.h" static int test_get_and_set(const struct test* t) { - struct pakfire_config* config; + struct pakfire_config* config = NULL; + int r = EXIT_FAILURE; ASSERT_SUCCESS(pakfire_config_create(&config)); @@ -63,12 +64,17 @@ static int test_get_and_set(const struct test* t) { ASSERT(pakfire_config_has_section(config, "section1")); ASSERT(!pakfire_config_has_section(config, "section3")); + // Everything passed + r = EXIT_SUCCESS; + +FAIL: pakfire_config_unref(config); - return EXIT_SUCCESS; + return r; } static int test_parse(const struct test* t) { + int r = EXIT_FAILURE; char* TEST_INPUT = "key1 = value1\n" "\n" @@ -83,7 +89,7 @@ static int test_parse(const struct test* t) { FILE* f = fmemopen(TEST_INPUT, strlen(TEST_INPUT), "r"); ASSERT(f); - struct pakfire_config* config; + struct pakfire_config* config = NULL; ASSERT_SUCCESS(pakfire_config_create(&config)); ASSERT_SUCCESS(pakfire_config_read(config, f)); @@ -110,11 +116,15 @@ static int test_parse(const struct test* t) { ASSERT_STRING_EQUALS(sections[1], "section2"); ASSERT_NULL(sections[2]); - pakfire_config_unref(config); + // Everything passed + r = EXIT_SUCCESS; - fclose(f); +FAIL: + if (f) + fclose(f); + pakfire_config_unref(config); - return EXIT_SUCCESS; + return r; } int main(int argc, char** argv) { diff --git a/tests/libpakfire/db.c b/tests/libpakfire/db.c index aa595c78e..6bcc60769 100644 --- a/tests/libpakfire/db.c +++ b/tests/libpakfire/db.c @@ -27,49 +27,65 @@ #include "../testsuite.h" static int test_open_ro(const struct test* t) { - struct pakfire_db* db; + struct pakfire_db* db = NULL; + int r = EXIT_FAILURE; - int r = pakfire_db_open(&db, t->pakfire, PAKFIRE_DB_READONLY); - ASSERT(!r); + ASSERT_SUCCESS(pakfire_db_open(&db, t->pakfire, PAKFIRE_DB_READONLY)); - pakfire_db_unref(db); + // Everything passed + r = EXIT_SUCCESS; - return 0; +FAIL: + if (db) + pakfire_db_unref(db); + + return r; } static int test_open_rw(const struct test* t) { - struct pakfire_db* db; + struct pakfire_db* db = NULL; + int r = EXIT_FAILURE; + + ASSERT_SUCCESS(pakfire_db_open(&db, t->pakfire, PAKFIRE_DB_READWRITE)); - int r = pakfire_db_open(&db, t->pakfire, PAKFIRE_DB_READWRITE); - ASSERT(!r); + // Everything passed + r = EXIT_SUCCESS; - pakfire_db_unref(db); +FAIL: + if (db) + pakfire_db_unref(db); - return 0; + return r; } static int test_check(const struct test* t) { - struct pakfire_db* db; + struct pakfire_db* db = NULL; + int r = EXIT_FAILURE; - int r = pakfire_db_open(&db, t->pakfire, PAKFIRE_DB_READWRITE); - ASSERT(!r); + ASSERT_SUCCESS(pakfire_db_open(&db, t->pakfire, PAKFIRE_DB_READWRITE)); // Perform check - ASSERT(!pakfire_db_check(db)); + ASSERT_SUCCESS(pakfire_db_check(db)); + + // Everything passed + r = EXIT_SUCCESS; - pakfire_db_unref(db); +FAIL: + if (db) + pakfire_db_unref(db); - return 0; + return r; } static int test_add_package(const struct test* t) { - struct pakfire_db* db; + struct pakfire_db* db = NULL; + struct pakfire_repo* repo = NULL; + struct pakfire_archive* archive = NULL; + int r = EXIT_FAILURE; - struct pakfire_repo* repo = pakfire_get_repo(t->pakfire, "@dummy"); - ASSERT(repo); + ASSERT(repo = pakfire_get_repo(t->pakfire, "@dummy")); - int r = pakfire_db_open(&db, t->pakfire, PAKFIRE_DB_READWRITE); - ASSERT(!r); + ASSERT_SUCCESS(pakfire_db_open(&db, t->pakfire, PAKFIRE_DB_READWRITE)); // There must be no packages installed ssize_t packages = pakfire_db_packages(db); @@ -78,7 +94,6 @@ static int test_add_package(const struct test* t) { const char* path = TEST_SRC_PATH "/data/beep-1.3-2.ip3.x86_64.pfm"; // Open archive - struct pakfire_archive* archive; ASSERT_SUCCESS(pakfire_archive_open(&archive, t->pakfire, path)); ASSERT(archive); @@ -89,28 +104,31 @@ static int test_add_package(const struct test* t) { ASSERT(package); // Try to add the package to the database - r = pakfire_db_add_package(db, package, archive, 1); - ASSERT(r == 0); + ASSERT_SUCCESS(pakfire_db_add_package(db, package, archive, 1)); // One package should be installed - packages = pakfire_db_packages(db); - ASSERT(packages == 1); + ASSERT(pakfire_db_packages(db) == 1); // Remove the package again - r = pakfire_db_remove_package(db, package); - ASSERT(r == 0); + ASSERT_SUCCESS(pakfire_db_remove_package(db, package)); // No packages should be installed any more - packages = pakfire_db_packages(db); - ASSERT(packages == 0); - - // Cleanup - pakfire_archive_unref(archive); - pakfire_db_unref(db); - pakfire_package_unref(package); - pakfire_repo_unref(repo); - - return 0; + ASSERT(pakfire_db_packages(db) == 0); + + // Everything passed + r = EXIT_SUCCESS; + +FAIL: + if (archive) + pakfire_archive_unref(archive); + if (db) + pakfire_db_unref(db); + if (package) + pakfire_package_unref(package); + if (repo) + pakfire_repo_unref(repo); + + return r; } int main(int argc, char** argv) { diff --git a/tests/libpakfire/downloader.c b/tests/libpakfire/downloader.c index febb25ee8..5fe0bd3fa 100644 --- a/tests/libpakfire/downloader.c +++ b/tests/libpakfire/downloader.c @@ -29,53 +29,65 @@ #define DOWNLOAD_PATH TEST_ROOTFS "/downloaded.file" static int test_simple(const struct test* t) { - struct pakfire_downloader* d; + struct pakfire_downloader* d = NULL; + int r = EXIT_FAILURE; // Create downloader - int r = pakfire_downloader_create(&d, t->pakfire); - ASSERT(r == 0); + ASSERT_SUCCESS(pakfire_downloader_create(&d, t->pakfire)); // Retrieve a file - r = pakfire_downloader_retrieve(d, NULL, NULL, NULL, DOWNLOAD_URL, DOWNLOAD_PATH, - PAKFIRE_DIGEST_NONE, NULL, 0, 0); - ASSERT(r == 0); + ASSERT_SUCCESS( + pakfire_downloader_retrieve(d, NULL, NULL, NULL, DOWNLOAD_URL, DOWNLOAD_PATH, + PAKFIRE_DIGEST_NONE, NULL, 0, 0) + ); - // Cleanup - pakfire_downloader_unref(d); + // Everything passed + r = EXIT_SUCCESS; - return EXIT_SUCCESS; +FAIL: + if (d) + pakfire_downloader_unref(d); + + return r; } static int test_retrieve_with_pending_transfers(const struct test* t) { - struct pakfire_downloader* d; + struct pakfire_downloader* d = NULL; + int r = EXIT_FAILURE; // Create downloader - int r = pakfire_downloader_create(&d, t->pakfire); - ASSERT(r == 0); + ASSERT_SUCCESS(pakfire_downloader_create(&d, t->pakfire)); // Add a transfer - r = pakfire_downloader_add_transfer(d, NULL, NULL, NULL, DOWNLOAD_URL, DOWNLOAD_PATH, - PAKFIRE_DIGEST_NONE, NULL, 0, 0); - ASSERT(r == 0); + ASSERT_SUCCESS( + pakfire_downloader_add_transfer(d, NULL, NULL, NULL, DOWNLOAD_URL, DOWNLOAD_PATH, + PAKFIRE_DIGEST_NONE, NULL, 0, 0) + ); // Retrieve a file - r = pakfire_downloader_retrieve(d, NULL, NULL, NULL, DOWNLOAD_URL, DOWNLOAD_PATH, - PAKFIRE_DIGEST_NONE, NULL, 0, 0); - ASSERT(r == 0); + ASSERT_SUCCESS( + pakfire_downloader_retrieve(d, NULL, NULL, NULL, DOWNLOAD_URL, DOWNLOAD_PATH, + PAKFIRE_DIGEST_NONE, NULL, 0, 0) + ); + - // Cleanup - pakfire_downloader_unref(d); + // Everything passed + r = EXIT_SUCCESS; - return EXIT_SUCCESS; +FAIL: + if (d) + pakfire_downloader_unref(d); + + return r; } static int test_retrieve_with_mirrors(const struct test* t) { - struct pakfire_downloader* d; - struct pakfire_mirrorlist* m; + struct pakfire_downloader* d = NULL; + struct pakfire_mirrorlist* m = NULL; + int r = EXIT_FAILURE; // Create downloader - int r = pakfire_downloader_create(&d, t->pakfire); - ASSERT(r == 0); + ASSERT_SUCCESS(pakfire_downloader_create(&d, t->pakfire)); // Create mirrorlist ASSERT_SUCCESS(pakfire_mirrorlist_create(&m, t->pakfire)); @@ -93,11 +105,16 @@ static int test_retrieve_with_mirrors(const struct test* t) { ASSERT_SUCCESS(pakfire_downloader_retrieve(d, NULL, m, NULL, "beep-1.3-2.ip3.x86_64.pfm", DOWNLOAD_PATH, PAKFIRE_DIGEST_NONE, NULL, 0, 0)); - // Cleanup - ASSERT_NULL(pakfire_mirrorlist_unref(m)); - ASSERT_NULL(pakfire_downloader_unref(d)); + // Everything passed + r = EXIT_SUCCESS; + +FAIL: + if (d) + pakfire_downloader_unref(d); + if (m) + pakfire_mirrorlist_unref(m); - return 0; + return r; } int main(int argc, char** argv) { diff --git a/tests/libpakfire/execute.c b/tests/libpakfire/execute.c index 2775983bd..823646513 100644 --- a/tests/libpakfire/execute.c +++ b/tests/libpakfire/execute.c @@ -30,10 +30,12 @@ static const char* cmd[2] = { }; static int test_does_not_exist(const struct test* t) { - int r = pakfire_execute(t->pakfire, cmd, NULL, 0, NULL, NULL); - ASSERT(r != 0); + ASSERT_FAILURE(pakfire_execute(t->pakfire, cmd, NULL, 0, NULL, NULL)); return EXIT_SUCCESS; + +FAIL: + return EXIT_FAILURE; } int main(int argc, char** argv) { diff --git a/tests/libpakfire/key.c b/tests/libpakfire/key.c index 29f9a7e49..b6fa3904e 100644 --- a/tests/libpakfire/key.c +++ b/tests/libpakfire/key.c @@ -29,6 +29,7 @@ static int test_init(const struct test* t) { struct pakfire_key** keys = NULL; + int r = EXIT_FAILURE; // Try loading any keys & delete them all ASSERT_SUCCESS(pakfire_list_keys(t->pakfire, &keys)); @@ -45,7 +46,11 @@ static int test_init(const struct test* t) { // Must be empty now ASSERT(keys == NULL); - return EXIT_SUCCESS; + // Everything passed + r = EXIT_SUCCESS; + +FAIL: + return r; } static int test_import_export(const struct test* t) { diff --git a/tests/libpakfire/main.c b/tests/libpakfire/main.c index ccb1fbdb8..7b8478292 100644 --- a/tests/libpakfire/main.c +++ b/tests/libpakfire/main.c @@ -33,6 +33,9 @@ static int test_path(const struct test* t) { ASSERT_STRING_STARTSWITH(path, TEST_ROOTFS); return EXIT_SUCCESS; + +FAIL: + return EXIT_FAILURE; } int main(int argc, char** argv) { diff --git a/tests/libpakfire/makefile.c b/tests/libpakfire/makefile.c index 6a44c3939..4ec9ef796 100644 --- a/tests/libpakfire/makefile.c +++ b/tests/libpakfire/makefile.c @@ -44,16 +44,22 @@ static int load_macros(struct pakfire_parser* parser) { ASSERT(r == 0); for (unsigned int i = 0; i < buffer.gl_pathc; i++) { - r = pakfire_parser_read_file(parser, buffer.gl_pathv[i], NULL); - ASSERT(r == 0); + ASSERT_SUCCESS( + pakfire_parser_read_file(parser, buffer.gl_pathv[i], NULL) + ); } - return 0; + return EXIT_SUCCESS; + +FAIL: + return EXIT_FAILURE; } static int test_parse(const struct test* t) { + struct pakfire_parser* parser = NULL; const char** makefile = makefiles; char path[PATH_MAX]; + int r = EXIT_FAILURE; while (*makefile) { int r = snprintf(path, sizeof(path) - 1, "%s/%s", TEST_SRC_PATH, *makefile); @@ -63,43 +69,57 @@ static int test_parse(const struct test* t) { FILE* f = fopen(path, "r"); ASSERT(f); - struct pakfire_parser* parser = pakfire_parser_create(t->pakfire, NULL, NULL, 0); + parser = pakfire_parser_create(t->pakfire, NULL, NULL, 0); - r = pakfire_parser_read(parser, f, NULL); - ASSERT(r == 0); + ASSERT_SUCCESS(pakfire_parser_read(parser, f, NULL)); pakfire_parser_unref(parser); + parser = NULL; fclose(f); // Next file makefile++; } - return EXIT_SUCCESS; + // Everything passed + r = EXIT_SUCCESS; + +FAIL: + if (parser) + pakfire_parser_unref(parser); + + return r; } static int test_macros(const struct test* t) { + int r = EXIT_FAILURE; + struct pakfire_parser* parser = pakfire_parser_create(t->pakfire, NULL, NULL, 0); ASSERT(parser); // Load 'em all - int r = load_macros(parser); - if (r) - return r; + ASSERT_SUCCESS(load_macros(parser)); - pakfire_parser_unref(parser); + // Everything passed + r = EXIT_SUCCESS; - return EXIT_SUCCESS; +FAIL: + if (parser) + pakfire_parser_unref(parser); + + return r; } static int test_packages(const struct test* t) { + struct pakfire_parser* parser = NULL; struct pakfire_package* pkg = NULL; struct pakfire_repo* repo = NULL; + int r = EXIT_FAILURE; ASSERT_SUCCESS(pakfire_repo_create(&repo, t->pakfire, "test")); ASSERT(repo); - struct pakfire_parser* parser = pakfire_parser_create(t->pakfire, NULL, NULL, + parser = pakfire_parser_create(t->pakfire, NULL, NULL, PAKFIRE_PARSER_FLAGS_EXPAND_COMMANDS); ASSERT(parser); @@ -107,15 +127,12 @@ static int test_packages(const struct test* t) { pakfire_parser_set(parser, NULL, "DISTRO_ARCH", "x86_64", 0); // Load macros - int r = load_macros(parser); - if (r) - return r; + ASSERT_SUCCESS(load_macros(parser)); // Read beep.nm const char* path = TEST_SRC_PATH "data/beep.nm"; - r = pakfire_parser_read_file(parser, path, NULL); - ASSERT(r == 0); + ASSERT_SUCCESS(pakfire_parser_read_file(parser, path, NULL)); // Create package r = pakfire_parser_create_package(parser, &pkg, repo, NULL, NULL); @@ -132,12 +149,18 @@ static int test_packages(const struct test* t) { const char* name = pakfire_package_get_name(pkg); ASSERT_STRING_EQUALS(name, "beep"); - // Cleanup - pakfire_parser_unref(parser); - pakfire_package_unref(pkg); - pakfire_repo_unref(repo); + // Everything passed + r = EXIT_SUCCESS; - return EXIT_SUCCESS; +FAIL: + if (parser) + pakfire_parser_unref(parser); + if (pkg) + pakfire_package_unref(pkg); + if (repo) + pakfire_repo_unref(repo); + + return r; } int main(int argc, char** argv) { diff --git a/tests/libpakfire/packager.c b/tests/libpakfire/packager.c index 05f81ba2a..73afb38d4 100644 --- a/tests/libpakfire/packager.c +++ b/tests/libpakfire/packager.c @@ -28,35 +28,41 @@ #include "../testsuite.h" static int test_create(const struct test* t) { - struct pakfire_packager* packager; + struct pakfire_packager* packager = NULL; + struct pakfire_package* pkg = NULL; struct pakfire_repo* repo = NULL; + int r = EXIT_FAILURE; ASSERT_SUCCESS(pakfire_repo_create(&repo, t->pakfire, "test")); ASSERT(repo); - struct pakfire_package* pkg = pakfire_package_create(t->pakfire, repo, + pkg = pakfire_package_create(t->pakfire, repo, "test", "1.0-1", "src"); ASSERT(pkg); // Create packager - int r = pakfire_packager_create(&packager, pkg); - ASSERT(r == 0); + ASSERT_SUCCESS(pakfire_packager_create(&packager, pkg)); // Add a file to the package const char* path = TEST_SRC_PATH "data/beep-1.3-2.ip3.x86_64.pfm"; - r = pakfire_packager_add(packager, path, NULL); - ASSERT(r == 0); + ASSERT_SUCCESS(pakfire_packager_add(packager, path, NULL)); // Write archive FILE* f = test_mktemp(); r = pakfire_packager_finish(packager, f); ASSERT(r == 0); - // Cleanup - pakfire_packager_unref(packager); - pakfire_package_unref(pkg); - pakfire_repo_unref(repo); + // Everything passed + r = EXIT_SUCCESS; + +FAIL: + if (packager) + pakfire_packager_unref(packager); + if (pkg) + pakfire_package_unref(pkg); + if (repo) + pakfire_repo_unref(repo); return EXIT_SUCCESS; } diff --git a/tests/libpakfire/parser.c b/tests/libpakfire/parser.c index f77c07931..52d0a312a 100644 --- a/tests/libpakfire/parser.c +++ b/tests/libpakfire/parser.c @@ -28,7 +28,9 @@ #include "../testsuite.h" static int test_parser(const struct test* t) { + struct pakfire_parser* subparser = NULL; char* value = NULL; + int r = EXIT_FAILURE; // Create a new parser struct pakfire_parser* parser = pakfire_parser_create(t->pakfire, NULL, NULL, 0); @@ -38,23 +40,21 @@ static int test_parser(const struct test* t) { ASSERT(!value); // Set a value - int r = pakfire_parser_set(parser, NULL, "a", "a", 0); - ASSERT(r == 0); + ASSERT_SUCCESS(pakfire_parser_set(parser, NULL, "a", "a", 0)); // Retrieve the value again value = pakfire_parser_get(parser, NULL, "a"); ASSERT_STRING_EQUALS(value, "a"); // Append something to the value - r = pakfire_parser_append(parser, NULL, "a", "b"); - ASSERT(r == 0); + ASSERT_SUCCESS(pakfire_parser_append(parser, NULL, "a", "b")); // Retrieve the value again value = pakfire_parser_get(parser, NULL, "a"); ASSERT_STRING_EQUALS(value, "a b"); // Make a child parser - struct pakfire_parser* subparser = pakfire_parser_create_child(parser, "child"); + subparser = pakfire_parser_create_child(parser, "child"); ASSERT(subparser); // Try to get a again @@ -62,8 +62,7 @@ static int test_parser(const struct test* t) { ASSERT_STRING_EQUALS(value, "a b"); // Append something to the subparser - r = pakfire_parser_append(subparser, NULL, "a", "c"); - ASSERT(r == 0); + ASSERT_SUCCESS(pakfire_parser_append(subparser, NULL, "a", "c")); // The subparser should return "a b c" value = pakfire_parser_get(subparser, NULL, "a"); @@ -74,15 +73,13 @@ static int test_parser(const struct test* t) { ASSERT_STRING_EQUALS(value, "a b"); // Set another value - r = pakfire_parser_append(subparser, NULL, "b", "1"); - ASSERT(r == 0); + ASSERT_SUCCESS(pakfire_parser_append(subparser, NULL, "b", "1")); // Merge the two parsers pakfire_parser_merge(parser, subparser); // Set a variable - r = pakfire_parser_set(parser, NULL, "c", "%{b}", 0); - ASSERT(r == 0); + ASSERT_SUCCESS(pakfire_parser_set(parser, NULL, "c", "%{b}", 0)); // Get the value of c value = pakfire_parser_get(parser, NULL, "c"); @@ -92,12 +89,18 @@ static int test_parser(const struct test* t) { char* s = pakfire_parser_dump(parser); printf("%s\n", s); - // Cleanup - pakfire_parser_unref(subparser); - pakfire_parser_unref(parser); - free(value); + // Everything passed + r = EXIT_SUCCESS; - return EXIT_SUCCESS; +FAIL: + if (subparser) + pakfire_parser_unref(subparser); + if (parser) + pakfire_parser_unref(parser); + if (value) + free(value); + + return r; } static const char* files[] = { @@ -114,6 +117,7 @@ static const char* files[] = { static int test_parser_files(const struct test* t) { const char** file = files; char path[PATH_MAX]; + int r = EXIT_FAILURE; while (*file) { int r = snprintf(path, sizeof(path) - 1, "%s/%s", TEST_SRC_PATH, *file); @@ -138,25 +142,36 @@ static int test_parser_files(const struct test* t) { file++; } - return EXIT_SUCCESS; + // Everything passed + r = EXIT_SUCCESS; + +FAIL: + return r; } static int test_parser_command(const struct test* t) { const char* command = "%(echo \"ABC\")"; + struct pakfire_parser* parser = NULL; + int r = EXIT_FAILURE; - struct pakfire_parser* parser = pakfire_parser_create(t->pakfire, NULL, NULL, + parser = pakfire_parser_create(t->pakfire, NULL, NULL, PAKFIRE_PARSER_FLAGS_EXPAND_COMMANDS); ASSERT(parser); - ASSERT(pakfire_parser_set(parser, NULL, "command", command, 0) == 0); + ASSERT_SUCCESS(pakfire_parser_set(parser, NULL, "command", command, 0)); // Retrieve the expanded value char* value = pakfire_parser_get(parser, NULL, "command"); ASSERT_STRING_EQUALS(value, "ABC"); - pakfire_parser_unref(parser); + // Everything passed + r = EXIT_SUCCESS; + +FAIL: + if (parser) + pakfire_parser_unref(parser); - return EXIT_SUCCESS; + return r; } int main(int argc, char** argv) { diff --git a/tests/libpakfire/progressbar.c b/tests/libpakfire/progressbar.c index 03ba54ba2..0d0bc8286 100644 --- a/tests/libpakfire/progressbar.c +++ b/tests/libpakfire/progressbar.c @@ -27,7 +27,8 @@ #include "../testsuite.h" static int test_run(const struct test* t) { - struct pakfire_progressbar* p; + struct pakfire_progressbar* p = NULL; + int r = EXIT_FAILURE; ASSERT_SUCCESS(pakfire_progressbar_create(&p, NULL)); @@ -57,9 +58,14 @@ static int test_run(const struct test* t) { ASSERT_SUCCESS(pakfire_progressbar_finish(p)); - ASSERT_NULL(pakfire_progressbar_unref(p)); + // Everything passed + r = EXIT_SUCCESS; - return EXIT_SUCCESS; +FAIL: + if (p) + pakfire_progressbar_unref(p); + + return r; } int main(int argc, char** argv) { diff --git a/tests/libpakfire/repo.c b/tests/libpakfire/repo.c index c28564f7f..cbf06ae1c 100644 --- a/tests/libpakfire/repo.c +++ b/tests/libpakfire/repo.c @@ -24,25 +24,30 @@ #include "../testsuite.h" static int test_scan(const struct test* t) { + struct pakfire_repo* repo = NULL; char baseurl[1024]; - snprintf(baseurl, sizeof(baseurl) - 1, "file://%s/data", TEST_SRC_PATH); + int r = EXIT_FAILURE; - struct pakfire_repo* repo = NULL; + pakfire_string_format(baseurl, "file://%s/data", TEST_SRC_PATH); ASSERT_SUCCESS(pakfire_repo_create(&repo, t->pakfire, "test")); ASSERT(repo); - pakfire_repo_set_baseurl(repo, baseurl); + ASSERT_SUCCESS(pakfire_repo_set_baseurl(repo, baseurl)); - int r = pakfire_repo_scan(repo, 0); - ASSERT(r == 0); + ASSERT_SUCCESS(pakfire_repo_scan(repo, 0)); // There should be one package in this repository now ASSERT(pakfire_repo_count(repo) == 1); - pakfire_repo_unref(repo); + // Everything passed + r = EXIT_SUCCESS; + +FAIL: + if (repo) + pakfire_repo_unref(repo); - return 0; + return r; } int main(int argc, char** argv) { diff --git a/tests/libpakfire/util.c b/tests/libpakfire/util.c index 2f7f47cea..c36bb06ba 100644 --- a/tests/libpakfire/util.c +++ b/tests/libpakfire/util.c @@ -35,6 +35,9 @@ static int test_basename(const struct test* t) { free(output); return EXIT_SUCCESS; + +FAIL: + return EXIT_FAILURE; } static int test_dirname(const struct test* t) { @@ -45,18 +48,19 @@ static int test_dirname(const struct test* t) { free(output); return EXIT_SUCCESS; + +FAIL: + return EXIT_FAILURE; } static int test_string_startswith(const struct test* t) { - int r; - - r = pakfire_string_startswith("ABC", "A"); - ASSERT(r); - - r = pakfire_string_startswith("ABC", "B"); - ASSERT(!r); + ASSERT_SUCCESS(pakfire_string_startswith("ABC", "A")); + ASSERT_FAILURE(pakfire_string_startswith("ABC", "B")); return EXIT_SUCCESS; + +FAIL: + return EXIT_FAILURE; } static int test_string_partition(const struct test* t) { @@ -106,6 +110,9 @@ static int test_string_partition(const struct test* t) { free(part2); return EXIT_SUCCESS; + +FAIL: + return EXIT_FAILURE; } static int test_string_replace(const struct test* t) { @@ -115,6 +122,9 @@ static int test_string_replace(const struct test* t) { ASSERT_STRING_EQUALS(result, "CCCCCCCCCCCC"); return EXIT_SUCCESS; + +FAIL: + return EXIT_FAILURE; } static int test_string_split(const struct test* t) { @@ -150,6 +160,9 @@ static int test_string_split(const struct test* t) { ASSERT_NULL(result[4]); return EXIT_SUCCESS; + +FAIL: + return EXIT_FAILURE; } int main(int argc, char** argv) { diff --git a/tests/testsuite.c b/tests/testsuite.c index 492f0bc5a..20594be8c 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -52,8 +52,6 @@ static int test_run(int i, struct test* t) { exit(1); } - ASSERT(t->pakfire); - // Enable debug logging pakfire_log_set_priority(t->pakfire, LOG_DEBUG); diff --git a/tests/testsuite.h b/tests/testsuite.h index ba7b95a71..2f4cb655e 100644 --- a/tests/testsuite.h +++ b/tests/testsuite.h @@ -60,7 +60,7 @@ int testsuite_run(); if ((!(expr))) { \ LOG_ERROR("Failed assertion: " #expr " %s:%d %s\n", \ __FILE__, __LINE__, __PRETTY_FUNCTION__); \ - return EXIT_FAILURE; \ + goto FAIL; \ } \ } while (0) @@ -69,7 +69,7 @@ int testsuite_run(); if (!((expr) == NULL)) { \ LOG_ERROR("Failed assertion: " #expr " == NULL %s:%d %s\n", \ __FILE__, __LINE__, __PRETTY_FUNCTION__); \ - return EXIT_FAILURE; \ + goto FAIL; \ } \ } while (0) @@ -78,7 +78,7 @@ int testsuite_run(); if ((expr)) { \ LOG_ERROR("Failed assertion: " #expr " %s:%d %s\n", \ __FILE__, __LINE__, __PRETTY_FUNCTION__); \ - return EXIT_FAILURE; \ + goto FAIL; \ } \ } while (0) @@ -87,7 +87,7 @@ int testsuite_run(); if ((expr) > 0) { \ LOG_ERROR("Failed assertion: " #expr " unexpectedly didn't fail in %s:%d %s\n", \ __FILE__, __LINE__, __PRETTY_FUNCTION__); \ - return EXIT_FAILURE; \ + goto FAIL; \ } \ } while (0) @@ -96,13 +96,13 @@ int testsuite_run(); if (!(expr)) { \ LOG_ERROR("Failed assertion: " #expr " unexpectedly didn't fail in %s:%d %s\n", \ __FILE__, __LINE__, __PRETTY_FUNCTION__); \ - return EXIT_FAILURE; \ + goto FAIL; \ } \ if (errno != e) { \ LOG_ERROR("Failed assertion: " #expr " failed with (%d - %s) " \ "but was expected to fail with (%d - %s) in %s:%d\n", \ errno, strerror(errno), e, strerror(e), __FILE__, __LINE__); \ - return EXIT_FAILURE; \ + goto FAIL; \ } \ } while (0) @@ -111,7 +111,7 @@ int testsuite_run(); if (strcmp(string, value) != 0) { \ LOG_ERROR("Failed assertion: " #value " (%s) != " #string " %s:%d %s\n", \ value, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ - return EXIT_FAILURE; \ + goto FAIL; \ } \ } while (0) @@ -120,7 +120,7 @@ int testsuite_run(); if (strcmp(value1, value2) == 0) { \ LOG_ERROR("Failed assertion: " #value1 " (%s) != " #value2 " (%s) %s:%d %s\n", \ value1, value2, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ - return EXIT_FAILURE; \ + goto FAIL; \ } \ } while (0) @@ -129,7 +129,7 @@ int testsuite_run(); if (strncmp(string, start, strlen(start)) != 0) { \ LOG_ERROR("Failed assertion: " #string " does not start with " #start " %s:%d %s\n", \ __FILE__, __LINE__, __PRETTY_FUNCTION__); \ - return EXIT_FAILURE; \ + goto FAIL; \ } \ } while (0)