aboutsummaryrefslogtreecommitdiff
path: root/tests/qtest/test-query-netdev.c
blob: 1118537a9fa59a444984e7520309ca6ad4ec5361 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * QTest testcase for the query-netdev
 *
 * Copyright Yandex N.V., 2019
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 *
 */

#include "qemu/osdep.h"

#include "libqos/libqtest.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qlist.h"

/*
 * Events can get in the way of responses we are actually waiting for.
 */
GCC_FMT_ATTR(2, 3)
static QObject *wait_command(QTestState *who, const char *command, ...)
{
    va_list ap;
    QDict *response;
    QObject *result;

    va_start(ap, command);
    qtest_qmp_vsend(who, command, ap);
    va_end(ap);

    response = qtest_qmp_receive(who);

    result = qdict_get(response, "return");
    g_assert(result);
    qobject_ref(result);
    qobject_unref(response);

    return result;
}

static void qmp_query_netdev_no_error(QTestState *qts, size_t netdevs_count)
{
    QObject *resp;
    QList *netdevs;

    resp = wait_command(qts, "{'execute': 'query-netdev'}");

    netdevs = qobject_to(QList, resp);
    g_assert(netdevs);
    g_assert(qlist_size(netdevs) == netdevs_count);

    qobject_unref(resp);
}

static void test_query_netdev(void)
{
    const char *arch = qtest_get_arch();
    QObject *resp;
    QTestState *state;

    /* Choosing machine for platforms without default one */
    if (g_str_equal(arch, "arm") ||
        g_str_equal(arch, "aarch64")) {
        state = qtest_init(
            "-nodefaults "
            "-M virt "
            "-netdev user,id=slirp0");
    } else if (g_str_equal(arch, "tricore")) {
        state = qtest_init(
            "-nodefaults "
            "-M tricore_testboard "
            "-netdev user,id=slirp0");
    } else if (g_str_equal(arch, "avr")) {
        state = qtest_init(
            "-nodefaults "
            "-M mega2560 "
            "-netdev user,id=slirp0");
    } else if (g_str_equal(arch, "rx")) {
        state = qtest_init(
            "-nodefaults "
            "-M gdbsim-r5f562n8 "
            "-netdev user,id=slirp0");
    } else {
        state = qtest_init(
            "-nodefaults "
            "-netdev user,id=slirp0");
    }
    g_assert(state);

    qmp_query_netdev_no_error(state, 1);

    resp = wait_command(state,
        "{'execute': 'netdev_add', 'arguments': {"
        " 'id': 'slirp1',"
        " 'type': 'user'}}");
    qobject_unref(resp);

    qmp_query_netdev_no_error(state, 2);

    resp = wait_command(state,
        "{'execute': 'netdev_del', 'arguments': {"
        " 'id': 'slirp1'}}");
    qobject_unref(resp);

    qmp_query_netdev_no_error(state, 1);

    qtest_quit(state);
}

int main(int argc, char **argv)
{
    int ret = 0;
    g_test_init(&argc, &argv, NULL);

    qtest_add_func("/net/qapi/query_netdev", test_query_netdev);

    ret = g_test_run();

    return ret;
}