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"
29 static int check_protocol_support(bool *has_ipv4
, bool *has_ipv6
)
32 struct ifaddrs
*ifaddr
= NULL
, *ifa
;
33 struct addrinfo hints
= { 0 };
34 struct addrinfo
*ai
= NULL
;
37 *has_ipv4
= *has_ipv6
= false;
39 if (getifaddrs(&ifaddr
) < 0) {
40 g_printerr("Failed to lookup interface addresses: %s\n",
45 for (ifa
= ifaddr
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
50 if (ifa
->ifa_addr
->sa_family
== AF_INET
) {
53 if (ifa
->ifa_addr
->sa_family
== AF_INET6
) {
60 hints
.ai_flags
= AI_PASSIVE
| AI_ADDRCONFIG
;
61 hints
.ai_family
= AF_INET6
;
62 hints
.ai_socktype
= SOCK_STREAM
;
64 gaierr
= getaddrinfo("::1", NULL
, &hints
, &ai
);
66 if (gaierr
== EAI_ADDRFAMILY
||
67 gaierr
== EAI_FAMILY
||
68 gaierr
== EAI_NONAME
) {
71 g_printerr("Failed to resolve ::1 address: %s\n",
72 gai_strerror(gaierr
));
81 *has_ipv4
= *has_ipv6
= false;
88 static void test_io_channel_set_socket_bufs(QIOChannel
*src
,
91 int buflen
= 64 * 1024;
94 * Make the socket buffers small so that we see
95 * the effects of partial reads/writes
97 setsockopt(((QIOChannelSocket
*)src
)->fd
,
98 SOL_SOCKET
, SO_SNDBUF
,
102 setsockopt(((QIOChannelSocket
*)dst
)->fd
,
103 SOL_SOCKET
, SO_SNDBUF
,
109 static void test_io_channel_setup_sync(SocketAddress
*listen_addr
,
110 SocketAddress
*connect_addr
,
114 QIOChannelSocket
*lioc
;
116 lioc
= qio_channel_socket_new();
117 qio_channel_socket_listen_sync(lioc
, listen_addr
, &error_abort
);
119 if (listen_addr
->type
== SOCKET_ADDRESS_KIND_INET
) {
120 SocketAddress
*laddr
= qio_channel_socket_get_local_address(
123 g_free(connect_addr
->u
.inet
->port
);
124 connect_addr
->u
.inet
->port
= g_strdup(laddr
->u
.inet
->port
);
126 qapi_free_SocketAddress(laddr
);
129 *src
= QIO_CHANNEL(qio_channel_socket_new());
130 qio_channel_socket_connect_sync(
131 QIO_CHANNEL_SOCKET(*src
), connect_addr
, &error_abort
);
132 qio_channel_set_delay(*src
, false);
134 *dst
= QIO_CHANNEL(qio_channel_socket_accept(lioc
, &error_abort
));
137 test_io_channel_set_socket_bufs(*src
, *dst
);
139 object_unref(OBJECT(lioc
));
143 struct TestIOChannelData
{
149 static void test_io_channel_complete(Object
*src
,
153 struct TestIOChannelData
*data
= opaque
;
154 data
->err
= err
!= NULL
;
155 g_main_loop_quit(data
->loop
);
159 static void test_io_channel_setup_async(SocketAddress
*listen_addr
,
160 SocketAddress
*connect_addr
,
164 QIOChannelSocket
*lioc
;
165 struct TestIOChannelData data
;
167 data
.loop
= g_main_loop_new(g_main_context_default(),
170 lioc
= qio_channel_socket_new();
171 qio_channel_socket_listen_async(
173 test_io_channel_complete
, &data
, NULL
);
175 g_main_loop_run(data
.loop
);
176 g_main_context_iteration(g_main_context_default(), FALSE
);
180 if (listen_addr
->type
== SOCKET_ADDRESS_KIND_INET
) {
181 SocketAddress
*laddr
= qio_channel_socket_get_local_address(
184 g_free(connect_addr
->u
.inet
->port
);
185 connect_addr
->u
.inet
->port
= g_strdup(laddr
->u
.inet
->port
);
187 qapi_free_SocketAddress(laddr
);
190 *src
= QIO_CHANNEL(qio_channel_socket_new());
192 qio_channel_socket_connect_async(
193 QIO_CHANNEL_SOCKET(*src
), connect_addr
,
194 test_io_channel_complete
, &data
, NULL
);
196 g_main_loop_run(data
.loop
);
197 g_main_context_iteration(g_main_context_default(), FALSE
);
201 *dst
= QIO_CHANNEL(qio_channel_socket_accept(lioc
, &error_abort
));
204 qio_channel_set_delay(*src
, false);
205 test_io_channel_set_socket_bufs(*src
, *dst
);
207 object_unref(OBJECT(lioc
));
209 g_main_loop_unref(data
.loop
);
213 static void test_io_channel(bool async
,
214 SocketAddress
*listen_addr
,
215 SocketAddress
*connect_addr
,
218 QIOChannel
*src
, *dst
;
219 QIOChannelTest
*test
;
221 test_io_channel_setup_async(listen_addr
, connect_addr
, &src
, &dst
);
224 qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_FD_PASS
));
226 qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_FD_PASS
));
228 test
= qio_channel_test_new();
229 qio_channel_test_run_threads(test
, true, src
, dst
);
230 qio_channel_test_validate(test
);
232 object_unref(OBJECT(src
));
233 object_unref(OBJECT(dst
));
235 test_io_channel_setup_async(listen_addr
, connect_addr
, &src
, &dst
);
238 qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_FD_PASS
));
240 qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_FD_PASS
));
242 test
= qio_channel_test_new();
243 qio_channel_test_run_threads(test
, false, src
, dst
);
244 qio_channel_test_validate(test
);
246 object_unref(OBJECT(src
));
247 object_unref(OBJECT(dst
));
249 test_io_channel_setup_sync(listen_addr
, connect_addr
, &src
, &dst
);
252 qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_FD_PASS
));
254 qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_FD_PASS
));
256 test
= qio_channel_test_new();
257 qio_channel_test_run_threads(test
, true, 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
));
270 test
= qio_channel_test_new();
271 qio_channel_test_run_threads(test
, false, src
, dst
);
272 qio_channel_test_validate(test
);
274 object_unref(OBJECT(src
));
275 object_unref(OBJECT(dst
));
280 static void test_io_channel_ipv4(bool async
)
282 SocketAddress
*listen_addr
= g_new0(SocketAddress
, 1);
283 SocketAddress
*connect_addr
= g_new0(SocketAddress
, 1);
285 listen_addr
->type
= SOCKET_ADDRESS_KIND_INET
;
286 listen_addr
->u
.inet
= g_new(InetSocketAddress
, 1);
287 *listen_addr
->u
.inet
= (InetSocketAddress
) {
288 .host
= g_strdup("127.0.0.1"),
289 .port
= NULL
, /* Auto-select */
292 connect_addr
->type
= SOCKET_ADDRESS_KIND_INET
;
293 connect_addr
->u
.inet
= g_new(InetSocketAddress
, 1);
294 *connect_addr
->u
.inet
= (InetSocketAddress
) {
295 .host
= g_strdup("127.0.0.1"),
296 .port
= NULL
, /* Filled in later */
299 test_io_channel(async
, listen_addr
, connect_addr
, false);
301 qapi_free_SocketAddress(listen_addr
);
302 qapi_free_SocketAddress(connect_addr
);
306 static void test_io_channel_ipv4_sync(void)
308 return test_io_channel_ipv4(false);
312 static void test_io_channel_ipv4_async(void)
314 return test_io_channel_ipv4(true);
318 static void test_io_channel_ipv6(bool async
)
320 SocketAddress
*listen_addr
= g_new0(SocketAddress
, 1);
321 SocketAddress
*connect_addr
= g_new0(SocketAddress
, 1);
323 listen_addr
->type
= SOCKET_ADDRESS_KIND_INET
;
324 listen_addr
->u
.inet
= g_new(InetSocketAddress
, 1);
325 *listen_addr
->u
.inet
= (InetSocketAddress
) {
326 .host
= g_strdup("::1"),
327 .port
= NULL
, /* Auto-select */
330 connect_addr
->type
= SOCKET_ADDRESS_KIND_INET
;
331 connect_addr
->u
.inet
= g_new(InetSocketAddress
, 1);
332 *connect_addr
->u
.inet
= (InetSocketAddress
) {
333 .host
= g_strdup("::1"),
334 .port
= NULL
, /* Filled in later */
337 test_io_channel(async
, listen_addr
, connect_addr
, false);
339 qapi_free_SocketAddress(listen_addr
);
340 qapi_free_SocketAddress(connect_addr
);
344 static void test_io_channel_ipv6_sync(void)
346 return test_io_channel_ipv6(false);
350 static void test_io_channel_ipv6_async(void)
352 return test_io_channel_ipv6(true);
357 static void test_io_channel_unix(bool async
)
359 SocketAddress
*listen_addr
= g_new0(SocketAddress
, 1);
360 SocketAddress
*connect_addr
= g_new0(SocketAddress
, 1);
362 #define TEST_SOCKET "test-io-channel-socket.sock"
363 listen_addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
364 listen_addr
->u
.q_unix
= g_new0(UnixSocketAddress
, 1);
365 listen_addr
->u
.q_unix
->path
= g_strdup(TEST_SOCKET
);
367 connect_addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
368 connect_addr
->u
.q_unix
= g_new0(UnixSocketAddress
, 1);
369 connect_addr
->u
.q_unix
->path
= g_strdup(TEST_SOCKET
);
371 test_io_channel(async
, listen_addr
, connect_addr
, true);
373 qapi_free_SocketAddress(listen_addr
);
374 qapi_free_SocketAddress(connect_addr
);
379 static void test_io_channel_unix_sync(void)
381 return test_io_channel_unix(false);
385 static void test_io_channel_unix_async(void)
387 return test_io_channel_unix(true);
390 static void test_io_channel_unix_fd_pass(void)
392 SocketAddress
*listen_addr
= g_new0(SocketAddress
, 1);
393 SocketAddress
*connect_addr
= g_new0(SocketAddress
, 1);
394 QIOChannel
*src
, *dst
;
400 char bufsend
[12], bufrecv
[12];
401 struct iovec iosend
[1], iorecv
[1];
403 #define TEST_SOCKET "test-io-channel-socket.sock"
404 #define TEST_FILE "test-io-channel-socket.txt"
406 testfd
= open(TEST_FILE
, O_RDWR
|O_TRUNC
|O_CREAT
, 0700);
407 g_assert(testfd
!= -1);
412 listen_addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
413 listen_addr
->u
.q_unix
= g_new0(UnixSocketAddress
, 1);
414 listen_addr
->u
.q_unix
->path
= g_strdup(TEST_SOCKET
);
416 connect_addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
417 connect_addr
->u
.q_unix
= g_new0(UnixSocketAddress
, 1);
418 connect_addr
->u
.q_unix
->path
= g_strdup(TEST_SOCKET
);
420 test_io_channel_setup_sync(listen_addr
, connect_addr
, &src
, &dst
);
422 memcpy(bufsend
, "Hello World", G_N_ELEMENTS(bufsend
));
424 iosend
[0].iov_base
= bufsend
;
425 iosend
[0].iov_len
= G_N_ELEMENTS(bufsend
);
427 iorecv
[0].iov_base
= bufrecv
;
428 iorecv
[0].iov_len
= G_N_ELEMENTS(bufrecv
);
430 g_assert(qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_FD_PASS
));
431 g_assert(qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_FD_PASS
));
433 qio_channel_writev_full(src
,
435 G_N_ELEMENTS(iosend
),
437 G_N_ELEMENTS(fdsend
),
440 qio_channel_readv_full(dst
,
442 G_N_ELEMENTS(iorecv
),
447 g_assert(nfdrecv
== G_N_ELEMENTS(fdsend
));
448 /* Each recvd FD should be different from sent FD */
449 for (i
= 0; i
< nfdrecv
; i
++) {
450 g_assert_cmpint(fdrecv
[i
], !=, testfd
);
452 /* Each recvd FD should be different from each other */
453 g_assert_cmpint(fdrecv
[0], !=, fdrecv
[1]);
454 g_assert_cmpint(fdrecv
[0], !=, fdrecv
[2]);
455 g_assert_cmpint(fdrecv
[1], !=, fdrecv
[2]);
457 /* Check the I/O buf we sent at the same time matches */
458 g_assert(memcmp(bufsend
, bufrecv
, G_N_ELEMENTS(bufsend
)) == 0);
460 /* Write some data into the FD we received */
461 g_assert(write(fdrecv
[0], bufsend
, G_N_ELEMENTS(bufsend
)) ==
462 G_N_ELEMENTS(bufsend
));
464 /* Read data from the original FD and make sure it matches */
465 memset(bufrecv
, 0, G_N_ELEMENTS(bufrecv
));
466 g_assert(lseek(testfd
, 0, SEEK_SET
) == 0);
467 g_assert(read(testfd
, bufrecv
, G_N_ELEMENTS(bufrecv
)) ==
468 G_N_ELEMENTS(bufrecv
));
469 g_assert(memcmp(bufsend
, bufrecv
, G_N_ELEMENTS(bufsend
)) == 0);
471 object_unref(OBJECT(src
));
472 object_unref(OBJECT(dst
));
473 qapi_free_SocketAddress(listen_addr
);
474 qapi_free_SocketAddress(connect_addr
);
478 for (i
= 0; i
< nfdrecv
; i
++) {
486 static void test_io_channel_ipv4_fd(void)
491 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
492 g_assert_cmpint(fd
, >, -1);
494 ioc
= qio_channel_new_fd(fd
, &error_abort
);
496 g_assert_cmpstr(object_get_typename(OBJECT(ioc
)),
498 TYPE_QIO_CHANNEL_SOCKET
);
500 object_unref(OBJECT(ioc
));
504 int main(int argc
, char **argv
)
506 bool has_ipv4
, has_ipv6
;
508 module_call_init(MODULE_INIT_QOM
);
510 g_test_init(&argc
, &argv
, NULL
);
512 /* We're creating actual IPv4/6 sockets, so we should
513 * check if the host running tests actually supports
514 * each protocol to avoid breaking tests on machines
515 * with either IPv4 or IPv6 disabled.
517 if (check_protocol_support(&has_ipv4
, &has_ipv6
) < 0) {
522 g_test_add_func("/io/channel/socket/ipv4-sync",
523 test_io_channel_ipv4_sync
);
524 g_test_add_func("/io/channel/socket/ipv4-async",
525 test_io_channel_ipv4_async
);
526 g_test_add_func("/io/channel/socket/ipv4-fd",
527 test_io_channel_ipv4_fd
);
530 g_test_add_func("/io/channel/socket/ipv6-sync",
531 test_io_channel_ipv6_sync
);
532 g_test_add_func("/io/channel/socket/ipv6-async",
533 test_io_channel_ipv6_async
);
537 g_test_add_func("/io/channel/socket/unix-sync",
538 test_io_channel_unix_sync
);
539 g_test_add_func("/io/channel/socket/unix-async",
540 test_io_channel_unix_async
);
541 g_test_add_func("/io/channel/socket/unix-fd-pass",
542 test_io_channel_unix_fd_pass
);