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-visit.h"
16 #include "qapi/error.h"
17 #include "qapi/qmp/qdict.h"
18 #include "qapi/qmp/qlist.h"
19 #include "qapi/qobject-input-visitor.h"
20 #include "qapi/util.h"
21 #include "qapi/visitor.h"
23 const char common_args
[] = "-nodefaults -machine none";
25 static const char *get_error_class(QDict
*resp
)
27 QDict
*error
= qdict_get_qdict(resp
, "error");
28 const char *desc
= qdict_get_try_str(error
, "desc");
31 return error
? qdict_get_try_str(error
, "class") : NULL
;
34 static void test_version(QObject
*version
)
40 v
= qobject_input_visitor_new(version
);
41 visit_type_VersionInfo(v
, "version", &vinfo
, &error_abort
);
42 qapi_free_VersionInfo(vinfo
);
46 static void test_malformed(QTestState
*qts
)
50 /* Not even a dictionary */
51 resp
= qtest_qmp(qts
, "null");
52 g_assert_cmpstr(get_error_class(resp
), ==, "GenericError");
55 /* No "execute" key */
56 resp
= qtest_qmp(qts
, "{}");
57 g_assert_cmpstr(get_error_class(resp
), ==, "GenericError");
60 /* "execute" isn't a string */
61 resp
= qtest_qmp(qts
, "{ 'execute': true }");
62 g_assert_cmpstr(get_error_class(resp
), ==, "GenericError");
65 /* "arguments" isn't a dictionary */
66 resp
= qtest_qmp(qts
, "{ 'execute': 'no-such-cmd', 'arguments': [] }");
67 g_assert_cmpstr(get_error_class(resp
), ==, "GenericError");
71 resp
= qtest_qmp(qts
, "{ 'execute': 'no-such-cmd', 'extra': true }");
72 g_assert_cmpstr(get_error_class(resp
), ==, "GenericError");
76 static void test_qmp_protocol(void)
78 QDict
*resp
, *q
, *ret
;
82 qts
= qtest_init_without_qmp_handshake(common_args
);
85 resp
= qtest_qmp_receive(qts
);
86 q
= qdict_get_qdict(resp
, "QMP");
88 test_version(qdict_get(q
, "version"));
89 capabilities
= qdict_get_qlist(q
, "capabilities");
90 g_assert(capabilities
&& qlist_empty(capabilities
));
93 /* Test valid command before handshake */
94 resp
= qtest_qmp(qts
, "{ 'execute': 'query-version' }");
95 g_assert_cmpstr(get_error_class(resp
), ==, "CommandNotFound");
98 /* Test malformed commands before handshake */
102 resp
= qtest_qmp(qts
, "{ 'execute': 'qmp_capabilities' }");
103 ret
= qdict_get_qdict(resp
, "return");
104 g_assert(ret
&& !qdict_size(ret
));
107 /* Test repeated handshake */
108 resp
= qtest_qmp(qts
, "{ 'execute': 'qmp_capabilities' }");
109 g_assert_cmpstr(get_error_class(resp
), ==, "CommandNotFound");
112 /* Test valid command */
113 resp
= qtest_qmp(qts
, "{ 'execute': 'query-version' }");
114 test_version(qdict_get(resp
, "return"));
117 /* Test malformed commands */
121 resp
= qtest_qmp(qts
, "{ 'execute': 'query-name', 'id': 'cookie#1' }");
122 ret
= qdict_get_qdict(resp
, "return");
124 g_assert_cmpstr(qdict_get_try_str(resp
, "id"), ==, "cookie#1");
127 /* Test command failure with 'id' */
128 resp
= qtest_qmp(qts
, "{ 'execute': 'human-monitor-command', 'id': 2 }");
129 g_assert_cmpstr(get_error_class(resp
), ==, "GenericError");
130 g_assert_cmpint(qdict_get_int(resp
, "id"), ==, 2);
136 static int query_error_class(const char *cmd
)
142 /* Success depends on build configuration: */
144 { "query-spice", ERROR_CLASS_COMMAND_NOT_FOUND
},
147 { "query-vnc", ERROR_CLASS_GENERIC_ERROR
},
148 { "query-vnc-servers", ERROR_CLASS_GENERIC_ERROR
},
150 #ifndef CONFIG_REPLICATION
151 { "query-xen-replication-status", ERROR_CLASS_COMMAND_NOT_FOUND
},
153 /* Likewise, and require special QEMU command-line arguments: */
154 { "query-acpi-ospm-status", ERROR_CLASS_GENERIC_ERROR
},
155 { "query-balloon", ERROR_CLASS_DEVICE_NOT_ACTIVE
},
156 { "query-hotpluggable-cpus", ERROR_CLASS_GENERIC_ERROR
},
157 { "query-vm-generation-id", ERROR_CLASS_GENERIC_ERROR
},
162 for (i
= 0; fails
[i
].cmd
; i
++) {
163 if (!strcmp(cmd
, fails
[i
].cmd
)) {
164 return fails
[i
].err_class
;
170 static void test_query(const void *data
)
172 const char *cmd
= data
;
173 int expected_error_class
= query_error_class(cmd
);
175 const char *error_class
;
177 qtest_start(common_args
);
179 resp
= qmp("{ 'execute': %s }", cmd
);
180 error
= qdict_get_qdict(resp
, "error");
181 error_class
= error
? qdict_get_str(error
, "class") : NULL
;
183 if (expected_error_class
< 0) {
184 g_assert(qdict_haskey(resp
, "return"));
187 g_assert_cmpint(qapi_enum_parse(&QapiErrorClass_lookup
, error_class
,
189 ==, expected_error_class
);
196 static bool query_is_blacklisted(const char *cmd
)
198 const char *blacklist
[] = {
199 /* Not actually queries: */
201 /* Success depends on target arch: */
202 "query-cpu-definitions", /* arm, i386, ppc, s390x */
203 "query-gic-capabilities", /* arm */
204 /* Success depends on target-specific build configuration: */
205 "query-pci", /* CONFIG_PCI */
210 for (i
= 0; blacklist
[i
]; i
++) {
211 if (!strcmp(cmd
, blacklist
[i
])) {
219 SchemaInfoList
*list
;
223 static void qmp_schema_init(QmpSchema
*schema
)
227 SchemaInfoList
*tail
;
229 qtest_start(common_args
);
230 resp
= qmp("{ 'execute': 'query-qmp-schema' }");
232 qiv
= qobject_input_visitor_new(qdict_get(resp
, "return"));
233 visit_type_SchemaInfoList(qiv
, NULL
, &schema
->list
, &error_abort
);
239 schema
->hash
= g_hash_table_new(g_str_hash
, g_str_equal
);
241 /* Build @schema: hash table mapping entity name to SchemaInfo */
242 for (tail
= schema
->list
; tail
; tail
= tail
->next
) {
243 g_hash_table_insert(schema
->hash
, tail
->value
->name
, tail
->value
);
247 static SchemaInfo
*qmp_schema_lookup(QmpSchema
*schema
, const char *name
)
249 return g_hash_table_lookup(schema
->hash
, name
);
252 static void qmp_schema_cleanup(QmpSchema
*schema
)
254 qapi_free_SchemaInfoList(schema
->list
);
255 g_hash_table_destroy(schema
->hash
);
258 static bool object_type_has_mandatory_members(SchemaInfo
*type
)
260 SchemaInfoObjectMemberList
*tail
;
262 g_assert(type
->meta_type
== SCHEMA_META_TYPE_OBJECT
);
264 for (tail
= type
->u
.object
.members
; tail
; tail
= tail
->next
) {
265 if (!tail
->value
->has_q_default
) {
273 static void add_query_tests(QmpSchema
*schema
)
275 SchemaInfoList
*tail
;
276 SchemaInfo
*si
, *arg_type
, *ret_type
;
279 /* Test the query-like commands */
280 for (tail
= schema
->list
; tail
; tail
= tail
->next
) {
282 if (si
->meta_type
!= SCHEMA_META_TYPE_COMMAND
) {
286 if (query_is_blacklisted(si
->name
)) {
290 arg_type
= qmp_schema_lookup(schema
, si
->u
.command
.arg_type
);
291 if (object_type_has_mandatory_members(arg_type
)) {
295 ret_type
= qmp_schema_lookup(schema
, si
->u
.command
.ret_type
);
296 if (ret_type
->meta_type
== SCHEMA_META_TYPE_OBJECT
297 && !ret_type
->u
.object
.members
) {
301 test_name
= g_strdup_printf("qmp/%s", si
->name
);
302 qtest_add_data_func(test_name
, si
->name
, test_query
);
307 int main(int argc
, char *argv
[])
312 g_test_init(&argc
, &argv
, NULL
);
314 qtest_add_func("qmp/protocol", test_qmp_protocol
);
315 qmp_schema_init(&schema
);
316 add_query_tests(&schema
);
320 qmp_schema_cleanup(&schema
);