connection-test.c 17 KB
Newer Older
1 2 3
/*
 * Copyright © 2012 Intel Corporation
 *
4 5 6 7 8 9 10
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
11
 *
12 13 14 15 16 17 18 19 20 21 22 23
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
24 25
 */

26
#include <math.h>
27 28
#include <stdio.h>
#include <stdlib.h>
29
#include <stdarg.h>
Yong Bakos's avatar
Yong Bakos committed
30
#include <stdint.h>
31 32 33 34 35
#include <string.h>
#include <assert.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
36 37
#include <sys/types.h>
#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 45 46

static const char message[] = "Hello, world";

static struct wl_connection *
47
setup(int *s)
48 49 50 51 52
{
	struct wl_connection *connection;

	assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);

53
	connection = wl_connection_create(s[0]);
54 55 56 57 58 59 60 61 62 63
	assert(connection);

	return connection;
}

TEST(connection_create)
{
	struct wl_connection *connection;
	int s[2];

64
	connection = setup(s);
65
	wl_connection_destroy(connection);
66
	close(s[0]);
67 68 69 70 71 72 73 74 75
	close(s[1]);
}

TEST(connection_write)
{
	struct wl_connection *connection;
	int s[2];
	char buffer[64];

76
	connection = setup(s);
77 78

	assert(wl_connection_write(connection, message, sizeof message) == 0);
79
	assert(wl_connection_flush(connection) == sizeof message);
80 81 82 83
	assert(read(s[1], buffer, sizeof buffer) == sizeof message);
	assert(memcmp(message, buffer, sizeof message) == 0);

	wl_connection_destroy(connection);
84
	close(s[0]);
85 86 87 88 89 90 91 92 93
	close(s[1]);
}

TEST(connection_data)
{
	struct wl_connection *connection;
	int s[2];
	char buffer[64];

94
	connection = setup(s);
95 96

	assert(write(s[1], message, sizeof message) == sizeof message);
97
	assert(wl_connection_read(connection) == sizeof message);
98 99 100 101 102
	wl_connection_copy(connection, buffer, sizeof message);
	assert(memcmp(message, buffer, sizeof message) == 0);
	wl_connection_consume(connection, sizeof message);

	wl_connection_destroy(connection);
103
	close(s[0]);
104 105 106 107 108 109 110 111 112
	close(s[1]);
}

