diff mbox series

[v5] tests/qtest: netdev: test stream and dgram backends

Message ID 20230105093751.416666-1-lvivier@redhat.com (mailing list archive)
State New, archived
Headers show
Series [v5] tests/qtest: netdev: test stream and dgram backends | expand

Commit Message

Laurent Vivier Jan. 5, 2023, 9:37 a.m. UTC
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
---

Notes:
    v5:
      - disable test_stream_fd and  test_dgram_fd on windows as socketpair()
        is not defined.
      - enable test_stream_unix_abstract only on linux as "abstract"
        unix socket parameter is only defined on linux.
    
    v4:
      - rework EXPECT_STATE()
      - use g_dir_make_tmp()
    
    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 | 444 ++++++++++++++++++++++++++++++++++++
 2 files changed, 446 insertions(+)
 create mode 100644 tests/qtest/netdev-socket.c

Comments

Laurent Vivier Jan. 16, 2023, 8:29 a.m. UTC | #1
ping

On 1/5/23 10:37, Laurent Vivier wrote:
> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
> Acked-by: Michael S. Tsirkin <mst@redhat.com>
> ---
> 
> Notes:
>      v5:
>        - disable test_stream_fd and  test_dgram_fd on windows as socketpair()
>          is not defined.
>        - enable test_stream_unix_abstract only on linux as "abstract"
>          unix socket parameter is only defined on linux.
>      
>      v4:
>        - rework EXPECT_STATE()
>        - use g_dir_make_tmp()
>      
>      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 | 444 ++++++++++++++++++++++++++++++++++++
>   2 files changed, 446 insertions(+)
>   create mode 100644 tests/qtest/netdev-socket.c
> 
> diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build
> index f0ebb5fac603..d752304711e2 100644
> --- a/tests/qtest/meson.build
> +++ b/tests/qtest/meson.build
> @@ -21,6 +21,7 @@ qtests_generic = [
>     'test-hmp',
>     'qos-test',
>     'readconfig-test',
> +  'netdev-socket',
>   ]
>   if config_host.has_key('CONFIG_MODULES')
>     qtests_generic += [ 'modules-test' ]
> @@ -298,6 +299,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)
> diff --git a/tests/qtest/netdev-socket.c b/tests/qtest/netdev-socket.c
> new file mode 100644
> index 000000000000..a9db9fa06e79
> --- /dev/null
> +++ b/tests/qtest/netdev-socket.c
> @@ -0,0 +1,444 @@
> +/*
> + * 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 = 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);                                         \
> +} while (0)
> +
> +static gchar *tmpdir;
> +
> +static int inet_get_free_port_socket_ipv4(int sock)
> +{
> +    struct sockaddr_in addr;
> +    socklen_t len;
> +
> +    memset(&addr, 0, sizeof(addr));
> +    addr.sin_family = AF_INET;
> +    addr.sin_addr.s_addr = INADDR_ANY;
> +    addr.sin_port = 0;
> +    if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
> +        return -1;
> +    }
> +
> +    len = sizeof(addr);
> +    if (getsockname(sock,  (struct sockaddr *)&addr, &len) < 0) {
> +        return -1;
> +    }
> +
> +    return ntohs(addr.sin_port);
> +}
> +
> +static int inet_get_free_port_socket_ipv6(int sock)
> +{
> +    struct sockaddr_in6 addr;
> +    socklen_t len;
> +
> +    memset(&addr, 0, sizeof(addr));
> +    addr.sin6_family = AF_INET6;
> +    addr.sin6_addr = in6addr_any;
> +    addr.sin6_port = 0;
> +    if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
> +        return -1;
> +    }
> +
> +    len = sizeof(addr);
> +    if (getsockname(sock,  (struct sockaddr *)&addr, &len) < 0) {
> +        return -1;
> +    }
> +
> +    return ntohs(addr.sin6_port);
> +}
> +
> +static int inet_get_free_port_multiple(int nb, int *port, bool ipv6)
> +{
> +    int sock[nb];
> +    int i;
> +
> +    for (i = 0; i < nb; i++) {
> +        sock[i] = socket(ipv6 ? AF_INET6 : AF_INET, SOCK_STREAM, 0);
> +        if (sock[i] < 0) {
> +            break;
> +        }
> +        port[i] = ipv6 ? inet_get_free_port_socket_ipv6(sock[i]) :
> +                         inet_get_free_port_socket_ipv4(sock[i]);
> +        if (port[i] == -1) {
> +            break;
> +        }
> +    }
> +
> +    nb = i;
> +    for (i = 0; i < nb; i++) {
> +        closesocket(sock[i]);
> +    }
> +
> +    return nb;
> +}
> +
> +static int inet_get_free_port(bool ipv6)
> +{
> +    int nb, port;
> +
> +    nb = inet_get_free_port_multiple(1, &port, ipv6);
> +    g_assert_cmpint(nb, ==, 1);
> +
> +    return port;
> +}
> +
> +static void test_stream_inet_ipv4(void)
> +{
> +    QTestState *qts0, *qts1;
> +    char *expect;
> +    int port;
> +
> +    port = inet_get_free_port(false);
> +    qts0 = qtest_initf("-nodefaults -M none "
> +                       "-netdev stream,id=st0,server=true,addr.type=inet,"
> +                       "addr.ipv4=on,addr.ipv6=off,"
> +                       "addr.host=127.0.0.1,addr.port=%d", port);
> +
> +    EXPECT_STATE(qts0, "st0: index=0,type=stream,\r\n", 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);
> +    g_free(expect);
> +
> +    /* the port is unknown, check only the address */
> +    EXPECT_STATE(qts0, "st0: index=0,type=stream,tcp:127.0.0.1", ':');
> +
> +    qtest_quit(qts1);
> +    qtest_quit(qts0);
> +}
> +
> +static void test_stream_inet_ipv6(void)
> +{
> +    QTestState *qts0, *qts1;
> +    char *expect;
> +    int port;
> +
> +    port = inet_get_free_port(true);
> +    qts0 = qtest_initf("-nodefaults -M none "
> +                       "-netdev stream,id=st0,server=true,addr.type=inet,"
> +                       "addr.ipv4=off,addr.ipv6=on,"
> +                       "addr.host=::1,addr.port=%d", port);
> +
> +    EXPECT_STATE(qts0, "st0: index=0,type=stream,\r\n", 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);
> +    g_free(expect);
> +
> +    /* the port is unknown, check only the address */
> +    EXPECT_STATE(qts0, "st0: index=0,type=stream,tcp:::1", ':');
> +
> +    qtest_quit(qts1);
> +    qtest_quit(qts0);
> +}
> +
> +static void test_stream_unix(void)
> +{
> +    QTestState *qts0, *qts1;
> +    char *expect;
> +    gchar *path;
> +
> +    path = g_strconcat(tmpdir, "/stream_unix", NULL);
> +
> +    qts0 = qtest_initf("-nodefaults -M none "
> +                       "-netdev stream,id=st0,server=true,"
> +                       "addr.type=unix,addr.path=%s,",
> +                       path);
> +
> +    EXPECT_STATE(qts0, "st0: index=0,type=stream,\r\n", 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);
> +    g_free(expect);
> +    g_free(path);
> +
> +    qtest_quit(qts1);
> +    qtest_quit(qts0);
> +}
> +
> +#ifdef CONFIG_LINUX
> +static void test_stream_unix_abstract(void)
> +{
> +    QTestState *qts0, *qts1;
> +    char *expect;
> +    gchar *path;
> +
> +    path = g_strconcat(tmpdir, "/stream_unix_abstract", NULL);
> +
> +    qts0 = qtest_initf("-nodefaults -M none "
> +                       "-netdev stream,id=st0,server=true,"
> +                       "addr.type=unix,addr.path=%s,"
> +                       "addr.abstract=on",
> +                       path);
> +
> +    EXPECT_STATE(qts0, "st0: index=0,type=stream,\r\n", 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);
> +    g_free(expect);
> +    g_free(path);
> +
> +    qtest_quit(qts1);
> +    qtest_quit(qts0);
> +}
> +#endif
> +
> +#ifndef _WIN32
> +static void test_stream_fd(void)
> +{
> +    QTestState *qts0, *qts1;
> +    int sock[2];
> +    int ret;
> +
> +    ret = socketpair(AF_LOCAL, SOCK_STREAM, 0, sock);
> +    g_assert_true(ret == 0);
> +
> +    qts0 = qtest_initf("-nodefaults -M none "
> +                       "-netdev stream,id=st0,addr.type=fd,addr.str=%d",
> +                       sock[0]);
> +
> +    EXPECT_STATE(qts0, "st0: index=0,type=stream,unix:\r\n", 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);
> +
> +    qtest_quit(qts1);
> +    qtest_quit(qts0);
> +
> +    closesocket(sock[0]);
> +    closesocket(sock[1]);
> +}
> +#endif
> +
> +static void test_dgram_inet(void)
> +{
> +    QTestState *qts0, *qts1;
> +    char *expect;
> +    int port[2];
> +    int nb;
> +
> +    nb = inet_get_free_port_multiple(2, port, false);
> +    g_assert_cmpint(nb, ==, 2);
> +
> +    qts0 = qtest_initf("-nodefaults -M none "
> +                       "-netdev dgram,id=st0,"
> +                       "local.type=inet,local.host=127.0.0.1,local.port=%d,"
> +                       "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",
> +                             port[0], port[1]);
> +    EXPECT_STATE(qts0, expect, 0);
> +    g_free(expect);
> +
> +    qts1 = qtest_initf("-nodefaults -M none "
> +                       "-netdev dgram,id=st0,"
> +                       "local.type=inet,local.host=127.0.0.1,local.port=%d,"
> +                       "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",
> +                             port[1], port[0]);
> +    EXPECT_STATE(qts1, expect, 0);
> +    g_free(expect);
> +
> +    qtest_quit(qts1);
> +    qtest_quit(qts0);
> +}
> +
> +static void test_dgram_mcast(void)
> +{
> +    QTestState *qts;
> +
> +    qts = qtest_initf("-nodefaults -M none "
> +                      "-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);
> +
> +    qtest_quit(qts);
> +}
> +
> +static void test_dgram_unix(void)
> +{
> +    QTestState *qts0, *qts1;
> +    char *expect;
> +    gchar *path0, *path1;
> +
> +    path0 = g_strconcat(tmpdir, "/dgram_unix0", NULL);
> +    path1 = g_strconcat(tmpdir, "/dgram_unix1", NULL);
> +
> +    qts0 = qtest_initf("-nodefaults -M none "
> +                       "-netdev dgram,id=st0,local.type=unix,local.path=%s,"
> +                       "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);
> +    g_free(expect);
> +
> +    qts1 = qtest_initf("-nodefaults -M none "
> +                       "-netdev dgram,id=st0,local.type=unix,local.path=%s,"
> +                       "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);
> +    g_free(expect);
> +
> +    unlink(path0);
> +    g_free(path0);
> +    unlink(path1);
> +    g_free(path1);
> +
> +    qtest_quit(qts1);
> +    qtest_quit(qts0);
> +}
> +
> +#ifndef _WIN32
> +static void test_dgram_fd(void)
> +{
> +    QTestState *qts0, *qts1;
> +    char *expect;
> +    int ret;
> +    int sv[2];
> +
> +    ret = socketpair(PF_UNIX, SOCK_DGRAM, 0, sv);
> +    g_assert_cmpint(ret, !=, -1);
> +
> +    qts0 = qtest_initf("-nodefaults -M none "
> +                       "-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);
> +    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);
> +    g_free(expect);
> +
> +    qtest_quit(qts1);
> +    qtest_quit(qts0);
> +
> +    closesocket(sv[0]);
> +    closesocket(sv[1]);
> +}
> +#endif
> +
> +int main(int argc, char **argv)
> +{
> +    int ret;
> +    bool has_ipv4, has_ipv6, has_afunix;
> +    g_autoptr(GError) err = NULL;
> +
> +    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;
> +    }
> +
> +    tmpdir = g_dir_make_tmp("netdev-socket.XXXXXX", &err);
> +    if (tmpdir == NULL) {
> +        g_error("Can't create temporary directory in %s: %s",
> +                g_get_tmp_dir(), err->message);
> +    }
> +
> +    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);
> +#ifdef CONFIG_LINUX
> +        qtest_add_func("/netdev/stream/unix/abstract",
> +                       test_stream_unix_abstract);
> +#endif
> +#ifndef _WIN32
> +        qtest_add_func("/netdev/stream/fd", test_stream_fd);
> +        qtest_add_func("/netdev/dgram/fd", test_dgram_fd);
> +#endif
> +    }
> +
> +end:
> +    ret = g_test_run();
> +
> +    g_rmdir(tmpdir);
> +    g_free(tmpdir);
> +
> +    return ret;
> +}
Thomas Huth Jan. 16, 2023, 8:40 a.m. UTC | #2
On 16/01/2023 09.29, Laurent Vivier wrote:
> ping
> 
> On 1/5/23 10:37, Laurent Vivier wrote:
>> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
>> Acked-by: Michael S. Tsirkin <mst@redhat.com>
>> ---
>>
>> Notes:
>>      v5:
>>        - disable test_stream_fd and  test_dgram_fd on windows as socketpair()
>>          is not defined.
>>        - enable test_stream_unix_abstract only on linux as "abstract"
>>          unix socket parameter is only defined on linux.
>>      v4:
>>        - rework EXPECT_STATE()
>>        - use g_dir_make_tmp()
>>      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 | 444 ++++++++++++++++++++++++++++++++++++
>>   2 files changed, 446 insertions(+)
>>   create mode 100644 tests/qtest/netdev-socket.c

