]> Git Repo - qemu.git/blob - tests/qmp-test.c
scripts: Add decodetree.py
[qemu.git] / tests / qmp-test.c
1 /*
2  * QMP protocol test cases
3  *
4  * Copyright (c) 2017 Red Hat Inc.
5  *
6  * Authors:
7  *  Markus Armbruster <[email protected]>,
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or later.
10  * See the COPYING file in the top-level directory.
11  */
12
13 #include "qemu/osdep.h"
14 #include "libqtest.h"
15 #include "qapi-visit.h"
16 #include "qapi/error.h"
17 #include "qapi/qmp/qdict.h"
18 #include "qapi/qmp/qlist.h"
19 #include "qapi/qobject-input-visitor.h"
20 #include "qapi/util.h"
21 #include "qapi/visitor.h"
22
23 const char common_args[] = "-nodefaults -machine none";
24
25 static const char *get_error_class(QDict *resp)
26 {
27     QDict *error = qdict_get_qdict(resp, "error");
28     const char *desc = qdict_get_try_str(error, "desc");
29
30     g_assert(desc);
31     return error ? qdict_get_try_str(error, "class") : NULL;
32 }
33
34 static void test_version(QObject *version)
35 {
36     Visitor *v;
37     VersionInfo *vinfo;
38
39     g_assert(version);
40     v = qobject_input_visitor_new(version);
41     visit_type_VersionInfo(v, "version", &vinfo, &error_abort);
42     qapi_free_VersionInfo(vinfo);
43     visit_free(v);
44 }
45
46 static void test_malformed(QTestState *qts)
47 {
48     QDict *resp;
49
50     /* Not even a dictionary */
51     resp = qtest_qmp(qts, "null");
52     g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
53     QDECREF(resp);
54
55     /* No "execute" key */
56     resp = qtest_qmp(qts, "{}");
57     g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
58     QDECREF(resp);
59
60     /* "execute" isn't a string */
61     resp = qtest_qmp(qts, "{ 'execute': true }");
62     g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
63     QDECREF(resp);
64
65     /* "arguments" isn't a dictionary */
66     resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'arguments': [] }");
67     g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
68     QDECREF(resp);
69
70     /* extra key */
71     resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'extra': true }");
72     g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
73     QDECREF(resp);
74 }
75
76 static void test_qmp_protocol(void)
77 {
78     QDict *resp, *q, *ret;
79     QList *capabilities;
80     QTestState *qts;
81
82     qts = qtest_init_without_qmp_handshake(common_args);
83
84     /* Test greeting */
85     resp = qtest_qmp_receive(qts);
86     q = qdict_get_qdict(resp, "QMP");
87     g_assert(q);
88     test_version(qdict_get(q, "version"));
89     capabilities = qdict_get_qlist(q, "capabilities");
90     g_assert(capabilities && qlist_empty(capabilities));
91     QDECREF(resp);
92
93     /* Test valid command before handshake */
94     resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
95     g_assert_cmpstr(get_error_class(resp), ==, "CommandNotFound");
96     QDECREF(resp);
97
98     /* Test malformed commands before handshake */
99     test_malformed(qts);
100
101     /* Test handshake */
102     resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
103     ret = qdict_get_qdict(resp, "return");
104     g_assert(ret && !qdict_size(ret));
105     QDECREF(resp);
106
107     /* Test repeated handshake */
108     resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
109     g_assert_cmpstr(get_error_class(resp), ==, "CommandNotFound");
110     QDECREF(resp);
111
112     /* Test valid command */
113     resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
114     test_version(qdict_get(resp, "return"));
115     QDECREF(resp);
116
117     /* Test malformed commands */
118     test_malformed(qts);
119
120     /* Test 'id' */
121     resp = qtest_qmp(qts, "{ 'execute': 'query-name', 'id': 'cookie#1' }");
122     ret = qdict_get_qdict(resp, "return");
123     g_assert(ret);
124     g_assert_cmpstr(qdict_get_try_str(resp, "id"), ==, "cookie#1");
125     QDECREF(resp);
126
127     /* Test command failure with 'id' */
128     resp = qtest_qmp(qts, "{ 'execute': 'human-monitor-command', 'id': 2 }");
129     g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
130     g_assert_cmpint(qdict_get_int(resp, "id"), ==, 2);
131     QDECREF(resp);
132
133     qtest_quit(qts);
134 }
135
136 static int query_error_class(const char *cmd)
137 {
138     static struct {
139         const char *cmd;
140         int err_class;
141     } fails[] = {
142         /* Success depends on build configuration: */
143 #ifndef CONFIG_SPICE
144         { "query-spice", ERROR_CLASS_COMMAND_NOT_FOUND },
145 #endif
146 #ifndef CONFIG_VNC
147         { "query-vnc", ERROR_CLASS_GENERIC_ERROR },
148         { "query-vnc-servers", ERROR_CLASS_GENERIC_ERROR },
149 #endif
150 #ifndef CONFIG_REPLICATION
151         { "query-xen-replication-status", ERROR_CLASS_COMMAND_NOT_FOUND },
152 #endif
153         /* Likewise, and require special QEMU command-line arguments: */
154         { "query-acpi-ospm-status", ERROR_CLASS_GENERIC_ERROR },
155         { "query-balloon", ERROR_CLASS_DEVICE_NOT_ACTIVE },
156         { "query-hotpluggable-cpus", ERROR_CLASS_GENERIC_ERROR },
157         { "query-vm-generation-id", ERROR_CLASS_GENERIC_ERROR },
158         { NULL, -1 }
159     };
160     int i;
161
162     for (i = 0; fails[i].cmd; i++) {
163         if (!strcmp(cmd, fails[i].cmd)) {
164             return fails[i].err_class;
165         }
166     }
167     return -1;
168 }
169
170 static void test_query(const void *data)
171 {
172     const char *cmd = data;
173     int expected_error_class = query_error_class(cmd);
174     QDict *resp, *error;
175     const char *error_class;
176
177     qtest_start(common_args);
178
179     resp = qmp("{ 'execute': %s }", cmd);
180     error = qdict_get_qdict(resp, "error");
181     error_class = error ? qdict_get_str(error, "class") : NULL;
182
183     if (expected_error_class < 0) {
184         g_assert(qdict_haskey(resp, "return"));
185     } else {
186         g_assert(error);
187         g_assert_cmpint(qapi_enum_parse(&QapiErrorClass_lookup, error_class,
188                                         -1, &error_abort),
189                         ==, expected_error_class);
190     }
191     QDECREF(resp);
192
193     qtest_end();
194 }
195
196 static bool query_is_blacklisted(const char *cmd)
197 {
198     const char *blacklist[] = {
199         /* Not actually queries: */
200         "add-fd",
201         /* Success depends on target arch: */
202         "query-cpu-definitions",  /* arm, i386, ppc, s390x */
203         "query-gic-capabilities", /* arm */
204         /* Success depends on target-specific build configuration: */
205         "query-pci",              /* CONFIG_PCI */
206         NULL
207     };
208     int i;
209
210     for (i = 0; blacklist[i]; i++) {
211         if (!strcmp(cmd, blacklist[i])) {
212             return true;
213         }
214     }
215     return false;
216 }
217
218 typedef struct {
219     SchemaInfoList *list;
220     GHashTable *hash;
221 } QmpSchema;
222
223 static void qmp_schema_init(QmpSchema *schema)
224 {
225     QDict *resp;
226     Visitor *qiv;
227     SchemaInfoList *tail;
228
229     qtest_start(common_args);
230     resp = qmp("{ 'execute': 'query-qmp-schema' }");
231
232     qiv = qobject_input_visitor_new(qdict_get(resp, "return"));
233     visit_type_SchemaInfoList(qiv, NULL, &schema->list, &error_abort);
234     visit_free(qiv);
235
236     QDECREF(resp);
237     qtest_end();
238
239     schema->hash = g_hash_table_new(g_str_hash, g_str_equal);
240
241     /* Build @schema: hash table mapping entity name to SchemaInfo */
242     for (tail = schema->list; tail; tail = tail->next) {
243         g_hash_table_insert(schema->hash, tail->value->name, tail->value);
244     }
245 }
246
247 static SchemaInfo *qmp_schema_lookup(QmpSchema *schema, const char *name)
248 {
249     return g_hash_table_lookup(schema->hash, name);
250 }
251
252 static void qmp_schema_cleanup(QmpSchema *schema)
253 {
254     qapi_free_SchemaInfoList(schema->list);
255     g_hash_table_destroy(schema->hash);
256 }
257
258 static bool object_type_has_mandatory_members(SchemaInfo *type)
259 {
260     SchemaInfoObjectMemberList *tail;
261
262     g_assert(type->meta_type == SCHEMA_META_TYPE_OBJECT);
263
264     for (tail = type->u.object.members; tail; tail = tail->next) {
265         if (!tail->value->has_q_default) {
266             return true;
267         }
268     }
269
270     return false;
271 }
272
273 static void add_query_tests(QmpSchema *schema)
274 {
275     SchemaInfoList *tail;
276     SchemaInfo *si, *arg_type, *ret_type;
277     char *test_name;
278
279     /* Test the query-like commands */
280     for (tail = schema->list; tail; tail = tail->next) {
281         si = tail->value;
282         if (si->meta_type != SCHEMA_META_TYPE_COMMAND) {
283             continue;
284         }
285
286         if (query_is_blacklisted(si->name)) {
287             continue;
288         }
289
290         arg_type = qmp_schema_lookup(schema, si->u.command.arg_type);
291         if (object_type_has_mandatory_members(arg_type)) {
292             continue;
293         }
294
295         ret_type = qmp_schema_lookup(schema, si->u.command.ret_type);
296         if (ret_type->meta_type == SCHEMA_META_TYPE_OBJECT
297             && !ret_type->u.object.members) {
298             continue;
299         }
300
301         test_name = g_strdup_printf("qmp/%s", si->name);
302         qtest_add_data_func(test_name, si->name, test_query);
303         g_free(test_name);
304     }
305 }
306
307 int main(int argc, char *argv[])
308 {
309     QmpSchema schema;
310     int ret;
311
312     g_test_init(&argc, &argv, NULL);
313
314     qtest_add_func("qmp/protocol", test_qmp_protocol);
315     qmp_schema_init(&schema);
316     add_query_tests(&schema);
317
318     ret = g_test_run();
319
320     qmp_schema_cleanup(&schema);
321     return ret;
322 }
This page took 0.042364 seconds and 4 git commands to generate.