MAINTAINERS: add all-match entry for qemu-devel@
[qemu/cris-port.git] / tests / test-io-channel-socket.c
blob1a36a3c69626a972fffec1cd81757b3d9df470da
1 /*
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"
23 #ifdef HAVE_IFADDRS_H
24 #include <ifaddrs.h>
25 #endif
27 static int check_protocol_support(bool *has_ipv4, bool *has_ipv6)
29 #ifdef HAVE_IFADDRS_H
30 struct ifaddrs *ifaddr = NULL, *ifa;
31 struct addrinfo hints = { 0 };
32 struct addrinfo *ai = NULL;
33 int gaierr;
35 *has_ipv4 = *has_ipv6 = false;
37 if (getifaddrs(&ifaddr) < 0) {
38 g_printerr("Failed to lookup interface addresses: %s\n",
39 strerror(errno));
40 return -1;
43 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
44 if (!ifa->ifa_addr) {
45 continue;
48 if (ifa->ifa_addr->sa_family == AF_INET) {
49 *has_ipv4 = true;
51 if (ifa->ifa_addr->sa_family == AF_INET6) {
52 *has_ipv6 = true;
56 freeifaddrs(ifaddr);
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);
63 if (gaierr != 0) {
64 if (gaierr == EAI_ADDRFAMILY ||
65 gaierr == EAI_FAMILY ||
66 gaierr == EAI_NONAME) {
67 *has_ipv6 = false;
68 } else {
69 g_printerr("Failed to resolve ::1 address: %s\n",
70 gai_strerror(gaierr));
71 return -1;
75 freeaddrinfo(ai);
77 return 0;
78 #else
79 *has_ipv4 = *has_ipv6 = false;
81 return -1;
82 #endif
86 static void test_io_channel_set_socket_bufs(QIOChannel *src,
87 QIOChannel *dst)
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,
97 (char *)&buflen,
98 sizeof(buflen));
100 setsockopt(((QIOChannelSocket *)dst)->fd,
101 SOL_SOCKET, SO_SNDBUF,
102 (char *)&buflen,
103 sizeof(buflen));
107 static void test_io_channel_setup_sync(SocketAddress *listen_addr,
108 SocketAddress *connect_addr,
109 QIOChannel **src,
110 QIOChannel **dst)
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(
119 lioc, &error_abort);
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));
133 g_assert(*dst);
135 test_io_channel_set_socket_bufs(*src, *dst);
137 object_unref(OBJECT(lioc));
141 struct TestIOChannelData {
142 bool err;
143 GMainLoop *loop;
147 static void test_io_channel_complete(Object *src,
148 Error *err,
149 gpointer opaque)
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,
159 QIOChannel **src,
160 QIOChannel **dst)
162 QIOChannelSocket *lioc;
163 struct TestIOChannelData data;
165 data.loop = g_main_loop_new(g_main_context_default(),
166 TRUE);
168 lioc = qio_channel_socket_new();
169 qio_channel_socket_listen_async(
170 lioc, listen_addr,
171 test_io_channel_complete, &data, NULL);
173 g_main_loop_run(data.loop);
174 g_main_context_iteration(g_main_context_default(), FALSE);
176 g_assert(!data.err);
178 if (listen_addr->type == SOCKET_ADDRESS_KIND_INET) {
179 SocketAddress *laddr = qio_channel_socket_get_local_address(
180 lioc, &error_abort);
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);
197 g_assert(!data.err);
199 *dst = QIO_CHANNEL(qio_channel_socket_accept(lioc, &error_abort));
200 g_assert(*dst);
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,
214 bool passFD)
216 QIOChannel *src, *dst;
217 QIOChannelTest *test;
218 if (async) {
219 test_io_channel_setup_async(listen_addr, connect_addr, &src, &dst);
221 g_assert(!passFD ||
222 qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
223 g_assert(!passFD ||
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);
235 g_assert(!passFD ||
236 qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
237 g_assert(!passFD ||
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));
246 } else {
247 test_io_channel_setup_sync(listen_addr, connect_addr, &src, &dst);
249 g_assert(!passFD ||
250 qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
251 g_assert(!passFD ||
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);
263 g_assert(!passFD ||
264 qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
265 g_assert(!passFD ||
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);
346 #ifndef _WIN32
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);
365 unlink(TEST_SOCKET);
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;
385 int testfd;
386 int fdsend[3];
387 int *fdrecv = NULL;
388 size_t nfdrecv = 0;
389 size_t i;
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);
398 fdsend[0] = testfd;
399 fdsend[1] = testfd;
400 fdsend[2] = testfd;
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,
424 iosend,
425 G_N_ELEMENTS(iosend),
426 fdsend,
427 G_N_ELEMENTS(fdsend),
428 &error_abort);
430 qio_channel_readv_full(dst,
431 iorecv,
432 G_N_ELEMENTS(iorecv),
433 &fdrecv,
434 &nfdrecv,
435 &error_abort);
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);
465 unlink(TEST_SOCKET);
466 unlink(TEST_FILE);
467 close(testfd);
468 for (i = 0; i < nfdrecv; i++) {
469 close(fdrecv[i]);
471 g_free(fdrecv);
473 #endif /* _WIN32 */
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) {
490 return 1;
493 if (has_ipv4) {
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);
499 if (has_ipv6) {
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);
506 #ifndef _WIN32
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);
513 #endif /* _WIN32 */
515 return g_test_run();