TEST(connection_queue)
{
	struct wl_connection *connection;
	int s[2];
	char buffer[64];

113
	connection = setup(s);
114 115

	/* Test that wl_connection_queue() puts data in the output
116 117 118
	 * buffer without flush it.  Verify that the data did get in
	 * the buffer by writing another message and making sure that
	 * 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 125 126 127 128
	assert(read(s[1], buffer, sizeof buffer) == 2 * sizeof message);
	assert(memcmp(message, buffer, sizeof message) == 0);
	assert(memcmp(message, buffer + sizeof message, sizeof message) == 0);

	wl_connection_destroy(connection);
129
	close(s[0]);
130 131
	close(s[1]);
}
132

133 134 135 136 137 138 139 140 141 142 143 144
static void
va_list_wrapper(const char *signature, union wl_argument *args, int count, ...)
{
	va_list ap;
	va_start(ap, count);
	wl_argument_from_va_list(signature, args, count, ap);
	va_end(ap);
}

TEST(argument_from_va_list)
{
	union wl_argument args[WL_CLOSURE_MAX_ARGS];
145
	struct wl_object fake_object, fake_new_object;
146 147 148 149 150 151 152 153 154 155 156
	struct wl_array fake_array;

	va_list_wrapper("i", args, 1, 100);
	assert(args[0].i == 100);

	va_list_wrapper("is", args, 2, 101, "value");
	assert(args[0].i == 101);
	assert(strcmp(args[1].s, "value") == 0);

	va_list_wrapper("?iuf?sonah", args, 8,
			102, 103, wl_fixed_from_int(104), "value",
157
			&fake_object, &fake_new_object, &fake_array, 106);
158 159 160 161 162
	assert(args[0].i == 102);
	assert(args[1].u == 103);
	assert(args[2].f == wl_fixed_from_int(104));
	assert(strcmp(args[3].s, "value") == 0);
	assert(args[4].o == &fake_object);
163
	assert(args[5].o == &fake_new_object);
164 165 166 167
	assert(args[6].a == &fake_array);
	assert(args[7].h == 106);
}

168
struct marshal_data {
169 170
	struct wl_connection *read_connection;
	struct wl_connection *write_connection;
171 172
	int s[2];
	uint32_t buffer[10];
173 174 175 176
	union {
		uint32_t u;
		int32_t i;
		const char *s;
177
		int h;
178
	} value;
179 180
};

181 182 183 184 185
static void
setup_marshal_data(struct marshal_data *data)
{
	assert(socketpair(AF_UNIX,
			  SOCK_STREAM | SOCK_CLOEXEC, 0, data->s) == 0);
186
	data->read_connection = wl_connection_create(data->s[0]);
187
	assert(data->read_connection);
188
	data->write_connection = wl_connection_create(data->s[1]);
189 190 191 192 193 194
	assert(data->write_connection);
}

static void
release_marshal_data(struct marshal_data *data)
{
195 196
	close(wl_connection_destroy(data->read_connection));
	close(wl_connection_destroy(data->write_connection));
197 198
}

199
static void
200
marshal(struct marshal_data *data, const char *format, int size, ...)
201
{
202
	struct wl_closure *closure;
203
	static const uint32_t opcode = 4444;
204
	static struct wl_object sender = { NULL, NULL, 1234 };
205
	struct wl_message message = { "test", format, NULL };
206 207 208
	va_list ap;

	va_start(ap, size);
209
	closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
210 211
	va_end(ap);

212 213 214
	assert(closure);
	assert(wl_closure_send(closure, data->write_connection) == 0);
	wl_closure_destroy(closure);
215
	assert(wl_connection_flush(data->write_connection) == size);
216
	assert(read(data->s[0], data->buffer, sizeof data->buffer) == size);
217 218 219 220 221 222 223 224

	assert(data->buffer[0] == sender.id);
	assert(data->buffer[1] == (opcode | (size << 16)));
}

TEST(connection_marshal)
{
	struct marshal_data data;
225 226 227
	struct wl_object object;
	struct wl_array array;
	static const char text[] = "curry";
228

229
	setup_marshal_data(&data);
230

231
	marshal(&data, "i", 12, 42);
232 233
	assert(data.buffer[2] == 42);

234
	marshal(&data, "u", 12, 55);
235 236
	assert(data.buffer[2] == 55);

237
	marshal(&data, "s", 20, "frappo");
238 239 240
	assert(data.buffer[2] == 7);
	assert(strcmp((char *) &data.buffer[3], "frappo") == 0);

241 242 243 244 245 246 247
	object.id = 557799;
	marshal(&data, "o", 12, &object);
	assert(data.buffer[2] == object.id);

	marshal(&data, "n", 12, &object);
	assert(data.buffer[2] == object.id);

Daniel Stone's avatar
Daniel Stone committed
248
	marshal(&data, "?n", 12, NULL);
249 250 251 252 253 254 255 256
	assert(data.buffer[2] == 0);

	array.data = (void *) text;
	array.size = sizeof text;
	marshal(&data, "a", 20, &array);
	assert(data.buffer[2] == array.size);
	assert(memcmp(&data.buffer[3], text, array.size) == 0);

257
	release_marshal_data(&data);
258
}
259

260 261 262 263 264
static void
expected_fail_marshal(int expected_error, const char *format, ...)
{
	struct wl_closure *closure;
	static const uint32_t opcode = 4444;
Michael Vetter's avatar
Michael Vetter committed
265
	static const struct wl_interface test_interface = {
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
		.name = "test_object"
	};
	static struct wl_object sender = { 0 };
	struct wl_message message = { "test", format, NULL };

	sender.interface = &test_interface;
	sender.id = 1234;
	va_list ap;

	va_start(ap, format);
	closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
	va_end(ap);

	assert(closure == NULL);
	assert(errno == expected_error);
}

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
static void
expected_fail_marshal_send(struct marshal_data *data, int expected_error,
			   const char *format, ...)
{
	struct wl_closure *closure;
	static const uint32_t opcode = 4444;
	static struct wl_object sender = { NULL, NULL, 1234 };
	struct wl_message message = { "test", format, NULL };
	va_list ap;

	va_start(ap, format);
	closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
	va_end(ap);

	assert(closure);
	assert(wl_closure_send(closure, data->write_connection) < 0);
	assert(errno == expected_error);

	wl_closure_destroy(closure);
}

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
TEST(connection_marshal_nullables)
{
	struct marshal_data data;
	struct wl_object object;
	struct wl_array array;
	const char text[] = "curry";

	setup_marshal_data(&data);

	expected_fail_marshal(EINVAL, "o", NULL);
	expected_fail_marshal(EINVAL, "s", NULL);
	expected_fail_marshal(EINVAL, "a", NULL);

	marshal(&data, "?o", 12, NULL);
	assert(data.buffer[2] == 0);

	marshal(&data, "?a", 12, NULL);
	assert(data.buffer[2] == 0);

	marshal(&data, "?s", 12, NULL);
	assert(data.buffer[2] == 0);

	object.id = 55293;
	marshal(&data, "?o", 12, &object);
	assert(data.buffer[2] == object.id);

	array.data = (void *) text;
	array.size = sizeof text;
	marshal(&data, "?a", 20, &array);
	assert(data.buffer[2] == array.size);
	assert(memcmp(&data.buffer[3], text, array.size) == 0);

	marshal(&data, "?s", 20, text);
	assert(data.buffer[2] == sizeof text);
	assert(strcmp((char *) &data.buffer[3], text) == 0);

	release_marshal_data(&data);
}

343
static void
344 345
validate_demarshal_u(struct marshal_data *data,
		     struct wl_object *object, uint32_t u)
346
{
347
	assert(data->value.u == u);
348 349 350
}

static void
351 352
validate_demarshal_i(struct marshal_data *data,
		     struct wl_object *object, int32_t i)
353
{
354
	assert(data->value.i == i);
355 356 357
}

static void
358 359
validate_demarshal_s(struct marshal_data *data,
		     struct wl_object *object, const char *s)
360
{
361 362 363 364
	if (data->value.s != NULL)
		assert(strcmp(data->value.s, s) == 0);
	else
		assert(s == NULL);
365 366
}

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
static void
validate_demarshal_h(struct marshal_data *data,
		     struct wl_object *object, int fd)
{
	struct stat buf1, buf2;

	assert(fd != data->value.h);
	fstat(fd, &buf1);
	fstat(data->value.h, &buf2);
	assert(buf1.st_dev == buf2.st_dev);
	assert(buf1.st_ino == buf2.st_ino);
	close(fd);
	close(data->value.h);
}

382 383 384 385 386 387 388
static void
validate_demarshal_f(struct marshal_data *data,
		     struct wl_object *object, wl_fixed_t f)
{
	assert(data->value.i == f);
}

389 390 391 392 393
static void
demarshal(struct marshal_data *data, const char *format,
	  uint32_t *msg, void (*func)(void))
{
	struct wl_message message = { "test", format, NULL };
394
	struct wl_closure *closure;
395
	struct wl_map objects;
396
	struct wl_object object = { NULL, &func, 0 };
397
	int size = msg[1];
398 399

	assert(write(data->s[1], msg, size) == size);
400
	assert(wl_connection_read(data->read_connection) == size);
401

402
	wl_map_init(&objects, WL_MAP_SERVER_SIDE);
403
	object.id = msg[0];
404 405 406
	closure = wl_connection_demarshal(data->read_connection,
					  size, &objects, &message);
	assert(closure);
407
	wl_closure_invoke(closure, WL_CLOSURE_INVOKE_SERVER, &object, 0, data);
408
	wl_closure_destroy(closure);
409 410 411 412 413 414 415
}

TEST(connection_demarshal)
{
	struct marshal_data data;
	uint32_t msg[10];

416
	setup_marshal_data(&data);
417

418
	data.value.u = 8000;
419 420
	msg[0] = 400200;	/* object id */
	msg[1] = 12;		/* size = 12, opcode = 0 */
421
	msg[2] = data.value.u;
422 423
	demarshal(&data, "u", msg, (void *) validate_demarshal_u);

424
	data.value.i = -557799;
425 426
	msg[0] = 400200;
	msg[1] = 12;
427
	msg[2] = data.value.i;
428 429
	demarshal(&data, "i", msg, (void *) validate_demarshal_i);

430
	data.value.s = "superdude";
431 432 433
	msg[0] = 400200;
	msg[1] = 24;
	msg[2] = 10;
434
	memcpy(&msg[3], data.value.s, msg[2]);
435 436
	demarshal(&data, "s", msg, (void *) validate_demarshal_s);

437 438 439 440 441 442 443
	data.value.s = "superdude";
	msg[0] = 400200;
	msg[1] = 24;
	msg[2] = 10;
	memcpy(&msg[3], data.value.s, msg[2]);
	demarshal(&data, "?s", msg, (void *) validate_demarshal_s);

444 445 446 447 448 449
	data.value.i = wl_fixed_from_double(-90000.2390);
	msg[0] = 400200;
	msg[1] = 12;
	msg[2] = data.value.i;
	demarshal(&data, "f", msg, (void *) validate_demarshal_f);

450 451 452 453
	data.value.s = NULL;
	msg[0] = 400200;
	msg[1] = 12;
	msg[2] = 0;
Michael Vetter's avatar
Michael Vetter committed
454
	demarshal(&data, "?s", msg, (void *) validate_demarshal_s);
455

456
	release_marshal_data(&data);
457
}
458 459

