blob: 04f22903b0114d00c9efd32ec8e9a343b8f9bb59 [file] [log] [blame]
Markus Armbruster2d1abb82015-10-01 10:59:56 +02001/*
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
Peter Maydell681c28a2016-02-08 18:08:51 +000020#include "qemu/osdep.h"
Markus Armbruster2d1abb82015-10-01 10:59:56 +020021#include "qemu-common.h"
22#include "qapi/qmp/qstring.h"
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -020023#include "qapi/qmp/qdict.h"
Markus Armbruster47e6b292018-02-01 12:18:38 +010024#include "qapi/qmp/qlist.h"
Markus Armbruster2d1abb82015-10-01 10:59:56 +020025#include "libqtest.h"
26
27const char common_args[] = "-nodefaults -machine none";
28
Thomas Huth39fb7952019-05-15 19:43:27 +020029static QList *qom_list_types(QTestState * qts, const char *implements,
30 bool abstract)
Markus Armbruster2d1abb82015-10-01 10:59:56 +020031{
32 QDict *resp;
33 QList *ret;
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -020034 QDict *args = qdict_new();
Markus Armbruster2d1abb82015-10-01 10:59:56 +020035
Eric Blake46f5ac22017-04-27 16:58:17 -050036 qdict_put_bool(args, "abstract", abstract);
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -020037 if (implements) {
Eric Blake46f5ac22017-04-27 16:58:17 -050038 qdict_put_str(args, "implements", implements);
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -020039 }
Thomas Huth39fb7952019-05-15 19:43:27 +020040 resp = qtest_qmp(qts, "{'execute': 'qom-list-types', 'arguments': %p }",
41 args);
Markus Armbruster2d1abb82015-10-01 10:59:56 +020042 g_assert(qdict_haskey(resp, "return"));
43 ret = qdict_get_qlist(resp, "return");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +020044 qobject_ref(ret);
45 qobject_unref(resp);
Markus Armbruster2d1abb82015-10-01 10:59:56 +020046 return ret;
47}
48
Eduardo Habkostf86285c2017-07-07 09:22:15 -030049/* Build a name -> ObjectTypeInfo index from a ObjectTypeInfo list */
50static QDict *qom_type_index(QList *types)
51{
52 QDict *index = qdict_new();
53 QListEntry *e;
54
55 QLIST_FOREACH_ENTRY(types, e) {
Max Reitz7dc847e2018-02-24 16:40:29 +010056 QDict *d = qobject_to(QDict, qlist_entry_obj(e));
Eduardo Habkostf86285c2017-07-07 09:22:15 -030057 const char *name = qdict_get_str(d, "name");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +020058 qobject_ref(d);
Eduardo Habkostf86285c2017-07-07 09:22:15 -030059 qdict_put(index, name, d);
60 }
61 return index;
62}
63
64/* Check if @parent is present in the parent chain of @type */
65static bool qom_has_parent(QDict *index, const char *type, const char *parent)
66{
67 while (type) {
68 QDict *d = qdict_get_qdict(index, type);
69 const char *p = d && qdict_haskey(d, "parent") ?
70 qdict_get_str(d, "parent") :
71 NULL;
72
73 if (!strcmp(type, parent)) {
74 return true;
75 }
76
77 type = p;
78 }
79
80 return false;
81}
82
Eduardo Habkostdbb2a602017-07-07 09:22:13 -030083/* Find an entry on a list returned by qom-list-types */
84static QDict *type_list_find(QList *types, const char *name)
85{
86 QListEntry *e;
87
88 QLIST_FOREACH_ENTRY(types, e) {
Max Reitz7dc847e2018-02-24 16:40:29 +010089 QDict *d = qobject_to(QDict, qlist_entry_obj(e));
Eduardo Habkostdbb2a602017-07-07 09:22:13 -030090 const char *ename = qdict_get_str(d, "name");
91 if (!strcmp(ename, name)) {
92 return d;
93 }
94 }
95
96 return NULL;
97}
98
Thomas Huth39fb7952019-05-15 19:43:27 +020099static QList *device_type_list(QTestState *qts, bool abstract)
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -0200100{
Thomas Huth39fb7952019-05-15 19:43:27 +0200101 return qom_list_types(qts, "device", abstract);
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -0200102}
103
Thomas Huth39fb7952019-05-15 19:43:27 +0200104static void test_one_device(QTestState *qts, const char *type)
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200105{
106 QDict *resp;
Thomas Huthd0685212018-08-16 13:35:56 +0200107 char *help;
108 char *qom_tree_start, *qom_tree_end;
109 char *qtree_start, *qtree_end;
110
111 g_test_message("Testing device '%s'", type);
112
Thomas Huth39fb7952019-05-15 19:43:27 +0200113 qom_tree_start = qtest_hmp(qts, "info qom-tree");
114 qtree_start = qtest_hmp(qts, "info qtree");
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200115
Thomas Huth39fb7952019-05-15 19:43:27 +0200116 resp = qtest_qmp(qts, "{'execute': 'device-list-properties',"
117 " 'arguments': {'typename': %s}}",
Markus Armbrusteredb15232015-10-01 10:59:57 +0200118 type);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200119 qobject_unref(resp);
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200120
Thomas Huth39fb7952019-05-15 19:43:27 +0200121 help = qtest_hmp(qts, "device_add \"%s,help\"", type);
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200122 g_free(help);
123
124 /*
125 * Some devices leave dangling pointers in QOM behind.
Thomas Huthd0685212018-08-16 13:35:56 +0200126 * "info qom-tree" or "info qtree" have a good chance at crashing then.
127 * Also make sure that the tree did not change.
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200128 */
Thomas Huth39fb7952019-05-15 19:43:27 +0200129 qom_tree_end = qtest_hmp(qts, "info qom-tree");
Thomas Huthd0685212018-08-16 13:35:56 +0200130 g_assert_cmpstr(qom_tree_start, ==, qom_tree_end);
131 g_free(qom_tree_start);
132 g_free(qom_tree_end);
133
Thomas Huth39fb7952019-05-15 19:43:27 +0200134 qtree_end = qtest_hmp(qts, "info qtree");
Thomas Huthd0685212018-08-16 13:35:56 +0200135 g_assert_cmpstr(qtree_start, ==, qtree_end);
136 g_free(qtree_start);
137 g_free(qtree_end);
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200138}
139
140static void test_device_intro_list(void)
141{
142 QList *types;
143 char *help;
Thomas Huth39fb7952019-05-15 19:43:27 +0200144 QTestState *qts;
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200145
Thomas Huth39fb7952019-05-15 19:43:27 +0200146 qts = qtest_init(common_args);
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200147
Thomas Huth39fb7952019-05-15 19:43:27 +0200148 types = device_type_list(qts, true);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200149 qobject_unref(types);
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200150
Thomas Huth39fb7952019-05-15 19:43:27 +0200151 help = qtest_hmp(qts, "device_add help");
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200152 g_free(help);
153
Thomas Huth39fb7952019-05-15 19:43:27 +0200154 qtest_quit(qts);
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200155}
156
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300157/*
158 * Ensure all entries returned by qom-list-types implements=<parent>
159 * have <parent> as a parent.
160 */
Thomas Huth39fb7952019-05-15 19:43:27 +0200161static void test_qom_list_parents(QTestState *qts, const char *parent)
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300162{
163 QList *types;
164 QListEntry *e;
165 QDict *index;
166
Thomas Huth39fb7952019-05-15 19:43:27 +0200167 types = qom_list_types(qts, parent, true);
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300168 index = qom_type_index(types);
169
170 QLIST_FOREACH_ENTRY(types, e) {
Max Reitz7dc847e2018-02-24 16:40:29 +0100171 QDict *d = qobject_to(QDict, qlist_entry_obj(e));
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300172 const char *name = qdict_get_str(d, "name");
173
174 g_assert(qom_has_parent(index, name, parent));
175 }
176
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200177 qobject_unref(types);
178 qobject_unref(index);
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300179}
180
Eduardo Habkost87467ea2017-07-07 09:22:14 -0300181static void test_qom_list_fields(void)
182{
183 QList *all_types;
184 QList *non_abstract;
185 QListEntry *e;
Thomas Huth39fb7952019-05-15 19:43:27 +0200186 QTestState *qts;
Eduardo Habkost87467ea2017-07-07 09:22:14 -0300187
Thomas Huth39fb7952019-05-15 19:43:27 +0200188 qts = qtest_init(common_args);
Eduardo Habkost87467ea2017-07-07 09:22:14 -0300189
Thomas Huth39fb7952019-05-15 19:43:27 +0200190 all_types = qom_list_types(qts, NULL, true);
191 non_abstract = qom_list_types(qts, NULL, false);
Eduardo Habkost87467ea2017-07-07 09:22:14 -0300192
193 QLIST_FOREACH_ENTRY(all_types, e) {
Max Reitz7dc847e2018-02-24 16:40:29 +0100194 QDict *d = qobject_to(QDict, qlist_entry_obj(e));
Eduardo Habkost87467ea2017-07-07 09:22:14 -0300195 const char *name = qdict_get_str(d, "name");
196 bool abstract = qdict_haskey(d, "abstract") ?
197 qdict_get_bool(d, "abstract") :
198 false;
199 bool expected_abstract = !type_list_find(non_abstract, name);
200
201 g_assert(abstract == expected_abstract);
202 }
203
Thomas Huth39fb7952019-05-15 19:43:27 +0200204 test_qom_list_parents(qts, "object");
205 test_qom_list_parents(qts, "device");
206 test_qom_list_parents(qts, "sys-bus-device");
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300207
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200208 qobject_unref(all_types);
209 qobject_unref(non_abstract);
Thomas Huth39fb7952019-05-15 19:43:27 +0200210 qtest_quit(qts);
Eduardo Habkost87467ea2017-07-07 09:22:14 -0300211}
212
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200213static void test_device_intro_none(void)
214{
Thomas Huth39fb7952019-05-15 19:43:27 +0200215 QTestState *qts = qtest_init(common_args);
216
217 test_one_device(qts, "nonexistent");
218 qtest_quit(qts);
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200219}
220
221static void test_device_intro_abstract(void)
222{
Thomas Huth39fb7952019-05-15 19:43:27 +0200223 QTestState *qts = qtest_init(common_args);
224
225 test_one_device(qts, "device");
226 qtest_quit(qts);
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200227}
228
Thomas Huth410573a2018-08-16 13:35:57 +0200229static void test_device_intro_concrete(const void *args)
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200230{
231 QList *types;
232 QListEntry *entry;
233 const char *type;
Thomas Huth39fb7952019-05-15 19:43:27 +0200234 QTestState *qts;
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200235
Thomas Huth39fb7952019-05-15 19:43:27 +0200236 qts = qtest_init(args);
237 types = device_type_list(qts, false);
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200238
239 QLIST_FOREACH_ENTRY(types, entry) {
Max Reitz7dc847e2018-02-24 16:40:29 +0100240 type = qdict_get_try_str(qobject_to(QDict, qlist_entry_obj(entry)),
241 "name");
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200242 g_assert(type);
Thomas Huth39fb7952019-05-15 19:43:27 +0200243 test_one_device(qts, type);
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200244 }
245
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200246 qobject_unref(types);
Thomas Huth39fb7952019-05-15 19:43:27 +0200247 qtest_quit(qts);
Thomas Huth410573a2018-08-16 13:35:57 +0200248 g_free((void *)args);
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200249}
250
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -0200251static void test_abstract_interfaces(void)
252{
253 QList *all_types;
Eduardo Habkostdbb2a602017-07-07 09:22:13 -0300254 QListEntry *e;
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300255 QDict *index;
Thomas Huth39fb7952019-05-15 19:43:27 +0200256 QTestState *qts;
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -0200257
Thomas Huth39fb7952019-05-15 19:43:27 +0200258 qts = qtest_init(common_args);
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300259
Thomas Huth39fb7952019-05-15 19:43:27 +0200260 all_types = qom_list_types(qts, "interface", true);
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300261 index = qom_type_index(all_types);
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -0200262
Eduardo Habkostdbb2a602017-07-07 09:22:13 -0300263 QLIST_FOREACH_ENTRY(all_types, e) {
Max Reitz7dc847e2018-02-24 16:40:29 +0100264 QDict *d = qobject_to(QDict, qlist_entry_obj(e));
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300265 const char *name = qdict_get_str(d, "name");
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -0200266
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300267 /*
268 * qom-list-types implements=interface returns all types
269 * that implement _any_ interface (not just interface
270 * types), so skip the ones that don't have "interface"
271 * on the parent type chain.
272 */
273 if (!qom_has_parent(index, name, "interface")) {
Eduardo Habkost87467ea2017-07-07 09:22:14 -0300274 /* Not an interface type */
275 continue;
276 }
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -0200277
Eduardo Habkost87467ea2017-07-07 09:22:14 -0300278 g_assert(qdict_haskey(d, "abstract") && qdict_get_bool(d, "abstract"));
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -0200279 }
280
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200281 qobject_unref(all_types);
282 qobject_unref(index);
Thomas Huth39fb7952019-05-15 19:43:27 +0200283 qtest_quit(qts);
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -0200284}
285
Thomas Huth410573a2018-08-16 13:35:57 +0200286static void add_machine_test_case(const char *mname)
287{
288 char *path, *args;
289
290 /* Ignore blacklisted machines */
291 if (g_str_equal("xenfv", mname) || g_str_equal("xenpv", mname)) {
292 return;
293 }
294
295 path = g_strdup_printf("device/introspect/concrete/defaults/%s", mname);
296 args = g_strdup_printf("-M %s", mname);
297 qtest_add_data_func(path, args, test_device_intro_concrete);
298 g_free(path);
299
300 path = g_strdup_printf("device/introspect/concrete/nodefaults/%s", mname);
301 args = g_strdup_printf("-nodefaults -M %s", mname);
302 qtest_add_data_func(path, args, test_device_intro_concrete);
303 g_free(path);
304}
305
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200306int main(int argc, char **argv)
307{
308 g_test_init(&argc, &argv, NULL);
309
310 qtest_add_func("device/introspect/list", test_device_intro_list);
Eduardo Habkost87467ea2017-07-07 09:22:14 -0300311 qtest_add_func("device/introspect/list-fields", test_qom_list_fields);
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200312 qtest_add_func("device/introspect/none", test_device_intro_none);
313 qtest_add_func("device/introspect/abstract", test_device_intro_abstract);
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -0200314 qtest_add_func("device/introspect/abstract-interfaces", test_abstract_interfaces);
Thomas Huth410573a2018-08-16 13:35:57 +0200315 if (g_test_quick()) {
316 qtest_add_data_func("device/introspect/concrete/defaults/none",
317 g_strdup(common_args), test_device_intro_concrete);
318 } else {
319 qtest_cb_for_every_machine(add_machine_test_case, true);
320 }
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200321
322 return g_test_run();
323}