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