xref: /openbmc/qemu/tests/qtest/qmp-cmd-test.c (revision 500eb21c)
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         "query-sgx-capabilities",
105         NULL
106     };
107     int i;
108 
109     for (i = 0; ignored[i]; i++) {
110         if (!strcmp(cmd, ignored[i])) {
111             return true;
112         }
113     }
114     return false;
115 }
116 
117 typedef struct {
118     SchemaInfoList *list;
119     GHashTable *hash;
120 } QmpSchema;
121 
122 static void qmp_schema_init(QmpSchema *schema)
123 {
124     QDict *resp;
125     Visitor *qiv;
126     SchemaInfoList *tail;
127     QTestState *qts;
128 
129     qts = qtest_init(common_args);
130 
131     resp = qtest_qmp(qts, "{ 'execute': 'query-qmp-schema' }");
132 
133     qiv = qobject_input_visitor_new(qdict_get(resp, "return"));
134     visit_type_SchemaInfoList(qiv, NULL, &schema->list, &error_abort);
135     visit_free(qiv);
136 
137     qobject_unref(resp);
138     qtest_quit(qts);
139 
140     schema->hash = g_hash_table_new(g_str_hash, g_str_equal);
141 
142     /* Build @schema: hash table mapping entity name to SchemaInfo */
143     for (tail = schema->list; tail; tail = tail->next) {
144         g_hash_table_insert(schema->hash, tail->value->name, tail->value);
145     }
146 }
147 
148 static SchemaInfo *qmp_schema_lookup(QmpSchema *schema, const char *name)
149 {
150     return g_hash_table_lookup(schema->hash, name);
151 }
152 
153 static void qmp_schema_cleanup(QmpSchema *schema)
154 {
155     qapi_free_SchemaInfoList(schema->list);
156     g_hash_table_destroy(schema->hash);
157 }
158 
159 static bool object_type_has_mandatory_members(SchemaInfo *type)
160 {
161     SchemaInfoObjectMemberList *tail;
162 
163     g_assert(type->meta_type == SCHEMA_META_TYPE_OBJECT);
164 
165     for (tail = type->u.object.members; tail; tail = tail->next) {
166         if (!tail->value->has_q_default) {
167             return true;
168         }
169     }
170 
171     return false;
172 }
173 
174 static void add_query_tests(QmpSchema *schema)
175 {
176     SchemaInfoList *tail;
177     SchemaInfo *si, *arg_type, *ret_type;
178     char *test_name;
179 
180     /* Test the query-like commands */
181     for (tail = schema->list; tail; tail = tail->next) {
182         si = tail->value;
183         if (si->meta_type != SCHEMA_META_TYPE_COMMAND) {
184             continue;
185         }
186 
187         if (query_is_ignored(si->name)) {
188             continue;
189         }
190 
191         arg_type = qmp_schema_lookup(schema, si->u.command.arg_type);
192         if (object_type_has_mandatory_members(arg_type)) {
193             continue;
194         }
195 
196         ret_type = qmp_schema_lookup(schema, si->u.command.ret_type);
197         if (ret_type->meta_type == SCHEMA_META_TYPE_OBJECT
198             && !ret_type->u.object.members) {
199             continue;
200         }
201 
202         test_name = g_strdup_printf("qmp/%s", si->name);
203         qtest_add_data_func(test_name, si->name, test_query);
204         g_free(test_name);
205     }
206 }
207 
208 static void test_object_add_failure_modes(void)
209 {
210     QTestState *qts;
211     QDict *resp;
212 
213     /* attempt to create an object without props */
214     qts = qtest_init(common_args);
215     resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
216                      " {'qom-type': 'memory-backend-ram', 'id': 'ram1' } }");
217     g_assert_nonnull(resp);
218     qmp_expect_error_and_unref(resp, "GenericError");
219 
220     /* attempt to create an object without qom-type */
221     resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
222                      " {'id': 'ram1' } }");
223     g_assert_nonnull(resp);
224     qmp_expect_error_and_unref(resp, "GenericError");
225 
226     /* attempt to delete an object that does not exist */
227     resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
228                      " {'id': 'ram1' } }");
229     g_assert_nonnull(resp);
230     qmp_expect_error_and_unref(resp, "GenericError");
231 
232     /* attempt to create 2 objects with duplicate id */
233     resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
234                      " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
235                      " 'size': 1048576 } }");
236     g_assert_nonnull(resp);
237     g_assert(qdict_haskey(resp, "return"));
238     qobject_unref(resp);
239 
240     resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
241                      " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
242                      " 'size': 1048576 } }");
243     g_assert_nonnull(resp);
244     qmp_expect_error_and_unref(resp, "GenericError");
245 
246     /* delete ram1 object */
247     resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
248                      " {'id': 'ram1' } }");
249     g_assert_nonnull(resp);
250     g_assert(qdict_haskey(resp, "return"));
251     qobject_unref(resp);
252 
253     /* attempt to create an object with a property of a wrong type */
254     resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
255                      " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
256                      " 'size': '1048576' } }");
257     g_assert_nonnull(resp);
258     /* now do it right */
259     qmp_expect_error_and_unref(resp, "GenericError");
260 
261     resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
262                      " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
263                      " 'size': 1048576 } }");
264     g_assert_nonnull(resp);
265     g_assert(qdict_haskey(resp, "return"));
266     qobject_unref(resp);
267 
268     /* delete ram1 object */
269     resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
270                      " {'id': 'ram1' } }");
271     g_assert_nonnull(resp);
272     g_assert(qdict_haskey(resp, "return"));
273     qobject_unref(resp);
274 
275     /* attempt to create an object without the id */
276     resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
277                      " {'qom-type': 'memory-backend-ram',"
278                      " 'size': 1048576 } }");
279     g_assert_nonnull(resp);
280     qmp_expect_error_and_unref(resp, "GenericError");
281 
282     /* now do it right */
283     resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
284                      " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
285                      " 'size': 1048576 } }");
286     g_assert_nonnull(resp);
287     g_assert(qdict_haskey(resp, "return"));
288     qobject_unref(resp);
289 
290     /* delete ram1 object */
291     resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
292                      " {'id': 'ram1' } }");
293     g_assert_nonnull(resp);
294     g_assert(qdict_haskey(resp, "return"));
295     qobject_unref(resp);
296 
297     /* attempt to set a non existing property */
298     resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
299                      " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
300                      " 'sized': 1048576 } }");
301     g_assert_nonnull(resp);
302     qmp_expect_error_and_unref(resp, "GenericError");
303 
304     /* now do it right */
305     resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
306                      " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
307                      " 'size': 1048576 } }");
308     g_assert_nonnull(resp);
309     g_assert(qdict_haskey(resp, "return"));
310     qobject_unref(resp);
311 
312     /* delete ram1 object without id */
313     resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
314                      " {'ida': 'ram1' } }");
315     g_assert_nonnull(resp);
316     qobject_unref(resp);
317 
318     /* delete ram1 object */
319     resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
320                      " {'id': 'ram1' } }");
321     g_assert_nonnull(resp);
322     g_assert(qdict_haskey(resp, "return"));
323     qobject_unref(resp);
324 
325     /* delete ram1 object that does not exist anymore*/
326     resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
327                      " {'id': 'ram1' } }");
328     g_assert_nonnull(resp);
329     qmp_expect_error_and_unref(resp, "GenericError");
330 
331     qtest_quit(qts);
332 }
333 
334 int main(int argc, char *argv[])
335 {
336     QmpSchema schema;
337     int ret;
338 
339     g_test_init(&argc, &argv, NULL);
340 
341     qmp_schema_init(&schema);
342     add_query_tests(&schema);
343 
344     qtest_add_func("qmp/object-add-failure-modes",
345                    test_object_add_failure_modes);
346 
347     ret = g_test_run();
348 
349     qmp_schema_cleanup(&schema);
350     return ret;
351 }
352