tests/device-introspection: Check that the qom-tree and qtree do not change
[qemu/kevin.git] / tests / device-introspect-test.c
bloba38193b2c7b513e14df0e6ef23e88b6bc771298e
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(const char *implements, bool abstract)
31 QDict *resp;
32 QList *ret;
33 QDict *args = qdict_new();
35 qdict_put_bool(args, "abstract", abstract);
36 if (implements) {
37 qdict_put_str(args, "implements", implements);
39 resp = qmp("{'execute': 'qom-list-types',"
40 " 'arguments': %p }", args);
41 g_assert(qdict_haskey(resp, "return"));
42 ret = qdict_get_qlist(resp, "return");
43 qobject_ref(ret);
44 qobject_unref(resp);
45 return ret;
48 /* Build a name -> ObjectTypeInfo index from a ObjectTypeInfo list */
49 static QDict *qom_type_index(QList *types)
51 QDict *index = qdict_new();
52 QListEntry *e;
54 QLIST_FOREACH_ENTRY(types, e) {
55 QDict *d = qobject_to(QDict, qlist_entry_obj(e));
56 const char *name = qdict_get_str(d, "name");
57 qobject_ref(d);
58 qdict_put(index, name, d);
60 return index;
63 /* Check if @parent is present in the parent chain of @type */
64 static bool qom_has_parent(QDict *index, const char *type, const char *parent)
66 while (type) {
67 QDict *d = qdict_get_qdict(index, type);
68 const char *p = d && qdict_haskey(d, "parent") ?
69 qdict_get_str(d, "parent") :
70 NULL;
72 if (!strcmp(type, parent)) {
73 return true;
76 type = p;
79 return false;
82 /* Find an entry on a list returned by qom-list-types */
83 static QDict *type_list_find(QList *types, const char *name)
85 QListEntry *e;
87 QLIST_FOREACH_ENTRY(types, e) {
88 QDict *d = qobject_to(QDict, qlist_entry_obj(e));
89 const char *ename = qdict_get_str(d, "name");
90 if (!strcmp(ename, name)) {
91 return d;
95 return NULL;
98 static QList *device_type_list(bool abstract)
100 return qom_list_types("device", abstract);
103 static void test_one_device(const char *type)
105 QDict *resp;
106 char *help;
107 char *qom_tree_start, *qom_tree_end;
108 char *qtree_start, *qtree_end;
110 g_test_message("Testing device '%s'", type);
112 qom_tree_start = hmp("info qom-tree");
113 qtree_start = hmp("info qtree");
115 resp = qmp("{'execute': 'device-list-properties',"
116 " 'arguments': {'typename': %s}}",
117 type);
118 qobject_unref(resp);
120 help = hmp("device_add \"%s,help\"", type);
121 g_free(help);
124 * Some devices leave dangling pointers in QOM behind.
125 * "info qom-tree" or "info qtree" have a good chance at crashing then.
126 * Also make sure that the tree did not change.
128 qom_tree_end = hmp("info qom-tree");
129 g_assert_cmpstr(qom_tree_start, ==, qom_tree_end);
130 g_free(qom_tree_start);
131 g_free(qom_tree_end);
133 qtree_end = hmp("info qtree");
134 g_assert_cmpstr(qtree_start, ==, qtree_end);
135 g_free(qtree_start);
136 g_free(qtree_end);
139 static void test_device_intro_list(void)
141 QList *types;
142 char *help;
144 qtest_start(common_args);
146 types = device_type_list(true);
147 qobject_unref(types);
149 help = hmp("device_add help");
150 g_free(help);
152 qtest_end();
156 * Ensure all entries returned by qom-list-types implements=<parent>
157 * have <parent> as a parent.
159 static void test_qom_list_parents(const char *parent)
161 QList *types;
162 QListEntry *e;
163 QDict *index;
165 types = qom_list_types(parent, true);
166 index = qom_type_index(types);
168 QLIST_FOREACH_ENTRY(types, e) {
169 QDict *d = qobject_to(QDict, qlist_entry_obj(e));
170 const char *name = qdict_get_str(d, "name");
172 g_assert(qom_has_parent(index, name, parent));
175 qobject_unref(types);
176 qobject_unref(index);
179 static void test_qom_list_fields(void)
181 QList *all_types;
182 QList *non_abstract;
183 QListEntry *e;
185 qtest_start(common_args);
187 all_types = qom_list_types(NULL, true);
188 non_abstract = qom_list_types(NULL, false);
190 QLIST_FOREACH_ENTRY(all_types, e) {
191 QDict *d = qobject_to(QDict, qlist_entry_obj(e));
192 const char *name = qdict_get_str(d, "name");
193 bool abstract = qdict_haskey(d, "abstract") ?
194 qdict_get_bool(d, "abstract") :
195 false;
196 bool expected_abstract = !type_list_find(non_abstract, name);
198 g_assert(abstract == expected_abstract);
201 test_qom_list_parents("object");
202 test_qom_list_parents("device");
203 test_qom_list_parents("sys-bus-device");
205 qobject_unref(all_types);
206 qobject_unref(non_abstract);
207 qtest_end();
210 static void test_device_intro_none(void)
212 qtest_start(common_args);
213 test_one_device("nonexistent");
214 qtest_end();
217 static void test_device_intro_abstract(void)
219 qtest_start(common_args);
220 test_one_device("device");
221 qtest_end();
224 static void test_device_intro_concrete(void)
226 QList *types;
227 QListEntry *entry;
228 const char *type;
230 qtest_start(common_args);
231 types = device_type_list(false);
233 QLIST_FOREACH_ENTRY(types, entry) {
234 type = qdict_get_try_str(qobject_to(QDict, qlist_entry_obj(entry)),
235 "name");
236 g_assert(type);
237 test_one_device(type);
240 qobject_unref(types);
241 qtest_end();
244 static void test_abstract_interfaces(void)
246 QList *all_types;
247 QListEntry *e;
248 QDict *index;
250 qtest_start(common_args);
252 all_types = qom_list_types("interface", true);
253 index = qom_type_index(all_types);
255 QLIST_FOREACH_ENTRY(all_types, e) {
256 QDict *d = qobject_to(QDict, qlist_entry_obj(e));
257 const char *name = qdict_get_str(d, "name");
260 * qom-list-types implements=interface returns all types
261 * that implement _any_ interface (not just interface
262 * types), so skip the ones that don't have "interface"
263 * on the parent type chain.
265 if (!qom_has_parent(index, name, "interface")) {
266 /* Not an interface type */
267 continue;
270 g_assert(qdict_haskey(d, "abstract") && qdict_get_bool(d, "abstract"));
273 qobject_unref(all_types);
274 qobject_unref(index);
275 qtest_end();
278 int main(int argc, char **argv)
280 g_test_init(&argc, &argv, NULL);
282 qtest_add_func("device/introspect/list", test_device_intro_list);
283 qtest_add_func("device/introspect/list-fields", test_qom_list_fields);
284 qtest_add_func("device/introspect/none", test_device_intro_none);
285 qtest_add_func("device/introspect/abstract", test_device_intro_abstract);
286 qtest_add_func("device/introspect/concrete", test_device_intro_concrete);
287 qtest_add_func("device/introspect/abstract-interfaces", test_abstract_interfaces);
289 return g_test_run();