]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
testsuite: fold test-util into test-shared
authorEmil Velikov <emil.l.velikov@gmail.com>
Sat, 18 Jul 2026 14:06:14 +0000 (15:06 +0100)
committerLucas De Marchi <ldemarchi@kernel.org>
Tue, 28 Jul 2026 14:11:03 +0000 (09:11 -0500)
Arguably improves things by providing some grouping and clarity on the
origin, structure and coverage of the tests.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/450
Signed-off-by: Lucas De Marchi <ldemarchi@kernel.org>
testsuite/meson.build
testsuite/rootfs-pristine/test-shared/alias/correct.txt [moved from testsuite/rootfs-pristine/test-util/alias-correct.txt with 100% similarity]
testsuite/rootfs-pristine/test-shared/freadline-wrapped/correct.txt [moved from testsuite/rootfs-pristine/test-util/freadline_wrapped-correct.txt with 100% similarity]
testsuite/rootfs-pristine/test-shared/freadline-wrapped/input.txt [moved from testsuite/rootfs-pristine/test-util/freadline_wrapped-input.txt with 100% similarity]
testsuite/rootfs-pristine/test-shared/write-str-safe/correct.txt [moved from testsuite/rootfs-pristine/test-util2/write-str-safe-correct.txt with 100% similarity]
testsuite/test-shared.c
testsuite/test-util.c [deleted file]

index 731d6c24260e09179352b4ba9eccc6ad02641e66..ad3297360832b158bb719a879ac8c78ea96322ea 100644 (file)
@@ -91,7 +91,6 @@ _testsuite = [
   'test-remove',
   'test-shared',
   'test-testsuite',
-  'test-util',
   'test-weakdep',
 ]
 
index 1dc3ed035da5ea061a1cbc38efc7cca7cdc14a11..695cc2d23e58ca1acd0c2bf17964f4b13d43987a 100644 (file)
@@ -1,15 +1,22 @@
 // SPDX-License-Identifier: LGPL-2.1-or-later
 /*
  * Copyright (C)  2014 Intel Corporation. All rights reserved.
+ * Copyright (C) 2012-2013  ProFUSION embedded systems
+ * Copyright (C) 2012  Pedro Pedruzzi
  */
 
 #include <errno.h>
+#include <fcntl.h>
+#include <stdbool.h>
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
+#include <sys/stat.h>
+#include <sys/types.h>
+
 #include <shared/array.h>
 #include <shared/hash.h>
 #include <shared/strbuf.h>
@@ -650,4 +657,282 @@ static int xfail_strbuf_shrink_to(void)
 DEFINE_TEST(xfail_strbuf_shrink_to, .description = "xfail strbuf_shrink_to",
            .expected_fail = true);
 
