]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
testsuite: convert all tests to TS_ASSERT()
authorEmil Velikov <emil.l.velikov@gmail.com>
Fri, 3 Jul 2026 11:45:56 +0000 (12:45 +0100)
committerLucas De Marchi <ldemarchi@kernel.org>
Thu, 16 Jul 2026 02:19:53 +0000 (23:19 -0300)
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 <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/375
Signed-off-by: Lucas De Marchi <ldemarchi@kernel.org>
12 files changed:
testsuite/test-blacklist.c
testsuite/test-dependencies.c
testsuite/test-init.c
testsuite/test-initstate.c
testsuite/test-loaded.c
testsuite/test-modprobe.c
testsuite/test-multi-softdep.c
testsuite/test-new-module.c
testsuite/test-remove.c
testsuite/test-testsuite.c
testsuite/test-util.c
testsuite/test-weakdep.c

index 83e4e266a1967f9da7740d2bb475edd12938e2aa..6ba996c2a49ac20b7744cffc7a21396c56a851f7 100644 (file)
@@ -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",
index 24b241824f2bb0e4efd44347311d25d724c91f17..a88f3a582417e4720fe6d6052b931e8578da97c2 100644 (file)
@@ -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);
index fb4cdce41a2782c9a94b54b501a167297e4c7a8d..3c0ff8513bf3fc1c5994acab4986ad0b148f4016 100644 (file)
@@ -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);
index 0d46ac022bfdf823a001036c2cebb979840748ea..4d3430397a834043397a0529bdf709ca4662affc 100644 (file)
@@ -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);
index fdfb280e4e8df2be8b326272ec31218be3bafce6..bf8efd7bbc797786b9b272ce560aa2624db91780 100644 (file)
@@ -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");
 
index d48686fc6b82d6edb7a3a8784ebe4ea7b0d35686..c15f54424ee578e0d45144a414455ca852bc7f25 100644 (file)
@@ -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");
 }
index 74226a4adefb575b9af5f7a45a3fd6c3a1c54486..0d51f924cb772e4cafb61e0354766fadb112536d 100644 (file)
@@ -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;
 }
 
 /*
index c4968b2f8ebadd1602f6e338651c9b2bc9000d3a..30683a607384fb1d0c5813ad74d68484159ed3bf 100644 (file)
@@ -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;
index f4f6975697e7777a8255134abb844dcdca50bf66..b47e5e8f2673caaff9a735f2545bf1335863e0a6 100644 (file)
@@ -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);
 
index 43c946f9d827d35e093fe67ea930e7e5e7c57ca0..9570c0d7ed49214440b140e87437bfc248885bc1 100644 (file)
@@ -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;
index 292fc04300477a27c336e38c8f2065433f506ba4..7d68ba2c87a47a792c106e4e41c70eb2d0a5c42b 100644 (file)
@@ -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;
index a98df8e0154067171850754cdc873c9a38a5da87..342af1272d6fb078a3fe5fc9270cac05209e2e1d 100644 (file)
@@ -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);