Skip to contentSkip to conversation

tests/qtest: netdev: test stream and dgram backends

PATCH Superseded Laurent Vivier 2 participants 3 messages Nov 9, 2022 – Jan 2, 2023 Arm/Amlogic Meson SoC support

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 tests/qtest: netdev: test stream and dgram backends

---

Notes:
v3:
- Add "-M none" to avoid error:
"No machine specified, and there is no default"

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
tests/qtest/meson.build +2 -0
index c07a5b1a5f43..43d075b76280 100644
@@ -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)
tests/qtest/netdev-socket.c new file +435 -0

435 lines not on this page · raw message

Signature · 1 line
2.38.1
#1 lore raw

On 09/11/2022 14.03, Laurent Vivier wrote:
[...]

Quoted 31 lines
tests/qtest/netdev-socket.c new file +25 -0
new file mode 100644
index 000000000000..b6b59244a282
@@ -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;                                        \

tests/qtest/netdev-socket.c line 35

Quoted 10 lines
tests/qtest/netdev-socket.c new file +10 -0
+        }                                                 \
+        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)

Wouldn't it be possible to write this without the duplicated qtest_hmp() ? Something like this:

#define EXPECT_STATE(q, e, t)                             \
do {                                                      \
    char *resp = NULL;                                    \
    g_test_timer_start();                                 \
    do {                                                  \
        g_free(resp);                                     \
        resp = qtest_hmp(q, "info network");              \
        if (t) {                                          \
            strrchr(resp, t)[0] = 0;                      \
        }                                                 \
        if (g_strequal(resp, e)) {                        \
            break;                                        \
        }                                                 \
    } while (g_test_timer_elapsed() < CONNECTION_TIMEOUT); \
    g_assert_cmpstr(resp, ==, e);                         \
    g_free(resp);                                         \
} while (0)

?

Also matching strings against the output of a HMP command sound very fragile - isn't there a way to do this with QMP instead?

[...]

tests/qtest/netdev-socket.c line 396

Quoted 5 lines
tests/qtest/netdev-socket.c new file +5 -0
+int main(int argc, char **argv)
+{
+    int ret;
+    bool has_ipv4, has_ipv6, has_afunix;
+    gchar dir[] = "/tmp/netdev-socket.XXXXXX";

No more hard-coded /tmp/ paths, please. We're currently in progress of enabling the qtests on Windows, too. Please use g_dir_make_tmp() or something similar instead.

Quoted 25 lines
tests/qtest/netdev-socket.c new file +25 -0
+    g_test_init(&argc, &argv, NULL);
+
+    if (socket_check_protocol_support(&has_ipv4, &has_ipv6) < 0) {
+        g_printerr("socket_check_protocol_support() failed\n");
+        goto end;
+    }
+
+    if (g_mkdtemp(dir) == NULL) {
+        g_error("g_mkdtemp: %s", g_strerror(errno));
+    }
+    tmpdir = dir;
+
+    if (has_ipv4) {
+        qtest_add_func("/netdev/stream/inet/ipv4", test_stream_inet_ipv4);
+        qtest_add_func("/netdev/dgram/inet", test_dgram_inet);
+        qtest_add_func("/netdev/dgram/mcast", test_dgram_mcast);
+    }
+    if (has_ipv6) {
+        qtest_add_func("/netdev/stream/inet/ipv6", test_stream_inet_ipv6);
+    }
+
+    socket_check_afunix_support(&has_afunix);
+    if (has_afunix) {
+        qtest_add_func("/netdev/dgram/unix", test_dgram_unix);
+        qtest_add_func("/netdev/stream/unix", test_stream_unix);

tests/qtest/netdev-socket.c line 432

Quoted 10 lines
tests/qtest/netdev-socket.c new file +10 -0
+        qtest_add_func("/netdev/stream/unix/abstract",
+                       test_stream_unix_abstract);
+        qtest_add_func("/netdev/stream/fd", test_stream_fd);
+        qtest_add_func("/netdev/dgram/fd", test_dgram_fd);
+    }
+
+end:
+    ret = g_test_run();
+
+    g_rmdir(dir);

Maybe check the return code of g_rmdir(), to make sure that all temporary files in the directory have indeed been cleaned up successfully?

tests/qtest/netdev-socket.c line 435

Quoted 2 lines
tests/qtest/netdev-socket.c new file +2 -0
+    return ret;
+}

