@samitouri / QOSamiQemu / commits / 5261dda976

tests/qtest/netdev-socket: replace HMP with x-query-network QMP

Replace the HMP "info network" text parsing with the structured x-query-network QMP command. This makes the test assertions more robust by matching individual fields (info-str) instead of parsing the full HMP line format. The EXPECT_STATE macro now takes an explicit netdev id argument to look up the client by name in the QMP response. The strrchr truncation is made NULL-safe: if the truncation character is not found, the full string is compared (and the final g_assert_cmpstr will catch mismatches). While at it, fix the pre-existing duplicate "qemu/sockets.h" include, and reorder includes. Reviewed-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com> Message-ID: <20260828-qemu-no-hmp-v5-20-9227de146347@redhat.com>

Marc-André Lureau committed Aug 28, 2026 at 16:04 UTC 5261dda97697ce4e1ff6dacf7b6a3cf4516ec547
1 file changed +82 -61
tests/qtest/netdev-socket.c
+82 -61
@@ -7,33 +7,62 @@
7 */
8
9 #include "qemu/osdep.h"
10 -#include "qemu/sockets.h"
10 #include <glib/gstdio.h>
11 #include "../unit/socket-helpers.h"
12 #include "libqtest.h"
14 -#include "qobject/qstring.h"
15 -#include "qemu/sockets.h"
16 -#include "qapi/qobject-input-visitor.h"
13 #include "qapi/qapi-visit-sockets.h"
14 +#include "qapi/qobject-input-visitor.h"
15 +#include "qemu/sockets.h"
16 +#include "qobject/qdict.h"
17 +#include "qobject/qlist.h"
18 +#include "qobject/qstring.h"
19
20 #define CONNECTION_TIMEOUT 60
21
21 -#define EXPECT_STATE(q, e, t) \
22 -do { \
23 - char *resp = NULL; \
24 - g_test_timer_start(); \
25 - do { \
26 - g_free(resp); \
27 - resp = qtest_hmp(q, "info network"); \
28 - if (t) { \
29 - strrchr(resp, t)[0] = 0; \
30 - } \
31 - if (g_str_equal(resp, e)) { \
32 - break; \
33 - } \
34 - } while (g_test_timer_elapsed() < CONNECTION_TIMEOUT); \
35 - g_assert_cmpstr(resp, ==, e); \
36 - g_free(resp); \
22 +/*
23 + * Only checks info-str; type is implicit from the netdev created by each test
24 + */
25 +static char *query_net_info_str(QTestState *q, const char *name)
26 +{
27 + QDict *ret;
28 + QList *clients;
29 + QListEntry *entry;
30 +
31 + ret = qtest_qmp_assert_success_ref(q, "{'execute': 'x-query-network'}");
32 + clients = qdict_get_qlist(ret, "clients");
33 + g_assert_nonnull(clients);
34 +
35 + QLIST_FOREACH_ENTRY(clients, entry) {
36 + QDict *client = qobject_to(QDict, qlist_entry_obj(entry));
37 + if (g_str_equal(qdict_get_str(client, "name"), name)) {
38 + char *info = g_strdup(qdict_get_str(client, "info-str"));
39 + qobject_unref(ret);
40 + return info;
41 + }
42 + }
43 +
44 + g_assert_not_reached();
45 +}
46 +
47 +#define EXPECT_STATE(q, id, e, t) \
48 +do { \
49 + char *info = NULL; \
50 + g_test_timer_start(); \
51 + do { \
52 + g_free(info); \
53 + info = query_net_info_str(q, id); \
54 + if (t) { \
55 + char *p = strrchr(info, t); \
56 + if (p) { \
57 + p[0] = 0; \
58 + } \
59 + } \
60 + if (g_str_equal(info, e)) { \
61 + break; \
62 + } \
63 + } while (g_test_timer_elapsed() < CONNECTION_TIMEOUT); \
64 + g_assert_cmpstr(info, ==, e); \
65 + g_free(info); \
66 } while (0)
67
68 static gchar *tmpdir;
@@ -127,20 +156,19 @@ static void test_stream_inet_ipv4(void)
156 "addr.ipv4=on,addr.ipv6=off,"
157 "addr.host=127.0.0.1,addr.port=%d", port);
158
130 - EXPECT_STATE(qts0, "st0: index=0,type=stream,listening\r\n", 0);
159 + EXPECT_STATE(qts0, "st0", "listening", 0);
160
161 qts1 = qtest_initf("-nodefaults -M none "
162 "-netdev stream,server=false,id=st0,addr.type=inet,"
163 "addr.ipv4=on,addr.ipv6=off,"
164 "addr.host=127.0.0.1,addr.port=%d", port);
165
137 - expect = g_strdup_printf("st0: index=0,type=stream,tcp:127.0.0.1:%d\r\n",
138 - port);
139 - EXPECT_STATE(qts1, expect, 0);
166 + expect = g_strdup_printf("tcp:127.0.0.1:%d", port);
167 + EXPECT_STATE(qts1, "st0", expect, 0);
168 g_free(expect);
169
170 /* the port is unknown, check only the address */
143 - EXPECT_STATE(qts0, "st0: index=0,type=stream,tcp:127.0.0.1", ':');
171 + EXPECT_STATE(qts0, "st0", "tcp:127.0.0.1", ':');
172
173 qtest_quit(qts1);
174 qtest_quit(qts0);
@@ -200,7 +228,7 @@ static void test_stream_unix_reconnect(void)
228 "-netdev stream,id=st0,server=true,addr.type=unix,"
229 "addr.path=%s", path);
230
203 - EXPECT_STATE(qts0, "st0: index=0,type=stream,listening\r\n", 0);
231 + EXPECT_STATE(qts0, "st0", "listening", 0);
232
233 qts1 = qtest_initf("-nodefaults -M none "
234 "-netdev stream,server=false,id=st0,addr.type=unix,"
@@ -250,20 +278,19 @@ static void test_stream_inet_ipv6(void)
278 "addr.ipv4=off,addr.ipv6=on,"
279 "addr.host=::1,addr.port=%d", port);
280
253 - EXPECT_STATE(qts0, "st0: index=0,type=stream,listening\r\n", 0);
281 + EXPECT_STATE(qts0, "st0", "listening", 0);
282
283 qts1 = qtest_initf("-nodefaults -M none "
284 "-netdev stream,server=false,id=st0,addr.type=inet,"
285 "addr.ipv4=off,addr.ipv6=on,"
286 "addr.host=::1,addr.port=%d", port);
287
260 - expect = g_strdup_printf("st0: index=0,type=stream,tcp:::1:%d\r\n",
261 - port);
262 - EXPECT_STATE(qts1, expect, 0);
288 + expect = g_strdup_printf("tcp:::1:%d", port);
289 + EXPECT_STATE(qts1, "st0", expect, 0);
290 g_free(expect);
291
292 /* the port is unknown, check only the address */
266 - EXPECT_STATE(qts0, "st0: index=0,type=stream,tcp:::1", ':');
293 + EXPECT_STATE(qts0, "st0", "tcp:::1", ':');
294
295 qtest_quit(qts1);
296 qtest_quit(qts0);
@@ -282,16 +309,16 @@ static void test_stream_unix(void)
309 "addr.type=unix,addr.path=%s,",
310 path);
311
285 - EXPECT_STATE(qts0, "st0: index=0,type=stream,listening\r\n", 0);
312 + EXPECT_STATE(qts0, "st0", "listening", 0);
313
314 qts1 = qtest_initf("-nodefaults -M none "
315 "-netdev stream,id=st0,server=false,"
316 "addr.type=unix,addr.path=%s",
317 path);
318
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);
319 + expect = g_strdup_printf("unix:%s", path);
320 + EXPECT_STATE(qts1, "st0", expect, 0);
321 + EXPECT_STATE(qts0, "st0", expect, 0);
322 g_free(expect);
323 g_free(path);
324
@@ -314,16 +341,16 @@ static void test_stream_unix_abstract(void)
341 "addr.abstract=on",
342 path);
343
317 - EXPECT_STATE(qts0, "st0: index=0,type=stream,listening\r\n", 0);
344 + EXPECT_STATE(qts0, "st0", "listening", 0);
345
346 qts1 = qtest_initf("-nodefaults -M none "
347 "-netdev stream,id=st0,server=false,"
348 "addr.type=unix,addr.path=%s,addr.abstract=on",
349 path);
350
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);
351 + expect = g_strdup_printf("unix:%s", path);
352 + EXPECT_STATE(qts1, "st0", expect, 0);
353 + EXPECT_STATE(qts0, "st0", expect, 0);
354 g_free(expect);
355 g_free(path);
356
@@ -346,14 +373,14 @@ static void test_stream_fd(void)
373 "-netdev stream,id=st0,addr.type=fd,addr.str=%d",
374 sock[0]);
375
349 - EXPECT_STATE(qts0, "st0: index=0,type=stream,unix:\r\n", 0);
376 + EXPECT_STATE(qts0, "st0", "unix:", 0);
377
378 qts1 = qtest_initf("-nodefaults -M none "
379 "-netdev stream,id=st0,addr.type=fd,addr.str=%d",
380 sock[1]);
381
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);
382 + EXPECT_STATE(qts1, "st0", "unix:", 0);
383 + EXPECT_STATE(qts0, "st0", "unix:", 0);
384
385 qtest_quit(qts1);
386 qtest_quit(qts0);
@@ -379,10 +406,9 @@ static void test_dgram_inet(void)
406 "remote.type=inet,remote.host=127.0.0.1,remote.port=%d",
407 port[0], port[1]);
408
382 - expect = g_strdup_printf("st0: index=0,type=dgram,"
383 - "udp=127.0.0.1:%d/127.0.0.1:%d\r\n",
409 + expect = g_strdup_printf("udp=127.0.0.1:%d/127.0.0.1:%d",
410 port[0], port[1]);
385 - EXPECT_STATE(qts0, expect, 0);
411 + EXPECT_STATE(qts0, "st0", expect, 0);
412 g_free(expect);
413
414 qts1 = qtest_initf("-nodefaults -M none "
@@ -391,10 +417,9 @@ static void test_dgram_inet(void)
417 "remote.type=inet,remote.host=127.0.0.1,remote.port=%d",
418 port[1], port[0]);
419
394 - expect = g_strdup_printf("st0: index=0,type=dgram,"
395 - "udp=127.0.0.1:%d/127.0.0.1:%d\r\n",
420 + expect = g_strdup_printf("udp=127.0.0.1:%d/127.0.0.1:%d",
421 port[1], port[0]);
397 - EXPECT_STATE(qts1, expect, 0);
422 + EXPECT_STATE(qts1, "st0", expect, 0);
423 g_free(expect);
424
425 qtest_quit(qts1);
@@ -410,7 +435,7 @@ static void test_dgram_mcast(void)
435 "-netdev dgram,id=st0,"
436 "remote.type=inet,remote.host=230.0.0.1,remote.port=1234");
437
413 - EXPECT_STATE(qts, "st0: index=0,type=dgram,mcast=230.0.0.1:1234\r\n", 0);
438 + EXPECT_STATE(qts, "st0", "mcast=230.0.0.1:1234", 0);
439
440 qtest_quit(qts);
441 }
@@ -431,9 +456,8 @@ static void test_dgram_unix(void)
456 "remote.type=unix,remote.path=%s",
457 path0, path1);
458
434 - expect = g_strdup_printf("st0: index=0,type=dgram,udp=%s:%s\r\n",
435 - path0, path1);
436 - EXPECT_STATE(qts0, expect, 0);
459 + expect = g_strdup_printf("udp=%s:%s", path0, path1);
460 + EXPECT_STATE(qts0, "st0", expect, 0);
461 g_free(expect);
462
463 qts1 = qtest_initf("-nodefaults -M none "
@@ -441,10 +465,8 @@ static void test_dgram_unix(void)
465 "remote.type=unix,remote.path=%s",
466 path1, path0);
467
444 -
445 - expect = g_strdup_printf("st0: index=0,type=dgram,udp=%s:%s\r\n",
446 - path1, path0);
447 - EXPECT_STATE(qts1, expect, 0);
468 + expect = g_strdup_printf("udp=%s:%s", path1, path0);
469 + EXPECT_STATE(qts1, "st0", expect, 0);
470 g_free(expect);
471
472 unlink(path0);
@@ -470,17 +492,16 @@ static void test_dgram_fd(void)
492 "-netdev dgram,id=st0,local.type=fd,local.str=%d",
493 sv[0]);
494
473 - expect = g_strdup_printf("st0: index=0,type=dgram,fd=%d unix\r\n", sv[0]);
474 - EXPECT_STATE(qts0, expect, 0);
495 + expect = g_strdup_printf("fd=%d unix", sv[0]);
496 + EXPECT_STATE(qts0, "st0", expect, 0);
497 g_free(expect);
498
499 qts1 = qtest_initf("-nodefaults -M none "
500 "-netdev dgram,id=st0,local.type=fd,local.str=%d",
501 sv[1]);
502
481 -
482 - expect = g_strdup_printf("st0: index=0,type=dgram,fd=%d unix\r\n", sv[1]);
483 - EXPECT_STATE(qts1, expect, 0);
503 + expect = g_strdup_printf("fd=%d unix", sv[1]);
504 + EXPECT_STATE(qts1, "st0", expect, 0);
505 g_free(expect);
506
507 qtest_quit(qts1);