client: Fix segmentation fault in the case weston-nested
[platform/upstream/weston.git] / tests / weston-test-runner.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  *
4  * 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:
11  *
12  * 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.
24  */
25
26 #include "config.h"
27
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <sys/wait.h>
32 #include <string.h>
33 #include <assert.h>
34 #include <errno.h>
35 #include <signal.h>
36
37 #include "weston-test-runner.h"
38
39 #define SKIP 77
40
41 char __attribute__((weak)) *server_parameters="";
42
43 extern const struct weston_test __start_test_section, __stop_test_section;
44
45 static const char *test_name_;
46
47 const char *
48 get_test_name(void)
49 {
50         return test_name_;
51 }
52
53 static const struct weston_test *
54 find_test(const char *name)
55 {
56         const struct weston_test *t;
57
58         for (t = &__start_test_section; t < &__stop_test_section; t++)
59                 if (strcmp(t->name, name) == 0)
60                         return t;
61
62         return NULL;
63 }
64
65 static void
66 run_test(const struct weston_test *t, void *data, int iteration)
67 {
68         char str[512];
69
70         if (data) {
71                 snprintf(str, sizeof(str), "%s[%d]", t->name, iteration);
72                 test_name_ = str;
73         } else {
74                 test_name_ = t->name;
75         }
76
77         t->run(data);
78         exit(EXIT_SUCCESS);
79 }
80
81 static void
82 list_tests(void)
83 {
84         const struct weston_test *t;
85
86         fprintf(stderr, "Available test names:\n");
87         for (t = &__start_test_section; t < &__stop_test_section; t++)
88                 fprintf(stderr, "       %s\n", t->name);
89 }
90
91 /* iteration is valid only if test_data is not NULL */
92 static int
93 exec_and_report_test(const struct weston_test *t, void *test_data, int iteration)
94 {
95         int success = 0;
96         int skip = 0;
97         int hardfail = 0;
98         siginfo_t info;
99
100         pid_t pid = fork();
101         assert(pid >= 0);
102
103         if (pid == 0)
104                 run_test(t, test_data, iteration); /* never returns */
105
106         if (waitid(P_ALL, 0, &info, WEXITED)) {
107                 fprintf(stderr, "waitid failed: %m\n");
108                 abort();
109         }
110
111         if (test_data)
112                 fprintf(stderr, "test \"%s/%i\":\t", t->name, iteration);
113         else
114                 fprintf(stderr, "test \"%s\":\t", t->name);
115
116         switch (info.si_code) {
117         case CLD_EXITED:
118                 fprintf(stderr, "exit status %d", info.si_status);
119                 if (info.si_status == EXIT_SUCCESS)
120                         success = 1;
121                 else if (info.si_status == SKIP)
122                         skip = 1;
123                 break;
124         case CLD_KILLED:
125         case CLD_DUMPED:
126                 fprintf(stderr, "signal %d", info.si_status);
127                 if (info.si_status != SIGABRT)
128                         hardfail = 1;
129                 break;
130         }
131
132         if (t->must_fail)
133                 success = !success;
134
135         if (success && !hardfail) {
136                 fprintf(stderr, ", pass.\n");
137                 return 1;
138         } else if (skip) {
139                 fprintf(stderr, ", skip.\n");
140                 return SKIP;
141         } else {
142                 fprintf(stderr, ", fail.\n");
143                 return 0;
144         }
145 }
146
147 /* Returns number of tests and number of pass / fail in param args.
148  * Even non-iterated tests go through here, they simply have n_elements = 1 and
149  * table_data = NULL.
150  */
151 static int
152 iterate_test(const struct weston_test *t, int *passed, int *skipped)
153 {
154         int ret, i;
155         void *current_test_data = (void *) t->table_data;
156         for (i = 0; i < t->n_elements; ++i, current_test_data += t->element_size)
157         {
158                 ret = exec_and_report_test(t, current_test_data, i);
159                 if (ret == SKIP)
160                         ++(*skipped);
161                 else if (ret)
162                         ++(*passed);
163         }
164
165         return t->n_elements;
166 }
167
168 int main(int argc, char *argv[])
169 {
170         const struct weston_test *t;
171         int total = 0;
172         int pass = 0;
173         int skip = 0;
174
175         if (argc == 2) {
176                 const char *testname = argv[1];
177                 if (strcmp(testname, "--help") == 0 ||
178                     strcmp(testname, "-h") == 0) {
179                         fprintf(stderr, "Usage: %s [test-name]\n", program_invocation_short_name);
180                         list_tests();
181                         exit(EXIT_SUCCESS);
182                 }
183
184                 if (strcmp(testname, "--params") == 0 ||
185                     strcmp(testname, "-p") == 0) {
186                         printf("%s", server_parameters);
187                         exit(EXIT_SUCCESS);
188                 }
189
190                 t = find_test(argv[1]);
191                 if (t == NULL) {
192                         fprintf(stderr, "unknown test: \"%s\"\n", argv[1]);
193                         list_tests();
194                         exit(EXIT_FAILURE);
195                 }
196
197                 int number_passed_in_test = 0, number_skipped_in_test = 0;
198                 total += iterate_test(t, &number_passed_in_test, &number_skipped_in_test);
199                 pass += number_passed_in_test;
200                 skip += number_skipped_in_test;
201         } else {
202                 for (t = &__start_test_section; t < &__stop_test_section; t++) {
203                         int number_passed_in_test = 0, number_skipped_in_test = 0;
204                         total += iterate_test(t, &number_passed_in_test, &number_skipped_in_test);
205                         pass += number_passed_in_test;
206                         skip += number_skipped_in_test;
207                 }
208         }
209
210         fprintf(stderr, "%d tests, %d pass, %d skip, %d fail\n",
211                 total, pass, skip, total - pass - skip);
212
213         if (skip == total)
214                 return SKIP;
215         else if (pass + skip == total)
216                 return EXIT_SUCCESS;
217
218         return EXIT_FAILURE;
219 }