]> git.ipfire.org Git - thirdparty/kmod.git/blobdiff - testsuite/testsuite.h
testsuite: improve coverage of shared/util.h
[thirdparty/kmod.git] / testsuite / testsuite.h
index 97183cdedea81e0f37771caff0770ac364dbabc1..9db1d1ab554d2acc8c29edaa70095b547660d3be 100644 (file)
@@ -12,8 +12,7 @@
  * 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/>.
  */
 
 #pragma once
@@ -21,7 +20,7 @@
 #include <stdbool.h>
 #include <stdarg.h>
 
-#include "macro.h"
+#include <shared/macro.h>
 
 struct test;
 typedef int (*testfunc)(const struct test *t);
@@ -95,19 +94,22 @@ struct test {
                 */
                const struct keyval *files;
        } output;
+       /* comma-separated list of loaded modules at the end of the test */
+       const char *modules_loaded;
        testfunc func;
        const char *config[_TC_LAST];
        const char *path;
        const struct keyval *env_vars;
        bool need_spawn;
        bool expected_fail;
-};
+} __attribute__((aligned(8)));
 
 
-const struct test *test_find(const struct test *tests[], const char *name);
-int test_init(int argc, char *const argv[], const struct test *tests[]);
+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")))
@@ -117,42 +119,54 @@ int test_run(const struct test *t);
 #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 = { \
+       static const struct test s##_name##UNIQ \
+       __attribute__((used, section("kmod_tests"), aligned(8))) = { \
                .name = #_name, \
                .func = _name, \
                ## __VA_ARGS__ \
-       }
-
-#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);                             \
-       }                                                       \
+       };
+
+#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 < 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);                                                             \
+       }                                                                                       \
 
 #ifdef noreturn
 # define __noreturn noreturn