2 * Copyright © 2012 Intel Corporation
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
29 #include <sys/socket.h>
32 #include <sys/types.h>
35 #include "wayland-private.h"
36 #include "test-runner.h"
38 static const char message[] = "Hello, world";
41 update_func(struct wl_connection *connection, uint32_t mask, void *data)
50 static struct wl_connection *
51 setup(int *s, uint32_t *mask)
53 struct wl_connection *connection;
55 assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);
57 connection = wl_connection_create(s[0], update_func, mask);
59 assert(*mask == WL_CONNECTION_READABLE);
64 TEST(connection_create)
66 struct wl_connection *connection;
70 connection = setup(s, &mask);
71 wl_connection_destroy(connection);
75 TEST(connection_write)
77 struct wl_connection *connection;
82 connection = setup(s, &mask);
84 assert(wl_connection_write(connection, message, sizeof message) == 0);
85 assert(mask == (WL_CONNECTION_WRITABLE | WL_CONNECTION_READABLE));
86 assert(wl_connection_data(connection, WL_CONNECTION_WRITABLE) == 0);
87 assert(mask == WL_CONNECTION_READABLE);
88 assert(read(s[1], buffer, sizeof buffer) == sizeof message);
89 assert(memcmp(message, buffer, sizeof message) == 0);
91 wl_connection_destroy(connection);
97 struct wl_connection *connection;
102 connection = setup(s, &mask);
104 assert(write(s[1], message, sizeof message) == sizeof message);
105 assert(mask == WL_CONNECTION_READABLE);
106 assert(wl_connection_data(connection, WL_CONNECTION_READABLE) ==
108 wl_connection_copy(connection, buffer, sizeof message);
109 assert(memcmp(message, buffer, sizeof message) == 0);
110 wl_connection_consume(connection, sizeof message);
112 wl_connection_destroy(connection);
116 TEST(connection_queue)
118 struct wl_connection *connection;
123 connection = setup(s, &mask);
125 /* Test that wl_connection_queue() puts data in the output
126 * buffer without asking for WL_CONNECTION_WRITABLE. Verify
127 * that the data did get in the buffer by writing another
128 * message and making sure that we receive the two messages on
131 assert(wl_connection_queue(connection, message, sizeof message) == 0);
132 assert(mask == WL_CONNECTION_READABLE);
133 assert(wl_connection_write(connection, message, sizeof message) == 0);
134 assert(mask == (WL_CONNECTION_WRITABLE | WL_CONNECTION_READABLE));
135 assert(wl_connection_data(connection, WL_CONNECTION_WRITABLE) == 0);
136 assert(mask == WL_CONNECTION_READABLE);
137 assert(read(s[1], buffer, sizeof buffer) == 2 * sizeof message);
138 assert(memcmp(message, buffer, sizeof message) == 0);
139 assert(memcmp(message, buffer + sizeof message, sizeof message) == 0);
141 wl_connection_destroy(connection);
145 struct marshal_data {
146 struct wl_connection *read_connection;
147 struct wl_connection *write_connection;
161 setup_marshal_data(struct marshal_data *data)
163 assert(socketpair(AF_UNIX,
164 SOCK_STREAM | SOCK_CLOEXEC, 0, data->s) == 0);
166 data->read_connection =
167 wl_connection_create(data->s[0],
168 update_func, &data->read_mask);
169 assert(data->read_connection);
170 assert(data->read_mask == WL_CONNECTION_READABLE);
172 data->write_connection =
173 wl_connection_create(data->s[1],
174 update_func, &data->write_mask);
175 assert(data->write_connection);
176 assert(data->write_mask == WL_CONNECTION_READABLE);
180 release_marshal_data(struct marshal_data *data)
182 wl_connection_destroy(data->read_connection);
183 wl_connection_destroy(data->write_connection);
187 marshal(struct marshal_data *data, const char *format, int size, ...)
189 struct wl_closure *closure;
190 static const uint32_t opcode = 4444;
191 static struct wl_object sender = { NULL, NULL, 1234 };
192 struct wl_message message = { "test", format, NULL };
196 closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
200 assert(wl_closure_send(closure, data->write_connection) == 0);
201 wl_closure_destroy(closure);
202 assert(data->write_mask ==
203 (WL_CONNECTION_WRITABLE | WL_CONNECTION_READABLE));
204 assert(wl_connection_data(data->write_connection,
205 WL_CONNECTION_WRITABLE) == 0);
206 assert(data->write_mask == WL_CONNECTION_READABLE);
207 assert(read(data->s[0], data->buffer, sizeof data->buffer) == size);
209 assert(data->buffer[0] == sender.id);
210 assert(data->buffer[1] == (opcode | (size << 16)));
213 TEST(connection_marshal)
215 struct marshal_data data;
216 struct wl_object object;
217 struct wl_array array;
218 static const char text[] = "curry";
220 setup_marshal_data(&data);
222 marshal(&data, "i", 12, 42);
223 assert(data.buffer[2] == 42);
225 marshal(&data, "u", 12, 55);
226 assert(data.buffer[2] == 55);
228 marshal(&data, "s", 20, "frappo");
229 assert(data.buffer[2] == 7);
230 assert(strcmp((char *) &data.buffer[3], "frappo") == 0);
233 marshal(&data, "o", 12, &object);
234 assert(data.buffer[2] == object.id);
236 marshal(&data, "n", 12, &object);
237 assert(data.buffer[2] == object.id);
239 marshal(&data, "n", 12, NULL);
240 assert(data.buffer[2] == 0);
242 array.data = (void *) text;
243 array.size = sizeof text;
244 marshal(&data, "a", 20, &array);
245 assert(data.buffer[2] == array.size);
246 assert(memcmp(&data.buffer[3], text, array.size) == 0);
248 release_marshal_data(&data);
252 expected_fail_marshal(int expected_error, const char *format, ...)
254 struct wl_closure *closure;
255 static const uint32_t opcode = 4444;
256 static const struct wl_interface test_interface = {
257 .name = "test_object"
259 static struct wl_object sender = { 0 };
260 struct wl_message message = { "test", format, NULL };
262 sender.interface = &test_interface;
266 va_start(ap, format);
267 closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
270 assert(closure == NULL);
271 assert(errno == expected_error);
274 TEST(connection_marshal_nullables)
276 struct marshal_data data;
277 struct wl_object object;
278 struct wl_array array;
279 const char text[] = "curry";
281 setup_marshal_data(&data);
283 expected_fail_marshal(EINVAL, "o", NULL);
284 expected_fail_marshal(EINVAL, "s", NULL);
285 expected_fail_marshal(EINVAL, "a", NULL);
287 marshal(&data, "?o", 12, NULL);
288 assert(data.buffer[2] == 0);
290 marshal(&data, "?a", 12, NULL);
291 assert(data.buffer[2] == 0);
293 marshal(&data, "?s", 12, NULL);
294 assert(data.buffer[2] == 0);
297 marshal(&data, "?o", 12, &object);
298 assert(data.buffer[2] == object.id);
300 array.data = (void *) text;
301 array.size = sizeof text;
302 marshal(&data, "?a", 20, &array);
303 assert(data.buffer[2] == array.size);
304 assert(memcmp(&data.buffer[3], text, array.size) == 0);
306 marshal(&data, "?s", 20, text);
307 assert(data.buffer[2] == sizeof text);
308 assert(strcmp((char *) &data.buffer[3], text) == 0);
310 release_marshal_data(&data);
314 validate_demarshal_u(struct marshal_data *data,
315 struct wl_object *object, uint32_t u)
317 assert(data->value.u == u);
321 validate_demarshal_i(struct marshal_data *data,
322 struct wl_object *object, int32_t i)
324 assert(data->value.i == i);
328 validate_demarshal_s(struct marshal_data *data,
329 struct wl_object *object, const char *s)
331 if (data->value.s != NULL)
332 assert(strcmp(data->value.s, s) == 0);
338 validate_demarshal_h(struct marshal_data *data,
339 struct wl_object *object, int fd)
341 struct stat buf1, buf2;
343 assert(fd != data->value.h);
345 fstat(data->value.h, &buf2);
346 assert(buf1.st_dev == buf2.st_dev);
347 assert(buf1.st_ino == buf2.st_ino);
349 close(data->value.h);
353 validate_demarshal_f(struct marshal_data *data,
354 struct wl_object *object, wl_fixed_t f)
356 assert(data->value.i == f);
360 demarshal(struct marshal_data *data, const char *format,
361 uint32_t *msg, void (*func)(void))
363 struct wl_message message = { "test", format, NULL };
364 struct wl_closure *closure;
365 struct wl_map objects;
366 struct wl_object object;
369 assert(write(data->s[1], msg, size) == size);
370 assert(wl_connection_data(data->read_connection,
371 WL_CONNECTION_READABLE) == size);
373 wl_map_init(&objects);
375 closure = wl_connection_demarshal(data->read_connection,
376 size, &objects, &message);
378 wl_closure_invoke(closure, &object, func, data);
379 wl_closure_destroy(closure);
382 TEST(connection_demarshal)
384 struct marshal_data data;
387 setup_marshal_data(&data);
390 msg[0] = 400200; /* object id */
391 msg[1] = 12; /* size = 12, opcode = 0 */
392 msg[2] = data.value.u;
393 demarshal(&data, "u", msg, (void *) validate_demarshal_u);
395 data.value.i = -557799;
398 msg[2] = data.value.i;
399 demarshal(&data, "i", msg, (void *) validate_demarshal_i);
401 data.value.s = "superdude";
405 memcpy(&msg[3], data.value.s, msg[2]);
406 demarshal(&data, "s", msg, (void *) validate_demarshal_s);
408 data.value.s = "superdude";
412 memcpy(&msg[3], data.value.s, msg[2]);
413 demarshal(&data, "?s", msg, (void *) validate_demarshal_s);
415 data.value.i = wl_fixed_from_double(-90000.2390);
418 msg[2] = data.value.i;
419 demarshal(&data, "f", msg, (void *) validate_demarshal_f);
425 demarshal(&data, "?s", msg, (void *) validate_demarshal_s);
427 release_marshal_data(&data);
431 marshal_demarshal(struct marshal_data *data,
432 void (*func)(void), int size, const char *format, ...)
434 struct wl_closure *closure;
435 static const int opcode = 4444;
436 static struct wl_object sender = { NULL, NULL, 1234 };
437 struct wl_message message = { "test", format, NULL };
438 struct wl_map objects;
439 struct wl_object object;
441 uint32_t msg[1] = { 1234 };
443 va_start(ap, format);
444 closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
448 assert(wl_closure_send(closure, data->write_connection) == 0);
449 wl_closure_destroy(closure);
450 assert(data->write_mask ==
451 (WL_CONNECTION_WRITABLE | WL_CONNECTION_READABLE));
452 assert(wl_connection_data(data->write_connection,
453 WL_CONNECTION_WRITABLE) == 0);
454 assert(data->write_mask == WL_CONNECTION_READABLE);
456 assert(wl_connection_data(data->read_connection,
457 WL_CONNECTION_READABLE) == size);
459 wl_map_init(&objects);
461 closure = wl_connection_demarshal(data->read_connection,
462 size, &objects, &message);
463 wl_closure_invoke(closure, &object, func, data);
464 wl_closure_destroy(closure);
467 TEST(connection_marshal_demarshal)
469 struct marshal_data data;
470 char f[] = "/tmp/weston-tests-XXXXXX";
472 setup_marshal_data(&data);
474 data.value.u = 889911;
475 marshal_demarshal(&data, (void *) validate_demarshal_u,
476 12, "u", data.value.u);
479 marshal_demarshal(&data, (void *) validate_demarshal_i,
480 12, "i", data.value.i);
482 data.value.s = "cookie robots";
483 marshal_demarshal(&data, (void *) validate_demarshal_s,
484 28, "s", data.value.s);
486 data.value.s = "cookie robots";
487 marshal_demarshal(&data, (void *) validate_demarshal_s,
488 28, "?s", data.value.s);
490 data.value.h = mkstemp(f);
491 assert(data.value.h >= 0);
492 marshal_demarshal(&data, (void *) validate_demarshal_h,
493 8, "h", data.value.h);
495 data.value.i = wl_fixed_from_double(1234.5678);
496 marshal_demarshal(&data, (void *) validate_demarshal_f,
497 12, "f", data.value.i);
499 data.value.i = wl_fixed_from_double(-90000.2390);
500 marshal_demarshal(&data, (void *) validate_demarshal_f,
501 12, "f", data.value.i);
503 data.value.i = wl_fixed_from_double((1 << 23) - 1 + 0.0941);
504 marshal_demarshal(&data, (void *) validate_demarshal_f,
505 12, "f", data.value.i);
507 release_marshal_data(&data);
510 TEST(connection_marshal_alot)
512 struct marshal_data data;
516 setup_marshal_data(&data);
518 /* We iterate enough to make sure we wrap the circular buffers
519 * for both regular data an fds. */
521 for (i = 0; i < 2000; i++) {
522 strcpy(f, "/tmp/weston-tests-XXXXXX");
523 data.value.h = mkstemp(f);
524 assert(data.value.h >= 0);
525 marshal_demarshal(&data, (void *) validate_demarshal_h,
526 8, "h", data.value.h);
529 release_marshal_data(&data);
533 marshal_helper(const char *format, void *handler, ...)
535 struct wl_closure *closure;
536 static struct wl_object sender = { NULL, NULL, 1234 }, object;
537 static const int opcode = 4444;
538 struct wl_message message = { "test", format, NULL };
542 va_start(ap, handler);
543 closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
548 wl_closure_invoke(closure, &object, handler, &done);
549 wl_closure_destroy(closure);
554 suu_handler(void *data, struct wl_object *object,
555 const char *s, uint32_t u1, uint32_t u2)
559 assert(strcmp(s, "foo") == 0);
567 marshal_helper("suu", suu_handler, "foo", 500, 404040);