xref: /qemu/tests/unit/test-io-channel-socket.c (revision da34e65cb4025728566d6504a99916f6e7e1dd6a)
1 /*
2  * QEMU I/O channel sockets test
3  *
4  * Copyright (c) 2015-2016 Red Hat, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  */
20 
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"
26 
27 #ifndef AI_ADDRCONFIG
28 # define AI_ADDRCONFIG 0
29 #endif
30 #ifndef AI_V4MAPPED
31 # define AI_V4MAPPED 0
32 #endif
33 #ifndef EAI_ADDRFAMILY
34 # define EAI_ADDRFAMILY 0
35 #endif
36 
37 static int check_bind(const char *hostname, bool *has_proto)
38 {
39     int fd = -1;
40     struct addrinfo ai, *res = NULL;
41     int rc;
42     int ret = -1;
43 
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;
48 
49     /* lookup */
50     rc = getaddrinfo(hostname, NULL, &ai, &res);
51     if (rc != 0) {
52         if (rc == EAI_ADDRFAMILY ||
53             rc == EAI_FAMILY) {
54             *has_proto = false;
55             goto done;
56         }
57         goto cleanup;
58     }
59 
60     fd = qemu_socket(res->ai_family, res->ai_socktype, res->ai_protocol);
61     if (fd < 0) {
62         goto cleanup;
63     }
64 
65     if (bind(fd, res->ai_addr, res->ai_addrlen) < 0) {
66         if (errno == EADDRNOTAVAIL) {
67             *has_proto = false;
68             goto done;
69         }
70         goto cleanup;
71     }
72 
73     *has_proto = true;
74  done:
75     ret = 0;
76 
77  cleanup:
78     if (fd != -1) {
79         close(fd);
80     }
81     if (res) {
82         freeaddrinfo(res);
83     }
84     return ret;
85 }
86 
87 static int check_protocol_support(bool *has_ipv4, bool *has_ipv6)
88 {
89     if (check_bind("127.0.0.1", has_ipv4) < 0) {
90         return -1;
91     }
92     if (check_bind("::1", has_ipv6) < 0) {
93         return -1;
94     }
95 
96     return 0;
97 }
98 
99 
100 static void test_io_channel_set_socket_bufs(QIOChannel *src,
101                                             QIOChannel *dst)
102 {
103     int buflen = 64 * 1024;
104 
105     /*
106      * Make the socket buffers small so that we see
107      * the effects of partial reads/writes
108      */
109     setsockopt(((QIOChannelSocket *)src)->fd,
110                SOL_SOCKET, SO_SNDBUF,
111                (char *)&buflen,
112                sizeof(buflen));
113 
114     setsockopt(((QIOChannelSocket *)dst)->fd,
115                SOL_SOCKET, SO_SNDBUF,
116                (char *)&buflen,
117                sizeof(buflen));
118 }
119 
120 
121 static void test_io_channel_setup_sync(SocketAddress *listen_addr,
122                                        SocketAddress *connect_addr,
123                                        QIOChannel **src,
124                                        QIOChannel **dst)
125 {
126     QIOChannelSocket *lioc;
127 
128     lioc = qio_channel_socket_new();
129     qio_channel_socket_listen_sync(lioc, listen_addr, &error_abort);
130 
131     if (listen_addr->type == SOCKET_ADDRESS_KIND_INET) {
132         SocketAddress *laddr = qio_channel_socket_get_local_address(
133             lioc, &error_abort);
134 
135         g_free(connect_addr->u.inet.data->port);
136         connect_addr->u.inet.data->port = g_strdup(laddr->u.inet.data->port);
137 
138         qapi_free_SocketAddress(laddr);
139     }
140 
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);
145 
146     qio_channel_wait(QIO_CHANNEL(lioc), G_IO_IN);
147     *dst = QIO_CHANNEL(qio_channel_socket_accept(lioc, &error_abort));
148     g_assert(*dst);
149 
150     test_io_channel_set_socket_bufs(*src, *dst);
151 
152     object_unref(OBJECT(lioc));
153 }
154 
155 
156 struct TestIOChannelData {
157     bool err;
158     GMainLoop *loop;
159 };
160 
161 
162 static void test_io_channel_complete(Object *src,
163                                      Error *err,
164                                      gpointer opaque)
165 {
166     struct TestIOChannelData *data = opaque;
167     data->err = err != NULL;
168     g_main_loop_quit(data->loop);
169 }
170 
171 
172 static void test_io_channel_setup_async(SocketAddress *listen_addr,
173                                         SocketAddress *connect_addr,
174                                         QIOChannel **src,
175                                         QIOChannel **dst)
176 {
177     QIOChannelSocket *lioc;
178     struct TestIOChannelData data;
179 
180     data.loop = g_main_loop_new(g_main_context_default(),
181                                 TRUE);
182 
183     lioc = qio_channel_socket_new();
184     qio_channel_socket_listen_async(
185         lioc, listen_addr,
186         test_io_channel_complete, &data, NULL);
187 
188     g_main_loop_run(data.loop);
189     g_main_context_iteration(g_main_context_default(), FALSE);
190 
191     g_assert(!data.err);
192 
193     if (listen_addr->type == SOCKET_ADDRESS_KIND_INET) {
194         SocketAddress *laddr = qio_channel_socket_get_local_address(
195             lioc, &error_abort);
196 
197         g_free(connect_addr->u.inet.data->port);
198         connect_addr->u.inet.data->port = g_strdup(laddr->u.inet.data->port);
199 
200         qapi_free_SocketAddress(laddr);
201     }
202 
203     *src = QIO_CHANNEL(qio_channel_socket_new());
204 
205     qio_channel_socket_connect_async(
206         QIO_CHANNEL_SOCKET(*src), connect_addr,
207         test_io_channel_complete, &data, NULL);
208 
209     g_main_loop_run(data.loop);
210     g_main_context_iteration(g_main_context_default(), FALSE);
211 
212     g_assert(!data.err);
213 
214     qio_channel_wait(QIO_CHANNEL(lioc), G_IO_IN);
215     *dst = QIO_CHANNEL(qio_channel_socket_accept(lioc, &error_abort));
216     g_assert(*dst);
217 
218     qio_channel_set_delay(*src, false);
219     test_io_channel_set_socket_bufs(*src, *dst);
220 
221     object_unref(OBJECT(lioc));
222 
223     g_main_loop_unref(data.loop);
224 }
225 
226 
227 static void test_io_channel(bool async,
228                             SocketAddress *listen_addr,
229                             SocketAddress *connect_addr,
230                             bool passFD)
231 {
232     QIOChannel *src, *dst;
233     QIOChannelTest *test;
234     if (async) {
235         test_io_channel_setup_async(listen_addr, connect_addr, &src, &dst);
236 
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));
241 
242         test = qio_channel_test_new();
243         qio_channel_test_run_threads(test, true, src, dst);
244         qio_channel_test_validate(test);
245 
246         object_unref(OBJECT(src));
247         object_unref(OBJECT(dst));
248 
249         test_io_channel_setup_async(listen_addr, connect_addr, &src, &dst);
250 
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));
255 
256         test = qio_channel_test_new();
257         qio_channel_test_run_threads(test, false, src, dst);
258         qio_channel_test_validate(test);
259 
260         object_unref(OBJECT(src));
261         object_unref(OBJECT(dst));
262     } else {
263         test_io_channel_setup_sync(listen_addr, connect_addr, &src, &dst);
264 
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));
269 
270         test = qio_channel_test_new();
271         qio_channel_test_run_threads(test, true, src, dst);
272         qio_channel_test_validate(test);
273 
274         object_unref(OBJECT(src));
275         object_unref(OBJECT(dst));
276 
277         test_io_channel_setup_sync(listen_addr, connect_addr, &src, &dst);
278 
279         g_assert(!passFD ||
280                  qio_channel_has_feature(src, QIO_CHANNEL_FEATURE_FD_PASS));
281         g_assert(!passFD ||
282                  qio_channel_has_feature(dst, QIO_CHANNEL_FEATURE_FD_PASS));
283 
284         test = qio_channel_test_new();
285         qio_channel_test_run_threads(test, false, src, dst);
286         qio_channel_test_validate(test);
287 
288         object_unref(OBJECT(src));
289         object_unref(OBJECT(dst));
290     }
291 }
292 
293 
294 static void test_io_channel_ipv4(bool async)
295 {
296     SocketAddress *listen_addr = g_new0(SocketAddress, 1);
297     SocketAddress *connect_addr = g_new0(SocketAddress, 1);
298 
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 */
304     };
305 
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 */
311     };
312 
313     test_io_channel(async, listen_addr, connect_addr, false);
314 
315     qapi_free_SocketAddress(listen_addr);
316     qapi_free_SocketAddress(connect_addr);
317 }
318 
319 
320 static void test_io_channel_ipv4_sync(void)
321 {
322     return test_io_channel_ipv4(false);
323 }
324 
325 
326 static void test_io_channel_ipv4_async(void)
327 {
328     return test_io_channel_ipv4(true);
329 }
330 
331 
332 static void test_io_channel_ipv6(bool async)
333 {
334     SocketAddress *listen_addr = g_new0(SocketAddress, 1);
335     SocketAddress *connect_addr = g_new0(SocketAddress, 1);
336 
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 */
342     };
343 
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 */
349     };
350 
351     test_io_channel(async, listen_addr, connect_addr, false);
352 
353     qapi_free_SocketAddress(listen_addr);
354     qapi_free_SocketAddress(connect_addr);
355 }
356 
357 
358 static void test_io_channel_ipv6_sync(void)
359 {
360     return test_io_channel_ipv6(false);
361 }
362 
363 
364 static void test_io_channel_ipv6_async(void)
365 {
366     return test_io_channel_ipv6(true);
367 }
368 
369 
370 #ifndef _WIN32
371 static void test_io_channel_unix(bool async)
372 {
373     SocketAddress *listen_addr = g_new0(SocketAddress, 1);
374     SocketAddress *connect_addr = g_new0(SocketAddress, 1);
375 
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);
380 
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);
384 
385     test_io_channel(async, listen_addr, connect_addr, true);
386 
387     qapi_free_SocketAddress(listen_addr);
388     qapi_free_SocketAddress(connect_addr);
389     unlink(TEST_SOCKET);
390 }
391 
392 
393 static void test_io_channel_unix_sync(void)
394 {
395     return test_io_channel_unix(false);
396 }
397 
398 
399 static void test_io_channel_unix_async(void)
400 {
401     return test_io_channel_unix(true);
402 }
403 
404 static void test_io_channel_unix_fd_pass(void)
405 {
406     SocketAddress *listen_addr = g_new0(SocketAddress, 1);
407     SocketAddress *connect_addr = g_new0(SocketAddress, 1);
408     QIOChannel *src, *dst;
409     int testfd;
410     int fdsend[3];
411     int *fdrecv = NULL;
412     size_t nfdrecv = 0;
413     size_t i;
414     char bufsend[12], bufrecv[12];
415     struct iovec iosend[1], iorecv[1];
416 
417 #define TEST_SOCKET "test-io-channel-socket.sock"
418 #define TEST_FILE "test-io-channel-socket.txt"
419 
420     testfd = open(TEST_FILE, O_RDWR|O_TRUNC|O_CREAT, 0700);
421     g_assert(testfd != -1);
422     fdsend[0] = testfd;
423     fdsend[1] = testfd;
424     fdsend[2] = testfd;
425 
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);
429 
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);
433 
434     test_io_channel_setup_sync(listen_addr, connect_addr, &src, &dst);
435 
436     memcpy(bufsend, "Hello World", G_N_ELEMENTS(bufsend));
437 
438     iosend[0].iov_base = bufsend;
439     iosend[0].iov_len = G_N_ELEMENTS(bufsend);
440 
441     iorecv[0].iov_base = bufrecv;
442     iorecv[0].iov_len = G_N_ELEMENTS(bufrecv);
443 
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));
446 
447     qio_channel_writev_full(src,
448                             iosend,
449                             G_N_ELEMENTS(iosend),
450                             fdsend,
451                             G_N_ELEMENTS(fdsend),
452                             &error_abort);
453 
454     qio_channel_readv_full(dst,
455                            iorecv,
456                            G_N_ELEMENTS(iorecv),
457                            &fdrecv,
458                            &nfdrecv,
459                            &error_abort);
460 
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);
465     }
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]);
470 
471     /* Check the I/O buf we sent at the same time matches */
472     g_assert(memcmp(bufsend, bufrecv, G_N_ELEMENTS(bufsend)) == 0);
473 
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));
477 
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);
484 
485     object_unref(OBJECT(src));
486     object_unref(OBJECT(dst));
487     qapi_free_SocketAddress(listen_addr);
488     qapi_free_SocketAddress(connect_addr);
489     unlink(TEST_SOCKET);
490     unlink(TEST_FILE);
491     close(testfd);
492     for (i = 0; i < nfdrecv; i++) {
493         close(fdrecv[i]);
494     }
495     g_free(fdrecv);
496 }
497 #endif /* _WIN32 */
498 
499 
500 static void test_io_channel_ipv4_fd(void)
501 {
502     QIOChannel *ioc;
503     int fd = -1;
504     struct sockaddr_in sa = {
505         .sin_family = AF_INET,
506         .sin_addr = {
507             .s_addr =  htonl(INADDR_LOOPBACK),
508         }
509         /* Leave port unset for auto-assign */
510     };
511     socklen_t salen = sizeof(sa);
512 
513     fd = socket(AF_INET, SOCK_STREAM, 0);
514     g_assert_cmpint(fd, >, -1);
515 
516     g_assert_cmpint(bind(fd, (struct sockaddr *)&sa, salen), ==, 0);
517 
518     ioc = qio_channel_new_fd(fd, &error_abort);
519 
520     g_assert_cmpstr(object_get_typename(OBJECT(ioc)),
521                     ==,
522                     TYPE_QIO_CHANNEL_SOCKET);
523 
524     object_unref(OBJECT(ioc));
525 }
526 
527 
528 int main(int argc, char **argv)
529 {
530     bool has_ipv4, has_ipv6;
531 
532     module_call_init(MODULE_INIT_QOM);
533     socket_init();
534 
535     g_test_init(&argc, &argv, NULL);
536 
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.
541      */
542     if (check_protocol_support(&has_ipv4, &has_ipv6) < 0) {
543         return 1;
544     }
545 
546     if (has_ipv4) {
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);
553     }
554     if (has_ipv6) {
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);
559     }
560 
561 #ifndef _WIN32
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);
568 #endif /* _WIN32 */
569 
570     return g_test_run();
571 }
572