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