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
31 # define AI_V4MAPPED 0
33 #ifndef EAI_ADDRFAMILY
34 # define EAI_ADDRFAMILY 0
37 static int check_bind(const char *hostname
, bool *has_proto
)
40 struct addrinfo ai
, *res
= NULL
;
44 memset(&ai
, 0, sizeof(ai
));
45 ai
.ai_flags
= AI_CANONNAME
| AI_V4MAPPED
| AI_ADDRCONFIG
;
46 ai
.ai_family
= AF_UNSPEC
;
47 ai
.ai_socktype
= SOCK_STREAM
;
50 rc
= getaddrinfo(hostname
, NULL
, &ai
, &res
);
52 if (rc
== EAI_ADDRFAMILY
||
60 fd
= qemu_socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
65 if (bind(fd
, res
->ai_addr
, res
->ai_addrlen
) < 0) {
66 if (errno
== EADDRNOTAVAIL
) {
87 static int check_protocol_support(bool *has_ipv4
, bool *has_ipv6
)
89 if (check_bind("127.0.0.1", has_ipv4
) < 0) {
92 if (check_bind("::1", has_ipv6
) < 0) {
100 static void test_io_channel_set_socket_bufs(QIOChannel
*src
,
103 int buflen
= 64 * 1024;
106 * Make the socket buffers small so that we see
107 * the effects of partial reads/writes
109 setsockopt(((QIOChannelSocket
*)src
)->fd
,
110 SOL_SOCKET
, SO_SNDBUF
,
114 setsockopt(((QIOChannelSocket
*)dst
)->fd
,
115 SOL_SOCKET
, SO_SNDBUF
,
121 static void test_io_channel_setup_sync(SocketAddress
*listen_addr
,
122 SocketAddress
*connect_addr
,
126 QIOChannelSocket
*lioc
;
128 lioc
= qio_channel_socket_new();
129 qio_channel_socket_listen_sync(lioc
, listen_addr
, &error_abort
);
131 if (listen_addr
->type
== SOCKET_ADDRESS_KIND_INET
) {
132 SocketAddress
*laddr
= qio_channel_socket_get_local_address(
135 g_free(connect_addr
->u
.inet
.data
->port
);
136 connect_addr
->u
.inet
.data
->port
= g_strdup(laddr
->u
.inet
.data
->port
);
138 qapi_free_SocketAddress(laddr
);
141 *src
= QIO_CHANNEL(qio_channel_socket_new());
142 qio_channel_socket_connect_sync(
143 QIO_CHANNEL_SOCKET(*src
), connect_addr
, &error_abort
);
144 qio_channel_set_delay(*src
, false);
146 qio_channel_wait(QIO_CHANNEL(lioc
), G_IO_IN
);
147 *dst
= QIO_CHANNEL(qio_channel_socket_accept(lioc
, &error_abort
));
150 test_io_channel_set_socket_bufs(*src
, *dst
);
152 object_unref(OBJECT(lioc
));
156 struct TestIOChannelData
{
162 static void test_io_channel_complete(Object
*src
,
166 struct TestIOChannelData
*data
= opaque
;
167 data
->err
= err
!= NULL
;
168 g_main_loop_quit(data
->loop
);
172 static void test_io_channel_setup_async(SocketAddress
*listen_addr
,
173 SocketAddress
*connect_addr
,
177 QIOChannelSocket
*lioc
;
178 struct TestIOChannelData data
;
180 data
.loop
= g_main_loop_new(g_main_context_default(),
183 lioc
= qio_channel_socket_new();
184 qio_channel_socket_listen_async(
186 test_io_channel_complete
, &data
, NULL
);
188 g_main_loop_run(data
.loop
);
189 g_main_context_iteration(g_main_context_default(), FALSE
);
193 if (listen_addr
->type
== SOCKET_ADDRESS_KIND_INET
) {
194 SocketAddress
*laddr
= qio_channel_socket_get_local_address(
197 g_free(connect_addr
->u
.inet
.data
->port
);
198 connect_addr
->u
.inet
.data
->port
= g_strdup(laddr
->u
.inet
.data
->port
);
200 qapi_free_SocketAddress(laddr
);
203 *src
= QIO_CHANNEL(qio_channel_socket_new());
205 qio_channel_socket_connect_async(
206 QIO_CHANNEL_SOCKET(*src
), connect_addr
,
207 test_io_channel_complete
, &data
, NULL
);
209 g_main_loop_run(data
.loop
);
210 g_main_context_iteration(g_main_context_default(), FALSE
);
214 qio_channel_wait(QIO_CHANNEL(lioc
), G_IO_IN
);
215 *dst
= QIO_CHANNEL(qio_channel_socket_accept(lioc
, &error_abort
));
218 qio_channel_set_delay(*src
, false);
219 test_io_channel_set_socket_bufs(*src
, *dst
);
221 object_unref(OBJECT(lioc
));
223 g_main_loop_unref(data
.loop
);
227 static void test_io_channel(bool async
,
228 SocketAddress
*listen_addr
,
229 SocketAddress
*connect_addr
,
232 QIOChannel
*src
, *dst
;
233 QIOChannelTest
*test
;
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
, true, src
, dst
);
244 qio_channel_test_validate(test
);
246 object_unref(OBJECT(src
));
247 object_unref(OBJECT(dst
));
249 test_io_channel_setup_async(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
, 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
));
270 test
= qio_channel_test_new();
271 qio_channel_test_run_threads(test
, true, src
, dst
);
272 qio_channel_test_validate(test
);
274 object_unref(OBJECT(src
));
275 object_unref(OBJECT(dst
));
277 test_io_channel_setup_sync(listen_addr
, connect_addr
, &src
, &dst
);
280 qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_FD_PASS
));
282 qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_FD_PASS
));
284 test
= qio_channel_test_new();
285 qio_channel_test_run_threads(test
, false, src
, dst
);
286 qio_channel_test_validate(test
);
288 object_unref(OBJECT(src
));
289 object_unref(OBJECT(dst
));
294 static void test_io_channel_ipv4(bool async
)
296 SocketAddress
*listen_addr
= g_new0(SocketAddress
, 1);
297 SocketAddress
*connect_addr
= g_new0(SocketAddress
, 1);
299 listen_addr
->type
= SOCKET_ADDRESS_KIND_INET
;
300 listen_addr
->u
.inet
.data
= g_new(InetSocketAddress
, 1);
301 *listen_addr
->u
.inet
.data
= (InetSocketAddress
) {
302 .host
= g_strdup("127.0.0.1"),
303 .port
= NULL
, /* Auto-select */
306 connect_addr
->type
= SOCKET_ADDRESS_KIND_INET
;
307 connect_addr
->u
.inet
.data
= g_new(InetSocketAddress
, 1);
308 *connect_addr
->u
.inet
.data
= (InetSocketAddress
) {
309 .host
= g_strdup("127.0.0.1"),
310 .port
= NULL
, /* Filled in later */
313 test_io_channel(async
, listen_addr
, connect_addr
, false);
315 qapi_free_SocketAddress(listen_addr
);
316 qapi_free_SocketAddress(connect_addr
);
320 static void test_io_channel_ipv4_sync(void)
322 return test_io_channel_ipv4(false);
326 static void test_io_channel_ipv4_async(void)
328 return test_io_channel_ipv4(true);
332 static void test_io_channel_ipv6(bool async
)
334 SocketAddress
*listen_addr
= g_new0(SocketAddress
, 1);
335 SocketAddress
*connect_addr
= g_new0(SocketAddress
, 1);
337 listen_addr
->type
= SOCKET_ADDRESS_KIND_INET
;
338 listen_addr
->u
.inet
.data
= g_new(InetSocketAddress
, 1);
339 *listen_addr
->u
.inet
.data
= (InetSocketAddress
) {
340 .host
= g_strdup("::1"),
341 .port
= NULL
, /* Auto-select */
344 connect_addr
->type
= SOCKET_ADDRESS_KIND_INET
;
345 connect_addr
->u
.inet
.data
= g_new(InetSocketAddress
, 1);
346 *connect_addr
->u
.inet
.data
= (InetSocketAddress
) {
347 .host
= g_strdup("::1"),
348 .port
= NULL
, /* Filled in later */
351 test_io_channel(async
, listen_addr
, connect_addr
, false);
353 qapi_free_SocketAddress(listen_addr
);
354 qapi_free_SocketAddress(connect_addr
);
358 static void test_io_channel_ipv6_sync(void)
360 return test_io_channel_ipv6(false);
364 static void test_io_channel_ipv6_async(void)
366 return test_io_channel_ipv6(true);
371 static void test_io_channel_unix(bool async
)
373 SocketAddress
*listen_addr
= g_new0(SocketAddress
, 1);
374 SocketAddress
*connect_addr
= g_new0(SocketAddress
, 1);
376 #define TEST_SOCKET "test-io-channel-socket.sock"
377 listen_addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
378 listen_addr
->u
.q_unix
.data
= g_new0(UnixSocketAddress
, 1);
379 listen_addr
->u
.q_unix
.data
->path
= g_strdup(TEST_SOCKET
);
381 connect_addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
382 connect_addr
->u
.q_unix
.data
= g_new0(UnixSocketAddress
, 1);
383 connect_addr
->u
.q_unix
.data
->path
= g_strdup(TEST_SOCKET
);
385 test_io_channel(async
, listen_addr
, connect_addr
, true);
387 qapi_free_SocketAddress(listen_addr
);
388 qapi_free_SocketAddress(connect_addr
);
393 static void test_io_channel_unix_sync(void)
395 return test_io_channel_unix(false);
399 static void test_io_channel_unix_async(void)
401 return test_io_channel_unix(true);
404 static void test_io_channel_unix_fd_pass(void)
406 SocketAddress
*listen_addr
= g_new0(SocketAddress
, 1);
407 SocketAddress
*connect_addr
= g_new0(SocketAddress
, 1);
408 QIOChannel
*src
, *dst
;
414 char bufsend
[12], bufrecv
[12];
415 struct iovec iosend
[1], iorecv
[1];
417 #define TEST_SOCKET "test-io-channel-socket.sock"
418 #define TEST_FILE "test-io-channel-socket.txt"
420 testfd
= open(TEST_FILE
, O_RDWR
|O_TRUNC
|O_CREAT
, 0700);
421 g_assert(testfd
!= -1);
426 listen_addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
427 listen_addr
->u
.q_unix
.data
= g_new0(UnixSocketAddress
, 1);
428 listen_addr
->u
.q_unix
.data
->path
= g_strdup(TEST_SOCKET
);
430 connect_addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
431 connect_addr
->u
.q_unix
.data
= g_new0(UnixSocketAddress
, 1);
432 connect_addr
->u
.q_unix
.data
->path
= g_strdup(TEST_SOCKET
);
434 test_io_channel_setup_sync(listen_addr
, connect_addr
, &src
, &dst
);
436 memcpy(bufsend
, "Hello World", G_N_ELEMENTS(bufsend
));
438 iosend
[0].iov_base
= bufsend
;
439 iosend
[0].iov_len
= G_N_ELEMENTS(bufsend
);
441 iorecv
[0].iov_base
= bufrecv
;
442 iorecv
[0].iov_len
= G_N_ELEMENTS(bufrecv
);
444 g_assert(qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_FD_PASS
));
445 g_assert(qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_FD_PASS
));
447 qio_channel_writev_full(src
,
449 G_N_ELEMENTS(iosend
),
451 G_N_ELEMENTS(fdsend
),
454 qio_channel_readv_full(dst
,
456 G_N_ELEMENTS(iorecv
),
461 g_assert(nfdrecv
== G_N_ELEMENTS(fdsend
));
462 /* Each recvd FD should be different from sent FD */
463 for (i
= 0; i
< nfdrecv
; i
++) {
464 g_assert_cmpint(fdrecv
[i
], !=, testfd
);
466 /* Each recvd FD should be different from each other */
467 g_assert_cmpint(fdrecv
[0], !=, fdrecv
[1]);
468 g_assert_cmpint(fdrecv
[0], !=, fdrecv
[2]);
469 g_assert_cmpint(fdrecv
[1], !=, fdrecv
[2]);
471 /* Check the I/O buf we sent at the same time matches */
472 g_assert(memcmp(bufsend
, bufrecv
, G_N_ELEMENTS(bufsend
)) == 0);
474 /* Write some data into the FD we received */
475 g_assert(write(fdrecv
[0], bufsend
, G_N_ELEMENTS(bufsend
)) ==
476 G_N_ELEMENTS(bufsend
));
478 /* Read data from the original FD and make sure it matches */
479 memset(bufrecv
, 0, G_N_ELEMENTS(bufrecv
));
480 g_assert(lseek(testfd
, 0, SEEK_SET
) == 0);
481 g_assert(read(testfd
, bufrecv
, G_N_ELEMENTS(bufrecv
)) ==
482 G_N_ELEMENTS(bufrecv
));
483 g_assert(memcmp(bufsend
, bufrecv
, G_N_ELEMENTS(bufsend
)) == 0);
485 object_unref(OBJECT(src
));
486 object_unref(OBJECT(dst
));
487 qapi_free_SocketAddress(listen_addr
);
488 qapi_free_SocketAddress(connect_addr
);
492 for (i
= 0; i
< nfdrecv
; i
++) {
500 static void test_io_channel_ipv4_fd(void)
504 struct sockaddr_in sa
= {
505 .sin_family
= AF_INET
,
507 .s_addr
= htonl(INADDR_LOOPBACK
),
509 /* Leave port unset for auto-assign */
511 socklen_t salen
= sizeof(sa
);
513 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
514 g_assert_cmpint(fd
, >, -1);
516 g_assert_cmpint(bind(fd
, (struct sockaddr
*)&sa
, salen
), ==, 0);
518 ioc
= qio_channel_new_fd(fd
, &error_abort
);
520 g_assert_cmpstr(object_get_typename(OBJECT(ioc
)),
522 TYPE_QIO_CHANNEL_SOCKET
);
524 object_unref(OBJECT(ioc
));
528 int main(int argc
, char **argv
)
530 bool has_ipv4
, has_ipv6
;
532 module_call_init(MODULE_INIT_QOM
);
535 g_test_init(&argc
, &argv
, NULL
);
537 /* We're creating actual IPv4/6 sockets, so we should
538 * check if the host running tests actually supports
539 * each protocol to avoid breaking tests on machines
540 * with either IPv4 or IPv6 disabled.
542 if (check_protocol_support(&has_ipv4
, &has_ipv6
) < 0) {
547 g_test_add_func("/io/channel/socket/ipv4-sync",
548 test_io_channel_ipv4_sync
);
549 g_test_add_func("/io/channel/socket/ipv4-async",
550 test_io_channel_ipv4_async
);
551 g_test_add_func("/io/channel/socket/ipv4-fd",
552 test_io_channel_ipv4_fd
);
555 g_test_add_func("/io/channel/socket/ipv6-sync",
556 test_io_channel_ipv6_sync
);
557 g_test_add_func("/io/channel/socket/ipv6-async",
558 test_io_channel_ipv6_async
);
562 g_test_add_func("/io/channel/socket/unix-sync",
563 test_io_channel_unix_sync
);
564 g_test_add_func("/io/channel/socket/unix-async",
565 test_io_channel_unix_async
);
566 g_test_add_func("/io/channel/socket/unix-fd-pass",
567 test_io_channel_unix_fd_pass
);