pflash: Use ERRP_GUARD()
[qemu/ar7.git] / tests / test-io-channel-socket.c
blobd43083a766713df3910da3fc1d24d23b6cd99535
1 /*
2 * QEMU I/O channel sockets test
4 * Copyright (c) 2015-2016 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "io/channel-socket.h"
23 #include "io/channel-util.h"
24 #include "io-channel-helpers.h"
25 #include "socket-helpers.h"
26 #include "qapi/error.h"
27 #include "qemu/module.h"
30 static void test_io_channel_set_socket_bufs(QIOChannel *src,
31 QIOChannel *dst)
33 int buflen = 64 * 1024;
36 * Make the socket buffers small so that we see
37 * the effects of partial reads/writes
39 setsockopt(((QIOChannelSocket *)src)->fd,
40 SOL_SOCKET, SO_SNDBUF,
41 (char *)&buflen,
42 sizeof(buflen));
44 setsockopt(((QIOChannelSocket *)dst)->fd,
45 SOL_SOCKET, SO_SNDBUF,
46 (char *)&buflen,
47 sizeof(buflen));
51 static void test_io_channel_setup_sync(SocketAddress *listen_addr,
52 SocketAddress *connect_addr,
53 QIOChannel **srv,
54 QIOChannel **src,
55 QIOChannel **dst)
57 QIOChannelSocket *lioc;
59 lioc = qio_channel_socket_new();
60 qio_channel_socket_listen_sync(lioc, listen_addr, 1, &error_abort);
62 if (listen_addr->type == SOCKET_ADDRESS_TYPE_INET) {
63 SocketAddress *laddr = qio_channel_socket_get_local_address(
64 lioc, &error_abort);
66 g_free(connect_addr->u.inet.port);
67 connect_addr->u.inet.port = g_strdup(laddr->u.inet.port);
69 qapi_free_SocketAddress(laddr);
72 *src = QIO_CHANNEL(qio_channel_socket_new());
73 qio_channel_socket_connect_sync(
74 QIO_CHANNEL_SOCKET(*src), connect_addr, &error_abort);
75 qio_channel_set_delay(*src, false);
77 qio_channel_wait(QIO_CHANNEL(lioc), G_IO_IN);
78 *dst = QIO_CHANNEL(qio_channel_socket_accept(lioc, &error_abort));
79 g_assert(*dst);
81 test_io_channel_set_socket_bufs(*src, *dst);
83 *srv = QIO_CHANNEL(lioc);
87 struct TestIOChannelData {
88 bool err;
89 GMainLoop *loop;
93 static void test_io_channel_complete(QIOTask *task,
94 gpointer opaque)
96 struct TestIOChannelData *data = opaque;
97 data->err = qio_task_propagate_error(task, NULL);
98 g_main_loop_quit(data->loop);
102 static void test_io_channel_setup_async(SocketAddress *listen_addr,
103 SocketAddress *connect_addr,
104 QIOChannel **srv,
105 QIOChannel **src,
106 QIOChannel **dst)
108 QIOChannelSocket *lioc;
109 struct TestIOChannelData data;
111 data.loop = g_main_loop_new(g_main_context_default(),
112 TRUE);
114 lioc = qio_channel_socket_new();
115 qio_channel_socket_listen_async(
116 lioc, listen_addr, 1,
117 test_io_channel_complete, &data, NULL, NULL);
119 g_main_loop_run(data.loop);
120 g_main_context_iteration(g_main_context_default(), FALSE);
122 g_assert(!data.err);
124 if (listen_addr->type == SOCKET_ADDRESS_TYPE_INET) {
125 SocketAddress *laddr = qio_channel_socket_get_local_address(
126 lioc, &error_abort);
128 g_free(connect_addr->u.inet.port);
129 connect_addr->u.inet.port = g_strdup(laddr->u.inet.port);
131 qapi_free_SocketAddress(laddr);
134 *src = QIO_CHANNEL(qio_channel_socket_new());
136 qio_channel_socket_connect_async(
137 QIO_CHANNEL_SOCKET(*src), connect_addr,
138 test_io_channel_complete, &data, NULL, NULL);
140 g_main_loop_run(data.loop);
141 g_main_context_iteration(g_main_context_default(), FALSE);
143 g_assert(!data.err);
145 qio_channel_wait(QIO_CHANNEL(lioc), G_IO_IN);
146 *dst = QIO_CHANNEL(qio_channel_socket_accept(lioc, &error_abort));
147 g_assert(*dst);
149 qio_channel_set_delay(*src, false);
150 test_io_channel_set_socket_bufs(*src, *dst);
152 *srv = QIO_CHANNEL(lioc);
154 g_main_loop_unref(data.loop);
158 static void test_io_channel_socket_path_exists(SocketAddress *addr,
159 bool expectExists)
161 if (addr->type != SOCKET_ADDRESS_TYPE_UNIX) {
162 return;
165 g_assert(g_file_test(addr->u.q_unix.path,
166 G_FILE_TEST_EXISTS) == expectExists);
170 static void test_io_channel(bool async,
171 SocketAddress *listen_addr,
172 SocketAddress *connect_addr,
173 bool passFD)
175 QIOChannel *src, *dst, *srv;
176 QIOChannelTest *test;
177 if (async) {
178 test_io_channel_setup_async(listen_addr, connect_addr,
179 &srv, &src, &dst);
181 g_assert(!passFD ||
182 qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
183 g_assert(!passFD ||
184 qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_FD_PASS));
185 g_assert(qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_SHUTDOWN));
186 g_assert(qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_SHUTDOWN));
188 test_io_channel_socket_path_exists(listen_addr, true);
190 test = qio_channel_test_new();
191 qio_channel_test_run_threads(test, true, src, dst);
192 qio_channel_test_validate(test);
194 test_io_channel_socket_path_exists(listen_addr, true);
196 /* unref without close, to ensure finalize() cleans up */
198 object_unref(OBJECT(src));
199 object_unref(OBJECT(dst));
200 test_io_channel_socket_path_exists(listen_addr, true);
202 object_unref(OBJECT(srv));
203 test_io_channel_socket_path_exists(listen_addr, false);
205 test_io_channel_setup_async(listen_addr, connect_addr,
206 &srv, &src, &dst);
208 g_assert(!passFD ||
209 qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
210 g_assert(!passFD ||
211 qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_FD_PASS));
212 g_assert(qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_SHUTDOWN));
213 g_assert(qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_SHUTDOWN));
215 test = qio_channel_test_new();
216 qio_channel_test_run_threads(test, false, src, dst);
217 qio_channel_test_validate(test);
219 /* close before unref, to ensure finalize copes with already closed */
221 qio_channel_close(src, &error_abort);
222 qio_channel_close(dst, &error_abort);
223 test_io_channel_socket_path_exists(listen_addr, true);
225 object_unref(OBJECT(src));
226 object_unref(OBJECT(dst));
227 test_io_channel_socket_path_exists(listen_addr, true);
229 qio_channel_close(srv, &error_abort);
230 test_io_channel_socket_path_exists(listen_addr, false);
232 object_unref(OBJECT(srv));
233 test_io_channel_socket_path_exists(listen_addr, false);
234 } else {
235 test_io_channel_setup_sync(listen_addr, connect_addr,
236 &srv, &src, &dst);
238 g_assert(!passFD ||
239 qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
240 g_assert(!passFD ||
241 qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_FD_PASS));
242 g_assert(qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_SHUTDOWN));
243 g_assert(qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_SHUTDOWN));
245 test_io_channel_socket_path_exists(listen_addr, true);
247 test = qio_channel_test_new();
248 qio_channel_test_run_threads(test, true, src, dst);
249 qio_channel_test_validate(test);
251 test_io_channel_socket_path_exists(listen_addr, true);
253 /* unref without close, to ensure finalize() cleans up */
255 object_unref(OBJECT(src));
256 object_unref(OBJECT(dst));
257 test_io_channel_socket_path_exists(listen_addr, true);
259 object_unref(OBJECT(srv));
260 test_io_channel_socket_path_exists(listen_addr, false);
262 test_io_channel_setup_sync(listen_addr, connect_addr,
263 &srv, &src, &dst);
265 g_assert(!passFD ||
266 qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
267 g_assert(!passFD ||
268 qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_FD_PASS));
269 g_assert(qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_SHUTDOWN));
270 g_assert(qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_SHUTDOWN));
272 test = qio_channel_test_new();
273 qio_channel_test_run_threads(test, false, src, dst);
274 qio_channel_test_validate(test);
276 test_io_channel_socket_path_exists(listen_addr, true);
278 /* close before unref, to ensure finalize copes with already closed */
280 qio_channel_close(src, &error_abort);
281 qio_channel_close(dst, &error_abort);
282 test_io_channel_socket_path_exists(listen_addr, true);
284 object_unref(OBJECT(src));
285 object_unref(OBJECT(dst));
286 test_io_channel_socket_path_exists(listen_addr, true);
288 qio_channel_close(srv, &error_abort);
289 test_io_channel_socket_path_exists(listen_addr, false);
291 object_unref(OBJECT(srv));
292 test_io_channel_socket_path_exists(listen_addr, false);
297 static void test_io_channel_ipv4(bool async)
299 SocketAddress *listen_addr = g_new0(SocketAddress, 1);
300 SocketAddress *connect_addr = g_new0(SocketAddress, 1);
302 listen_addr->type = SOCKET_ADDRESS_TYPE_INET;
303 listen_addr->u.inet = (InetSocketAddress) {
304 .host = g_strdup("127.0.0.1"),
305 .port = NULL, /* Auto-select */
308 connect_addr->type = SOCKET_ADDRESS_TYPE_INET;
309 connect_addr->u.inet = (InetSocketAddress) {
310 .host = g_strdup("127.0.0.1"),
311 .port = NULL, /* Filled in later */
314 test_io_channel(async, listen_addr, connect_addr, false);
316 qapi_free_SocketAddress(listen_addr);
317 qapi_free_SocketAddress(connect_addr);
321 static void test_io_channel_ipv4_sync(void)
323 return test_io_channel_ipv4(false);
327 static void test_io_channel_ipv4_async(void)
329 return test_io_channel_ipv4(true);
333 static void test_io_channel_ipv6(bool async)
335 SocketAddress *listen_addr = g_new0(SocketAddress, 1);
336 SocketAddress *connect_addr = g_new0(SocketAddress, 1);
338 listen_addr->type = SOCKET_ADDRESS_TYPE_INET;
339 listen_addr->u.inet = (InetSocketAddress) {
340 .host = g_strdup("::1"),
341 .port = NULL, /* Auto-select */
344 connect_addr->type = SOCKET_ADDRESS_TYPE_INET;
345 connect_addr->u.inet = (InetSocketAddress) {
346 .host = g_strdup("::1"),
347 .port = NULL, /* Filled in later */
350 test_io_channel(async, listen_addr, connect_addr, false);
352 qapi_free_SocketAddress(listen_addr);
353 qapi_free_SocketAddress(connect_addr);
357 static void test_io_channel_ipv6_sync(void)
359 return test_io_channel_ipv6(false);
363 static void test_io_channel_ipv6_async(void)
365 return test_io_channel_ipv6(true);
369 #ifndef _WIN32
370 static void test_io_channel_unix(bool async)
372 SocketAddress *listen_addr = g_new0(SocketAddress, 1);
373 SocketAddress *connect_addr = g_new0(SocketAddress, 1);
375 #define TEST_SOCKET "test-io-channel-socket.sock"
376 listen_addr->type = SOCKET_ADDRESS_TYPE_UNIX;
377 listen_addr->u.q_unix.path = g_strdup(TEST_SOCKET);
379 connect_addr->type = SOCKET_ADDRESS_TYPE_UNIX;
380 connect_addr->u.q_unix.path = g_strdup(TEST_SOCKET);
382 test_io_channel(async, listen_addr, connect_addr, true);
384 qapi_free_SocketAddress(listen_addr);
385 qapi_free_SocketAddress(connect_addr);
389 static void test_io_channel_unix_sync(void)
391 return test_io_channel_unix(false);
395 static void test_io_channel_unix_async(void)
397 return test_io_channel_unix(true);
400 static void test_io_channel_unix_fd_pass(void)
402 SocketAddress *listen_addr = g_new0(SocketAddress, 1);
403 SocketAddress *connect_addr = g_new0(SocketAddress, 1);
404 QIOChannel *src, *dst, *srv;
405 int testfd;
406 int fdsend[3];
407 int *fdrecv = NULL;
408 size_t nfdrecv = 0;
409 size_t i;
410 char bufsend[12], bufrecv[12];
411 struct iovec iosend[1], iorecv[1];
413 #define TEST_SOCKET "test-io-channel-socket.sock"
414 #define TEST_FILE "test-io-channel-socket.txt"
416 testfd = open(TEST_FILE, O_RDWR|O_TRUNC|O_CREAT, 0700);
417 g_assert(testfd != -1);
418 fdsend[0] = testfd;
419 fdsend[1] = testfd;
420 fdsend[2] = testfd;
422 listen_addr->type = SOCKET_ADDRESS_TYPE_UNIX;
423 listen_addr->u.q_unix.path = g_strdup(TEST_SOCKET);
425 connect_addr->type = SOCKET_ADDRESS_TYPE_UNIX;
426 connect_addr->u.q_unix.path = g_strdup(TEST_SOCKET);
428 test_io_channel_setup_sync(listen_addr, connect_addr, &srv, &src, &dst);
430 memcpy(bufsend, "Hello World", G_N_ELEMENTS(bufsend));
432 iosend[0].iov_base = bufsend;
433 iosend[0].iov_len = G_N_ELEMENTS(bufsend);
435 iorecv[0].iov_base = bufrecv;
436 iorecv[0].iov_len = G_N_ELEMENTS(bufrecv);
438 g_assert(qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
439 g_assert(qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_FD_PASS));
441 qio_channel_writev_full(src,
442 iosend,
443 G_N_ELEMENTS(iosend),
444 fdsend,
445 G_N_ELEMENTS(fdsend),
446 &error_abort);
448 qio_channel_readv_full(dst,
449 iorecv,
450 G_N_ELEMENTS(iorecv),
451 &fdrecv,
452 &nfdrecv,
453 &error_abort);
455 g_assert(nfdrecv == G_N_ELEMENTS(fdsend));
456 /* Each recvd FD should be different from sent FD */
457 for (i = 0; i < nfdrecv; i++) {
458 g_assert_cmpint(fdrecv[i], !=, testfd);
460 /* Each recvd FD should be different from each other */
461 g_assert_cmpint(fdrecv[0], !=, fdrecv[1]);
462 g_assert_cmpint(fdrecv[0], !=, fdrecv[2]);
463 g_assert_cmpint(fdrecv[1], !=, fdrecv[2]);
465 /* Check the I/O buf we sent at the same time matches */
466 g_assert(memcmp(bufsend, bufrecv, G_N_ELEMENTS(bufsend)) == 0);
468 /* Write some data into the FD we received */
469 g_assert(write(fdrecv[0], bufsend, G_N_ELEMENTS(bufsend)) ==
470 G_N_ELEMENTS(bufsend));
472 /* Read data from the original FD and make sure it matches */
473 memset(bufrecv, 0, G_N_ELEMENTS(bufrecv));
474 g_assert(lseek(testfd, 0, SEEK_SET) == 0);
475 g_assert(read(testfd, bufrecv, G_N_ELEMENTS(bufrecv)) ==
476 G_N_ELEMENTS(bufrecv));
477 g_assert(memcmp(bufsend, bufrecv, G_N_ELEMENTS(bufsend)) == 0);
479 object_unref(OBJECT(src));
480 object_unref(OBJECT(dst));
481 object_unref(OBJECT(srv));
482 qapi_free_SocketAddress(listen_addr);
483 qapi_free_SocketAddress(connect_addr);
484 unlink(TEST_SOCKET);
485 unlink(TEST_FILE);
486 close(testfd);
487 for (i = 0; i < nfdrecv; i++) {
488 close(fdrecv[i]);
490 g_free(fdrecv);
493 static void test_io_channel_unix_listen_cleanup(void)
495 QIOChannelSocket *ioc;
496 struct sockaddr_un un;
497 int sock;
499 #define TEST_SOCKET "test-io-channel-socket.sock"
501 ioc = qio_channel_socket_new();
503 /* Manually bind ioc without calling the qio api to avoid setting
504 * the LISTEN feature */
505 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
506 memset(&un, 0, sizeof(un));
507 un.sun_family = AF_UNIX;
508 snprintf(un.sun_path, sizeof(un.sun_path), "%s", TEST_SOCKET);
509 unlink(TEST_SOCKET);
510 bind(sock, (struct sockaddr *)&un, sizeof(un));
511 ioc->fd = sock;
512 ioc->localAddrLen = sizeof(ioc->localAddr);
513 getsockname(sock, (struct sockaddr *)&ioc->localAddr,
514 &ioc->localAddrLen);
516 g_assert(g_file_test(TEST_SOCKET, G_FILE_TEST_EXISTS));
517 object_unref(OBJECT(ioc));
518 g_assert(g_file_test(TEST_SOCKET, G_FILE_TEST_EXISTS));
520 unlink(TEST_SOCKET);
523 #endif /* _WIN32 */
526 static void test_io_channel_ipv4_fd(void)
528 QIOChannel *ioc;
529 int fd = -1;
530 struct sockaddr_in sa = {
531 .sin_family = AF_INET,
532 .sin_addr = {
533 .s_addr = htonl(INADDR_LOOPBACK),
535 /* Leave port unset for auto-assign */
537 socklen_t salen = sizeof(sa);
539 fd = socket(AF_INET, SOCK_STREAM, 0);
540 g_assert_cmpint(fd, >, -1);
542 g_assert_cmpint(bind(fd, (struct sockaddr *)&sa, salen), ==, 0);
544 ioc = qio_channel_new_fd(fd, &error_abort);
546 g_assert_cmpstr(object_get_typename(OBJECT(ioc)),
548 TYPE_QIO_CHANNEL_SOCKET);
550 object_unref(OBJECT(ioc));
554 int main(int argc, char **argv)
556 bool has_ipv4, has_ipv6;
558 module_call_init(MODULE_INIT_QOM);
559 socket_init();
561 g_test_init(&argc, &argv, NULL);
563 /* We're creating actual IPv4/6 sockets, so we should
564 * check if the host running tests actually supports
565 * each protocol to avoid breaking tests on machines
566 * with either IPv4 or IPv6 disabled.
568 if (socket_check_protocol_support(&has_ipv4, &has_ipv6) < 0) {
569 g_printerr("socket_check_protocol_support() failed\n");
570 goto end;
573 if (has_ipv4) {
574 g_test_add_func("/io/channel/socket/ipv4-sync",
575 test_io_channel_ipv4_sync);
576 g_test_add_func("/io/channel/socket/ipv4-async",
577 test_io_channel_ipv4_async);
578 g_test_add_func("/io/channel/socket/ipv4-fd",
579 test_io_channel_ipv4_fd);
581 if (has_ipv6) {
582 g_test_add_func("/io/channel/socket/ipv6-sync",
583 test_io_channel_ipv6_sync);
584 g_test_add_func("/io/channel/socket/ipv6-async",
585 test_io_channel_ipv6_async);
588 #ifndef _WIN32
589 g_test_add_func("/io/channel/socket/unix-sync",
590 test_io_channel_unix_sync);
591 g_test_add_func("/io/channel/socket/unix-async",
592 test_io_channel_unix_async);
593 g_test_add_func("/io/channel/socket/unix-fd-pass",
594 test_io_channel_unix_fd_pass);
595 g_test_add_func("/io/channel/socket/unix-listen-cleanup",
596 test_io_channel_unix_listen_cleanup);
597 #endif /* _WIN32 */
599 end:
600 return g_test_run();