Acked-by: Thomas Huth <thuth@redhat.com>

I'll queue it for my next pull request (unless someone else wants to take 
this first)
Thomas Huth Jan. 17, 2023, 10 a.m. UTC | #3
On 16/01/2023 09.40, Thomas Huth wrote:
> On 16/01/2023 09.29, Laurent Vivier wrote:
>> ping
>>
>> On 1/5/23 10:37, Laurent Vivier wrote:
>>> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
>>> Acked-by: Michael S. Tsirkin <mst@redhat.com>
>>> ---
>>>
>>> Notes:
>>>      v5:
>>>        - disable test_stream_fd and  test_dgram_fd on windows as 
>>> socketpair()
>>>          is not defined.
>>>        - enable test_stream_unix_abstract only on linux as "abstract"
>>>          unix socket parameter is only defined on linux.
>>>      v4:
>>>        - rework EXPECT_STATE()
>>>        - use g_dir_make_tmp()
>>>      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 | 444 ++++++++++++++++++++++++++++++++++++
>>>   2 files changed, 446 insertions(+)
>>>   create mode 100644 tests/qtest/netdev-socket.c
> 
> Acked-by: Thomas Huth <thuth@redhat.com>
> 
> I'll queue it for my next pull request (unless someone else wants to take 
> this first)

