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. Signed-off-by: Marc-André Lureau <[email protected]> --- tests/qtest/netdev-socket.c | 143 +++++++++++++++++++++++++------------------- 1 file changed, 82 insertions(+), 61 deletions(-) diff --git a/tests/qtest/netdev-socket.c b/tests/qtest/netdev-socket.c index b731af0ad95..30005c2e001 100644 --- a/tests/qtest/netdev-socket.c +++ b/tests/qtest/netdev-socket.c @@ -7,33 +7,62 @@ */ #include "qemu/osdep.h" -#include "qemu/sockets.h" #include <glib/gstdio.h> #include "../unit/socket-helpers.h" #include "libqtest.h" -#include "qobject/qstring.h" -#include "qemu/sockets.h" -#include "qapi/qobject-input-visitor.h" #include "qapi/qapi-visit-sockets.h" +#include "qapi/qobject-input-visitor.h" +#include "qemu/sockets.h" +#include "qobject/qdict.h" +#include "qobject/qlist.h" +#include "qobject/qstring.h" #define CONNECTION_TIMEOUT 60 -#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_str_equal(resp, e)) { \ - break; \ - } \ - } while (g_test_timer_elapsed() < CONNECTION_TIMEOUT); \ - g_assert_cmpstr(resp, ==, e); \ - g_free(resp); \ +/* + * Only checks info-str; type is implicit from the netdev created by each test + */ +static char *query_net_info_str(QTestState *q, const char *name) +{ + QDict *ret; + QList *clients; + QListEntry *entry; + + ret = qtest_qmp_assert_success_ref(q, "{'execute': 'x-query-network'}"); + clients = qdict_get_qlist(ret, "clients"); + g_assert_nonnull(clients); + + QLIST_FOREACH_ENTRY(clients, entry) { + QDict *client = qobject_to(QDict, qlist_entry_obj(entry)); + if (g_str_equal(qdict_get_str(client, "name"), name)) { + char *info = g_strdup(qdict_get_str(client, "info-str")); + qobject_unref(ret); + return info; + } + } + + g_assert_not_reached(); +} + +#define EXPECT_STATE(q, id, e, t) \ +do { \ + char *info = NULL; \ + g_test_timer_start(); \ + do { \ + g_free(info); \ + info = query_net_info_str(q, id); \ + if (t) { \ + char *p = strrchr(info, t); \ + if (p) { \ + p[0] = 0; \ + } \ + } \ + if (g_str_equal(info, e)) { \ + break; \ + } \ + } while (g_test_timer_elapsed() < CONNECTION_TIMEOUT); \ + g_assert_cmpstr(info, ==, e); \ + g_free(info); \ } while (0) static gchar *tmpdir; @@ -127,20 +156,19 @@ static void test_stream_inet_ipv4(void) "addr.ipv4=on,addr.ipv6=off," "addr.host=127.0.0.1,addr.port=%d", port); - EXPECT_STATE(qts0, "st0: index=0,type=stream,listening\r\n", 0); + EXPECT_STATE(qts0, "st0", "listening", 0); qts1 = qtest_initf("-nodefaults -M none " "-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); + expect = g_strdup_printf("tcp:127.0.0.1:%d", port); + EXPECT_STATE(qts1, "st0", 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", ':'); + EXPECT_STATE(qts0, "st0", "tcp:127.0.0.1", ':'); qtest_quit(qts1); qtest_quit(qts0); @@ -200,7 +228,7 @@ static void test_stream_unix_reconnect(void) "-netdev stream,id=st0,server=true,addr.type=unix," "addr.path=%s", path); - EXPECT_STATE(qts0, "st0: index=0,type=stream,listening\r\n", 0); + EXPECT_STATE(qts0, "st0", "listening", 0); qts1 = qtest_initf("-nodefaults -M none " "-netdev stream,server=false,id=st0,addr.type=unix," @@ -250,20 +278,19 @@ static void test_stream_inet_ipv6(void) "addr.ipv4=off,addr.ipv6=on," "addr.host=::1,addr.port=%d", port); - EXPECT_STATE(qts0, "st0: index=0,type=stream,listening\r\n", 0); + EXPECT_STATE(qts0, "st0", "listening", 0); qts1 = qtest_initf("-nodefaults -M none " "-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); + expect = g_strdup_printf("tcp:::1:%d", port); + EXPECT_STATE(qts1, "st0", expect, 0); g_free(expect); /* the port is unknown, check only the address */ - EXPECT_STATE(qts0, "st0: index=0,type=stream,tcp:::1", ':'); + EXPECT_STATE(qts0, "st0", "tcp:::1", ':'); qtest_quit(qts1); qtest_quit(qts0); @@ -282,16 +309,16 @@ static void test_stream_unix(void) "addr.type=unix,addr.path=%s,", path); - EXPECT_STATE(qts0, "st0: index=0,type=stream,listening\r\n", 0); + EXPECT_STATE(qts0, "st0", "listening", 0); qts1 = qtest_initf("-nodefaults -M none " "-netdev stream,id=st0,server=false," "addr.type=unix,addr.path=%s", path); - expect = g_strdup_printf("st0: index=0,type=stream,unix:%s\r\n", path); - EXPECT_STATE(qts1, expect, 0); - EXPECT_STATE(qts0, expect, 0); + expect = g_strdup_printf("unix:%s", path); + EXPECT_STATE(qts1, "st0", expect, 0); + EXPECT_STATE(qts0, "st0", expect, 0); g_free(expect); g_free(path); @@ -314,16 +341,16 @@ static void test_stream_unix_abstract(void) "addr.abstract=on", path); - EXPECT_STATE(qts0, "st0: index=0,type=stream,listening\r\n", 0); + EXPECT_STATE(qts0, "st0", "listening", 0); qts1 = qtest_initf("-nodefaults -M none " "-netdev stream,id=st0,server=false," "addr.type=unix,addr.path=%s,addr.abstract=on", path); - expect = g_strdup_printf("st0: index=0,type=stream,unix:%s\r\n", path); - EXPECT_STATE(qts1, expect, 0); - EXPECT_STATE(qts0, expect, 0); + expect = g_strdup_printf("unix:%s", path); + EXPECT_STATE(qts1, "st0", expect, 0); + EXPECT_STATE(qts0, "st0", expect, 0); g_free(expect); g_free(path); @@ -346,14 +373,14 @@ static void test_stream_fd(void) "-netdev stream,id=st0,addr.type=fd,addr.str=%d", sock[0]); - EXPECT_STATE(qts0, "st0: index=0,type=stream,unix:\r\n", 0); + EXPECT_STATE(qts0, "st0", "unix:", 0); qts1 = qtest_initf("-nodefaults -M none " "-netdev stream,id=st0,addr.type=fd,addr.str=%d", sock[1]); - EXPECT_STATE(qts1, "st0: index=0,type=stream,unix:\r\n", 0); - EXPECT_STATE(qts0, "st0: index=0,type=stream,unix:\r\n", 0); + EXPECT_STATE(qts1, "st0", "unix:", 0); + EXPECT_STATE(qts0, "st0", "unix:", 0); qtest_quit(qts1); qtest_quit(qts0); @@ -379,10 +406,9 @@ static void test_dgram_inet(void) "remote.type=inet,remote.host=127.0.0.1,remote.port=%d", port[0], port[1]); - expect = g_strdup_printf("st0: index=0,type=dgram," - "udp=127.0.0.1:%d/127.0.0.1:%d\r\n", + expect = g_strdup_printf("udp=127.0.0.1:%d/127.0.0.1:%d", port[0], port[1]); - EXPECT_STATE(qts0, expect, 0); + EXPECT_STATE(qts0, "st0", expect, 0); g_free(expect); qts1 = qtest_initf("-nodefaults -M none " @@ -391,10 +417,9 @@ static void test_dgram_inet(void) "remote.type=inet,remote.host=127.0.0.1,remote.port=%d", port[1], port[0]); - expect = g_strdup_printf("st0: index=0,type=dgram," - "udp=127.0.0.1:%d/127.0.0.1:%d\r\n", + expect = g_strdup_printf("udp=127.0.0.1:%d/127.0.0.1:%d", port[1], port[0]); - EXPECT_STATE(qts1, expect, 0); + EXPECT_STATE(qts1, "st0", expect, 0); g_free(expect); qtest_quit(qts1); @@ -410,7 +435,7 @@ static void test_dgram_mcast(void) "-netdev dgram,id=st0," "remote.type=inet,remote.host=230.0.0.1,remote.port=1234"); - EXPECT_STATE(qts, "st0: index=0,type=dgram,mcast=230.0.0.1:1234\r\n", 0); + EXPECT_STATE(qts, "st0", "mcast=230.0.0.1:1234", 0); qtest_quit(qts); } @@ -431,9 +456,8 @@ static void test_dgram_unix(void) "remote.type=unix,remote.path=%s", path0, path1); - expect = g_strdup_printf("st0: index=0,type=dgram,udp=%s:%s\r\n", - path0, path1); - EXPECT_STATE(qts0, expect, 0); + expect = g_strdup_printf("udp=%s:%s", path0, path1); + EXPECT_STATE(qts0, "st0", expect, 0); g_free(expect); qts1 = qtest_initf("-nodefaults -M none " @@ -441,10 +465,8 @@ static void test_dgram_unix(void) "remote.type=unix,remote.path=%s", path1, path0); - - expect = g_strdup_printf("st0: index=0,type=dgram,udp=%s:%s\r\n", - path1, path0); - EXPECT_STATE(qts1, expect, 0); + expect = g_strdup_printf("udp=%s:%s", path1, path0); + EXPECT_STATE(qts1, "st0", expect, 0); g_free(expect); unlink(path0); @@ -470,17 +492,16 @@ static void test_dgram_fd(void) "-netdev dgram,id=st0,local.type=fd,local.str=%d", sv[0]); - expect = g_strdup_printf("st0: index=0,type=dgram,fd=%d unix\r\n", sv[0]); - EXPECT_STATE(qts0, expect, 0); + expect = g_strdup_printf("fd=%d unix", sv[0]); + EXPECT_STATE(qts0, "st0", expect, 0); g_free(expect); qts1 = qtest_initf("-nodefaults -M none " "-netdev dgram,id=st0,local.type=fd,local.str=%d", sv[1]); - - expect = g_strdup_printf("st0: index=0,type=dgram,fd=%d unix\r\n", sv[1]); - EXPECT_STATE(qts1, expect, 0); + expect = g_strdup_printf("fd=%d unix", sv[1]); + EXPECT_STATE(qts1, "st0", expect, 0); g_free(expect); qtest_quit(qts1); -- 2.54.0
