trap signals for "-serial mon:stdio"
[qemu.git] / tests / test-qmp-commands.c
1 #include <glib.h>
2 #include "qemu-common.h"
3 #include "qapi/qmp/types.h"
4 #include "test-qmp-commands.h"
5 #include "qapi/qmp/dispatch.h"
6 #include "qemu/module.h"
7 #include "qapi/qmp-input-visitor.h"
8 #include "tests/test-qapi-types.h"
9 #include "tests/test-qapi-visit.h"
10
11 void qmp_user_def_cmd(Error **errp)
12 {
13 }
14
15 void qmp_user_def_cmd1(UserDefOne * ud1, Error **errp)
16 {
17 }
18
19 UserDefTwo * qmp_user_def_cmd2(UserDefOne * ud1a, UserDefOne * ud1b, Error **errp)
20 {
21 UserDefTwo *ret;
22 UserDefOne *ud1c = g_malloc0(sizeof(UserDefOne));
23 UserDefOne *ud1d = g_malloc0(sizeof(UserDefOne));
24
25 ud1c->string = strdup(ud1a->string);
26 ud1c->integer = ud1a->integer;
27 ud1d->string = strdup(ud1b->string);
28 ud1d->integer = ud1b->integer;
29
30 ret = g_malloc0(sizeof(UserDefTwo));
31 ret->string = strdup("blah1");
32 ret->dict.string = strdup("blah2");
33 ret->dict.dict.userdef = ud1c;
34 ret->dict.dict.string = strdup("blah3");
35 ret->dict.has_dict2 = true;
36 ret->dict.dict2.userdef = ud1d;
37 ret->dict.dict2.string = strdup("blah4");
38
39 return ret;
40 }
41
42 /* test commands with no input and no return value */
43 static void test_dispatch_cmd(void)
44 {
45 QDict *req = qdict_new();
46 QObject *resp;
47
48 qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd")));
49
50 resp = qmp_dispatch(QOBJECT(req));
51 assert(resp != NULL);
52 assert(!qdict_haskey(qobject_to_qdict(resp), "error"));
53
54 qobject_decref(resp);
55 QDECREF(req);
56 }
57
58 /* test commands that return an error due to invalid parameters */
59 static void test_dispatch_cmd_error(void)
60 {
61 QDict *req = qdict_new();
62 QObject *resp;
63
64 qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd2")));
65
66 resp = qmp_dispatch(QOBJECT(req));
67 assert(resp != NULL);
68 assert(qdict_haskey(qobject_to_qdict(resp), "error"));
69
70 qobject_decref(resp);
71 QDECREF(req);
72 }
73
74 /* test commands that involve both input parameters and return values */
75 static void test_dispatch_cmd_io(void)
76 {
77 QDict *req = qdict_new();
78 QDict *args = qdict_new();
79 QDict *ud1a = qdict_new();
80 QDict *ud1b = qdict_new();
81 QObject *resp;
82
83 qdict_put_obj(ud1a, "integer", QOBJECT(qint_from_int(42)));
84 qdict_put_obj(ud1a, "string", QOBJECT(qstring_from_str("hello")));
85 qdict_put_obj(ud1b, "integer", QOBJECT(qint_from_int(422)));
86 qdict_put_obj(ud1b, "string", QOBJECT(qstring_from_str("hello2")));
87 qdict_put_obj(args, "ud1a", QOBJECT(ud1a));
88 qdict_put_obj(args, "ud1b", QOBJECT(ud1b));
89 qdict_put_obj(req, "arguments", QOBJECT(args));
90
91 qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd2")));
92
93 /* TODO: put in full payload and check for errors */
94 resp = qmp_dispatch(QOBJECT(req));
95 assert(resp != NULL);
96 assert(!qdict_haskey(qobject_to_qdict(resp), "error"));
97
98 qobject_decref(resp);
99 QDECREF(req);
100 }
101
102 /* test generated dealloc functions for generated types */
103 static void test_dealloc_types(void)
104 {
105 UserDefOne *ud1test, *ud1a, *ud1b;
106 UserDefOneList *ud1list;
107
108 ud1test = g_malloc0(sizeof(UserDefOne));
109 ud1test->integer = 42;
110 ud1test->string = g_strdup("hi there 42");
111
112 qapi_free_UserDefOne(ud1test);
113
114 ud1a = g_malloc0(sizeof(UserDefOne));
115 ud1a->integer = 43;
116 ud1a->string = g_strdup("hi there 43");
117
118 ud1b = g_malloc0(sizeof(UserDefOne));
119 ud1b->integer = 44;
120 ud1b->string = g_strdup("hi there 44");
121
122 ud1list = g_malloc0(sizeof(UserDefOneList));
123 ud1list->value = ud1a;
124 ud1list->next = g_malloc0(sizeof(UserDefOneList));
125 ud1list->next->value = ud1b;
126
127 qapi_free_UserDefOneList(ud1list);
128 }
129
130 /* test generated deallocation on an object whose construction was prematurely
131 * terminated due to an error */
132 static void test_dealloc_partial(void)
133 {
134 static const char text[] = "don't leak me";
135
136 UserDefTwo *ud2 = NULL;
137 Error *err = NULL;
138
139 /* create partial object */
140 {
141 QDict *ud2_dict;
142 QmpInputVisitor *qiv;
143
144 ud2_dict = qdict_new();
145 qdict_put_obj(ud2_dict, "string", QOBJECT(qstring_from_str(text)));
146
147 qiv = qmp_input_visitor_new(QOBJECT(ud2_dict));
148 visit_type_UserDefTwo(qmp_input_get_visitor(qiv), &ud2, NULL, &err);
149 qmp_input_visitor_cleanup(qiv);
150 QDECREF(ud2_dict);
151 }
152
153 /* verify partial success */
154 assert(ud2 != NULL);
155 assert(ud2->string != NULL);
156 assert(strcmp(ud2->string, text) == 0);
157 assert(ud2->dict.dict.userdef == NULL);
158
159 /* confirm & release construction error */
160 assert(err != NULL);
161 error_free(err);
162
163 /* tear down partial object */
164 qapi_free_UserDefTwo(ud2);
165 }
166
167
168 int main(int argc, char **argv)
169 {
170 g_test_init(&argc, &argv, NULL);
171
172 g_test_add_func("/0.15/dispatch_cmd", test_dispatch_cmd);
173 g_test_add_func("/0.15/dispatch_cmd_error", test_dispatch_cmd_error);
174 g_test_add_func("/0.15/dispatch_cmd_io", test_dispatch_cmd_io);
175 g_test_add_func("/0.15/dealloc_types", test_dealloc_types);
176 g_test_add_func("/0.15/dealloc_partial", test_dealloc_partial);
177
178 module_call_init(MODULE_INIT_QAPI);
179 g_test_run();
180
181 return 0;
182 }