device-introspect-test: New, covering device introspection
[qemu/ar7.git] / tests / device-introspect-test.c
blobf240b5c827db56d784498397e087afaaa8807908
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 <glib.h>
21 #include <stdarg.h>
22 #include "qemu-common.h"
23 #include "qapi/qmp/qstring.h"
24 #include "libqtest.h"
26 const char common_args[] = "-nodefaults -machine none";
28 static QList *device_type_list(bool abstract)
30 QDict *resp;
31 QList *ret;
33 resp = qmp("{'execute': 'qom-list-types',"
34 " 'arguments': {'implements': 'device', 'abstract': %i}}",
35 abstract);
36 g_assert(qdict_haskey(resp, "return"));
37 ret = qdict_get_qlist(resp, "return");
38 QINCREF(ret);
39 QDECREF(resp);
40 return ret;
43 static void test_one_device(const char *type)
45 QDict *resp;
46 char *help, *qom_tree;
49 * Skip this part for the abstract device test case, because
50 * device-list-properties crashes for such devices.
51 * FIXME fix it not to crash
53 if (strcmp(type, "device")) {
54 resp = qmp("{'execute': 'device-list-properties',"
55 " 'arguments': {'typename': %s}}",
56 type);
57 QDECREF(resp);
60 help = hmp("device_add \"%s,help\"", type);
61 g_free(help);
64 * Some devices leave dangling pointers in QOM behind.
65 * "info qom-tree" has a good chance at crashing then
67 qom_tree = hmp("info qom-tree");
68 g_free(qom_tree);
71 static void test_device_intro_list(void)
73 QList *types;
74 char *help;
76 qtest_start(common_args);
78 types = device_type_list(true);
79 QDECREF(types);
81 help = hmp("device_add help");
82 g_free(help);
84 qtest_end();
87 static void test_device_intro_none(void)
89 qtest_start(common_args);
90 test_one_device("nonexistent");
91 qtest_end();
94 static void test_device_intro_abstract(void)
96 qtest_start(common_args);
97 test_one_device("device");
98 qtest_end();
101 static bool blacklisted(const char *type)
103 static const char *blacklist[] = {
104 /* hang in object_unref(): */
105 "realview_pci", "versatile_pci",
106 /* create a CPU, thus use after free (see below): */
107 "allwinner-a10", "digic", "fsl,imx25", "fsl,imx31", "xlnx,zynqmp",
109 size_t len = strlen(type);
110 int i;
112 if (len >= 4 && !strcmp(type + len - 4, "-cpu")) {
113 /* use after free: cpu_exec_init() saves CPUState in cpus */
114 return true;
117 for (i = 0; i < ARRAY_SIZE(blacklist); i++) {
118 if (!strcmp(blacklist[i], type)) {
119 return true;
122 return false;
125 static void test_device_intro_concrete(void)
127 QList *types;
128 QListEntry *entry;
129 const char *type;
131 qtest_start(common_args);
132 types = device_type_list(false);
134 QLIST_FOREACH_ENTRY(types, entry) {
135 type = qdict_get_try_str(qobject_to_qdict(qlist_entry_obj(entry)),
136 "name");
137 g_assert(type);
138 if (blacklisted(type)) {
139 continue; /* FIXME broken device, skip */
141 test_one_device(type);
144 QDECREF(types);
145 qtest_end();
148 int main(int argc, char **argv)
150 g_test_init(&argc, &argv, NULL);
152 qtest_add_func("device/introspect/list", test_device_intro_list);
153 qtest_add_func("device/introspect/none", test_device_intro_none);
154 qtest_add_func("device/introspect/abstract", test_device_intro_abstract);
155 qtest_add_func("device/introspect/concrete", test_device_intro_concrete);
157 return g_test_run();