On Mon, Nov 7, 2022 at 2:59 PM Jason Wang <jasow...@redhat.com> wrote: > > On Fri, Nov 4, 2022 at 11:01 PM Laurent Vivier <lviv...@redhat.com> wrote: > > > > Signed-off-by: Laurent Vivier <lviv...@redhat.com> > > Acked-by: Michael S. Tsirkin <m...@redhat.com> > > --- > > > > Notes: > > v2: > > - Fix ipv6 free port allocation > > - Check for IPv4, IPv6, AF_UNIX > > - Use g_mkdtemp() rather than g_file_open_tmp() > > - Use socketpair() in test_stream_fd() > > > > v1: compared to v14 of "qapi: net: add unix socket type support to > > netdev backend": > > - use IP addresses 127.0.0.1 and ::1 rather than localhost > > > > tests/qtest/meson.build | 2 + > > tests/qtest/netdev-socket.c | 435 ++++++++++++++++++++++++++++++++++++ > > 2 files changed, 437 insertions(+) > > create mode 100644 tests/qtest/netdev-socket.c > > > > diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build > > index c07a5b1a5f43..43d075b76280 100644 > > --- a/tests/qtest/meson.build > > +++ b/tests/qtest/meson.build > > @@ -27,6 +27,7 @@ qtests_generic = [ > > 'test-hmp', > > 'qos-test', > > 'readconfig-test', > > + 'netdev-socket', > > ] > > if config_host.has_key('CONFIG_MODULES') > > qtests_generic += [ 'modules-test' ] > > @@ -304,6 +305,7 @@ qtests = { > > 'tpm-tis-device-swtpm-test': [io, tpmemu_files, 'tpm-tis-util.c'], > > 'tpm-tis-device-test': [io, tpmemu_files, 'tpm-tis-util.c'], > > 'vmgenid-test': files('boot-sector.c', 'acpi-utils.c'), > > + 'netdev-socket': files('netdev-socket.c', '../unit/socket-helpers.c'), > > } > > > > gvnc = dependency('gvnc-1.0', required: false) > > diff --git a/tests/qtest/netdev-socket.c b/tests/qtest/netdev-socket.c > > new file mode 100644 > > index 000000000000..dd46214f69c7 > > --- /dev/null > > +++ b/tests/qtest/netdev-socket.c > > @@ -0,0 +1,435 @@ > > +/* > > + * QTest testcase for netdev stream and dgram > > + * > > + * Copyright (c) 2022 Red Hat, Inc. > > + * > > + * SPDX-License-Identifier: GPL-2.0-or-later > > + */ > > + > > +#include "qemu/osdep.h" > > +#include <glib/gstdio.h> > > +#include "../unit/socket-helpers.h" > > +#include "libqtest.h" > > + > > +#define CONNECTION_TIMEOUT 5 > > + > > +#define EXPECT_STATE(q, e, t) \ > > +do { \ > > + char *resp = qtest_hmp(q, "info network"); \ > > + if (t) { \ > > + strrchr(resp, t)[0] = 0; \ > > + } \ > > + g_test_timer_start(); \ > > + while (g_test_timer_elapsed() < CONNECTION_TIMEOUT) { \ > > + if (strcmp(resp, e) == 0) { \ > > + break; \ > > + } \ > > + g_free(resp); \ > > + resp = qtest_hmp(q, "info network"); \ > > + if (t) { \ > > + strrchr(resp, t)[0] = 0; \ > > + } \ > > + } \ > > + g_assert_cmpstr(resp, ==, e); \ > > + g_free(resp); \ > > +} while (0) > > + > > +static char *tmpdir; > > + > > +static int inet_get_free_port_socket_ipv4(int sock) > > +{ > > + struct sockaddr_in addr; > > + socklen_t len; > > + > > + memset(&addr, 0, sizeof(addr)); > > + addr.sin_family = AF_INET; > > + addr.sin_addr.s_addr = INADDR_ANY; > > + addr.sin_port = 0; > > + if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { > > + return -1; > > + } > > + > > + len = sizeof(addr); > > + if (getsockname(sock, (struct sockaddr *)&addr, &len) < 0) { > > + return -1; > > + } > > + > > + return ntohs(addr.sin_port); > > +} > > + > > +static int inet_get_free_port_socket_ipv6(int sock) > > +{ > > + struct sockaddr_in6 addr; > > + socklen_t len; > > + > > + memset(&addr, 0, sizeof(addr)); > > + addr.sin6_family = AF_INET6; > > + addr.sin6_addr = in6addr_any; > > + addr.sin6_port = 0; > > + if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { > > + return -1; > > + } > > + > > + len = sizeof(addr); > > + if (getsockname(sock, (struct sockaddr *)&addr, &len) < 0) { > > + return -1; > > + } > > + > > + return ntohs(addr.sin6_port); > > +} > > + > > +static int inet_get_free_port_multiple(int nb, int *port, bool ipv6) > > +{ > > + int sock[nb]; > > + int i; > > + > > + for (i = 0; i < nb; i++) { > > + sock[i] = socket(ipv6 ? AF_INET6 : AF_INET, SOCK_STREAM, 0); > > + if (sock[i] < 0) { > > + break; > > + } > > + port[i] = ipv6 ? inet_get_free_port_socket_ipv6(sock[i]) : > > + inet_get_free_port_socket_ipv4(sock[i]); > > + if (port[i] == -1) { > > + break; > > + } > > + } > > + > > + nb = i; > > + for (i = 0; i < nb; i++) { > > + closesocket(sock[i]); > > + } > > + > > + return nb; > > +} > > + > > +static int inet_get_free_port(bool ipv6) > > +{ > > + int nb, port; > > + > > + nb = inet_get_free_port_multiple(1, &port, ipv6); > > + g_assert_cmpint(nb, ==, 1); > > + > > + return port; > > +} > > + > > +static void test_stream_inet_ipv4(void) > > +{ > > + QTestState *qts0, *qts1; > > + char *expect; > > + int port; > > + > > + port = inet_get_free_port(false); > > + qts0 = qtest_initf("-nodefaults " > > + "-netdev stream,id=st0,server=true,addr.type=inet," > > + "addr.ipv4=on,addr.ipv6=off," > > + "addr.host=127.0.0.1,addr.port=%d", port); > > + > > + EXPECT_STATE(qts0, "st0: index=0,type=stream,\r\n", 0); > > + > > + qts1 = qtest_initf("-nodefaults " > > + "-netdev stream,server=false,id=st0,addr.type=inet," > > + "addr.ipv4=on,addr.ipv6=off," > > + "addr.host=127.0.0.1,addr.port=%d", port); > > + > > + expect = g_strdup_printf("st0: > > index=0,type=stream,tcp:127.0.0.1:%d\r\n", > > + port); > > + EXPECT_STATE(qts1, expect, 0); > > + g_free(expect); > > + > > + /* the port is unknown, check only the address */ > > + EXPECT_STATE(qts0, "st0: index=0,type=stream,tcp:127.0.0.1", ':'); > > + > > + qtest_quit(qts1); > > + qtest_quit(qts0); > > +} > > + > > +static void test_stream_inet_ipv6(void) > > +{ > > + QTestState *qts0, *qts1; > > + char *expect; > > + int port; > > + > > + port = inet_get_free_port(true); > > + qts0 = qtest_initf("-nodefaults " > > + "-netdev stream,id=st0,server=true,addr.type=inet," > > + "addr.ipv4=off,addr.ipv6=on," > > + "addr.host=::1,addr.port=%d", port); > > + > > + EXPECT_STATE(qts0, "st0: index=0,type=stream,\r\n", 0); > > + > > + qts1 = qtest_initf("-nodefaults " > > + "-netdev stream,server=false,id=st0,addr.type=inet," > > + "addr.ipv4=off,addr.ipv6=on," > > + "addr.host=::1,addr.port=%d", port); > > + > > + expect = g_strdup_printf("st0: index=0,type=stream,tcp:::1:%d\r\n", > > + port); > > + EXPECT_STATE(qts1, expect, 0); > > + g_free(expect); > > + > > + /* the port is unknown, check only the address */ > > + EXPECT_STATE(qts0, "st0: index=0,type=stream,tcp:::1", ':'); > > + > > + qtest_quit(qts1); > > + qtest_quit(qts0); > > +} > > + > > +static void test_stream_unix(void) > > +{ > > + QTestState *qts0, *qts1; > > + char *expect; > > + gchar *path; > > + > > + path = g_strconcat(tmpdir, "/stream_unix", NULL); > > Nit: I wonder if socketpair() is better since we don't even need to > care about path. > > Thanks
Speak too fast, I misread the code. I've queued this. Thanks