blob: f0566ac06e34ba87cba4e35c878890bec881a5e4 [file] [log] [blame]
U. Artie Eoff1ba9b382012-12-07 13:50:32 -08001/*
2 * Copyright © 2012 Intel Corporation
3 *
Bryce Harrington2cc92972015-06-11 15:39:40 -07004 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080011 *
Bryce Harrington2cc92972015-06-11 15:39:40 -070012 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080024 */
25
Peter Huttererd8ca8902013-09-11 16:08:04 +100026#include "config.h"
Bryce Harrington12cc4052014-11-19 17:18:33 -080027
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080028#include <unistd.h>
29#include <stdio.h>
30#include <stdlib.h>
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080031#include <sys/wait.h>
32#include <string.h>
33#include <assert.h>
34#include <errno.h>
Pekka Paalanen585c27c2012-12-18 17:30:16 +020035#include <signal.h>
Bryce Harrington12cc4052014-11-19 17:18:33 -080036
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080037#include "weston-test-runner.h"
38
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +010039#define SKIP 77
40
Derek Foreman6bef2372014-11-19 15:06:20 -080041char __attribute__((weak)) *server_parameters="";
42
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080043extern const struct weston_test __start_test_section, __stop_test_section;
44
Pekka Paalanenb5817832017-01-27 17:30:26 +010045static const char *test_name_;
46
47const char *
48get_test_name(void)
49{
50 return test_name_;
51}
52
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080053static const struct weston_test *
54find_test(const char *name)
55{
56 const struct weston_test *t;
57
58 for (t = &__start_test_section; t < &__stop_test_section; t++)
59 if (strcmp(t->name, name) == 0)
60 return t;
61
62 return NULL;
63}
64
65static void
Pekka Paalanenb5817832017-01-27 17:30:26 +010066run_test(const struct weston_test *t, void *data, int iteration)
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080067{
Pekka Paalanenb5817832017-01-27 17:30:26 +010068 char str[512];
69
70 if (data) {
71 snprintf(str, sizeof(str), "%s[%d]", t->name, iteration);
72 test_name_ = str;
73 } else {
74 test_name_ = t->name;
75 }
76
Sam Spilsburyb5021262013-09-13 10:01:20 +080077 t->run(data);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080078 exit(EXIT_SUCCESS);
79}
80
Peter Hutterer534f0a42013-09-11 16:08:30 +100081static void
82list_tests(void)
83{
84 const struct weston_test *t;
85
86 fprintf(stderr, "Available test names:\n");
87 for (t = &__start_test_section; t < &__stop_test_section; t++)
88 fprintf(stderr, " %s\n", t->name);
89}
90
Pekka Paalanen19222b42017-02-07 14:18:01 +020091/* iteration is valid only if test_data is not NULL */
Sam Spilsburyb5021262013-09-13 10:01:20 +080092static int
93exec_and_report_test(const struct weston_test *t, void *test_data, int iteration)
94{
95 int success = 0;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +010096 int skip = 0;
Sam Spilsburyb5021262013-09-13 10:01:20 +080097 int hardfail = 0;
98 siginfo_t info;
99
100 pid_t pid = fork();
101 assert(pid >= 0);
102
103 if (pid == 0)
Pekka Paalanenb5817832017-01-27 17:30:26 +0100104 run_test(t, test_data, iteration); /* never returns */
Sam Spilsburyb5021262013-09-13 10:01:20 +0800105
106 if (waitid(P_ALL, 0, &info, WEXITED)) {
107 fprintf(stderr, "waitid failed: %m\n");
108 abort();
109 }
110
111 if (test_data)
112 fprintf(stderr, "test \"%s/%i\":\t", t->name, iteration);
113 else
114 fprintf(stderr, "test \"%s\":\t", t->name);
115
116 switch (info.si_code) {
117 case CLD_EXITED:
118 fprintf(stderr, "exit status %d", info.si_status);
119 if (info.si_status == EXIT_SUCCESS)
120 success = 1;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100121 else if (info.si_status == SKIP)
122 skip = 1;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800123 break;
124 case CLD_KILLED:
125 case CLD_DUMPED:
126 fprintf(stderr, "signal %d", info.si_status);
127 if (info.si_status != SIGABRT)
128 hardfail = 1;
129 break;
130 }
131
132 if (t->must_fail)
133 success = !success;
134
135 if (success && !hardfail) {
136 fprintf(stderr, ", pass.\n");
137 return 1;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100138 } else if (skip) {
139 fprintf(stderr, ", skip.\n");
140 return SKIP;
141 } else {
Sam Spilsburyb5021262013-09-13 10:01:20 +0800142 fprintf(stderr, ", fail.\n");
143 return 0;
144 }
145}
146
Pekka Paalanen19222b42017-02-07 14:18:01 +0200147/* Returns number of tests and number of pass / fail in param args.
148 * Even non-iterated tests go through here, they simply have n_elements = 1 and
149 * table_data = NULL.
150 */
Sam Spilsburyb5021262013-09-13 10:01:20 +0800151static int
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100152iterate_test(const struct weston_test *t, int *passed, int *skipped)
Sam Spilsburyb5021262013-09-13 10:01:20 +0800153{
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100154 int ret, i;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800155 void *current_test_data = (void *) t->table_data;
156 for (i = 0; i < t->n_elements; ++i, current_test_data += t->element_size)
157 {
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100158 ret = exec_and_report_test(t, current_test_data, i);
159 if (ret == SKIP)
160 ++(*skipped);
161 else if (ret)
Sam Spilsburyb5021262013-09-13 10:01:20 +0800162 ++(*passed);
163 }
164
165 return t->n_elements;
166}
167
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800168int main(int argc, char *argv[])
169{
170 const struct weston_test *t;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800171 int total = 0;
172 int pass = 0;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100173 int skip = 0;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800174
175 if (argc == 2) {
Peter Hutterer534f0a42013-09-11 16:08:30 +1000176 const char *testname = argv[1];
177 if (strcmp(testname, "--help") == 0 ||
178 strcmp(testname, "-h") == 0) {
179 fprintf(stderr, "Usage: %s [test-name]\n", program_invocation_short_name);
180 list_tests();
181 exit(EXIT_SUCCESS);
182 }
183
Derek Foreman6bef2372014-11-19 15:06:20 -0800184 if (strcmp(testname, "--params") == 0 ||
185 strcmp(testname, "-p") == 0) {
186 printf("%s", server_parameters);
187 exit(EXIT_SUCCESS);
188 }
189
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800190 t = find_test(argv[1]);
191 if (t == NULL) {
192 fprintf(stderr, "unknown test: \"%s\"\n", argv[1]);
Peter Hutterer9715d4d2013-09-11 16:08:47 +1000193 list_tests();
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800194 exit(EXIT_FAILURE);
195 }
196
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100197 int number_passed_in_test = 0, number_skipped_in_test = 0;
198 total += iterate_test(t, &number_passed_in_test, &number_skipped_in_test);
Sam Spilsburyb5021262013-09-13 10:01:20 +0800199 pass += number_passed_in_test;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100200 skip += number_skipped_in_test;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800201 } else {
202 for (t = &__start_test_section; t < &__stop_test_section; t++) {
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100203 int number_passed_in_test = 0, number_skipped_in_test = 0;
204 total += iterate_test(t, &number_passed_in_test, &number_skipped_in_test);
Sam Spilsburyb5021262013-09-13 10:01:20 +0800205 pass += number_passed_in_test;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100206 skip += number_skipped_in_test;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800207 }
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800208 }
209
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100210 fprintf(stderr, "%d tests, %d pass, %d skip, %d fail\n",
211 total, pass, skip, total - pass - skip);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800212
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100213 if (skip == total)
214 return SKIP;
215 else if (pass + skip == total)
216 return EXIT_SUCCESS;
217
218 return EXIT_FAILURE;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800219}