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 | #include "../config.h" |
26 | |
27 | #define _GNU_SOURCE |
28 | |
29 | #include <unistd.h> |
30 | #include <stdio.h> |
31 | #include <stdlib.h> |
32 | #include <sys/types.h> |
33 | #include <sys/wait.h> |
34 | #include <sys/stat.h> |
35 | #include <string.h> |
36 | #include <assert.h> |
37 | #include <dlfcn.h> |
38 | #include <errno.h> |
39 | #include <limits.h> |
40 | #include <signal.h> |
41 | #include <sys/ptrace.h> |
42 | #ifdef HAVE_SYS_PROCCTL_H |
43 | #include <sys/procctl.h> |
44 | #elif defined(HAVE_SYS_PRCTL_H) |
45 | #include <sys/prctl.h> |
46 | #ifndef PR_SET_PTRACER |
47 | # define PR_SET_PTRACER 0x59616d61 |
48 | #endif |
49 | #endif |
50 | |
51 | #include "test-runner.h" |
52 | |
53 | /* when set to 1, check if tests are not leaking opened files. |
54 | * It is turned on by default. It can be turned off by |
55 | * WAYLAND_TEST_NO_LEAK_CHECK environment variable. */ |
56 | int fd_leak_check_enabled; |
57 | |
58 | /* when this var is set to 0, every call to test_set_timeout() is |
59 | * suppressed - handy when debugging the test. Can be set by |
60 | * WAYLAND_TEST_NO_TIMEOUTS environment variable. */ |
61 | static int timeouts_enabled = 1; |
62 | |
63 | /* set to one if the output goes to the terminal */ |
64 | static int is_atty = 0; |
65 | |
66 | extern const struct test __start_test_section, __stop_test_section; |
67 | |
68 | static const struct test * |
69 | find_test(const char *name) |
70 | { |
71 | const struct test *t; |
72 | |
73 | for (t = &__start_test_section; t < &__stop_test_section; t++) |
74 | if (strcmp(s1: t->name, s2: name) == 0) |
75 | return t; |
76 | |
77 | return NULL; |
78 | } |
79 | |
80 | static void |
81 | usage(const char *name, int status) |
82 | { |
83 | const struct test *t; |
84 | |
85 | fprintf(stderr, format: "Usage: %s [TEST]\n\n" |
86 | "With no arguments, run all test. Specify test case to run\n" |
87 | "only that test without forking. Available tests:\n\n" , |
88 | name); |
89 | |
90 | for (t = &__start_test_section; t < &__stop_test_section; t++) |
91 | fprintf(stderr, format: " %s\n" , t->name); |
92 | |
93 | fprintf(stderr, format: "\n" ); |
94 | |
95 | exit(status: status); |
96 | } |
97 | |
98 | void |
99 | test_set_timeout(unsigned int to) |
100 | { |
101 | int re; |
102 | |
103 | if (!timeouts_enabled) { |
104 | fprintf(stderr, format: "Timeouts suppressed.\n" ); |
105 | return; |
106 | } |
107 | |
108 | re = alarm(seconds: to); |
109 | fprintf(stderr, format: "Timeout was %sset" , re ? "re-" : "" ); |
110 | |
111 | if (to != 0) |
112 | fprintf(stderr, format: " to %d second%s from now.\n" , |
113 | to, to > 1 ? "s" : "" ); |
114 | else |
115 | fprintf(stderr, format: " off.\n" ); |
116 | } |
117 | |
118 | static void |
119 | sigalrm_handler(int signum) |
120 | { |
121 | fprintf(stderr, format: "Test timed out.\n" ); |
122 | abort(); |
123 | } |
124 | |
125 | void |
126 | check_fd_leaks(int supposed_fds) |
127 | { |
128 | int num_fds; |
129 | |
130 | if (fd_leak_check_enabled) { |
131 | num_fds = count_open_fds(); |
132 | if (supposed_fds != num_fds) { |
133 | fprintf(stderr, format: "fd leak detected in test. " |
134 | "Opened %d files, unclosed %d\n" , num_fds, |
135 | num_fds - supposed_fds); |
136 | abort(); |
137 | } |
138 | } else { |
139 | fprintf(stderr, format: "FD leak checks disabled\n" ); |
140 | } |
141 | } |
142 | |
143 | static void |
144 | run_test(const struct test *t) |
145 | { |
146 | int cur_fds; |
147 | struct sigaction sa; |
148 | |
149 | if (timeouts_enabled) { |
150 | sa.sa_handler = sigalrm_handler; |
151 | sa.sa_flags = 0; |
152 | sigemptyset(set: &sa.sa_mask); |
153 | assert(sigaction(SIGALRM, &sa, NULL) == 0); |
154 | } |
155 | |
156 | //cur_alloc = get_current_alloc_num(); |
157 | cur_fds = count_open_fds(); |
158 | |
159 | t->run(); |
160 | |
161 | /* turn off timeout (if any) after test completion */ |
162 | if (timeouts_enabled) |
163 | alarm(seconds: 0); |
164 | |
165 | check_fd_leaks(supposed_fds: cur_fds); |
166 | |
167 | exit(EXIT_SUCCESS); |
168 | } |
169 | |
170 | #ifndef PATH_MAX |
171 | #define PATH_MAX 256 |
172 | #endif |
173 | |
174 | static void |
175 | set_xdg_runtime_dir(void) |
176 | { |
177 | char xdg_runtime_dir[PATH_MAX]; |
178 | const char *xrd_env; |
179 | |
180 | xrd_env = getenv(name: "XDG_RUNTIME_DIR" ); |
181 | /* if XDG_RUNTIME_DIR is not set in environ, fallback to /tmp */ |
182 | assert((snprintf(xdg_runtime_dir, PATH_MAX, "%s/wayland-tests-XXXXXX" , |
183 | xrd_env ? xrd_env : "/tmp" ) < PATH_MAX) |
184 | && "test error: XDG_RUNTIME_DIR too long" ); |
185 | |
186 | assert(mkdtemp(xdg_runtime_dir) && "test error: mkdtemp failed" ); |
187 | if (mkdir(path: xdg_runtime_dir, mode: 0700) == -1) |
188 | if (errno != EEXIST) { |
189 | perror(s: "Creating XDG_RUNTIME_DIR" ); |
190 | abort(); |
191 | } |
192 | |
193 | if (setenv(name: "XDG_RUNTIME_DIR" , value: xdg_runtime_dir, replace: 1) == -1) { |
194 | perror(s: "Setting XDG_RUNTIME_DIR" ); |
195 | abort(); |
196 | } |
197 | } |
198 | |
199 | static void |
200 | rmdir_xdg_runtime_dir(void) |
201 | { |
202 | const char *xrd_env = getenv(name: "XDG_RUNTIME_DIR" ); |
203 | assert(xrd_env && "No XDG_RUNTIME_DIR set" ); |
204 | |
205 | /* rmdir may fail if some test didn't do clean up */ |
206 | if (rmdir(path: xrd_env) == -1) |
207 | perror(s: "Cleaning XDG_RUNTIME_DIR" ); |
208 | } |
209 | |
210 | #define RED "\033[31m" |
211 | #define GREEN "\033[32m" |
212 | |
213 | static void |
214 | stderr_set_color(const char *color) |
215 | { |
216 | /* use colors only when the output is connected to |
217 | * the terminal */ |
218 | if (is_atty) |
219 | fprintf(stderr, format: "%s" , color); |
220 | } |
221 | |
222 | static void |
223 | stderr_reset_color(void) |
224 | { |
225 | if (is_atty) |
226 | fprintf(stderr, format: "\033[0m" ); |
227 | } |
228 | |
229 | /* this function is taken from libinput/test/litest.c |
230 | * (rev 028513a0a723e97941c39) |
231 | * |
232 | * Returns: 1 if a debugger is confirmed present; 0 if no debugger is |
233 | * present or if it can't be determined. |
234 | */ |
235 | #if defined(HAVE_SYS_PROCCTL_H) && defined(PROC_TRACE_STATUS) |
236 | static int |
237 | is_debugger_attached(void) |
238 | { |
239 | int rc; |
240 | int status; |
241 | rc = procctl(P_PID, getpid(), PROC_TRACE_STATUS, &status); |
242 | if (rc == -1) { |
243 | perror("procctl" ); |
244 | return 0; |
245 | } |
246 | /* -1=tracing disabled, 0=no debugger attached, >0=pid of debugger. */ |
247 | return status > 0; |
248 | } |
249 | #elif defined(HAVE_SYS_PRCTL_H) |
250 | static int |
251 | is_debugger_attached(void) |
252 | { |
253 | int status; |
254 | int rc; |
255 | pid_t pid; |
256 | int pipefd[2]; |
257 | |
258 | if (pipe(pipedes: pipefd) == -1) { |
259 | perror(s: "pipe" ); |
260 | return 0; |
261 | } |
262 | |
263 | pid = fork(); |
264 | if (pid == -1) { |
265 | perror(s: "fork" ); |
266 | close(fd: pipefd[0]); |
267 | close(fd: pipefd[1]); |
268 | return 0; |
269 | } else if (pid == 0) { |
270 | char buf; |
271 | pid_t ppid = getppid(); |
272 | |
273 | /* Wait until parent is ready */ |
274 | close(fd: pipefd[1]); /* Close unused write end */ |
275 | read(fd: pipefd[0], buf: &buf, nbytes: 1); |
276 | close(fd: pipefd[0]); |
277 | if (buf == '-') |
278 | _exit(status: 1); |
279 | if (ptrace(request: PTRACE_ATTACH, ppid, NULL, NULL) != 0) |
280 | _exit(status: 1); |
281 | if (!waitpid(pid: -1, NULL, options: 0)) |
282 | _exit(status: 1); |
283 | ptrace(request: PTRACE_CONT, NULL, NULL); |
284 | ptrace(request: PTRACE_DETACH, ppid, NULL, NULL); |
285 | _exit(status: 0); |
286 | } else { |
287 | close(fd: pipefd[0]); |
288 | |
289 | /* Enable child to ptrace the parent process */ |
290 | rc = prctl(PR_SET_PTRACER, pid); |
291 | if (rc != 0 && errno != EINVAL) { |
292 | /* An error prevents us from telling if a debugger is attached. |
293 | * Instead of propagating the error, assume no debugger present. |
294 | * But note the error to the log as a clue for troubleshooting. |
295 | * Then flag the error state to the client by sending '-'. |
296 | */ |
297 | perror(s: "prctl" ); |
298 | write(fd: pipefd[1], buf: "-" , n: 1); |
299 | } else { |
300 | /* Signal to client that parent is ready by passing '+' */ |
301 | write(fd: pipefd[1], buf: "+" , n: 1); |
302 | } |
303 | close(fd: pipefd[1]); |
304 | |
305 | waitpid(pid: pid, stat_loc: &status, options: 0); |
306 | rc = WEXITSTATUS(status); |
307 | } |
308 | |
309 | return rc; |
310 | } |
311 | #endif |
312 | |
313 | int main(int argc, char *argv[]) |
314 | { |
315 | const struct test *t; |
316 | pid_t pid; |
317 | int total, pass; |
318 | siginfo_t info; |
319 | |
320 | if (isatty(fd: fileno(stderr))) |
321 | is_atty = 1; |
322 | |
323 | if (is_debugger_attached()) { |
324 | fd_leak_check_enabled = 0; |
325 | timeouts_enabled = 0; |
326 | } else { |
327 | fd_leak_check_enabled = !getenv(name: "WAYLAND_TEST_NO_LEAK_CHECK" ); |
328 | timeouts_enabled = !getenv(name: "WAYLAND_TEST_NO_TIMEOUTS" ); |
329 | } |
330 | |
331 | if (argc == 2 && strcmp(s1: argv[1], s2: "--help" ) == 0) |
332 | usage(name: argv[0], EXIT_SUCCESS); |
333 | |
334 | if (argc == 2) { |
335 | t = find_test(name: argv[1]); |
336 | if (t == NULL) { |
337 | fprintf(stderr, format: "unknown test: \"%s\"\n" , argv[1]); |
338 | usage(name: argv[0], EXIT_FAILURE); |
339 | } |
340 | |
341 | set_xdg_runtime_dir(); |
342 | /* run_test calls exit() */ |
343 | assert(atexit(rmdir_xdg_runtime_dir) == 0); |
344 | |
345 | run_test(t); |
346 | } |
347 | |
348 | /* set our own XDG_RUNTIME_DIR */ |
349 | set_xdg_runtime_dir(); |
350 | |
351 | pass = 0; |
352 | for (t = &__start_test_section; t < &__stop_test_section; t++) { |
353 | int success = 0; |
354 | |
355 | pid = fork(); |
356 | assert(pid >= 0); |
357 | |
358 | if (pid == 0) |
359 | run_test(t); /* never returns */ |
360 | |
361 | if (waitid(idtype: P_PID, id: pid, infop: &info, WEXITED)) { |
362 | stderr_set_color(RED); |
363 | fprintf(stderr, format: "waitid failed: %s\n" , |
364 | strerror(errno)); |
365 | stderr_reset_color(); |
366 | |
367 | abort(); |
368 | } |
369 | |
370 | switch (info.si_code) { |
371 | case CLD_EXITED: |
372 | if (info.si_status == EXIT_SUCCESS) |
373 | success = !t->must_fail; |
374 | else |
375 | success = t->must_fail; |
376 | |
377 | stderr_set_color(color: success ? GREEN : RED); |
378 | fprintf(stderr, format: "test \"%s\":\texit status %d" , |
379 | t->name, info.si_status); |
380 | |
381 | break; |
382 | case CLD_KILLED: |
383 | case CLD_DUMPED: |
384 | if (t->must_fail) |
385 | success = 1; |
386 | |
387 | stderr_set_color(color: success ? GREEN : RED); |
388 | fprintf(stderr, format: "test \"%s\":\tsignal %d" , |
389 | t->name, info.si_status); |
390 | |
391 | break; |
392 | } |
393 | |
394 | if (success) { |
395 | pass++; |
396 | fprintf(stderr, format: ", pass.\n" ); |
397 | } else |
398 | fprintf(stderr, format: ", fail.\n" ); |
399 | |
400 | stderr_reset_color(); |
401 | |
402 | /* print separator line */ |
403 | fprintf(stderr, format: "----------------------------------------\n" ); |
404 | } |
405 | |
406 | total = &__stop_test_section - &__start_test_section; |
407 | fprintf(stderr, format: "%d tests, %d pass, %d fail\n" , |
408 | total, pass, total - pass); |
409 | |
410 | /* cleaning */ |
411 | rmdir_xdg_runtime_dir(); |
412 | |
413 | return pass == total ? EXIT_SUCCESS : EXIT_FAILURE; |
414 | } |
415 | |