+/* util sub-group */
+
+static int alias_1(void)
+{
+       static const char *const aliases[] = {
+               // clang-format off
+               "test1234",
+               "test[abcfoobar]2211",
+               "bar[aaa][bbbb]sss",
+               "kmod[p.b]lib",
+               "[az]1234[AZ]",
+               // clang-format on
+       };
+
+       char buf[PATH_MAX];
+       size_t len;
+
+       for (size_t i = 0; i < ARRAY_SIZE(aliases); i++) {
+               int ret;
+
+               ret = alias_normalize(aliases[i], buf, &len);
+               printf("input   %s\n", aliases[i]);
+               printf("return  %d\n", ret);
+
+               if (ret == 0) {
+                       printf("len     %zu\n", len);
+                       printf("output  %s\n", buf);
+               }
+
+               printf("\n");
+       }
+
+       return 0;
+}
+DEFINE_TEST(alias_1,
+       .description = "check if alias_normalize does the right thing",
+       .config = {
+               [TC_ROOTFS] = TESTSUITE_ROOTFS "test-shared/alias/",
+       },
+       .output = {
+               .out = TESTSUITE_ROOTFS "test-shared/alias/correct.txt",
+       });
+
+static int test_freadline_wrapped(void)
+{
+       FILE *fp = fopen("/input.txt", "re");
+
+       TS_ASSERT(fp != NULL);
+
+       while (!feof(fp) && !ferror(fp)) {
+               unsigned int num = 0;
+               char *s = freadline_wrapped(fp, &num);
+               if (!s)
+                       break;
+               puts(s);
+               free(s);
+               printf("%u\n", num);
+       }
+
+       fclose(fp);
+       return 0;
+}
+DEFINE_TEST(test_freadline_wrapped,
+       .description = "check if freadline_wrapped() does the right thing",
+       .config = {
+               [TC_ROOTFS] = TESTSUITE_ROOTFS "test-shared/freadline-wrapped/",
+       },
+       .output = {
+               .out = TESTSUITE_ROOTFS "test-shared/freadline-wrapped/correct.txt",
+       });
+
+static int test_strchr_replace(void)
+{
+       _cleanup_free_ char *s = strdup("this is a test string");
+       const char *res = "thiC iC a teCt Ctring";
+
+       strchr_replace(s, 's', 'C');
+       TS_ASSERT(streq(s, res));
+
+       return 0;
+}
+DEFINE_TEST(test_strchr_replace,
+           .description = "check implementation of strchr_replace()");
+
+static int test_underscores(void)
+{
+       static const struct teststr {
+               const char *val;
+               const char *res;
+       } teststr[] = {
+               // clang-format off
+               { "aa-bb-cc_", "aa_bb_cc_" },
+               { "-aa-bb-cc-", "_aa_bb_cc_" },
+               { "-aa[-bb-]cc-", "_aa[-bb-]cc_" },
+               { "-aa-[bb]-cc-", "_aa_[bb]_cc_" },
+               { "-aa-[b-b]-cc-", "_aa_[b-b]_cc_" },
+               { "-aa-b[-]b-cc", "_aa_b[-]b_cc" },
+               // clang-format on
+       };
+
+       for (size_t i = 0; i < ARRAY_SIZE(teststr); i++) {
+               _cleanup_free_ char *val = strdup(teststr[i].val);
+               TS_ASSERT(val != NULL);
+               TS_ASSERT(!underscores(val));
+               TS_ASSERT(streq(val, teststr[i].res));
+       }
+
+       return 0;
+}
+DEFINE_TEST(test_underscores, .description = "check implementation of underscores()");
+
+static int test_path_ends_with_kmod_ext(void)
+{
+       static const struct teststr {
+               const char *val;
+               bool res;
+       } teststr[] = {
+               // clang-format off
+               { "/bla.ko", true },
+#if ENABLE_ZLIB
+               { "/bla.ko.gz", true },
+#endif
+#if ENABLE_XZ
+               { "/bla.ko.xz", true },
+#endif
+#if ENABLE_ZSTD
+               { "/bla.ko.zst", true },
+#endif
+               { "/bla.ko.x", false },
+               { "/bla.ko.", false },
+               { "/bla.koz", false },
+               { "/b", false },
+               // clang-format on
+       };
+
+       for (size_t i = 0; i < ARRAY_SIZE(teststr); i++) {
+               TS_ASSERT(
+                       path_ends_with_kmod_ext(teststr[i].val, strlen(teststr[i].val)) ==
+                       teststr[i].res);
+       }
+
+       return 0;
+}
+DEFINE_TEST(test_path_ends_with_kmod_ext,
+           .description = "check implementation of path_ends_with_kmod_ext()");
+
+#define TEST_WRITE_STR_SAFE_PATH TESTSUITE_ROOTFS "test-shared/write-str-safe/"
+static int test_write_str_safe(void)
+{
+       const char *s = "test";
+       int fd;
+
+       fd = open("/output.txt", O_CREAT | O_TRUNC | O_WRONLY, 0644);
+       TS_ASSERT(fd >= 0);
+
+       write_str_safe(fd, s, strlen(s));
+       close(fd);
+
+       return 0;
+}
+DEFINE_TEST(test_write_str_safe,
+       .description = "check implementation of write_str_safe()",
+       .config = {
+               [TC_ROOTFS] = TEST_WRITE_STR_SAFE_PATH,
+       },
+       .output = {
+               .files = (const struct keyval[]) {
+                       { TEST_WRITE_STR_SAFE_PATH "output.txt",
+                         TEST_WRITE_STR_SAFE_PATH "correct.txt" },
+                       { },
+               },
+       });
+
+static int test_uadd32_overflow(void)
+{
+       uint32_t res;
+       bool overflow;
+
+       overflow = uadd32_overflow(UINT32_MAX - 1, 1, &res);
+       TS_ASSERT(!overflow);
+       TS_ASSERT(res == UINT32_MAX);
+
+       overflow = uadd32_overflow(UINT32_MAX, 1, &res);
+       TS_ASSERT(overflow);
+
+       return 0;
+}
+DEFINE_TEST(test_uadd32_overflow,
+           .description = "check implementation of uadd32_overflow()");
+
+static int test_uadd64_overflow(void)
+{
+       uint64_t res;
+       bool overflow;
+
+       overflow = uadd64_overflow(UINT64_MAX - 1, 1, &res);
+       TS_ASSERT(!overflow);
+       TS_ASSERT(res == UINT64_MAX);
+
+       overflow = uadd64_overflow(UINT64_MAX, 1, &res);
+       TS_ASSERT(overflow);
+
+       return 0;
+}
+DEFINE_TEST(test_uadd64_overflow,
+           .description = "check implementation of uadd64_overflow()");
+
+static int test_umul32_overflow(void)
+{
+       uint32_t res;
+       bool overflow;
+
+       overflow = umul32_overflow(UINT32_MAX / 0x10, 0x10, &res);
+       TS_ASSERT(!overflow);
+       TS_ASSERT(res == (UINT32_MAX & ~0xf));
+
+       overflow = umul32_overflow(UINT32_MAX, 0x10, &res);
+       TS_ASSERT(overflow);
+
+       return 0;
+}
+DEFINE_TEST(test_umul32_overflow,
+           .description = "check implementation of umul32_overflow()");
+
+static int test_umul64_overflow(void)
+{
+       uint64_t res;
+       bool overflow;
+
+       overflow = umul64_overflow(UINT64_MAX / 0x10, 0x10, &res);
+       TS_ASSERT(!overflow);
+       TS_ASSERT(res == (UINT64_MAX & ~0xf));
+
+       overflow = umul64_overflow(UINT64_MAX, 0x10, &res);
+       TS_ASSERT(overflow);
+
+       return 0;
+}
+DEFINE_TEST(test_umul64_overflow,
+           .description = "check implementation of umul64_overflow()");
+
+static int test_backoff_time(void)
+{
+       unsigned long long delta = 0;
+
+       /* Check exponential increments */
+       get_backoff_delta_msec(now_msec() + 10, &delta);
+       TS_ASSERT(delta == 1);
+       get_backoff_delta_msec(now_msec() + 10, &delta);
+       TS_ASSERT(delta == 2);
+       get_backoff_delta_msec(now_msec() + 10, &delta);
+       TS_ASSERT(delta == 4);
+       get_backoff_delta_msec(now_msec() + 10, &delta);
+       TS_ASSERT(delta == 8);
+       get_backoff_delta_msec(now_msec() + 10, &delta);
+       TS_ASSERT(delta == 8);
+
+       {
+               /* Check tail */
+               delta = 4;
+               get_backoff_delta_msec(now_msec() + 3, &delta);
+               TS_ASSERT(delta == 2);
+               get_backoff_delta_msec(now_msec() + 1, &delta);
+               TS_ASSERT(delta == 1);
+               get_backoff_delta_msec(now_msec(), &delta);
+               TS_ASSERT(delta == 0);
+       }
+
+       /* Check when end time has passed */
+       delta = 0;
+       get_backoff_delta_msec(now_msec() - 10, &delta);
+       TS_ASSERT(delta == 0);
+
+       return 0;
+}
+DEFINE_TEST(test_backoff_time,
+           .description = "check implementation of get_backoff_delta_msec()");
+
 TESTSUITE_MAIN();
