1*84e872a0SLloyd Pique /*
2*84e872a0SLloyd Pique * Copyright © 2012 Collabora, Ltd.
3*84e872a0SLloyd Pique *
4*84e872a0SLloyd Pique * Permission is hereby granted, free of charge, to any person obtaining
5*84e872a0SLloyd Pique * a copy of this software and associated documentation files (the
6*84e872a0SLloyd Pique * "Software"), to deal in the Software without restriction, including
7*84e872a0SLloyd Pique * without limitation the rights to use, copy, modify, merge, publish,
8*84e872a0SLloyd Pique * distribute, sublicense, and/or sell copies of the Software, and to
9*84e872a0SLloyd Pique * permit persons to whom the Software is furnished to do so, subject to
10*84e872a0SLloyd Pique * the following conditions:
11*84e872a0SLloyd Pique *
12*84e872a0SLloyd Pique * The above copyright notice and this permission notice (including the
13*84e872a0SLloyd Pique * next paragraph) shall be included in all copies or substantial
14*84e872a0SLloyd Pique * portions of the Software.
15*84e872a0SLloyd Pique *
16*84e872a0SLloyd Pique * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17*84e872a0SLloyd Pique * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18*84e872a0SLloyd Pique * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19*84e872a0SLloyd Pique * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20*84e872a0SLloyd Pique * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21*84e872a0SLloyd Pique * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22*84e872a0SLloyd Pique * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23*84e872a0SLloyd Pique * SOFTWARE.
24*84e872a0SLloyd Pique */
25*84e872a0SLloyd Pique
26*84e872a0SLloyd Pique #include <stdlib.h>
27*84e872a0SLloyd Pique #include <assert.h>
28*84e872a0SLloyd Pique #include <string.h>
29*84e872a0SLloyd Pique #include <sys/types.h>
30*84e872a0SLloyd Pique #include <sys/mman.h>
31*84e872a0SLloyd Pique #include <signal.h>
32*84e872a0SLloyd Pique #include <unistd.h>
33*84e872a0SLloyd Pique
34*84e872a0SLloyd Pique #include "test-runner.h"
35*84e872a0SLloyd Pique #include "wayland-util.h"
36*84e872a0SLloyd Pique #include "wayland-private.h"
37*84e872a0SLloyd Pique
38*84e872a0SLloyd Pique #include "test-compositor.h"
39*84e872a0SLloyd Pique
40*84e872a0SLloyd Pique extern int fd_leak_check_enabled;
41*84e872a0SLloyd Pique
TEST(empty)42*84e872a0SLloyd Pique TEST(empty)
43*84e872a0SLloyd Pique {
44*84e872a0SLloyd Pique }
45*84e872a0SLloyd Pique
TEST(exit_success)46*84e872a0SLloyd Pique TEST(exit_success)
47*84e872a0SLloyd Pique {
48*84e872a0SLloyd Pique exit(EXIT_SUCCESS);
49*84e872a0SLloyd Pique }
50*84e872a0SLloyd Pique
FAIL_TEST(exit_failure)51*84e872a0SLloyd Pique FAIL_TEST(exit_failure)
52*84e872a0SLloyd Pique {
53*84e872a0SLloyd Pique exit(EXIT_FAILURE);
54*84e872a0SLloyd Pique }
55*84e872a0SLloyd Pique
FAIL_TEST(fail_abort)56*84e872a0SLloyd Pique FAIL_TEST(fail_abort)
57*84e872a0SLloyd Pique {
58*84e872a0SLloyd Pique test_disable_coredumps();
59*84e872a0SLloyd Pique abort();
60*84e872a0SLloyd Pique }
61*84e872a0SLloyd Pique
FAIL_TEST(fail_wl_abort)62*84e872a0SLloyd Pique FAIL_TEST(fail_wl_abort)
63*84e872a0SLloyd Pique {
64*84e872a0SLloyd Pique test_disable_coredumps();
65*84e872a0SLloyd Pique wl_abort("Abort the program\n");
66*84e872a0SLloyd Pique }
67*84e872a0SLloyd Pique
FAIL_TEST(fail_kill)68*84e872a0SLloyd Pique FAIL_TEST(fail_kill)
69*84e872a0SLloyd Pique {
70*84e872a0SLloyd Pique kill(getpid(), SIGTERM);
71*84e872a0SLloyd Pique }
72*84e872a0SLloyd Pique
FAIL_TEST(fail_segv)73*84e872a0SLloyd Pique FAIL_TEST(fail_segv)
74*84e872a0SLloyd Pique {
75*84e872a0SLloyd Pique char * volatile *null = 0;
76*84e872a0SLloyd Pique
77*84e872a0SLloyd Pique test_disable_coredumps();
78*84e872a0SLloyd Pique *null = "Goodbye, world";
79*84e872a0SLloyd Pique }
80*84e872a0SLloyd Pique
FAIL_TEST(sanity_assert)81*84e872a0SLloyd Pique FAIL_TEST(sanity_assert)
82*84e872a0SLloyd Pique {
83*84e872a0SLloyd Pique test_disable_coredumps();
84*84e872a0SLloyd Pique /* must fail */
85*84e872a0SLloyd Pique assert(0);
86*84e872a0SLloyd Pique }
87*84e872a0SLloyd Pique
FAIL_TEST(sanity_fd_leak)88*84e872a0SLloyd Pique FAIL_TEST(sanity_fd_leak)
89*84e872a0SLloyd Pique {
90*84e872a0SLloyd Pique int fd[2];
91*84e872a0SLloyd Pique
92*84e872a0SLloyd Pique assert(fd_leak_check_enabled);
93*84e872a0SLloyd Pique
94*84e872a0SLloyd Pique /* leak 2 file descriptors */
95*84e872a0SLloyd Pique if (pipe(fd) < 0)
96*84e872a0SLloyd Pique exit(EXIT_SUCCESS); /* failed to fail */
97*84e872a0SLloyd Pique
98*84e872a0SLloyd Pique test_disable_coredumps();
99*84e872a0SLloyd Pique }
100*84e872a0SLloyd Pique
FAIL_TEST(sanity_fd_leak_exec)101*84e872a0SLloyd Pique FAIL_TEST(sanity_fd_leak_exec)
102*84e872a0SLloyd Pique {
103*84e872a0SLloyd Pique int fd[2];
104*84e872a0SLloyd Pique int nr_fds = count_open_fds();
105*84e872a0SLloyd Pique
106*84e872a0SLloyd Pique /* leak 2 file descriptors */
107*84e872a0SLloyd Pique if (pipe(fd) < 0)
108*84e872a0SLloyd Pique exit(EXIT_SUCCESS); /* failed to fail */
109*84e872a0SLloyd Pique
110*84e872a0SLloyd Pique test_disable_coredumps();
111*84e872a0SLloyd Pique exec_fd_leak_check(nr_fds);
112*84e872a0SLloyd Pique }
113*84e872a0SLloyd Pique
TEST(sanity_fd_exec)114*84e872a0SLloyd Pique TEST(sanity_fd_exec)
115*84e872a0SLloyd Pique {
116*84e872a0SLloyd Pique int fd[2];
117*84e872a0SLloyd Pique int nr_fds = count_open_fds();
118*84e872a0SLloyd Pique
119*84e872a0SLloyd Pique /* create 2 file descriptors, that should pass over exec */
120*84e872a0SLloyd Pique assert(pipe(fd) >= 0);
121*84e872a0SLloyd Pique
122*84e872a0SLloyd Pique exec_fd_leak_check(nr_fds + 2);
123*84e872a0SLloyd Pique }
124*84e872a0SLloyd Pique
125*84e872a0SLloyd Pique static void
sanity_fd_no_leak(void)126*84e872a0SLloyd Pique sanity_fd_no_leak(void)
127*84e872a0SLloyd Pique {
128*84e872a0SLloyd Pique int fd[2];
129*84e872a0SLloyd Pique
130*84e872a0SLloyd Pique assert(fd_leak_check_enabled);
131*84e872a0SLloyd Pique
132*84e872a0SLloyd Pique /* leak 2 file descriptors */
133*84e872a0SLloyd Pique if (pipe(fd) < 0)
134*84e872a0SLloyd Pique exit(EXIT_SUCCESS); /* failed to fail */
135*84e872a0SLloyd Pique
136*84e872a0SLloyd Pique close(fd[0]);
137*84e872a0SLloyd Pique close(fd[1]);
138*84e872a0SLloyd Pique }
139*84e872a0SLloyd Pique
140*84e872a0SLloyd Pique static void
sanity_client_no_leak(void)141*84e872a0SLloyd Pique sanity_client_no_leak(void)
142*84e872a0SLloyd Pique {
143*84e872a0SLloyd Pique struct wl_display *display = wl_display_connect(NULL);
144*84e872a0SLloyd Pique assert(display);
145*84e872a0SLloyd Pique
146*84e872a0SLloyd Pique wl_display_disconnect(display);
147*84e872a0SLloyd Pique }
148*84e872a0SLloyd Pique
TEST(tc_client_no_fd_leaks)149*84e872a0SLloyd Pique TEST(tc_client_no_fd_leaks)
150*84e872a0SLloyd Pique {
151*84e872a0SLloyd Pique struct display *d = display_create();
152*84e872a0SLloyd Pique
153*84e872a0SLloyd Pique /* Client which does not consume the WAYLAND_DISPLAY socket. */
154*84e872a0SLloyd Pique client_create_noarg(d, sanity_fd_no_leak);
155*84e872a0SLloyd Pique display_run(d);
156*84e872a0SLloyd Pique
157*84e872a0SLloyd Pique /* Client which does consume the WAYLAND_DISPLAY socket. */
158*84e872a0SLloyd Pique client_create_noarg(d, sanity_client_no_leak);
159*84e872a0SLloyd Pique display_run(d);
160*84e872a0SLloyd Pique
161*84e872a0SLloyd Pique display_destroy(d);
162*84e872a0SLloyd Pique }
163*84e872a0SLloyd Pique
FAIL_TEST(tc_client_fd_leaks)164*84e872a0SLloyd Pique FAIL_TEST(tc_client_fd_leaks)
165*84e872a0SLloyd Pique {
166*84e872a0SLloyd Pique struct display *d = display_create();
167*84e872a0SLloyd Pique
168*84e872a0SLloyd Pique client_create_noarg(d, sanity_fd_leak);
169*84e872a0SLloyd Pique display_run(d);
170*84e872a0SLloyd Pique
171*84e872a0SLloyd Pique test_disable_coredumps();
172*84e872a0SLloyd Pique display_destroy(d);
173*84e872a0SLloyd Pique }
174*84e872a0SLloyd Pique
FAIL_TEST(tc_client_fd_leaks_exec)175*84e872a0SLloyd Pique FAIL_TEST(tc_client_fd_leaks_exec)
176*84e872a0SLloyd Pique {
177*84e872a0SLloyd Pique struct display *d = display_create();
178*84e872a0SLloyd Pique
179*84e872a0SLloyd Pique client_create_noarg(d, sanity_fd_leak_exec);
180*84e872a0SLloyd Pique display_run(d);
181*84e872a0SLloyd Pique
182*84e872a0SLloyd Pique test_disable_coredumps();
183*84e872a0SLloyd Pique display_destroy(d);
184*84e872a0SLloyd Pique }
185*84e872a0SLloyd Pique
186*84e872a0SLloyd Pique static char *
map_file(int fd,size_t * len)187*84e872a0SLloyd Pique map_file(int fd, size_t *len)
188*84e872a0SLloyd Pique {
189*84e872a0SLloyd Pique char *data;
190*84e872a0SLloyd Pique
191*84e872a0SLloyd Pique *len = lseek(fd, 0, SEEK_END);
192*84e872a0SLloyd Pique data = mmap(0, *len, PROT_READ, MAP_PRIVATE, fd, 0);
193*84e872a0SLloyd Pique assert(data != MAP_FAILED && "Failed to mmap file");
194*84e872a0SLloyd Pique
195*84e872a0SLloyd Pique return data;
196*84e872a0SLloyd Pique }
197*84e872a0SLloyd Pique
198*84e872a0SLloyd Pique static void
sanity_client_log(void)199*84e872a0SLloyd Pique sanity_client_log(void)
200*84e872a0SLloyd Pique {
201*84e872a0SLloyd Pique char *log;
202*84e872a0SLloyd Pique size_t log_len;
203*84e872a0SLloyd Pique char *wayland_socket = strdup(getenv("WAYLAND_SOCKET"));
204*84e872a0SLloyd Pique char *xdg_runtime_dir = strdup(getenv("XDG_RUNTIME_DIR"));
205*84e872a0SLloyd Pique
206*84e872a0SLloyd Pique unsetenv("WAYLAND_SOCKET");
207*84e872a0SLloyd Pique unsetenv("XDG_RUNTIME_DIR");
208*84e872a0SLloyd Pique
209*84e872a0SLloyd Pique /* Try to connect to the default wayland display, which should fail since
210*84e872a0SLloyd Pique * we have neither WAYLAND_SOCKET nor XDG_RUNTIME_DIR. */
211*84e872a0SLloyd Pique assert(!wl_display_connect(NULL));
212*84e872a0SLloyd Pique
213*84e872a0SLloyd Pique /* Check that the client log contains some mention of XDG_RUNTIME_DIR. */
214*84e872a0SLloyd Pique log = map_file(client_log_fd, &log_len);
215*84e872a0SLloyd Pique assert(strstr(log, "XDG_RUNTIME_DIR"));
216*84e872a0SLloyd Pique munmap(log, log_len);
217*84e872a0SLloyd Pique
218*84e872a0SLloyd Pique /* Reset the environment variables we unset for the test. The test harness
219*84e872a0SLloyd Pique * leak checker cares about the value of WAYLAND_SOCKET during teardown for
220*84e872a0SLloyd Pique * correct fd accounting. */
221*84e872a0SLloyd Pique setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 0);
222*84e872a0SLloyd Pique setenv("WAYLAND_SOCKET", wayland_socket, 0);
223*84e872a0SLloyd Pique free(xdg_runtime_dir);
224*84e872a0SLloyd Pique free(wayland_socket);
225*84e872a0SLloyd Pique }
226*84e872a0SLloyd Pique
TEST(tc_client_log)227*84e872a0SLloyd Pique TEST(tc_client_log)
228*84e872a0SLloyd Pique {
229*84e872a0SLloyd Pique struct display *d = display_create();
230*84e872a0SLloyd Pique struct client_info *ci;
231*84e872a0SLloyd Pique char *log;
232*84e872a0SLloyd Pique size_t log_len;
233*84e872a0SLloyd Pique
234*84e872a0SLloyd Pique ci = client_create_noarg(d, sanity_client_log);
235*84e872a0SLloyd Pique display_run(d);
236*84e872a0SLloyd Pique
237*84e872a0SLloyd Pique /* Check that the client log contains some mention of XDG_RUNTIME_DIR. */
238*84e872a0SLloyd Pique log = map_file(ci->log_fd, &log_len);
239*84e872a0SLloyd Pique assert(strstr(log, "XDG_RUNTIME_DIR"));
240*84e872a0SLloyd Pique munmap(log, log_len);
241*84e872a0SLloyd Pique
242*84e872a0SLloyd Pique display_destroy(d);
243*84e872a0SLloyd Pique }
244*84e872a0SLloyd Pique
FAIL_TEST(timeout_tst)245*84e872a0SLloyd Pique FAIL_TEST(timeout_tst)
246*84e872a0SLloyd Pique {
247*84e872a0SLloyd Pique test_set_timeout(1);
248*84e872a0SLloyd Pique test_disable_coredumps();
249*84e872a0SLloyd Pique /* test should reach timeout */
250*84e872a0SLloyd Pique test_sleep(2);
251*84e872a0SLloyd Pique }
252*84e872a0SLloyd Pique
TEST(timeout2_tst)253*84e872a0SLloyd Pique TEST(timeout2_tst)
254*84e872a0SLloyd Pique {
255*84e872a0SLloyd Pique /* the test should end before reaching timeout,
256*84e872a0SLloyd Pique * thus it should pass */
257*84e872a0SLloyd Pique test_set_timeout(1);
258*84e872a0SLloyd Pique /* 200 000 microsec = 0.2 sec */
259*84e872a0SLloyd Pique test_usleep(200000);
260*84e872a0SLloyd Pique }
261*84e872a0SLloyd Pique
FAIL_TEST(timeout_reset_tst)262*84e872a0SLloyd Pique FAIL_TEST(timeout_reset_tst)
263*84e872a0SLloyd Pique {
264*84e872a0SLloyd Pique test_set_timeout(5);
265*84e872a0SLloyd Pique test_set_timeout(10);
266*84e872a0SLloyd Pique test_set_timeout(1);
267*84e872a0SLloyd Pique
268*84e872a0SLloyd Pique test_disable_coredumps();
269*84e872a0SLloyd Pique /* test should fail on timeout */
270*84e872a0SLloyd Pique test_sleep(2);
271*84e872a0SLloyd Pique }
272*84e872a0SLloyd Pique
TEST(timeout_turnoff)273*84e872a0SLloyd Pique TEST(timeout_turnoff)
274*84e872a0SLloyd Pique {
275*84e872a0SLloyd Pique test_set_timeout(1);
276*84e872a0SLloyd Pique test_set_timeout(0);
277*84e872a0SLloyd Pique
278*84e872a0SLloyd Pique test_usleep(2);
279*84e872a0SLloyd Pique }
280*84e872a0SLloyd Pique
281*84e872a0SLloyd Pique /* test timeouts with test-compositor */
FAIL_TEST(tc_timeout_tst)282*84e872a0SLloyd Pique FAIL_TEST(tc_timeout_tst)
283*84e872a0SLloyd Pique {
284*84e872a0SLloyd Pique struct display *d = display_create();
285*84e872a0SLloyd Pique client_create_noarg(d, timeout_tst);
286*84e872a0SLloyd Pique display_run(d);
287*84e872a0SLloyd Pique test_disable_coredumps();
288*84e872a0SLloyd Pique display_destroy(d);
289*84e872a0SLloyd Pique }
290*84e872a0SLloyd Pique
FAIL_TEST(tc_timeout2_tst)291*84e872a0SLloyd Pique FAIL_TEST(tc_timeout2_tst)
292*84e872a0SLloyd Pique {
293*84e872a0SLloyd Pique struct display *d = display_create();
294*84e872a0SLloyd Pique client_create_noarg(d, timeout_reset_tst);
295*84e872a0SLloyd Pique display_run(d);
296*84e872a0SLloyd Pique test_disable_coredumps();
297*84e872a0SLloyd Pique display_destroy(d);
298*84e872a0SLloyd Pique }
299*84e872a0SLloyd Pique
TEST(tc_timeout3_tst)300*84e872a0SLloyd Pique TEST(tc_timeout3_tst)
301*84e872a0SLloyd Pique {
302*84e872a0SLloyd Pique struct display *d = display_create();
303*84e872a0SLloyd Pique
304*84e872a0SLloyd Pique client_create_noarg(d, timeout2_tst);
305*84e872a0SLloyd Pique display_run(d);
306*84e872a0SLloyd Pique
307*84e872a0SLloyd Pique client_create_noarg(d, timeout_turnoff);
308*84e872a0SLloyd Pique display_run(d);
309*84e872a0SLloyd Pique
310*84e872a0SLloyd Pique display_destroy(d);
311*84e872a0SLloyd Pique }
312