]> git.proxmox.com Git - mirror_qemu.git/blame - tests/device-introspect-test.c
test-cutils: Cover qemu_strtosz() invalid input
[mirror_qemu.git] / tests / device-introspect-test.c
CommitLineData
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
EH
23#include "qapi/qmp/qbool.h"
24#include "qapi/qmp/qdict.h"
2d1abb85
MA
25#include "libqtest.h"
26
27const char common_args[] = "-nodefaults -machine none";
28
1c6d75d5 29static 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
1c6d75d5
EH
35 qdict_put(args, "abstract", qbool_from_bool(abstract));
36 if (implements) {
37 qdict_put(args, "implements", qstring_from_str(implements));
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
1c6d75d5
EH
48static QList *device_type_list(bool abstract)
49{
50 return qom_list_types("device", abstract);
51}
52
2d1abb85
MA
53static void test_one_device(const char *type)
54{
55 QDict *resp;
56 char *help, *qom_tree;
57
edb1523d
MA
58 resp = qmp("{'execute': 'device-list-properties',"
59 " 'arguments': {'typename': %s}}",
60 type);
61 QDECREF(resp);
2d1abb85
MA
62
63 help = hmp("device_add \"%s,help\"", type);
64 g_free(help);
65
66 /*
67 * Some devices leave dangling pointers in QOM behind.
68 * "info qom-tree" has a good chance at crashing then
69 */
70 qom_tree = hmp("info qom-tree");
71 g_free(qom_tree);
72}
73
74static void test_device_intro_list(void)
75{
76 QList *types;
77 char *help;
78
79 qtest_start(common_args);
80
81 types = device_type_list(true);
82 QDECREF(types);
83
84 help = hmp("device_add help");
85 g_free(help);
86
87 qtest_end();
88}
89
90static void test_device_intro_none(void)
91{
92 qtest_start(common_args);
93 test_one_device("nonexistent");
94 qtest_end();
95}
96
97static void test_device_intro_abstract(void)
98{
99 qtest_start(common_args);
100 test_one_device("device");
101 qtest_end();
102}
103
2d1abb85
MA
104static void test_device_intro_concrete(void)
105{
106 QList *types;
107 QListEntry *entry;
108 const char *type;
109
110 qtest_start(common_args);
111 types = device_type_list(false);
112
113 QLIST_FOREACH_ENTRY(types, entry) {
114 type = qdict_get_try_str(qobject_to_qdict(qlist_entry_obj(entry)),
115 "name");
116 g_assert(type);
2d1abb85
MA
117 test_one_device(type);
118 }
119
120 QDECREF(types);
121 qtest_end();
122}
123
1c6d75d5
EH
124static void test_abstract_interfaces(void)
125{
126 QList *all_types;
127 QList *obj_types;
128 QListEntry *ae;
129
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
136 * on the first list
137 */
138 all_types = qom_list_types(NULL, false);
139 obj_types = qom_list_types("object", false);
140
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");
144 QListEntry *oe;
145 const char *found = NULL;
146
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)) {
151 found = oname;
152 break;
153 }
154 }
155
156 /* Using g_assert_cmpstr() will give more useful failure
157 * messages than g_assert(found) */
158 g_assert_cmpstr(aname, ==, found);
159 }
160
161 QDECREF(all_types);
162 QDECREF(obj_types);
163 qtest_end();
164}
165
2d1abb85
MA
166int main(int argc, char **argv)
167{
168 g_test_init(&argc, &argv, NULL);
169
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);
1c6d75d5 174 qtest_add_func("device/introspect/abstract-interfaces", test_abstract_interfaces);
2d1abb85
MA
175
176 return g_test_run();
177}