quorum: Inline quorum_aio_cb()
[qemu.git] / tests / test-qmp-commands.c
1 #include "qemu/osdep.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/qobject-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 Empty2 *qmp_user_def_cmd0(Error **errp)
16 {
17 return g_new0(Empty2, 1);
18 }
19
20 void qmp_user_def_cmd1(UserDefOne * ud1, Error **errp)
21 {
22 }
23
24 UserDefTwo *qmp_user_def_cmd2(UserDefOne *ud1a,
25 bool has_udb1, UserDefOne *ud1b,
26 Error **errp)
27 {
28 UserDefTwo *ret;
29 UserDefOne *ud1c = g_malloc0(sizeof(UserDefOne));
30 UserDefOne *ud1d = g_malloc0(sizeof(UserDefOne));
31
32 ud1c->string = strdup(ud1a->string);
33 ud1c->integer = ud1a->integer;
34 ud1d->string = strdup(has_udb1 ? ud1b->string : "blah0");
35 ud1d->integer = has_udb1 ? ud1b->integer : 0;
36
37 ret = g_new0(UserDefTwo, 1);
38 ret->string0 = strdup("blah1");
39 ret->dict1 = g_new0(UserDefTwoDict, 1);
40 ret->dict1->string1 = strdup("blah2");
41 ret->dict1->dict2 = g_new0(UserDefTwoDictDict, 1);
42 ret->dict1->dict2->userdef = ud1c;
43 ret->dict1->dict2->string = strdup("blah3");
44 ret->dict1->dict3 = g_new0(UserDefTwoDictDict, 1);
45 ret->dict1->has_dict3 = true;
46 ret->dict1->dict3->userdef = ud1d;
47 ret->dict1->dict3->string = strdup("blah4");
48
49 return ret;
50 }
51
52 int64_t qmp_guest_get_time(int64_t a, bool has_b, int64_t b, Error **errp)
53 {
54 return a + (has_b ? b : 0);
55 }
56
57 QObject *qmp_guest_sync(QObject *arg, Error **errp)
58 {
59 return arg;
60 }
61
62 void qmp_boxed_struct(UserDefZero *arg, Error **errp)
63 {
64 }
65
66 void qmp_boxed_union(UserDefNativeListUnion *arg, Error **errp)
67 {
68 }
69
70 __org_qemu_x_Union1 *qmp___org_qemu_x_command(__org_qemu_x_EnumList *a,
71 __org_qemu_x_StructList *b,
72 __org_qemu_x_Union2 *c,
73 __org_qemu_x_Alt *d,
74 Error **errp)
75 {
76 __org_qemu_x_Union1 *ret = g_new0(__org_qemu_x_Union1, 1);
77
78 ret->type = ORG_QEMU_X_UNION1_KIND___ORG_QEMU_X_BRANCH;
79 ret->u.__org_qemu_x_branch.data = strdup("blah1");
80
81 /* Also test that 'wchar-t' was munged to 'q_wchar_t' */
82 if (b && b->value && !b->value->has_q_wchar_t) {
83 b->value->q_wchar_t = 1;
84 }
85 return ret;
86 }
87
88
89 /* test commands with no input and no return value */
90 static void test_dispatch_cmd(void)
91 {
92 QDict *req = qdict_new();
93 QObject *resp;
94
95 qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd")));
96
97 resp = qmp_dispatch(QOBJECT(req));
98 assert(resp != NULL);
99 assert(!qdict_haskey(qobject_to_qdict(resp), "error"));
100
101 qobject_decref(resp);
102 QDECREF(req);
103 }
104
105 /* test commands that return an error due to invalid parameters */
106 static void test_dispatch_cmd_failure(void)
107 {
108 QDict *req = qdict_new();
109 QDict *args = qdict_new();
110 QObject *resp;
111
112 qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd2")));
113
114 resp = qmp_dispatch(QOBJECT(req));
115 assert(resp != NULL);
116 assert(qdict_haskey(qobject_to_qdict(resp), "error"));
117
118 qobject_decref(resp);
119 QDECREF(req);
120
121 /* check that with extra arguments it throws an error */
122 req = qdict_new();
123 qdict_put(args, "a", qint_from_int(66));
124 qdict_put(req, "arguments", args);
125
126 qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd")));
127
128 resp = qmp_dispatch(QOBJECT(req));
129 assert(resp != NULL);
130 assert(qdict_haskey(qobject_to_qdict(resp), "error"));
131
132 qobject_decref(resp);
133 QDECREF(req);
134 }
135
136 static QObject *test_qmp_dispatch(QDict *req)
137 {
138 QObject *resp_obj;
139 QDict *resp;
140 QObject *ret;
141
142 resp_obj = qmp_dispatch(QOBJECT(req));
143 assert(resp_obj);
144 resp = qobject_to_qdict(resp_obj);
145 assert(resp && !qdict_haskey(resp, "error"));
146 ret = qdict_get(resp, "return");
147 assert(ret);
148 qobject_incref(ret);
149 qobject_decref(resp_obj);
150 return ret;
151 }
152
153 /* test commands that involve both input parameters and return values */
154 static void test_dispatch_cmd_io(void)
155 {
156 QDict *req = qdict_new();
157 QDict *args = qdict_new();
158 QDict *args3 = qdict_new();
159 QDict *ud1a = qdict_new();
160 QDict *ud1b = qdict_new();
161 QDict *ret, *ret_dict, *ret_dict_dict, *ret_dict_dict_userdef;
162 QDict *ret_dict_dict2, *ret_dict_dict2_userdef;
163 QInt *ret3;
164
165 qdict_put_obj(ud1a, "integer", QOBJECT(qint_from_int(42)));
166 qdict_put_obj(ud1a, "string", QOBJECT(qstring_from_str("hello")));
167 qdict_put_obj(ud1b, "integer", QOBJECT(qint_from_int(422)));
168 qdict_put_obj(ud1b, "string", QOBJECT(qstring_from_str("hello2")));
169 qdict_put_obj(args, "ud1a", QOBJECT(ud1a));
170 qdict_put_obj(args, "ud1b", QOBJECT(ud1b));
171 qdict_put_obj(req, "arguments", QOBJECT(args));
172 qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd2")));
173
174 ret = qobject_to_qdict(test_qmp_dispatch(req));
175
176 assert(!strcmp(qdict_get_str(ret, "string0"), "blah1"));
177 ret_dict = qdict_get_qdict(ret, "dict1");
178 assert(!strcmp(qdict_get_str(ret_dict, "string1"), "blah2"));
179 ret_dict_dict = qdict_get_qdict(ret_dict, "dict2");
180 ret_dict_dict_userdef = qdict_get_qdict(ret_dict_dict, "userdef");
181 assert(qdict_get_int(ret_dict_dict_userdef, "integer") == 42);
182 assert(!strcmp(qdict_get_str(ret_dict_dict_userdef, "string"), "hello"));
183 assert(!strcmp(qdict_get_str(ret_dict_dict, "string"), "blah3"));
184 ret_dict_dict2 = qdict_get_qdict(ret_dict, "dict3");
185 ret_dict_dict2_userdef = qdict_get_qdict(ret_dict_dict2, "userdef");
186 assert(qdict_get_int(ret_dict_dict2_userdef, "integer") == 422);
187 assert(!strcmp(qdict_get_str(ret_dict_dict2_userdef, "string"), "hello2"));
188 assert(!strcmp(qdict_get_str(ret_dict_dict2, "string"), "blah4"));
189 QDECREF(ret);
190
191 qdict_put(args3, "a", qint_from_int(66));
192 qdict_put(req, "arguments", args3);
193 qdict_put(req, "execute", qstring_from_str("guest-get-time"));
194
195 ret3 = qobject_to_qint(test_qmp_dispatch(req));
196 assert(qint_get_int(ret3) == 66);
197 QDECREF(ret3);
198
199 QDECREF(req);
200 }
201
202 /* test generated dealloc functions for generated types */
203 static void test_dealloc_types(void)
204 {
205 UserDefOne *ud1test, *ud1a, *ud1b;
206 UserDefOneList *ud1list;
207
208 ud1test = g_malloc0(sizeof(UserDefOne));
209 ud1test->integer = 42;
210 ud1test->string = g_strdup("hi there 42");
211
212 qapi_free_UserDefOne(ud1test);
213
214 ud1a = g_malloc0(sizeof(UserDefOne));
215 ud1a->integer = 43;
216 ud1a->string = g_strdup("hi there 43");
217
218 ud1b = g_malloc0(sizeof(UserDefOne));
219 ud1b->integer = 44;
220 ud1b->string = g_strdup("hi there 44");
221
222 ud1list = g_malloc0(sizeof(UserDefOneList));
223 ud1list->value = ud1a;
224 ud1list->next = g_malloc0(sizeof(UserDefOneList));
225 ud1list->next->value = ud1b;
226
227 qapi_free_UserDefOneList(ud1list);
228 }
229
230 /* test generated deallocation on an object whose construction was prematurely
231 * terminated due to an error */
232 static void test_dealloc_partial(void)
233 {
234 static const char text[] = "don't leak me";
235
236 UserDefTwo *ud2 = NULL;
237 Error *err = NULL;
238
239 /* create partial object */
240 {
241 QDict *ud2_dict;
242 Visitor *v;
243
244 ud2_dict = qdict_new();
245 qdict_put_obj(ud2_dict, "string0", QOBJECT(qstring_from_str(text)));
246
247 v = qobject_input_visitor_new(QOBJECT(ud2_dict), true);
248 visit_type_UserDefTwo(v, NULL, &ud2, &err);
249 visit_free(v);
250 QDECREF(ud2_dict);
251 }
252
253 /* verify that visit_type_XXX() cleans up properly on error */
254 error_free_or_abort(&err);
255 assert(!ud2);
256
257 /* Manually create a partial object, leaving ud2->dict1 at NULL */
258 ud2 = g_new0(UserDefTwo, 1);
259 ud2->string0 = g_strdup(text);
260
261 /* tear down partial object */
262 qapi_free_UserDefTwo(ud2);
263 }
264
265
266 int main(int argc, char **argv)
267 {
268 g_test_init(&argc, &argv, NULL);
269
270 g_test_add_func("/0.15/dispatch_cmd", test_dispatch_cmd);
271 g_test_add_func("/0.15/dispatch_cmd_failure", test_dispatch_cmd_failure);
272 g_test_add_func("/0.15/dispatch_cmd_io", test_dispatch_cmd_io);
273 g_test_add_func("/0.15/dealloc_types", test_dealloc_types);
274 g_test_add_func("/0.15/dealloc_partial", test_dealloc_partial);
275
276 module_call_init(MODULE_INIT_QAPI);
277 g_test_run();
278
279 return 0;
280 }