1 /*
2 * Copyright © 2012 Collabora, Ltd.
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 <stdlib.h>
27 #include <assert.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/mman.h>
31 #include <signal.h>
32 #include <unistd.h>
33
34 #include "test-runner.h"
35 #include "wayland-util.h"
36 #include "wayland-private.h"
37
38 #include "test-compositor.h"
39
40 extern int fd_leak_check_enabled;
41
TEST(empty)42 TEST(empty)
43 {
44 }
45
TEST(exit_success)46 TEST(exit_success)
47 {
48 exit(EXIT_SUCCESS);
49 }
50
FAIL_TEST(exit_failure)51 FAIL_TEST(exit_failure)
52 {
53 exit(EXIT_FAILURE);
54 }
55
FAIL_TEST(fail_abort)56 FAIL_TEST(fail_abort)
57 {
58 test_disable_coredumps();
59 abort();
60 }
61
FAIL_TEST(fail_wl_abort)62 FAIL_TEST(fail_wl_abort)
63 {
64 test_disable_coredumps();
65 wl_abort("Abort the program\n");
66 }
67
FAIL_TEST(fail_kill)68 FAIL_TEST(fail_kill)
69 {
70 kill(getpid(), SIGTERM);
71 }
72
FAIL_TEST(fail_segv)73 FAIL_TEST(fail_segv)
74 {
75 char * volatile *null = 0;
76
77 test_disable_coredumps();
78 *null = "Goodbye, world";
79 }
80
FAIL_TEST(sanity_assert)81 FAIL_TEST(sanity_assert)
82 {
83 test_disable_coredumps();
84 /* must fail */
85 assert(0);
86 }
87
FAIL_TEST(sanity_fd_leak)88 FAIL_TEST(sanity_fd_leak)
89 {
90 int fd[2];
91
92 assert(fd_leak_check_enabled);
93
94 /* leak 2 file descriptors */
95 if (pipe(fd) < 0)
96 exit(EXIT_SUCCESS); /* failed to fail */
97
98 test_disable_coredumps();
99 }
100
FAIL_TEST(sanity_fd_leak_exec)101 FAIL_TEST(sanity_fd_leak_exec)
102 {
103 int fd[2];
104 int nr_fds = count_open_fds();
105
106 /* leak 2 file descriptors */
107 if (pipe(fd) < 0)
108 exit(EXIT_SUCCESS); /* failed to fail */
109
110 test_disable_coredumps();
111 exec_fd_leak_check(nr_fds);
112 }
113
TEST(sanity_fd_exec)114 TEST(sanity_fd_exec)
115 {
116 int fd[2];
117 int nr_fds = count_open_fds();
118
119 /* create 2 file descriptors, that should pass over exec */
120 assert(pipe(fd) >= 0);
121
122 exec_fd_leak_check(nr_fds + 2);
123 }
124
125 static void
sanity_fd_no_leak(void)126 sanity_fd_no_leak(void)
127 {
128 int fd[2];
129
130 assert(fd_leak_check_enabled);
131
132 /* leak 2 file descriptors */
133 if (pipe(fd) < 0)
134 exit(EXIT_SUCCESS); /* failed to fail */
135
136 close(fd[0]);
137 close(fd[1]);
138 }
139
140 static void
sanity_client_no_leak(void)141 sanity_client_no_leak(void)
142 {
143 struct wl_display *display = wl_display_connect(NULL);
144 assert(display);
145
146 wl_display_disconnect(display);
147 }
148
TEST(tc_client_no_fd_leaks)149 TEST(tc_client_no_fd_leaks)
150 {
151 struct display *d = display_create();
152
153 /* Client which does not consume the WAYLAND_DISPLAY socket. */
154 client_create_noarg(d, sanity_fd_no_leak);
155 display_run(d);
156
157 /* Client which does consume the WAYLAND_DISPLAY socket. */
158 client_create_noarg(d, sanity_client_no_leak);
159 display_run(d);
160
161 display_destroy(d);
162 }
163
FAIL_TEST(tc_client_fd_leaks)164 FAIL_TEST(tc_client_fd_leaks)
165 {
166 struct display *d = display_create();
167
168 client_create_noarg(d, sanity_fd_leak);
169 display_run(d);
170
171 test_disable_coredumps();
172 display_destroy(d);
173 }
174
FAIL_TEST(tc_client_fd_leaks_exec)175 FAIL_TEST(tc_client_fd_leaks_exec)
176 {
177 struct display *d = display_create();
178
179 client_create_noarg(d, sanity_fd_leak_exec);
180 display_run(d);
181
182 test_disable_coredumps();
183 display_destroy(d);
184 }
185
186 static char *
map_file(int fd,size_t * len)187 map_file(int fd, size_t *len)
188 {
189 char *data;
190
191 *len = lseek(fd, 0, SEEK_END);
192 data = mmap(0, *len, PROT_READ, MAP_PRIVATE, fd, 0);
193 assert(data != MAP_FAILED && "Failed to mmap file");
194
195 return data;
196 }
197
198 static void
sanity_client_log(void)199 sanity_client_log(void)
200 {
201 char *log;
202 size_t log_len;
203 char *wayland_socket = strdup(getenv("WAYLAND_SOCKET"));
204 char *xdg_runtime_dir = strdup(getenv("XDG_RUNTIME_DIR"));
205
206 unsetenv("WAYLAND_SOCKET");
207 unsetenv("XDG_RUNTIME_DIR");
208
209 /* Try to connect to the default wayland display, which should fail since
210 * we have neither WAYLAND_SOCKET nor XDG_RUNTIME_DIR. */
211 assert(!wl_display_connect(NULL));
212
213 /* Check that the client log contains some mention of XDG_RUNTIME_DIR. */
214 log = map_file(client_log_fd, &log_len);
215 assert(strstr(log, "XDG_RUNTIME_DIR"));
216 munmap(log, log_len);
217
218 /* Reset the environment variables we unset for the test. The test harness
219 * leak checker cares about the value of WAYLAND_SOCKET during teardown for
220 * correct fd accounting. */
221 setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 0);
222 setenv("WAYLAND_SOCKET", wayland_socket, 0);
223 free(xdg_runtime_dir);
224 free(wayland_socket);
225 }
226
TEST(tc_client_log)227 TEST(tc_client_log)
228 {
229 struct display *d = display_create();
230 struct client_info *ci;
231 char *log;
232 size_t log_len;
233
234 ci = client_create_noarg(d, sanity_client_log);
235 display_run(d);
236
237 /* Check that the client log contains some mention of XDG_RUNTIME_DIR. */
238 log = map_file(ci->log_fd, &log_len);
239 assert(strstr(log, "XDG_RUNTIME_DIR"));
240 munmap(log, log_len);
241
242 display_destroy(d);
243 }
244
FAIL_TEST(timeout_tst)245 FAIL_TEST(timeout_tst)
246 {
247 test_set_timeout(1);
248 test_disable_coredumps();
249 /* test should reach timeout */
250 test_sleep(2);
251 }
252
TEST(timeout2_tst)253 TEST(timeout2_tst)
254 {
255 /* the test should end before reaching timeout,
256 * thus it should pass */
257 test_set_timeout(1);
258 /* 200 000 microsec = 0.2 sec */
259 test_usleep(200000);
260 }
261
FAIL_TEST(timeout_reset_tst)262 FAIL_TEST(timeout_reset_tst)
263 {
264 test_set_timeout(5);
265 test_set_timeout(10);
266 test_set_timeout(1);
267
268 test_disable_coredumps();
269 /* test should fail on timeout */
270 test_sleep(2);
271 }
272
TEST(timeout_turnoff)273 TEST(timeout_turnoff)
274 {
275 test_set_timeout(1);
276 test_set_timeout(0);
277
278 test_usleep(2);
279 }
280
281 /* test timeouts with test-compositor */
FAIL_TEST(tc_timeout_tst)282 FAIL_TEST(tc_timeout_tst)
283 {
284 struct display *d = display_create();
285 client_create_noarg(d, timeout_tst);
286 display_run(d);
287 test_disable_coredumps();
288 display_destroy(d);
289 }
290
FAIL_TEST(tc_timeout2_tst)291 FAIL_TEST(tc_timeout2_tst)
292 {
293 struct display *d = display_create();
294 client_create_noarg(d, timeout_reset_tst);
295 display_run(d);
296 test_disable_coredumps();
297 display_destroy(d);
298 }
299
TEST(tc_timeout3_tst)300 TEST(tc_timeout3_tst)
301 {
302 struct display *d = display_create();
303
304 client_create_noarg(d, timeout2_tst);
305 display_run(d);
306
307 client_create_noarg(d, timeout_turnoff);
308 display_run(d);
309
310 display_destroy(d);
311 }
312