blob: f197265d89265944cca8a1a9e3e0efe1dff91924 [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
Sam Spilsburyb5021262013-09-13 10:01:20 +080091static int
92exec_and_report_test(const struct weston_test *t, void *test_data, int iteration)
93{
94 int success = 0;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +010095 int skip = 0;
Sam Spilsburyb5021262013-09-13 10:01:20 +080096 int hardfail = 0;
97 siginfo_t info;
98
99 pid_t pid = fork();
100 assert(pid >= 0);
101
102 if (pid == 0)
Pekka Paalanenb5817832017-01-27 17:30:26 +0100103 run_test(t, test_data, iteration); /* never returns */
Sam Spilsburyb5021262013-09-13 10:01:20 +0800104
105 if (waitid(P_ALL, 0, &info, WEXITED)) {
106 fprintf(stderr, "waitid failed: %m\n");
107 abort();
108 }
109
110 if (test_data)
111 fprintf(stderr, "test \"%s/%i\":\t", t->name, iteration);
112 else
113 fprintf(stderr, "test \"%s\":\t", t->name);
114
115 switch (info.si_code) {
116 case CLD_EXITED:
117 fprintf(stderr, "exit status %d", info.si_status);
118 if (info.si_status == EXIT_SUCCESS)
119 success = 1;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100120 else if (info.si_status == SKIP)
121 skip = 1;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800122 break;
123 case CLD_KILLED:
124 case CLD_DUMPED:
125 fprintf(stderr, "signal %d", info.si_status);
126 if (info.si_status != SIGABRT)
127 hardfail = 1;
128 break;
129 }
130
131 if (t->must_fail)
132 success = !success;
133
134 if (success && !hardfail) {
135 fprintf(stderr, ", pass.\n");
136 return 1;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100137 } else if (skip) {
138 fprintf(stderr, ", skip.\n");
139 return SKIP;
140 } else {
Sam Spilsburyb5021262013-09-13 10:01:20 +0800141 fprintf(stderr, ", fail.\n");
142 return 0;
143 }
144}
145
146/* Returns number of tests and number of pass / fail in param args */
147static int
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100148iterate_test(const struct weston_test *t, int *passed, int *skipped)
Sam Spilsburyb5021262013-09-13 10:01:20 +0800149{
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100150 int ret, i;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800151 void *current_test_data = (void *) t->table_data;
152 for (i = 0; i < t->n_elements; ++i, current_test_data += t->element_size)
153 {
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100154 ret = exec_and_report_test(t, current_test_data, i);
155 if (ret == SKIP)
156 ++(*skipped);
157 else if (ret)
Sam Spilsburyb5021262013-09-13 10:01:20 +0800158 ++(*passed);
159 }
160
161 return t->n_elements;
162}
163
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800164int main(int argc, char *argv[])
165{
166 const struct weston_test *t;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800167 int total = 0;
168 int pass = 0;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100169 int skip = 0;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800170
171 if (argc == 2) {
Peter Hutterer534f0a42013-09-11 16:08:30 +1000172 const char *testname = argv[1];
173 if (strcmp(testname, "--help") == 0 ||
174 strcmp(testname, "-h") == 0) {
175 fprintf(stderr, "Usage: %s [test-name]\n", program_invocation_short_name);
176 list_tests();
177 exit(EXIT_SUCCESS);
178 }
179
Derek Foreman6bef2372014-11-19 15:06:20 -0800180 if (strcmp(testname, "--params") == 0 ||
181 strcmp(testname, "-p") == 0) {
182 printf("%s", server_parameters);
183 exit(EXIT_SUCCESS);
184 }
185
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800186 t = find_test(argv[1]);
187 if (t == NULL) {
188 fprintf(stderr, "unknown test: \"%s\"\n", argv[1]);
Peter Hutterer9715d4d2013-09-11 16:08:47 +1000189 list_tests();
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800190 exit(EXIT_FAILURE);
191 }
192
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100193 int number_passed_in_test = 0, number_skipped_in_test = 0;
194 total += iterate_test(t, &number_passed_in_test, &number_skipped_in_test);
Sam Spilsburyb5021262013-09-13 10:01:20 +0800195 pass += number_passed_in_test;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100196 skip += number_skipped_in_test;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800197 } else {
198 for (t = &__start_test_section; t < &__stop_test_section; t++) {
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100199 int number_passed_in_test = 0, number_skipped_in_test = 0;
200 total += iterate_test(t, &number_passed_in_test, &number_skipped_in_test);
Sam Spilsburyb5021262013-09-13 10:01:20 +0800201 pass += number_passed_in_test;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100202 skip += number_skipped_in_test;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800203 }
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800204 }
205
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100206 fprintf(stderr, "%d tests, %d pass, %d skip, %d fail\n",
207 total, pass, skip, total - pass - skip);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800208
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100209 if (skip == total)
210 return SKIP;
211 else if (pass + skip == total)
212 return EXIT_SUCCESS;
213
214 return EXIT_FAILURE;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800215}