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 <sys/types.h>
29 #include <signal.h>
30 #include <unistd.h>
31
32 #include "test-runner.h"
33 #include "wayland-util.h"
34 #include "wayland-private.h"
35
36 #include "test-compositor.h"
37
38 extern int leak_check_enabled;
39
TEST(empty)40 TEST(empty)
41 {
42 }
43
TEST(exit_success)44 TEST(exit_success)
45 {
46 exit(EXIT_SUCCESS);
47 }
48
FAIL_TEST(exit_failure)49 FAIL_TEST(exit_failure)
50 {
51 exit(EXIT_FAILURE);
52 }
53
FAIL_TEST(fail_abort)54 FAIL_TEST(fail_abort)
55 {
56 abort();
57 }
58
FAIL_TEST(fail_wl_abort)59 FAIL_TEST(fail_wl_abort)
60 {
61 wl_abort("Abort the program\n");
62 }
63
FAIL_TEST(fail_kill)64 FAIL_TEST(fail_kill)
65 {
66 kill(getpid(), SIGTERM);
67 }
68
FAIL_TEST(fail_segv)69 FAIL_TEST(fail_segv)
70 {
71 * (char **) 0 = "Goodbye, world";
72 }
73
FAIL_TEST(sanity_assert)74 FAIL_TEST(sanity_assert)
75 {
76 /* must fail */
77 assert(0);
78 }
79
FAIL_TEST(sanity_malloc_direct)80 FAIL_TEST(sanity_malloc_direct)
81 {
82 void *p;
83
84 assert(leak_check_enabled);
85
86 p = malloc(10); /* memory leak */
87 assert(p); /* assert that we got memory, also prevents
88 * the malloc from getting optimized away. */
89 free(NULL); /* NULL must not be counted */
90 }
91
TEST(disable_leak_checks)92 TEST(disable_leak_checks)
93 {
94 volatile void *mem;
95 assert(leak_check_enabled);
96 /* normally this should be on the beginning of the test.
97 * Here we need to be sure, that the leak checks are
98 * turned on */
99 DISABLE_LEAK_CHECKS;
100
101 mem = malloc(16);
102 assert(mem);
103 }
104
FAIL_TEST(sanity_malloc_indirect)105 FAIL_TEST(sanity_malloc_indirect)
106 {
107 struct wl_array array;
108
109 assert(leak_check_enabled);
110
111 wl_array_init(&array);
112
113 /* call into library that calls malloc */
114 wl_array_add(&array, 14);
115
116 /* not freeing array, must leak */
117 }
118
FAIL_TEST(tc_client_memory_leaks)119 FAIL_TEST(tc_client_memory_leaks)
120 {
121 struct display *d = display_create();
122 client_create_noarg(d, sanity_malloc_direct);
123 display_run(d);
124 display_destroy(d);
125 }
126
FAIL_TEST(tc_client_memory_leaks2)127 FAIL_TEST(tc_client_memory_leaks2)
128 {
129 struct display *d = display_create();
130 client_create_noarg(d, sanity_malloc_indirect);
131 display_run(d);
132 display_destroy(d);
133 }
134
FAIL_TEST(sanity_fd_leak)135 FAIL_TEST(sanity_fd_leak)
136 {
137 int fd[2];
138
139 assert(leak_check_enabled);
140
141 /* leak 2 file descriptors */
142 if (pipe(fd) < 0)
143 exit(EXIT_SUCCESS); /* failed to fail */
144 }
145
FAIL_TEST(sanity_fd_leak_exec)146 FAIL_TEST(sanity_fd_leak_exec)
147 {
148 int fd[2];
149 int nr_fds = count_open_fds();
150
151 /* leak 2 file descriptors */
152 if (pipe(fd) < 0)
153 exit(EXIT_SUCCESS); /* failed to fail */
154
155 exec_fd_leak_check(nr_fds);
156 }
157
TEST(sanity_fd_exec)158 TEST(sanity_fd_exec)
159 {
160 int fd[2];
161 int nr_fds = count_open_fds();
162
163 /* create 2 file descriptors, that should pass over exec */
164 assert(pipe(fd) >= 0);
165
166 exec_fd_leak_check(nr_fds + 2);
167 }
168
169 static void
sanity_fd_no_leak(void)170 sanity_fd_no_leak(void)
171 {
172 int fd[2];
173
174 assert(leak_check_enabled);
175
176 /* leak 2 file descriptors */
177 if (pipe(fd) < 0)
178 exit(EXIT_SUCCESS); /* failed to fail */
179
180 close(fd[0]);
181 close(fd[1]);
182 }
183
184 static void
sanity_client_no_leak(void)185 sanity_client_no_leak(void)
186 {
187 struct wl_display *display = wl_display_connect(NULL);
188 assert(display);
189
190 wl_display_disconnect(display);
191 }
192
TEST(tc_client_no_fd_leaks)193 TEST(tc_client_no_fd_leaks)
194 {
195 struct display *d = display_create();
196
197 /* Client which does not consume the WAYLAND_DISPLAY socket. */
198 client_create_noarg(d, sanity_fd_no_leak);
199 display_run(d);
200
201 /* Client which does consume the WAYLAND_DISPLAY socket. */
202 client_create_noarg(d, sanity_client_no_leak);
203 display_run(d);
204
205 display_destroy(d);
206 }
207
FAIL_TEST(tc_client_fd_leaks)208 FAIL_TEST(tc_client_fd_leaks)
209 {
210 struct display *d = display_create();
211
212 client_create_noarg(d, sanity_fd_leak);
213 display_run(d);
214
215 display_destroy(d);
216 }
217
FAIL_TEST(tc_client_fd_leaks_exec)218 FAIL_TEST(tc_client_fd_leaks_exec)
219 {
220 struct display *d = display_create();
221
222 client_create_noarg(d, sanity_fd_leak);
223 display_run(d);
224
225 display_destroy(d);
226 }
227
FAIL_TEST(timeout_tst)228 FAIL_TEST(timeout_tst)
229 {
230 test_set_timeout(1);
231 /* test should reach timeout */
232 test_sleep(2);
233 }
234
TEST(timeout2_tst)235 TEST(timeout2_tst)
236 {
237 /* the test should end before reaching timeout,
238 * thus it should pass */
239 test_set_timeout(1);
240 /* 200 000 microsec = 0.2 sec */
241 test_usleep(200000);
242 }
243
FAIL_TEST(timeout_reset_tst)244 FAIL_TEST(timeout_reset_tst)
245 {
246 test_set_timeout(5);
247 test_set_timeout(10);
248 test_set_timeout(1);
249
250 /* test should fail on timeout */
251 test_sleep(2);
252 }
253
TEST(timeout_turnoff)254 TEST(timeout_turnoff)
255 {
256 test_set_timeout(1);
257 test_set_timeout(0);
258
259 test_usleep(2);
260 }
261
262 /* test timeouts with test-compositor */
FAIL_TEST(tc_timeout_tst)263 FAIL_TEST(tc_timeout_tst)
264 {
265 struct display *d = display_create();
266 client_create_noarg(d, timeout_tst);
267 display_run(d);
268 display_destroy(d);
269 }
270
FAIL_TEST(tc_timeout2_tst)271 FAIL_TEST(tc_timeout2_tst)
272 {
273 struct display *d = display_create();
274 client_create_noarg(d, timeout_reset_tst);
275 display_run(d);
276 display_destroy(d);
277 }
278
TEST(tc_timeout3_tst)279 TEST(tc_timeout3_tst)
280 {
281 struct display *d = display_create();
282
283 client_create_noarg(d, timeout2_tst);
284 display_run(d);
285
286 client_create_noarg(d, timeout_turnoff);
287 display_run(d);
288
289 display_destroy(d);
290 }
291