static void
Michael Vetter's avatar
Michael Vetter committed
460
marshal_demarshal(struct marshal_data *data,
461 462
		  void (*func)(void), int size, const char *format, ...)
{
463
	struct wl_closure *closure;
464 465 466 467
	static const int opcode = 4444;
	static struct wl_object sender = { NULL, NULL, 1234 };
	struct wl_message message = { "test", format, NULL };
	struct wl_map objects;
468
	struct wl_object object = { NULL, &func, 0 };
469 470 471 472
	va_list ap;
	uint32_t msg[1] = { 1234 };

	va_start(ap, format);
473
	closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
474 475
	va_end(ap);

476 477 478
	assert(closure);
	assert(wl_closure_send(closure, data->write_connection) == 0);
	wl_closure_destroy(closure);
479 480 481
	assert(wl_connection_flush(data->write_connection) == size);

	assert(wl_connection_read(data->read_connection) == size);
482

483
	wl_map_init(&objects, WL_MAP_SERVER_SIDE);
484
	object.id = msg[0];
485 486
	closure = wl_connection_demarshal(data->read_connection,
					  size, &objects, &message);
487
	assert(closure);
488
	wl_closure_invoke(closure, WL_CLOSURE_INVOKE_SERVER, &object, 0, data);
489
	wl_closure_destroy(closure);
490 491 492 493 494
}

