pci core: function pci_bus_init() cleanup
[qemu/ar7.git] / tests / test-io-channel-socket.c
blob069736373c3096c1eee082e9518c23bb18446bc1
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 "qemu/osdep.h"
22 #include "io/channel-socket.h"
23 #include "io/channel-util.h"
24 #include "io-channel-helpers.h"
25 #ifdef HAVE_IFADDRS_H
26 #include <ifaddrs.h>
27 #endif
29 static int check_protocol_support(bool *has_ipv4, bool *has_ipv6)
31 #ifdef HAVE_IFADDRS_H
32 struct ifaddrs *ifaddr = NULL, *ifa;
33 struct addrinfo hints = { 0 };
34 struct addrinfo *ai = NULL;
35 int gaierr;
37 *has_ipv4 = *has_ipv6 = false;
39 if (getifaddrs(&ifaddr) < 0) {
40 g_printerr("Failed to lookup interface addresses: %s\n",
41 strerror(errno));
42 return -1;
45 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
46 if (!ifa->ifa_addr) {
47 continue;
50 if (ifa->ifa_addr->sa_family == AF_INET) {
51 *has_ipv4 = true;
53 if (ifa->ifa_addr->sa_family == AF_INET6) {
54 *has_ipv6 = true;
58 freeifaddrs(ifaddr);
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);
65 if (gaierr != 0) {
66 if (gaierr == EAI_ADDRFAMILY ||
67 gaierr == EAI_FAMILY ||
68 gaierr == EAI_NONAME) {
69 *has_ipv6 = false;
70 } else {
71 g_printerr("Failed to resolve ::1 address: %s\n",
72 gai_strerror(gaierr));
73 return -1;
77 freeaddrinfo(ai);
79 return 0;
80 #else
81 *has_ipv4 = *has_ipv6 = false;
83 return -1;
84 #endif
88 static void test_io_channel_set_socket_bufs(QIOChannel *src,
89 QIOChannel *dst)
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,
99 (char *)&buflen,
100 sizeof(buflen));
102 setsockopt(((QIOChannelSocket *)dst)->fd,
103 SOL_SOCKET, SO_SNDBUF,
104 (char *)&buflen,
105 sizeof(buflen));
109 static void test_io_channel_setup_sync(SocketAddress *listen_addr,
110 SocketAddress *connect_addr,
111 QIOChannel **src,
112 QIOChannel **dst)
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(
121 lioc, &error_abort);
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));
135 g_assert(*dst);
137 test_io_channel_set_socket_bufs(*src, *dst);
139 object_unref(OBJECT(lioc));
143 struct TestIOChannelData {
144 bool err;
145 GMainLoop *loop;
149 static void test_io_channel_complete(Object *src,
150 Error *err,
151 gpointer opaque)
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,
161 QIOChannel **src,
162 QIOChannel **dst)
164 QIOChannelSocket *lioc;
165 struct TestIOChannelData data;
167 data.loop = g_main_loop_new(g_main_context_default(),
168 TRUE);
170 lioc = qio_channel_socket_new();
171 qio_channel_socket_listen_async(
172 lioc, listen_addr,
173 test_io_channel_complete, &data, NULL);
175 g_main_loop_run(data.loop);
176 g_main_context_iteration(g_main_context_default(), FALSE);
178 g_assert(!data.err);
180 if (listen_addr->type == SOCKET_ADDRESS_KIND_INET) {
181 SocketAddress *laddr = qio_channel_socket_get_local_address(
182 lioc, &error_abort);
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);
199 g_assert(!data.err);
201 *dst = QIO_CHANNEL(qio_channel_socket_accept(lioc, &error_abort));
202 g_assert(*dst);
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,
216 bool passFD)
218 QIOChannel *src, *dst;
219 QIOChannelTest *test;
220 if (async) {
221 test_io_channel_setup_async(listen_addr, connect_addr, &src, &dst);
223 g_assert(!passFD ||
224 qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
225 g_assert(!passFD ||
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);
237 g_assert(!passFD ||
238 qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
239 g_assert(!passFD ||
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));
248 } else {
249 test_io_channel_setup_sync(listen_addr, connect_addr, &src, &dst);
251 g_assert(!passFD ||
252 qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
253 g_assert(!passFD ||
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);
265 g_assert(!passFD ||
266 qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
267 g_assert(!passFD ||
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_new0(InetSocketAddress, 1);
287 listen_addr->u.inet->host = g_strdup("127.0.0.1");
288 listen_addr->u.inet->port = NULL; /* Auto-select */
290 connect_addr->type = SOCKET_ADDRESS_KIND_INET;
291 connect_addr->u.inet = g_new0(InetSocketAddress, 1);
292 connect_addr->u.inet->host = g_strdup("127.0.0.1");
293 connect_addr->u.inet->port = NULL; /* Filled in later */
295 test_io_channel(async, listen_addr, connect_addr, false);
297 qapi_free_SocketAddress(listen_addr);
298 qapi_free_SocketAddress(connect_addr);
302 static void test_io_channel_ipv4_sync(void)
304 return test_io_channel_ipv4(false);
308 static void test_io_channel_ipv4_async(void)
310 return test_io_channel_ipv4(true);
314 static void test_io_channel_ipv6(bool async)
316 SocketAddress *listen_addr = g_new0(SocketAddress, 1);
317 SocketAddress *connect_addr = g_new0(SocketAddress, 1);
319 listen_addr->type = SOCKET_ADDRESS_KIND_INET;
320 listen_addr->u.inet = g_new0(InetSocketAddress, 1);
321 listen_addr->u.inet->host = g_strdup("::1");
322 listen_addr->u.inet->port = NULL; /* Auto-select */
324 connect_addr->type = SOCKET_ADDRESS_KIND_INET;
325 connect_addr->u.inet = g_new0(InetSocketAddress, 1);
326 connect_addr->u.inet->host = g_strdup("::1");
327 connect_addr->u.inet->port = NULL; /* Filled in later */
329 test_io_channel(async, listen_addr, connect_addr, false);
331 qapi_free_SocketAddress(listen_addr);
332 qapi_free_SocketAddress(connect_addr);
336 static void test_io_channel_ipv6_sync(void)
338 return test_io_channel_ipv6(false);
342 static void test_io_channel_ipv6_async(void)
344 return test_io_channel_ipv6(true);
348 #ifndef _WIN32
349 static void test_io_channel_unix(bool async)
351 SocketAddress *listen_addr = g_new0(SocketAddress, 1);
352 SocketAddress *connect_addr = g_new0(SocketAddress, 1);
354 #define TEST_SOCKET "test-io-channel-socket.sock"
355 listen_addr->type = SOCKET_ADDRESS_KIND_UNIX;
356 listen_addr->u.q_unix = g_new0(UnixSocketAddress, 1);
357 listen_addr->u.q_unix->path = g_strdup(TEST_SOCKET);
359 connect_addr->type = SOCKET_ADDRESS_KIND_UNIX;
360 connect_addr->u.q_unix = g_new0(UnixSocketAddress, 1);
361 connect_addr->u.q_unix->path = g_strdup(TEST_SOCKET);
363 test_io_channel(async, listen_addr, connect_addr, true);
365 qapi_free_SocketAddress(listen_addr);
366 qapi_free_SocketAddress(connect_addr);
367 unlink(TEST_SOCKET);
371 static void test_io_channel_unix_sync(void)
373 return test_io_channel_unix(false);
377 static void test_io_channel_unix_async(void)
379 return test_io_channel_unix(true);
382 static void test_io_channel_unix_fd_pass(void)
384 SocketAddress *listen_addr = g_new0(SocketAddress, 1);
385 SocketAddress *connect_addr = g_new0(SocketAddress, 1);
386 QIOChannel *src, *dst;
387 int testfd;
388 int fdsend[3];
389 int *fdrecv = NULL;
390 size_t nfdrecv = 0;
391 size_t i;
392 char bufsend[12], bufrecv[12];
393 struct iovec iosend[1], iorecv[1];
395 #define TEST_SOCKET "test-io-channel-socket.sock"
396 #define TEST_FILE "test-io-channel-socket.txt"
398 testfd = open(TEST_FILE, O_RDWR|O_TRUNC|O_CREAT, 0700);
399 g_assert(testfd != -1);
400 fdsend[0] = testfd;
401 fdsend[1] = testfd;
402 fdsend[2] = testfd;
404 listen_addr->type = SOCKET_ADDRESS_KIND_UNIX;
405 listen_addr->u.q_unix = g_new0(UnixSocketAddress, 1);
406 listen_addr->u.q_unix->path = g_strdup(TEST_SOCKET);
408 connect_addr->type = SOCKET_ADDRESS_KIND_UNIX;
409 connect_addr->u.q_unix = g_new0(UnixSocketAddress, 1);
410 connect_addr->u.q_unix->path = g_strdup(TEST_SOCKET);
412 test_io_channel_setup_sync(listen_addr, connect_addr, &src, &dst);
414 memcpy(bufsend, "Hello World", G_N_ELEMENTS(bufsend));
416 iosend[0].iov_base = bufsend;
417 iosend[0].iov_len = G_N_ELEMENTS(bufsend);
419 iorecv[0].iov_base = bufrecv;
420 iorecv[0].iov_len = G_N_ELEMENTS(bufrecv);
422 g_assert(qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
423 g_assert(qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_FD_PASS));
425 qio_channel_writev_full(src,
426 iosend,
427 G_N_ELEMENTS(iosend),
428 fdsend,
429 G_N_ELEMENTS(fdsend),
430 &error_abort);
432 qio_channel_readv_full(dst,
433 iorecv,
434 G_N_ELEMENTS(iorecv),
435 &fdrecv,
436 &nfdrecv,
437 &error_abort);
439 g_assert(nfdrecv == G_N_ELEMENTS(fdsend));
440 /* Each recvd FD should be different from sent FD */
441 for (i = 0; i < nfdrecv; i++) {
442 g_assert_cmpint(fdrecv[i], !=, testfd);
444 /* Each recvd FD should be different from each other */
445 g_assert_cmpint(fdrecv[0], !=, fdrecv[1]);
446 g_assert_cmpint(fdrecv[0], !=, fdrecv[2]);
447 g_assert_cmpint(fdrecv[1], !=, fdrecv[2]);
449 /* Check the I/O buf we sent at the same time matches */
450 g_assert(memcmp(bufsend, bufrecv, G_N_ELEMENTS(bufsend)) == 0);
452 /* Write some data into the FD we received */
453 g_assert(write(fdrecv[0], bufsend, G_N_ELEMENTS(bufsend)) ==
454 G_N_ELEMENTS(bufsend));
456 /* Read data from the original FD and make sure it matches */
457 memset(bufrecv, 0, G_N_ELEMENTS(bufrecv));
458 g_assert(lseek(testfd, 0, SEEK_SET) == 0);
459 g_assert(read(testfd, bufrecv, G_N_ELEMENTS(bufrecv)) ==
460 G_N_ELEMENTS(bufrecv));
461 g_assert(memcmp(bufsend, bufrecv, G_N_ELEMENTS(bufsend)) == 0);
463 object_unref(OBJECT(src));
464 object_unref(OBJECT(dst));
465 qapi_free_SocketAddress(listen_addr);
466 qapi_free_SocketAddress(connect_addr);
467 unlink(TEST_SOCKET);
468 unlink(TEST_FILE);
469 close(testfd);
470 for (i = 0; i < nfdrecv; i++) {
471 close(fdrecv[i]);
473 g_free(fdrecv);
475 #endif /* _WIN32 */
478 static void test_io_channel_ipv4_fd(void)
480 QIOChannel *ioc;
481 int fd = -1;
483 fd = socket(AF_INET, SOCK_STREAM, 0);
484 g_assert_cmpint(fd, >, -1);
486 ioc = qio_channel_new_fd(fd, &error_abort);
488 g_assert_cmpstr(object_get_typename(OBJECT(ioc)),
490 TYPE_QIO_CHANNEL_SOCKET);
492 object_unref(OBJECT(ioc));
496 int main(int argc, char **argv)
498 bool has_ipv4, has_ipv6;
500 module_call_init(MODULE_INIT_QOM);
502 g_test_init(&argc, &argv, NULL);
504 /* We're creating actual IPv4/6 sockets, so we should
505 * check if the host running tests actually supports
506 * each protocol to avoid breaking tests on machines
507 * with either IPv4 or IPv6 disabled.
509 if (check_protocol_support(&has_ipv4, &has_ipv6) < 0) {
510 return 1;
513 if (has_ipv4) {
514 g_test_add_func("/io/channel/socket/ipv4-sync",
515 test_io_channel_ipv4_sync);
516 g_test_add_func("/io/channel/socket/ipv4-async",
517 test_io_channel_ipv4_async);
518 g_test_add_func("/io/channel/socket/ipv4-fd",
519 test_io_channel_ipv4_fd);
521 if (has_ipv6) {
522 g_test_add_func("/io/channel/socket/ipv6-sync",
523 test_io_channel_ipv6_sync);
524 g_test_add_func("/io/channel/socket/ipv6-async",
525 test_io_channel_ipv6_async);
528 #ifndef _WIN32
529 g_test_add_func("/io/channel/socket/unix-sync",
530 test_io_channel_unix_sync);
531 g_test_add_func("/io/channel/socket/unix-async",
532 test_io_channel_unix_async);
533 g_test_add_func("/io/channel/socket/unix-fd-pass",
534 test_io_channel_unix_fd_pass);
535 #endif /* _WIN32 */
537 return g_test_run();