]> git.proxmox.com Git - mirror_qemu.git/blob - tests/qmp-test.c
Merge remote-tracking branch 'remotes/dgilbert/tags/pull-migration-20180323a' into...
[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
84 qts = qtest_init_without_qmp_handshake(common_args);
85
86 /* Test greeting */
87 resp = qtest_qmp_receive(qts);
88 q = qdict_get_qdict(resp, "QMP");
89 g_assert(q);
90 test_version(qdict_get(q, "version"));
91 capabilities = qdict_get_qlist(q, "capabilities");
92 g_assert(capabilities && qlist_empty(capabilities));
93
94 /* Test valid command before handshake */
95 resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
96 g_assert_cmpstr(get_error_class(resp), ==, "CommandNotFound");
97 QDECREF(resp);
98
99 /* Test malformed commands before handshake */
100 test_malformed(qts);
101
102 /* Test handshake */
103 resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
104 ret = qdict_get_qdict(resp, "return");
105 g_assert(ret && !qdict_size(ret));
106 QDECREF(resp);
107
108 /* Test repeated handshake */
109 resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
110 g_assert_cmpstr(get_error_class(resp), ==, "CommandNotFound");
111 QDECREF(resp);
112
113 /* Test valid command */
114 resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
115 test_version(qdict_get(resp, "return"));
116 QDECREF(resp);
117
118 /* Test malformed commands */
119 test_malformed(qts);
120
121 /* Test 'id' */
122 resp = qtest_qmp(qts, "{ 'execute': 'query-name', 'id': 'cookie#1' }");
123 ret = qdict_get_qdict(resp, "return");
124 g_assert(ret);
125 g_assert_cmpstr(qdict_get_try_str(resp, "id"), ==, "cookie#1");
126 QDECREF(resp);
127
128 /* Test command failure with 'id' */
129 resp = qtest_qmp(qts, "{ 'execute': 'human-monitor-command', 'id': 2 }");
130 g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
131 g_assert_cmpint(qdict_get_int(resp, "id"), ==, 2);
132 QDECREF(resp);
133
134 qtest_quit(qts);
135 }
136
137 static int query_error_class(const char *cmd)
138 {
139 static struct {
140 const char *cmd;
141 int err_class;
142 } fails[] = {
143 /* Success depends on build configuration: */
144 #ifndef CONFIG_SPICE
145 { "query-spice", ERROR_CLASS_COMMAND_NOT_FOUND },
146 #endif
147 #ifndef CONFIG_VNC
148 { "query-vnc", ERROR_CLASS_GENERIC_ERROR },
149 { "query-vnc-servers", ERROR_CLASS_GENERIC_ERROR },
150 #endif
151 #ifndef CONFIG_REPLICATION
152 { "query-xen-replication-status", ERROR_CLASS_COMMAND_NOT_FOUND },
153 #endif
154 /* Likewise, and require special QEMU command-line arguments: */
155 { "query-acpi-ospm-status", ERROR_CLASS_GENERIC_ERROR },
156 { "query-balloon", ERROR_CLASS_DEVICE_NOT_ACTIVE },
157 { "query-hotpluggable-cpus", ERROR_CLASS_GENERIC_ERROR },
158 { "query-vm-generation-id", ERROR_CLASS_GENERIC_ERROR },
159 { NULL, -1 }
160 };
161 int i;
162
163 for (i = 0; fails[i].cmd; i++) {
164 if (!strcmp(cmd, fails[i].cmd)) {
165 return fails[i].err_class;
166 }
167 }
168 return -1;
169 }
170
171 static void test_query(const void *data)
172 {
173 const char *cmd = data;
174 int expected_error_class = query_error_class(cmd);
175 QDict *resp, *error;
176 const char *error_class;
177
178 qtest_start(common_args);
179
180 resp = qmp("{ 'execute': %s }", cmd);
181 error = qdict_get_qdict(resp, "error");
182 error_class = error ? qdict_get_str(error, "class") : NULL;
183
184 if (expected_error_class < 0) {
185 g_assert(qdict_haskey(resp, "return"));
186 } else {
187 g_assert(error);
188 g_assert_cmpint(qapi_enum_parse(&QapiErrorClass_lookup, error_class,
189 -1, &error_abort),
190 ==, expected_error_class);
191 }
192 QDECREF(resp);
193
194 qtest_end();
195 }
196
197 static bool query_is_blacklisted(const char *cmd)
198 {
199 const char *blacklist[] = {
200 /* Not actually queries: */
201 "add-fd",
202 /* Success depends on target arch: */
203 "query-cpu-definitions", /* arm, i386, ppc, s390x */
204 "query-gic-capabilities", /* arm */
205 /* Success depends on target-specific build configuration: */
206 "query-pci", /* CONFIG_PCI */
207 /* Success depends on launching SEV guest */
208 "query-sev-launch-measure",
209 /* Success depends on Host or Hypervisor SEV support */
210 "query-sev",
211 "query-sev-capabilities",
212 NULL
213 };
214 int i;
215
216 for (i = 0; blacklist[i]; i++) {
217 if (!strcmp(cmd, blacklist[i])) {
218 return true;
219 }
220 }
221 return false;
222 }
223
224 typedef struct {
225 SchemaInfoList *list;
226 GHashTable *hash;
227 } QmpSchema;
228
229 static void qmp_schema_init(QmpSchema *schema)
230 {
231 QDict *resp;
232 Visitor *qiv;
233 SchemaInfoList *tail;
234
235 qtest_start(common_args);
236 resp = qmp("{ 'execute': 'query-qmp-schema' }");
237
238 qiv = qobject_input_visitor_new(qdict_get(resp, "return"));
239 visit_type_SchemaInfoList(qiv, NULL, &schema->list, &error_abort);
240 visit_free(qiv);
241
242 QDECREF(resp);
243 qtest_end();
244
245 schema->hash = g_hash_table_new(g_str_hash, g_str_equal);
246
247 /* Build @schema: hash table mapping entity name to SchemaInfo */
248 for (tail = schema->list; tail; tail = tail->next) {
249 g_hash_table_insert(schema->hash, tail->value->name, tail->value);
250 }
251 }
252
253 static SchemaInfo *qmp_schema_lookup(QmpSchema *schema, const char *name)
254 {
255 return g_hash_table_lookup(schema->hash, name);
256 }
257
258 static void qmp_schema_cleanup(QmpSchema *schema)
259 {
260 qapi_free_SchemaInfoList(schema->list);
261 g_hash_table_destroy(schema->hash);
262 }
263
264 static bool object_type_has_mandatory_members(SchemaInfo *type)
265 {
266 SchemaInfoObjectMemberList *tail;
267
268 g_assert(type->meta_type == SCHEMA_META_TYPE_OBJECT);
269
270 for (tail = type->u.object.members; tail; tail = tail->next) {
271 if (!tail->value->has_q_default) {
272 return true;
273 }
274 }
275
276 return false;
277 }
278
279 static void add_query_tests(QmpSchema *schema)
280 {
281 SchemaInfoList *tail;
282 SchemaInfo *si, *arg_type, *ret_type;
283 char *test_name;
284
285 /* Test the query-like commands */
286 for (tail = schema->list; tail; tail = tail->next) {
287 si = tail->value;
288 if (si->meta_type != SCHEMA_META_TYPE_COMMAND) {
289 continue;
290 }
291
292 if (query_is_blacklisted(si->name)) {
293 continue;
294 }
295
296 arg_type = qmp_schema_lookup(schema, si->u.command.arg_type);
297 if (object_type_has_mandatory_members(arg_type)) {
298 continue;
299 }
300
301 ret_type = qmp_schema_lookup(schema, si->u.command.ret_type);
302 if (ret_type->meta_type == SCHEMA_META_TYPE_OBJECT
303 && !ret_type->u.object.members) {
304 continue;
305 }
306
307 test_name = g_strdup_printf("qmp/%s", si->name);
308 qtest_add_data_func(test_name, si->name, test_query);
309 g_free(test_name);
310 }
311 }
312
313 int main(int argc, char *argv[])
314 {
315 QmpSchema schema;
316 int ret;
317
318 g_test_init(&argc, &argv, NULL);
319
320 qtest_add_func("qmp/protocol", test_qmp_protocol);
321 qmp_schema_init(&schema);
322 add_query_tests(&schema);
323
324 ret = g_test_run();
325
326 qmp_schema_cleanup(&schema);
327 return ret;
328 }