Sorry, but I have to unqueue it again. I'm still seeing failures
in the Windows Cirrus-CI:

  https://cirrus-ci.com/task/5867407370092544

For example:

218/556 qemu:qtest+qtest-aarch64 / qtest-aarch64/netdev-socket                    ERROR           0.02s   exit status 3
------------------------------------- 8< -------------------------------------
stderr:
socket_check_protocol_support() failed

(C:/Users/ContainerAdministrator/AppData/Local/Temp/cirrus-ci-build/build/tests/qtest/netdev-socket.exe:3300): GLib-CRITICAL **: 09:08:00.984: g_utf8_to_utf16: assertion 'str != NULL' failed

(test program exited with status code 3)

No clue where this comes from, though, I don't see a call
to g_utf8_to_utf16() in your code?

  Thomas
Laurent Vivier Jan. 17, 2023, 10:53 a.m. UTC | #4
On 1/17/23 11:00, Thomas Huth wrote:
> On 16/01/2023 09.40, Thomas Huth wrote:
>> On 16/01/2023 09.29, Laurent Vivier wrote:
>>> ping
>>>
>>> On 1/5/23 10:37, Laurent Vivier wrote:
>>>> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
>>>> Acked-by: Michael S. Tsirkin <mst@redhat.com>
>>>> ---
>>>>
>>>> Notes:
>>>>      v5:
>>>>        - disable test_stream_fd and  test_dgram_fd on windows as socketpair()
>>>>          is not defined.
>>>>        - enable test_stream_unix_abstract only on linux as "abstract"
>>>>          unix socket parameter is only defined on linux.
>>>>      v4:
>>>>        - rework EXPECT_STATE()
>>>>        - use g_dir_make_tmp()
>>>>      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 | 444 ++++++++++++++++++++++++++++++++++++
>>>>   2 files changed, 446 insertions(+)
>>>>   create mode 100644 tests/qtest/netdev-socket.c
>>
>> Acked-by: Thomas Huth <thuth@redhat.com>
>>
>> I'll queue it for my next pull request (unless someone else wants to take this first)
> 
> Sorry, but I have to unqueue it again. I'm still seeing failures
> in the Windows Cirrus-CI:

