From: Emil Velikov Date: Fri, 3 Jul 2026 11:45:56 +0000 (+0100) Subject: testsuite: convert all tests to TS_ASSERT() X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=bd7ed71b0123433b58517ed5935a29c9240e00d2;p=thirdparty%2Fkmod.git testsuite: convert all tests to TS_ASSERT() Currently we have some inconsistencies across the tests: - using TS_ASSERT vs not - (non TS_ASSERT tests) leaking on error vs not In practical terms, we are not too worried about the leaks, since the test failure comes first. As such, convert all the tests to TS_ASSERT(). This means we loose the useful error messages in a few instances, which could be re-introduced at a later point alongside a TS_ASSERT_MSG() macro. Signed-off-by: Emil Velikov Link: https://github.com/kmod-project/kmod/pull/375 Signed-off-by: Lucas De Marchi --- diff --git a/testsuite/test-blacklist.c b/testsuite/test-blacklist.c index 83e4e26..6ba996c 100644 --- a/testsuite/test-blacklist.c +++ b/testsuite/test-blacklist.c @@ -33,50 +33,34 @@ static int blacklist_1(void) static const char *const names[] = { "pcspkr", "pcspkr2", "floppy", "ext4" }; ctx = kmod_new(NULL, NULL); - if (ctx == NULL) - return EXIT_FAILURE; + TS_ASSERT(ctx != NULL); for (size_t i = 0; i < ARRAY_SIZE(names); i++) { err = kmod_module_new_from_name(ctx, names[i], &mod); - if (err < 0) - goto fail_lookup; + TS_ASSERT(err == 0); list = kmod_list_append(list, mod); } err = kmod_module_apply_filter(ctx, KMOD_FILTER_BLACKLIST, list, &filtered); - if (err < 0) { - ERR("Could not filter: %s\n", strerror(-err)); - goto fail; - } - if (filtered == NULL) { - ERR("All modules were filtered out!\n"); - goto fail; - } + TS_ASSERT(err == 0); + TS_ASSERT(filtered != NULL); kmod_list_foreach(l, filtered) { const char *modname; mod = kmod_module_get_module(l); modname = kmod_module_get_name(mod); - if (streq("pcspkr", modname) || streq("floppy", modname)) - goto fail; + TS_ASSERT(!streq("pcspkr", modname) && !streq("floppy", modname)); len++; kmod_module_unref(mod); } - if (len != 2) - goto fail; + TS_ASSERT(len == 2); kmod_module_unref_list(filtered); kmod_module_unref_list(list); kmod_unref(ctx); return EXIT_SUCCESS; - -fail: - kmod_module_unref_list(list); -fail_lookup: - kmod_unref(ctx); - return EXIT_FAILURE; } DEFINE_TEST(blacklist_1, .description = "check if modules are correctly blacklisted", diff --git a/testsuite/test-dependencies.c b/testsuite/test-dependencies.c index 24b2418..a88f3a5 100644 --- a/testsuite/test-dependencies.c +++ b/testsuite/test-dependencies.c @@ -28,14 +28,10 @@ static int test_dependencies(void) int fooa = 0, foob = 0, fooc = 0; ctx = kmod_new(NULL, NULL); - if (ctx == NULL) - return EXIT_FAILURE; + TS_ASSERT(ctx != NULL); err = kmod_module_new_from_name(ctx, "mod-foo", &mod); - if (err < 0 || mod == NULL) { - kmod_unref(ctx); - return EXIT_FAILURE; - } + TS_ASSERT(err == 0 && mod != NULL); list = kmod_module_get_dependencies(mod); @@ -56,8 +52,7 @@ static int test_dependencies(void) } /* fooa, foob, fooc */ - if (len != 3 || !fooa || !foob || !fooc) - return EXIT_FAILURE; + TS_ASSERT(len == 3 && fooa && foob && fooc); kmod_module_unref_list(list); kmod_module_unref(mod); diff --git a/testsuite/test-init.c b/testsuite/test-init.c index fb4cdce..3c0ff85 100644 --- a/testsuite/test-init.c +++ b/testsuite/test-init.c @@ -24,16 +24,12 @@ static int test_load_resources(void) int err; ctx = kmod_new(NULL, &null_config); - if (ctx == NULL) - return EXIT_FAILURE; + TS_ASSERT(ctx != NULL); kmod_set_log_priority(ctx, 7); err = kmod_load_resources(ctx); - if (err != 0) { - ERR("could not load libkmod resources: %s\n", strerror(-err)); - return EXIT_FAILURE; - } + TS_ASSERT(err == 0); kmod_unref(ctx); @@ -64,8 +60,7 @@ static int test_initlib(void) const char *null_config = NULL; ctx = kmod_new(NULL, &null_config); - if (ctx == NULL) - return EXIT_FAILURE; + TS_ASSERT(ctx != NULL); kmod_unref(ctx); @@ -81,20 +76,14 @@ static int test_insert(void) int err; ctx = kmod_new(NULL, &null_config); - if (ctx == NULL) - return EXIT_FAILURE; + TS_ASSERT(ctx != NULL); err = kmod_module_new_from_path(ctx, "/mod-simple.ko", &mod); - if (err != 0) { - ERR("could not create module from path: %s\n", strerror(-err)); - return EXIT_FAILURE; - } + TS_ASSERT(err == 0); err = kmod_module_insert_module(mod, 0, NULL); - if (err != 0) { - ERR("could not insert module: %s\n", strerror(-err)); - return EXIT_FAILURE; - } + TS_ASSERT(err == 0); + kmod_module_unref(mod); kmod_unref(ctx); @@ -116,32 +105,19 @@ static int test_remove(void) int err; ctx = kmod_new(NULL, &null_config); - if (ctx == NULL) - return EXIT_FAILURE; + TS_ASSERT(ctx != NULL); err = kmod_module_new_from_name(ctx, "mod-simple", &mod_simple); - if (err != 0) { - ERR("could not create module from name: %s\n", strerror(-err)); - return EXIT_FAILURE; - } + TS_ASSERT(err == 0); err = kmod_module_new_from_name(ctx, "bla", &mod_bla); - if (err != 0) { - ERR("could not create module from name: %s\n", strerror(-err)); - return EXIT_FAILURE; - } + TS_ASSERT(err == 0); err = kmod_module_remove_module(mod_simple, 0); - if (err != 0) { - ERR("could not remove module: %s\n", strerror(-err)); - return EXIT_FAILURE; - } + TS_ASSERT(err == 0); err = kmod_module_remove_module(mod_bla, 0); - if (err != -ENOENT) { - ERR("wrong return code for failure test: %d\n", err); - return EXIT_FAILURE; - } + TS_ASSERT(err == -ENOENT); kmod_module_unref(mod_bla); kmod_module_unref(mod_simple); diff --git a/testsuite/test-initstate.c b/testsuite/test-initstate.c index 0d46ac0..4d34303 100644 --- a/testsuite/test-initstate.c +++ b/testsuite/test-initstate.c @@ -27,28 +27,16 @@ static int test_initstate_from_lookup(void) int err, r; ctx = kmod_new(NULL, &null_config); - if (ctx == NULL) - return EXIT_FAILURE; + TS_ASSERT(ctx != NULL); err = kmod_module_new_from_lookup(ctx, "fake-builtin", &list); - if (err < 0) { - ERR("could not create module from lookup: %s\n", strerror(-err)); - return EXIT_FAILURE; - } - - if (!list) { - ERR("could not create module from lookup: module not found: fake-builtin\n"); - return EXIT_FAILURE; - } + TS_ASSERT(err == 0); + TS_ASSERT(list != NULL); mod = kmod_module_get_module(list); r = kmod_module_get_initstate(mod); - if (r != KMOD_MODULE_BUILTIN) { - ERR("module should have builtin state but is: %s\n", - kmod_module_initstate_str(r)); - return EXIT_FAILURE; - } + TS_ASSERT(r == KMOD_MODULE_BUILTIN); kmod_module_unref(mod); kmod_module_unref_list(list); @@ -73,26 +61,14 @@ static int test_initstate_from_name(void) int err, r; ctx = kmod_new(NULL, &null_config); - if (ctx == NULL) - return EXIT_FAILURE; + TS_ASSERT(ctx != NULL); err = kmod_module_new_from_name(ctx, "fake-builtin", &mod); - if (err != 0) { - ERR("could not create module from lookup: %s\n", strerror(-err)); - return EXIT_FAILURE; - } - - if (!mod) { - ERR("could not create module from lookup: module not found: fake-builtin\n"); - return EXIT_FAILURE; - } + TS_ASSERT(err == 0); + TS_ASSERT(mod != NULL); r = kmod_module_get_initstate(mod); - if (r != KMOD_MODULE_BUILTIN) { - ERR("module should have builtin state but is: %s\n", - kmod_module_initstate_str(r)); - return EXIT_FAILURE; - } + TS_ASSERT(r == KMOD_MODULE_BUILTIN); kmod_module_unref(mod); kmod_unref(ctx); diff --git a/testsuite/test-loaded.c b/testsuite/test-loaded.c index fdfb280..bf8efd7 100644 --- a/testsuite/test-loaded.c +++ b/testsuite/test-loaded.c @@ -22,15 +22,10 @@ static int loaded_1(void) int err; ctx = kmod_new(NULL, &null_config); - if (ctx == NULL) - return EXIT_FAILURE; + TS_ASSERT(ctx != NULL); err = kmod_module_new_from_loaded(ctx, &list); - if (err < 0) { - fprintf(stderr, "%s\n", strerror(-err)); - kmod_unref(ctx); - return EXIT_FAILURE; - } + TS_ASSERT(err == 0); printf("Module Size Used by\n"); diff --git a/testsuite/test-modprobe.c b/testsuite/test-modprobe.c index d48686f..c15f544 100644 --- a/testsuite/test-modprobe.c +++ b/testsuite/test-modprobe.c @@ -393,10 +393,7 @@ DEFINE_TEST(modprobe_module_from_abspath, static int modprobe_module_from_relpath(void) { - if (chdir("/home/foo") != 0) { - perror("failed to change into /home/foo"); - return EXIT_FAILURE; - } + TS_ASSERT(chdir("/home/foo") == 0); return EXEC_TOOL(modprobe, "./mod-simple.ko"); } diff --git a/testsuite/test-multi-softdep.c b/testsuite/test-multi-softdep.c index 74226a4..0d51f92 100644 --- a/testsuite/test-multi-softdep.c +++ b/testsuite/test-multi-softdep.c @@ -36,7 +36,6 @@ static int check_dependencies(const char *const *modnames, const struct kmod_list *itr; bool visited[MAX_SOFTDEP_N] = {}; int mod_index; - bool all_loaded = true; kmod_list_foreach(itr, mod_list) { struct kmod_module *softdep_mod = kmod_module_get_module(itr); @@ -56,15 +55,9 @@ static int check_dependencies(const char *const *modnames, for (mod_index = 0; mod_index < MAX_SOFTDEP_N; mod_index++) { if (!modnames[mod_index]) break; - if (!visited[mod_index]) { - ERR("softdep %s not loaded\n", modnames[mod_index]); - all_loaded = false; - } + TS_ASSERT(visited[mod_index]); } - if (all_loaded) - return 0; - else - return -1; + return 0; } static int multi_softdep(void) @@ -78,32 +71,26 @@ static int multi_softdep(void) int err; ctx = kmod_new(NULL, NULL); - if (ctx == NULL) - return EXIT_FAILURE; + TS_ASSERT(ctx != NULL); for (mod_index = 0; mod_index < ARRAY_SIZE(test_modules); mod_index++) { modname = test_modules[mod_index].modname; printf("module %s:\n", modname); err = kmod_module_new_from_name(ctx, modname, &mod); - if (err < 0) - goto fail; + TS_ASSERT(err == 0); + pre = NULL; post = NULL; err = kmod_module_get_softdeps(mod, &pre, &post); - if (err < 0) { - ERR("could not get softdeps of '%s': %s\n", modname, - strerror(-err)); - goto fail; - } + TS_ASSERT(err == 0); printf("pre: "); err = check_dependencies(test_modules[mod_index].pre, pre); - if (err < 0) - goto fail; + TS_ASSERT(err == 0); + printf("post: "); err = check_dependencies(test_modules[mod_index].post, post); - if (err < 0) - goto fail; + TS_ASSERT(err == 0); kmod_module_unref_list(pre); kmod_module_unref_list(post); @@ -111,13 +98,6 @@ static int multi_softdep(void) } kmod_unref(ctx); return EXIT_SUCCESS; - -fail: - kmod_module_unref_list(pre); - kmod_module_unref_list(post); - kmod_module_unref(mod); - kmod_unref(ctx); - return EXIT_FAILURE; } /* diff --git a/testsuite/test-new-module.c b/testsuite/test-new-module.c index c4968b2..30683a6 100644 --- a/testsuite/test-new-module.c +++ b/testsuite/test-new-module.c @@ -31,13 +31,11 @@ static int from_name(void) int err; ctx = kmod_new(NULL, &null_config); - if (ctx == NULL) - return EXIT_FAILURE; + TS_ASSERT(ctx != NULL); for (size_t i = 0; i < ARRAY_SIZE(modnames); i++) { err = kmod_module_new_from_name(ctx, modnames[i], &mod); - if (err < 0) - return EXIT_FAILURE; + TS_ASSERT(err == 0); printf("modname: %s\n", kmod_module_get_name(mod)); kmod_module_unref(mod); @@ -65,15 +63,13 @@ static int from_alias(void) int err; ctx = kmod_new(NULL, NULL); - if (ctx == NULL) - return EXIT_FAILURE; + TS_ASSERT(ctx != NULL); for (size_t i = 0; i < ARRAY_SIZE(modnames); i++) { struct kmod_list *l, *list = NULL; err = kmod_module_new_from_lookup(ctx, modnames[i], &list); - if (err < 0) - return EXIT_FAILURE; + TS_ASSERT(err == 0); kmod_list_foreach(l, list) { struct kmod_module *m; diff --git a/testsuite/test-remove.c b/testsuite/test-remove.c index f4f6975..b47e5e8 100644 --- a/testsuite/test-remove.c +++ b/testsuite/test-remove.c @@ -26,31 +26,19 @@ static int test_remove(void) struct stat st; ctx = kmod_new(NULL, &null_config); - if (ctx == NULL) - return EXIT_FAILURE; + TS_ASSERT(ctx != NULL); err = kmod_module_new_from_path(ctx, "/mod-simple.ko", &mod); - if (err != 0) { - ERR("could not create module from path: %s\n", strerror(-err)); - return EXIT_FAILURE; - } + TS_ASSERT(err == 0); err = kmod_module_insert_module(mod, 0, NULL); - if (err != 0) { - ERR("could not insert module: %s\n", strerror(-err)); - return EXIT_FAILURE; - } + TS_ASSERT(err == 0); err = kmod_module_remove_module(mod, 0); - if (err != 0) { - ERR("could not remove module: %s\n", strerror(-err)); - return EXIT_FAILURE; - } + TS_ASSERT(err == 0); + + TS_ASSERT(stat("/sys/module/mod_simple", &st) != 0 || !S_ISDIR(st.st_mode)); - if (stat("/sys/module/mod_simple", &st) == 0 && S_ISDIR(st.st_mode)) { - ERR("could not remove module directory.\n"); - return EXIT_FAILURE; - } kmod_module_unref(mod); kmod_unref(ctx); diff --git a/testsuite/test-testsuite.c b/testsuite/test-testsuite.c index 43c946f..9570c0d 100644 --- a/testsuite/test-testsuite.c +++ b/testsuite/test-testsuite.c @@ -26,15 +26,9 @@ static int testsuite_uname(void) struct utsname u; int err = uname(&u); - if (err < 0) - return EXIT_FAILURE; - - if (!streq(u.release, TEST_UNAME)) { - const char *ldpreload = getenv("LD_PRELOAD"); - ERR("u.release=%s should be %s\n", u.release, TEST_UNAME); - ERR("LD_PRELOAD=%s\n", ldpreload); - return EXIT_FAILURE; - } + TS_ASSERT(err == 0); + + TS_ASSERT(streq(u.release, TEST_UNAME)); return EXIT_SUCCESS; } @@ -50,15 +44,12 @@ static int testsuite_rootfs_fopen(void) int n; fp = fopen(MODULE_DIRECTORY "/a", "r"); - if (fp == NULL) - return EXIT_FAILURE; + TS_ASSERT(fp != NULL); n = fscanf(fp, "%s", s); - if (n != 1) - return EXIT_FAILURE; + TS_ASSERT(n == 1); - if (!streq(s, "kmod-test-chroot-works")) - return EXIT_FAILURE; + TS_ASSERT(streq(s, "kmod-test-chroot-works")); return EXIT_SUCCESS; } @@ -73,8 +64,7 @@ static int testsuite_rootfs_open(void) int fd, done; fd = open(MODULE_DIRECTORY "/a", O_RDONLY); - if (fd < 0) - return EXIT_FAILURE; + TS_ASSERT(fd >= 0); for (done = 0;;) { int r = read(fd, buf + done, sizeof(buf) - 1 - done); @@ -88,8 +78,7 @@ static int testsuite_rootfs_open(void) buf[done] = '\0'; - if (!streq(buf, "kmod-test-chroot-works\n")) - return EXIT_FAILURE; + TS_ASSERT(streq(buf, "kmod-test-chroot-works\n")); return EXIT_SUCCESS; } @@ -102,10 +91,7 @@ static int testsuite_rootfs_stat(void) { struct stat st; - if (stat(MODULE_DIRECTORY "/a", &st) < 0) { - ERR("stat failed: %m\n"); - return EXIT_FAILURE; - } + TS_ASSERT(stat(MODULE_DIRECTORY "/a", &st) == 0); return EXIT_SUCCESS; } @@ -119,10 +105,7 @@ static int testsuite_rootfs_opendir(void) DIR *d; d = opendir("/testdir"); - if (d == NULL) { - ERR("opendir failed: %m\n"); - return EXIT_FAILURE; - } + TS_ASSERT(d != NULL); closedir(d); return EXIT_SUCCESS; diff --git a/testsuite/test-util.c b/testsuite/test-util.c index 292fc04..7d68ba2 100644 --- a/testsuite/test-util.c +++ b/testsuite/test-util.c @@ -62,8 +62,7 @@ static int test_freadline_wrapped(void) { FILE *fp = fopen("/freadline_wrapped-input.txt", "re"); - if (!fp) - return EXIT_FAILURE; + TS_ASSERT(fp != NULL); while (!feof(fp) && !ferror(fp)) { unsigned int num = 0; diff --git a/testsuite/test-weakdep.c b/testsuite/test-weakdep.c index a98df8e..342af12 100644 --- a/testsuite/test-weakdep.c +++ b/testsuite/test-weakdep.c @@ -24,8 +24,7 @@ static int test_weakdep(void) int err; ctx = kmod_new(NULL, NULL); - if (ctx == NULL) - return EXIT_FAILURE; + TS_ASSERT(ctx != NULL); for (size_t i = 0; i < ARRAY_SIZE(mod_name); i++) { struct kmod_list *list = NULL; @@ -35,20 +34,12 @@ static int test_weakdep(void) printf("%s:", mod_name[i]); err = kmod_module_new_from_lookup(ctx, mod_name[i], &list); - if (list == NULL || err < 0) { - fprintf(stderr, "module %s not found in directory %s\n", - mod_name[i], ctx ? kmod_get_dirname(ctx) : "(missing)"); - return EXIT_FAILURE; - } + TS_ASSERT(list != NULL && err == 0); mod = kmod_module_get_module(list); err = kmod_module_get_weakdeps(mod, &mod_list); - if (err) { - fprintf(stderr, "weak dependencies can not be read for %s (%d)\n", - mod_name[i], err); - return EXIT_FAILURE; - } + TS_ASSERT(err == 0); kmod_list_foreach(itr, mod_list) { struct kmod_module *weakdep_mod = kmod_module_get_module(itr);