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