blob: b1e89bc2e2727dd4fefc31a0f8b044eadf341228 [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
45static const struct weston_test *
46find_test(const char *name)
47{
48 const struct weston_test *t;
49
50 for (t = &__start_test_section; t < &__stop_test_section; t++)
51 if (strcmp(t->name, name) == 0)
52 return t;
53
54 return NULL;
55}
56
57static void
Sam Spilsburyb5021262013-09-13 10:01:20 +080058run_test(const struct weston_test *t, void *data)
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080059{
Sam Spilsburyb5021262013-09-13 10:01:20 +080060 t->run(data);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080061 exit(EXIT_SUCCESS);
62}
63
Peter Hutterer534f0a42013-09-11 16:08:30 +100064static void
65list_tests(void)
66{
67 const struct weston_test *t;
68
69 fprintf(stderr, "Available test names:\n");
70 for (t = &__start_test_section; t < &__stop_test_section; t++)
71 fprintf(stderr, " %s\n", t->name);
72}
73
Sam Spilsburyb5021262013-09-13 10:01:20 +080074static int
75exec_and_report_test(const struct weston_test *t, void *test_data, int iteration)
76{
77 int success = 0;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +010078 int skip = 0;
Sam Spilsburyb5021262013-09-13 10:01:20 +080079 int hardfail = 0;
80 siginfo_t info;
81
82 pid_t pid = fork();
83 assert(pid >= 0);
84
85 if (pid == 0)
86 run_test(t, test_data); /* never returns */
87
88 if (waitid(P_ALL, 0, &info, WEXITED)) {
89 fprintf(stderr, "waitid failed: %m\n");
90 abort();
91 }
92
93 if (test_data)
94 fprintf(stderr, "test \"%s/%i\":\t", t->name, iteration);
95 else
96 fprintf(stderr, "test \"%s\":\t", t->name);
97
98 switch (info.si_code) {
99 case CLD_EXITED:
100 fprintf(stderr, "exit status %d", info.si_status);
101 if (info.si_status == EXIT_SUCCESS)
102 success = 1;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100103 else if (info.si_status == SKIP)
104 skip = 1;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800105 break;
106 case CLD_KILLED:
107 case CLD_DUMPED:
108 fprintf(stderr, "signal %d", info.si_status);
109 if (info.si_status != SIGABRT)
110 hardfail = 1;
111 break;
112 }
113
114 if (t->must_fail)
115 success = !success;
116
117 if (success && !hardfail) {
118 fprintf(stderr, ", pass.\n");
119 return 1;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100120 } else if (skip) {
121 fprintf(stderr, ", skip.\n");
122 return SKIP;
123 } else {
Sam Spilsburyb5021262013-09-13 10:01:20 +0800124 fprintf(stderr, ", fail.\n");
125 return 0;
126 }
127}
128
129/* Returns number of tests and number of pass / fail in param args */
130static int
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100131iterate_test(const struct weston_test *t, int *passed, int *skipped)
Sam Spilsburyb5021262013-09-13 10:01:20 +0800132{
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100133 int ret, i;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800134 void *current_test_data = (void *) t->table_data;
135 for (i = 0; i < t->n_elements; ++i, current_test_data += t->element_size)
136 {
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100137 ret = exec_and_report_test(t, current_test_data, i);
138 if (ret == SKIP)
139 ++(*skipped);
140 else if (ret)
Sam Spilsburyb5021262013-09-13 10:01:20 +0800141 ++(*passed);
142 }
143
144 return t->n_elements;
145}
146
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800147int main(int argc, char *argv[])
148{
149 const struct weston_test *t;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800150 int total = 0;
151 int pass = 0;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100152 int skip = 0;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800153
154 if (argc == 2) {
Peter Hutterer534f0a42013-09-11 16:08:30 +1000155 const char *testname = argv[1];
156 if (strcmp(testname, "--help") == 0 ||
157 strcmp(testname, "-h") == 0) {
158 fprintf(stderr, "Usage: %s [test-name]\n", program_invocation_short_name);
159 list_tests();
160 exit(EXIT_SUCCESS);
161 }
162
Derek Foreman6bef2372014-11-19 15:06:20 -0800163 if (strcmp(testname, "--params") == 0 ||
164 strcmp(testname, "-p") == 0) {
165 printf("%s", server_parameters);
166 exit(EXIT_SUCCESS);
167 }
168
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800169 t = find_test(argv[1]);
170 if (t == NULL) {
171 fprintf(stderr, "unknown test: \"%s\"\n", argv[1]);
Peter Hutterer9715d4d2013-09-11 16:08:47 +1000172 list_tests();
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800173 exit(EXIT_FAILURE);
174 }
175
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100176 int number_passed_in_test = 0, number_skipped_in_test = 0;
177 total += iterate_test(t, &number_passed_in_test, &number_skipped_in_test);
Sam Spilsburyb5021262013-09-13 10:01:20 +0800178 pass += number_passed_in_test;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100179 skip += number_skipped_in_test;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800180 } else {
181 for (t = &__start_test_section; t < &__stop_test_section; t++) {
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100182 int number_passed_in_test = 0, number_skipped_in_test = 0;
183 total += iterate_test(t, &number_passed_in_test, &number_skipped_in_test);
Sam Spilsburyb5021262013-09-13 10:01:20 +0800184 pass += number_passed_in_test;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100185 skip += number_skipped_in_test;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800186 }
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800187 }
188
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100189 fprintf(stderr, "%d tests, %d pass, %d skip, %d fail\n",
190 total, pass, skip, total - pass - skip);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800191
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100192 if (skip == total)
193 return SKIP;
194 else if (pass + skip == total)
195 return EXIT_SUCCESS;
196
197 return EXIT_FAILURE;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800198}