hw/net: fix vmxnet3 live migration
[qemu/ar7.git] / tests / device-introspect-test.c
blob04f22903b0114d00c9efd32ec8e9a343b8f9bb59
1 /*
2 * Device introspection test cases
4 * Copyright (c) 2015 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.
14 * Covers QMP device-list-properties and HMP device_add help. We
15 * currently don't check that their output makes sense, only that QEMU
16 * survives. Useful since we've had an astounding number of crash
17 * bugs around here.
20 #include "qemu/osdep.h"
21 #include "qemu-common.h"
22 #include "qapi/qmp/qstring.h"
23 #include "qapi/qmp/qdict.h"
24 #include "qapi/qmp/qlist.h"
25 #include "libqtest.h"
27 const char common_args[] = "-nodefaults -machine none";
29 static QList *qom_list_types(QTestState * qts, const char *implements,
30 bool abstract)
32 QDict *resp;
33 QList *ret;
34 QDict *args = qdict_new();
36 qdict_put_bool(args, "abstract", abstract);
37 if (implements) {
38 qdict_put_str(args, "implements", implements);
40 resp = qtest_qmp(qts, "{'execute': 'qom-list-types', 'arguments': %p }",
41 args);
42 g_assert(qdict_haskey(resp, "return"));
43 ret = qdict_get_qlist(resp, "return");
44 qobject_ref(ret);
45 qobject_unref(resp);
46 return ret;
49 /* Build a name -> ObjectTypeInfo index from a ObjectTypeInfo list */
50 static QDict *qom_type_index(QList *types)
52 QDict *index = qdict_new();
53 QListEntry *e;
55 QLIST_FOREACH_ENTRY(types, e) {
56 QDict *d = qobject_to(QDict, qlist_entry_obj(e));
57 const char *name = qdict_get_str(d, "name");
58 qobject_ref(d);
59 qdict_put(index, name, d);
61 return index;
64 /* Check if @parent is present in the parent chain of @type */
65 static bool qom_has_parent(QDict *index, const char *type, const char *parent)
67 while (type) {
68 QDict *d = qdict_get_qdict(index, type);
69 const char *p = d && qdict_haskey(d, "parent") ?
70 qdict_get_str(d, "parent") :
71 NULL;
73 if (!strcmp(type, parent)) {
74 return true;
77 type = p;
80 return false;
83 /* Find an entry on a list returned by qom-list-types */
84 static QDict *type_list_find(QList *types, const char *name)
86 QListEntry *e;
88 QLIST_FOREACH_ENTRY(types, e) {
89 QDict *d = qobject_to(QDict, qlist_entry_obj(e));
90 const char *ename = qdict_get_str(d, "name");
91 if (!strcmp(ename, name)) {
92 return d;
96 return NULL;
99 static QList *device_type_list(QTestState *qts, bool abstract)
101 return qom_list_types(qts, "device", abstract);
104 static void test_one_device(QTestState *qts, const char *type)
106 QDict *resp;
107 char *help;
108 char *qom_tree_start, *qom_tree_end;
109 char *qtree_start, *qtree_end;
111 g_test_message("Testing device '%s'", type);
113 qom_tree_start = qtest_hmp(qts, "info qom-tree");
114 qtree_start = qtest_hmp(qts, "info qtree");
116 resp = qtest_qmp(qts, "{'execute': 'device-list-properties',"
117 " 'arguments': {'typename': %s}}",
118 type);
119 qobject_unref(resp);
121 help = qtest_hmp(qts, "device_add \"%s,help\"", type);
122 g_free(help);
125 * Some devices leave dangling pointers in QOM behind.
126 * "info qom-tree" or "info qtree" have a good chance at crashing then.
127 * Also make sure that the tree did not change.
129 qom_tree_end = qtest_hmp(qts, "info qom-tree");
130 g_assert_cmpstr(qom_tree_start, ==, qom_tree_end);
131 g_free(qom_tree_start);
132 g_free(qom_tree_end);
134 qtree_end = qtest_hmp(qts, "info qtree");
135 g_assert_cmpstr(qtree_start, ==, qtree_end);
136 g_free(qtree_start);
137 g_free(qtree_end);
140 static void test_device_intro_list(void)
142 QList *types;
143 char *help;
144 QTestState *qts;
146 qts = qtest_init(common_args);
148 types = device_type_list(qts, true);
149 qobject_unref(types);
151 help = qtest_hmp(qts, "device_add help");
152 g_free(help);
154 qtest_quit(qts);
158 * Ensure all entries returned by qom-list-types implements=<parent>
159 * have <parent> as a parent.
161 static void test_qom_list_parents(QTestState *qts, const char *parent)
163 QList *types;
164 QListEntry *e;
165 QDict *index;
167 types = qom_list_types(qts, parent, true);
168 index = qom_type_index(types);
170 QLIST_FOREACH_ENTRY(types, e) {
171 QDict *d = qobject_to(QDict, qlist_entry_obj(e));
172 const char *name = qdict_get_str(d, "name");
174 g_assert(qom_has_parent(index, name, parent));
177 qobject_unref(types);
178 qobject_unref(index);
181 static void test_qom_list_fields(void)
183 QList *all_types;
184 QList *non_abstract;
185 QListEntry *e;
186 QTestState *qts;
188 qts = qtest_init(common_args);
190 all_types = qom_list_types(qts, NULL, true);
191 non_abstract = qom_list_types(qts, NULL, false);
193 QLIST_FOREACH_ENTRY(all_types, e) {
194 QDict *d = qobject_to(QDict, qlist_entry_obj(e));
195 const char *name = qdict_get_str(d, "name");
196 bool abstract = qdict_haskey(d, "abstract") ?
197 qdict_get_bool(d, "abstract") :
198 false;
199 bool expected_abstract = !type_list_find(non_abstract, name);
201 g_assert(abstract == expected_abstract);
204 test_qom_list_parents(qts, "object");
205 test_qom_list_parents(qts, "device");
206 test_qom_list_parents(qts, "sys-bus-device");
208 qobject_unref(all_types);
209 qobject_unref(non_abstract);
210 qtest_quit(qts);
213 static void test_device_intro_none(void)
215 QTestState *qts = qtest_init(common_args);
217 test_one_device(qts, "nonexistent");
218 qtest_quit(qts);
221 static void test_device_intro_abstract(void)
223 QTestState *qts = qtest_init(common_args);
225 test_one_device(qts, "device");
226 qtest_quit(qts);
229 static void test_device_intro_concrete(const void *args)
231 QList *types;
232 QListEntry *entry;
233 const char *type;
234 QTestState *qts;
236 qts = qtest_init(args);
237 types = device_type_list(qts, false);
239 QLIST_FOREACH_ENTRY(types, entry) {
240 type = qdict_get_try_str(qobject_to(QDict, qlist_entry_obj(entry)),
241 "name");
242 g_assert(type);
243 test_one_device(qts, type);
246 qobject_unref(types);
247 qtest_quit(qts);
248 g_free((void *)args);
251 static void test_abstract_interfaces(void)
253 QList *all_types;
254 QListEntry *e;
255 QDict *index;
256 QTestState *qts;
258 qts = qtest_init(common_args);
260 all_types = qom_list_types(qts, "interface", true);
261 index = qom_type_index(all_types);
263 QLIST_FOREACH_ENTRY(all_types, e) {
264 QDict *d = qobject_to(QDict, qlist_entry_obj(e));
265 const char *name = qdict_get_str(d, "name");
268 * qom-list-types implements=interface returns all types
269 * that implement _any_ interface (not just interface
270 * types), so skip the ones that don't have "interface"
271 * on the parent type chain.
273 if (!qom_has_parent(index, name, "interface")) {
274 /* Not an interface type */
275 continue;
278 g_assert(qdict_haskey(d, "abstract") && qdict_get_bool(d, "abstract"));
281 qobject_unref(all_types);
282 qobject_unref(index);
283 qtest_quit(qts);
286 static void add_machine_test_case(const char *mname)
288 char *path, *args;
290 /* Ignore blacklisted machines */
291 if (g_str_equal("xenfv", mname) || g_str_equal("xenpv", mname)) {
292 return;
295 path = g_strdup_printf("device/introspect/concrete/defaults/%s", mname);
296 args = g_strdup_printf("-M %s", mname);
297 qtest_add_data_func(path, args, test_device_intro_concrete);
298 g_free(path);
300 path = g_strdup_printf("device/introspect/concrete/nodefaults/%s", mname);
301 args = g_strdup_printf("-nodefaults -M %s", mname);
302 qtest_add_data_func(path, args, test_device_intro_concrete);
303 g_free(path);
306 int main(int argc, char **argv)
308 g_test_init(&argc, &argv, NULL);
310 qtest_add_func("device/introspect/list", test_device_intro_list);
311 qtest_add_func("device/introspect/list-fields", test_qom_list_fields);
312 qtest_add_func("device/introspect/none", test_device_intro_none);
313 qtest_add_func("device/introspect/abstract", test_device_intro_abstract);
314 qtest_add_func("device/introspect/abstract-interfaces", test_abstract_interfaces);
315 if (g_test_quick()) {
316 qtest_add_data_func("device/introspect/concrete/defaults/none",
317 g_strdup(common_args), test_device_intro_concrete);
318 } else {
319 qtest_cb_for_every_machine(add_machine_test_case, true);
322 return g_test_run();