I'm sorry too :(

> 
>   https://cirrus-ci.com/task/5867407370092544
> 
> For example:
> 
> 218/556 qemu:qtest+qtest-aarch64 / qtest-aarch64/netdev-socket                    
> ERROR           0.02s   exit status 3
> ------------------------------------- 8< -------------------------------------
> stderr:
> socket_check_protocol_support() failed
> 
> (C:/Users/ContainerAdministrator/AppData/Local/Temp/cirrus-ci-build/build/tests/qtest/netdev-socket.exe:3300): GLib-CRITICAL **: 09:08:00.984: g_utf8_to_utf16: assertion 'str != NULL' failed
> 
> (test program exited with status code 3)
> 
> No clue where this comes from, though, I don't see a call
> to g_utf8_to_utf16() in your code?

Could you give the command line to reproduce the problem?

Thanks,
Laurent

>   Thomas
>
Laurent Vivier Jan. 17, 2023, 11:03 a.m. UTC | #5
On 1/17/23 11:00, Thomas Huth wrote:
> On 16/01/2023 09.40, Thomas Huth wrote:
>> On 16/01/2023 09.29, Laurent Vivier wrote:
>>> ping
>>>
>>> On 1/5/23 10:37, Laurent Vivier wrote:
>>>> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
>>>> Acked-by: Michael S. Tsirkin <mst@redhat.com>
>>>> ---
>>>>
>>>> Notes:
>>>>      v5:
>>>>        - disable test_stream_fd and  test_dgram_fd on windows as socketpair()
>>>>          is not defined.
>>>>        - enable test_stream_unix_abstract only on linux as "abstract"
>>>>          unix socket parameter is only defined on linux.
>>>>      v4:
>>>>        - rework EXPECT_STATE()
>>>>        - use g_dir_make_tmp()
>>>>      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 | 444 ++++++++++++++++++++++++++++++++++++
>>>>   2 files changed, 446 insertions(+)
>>>>   create mode 100644 tests/qtest/netdev-socket.c
>>
>> Acked-by: Thomas Huth <thuth@redhat.com>
>>
>> I'll queue it for my next pull request (unless someone else wants to take this first)
> 
> Sorry, but I have to unqueue it again. I'm still seeing failures
> in the Windows Cirrus-CI:
> 
>   https://cirrus-ci.com/task/5867407370092544
> 
> For example:
> 
> 218/556 qemu:qtest+qtest-aarch64 / qtest-aarch64/netdev-socket                    
> ERROR           0.02s   exit status 3
> ------------------------------------- 8< -------------------------------------
> stderr:
> socket_check_protocol_support() failed
> 
> (C:/Users/ContainerAdministrator/AppData/Local/Temp/cirrus-ci-build/build/tests/qtest/netdev-socket.exe:3300): GLib-CRITICAL **: 09:08:00.984: g_utf8_to_utf16: assertion 'str != NULL' failed
> 
> (test program exited with status code 3)
> 
> No clue where this comes from, though, I don't see a call
> to g_utf8_to_utf16() in your code?

