ed5979
From 39d5761fe1f546e764dedf2ea32c55d8f5222696 Mon Sep 17 00:00:00 2001
ed5979
From: Laurent Vivier <lvivier@redhat.com>
ed5979
Date: Wed, 18 Jan 2023 13:04:05 +0100
ed5979
Subject: [PATCH 1/8] tests/qtest: netdev: test stream and dgram backends
ed5979
MIME-Version: 1.0
ed5979
Content-Type: text/plain; charset=UTF-8
ed5979
Content-Transfer-Encoding: 8bit
ed5979
ed5979
RH-Author: Laurent Vivier <lvivier@redhat.com>
ed5979
RH-MergeRequest: 148: net: stream: add a new option to automatically reconnect
ed5979
RH-Bugzilla: 2169232
ed5979
RH-Acked-by: Eugenio PĂ©rez <eperezma@redhat.com>
ed5979
RH-Acked-by: Cindy Lu <lulu@redhat.com>
ed5979
RH-Acked-by: MST <mst@redhat.com>
ed5979
RH-Acked-by: David Gibson (Red Hat) <dgibson@redhat.com>
ed5979
RH-Commit: [1/2] 75c71b47eea072e14651a96612d402b50d2b8f1e (lvivier/qemu-kvm-centos)
ed5979
ed5979
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
ed5979
Acked-by: Michael S. Tsirkin <mst@redhat.com>
ed5979
Message-Id: <20230118120405.1876329-1-lvivier@redhat.com>
ed5979
Signed-off-by: Thomas Huth <thuth@redhat.com>
ed5979
(cherry picked from commit c95031a19f0d7f418a597243f6f84b031a858997)
ed5979
---
ed5979
 tests/qtest/meson.build     |   2 +
ed5979
 tests/qtest/netdev-socket.c | 448 ++++++++++++++++++++++++++++++++++++
ed5979
 2 files changed, 450 insertions(+)
ed5979
 create mode 100644 tests/qtest/netdev-socket.c
ed5979
ed5979
diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build
ed5979
index 9df3f9f8b9..2e7c6fe5e3 100644
ed5979
--- a/tests/qtest/meson.build
ed5979
+++ b/tests/qtest/meson.build
ed5979
@@ -27,6 +27,7 @@ qtests_generic = [
ed5979
   'test-hmp',
ed5979
   'qos-test',
ed5979
   'readconfig-test',
ed5979
+  'netdev-socket',
ed5979
 ]
ed5979
 if config_host.has_key('CONFIG_MODULES')
ed5979
   qtests_generic += [ 'modules-test' ]
ed5979
@@ -299,6 +300,7 @@ qtests = {
ed5979
   'tpm-tis-device-swtpm-test': [io, tpmemu_files, 'tpm-tis-util.c'],
ed5979
   'tpm-tis-device-test': [io, tpmemu_files, 'tpm-tis-util.c'],
ed5979
   'vmgenid-test': files('boot-sector.c', 'acpi-utils.c'),
ed5979
+  'netdev-socket': files('netdev-socket.c', '../unit/socket-helpers.c'),
ed5979
 }
ed5979
 
ed5979
 gvnc = dependency('gvnc-1.0', required: false)
