2 * Device introspection test cases
4 * Copyright (c) 2015 Red Hat Inc.
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
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"
27 const char common_args
[] = "-nodefaults -machine none";
29 static QList
*qom_list_types(const char *implements
, bool abstract
)
33 QDict
*args
= qdict_new();
35 qdict_put_bool(args
, "abstract", abstract
);
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");
48 /* Build a name -> ObjectTypeInfo index from a ObjectTypeInfo list */
49 static QDict
*qom_type_index(QList
*types
)
51 QDict
*index
= qdict_new();
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");
58 qdict_put(index
, name
, d
);
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
)
67 QDict
*d
= qdict_get_qdict(index
, type
);
68 const char *p
= d
&& qdict_haskey(d
, "parent") ?
69 qdict_get_str(d
, "parent") :
72 if (!strcmp(type
, parent
)) {
82 /* Find an entry on a list returned by qom-list-types */
83 static QDict
*type_list_find(QList
*types
, const char *name
)
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
)) {
98 static QList
*device_type_list(bool abstract
)
100 return qom_list_types("device", abstract
);
103 static void test_one_device(const char *type
)
106 char *help
, *qom_tree
;
108 resp
= qmp("{'execute': 'device-list-properties',"
109 " 'arguments': {'typename': %s}}",
113 help
= hmp("device_add \"%s,help\"", type
);
117 * Some devices leave dangling pointers in QOM behind.
118 * "info qom-tree" has a good chance at crashing then
120 qom_tree
= hmp("info qom-tree");
124 static void test_device_intro_list(void)
129 qtest_start(common_args
);
131 types
= device_type_list(true);
134 help
= hmp("device_add help");
141 * Ensure all entries returned by qom-list-types implements=<parent>
142 * have <parent> as a parent.
144 static void test_qom_list_parents(const char *parent
)
150 types
= qom_list_types(parent
, true);
151 index
= qom_type_index(types
);
153 QLIST_FOREACH_ENTRY(types
, e
) {
154 QDict
*d
= qobject_to_qdict(qlist_entry_obj(e
));
155 const char *name
= qdict_get_str(d
, "name");
157 g_assert(qom_has_parent(index
, name
, parent
));
164 static void test_qom_list_fields(void)
170 qtest_start(common_args
);
172 all_types
= qom_list_types(NULL
, true);
173 non_abstract
= qom_list_types(NULL
, false);
175 QLIST_FOREACH_ENTRY(all_types
, e
) {
176 QDict
*d
= qobject_to_qdict(qlist_entry_obj(e
));
177 const char *name
= qdict_get_str(d
, "name");
178 bool abstract
= qdict_haskey(d
, "abstract") ?
179 qdict_get_bool(d
, "abstract") :
181 bool expected_abstract
= !type_list_find(non_abstract
, name
);
183 g_assert(abstract
== expected_abstract
);
186 test_qom_list_parents("object");
187 test_qom_list_parents("device");
188 test_qom_list_parents("sys-bus-device");
191 QDECREF(non_abstract
);
195 static void test_device_intro_none(void)
197 qtest_start(common_args
);
198 test_one_device("nonexistent");
202 static void test_device_intro_abstract(void)
204 qtest_start(common_args
);
205 test_one_device("device");
209 static void test_device_intro_concrete(void)
215 qtest_start(common_args
);
216 types
= device_type_list(false);
218 QLIST_FOREACH_ENTRY(types
, entry
) {
219 type
= qdict_get_try_str(qobject_to_qdict(qlist_entry_obj(entry
)),
222 test_one_device(type
);
229 static void test_abstract_interfaces(void)
235 qtest_start(common_args
);
237 all_types
= qom_list_types("interface", true);
238 index
= qom_type_index(all_types
);
240 QLIST_FOREACH_ENTRY(all_types
, e
) {
241 QDict
*d
= qobject_to_qdict(qlist_entry_obj(e
));
242 const char *name
= qdict_get_str(d
, "name");
245 * qom-list-types implements=interface returns all types
246 * that implement _any_ interface (not just interface
247 * types), so skip the ones that don't have "interface"
248 * on the parent type chain.
250 if (!qom_has_parent(index
, name
, "interface")) {
251 /* Not an interface type */
255 g_assert(qdict_haskey(d
, "abstract") && qdict_get_bool(d
, "abstract"));
263 int main(int argc
, char **argv
)
265 g_test_init(&argc
, &argv
, NULL
);
267 qtest_add_func("device/introspect/list", test_device_intro_list
);
268 qtest_add_func("device/introspect/list-fields", test_qom_list_fields
);
269 qtest_add_func("device/introspect/none", test_device_intro_none
);
270 qtest_add_func("device/introspect/abstract", test_device_intro_abstract
);
271 qtest_add_func("device/introspect/concrete", test_device_intro_concrete
);
272 qtest_add_func("device/introspect/abstract-interfaces", test_abstract_interfaces
);