block-backend: inline bdrv_co_get_geometry
[qemu/ar7.git] / tests / qtest / qmp-cmd-test.c
bloba58de48d2a487d9fa216451a868010aba19d4f6d
1 /*
2 * QMP command 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/qmp/qdict.h"
18 #include "qapi/qobject-input-visitor.h"
20 const char common_args[] = "-nodefaults -machine none";
22 /* Query smoke tests */
24 static int query_error_class(const char *cmd)
26 static struct {
27 const char *cmd;
28 int err_class;
29 } fails[] = {
30 /* Success depends on build configuration: */
31 #ifndef CONFIG_SPICE
32 { "query-spice", ERROR_CLASS_COMMAND_NOT_FOUND },
33 #endif
34 #ifndef CONFIG_TCG
35 { "query-replay", ERROR_CLASS_COMMAND_NOT_FOUND },
36 #endif
37 #ifndef CONFIG_VNC
38 { "query-vnc", ERROR_CLASS_GENERIC_ERROR },
39 { "query-vnc-servers", ERROR_CLASS_GENERIC_ERROR },
40 #endif
41 #ifndef CONFIG_REPLICATION
42 { "query-xen-replication-status", ERROR_CLASS_COMMAND_NOT_FOUND },
43 #endif
44 /* Likewise, and require special QEMU command-line arguments: */
45 { "query-acpi-ospm-status", ERROR_CLASS_GENERIC_ERROR },
46 { "query-balloon", ERROR_CLASS_DEVICE_NOT_ACTIVE },
47 { "query-hotpluggable-cpus", ERROR_CLASS_GENERIC_ERROR },
48 { "query-vm-generation-id", ERROR_CLASS_GENERIC_ERROR },
49 #ifndef CONFIG_PROFILER
50 { "x-query-profile", ERROR_CLASS_GENERIC_ERROR },
51 #endif
52 /* Only valid with a USB bus added */
53 { "x-query-usb", ERROR_CLASS_GENERIC_ERROR },
54 /* Only valid with accel=tcg */
55 { "x-query-jit", ERROR_CLASS_GENERIC_ERROR },
56 { "x-query-opcount", ERROR_CLASS_GENERIC_ERROR },
57 { "xen-event-list", ERROR_CLASS_GENERIC_ERROR },
58 { NULL, -1 }
60 int i;
62 for (i = 0; fails[i].cmd; i++) {
63 if (!strcmp(cmd, fails[i].cmd)) {
64 return fails[i].err_class;
67 return -1;
70 static void test_query(const void *data)
72 const char *cmd = data;
73 int expected_error_class = query_error_class(cmd);
74 QDict *resp, *error;
75 const char *error_class;
76 QTestState *qts;
78 qts = qtest_init(common_args);
80 resp = qtest_qmp(qts, "{ 'execute': %s }", cmd);
81 error = qdict_get_qdict(resp, "error");
82 error_class = error ? qdict_get_str(error, "class") : NULL;
84 if (expected_error_class < 0) {
85 g_assert(qdict_haskey(resp, "return"));
86 } else {
87 g_assert(error);
88 g_assert_cmpint(qapi_enum_parse(&QapiErrorClass_lookup, error_class,
89 -1, &error_abort),
90 ==, expected_error_class);
92 qobject_unref(resp);
94 qtest_quit(qts);
97 static bool query_is_ignored(const char *cmd)
99 const char *ignored[] = {
100 /* Not actually queries: */
101 "add-fd",
102 /* Success depends on target arch: */
103 "query-cpu-definitions", /* arm, i386, ppc, s390x */
104 "query-gic-capabilities", /* arm */
105 /* Success depends on target-specific build configuration: */
106 "query-pci", /* CONFIG_PCI */
107 "x-query-virtio", /* CONFIG_VIRTIO */
108 /* Success depends on launching SEV guest */
109 "query-sev-launch-measure",
110 /* Success depends on Host or Hypervisor SEV support */
111 "query-sev",
112 "query-sev-capabilities",
113 "query-sgx",
114 "query-sgx-capabilities",
115 /* Success depends on enabling dirty page rate limit */
116 "query-vcpu-dirty-limit",
117 NULL
119 int i;
121 for (i = 0; ignored[i]; i++) {
122 if (!strcmp(cmd, ignored[i])) {
123 return true;
126 return false;
129 typedef struct {
130 SchemaInfoList *list;
131 GHashTable *hash;
132 } QmpSchema;
134 static void qmp_schema_init(QmpSchema *schema)
136 QDict *resp;
137 Visitor *qiv;
138 SchemaInfoList *tail;
139 QTestState *qts;
141 qts = qtest_init(common_args);
143 resp = qtest_qmp(qts, "{ 'execute': 'query-qmp-schema' }");
145 qiv = qobject_input_visitor_new(qdict_get(resp, "return"));
146 visit_type_SchemaInfoList(qiv, NULL, &schema->list, &error_abort);
147 visit_free(qiv);
149 qobject_unref(resp);
150 qtest_quit(qts);
152 schema->hash = g_hash_table_new(g_str_hash, g_str_equal);
154 /* Build @schema: hash table mapping entity name to SchemaInfo */
155 for (tail = schema->list; tail; tail = tail->next) {
156 g_hash_table_insert(schema->hash, tail->value->name, tail->value);
160 static SchemaInfo *qmp_schema_lookup(QmpSchema *schema, const char *name)
162 return g_hash_table_lookup(schema->hash, name);
165 static void qmp_schema_cleanup(QmpSchema *schema)
167 qapi_free_SchemaInfoList(schema->list);
168 g_hash_table_destroy(schema->hash);
171 static bool object_type_has_mandatory_members(SchemaInfo *type)
173 SchemaInfoObjectMemberList *tail;
175 g_assert(type->meta_type == SCHEMA_META_TYPE_OBJECT);
177 for (tail = type->u.object.members; tail; tail = tail->next) {
178 if (!tail->value->q_default) {
179 return true;
183 return false;
186 static void add_query_tests(QmpSchema *schema)
188 SchemaInfoList *tail;
189 SchemaInfo *si, *arg_type, *ret_type;
190 char *test_name;
192 /* Test the query-like commands */
193 for (tail = schema->list; tail; tail = tail->next) {
194 si = tail->value;
195 if (si->meta_type != SCHEMA_META_TYPE_COMMAND) {
196 continue;
199 if (query_is_ignored(si->name)) {
200 continue;
203 arg_type = qmp_schema_lookup(schema, si->u.command.arg_type);
204 if (object_type_has_mandatory_members(arg_type)) {
205 continue;
208 ret_type = qmp_schema_lookup(schema, si->u.command.ret_type);
209 if (ret_type->meta_type == SCHEMA_META_TYPE_OBJECT
210 && !ret_type->u.object.members) {
211 continue;
214 test_name = g_strdup_printf("qmp/%s", si->name);
215 qtest_add_data_func(test_name, si->name, test_query);
216 g_free(test_name);
220 static void test_object_add_failure_modes(void)
222 QTestState *qts;
223 QDict *resp;
225 /* attempt to create an object without props */
226 qts = qtest_init(common_args);
227 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
228 " {'qom-type': 'memory-backend-ram', 'id': 'ram1' } }");
229 g_assert_nonnull(resp);
230 qmp_expect_error_and_unref(resp, "GenericError");
232 /* attempt to create an object without qom-type */
233 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
234 " {'id': 'ram1' } }");
235 g_assert_nonnull(resp);
236 qmp_expect_error_and_unref(resp, "GenericError");
238 /* attempt to delete an object that does not exist */
239 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
240 " {'id': 'ram1' } }");
241 g_assert_nonnull(resp);
242 qmp_expect_error_and_unref(resp, "GenericError");
244 /* attempt to create 2 objects with duplicate id */
245 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
246 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
247 " 'size': 1048576 } }");
248 g_assert_nonnull(resp);
249 g_assert(qdict_haskey(resp, "return"));
250 qobject_unref(resp);
252 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
253 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
254 " 'size': 1048576 } }");
255 g_assert_nonnull(resp);
256 qmp_expect_error_and_unref(resp, "GenericError");
258 /* delete ram1 object */
259 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
260 " {'id': 'ram1' } }");
261 g_assert_nonnull(resp);
262 g_assert(qdict_haskey(resp, "return"));
263 qobject_unref(resp);
265 /* attempt to create an object with a property of a wrong type */
266 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
267 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
268 " 'size': '1048576' } }");
269 g_assert_nonnull(resp);
270 /* now do it right */
271 qmp_expect_error_and_unref(resp, "GenericError");
273 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
274 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
275 " 'size': 1048576 } }");
276 g_assert_nonnull(resp);
277 g_assert(qdict_haskey(resp, "return"));
278 qobject_unref(resp);
280 /* delete ram1 object */
281 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
282 " {'id': 'ram1' } }");
283 g_assert_nonnull(resp);
284 g_assert(qdict_haskey(resp, "return"));
285 qobject_unref(resp);
287 /* attempt to create an object without the id */
288 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
289 " {'qom-type': 'memory-backend-ram',"
290 " 'size': 1048576 } }");
291 g_assert_nonnull(resp);
292 qmp_expect_error_and_unref(resp, "GenericError");
294 /* now do it right */
295 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
296 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
297 " 'size': 1048576 } }");
298 g_assert_nonnull(resp);
299 g_assert(qdict_haskey(resp, "return"));
300 qobject_unref(resp);
302 /* delete ram1 object */
303 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
304 " {'id': 'ram1' } }");
305 g_assert_nonnull(resp);
306 g_assert(qdict_haskey(resp, "return"));
307 qobject_unref(resp);
309 /* attempt to set a non existing property */
310 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
311 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
312 " 'sized': 1048576 } }");
313 g_assert_nonnull(resp);
314 qmp_expect_error_and_unref(resp, "GenericError");
316 /* now do it right */
317 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
318 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
319 " 'size': 1048576 } }");
320 g_assert_nonnull(resp);
321 g_assert(qdict_haskey(resp, "return"));
322 qobject_unref(resp);
324 /* delete ram1 object without id */
325 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
326 " {'ida': 'ram1' } }");
327 g_assert_nonnull(resp);
328 qobject_unref(resp);
330 /* delete ram1 object */
331 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
332 " {'id': 'ram1' } }");
333 g_assert_nonnull(resp);
334 g_assert(qdict_haskey(resp, "return"));
335 qobject_unref(resp);
337 /* delete ram1 object that does not exist anymore*/
338 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
339 " {'id': 'ram1' } }");
340 g_assert_nonnull(resp);
341 qmp_expect_error_and_unref(resp, "GenericError");
343 qtest_quit(qts);
346 int main(int argc, char *argv[])
348 QmpSchema schema;
349 int ret;
351 g_test_init(&argc, &argv, NULL);
353 qmp_schema_init(&schema);
354 add_query_tests(&schema);
356 qtest_add_func("qmp/object-add-failure-modes",
357 test_object_add_failure_modes);
359 ret = g_test_run();
361 qmp_schema_cleanup(&schema);
362 return ret;