]> git.ipfire.org Git - thirdparty/qemu.git/blob - tests/test-qmp-cmds.c
hw/rdma: Utilize ibv_reg_mr_iova for memory registration
[thirdparty/qemu.git] / tests / test-qmp-cmds.c
1 #include "qemu/osdep.h"
2 #include "qapi/qmp/qdict.h"
3 #include "qapi/qmp/qnum.h"
4 #include "qapi/qmp/qstring.h"
5 #include "qapi/error.h"
6 #include "qapi/qobject-input-visitor.h"
7 #include "tests/test-qapi-types.h"
8 #include "tests/test-qapi-visit.h"
9 #include "test-qapi-commands.h"
10
11 static QmpCommandList qmp_commands;
12
13 #if defined(TEST_IF_STRUCT) && defined(TEST_IF_CMD)
14 UserDefThree *qmp_TestIfCmd(TestIfStruct *foo, Error **errp)
15 {
16 return NULL;
17 }
18 #endif
19
20 UserDefThree *qmp_TestCmdReturnDefThree(Error **errp)
21 {
22 return NULL;
23 }
24
25 void qmp_user_def_cmd(Error **errp)
26 {
27 }
28
29 void qmp_test_flags_command(Error **errp)
30 {
31 }
32
33 void qmp_cmd_success_response(Error **errp)
34 {
35 }
36
37 Empty2 *qmp_user_def_cmd0(Error **errp)
38 {
39 return g_new0(Empty2, 1);
40 }
41
42 void qmp_user_def_cmd1(UserDefOne * ud1, Error **errp)
43 {
44 }
45
46 void qmp_test_features(FeatureStruct0 *fs0, FeatureStruct1 *fs1,
47 FeatureStruct2 *fs2, FeatureStruct3 *fs3,
48 FeatureStruct4 *fs4, CondFeatureStruct1 *cfs1,
49 CondFeatureStruct2 *cfs2, CondFeatureStruct3 *cfs3,
50 Error **errp)
51 {
52 }
53
54 void qmp_test_command_features0(Error **errp)
55 {
56 }
57
58 void qmp_test_command_features1(Error **errp)
59 {
60 }
61
62 void qmp_test_command_features3(Error **errp)
63 {
64 }
65
66 void qmp_test_command_cond_features1(Error **errp)
67 {
68 }
69
70 void qmp_test_command_cond_features2(Error **errp)
71 {
72 }
73
74 void qmp_test_command_cond_features3(Error **errp)
75 {
76 }
77
78 UserDefTwo *qmp_user_def_cmd2(UserDefOne *ud1a,
79 bool has_udb1, UserDefOne *ud1b,
80 Error **errp)
81 {
82 UserDefTwo *ret;
83 UserDefOne *ud1c = g_malloc0(sizeof(UserDefOne));
84 UserDefOne *ud1d = g_malloc0(sizeof(UserDefOne));
85
86 ud1c->string = strdup(ud1a->string);
87 ud1c->integer = ud1a->integer;
88 ud1d->string = strdup(has_udb1 ? ud1b->string : "blah0");
89 ud1d->integer = has_udb1 ? ud1b->integer : 0;
90
91 ret = g_new0(UserDefTwo, 1);
92 ret->string0 = strdup("blah1");
93 ret->dict1 = g_new0(UserDefTwoDict, 1);
94 ret->dict1->string1 = strdup("blah2");
95 ret->dict1->dict2 = g_new0(UserDefTwoDictDict, 1);
96 ret->dict1->dict2->userdef = ud1c;
97 ret->dict1->dict2->string = strdup("blah3");
98 ret->dict1->dict3 = g_new0(UserDefTwoDictDict, 1);
99 ret->dict1->has_dict3 = true;
100 ret->dict1->dict3->userdef = ud1d;
101 ret->dict1->dict3->string = strdup("blah4");
102
103 return ret;
104 }
105
106 int64_t qmp_guest_get_time(int64_t a, bool has_b, int64_t b, Error **errp)
107 {
108 return a + (has_b ? b : 0);
109 }
110
111 QObject *qmp_guest_sync(QObject *arg, Error **errp)
112 {
113 return arg;
114 }
115
116 void qmp_boxed_struct(UserDefZero *arg, Error **errp)
117 {
118 }
119
120 void qmp_boxed_union(UserDefListUnion *arg, Error **errp)
121 {
122 }
123
124 void qmp_boxed_empty(Empty1 *arg, Error **errp)
125 {
126 }
127
128 __org_qemu_x_Union1 *qmp___org_qemu_x_command(__org_qemu_x_EnumList *a,
129 __org_qemu_x_StructList *b,
130 __org_qemu_x_Union2 *c,
131 __org_qemu_x_Alt *d,
132 Error **errp)
133 {
134 __org_qemu_x_Union1 *ret = g_new0(__org_qemu_x_Union1, 1);
135
136 ret->type = ORG_QEMU_X_UNION1_KIND___ORG_QEMU_X_BRANCH;
137 ret->u.__org_qemu_x_branch.data = strdup("blah1");
138
139 /* Also test that 'wchar-t' was munged to 'q_wchar_t' */
140 if (b && b->value && !b->value->has_q_wchar_t) {
141 b->value->q_wchar_t = 1;
142 }
143 return ret;
144 }
145
146
147 /* test commands with no input and no return value */
148 static void test_dispatch_cmd(void)
149 {
150 QDict *req = qdict_new();
151 QDict *resp;
152
153 qdict_put_str(req, "execute", "user_def_cmd");
154
155 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
156 assert(resp != NULL);
157 assert(!qdict_haskey(resp, "error"));
158
159 qobject_unref(resp);
160 qobject_unref(req);
161 }
162
163 static void test_dispatch_cmd_oob(void)
164 {
165 QDict *req = qdict_new();
166 QDict *resp;
167
168 qdict_put_str(req, "exec-oob", "test-flags-command");
169
170 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), true);
171 assert(resp != NULL);
172 assert(!qdict_haskey(resp, "error"));
173
174 qobject_unref(resp);
175 qobject_unref(req);
176 }
177
178 /* test commands that return an error due to invalid parameters */
179 static void test_dispatch_cmd_failure(void)
180 {
181 QDict *req = qdict_new();
182 QDict *args = qdict_new();
183 QDict *resp;
184
185 qdict_put_str(req, "execute", "user_def_cmd2");
186
187 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
188 assert(resp != NULL);
189 assert(qdict_haskey(resp, "error"));
190
191 qobject_unref(resp);
192 qobject_unref(req);
193
194 /* check that with extra arguments it throws an error */
195 req = qdict_new();
196 qdict_put_int(args, "a", 66);
197 qdict_put(req, "arguments", args);
198
199 qdict_put_str(req, "execute", "user_def_cmd");
200
201 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
202 assert(resp != NULL);
203 assert(qdict_haskey(resp, "error"));
204
205 qobject_unref(resp);
206 qobject_unref(req);
207 }
208
209 static void test_dispatch_cmd_success_response(void)
210 {
211 QDict *req = qdict_new();
212 QDict *resp;
213
214 qdict_put_str(req, "execute", "cmd-success-response");
215 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
216 g_assert_null(resp);
217 qobject_unref(req);
218 }
219
220 static QObject *test_qmp_dispatch(QDict *req)
221 {
222 QDict *resp;
223 QObject *ret;
224
225 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
226 assert(resp && !qdict_haskey(resp, "error"));
227 ret = qdict_get(resp, "return");
228 assert(ret);
229 qobject_ref(ret);
230 qobject_unref(resp);
231 return ret;
232 }
233
234 /* test commands that involve both input parameters and return values */
235 static void test_dispatch_cmd_io(void)
236 {
237 QDict *req = qdict_new();
238 QDict *args = qdict_new();
239 QDict *args3 = qdict_new();
240 QDict *ud1a = qdict_new();
241 QDict *ud1b = qdict_new();
242 QDict *ret, *ret_dict, *ret_dict_dict, *ret_dict_dict_userdef;
243 QDict *ret_dict_dict2, *ret_dict_dict2_userdef;
244 QNum *ret3;
245 int64_t val;
246
247 qdict_put_int(ud1a, "integer", 42);
248 qdict_put_str(ud1a, "string", "hello");
249 qdict_put_int(ud1b, "integer", 422);
250 qdict_put_str(ud1b, "string", "hello2");
251 qdict_put(args, "ud1a", ud1a);
252 qdict_put(args, "ud1b", ud1b);
253 qdict_put(req, "arguments", args);
254 qdict_put_str(req, "execute", "user_def_cmd2");
255
256 ret = qobject_to(QDict, test_qmp_dispatch(req));
257
258 assert(!strcmp(qdict_get_str(ret, "string0"), "blah1"));
259 ret_dict = qdict_get_qdict(ret, "dict1");
260 assert(!strcmp(qdict_get_str(ret_dict, "string1"), "blah2"));
261 ret_dict_dict = qdict_get_qdict(ret_dict, "dict2");
262 ret_dict_dict_userdef = qdict_get_qdict(ret_dict_dict, "userdef");
263 assert(qdict_get_int(ret_dict_dict_userdef, "integer") == 42);
264 assert(!strcmp(qdict_get_str(ret_dict_dict_userdef, "string"), "hello"));
265 assert(!strcmp(qdict_get_str(ret_dict_dict, "string"), "blah3"));
266 ret_dict_dict2 = qdict_get_qdict(ret_dict, "dict3");
267 ret_dict_dict2_userdef = qdict_get_qdict(ret_dict_dict2, "userdef");
268 assert(qdict_get_int(ret_dict_dict2_userdef, "integer") == 422);
269 assert(!strcmp(qdict_get_str(ret_dict_dict2_userdef, "string"), "hello2"));
270 assert(!strcmp(qdict_get_str(ret_dict_dict2, "string"), "blah4"));
271 qobject_unref(ret);
272
273 qdict_put_int(args3, "a", 66);
274 qdict_put(req, "arguments", args3);
275 qdict_put_str(req, "execute", "guest-get-time");
276
277 ret3 = qobject_to(QNum, test_qmp_dispatch(req));
278 g_assert(qnum_get_try_int(ret3, &val));
279 g_assert_cmpint(val, ==, 66);
280 qobject_unref(ret3);
281
282 qobject_unref(req);
283 }
284
285 /* test generated dealloc functions for generated types */
286 static void test_dealloc_types(void)
287 {
288 UserDefOne *ud1test, *ud1a, *ud1b;
289 UserDefOneList *ud1list;
290
291 ud1test = g_malloc0(sizeof(UserDefOne));
292 ud1test->integer = 42;
293 ud1test->string = g_strdup("hi there 42");
294
295 qapi_free_UserDefOne(ud1test);
296
297 ud1a = g_malloc0(sizeof(UserDefOne));
298 ud1a->integer = 43;
299 ud1a->string = g_strdup("hi there 43");
300
301 ud1b = g_malloc0(sizeof(UserDefOne));
302 ud1b->integer = 44;
303 ud1b->string = g_strdup("hi there 44");
304
305 ud1list = g_malloc0(sizeof(UserDefOneList));
306 ud1list->value = ud1a;
307 ud1list->next = g_malloc0(sizeof(UserDefOneList));
308 ud1list->next->value = ud1b;
309
310 qapi_free_UserDefOneList(ud1list);
311 }
312
313 /* test generated deallocation on an object whose construction was prematurely
314 * terminated due to an error */
315 static void test_dealloc_partial(void)
316 {
317 static const char text[] = "don't leak me";
318
319 UserDefTwo *ud2 = NULL;
320 Error *err = NULL;
321
322 /* create partial object */
323 {
324 QDict *ud2_dict;
325 Visitor *v;
326
327 ud2_dict = qdict_new();
328 qdict_put_str(ud2_dict, "string0", text);
329
330 v = qobject_input_visitor_new(QOBJECT(ud2_dict));
331 visit_type_UserDefTwo(v, NULL, &ud2, &err);
332 visit_free(v);
333 qobject_unref(ud2_dict);
334 }
335
336 /* verify that visit_type_XXX() cleans up properly on error */
337 error_free_or_abort(&err);
338 assert(!ud2);
339
340 /* Manually create a partial object, leaving ud2->dict1 at NULL */
341 ud2 = g_new0(UserDefTwo, 1);
342 ud2->string0 = g_strdup(text);
343
344 /* tear down partial object */
345 qapi_free_UserDefTwo(ud2);
346 }
347
348
349 int main(int argc, char **argv)
350 {
351 g_test_init(&argc, &argv, NULL);
352
353 g_test_add_func("/qmp/dispatch_cmd", test_dispatch_cmd);
354 g_test_add_func("/qmp/dispatch_cmd_oob", test_dispatch_cmd_oob);
355 g_test_add_func("/qmp/dispatch_cmd_failure", test_dispatch_cmd_failure);
356 g_test_add_func("/qmp/dispatch_cmd_io", test_dispatch_cmd_io);
357 g_test_add_func("/qmp/dispatch_cmd_success_response",
358 test_dispatch_cmd_success_response);
359 g_test_add_func("/qmp/dealloc_types", test_dealloc_types);
360 g_test_add_func("/qmp/dealloc_partial", test_dealloc_partial);
361
362 test_qmp_init_marshal(&qmp_commands);
363 g_test_run();
364
365 return 0;
366 }