xref: /qemu/tests/qtest/qmp-cmd-test.c (revision 57d874c4c7a0acbaa076a166e3da093b6edbdb0f)
1 /*
2  * QMP command 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 "libqos/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"
19 
20 const char common_args[] = "-nodefaults -machine none";
21 
22 /* Query smoke tests */
23 
24 static int query_error_class(const char *cmd)
25 {
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         { NULL, -1 }
50     };
51     int i;
52 
53     for (i = 0; fails[i].cmd; i++) {
54         if (!strcmp(cmd, fails[i].cmd)) {
55             return fails[i].err_class;
56         }
57     }
58     return -1;
59 }
60 
61 static void test_query(const void *data)
62 {
63     const char *cmd = data;
64     int expected_error_class = query_error_class(cmd);
65     QDict *resp, *error;
66     const char *error_class;
67     QTestState *qts;
68 
69     qts = qtest_init(common_args);
70 
71     resp = qtest_qmp(qts, "{ 'execute': %s }", cmd);
72     error = qdict_get_qdict(resp, "error");
73     error_class = error ? qdict_get_str(error, "class") : NULL;
74 
75     if (expected_error_class < 0) {
76         g_assert(qdict_haskey(resp, "return"));
77     } else {
78         g_assert(error);
79         g_assert_cmpint(qapi_enum_parse(&QapiErrorClass_lookup, error_class,
80                                         -1, &error_abort),
81                         ==, expected_error_class);
82     }
83     qobject_unref(resp);
84 
85     qtest_quit(qts);
86 }
87 
88 static bool query_is_ignored(const char *cmd)
89 {
90     const char *ignored[] = {
91         /* Not actually queries: */
92         "add-fd",
93         /* Success depends on target arch: */
94         "query-cpu-definitions",  /* arm, i386, ppc, s390x */
95         "query-gic-capabilities", /* arm */
96         /* Success depends on target-specific build configuration: */
97         "query-pci",              /* CONFIG_PCI */
98         /* Success depends on launching SEV guest */
99         "query-sev-launch-measure",
100         /* Success depends on Host or Hypervisor SEV support */
101         "query-sev",
102         "query-sev-capabilities",
103         "query-sgx",
104         NULL
105     };
106     int i;
107 
108     for (i = 0; ignored[i]; i++) {
109         if (!strcmp(cmd, ignored[i])) {
110             return true;
111         }
112     }
113     return false;
114 }
115 
116 typedef struct {
117     SchemaInfoList *list;
118     GHashTable *hash;
119 } QmpSchema;
120 
121 static void qmp_schema_init(QmpSchema *schema)
122 {
123     QDict *resp;
124     Visitor *qiv;
125     SchemaInfoList *tail;
126     QTestState *qts;
127 
128     qts = qtest_init(common_args);
129 
130     resp = qtest_qmp(qts, "{ 'execute': 'query-qmp-schema' }");
131 
132     qiv = qobject_input_visitor_new(qdict_get(resp, "return"));
133     visit_type_SchemaInfoList(qiv, NULL, &schema->list, &error_abort);
134     visit_free(qiv);
135 
136     qobject_unref(resp);
137     qtest_quit(qts);
138 
139     schema->hash = g_hash_table_new(g_str_hash, g_str_equal);
140 
141     /* Build @schema: hash table mapping entity name to SchemaInfo */
142     for (tail = schema->list; tail; tail = tail->next) {
143         g_hash_table_insert(schema->hash, tail->value->name, tail->value);
144     }
145 }
146 
147 static SchemaInfo *qmp_schema_lookup(QmpSchema *schema, const char *name)
148 {
149     return g_hash_table_lookup(schema->hash, name);
150 }
151 
152 static void qmp_schema_cleanup(QmpSchema *schema)
153 {
154     qapi_free_SchemaInfoList(schema->list);
155     g_hash_table_destroy(schema->hash);
156 }
157 
158 static bool object_type_has_mandatory_members(SchemaInfo *type)
159 {
160     SchemaInfoObjectMemberList *tail;
161 
162     g_assert(type->meta_type == SCHEMA_META_TYPE_OBJECT);
163 
164     for (tail = type->u.object.members; tail; tail = tail->next) {
165         if (!tail->value->has_q_default) {
166             return true;
167         }
168     }
169 
170     return false;
171 }
172 
173 static void add_query_tests(QmpSchema *schema)
174 {
175     SchemaInfoList *tail;
176     SchemaInfo *si, *arg_type, *ret_type;
177     char *test_name;
178 
179     /* Test the query-like commands */
180     for (tail = schema->list; tail; tail = tail->next) {
181         si = tail->value;
182         if (si->meta_type != SCHEMA_META_TYPE_COMMAND) {
183             continue;
184         }
185 
186         if (query_is_ignored(si->name)) {
187             continue;
188         }
189 
190         arg_type = qmp_schema_lookup(schema, si->u.command.arg_type);
191         if (object_type_has_mandatory_members(arg_type)) {
192             continue;
193         }
194 
195         ret_type = qmp_schema_lookup(schema, si->u.command.ret_type);
196         if (ret_type->meta_type == SCHEMA_META_TYPE_OBJECT
197             && !ret_type->u.object.members) {
198             continue;
199         }
200 
201         test_name = g_strdup_printf("qmp/%s", si->name);
202         qtest_add_data_func(test_name, si->name, test_query);
203         g_free(test_name);
204     }
205 }
206 
207 static void test_object_add_failure_modes(void)
208 {
209     QTestState *qts;
210     QDict *resp;
211 
212     /* attempt to create an object without props */
213     qts = qtest_init(common_args);
214     resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
215                      " {'qom-type': 'memory-backend-ram', 'id': 'ram1' } }");
216     g_assert_nonnull(resp);
217     qmp_expect_error_and_unref(resp, "GenericError");
218 
219     /* attempt to create an object without qom-type */
220     resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
221                      " {'id': 'ram1' } }");
222     g_assert_nonnull(resp);
223     qmp_expect_error_and_unref(resp, "GenericError");
224 
225     /* attempt to delete an object that does not exist */
226     resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
227                      " {'id': 'ram1' } }");
228     g_assert_nonnull(resp);
229     qmp_expect_error_and_unref(resp, "GenericError");
230 
231     /* attempt to create 2 objects with duplicate id */
232     resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
233                      " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
234                      " 'size': 1048576 } }");
235     g_assert_nonnull(resp);
236     g_assert(qdict_haskey(resp, "return"));
237     qobject_unref(resp);
238 
239     resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
240                      " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
241                      " 'size': 1048576 } }");
242     g_assert_nonnull(resp);
243     qmp_expect_error_and_unref(resp, "GenericError");
244 
245     /* delete ram1 object */
246     resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
247                      " {'id': 'ram1' } }");
248     g_assert_nonnull(resp);
249     g_assert(qdict_haskey(resp, "return"));
250     qobject_unref(resp);
251 
252     /* attempt to create an object with a property of a wrong type */
253     resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
254                      " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
255                      " 'size': '1048576' } }");
256     g_assert_nonnull(resp);
257     /* now do it right */
258     qmp_expect_error_and_unref(resp, "GenericError");
259 
260     resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
261                      " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
262                      " 'size': 1048576 } }");
263     g_assert_nonnull(resp);
264     g_assert(qdict_haskey(resp, "return"));
265     qobject_unref(resp);
266 
267     /* delete ram1 object */
268     resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
269                      " {'id': 'ram1' } }");
270     g_assert_nonnull(resp);
271     g_assert(qdict_haskey(resp, "return"));
272     qobject_unref(resp);
273 
274     /* attempt to create an object without the id */
275     resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
276                      " {'qom-type': 'memory-backend-ram',"
277                      " 'size': 1048576 } }");
278     g_assert_nonnull(resp);
279     qmp_expect_error_and_unref(resp, "GenericError");
280 
281     /* now do it right */
282     resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
283                      " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
284                      " 'size': 1048576 } }");
285     g_assert_nonnull(resp);
286     g_assert(qdict_haskey(resp, "return"));
287     qobject_unref(resp);
288 
289     /* delete ram1 object */
290     resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
291                      " {'id': 'ram1' } }");
292     g_assert_nonnull(resp);
293     g_assert(qdict_haskey(resp, "return"));
294     qobject_unref(resp);
295 
296     /* attempt to set a non existing property */
297     resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
298                      " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
299                      " 'sized': 1048576 } }");
300     g_assert_nonnull(resp);
301     qmp_expect_error_and_unref(resp, "GenericError");
302 
303     /* now do it right */
304     resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
305                      " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
306                      " 'size': 1048576 } }");
307     g_assert_nonnull(resp);
308     g_assert(qdict_haskey(resp, "return"));
309     qobject_unref(resp);
310 
311     /* delete ram1 object without id */
312     resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
313                      " {'ida': 'ram1' } }");
314     g_assert_nonnull(resp);
315     qobject_unref(resp);
316 
317     /* delete ram1 object */
318     resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
319                      " {'id': 'ram1' } }");
320     g_assert_nonnull(resp);
321     g_assert(qdict_haskey(resp, "return"));
322     qobject_unref(resp);
323 
324     /* delete ram1 object that does not exist anymore*/
325     resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
326                      " {'id': 'ram1' } }");
327     g_assert_nonnull(resp);
328     qmp_expect_error_and_unref(resp, "GenericError");
329 
330     qtest_quit(qts);
331 }
332 
333 int main(int argc, char *argv[])
334 {
335     QmpSchema schema;
336     int ret;
337 
338     g_test_init(&argc, &argv, NULL);
339 
340     qmp_schema_init(&schema);
341     add_query_tests(&schema);
342 
343     qtest_add_func("qmp/object-add-failure-modes",
344                    test_object_add_failure_modes);
345 
346     ret = g_test_run();
347 
348     qmp_schema_cleanup(&schema);
349     return ret;
350 }
351