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"
27 # define AI_ADDRCONFIG 0
30 # define AI_V4MAPPED 0
32 #ifndef EAI_ADDRFAMILY
33 # define EAI_ADDRFAMILY 0
36 static int check_bind(const char *hostname
, bool *has_proto
)
39 struct addrinfo ai
, *res
= NULL
;
43 memset(&ai
, 0, sizeof(ai
));
44 ai
.ai_flags
= AI_CANONNAME
| AI_V4MAPPED
| AI_ADDRCONFIG
;
45 ai
.ai_family
= AF_UNSPEC
;
46 ai
.ai_socktype
= SOCK_STREAM
;
49 rc
= getaddrinfo(hostname
, NULL
, &ai
, &res
);
51 if (rc
== EAI_ADDRFAMILY
||
59 fd
= qemu_socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
64 if (bind(fd
, res
->ai_addr
, res
->ai_addrlen
) < 0) {
65 if (errno
== EADDRNOTAVAIL
) {
86 static int check_protocol_support(bool *has_ipv4
, bool *has_ipv6
)
88 if (check_bind("127.0.0.1", has_ipv4
) < 0) {
91 if (check_bind("::1", has_ipv6
) < 0) {
99 static void test_io_channel_set_socket_bufs(QIOChannel
*src
,
102 int buflen
= 64 * 1024;
105 * Make the socket buffers small so that we see
106 * the effects of partial reads/writes
108 setsockopt(((QIOChannelSocket
*)src
)->fd
,
109 SOL_SOCKET
, SO_SNDBUF
,
113 setsockopt(((QIOChannelSocket
*)dst
)->fd
,
114 SOL_SOCKET
, SO_SNDBUF
,
120 static void test_io_channel_setup_sync(SocketAddress
*listen_addr
,
121 SocketAddress
*connect_addr
,
125 QIOChannelSocket
*lioc
;
127 lioc
= qio_channel_socket_new();
128 qio_channel_socket_listen_sync(lioc
, listen_addr
, &error_abort
);
130 if (listen_addr
->type
== SOCKET_ADDRESS_KIND_INET
) {
131 SocketAddress
*laddr
= qio_channel_socket_get_local_address(
134 g_free(connect_addr
->u
.inet
->port
);
135 connect_addr
->u
.inet
->port
= g_strdup(laddr
->u
.inet
->port
);
137 qapi_free_SocketAddress(laddr
);
140 *src
= QIO_CHANNEL(qio_channel_socket_new());
141 qio_channel_socket_connect_sync(
142 QIO_CHANNEL_SOCKET(*src
), connect_addr
, &error_abort
);
143 qio_channel_set_delay(*src
, false);
145 qio_channel_wait(QIO_CHANNEL(lioc
), G_IO_IN
);
146 *dst
= QIO_CHANNEL(qio_channel_socket_accept(lioc
, &error_abort
));
149 test_io_channel_set_socket_bufs(*src
, *dst
);
151 object_unref(OBJECT(lioc
));
155 struct TestIOChannelData
{
161 static void test_io_channel_complete(Object
*src
,
165 struct TestIOChannelData
*data
= opaque
;
166 data
->err
= err
!= NULL
;
167 g_main_loop_quit(data
->loop
);
171 static void test_io_channel_setup_async(SocketAddress
*listen_addr
,
172 SocketAddress
*connect_addr
,
176 QIOChannelSocket
*lioc
;
177 struct TestIOChannelData data
;
179 data
.loop
= g_main_loop_new(g_main_context_default(),
182 lioc
= qio_channel_socket_new();
183 qio_channel_socket_listen_async(
185 test_io_channel_complete
, &data
, NULL
);
187 g_main_loop_run(data
.loop
);
188 g_main_context_iteration(g_main_context_default(), FALSE
);
192 if (listen_addr
->type
== SOCKET_ADDRESS_KIND_INET
) {
193 SocketAddress
*laddr
= qio_channel_socket_get_local_address(
196 g_free(connect_addr
->u
.inet
->port
);
197 connect_addr
->u
.inet
->port
= g_strdup(laddr
->u
.inet
->port
);
199 qapi_free_SocketAddress(laddr
);
202 *src
= QIO_CHANNEL(qio_channel_socket_new());
204 qio_channel_socket_connect_async(
205 QIO_CHANNEL_SOCKET(*src
), connect_addr
,
206 test_io_channel_complete
, &data
, NULL
);
208 g_main_loop_run(data
.loop
);
209 g_main_context_iteration(g_main_context_default(), FALSE
);
213 qio_channel_wait(QIO_CHANNEL(lioc
), G_IO_IN
);
214 *dst
= QIO_CHANNEL(qio_channel_socket_accept(lioc
, &error_abort
));
217 qio_channel_set_delay(*src
, false);
218 test_io_channel_set_socket_bufs(*src
, *dst
);
220 object_unref(OBJECT(lioc
));
222 g_main_loop_unref(data
.loop
);
226 static void test_io_channel(bool async
,
227 SocketAddress
*listen_addr
,
228 SocketAddress
*connect_addr
,
231 QIOChannel
*src
, *dst
;
232 QIOChannelTest
*test
;
234 test_io_channel_setup_async(listen_addr
, connect_addr
, &src
, &dst
);
237 qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_FD_PASS
));
239 qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_FD_PASS
));
241 test
= qio_channel_test_new();
242 qio_channel_test_run_threads(test
, true, src
, dst
);
243 qio_channel_test_validate(test
);
245 object_unref(OBJECT(src
));
246 object_unref(OBJECT(dst
));
248 test_io_channel_setup_async(listen_addr
, connect_addr
, &src
, &dst
);
251 qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_FD_PASS
));
253 qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_FD_PASS
));
255 test
= qio_channel_test_new();
256 qio_channel_test_run_threads(test
, false, src
, dst
);
257 qio_channel_test_validate(test
);
259 object_unref(OBJECT(src
));
260 object_unref(OBJECT(dst
));
262 test_io_channel_setup_sync(listen_addr
, connect_addr
, &src
, &dst
);
265 qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_FD_PASS
));
267 qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_FD_PASS
));
269 test
= qio_channel_test_new();
270 qio_channel_test_run_threads(test
, true, src
, dst
);
271 qio_channel_test_validate(test
);
273 object_unref(OBJECT(src
));
274 object_unref(OBJECT(dst
));
276 test_io_channel_setup_sync(listen_addr
, connect_addr
, &src
, &dst
);
279 qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_FD_PASS
));
281 qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_FD_PASS
));
283 test
= qio_channel_test_new();
284 qio_channel_test_run_threads(test
, false, src
, dst
);
285 qio_channel_test_validate(test
);
287 object_unref(OBJECT(src
));
288 object_unref(OBJECT(dst
));
293 static void test_io_channel_ipv4(bool async
)
295 SocketAddress
*listen_addr
= g_new0(SocketAddress
, 1);
296 SocketAddress
*connect_addr
= g_new0(SocketAddress
, 1);
298 listen_addr
->type
= SOCKET_ADDRESS_KIND_INET
;
299 listen_addr
->u
.inet
= g_new(InetSocketAddress
, 1);
300 *listen_addr
->u
.inet
= (InetSocketAddress
) {
301 .host
= g_strdup("127.0.0.1"),
302 .port
= NULL
, /* Auto-select */
305 connect_addr
->type
= SOCKET_ADDRESS_KIND_INET
;
306 connect_addr
->u
.inet
= g_new(InetSocketAddress
, 1);
307 *connect_addr
->u
.inet
= (InetSocketAddress
) {
308 .host
= g_strdup("127.0.0.1"),
309 .port
= NULL
, /* Filled in later */
312 test_io_channel(async
, listen_addr
, connect_addr
, false);
314 qapi_free_SocketAddress(listen_addr
);
315 qapi_free_SocketAddress(connect_addr
);
319 static void test_io_channel_ipv4_sync(void)
321 return test_io_channel_ipv4(false);
325 static void test_io_channel_ipv4_async(void)
327 return test_io_channel_ipv4(true);
331 static void test_io_channel_ipv6(bool async
)
333 SocketAddress
*listen_addr
= g_new0(SocketAddress
, 1);
334 SocketAddress
*connect_addr
= g_new0(SocketAddress
, 1);
336 listen_addr
->type
= SOCKET_ADDRESS_KIND_INET
;
337 listen_addr
->u
.inet
= g_new(InetSocketAddress
, 1);
338 *listen_addr
->u
.inet
= (InetSocketAddress
) {
339 .host
= g_strdup("::1"),
340 .port
= NULL
, /* Auto-select */
343 connect_addr
->type
= SOCKET_ADDRESS_KIND_INET
;
344 connect_addr
->u
.inet
= g_new(InetSocketAddress
, 1);
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);
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_KIND_UNIX
;
377 listen_addr
->u
.q_unix
= g_new0(UnixSocketAddress
, 1);
378 listen_addr
->u
.q_unix
->path
= g_strdup(TEST_SOCKET
);
380 connect_addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
381 connect_addr
->u
.q_unix
= g_new0(UnixSocketAddress
, 1);
382 connect_addr
->u
.q_unix
->path
= g_strdup(TEST_SOCKET
);
384 test_io_channel(async
, listen_addr
, connect_addr
, true);
386 qapi_free_SocketAddress(listen_addr
);
387 qapi_free_SocketAddress(connect_addr
);
392 static void test_io_channel_unix_sync(void)
394 return test_io_channel_unix(false);
398 static void test_io_channel_unix_async(void)
400 return test_io_channel_unix(true);
403 static void test_io_channel_unix_fd_pass(void)
405 SocketAddress
*listen_addr
= g_new0(SocketAddress
, 1);
406 SocketAddress
*connect_addr
= g_new0(SocketAddress
, 1);
407 QIOChannel
*src
, *dst
;
413 char bufsend
[12], bufrecv
[12];
414 struct iovec iosend
[1], iorecv
[1];
416 #define TEST_SOCKET "test-io-channel-socket.sock"
417 #define TEST_FILE "test-io-channel-socket.txt"
419 testfd
= open(TEST_FILE
, O_RDWR
|O_TRUNC
|O_CREAT
, 0700);
420 g_assert(testfd
!= -1);
425 listen_addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
426 listen_addr
->u
.q_unix
= g_new0(UnixSocketAddress
, 1);
427 listen_addr
->u
.q_unix
->path
= g_strdup(TEST_SOCKET
);
429 connect_addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
430 connect_addr
->u
.q_unix
= g_new0(UnixSocketAddress
, 1);
431 connect_addr
->u
.q_unix
->path
= g_strdup(TEST_SOCKET
);
433 test_io_channel_setup_sync(listen_addr
, connect_addr
, &src
, &dst
);
435 memcpy(bufsend
, "Hello World", G_N_ELEMENTS(bufsend
));
437 iosend
[0].iov_base
= bufsend
;
438 iosend
[0].iov_len
= G_N_ELEMENTS(bufsend
);
440 iorecv
[0].iov_base
= bufrecv
;
441 iorecv
[0].iov_len
= G_N_ELEMENTS(bufrecv
);
443 g_assert(qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_FD_PASS
));
444 g_assert(qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_FD_PASS
));
446 qio_channel_writev_full(src
,
448 G_N_ELEMENTS(iosend
),
450 G_N_ELEMENTS(fdsend
),
453 qio_channel_readv_full(dst
,
455 G_N_ELEMENTS(iorecv
),
460 g_assert(nfdrecv
== G_N_ELEMENTS(fdsend
));
461 /* Each recvd FD should be different from sent FD */
462 for (i
= 0; i
< nfdrecv
; i
++) {
463 g_assert_cmpint(fdrecv
[i
], !=, testfd
);
465 /* Each recvd FD should be different from each other */
466 g_assert_cmpint(fdrecv
[0], !=, fdrecv
[1]);
467 g_assert_cmpint(fdrecv
[0], !=, fdrecv
[2]);
468 g_assert_cmpint(fdrecv
[1], !=, fdrecv
[2]);
470 /* Check the I/O buf we sent at the same time matches */
471 g_assert(memcmp(bufsend
, bufrecv
, G_N_ELEMENTS(bufsend
)) == 0);
473 /* Write some data into the FD we received */
474 g_assert(write(fdrecv
[0], bufsend
, G_N_ELEMENTS(bufsend
)) ==
475 G_N_ELEMENTS(bufsend
));
477 /* Read data from the original FD and make sure it matches */
478 memset(bufrecv
, 0, G_N_ELEMENTS(bufrecv
));
479 g_assert(lseek(testfd
, 0, SEEK_SET
) == 0);
480 g_assert(read(testfd
, bufrecv
, G_N_ELEMENTS(bufrecv
)) ==
481 G_N_ELEMENTS(bufrecv
));
482 g_assert(memcmp(bufsend
, bufrecv
, G_N_ELEMENTS(bufsend
)) == 0);
484 object_unref(OBJECT(src
));
485 object_unref(OBJECT(dst
));
486 qapi_free_SocketAddress(listen_addr
);
487 qapi_free_SocketAddress(connect_addr
);
491 for (i
= 0; i
< nfdrecv
; i
++) {
499 static void test_io_channel_ipv4_fd(void)
503 struct sockaddr_in sa
= {
504 .sin_family
= AF_INET
,
506 .s_addr
= htonl(INADDR_LOOPBACK
),
508 /* Leave port unset for auto-assign */
510 socklen_t salen
= sizeof(sa
);
512 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
513 g_assert_cmpint(fd
, >, -1);
515 g_assert_cmpint(bind(fd
, (struct sockaddr
*)&sa
, salen
), ==, 0);
517 ioc
= qio_channel_new_fd(fd
, &error_abort
);
519 g_assert_cmpstr(object_get_typename(OBJECT(ioc
)),
521 TYPE_QIO_CHANNEL_SOCKET
);
523 object_unref(OBJECT(ioc
));
527 int main(int argc
, char **argv
)
529 bool has_ipv4
, has_ipv6
;
531 module_call_init(MODULE_INIT_QOM
);
534 g_test_init(&argc
, &argv
, NULL
);
536 /* We're creating actual IPv4/6 sockets, so we should
537 * check if the host running tests actually supports
538 * each protocol to avoid breaking tests on machines
539 * with either IPv4 or IPv6 disabled.
541 if (check_protocol_support(&has_ipv4
, &has_ipv6
) < 0) {
546 g_test_add_func("/io/channel/socket/ipv4-sync",
547 test_io_channel_ipv4_sync
);
548 g_test_add_func("/io/channel/socket/ipv4-async",
549 test_io_channel_ipv4_async
);
550 g_test_add_func("/io/channel/socket/ipv4-fd",
551 test_io_channel_ipv4_fd
);
554 g_test_add_func("/io/channel/socket/ipv6-sync",
555 test_io_channel_ipv6_sync
);
556 g_test_add_func("/io/channel/socket/ipv6-async",
557 test_io_channel_ipv6_async
);
561 g_test_add_func("/io/channel/socket/unix-sync",
562 test_io_channel_unix_sync
);
563 g_test_add_func("/io/channel/socket/unix-async",
564 test_io_channel_unix_async
);
565 g_test_add_func("/io/channel/socket/unix-fd-pass",
566 test_io_channel_unix_fd_pass
);