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