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"
24 const char common_args
[] = "-nodefaults -machine none";
26 static const char *get_error_class(QDict
*resp
)
28 QDict
*error
= qdict_get_qdict(resp
, "error");
29 const char *desc
= qdict_get_try_str(error
, "desc");
32 return error
? qdict_get_try_str(error
, "class") : NULL
;
35 static void test_version(QObject
*version
)
41 v
= qobject_input_visitor_new(version
);
42 visit_type_VersionInfo(v
, "version", &vinfo
, &error_abort
);
43 qapi_free_VersionInfo(vinfo
);
47 static void test_malformed(QTestState
*qts
)
51 /* Not even a dictionary */
52 resp
= qtest_qmp(qts
, "null");
53 g_assert_cmpstr(get_error_class(resp
), ==, "GenericError");
56 /* No "execute" key */
57 resp
= qtest_qmp(qts
, "{}");
58 g_assert_cmpstr(get_error_class(resp
), ==, "GenericError");
61 /* "execute" isn't a string */
62 resp
= qtest_qmp(qts
, "{ 'execute': true }");
63 g_assert_cmpstr(get_error_class(resp
), ==, "GenericError");
66 /* "arguments" isn't a dictionary */
67 resp
= qtest_qmp(qts
, "{ 'execute': 'no-such-cmd', 'arguments': [] }");
68 g_assert_cmpstr(get_error_class(resp
), ==, "GenericError");
72 resp
= qtest_qmp(qts
, "{ 'execute': 'no-such-cmd', 'extra': true }");
73 g_assert_cmpstr(get_error_class(resp
), ==, "GenericError");
77 static void test_qmp_protocol(void)
79 QDict
*resp
, *q
, *ret
;
83 qts
= qtest_init_without_qmp_handshake(common_args
);
86 resp
= qtest_qmp_receive(qts
);
87 q
= qdict_get_qdict(resp
, "QMP");
89 test_version(qdict_get(q
, "version"));
90 capabilities
= qdict_get_qlist(q
, "capabilities");
91 g_assert(capabilities
&& qlist_empty(capabilities
));
94 /* Test valid command before handshake */
95 resp
= qtest_qmp(qts
, "{ 'execute': 'query-version' }");
96 g_assert_cmpstr(get_error_class(resp
), ==, "CommandNotFound");
99 /* Test malformed commands before handshake */
103 resp
= qtest_qmp(qts
, "{ 'execute': 'qmp_capabilities' }");
104 ret
= qdict_get_qdict(resp
, "return");
105 g_assert(ret
&& !qdict_size(ret
));
108 /* Test repeated handshake */
109 resp
= qtest_qmp(qts
, "{ 'execute': 'qmp_capabilities' }");
110 g_assert_cmpstr(get_error_class(resp
), ==, "CommandNotFound");
113 /* Test valid command */
114 resp
= qtest_qmp(qts
, "{ 'execute': 'query-version' }");
115 test_version(qdict_get(resp
, "return"));
118 /* Test malformed commands */
122 resp
= qtest_qmp(qts
, "{ 'execute': 'query-name', 'id': 'cookie#1' }");
123 ret
= qdict_get_qdict(resp
, "return");
125 g_assert_cmpstr(qdict_get_try_str(resp
, "id"), ==, "cookie#1");
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);
137 static int query_error_class(const char *cmd
)
143 /* Success depends on build configuration: */
145 { "query-spice", ERROR_CLASS_COMMAND_NOT_FOUND
},
148 { "query-vnc", ERROR_CLASS_GENERIC_ERROR
},
149 { "query-vnc-servers", ERROR_CLASS_GENERIC_ERROR
},
151 #ifndef CONFIG_REPLICATION
152 { "query-xen-replication-status", ERROR_CLASS_COMMAND_NOT_FOUND
},
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
},
163 for (i
= 0; fails
[i
].cmd
; i
++) {
164 if (!strcmp(cmd
, fails
[i
].cmd
)) {
165 return fails
[i
].err_class
;
171 static void test_query(const void *data
)
173 const char *cmd
= data
;
174 int expected_error_class
= query_error_class(cmd
);
176 const char *error_class
;
178 qtest_start(common_args
);
180 resp
= qmp("{ 'execute': %s }", cmd
);
181 error
= qdict_get_qdict(resp
, "error");
182 error_class
= error
? qdict_get_str(error
, "class") : NULL
;
184 if (expected_error_class
< 0) {
185 g_assert(qdict_haskey(resp
, "return"));
188 g_assert_cmpint(qapi_enum_parse(&QapiErrorClass_lookup
, error_class
,
190 ==, expected_error_class
);
197 static bool query_is_blacklisted(const char *cmd
)
199 const char *blacklist
[] = {
200 /* Not actually queries: */
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 */
215 for (i
= 0; blacklist
[i
]; i
++) {
216 if (!strcmp(cmd
, blacklist
[i
])) {
224 SchemaInfoList
*list
;
228 static void qmp_schema_init(QmpSchema
*schema
)
232 SchemaInfoList
*tail
;
234 qtest_start(common_args
);
235 resp
= qmp("{ 'execute': 'query-qmp-schema' }");
237 qiv
= qobject_input_visitor_new(qdict_get(resp
, "return"));
238 visit_type_SchemaInfoList(qiv
, NULL
, &schema
->list
, &error_abort
);
244 schema
->hash
= g_hash_table_new(g_str_hash
, g_str_equal
);
246 /* Build @schema: hash table mapping entity name to SchemaInfo */
247 for (tail
= schema
->list
; tail
; tail
= tail
->next
) {
248 g_hash_table_insert(schema
->hash
, tail
->value
->name
, tail
->value
);
252 static SchemaInfo
*qmp_schema_lookup(QmpSchema
*schema
, const char *name
)
254 return g_hash_table_lookup(schema
->hash
, name
);
257 static void qmp_schema_cleanup(QmpSchema
*schema
)
259 qapi_free_SchemaInfoList(schema
->list
);
260 g_hash_table_destroy(schema
->hash
);
263 static bool object_type_has_mandatory_members(SchemaInfo
*type
)
265 SchemaInfoObjectMemberList
*tail
;
267 g_assert(type
->meta_type
== SCHEMA_META_TYPE_OBJECT
);
269 for (tail
= type
->u
.object
.members
; tail
; tail
= tail
->next
) {
270 if (!tail
->value
->has_q_default
) {
278 static void add_query_tests(QmpSchema
*schema
)
280 SchemaInfoList
*tail
;
281 SchemaInfo
*si
, *arg_type
, *ret_type
;
284 /* Test the query-like commands */
285 for (tail
= schema
->list
; tail
; tail
= tail
->next
) {
287 if (si
->meta_type
!= SCHEMA_META_TYPE_COMMAND
) {
291 if (query_is_blacklisted(si
->name
)) {
295 arg_type
= qmp_schema_lookup(schema
, si
->u
.command
.arg_type
);
296 if (object_type_has_mandatory_members(arg_type
)) {
300 ret_type
= qmp_schema_lookup(schema
, si
->u
.command
.ret_type
);
301 if (ret_type
->meta_type
== SCHEMA_META_TYPE_OBJECT
302 && !ret_type
->u
.object
.members
) {
306 test_name
= g_strdup_printf("qmp/%s", si
->name
);
307 qtest_add_data_func(test_name
, si
->name
, test_query
);
312 int main(int argc
, char *argv
[])
317 g_test_init(&argc
, &argv
, NULL
);
319 qtest_add_func("qmp/protocol", test_qmp_protocol
);
320 qmp_schema_init(&schema
);
321 add_query_tests(&schema
);
325 qmp_schema_cleanup(&schema
);