TEST(connection_marshal_demarshal)
{
	struct marshal_data data;
Pekka Paalanen's avatar
Pekka Paalanen committed
495
	char f[] = "/tmp/wayland-tests-XXXXXX";
496 497 498

	setup_marshal_data(&data);

499 500 501 502 503 504 505 506
	data.value.u = 889911;
	marshal_demarshal(&data, (void *) validate_demarshal_u,
			  12, "u", data.value.u);

	data.value.i = -13;
	marshal_demarshal(&data, (void *) validate_demarshal_i,
			  12, "i", data.value.i);

507 508 509
	data.value.s = "cookie robots";
	marshal_demarshal(&data, (void *) validate_demarshal_s,
			  28, "s", data.value.s);
510

511 512 513 514
	data.value.s = "cookie robots";
	marshal_demarshal(&data, (void *) validate_demarshal_s,
			  28, "?s", data.value.s);

515 516
	data.value.h = mkstemp(f);
	assert(data.value.h >= 0);
517
	unlink(f);
518 519 520
	marshal_demarshal(&data, (void *) validate_demarshal_h,
			  8, "h", data.value.h);

521 522 523 524 525 526 527 528 529 530 531 532
	data.value.i = wl_fixed_from_double(1234.5678);
	marshal_demarshal(&data, (void *) validate_demarshal_f,
	                  12, "f", data.value.i);

	data.value.i = wl_fixed_from_double(-90000.2390);
	marshal_demarshal(&data, (void *) validate_demarshal_f,
	                  12, "f", data.value.i);

	data.value.i = wl_fixed_from_double((1 << 23) - 1 + 0.0941);
	marshal_demarshal(&data, (void *) validate_demarshal_f,
	                  12, "f", data.value.i);

533 534
	release_marshal_data(&data);
}
535 536 537 538 539 540 541 542

