2 * QMP protocol test cases
4 * Copyright (c) 2017 Red Hat Inc.
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 /* Tests for Out-Of-Band support. */
139 static void test_qmp_oob(void)
144 const QListEntry *entry;
149 qts = qtest_init_without_qmp_handshake(true, common_args);
151 /* Check the greeting message. */
152 resp = qtest_qmp_receive(qts);
153 q = qdict_get_qdict(resp, "QMP");
155 capabilities = qdict_get_qlist(q, "capabilities");
156 g_assert(capabilities && !qlist_empty(capabilities));
157 entry = qlist_first(capabilities);
159 qstr = qobject_to(QString, entry->value);
161 g_assert_cmpstr(qstring_get_str(qstr), ==, "oob");
164 /* Try a fake capability, it should fail. */
165 resp = qtest_qmp(qts,
166 "{ 'execute': 'qmp_capabilities', "
167 " 'arguments': { 'enable': [ 'cap-does-not-exist' ] } }");
168 g_assert(qdict_haskey(resp, "error"));
171 /* Now, enable OOB in current QMP session, it should succeed. */
172 resp = qtest_qmp(qts,
173 "{ 'execute': 'qmp_capabilities', "
174 " 'arguments': { 'enable': [ 'oob' ] } }");
175 g_assert(qdict_haskey(resp, "return"));
179 * Try any command that does not support OOB but with OOB flag. We
180 * should get failure.
182 resp = qtest_qmp(qts,
183 "{ 'execute': 'query-cpus',"
184 " 'control': { 'run-oob': true } }");
185 g_assert(qdict_haskey(resp, "error"));
189 * First send the "x-oob-test" command with lock=true and
190 * oob=false, it should hang the dispatcher and main thread;
191 * later, we send another lock=false with oob=true to continue
192 * that thread processing. Finally we should receive replies from
196 "{ 'execute': 'x-oob-test',"
197 " 'arguments': { 'lock': true }, "
198 " 'id': 'lock-cmd'}");
200 "{ 'execute': 'x-oob-test', "
201 " 'arguments': { 'lock': false }, "
202 " 'control': { 'run-oob': true }, "
203 " 'id': 'unlock-cmd' }");
205 /* Ignore all events. Wait for 2 acks */
207 resp = qtest_qmp_receive(qts);
208 cmd_id = qdict_get_str(resp, "id");
209 if (!g_strcmp0(cmd_id, "lock-cmd") ||
210 !g_strcmp0(cmd_id, "unlock-cmd")) {
219 static int query_error_class(const char *cmd)
225 /* Success depends on build configuration: */
227 { "query-spice", ERROR_CLASS_COMMAND_NOT_FOUND },
230 { "query-vnc", ERROR_CLASS_GENERIC_ERROR },
231 { "query-vnc-servers", ERROR_CLASS_GENERIC_ERROR },
233 #ifndef CONFIG_REPLICATION
234 { "query-xen-replication-status", ERROR_CLASS_COMMAND_NOT_FOUND },
236 /* Likewise, and require special QEMU command-line arguments: */
237 { "query-acpi-ospm-status", ERROR_CLASS_GENERIC_ERROR },
238 { "query-balloon", ERROR_CLASS_DEVICE_NOT_ACTIVE },
239 { "query-hotpluggable-cpus", ERROR_CLASS_GENERIC_ERROR },
240 { "query-vm-generation-id", ERROR_CLASS_GENERIC_ERROR },
245 for (i = 0; fails[i].cmd; i++) {
246 if (!strcmp(cmd, fails[i].cmd)) {
247 return fails[i].err_class;
253 static void test_query(const void *data)
255 const char *cmd = data;
256 int expected_error_class = query_error_class(cmd);
258 const char *error_class;
260 qtest_start(common_args);
262 resp = qmp("{ 'execute': %s }", cmd);
263 error = qdict_get_qdict(resp, "error");
264 error_class = error ? qdict_get_str(error, "class") : NULL;
266 if (expected_error_class < 0) {
267 g_assert(qdict_haskey(resp, "return"));
270 g_assert_cmpint(qapi_enum_parse(&QapiErrorClass_lookup, error_class,
272 ==, expected_error_class);
279 static bool query_is_blacklisted(const char *cmd)
281 const char *blacklist[] = {
282 /* Not actually queries: */
284 /* Success depends on target arch: */
285 "query-cpu-definitions", /* arm, i386, ppc, s390x */
286 "query-gic-capabilities", /* arm */
287 /* Success depends on target-specific build configuration: */
288 "query-pci", /* CONFIG_PCI */
289 /* Success depends on launching SEV guest */
290 "query-sev-launch-measure",
291 /* Success depends on Host or Hypervisor SEV support */
293 "query-sev-capabilities",
298 for (i = 0; blacklist[i]; i++) {
299 if (!strcmp(cmd, blacklist[i])) {
307 SchemaInfoList *list;
311 static void qmp_schema_init(QmpSchema *schema)
315 SchemaInfoList *tail;
317 qtest_start(common_args);
318 resp = qmp("{ 'execute': 'query-qmp-schema' }");
320 qiv = qobject_input_visitor_new(qdict_get(resp, "return"));
321 visit_type_SchemaInfoList(qiv, NULL, &schema->list, &error_abort);
327 schema->hash = g_hash_table_new(g_str_hash, g_str_equal);
329 /* Build @schema: hash table mapping entity name to SchemaInfo */
330 for (tail = schema->list; tail; tail = tail->next) {
331 g_hash_table_insert(schema->hash, tail->value->name, tail->value);
335 static SchemaInfo *qmp_schema_lookup(QmpSchema *schema, const char *name)
337 return g_hash_table_lookup(schema->hash, name);
340 static void qmp_schema_cleanup(QmpSchema *schema)
342 qapi_free_SchemaInfoList(schema->list);
343 g_hash_table_destroy(schema->hash);
346 static bool object_type_has_mandatory_members(SchemaInfo *type)
348 SchemaInfoObjectMemberList *tail;
350 g_assert(type->meta_type == SCHEMA_META_TYPE_OBJECT);
352 for (tail = type->u.object.members; tail; tail = tail->next) {
353 if (!tail->value->has_q_default) {
361 static void add_query_tests(QmpSchema *schema)
363 SchemaInfoList *tail;
364 SchemaInfo *si, *arg_type, *ret_type;
367 /* Test the query-like commands */
368 for (tail = schema->list; tail; tail = tail->next) {
370 if (si->meta_type != SCHEMA_META_TYPE_COMMAND) {
374 if (query_is_blacklisted(si->name)) {
378 arg_type = qmp_schema_lookup(schema, si->u.command.arg_type);
379 if (object_type_has_mandatory_members(arg_type)) {
383 ret_type = qmp_schema_lookup(schema, si->u.command.ret_type);
384 if (ret_type->meta_type == SCHEMA_META_TYPE_OBJECT
385 && !ret_type->u.object.members) {
389 test_name = g_strdup_printf("qmp/%s", si->name);
390 qtest_add_data_func(test_name, si->name, test_query);
395 static void test_qmp_preconfig(void)
398 QTestState *qs = qtest_startf("%s --preconfig", common_args);
400 /* preconfig state */
401 /* enabled commands, no error expected */
402 g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-commands' }")));
404 /* forbidden commands, expected error */
405 g_assert(qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-cpus' }")));
407 /* check that query-status returns preconfig state */
408 rsp = qtest_qmp(qs, "{ 'execute': 'query-status' }");
409 ret = qdict_get_qdict(rsp, "return");
411 g_assert_cmpstr(qdict_get_try_str(ret, "status"), ==, "preconfig");
414 /* exit preconfig state */
415 g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'exit-preconfig' }")));
416 qtest_qmp_eventwait(qs, "RESUME");
418 /* check that query-status returns running state */
419 rsp = qtest_qmp(qs, "{ 'execute': 'query-status' }");
420 ret = qdict_get_qdict(rsp, "return");
422 g_assert_cmpstr(qdict_get_try_str(ret, "status"), ==, "running");
425 /* check that exit-preconfig returns error after exiting preconfig */
426 g_assert(qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'exit-preconfig' }")));
428 /* enabled commands, no error expected */
429 g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'query-cpus' }")));
434 int main(int argc, char *argv[])
439 g_test_init(&argc, &argv, NULL);
441 qtest_add_func("qmp/protocol", test_qmp_protocol);
442 qtest_add_func("qmp/oob", test_qmp_oob);
443 qmp_schema_init(&schema);
444 add_query_tests(&schema);
445 qtest_add_func("qmp/preconfig", test_qmp_preconfig);
449 qmp_schema_cleanup(&schema);