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 "qapi/error.h"
28 # define AI_ADDRCONFIG 0
30 #ifndef EAI_ADDRFAMILY
31 # define EAI_ADDRFAMILY 0
34 static int check_bind(const char *hostname
, bool *has_proto
)
37 struct addrinfo ai
, *res
= NULL
;
41 memset(&ai
, 0, sizeof(ai
));
42 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
43 ai
.ai_family
= AF_UNSPEC
;
44 ai
.ai_socktype
= SOCK_STREAM
;
47 rc
= getaddrinfo(hostname
, NULL
, &ai
, &res
);
49 if (rc
== EAI_ADDRFAMILY
||
57 fd
= qemu_socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
62 if (bind(fd
, res
->ai_addr
, res
->ai_addrlen
) < 0) {
63 if (errno
== EADDRNOTAVAIL
) {
84 static int check_protocol_support(bool *has_ipv4
, bool *has_ipv6
)
86 if (check_bind("127.0.0.1", has_ipv4
) < 0) {
89 if (check_bind("::1", has_ipv6
) < 0) {
97 static void test_io_channel_set_socket_bufs(QIOChannel
*src
,
100 int buflen
= 64 * 1024;
103 * Make the socket buffers small so that we see
104 * the effects of partial reads/writes
106 setsockopt(((QIOChannelSocket
*)src
)->fd
,
107 SOL_SOCKET
, SO_SNDBUF
,
111 setsockopt(((QIOChannelSocket
*)dst
)->fd
,
112 SOL_SOCKET
, SO_SNDBUF
,
118 static void test_io_channel_setup_sync(SocketAddress
*listen_addr
,
119 SocketAddress
*connect_addr
,
123 QIOChannelSocket
*lioc
;
125 lioc
= qio_channel_socket_new();
126 qio_channel_socket_listen_sync(lioc
, listen_addr
, &error_abort
);
128 if (listen_addr
->type
== SOCKET_ADDRESS_KIND_INET
) {
129 SocketAddress
*laddr
= qio_channel_socket_get_local_address(
132 g_free(connect_addr
->u
.inet
.data
->port
);
133 connect_addr
->u
.inet
.data
->port
= g_strdup(laddr
->u
.inet
.data
->port
);
135 qapi_free_SocketAddress(laddr
);
138 *src
= QIO_CHANNEL(qio_channel_socket_new());
139 qio_channel_socket_connect_sync(
140 QIO_CHANNEL_SOCKET(*src
), connect_addr
, &error_abort
);
141 qio_channel_set_delay(*src
, false);
143 qio_channel_wait(QIO_CHANNEL(lioc
), G_IO_IN
);
144 *dst
= QIO_CHANNEL(qio_channel_socket_accept(lioc
, &error_abort
));
147 test_io_channel_set_socket_bufs(*src
, *dst
);
149 object_unref(OBJECT(lioc
));
153 struct TestIOChannelData
{
159 static void test_io_channel_complete(QIOTask
*task
,
162 struct TestIOChannelData
*data
= opaque
;
163 data
->err
= qio_task_propagate_error(task
, NULL
);
164 g_main_loop_quit(data
->loop
);
168 static void test_io_channel_setup_async(SocketAddress
*listen_addr
,
169 SocketAddress
*connect_addr
,
173 QIOChannelSocket
*lioc
;
174 struct TestIOChannelData data
;
176 data
.loop
= g_main_loop_new(g_main_context_default(),
179 lioc
= qio_channel_socket_new();
180 qio_channel_socket_listen_async(
182 test_io_channel_complete
, &data
, NULL
);
184 g_main_loop_run(data
.loop
);
185 g_main_context_iteration(g_main_context_default(), FALSE
);
189 if (listen_addr
->type
== SOCKET_ADDRESS_KIND_INET
) {
190 SocketAddress
*laddr
= qio_channel_socket_get_local_address(
193 g_free(connect_addr
->u
.inet
.data
->port
);
194 connect_addr
->u
.inet
.data
->port
= g_strdup(laddr
->u
.inet
.data
->port
);
196 qapi_free_SocketAddress(laddr
);
199 *src
= QIO_CHANNEL(qio_channel_socket_new());
201 qio_channel_socket_connect_async(
202 QIO_CHANNEL_SOCKET(*src
), connect_addr
,
203 test_io_channel_complete
, &data
, NULL
);
205 g_main_loop_run(data
.loop
);
206 g_main_context_iteration(g_main_context_default(), FALSE
);
210 qio_channel_wait(QIO_CHANNEL(lioc
), G_IO_IN
);
211 *dst
= QIO_CHANNEL(qio_channel_socket_accept(lioc
, &error_abort
));
214 qio_channel_set_delay(*src
, false);
215 test_io_channel_set_socket_bufs(*src
, *dst
);
217 object_unref(OBJECT(lioc
));
219 g_main_loop_unref(data
.loop
);
223 static void test_io_channel(bool async
,
224 SocketAddress
*listen_addr
,
225 SocketAddress
*connect_addr
,
228 QIOChannel
*src
, *dst
;
229 QIOChannelTest
*test
;
231 test_io_channel_setup_async(listen_addr
, connect_addr
, &src
, &dst
);
234 qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_FD_PASS
));
236 qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_FD_PASS
));
237 g_assert(qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_SHUTDOWN
));
238 g_assert(qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_SHUTDOWN
));
240 test
= qio_channel_test_new();
241 qio_channel_test_run_threads(test
, true, src
, dst
);
242 qio_channel_test_validate(test
);
244 object_unref(OBJECT(src
));
245 object_unref(OBJECT(dst
));
247 test_io_channel_setup_async(listen_addr
, connect_addr
, &src
, &dst
);
250 qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_FD_PASS
));
252 qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_FD_PASS
));
253 g_assert(qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_SHUTDOWN
));
254 g_assert(qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_SHUTDOWN
));
256 test
= qio_channel_test_new();
257 qio_channel_test_run_threads(test
, false, src
, dst
);
258 qio_channel_test_validate(test
);
260 object_unref(OBJECT(src
));
261 object_unref(OBJECT(dst
));
263 test_io_channel_setup_sync(listen_addr
, connect_addr
, &src
, &dst
);
266 qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_FD_PASS
));
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
, true, src
, dst
);
274 qio_channel_test_validate(test
);
276 object_unref(OBJECT(src
));
277 object_unref(OBJECT(dst
));
279 test_io_channel_setup_sync(listen_addr
, connect_addr
, &src
, &dst
);
282 qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_FD_PASS
));
284 qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_FD_PASS
));
285 g_assert(qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_SHUTDOWN
));
286 g_assert(qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_SHUTDOWN
));
288 test
= qio_channel_test_new();
289 qio_channel_test_run_threads(test
, false, src
, dst
);
290 qio_channel_test_validate(test
);
292 object_unref(OBJECT(src
));
293 object_unref(OBJECT(dst
));
298 static void test_io_channel_ipv4(bool async
)
300 SocketAddress
*listen_addr
= g_new0(SocketAddress
, 1);
301 SocketAddress
*connect_addr
= g_new0(SocketAddress
, 1);
303 listen_addr
->type
= SOCKET_ADDRESS_KIND_INET
;
304 listen_addr
->u
.inet
.data
= g_new(InetSocketAddress
, 1);
305 *listen_addr
->u
.inet
.data
= (InetSocketAddress
) {
306 .host
= g_strdup("127.0.0.1"),
307 .port
= NULL
, /* Auto-select */
310 connect_addr
->type
= SOCKET_ADDRESS_KIND_INET
;
311 connect_addr
->u
.inet
.data
= g_new(InetSocketAddress
, 1);
312 *connect_addr
->u
.inet
.data
= (InetSocketAddress
) {
313 .host
= g_strdup("127.0.0.1"),
314 .port
= NULL
, /* Filled in later */
317 test_io_channel(async
, listen_addr
, connect_addr
, false);
319 qapi_free_SocketAddress(listen_addr
);
320 qapi_free_SocketAddress(connect_addr
);
324 static void test_io_channel_ipv4_sync(void)
326 return test_io_channel_ipv4(false);
330 static void test_io_channel_ipv4_async(void)
332 return test_io_channel_ipv4(true);
336 static void test_io_channel_ipv6(bool async
)
338 SocketAddress
*listen_addr
= g_new0(SocketAddress
, 1);
339 SocketAddress
*connect_addr
= g_new0(SocketAddress
, 1);
341 listen_addr
->type
= SOCKET_ADDRESS_KIND_INET
;
342 listen_addr
->u
.inet
.data
= g_new(InetSocketAddress
, 1);
343 *listen_addr
->u
.inet
.data
= (InetSocketAddress
) {
344 .host
= g_strdup("::1"),
345 .port
= NULL
, /* Auto-select */
348 connect_addr
->type
= SOCKET_ADDRESS_KIND_INET
;
349 connect_addr
->u
.inet
.data
= g_new(InetSocketAddress
, 1);
350 *connect_addr
->u
.inet
.data
= (InetSocketAddress
) {
351 .host
= g_strdup("::1"),
352 .port
= NULL
, /* Filled in later */
355 test_io_channel(async
, listen_addr
, connect_addr
, false);
357 qapi_free_SocketAddress(listen_addr
);
358 qapi_free_SocketAddress(connect_addr
);
362 static void test_io_channel_ipv6_sync(void)
364 return test_io_channel_ipv6(false);
368 static void test_io_channel_ipv6_async(void)
370 return test_io_channel_ipv6(true);
375 static void test_io_channel_unix(bool async
)
377 SocketAddress
*listen_addr
= g_new0(SocketAddress
, 1);
378 SocketAddress
*connect_addr
= g_new0(SocketAddress
, 1);
380 #define TEST_SOCKET "test-io-channel-socket.sock"
381 listen_addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
382 listen_addr
->u
.q_unix
.data
= g_new0(UnixSocketAddress
, 1);
383 listen_addr
->u
.q_unix
.data
->path
= g_strdup(TEST_SOCKET
);
385 connect_addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
386 connect_addr
->u
.q_unix
.data
= g_new0(UnixSocketAddress
, 1);
387 connect_addr
->u
.q_unix
.data
->path
= g_strdup(TEST_SOCKET
);
389 test_io_channel(async
, listen_addr
, connect_addr
, true);
391 qapi_free_SocketAddress(listen_addr
);
392 qapi_free_SocketAddress(connect_addr
);
393 g_assert(g_file_test(TEST_SOCKET
, G_FILE_TEST_EXISTS
) == FALSE
);
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);
408 static void test_io_channel_unix_fd_pass(void)
410 SocketAddress
*listen_addr
= g_new0(SocketAddress
, 1);
411 SocketAddress
*connect_addr
= g_new0(SocketAddress
, 1);
412 QIOChannel
*src
, *dst
;
418 char bufsend
[12], bufrecv
[12];
419 struct iovec iosend
[1], iorecv
[1];
421 #define TEST_SOCKET "test-io-channel-socket.sock"
422 #define TEST_FILE "test-io-channel-socket.txt"
424 testfd
= open(TEST_FILE
, O_RDWR
|O_TRUNC
|O_CREAT
, 0700);
425 g_assert(testfd
!= -1);
430 listen_addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
431 listen_addr
->u
.q_unix
.data
= g_new0(UnixSocketAddress
, 1);
432 listen_addr
->u
.q_unix
.data
->path
= g_strdup(TEST_SOCKET
);
434 connect_addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
435 connect_addr
->u
.q_unix
.data
= g_new0(UnixSocketAddress
, 1);
436 connect_addr
->u
.q_unix
.data
->path
= g_strdup(TEST_SOCKET
);
438 test_io_channel_setup_sync(listen_addr
, connect_addr
, &src
, &dst
);
440 memcpy(bufsend
, "Hello World", G_N_ELEMENTS(bufsend
));
442 iosend
[0].iov_base
= bufsend
;
443 iosend
[0].iov_len
= G_N_ELEMENTS(bufsend
);
445 iorecv
[0].iov_base
= bufrecv
;
446 iorecv
[0].iov_len
= G_N_ELEMENTS(bufrecv
);
448 g_assert(qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_FD_PASS
));
449 g_assert(qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_FD_PASS
));
451 qio_channel_writev_full(src
,
453 G_N_ELEMENTS(iosend
),
455 G_N_ELEMENTS(fdsend
),
458 qio_channel_readv_full(dst
,
460 G_N_ELEMENTS(iorecv
),
465 g_assert(nfdrecv
== G_N_ELEMENTS(fdsend
));
466 /* Each recvd FD should be different from sent FD */
467 for (i
= 0; i
< nfdrecv
; i
++) {
468 g_assert_cmpint(fdrecv
[i
], !=, testfd
);
470 /* Each recvd FD should be different from each other */
471 g_assert_cmpint(fdrecv
[0], !=, fdrecv
[1]);
472 g_assert_cmpint(fdrecv
[0], !=, fdrecv
[2]);
473 g_assert_cmpint(fdrecv
[1], !=, fdrecv
[2]);
475 /* Check the I/O buf we sent at the same time matches */
476 g_assert(memcmp(bufsend
, bufrecv
, G_N_ELEMENTS(bufsend
)) == 0);
478 /* Write some data into the FD we received */
479 g_assert(write(fdrecv
[0], bufsend
, G_N_ELEMENTS(bufsend
)) ==
480 G_N_ELEMENTS(bufsend
));
482 /* Read data from the original FD and make sure it matches */
483 memset(bufrecv
, 0, G_N_ELEMENTS(bufrecv
));
484 g_assert(lseek(testfd
, 0, SEEK_SET
) == 0);
485 g_assert(read(testfd
, bufrecv
, G_N_ELEMENTS(bufrecv
)) ==
486 G_N_ELEMENTS(bufrecv
));
487 g_assert(memcmp(bufsend
, bufrecv
, G_N_ELEMENTS(bufsend
)) == 0);
489 object_unref(OBJECT(src
));
490 object_unref(OBJECT(dst
));
491 qapi_free_SocketAddress(listen_addr
);
492 qapi_free_SocketAddress(connect_addr
);
496 for (i
= 0; i
< nfdrecv
; i
++) {
502 static void test_io_channel_unix_listen_cleanup(void)
504 QIOChannelSocket
*ioc
;
505 struct sockaddr_un un
;
508 #define TEST_SOCKET "test-io-channel-socket.sock"
510 ioc
= qio_channel_socket_new();
512 /* Manually bind ioc without calling the qio api to avoid setting
513 * the LISTEN feature */
514 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
515 memset(&un
, 0, sizeof(un
));
516 un
.sun_family
= AF_UNIX
;
517 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", TEST_SOCKET
);
519 bind(sock
, (struct sockaddr
*)&un
, sizeof(un
));
521 ioc
->localAddrLen
= sizeof(ioc
->localAddr
);
522 getsockname(sock
, (struct sockaddr
*)&ioc
->localAddr
,
525 g_assert(g_file_test(TEST_SOCKET
, G_FILE_TEST_EXISTS
));
526 object_unref(OBJECT(ioc
));
527 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
;
567 module_call_init(MODULE_INIT_QOM
);
570 g_test_init(&argc
, &argv
, NULL
);
572 /* We're creating actual IPv4/6 sockets, so we should
573 * check if the host running tests actually supports
574 * each protocol to avoid breaking tests on machines
575 * with either IPv4 or IPv6 disabled.
577 if (check_protocol_support(&has_ipv4
, &has_ipv6
) < 0) {
582 g_test_add_func("/io/channel/socket/ipv4-sync",
583 test_io_channel_ipv4_sync
);
584 g_test_add_func("/io/channel/socket/ipv4-async",
585 test_io_channel_ipv4_async
);
586 g_test_add_func("/io/channel/socket/ipv4-fd",
587 test_io_channel_ipv4_fd
);
590 g_test_add_func("/io/channel/socket/ipv6-sync",
591 test_io_channel_ipv6_sync
);
592 g_test_add_func("/io/channel/socket/ipv6-async",
593 test_io_channel_ipv6_async
);
597 g_test_add_func("/io/channel/socket/unix-sync",
598 test_io_channel_unix_sync
);
599 g_test_add_func("/io/channel/socket/unix-async",
600 test_io_channel_unix_async
);
601 g_test_add_func("/io/channel/socket/unix-fd-pass",
602 test_io_channel_unix_fd_pass
);
603 g_test_add_func("/io/channel/socket/unix-listen-cleanup",
604 test_io_channel_unix_listen_cleanup
);