blob: 5b0ffe43f5f428d7ae6d6c700181e36ec95aee15 [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 "qapi/qmp/qstring.h"
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -020022#include "qapi/qmp/qdict.h"
Markus Armbruster47e6b292018-02-01 12:18:38 +010023#include "qapi/qmp/qlist.h"
Marc-André Lureau907b5102022-03-30 13:39:05 +040024#include "libqtest.h"
Markus Armbruster2d1abb82015-10-01 10:59:56 +020025
26const char common_args[] = "-nodefaults -machine none";
27
Thomas Huth39fb7952019-05-15 19:43:27 +020028static QList *qom_list_types(QTestState * qts, const char *implements,
29 bool abstract)
Markus Armbruster2d1abb82015-10-01 10:59:56 +020030{
31 QDict *resp;
32 QList *ret;
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -020033 QDict *args = qdict_new();
Markus Armbruster2d1abb82015-10-01 10:59:56 +020034
Eric Blake46f5ac22017-04-27 16:58:17 -050035 qdict_put_bool(args, "abstract", abstract);
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -020036 if (implements) {
Eric Blake46f5ac22017-04-27 16:58:17 -050037 qdict_put_str(args, "implements", implements);
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -020038 }
Thomas Huth39fb7952019-05-15 19:43:27 +020039 resp = qtest_qmp(qts, "{'execute': 'qom-list-types', 'arguments': %p }",
40 args);
Markus Armbruster2d1abb82015-10-01 10:59:56 +020041 g_assert(qdict_haskey(resp, "return"));
42 ret = qdict_get_qlist(resp, "return");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +020043 qobject_ref(ret);
44 qobject_unref(resp);
Markus Armbruster2d1abb82015-10-01 10:59:56 +020045 return ret;
46}
47
Eduardo Habkostf86285c2017-07-07 09:22:15 -030048/* Build a name -> ObjectTypeInfo index from a ObjectTypeInfo list */
49static QDict *qom_type_index(QList *types)
50{
51 QDict *index = qdict_new();
52 QListEntry *e;
53
54 QLIST_FOREACH_ENTRY(types, e) {
Max Reitz7dc847e2018-02-24 16:40:29 +010055 QDict *d = qobject_to(QDict, qlist_entry_obj(e));
Eduardo Habkostf86285c2017-07-07 09:22:15 -030056 const char *name = qdict_get_str(d, "name");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +020057 qobject_ref(d);
Eduardo Habkostf86285c2017-07-07 09:22:15 -030058 qdict_put(index, name, d);
59 }
60 return index;
61}
62
63/* Check if @parent is present in the parent chain of @type */
64static 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
Eduardo Habkostdbb2a602017-07-07 09:22:13 -030082/* Find an entry on a list returned by qom-list-types */
83static QDict *type_list_find(QList *types, const char *name)
84{
85 QListEntry *e;
86
87 QLIST_FOREACH_ENTRY(types, e) {
Max Reitz7dc847e2018-02-24 16:40:29 +010088 QDict *d = qobject_to(QDict, qlist_entry_obj(e));
Eduardo Habkostdbb2a602017-07-07 09:22:13 -030089 const char *ename = qdict_get_str(d, "name");
90 if (!strcmp(ename, name)) {
91 return d;
92 }
93 }
94
95 return NULL;
96}
97
Thomas Huth39fb7952019-05-15 19:43:27 +020098static QList *device_type_list(QTestState *qts, bool abstract)
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -020099{
Thomas Huth39fb7952019-05-15 19:43:27 +0200100 return qom_list_types(qts, "device", abstract);
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -0200101}
102
Thomas Huth39fb7952019-05-15 19:43:27 +0200103static void test_one_device(QTestState *qts, const char *type)
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200104{
105 QDict *resp;
Paolo Bonzinie27bd492020-11-03 06:57:46 -0500106 char *help, *escaped;
107 GRegex *comma;
Thomas Huthd0685212018-08-16 13:35:56 +0200108
109 g_test_message("Testing device '%s'", type);
110
Thomas Huth39fb7952019-05-15 19:43:27 +0200111 resp = qtest_qmp(qts, "{'execute': 'device-list-properties',"
112 " 'arguments': {'typename': %s}}",
Markus Armbrusteredb15232015-10-01 10:59:57 +0200113 type);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200114 qobject_unref(resp);
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200115
Paolo Bonzinie27bd492020-11-03 06:57:46 -0500116 comma = g_regex_new(",", 0, 0, NULL);
117 escaped = g_regex_replace_literal(comma, type, -1, 0, ",,", 0, NULL);
118 g_regex_unref(comma);
119
120 help = qtest_hmp(qts, "device_add \"%s,help\"", escaped);
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200121 g_free(help);
Paolo Bonzinie27bd492020-11-03 06:57:46 -0500122 g_free(escaped);
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200123}
124
125static void test_device_intro_list(void)
126{
127 QList *types;
128 char *help;
Thomas Huth39fb7952019-05-15 19:43:27 +0200129 QTestState *qts;
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200130
Thomas Huth39fb7952019-05-15 19:43:27 +0200131 qts = qtest_init(common_args);
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200132
Thomas Huth39fb7952019-05-15 19:43:27 +0200133 types = device_type_list(qts, true);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200134 qobject_unref(types);
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200135
Thomas Huth39fb7952019-05-15 19:43:27 +0200136 help = qtest_hmp(qts, "device_add help");
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200137 g_free(help);
138
Thomas Huth39fb7952019-05-15 19:43:27 +0200139 qtest_quit(qts);
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200140}
141
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300142/*
143 * Ensure all entries returned by qom-list-types implements=<parent>
144 * have <parent> as a parent.
145 */
Thomas Huth39fb7952019-05-15 19:43:27 +0200146static void test_qom_list_parents(QTestState *qts, const char *parent)
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300147{
148 QList *types;
149 QListEntry *e;
150 QDict *index;
151
Thomas Huth39fb7952019-05-15 19:43:27 +0200152 types = qom_list_types(qts, parent, true);
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300153 index = qom_type_index(types);
154
155 QLIST_FOREACH_ENTRY(types, e) {
Max Reitz7dc847e2018-02-24 16:40:29 +0100156 QDict *d = qobject_to(QDict, qlist_entry_obj(e));
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300157 const char *name = qdict_get_str(d, "name");
158
159 g_assert(qom_has_parent(index, name, parent));
160 }
161
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200162 qobject_unref(types);
163 qobject_unref(index);
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300164}
165
Eduardo Habkost87467ea2017-07-07 09:22:14 -0300166static void test_qom_list_fields(void)
167{
168 QList *all_types;
169 QList *non_abstract;
170 QListEntry *e;
Thomas Huth39fb7952019-05-15 19:43:27 +0200171 QTestState *qts;
Eduardo Habkost87467ea2017-07-07 09:22:14 -0300172
Thomas Huth39fb7952019-05-15 19:43:27 +0200173 qts = qtest_init(common_args);
Eduardo Habkost87467ea2017-07-07 09:22:14 -0300174
Thomas Huth39fb7952019-05-15 19:43:27 +0200175 all_types = qom_list_types(qts, NULL, true);
176 non_abstract = qom_list_types(qts, NULL, false);
Eduardo Habkost87467ea2017-07-07 09:22:14 -0300177
178 QLIST_FOREACH_ENTRY(all_types, e) {
Max Reitz7dc847e2018-02-24 16:40:29 +0100179 QDict *d = qobject_to(QDict, qlist_entry_obj(e));
Eduardo Habkost87467ea2017-07-07 09:22:14 -0300180 const char *name = qdict_get_str(d, "name");
181 bool abstract = qdict_haskey(d, "abstract") ?
182 qdict_get_bool(d, "abstract") :
183 false;
184 bool expected_abstract = !type_list_find(non_abstract, name);
185
186 g_assert(abstract == expected_abstract);
187 }
188
Thomas Huth39fb7952019-05-15 19:43:27 +0200189 test_qom_list_parents(qts, "object");
190 test_qom_list_parents(qts, "device");
191 test_qom_list_parents(qts, "sys-bus-device");
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300192
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200193 qobject_unref(all_types);
194 qobject_unref(non_abstract);
Thomas Huth39fb7952019-05-15 19:43:27 +0200195 qtest_quit(qts);
Eduardo Habkost87467ea2017-07-07 09:22:14 -0300196}
197
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200198static void test_device_intro_none(void)
199{
Thomas Huth39fb7952019-05-15 19:43:27 +0200200 QTestState *qts = qtest_init(common_args);
Daniel P. Berrangé3e7b80f2020-07-10 08:07:19 +0200201 g_autofree char *qom_tree_start = qtest_hmp(qts, "info qom-tree");
202 g_autofree char *qom_tree_end = NULL;
203 g_autofree char *qtree_start = qtest_hmp(qts, "info qtree");
204 g_autofree char *qtree_end = NULL;
Thomas Huth39fb7952019-05-15 19:43:27 +0200205
206 test_one_device(qts, "nonexistent");
Daniel P. Berrangé3e7b80f2020-07-10 08:07:19 +0200207
208 /* Make sure that really nothing changed in the trees */
209 qom_tree_end = qtest_hmp(qts, "info qom-tree");
210 g_assert_cmpstr(qom_tree_start, ==, qom_tree_end);
211 qtree_end = qtest_hmp(qts, "info qtree");
212 g_assert_cmpstr(qtree_start, ==, qtree_end);
213
Thomas Huth39fb7952019-05-15 19:43:27 +0200214 qtest_quit(qts);
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200215}
216
217static void test_device_intro_abstract(void)
218{
Thomas Huth39fb7952019-05-15 19:43:27 +0200219 QTestState *qts = qtest_init(common_args);
Daniel P. Berrangé3e7b80f2020-07-10 08:07:19 +0200220 g_autofree char *qom_tree_start = qtest_hmp(qts, "info qom-tree");
221 g_autofree char *qom_tree_end = NULL;
222 g_autofree char *qtree_start = qtest_hmp(qts, "info qtree");
223 g_autofree char *qtree_end = NULL;
Thomas Huth39fb7952019-05-15 19:43:27 +0200224
225 test_one_device(qts, "device");
Daniel P. Berrangé3e7b80f2020-07-10 08:07:19 +0200226
227 /* Make sure that really nothing changed in the trees */
228 qom_tree_end = qtest_hmp(qts, "info qom-tree");
229 g_assert_cmpstr(qom_tree_start, ==, qom_tree_end);
230 qtree_end = qtest_hmp(qts, "info qtree");
231 g_assert_cmpstr(qtree_start, ==, qtree_end);
232
Thomas Huth39fb7952019-05-15 19:43:27 +0200233 qtest_quit(qts);
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200234}
235
Thomas Huth410573a2018-08-16 13:35:57 +0200236static void test_device_intro_concrete(const void *args)
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200237{
238 QList *types;
239 QListEntry *entry;
240 const char *type;
Daniel P. Berrangé3e7b80f2020-07-10 08:07:19 +0200241 QTestState *qts = qtest_init(args);
242 g_autofree char *qom_tree_start = qtest_hmp(qts, "info qom-tree");
243 g_autofree char *qom_tree_end = NULL;
244 g_autofree char *qtree_start = qtest_hmp(qts, "info qtree");
245 g_autofree char *qtree_end = NULL;
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200246
Thomas Huth39fb7952019-05-15 19:43:27 +0200247 types = device_type_list(qts, false);
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200248
249 QLIST_FOREACH_ENTRY(types, entry) {
Max Reitz7dc847e2018-02-24 16:40:29 +0100250 type = qdict_get_try_str(qobject_to(QDict, qlist_entry_obj(entry)),
251 "name");
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200252 g_assert(type);
Thomas Huth39fb7952019-05-15 19:43:27 +0200253 test_one_device(qts, type);
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200254 }
255
Daniel P. Berrangé3e7b80f2020-07-10 08:07:19 +0200256 /*
257 * Some devices leave dangling pointers in QOM behind.
258 * "info qom-tree" or "info qtree" have a good chance at crashing then.
259 * Also make sure that the tree did not change.
260 */
261 qom_tree_end = qtest_hmp(qts, "info qom-tree");
262 g_assert_cmpstr(qom_tree_start, ==, qom_tree_end);
263
264 qtree_end = qtest_hmp(qts, "info qtree");
265 g_assert_cmpstr(qtree_start, ==, qtree_end);
266
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200267 qobject_unref(types);
Thomas Huth39fb7952019-05-15 19:43:27 +0200268 qtest_quit(qts);
Thomas Huth410573a2018-08-16 13:35:57 +0200269 g_free((void *)args);
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200270}
271
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -0200272static void test_abstract_interfaces(void)
273{
274 QList *all_types;
Eduardo Habkostdbb2a602017-07-07 09:22:13 -0300275 QListEntry *e;
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300276 QDict *index;
Thomas Huth39fb7952019-05-15 19:43:27 +0200277 QTestState *qts;
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -0200278
Thomas Huth39fb7952019-05-15 19:43:27 +0200279 qts = qtest_init(common_args);
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300280
Thomas Huth39fb7952019-05-15 19:43:27 +0200281 all_types = qom_list_types(qts, "interface", true);
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300282 index = qom_type_index(all_types);
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -0200283
Eduardo Habkostdbb2a602017-07-07 09:22:13 -0300284 QLIST_FOREACH_ENTRY(all_types, e) {
Max Reitz7dc847e2018-02-24 16:40:29 +0100285 QDict *d = qobject_to(QDict, qlist_entry_obj(e));
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300286 const char *name = qdict_get_str(d, "name");
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -0200287
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300288 /*
289 * qom-list-types implements=interface returns all types
290 * that implement _any_ interface (not just interface
291 * types), so skip the ones that don't have "interface"
292 * on the parent type chain.
293 */
294 if (!qom_has_parent(index, name, "interface")) {
Eduardo Habkost87467ea2017-07-07 09:22:14 -0300295 /* Not an interface type */
296 continue;
297 }
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -0200298
Eduardo Habkost87467ea2017-07-07 09:22:14 -0300299 g_assert(qdict_haskey(d, "abstract") && qdict_get_bool(d, "abstract"));
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -0200300 }
301
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200302 qobject_unref(all_types);
303 qobject_unref(index);
Thomas Huth39fb7952019-05-15 19:43:27 +0200304 qtest_quit(qts);
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -0200305}
306
Thomas Huth410573a2018-08-16 13:35:57 +0200307static void add_machine_test_case(const char *mname)
308{
309 char *path, *args;
310
Thomas Huth410573a2018-08-16 13:35:57 +0200311 path = g_strdup_printf("device/introspect/concrete/defaults/%s", mname);
312 args = g_strdup_printf("-M %s", mname);
313 qtest_add_data_func(path, args, test_device_intro_concrete);
314 g_free(path);
315
316 path = g_strdup_printf("device/introspect/concrete/nodefaults/%s", mname);
317 args = g_strdup_printf("-nodefaults -M %s", mname);
318 qtest_add_data_func(path, args, test_device_intro_concrete);
319 g_free(path);
320}
321
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200322int main(int argc, char **argv)
323{
324 g_test_init(&argc, &argv, NULL);
325
326 qtest_add_func("device/introspect/list", test_device_intro_list);
Eduardo Habkost87467ea2017-07-07 09:22:14 -0300327 qtest_add_func("device/introspect/list-fields", test_qom_list_fields);
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200328 qtest_add_func("device/introspect/none", test_device_intro_none);
329 qtest_add_func("device/introspect/abstract", test_device_intro_abstract);
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -0200330 qtest_add_func("device/introspect/abstract-interfaces", test_abstract_interfaces);
Thomas Huth410573a2018-08-16 13:35:57 +0200331 if (g_test_quick()) {
332 qtest_add_data_func("device/introspect/concrete/defaults/none",
333 g_strdup(common_args), test_device_intro_concrete);
334 } else {
335 qtest_cb_for_every_machine(add_machine_test_case, true);
336 }
Markus Armbruster2d1abb82015-10-01 10:59:56 +0200337
338 return g_test_run();
339}