TEST(connection_marshal_alot)
{
	struct marshal_data data;
	char f[64];
	int i;

	setup_marshal_data(&data);
Michael Vetter's avatar
Michael Vetter committed
543

544 545 546 547
	/* We iterate enough to make sure we wrap the circular buffers
	 * for both regular data an fds. */

	for (i = 0; i < 2000; i++) {
Pekka Paalanen's avatar
Pekka Paalanen committed
548
		strcpy(f, "/tmp/wayland-tests-XXXXXX");
549 550
		data.value.h = mkstemp(f);
		assert(data.value.h >= 0);
551
		unlink(f);
552 553 554 555 556 557
		marshal_demarshal(&data, (void *) validate_demarshal_h,
				  8, "h", data.value.h);
	}

	release_marshal_data(&data);
}
558

559 560 561 562 563
TEST(connection_marshal_too_big)
{
	struct marshal_data data;
	char *big_string = malloc(5000);

564 565
	assert(big_string);

566 567 568 569 570 571 572 573 574 575 576
	memset(big_string, ' ', 4999);
	big_string[4999] = '\0';

	setup_marshal_data(&data);

	expected_fail_marshal_send(&data, E2BIG, "s", big_string);

	release_marshal_data(&data);
	free(big_string);
}

577 578 579
static void
marshal_helper(const char *format, void *handler, ...)
{
580
	struct wl_closure *closure;
581 582
	static struct wl_object sender = { NULL, NULL, 1234 };
	struct wl_object object = { NULL, &handler, 0 };
583 584 585
	static const int opcode = 4444;
	struct wl_message message = { "test", format, NULL };
	va_list ap;
586
	int done;
587 588

	va_start(ap, handler);
589
	closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
590 591
	va_end(ap);

592
	assert(closure);
593
	done = 0;
594
	wl_closure_invoke(closure, WL_CLOSURE_INVOKE_SERVER, &object, 0, &done);
595
	wl_closure_destroy(closure);
596 597 598 599 600 601 602 603 604 605
	assert(done);
}

static void
suu_handler(void *data, struct wl_object *object,
	    const char *s, uint32_t u1, uint32_t u2)
{
	int *done = data;

	assert(strcmp(s, "foo") == 0);
Eric Engestrom's avatar
Eric Engestrom committed
606 607
	assert(u1 == 500);
	assert(u2 == 404040);
608 609 610 611 612 613 614
	*done = 1;
}

TEST(invoke_closure)
{
	marshal_helper("suu", suu_handler, "foo", 500, 404040);
}
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652

static void
leak_closure(void)
{
	struct wl_callback *cb;
	struct pollfd pfd;
	struct client *c = client_connect();

	cb = wl_display_sync(c->wl_display);
	assert(cb);
	assert(wl_display_flush(c->wl_display) > 0);

	/* we don't need it, it is referenced */
	wl_callback_destroy(cb);

	pfd.fd = wl_display_get_fd(c->wl_display);
	pfd.events = POLLIN;

	test_set_timeout(2);
	assert(poll(&pfd, 1, -1) == 1);

	/* read events, but do not dispatch them */
	assert(wl_display_prepare_read(c->wl_display) == 0);
	assert(wl_display_read_events(c->wl_display) == 0);

	/*
	 * now we have wl_callback.done and wl_display.delete_id queued;
	 * if we now release the queue (in wl_display_disconnect())
	 * we should not leak memory
	 */

	client_disconnect(c);
}

TEST(closure_leaks)
{
	struct display *d = display_create();

653
	client_create_noarg(d, leak_closure);
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
	display_run(d);

	display_destroy(d);
}

static void
leak_after_error(void)
{
	struct client *c = client_connect();

	/* this should return -1, because we'll send error
	 * from server. */
	assert(stop_display(c, 1) == -1);
	assert(wl_display_dispatch_pending(c->wl_display) == -1);
	assert(wl_display_get_error(c->wl_display) == ENOMEM);

	/* after we got error, we have display_resume event
	 * in the queue. It should be freed in wl_display_disconnect().
	 * Let's see! */

	wl_proxy_destroy((struct wl_proxy *) c->tc);
	wl_display_disconnect(c->wl_display);
	free(c);
}

TEST(closure_leaks_after_error)
{
	struct display *d = display_create();
	struct client_info *cl;

684
	cl = client_create_noarg(d, leak_after_error);
685 686 687 688 689 690 691
	display_run(d);

	wl_client_post_no_memory(cl->wl_client);
	display_resume(d);

	display_destroy(d);
}