blob: ce0a6708eca4674913295f39d66b3eebcffc6fa8 [file] [log] [blame]
U. Artie Eoff1ba9b382012-12-07 13:50:32 -08001/*
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 Huttererd8ca8902013-09-11 16:08:04 +100023#include "config.h"
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080024#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 Paalanen585c27c2012-12-18 17:30:16 +020032#include <signal.h>
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080033#include "weston-test-runner.h"
34
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +010035#define SKIP 77
36
Derek Foreman6bef2372014-11-19 15:06:20 -080037char __attribute__((weak)) *server_parameters="";
38
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080039extern const struct weston_test __start_test_section, __stop_test_section;
40
41static const struct weston_test *
42find_test(const char *name)
43{
44 const struct weston_test *t;
45
46 for (t = &__start_test_section; t < &__stop_test_section; t++)
47 if (strcmp(t->name, name) == 0)
48 return t;
49
50 return NULL;
51}
52
53static void
Sam Spilsburyb5021262013-09-13 10:01:20 +080054run_test(const struct weston_test *t, void *data)
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080055{
Sam Spilsburyb5021262013-09-13 10:01:20 +080056 t->run(data);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080057 exit(EXIT_SUCCESS);
58}
59
Peter Hutterer534f0a42013-09-11 16:08:30 +100060static void
61list_tests(void)
62{
63 const struct weston_test *t;
64
65 fprintf(stderr, "Available test names:\n");
66 for (t = &__start_test_section; t < &__stop_test_section; t++)
67 fprintf(stderr, " %s\n", t->name);
68}
69
Sam Spilsburyb5021262013-09-13 10:01:20 +080070static int
71exec_and_report_test(const struct weston_test *t, void *test_data, int iteration)
72{
73 int success = 0;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +010074 int skip = 0;
Sam Spilsburyb5021262013-09-13 10:01:20 +080075 int hardfail = 0;
76 siginfo_t info;
77
78 pid_t pid = fork();
79 assert(pid >= 0);
80
81 if (pid == 0)
82 run_test(t, test_data); /* never returns */
83
84 if (waitid(P_ALL, 0, &info, WEXITED)) {
85 fprintf(stderr, "waitid failed: %m\n");
86 abort();
87 }
88
89 if (test_data)
90 fprintf(stderr, "test \"%s/%i\":\t", t->name, iteration);
91 else
92 fprintf(stderr, "test \"%s\":\t", t->name);
93
94 switch (info.si_code) {
95 case CLD_EXITED:
96 fprintf(stderr, "exit status %d", info.si_status);
97 if (info.si_status == EXIT_SUCCESS)
98 success = 1;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +010099 else if (info.si_status == SKIP)
100 skip = 1;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800101 break;
102 case CLD_KILLED:
103 case CLD_DUMPED:
104 fprintf(stderr, "signal %d", info.si_status);
105 if (info.si_status != SIGABRT)
106 hardfail = 1;
107 break;
108 }
109
110 if (t->must_fail)
111 success = !success;
112
113 if (success && !hardfail) {
114 fprintf(stderr, ", pass.\n");
115 return 1;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100116 } else if (skip) {
117 fprintf(stderr, ", skip.\n");
118 return SKIP;
119 } else {
Sam Spilsburyb5021262013-09-13 10:01:20 +0800120 fprintf(stderr, ", fail.\n");
121 return 0;
122 }
123}
124
125/* Returns number of tests and number of pass / fail in param args */
126static int
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100127iterate_test(const struct weston_test *t, int *passed, int *skipped)
Sam Spilsburyb5021262013-09-13 10:01:20 +0800128{
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100129 int ret, i;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800130 void *current_test_data = (void *) t->table_data;
131 for (i = 0; i < t->n_elements; ++i, current_test_data += t->element_size)
132 {
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100133 ret = exec_and_report_test(t, current_test_data, i);
134 if (ret == SKIP)
135 ++(*skipped);
136 else if (ret)
Sam Spilsburyb5021262013-09-13 10:01:20 +0800137 ++(*passed);
138 }
139
140 return t->n_elements;
141}
142
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800143int main(int argc, char *argv[])
144{
145 const struct weston_test *t;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800146 int total = 0;
147 int pass = 0;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100148 int skip = 0;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800149
150 if (argc == 2) {
Peter Hutterer534f0a42013-09-11 16:08:30 +1000151 const char *testname = argv[1];
152 if (strcmp(testname, "--help") == 0 ||
153 strcmp(testname, "-h") == 0) {
154 fprintf(stderr, "Usage: %s [test-name]\n", program_invocation_short_name);
155 list_tests();
156 exit(EXIT_SUCCESS);
157 }
158
Derek Foreman6bef2372014-11-19 15:06:20 -0800159 if (strcmp(testname, "--params") == 0 ||
160 strcmp(testname, "-p") == 0) {
161 printf("%s", server_parameters);
162 exit(EXIT_SUCCESS);
163 }
164
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800165 t = find_test(argv[1]);
166 if (t == NULL) {
167 fprintf(stderr, "unknown test: \"%s\"\n", argv[1]);
Peter Hutterer9715d4d2013-09-11 16:08:47 +1000168 list_tests();
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800169 exit(EXIT_FAILURE);
170 }
171
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100172 int number_passed_in_test = 0, number_skipped_in_test = 0;
173 total += iterate_test(t, &number_passed_in_test, &number_skipped_in_test);
Sam Spilsburyb5021262013-09-13 10:01:20 +0800174 pass += number_passed_in_test;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100175 skip += number_skipped_in_test;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800176 } else {
177 for (t = &__start_test_section; t < &__stop_test_section; t++) {
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100178 int number_passed_in_test = 0, number_skipped_in_test = 0;
179 total += iterate_test(t, &number_passed_in_test, &number_skipped_in_test);
Sam Spilsburyb5021262013-09-13 10:01:20 +0800180 pass += number_passed_in_test;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100181 skip += number_skipped_in_test;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800182 }
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800183 }
184
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100185 fprintf(stderr, "%d tests, %d pass, %d skip, %d fail\n",
186 total, pass, skip, total - pass - skip);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800187
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100188 if (skip == total)
189 return SKIP;
190 else if (pass + skip == total)
191 return EXIT_SUCCESS;
192
193 return EXIT_FAILURE;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800194}