Thomas

#2 lore raw
Quoted 64 lines · Thomas Huth

On 09/11/2022 14.03, Laurent Vivier wrote:
[...]

Quoted 31 lines
tests/qtest/netdev-socket.c new file +25 -0
new file mode 100644
index 000000000000..b6b59244a282
@@ -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;                                        \

tests/qtest/netdev-socket.c line 35

Quoted 10 lines
tests/qtest/netdev-socket.c new file +10 -0
+        }                                                 \
+        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)

Wouldn't it be possible to write this without the duplicated qtest_hmp() ? Something like this:

#define EXPECT_STATE(q, e, t)                             \
do {                                                      \
    char *resp = NULL;                                    \
    g_test_timer_start();                                 \
    do {                                                  \
        g_free(resp);                                     \
        resp = qtest_hmp(q, "info network");              \
        if (t) {                                          \
            strrchr(resp, t)[0] = 0;                      \
        }                                                 \
        if (g_strequal(resp, e)) {                        \
            break;                                        \
        }                                                 \
    } while (g_test_timer_elapsed() < CONNECTION_TIMEOUT); \
    g_assert_cmpstr(resp, ==, e);                         \
    g_free(resp);                                         \
} while (0)

Quoted 1 line

?

OK

Quoted 2 lines

Also matching strings against the output of a HMP command sound very fragile - isn't there a way to do this with QMP instead?

No, there is no similar command with QMP. There was a tentative in the past but it has never been merged.

https://wiki.qemu.org/Google_Summer_of_Code_2010/QMP#query-netdev

Quoted 9 lines

[...]

tests/qtest/netdev-socket.c line 396

Quoted 5 lines
tests/qtest/netdev-socket.c new file +5 -0
+int main(int argc, char **argv)
+{
+    int ret;
+    bool has_ipv4, has_ipv6, has_afunix;
+    gchar dir[] = "/tmp/netdev-socket.XXXXXX";

No more hard-coded /tmp/ paths, please. We're currently in progress of enabling the qtests on Windows, too. Please use g_dir_make_tmp() or something similar instead.

ok

Quoted 30 lines
Quoted 30 lines

+    g_test_init(&argc, &argv, NULL);

tests/qtest/netdev-socket.c new file +28 -0
+
+    if (socket_check_protocol_support(&has_ipv4, &has_ipv6) < 0) {
+        g_printerr("socket_check_protocol_support() failed\n");
+        goto end;
+    }
+
+    if (g_mkdtemp(dir) == NULL) {
+        g_error("g_mkdtemp: %s", g_strerror(errno));
+    }
+    tmpdir = dir;
+
+    if (has_ipv4) {
+        qtest_add_func("/netdev/stream/inet/ipv4", test_stream_inet_ipv4);
+        qtest_add_func("/netdev/dgram/inet", test_dgram_inet);
+        qtest_add_func("/netdev/dgram/mcast", test_dgram_mcast);
+    }
+    if (has_ipv6) {
+        qtest_add_func("/netdev/stream/inet/ipv6", test_stream_inet_ipv6);
+    }
+
+    socket_check_afunix_support(&has_afunix);
+    if (has_afunix) {
+        qtest_add_func("/netdev/dgram/unix", test_dgram_unix);
+        qtest_add_func("/netdev/stream/unix", test_stream_unix);
+        qtest_add_func("/netdev/stream/unix/abstract",
+                       test_stream_unix_abstract);
+        qtest_add_func("/netdev/stream/fd", test_stream_fd);
+        qtest_add_func("/netdev/dgram/fd", test_dgram_fd);
Quoted 8 lines

tests/qtest/netdev-socket.c line 432

Quoted 6 lines

+    }

tests/qtest/netdev-socket.c new file +5 -0
+
+end:
+    ret = g_test_run();
+
+    g_rmdir(dir);

Maybe check the return code of g_rmdir(), to make sure that all temporary files in the directory have indeed been cleaned up successfully?

If it fails there is nothing we can do, I think it's better to ignore the result.

Quoted 6 lines
Quoted 3 lines

+    return ret;
+}

 Thomas

Thanks,
Laurent