aboutsummaryrefslogtreecommitdiff
path: root/tests/qtest/qmp-cmd-test.c
blob: 0d52ea6c4bc002bc5a84f6209c816f97d33439c1 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
 * QMP command test cases
 *
 * Copyright (c) 2017 Red Hat Inc.
 *
 * Authors:
 *  Markus Armbruster <armbru@redhat.com>
 *
 * 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/error.h"
#include "qapi/qapi-visit-introspect.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qobject-input-visitor.h"

const char common_args[] = "-nodefaults -machine none";

/* Query smoke tests */

static int query_error_class(const char *cmd)
{
    static struct {
        const char *cmd;
        int err_class;
    } fails[] = {
        /* Success depends on build configuration: */
#ifndef CONFIG_SPICE
        { "query-spice", ERROR_CLASS_COMMAND_NOT_FOUND },
#endif
#ifndef CONFIG_TCG
        { "query-replay", ERROR_CLASS_COMMAND_NOT_FOUND },
#endif
#ifndef CONFIG_VNC
        { "query-vnc", ERROR_CLASS_GENERIC_ERROR },
        { "query-vnc-servers", ERROR_CLASS_GENERIC_ERROR },
#endif
#ifndef CONFIG_REPLICATION
        { "query-xen-replication-status", ERROR_CLASS_COMMAND_NOT_FOUND },
#endif
        /* Likewise, and require special QEMU command-line arguments: */
        { "query-acpi-ospm-status", ERROR_CLASS_GENERIC_ERROR },
        { "query-balloon", ERROR_CLASS_DEVICE_NOT_ACTIVE },
        { "query-hotpluggable-cpus", ERROR_CLASS_GENERIC_ERROR },
        { "query-vm-generation-id", ERROR_CLASS_GENERIC_ERROR },
#ifndef CONFIG_PROFILER
        { "x-query-profile", ERROR_CLASS_GENERIC_ERROR },
#endif
        /* Only valid with a USB bus added */
        { "x-query-usb", ERROR_CLASS_GENERIC_ERROR },
        { NULL, -1 }
    };
    int i;

    for (i = 0; fails[i].cmd; i++) {
        if (!strcmp(cmd, fails[i].cmd)) {
            return fails[i].err_class;
        }
    }
    return -1;
}

static void test_query(const void *data)
{
    const char *cmd = data;
    int expected_error_class = query_error_class(cmd);
    QDict *resp, *error;
    const char *error_class;
    QTestState *qts;

    qts = qtest_init(common_args);

    resp = qtest_qmp(qts, "{ 'execute': %s }", cmd);
    error = qdict_get_qdict(resp, "error");
    error_class = error ? qdict_get_str(error, "class") : NULL;

    if (expected_error_class < 0) {
        g_assert(qdict_haskey(resp, "return"));
    } else {
        g_assert(error);
        g_assert_cmpint(qapi_enum_parse(&QapiErrorClass_lookup, error_class,
                                        -1, &error_abort),
                        ==, expected_error_class);
    }
    qobject_unref(resp);

    qtest_quit(qts);
}

static bool query_is_ignored(const char *cmd)
{
    const char *ignored[] = {
        /* Not actually queries: */
        "add-fd",
        /* Success depends on target arch: */
        "query-cpu-definitions",  /* arm, i386, ppc, s390x */
        "query-gic-capabilities", /* arm */
        /* Success depends on target-specific build configuration: */
        "query-pci",              /* CONFIG_PCI */
        /* Success depends on launching SEV guest */
        "query-sev-launch-measure",
        /* Success depends on Host or Hypervisor SEV support */
        "query-sev",
        "query-sev-capabilities",
        "query-sgx",
        "query-sgx-capabilities",
        NULL
    };
    int i;

    for (i = 0; ignored[i]; i++) {
        if (!strcmp(cmd, ignored[i])) {
            return true;
        }
    }
    return false;
}

typedef struct {
    SchemaInfoList *list;
    GHashTable *hash;
} QmpSchema;

static void qmp_schema_init(QmpSchema *schema)
{
    QDict *resp;
    Visitor *qiv;
    SchemaInfoList *tail;
    QTestState *qts;

    qts = qtest_init(common_args);

    resp = qtest_qmp(qts, "{ 'execute': 'query-qmp-schema' }");

    qiv = qobject_input_visitor_new(qdict_get(resp, "return"));
    visit_type_SchemaInfoList(qiv, NULL, &schema->list, &error_abort);
    visit_free(qiv);

    qobject_unref(resp);
    qtest_quit(qts);

    schema->hash = g_hash_table_new(g_str_hash, g_str_equal);

    /* Build @schema: hash table mapping entity name to SchemaInfo */
    for (tail = schema->list; tail; tail = tail->next) {
        g_hash_table_insert(schema->hash, tail->value->name, tail->value);
    }
}

static SchemaInfo *qmp_schema_lookup(QmpSchema *schema, const char *name)
{
    return g_hash_table_lookup(schema->hash, name);
}

static void qmp_schema_cleanup(QmpSchema *schema)
{
    qapi_free_SchemaInfoList(schema->list);
    g_hash_table_destroy(schema->hash);
}

