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.1 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"
28 #include "qemu/main-loop.h"
31 static void test_io_channel_set_socket_bufs(QIOChannel
*src
,
34 int buflen
= 64 * 1024;
37 * Make the socket buffers small so that we see
38 * the effects of partial reads/writes
40 setsockopt(((QIOChannelSocket
*)src
)->fd
,
41 SOL_SOCKET
, SO_SNDBUF
,
45 setsockopt(((QIOChannelSocket
*)dst
)->fd
,
46 SOL_SOCKET
, SO_SNDBUF
,
52 static void test_io_channel_setup_sync(SocketAddress
*listen_addr
,
53 SocketAddress
*connect_addr
,
58 QIOChannelSocket
*lioc
;
60 lioc
= qio_channel_socket_new();
61 qio_channel_socket_listen_sync(lioc
, listen_addr
, 1, &error_abort
);
63 if (listen_addr
->type
== SOCKET_ADDRESS_TYPE_INET
) {
64 SocketAddress
*laddr
= qio_channel_socket_get_local_address(
67 g_free(connect_addr
->u
.inet
.port
);
68 connect_addr
->u
.inet
.port
= g_strdup(laddr
->u
.inet
.port
);
70 qapi_free_SocketAddress(laddr
);
73 *src
= QIO_CHANNEL(qio_channel_socket_new());
74 qio_channel_socket_connect_sync(
75 QIO_CHANNEL_SOCKET(*src
), connect_addr
, &error_abort
);
76 qio_channel_set_delay(*src
, false);
78 qio_channel_wait(QIO_CHANNEL(lioc
), G_IO_IN
);
79 *dst
= QIO_CHANNEL(qio_channel_socket_accept(lioc
, &error_abort
));
82 test_io_channel_set_socket_bufs(*src
, *dst
);
84 *srv
= QIO_CHANNEL(lioc
);
88 struct TestIOChannelData
{
94 static void test_io_channel_complete(QIOTask
*task
,
97 struct TestIOChannelData
*data
= opaque
;
98 data
->err
= qio_task_propagate_error(task
, NULL
);
99 g_main_loop_quit(data
->loop
);
103 static void test_io_channel_setup_async(SocketAddress
*listen_addr
,
104 SocketAddress
*connect_addr
,
109 QIOChannelSocket
*lioc
;
110 struct TestIOChannelData data
;
112 data
.loop
= g_main_loop_new(g_main_context_default(),
115 lioc
= qio_channel_socket_new();
116 qio_channel_socket_listen_async(
117 lioc
, listen_addr
, 1,
118 test_io_channel_complete
, &data
, NULL
, NULL
);
120 g_main_loop_run(data
.loop
);
121 g_main_context_iteration(g_main_context_default(), FALSE
);
125 if (listen_addr
->type
== SOCKET_ADDRESS_TYPE_INET
) {
126 SocketAddress
*laddr
= qio_channel_socket_get_local_address(
129 g_free(connect_addr
->u
.inet
.port
);
130 connect_addr
->u
.inet
.port
= g_strdup(laddr
->u
.inet
.port
);
132 qapi_free_SocketAddress(laddr
);
135 *src
= QIO_CHANNEL(qio_channel_socket_new());
137 qio_channel_socket_connect_async(
138 QIO_CHANNEL_SOCKET(*src
), connect_addr
,
139 test_io_channel_complete
, &data
, NULL
, NULL
);
141 g_main_loop_run(data
.loop
);
142 g_main_context_iteration(g_main_context_default(), FALSE
);
146 qio_channel_wait(QIO_CHANNEL(lioc
), G_IO_IN
);
147 *dst
= QIO_CHANNEL(qio_channel_socket_accept(lioc
, &error_abort
));
150 qio_channel_set_delay(*src
, false);
151 test_io_channel_set_socket_bufs(*src
, *dst
);
153 *srv
= QIO_CHANNEL(lioc
);
155 g_main_loop_unref(data
.loop
);
159 static void test_io_channel_socket_path_exists(SocketAddress
*addr
,
162 if (addr
->type
!= SOCKET_ADDRESS_TYPE_UNIX
) {
166 g_assert(g_file_test(addr
->u
.q_unix
.path
,
167 G_FILE_TEST_EXISTS
) == expectExists
);
171 static void test_io_channel(bool async
,
172 SocketAddress
*listen_addr
,
173 SocketAddress
*connect_addr
,
176 QIOChannel
*src
, *dst
, *srv
;
177 QIOChannelTest
*test
;
179 test_io_channel_setup_async(listen_addr
, connect_addr
,
184 qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_FD_PASS
));
186 qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_FD_PASS
));
188 g_assert(qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_SHUTDOWN
));
189 g_assert(qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_SHUTDOWN
));
191 test_io_channel_socket_path_exists(listen_addr
, true);
193 test
= qio_channel_test_new();
194 qio_channel_test_run_threads(test
, true, src
, dst
);
195 qio_channel_test_validate(test
);
197 test_io_channel_socket_path_exists(listen_addr
, true);
199 /* unref without close, to ensure finalize() cleans up */
201 object_unref(OBJECT(src
));
202 object_unref(OBJECT(dst
));
203 test_io_channel_socket_path_exists(listen_addr
, true);
205 object_unref(OBJECT(srv
));
206 test_io_channel_socket_path_exists(listen_addr
, false);
208 test_io_channel_setup_async(listen_addr
, connect_addr
,
213 qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_FD_PASS
));
215 qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_FD_PASS
));
217 g_assert(qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_SHUTDOWN
));
218 g_assert(qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_SHUTDOWN
));
220 test
= qio_channel_test_new();
221 qio_channel_test_run_threads(test
, false, src
, dst
);
222 qio_channel_test_validate(test
);
224 /* close before unref, to ensure finalize copes with already closed */
226 qio_channel_close(src
, &error_abort
);
227 qio_channel_close(dst
, &error_abort
);
228 test_io_channel_socket_path_exists(listen_addr
, true);
230 object_unref(OBJECT(src
));
231 object_unref(OBJECT(dst
));
232 test_io_channel_socket_path_exists(listen_addr
, true);
234 qio_channel_close(srv
, &error_abort
);
235 test_io_channel_socket_path_exists(listen_addr
, false);
237 object_unref(OBJECT(srv
));
238 test_io_channel_socket_path_exists(listen_addr
, false);
240 test_io_channel_setup_sync(listen_addr
, connect_addr
,
245 qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_FD_PASS
));
247 qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_FD_PASS
));
249 g_assert(qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_SHUTDOWN
));
250 g_assert(qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_SHUTDOWN
));
252 test_io_channel_socket_path_exists(listen_addr
, true);
254 test
= qio_channel_test_new();
255 qio_channel_test_run_threads(test
, true, src
, dst
);
256 qio_channel_test_validate(test
);
258 test_io_channel_socket_path_exists(listen_addr
, true);
260 /* unref without close, to ensure finalize() cleans up */
262 object_unref(OBJECT(src
));
263 object_unref(OBJECT(dst
));
264 test_io_channel_socket_path_exists(listen_addr
, true);
266 object_unref(OBJECT(srv
));
267 test_io_channel_socket_path_exists(listen_addr
, false);
269 test_io_channel_setup_sync(listen_addr
, connect_addr
,
274 qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_FD_PASS
));
276 qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_FD_PASS
));
278 g_assert(qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_SHUTDOWN
));
279 g_assert(qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_SHUTDOWN
));
281 test
= qio_channel_test_new();
282 qio_channel_test_run_threads(test
, false, src
, dst
);
283 qio_channel_test_validate(test
);
285 test_io_channel_socket_path_exists(listen_addr
, true);
287 /* close before unref, to ensure finalize copes with already closed */
289 qio_channel_close(src
, &error_abort
);
290 qio_channel_close(dst
, &error_abort
);
291 test_io_channel_socket_path_exists(listen_addr
, true);
293 object_unref(OBJECT(src
));
294 object_unref(OBJECT(dst
));
295 test_io_channel_socket_path_exists(listen_addr
, true);
297 qio_channel_close(srv
, &error_abort
);
298 test_io_channel_socket_path_exists(listen_addr
, false);
300 object_unref(OBJECT(srv
));
301 test_io_channel_socket_path_exists(listen_addr
, false);
306 static void test_io_channel_ipv4(bool async
)
308 SocketAddress
*listen_addr
= g_new0(SocketAddress
, 1);
309 SocketAddress
*connect_addr
= g_new0(SocketAddress
, 1);
311 listen_addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
312 listen_addr
->u
.inet
= (InetSocketAddress
) {
313 .host
= g_strdup("127.0.0.1"),
314 .port
= NULL
, /* Auto-select */
317 connect_addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
318 connect_addr
->u
.inet
= (InetSocketAddress
) {
319 .host
= g_strdup("127.0.0.1"),
320 .port
= NULL
, /* Filled in later */
323 test_io_channel(async
, listen_addr
, connect_addr
, false);
325 qapi_free_SocketAddress(listen_addr
);
326 qapi_free_SocketAddress(connect_addr
);
330 static void test_io_channel_ipv4_sync(void)
332 return test_io_channel_ipv4(false);
336 static void test_io_channel_ipv4_async(void)
338 return test_io_channel_ipv4(true);
342 static void test_io_channel_ipv6(bool async
)
344 SocketAddress
*listen_addr
= g_new0(SocketAddress
, 1);
345 SocketAddress
*connect_addr
= g_new0(SocketAddress
, 1);
347 listen_addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
348 listen_addr
->u
.inet
= (InetSocketAddress
) {
349 .host
= g_strdup("::1"),
350 .port
= NULL
, /* Auto-select */
353 connect_addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
354 connect_addr
->u
.inet
= (InetSocketAddress
) {
355 .host
= g_strdup("::1"),
356 .port
= NULL
, /* Filled in later */
359 test_io_channel(async
, listen_addr
, connect_addr
, false);
361 qapi_free_SocketAddress(listen_addr
);
362 qapi_free_SocketAddress(connect_addr
);
366 static void test_io_channel_ipv6_sync(void)
368 return test_io_channel_ipv6(false);
372 static void test_io_channel_ipv6_async(void)
374 return test_io_channel_ipv6(true);
378 static void test_io_channel_unix(bool async
)
380 SocketAddress
*listen_addr
= g_new0(SocketAddress
, 1);
381 SocketAddress
*connect_addr
= g_new0(SocketAddress
, 1);
383 #define TEST_SOCKET "test-io-channel-socket.sock"
384 listen_addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
385 listen_addr
->u
.q_unix
.path
= g_strdup(TEST_SOCKET
);
387 connect_addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
388 connect_addr
->u
.q_unix
.path
= g_strdup(TEST_SOCKET
);
390 test_io_channel(async
, listen_addr
, connect_addr
, true);
392 qapi_free_SocketAddress(listen_addr
);
393 qapi_free_SocketAddress(connect_addr
);
397 static void test_io_channel_unix_sync(void)
399 return test_io_channel_unix(false);
403 static void test_io_channel_unix_async(void)
405 return test_io_channel_unix(true);
409 static void test_io_channel_unix_fd_pass(void)
411 SocketAddress
*listen_addr
= g_new0(SocketAddress
, 1);
412 SocketAddress
*connect_addr
= g_new0(SocketAddress
, 1);
413 QIOChannel
*src
, *dst
, *srv
;
419 char bufsend
[12], bufrecv
[12];
420 struct iovec iosend
[1], iorecv
[1];
422 #define TEST_SOCKET "test-io-channel-socket.sock"
423 #define TEST_FILE "test-io-channel-socket.txt"
425 testfd
= open(TEST_FILE
, O_RDWR
|O_TRUNC
|O_CREAT
, 0700);
426 g_assert(testfd
!= -1);
431 listen_addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
432 listen_addr
->u
.q_unix
.path
= g_strdup(TEST_SOCKET
);
434 connect_addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
435 connect_addr
->u
.q_unix
.path
= g_strdup(TEST_SOCKET
);
437 test_io_channel_setup_sync(listen_addr
, connect_addr
, &srv
, &src
, &dst
);
439 memcpy(bufsend
, "Hello World", G_N_ELEMENTS(bufsend
));
441 iosend
[0].iov_base
= bufsend
;
442 iosend
[0].iov_len
= G_N_ELEMENTS(bufsend
);
444 iorecv
[0].iov_base
= bufrecv
;
445 iorecv
[0].iov_len
= G_N_ELEMENTS(bufrecv
);
447 g_assert(qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_FD_PASS
));
448 g_assert(qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_FD_PASS
));
450 qio_channel_writev_full(src
,
452 G_N_ELEMENTS(iosend
),
454 G_N_ELEMENTS(fdsend
),
458 qio_channel_readv_full(dst
,
460 G_N_ELEMENTS(iorecv
),
466 g_assert(nfdrecv
== G_N_ELEMENTS(fdsend
));
467 /* Each recvd FD should be different from sent FD */
468 for (i
= 0; i
< nfdrecv
; i
++) {
469 g_assert_cmpint(fdrecv
[i
], !=, testfd
);
471 /* Each recvd FD should be different from each other */
472 g_assert_cmpint(fdrecv
[0], !=, fdrecv
[1]);
473 g_assert_cmpint(fdrecv
[0], !=, fdrecv
[2]);
474 g_assert_cmpint(fdrecv
[1], !=, fdrecv
[2]);
476 /* Check the I/O buf we sent at the same time matches */
477 g_assert(memcmp(bufsend
, bufrecv
, G_N_ELEMENTS(bufsend
)) == 0);
479 /* Write some data into the FD we received */
480 g_assert(write(fdrecv
[0], bufsend
, G_N_ELEMENTS(bufsend
)) ==
481 G_N_ELEMENTS(bufsend
));
483 /* Read data from the original FD and make sure it matches */
484 memset(bufrecv
, 0, G_N_ELEMENTS(bufrecv
));
485 g_assert(lseek(testfd
, 0, SEEK_SET
) == 0);
486 g_assert(read(testfd
, bufrecv
, G_N_ELEMENTS(bufrecv
)) ==
487 G_N_ELEMENTS(bufrecv
));
488 g_assert(memcmp(bufsend
, bufrecv
, G_N_ELEMENTS(bufsend
)) == 0);
490 object_unref(OBJECT(src
));
491 object_unref(OBJECT(dst
));
492 object_unref(OBJECT(srv
));
493 qapi_free_SocketAddress(listen_addr
);
494 qapi_free_SocketAddress(connect_addr
);
498 for (i
= 0; i
< nfdrecv
; i
++) {
505 static void test_io_channel_unix_listen_cleanup(void)
507 QIOChannelSocket
*ioc
;
508 struct sockaddr_un un
;
511 #define TEST_SOCKET "test-io-channel-socket.sock"
513 ioc
= qio_channel_socket_new();
515 /* Manually bind ioc without calling the qio api to avoid setting
516 * the LISTEN feature */
517 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
518 memset(&un
, 0, sizeof(un
));
519 un
.sun_family
= AF_UNIX
;
520 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", TEST_SOCKET
);
522 bind(sock
, (struct sockaddr
*)&un
, sizeof(un
));
524 ioc
->localAddrLen
= sizeof(ioc
->localAddr
);
525 getsockname(sock
, (struct sockaddr
*)&ioc
->localAddr
,
528 g_assert(g_file_test(TEST_SOCKET
, G_FILE_TEST_EXISTS
));
529 object_unref(OBJECT(ioc
));
530 g_assert(g_file_test(TEST_SOCKET
, G_FILE_TEST_EXISTS
));
535 static void test_io_channel_ipv4_fd(void)
539 struct sockaddr_in sa
= {
540 .sin_family
= AF_INET
,
542 .s_addr
= htonl(INADDR_LOOPBACK
),
544 /* Leave port unset for auto-assign */
546 socklen_t salen
= sizeof(sa
);
548 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
549 g_assert_cmpint(fd
, >, -1);
551 g_assert_cmpint(bind(fd
, (struct sockaddr
*)&sa
, salen
), ==, 0);
553 ioc
= qio_channel_new_fd(fd
, &error_abort
);
555 g_assert_cmpstr(object_get_typename(OBJECT(ioc
)),
557 TYPE_QIO_CHANNEL_SOCKET
);
559 object_unref(OBJECT(ioc
));
563 int main(int argc
, char **argv
)
565 bool has_ipv4
, has_ipv6
, has_afunix
;
567 module_call_init(MODULE_INIT_QOM
);
568 qemu_init_main_loop(&error_abort
);
571 g_test_init(&argc
, &argv
, NULL
);
573 /* We're creating actual IPv4/6 sockets, so we should
574 * check if the host running tests actually supports
575 * each protocol to avoid breaking tests on machines
576 * with either IPv4 or IPv6 disabled.
578 if (socket_check_protocol_support(&has_ipv4
, &has_ipv6
) < 0) {
579 g_printerr("socket_check_protocol_support() failed\n");
584 g_test_add_func("/io/channel/socket/ipv4-sync",
585 test_io_channel_ipv4_sync
);
586 g_test_add_func("/io/channel/socket/ipv4-async",
587 test_io_channel_ipv4_async
);
588 g_test_add_func("/io/channel/socket/ipv4-fd",
589 test_io_channel_ipv4_fd
);
592 g_test_add_func("/io/channel/socket/ipv6-sync",
593 test_io_channel_ipv6_sync
);
594 g_test_add_func("/io/channel/socket/ipv6-async",
595 test_io_channel_ipv6_async
);
598 socket_check_afunix_support(&has_afunix
);
600 g_test_add_func("/io/channel/socket/unix-sync",
601 test_io_channel_unix_sync
);
602 g_test_add_func("/io/channel/socket/unix-async",
603 test_io_channel_unix_async
);
605 g_test_add_func("/io/channel/socket/unix-fd-pass",
606 test_io_channel_unix_fd_pass
);
608 g_test_add_func("/io/channel/socket/unix-listen-cleanup",
609 test_io_channel_unix_listen_cleanup
);