ed5979
diff --git a/tests/qtest/netdev-socket.c b/tests/qtest/netdev-socket.c
ed5979
new file mode 100644
ed5979
index 0000000000..6ba256e173
ed5979
--- /dev/null
ed5979
+++ b/tests/qtest/netdev-socket.c
ed5979
@@ -0,0 +1,448 @@
ed5979
+/*
ed5979
+ * QTest testcase for netdev stream and dgram
ed5979
+ *
ed5979
+ * Copyright (c) 2022 Red Hat, Inc.
ed5979
+ *
ed5979
+ * SPDX-License-Identifier: GPL-2.0-or-later
ed5979
+ */
ed5979
+
ed5979
+#include "qemu/osdep.h"
ed5979
+#include "qemu/sockets.h"
ed5979
+#include <glib/gstdio.h>
ed5979
+#include "../unit/socket-helpers.h"
ed5979
+#include "libqtest.h"
ed5979
+
ed5979
+#define CONNECTION_TIMEOUT    5
ed5979
+
ed5979
+#define EXPECT_STATE(q, e, t)                             \
ed5979
+do {                                                      \
ed5979
+    char *resp = NULL;                                    \
ed5979
+    g_test_timer_start();                                 \
ed5979
+    do {                                                  \
ed5979
+        g_free(resp);                                     \
ed5979
+        resp = qtest_hmp(q, "info network");              \
ed5979
+        if (t) {                                          \
ed5979
+            strrchr(resp, t)[0] = 0;                      \
ed5979
+        }                                                 \
ed5979
+        if (g_str_equal(resp, e)) {                       \
ed5979
+            break;                                        \
ed5979
+        }                                                 \
ed5979
+    } while (g_test_timer_elapsed() < CONNECTION_TIMEOUT); \
ed5979
+    g_assert_cmpstr(resp, ==, e);                         \
ed5979
+    g_free(resp);                                         \
ed5979
+} while (0)
ed5979
+
ed5979
+static gchar *tmpdir;
ed5979
+
ed5979
+static int inet_get_free_port_socket_ipv4(int sock)
ed5979
+{
ed5979
+    struct sockaddr_in addr;
ed5979
+    socklen_t len;
ed5979
+
ed5979
+    memset(&addr, 0, sizeof(addr));
ed5979
+    addr.sin_family = AF_INET;
ed5979
+    addr.sin_addr.s_addr = INADDR_ANY;
ed5979
+    addr.sin_port = 0;
ed5979
+    if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
ed5979
+        return -1;
ed5979
+    }
ed5979
+
ed5979
+    len = sizeof(addr);
ed5979
+    if (getsockname(sock,  (struct sockaddr *)&addr, &len) < 0) {
ed5979
+        return -1;
ed5979
+    }
ed5979
+
ed5979
+    return ntohs(addr.sin_port);
ed5979
+}
ed5979
+
ed5979
+static int inet_get_free_port_socket_ipv6(int sock)
ed5979
+{
ed5979
+    struct sockaddr_in6 addr;
ed5979
+    socklen_t len;
ed5979
+
ed5979
+    memset(&addr, 0, sizeof(addr));
ed5979
+    addr.sin6_family = AF_INET6;
ed5979
+    addr.sin6_addr = in6addr_any;
ed5979
+    addr.sin6_port = 0;
ed5979
+    if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
ed5979
+        return -1;
ed5979
+    }
ed5979
+
ed5979
+    len = sizeof(addr);
ed5979
+    if (getsockname(sock,  (struct sockaddr *)&addr, &len) < 0) {
ed5979
+        return -1;
ed5979
+    }
ed5979
+
ed5979
+    return ntohs(addr.sin6_port);
ed5979
+}
ed5979
+
ed5979
+static int inet_get_free_port_multiple(int nb, int *port, bool ipv6)
ed5979
+{
ed5979
+    int sock[nb];
ed5979
+    int i;
ed5979
+
ed5979
+    for (i = 0; i < nb; i++) {
ed5979
+        sock[i] = socket(ipv6 ? AF_INET6 : AF_INET, SOCK_STREAM, 0);
ed5979
+        if (sock[i] < 0) {
ed5979
+            break;
ed5979
+        }
ed5979
+        port[i] = ipv6 ? inet_get_free_port_socket_ipv6(sock[i]) :
ed5979
+                         inet_get_free_port_socket_ipv4(sock[i]);
ed5979
+        if (port[i] == -1) {
ed5979
+            break;
ed5979
+        }
ed5979
+    }
ed5979
+
ed5979
+    nb = i;
ed5979
+    for (i = 0; i < nb; i++) {
ed5979
+        closesocket(sock[i]);
ed5979
+    }
ed5979
+
ed5979
+    return nb;
ed5979
+}
ed5979
+
ed5979
+static int inet_get_free_port(bool ipv6)
ed5979
+{
ed5979
+    int nb, port;
ed5979
+
ed5979
+    nb = inet_get_free_port_multiple(1, &port, ipv6);
ed5979
+    g_assert_cmpint(nb, ==, 1);
ed5979
+
ed5979
+    return port;
ed5979
+}
ed5979
+
ed5979
+static void test_stream_inet_ipv4(void)
ed5979
+{
ed5979
+    QTestState *qts0, *qts1;
ed5979
+    char *expect;
ed5979
+    int port;
ed5979
+
ed5979
+    port = inet_get_free_port(false);
ed5979
+    qts0 = qtest_initf("-nodefaults -M none "
ed5979
+                       "-netdev stream,id=st0,server=true,addr.type=inet,"
ed5979
+                       "addr.ipv4=on,addr.ipv6=off,"
ed5979
+                       "addr.host=127.0.0.1,addr.port=%d", port);
ed5979
+
ed5979
+    EXPECT_STATE(qts0, "st0: index=0,type=stream,\r\n", 0);
ed5979
+
ed5979
+    qts1 = qtest_initf("-nodefaults -M none "
ed5979
+                       "-netdev stream,server=false,id=st0,addr.type=inet,"
ed5979
+                       "addr.ipv4=on,addr.ipv6=off,"
ed5979
+                       "addr.host=127.0.0.1,addr.port=%d", port);
ed5979
+
ed5979
+    expect = g_strdup_printf("st0: index=0,type=stream,tcp:127.0.0.1:%d\r\n",
ed5979
+                             port);
ed5979
+    EXPECT_STATE(qts1, expect, 0);
ed5979
+    g_free(expect);
ed5979
+
ed5979
+    /* the port is unknown, check only the address */
ed5979
+    EXPECT_STATE(qts0, "st0: index=0,type=stream,tcp:127.0.0.1", ':');
ed5979
+
ed5979
+    qtest_quit(qts1);
ed5979
+    qtest_quit(qts0);
ed5979
+}
ed5979
+
ed5979
+static void test_stream_inet_ipv6(void)
ed5979
+{
ed5979
+    QTestState *qts0, *qts1;
ed5979
+    char *expect;
ed5979
+    int port;
ed5979
+
ed5979
+    port = inet_get_free_port(true);
ed5979
+    qts0 = qtest_initf("-nodefaults -M none "
ed5979
+                       "-netdev stream,id=st0,server=true,addr.type=inet,"
ed5979
+                       "addr.ipv4=off,addr.ipv6=on,"
ed5979
+                       "addr.host=::1,addr.port=%d", port);
ed5979
+
ed5979
+    EXPECT_STATE(qts0, "st0: index=0,type=stream,\r\n", 0);
ed5979
+
ed5979
+    qts1 = qtest_initf("-nodefaults -M none "
ed5979
+                       "-netdev stream,server=false,id=st0,addr.type=inet,"
ed5979
+                       "addr.ipv4=off,addr.ipv6=on,"
ed5979
+                       "addr.host=::1,addr.port=%d", port);
ed5979
+
ed5979
+    expect = g_strdup_printf("st0: index=0,type=stream,tcp:::1:%d\r\n",
ed5979
+                             port);
ed5979
+    EXPECT_STATE(qts1, expect, 0);
ed5979
+    g_free(expect);
ed5979
+
ed5979
+    /* the port is unknown, check only the address */
ed5979
+    EXPECT_STATE(qts0, "st0: index=0,type=stream,tcp:::1", ':');
ed5979
+
ed5979
+    qtest_quit(qts1);
ed5979
+    qtest_quit(qts0);
ed5979
+}
ed5979
+
ed5979
+static void test_stream_unix(void)
ed5979
+{
ed5979
+    QTestState *qts0, *qts1;
ed5979
+    char *expect;
ed5979
+    gchar *path;
ed5979
+
ed5979
+    path = g_strconcat(tmpdir, "/stream_unix", NULL);
ed5979
+
ed5979
+    qts0 = qtest_initf("-nodefaults -M none "
ed5979
+                       "-netdev stream,id=st0,server=true,"
ed5979
+                       "addr.type=unix,addr.path=%s,",
ed5979
+                       path);
ed5979
+
ed5979
+    EXPECT_STATE(qts0, "st0: index=0,type=stream,\r\n", 0);
ed5979
+
ed5979
+    qts1 = qtest_initf("-nodefaults -M none "
ed5979
+                       "-netdev stream,id=st0,server=false,"
ed5979
+                       "addr.type=unix,addr.path=%s",
ed5979
+                       path);
ed5979
+
ed5979
+    expect = g_strdup_printf("st0: index=0,type=stream,unix:%s\r\n", path);
ed5979
+    EXPECT_STATE(qts1, expect, 0);
ed5979
+    EXPECT_STATE(qts0, expect, 0);
ed5979
+    g_free(expect);
ed5979
+    g_free(path);
ed5979
+
ed5979
+    qtest_quit(qts1);
ed5979
+    qtest_quit(qts0);
ed5979
+}
ed5979
+
ed5979
+#ifdef CONFIG_LINUX
ed5979
+static void test_stream_unix_abstract(void)
ed5979
+{
ed5979
+    QTestState *qts0, *qts1;
ed5979
+    char *expect;
ed5979
+    gchar *path;
ed5979
+
ed5979
+    path = g_strconcat(tmpdir, "/stream_unix_abstract", NULL);
ed5979
+
ed5979
+    qts0 = qtest_initf("-nodefaults -M none "
ed5979
+                       "-netdev stream,id=st0,server=true,"
ed5979
+                       "addr.type=unix,addr.path=%s,"
ed5979
+                       "addr.abstract=on",
ed5979
+                       path);
ed5979
+
ed5979
+    EXPECT_STATE(qts0, "st0: index=0,type=stream,\r\n", 0);
ed5979
+
ed5979
+    qts1 = qtest_initf("-nodefaults -M none "
ed5979
+                       "-netdev stream,id=st0,server=false,"
ed5979
+                       "addr.type=unix,addr.path=%s,addr.abstract=on",
ed5979
+                       path);
ed5979
+
ed5979
+    expect = g_strdup_printf("st0: index=0,type=stream,unix:%s\r\n", path);
ed5979
+    EXPECT_STATE(qts1, expect, 0);
ed5979
+    EXPECT_STATE(qts0, expect, 0);
ed5979
+    g_free(expect);
ed5979
+    g_free(path);
ed5979
+
ed5979
+    qtest_quit(qts1);
ed5979
+    qtest_quit(qts0);
ed5979
+}
ed5979
+#endif
ed5979
+
ed5979
+#ifndef _WIN32
ed5979
+static void test_stream_fd(void)
ed5979
+{
ed5979
+    QTestState *qts0, *qts1;
ed5979
+    int sock[2];
ed5979
+    int ret;
ed5979
+
ed5979
+    ret = socketpair(AF_LOCAL, SOCK_STREAM, 0, sock);
ed5979
+    g_assert_true(ret == 0);
ed5979
+
ed5979
+    qts0 = qtest_initf("-nodefaults -M none "
ed5979
+                       "-netdev stream,id=st0,addr.type=fd,addr.str=%d",
ed5979
+                       sock[0]);
ed5979
+
ed5979
+    EXPECT_STATE(qts0, "st0: index=0,type=stream,unix:\r\n", 0);
ed5979
+
ed5979
+    qts1 = qtest_initf("-nodefaults -M none "
ed5979
+                       "-netdev stream,id=st0,addr.type=fd,addr.str=%d",
ed5979
+                       sock[1]);
ed5979
+
ed5979
+    EXPECT_STATE(qts1, "st0: index=0,type=stream,unix:\r\n", 0);
ed5979
+    EXPECT_STATE(qts0, "st0: index=0,type=stream,unix:\r\n", 0);
ed5979
+
ed5979
+    qtest_quit(qts1);
ed5979
+    qtest_quit(qts0);
ed5979
+
ed5979
+    closesocket(sock[0]);
ed5979
+    closesocket(sock[1]);
ed5979
+}
ed5979
+#endif
ed5979
+
ed5979
+static void test_dgram_inet(void)
ed5979
+{
ed5979
+    QTestState *qts0, *qts1;
ed5979
+    char *expect;
ed5979
+    int port[2];
ed5979
+    int nb;
ed5979
+
ed5979
+    nb = inet_get_free_port_multiple(2, port, false);
ed5979
+    g_assert_cmpint(nb, ==, 2);
ed5979
+
ed5979
+    qts0 = qtest_initf("-nodefaults -M none "
ed5979
+                       "-netdev dgram,id=st0,"
ed5979
+                       "local.type=inet,local.host=127.0.0.1,local.port=%d,"
ed5979
+                       "remote.type=inet,remote.host=127.0.0.1,remote.port=%d",
ed5979
+                        port[0], port[1]);
ed5979
+
ed5979
+    expect = g_strdup_printf("st0: index=0,type=dgram,"
ed5979
+                             "udp=127.0.0.1:%d/127.0.0.1:%d\r\n",
ed5979
+                             port[0], port[1]);
ed5979
+    EXPECT_STATE(qts0, expect, 0);
ed5979
+    g_free(expect);
ed5979
+
ed5979
+    qts1 = qtest_initf("-nodefaults -M none "
ed5979
+                       "-netdev dgram,id=st0,"
ed5979
+                       "local.type=inet,local.host=127.0.0.1,local.port=%d,"
ed5979
+                       "remote.type=inet,remote.host=127.0.0.1,remote.port=%d",
ed5979
+                        port[1], port[0]);
ed5979
+
ed5979
+    expect = g_strdup_printf("st0: index=0,type=dgram,"
ed5979
+                             "udp=127.0.0.1:%d/127.0.0.1:%d\r\n",
ed5979
+                             port[1], port[0]);
ed5979
+    EXPECT_STATE(qts1, expect, 0);
ed5979
+    g_free(expect);
ed5979
+
ed5979
+    qtest_quit(qts1);
ed5979
+    qtest_quit(qts0);
ed5979
+}
ed5979
+
ed5979
+#ifndef _WIN32
ed5979
+static void test_dgram_mcast(void)
ed5979
+{
ed5979
+    QTestState *qts;
ed5979
+
ed5979
+    qts = qtest_initf("-nodefaults -M none "
ed5979
+                      "-netdev dgram,id=st0,"
ed5979
+                      "remote.type=inet,remote.host=230.0.0.1,remote.port=1234");
ed5979
+
ed5979
+    EXPECT_STATE(qts, "st0: index=0,type=dgram,mcast=230.0.0.1:1234\r\n", 0);
ed5979
+
ed5979
+    qtest_quit(qts);
ed5979
+}
ed5979
+
ed5979
+static void test_dgram_unix(void)
ed5979
+{
ed5979
+    QTestState *qts0, *qts1;
ed5979
+    char *expect;
ed5979
+    gchar *path0, *path1;
ed5979
+
ed5979
+    path0 = g_strconcat(tmpdir, "/dgram_unix0", NULL);
ed5979
+    path1 = g_strconcat(tmpdir, "/dgram_unix1", NULL);
ed5979
+
ed5979
+    qts0 = qtest_initf("-nodefaults -M none "
ed5979
+                       "-netdev dgram,id=st0,local.type=unix,local.path=%s,"
ed5979
+                       "remote.type=unix,remote.path=%s",
ed5979
+                       path0, path1);
ed5979
+
ed5979
+    expect = g_strdup_printf("st0: index=0,type=dgram,udp=%s:%s\r\n",
ed5979
+                             path0, path1);
ed5979
+    EXPECT_STATE(qts0, expect, 0);
ed5979
+    g_free(expect);
ed5979
+
ed5979
+    qts1 = qtest_initf("-nodefaults -M none "
ed5979
+                       "-netdev dgram,id=st0,local.type=unix,local.path=%s,"
ed5979
+                       "remote.type=unix,remote.path=%s",
ed5979
+                       path1, path0);
ed5979
+
ed5979
+
ed5979
+    expect = g_strdup_printf("st0: index=0,type=dgram,udp=%s:%s\r\n",
ed5979
+                             path1, path0);
ed5979
+    EXPECT_STATE(qts1, expect, 0);
ed5979
+    g_free(expect);
ed5979
+
ed5979
+    unlink(path0);
ed5979
+    g_free(path0);
ed5979
+    unlink(path1);
ed5979
+    g_free(path1);
ed5979
+
ed5979
+    qtest_quit(qts1);
ed5979
+    qtest_quit(qts0);
ed5979
+}
ed5979
+
ed5979
+static void test_dgram_fd(void)
ed5979
+{
ed5979
+    QTestState *qts0, *qts1;
ed5979
+    char *expect;
ed5979
+    int ret;
ed5979
+    int sv[2];
ed5979
+
ed5979
+    ret = socketpair(PF_UNIX, SOCK_DGRAM, 0, sv);
ed5979
+    g_assert_cmpint(ret, !=, -1);
ed5979
+
ed5979
+    qts0 = qtest_initf("-nodefaults -M none "
ed5979
+                       "-netdev dgram,id=st0,local.type=fd,local.str=%d",
ed5979
+                       sv[0]);
ed5979
+
ed5979
+    expect = g_strdup_printf("st0: index=0,type=dgram,fd=%d unix\r\n", sv[0]);
ed5979
+    EXPECT_STATE(qts0, expect, 0);
ed5979
+    g_free(expect);
ed5979
+
ed5979
+    qts1 = qtest_initf("-nodefaults -M none "
ed5979
+                       "-netdev dgram,id=st0,local.type=fd,local.str=%d",
ed5979
+                       sv[1]);
ed5979
+
ed5979
+
ed5979
+    expect = g_strdup_printf("st0: index=0,type=dgram,fd=%d unix\r\n", sv[1]);
ed5979
+    EXPECT_STATE(qts1, expect, 0);
ed5979
+    g_free(expect);
ed5979
+
ed5979
+    qtest_quit(qts1);
ed5979
+    qtest_quit(qts0);
ed5979
+
ed5979
+    closesocket(sv[0]);
ed5979
+    closesocket(sv[1]);
ed5979
+}
ed5979
+#endif
ed5979
+
ed5979
+int main(int argc, char **argv)
ed5979
+{
ed5979
+    int ret;
ed5979
+    bool has_ipv4, has_ipv6, has_afunix;
ed5979
+    g_autoptr(GError) err = NULL;
ed5979
+
ed5979
+    socket_init();
ed5979
+    g_test_init(&argc, &argv, NULL);
ed5979
+
ed5979
+    if (socket_check_protocol_support(&has_ipv4, &has_ipv6) < 0) {
ed5979
+        g_error("socket_check_protocol_support() failed\n");
ed5979
+    }
ed5979
+
ed5979
+    tmpdir = g_dir_make_tmp("netdev-socket.XXXXXX", &err;;
ed5979
+    if (tmpdir == NULL) {
ed5979
+        g_error("Can't create temporary directory in %s: %s",
ed5979
+                g_get_tmp_dir(), err->message);
ed5979
+    }
ed5979
+
ed5979
+    if (has_ipv4) {
ed5979
+        qtest_add_func("/netdev/stream/inet/ipv4", test_stream_inet_ipv4);
ed5979
+        qtest_add_func("/netdev/dgram/inet", test_dgram_inet);
ed5979
+#ifndef _WIN32
ed5979
+        qtest_add_func("/netdev/dgram/mcast", test_dgram_mcast);
ed5979
+#endif
ed5979
+    }
ed5979
+    if (has_ipv6) {
ed5979
+        qtest_add_func("/netdev/stream/inet/ipv6", test_stream_inet_ipv6);
ed5979
+    }
ed5979
+
ed5979
+    socket_check_afunix_support(&has_afunix);
ed5979
+    if (has_afunix) {
ed5979
+#ifndef _WIN32
ed5979
+        qtest_add_func("/netdev/dgram/unix", test_dgram_unix);
ed5979
+#endif
ed5979
+        qtest_add_func("/netdev/stream/unix", test_stream_unix);
ed5979
+#ifdef CONFIG_LINUX
ed5979
+        qtest_add_func("/netdev/stream/unix/abstract",
ed5979
+                       test_stream_unix_abstract);
ed5979
+#endif
ed5979
+#ifndef _WIN32
ed5979
+        qtest_add_func("/netdev/stream/fd", test_stream_fd);
ed5979
+        qtest_add_func("/netdev/dgram/fd", test_dgram_fd);
ed5979
+#endif
ed5979
+    }
ed5979
+
ed5979
+    ret = g_test_run();
ed5979
+
ed5979
+    g_rmdir(tmpdir);
ed5979
+    g_free(tmpdir);
ed5979
+
ed5979
+    return ret;
ed5979
+}
ed5979
-- 
ed5979
2.31.1
ed5979