OK, there is an error in the log:

socket_check_protocol_support() failed

So tmpdir is NULL and we try an g_rmdir(tmpdir)

I think this is the problem.

But why socket_check_protocol_support() fails?

test-char, test-io-channel-socket, test-util-sockets and test-yank also use it.

It seems we need to call socket_init() before...

I update my test.

Thanks,
Laurent
Thomas Huth Jan. 17, 2023, 11:20 a.m. UTC | #6
On 17/01/2023 11.53, Laurent Vivier wrote:
> On 1/17/23 11:00, Thomas Huth wrote:
>> On 16/01/2023 09.40, Thomas Huth wrote:
>>> On 16/01/2023 09.29, Laurent Vivier wrote:
>>>> ping
>>>>
>>>> On 1/5/23 10:37, Laurent Vivier wrote:
>>>>> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
>>>>> Acked-by: Michael S. Tsirkin <mst@redhat.com>
>>>>> ---
>>>>>
>>>>> Notes:
>>>>>      v5:
>>>>>        - disable test_stream_fd and  test_dgram_fd on windows as 
>>>>> socketpair()
>>>>>          is not defined.
>>>>>        - enable test_stream_unix_abstract only on linux as "abstract"
>>>>>          unix socket parameter is only defined on linux.
>>>>>      v4:
>>>>>        - rework EXPECT_STATE()
>>>>>        - use g_dir_make_tmp()
>>>>>      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 | 444 ++++++++++++++++++++++++++++++++++++
>>>>>   2 files changed, 446 insertions(+)
>>>>>   create mode 100644 tests/qtest/netdev-socket.c
>>>
>>> Acked-by: Thomas Huth <thuth@redhat.com>
>>>
>>> I'll queue it for my next pull request (unless someone else wants to take 
>>> this first)
>>
>> Sorry, but I have to unqueue it again. I'm still seeing failures
>> in the Windows Cirrus-CI:
> 
> I'm sorry too :(
> 
>>
>>   https://cirrus-ci.com/task/5867407370092544
>>
>> For example:
>>
>> 218/556 qemu:qtest+qtest-aarch64 / qtest-aarch64/netdev-socket 
>> ERROR           0.02s   exit status 3
>> ------------------------------------- 8< 
>> -------------------------------------
>> stderr:
>> socket_check_protocol_support() failed
>>
>> (C:/Users/ContainerAdministrator/AppData/Local/Temp/cirrus-ci-build/build/tests/qtest/netdev-socket.exe:3300): 
>> GLib-CRITICAL **: 09:08:00.984: g_utf8_to_utf16: assertion 'str != NULL' 
>> failed
>>
>> (test program exited with status code 3)
>>
>> No clue where this comes from, though, I don't see a call
>> to g_utf8_to_utf16() in your code?
> 
> Could you give the command line to reproduce the problem?

This was running in the Cirrus-CI via the .cirrus.yml file. You need a git 
repo on github for this and enable the Cirrus-CI for it, then it should 
trigger automatically when you push a new branch to the repository.

  Thomas
Philippe Mathieu-Daudé Jan. 17, 2023, 11:32 a.m. UTC | #7
On 17/1/23 12:03, Laurent Vivier wrote:
> On 1/17/23 11:00, Thomas Huth wrote:
>> On 16/01/2023 09.40, Thomas Huth wrote:
>>> On 16/01/2023 09.29, Laurent Vivier wrote:
>>>> ping
>>>>
>>>> On 1/5/23 10:37, Laurent Vivier wrote:
>>>>> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
>>>>> Acked-by: Michael S. Tsirkin <mst@redhat.com>

