tests: Simplify abstract-interfaces check with a helper
[qemu/ar7.git] / tests / device-introspect-test.c
blobf1e6dddfa35530852fbcba791101eea3f15d1d2e
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/qbool.h"
24 #include "qapi/qmp/qdict.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 QINCREF(ret);
44 QDECREF(resp);
45 return ret;
48 /* Find an entry on a list returned by qom-list-types */
49 static QDict *type_list_find(QList *types, const char *name)
51 QListEntry *e;
53 QLIST_FOREACH_ENTRY(types, e) {
54 QDict *d = qobject_to_qdict(qlist_entry_obj(e));
55 const char *ename = qdict_get_str(d, "name");
56 if (!strcmp(ename, name)) {
57 return d;
61 return NULL;
64 static QList *device_type_list(bool abstract)
66 return qom_list_types("device", abstract);
69 static void test_one_device(const char *type)
71 QDict *resp;
72 char *help, *qom_tree;
74 resp = qmp("{'execute': 'device-list-properties',"
75 " 'arguments': {'typename': %s}}",
76 type);
77 QDECREF(resp);
79 help = hmp("device_add \"%s,help\"", type);
80 g_free(help);
83 * Some devices leave dangling pointers in QOM behind.
84 * "info qom-tree" has a good chance at crashing then
86 qom_tree = hmp("info qom-tree");
87 g_free(qom_tree);
90 static void test_device_intro_list(void)
92 QList *types;
93 char *help;
95 qtest_start(common_args);
97 types = device_type_list(true);
98 QDECREF(types);
100 help = hmp("device_add help");
101 g_free(help);
103 qtest_end();
106 static void test_device_intro_none(void)
108 qtest_start(common_args);
109 test_one_device("nonexistent");
110 qtest_end();
113 static void test_device_intro_abstract(void)
115 qtest_start(common_args);
116 test_one_device("device");
117 qtest_end();
120 static void test_device_intro_concrete(void)
122 QList *types;
123 QListEntry *entry;
124 const char *type;
126 qtest_start(common_args);
127 types = device_type_list(false);
129 QLIST_FOREACH_ENTRY(types, entry) {
130 type = qdict_get_try_str(qobject_to_qdict(qlist_entry_obj(entry)),
131 "name");
132 g_assert(type);
133 test_one_device(type);
136 QDECREF(types);
137 qtest_end();
140 static void test_abstract_interfaces(void)
142 QList *all_types;
143 QList *obj_types;
144 QListEntry *e;
146 qtest_start(common_args);
147 /* qom-list-types implements=interface would return any type
148 * that implements _any_ interface (not just interface types),
149 * so use a trick to find the interface type names:
150 * - list all object types
151 * - list all types, and look for items that are not
152 * on the first list
154 all_types = qom_list_types(NULL, false);
155 obj_types = qom_list_types("object", false);
157 QLIST_FOREACH_ENTRY(all_types, e) {
158 QDict *d = qobject_to_qdict(qlist_entry_obj(e));
161 * An interface (incorrectly) marked as non-abstract would
162 * appear on all_types, but not on obj_types:
164 g_assert(type_list_find(obj_types, qdict_get_str(d, "name")));
168 QDECREF(all_types);
169 QDECREF(obj_types);
170 qtest_end();
173 int main(int argc, char **argv)
175 g_test_init(&argc, &argv, NULL);
177 qtest_add_func("device/introspect/list", test_device_intro_list);
178 qtest_add_func("device/introspect/none", test_device_intro_none);
179 qtest_add_func("device/introspect/abstract", test_device_intro_abstract);
180 qtest_add_func("device/introspect/concrete", test_device_intro_concrete);
181 qtest_add_func("device/introspect/abstract-interfaces", test_abstract_interfaces);
183 return g_test_run();