xref: /openbmc/qemu/tests/qtest/qom-test.c (revision ffe98631)
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 
12 #include "qapi/qmp/qdict.h"
13 #include "qapi/qmp/qlist.h"
14 #include "qemu/cutils.h"
15 #include "libqtest.h"
16 
17 static bool verbose;
18 
19 static void test_properties(QTestState *qts, const char *path, bool recurse)
20 {
21     char *child_path;
22     QDict *response, *tuple, *tmp;
23     QList *list;
24     QListEntry *entry;
25     GSList *children = NULL, *links = NULL;
26 
27     g_test_message("Obtaining properties of %s", path);
28     response = qtest_qmp(qts, "{ 'execute': 'qom-list',"
29                               "  'arguments': { 'path': %s } }", path);
30     g_assert(response);
31 
32     if (!recurse) {
33         qobject_unref(response);
34         return;
35     }
36 
37     g_assert(qdict_haskey(response, "return"));
38     list = qobject_to(QList, qdict_get(response, "return"));
39     QLIST_FOREACH_ENTRY(list, entry) {
40         tuple = qobject_to(QDict, qlist_entry_obj(entry));
41         bool is_child = strstart(qdict_get_str(tuple, "type"), "child<", NULL);
42         bool is_link = strstart(qdict_get_str(tuple, "type"), "link<", NULL);
43 
44         if (is_child || is_link) {
45             child_path = g_strdup_printf("%s/%s",
46                                          path, qdict_get_str(tuple, "name"));
47             if (is_child) {
48                 children = g_slist_prepend(children, child_path);
49             } else {
50                 links = g_slist_prepend(links, child_path);
51             }
52         } else {
53             const char *prop = qdict_get_str(tuple, "name");
54             if (verbose) {
55                 g_test_message("-> %s", prop);
56             }
57             tmp = qtest_qmp(qts,
58                             "{ 'execute': 'qom-get',"
59                             "  'arguments': { 'path': %s, 'property': %s } }",
60                             path, prop);
61             /* qom-get may fail but should not, e.g., segfault. */
62             g_assert(tmp);
63             qobject_unref(tmp);
64         }
65     }
66 
67     while (links) {
68         test_properties(qts, links->data, false);
69         g_free(links->data);
70         links = g_slist_delete_link(links, links);
71     }
72     while (children) {
73         test_properties(qts, children->data, true);
74         g_free(children->data);
75         children = g_slist_delete_link(children, children);
76     }
77 
78     qobject_unref(response);
79 }
80 
81 static void test_machine(gconstpointer data)
82 {
83     const char *machine = data;
84     QDict *response;
85     QTestState *qts;
86 
87     qts = qtest_initf("-machine %s", machine);
88 
89     test_properties(qts, "/machine", true);
90 
91     response = qtest_qmp(qts, "{ 'execute': 'quit' }");
92     g_assert(qdict_haskey(response, "return"));
93     qobject_unref(response);
94 
95     qtest_quit(qts);
96     g_free((void *)machine);
97 }
98 
99 static void add_machine_test_case(const char *mname)
100 {
101     char *path;
102 
103     path = g_strdup_printf("qom/%s", mname);
104     qtest_add_data_func(path, g_strdup(mname), test_machine);
105     g_free(path);
106 }
107 
108 int main(int argc, char **argv)
109 {
110     char *v_env = getenv("V");
111 
112     if (v_env && atoi(v_env) >= 2) {
113         verbose = true;
114     }
115 
116     g_test_init(&argc, &argv, NULL);
117 
118     qtest_cb_for_every_machine(add_machine_test_case, g_test_quick());
119 
120     return g_test_run();
121 }
122