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 <math.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <stdint.h>
31 #include <string.h>
32 #include <assert.h>
33 #include <sys/socket.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <poll.h>
39
40 #include "wayland-private.h"
41 #include "test-runner.h"
42 #include "test-compositor.h"
43
44 static const char message[] = "Hello, world";
45
46 static struct wl_connection *
setup(int * s)47 setup(int *s)
48 {
49 struct wl_connection *connection;
50
51 assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);
52
53 connection = wl_connection_create(s[0]);
54 assert(connection);
55
56 return connection;
57 }
58
TEST(connection_create)59 TEST(connection_create)
60 {
61 struct wl_connection *connection;
62 int s[2];
63
64 connection = setup(s);
65 wl_connection_destroy(connection);
66 close(s[0]);
67 close(s[1]);
68 }
69
TEST(connection_write)70 TEST(connection_write)
71 {
72 struct wl_connection *connection;
73 int s[2];
74 char buffer[64];
75
76 connection = setup(s);
77
78 assert(wl_connection_write(connection, message, sizeof message) == 0);
79 assert(wl_connection_flush(connection) == sizeof message);
80 assert(read(s[1], buffer, sizeof buffer) == sizeof message);
81 assert(memcmp(message, buffer, sizeof message) == 0);
82
83 wl_connection_destroy(connection);
84 close(s[0]);
85 close(s[1]);
86 }
87
TEST(connection_data)88 TEST(connection_data)
89 {
90 struct wl_connection *connection;
91 int s[2];
92 char buffer[64];
93
94 connection = setup(s);
95
96 assert(write(s[1], message, sizeof message) == sizeof message);
97 assert(wl_connection_read(connection) == sizeof message);
98 wl_connection_copy(connection, buffer, sizeof message);
99 assert(memcmp(message, buffer, sizeof message) == 0);
100 wl_connection_consume(connection, sizeof message);
101
102 wl_connection_destroy(connection);
103 close(s[0]);
104 close(s[1]);
105 }
106
TEST(connection_queue)107 TEST(connection_queue)
108 {
109 struct wl_connection *connection;
110 int s[2];
111 char buffer[64];
112
113 connection = setup(s);
114
115 /* Test that wl_connection_queue() puts data in the output
116 * buffer without flush it. Verify that the data did get in
117 * the buffer by writing another message and making sure that
118 * we receive the two messages on the other fd. */
119
120 assert(wl_connection_queue(connection, message, sizeof message) == 0);
121 assert(wl_connection_flush(connection) == 0);
122 assert(wl_connection_write(connection, message, sizeof message) == 0);
123 assert(wl_connection_flush(connection) == 2 * sizeof message);
124 assert(read(s[1], buffer, sizeof buffer) == 2 * sizeof message);
125 assert(memcmp(message, buffer, sizeof message) == 0);
126 assert(memcmp(message, buffer + sizeof message, sizeof message) == 0);
127
128 wl_connection_destroy(connection);
129 close(s[0]);
130 close(s[1]);
131 }
132
133 struct marshal_data {
134 struct wl_connection *read_connection;
135 struct wl_connection *write_connection;
136 int s[2];
137 uint32_t buffer[10];
138 union {
139 uint32_t u;
140 int32_t i;
141 const char *s;
142 int h;
143 } value;
144 };
145
146 static void
setup_marshal_data(struct marshal_data * data)147 setup_marshal_data(struct marshal_data *data)
148 {
149 assert(socketpair(AF_UNIX,
150 SOCK_STREAM | SOCK_CLOEXEC, 0, data->s) == 0);
151 data->read_connection = wl_connection_create(data->s[0]);
152 assert(data->read_connection);
153 data->write_connection = wl_connection_create(data->s[1]);
154 assert(data->write_connection);
155 }
156
157 static void
release_marshal_data(struct marshal_data * data)158 release_marshal_data(struct marshal_data *data)
159 {
160 close(wl_connection_destroy(data->read_connection));
161 close(wl_connection_destroy(data->write_connection));
162 }
163
164 static void
marshal(struct marshal_data * data,const char * format,int size,...)165 marshal(struct marshal_data *data, const char *format, int size, ...)
166 {
167 struct wl_closure *closure;
168 static const uint32_t opcode = 4444;
169 static struct wl_object sender = { NULL, NULL, 1234 };
170 struct wl_message message = { "test", format, NULL };
171 va_list ap;
172
173 va_start(ap, size);
174 closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
175 va_end(ap);
176
177 assert(closure);
178 assert(wl_closure_send(closure, data->write_connection) == 0);
179 wl_closure_destroy(closure);
180 assert(wl_connection_flush(data->write_connection) == size);
181 assert(read(data->s[0], data->buffer, sizeof data->buffer) == size);
182
183 assert(data->buffer[0] == sender.id);
184 assert(data->buffer[1] == (opcode | (size << 16)));
185 }
186
TEST(connection_marshal)187 TEST(connection_marshal)
188 {
189 struct marshal_data data;
190 struct wl_object object;
191 struct wl_array array;
192 static const char text[] = "curry";
193
194 setup_marshal_data(&data);
195
196 marshal(&data, "i", 12, 42);
197 assert(data.buffer[2] == 42);
198
199 marshal(&data, "u", 12, 55);
200 assert(data.buffer[2] == 55);
201
202 marshal(&data, "s", 20, "frappo");
203 assert(data.buffer[2] == 7);
204 assert(strcmp((char *) &data.buffer[3], "frappo") == 0);
205
206 object.id = 557799;
207 marshal(&data, "o", 12, &object);
208 assert(data.buffer[2] == object.id);
209
210 marshal(&data, "n", 12, &object);
211 assert(data.buffer[2] == object.id);
212
213 marshal(&data, "?n", 12, NULL);
214 assert(data.buffer[2] == 0);
215
216 array.data = (void *) text;
217 array.size = sizeof text;
218 marshal(&data, "a", 20, &array);
219 assert(data.buffer[2] == array.size);
220 assert(memcmp(&data.buffer[3], text, array.size) == 0);
221
222 release_marshal_data(&data);
223 }
224
225 static void
expected_fail_marshal(int expected_error,const char * format,...)226 expected_fail_marshal(int expected_error, const char *format, ...)
227 {
228 struct wl_closure *closure;
229 static const uint32_t opcode = 4444;
230 static const struct wl_interface test_interface = {
231 .name = "test_object"
232 };
233 static struct wl_object sender = { 0 };
234 struct wl_message message = { "test", format, NULL };
235
236 sender.interface = &test_interface;
237 sender.id = 1234;
238 va_list ap;
239
240 va_start(ap, format);
241 closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
242 va_end(ap);
243
244 assert(closure == NULL);
245 assert(errno == expected_error);
246 }
247
248 static void
expected_fail_marshal_send(struct marshal_data * data,int expected_error,const char * format,...)249 expected_fail_marshal_send(struct marshal_data *data, int expected_error,
250 const char *format, ...)
251 {
252 struct wl_closure *closure;
253 static const uint32_t opcode = 4444;
254 static struct wl_object sender = { NULL, NULL, 1234 };
255 struct wl_message message = { "test", format, NULL };
256 va_list ap;
257
258 va_start(ap, format);
259 closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
260 va_end(ap);
261
262 assert(closure);
263 assert(wl_closure_send(closure, data->write_connection) < 0);
264 assert(errno == expected_error);
265
266 wl_closure_destroy(closure);
267 }
268
TEST(connection_marshal_nullables)269 TEST(connection_marshal_nullables)
270 {
271 struct marshal_data data;
272 struct wl_object object;
273 struct wl_array array;
274 const char text[] = "curry";
275
276 setup_marshal_data(&data);
277
278 expected_fail_marshal(EINVAL, "o", NULL);
279 expected_fail_marshal(EINVAL, "s", NULL);
280 expected_fail_marshal(EINVAL, "a", NULL);
281
282 marshal(&data, "?o", 12, NULL);
283 assert(data.buffer[2] == 0);
284
285 marshal(&data, "?a", 12, NULL);
286 assert(data.buffer[2] == 0);
287
288 marshal(&data, "?s", 12, NULL);
289 assert(data.buffer[2] == 0);
290
291 object.id = 55293;
292 marshal(&data, "?o", 12, &object);
293 assert(data.buffer[2] == object.id);
294
295 array.data = (void *) text;
296 array.size = sizeof text;
297 marshal(&data, "?a", 20, &array);
298 assert(data.buffer[2] == array.size);
299 assert(memcmp(&data.buffer[3], text, array.size) == 0);
300
301 marshal(&data, "?s", 20, text);
302 assert(data.buffer[2] == sizeof text);
303 assert(strcmp((char *) &data.buffer[3], text) == 0);
304
305 release_marshal_data(&data);
306 }
307
308 static void
validate_demarshal_u(struct marshal_data * data,struct wl_object * object,uint32_t u)309 validate_demarshal_u(struct marshal_data *data,
310 struct wl_object *object, uint32_t u)
311 {
312 assert(data->value.u == u);
313 }
314
315 static void
validate_demarshal_i(struct marshal_data * data,struct wl_object * object,int32_t i)316 validate_demarshal_i(struct marshal_data *data,
317 struct wl_object *object, int32_t i)
318 {
319 assert(data->value.i == i);
320 }
321
322 static void
validate_demarshal_s(struct marshal_data * data,struct wl_object * object,const char * s)323 validate_demarshal_s(struct marshal_data *data,
324 struct wl_object *object, const char *s)
325 {
326 if (data->value.s != NULL)
327 assert(strcmp(data->value.s, s) == 0);
328 else
329 assert(s == NULL);
330 }
331
332 static void
validate_demarshal_h(struct marshal_data * data,struct wl_object * object,int fd)333 validate_demarshal_h(struct marshal_data *data,
334 struct wl_object *object, int fd)
335 {
336 struct stat buf1, buf2;
337
338 assert(fd != data->value.h);
339 fstat(fd, &buf1);
340 fstat(data->value.h, &buf2);
341 assert(buf1.st_dev == buf2.st_dev);
342 assert(buf1.st_ino == buf2.st_ino);
343 close(fd);
344 close(data->value.h);
345 }
346
347 static void
validate_demarshal_f(struct marshal_data * data,struct wl_object * object,wl_fixed_t f)348 validate_demarshal_f(struct marshal_data *data,
349 struct wl_object *object, wl_fixed_t f)
350 {
351 assert(data->value.i == f);
352 }
353
354 static void
demarshal(struct marshal_data * data,const char * format,uint32_t * msg,void (* func)(void))355 demarshal(struct marshal_data *data, const char *format,
356 uint32_t *msg, void (*func)(void))
357 {
358 struct wl_message message = { "test", format, NULL };
359 struct wl_closure *closure;
360 struct wl_map objects;
361 struct wl_object object = { NULL, &func, 0 };
362 int size = msg[1];
363
364 assert(write(data->s[1], msg, size) == size);
365 assert(wl_connection_read(data->read_connection) == size);
366
367 wl_map_init(&objects, WL_MAP_SERVER_SIDE);
368 object.id = msg[0];
369 closure = wl_connection_demarshal(data->read_connection,
370 size, &objects, &message);
371 assert(closure);
372 wl_closure_invoke(closure, WL_CLOSURE_INVOKE_SERVER, &object, 0, data);
373 wl_closure_destroy(closure);
374 }
375
TEST(connection_demarshal)376 TEST(connection_demarshal)
377 {
378 struct marshal_data data;
379 uint32_t msg[10];
380
381 setup_marshal_data(&data);
382
383 data.value.u = 8000;
384 msg[0] = 400200; /* object id */
385 msg[1] = 12; /* size = 12, opcode = 0 */
386 msg[2] = data.value.u;
387 demarshal(&data, "u", msg, (void *) validate_demarshal_u);
388
389 data.value.i = -557799;
390 msg[0] = 400200;
391 msg[1] = 12;
392 msg[2] = data.value.i;
393 demarshal(&data, "i", msg, (void *) validate_demarshal_i);
394
395 data.value.s = "superdude";
396 msg[0] = 400200;
397 msg[1] = 24;
398 msg[2] = 10;
399 memcpy(&msg[3], data.value.s, msg[2]);
400 demarshal(&data, "s", msg, (void *) validate_demarshal_s);
401
402 data.value.s = "superdude";
403 msg[0] = 400200;
404 msg[1] = 24;
405 msg[2] = 10;
406 memcpy(&msg[3], data.value.s, msg[2]);
407 demarshal(&data, "?s", msg, (void *) validate_demarshal_s);
408
409 data.value.i = wl_fixed_from_double(-90000.2390);
410 msg[0] = 400200;
411 msg[1] = 12;
412 msg[2] = data.value.i;
413 demarshal(&data, "f", msg, (void *) validate_demarshal_f);
414
415 data.value.s = NULL;
416 msg[0] = 400200;
417 msg[1] = 12;
418 msg[2] = 0;
419 demarshal(&data, "?s", msg, (void *) validate_demarshal_s);
420
421 release_marshal_data(&data);
422 }
423
424 static void
marshal_demarshal(struct marshal_data * data,void (* func)(void),int size,const char * format,...)425 marshal_demarshal(struct marshal_data *data,
426 void (*func)(void), int size, const char *format, ...)
427 {
428 struct wl_closure *closure;
429 static const int opcode = 4444;
430 static struct wl_object sender = { NULL, NULL, 1234 };
431 struct wl_message message = { "test", format, NULL };
432 struct wl_map objects;
433 struct wl_object object = { NULL, &func, 0 };
434 va_list ap;
435 uint32_t msg[1] = { 1234 };
436
437 va_start(ap, format);
438 closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
439 va_end(ap);
440
441 assert(closure);
442 assert(wl_closure_send(closure, data->write_connection) == 0);
443 wl_closure_destroy(closure);
444 assert(wl_connection_flush(data->write_connection) == size);
445
446 assert(wl_connection_read(data->read_connection) == size);
447
448 wl_map_init(&objects, WL_MAP_SERVER_SIDE);
449 object.id = msg[0];
450 closure = wl_connection_demarshal(data->read_connection,
451 size, &objects, &message);
452 assert(closure);
453 wl_closure_invoke(closure, WL_CLOSURE_INVOKE_SERVER, &object, 0, data);
454 wl_closure_destroy(closure);
455 }
456
TEST(connection_marshal_demarshal)457 TEST(connection_marshal_demarshal)
458 {
459 struct marshal_data data;
460 char f[] = "/tmp/wayland-tests-XXXXXX";
461
462 setup_marshal_data(&data);
463
464 data.value.u = 889911;
465 marshal_demarshal(&data, (void *) validate_demarshal_u,
466 12, "u", data.value.u);
467
468 data.value.i = -13;
469 marshal_demarshal(&data, (void *) validate_demarshal_i,
470 12, "i", data.value.i);
471
472 data.value.s = "cookie robots";
473 marshal_demarshal(&data, (void *) validate_demarshal_s,
474 28, "s", data.value.s);
475
476 data.value.s = "cookie robots";
477 marshal_demarshal(&data, (void *) validate_demarshal_s,
478 28, "?s", data.value.s);
479
480 data.value.h = mkstemp(f);
481 assert(data.value.h >= 0);
482 unlink(f);
483 marshal_demarshal(&data, (void *) validate_demarshal_h,
484 8, "h", data.value.h);
485
486 data.value.i = wl_fixed_from_double(1234.5678);
487 marshal_demarshal(&data, (void *) validate_demarshal_f,
488 12, "f", data.value.i);
489
490 data.value.i = wl_fixed_from_double(-90000.2390);
491 marshal_demarshal(&data, (void *) validate_demarshal_f,
492 12, "f", data.value.i);
493
494 data.value.i = wl_fixed_from_double((1 << 23) - 1 + 0.0941);
495 marshal_demarshal(&data, (void *) validate_demarshal_f,
496 12, "f", data.value.i);
497
498 release_marshal_data(&data);
499 }
500
TEST(connection_marshal_alot)501 TEST(connection_marshal_alot)
502 {
503 struct marshal_data data;
504 char f[64];
505 int i;
506
507 setup_marshal_data(&data);
508
509 /* We iterate enough to make sure we wrap the circular buffers
510 * for both regular data an fds. */
511
512 for (i = 0; i < 2000; i++) {
513 strcpy(f, "/tmp/wayland-tests-XXXXXX");
514 data.value.h = mkstemp(f);
515 assert(data.value.h >= 0);
516 unlink(f);
517 marshal_demarshal(&data, (void *) validate_demarshal_h,
518 8, "h", data.value.h);
519 }
520
521 release_marshal_data(&data);
522 }
523
TEST(connection_marshal_too_big)524 TEST(connection_marshal_too_big)
525 {
526 struct marshal_data data;
527 char *big_string = malloc(5000);
528
529 assert(big_string);
530
531 memset(big_string, ' ', 4999);
532 big_string[4999] = '\0';
533
534 setup_marshal_data(&data);
535
536 expected_fail_marshal_send(&data, E2BIG, "s", big_string);
537
538 release_marshal_data(&data);
539 free(big_string);
540 }
541
542 static void
marshal_helper(const char * format,void * handler,...)543 marshal_helper(const char *format, void *handler, ...)
544 {
545 struct wl_closure *closure;
546 static struct wl_object sender = { NULL, NULL, 1234 };
547 struct wl_object object = { NULL, &handler, 0 };
548 static const int opcode = 4444;
549 struct wl_message message = { "test", format, NULL };
550 va_list ap;
551 int done;
552
553 va_start(ap, handler);
554 closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
555 va_end(ap);
556
557 assert(closure);
558 done = 0;
559 wl_closure_invoke(closure, WL_CLOSURE_INVOKE_SERVER, &object, 0, &done);
560 wl_closure_destroy(closure);
561 assert(done);
562 }
563
564 static void
suu_handler(void * data,struct wl_object * object,const char * s,uint32_t u1,uint32_t u2)565 suu_handler(void *data, struct wl_object *object,
566 const char *s, uint32_t u1, uint32_t u2)
567 {
568 int *done = data;
569
570 assert(strcmp(s, "foo") == 0);
571 assert(u1 = 500);
572 assert(u2 = 404040);
573 *done = 1;
574 }
575
TEST(invoke_closure)576 TEST(invoke_closure)
577 {
578 marshal_helper("suu", suu_handler, "foo", 500, 404040);
579 }
580
581 static void
leak_closure(void)582 leak_closure(void)
583 {
584 struct wl_callback *cb;
585 struct pollfd pfd;
586 struct client *c = client_connect();
587
588 cb = wl_display_sync(c->wl_display);
589 assert(cb);
590 assert(wl_display_flush(c->wl_display) > 0);
591
592 /* we don't need it, it is referenced */
593 wl_callback_destroy(cb);
594
595 pfd.fd = wl_display_get_fd(c->wl_display);
596 pfd.events = POLLIN;
597
598 test_set_timeout(2);
599 assert(poll(&pfd, 1, -1) == 1);
600
601 /* read events, but do not dispatch them */
602 assert(wl_display_prepare_read(c->wl_display) == 0);
603 assert(wl_display_read_events(c->wl_display) == 0);
604
605 /*
606 * now we have wl_callback.done and wl_display.delete_id queued;
607 * if we now release the queue (in wl_display_disconnect())
608 * we should not leak memory
609 */
610
611 client_disconnect(c);
612 }
613
TEST(closure_leaks)614 TEST(closure_leaks)
615 {
616 struct display *d = display_create();
617
618 client_create_noarg(d, leak_closure);
619 display_run(d);
620
621 display_destroy(d);
622 }
623
624 static void
leak_after_error(void)625 leak_after_error(void)
626 {
627 struct client *c = client_connect();
628
629 /* this should return -1, because we'll send error
630 * from server. */
631 assert(stop_display(c, 1) == -1);
632 assert(wl_display_dispatch_pending(c->wl_display) == -1);
633 assert(wl_display_get_error(c->wl_display) == ENOMEM);
634
635 /* after we got error, we have display_resume event
636 * in the queue. It should be freed in wl_display_disconnect().
637 * Let's see! */
638
639 wl_proxy_destroy((struct wl_proxy *) c->tc);
640 wl_display_disconnect(c->wl_display);
641 free(c);
642 }
643
TEST(closure_leaks_after_error)644 TEST(closure_leaks_after_error)
645 {
646 struct display *d = display_create();
647 struct client_info *cl;
648
649 cl = client_create_noarg(d, leak_after_error);
650 display_run(d);
651
652 wl_client_post_no_memory(cl->wl_client);
653 display_resume(d);
654
655 display_destroy(d);
656 }
657