>>> Acked-by: Thomas Huth <thuth@redhat.com>
>>>
>>> I'll queue it for my next pull request (unless someone else wants to 
>>> take this first)
>>
>> Sorry, but I have to unqueue it again. I'm still seeing failures
>> in the Windows Cirrus-CI:
>>
>>   https://cirrus-ci.com/task/5867407370092544
>>
>> For example:
>>
>> 218/556 qemu:qtest+qtest-aarch64 / qtest-aarch64/netdev-socket 
>> ERROR           0.02s   exit status 3
>> ------------------------------------- 8< 
>> -------------------------------------
>> stderr:
>> socket_check_protocol_support() failed
>>
>> (C:/Users/ContainerAdministrator/AppData/Local/Temp/cirrus-ci-build/build/tests/qtest/netdev-socket.exe:3300): GLib-CRITICAL **: 09:08:00.984: g_utf8_to_utf16: assertion 'str != NULL' failed
>>
>> (test program exited with status code 3)
>>
>> No clue where this comes from, though, I don't see a call
>> to g_utf8_to_utf16() in your code?
> 
> OK, there is an error in the log:
> 
> socket_check_protocol_support() failed
> 
> So tmpdir is NULL and we try an g_rmdir(tmpdir)

"In case of errors, NULL is returned and error will be set."

I don't see the ""Can't create temporary directory" string in
Thomas' failing job.

In such case, are the g_strconcat(tmpdir) calls returning "\0"?
Laurent Vivier Jan. 17, 2023, 11:44 a.m. UTC | #8
On 1/17/23 12:32, Philippe Mathieu-Daudé wrote:
> On 17/1/23 12:03, Laurent Vivier wrote:
>> On 1/17/23 11:00, Thomas Huth wrote:
>>> On 16/01/2023 09.40, Thomas Huth wrote:
>>>> On 16/01/2023 09.29, Laurent Vivier wrote:
>>>>> ping
>>>>>
>>>>> On 1/5/23 10:37, Laurent Vivier wrote:
>>>>>> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
>>>>>> Acked-by: Michael S. Tsirkin <mst@redhat.com>
> 
>>>> Acked-by: Thomas Huth <thuth@redhat.com>
>>>>
>>>> I'll queue it for my next pull request (unless someone else wants to take this first)
>>>
>>> Sorry, but I have to unqueue it again. I'm still seeing failures
>>> in the Windows Cirrus-CI:
>>>
>>>   https://cirrus-ci.com/task/5867407370092544
>>>
>>> For example:
>>>
>>> 218/556 qemu:qtest+qtest-aarch64 / qtest-aarch64/netdev-socket ERROR           0.02s   
>>> exit status 3
>>> ------------------------------------- 8< -------------------------------------
>>> stderr:
>>> socket_check_protocol_support() failed
>>>
>>> (C:/Users/ContainerAdministrator/AppData/Local/Temp/cirrus-ci-build/build/tests/qtest/netdev-socket.exe:3300): GLib-CRITICAL **: 09:08:00.984: g_utf8_to_utf16: assertion 'str != NULL' failed
>>>
>>> (test program exited with status code 3)
>>>
>>> No clue where this comes from, though, I don't see a call
>>> to g_utf8_to_utf16() in your code?
>>
>> OK, there is an error in the log:
>>
>> socket_check_protocol_support() failed
>>
>> So tmpdir is NULL and we try an g_rmdir(tmpdir)
> 
> "In case of errors, NULL is returned and error will be set."
> 
> I don't see the ""Can't create temporary directory" string in
> Thomas' failing job.
> 
> In such case, are the g_strconcat(tmpdir) calls returning "\0"?
> 
> 

I'm going to use g_error() on socket_check_protocol_support() failure to exit. Without the 
problem with g_utf8_to_utf16() I would not know the socket_init() function was needed.

Thanks,
Laurent
diff mbox series

Patch

diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build
index f0ebb5fac603..d752304711e2 100644
--- a/tests/qtest/meson.build
+++ b/tests/qtest/meson.build
@@ -21,6 +21,7 @@  qtests_generic = [
   'test-hmp',
   'qos-test',
   'readconfig-test',
+  'netdev-socket',
 ]
 if config_host.has_key('CONFIG_MODULES')
   qtests_generic += [ 'modules-test' ]
