]> git.ipfire.org Git - thirdparty/kmod.git/blobdiff - testsuite/testsuite.h
ci: add clang-format action
[thirdparty/kmod.git] / testsuite / testsuite.h
index 015120645326408756048876873133dbf7d73ad9..59d29c132882283bb3daf47d8586f7bf621a4590 100644 (file)
@@ -1,25 +1,15 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
 /*
- * Copyright (C) 2012  ProFUSION embedded systems
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ * Copyright (C) 2012-2013  ProFUSION embedded systems
  */
 
-#ifndef _LIBKMOD_TESTSUITE_
-#define _LIBKMOD_TESTSUITE_
+#pragma once
 
 #include <stdbool.h>
 #include <stdarg.h>
+#include <stdio.h>
+
+#include <shared/macro.h>
 
 struct test;
 typedef int (*testfunc)(const struct test *t);
@@ -72,69 +62,109 @@ enum test_config {
 #define S_TC_INIT_MODULE_RETCODES "TESTSUITE_INIT_MODULE_RETCODES"
 #define S_TC_DELETE_MODULE_RETCODES "TESTSUITE_DELETE_MODULE_RETCODES"
 
+struct keyval {
+       const char *key;
+       const char *val;
+};
 
 struct test {
        const char *name;
        const char *description;
        struct {
-               const char *stdout;
-               const char *stderr;
+               /* File with correct stdout */
+               const char *out;
+               /* File with correct stderr */
+               const char *err;
+
+               /*
+                * whether to treat the correct files as regex to the real
+                * output
+                */
+               bool regex;
+
+               /*
+                * Vector with pair of files
+                * key = correct file
+                * val = file to check
+                */
+               const struct keyval *files;
        } output;
+       /* comma-separated list of loaded modules at the end of the test */
+       const char *modules_loaded;
+       const char *modules_not_loaded;
        testfunc func;
        const char *config[_TC_LAST];
+       const char *path;
+       const struct keyval *env_vars;
        bool need_spawn;
        bool expected_fail;
-};
-
-
-const struct test *test_find(const struct test *tests[], const char *name);
-int test_init(int argc, char *const argv[], const struct test *tests[]);
+       /* allow to skip tests that don't meet compile-time dependencies */
+       bool skip;
+       bool print_outputs;
+} __attribute__((aligned(8)));
+
+int test_init(const struct test *start, const struct test *stop, int argc,
+             char *const argv[]);
+const struct test *test_find(const struct test *start, const struct test *stop,
+                            const char *name);
 int test_spawn_prog(const char *prog, const char *const args[]);
-
 int test_run(const struct test *t);
 
-#define TS_EXPORT __attribute__ ((visibility("default")))
+#define TS_EXPORT __attribute__((visibility("default")))
 
-#define _LOG(prefix, fmt, ...) printf("TESTSUITE: " prefix fmt, ## __VA_ARGS__)
-#define LOG(fmt, ...) _LOG("", fmt, ## __VA_ARGS__)
-#define WARN(fmt, ...) _LOG("WARN: ", fmt, ## __VA_ARGS__)
-#define ERR(fmt, ...) _LOG("ERR: ", fmt, ## __VA_ARGS__)
+#define _LOG(prefix, fmt, ...) printf("TESTSUITE: " prefix fmt, ##__VA_ARGS__)
+#define LOG(fmt, ...) _LOG("", fmt, ##__VA_ARGS__)
+#define WARN(fmt, ...) _LOG("WARN: ", fmt, ##__VA_ARGS__)
+#define ERR(fmt, ...) _LOG("ERR: ", fmt, ##__VA_ARGS__)
+
+#define assert_return(expr, r)                                                  \
+       do {                                                                    \
+               if ((!(expr))) {                                                \
+                       ERR("Failed assertion: " #expr " %s:%d %s\n", __FILE__, \
+                           __LINE__, __PRETTY_FUNCTION__);                     \
+                       return (r);                                             \
+               }                                                               \
+       } while (false)
 
 /* Test definitions */
-#define DEFINE_TEST(_name, ...) \
-       const struct test s##_name = { \
-               .name = #_name, \
-               .func = _name, \
-               ## __VA_ARGS__ \
+#define DEFINE_TEST_WITH_FUNC(_name, _func, ...)                             \
+       static const struct test UNIQ(s##_name)                              \
+               __attribute__((used, section("kmod_tests"), aligned(8))) = { \
+                       .name = #_name, .func = _func, ##__VA_ARGS__         \
+               };
+
+#define DEFINE_TEST(_name, ...) DEFINE_TEST_WITH_FUNC(_name, _name, __VA_ARGS__)
+
+#define TESTSUITE_MAIN()                                                                 \
+       extern struct test __start_kmod_tests[]                                          \
+               __attribute__((weak, visibility("hidden")));                             \
+       extern struct test __stop_kmod_tests[]                                           \
+               __attribute__((weak, visibility("hidden")));                             \
+       int main(int argc, char *argv[])                                                 \
+       {                                                                                \
+               const struct test *t;                                                    \
+               int arg;                                                                 \
+                                                                                         \
+               arg = test_init(__start_kmod_tests, __stop_kmod_tests, argc, argv);      \
+               if (arg == 0)                                                            \
+                       return 0;                                                        \
+               if (arg < 0)                                                             \
+                       return EXIT_FAILURE;                                             \
+                                                                                         \
+               if (arg < argc) {                                                        \
+                       t = test_find(__start_kmod_tests, __stop_kmod_tests, argv[arg]); \
+                       if (t == NULL) {                                                 \
+                               fprintf(stderr, "could not find test %s\n", argv[arg]);  \
+                               exit(EXIT_FAILURE);                                      \
+                       }                                                                \
+                                                                                         \
+                       return test_run(t);                                              \
+               }                                                                        \
+                                                                                         \
+               for (t = __start_kmod_tests; t < __stop_kmod_tests; t++) {               \
+                       if (test_run(t) != 0)                                            \
+                               exit(EXIT_FAILURE);                                      \
+               }                                                                        \
+                                                                                         \
+               exit(EXIT_SUCCESS);                                                      \
        }
-
-#define TESTSUITE_MAIN(_tests) \
-       int main(int argc, char *argv[])                        \
-       {                                                       \
-               const struct test *t;                           \
-               int arg;                                        \
-               size_t i;                                       \
-                                                               \
-               arg = test_init(argc, argv, tests);             \
-               if (arg == 0)                                   \
-                       return 0;                               \
-                                                               \
-               if (arg < argc) {                               \
-                       t = test_find(tests, argv[arg]);        \
-                       if (t == NULL) {                        \
-                               fprintf(stderr, "could not find test %s\n", argv[arg]);\
-                               exit(EXIT_FAILURE);             \
-                       }                                       \
-                                                               \
-                       return test_run(t);                     \
-               }                                               \
-                                                               \
-               for (i = 0; tests[i] != NULL; i++) {            \
-                       if (test_run(tests[i]) != 0)            \
-                               exit(EXIT_FAILURE);             \
-               }                                               \
-                                                               \
-               exit(EXIT_SUCCESS);                             \
-       }                                                       \
-
-#endif