U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Intel Corporation |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and its |
| 5 | * documentation for any purpose is hereby granted without fee, provided that |
| 6 | * the above copyright notice appear in all copies and that both that copyright |
| 7 | * notice and this permission notice appear in supporting documentation, and |
| 8 | * that the name of the copyright holders not be used in advertising or |
| 9 | * publicity pertaining to distribution of the software without specific, |
| 10 | * written prior permission. The copyright holders make no representations |
| 11 | * about the suitability of this software for any purpose. It is provided "as |
| 12 | * is" without express or implied warranty. |
| 13 | * |
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 15 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 16 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 17 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 18 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 19 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 20 | * OF THIS SOFTWARE. |
| 21 | */ |
| 22 | |
Peter Hutterer | d8ca890 | 2013-09-11 16:08:04 +1000 | [diff] [blame] | 23 | #include "config.h" |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/wait.h> |
| 29 | #include <string.h> |
| 30 | #include <assert.h> |
| 31 | #include <errno.h> |
Pekka Paalanen | 585c27c | 2012-12-18 17:30:16 +0200 | [diff] [blame] | 32 | #include <signal.h> |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 33 | #include "weston-test-runner.h" |
| 34 | |
| 35 | extern const struct weston_test __start_test_section, __stop_test_section; |
| 36 | |
| 37 | static const struct weston_test * |
| 38 | find_test(const char *name) |
| 39 | { |
| 40 | const struct weston_test *t; |
| 41 | |
| 42 | for (t = &__start_test_section; t < &__stop_test_section; t++) |
| 43 | if (strcmp(t->name, name) == 0) |
| 44 | return t; |
| 45 | |
| 46 | return NULL; |
| 47 | } |
| 48 | |
| 49 | static void |
| 50 | run_test(const struct weston_test *t) |
| 51 | { |
| 52 | t->run(); |
| 53 | exit(EXIT_SUCCESS); |
| 54 | } |
| 55 | |
Peter Hutterer | 534f0a4 | 2013-09-11 16:08:30 +1000 | [diff] [blame^] | 56 | static void |
| 57 | list_tests(void) |
| 58 | { |
| 59 | const struct weston_test *t; |
| 60 | |
| 61 | fprintf(stderr, "Available test names:\n"); |
| 62 | for (t = &__start_test_section; t < &__stop_test_section; t++) |
| 63 | fprintf(stderr, " %s\n", t->name); |
| 64 | } |
| 65 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 66 | int main(int argc, char *argv[]) |
| 67 | { |
| 68 | const struct weston_test *t; |
| 69 | pid_t pid; |
| 70 | int total, pass; |
| 71 | siginfo_t info; |
| 72 | |
| 73 | if (argc == 2) { |
Peter Hutterer | 534f0a4 | 2013-09-11 16:08:30 +1000 | [diff] [blame^] | 74 | const char *testname = argv[1]; |
| 75 | if (strcmp(testname, "--help") == 0 || |
| 76 | strcmp(testname, "-h") == 0) { |
| 77 | fprintf(stderr, "Usage: %s [test-name]\n", program_invocation_short_name); |
| 78 | list_tests(); |
| 79 | exit(EXIT_SUCCESS); |
| 80 | } |
| 81 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 82 | t = find_test(argv[1]); |
| 83 | if (t == NULL) { |
| 84 | fprintf(stderr, "unknown test: \"%s\"\n", argv[1]); |
| 85 | exit(EXIT_FAILURE); |
| 86 | } |
| 87 | |
| 88 | run_test(t); |
| 89 | } |
| 90 | |
| 91 | pass = 0; |
| 92 | for (t = &__start_test_section; t < &__stop_test_section; t++) { |
| 93 | int success = 0; |
Pekka Paalanen | 585c27c | 2012-12-18 17:30:16 +0200 | [diff] [blame] | 94 | int hardfail = 0; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 95 | |
| 96 | pid = fork(); |
| 97 | assert(pid >= 0); |
| 98 | |
| 99 | if (pid == 0) |
| 100 | run_test(t); /* never returns */ |
| 101 | |
| 102 | if (waitid(P_ALL, 0, &info, WEXITED)) { |
| 103 | fprintf(stderr, "waitid failed: %m\n"); |
| 104 | abort(); |
| 105 | } |
| 106 | |
| 107 | fprintf(stderr, "test \"%s\":\t", t->name); |
| 108 | switch (info.si_code) { |
| 109 | case CLD_EXITED: |
| 110 | fprintf(stderr, "exit status %d", info.si_status); |
| 111 | if (info.si_status == EXIT_SUCCESS) |
| 112 | success = 1; |
| 113 | break; |
| 114 | case CLD_KILLED: |
| 115 | case CLD_DUMPED: |
| 116 | fprintf(stderr, "signal %d", info.si_status); |
Pekka Paalanen | 585c27c | 2012-12-18 17:30:16 +0200 | [diff] [blame] | 117 | if (info.si_status != SIGABRT) |
| 118 | hardfail = 1; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 119 | break; |
| 120 | } |
| 121 | |
| 122 | if (t->must_fail) |
| 123 | success = !success; |
| 124 | |
Pekka Paalanen | 585c27c | 2012-12-18 17:30:16 +0200 | [diff] [blame] | 125 | if (success && !hardfail) { |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 126 | pass++; |
| 127 | fprintf(stderr, ", pass.\n"); |
| 128 | } else |
| 129 | fprintf(stderr, ", fail.\n"); |
| 130 | } |
| 131 | |
| 132 | total = &__stop_test_section - &__start_test_section; |
| 133 | fprintf(stderr, "%d tests, %d pass, %d fail\n", |
| 134 | total, pass, total - pass); |
| 135 | |
| 136 | return pass == total ? EXIT_SUCCESS : EXIT_FAILURE; |
| 137 | } |