blob: 3e1ace689b38ef13c6fffdcd4d9da1e3d9e9ee7f [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"
Bryce Harrington12cc4052014-11-19 17:18:33 -080024
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080025#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 Paalanen585c27c2012-12-18 17:30:16 +020033#include <signal.h>
Bryce Harrington12cc4052014-11-19 17:18:33 -080034
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080035#include "weston-test-runner.h"
36
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +010037#define SKIP 77
38
Derek Foreman6bef2372014-11-19 15:06:20 -080039char __attribute__((weak)) *server_parameters="";
40
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080041extern const struct weston_test __start_test_section, __stop_test_section;
42
43static const struct weston_test *
44find_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
55static void
Sam Spilsburyb5021262013-09-13 10:01:20 +080056run_test(const struct weston_test *t, void *data)
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080057{
Sam Spilsburyb5021262013-09-13 10:01:20 +080058 t->run(data);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080059 exit(EXIT_SUCCESS);
60}
61
Peter Hutterer534f0a42013-09-11 16:08:30 +100062static void
63list_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 Spilsburyb5021262013-09-13 10:01:20 +080072static int
73exec_and_report_test(const struct weston_test *t, void *test_data, int iteration)
74{
75 int success = 0;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +010076 int skip = 0;
Sam Spilsburyb5021262013-09-13 10:01:20 +080077 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 Monfortdae8a4b2014-02-07 09:34:48 +0100101 else if (info.si_status == SKIP)
102 skip = 1;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800103 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 Monfortdae8a4b2014-02-07 09:34:48 +0100118 } else if (skip) {
119 fprintf(stderr, ", skip.\n");
120 return SKIP;
121 } else {
Sam Spilsburyb5021262013-09-13 10:01:20 +0800122 fprintf(stderr, ", fail.\n");
123 return 0;
124 }
125}
126
127/* Returns number of tests and number of pass / fail in param args */
128static int
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100129iterate_test(const struct weston_test *t, int *passed, int *skipped)
Sam Spilsburyb5021262013-09-13 10:01:20 +0800130{
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100131 int ret, i;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800132 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 Monfortdae8a4b2014-02-07 09:34:48 +0100135 ret = exec_and_report_test(t, current_test_data, i);
136 if (ret == SKIP)
137 ++(*skipped);
138 else if (ret)
Sam Spilsburyb5021262013-09-13 10:01:20 +0800139 ++(*passed);
140 }
141
142 return t->n_elements;
143}
144
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800145int main(int argc, char *argv[])
146{
147 const struct weston_test *t;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800148 int total = 0;
149 int pass = 0;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100150 int skip = 0;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800151
152 if (argc == 2) {
Peter Hutterer534f0a42013-09-11 16:08:30 +1000153 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 Foreman6bef2372014-11-19 15:06:20 -0800161 if (strcmp(testname, "--params") == 0 ||
162 strcmp(testname, "-p") == 0) {
163 printf("%s", server_parameters);
164 exit(EXIT_SUCCESS);
165 }
166
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800167 t = find_test(argv[1]);
168 if (t == NULL) {
169 fprintf(stderr, "unknown test: \"%s\"\n", argv[1]);
Peter Hutterer9715d4d2013-09-11 16:08:47 +1000170 list_tests();
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800171 exit(EXIT_FAILURE);
172 }
173
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100174 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 Spilsburyb5021262013-09-13 10:01:20 +0800176 pass += number_passed_in_test;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100177 skip += number_skipped_in_test;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800178 } else {
179 for (t = &__start_test_section; t < &__stop_test_section; t++) {
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100180 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 Spilsburyb5021262013-09-13 10:01:20 +0800182 pass += number_passed_in_test;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100183 skip += number_skipped_in_test;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800184 }
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800185 }
186
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100187 fprintf(stderr, "%d tests, %d pass, %d skip, %d fail\n",
188 total, pass, skip, total - pass - skip);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800189
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100190 if (skip == total)
191 return SKIP;
192 else if (pass + skip == total)
193 return EXIT_SUCCESS;
194
195 return EXIT_FAILURE;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800196}