diff --git a/testsuite/test-util.c b/testsuite/test-util.c
deleted file mode 100644 (file)
index 1749d2f..0000000
+++ /dev/null
@@ -1,297 +0,0 @@
-// SPDX-License-Identifier: LGPL-2.1-or-later
-/*
- * Copyright (C) 2012-2013  ProFUSION embedded systems
- * Copyright (C) 2012  Pedro Pedruzzi
- */
-
-#include <fcntl.h>
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-
-#include <shared/util.h>
-
-#include "testsuite.h"
-
-static int alias_1(void)
-{
-       static const char *const aliases[] = {
-               // clang-format off
-               "test1234",
-               "test[abcfoobar]2211",
-               "bar[aaa][bbbb]sss",
-               "kmod[p.b]lib",
-               "[az]1234[AZ]",
-               // clang-format on
-       };
-
-       char buf[PATH_MAX];
-       size_t len;
-
-       for (size_t i = 0; i < ARRAY_SIZE(aliases); i++) {
-               int ret;
-
-               ret = alias_normalize(aliases[i], buf, &len);
-               printf("input   %s\n", aliases[i]);
-               printf("return  %d\n", ret);
-
-               if (ret == 0) {
-                       printf("len     %zu\n", len);
-                       printf("output  %s\n", buf);
-               }
-
-               printf("\n");
-       }
-
-       return 0;
-}
-DEFINE_TEST(alias_1,
-       .description = "check if alias_normalize does the right thing",
-       .config = {
-               [TC_ROOTFS] = TESTSUITE_ROOTFS "test-util/",
-       },
-       .output = {
-               .out = TESTSUITE_ROOTFS "test-util/alias-correct.txt",
-       });
-
-static int test_freadline_wrapped(void)
-{
-       FILE *fp = fopen("/freadline_wrapped-input.txt", "re");
-
-       TS_ASSERT(fp != NULL);
-
-       while (!feof(fp) && !ferror(fp)) {
-               unsigned int num = 0;
-               char *s = freadline_wrapped(fp, &num);
-               if (!s)
-                       break;
-               puts(s);
-               free(s);
-               printf("%u\n", num);
-       }
-
-       fclose(fp);
-       return 0;
-}
-DEFINE_TEST(test_freadline_wrapped,
-       .description = "check if freadline_wrapped() does the right thing",
-       .config = {
-               [TC_ROOTFS] = TESTSUITE_ROOTFS "test-util/",
-       },
-       .output = {
-               .out = TESTSUITE_ROOTFS "test-util/freadline_wrapped-correct.txt",
-       });
-
-static int test_strchr_replace(void)
-{
-       _cleanup_free_ char *s = strdup("this is a test string");
-       const char *res = "thiC iC a teCt Ctring";
-
-       strchr_replace(s, 's', 'C');
-       TS_ASSERT(streq(s, res));
-
-       return 0;
-}
-DEFINE_TEST(test_strchr_replace,
-           .description = "check implementation of strchr_replace()");
-
-static int test_underscores(void)
-{
-       static const struct teststr {
-               const char *val;
-               const char *res;
-       } teststr[] = {
-               // clang-format off
-               { "aa-bb-cc_", "aa_bb_cc_" },
-               { "-aa-bb-cc-", "_aa_bb_cc_" },
-               { "-aa[-bb-]cc-", "_aa[-bb-]cc_" },
-               { "-aa-[bb]-cc-", "_aa_[bb]_cc_" },
-               { "-aa-[b-b]-cc-", "_aa_[b-b]_cc_" },
-               { "-aa-b[-]b-cc", "_aa_b[-]b_cc" },
-               // clang-format on
-       };
-
-       for (size_t i = 0; i < ARRAY_SIZE(teststr); i++) {
-               _cleanup_free_ char *val = strdup(teststr[i].val);
-               TS_ASSERT(val != NULL);
-               TS_ASSERT(!underscores(val));
-               TS_ASSERT(streq(val, teststr[i].res));
-       }
-
-       return 0;
-}
-DEFINE_TEST(test_underscores, .description = "check implementation of underscores()");
-
-static int test_path_ends_with_kmod_ext(void)
-{
-       static const struct teststr {
-               const char *val;
-               bool res;
-       } teststr[] = {
-               // clang-format off
-               { "/bla.ko", true },
-#if ENABLE_ZLIB
-               { "/bla.ko.gz", true },
-#endif
-#if ENABLE_XZ
-               { "/bla.ko.xz", true },
-#endif
-#if ENABLE_ZSTD
-               { "/bla.ko.zst", true },
-#endif
-               { "/bla.ko.x", false },
-               { "/bla.ko.", false },
-               { "/bla.koz", false },
-               { "/b", false },
-               // clang-format on
-       };
-
-       for (size_t i = 0; i < ARRAY_SIZE(teststr); i++) {
-               TS_ASSERT(
-                       path_ends_with_kmod_ext(teststr[i].val, strlen(teststr[i].val)) ==
-                       teststr[i].res);
-       }
-
-       return 0;
-}
-DEFINE_TEST(test_path_ends_with_kmod_ext,
-           .description = "check implementation of path_ends_with_kmod_ext()");
-
-#define TEST_WRITE_STR_SAFE_FILE "/write-str-safe"
-#define TEST_WRITE_STR_SAFE_PATH TESTSUITE_ROOTFS "test-util2/" TEST_WRITE_STR_SAFE_FILE
-static int test_write_str_safe(void)
-{
-       const char *s = "test";
-       int fd;
-
-       fd = open(TEST_WRITE_STR_SAFE_FILE ".txt", O_CREAT | O_TRUNC | O_WRONLY, 0644);
-       TS_ASSERT(fd >= 0);
-
-       write_str_safe(fd, s, strlen(s));
-       close(fd);
-
-       return 0;
-}
-DEFINE_TEST(test_write_str_safe,
-       .description = "check implementation of write_str_safe()",
-       .config = {
-               [TC_ROOTFS] = TESTSUITE_ROOTFS "test-util2/",
-       },
-       .output = {
-               .files = (const struct keyval[]) {
-                       { TEST_WRITE_STR_SAFE_PATH ".txt",
-                         TEST_WRITE_STR_SAFE_PATH "-correct.txt" },
-                       { },
-               },
-       });
-
-static int test_uadd32_overflow(void)
-{
-       uint32_t res;
-       bool overflow;
-
-       overflow = uadd32_overflow(UINT32_MAX - 1, 1, &res);
-       TS_ASSERT(!overflow);
-       TS_ASSERT(res == UINT32_MAX);
-
-       overflow = uadd32_overflow(UINT32_MAX, 1, &res);
-       TS_ASSERT(overflow);
-
-       return 0;
-}
-DEFINE_TEST(test_uadd32_overflow,
-           .description = "check implementation of uadd32_overflow()");
-
-static int test_uadd64_overflow(void)
-{
-       uint64_t res;
-       bool overflow;
-
-       overflow = uadd64_overflow(UINT64_MAX - 1, 1, &res);
-       TS_ASSERT(!overflow);
-       TS_ASSERT(res == UINT64_MAX);
-
-       overflow = uadd64_overflow(UINT64_MAX, 1, &res);
-       TS_ASSERT(overflow);
-
-       return 0;
-}
-DEFINE_TEST(test_uadd64_overflow,
-           .description = "check implementation of uadd64_overflow()");
-
-static int test_umul32_overflow(void)
-{
-       uint32_t res;
-       bool overflow;
-
-       overflow = umul32_overflow(UINT32_MAX / 0x10, 0x10, &res);
-       TS_ASSERT(!overflow);
-       TS_ASSERT(res == (UINT32_MAX & ~0xf));
-
-       overflow = umul32_overflow(UINT32_MAX, 0x10, &res);
-       TS_ASSERT(overflow);
-
-       return 0;
-}
-DEFINE_TEST(test_umul32_overflow,
-           .description = "check implementation of umul32_overflow()");
-
-static int test_umul64_overflow(void)
-{
-       uint64_t res;
-       bool overflow;
-
-       overflow = umul64_overflow(UINT64_MAX / 0x10, 0x10, &res);
-       TS_ASSERT(!overflow);
-       TS_ASSERT(res == (UINT64_MAX & ~0xf));
-
-       overflow = umul64_overflow(UINT64_MAX, 0x10, &res);
-       TS_ASSERT(overflow);
-
-       return 0;
-}
-DEFINE_TEST(test_umul64_overflow,
-           .description = "check implementation of umul64_overflow()");
-
-static int test_backoff_time(void)
-{
-       unsigned long long delta = 0;
-
-       /* Check exponential increments */
-       get_backoff_delta_msec(now_msec() + 10, &delta);
-       TS_ASSERT(delta == 1);
-       get_backoff_delta_msec(now_msec() + 10, &delta);
-       TS_ASSERT(delta == 2);
-       get_backoff_delta_msec(now_msec() + 10, &delta);
-       TS_ASSERT(delta == 4);
-       get_backoff_delta_msec(now_msec() + 10, &delta);
-       TS_ASSERT(delta == 8);
-       get_backoff_delta_msec(now_msec() + 10, &delta);
-       TS_ASSERT(delta == 8);
-
-       {
-               /* Check tail */
-               delta = 4;
-               get_backoff_delta_msec(now_msec() + 3, &delta);
-               TS_ASSERT(delta == 2);
-               get_backoff_delta_msec(now_msec() + 1, &delta);
-               TS_ASSERT(delta == 1);
-               get_backoff_delta_msec(now_msec(), &delta);
-               TS_ASSERT(delta == 0);
-       }
-
-       /* Check when end time has passed */
-       delta = 0;
-       get_backoff_delta_msec(now_msec() - 10, &delta);
-       TS_ASSERT(delta == 0);
-
-       return 0;
-}
-DEFINE_TEST(test_backoff_time,
-           .description = "check implementation of get_backoff_delta_msec()");
-
-TESTSUITE_MAIN();