@@ -298,6 +299,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)
diff --git a/tests/qtest/netdev-socket.c b/tests/qtest/netdev-socket.c
new file mode 100644
index 000000000000..a9db9fa06e79
--- /dev/null
+++ b/tests/qtest/netdev-socket.c
@@ -0,0 +1,444 @@ 
+/*
+ * 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 = 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);                                         \
+} while (0)
+
+static gchar *tmpdir;
+
+static int inet_get_free_port_socket_ipv4(int sock)
+{
+    struct sockaddr_in addr;
+    socklen_t len;
+
+    memset(&addr, 0, sizeof(addr));
+    addr.sin_family = AF_INET;
+    addr.sin_addr.s_addr = INADDR_ANY;
+    addr.sin_port = 0;
+    if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
+        return -1;
+    }
+
+    len = sizeof(addr);
+    if (getsockname(sock,  (struct sockaddr *)&addr, &len) < 0) {
+        return -1;
+    }
+
+    return ntohs(addr.sin_port);
+}
+
+static int inet_get_free_port_socket_ipv6(int sock)
+{
+    struct sockaddr_in6 addr;
+    socklen_t len;
+
+    memset(&addr, 0, sizeof(addr));
+    addr.sin6_family = AF_INET6;
+    addr.sin6_addr = in6addr_any;
+    addr.sin6_port = 0;
+    if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
+        return -1;
+    }
+
+    len = sizeof(addr);
+    if (getsockname(sock,  (struct sockaddr *)&addr, &len) < 0) {
+        return -1;
+    }
+
+    return ntohs(addr.sin6_port);
+}
+
+static int inet_get_free_port_multiple(int nb, int *port, bool ipv6)
+{
+    int sock[nb];
+    int i;
+
+    for (i = 0; i < nb; i++) {
+        sock[i] = socket(ipv6 ? AF_INET6 : AF_INET, SOCK_STREAM, 0);
+        if (sock[i] < 0) {
+            break;
+        }
+        port[i] = ipv6 ? inet_get_free_port_socket_ipv6(sock[i]) :
+                         inet_get_free_port_socket_ipv4(sock[i]);
+        if (port[i] == -1) {
+            break;
+        }
+    }
+
+    nb = i;
+    for (i = 0; i < nb; i++) {
+        closesocket(sock[i]);
+    }
+
+    return nb;
+}
+
+static int inet_get_free_port(bool ipv6)
+{
+    int nb, port;
+
+    nb = inet_get_free_port_multiple(1, &port, ipv6);
+    g_assert_cmpint(nb, ==, 1);
+
+    return port;
+}
+
+static void test_stream_inet_ipv4(void)
+{
+    QTestState *qts0, *qts1;
+    char *expect;
+    int port;
+
+    port = inet_get_free_port(false);
+    qts0 = qtest_initf("-nodefaults -M none "
+                       "-netdev stream,id=st0,server=true,addr.type=inet,"
+                       "addr.ipv4=on,addr.ipv6=off,"
+                       "addr.host=127.0.0.1,addr.port=%d", port);
+
+    EXPECT_STATE(qts0, "st0: index=0,type=stream,\r\n", 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);
+    g_free(expect);
+
+    /* the port is unknown, check only the address */
+    EXPECT_STATE(qts0, "st0: index=0,type=stream,tcp:127.0.0.1", ':');
+
+    qtest_quit(qts1);
+    qtest_quit(qts0);
+}
+
+static void test_stream_inet_ipv6(void)
+{
+    QTestState *qts0, *qts1;
+    char *expect;
+    int port;
+
+    port = inet_get_free_port(true);
+    qts0 = qtest_initf("-nodefaults -M none "
+                       "-netdev stream,id=st0,server=true,addr.type=inet,"
+                       "addr.ipv4=off,addr.ipv6=on,"
+                       "addr.host=::1,addr.port=%d", port);
+
+    EXPECT_STATE(qts0, "st0: index=0,type=stream,\r\n", 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);
+    g_free(expect);
+
+    /* the port is unknown, check only the address */
+    EXPECT_STATE(qts0, "st0: index=0,type=stream,tcp:::1", ':');
+
+    qtest_quit(qts1);
+    qtest_quit(qts0);
+}
+
+static void test_stream_unix(void)
+{
+    QTestState *qts0, *qts1;
+    char *expect;
+    gchar *path;
+
+    path = g_strconcat(tmpdir, "/stream_unix", NULL);
+
+    qts0 = qtest_initf("-nodefaults -M none "
+                       "-netdev stream,id=st0,server=true,"
+                       "addr.type=unix,addr.path=%s,",
+                       path);
+
+    EXPECT_STATE(qts0, "st0: index=0,type=stream,\r\n", 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);
+    g_free(expect);
+    g_free(path);
+
+    qtest_quit(qts1);
+    qtest_quit(qts0);
+}
+
+#ifdef CONFIG_LINUX
+static void test_stream_unix_abstract(void)
+{
+    QTestState *qts0, *qts1;
+    char *expect;
+    gchar *path;
+
+    path = g_strconcat(tmpdir, "/stream_unix_abstract", NULL);
+
+    qts0 = qtest_initf("-nodefaults -M none "
+                       "-netdev stream,id=st0,server=true,"
+                       "addr.type=unix,addr.path=%s,"
+                       "addr.abstract=on",
+                       path);
+
+    EXPECT_STATE(qts0, "st0: index=0,type=stream,\r\n", 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);
+    g_free(expect);
+    g_free(path);
+
+    qtest_quit(qts1);
+    qtest_quit(qts0);
+}
+#endif
+
+#ifndef _WIN32
+static void test_stream_fd(void)
+{
+    QTestState *qts0, *qts1;
+    int sock[2];
+    int ret;
+
+    ret = socketpair(AF_LOCAL, SOCK_STREAM, 0, sock);
+    g_assert_true(ret == 0);
+
+    qts0 = qtest_initf("-nodefaults -M none "
+                       "-netdev stream,id=st0,addr.type=fd,addr.str=%d",
+                       sock[0]);
+
+    EXPECT_STATE(qts0, "st0: index=0,type=stream,unix:\r\n", 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);
+
+    qtest_quit(qts1);
+    qtest_quit(qts0);
+
+    closesocket(sock[0]);
+    closesocket(sock[1]);
+}
+#endif
+
+static void test_dgram_inet(void)
+{
+    QTestState *qts0, *qts1;
+    char *expect;
+    int port[2];
+    int nb;
+
+    nb = inet_get_free_port_multiple(2, port, false);
+    g_assert_cmpint(nb, ==, 2);
+
+    qts0 = qtest_initf("-nodefaults -M none "
+                       "-netdev dgram,id=st0,"
+                       "local.type=inet,local.host=127.0.0.1,local.port=%d,"
+                       "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",
+                             port[0], port[1]);
+    EXPECT_STATE(qts0, expect, 0);
+    g_free(expect);
+
+    qts1 = qtest_initf("-nodefaults -M none "
+                       "-netdev dgram,id=st0,"
+                       "local.type=inet,local.host=127.0.0.1,local.port=%d,"
+                       "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",
+                             port[1], port[0]);
+    EXPECT_STATE(qts1, expect, 0);
+    g_free(expect);
+
+    qtest_quit(qts1);
+    qtest_quit(qts0);
+}
+
+static void test_dgram_mcast(void)
+{
+    QTestState *qts;
+
+    qts = qtest_initf("-nodefaults -M none "
+                      "-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);
+
+    qtest_quit(qts);
+}
+
+static void test_dgram_unix(void)
+{
+    QTestState *qts0, *qts1;
+    char *expect;
+    gchar *path0, *path1;
+
+    path0 = g_strconcat(tmpdir, "/dgram_unix0", NULL);
+    path1 = g_strconcat(tmpdir, "/dgram_unix1", NULL);
+
+    qts0 = qtest_initf("-nodefaults -M none "
+                       "-netdev dgram,id=st0,local.type=unix,local.path=%s,"
+                       "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);
+    g_free(expect);
+
+    qts1 = qtest_initf("-nodefaults -M none "
+                       "-netdev dgram,id=st0,local.type=unix,local.path=%s,"
+                       "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);
+    g_free(expect);
+
+    unlink(path0);
+    g_free(path0);
+    unlink(path1);
+    g_free(path1);
+
+    qtest_quit(qts1);
+    qtest_quit(qts0);
+}
+
+#ifndef _WIN32
+static void test_dgram_fd(void)
+{
+    QTestState *qts0, *qts1;
+    char *expect;
+    int ret;
+    int sv[2];
+
+    ret = socketpair(PF_UNIX, SOCK_DGRAM, 0, sv);
+    g_assert_cmpint(ret, !=, -1);
+
+    qts0 = qtest_initf("-nodefaults -M none "
+                       "-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);
+    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);
+    g_free(expect);
+
+    qtest_quit(qts1);
+    qtest_quit(qts0);
+
+    closesocket(sv[0]);
+    closesocket(sv[1]);
+}
+#endif
+
+int main(int argc, char **argv)
+{
+    int ret;
+    bool has_ipv4, has_ipv6, has_afunix;
+    g_autoptr(GError) err = NULL;
+
+    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;
+    }
+
+    tmpdir = g_dir_make_tmp("netdev-socket.XXXXXX", &err);
+    if (tmpdir == NULL) {
+        g_error("Can't create temporary directory in %s: %s",
+                g_get_tmp_dir(), err->message);
+    }
+
+    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);
+#ifdef CONFIG_LINUX
+        qtest_add_func("/netdev/stream/unix/abstract",
+                       test_stream_unix_abstract);
+#endif
+#ifndef _WIN32
+        qtest_add_func("/netdev/stream/fd", test_stream_fd);
+        qtest_add_func("/netdev/dgram/fd", test_dgram_fd);
+#endif
+    }
+
+end:
+    ret = g_test_run();
+
+    g_rmdir(tmpdir);
+    g_free(tmpdir);
+
+    return ret;
+}