2 * QTest testcase for netdev stream and dgram
4 * Copyright (c) 2022 Red Hat, Inc.
6 * SPDX-License-Identifier: GPL-2.0-or-later
9 #include "qemu/osdep.h"
10 #include "qemu/sockets.h"
11 #include <glib/gstdio.h>
12 #include "../unit/socket-helpers.h"
14 #include "qapi/qmp/qstring.h"
15 #include "qemu/sockets.h"
16 #include "qapi/qobject-input-visitor.h"
17 #include "qapi/qapi-visit-sockets.h"
19 #define CONNECTION_TIMEOUT 60
21 #define EXPECT_STATE(q, e, t) \
24 g_test_timer_start(); \
27 resp = qtest_hmp(q, "info network"); \
29 strrchr(resp, t)[0] = 0; \
31 if (g_str_equal(resp, e)) { \
34 } while (g_test_timer_elapsed() < CONNECTION_TIMEOUT); \
35 g_assert_cmpstr(resp, ==, e); \
41 static int inet_get_free_port_socket_ipv4(int sock
)
43 struct sockaddr_in addr
;
46 memset(&addr
, 0, sizeof(addr
));
47 addr
.sin_family
= AF_INET
;
48 addr
.sin_addr
.s_addr
= INADDR_ANY
;
50 if (bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
)) < 0) {
55 if (getsockname(sock
, (struct sockaddr
*)&addr
, &len
) < 0) {
59 return ntohs(addr
.sin_port
);
62 static int inet_get_free_port_socket_ipv6(int sock
)
64 struct sockaddr_in6 addr
;
67 memset(&addr
, 0, sizeof(addr
));
68 addr
.sin6_family
= AF_INET6
;
69 addr
.sin6_addr
= in6addr_any
;
71 if (bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
)) < 0) {
76 if (getsockname(sock
, (struct sockaddr
*)&addr
, &len
) < 0) {
80 return ntohs(addr
.sin6_port
);
83 static int inet_get_free_port_multiple(int nb
, int *port
, bool ipv6
)
85 g_autofree
int *sock
= g_new(int, nb
);
88 for (i
= 0; i
< nb
; i
++) {
89 sock
[i
] = socket(ipv6
? AF_INET6
: AF_INET
, SOCK_STREAM
, 0);
93 port
[i
] = ipv6
? inet_get_free_port_socket_ipv6(sock
[i
]) :
94 inet_get_free_port_socket_ipv4(sock
[i
]);
101 for (i
= 0; i
< nb
; i
++) {
108 static int inet_get_free_port(bool ipv6
)
112 nb
= inet_get_free_port_multiple(1, &port
, ipv6
);
113 g_assert_cmpint(nb
, ==, 1);
118 static void test_stream_inet_ipv4(void)
120 QTestState
*qts0
, *qts1
;
124 port
= inet_get_free_port(false);
125 qts0
= qtest_initf("-nodefaults -M none "
126 "-netdev stream,id=st0,server=true,addr.type=inet,"
127 "addr.ipv4=on,addr.ipv6=off,"
128 "addr.host=127.0.0.1,addr.port=%d", port
);
130 EXPECT_STATE(qts0
, "st0: index=0,type=stream,listening\r\n", 0);
132 qts1
= qtest_initf("-nodefaults -M none "
133 "-netdev stream,server=false,id=st0,addr.type=inet,"
134 "addr.ipv4=on,addr.ipv6=off,"
135 "addr.host=127.0.0.1,addr.port=%d", port
);
137 expect
= g_strdup_printf("st0: index=0,type=stream,tcp:127.0.0.1:%d\r\n",
139 EXPECT_STATE(qts1
, expect
, 0);
142 /* the port is unknown, check only the address */
143 EXPECT_STATE(qts0
, "st0: index=0,type=stream,tcp:127.0.0.1", ':');
149 static void wait_stream_connected(QTestState
*qts
, const char *id
,
150 SocketAddress
**addr
)
157 resp
= qtest_qmp_eventwait_ref(qts
, "NETDEV_STREAM_CONNECTED");
158 g_assert_nonnull(resp
);
159 data
= qdict_get_qdict(resp
, "data");
160 g_assert_nonnull(data
);
162 qstr
= qobject_to(QString
, qdict_get(data
, "netdev-id"));
163 g_assert_nonnull(data
);
165 g_assert(!strcmp(qstring_get_str(qstr
), id
));
167 obj
= qdict_get(data
, "addr");
169 v
= qobject_input_visitor_new(obj
);
170 visit_type_SocketAddress(v
, NULL
, addr
, NULL
);
175 static void wait_stream_disconnected(QTestState
*qts
, const char *id
)
180 resp
= qtest_qmp_eventwait_ref(qts
, "NETDEV_STREAM_DISCONNECTED");
181 g_assert_nonnull(resp
);
182 data
= qdict_get_qdict(resp
, "data");
183 g_assert_nonnull(data
);
185 qstr
= qobject_to(QString
, qdict_get(data
, "netdev-id"));
186 g_assert_nonnull(data
);
188 g_assert(!strcmp(qstring_get_str(qstr
), id
));
192 static void test_stream_unix_reconnect(void)
194 QTestState
*qts0
, *qts1
;
198 path
= g_strconcat(tmpdir
, "/stream_unix_reconnect", NULL
);
199 qts0
= qtest_initf("-nodefaults -M none "
200 "-netdev stream,id=st0,server=true,addr.type=unix,"
201 "addr.path=%s", path
);
203 EXPECT_STATE(qts0
, "st0: index=0,type=stream,listening\r\n", 0);
205 qts1
= qtest_initf("-nodefaults -M none "
206 "-netdev stream,server=false,id=st0,addr.type=unix,"
207 "addr.path=%s,reconnect=1", path
);
209 wait_stream_connected(qts0
, "st0", &addr
);
210 g_assert_cmpint(addr
->type
, ==, SOCKET_ADDRESS_TYPE_UNIX
);
211 g_assert_cmpstr(addr
->u
.q_unix
.path
, ==, path
);
212 qapi_free_SocketAddress(addr
);
217 /* check client has been disconnected */
218 wait_stream_disconnected(qts1
, "st0");
221 qts0
= qtest_initf("-nodefaults -M none "
222 "-netdev stream,id=st0,server=true,addr.type=unix,"
223 "addr.path=%s", path
);
225 /* wait connection events*/
226 wait_stream_connected(qts0
, "st0", &addr
);
227 g_assert_cmpint(addr
->type
, ==, SOCKET_ADDRESS_TYPE_UNIX
);
228 g_assert_cmpstr(addr
->u
.q_unix
.path
, ==, path
);
229 qapi_free_SocketAddress(addr
);
231 wait_stream_connected(qts1
, "st0", &addr
);
232 g_assert_cmpint(addr
->type
, ==, SOCKET_ADDRESS_TYPE_UNIX
);
233 g_assert_cmpstr(addr
->u
.q_unix
.path
, ==, path
);
234 qapi_free_SocketAddress(addr
);
241 static void test_stream_inet_ipv6(void)
243 QTestState
*qts0
, *qts1
;
247 port
= inet_get_free_port(true);
248 qts0
= qtest_initf("-nodefaults -M none "
249 "-netdev stream,id=st0,server=true,addr.type=inet,"
250 "addr.ipv4=off,addr.ipv6=on,"
251 "addr.host=::1,addr.port=%d", port
);
253 EXPECT_STATE(qts0
, "st0: index=0,type=stream,listening\r\n", 0);
255 qts1
= qtest_initf("-nodefaults -M none "
256 "-netdev stream,server=false,id=st0,addr.type=inet,"
257 "addr.ipv4=off,addr.ipv6=on,"
258 "addr.host=::1,addr.port=%d", port
);
260 expect
= g_strdup_printf("st0: index=0,type=stream,tcp:::1:%d\r\n",
262 EXPECT_STATE(qts1
, expect
, 0);
265 /* the port is unknown, check only the address */
266 EXPECT_STATE(qts0
, "st0: index=0,type=stream,tcp:::1", ':');
272 static void test_stream_unix(void)
274 QTestState
*qts0
, *qts1
;
278 path
= g_strconcat(tmpdir
, "/stream_unix", NULL
);
280 qts0
= qtest_initf("-nodefaults -M none "
281 "-netdev stream,id=st0,server=true,"
282 "addr.type=unix,addr.path=%s,",
285 EXPECT_STATE(qts0
, "st0: index=0,type=stream,listening\r\n", 0);
287 qts1
= qtest_initf("-nodefaults -M none "
288 "-netdev stream,id=st0,server=false,"
289 "addr.type=unix,addr.path=%s",
292 expect
= g_strdup_printf("st0: index=0,type=stream,unix:%s\r\n", path
);
293 EXPECT_STATE(qts1
, expect
, 0);
294 EXPECT_STATE(qts0
, expect
, 0);
303 static void test_stream_unix_abstract(void)
305 QTestState
*qts0
, *qts1
;
309 path
= g_strconcat(tmpdir
, "/stream_unix_abstract", NULL
);
311 qts0
= qtest_initf("-nodefaults -M none "
312 "-netdev stream,id=st0,server=true,"
313 "addr.type=unix,addr.path=%s,"
317 EXPECT_STATE(qts0
, "st0: index=0,type=stream,listening\r\n", 0);
319 qts1
= qtest_initf("-nodefaults -M none "
320 "-netdev stream,id=st0,server=false,"
321 "addr.type=unix,addr.path=%s,addr.abstract=on",
324 expect
= g_strdup_printf("st0: index=0,type=stream,unix:%s\r\n", path
);
325 EXPECT_STATE(qts1
, expect
, 0);
326 EXPECT_STATE(qts0
, expect
, 0);
336 static void test_stream_fd(void)
338 QTestState
*qts0
, *qts1
;
342 ret
= socketpair(AF_LOCAL
, SOCK_STREAM
, 0, sock
);
343 g_assert_true(ret
== 0);
345 qts0
= qtest_initf("-nodefaults -M none "
346 "-netdev stream,id=st0,addr.type=fd,addr.str=%d",
349 EXPECT_STATE(qts0
, "st0: index=0,type=stream,unix:\r\n", 0);
351 qts1
= qtest_initf("-nodefaults -M none "
352 "-netdev stream,id=st0,addr.type=fd,addr.str=%d",
355 EXPECT_STATE(qts1
, "st0: index=0,type=stream,unix:\r\n", 0);
356 EXPECT_STATE(qts0
, "st0: index=0,type=stream,unix:\r\n", 0);
366 static void test_dgram_inet(void)
368 QTestState
*qts0
, *qts1
;
373 nb
= inet_get_free_port_multiple(2, port
, false);
374 g_assert_cmpint(nb
, ==, 2);
376 qts0
= qtest_initf("-nodefaults -M none "
377 "-netdev dgram,id=st0,"
378 "local.type=inet,local.host=127.0.0.1,local.port=%d,"
379 "remote.type=inet,remote.host=127.0.0.1,remote.port=%d",
382 expect
= g_strdup_printf("st0: index=0,type=dgram,"
383 "udp=127.0.0.1:%d/127.0.0.1:%d\r\n",
385 EXPECT_STATE(qts0
, expect
, 0);
388 qts1
= qtest_initf("-nodefaults -M none "
389 "-netdev dgram,id=st0,"
390 "local.type=inet,local.host=127.0.0.1,local.port=%d,"
391 "remote.type=inet,remote.host=127.0.0.1,remote.port=%d",
394 expect
= g_strdup_printf("st0: index=0,type=dgram,"
395 "udp=127.0.0.1:%d/127.0.0.1:%d\r\n",
397 EXPECT_STATE(qts1
, expect
, 0);
404 #if !defined(_WIN32) && !defined(CONFIG_DARWIN)
405 static void test_dgram_mcast(void)
409 qts
= qtest_initf("-nodefaults -M none "
410 "-netdev dgram,id=st0,"
411 "remote.type=inet,remote.host=230.0.0.1,remote.port=1234");
413 EXPECT_STATE(qts
, "st0: index=0,type=dgram,mcast=230.0.0.1:1234\r\n", 0);
420 static void test_dgram_unix(void)
422 QTestState
*qts0
, *qts1
;
424 gchar
*path0
, *path1
;
426 path0
= g_strconcat(tmpdir
, "/dgram_unix0", NULL
);
427 path1
= g_strconcat(tmpdir
, "/dgram_unix1", NULL
);
429 qts0
= qtest_initf("-nodefaults -M none "
430 "-netdev dgram,id=st0,local.type=unix,local.path=%s,"
431 "remote.type=unix,remote.path=%s",
434 expect
= g_strdup_printf("st0: index=0,type=dgram,udp=%s:%s\r\n",
436 EXPECT_STATE(qts0
, expect
, 0);
439 qts1
= qtest_initf("-nodefaults -M none "
440 "-netdev dgram,id=st0,local.type=unix,local.path=%s,"
441 "remote.type=unix,remote.path=%s",
445 expect
= g_strdup_printf("st0: index=0,type=dgram,udp=%s:%s\r\n",
447 EXPECT_STATE(qts1
, expect
, 0);
459 static void test_dgram_fd(void)
461 QTestState
*qts0
, *qts1
;
466 ret
= socketpair(PF_UNIX
, SOCK_DGRAM
, 0, sv
);
467 g_assert_cmpint(ret
, !=, -1);
469 qts0
= qtest_initf("-nodefaults -M none "
470 "-netdev dgram,id=st0,local.type=fd,local.str=%d",
473 expect
= g_strdup_printf("st0: index=0,type=dgram,fd=%d unix\r\n", sv
[0]);
474 EXPECT_STATE(qts0
, expect
, 0);
477 qts1
= qtest_initf("-nodefaults -M none "
478 "-netdev dgram,id=st0,local.type=fd,local.str=%d",
482 expect
= g_strdup_printf("st0: index=0,type=dgram,fd=%d unix\r\n", sv
[1]);
483 EXPECT_STATE(qts1
, expect
, 0);
494 int main(int argc
, char **argv
)
497 bool has_ipv4
, has_ipv6
, has_afunix
;
498 g_autoptr(GError
) err
= NULL
;
501 g_test_init(&argc
, &argv
, NULL
);
503 if (socket_check_protocol_support(&has_ipv4
, &has_ipv6
) < 0) {
504 g_error("socket_check_protocol_support() failed\n");
507 tmpdir
= g_dir_make_tmp("netdev-socket.XXXXXX", &err
);
508 if (tmpdir
== NULL
) {
509 g_error("Can't create temporary directory in %s: %s",
510 g_get_tmp_dir(), err
->message
);
514 qtest_add_func("/netdev/stream/inet/ipv4", test_stream_inet_ipv4
);
515 qtest_add_func("/netdev/dgram/inet", test_dgram_inet
);
516 #if !defined(_WIN32) && !defined(CONFIG_DARWIN)
517 qtest_add_func("/netdev/dgram/mcast", test_dgram_mcast
);
521 qtest_add_func("/netdev/stream/inet/ipv6", test_stream_inet_ipv6
);
524 socket_check_afunix_support(&has_afunix
);
527 qtest_add_func("/netdev/dgram/unix", test_dgram_unix
);
529 qtest_add_func("/netdev/stream/unix/oneshot", test_stream_unix
);
530 qtest_add_func("/netdev/stream/unix/reconnect",
531 test_stream_unix_reconnect
);
533 qtest_add_func("/netdev/stream/unix/abstract",
534 test_stream_unix_abstract
);
537 qtest_add_func("/netdev/stream/fd", test_stream_fd
);
538 qtest_add_func("/netdev/dgram/fd", test_dgram_fd
);