On 12/12/22 14:20, Thomas Huth wrote:
On 09/11/2022 14.03, Laurent Vivier wrote:
[...]
diff --git a/tests/qtest/netdev-socket.c b/tests/qtest/netdev-socket.c
new file mode 100644
index 000000000000..b6b59244a282
--- /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)
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)
?
OK
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
[...]
+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
+ 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);
+ 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?
If it fails there is nothing we can do, I think it's better to ignore the
result.
+ return ret;
+}
Thomas
Thanks,
Laurent