2 * QMP protocol test cases
4 * Copyright (c) 2017 Red Hat Inc.
7 * Markus Armbruster <armbru@redhat.com>,
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.
13 #include "qemu/osdep.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"
25 const char common_args
[] = "-nodefaults -machine none";
27 static const char *get_error_class(QDict
*resp
)
29 QDict
*error
= qdict_get_qdict(resp
, "error");
30 const char *desc
= qdict_get_try_str(error
, "desc");
33 return error
? qdict_get_try_str(error
, "class") : NULL
;
36 static void test_version(QObject
*version
)
42 v
= qobject_input_visitor_new(version
);
43 visit_type_VersionInfo(v
, "version", &vinfo
, &error_abort
);
44 qapi_free_VersionInfo(vinfo
);
48 static void test_malformed(QTestState
*qts
)
52 /* Not even a dictionary */
53 resp
= qtest_qmp(qts
, "null");
54 g_assert_cmpstr(get_error_class(resp
), ==, "GenericError");
57 /* No "execute" key */
58 resp
= qtest_qmp(qts
, "{}");
59 g_assert_cmpstr(get_error_class(resp
), ==, "GenericError");
62 /* "execute" isn't a string */
63 resp
= qtest_qmp(qts
, "{ 'execute': true }");
64 g_assert_cmpstr(get_error_class(resp
), ==, "GenericError");
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");
73 resp
= qtest_qmp(qts
, "{ 'execute': 'no-such-cmd', 'extra': true }");
74 g_assert_cmpstr(get_error_class(resp
), ==, "GenericError");
78 static void test_qmp_protocol(void)
80 QDict
*resp
, *q
, *ret
;
84 qts
= qtest_init_without_qmp_handshake(false, common_args
);
87 resp
= qtest_qmp_receive(qts
);
88 q
= qdict_get_qdict(resp
, "QMP");
90 test_version(qdict_get(q
, "version"));
91 capabilities
= qdict_get_qlist(q
, "capabilities");
92 g_assert(capabilities
&& qlist_empty(capabilities
));
95 /* Test valid command before handshake */
96 resp
= qtest_qmp(qts
, "{ 'execute': 'query-version' }");
97 g_assert_cmpstr(get_error_class(resp
), ==, "CommandNotFound");
100 /* Test malformed commands before handshake */
104 resp
= qtest_qmp(qts
, "{ 'execute': 'qmp_capabilities' }");
105 ret
= qdict_get_qdict(resp
, "return");
106 g_assert(ret
&& !qdict_size(ret
));
109 /* Test repeated handshake */
110 resp
= qtest_qmp(qts
, "{ 'execute': 'qmp_capabilities' }");
111 g_assert_cmpstr(get_error_class(resp
), ==, "CommandNotFound");
114 /* Test valid command */
115 resp
= qtest_qmp(qts
, "{ 'execute': 'query-version' }");
116 test_version(qdict_get(resp
, "return"));
119 /* Test malformed commands */
123 resp
= qtest_qmp(qts
, "{ 'execute': 'query-name', 'id': 'cookie#1' }");
124 ret
= qdict_get_qdict(resp
, "return");
126 g_assert_cmpstr(qdict_get_try_str(resp
, "id"), ==, "cookie#1");
129 /* Test command failure with 'id' */
130 resp
= qtest_qmp(qts
, "{ 'execute': 'human-monitor-command', 'id': 2 }");
131 g_assert_cmpstr(get_error_class(resp
), ==, "GenericError");
132 g_assert_cmpint(qdict_get_int(resp
, "id"), ==, 2);
138 /* Out-of-band tests */
140 char tmpdir
[] = "/tmp/qmp-test-XXXXXX";
143 static void setup_blocking_cmd(void)
145 if (!mkdtemp(tmpdir
)) {
146 g_error("mkdtemp: %s", strerror(errno
));
148 fifo_name
= g_strdup_printf("%s/fifo", tmpdir
);
149 if (mkfifo(fifo_name
, 0666)) {
150 g_error("mkfifo: %s", strerror(errno
));
154 static void cleanup_blocking_cmd(void)
160 static void send_cmd_that_blocks(QTestState
*s
, const char *id
)
162 qtest_async_qmp(s
, "{ 'execute': 'blockdev-add', 'id': %s,"
164 " 'driver': 'blkdebug', 'node-name': %s,"
166 " 'image': { 'driver': 'null-co' } } }",
170 static void unblock_blocked_cmd(void)
172 int fd
= open(fifo_name
, O_WRONLY
);
177 static void send_oob_cmd_that_fails(QTestState
*s
, const char *id
)
179 qtest_async_qmp(s
, "{ 'exec-oob': 'migrate-pause', 'id': %s }", id
);
182 static void recv_cmd_id(QTestState
*s
, const char *id
)
184 QDict
*resp
= qtest_qmp_receive(s
);
186 g_assert_cmpstr(qdict_get_try_str(resp
, "id"), ==, id
);
190 static void test_qmp_oob(void)
194 const QListEntry
*entry
;
198 qts
= qtest_init_without_qmp_handshake(true, common_args
);
200 /* Check the greeting message. */
201 resp
= qtest_qmp_receive(qts
);
202 q
= qdict_get_qdict(resp
, "QMP");
204 capabilities
= qdict_get_qlist(q
, "capabilities");
205 g_assert(capabilities
&& !qlist_empty(capabilities
));
206 entry
= qlist_first(capabilities
);
208 qstr
= qobject_to(QString
, entry
->value
);
210 g_assert_cmpstr(qstring_get_str(qstr
), ==, "oob");
213 /* Try a fake capability, it should fail. */
214 resp
= qtest_qmp(qts
,
215 "{ 'execute': 'qmp_capabilities', "
216 " 'arguments': { 'enable': [ 'cap-does-not-exist' ] } }");
217 g_assert(qdict_haskey(resp
, "error"));
220 /* Now, enable OOB in current QMP session, it should succeed. */
221 resp
= qtest_qmp(qts
,
222 "{ 'execute': 'qmp_capabilities', "
223 " 'arguments': { 'enable': [ 'oob' ] } }");
224 g_assert(qdict_haskey(resp
, "return"));
228 * Try any command that does not support OOB but with OOB flag. We
229 * should get failure.
231 resp
= qtest_qmp(qts
, "{ 'exec-oob': 'query-cpus' }");
232 g_assert(qdict_haskey(resp
, "error"));
235 /* OOB command overtakes slow in-band command */
236 setup_blocking_cmd();
237 send_cmd_that_blocks(qts
, "ib-blocks-1");
238 qtest_async_qmp(qts
, "{ 'execute': 'query-name', 'id': 'ib-quick-1' }");
239 send_oob_cmd_that_fails(qts
, "oob-1");
240 recv_cmd_id(qts
, "oob-1");
241 unblock_blocked_cmd();
242 recv_cmd_id(qts
, "ib-blocks-1");
243 recv_cmd_id(qts
, "ib-quick-1");
245 /* Even malformed in-band command fails in-band */
246 send_cmd_that_blocks(qts
, "blocks-2");
247 qtest_async_qmp(qts
, "{ 'id': 'err-2' }");
248 unblock_blocked_cmd();
249 recv_cmd_id(qts
, "blocks-2");
250 recv_cmd_id(qts
, "err-2");
251 cleanup_blocking_cmd();
256 /* Query smoke tests */
258 static int query_error_class(const char *cmd
)
264 /* Success depends on build configuration: */
266 { "query-spice", ERROR_CLASS_COMMAND_NOT_FOUND
},
269 { "query-vnc", ERROR_CLASS_GENERIC_ERROR
},
270 { "query-vnc-servers", ERROR_CLASS_GENERIC_ERROR
},
272 #ifndef CONFIG_REPLICATION
273 { "query-xen-replication-status", ERROR_CLASS_COMMAND_NOT_FOUND
},
275 /* Likewise, and require special QEMU command-line arguments: */
276 { "query-acpi-ospm-status", ERROR_CLASS_GENERIC_ERROR
},
277 { "query-balloon", ERROR_CLASS_DEVICE_NOT_ACTIVE
},
278 { "query-hotpluggable-cpus", ERROR_CLASS_GENERIC_ERROR
},
279 { "query-vm-generation-id", ERROR_CLASS_GENERIC_ERROR
},
284 for (i
= 0; fails
[i
].cmd
; i
++) {
285 if (!strcmp(cmd
, fails
[i
].cmd
)) {
286 return fails
[i
].err_class
;
292 static void test_query(const void *data
)
294 const char *cmd
= data
;
295 int expected_error_class
= query_error_class(cmd
);
297 const char *error_class
;
299 qtest_start(common_args
);
301 resp
= qmp("{ 'execute': %s }", cmd
);
302 error
= qdict_get_qdict(resp
, "error");
303 error_class
= error
? qdict_get_str(error
, "class") : NULL
;
305 if (expected_error_class
< 0) {
306 g_assert(qdict_haskey(resp
, "return"));
309 g_assert_cmpint(qapi_enum_parse(&QapiErrorClass_lookup
, error_class
,
311 ==, expected_error_class
);
318 static bool query_is_blacklisted(const char *cmd
)
320 const char *blacklist
[] = {
321 /* Not actually queries: */
323 /* Success depends on target arch: */
324 "query-cpu-definitions", /* arm, i386, ppc, s390x */
325 "query-gic-capabilities", /* arm */
326 /* Success depends on target-specific build configuration: */
327 "query-pci", /* CONFIG_PCI */
328 /* Success depends on launching SEV guest */
329 "query-sev-launch-measure",
330 /* Success depends on Host or Hypervisor SEV support */
332 "query-sev-capabilities",
337 for (i
= 0; blacklist
[i
]; i
++) {
338 if (!strcmp(cmd
, blacklist
[i
])) {
346 SchemaInfoList
*list
;
350 static void qmp_schema_init(QmpSchema
*schema
)
354 SchemaInfoList
*tail
;
356 qtest_start(common_args
);
357 resp
= qmp("{ 'execute': 'query-qmp-schema' }");
359 qiv
= qobject_input_visitor_new(qdict_get(resp
, "return"));
360 visit_type_SchemaInfoList(qiv
, NULL
, &schema
->list
, &error_abort
);
366 schema
->hash
= g_hash_table_new(g_str_hash
, g_str_equal
);
368 /* Build @schema: hash table mapping entity name to SchemaInfo */
369 for (tail
= schema
->list
; tail
; tail
= tail
->next
) {
370 g_hash_table_insert(schema
->hash
, tail
->value
->name
, tail
->value
);
374 static SchemaInfo
*qmp_schema_lookup(QmpSchema
*schema
, const char *name
)
376 return g_hash_table_lookup(schema
->hash
, name
);
379 static void qmp_schema_cleanup(QmpSchema
*schema
)
381 qapi_free_SchemaInfoList(schema
->list
);
382 g_hash_table_destroy(schema
->hash
);
385 static bool object_type_has_mandatory_members(SchemaInfo
*type
)
387 SchemaInfoObjectMemberList
*tail
;
389 g_assert(type
->meta_type
== SCHEMA_META_TYPE_OBJECT
);
391 for (tail
= type
->u
.object
.members
; tail
; tail
= tail
->next
) {
392 if (!tail
->value
->has_q_default
) {
400 static void add_query_tests(QmpSchema
*schema
)
402 SchemaInfoList
*tail
;
403 SchemaInfo
*si
, *arg_type
, *ret_type
;
406 /* Test the query-like commands */
407 for (tail
= schema
->list
; tail
; tail
= tail
->next
) {
409 if (si
->meta_type
!= SCHEMA_META_TYPE_COMMAND
) {
413 if (query_is_blacklisted(si
->name
)) {
417 arg_type
= qmp_schema_lookup(schema
, si
->u
.command
.arg_type
);
418 if (object_type_has_mandatory_members(arg_type
)) {
422 ret_type
= qmp_schema_lookup(schema
, si
->u
.command
.ret_type
);
423 if (ret_type
->meta_type
== SCHEMA_META_TYPE_OBJECT
424 && !ret_type
->u
.object
.members
) {
428 test_name
= g_strdup_printf("qmp/%s", si
->name
);
429 qtest_add_data_func(test_name
, si
->name
, test_query
);
434 /* Preconfig tests */
436 static void test_qmp_preconfig(void)
439 QTestState
*qs
= qtest_startf("%s --preconfig", common_args
);
441 /* preconfig state */
442 /* enabled commands, no error expected */
443 g_assert(!qmp_rsp_is_err(qtest_qmp(qs
, "{ 'execute': 'query-commands' }")));
445 /* forbidden commands, expected error */
446 g_assert(qmp_rsp_is_err(qtest_qmp(qs
, "{ 'execute': 'query-cpus' }")));
448 /* check that query-status returns preconfig state */
449 rsp
= qtest_qmp(qs
, "{ 'execute': 'query-status' }");
450 ret
= qdict_get_qdict(rsp
, "return");
452 g_assert_cmpstr(qdict_get_try_str(ret
, "status"), ==, "preconfig");
455 /* exit preconfig state */
456 g_assert(!qmp_rsp_is_err(qtest_qmp(qs
, "{ 'execute': 'x-exit-preconfig' }")));
457 qtest_qmp_eventwait(qs
, "RESUME");
459 /* check that query-status returns running state */
460 rsp
= qtest_qmp(qs
, "{ 'execute': 'query-status' }");
461 ret
= qdict_get_qdict(rsp
, "return");
463 g_assert_cmpstr(qdict_get_try_str(ret
, "status"), ==, "running");
466 /* check that x-exit-preconfig returns error after exiting preconfig */
467 g_assert(qmp_rsp_is_err(qtest_qmp(qs
, "{ 'execute': 'x-exit-preconfig' }")));
469 /* enabled commands, no error expected */
470 g_assert(!qmp_rsp_is_err(qtest_qmp(qs
, "{ 'execute': 'query-cpus' }")));
475 int main(int argc
, char *argv
[])
480 g_test_init(&argc
, &argv
, NULL
);
482 qtest_add_func("qmp/protocol", test_qmp_protocol
);
483 qtest_add_func("qmp/oob", test_qmp_oob
);
484 qmp_schema_init(&schema
);
485 add_query_tests(&schema
);
486 qtest_add_func("qmp/preconfig", test_qmp_preconfig
);
490 qmp_schema_cleanup(&schema
);