diff options
Diffstat (limited to 'tests/runner.c')
| -rw-r--r-- | tests/runner.c | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/tests/runner.c b/tests/runner.c new file mode 100644 index 0000000..2398991 --- /dev/null +++ b/tests/runner.c | |||
| @@ -0,0 +1,115 @@ | |||
| 1 | /* This software was written by Dirk Engling <erdgeist@erdgeist.org> | ||
| 2 | It is considered beerware. Prost. Skol. Cheers or whatever. */ | ||
| 3 | |||
| 4 | /* System */ | ||
| 5 | #include <stdio.h> | ||
| 6 | #include <stdlib.h> | ||
| 7 | #include <string.h> | ||
| 8 | #include <sys/wait.h> | ||
| 9 | #include <unistd.h> | ||
| 10 | |||
| 11 | /* Opentracker */ | ||
| 12 | #include "test.h" | ||
| 13 | |||
| 14 | struct module { | ||
| 15 | const char *name; | ||
| 16 | void (*run)(void); | ||
| 17 | }; | ||
| 18 | |||
| 19 | static const struct module modules[] = {{"ot_urlencoded", test_urlencoded}, | ||
| 20 | {"ot_iovec", test_iovec}, | ||
| 21 | {"ot_vector", test_vector}, | ||
| 22 | {"ot_random", test_random}, | ||
| 23 | {"ot_hex", test_hex}, | ||
| 24 | {"ot_scan", test_scan}, | ||
| 25 | {"ot_buf", test_buf}, | ||
| 26 | {"ot_ip", test_ip}, | ||
| 27 | {"ot_socket", test_socket}, | ||
| 28 | {"ot_mutex", test_mutex}, | ||
| 29 | {"trackerlogic", test_trackerlogic}, | ||
| 30 | {"clean", test_clean}, | ||
| 31 | {"ot_workqueue", test_workqueue}, | ||
| 32 | {"ot_batch", test_batch}, | ||
| 33 | {"ot_fdstate", test_fdstate}, | ||
| 34 | {"ot_event", test_event}, | ||
| 35 | {"ot_fmt", test_fmt}, | ||
| 36 | {"ot_accesslist", test_accesslist}, | ||
| 37 | {NULL, NULL}}; | ||
| 38 | |||
| 39 | struct tally { | ||
| 40 | unsigned long checks; | ||
| 41 | unsigned long failures; | ||
| 42 | }; | ||
| 43 | |||
| 44 | /* Each module runs in a child of its own, so that a crash or a sanitizer | ||
| 45 | abort costs that module's results rather than every module after it. The | ||
| 46 | child reports its tally through a pipe; one that dies without reporting is | ||
| 47 | counted as a failure under its own name. */ | ||
| 48 | static void run_forked(const struct module *m) { | ||
| 49 | struct tally tally = {0, 0}; | ||
| 50 | int pipefd[2], status = 0; | ||
| 51 | pid_t child; | ||
| 52 | ssize_t got; | ||
| 53 | |||
| 54 | if (pipe(pipefd)) { | ||
| 55 | test_failf(__FILE__, __LINE__, "pipe failed, cannot isolate %s", m->name); | ||
| 56 | return; | ||
| 57 | } | ||
| 58 | |||
| 59 | fflush(stdout); /* or the child inherits a copy and prints it again */ | ||
| 60 | child = fork(); | ||
| 61 | if (child < 0) { | ||
| 62 | close(pipefd[0]); | ||
| 63 | close(pipefd[1]); | ||
| 64 | test_failf(__FILE__, __LINE__, "fork failed, cannot isolate %s", m->name); | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | |||
| 68 | if (!child) { | ||
| 69 | close(pipefd[0]); | ||
| 70 | test_checks = test_failures = 0; | ||
| 71 | m->run(); | ||
| 72 | tally.checks = test_checks; | ||
| 73 | tally.failures = test_failures; | ||
| 74 | if (write(pipefd[1], &tally, sizeof(tally)) != (ssize_t)sizeof(tally)) | ||
| 75 | _exit(1); | ||
| 76 | fflush(stdout); | ||
| 77 | _exit(0); | ||
| 78 | } | ||
| 79 | |||
| 80 | close(pipefd[1]); | ||
| 81 | got = read(pipefd[0], &tally, sizeof(tally)); | ||
| 82 | close(pipefd[0]); | ||
| 83 | while (waitpid(child, &status, 0) < 0) | ||
| 84 | ; | ||
| 85 | |||
| 86 | if (got == (ssize_t)sizeof(tally)) { | ||
| 87 | test_checks += tally.checks; | ||
| 88 | test_failures += tally.failures; | ||
| 89 | } | ||
| 90 | if (got != (ssize_t)sizeof(tally) || !WIFEXITED(status) || WEXITSTATUS(status)) | ||
| 91 | test_failf(__FILE__, __LINE__, "%s did not run to the end", m->name); | ||
| 92 | } | ||
| 93 | |||
| 94 | /* Run every module, or only those whose name contains argv[1]. | ||
| 95 | OT_TEST_FORK=0 keeps everything in one process, which is what a debugger | ||
| 96 | wants. */ | ||
| 97 | int main(int argc, char **argv) { | ||
| 98 | const char *fork_setting = getenv("OT_TEST_FORK"); | ||
| 99 | int isolate = !fork_setting || strcmp(fork_setting, "0"); | ||
| 100 | const struct module *m; | ||
| 101 | |||
| 102 | for (m = modules; m->name; ++m) { | ||
| 103 | if (argc > 1 && !strstr(m->name, argv[1])) | ||
| 104 | continue; | ||
| 105 | test_module(m->name); | ||
| 106 | if (!isolate) { | ||
| 107 | m->run(); | ||
| 108 | continue; | ||
| 109 | } | ||
| 110 | test_case("the module runs to the end"); | ||
| 111 | run_forked(m); | ||
| 112 | } | ||
| 113 | |||
| 114 | return test_summary(); | ||
| 115 | } | ||
