2 * QEMU I/O channel sockets test
4 * Copyright (c) 2015 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 "io/channel-socket.h"
22 #include "io-channel-helpers.h"
27 static int check_protocol_support(bool *has_ipv4
, bool *has_ipv6
)
30 struct ifaddrs
*ifaddr
= NULL
, *ifa
;
31 struct addrinfo hints
= { 0 };
32 struct addrinfo
*ai
= NULL
;
35 *has_ipv4
= *has_ipv6
= false;
37 if (getifaddrs(&ifaddr
) < 0) {
38 g_printerr("Failed to lookup interface addresses: %s\n",
43 for (ifa
= ifaddr
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
48 if (ifa
->ifa_addr
->sa_family
== AF_INET
) {
51 if (ifa
->ifa_addr
->sa_family
== AF_INET6
) {
58 hints
.ai_flags
= AI_PASSIVE
| AI_ADDRCONFIG
;
59 hints
.ai_family
= AF_INET6
;
60 hints
.ai_socktype
= SOCK_STREAM
;
62 gaierr
= getaddrinfo("::1", NULL
, &hints
, &ai
);
64 if (gaierr
== EAI_ADDRFAMILY
||
65 gaierr
== EAI_FAMILY
||
66 gaierr
== EAI_NONAME
) {
69 g_printerr("Failed to resolve ::1 address: %s\n",
70 gai_strerror(gaierr
));
79 *has_ipv4
= *has_ipv6
= false;
86 static void test_io_channel_set_socket_bufs(QIOChannel
*src
,
89 int buflen
= 64 * 1024;
92 * Make the socket buffers small so that we see
93 * the effects of partial reads/writes
95 setsockopt(((QIOChannelSocket
*)src
)->fd
,
96 SOL_SOCKET
, SO_SNDBUF
,
100 setsockopt(((QIOChannelSocket
*)dst
)->fd
,
101 SOL_SOCKET
, SO_SNDBUF
,
107 static void test_io_channel_setup_sync(SocketAddress
*listen_addr
,
108 SocketAddress
*connect_addr
,
112 QIOChannelSocket
*lioc
;
114 lioc
= qio_channel_socket_new();
115 qio_channel_socket_listen_sync(lioc
, listen_addr
, &error_abort
);
117 if (listen_addr
->type
== SOCKET_ADDRESS_KIND_INET
) {
118 SocketAddress
*laddr
= qio_channel_socket_get_local_address(
121 g_free(connect_addr
->u
.inet
->port
);
122 connect_addr
->u
.inet
->port
= g_strdup(laddr
->u
.inet
->port
);
124 qapi_free_SocketAddress(laddr
);
127 *src
= QIO_CHANNEL(qio_channel_socket_new());
128 qio_channel_socket_connect_sync(
129 QIO_CHANNEL_SOCKET(*src
), connect_addr
, &error_abort
);
130 qio_channel_set_delay(*src
, false);
132 *dst
= QIO_CHANNEL(qio_channel_socket_accept(lioc
, &error_abort
));
135 test_io_channel_set_socket_bufs(*src
, *dst
);
137 object_unref(OBJECT(lioc
));
141 struct TestIOChannelData
{
147 static void test_io_channel_complete(Object
*src
,
151 struct TestIOChannelData
*data
= opaque
;
152 data
->err
= err
!= NULL
;
153 g_main_loop_quit(data
->loop
);
157 static void test_io_channel_setup_async(SocketAddress
*listen_addr
,
158 SocketAddress
*connect_addr
,
162 QIOChannelSocket
*lioc
;
163 struct TestIOChannelData data
;
165 data
.loop
= g_main_loop_new(g_main_context_default(),
168 lioc
= qio_channel_socket_new();
169 qio_channel_socket_listen_async(
171 test_io_channel_complete
, &data
, NULL
);
173 g_main_loop_run(data
.loop
);
174 g_main_context_iteration(g_main_context_default(), FALSE
);
178 if (listen_addr
->type
== SOCKET_ADDRESS_KIND_INET
) {
179 SocketAddress
*laddr
= qio_channel_socket_get_local_address(
182 g_free(connect_addr
->u
.inet
->port
);
183 connect_addr
->u
.inet
->port
= g_strdup(laddr
->u
.inet
->port
);
185 qapi_free_SocketAddress(laddr
);
188 *src
= QIO_CHANNEL(qio_channel_socket_new());
190 qio_channel_socket_connect_async(
191 QIO_CHANNEL_SOCKET(*src
), connect_addr
,
192 test_io_channel_complete
, &data
, NULL
);
194 g_main_loop_run(data
.loop
);
195 g_main_context_iteration(g_main_context_default(), FALSE
);
199 *dst
= QIO_CHANNEL(qio_channel_socket_accept(lioc
, &error_abort
));
202 qio_channel_set_delay(*src
, false);
203 test_io_channel_set_socket_bufs(*src
, *dst
);
205 object_unref(OBJECT(lioc
));
207 g_main_loop_unref(data
.loop
);
211 static void test_io_channel(bool async
,
212 SocketAddress
*listen_addr
,
213 SocketAddress
*connect_addr
,
216 QIOChannel
*src
, *dst
;
217 QIOChannelTest
*test
;
219 test_io_channel_setup_async(listen_addr
, connect_addr
, &src
, &dst
);
222 qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_FD_PASS
));
224 qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_FD_PASS
));
226 test
= qio_channel_test_new();
227 qio_channel_test_run_threads(test
, true, src
, dst
);
228 qio_channel_test_validate(test
);
230 object_unref(OBJECT(src
));
231 object_unref(OBJECT(dst
));
233 test_io_channel_setup_async(listen_addr
, connect_addr
, &src
, &dst
);
236 qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_FD_PASS
));
238 qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_FD_PASS
));
240 test
= qio_channel_test_new();
241 qio_channel_test_run_threads(test
, false, src
, dst
);
242 qio_channel_test_validate(test
);
244 object_unref(OBJECT(src
));
245 object_unref(OBJECT(dst
));
247 test_io_channel_setup_sync(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
));
254 test
= qio_channel_test_new();
255 qio_channel_test_run_threads(test
, true, src
, dst
);
256 qio_channel_test_validate(test
);
258 object_unref(OBJECT(src
));
259 object_unref(OBJECT(dst
));
261 test_io_channel_setup_sync(listen_addr
, connect_addr
, &src
, &dst
);
264 qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_FD_PASS
));
266 qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_FD_PASS
));
268 test
= qio_channel_test_new();
269 qio_channel_test_run_threads(test
, false, src
, dst
);
270 qio_channel_test_validate(test
);
272 object_unref(OBJECT(src
));
273 object_unref(OBJECT(dst
));
278 static void test_io_channel_ipv4(bool async
)
280 SocketAddress
*listen_addr
= g_new0(SocketAddress
, 1);
281 SocketAddress
*connect_addr
= g_new0(SocketAddress
, 1);
283 listen_addr
->type
= SOCKET_ADDRESS_KIND_INET
;
284 listen_addr
->u
.inet
= g_new0(InetSocketAddress
, 1);
285 listen_addr
->u
.inet
->host
= g_strdup("127.0.0.1");
286 listen_addr
->u
.inet
->port
= NULL
; /* Auto-select */
288 connect_addr
->type
= SOCKET_ADDRESS_KIND_INET
;
289 connect_addr
->u
.inet
= g_new0(InetSocketAddress
, 1);
290 connect_addr
->u
.inet
->host
= g_strdup("127.0.0.1");
291 connect_addr
->u
.inet
->port
= NULL
; /* Filled in later */
293 test_io_channel(async
, listen_addr
, connect_addr
, false);
295 qapi_free_SocketAddress(listen_addr
);
296 qapi_free_SocketAddress(connect_addr
);
300 static void test_io_channel_ipv4_sync(void)
302 return test_io_channel_ipv4(false);
306 static void test_io_channel_ipv4_async(void)
308 return test_io_channel_ipv4(true);
312 static void test_io_channel_ipv6(bool async
)
314 SocketAddress
*listen_addr
= g_new0(SocketAddress
, 1);
315 SocketAddress
*connect_addr
= g_new0(SocketAddress
, 1);
317 listen_addr
->type
= SOCKET_ADDRESS_KIND_INET
;
318 listen_addr
->u
.inet
= g_new0(InetSocketAddress
, 1);
319 listen_addr
->u
.inet
->host
= g_strdup("::1");
320 listen_addr
->u
.inet
->port
= NULL
; /* Auto-select */
322 connect_addr
->type
= SOCKET_ADDRESS_KIND_INET
;
323 connect_addr
->u
.inet
= g_new0(InetSocketAddress
, 1);
324 connect_addr
->u
.inet
->host
= g_strdup("::1");
325 connect_addr
->u
.inet
->port
= NULL
; /* Filled in later */
327 test_io_channel(async
, listen_addr
, connect_addr
, false);
329 qapi_free_SocketAddress(listen_addr
);
330 qapi_free_SocketAddress(connect_addr
);
334 static void test_io_channel_ipv6_sync(void)
336 return test_io_channel_ipv6(false);
340 static void test_io_channel_ipv6_async(void)
342 return test_io_channel_ipv6(true);
347 static void test_io_channel_unix(bool async
)
349 SocketAddress
*listen_addr
= g_new0(SocketAddress
, 1);
350 SocketAddress
*connect_addr
= g_new0(SocketAddress
, 1);
352 #define TEST_SOCKET "test-io-channel-socket.sock"
353 listen_addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
354 listen_addr
->u
.q_unix
= g_new0(UnixSocketAddress
, 1);
355 listen_addr
->u
.q_unix
->path
= g_strdup(TEST_SOCKET
);
357 connect_addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
358 connect_addr
->u
.q_unix
= g_new0(UnixSocketAddress
, 1);
359 connect_addr
->u
.q_unix
->path
= g_strdup(TEST_SOCKET
);
361 test_io_channel(async
, listen_addr
, connect_addr
, true);
363 qapi_free_SocketAddress(listen_addr
);
364 qapi_free_SocketAddress(connect_addr
);
369 static void test_io_channel_unix_sync(void)
371 return test_io_channel_unix(false);
375 static void test_io_channel_unix_async(void)
377 return test_io_channel_unix(true);
380 static void test_io_channel_unix_fd_pass(void)
382 SocketAddress
*listen_addr
= g_new0(SocketAddress
, 1);
383 SocketAddress
*connect_addr
= g_new0(SocketAddress
, 1);
384 QIOChannel
*src
, *dst
;
390 char bufsend
[12], bufrecv
[12];
391 struct iovec iosend
[1], iorecv
[1];
393 #define TEST_SOCKET "test-io-channel-socket.sock"
394 #define TEST_FILE "test-io-channel-socket.txt"
396 testfd
= open(TEST_FILE
, O_RDWR
|O_TRUNC
|O_CREAT
, 0700);
397 g_assert(testfd
!= -1);
402 listen_addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
403 listen_addr
->u
.q_unix
= g_new0(UnixSocketAddress
, 1);
404 listen_addr
->u
.q_unix
->path
= g_strdup(TEST_SOCKET
);
406 connect_addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
407 connect_addr
->u
.q_unix
= g_new0(UnixSocketAddress
, 1);
408 connect_addr
->u
.q_unix
->path
= g_strdup(TEST_SOCKET
);
410 test_io_channel_setup_sync(listen_addr
, connect_addr
, &src
, &dst
);
412 memcpy(bufsend
, "Hello World", G_N_ELEMENTS(bufsend
));
414 iosend
[0].iov_base
= bufsend
;
415 iosend
[0].iov_len
= G_N_ELEMENTS(bufsend
);
417 iorecv
[0].iov_base
= bufrecv
;
418 iorecv
[0].iov_len
= G_N_ELEMENTS(bufrecv
);
420 g_assert(qio_channel_has_feature(src
, QIO_CHANNEL_FEATURE_FD_PASS
));
421 g_assert(qio_channel_has_feature(dst
, QIO_CHANNEL_FEATURE_FD_PASS
));
423 qio_channel_writev_full(src
,
425 G_N_ELEMENTS(iosend
),
427 G_N_ELEMENTS(fdsend
),
430 qio_channel_readv_full(dst
,
432 G_N_ELEMENTS(iorecv
),
437 g_assert(nfdrecv
== G_N_ELEMENTS(fdsend
));
438 /* Each recvd FD should be different from sent FD */
439 for (i
= 0; i
< nfdrecv
; i
++) {
440 g_assert_cmpint(fdrecv
[i
], !=, testfd
);
442 /* Each recvd FD should be different from each other */
443 g_assert_cmpint(fdrecv
[0], !=, fdrecv
[1]);
444 g_assert_cmpint(fdrecv
[0], !=, fdrecv
[2]);
445 g_assert_cmpint(fdrecv
[1], !=, fdrecv
[2]);
447 /* Check the I/O buf we sent at the same time matches */
448 g_assert(memcmp(bufsend
, bufrecv
, G_N_ELEMENTS(bufsend
)) == 0);
450 /* Write some data into the FD we received */
451 g_assert(write(fdrecv
[0], bufsend
, G_N_ELEMENTS(bufsend
)) ==
452 G_N_ELEMENTS(bufsend
));
454 /* Read data from the original FD and make sure it matches */
455 memset(bufrecv
, 0, G_N_ELEMENTS(bufrecv
));
456 g_assert(lseek(testfd
, 0, SEEK_SET
) == 0);
457 g_assert(read(testfd
, bufrecv
, G_N_ELEMENTS(bufrecv
)) ==
458 G_N_ELEMENTS(bufrecv
));
459 g_assert(memcmp(bufsend
, bufrecv
, G_N_ELEMENTS(bufsend
)) == 0);
461 object_unref(OBJECT(src
));
462 object_unref(OBJECT(dst
));
463 qapi_free_SocketAddress(listen_addr
);
464 qapi_free_SocketAddress(connect_addr
);
468 for (i
= 0; i
< nfdrecv
; i
++) {
476 int main(int argc
, char **argv
)
478 bool has_ipv4
, has_ipv6
;
480 module_call_init(MODULE_INIT_QOM
);
482 g_test_init(&argc
, &argv
, NULL
);
484 /* We're creating actual IPv4/6 sockets, so we should
485 * check if the host running tests actually supports
486 * each protocol to avoid breaking tests on machines
487 * with either IPv4 or IPv6 disabled.
489 if (check_protocol_support(&has_ipv4
, &has_ipv6
) < 0) {
494 g_test_add_func("/io/channel/socket/ipv4-sync",
495 test_io_channel_ipv4_sync
);
496 g_test_add_func("/io/channel/socket/ipv4-async",
497 test_io_channel_ipv4_async
);
500 g_test_add_func("/io/channel/socket/ipv6-sync",
501 test_io_channel_ipv6_sync
);
502 g_test_add_func("/io/channel/socket/ipv6-async",
503 test_io_channel_ipv6_async
);
507 g_test_add_func("/io/channel/socket/unix-sync",
508 test_io_channel_unix_sync
);
509 g_test_add_func("/io/channel/socket/unix-async",
510 test_io_channel_unix_async
);
511 g_test_add_func("/io/channel/socket/unix-fd-pass",
512 test_io_channel_unix_fd_pass
);