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" |
Bryce Harrington | 12cc405 | 2014-11-19 17:18:33 -0800 | [diff] [blame^] | 24 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 25 | #include <unistd.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/wait.h> |
| 30 | #include <string.h> |
| 31 | #include <assert.h> |
| 32 | #include <errno.h> |
Pekka Paalanen | 585c27c | 2012-12-18 17:30:16 +0200 | [diff] [blame] | 33 | #include <signal.h> |
Bryce Harrington | 12cc405 | 2014-11-19 17:18:33 -0800 | [diff] [blame^] | 34 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 35 | #include "weston-test-runner.h" |
| 36 | |
Emilio Pozuelo Monfort | dae8a4b | 2014-02-07 09:34:48 +0100 | [diff] [blame] | 37 | #define SKIP 77 |
| 38 | |
Derek Foreman | 6bef237 | 2014-11-19 15:06:20 -0800 | [diff] [blame] | 39 | char __attribute__((weak)) *server_parameters=""; |
| 40 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 41 | extern const struct weston_test __start_test_section, __stop_test_section; |
| 42 | |
| 43 | static const struct weston_test * |
| 44 | find_test(const char *name) |
| 45 | { |
| 46 | const struct weston_test *t; |
| 47 | |
| 48 | for (t = &__start_test_section; t < &__stop_test_section; t++) |
| 49 | if (strcmp(t->name, name) == 0) |
| 50 | return t; |
| 51 | |
| 52 | return NULL; |
| 53 | } |
| 54 | |
| 55 | static void |
Sam Spilsbury | b502126 | 2013-09-13 10:01:20 +0800 | [diff] [blame] | 56 | run_test(const struct weston_test *t, void *data) |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 57 | { |
Sam Spilsbury | b502126 | 2013-09-13 10:01:20 +0800 | [diff] [blame] | 58 | t->run(data); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 59 | exit(EXIT_SUCCESS); |
| 60 | } |
| 61 | |
Peter Hutterer | 534f0a4 | 2013-09-11 16:08:30 +1000 | [diff] [blame] | 62 | static void |
| 63 | list_tests(void) |
| 64 | { |
| 65 | const struct weston_test *t; |
| 66 | |
| 67 | fprintf(stderr, "Available test names:\n"); |
| 68 | for (t = &__start_test_section; t < &__stop_test_section; t++) |
| 69 | fprintf(stderr, " %s\n", t->name); |
| 70 | } |
| 71 | |
Sam Spilsbury | b502126 | 2013-09-13 10:01:20 +0800 | [diff] [blame] | 72 | static int |
| 73 | exec_and_report_test(const struct weston_test *t, void *test_data, int iteration) |
| 74 | { |
| 75 | int success = 0; |
Emilio Pozuelo Monfort | dae8a4b | 2014-02-07 09:34:48 +0100 | [diff] [blame] | 76 | int skip = 0; |
Sam Spilsbury | b502126 | 2013-09-13 10:01:20 +0800 | [diff] [blame] | 77 | int hardfail = 0; |
| 78 | siginfo_t info; |
| 79 | |
| 80 | pid_t pid = fork(); |
| 81 | assert(pid >= 0); |
| 82 | |
| 83 | if (pid == 0) |
| 84 | run_test(t, test_data); /* never returns */ |
| 85 | |
| 86 | if (waitid(P_ALL, 0, &info, WEXITED)) { |
| 87 | fprintf(stderr, "waitid failed: %m\n"); |
| 88 | abort(); |
| 89 | } |
| 90 | |
| 91 | if (test_data) |
| 92 | fprintf(stderr, "test \"%s/%i\":\t", t->name, iteration); |
| 93 | else |
| 94 | fprintf(stderr, "test \"%s\":\t", t->name); |
| 95 | |
| 96 | switch (info.si_code) { |
| 97 | case CLD_EXITED: |
| 98 | fprintf(stderr, "exit status %d", info.si_status); |
| 99 | if (info.si_status == EXIT_SUCCESS) |
| 100 | success = 1; |
Emilio Pozuelo Monfort | dae8a4b | 2014-02-07 09:34:48 +0100 | [diff] [blame] | 101 | else if (info.si_status == SKIP) |
| 102 | skip = 1; |
Sam Spilsbury | b502126 | 2013-09-13 10:01:20 +0800 | [diff] [blame] | 103 | break; |
| 104 | case CLD_KILLED: |
| 105 | case CLD_DUMPED: |
| 106 | fprintf(stderr, "signal %d", info.si_status); |
| 107 | if (info.si_status != SIGABRT) |
| 108 | hardfail = 1; |
| 109 | break; |
| 110 | } |
| 111 | |
| 112 | if (t->must_fail) |
| 113 | success = !success; |
| 114 | |
| 115 | if (success && !hardfail) { |
| 116 | fprintf(stderr, ", pass.\n"); |
| 117 | return 1; |
Emilio Pozuelo Monfort | dae8a4b | 2014-02-07 09:34:48 +0100 | [diff] [blame] | 118 | } else if (skip) { |
| 119 | fprintf(stderr, ", skip.\n"); |
| 120 | return SKIP; |
| 121 | } else { |
Sam Spilsbury | b502126 | 2013-09-13 10:01:20 +0800 | [diff] [blame] | 122 | fprintf(stderr, ", fail.\n"); |
| 123 | return 0; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /* Returns number of tests and number of pass / fail in param args */ |
| 128 | static int |
Emilio Pozuelo Monfort | dae8a4b | 2014-02-07 09:34:48 +0100 | [diff] [blame] | 129 | iterate_test(const struct weston_test *t, int *passed, int *skipped) |
Sam Spilsbury | b502126 | 2013-09-13 10:01:20 +0800 | [diff] [blame] | 130 | { |
Emilio Pozuelo Monfort | dae8a4b | 2014-02-07 09:34:48 +0100 | [diff] [blame] | 131 | int ret, i; |
Sam Spilsbury | b502126 | 2013-09-13 10:01:20 +0800 | [diff] [blame] | 132 | void *current_test_data = (void *) t->table_data; |
| 133 | for (i = 0; i < t->n_elements; ++i, current_test_data += t->element_size) |
| 134 | { |
Emilio Pozuelo Monfort | dae8a4b | 2014-02-07 09:34:48 +0100 | [diff] [blame] | 135 | ret = exec_and_report_test(t, current_test_data, i); |
| 136 | if (ret == SKIP) |
| 137 | ++(*skipped); |
| 138 | else if (ret) |
Sam Spilsbury | b502126 | 2013-09-13 10:01:20 +0800 | [diff] [blame] | 139 | ++(*passed); |
| 140 | } |
| 141 | |
| 142 | return t->n_elements; |
| 143 | } |
| 144 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 145 | int main(int argc, char *argv[]) |
| 146 | { |
| 147 | const struct weston_test *t; |
Sam Spilsbury | b502126 | 2013-09-13 10:01:20 +0800 | [diff] [blame] | 148 | int total = 0; |
| 149 | int pass = 0; |
Emilio Pozuelo Monfort | dae8a4b | 2014-02-07 09:34:48 +0100 | [diff] [blame] | 150 | int skip = 0; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 151 | |
| 152 | if (argc == 2) { |
Peter Hutterer | 534f0a4 | 2013-09-11 16:08:30 +1000 | [diff] [blame] | 153 | const char *testname = argv[1]; |
| 154 | if (strcmp(testname, "--help") == 0 || |
| 155 | strcmp(testname, "-h") == 0) { |
| 156 | fprintf(stderr, "Usage: %s [test-name]\n", program_invocation_short_name); |
| 157 | list_tests(); |
| 158 | exit(EXIT_SUCCESS); |
| 159 | } |
| 160 | |
Derek Foreman | 6bef237 | 2014-11-19 15:06:20 -0800 | [diff] [blame] | 161 | if (strcmp(testname, "--params") == 0 || |
| 162 | strcmp(testname, "-p") == 0) { |
| 163 | printf("%s", server_parameters); |
| 164 | exit(EXIT_SUCCESS); |
| 165 | } |
| 166 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 167 | t = find_test(argv[1]); |
| 168 | if (t == NULL) { |
| 169 | fprintf(stderr, "unknown test: \"%s\"\n", argv[1]); |
Peter Hutterer | 9715d4d | 2013-09-11 16:08:47 +1000 | [diff] [blame] | 170 | list_tests(); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 171 | exit(EXIT_FAILURE); |
| 172 | } |
| 173 | |
Emilio Pozuelo Monfort | dae8a4b | 2014-02-07 09:34:48 +0100 | [diff] [blame] | 174 | int number_passed_in_test = 0, number_skipped_in_test = 0; |
| 175 | total += iterate_test(t, &number_passed_in_test, &number_skipped_in_test); |
Sam Spilsbury | b502126 | 2013-09-13 10:01:20 +0800 | [diff] [blame] | 176 | pass += number_passed_in_test; |
Emilio Pozuelo Monfort | dae8a4b | 2014-02-07 09:34:48 +0100 | [diff] [blame] | 177 | skip += number_skipped_in_test; |
Sam Spilsbury | b502126 | 2013-09-13 10:01:20 +0800 | [diff] [blame] | 178 | } else { |
| 179 | for (t = &__start_test_section; t < &__stop_test_section; t++) { |
Emilio Pozuelo Monfort | dae8a4b | 2014-02-07 09:34:48 +0100 | [diff] [blame] | 180 | int number_passed_in_test = 0, number_skipped_in_test = 0; |
| 181 | total += iterate_test(t, &number_passed_in_test, &number_skipped_in_test); |
Sam Spilsbury | b502126 | 2013-09-13 10:01:20 +0800 | [diff] [blame] | 182 | pass += number_passed_in_test; |
Emilio Pozuelo Monfort | dae8a4b | 2014-02-07 09:34:48 +0100 | [diff] [blame] | 183 | skip += number_skipped_in_test; |
Sam Spilsbury | b502126 | 2013-09-13 10:01:20 +0800 | [diff] [blame] | 184 | } |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Emilio Pozuelo Monfort | dae8a4b | 2014-02-07 09:34:48 +0100 | [diff] [blame] | 187 | fprintf(stderr, "%d tests, %d pass, %d skip, %d fail\n", |
| 188 | total, pass, skip, total - pass - skip); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 189 | |
Emilio Pozuelo Monfort | dae8a4b | 2014-02-07 09:34:48 +0100 | [diff] [blame] | 190 | if (skip == total) |
| 191 | return SKIP; |
| 192 | else if (pass + skip == total) |
| 193 | return EXIT_SUCCESS; |
| 194 | |
| 195 | return EXIT_FAILURE; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 196 | } |