]> git.proxmox.com Git - mirror_qemu.git/blob - tests/qmp-test.c
Revert "tests: qmp-test: add oob test"
[mirror_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 <armbru@redhat.com>,
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/error.h"
16 #include "qapi/qapi-visit-introspect.h"
17 #include "qapi/qapi-visit-misc.h"
18 #include "qapi/qmp/qdict.h"
19 #include "qapi/qmp/qlist.h"
20 #include "qapi/qobject-input-visitor.h"
21 #include "qapi/util.h"
22 #include "qapi/visitor.h"
23 #include "qapi/qmp/qstring.h"
24
25 const char common_args[] = "-nodefaults -machine none";
26
27 static const char *get_error_class(QDict *resp)
28 {
29 QDict *error = qdict_get_qdict(resp, "error");
30 const char *desc = qdict_get_try_str(error, "desc");
31
32 g_assert(desc);
33 return error ? qdict_get_try_str(error, "class") : NULL;
34 }
35
36 static void test_version(QObject *version)
37 {
38 Visitor *v;
39 VersionInfo *vinfo;
40
41 g_assert(version);
42 v = qobject_input_visitor_new(version);
43 visit_type_VersionInfo(v, "version", &vinfo, &error_abort);
44 qapi_free_VersionInfo(vinfo);
45 visit_free(v);
46 }
47
48 static void test_malformed(QTestState *qts)
49 {
50 QDict *resp;
51
52 /* Not even a dictionary */
53 resp = qtest_qmp(qts, "null");
54 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
55 QDECREF(resp);
56
57 /* No "execute" key */
58 resp = qtest_qmp(qts, "{}");
59 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
60 QDECREF(resp);
61
62 /* "execute" isn't a string */
63 resp = qtest_qmp(qts, "{ 'execute': true }");
64 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
65 QDECREF(resp);
66
67 /* "arguments" isn't a dictionary */
68 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'arguments': [] }");
69 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
70 QDECREF(resp);
71
72 /* extra key */
73 resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'extra': true }");
74 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
75 QDECREF(resp);
76 }
77
78 static void test_qmp_protocol(void)
79 {
80 QDict *resp, *q, *ret;
81 QList *capabilities;
82 QTestState *qts;
83 const QListEntry *entry;
84 QString *qstr;
85 int i;
86
87 qts = qtest_init_without_qmp_handshake(common_args);
88
89 /* Test greeting */
90 resp = qtest_qmp_receive(qts);
91 q = qdict_get_qdict(resp, "QMP");
92 g_assert(q);
93 test_version(qdict_get(q, "version"));
94 capabilities = qdict_get_qlist(q, "capabilities");
95 g_assert(capabilities);
96 entry = qlist_first(capabilities);
97 g_assert(entry);
98 qstr = qobject_to(QString, entry->value);
99 g_assert(qstr);
100 g_assert_cmpstr(qstring_get_str(qstr), ==, "oob");
101 QDECREF(resp);
102
103 /* Test valid command before handshake */
104 resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
105 g_assert_cmpstr(get_error_class(resp), ==, "CommandNotFound");
106 QDECREF(resp);
107
108 /* Test malformed commands before handshake */
109 test_malformed(qts);
110
111 /* Test handshake */
112 resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
113 ret = qdict_get_qdict(resp, "return");
114 g_assert(ret && !qdict_size(ret));
115 QDECREF(resp);
116
117 /* Test repeated handshake */
118 resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
119 g_assert_cmpstr(get_error_class(resp), ==, "CommandNotFound");
120 QDECREF(resp);
121
122 /* Test valid command */
123 resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
124 test_version(qdict_get(resp, "return"));
125 QDECREF(resp);
126
127 /* Test malformed commands */
128 test_malformed(qts);
129
130 /* Test 'id' */
131 resp = qtest_qmp(qts, "{ 'execute': 'query-name', 'id': 'cookie#1' }");
132 ret = qdict_get_qdict(resp, "return");
133 g_assert(ret);
134 g_assert_cmpstr(qdict_get_try_str(resp, "id"), ==, "cookie#1");
135 QDECREF(resp);
136
137 /* Test command failure with 'id' */
138 resp = qtest_qmp(qts, "{ 'execute': 'human-monitor-command', 'id': 2 }");
139 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
140 g_assert_cmpint(qdict_get_int(resp, "id"), ==, 2);
141 QDECREF(resp);
142
143 /*
144 * Test command batching. In current test OOB is not enabled, we
145 * should be able to run as many commands in batch as we like.
146 * Using 16 (>8, which is OOB queue length) to make sure OOB won't
147 * break existing clients. Note: this test does not control the
148 * scheduling of QEMU's QMP command processing threads so it may
149 * not really trigger batching inside QEMU. This is just a
150 * best-effort test.
151 */
152 for (i = 0; i < 16; i++) {
153 qtest_async_qmp(qts, "{ 'execute': 'query-version' }");
154 }
155 /* Verify the replies to make sure no command is dropped. */
156 for (i = 0; i < 16; i++) {
157 resp = qtest_qmp_receive(qts);
158 /* It should never be dropped. Each of them should be a reply. */
159 g_assert(qdict_haskey(resp, "return"));
160 g_assert(!qdict_haskey(resp, "event"));
161 QDECREF(resp);
162 }
163
164 qtest_quit(qts);
165 }
166
167 static int query_error_class(const char *cmd)
168 {
169 static struct {
170 const char *cmd;
171 int err_class;
172 } fails[] = {
173 /* Success depends on build configuration: */
174 #ifndef CONFIG_SPICE
175 { "query-spice", ERROR_CLASS_COMMAND_NOT_FOUND },
176 #endif
177 #ifndef CONFIG_VNC
178 { "query-vnc", ERROR_CLASS_GENERIC_ERROR },
179 { "query-vnc-servers", ERROR_CLASS_GENERIC_ERROR },
180 #endif
181 #ifndef CONFIG_REPLICATION
182 { "query-xen-replication-status", ERROR_CLASS_COMMAND_NOT_FOUND },
183 #endif
184 /* Likewise, and require special QEMU command-line arguments: */
185 { "query-acpi-ospm-status", ERROR_CLASS_GENERIC_ERROR },
186 { "query-balloon", ERROR_CLASS_DEVICE_NOT_ACTIVE },
187 { "query-hotpluggable-cpus", ERROR_CLASS_GENERIC_ERROR },
188 { "query-vm-generation-id", ERROR_CLASS_GENERIC_ERROR },
189 { NULL, -1 }
190 };
191 int i;
192
193 for (i = 0; fails[i].cmd; i++) {
194 if (!strcmp(cmd, fails[i].cmd)) {
195 return fails[i].err_class;
196 }
197 }
198 return -1;
199 }
200
201 static void test_query(const void *data)
202 {
203 const char *cmd = data;
204 int expected_error_class = query_error_class(cmd);
205 QDict *resp, *error;
206 const char *error_class;
207
208 qtest_start(common_args);
209
210 resp = qmp("{ 'execute': %s }", cmd);
211 error = qdict_get_qdict(resp, "error");
212 error_class = error ? qdict_get_str(error, "class") : NULL;
213
214 if (expected_error_class < 0) {
215 g_assert(qdict_haskey(resp, "return"));
216 } else {
217 g_assert(error);
218 g_assert_cmpint(qapi_enum_parse(&QapiErrorClass_lookup, error_class,
219 -1, &error_abort),
220 ==, expected_error_class);
221 }
222 QDECREF(resp);
223
224 qtest_end();
225 }
226
227 static bool query_is_blacklisted(const char *cmd)
228 {
229 const char *blacklist[] = {
230 /* Not actually queries: */
231 "add-fd",
232 /* Success depends on target arch: */
233 "query-cpu-definitions", /* arm, i386, ppc, s390x */
234 "query-gic-capabilities", /* arm */
235 /* Success depends on target-specific build configuration: */
236 "query-pci", /* CONFIG_PCI */
237 /* Success depends on launching SEV guest */
238 "query-sev-launch-measure",
239 /* Success depends on Host or Hypervisor SEV support */
240 "query-sev",
241 "query-sev-capabilities",
242 NULL
243 };
244 int i;
245
246 for (i = 0; blacklist[i]; i++) {
247 if (!strcmp(cmd, blacklist[i])) {
248 return true;
249 }
250 }
251 return false;
252 }
253
254 typedef struct {
255 SchemaInfoList *list;
256 GHashTable *hash;
257 } QmpSchema;
258
259 static void qmp_schema_init(QmpSchema *schema)
260 {
261 QDict *resp;
262 Visitor *qiv;
263 SchemaInfoList *tail;
264
265 qtest_start(common_args);
266 resp = qmp("{ 'execute': 'query-qmp-schema' }");
267
268 qiv = qobject_input_visitor_new(qdict_get(resp, "return"));
269 visit_type_SchemaInfoList(qiv, NULL, &schema->list, &error_abort);
270 visit_free(qiv);
271
272 QDECREF(resp);
273 qtest_end();
274
275 schema->hash = g_hash_table_new(g_str_hash, g_str_equal);
276
277 /* Build @schema: hash table mapping entity name to SchemaInfo */
278 for (tail = schema->list; tail; tail = tail->next) {
279 g_hash_table_insert(schema->hash, tail->value->name, tail->value);
280 }
281 }
282
283 static SchemaInfo *qmp_schema_lookup(QmpSchema *schema, const char *name)
284 {
285 return g_hash_table_lookup(schema->hash, name);
286 }
287
288 static void qmp_schema_cleanup(QmpSchema *schema)
289 {
290 qapi_free_SchemaInfoList(schema->list);
291 g_hash_table_destroy(schema->hash);
292 }
293
294 static bool object_type_has_mandatory_members(SchemaInfo *type)
295 {
296 SchemaInfoObjectMemberList *tail;
297
298 g_assert(type->meta_type == SCHEMA_META_TYPE_OBJECT);
299
300 for (tail = type->u.object.members; tail; tail = tail->next) {
301 if (!tail->value->has_q_default) {
302 return true;
303 }
304 }
305
306 return false;
307 }
308
309 static void add_query_tests(QmpSchema *schema)
310 {
311 SchemaInfoList *tail;
312 SchemaInfo *si, *arg_type, *ret_type;
313 char *test_name;
314
315 /* Test the query-like commands */
316 for (tail = schema->list; tail; tail = tail->next) {
317 si = tail->value;
318 if (si->meta_type != SCHEMA_META_TYPE_COMMAND) {
319 continue;
320 }
321
322 if (query_is_blacklisted(si->name)) {
323 continue;
324 }
325
326 arg_type = qmp_schema_lookup(schema, si->u.command.arg_type);
327 if (object_type_has_mandatory_members(arg_type)) {
328 continue;
329 }
330
331 ret_type = qmp_schema_lookup(schema, si->u.command.ret_type);
332 if (ret_type->meta_type == SCHEMA_META_TYPE_OBJECT
333 && !ret_type->u.object.members) {
334 continue;
335 }
336
337 test_name = g_strdup_printf("qmp/%s", si->name);
338 qtest_add_data_func(test_name, si->name, test_query);
339 g_free(test_name);
340 }
341 }
342
343 int main(int argc, char *argv[])
344 {
345 QmpSchema schema;
346 int ret;
347
348 g_test_init(&argc, &argv, NULL);
349
350 qtest_add_func("qmp/protocol", test_qmp_protocol);
351 qmp_schema_init(&schema);
352 add_query_tests(&schema);
353
354 ret = g_test_run();
355
356 qmp_schema_cleanup(&schema);
357 return ret;
358 }