]> git.proxmox.com Git - mirror_qemu.git/blame - tests/qtest/qom-test.c
Merge tag 'pull-ppc-20211217' of https://github.com/legoater/qemu into staging
[mirror_qemu.git] / tests / qtest / qom-test.c
CommitLineData
7c41f217
AF
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 */
7c41f217 9
681c28a3 10#include "qemu/osdep.h"
91f32b0c 11
dc06cbd2 12#include "qemu-common.h"
452fcdbc 13#include "qapi/qmp/qdict.h"
47e6b297 14#include "qapi/qmp/qlist.h"
f348b6d1 15#include "qemu/cutils.h"
a2ce7dbd 16#include "libqos/libqtest.h"
5c1904f1 17
9266ebc9 18static void test_properties(QTestState *qts, const char *path, bool recurse)
dc06cbd2
AF
19{
20 char *child_path;
0d2cd785 21 QDict *response, *tuple, *tmp;
dc06cbd2
AF
22 QList *list;
23 QListEntry *entry;
24
25 g_test_message("Obtaining properties of %s", path);
9266ebc9
TH
26 response = qtest_qmp(qts, "{ 'execute': 'qom-list',"
27 " 'arguments': { 'path': %s } }", path);
dc06cbd2
AF
28 g_assert(response);
29
0380aef3 30 if (!recurse) {
cb3e7f08 31 qobject_unref(response);
0380aef3
CR
32 return;
33 }
34
dc06cbd2 35 g_assert(qdict_haskey(response, "return"));
7dc847eb 36 list = qobject_to(QList, qdict_get(response, "return"));
dc06cbd2 37 QLIST_FOREACH_ENTRY(list, entry) {
7dc847eb 38 tuple = qobject_to(QDict, qlist_entry_obj(entry));
0380aef3
CR
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) {
dc06cbd2
AF
43 child_path = g_strdup_printf("%s/%s",
44 path, qdict_get_str(tuple, "name"));
9266ebc9 45 test_properties(qts, child_path, is_child);
dc06cbd2
AF
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);
9266ebc9
TH
50 tmp = qtest_qmp(qts,
51 "{ 'execute': 'qom-get',"
52 " 'arguments': { 'path': %s, 'property': %s } }",
53 path, prop);
dc06cbd2 54 /* qom-get may fail but should not, e.g., segfault. */
0d2cd785 55 g_assert(tmp);
cb3e7f08 56 qobject_unref(tmp);
dc06cbd2
AF
57 }
58 }
cb3e7f08 59 qobject_unref(response);
dc06cbd2
AF
60}
61
bb6c5e3c 62static void test_machine(gconstpointer data)
7c41f217 63{
7c41f217 64 const char *machine = data;
bb6c5e3c 65 QDict *response;
9266ebc9 66 QTestState *qts;
7c41f217 67
9266ebc9 68 qts = qtest_initf("-machine %s", machine);
dc06cbd2 69
9266ebc9 70 test_properties(qts, "/machine", true);
dc06cbd2 71
9266ebc9 72 response = qtest_qmp(qts, "{ 'execute': 'quit' }");
bb6c5e3c 73 g_assert(qdict_haskey(response, "return"));
cb3e7f08 74 qobject_unref(response);
dc06cbd2 75
9266ebc9 76 qtest_quit(qts);
0d2cd785 77 g_free((void *)machine);
7c41f217
AF
78}
79
02ef6e87 80static void add_machine_test_case(const char *mname)
7c41f217 81{
9a709f06 82 char *path;
5c1904f1 83
9a709f06
OH
84 path = g_strdup_printf("qom/%s", mname);
85 qtest_add_data_func(path, g_strdup(mname), test_machine);
86 g_free(path);
7c41f217
AF
87}
88
7c41f217
AF
89int main(int argc, char **argv)
90{
7c41f217
AF
91 g_test_init(&argc, &argv, NULL);
92
1f4a0d81 93 qtest_cb_for_every_machine(add_machine_test_case, g_test_quick());
7c41f217
AF
94
95 return g_test_run();
96}