These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / tests / qom-test.c
1 /*
2  * QTest testcase for QOM
3  *
4  * Copyright (c) 2013 SUSE LINUX Products GmbH
5  *
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  */
9
10 #include "qemu/osdep.h"
11 #include <glib.h>
12
13 #include "qemu-common.h"
14 #include "qemu/cutils.h"
15 #include "libqtest.h"
16 #include "qapi/qmp/types.h"
17
18 static const char *blacklist_x86[] = {
19     "xenfv", "xenpv", NULL
20 };
21
22 static const struct {
23     const char *arch;
24     const char **machine;
25 } blacklists[] = {
26     { "i386", blacklist_x86 },
27     { "x86_64", blacklist_x86 },
28 };
29
30 static bool is_blacklisted(const char *arch, const char *mach)
31 {
32     int i;
33     const char **p;
34
35     for (i = 0; i < ARRAY_SIZE(blacklists); i++) {
36         if (!strcmp(blacklists[i].arch, arch)) {
37             for (p = blacklists[i].machine; *p; p++) {
38                 if (!strcmp(*p, mach)) {
39                     return true;
40                 }
41             }
42         }
43     }
44     return false;
45 }
46
47 static void test_properties(const char *path, bool recurse)
48 {
49     char *child_path;
50     QDict *response, *tuple, *tmp;
51     QList *list;
52     QListEntry *entry;
53
54     g_test_message("Obtaining properties of %s", path);
55     response = qmp("{ 'execute': 'qom-list',"
56                    "  'arguments': { 'path': %s } }", path);
57     g_assert(response);
58
59     if (!recurse) {
60         QDECREF(response);
61         return;
62     }
63
64     g_assert(qdict_haskey(response, "return"));
65     list = qobject_to_qlist(qdict_get(response, "return"));
66     QLIST_FOREACH_ENTRY(list, entry) {
67         tuple = qobject_to_qdict(qlist_entry_obj(entry));
68         bool is_child = strstart(qdict_get_str(tuple, "type"), "child<", NULL);
69         bool is_link = strstart(qdict_get_str(tuple, "type"), "link<", NULL);
70
71         if (is_child || is_link) {
72             child_path = g_strdup_printf("%s/%s",
73                                          path, qdict_get_str(tuple, "name"));
74             test_properties(child_path, is_child);
75             g_free(child_path);
76         } else {
77             const char *prop = qdict_get_str(tuple, "name");
78             g_test_message("Testing property %s.%s", path, prop);
79             tmp = qmp("{ 'execute': 'qom-get',"
80                       "  'arguments': { 'path': %s,"
81                       "                 'property': %s } }",
82                       path, prop);
83             /* qom-get may fail but should not, e.g., segfault. */
84             g_assert(tmp);
85             QDECREF(tmp);
86         }
87     }
88     QDECREF(response);
89 }
90
91 static void test_machine(gconstpointer data)
92 {
93     const char *machine = data;
94     char *args;
95     QDict *response;
96
97     args = g_strdup_printf("-machine %s", machine);
98     qtest_start(args);
99
100     test_properties("/machine", true);
101
102     response = qmp("{ 'execute': 'quit' }");
103     g_assert(qdict_haskey(response, "return"));
104     QDECREF(response);
105
106     qtest_end();
107     g_free(args);
108     g_free((void *)machine);
109 }
110
111 static void add_machine_test_cases(void)
112 {
113     const char *arch = qtest_get_arch();
114     QDict *response, *minfo;
115     QList *list;
116     const QListEntry *p;
117     QObject *qobj;
118     QString *qstr;
119     const char *mname, *path;
120
121     qtest_start("-machine none");
122     response = qmp("{ 'execute': 'query-machines' }");
123     g_assert(response);
124     list = qdict_get_qlist(response, "return");
125     g_assert(list);
126
127     for (p = qlist_first(list); p; p = qlist_next(p)) {
128         minfo = qobject_to_qdict(qlist_entry_obj(p));
129         g_assert(minfo);
130         qobj = qdict_get(minfo, "name");
131         g_assert(qobj);
132         qstr = qobject_to_qstring(qobj);
133         g_assert(qstr);
134         mname = qstring_get_str(qstr);
135         if (!is_blacklisted(arch, mname)) {
136             path = g_strdup_printf("qom/%s", mname);
137             qtest_add_data_func(path, g_strdup(mname), test_machine);
138         }
139     }
140
141     qtest_end();
142     QDECREF(response);
143 }
144
145 int main(int argc, char **argv)
146 {
147     g_test_init(&argc, &argv, NULL);
148
149     add_machine_test_cases();
150
151     return g_test_run();
152 }