configure: eliminate Python dependency for --help
[qemu/ar7.git] / tests / qom-test.c
blobd48f890e84bc78216e7054d0e0a247b3cbca9c86
1 /*
2 * QTest testcase for QOM
4 * Copyright (c) 2013 SUSE LINUX Products GmbH
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
8 */
10 #include "qemu/osdep.h"
12 #include "qemu-common.h"
13 #include "qemu/cutils.h"
14 #include "libqtest.h"
15 #include "qapi/qmp/types.h"
17 static const char *blacklist_x86[] = {
18 "xenfv", "xenpv", NULL
21 static const struct {
22 const char *arch;
23 const char **machine;
24 } blacklists[] = {
25 { "i386", blacklist_x86 },
26 { "x86_64", blacklist_x86 },
29 static bool is_blacklisted(const char *arch, const char *mach)
31 int i;
32 const char **p;
34 for (i = 0; i < ARRAY_SIZE(blacklists); i++) {
35 if (!strcmp(blacklists[i].arch, arch)) {
36 for (p = blacklists[i].machine; *p; p++) {
37 if (!strcmp(*p, mach)) {
38 return true;
43 return false;
46 static void test_properties(const char *path, bool recurse)
48 char *child_path;
49 QDict *response, *tuple, *tmp;
50 QList *list;
51 QListEntry *entry;
53 g_test_message("Obtaining properties of %s", path);
54 response = qmp("{ 'execute': 'qom-list',"
55 " 'arguments': { 'path': %s } }", path);
56 g_assert(response);
58 if (!recurse) {
59 QDECREF(response);
60 return;
63 g_assert(qdict_haskey(response, "return"));
64 list = qobject_to_qlist(qdict_get(response, "return"));
65 QLIST_FOREACH_ENTRY(list, entry) {
66 tuple = qobject_to_qdict(qlist_entry_obj(entry));
67 bool is_child = strstart(qdict_get_str(tuple, "type"), "child<", NULL);
68 bool is_link = strstart(qdict_get_str(tuple, "type"), "link<", NULL);
70 if (is_child || is_link) {
71 child_path = g_strdup_printf("%s/%s",
72 path, qdict_get_str(tuple, "name"));
73 test_properties(child_path, is_child);
74 g_free(child_path);
75 } else {
76 const char *prop = qdict_get_str(tuple, "name");
77 g_test_message("Testing property %s.%s", path, prop);
78 tmp = qmp("{ 'execute': 'qom-get',"
79 " 'arguments': { 'path': %s,"
80 " 'property': %s } }",
81 path, prop);
82 /* qom-get may fail but should not, e.g., segfault. */
83 g_assert(tmp);
84 QDECREF(tmp);
87 QDECREF(response);
90 static void test_machine(gconstpointer data)
92 const char *machine = data;
93 char *args;
94 QDict *response;
96 args = g_strdup_printf("-machine %s", machine);
97 qtest_start(args);
99 test_properties("/machine", true);
101 response = qmp("{ 'execute': 'quit' }");
102 g_assert(qdict_haskey(response, "return"));
103 QDECREF(response);
105 qtest_end();
106 g_free(args);
107 g_free((void *)machine);
110 static void add_machine_test_cases(void)
112 const char *arch = qtest_get_arch();
113 QDict *response, *minfo;
114 QList *list;
115 const QListEntry *p;
116 QObject *qobj;
117 QString *qstr;
118 const char *mname;
120 qtest_start("-machine none");
121 response = qmp("{ 'execute': 'query-machines' }");
122 g_assert(response);
123 list = qdict_get_qlist(response, "return");
124 g_assert(list);
126 for (p = qlist_first(list); p; p = qlist_next(p)) {
127 minfo = qobject_to_qdict(qlist_entry_obj(p));
128 g_assert(minfo);
129 qobj = qdict_get(minfo, "name");
130 g_assert(qobj);
131 qstr = qobject_to_qstring(qobj);
132 g_assert(qstr);
133 mname = qstring_get_str(qstr);
134 if (!is_blacklisted(arch, mname)) {
135 char *path = g_strdup_printf("qom/%s", mname);
136 qtest_add_data_func(path, g_strdup(mname), test_machine);
137 g_free(path);
141 qtest_end();
142 QDECREF(response);
145 int main(int argc, char **argv)
147 g_test_init(&argc, &argv, NULL);
149 add_machine_test_cases();
151 return g_test_run();