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