blob: c8c2a6cfeb64fe554d1a1941c93ba39957aeef96 [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>
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080028#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>
Bryce Harrington12cc4052014-11-19 17:18:33 -080033
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080034#include "weston-test-runner.h"
35
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +010036#define SKIP 77
37
Derek Foreman6bef2372014-11-19 15:06:20 -080038char __attribute__((weak)) *server_parameters="";
39
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080040extern const struct weston_test __start_test_section, __stop_test_section;
41
42static const struct weston_test *
43find_test(const char *name)
44{
45 const struct weston_test *t;
46
47 for (t = &__start_test_section; t < &__stop_test_section; t++)
48 if (strcmp(t->name, name) == 0)
49 return t;
50
51 return NULL;
52}
53
54static void
Sam Spilsburyb5021262013-09-13 10:01:20 +080055run_test(const struct weston_test *t, void *data)
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080056{
Sam Spilsburyb5021262013-09-13 10:01:20 +080057 t->run(data);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080058 exit(EXIT_SUCCESS);
59}
60
Peter Hutterer534f0a42013-09-11 16:08:30 +100061static void
62list_tests(void)
63{
64 const struct weston_test *t;
65
66 fprintf(stderr, "Available test names:\n");
67 for (t = &__start_test_section; t < &__stop_test_section; t++)
68 fprintf(stderr, " %s\n", t->name);
69}
70
Sam Spilsburyb5021262013-09-13 10:01:20 +080071static int
72exec_and_report_test(const struct weston_test *t, void *test_data, int iteration)
73{
74 int success = 0;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +010075 int skip = 0;
Sam Spilsburyb5021262013-09-13 10:01:20 +080076 int hardfail = 0;
77 siginfo_t info;
78
79 pid_t pid = fork();
80 assert(pid >= 0);
81
82 if (pid == 0)
83 run_test(t, test_data); /* never returns */
84
85 if (waitid(P_ALL, 0, &info, WEXITED)) {
86 fprintf(stderr, "waitid failed: %m\n");
87 abort();
88 }
89
90 if (test_data)
91 fprintf(stderr, "test \"%s/%i\":\t", t->name, iteration);
92 else
93 fprintf(stderr, "test \"%s\":\t", t->name);
94
95 switch (info.si_code) {
96 case CLD_EXITED:
97 fprintf(stderr, "exit status %d", info.si_status);
98 if (info.si_status == EXIT_SUCCESS)
99 success = 1;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100100 else if (info.si_status == SKIP)
101 skip = 1;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800102 break;
103 case CLD_KILLED:
104 case CLD_DUMPED:
105 fprintf(stderr, "signal %d", info.si_status);
106 if (info.si_status != SIGABRT)
107 hardfail = 1;
108 break;
109 }
110
111 if (t->must_fail)
112 success = !success;
113
114 if (success && !hardfail) {
115 fprintf(stderr, ", pass.\n");
116 return 1;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100117 } else if (skip) {
118 fprintf(stderr, ", skip.\n");
119 return SKIP;
120 } else {
Sam Spilsburyb5021262013-09-13 10:01:20 +0800121 fprintf(stderr, ", fail.\n");
122 return 0;
123 }
124}
125
126/* Returns number of tests and number of pass / fail in param args */
127static int
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100128iterate_test(const struct weston_test *t, int *passed, int *skipped)
Sam Spilsburyb5021262013-09-13 10:01:20 +0800129{
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100130 int ret, i;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800131 void *current_test_data = (void *) t->table_data;
132 for (i = 0; i < t->n_elements; ++i, current_test_data += t->element_size)
133 {
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100134 ret = exec_and_report_test(t, current_test_data, i);
135 if (ret == SKIP)
136 ++(*skipped);
137 else if (ret)
Sam Spilsburyb5021262013-09-13 10:01:20 +0800138 ++(*passed);
139 }
140
141 return t->n_elements;
142}
143
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800144int main(int argc, char *argv[])
145{
146 const struct weston_test *t;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800147 int total = 0;
148 int pass = 0;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100149 int skip = 0;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800150
151 if (argc == 2) {
Peter Hutterer534f0a42013-09-11 16:08:30 +1000152 const char *testname = argv[1];
153 if (strcmp(testname, "--help") == 0 ||
154 strcmp(testname, "-h") == 0) {
155 fprintf(stderr, "Usage: %s [test-name]\n", program_invocation_short_name);
156 list_tests();
157 exit(EXIT_SUCCESS);
158 }
159
Derek Foreman6bef2372014-11-19 15:06:20 -0800160 if (strcmp(testname, "--params") == 0 ||
161 strcmp(testname, "-p") == 0) {
162 printf("%s", server_parameters);
163 exit(EXIT_SUCCESS);
164 }
165
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800166 t = find_test(argv[1]);
167 if (t == NULL) {
168 fprintf(stderr, "unknown test: \"%s\"\n", argv[1]);
Peter Hutterer9715d4d2013-09-11 16:08:47 +1000169 list_tests();
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800170 exit(EXIT_FAILURE);
171 }
172
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100173 int number_passed_in_test = 0, number_skipped_in_test = 0;
174 total += iterate_test(t, &number_passed_in_test, &number_skipped_in_test);
Sam Spilsburyb5021262013-09-13 10:01:20 +0800175 pass += number_passed_in_test;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100176 skip += number_skipped_in_test;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800177 } else {
178 for (t = &__start_test_section; t < &__stop_test_section; t++) {
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100179 int number_passed_in_test = 0, number_skipped_in_test = 0;
180 total += iterate_test(t, &number_passed_in_test, &number_skipped_in_test);
Sam Spilsburyb5021262013-09-13 10:01:20 +0800181 pass += number_passed_in_test;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100182 skip += number_skipped_in_test;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800183 }
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800184 }
185
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100186 fprintf(stderr, "%d tests, %d pass, %d skip, %d fail\n",
187 total, pass, skip, total - pass - skip);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800188
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100189 if (skip == total)
190 return SKIP;
191 else if (pass + skip == total)
192 return EXIT_SUCCESS;
193
194 return EXIT_FAILURE;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800195}