xref: /openbmc/qemu/tests/qtest/qom-test.c (revision a68694cd)
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 "qemu-common.h"
13 #include "qapi/qmp/qdict.h"
14 #include "qapi/qmp/qlist.h"
15 #include "qemu/cutils.h"
16 #include "libqos/libqtest.h"
17 
18 static void test_properties(QTestState *qts, const char *path, bool recurse)
19 {
20     char *child_path;
21     QDict *response, *tuple, *tmp;
22     QList *list;
23     QListEntry *entry;
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             test_properties(qts, child_path, is_child);
46             g_free(child_path);
47         } else {
48             const char *prop = qdict_get_str(tuple, "name");
49             g_test_message("Testing property %s.%s", path, prop);
50             tmp = qtest_qmp(qts,
51                             "{ 'execute': 'qom-get',"
52                             "  'arguments': { 'path': %s, 'property': %s } }",
53                             path, prop);
54             /* qom-get may fail but should not, e.g., segfault. */
55             g_assert(tmp);
56             qobject_unref(tmp);
57         }
58     }
59     qobject_unref(response);
60 }
61 
62 static void test_machine(gconstpointer data)
63 {
64     const char *machine = data;
65     QDict *response;
66     QTestState *qts;
67 
68     qts = qtest_initf("-machine %s", machine);
69 
70     test_properties(qts, "/machine", true);
71 
72     response = qtest_qmp(qts, "{ 'execute': 'quit' }");
73     g_assert(qdict_haskey(response, "return"));
74     qobject_unref(response);
75 
76     qtest_quit(qts);
77     g_free((void *)machine);
78 }
79 
80 static void add_machine_test_case(const char *mname)
81 {
82     char *path;
83 
84     path = g_strdup_printf("qom/%s", mname);
85     qtest_add_data_func(path, g_strdup(mname), test_machine);
86     g_free(path);
87 }
88 
89 int main(int argc, char **argv)
90 {
91     g_test_init(&argc, &argv, NULL);
92 
93     qtest_cb_for_every_machine(add_machine_test_case, g_test_quick());
94 
95     return g_test_run();
96 }
97