vmdk: Switch to heap arrays for vmdk_read_cid
[qemu/ar7.git] / tests / test-io-channel-socket.c
blobae665f5ea01284999d944a21d392ed46c6e43bda
1 /*
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"
26 static int check_bind(struct sockaddr *sa, socklen_t salen, bool *has_proto)
28 int fd;
30 fd = socket(sa->sa_family, SOCK_STREAM, 0);
31 if (fd < 0) {
32 return -1;
35 if (bind(fd, sa, salen) < 0) {
36 close(fd);
37 if (errno == EADDRNOTAVAIL) {
38 *has_proto = false;
39 return 0;
41 return -1;
44 close(fd);
45 *has_proto = true;
46 return 0;
49 static int check_protocol_support(bool *has_ipv4, bool *has_ipv6)
51 struct sockaddr_in sin = {
52 .sin_family = AF_INET,
53 .sin_addr = { .s_addr = htonl(INADDR_LOOPBACK) },
55 struct sockaddr_in6 sin6 = {
56 .sin6_family = AF_INET6,
57 .sin6_addr = IN6ADDR_LOOPBACK_INIT,
60 if (check_bind((struct sockaddr *)&sin, sizeof(sin), has_ipv4) < 0) {
61 return -1;
63 if (check_bind((struct sockaddr *)&sin6, sizeof(sin6), has_ipv6) < 0) {
64 return -1;
67 return 0;
71 static void test_io_channel_set_socket_bufs(QIOChannel *src,
72 QIOChannel *dst)
74 int buflen = 64 * 1024;
77 * Make the socket buffers small so that we see
78 * the effects of partial reads/writes
80 setsockopt(((QIOChannelSocket *)src)->fd,
81 SOL_SOCKET, SO_SNDBUF,
82 (char *)&buflen,
83 sizeof(buflen));
85 setsockopt(((QIOChannelSocket *)dst)->fd,
86 SOL_SOCKET, SO_SNDBUF,
87 (char *)&buflen,
88 sizeof(buflen));
92 static void test_io_channel_setup_sync(SocketAddress *listen_addr,
93 SocketAddress *connect_addr,
94 QIOChannel **src,
95 QIOChannel **dst)
97 QIOChannelSocket *lioc;
99 lioc = qio_channel_socket_new();
100 qio_channel_socket_listen_sync(lioc, listen_addr, &error_abort);
102 if (listen_addr->type == SOCKET_ADDRESS_KIND_INET) {
103 SocketAddress *laddr = qio_channel_socket_get_local_address(
104 lioc, &error_abort);
106 g_free(connect_addr->u.inet->port);
107 connect_addr->u.inet->port = g_strdup(laddr->u.inet->port);
109 qapi_free_SocketAddress(laddr);
112 *src = QIO_CHANNEL(qio_channel_socket_new());
113 qio_channel_socket_connect_sync(
114 QIO_CHANNEL_SOCKET(*src), connect_addr, &error_abort);
115 qio_channel_set_delay(*src, false);
117 qio_channel_wait(QIO_CHANNEL(lioc), G_IO_IN);
118 *dst = QIO_CHANNEL(qio_channel_socket_accept(lioc, &error_abort));
119 g_assert(*dst);
121 test_io_channel_set_socket_bufs(*src, *dst);
123 object_unref(OBJECT(lioc));
127 struct TestIOChannelData {
128 bool err;
129 GMainLoop *loop;
133 static void test_io_channel_complete(Object *src,
134 Error *err,
135 gpointer opaque)
137 struct TestIOChannelData *data = opaque;
138 data->err = err != NULL;
139 g_main_loop_quit(data->loop);
143 static void test_io_channel_setup_async(SocketAddress *listen_addr,
144 SocketAddress *connect_addr,
145 QIOChannel **src,
146 QIOChannel **dst)
148 QIOChannelSocket *lioc;
149 struct TestIOChannelData data;
151 data.loop = g_main_loop_new(g_main_context_default(),
152 TRUE);
154 lioc = qio_channel_socket_new();
155 qio_channel_socket_listen_async(
156 lioc, listen_addr,
157 test_io_channel_complete, &data, NULL);
159 g_main_loop_run(data.loop);
160 g_main_context_iteration(g_main_context_default(), FALSE);
162 g_assert(!data.err);
164 if (listen_addr->type == SOCKET_ADDRESS_KIND_INET) {
165 SocketAddress *laddr = qio_channel_socket_get_local_address(
166 lioc, &error_abort);
168 g_free(connect_addr->u.inet->port);
169 connect_addr->u.inet->port = g_strdup(laddr->u.inet->port);
171 qapi_free_SocketAddress(laddr);
174 *src = QIO_CHANNEL(qio_channel_socket_new());
176 qio_channel_socket_connect_async(
177 QIO_CHANNEL_SOCKET(*src), connect_addr,
178 test_io_channel_complete, &data, NULL);
180 g_main_loop_run(data.loop);
181 g_main_context_iteration(g_main_context_default(), FALSE);
183 g_assert(!data.err);
185 qio_channel_wait(QIO_CHANNEL(lioc), G_IO_IN);
186 *dst = QIO_CHANNEL(qio_channel_socket_accept(lioc, &error_abort));
187 g_assert(*dst);
189 qio_channel_set_delay(*src, false);
190 test_io_channel_set_socket_bufs(*src, *dst);
192 object_unref(OBJECT(lioc));
194 g_main_loop_unref(data.loop);
198 static void test_io_channel(bool async,
199 SocketAddress *listen_addr,
200 SocketAddress *connect_addr,
201 bool passFD)
203 QIOChannel *src, *dst;
204 QIOChannelTest *test;
205 if (async) {
206 test_io_channel_setup_async(listen_addr, connect_addr, &src, &dst);
208 g_assert(!passFD ||
209 qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
210 g_assert(!passFD ||
211 qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_FD_PASS));
213 test = qio_channel_test_new();
214 qio_channel_test_run_threads(test, true, src, dst);
215 qio_channel_test_validate(test);
217 object_unref(OBJECT(src));
218 object_unref(OBJECT(dst));
220 test_io_channel_setup_async(listen_addr, connect_addr, &src, &dst);
222 g_assert(!passFD ||
223 qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
224 g_assert(!passFD ||
225 qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_FD_PASS));
227 test = qio_channel_test_new();
228 qio_channel_test_run_threads(test, false, src, dst);
229 qio_channel_test_validate(test);
231 object_unref(OBJECT(src));
232 object_unref(OBJECT(dst));
233 } else {
234 test_io_channel_setup_sync(listen_addr, connect_addr, &src, &dst);
236 g_assert(!passFD ||
237 qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
238 g_assert(!passFD ||
239 qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_FD_PASS));
241 test = qio_channel_test_new();
242 qio_channel_test_run_threads(test, true, src, dst);
243 qio_channel_test_validate(test);
245 object_unref(OBJECT(src));
246 object_unref(OBJECT(dst));
248 test_io_channel_setup_sync(listen_addr, connect_addr, &src, &dst);
250 g_assert(!passFD ||
251 qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
252 g_assert(!passFD ||
253 qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_FD_PASS));
255 test = qio_channel_test_new();
256 qio_channel_test_run_threads(test, false, src, dst);
257 qio_channel_test_validate(test);
259 object_unref(OBJECT(src));
260 object_unref(OBJECT(dst));
265 static void test_io_channel_ipv4(bool async)
267 SocketAddress *listen_addr = g_new0(SocketAddress, 1);
268 SocketAddress *connect_addr = g_new0(SocketAddress, 1);
270 listen_addr->type = SOCKET_ADDRESS_KIND_INET;
271 listen_addr->u.inet = g_new(InetSocketAddress, 1);
272 *listen_addr->u.inet = (InetSocketAddress) {
273 .host = g_strdup("127.0.0.1"),
274 .port = NULL, /* Auto-select */
277 connect_addr->type = SOCKET_ADDRESS_KIND_INET;
278 connect_addr->u.inet = g_new(InetSocketAddress, 1);
279 *connect_addr->u.inet = (InetSocketAddress) {
280 .host = g_strdup("127.0.0.1"),
281 .port = NULL, /* Filled in later */
284 test_io_channel(async, listen_addr, connect_addr, false);
286 qapi_free_SocketAddress(listen_addr);
287 qapi_free_SocketAddress(connect_addr);
291 static void test_io_channel_ipv4_sync(void)
293 return test_io_channel_ipv4(false);
297 static void test_io_channel_ipv4_async(void)
299 return test_io_channel_ipv4(true);
303 static void test_io_channel_ipv6(bool async)
305 SocketAddress *listen_addr = g_new0(SocketAddress, 1);
306 SocketAddress *connect_addr = g_new0(SocketAddress, 1);
308 listen_addr->type = SOCKET_ADDRESS_KIND_INET;
309 listen_addr->u.inet = g_new(InetSocketAddress, 1);
310 *listen_addr->u.inet = (InetSocketAddress) {
311 .host = g_strdup("::1"),
312 .port = NULL, /* Auto-select */
315 connect_addr->type = SOCKET_ADDRESS_KIND_INET;
316 connect_addr->u.inet = g_new(InetSocketAddress, 1);
317 *connect_addr->u.inet = (InetSocketAddress) {
318 .host = g_strdup("::1"),
319 .port = NULL, /* Filled in later */
322 test_io_channel(async, listen_addr, connect_addr, false);
324 qapi_free_SocketAddress(listen_addr);
325 qapi_free_SocketAddress(connect_addr);
329 static void test_io_channel_ipv6_sync(void)
331 return test_io_channel_ipv6(false);
335 static void test_io_channel_ipv6_async(void)
337 return test_io_channel_ipv6(true);
341 #ifndef _WIN32
342 static void test_io_channel_unix(bool async)
344 SocketAddress *listen_addr = g_new0(SocketAddress, 1);
345 SocketAddress *connect_addr = g_new0(SocketAddress, 1);
347 #define TEST_SOCKET "test-io-channel-socket.sock"
348 listen_addr->type = SOCKET_ADDRESS_KIND_UNIX;
349 listen_addr->u.q_unix = g_new0(UnixSocketAddress, 1);
350 listen_addr->u.q_unix->path = g_strdup(TEST_SOCKET);
352 connect_addr->type = SOCKET_ADDRESS_KIND_UNIX;
353 connect_addr->u.q_unix = g_new0(UnixSocketAddress, 1);
354 connect_addr->u.q_unix->path = g_strdup(TEST_SOCKET);
356 test_io_channel(async, listen_addr, connect_addr, true);
358 qapi_free_SocketAddress(listen_addr);
359 qapi_free_SocketAddress(connect_addr);
360 unlink(TEST_SOCKET);
364 static void test_io_channel_unix_sync(void)
366 return test_io_channel_unix(false);
370 static void test_io_channel_unix_async(void)
372 return test_io_channel_unix(true);
375 static void test_io_channel_unix_fd_pass(void)
377 SocketAddress *listen_addr = g_new0(SocketAddress, 1);
378 SocketAddress *connect_addr = g_new0(SocketAddress, 1);
379 QIOChannel *src, *dst;
380 int testfd;
381 int fdsend[3];
382 int *fdrecv = NULL;
383 size_t nfdrecv = 0;
384 size_t i;
385 char bufsend[12], bufrecv[12];
386 struct iovec iosend[1], iorecv[1];
388 #define TEST_SOCKET "test-io-channel-socket.sock"
389 #define TEST_FILE "test-io-channel-socket.txt"
391 testfd = open(TEST_FILE, O_RDWR|O_TRUNC|O_CREAT, 0700);
392 g_assert(testfd != -1);
393 fdsend[0] = testfd;
394 fdsend[1] = testfd;
395 fdsend[2] = testfd;
397 listen_addr->type = SOCKET_ADDRESS_KIND_UNIX;
398 listen_addr->u.q_unix = g_new0(UnixSocketAddress, 1);
399 listen_addr->u.q_unix->path = g_strdup(TEST_SOCKET);
401 connect_addr->type = SOCKET_ADDRESS_KIND_UNIX;
402 connect_addr->u.q_unix = g_new0(UnixSocketAddress, 1);
403 connect_addr->u.q_unix->path = g_strdup(TEST_SOCKET);
405 test_io_channel_setup_sync(listen_addr, connect_addr, &src, &dst);
407 memcpy(bufsend, "Hello World", G_N_ELEMENTS(bufsend));
409 iosend[0].iov_base = bufsend;
410 iosend[0].iov_len = G_N_ELEMENTS(bufsend);
412 iorecv[0].iov_base = bufrecv;
413 iorecv[0].iov_len = G_N_ELEMENTS(bufrecv);
415 g_assert(qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
416 g_assert(qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_FD_PASS));
418 qio_channel_writev_full(src,
419 iosend,
420 G_N_ELEMENTS(iosend),
421 fdsend,
422 G_N_ELEMENTS(fdsend),
423 &error_abort);
425 qio_channel_readv_full(dst,
426 iorecv,
427 G_N_ELEMENTS(iorecv),
428 &fdrecv,
429 &nfdrecv,
430 &error_abort);
432 g_assert(nfdrecv == G_N_ELEMENTS(fdsend));
433 /* Each recvd FD should be different from sent FD */
434 for (i = 0; i < nfdrecv; i++) {
435 g_assert_cmpint(fdrecv[i], !=, testfd);
437 /* Each recvd FD should be different from each other */
438 g_assert_cmpint(fdrecv[0], !=, fdrecv[1]);
439 g_assert_cmpint(fdrecv[0], !=, fdrecv[2]);
440 g_assert_cmpint(fdrecv[1], !=, fdrecv[2]);
442 /* Check the I/O buf we sent at the same time matches */
443 g_assert(memcmp(bufsend, bufrecv, G_N_ELEMENTS(bufsend)) == 0);
445 /* Write some data into the FD we received */
446 g_assert(write(fdrecv[0], bufsend, G_N_ELEMENTS(bufsend)) ==
447 G_N_ELEMENTS(bufsend));
449 /* Read data from the original FD and make sure it matches */
450 memset(bufrecv, 0, G_N_ELEMENTS(bufrecv));
451 g_assert(lseek(testfd, 0, SEEK_SET) == 0);
452 g_assert(read(testfd, bufrecv, G_N_ELEMENTS(bufrecv)) ==
453 G_N_ELEMENTS(bufrecv));
454 g_assert(memcmp(bufsend, bufrecv, G_N_ELEMENTS(bufsend)) == 0);
456 object_unref(OBJECT(src));
457 object_unref(OBJECT(dst));
458 qapi_free_SocketAddress(listen_addr);
459 qapi_free_SocketAddress(connect_addr);
460 unlink(TEST_SOCKET);
461 unlink(TEST_FILE);
462 close(testfd);
463 for (i = 0; i < nfdrecv; i++) {
464 close(fdrecv[i]);
466 g_free(fdrecv);
468 #endif /* _WIN32 */
471 static void test_io_channel_ipv4_fd(void)
473 QIOChannel *ioc;
474 int fd = -1;
475 struct sockaddr_in sa = {
476 .sin_family = AF_INET,
477 .sin_addr = {
478 .s_addr = htonl(INADDR_LOOPBACK),
480 /* Leave port unset for auto-assign */
482 socklen_t salen = sizeof(sa);
484 fd = socket(AF_INET, SOCK_STREAM, 0);
485 g_assert_cmpint(fd, >, -1);
487 g_assert_cmpint(bind(fd, (struct sockaddr *)&sa, salen), ==, 0);
489 ioc = qio_channel_new_fd(fd, &error_abort);
491 g_assert_cmpstr(object_get_typename(OBJECT(ioc)),
493 TYPE_QIO_CHANNEL_SOCKET);
495 object_unref(OBJECT(ioc));
499 int main(int argc, char **argv)
501 bool has_ipv4, has_ipv6;
503 module_call_init(MODULE_INIT_QOM);
504 socket_init();
506 g_test_init(&argc, &argv, NULL);
508 /* We're creating actual IPv4/6 sockets, so we should
509 * check if the host running tests actually supports
510 * each protocol to avoid breaking tests on machines
511 * with either IPv4 or IPv6 disabled.
513 if (check_protocol_support(&has_ipv4, &has_ipv6) < 0) {
514 return 1;
517 if (has_ipv4) {
518 g_test_add_func("/io/channel/socket/ipv4-sync",
519 test_io_channel_ipv4_sync);
520 g_test_add_func("/io/channel/socket/ipv4-async",
521 test_io_channel_ipv4_async);
522 g_test_add_func("/io/channel/socket/ipv4-fd",
523 test_io_channel_ipv4_fd);
525 if (has_ipv6) {
526 g_test_add_func("/io/channel/socket/ipv6-sync",
527 test_io_channel_ipv6_sync);
528 g_test_add_func("/io/channel/socket/ipv6-async",
529 test_io_channel_ipv6_async);
532 #ifndef _WIN32
533 g_test_add_func("/io/channel/socket/unix-sync",
534 test_io_channel_unix_sync);
535 g_test_add_func("/io/channel/socket/unix-async",
536 test_io_channel_unix_async);
537 g_test_add_func("/io/channel/socket/unix-fd-pass",
538 test_io_channel_unix_fd_pass);
539 #endif /* _WIN32 */
541 return g_test_run();