blob: ef45bae7f2ae707a09c65781173f3b55668a1fd8 [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
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080037extern const struct weston_test __start_test_section, __stop_test_section;
38
39static const struct weston_test *
40find_test(const char *name)
41{
42 const struct weston_test *t;
43
44 for (t = &__start_test_section; t < &__stop_test_section; t++)
45 if (strcmp(t->name, name) == 0)
46 return t;
47
48 return NULL;
49}
50
51static void
Sam Spilsburyb5021262013-09-13 10:01:20 +080052run_test(const struct weston_test *t, void *data)
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080053{
Sam Spilsburyb5021262013-09-13 10:01:20 +080054 t->run(data);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -080055 exit(EXIT_SUCCESS);
56}
57
Peter Hutterer534f0a42013-09-11 16:08:30 +100058static void
59list_tests(void)
60{
61 const struct weston_test *t;
62
63 fprintf(stderr, "Available test names:\n");
64 for (t = &__start_test_section; t < &__stop_test_section; t++)
65 fprintf(stderr, " %s\n", t->name);
66}
67
Sam Spilsburyb5021262013-09-13 10:01:20 +080068static int
69exec_and_report_test(const struct weston_test *t, void *test_data, int iteration)
70{
71 int success = 0;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +010072 int skip = 0;
Sam Spilsburyb5021262013-09-13 10:01:20 +080073 int hardfail = 0;
74 siginfo_t info;
75
76 pid_t pid = fork();
77 assert(pid >= 0);
78
79 if (pid == 0)
80 run_test(t, test_data); /* never returns */
81
82 if (waitid(P_ALL, 0, &info, WEXITED)) {
83 fprintf(stderr, "waitid failed: %m\n");
84 abort();
85 }
86
87 if (test_data)
88 fprintf(stderr, "test \"%s/%i\":\t", t->name, iteration);
89 else
90 fprintf(stderr, "test \"%s\":\t", t->name);
91
92 switch (info.si_code) {
93 case CLD_EXITED:
94 fprintf(stderr, "exit status %d", info.si_status);
95 if (info.si_status == EXIT_SUCCESS)
96 success = 1;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +010097 else if (info.si_status == SKIP)
98 skip = 1;
Sam Spilsburyb5021262013-09-13 10:01:20 +080099 break;
100 case CLD_KILLED:
101 case CLD_DUMPED:
102 fprintf(stderr, "signal %d", info.si_status);
103 if (info.si_status != SIGABRT)
104 hardfail = 1;
105 break;
106 }
107
108 if (t->must_fail)
109 success = !success;
110
111 if (success && !hardfail) {
112 fprintf(stderr, ", pass.\n");
113 return 1;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100114 } else if (skip) {
115 fprintf(stderr, ", skip.\n");
116 return SKIP;
117 } else {
Sam Spilsburyb5021262013-09-13 10:01:20 +0800118 fprintf(stderr, ", fail.\n");
119 return 0;
120 }
121}
122
123/* Returns number of tests and number of pass / fail in param args */
124static int
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100125iterate_test(const struct weston_test *t, int *passed, int *skipped)
Sam Spilsburyb5021262013-09-13 10:01:20 +0800126{
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100127 int ret, i;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800128 void *current_test_data = (void *) t->table_data;
129 for (i = 0; i < t->n_elements; ++i, current_test_data += t->element_size)
130 {
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100131 ret = exec_and_report_test(t, current_test_data, i);
132 if (ret == SKIP)
133 ++(*skipped);
134 else if (ret)
Sam Spilsburyb5021262013-09-13 10:01:20 +0800135 ++(*passed);
136 }
137
138 return t->n_elements;
139}
140
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800141int main(int argc, char *argv[])
142{
143 const struct weston_test *t;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800144 int total = 0;
145 int pass = 0;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100146 int skip = 0;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800147
148 if (argc == 2) {
Peter Hutterer534f0a42013-09-11 16:08:30 +1000149 const char *testname = argv[1];
150 if (strcmp(testname, "--help") == 0 ||
151 strcmp(testname, "-h") == 0) {
152 fprintf(stderr, "Usage: %s [test-name]\n", program_invocation_short_name);
153 list_tests();
154 exit(EXIT_SUCCESS);
155 }
156
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800157 t = find_test(argv[1]);
158 if (t == NULL) {
159 fprintf(stderr, "unknown test: \"%s\"\n", argv[1]);
Peter Hutterer9715d4d2013-09-11 16:08:47 +1000160 list_tests();
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800161 exit(EXIT_FAILURE);
162 }
163
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100164 int number_passed_in_test = 0, number_skipped_in_test = 0;
165 total += iterate_test(t, &number_passed_in_test, &number_skipped_in_test);
Sam Spilsburyb5021262013-09-13 10:01:20 +0800166 pass += number_passed_in_test;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100167 skip += number_skipped_in_test;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800168 } else {
169 for (t = &__start_test_section; t < &__stop_test_section; t++) {
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100170 int number_passed_in_test = 0, number_skipped_in_test = 0;
171 total += iterate_test(t, &number_passed_in_test, &number_skipped_in_test);
Sam Spilsburyb5021262013-09-13 10:01:20 +0800172 pass += number_passed_in_test;
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100173 skip += number_skipped_in_test;
Sam Spilsburyb5021262013-09-13 10:01:20 +0800174 }
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800175 }
176
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100177 fprintf(stderr, "%d tests, %d pass, %d skip, %d fail\n",
178 total, pass, skip, total - pass - skip);
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800179
Emilio Pozuelo Monfortdae8a4b2014-02-07 09:34:48 +0100180 if (skip == total)
181 return SKIP;
182 else if (pass + skip == total)
183 return EXIT_SUCCESS;
184
185 return EXIT_FAILURE;
U. Artie Eoff1ba9b382012-12-07 13:50:32 -0800186}