2 * Device introspection test cases
4 * Copyright (c) 2015 Red Hat Inc.
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/qbool.h"
24 #include "qapi/qmp/qdict.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 static QList *device_type_list(bool abstract)
50 return qom_list_types("device", abstract);
53 static void test_one_device(const char *type)
56 char *help, *qom_tree;
58 resp = qmp("{'execute': 'device-list-properties',"
59 " 'arguments': {'typename': %s}}",
63 help = hmp("device_add \"%s,help\"", type);
67 * Some devices leave dangling pointers in QOM behind.
68 * "info qom-tree" has a good chance at crashing then
70 qom_tree = hmp("info qom-tree");
74 static void test_device_intro_list(void)
79 qtest_start(common_args);
81 types = device_type_list(true);
84 help = hmp("device_add help");
90 static void test_device_intro_none(void)
92 qtest_start(common_args);
93 test_one_device("nonexistent");
97 static void test_device_intro_abstract(void)
99 qtest_start(common_args);
100 test_one_device("device");
104 static void test_device_intro_concrete(void)
110 qtest_start(common_args);
111 types = device_type_list(false);
113 QLIST_FOREACH_ENTRY(types, entry) {
114 type = qdict_get_try_str(qobject_to_qdict(qlist_entry_obj(entry)),
117 test_one_device(type);
124 static void test_abstract_interfaces(void)
130 qtest_start(common_args);
131 /* qom-list-types implements=interface would return any type
132 * that implements _any_ interface (not just interface types),
133 * so use a trick to find the interface type names:
134 * - list all object types
135 * - list all types, and look for items that are not
138 all_types = qom_list_types(NULL, false);
139 obj_types = qom_list_types("object", false);
141 QLIST_FOREACH_ENTRY(all_types, ae) {
142 QDict *at = qobject_to_qdict(qlist_entry_obj(ae));
143 const char *aname = qdict_get_str(at, "name");
145 const char *found = NULL;
147 QLIST_FOREACH_ENTRY(obj_types, oe) {
148 QDict *ot = qobject_to_qdict(qlist_entry_obj(oe));
149 const char *oname = qdict_get_str(ot, "name");
150 if (!strcmp(aname, oname)) {
156 /* Using g_assert_cmpstr() will give more useful failure
157 * messages than g_assert(found) */
158 g_assert_cmpstr(aname, ==, found);
166 int main(int argc, char **argv)
168 g_test_init(&argc, &argv, NULL);
170 qtest_add_func("device/introspect/list", test_device_intro_list);
171 qtest_add_func("device/introspect/none", test_device_intro_none);
172 qtest_add_func("device/introspect/abstract", test_device_intro_abstract);
173 qtest_add_func("device/introspect/concrete", test_device_intro_concrete);
174 qtest_add_func("device/introspect/abstract-interfaces", test_abstract_interfaces);