static bool object_type_has_mandatory_members(SchemaInfo *type)
{
    SchemaInfoObjectMemberList *tail;

    g_assert(type->meta_type == SCHEMA_META_TYPE_OBJECT);

    for (tail = type->u.object.members; tail; tail = tail->next) {
        if (!tail->value->has_q_default) {
            return true;
        }
    }

    return false;
}

static void add_query_tests(QmpSchema *schema)
{
    SchemaInfoList *tail;
    SchemaInfo *si, *arg_type, *ret_type;
    char *test_name;

    /* Test the query-like commands */
    for (tail = schema->list; tail; tail = tail->next) {
        si = tail->value;
        if (si->meta_type != SCHEMA_META_TYPE_COMMAND) {
            continue;
        }

        if (query_is_ignored(si->name)) {
            continue;
        }

        arg_type = qmp_schema_lookup(schema, si->u.command.arg_type);
        if (object_type_has_mandatory_members(arg_type)) {
            continue;
        }

        ret_type = qmp_schema_lookup(schema, si->u.command.ret_type);
        if (ret_type->meta_type == SCHEMA_META_TYPE_OBJECT
            && !ret_type->u.object.members) {
            continue;
        }

        test_name = g_strdup_printf("qmp/%s", si->name);
        qtest_add_data_func(test_name, si->name, test_query);
        g_free(test_name);
    }
}

static void test_object_add_failure_modes(void)
{
    QTestState *qts;
    QDict *resp;

    /* attempt to create an object without props */
    qts = qtest_init(common_args);
    resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
                     " {'qom-type': 'memory-backend-ram', 'id': 'ram1' } }");
    g_assert_nonnull(resp);
    qmp_expect_error_and_unref(resp, "GenericError");

    /* attempt to create an object without qom-type */
    resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
                     " {'id': 'ram1' } }");
    g_assert_nonnull(resp);
    qmp_expect_error_and_unref(resp, "GenericError");

    /* attempt to delete an object that does not exist */
    resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
                     " {'id': 'ram1' } }");
    g_assert_nonnull(resp);
    qmp_expect_error_and_unref(resp, "GenericError");

    /* attempt to create 2 objects with duplicate id */
    resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
                     " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
                     " 'size': 1048576 } }");
    g_assert_nonnull(resp);
    g_assert(qdict_haskey(resp, "return"));
    qobject_unref(resp);

    resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
                     " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
                     " 'size': 1048576 } }");
    g_assert_nonnull(resp);
    qmp_expect_error_and_unref(resp, "GenericError");

    /* delete ram1 object */
    resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
                     " {'id': 'ram1' } }");
    g_assert_nonnull(resp);
    g_assert(qdict_haskey(resp, "return"));
    qobject_unref(resp);

    /* attempt to create an object with a property of a wrong type */
    resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
                     " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
                     " 'size': '1048576' } }");
    g_assert_nonnull(resp);
    /* now do it right */
    qmp_expect_error_and_unref(resp, "GenericError");

    resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
                     " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
                     " 'size': 1048576 } }");
    g_assert_nonnull(resp);
    g_assert(qdict_haskey(resp, "return"));
    qobject_unref(resp);

    /* delete ram1 object */
    resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
                     " {'id': 'ram1' } }");
    g_assert_nonnull(resp);
    g_assert(qdict_haskey(resp, "return"));
    qobject_unref(resp);

    /* attempt to create an object without the id */
    resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
                     " {'qom-type': 'memory-backend-ram',"
                     " 'size': 1048576 } }");
    g_assert_nonnull(resp);
    qmp_expect_error_and_unref(resp, "GenericError");

    /* now do it right */
    resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
                     " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
                     " 'size': 1048576 } }");
    g_assert_nonnull(resp);
    g_assert(qdict_haskey(resp, "return"));
    qobject_unref(resp);

    /* delete ram1 object */
    resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
                     " {'id': 'ram1' } }");
    g_assert_nonnull(resp);
    g_assert(qdict_haskey(resp, "return"));
    qobject_unref(resp);

    /* attempt to set a non existing property */
    resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
                     " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
                     " 'sized': 1048576 } }");
    g_assert_nonnull(resp);
    qmp_expect_error_and_unref(resp, "GenericError");

    /* now do it right */
    resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
                     " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
                     " 'size': 1048576 } }");
    g_assert_nonnull(resp);
    g_assert(qdict_haskey(resp, "return"));
    qobject_unref(resp);

    /* delete ram1 object without id */
    resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
                     " {'ida': 'ram1' } }");
    g_assert_nonnull(resp);
    qobject_unref(resp);

    /* delete ram1 object */
    resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
                     " {'id': 'ram1' } }");
    g_assert_nonnull(resp);
    g_assert(qdict_haskey(resp, "return"));
    qobject_unref(resp);

    /* delete ram1 object that does not exist anymore*/
    resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
                     " {'id': 'ram1' } }");
    g_assert_nonnull(resp);
    qmp_expect_error_and_unref(resp, "GenericError");

    qtest_quit(qts);
}

int main(int argc, char *argv[])
{
    QmpSchema schema;
    int ret;

    g_test_init(&argc, &argv, NULL);

    qmp_schema_init(&schema);
    add_query_tests(&schema);

    qtest_add_func("qmp/object-add-failure-modes",
                   test_object_add_failure_modes);

    ret = g_test_run();

    qmp_schema_cleanup(&schema);
    return ret;
}