dump-guest-memory: more descriptive lookup_type failure
[qemu/ar7.git] / tests / qmp-test.c
blob07c0b87e27a5f8781f3ff09634ef1b7b3217eca2
1 /*
2 * QMP protocol test cases
4 * Copyright (c) 2017 Red Hat Inc.
6 * Authors:
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"
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"
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");
32 g_assert(desc);
33 return error ? qdict_get_try_str(error, "class") : NULL;
36 static void test_version(QObject *version)
38 Visitor *v;
39 VersionInfo *vinfo;
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);
48 static void test_malformed(QTestState *qts)
50 QDict *resp;
52 /* Not even a dictionary */
53 resp = qtest_qmp(qts, "null");
54 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
55 QDECREF(resp);
57 /* No "execute" key */
58 resp = qtest_qmp(qts, "{}");
59 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
60 QDECREF(resp);
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);
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);
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);
78 static void test_qmp_protocol(void)
80 QDict *resp, *q, *ret;
81 QList *capabilities;
82 QTestState *qts;
83 const QListEntry *entry;
84 QString *qstr;
85 int i;
87 qts = qtest_init_without_qmp_handshake(common_args);
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);
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);
108 /* Test malformed commands before handshake */
109 test_malformed(qts);
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);
117 /* Test repeated handshake */
118 resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
119 g_assert_cmpstr(get_error_class(resp), ==, "CommandNotFound");
120 QDECREF(resp);
122 /* Test valid command */
123 resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
124 test_version(qdict_get(resp, "return"));
125 QDECREF(resp);
127 /* Test malformed commands */
128 test_malformed(qts);
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);
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);
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.
152 for (i = 0; i < 16; i++) {
153 qtest_async_qmp(qts, "{ 'execute': 'query-version' }");
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);
164 qtest_quit(qts);
167 /* Tests for Out-Of-Band support. */
168 static void test_qmp_oob(void)
170 QDict *resp;
171 int acks = 0;
172 const char *cmd_id;
174 global_qtest = qtest_init_without_qmp_handshake(common_args);
176 /* Ignore the greeting message. */
177 resp = qmp_receive();
178 g_assert(qdict_get_qdict(resp, "QMP"));
179 QDECREF(resp);
181 /* Try a fake capability, it should fail. */
182 resp = qmp("{ 'execute': 'qmp_capabilities', "
183 " 'arguments': { 'enable': [ 'cap-does-not-exist' ] } }");
184 g_assert(qdict_haskey(resp, "error"));
185 QDECREF(resp);
187 /* Now, enable OOB in current QMP session, it should succeed. */
188 resp = qmp("{ 'execute': 'qmp_capabilities', "
189 " 'arguments': { 'enable': [ 'oob' ] } }");
190 g_assert(qdict_haskey(resp, "return"));
191 QDECREF(resp);
194 * Try any command that does not support OOB but with OOB flag. We
195 * should get failure.
197 resp = qmp("{ 'execute': 'query-cpus',"
198 " 'control': { 'run-oob': true } }");
199 g_assert(qdict_haskey(resp, "error"));
200 QDECREF(resp);
203 * First send the "x-oob-test" command with lock=true and
204 * oob=false, it should hang the dispatcher and main thread;
205 * later, we send another lock=false with oob=true to continue
206 * that thread processing. Finally we should receive replies from
207 * both commands.
209 qmp_async("{ 'execute': 'x-oob-test',"
210 " 'arguments': { 'lock': true }, "
211 " 'id': 'lock-cmd'}");
212 qmp_async("{ 'execute': 'x-oob-test', "
213 " 'arguments': { 'lock': false }, "
214 " 'control': { 'run-oob': true }, "
215 " 'id': 'unlock-cmd' }");
217 /* Ignore all events. Wait for 2 acks */
218 while (acks < 2) {
219 resp = qmp_receive();
220 cmd_id = qdict_get_str(resp, "id");
221 if (!g_strcmp0(cmd_id, "lock-cmd") ||
222 !g_strcmp0(cmd_id, "unlock-cmd")) {
223 acks++;
225 QDECREF(resp);
228 qtest_end();
231 static int query_error_class(const char *cmd)
233 static struct {
234 const char *cmd;
235 int err_class;
236 } fails[] = {
237 /* Success depends on build configuration: */
238 #ifndef CONFIG_SPICE
239 { "query-spice", ERROR_CLASS_COMMAND_NOT_FOUND },
240 #endif
241 #ifndef CONFIG_VNC
242 { "query-vnc", ERROR_CLASS_GENERIC_ERROR },
243 { "query-vnc-servers", ERROR_CLASS_GENERIC_ERROR },
244 #endif
245 #ifndef CONFIG_REPLICATION
246 { "query-xen-replication-status", ERROR_CLASS_COMMAND_NOT_FOUND },
247 #endif
248 /* Likewise, and require special QEMU command-line arguments: */
249 { "query-acpi-ospm-status", ERROR_CLASS_GENERIC_ERROR },
250 { "query-balloon", ERROR_CLASS_DEVICE_NOT_ACTIVE },
251 { "query-hotpluggable-cpus", ERROR_CLASS_GENERIC_ERROR },
252 { "query-vm-generation-id", ERROR_CLASS_GENERIC_ERROR },
253 { NULL, -1 }
255 int i;
257 for (i = 0; fails[i].cmd; i++) {
258 if (!strcmp(cmd, fails[i].cmd)) {
259 return fails[i].err_class;
262 return -1;
265 static void test_query(const void *data)
267 const char *cmd = data;
268 int expected_error_class = query_error_class(cmd);
269 QDict *resp, *error;
270 const char *error_class;
272 qtest_start(common_args);
274 resp = qmp("{ 'execute': %s }", cmd);
275 error = qdict_get_qdict(resp, "error");
276 error_class = error ? qdict_get_str(error, "class") : NULL;
278 if (expected_error_class < 0) {
279 g_assert(qdict_haskey(resp, "return"));
280 } else {
281 g_assert(error);
282 g_assert_cmpint(qapi_enum_parse(&QapiErrorClass_lookup, error_class,
283 -1, &error_abort),
284 ==, expected_error_class);
286 QDECREF(resp);
288 qtest_end();
291 static bool query_is_blacklisted(const char *cmd)
293 const char *blacklist[] = {
294 /* Not actually queries: */
295 "add-fd",
296 /* Success depends on target arch: */
297 "query-cpu-definitions", /* arm, i386, ppc, s390x */
298 "query-gic-capabilities", /* arm */
299 /* Success depends on target-specific build configuration: */
300 "query-pci", /* CONFIG_PCI */
301 /* Success depends on launching SEV guest */
302 "query-sev-launch-measure",
303 /* Success depends on Host or Hypervisor SEV support */
304 "query-sev",
305 "query-sev-capabilities",
306 NULL
308 int i;
310 for (i = 0; blacklist[i]; i++) {
311 if (!strcmp(cmd, blacklist[i])) {
312 return true;
315 return false;
318 typedef struct {
319 SchemaInfoList *list;
320 GHashTable *hash;
321 } QmpSchema;
323 static void qmp_schema_init(QmpSchema *schema)
325 QDict *resp;
326 Visitor *qiv;
327 SchemaInfoList *tail;
329 qtest_start(common_args);
330 resp = qmp("{ 'execute': 'query-qmp-schema' }");
332 qiv = qobject_input_visitor_new(qdict_get(resp, "return"));
333 visit_type_SchemaInfoList(qiv, NULL, &schema->list, &error_abort);
334 visit_free(qiv);
336 QDECREF(resp);
337 qtest_end();
339 schema->hash = g_hash_table_new(g_str_hash, g_str_equal);
341 /* Build @schema: hash table mapping entity name to SchemaInfo */
342 for (tail = schema->list; tail; tail = tail->next) {
343 g_hash_table_insert(schema->hash, tail->value->name, tail->value);
347 static SchemaInfo *qmp_schema_lookup(QmpSchema *schema, const char *name)
349 return g_hash_table_lookup(schema->hash, name);
352 static void qmp_schema_cleanup(QmpSchema *schema)
354 qapi_free_SchemaInfoList(schema->list);
355 g_hash_table_destroy(schema->hash);
358 static bool object_type_has_mandatory_members(SchemaInfo *type)
360 SchemaInfoObjectMemberList *tail;
362 g_assert(type->meta_type == SCHEMA_META_TYPE_OBJECT);
364 for (tail = type->u.object.members; tail; tail = tail->next) {
365 if (!tail->value->has_q_default) {
366 return true;
370 return false;
373 static void add_query_tests(QmpSchema *schema)
375 SchemaInfoList *tail;
376 SchemaInfo *si, *arg_type, *ret_type;
377 char *test_name;
379 /* Test the query-like commands */
380 for (tail = schema->list; tail; tail = tail->next) {
381 si = tail->value;
382 if (si->meta_type != SCHEMA_META_TYPE_COMMAND) {
383 continue;
386 if (query_is_blacklisted(si->name)) {
387 continue;
390 arg_type = qmp_schema_lookup(schema, si->u.command.arg_type);
391 if (object_type_has_mandatory_members(arg_type)) {
392 continue;
395 ret_type = qmp_schema_lookup(schema, si->u.command.ret_type);
396 if (ret_type->meta_type == SCHEMA_META_TYPE_OBJECT
397 && !ret_type->u.object.members) {
398 continue;
401 test_name = g_strdup_printf("qmp/%s", si->name);
402 qtest_add_data_func(test_name, si->name, test_query);
403 g_free(test_name);
407 int main(int argc, char *argv[])
409 QmpSchema schema;
410 int ret;
412 g_test_init(&argc, &argv, NULL);
414 qtest_add_func("qmp/protocol", test_qmp_protocol);
415 qtest_add_func("qmp/oob", test_qmp_oob);
416 qmp_schema_init(&schema);
417 add_query_tests(&schema);
419 ret = g_test_run();
421 qmp_schema_cleanup(&schema);
422 return ret;