]>
Commit | Line | Data |
---|---|---|
2d1abb85 MA |
1 | /* |
2 | * Device introspection test cases | |
3 | * | |
4 | * Copyright (c) 2015 Red Hat Inc. | |
5 | * | |
6 | * Authors: | |
7 | * Markus Armbruster <armbru@redhat.com>, | |
8 | * | |
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. | |
11 | */ | |
12 | ||
13 | /* | |
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. | |
18 | */ | |
19 | ||
681c28a3 | 20 | #include "qemu/osdep.h" |
2d1abb85 MA |
21 | #include "qemu-common.h" |
22 | #include "qapi/qmp/qstring.h" | |
1c6d75d5 | 23 | #include "qapi/qmp/qdict.h" |
47e6b297 | 24 | #include "qapi/qmp/qlist.h" |
2d1abb85 MA |
25 | #include "libqtest.h" |
26 | ||
27 | const char common_args[] = "-nodefaults -machine none"; | |
28 | ||
1c6d75d5 | 29 | static QList *qom_list_types(const char *implements, bool abstract) |
2d1abb85 MA |
30 | { |
31 | QDict *resp; | |
32 | QList *ret; | |
1c6d75d5 | 33 | QDict *args = qdict_new(); |
2d1abb85 | 34 | |
46f5ac20 | 35 | qdict_put_bool(args, "abstract", abstract); |
1c6d75d5 | 36 | if (implements) { |
46f5ac20 | 37 | qdict_put_str(args, "implements", implements); |
1c6d75d5 | 38 | } |
2d1abb85 | 39 | resp = qmp("{'execute': 'qom-list-types'," |
1c6d75d5 | 40 | " 'arguments': %p }", args); |
2d1abb85 MA |
41 | g_assert(qdict_haskey(resp, "return")); |
42 | ret = qdict_get_qlist(resp, "return"); | |
43 | QINCREF(ret); | |
44 | QDECREF(resp); | |
45 | return ret; | |
46 | } | |
47 | ||
f86285c5 EH |
48 | /* Build a name -> ObjectTypeInfo index from a ObjectTypeInfo list */ |
49 | static QDict *qom_type_index(QList *types) | |
50 | { | |
51 | QDict *index = qdict_new(); | |
52 | QListEntry *e; | |
53 | ||
54 | QLIST_FOREACH_ENTRY(types, e) { | |
7dc847eb | 55 | QDict *d = qobject_to(QDict, qlist_entry_obj(e)); |
f86285c5 EH |
56 | const char *name = qdict_get_str(d, "name"); |
57 | QINCREF(d); | |
58 | qdict_put(index, name, d); | |
59 | } | |
60 | return index; | |
61 | } | |
62 | ||
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) | |
65 | { | |
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; | |
71 | ||
72 | if (!strcmp(type, parent)) { | |
73 | return true; | |
74 | } | |
75 | ||
76 | type = p; | |
77 | } | |
78 | ||
79 | return false; | |
80 | } | |
81 | ||
dbb2a604 EH |
82 | /* Find an entry on a list returned by qom-list-types */ |
83 | static QDict *type_list_find(QList *types, const char *name) | |
84 | { | |
85 | QListEntry *e; | |
86 | ||
87 | QLIST_FOREACH_ENTRY(types, e) { | |
7dc847eb | 88 | QDict *d = qobject_to(QDict, qlist_entry_obj(e)); |
dbb2a604 EH |
89 | const char *ename = qdict_get_str(d, "name"); |
90 | if (!strcmp(ename, name)) { | |
91 | return d; | |
92 | } | |
93 | } | |
94 | ||
95 | return NULL; | |
96 | } | |
97 | ||
1c6d75d5 EH |
98 | static QList *device_type_list(bool abstract) |
99 | { | |
100 | return qom_list_types("device", abstract); | |
101 | } | |
102 | ||
2d1abb85 MA |
103 | static void test_one_device(const char *type) |
104 | { | |
105 | QDict *resp; | |
106 | char *help, *qom_tree; | |
107 | ||
edb1523d MA |
108 | resp = qmp("{'execute': 'device-list-properties'," |
109 | " 'arguments': {'typename': %s}}", | |
110 | type); | |
111 | QDECREF(resp); | |
2d1abb85 MA |
112 | |
113 | help = hmp("device_add \"%s,help\"", type); | |
114 | g_free(help); | |
115 | ||
116 | /* | |
117 | * Some devices leave dangling pointers in QOM behind. | |
118 | * "info qom-tree" has a good chance at crashing then | |
119 | */ | |
120 | qom_tree = hmp("info qom-tree"); | |
121 | g_free(qom_tree); | |
122 | } | |
123 | ||
124 | static void test_device_intro_list(void) | |
125 | { | |
126 | QList *types; | |
127 | char *help; | |
128 | ||
129 | qtest_start(common_args); | |
130 | ||
131 | types = device_type_list(true); | |
132 | QDECREF(types); | |
133 | ||
134 | help = hmp("device_add help"); | |
135 | g_free(help); | |
136 | ||
137 | qtest_end(); | |
138 | } | |
139 | ||
f86285c5 EH |
140 | /* |
141 | * Ensure all entries returned by qom-list-types implements=<parent> | |
142 | * have <parent> as a parent. | |
143 | */ | |
144 | static void test_qom_list_parents(const char *parent) | |
145 | { | |
146 | QList *types; | |
147 | QListEntry *e; | |
148 | QDict *index; | |
149 | ||
150 | types = qom_list_types(parent, true); | |
151 | index = qom_type_index(types); | |
152 | ||
153 | QLIST_FOREACH_ENTRY(types, e) { | |
7dc847eb | 154 | QDict *d = qobject_to(QDict, qlist_entry_obj(e)); |
f86285c5 EH |
155 | const char *name = qdict_get_str(d, "name"); |
156 | ||
157 | g_assert(qom_has_parent(index, name, parent)); | |
158 | } | |
159 | ||
160 | QDECREF(types); | |
161 | QDECREF(index); | |
162 | } | |
163 | ||
87467eae EH |
164 | static void test_qom_list_fields(void) |
165 | { | |
166 | QList *all_types; | |
167 | QList *non_abstract; | |
168 | QListEntry *e; | |
169 | ||
170 | qtest_start(common_args); | |
171 | ||
172 | all_types = qom_list_types(NULL, true); | |
173 | non_abstract = qom_list_types(NULL, false); | |
174 | ||
175 | QLIST_FOREACH_ENTRY(all_types, e) { | |
7dc847eb | 176 | QDict *d = qobject_to(QDict, qlist_entry_obj(e)); |
87467eae EH |
177 | const char *name = qdict_get_str(d, "name"); |
178 | bool abstract = qdict_haskey(d, "abstract") ? | |
179 | qdict_get_bool(d, "abstract") : | |
180 | false; | |
181 | bool expected_abstract = !type_list_find(non_abstract, name); | |
182 | ||
183 | g_assert(abstract == expected_abstract); | |
184 | } | |
185 | ||
f86285c5 EH |
186 | test_qom_list_parents("object"); |
187 | test_qom_list_parents("device"); | |
188 | test_qom_list_parents("sys-bus-device"); | |
189 | ||
87467eae EH |
190 | QDECREF(all_types); |
191 | QDECREF(non_abstract); | |
192 | qtest_end(); | |
193 | } | |
194 | ||
2d1abb85 MA |
195 | static void test_device_intro_none(void) |
196 | { | |
197 | qtest_start(common_args); | |
198 | test_one_device("nonexistent"); | |
199 | qtest_end(); | |
200 | } | |
201 | ||
202 | static void test_device_intro_abstract(void) | |
203 | { | |
204 | qtest_start(common_args); | |
205 | test_one_device("device"); | |
206 | qtest_end(); | |
207 | } | |
208 | ||
2d1abb85 MA |
209 | static void test_device_intro_concrete(void) |
210 | { | |
211 | QList *types; | |
212 | QListEntry *entry; | |
213 | const char *type; | |
214 | ||
215 | qtest_start(common_args); | |
216 | types = device_type_list(false); | |
217 | ||
218 | QLIST_FOREACH_ENTRY(types, entry) { | |
7dc847eb HR |
219 | type = qdict_get_try_str(qobject_to(QDict, qlist_entry_obj(entry)), |
220 | "name"); | |
2d1abb85 | 221 | g_assert(type); |
2d1abb85 MA |
222 | test_one_device(type); |
223 | } | |
224 | ||
225 | QDECREF(types); | |
226 | qtest_end(); | |
227 | } | |
228 | ||
1c6d75d5 EH |
229 | static void test_abstract_interfaces(void) |
230 | { | |
231 | QList *all_types; | |
dbb2a604 | 232 | QListEntry *e; |
f86285c5 | 233 | QDict *index; |
1c6d75d5 EH |
234 | |
235 | qtest_start(common_args); | |
f86285c5 | 236 | |
87467eae | 237 | all_types = qom_list_types("interface", true); |
f86285c5 | 238 | index = qom_type_index(all_types); |
1c6d75d5 | 239 | |
dbb2a604 | 240 | QLIST_FOREACH_ENTRY(all_types, e) { |
7dc847eb | 241 | QDict *d = qobject_to(QDict, qlist_entry_obj(e)); |
f86285c5 | 242 | const char *name = qdict_get_str(d, "name"); |
dbb2a604 | 243 | |
f86285c5 EH |
244 | /* |
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. | |
249 | */ | |
250 | if (!qom_has_parent(index, name, "interface")) { | |
87467eae EH |
251 | /* Not an interface type */ |
252 | continue; | |
253 | } | |
1c6d75d5 | 254 | |
87467eae | 255 | g_assert(qdict_haskey(d, "abstract") && qdict_get_bool(d, "abstract")); |
1c6d75d5 EH |
256 | } |
257 | ||
258 | QDECREF(all_types); | |
f86285c5 | 259 | QDECREF(index); |
1c6d75d5 EH |
260 | qtest_end(); |
261 | } | |
262 | ||
2d1abb85 MA |
263 | int main(int argc, char **argv) |
264 | { | |
265 | g_test_init(&argc, &argv, NULL); | |
266 | ||
267 | qtest_add_func("device/introspect/list", test_device_intro_list); | |
87467eae | 268 | qtest_add_func("device/introspect/list-fields", test_qom_list_fields); |
2d1abb85 MA |
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); | |
1c6d75d5 | 272 | qtest_add_func("device/introspect/abstract-interfaces", test_abstract_interfaces); |
2d1abb85 MA |
273 | |
274 | return g_test_run(); | |
275 | } |