]> git.ipfire.org Git - thirdparty/kmod.git/blobdiff - testsuite/test-util.c
add Zstandard compression support
[thirdparty/kmod.git] / testsuite / test-util.c
index 17f242234ab2dea52e5de2e3e3dc2c78e861fccd..621446b10503908371c5b820fe0f6dc8a4a5e1f0 100644 (file)
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 
+#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 <libkmod.h>
-#include <libkmod-util.h>
-
 #include "testsuite.h"
 
 static int alias_1(const struct test *t)
@@ -60,7 +61,7 @@ static int alias_1(const struct test *t)
 
        return EXIT_SUCCESS;
 }
-static DEFINE_TEST(alias_1,
+DEFINE_TEST(alias_1,
        .description = "check if alias_normalize does the right thing",
        .config = {
                [TC_ROOTFS] = TESTSUITE_ROOTFS "test-util/",
@@ -70,16 +71,16 @@ static DEFINE_TEST(alias_1,
                .out = TESTSUITE_ROOTFS "test-util/alias-correct.txt",
        });
 
-static int test_getline_wrapped(const struct test *t)
+static int test_freadline_wrapped(const struct test *t)
 {
-       FILE *fp = fopen("/getline_wrapped-input.txt", "re");
+       FILE *fp = fopen("/freadline_wrapped-input.txt", "re");
 
        if (!fp)
                return EXIT_FAILURE;
 
        while (!feof(fp) && !ferror(fp)) {
                unsigned int num = 0;
-               char *s = getline_wrapped(fp, &num);
+               char *s = freadline_wrapped(fp, &num);
                if (!s)
                        break;
                puts(s);
@@ -90,20 +91,142 @@ static int test_getline_wrapped(const struct test *t)
        fclose(fp);
        return EXIT_SUCCESS;
 }
-static DEFINE_TEST(test_getline_wrapped,
-       .description = "check if getline_wrapped() does the right thing",
+DEFINE_TEST(test_freadline_wrapped,
+       .description = "check if freadline_wrapped() does the right thing",
        .config = {
                [TC_ROOTFS] = TESTSUITE_ROOTFS "test-util/",
        },
        .need_spawn = true,
        .output = {
-               .out = TESTSUITE_ROOTFS "test-util/getline_wrapped-correct.txt",
+               .out = TESTSUITE_ROOTFS "test-util/freadline_wrapped-correct.txt",
        });
 
-static const struct test *tests[] = {
-       &salias_1,
-       &stest_getline_wrapped,
-       NULL,
-};
+static int test_strchr_replace(const struct test *t)
+{
+       _cleanup_free_ char *s = strdup("this is a test string");
+       const char *res = "thiC iC a teCt Ctring";
+
+       strchr_replace(s, 's', 'C');
+       assert_return(streq(s, res), EXIT_FAILURE);
+
+       return EXIT_SUCCESS;
+}
+DEFINE_TEST(test_strchr_replace,
+       .description = "check implementation of strchr_replace()",
+       .need_spawn = false,
+       );
+
+static int test_underscores(const struct test *t)
+{
+       struct teststr {
+               char *val;
+               const char *res;
+       } teststr[] = {
+               { strdup("aa-bb-cc_"), "aa_bb_cc_" },
+               { strdup("-aa-bb-cc-"), "_aa_bb_cc_" },
+               { strdup("-aa[-bb-]cc-"), "_aa[-bb-]cc_" },
+               { strdup("-aa-[bb]-cc-"), "_aa_[bb]_cc_" },
+               { strdup("-aa-[b-b]-cc-"), "_aa_[b-b]_cc_" },
+               { strdup("-aa-b[-]b-cc"), "_aa_b[-]b_cc" },
+               { }
+       }, *iter;
+
+       for (iter = &teststr[0]; iter->val != NULL; iter++) {
+               _cleanup_free_ char *val = iter->val;
+               underscores(val);
+               assert_return(streq(val, iter->res), EXIT_FAILURE);
+       }
+
+       return EXIT_SUCCESS;
+}
+DEFINE_TEST(test_underscores,
+       .description = "check implementation of underscores()",
+       .need_spawn = false,
+       );
+
+static int test_path_ends_with_kmod_ext(const struct test *t)
+{
+       struct teststr {
+               const char *val;
+               bool res;
+       } teststr[] = {
+               { "/bla.ko", true },
+#ifdef ENABLE_ZLIB
+               { "/bla.ko.gz", true },
+#endif
+#ifdef ENABLE_XZ
+               { "/bla.ko.xz", true },
+#endif
+#ifdef ENABLE_ZSTD
+               { "/bla.ko.zst", true },
+#endif
+               { "/bla.ko.x", false },
+               { "/bla.ko.", false },
+               { "/bla.koz", false },
+               { "/b", false },
+               { }
+       }, *iter;
+
+       for (iter = &teststr[0]; iter->val != NULL; iter++) {
+               assert_return(path_ends_with_kmod_ext(iter->val,
+                                                     strlen(iter->val)) == iter->res,
+                             EXIT_FAILURE);
+       }
+
+       return EXIT_SUCCESS;
+}
+DEFINE_TEST(test_path_ends_with_kmod_ext,
+       .description = "check implementation of path_ends_with_kmod_ext()",
+       .need_spawn = false,
+       );
+
+#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(const struct test *t)
+{
+       const char *s = "test";
+       int fd;
+
+       fd = open(TEST_WRITE_STR_SAFE_FILE ".txt", O_CREAT|O_TRUNC|O_WRONLY, 0644);
+       assert_return(fd >= 0, EXIT_FAILURE);
+
+       write_str_safe(fd, s, strlen(s));
+       close(fd);
+
+       return EXIT_SUCCESS;
+}
+DEFINE_TEST(test_write_str_safe,
+       .description = "check implementation of write_str_safe()",
+       .config = {
+               [TC_ROOTFS] = TESTSUITE_ROOTFS "test-util2/",
+       },
+       .need_spawn = true,
+       .output = {
+               .files = (const struct keyval[]) {
+                       { TEST_WRITE_STR_SAFE_PATH ".txt",
+                         TEST_WRITE_STR_SAFE_PATH "-correct.txt" },
+                       { }
+               },
+       });
+
+static int test_addu64_overflow(const struct test *t)
+{
+       uint64_t res;
+       bool overflow;
+
+       overflow = addu64_overflow(UINT64_MAX - 1, 1, &res);
+       assert_return(!overflow, EXIT_FAILURE);
+       assert_return(res == UINT64_MAX, EXIT_FAILURE);
+
+       overflow = addu64_overflow(UINT64_MAX, 1, &res);
+       assert_return(overflow, EXIT_FAILURE);
+
+       return EXIT_SUCCESS;
+}
+DEFINE_TEST(test_addu64_overflow,
+       .description = "check implementation of addu4_overflow()",
+       .need_spawn = false,
+       );
+
 
-TESTSUITE_MAIN(tests);
+TESTSUITE_MAIN();