1 #include "qemu/osdep.h"
2 #include "qapi/compat-policy.h"
3 #include "qapi/qmp/qdict.h"
4 #include "qapi/qmp/qjson.h"
5 #include "qapi/qmp/qnum.h"
6 #include "qapi/qmp/qstring.h"
7 #include "qapi/error.h"
8 #include "qapi/qobject-input-visitor.h"
9 #include "tests/test-qapi-types.h"
10 #include "tests/test-qapi-visit.h"
11 #include "test-qapi-commands.h"
12 #include "test-qapi-init-commands.h"
14 static QmpCommandList qmp_commands;
16 UserDefThree *qmp_test_cmd_return_def_three(Error **errp)
21 void qmp_user_def_cmd(Error **errp)
25 void qmp_test_flags_command(Error **errp)
29 void qmp_cmd_success_response(Error **errp)
33 void qmp_coroutine_cmd(Error **errp)
37 Empty2 *qmp_user_def_cmd0(Error **errp)
39 return g_new0(Empty2, 1);
42 void qmp_user_def_cmd1(UserDefOne * ud1, Error **errp)
46 FeatureStruct1 *qmp_test_features0(FeatureStruct0 *fs0,
51 CondFeatureStruct1 *cfs1,
52 CondFeatureStruct2 *cfs2,
53 CondFeatureStruct3 *cfs3,
54 CondFeatureStruct4 *cfs4,
57 return g_new0(FeatureStruct1, 1);
60 void qmp_test_command_features1(Error **errp)
64 void qmp_test_command_features3(Error **errp)
68 void qmp_test_command_cond_features1(Error **errp)
72 void qmp_test_command_cond_features2(Error **errp)
76 void qmp_test_command_cond_features3(Error **errp)
80 UserDefTwo *qmp_user_def_cmd2(UserDefOne *ud1a, UserDefOne *ud1b,
84 UserDefOne *ud1c = g_new0(UserDefOne, 1);
85 UserDefOne *ud1d = g_new0(UserDefOne, 1);
87 ud1c->string = strdup(ud1a->string);
88 ud1c->integer = ud1a->integer;
89 ud1d->string = strdup(ud1b ? ud1b->string : "blah0");
90 ud1d->integer = ud1b ? ud1b->integer : 0;
92 ret = g_new0(UserDefTwo, 1);
93 ret->string0 = strdup("blah1");
94 ret->dict1 = g_new0(UserDefTwoDict, 1);
95 ret->dict1->string1 = strdup("blah2");
96 ret->dict1->dict2 = g_new0(UserDefTwoDictDict, 1);
97 ret->dict1->dict2->userdef = ud1c;
98 ret->dict1->dict2->string = strdup("blah3");
99 ret->dict1->dict3 = g_new0(UserDefTwoDictDict, 1);
100 ret->dict1->dict3->userdef = ud1d;
101 ret->dict1->dict3->string = strdup("blah4");
106 int64_t qmp_guest_get_time(int64_t a, bool has_b, int64_t b, Error **errp)
108 return a + (has_b ? b : 0);
111 QObject *qmp_guest_sync(QObject *arg, Error **errp)
116 void qmp_boxed_struct(UserDefZero *arg, Error **errp)
120 void qmp_boxed_union(UserDefFlatUnion *arg, Error **errp)
124 void qmp_boxed_empty(Empty1 *arg, Error **errp)
128 void qmp___org_qemu_x_command(__org_qemu_x_EnumList *a,
129 __org_qemu_x_StructList *b,
130 __org_qemu_x_Union *c,
134 /* Also test that 'wchar-t' was munged to 'q_wchar_t' */
135 if (b && b->value && !b->value->has_q_wchar_t) {
136 b->value->q_wchar_t = 1;
142 static QObject *do_qmp_dispatch(bool allow_oob, const char *template, ...)
148 va_start(ap, template);
149 req = qdict_from_vjsonf_nofail(template, ap);
152 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), allow_oob, NULL);
154 ret = qdict_get(resp, "return");
156 g_assert(qdict_size(resp) == 1);
165 static void do_qmp_dispatch_error(bool allow_oob, ErrorClass cls,
166 const char *template, ...)
172 va_start(ap, template);
173 req = qdict_from_vjsonf_nofail(template, ap);
176 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), allow_oob, NULL);
178 error = qdict_get_qdict(resp, "error");
180 g_assert_cmpstr(qdict_get_try_str(error, "class"),
181 ==, QapiErrorClass_str(cls));
182 g_assert(qdict_get_try_str(error, "desc"));
183 g_assert(qdict_size(error) == 2);
184 g_assert(qdict_size(resp) == 1);
190 /* test commands with no input and no return value */
191 static void test_dispatch_cmd(void)
195 ret = qobject_to(QDict,
196 do_qmp_dispatch(false,
197 "{ 'execute': 'user-def-cmd' }"));
198 assert(ret && qdict_size(ret) == 0);
202 static void test_dispatch_cmd_oob(void)
206 ret = qobject_to(QDict,
207 do_qmp_dispatch(true,
208 "{ 'exec-oob': 'test-flags-command' }"));
209 assert(ret && qdict_size(ret) == 0);
213 /* test commands that return an error due to invalid parameters */
214 static void test_dispatch_cmd_failure(void)
216 /* missing arguments */
217 do_qmp_dispatch_error(false, ERROR_CLASS_GENERIC_ERROR,
218 "{ 'execute': 'user-def-cmd2' }");
220 /* extra arguments */
221 do_qmp_dispatch_error(false, ERROR_CLASS_GENERIC_ERROR,
222 "{ 'execute': 'user-def-cmd',"
223 " 'arguments': { 'a': 66 } }");
226 static void test_dispatch_cmd_success_response(void)
228 QDict *req = qdict_new();
231 qdict_put_str(req, "execute", "cmd-success-response");
232 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false, NULL);
237 /* test commands that involve both input parameters and return values */
238 static void test_dispatch_cmd_io(void)
240 QDict *ret, *ret_dict, *ret_dict_dict, *ret_dict_dict_userdef;
241 QDict *ret_dict_dict2, *ret_dict_dict2_userdef;
245 ret = qobject_to(QDict, do_qmp_dispatch(false,
246 "{ 'execute': 'user-def-cmd2', 'arguments': {"
247 " 'ud1a': { 'integer': 42, 'string': 'hello' },"
248 " 'ud1b': { 'integer': 422, 'string': 'hello2' } } }"));
250 assert(!strcmp(qdict_get_str(ret, "string0"), "blah1"));
251 ret_dict = qdict_get_qdict(ret, "dict1");
252 assert(!strcmp(qdict_get_str(ret_dict, "string1"), "blah2"));
253 ret_dict_dict = qdict_get_qdict(ret_dict, "dict2");
254 ret_dict_dict_userdef = qdict_get_qdict(ret_dict_dict, "userdef");
255 assert(qdict_get_int(ret_dict_dict_userdef, "integer") == 42);
256 assert(!strcmp(qdict_get_str(ret_dict_dict_userdef, "string"), "hello"));
257 assert(!strcmp(qdict_get_str(ret_dict_dict, "string"), "blah3"));
258 ret_dict_dict2 = qdict_get_qdict(ret_dict, "dict3");
259 ret_dict_dict2_userdef = qdict_get_qdict(ret_dict_dict2, "userdef");
260 assert(qdict_get_int(ret_dict_dict2_userdef, "integer") == 422);
261 assert(!strcmp(qdict_get_str(ret_dict_dict2_userdef, "string"), "hello2"));
262 assert(!strcmp(qdict_get_str(ret_dict_dict2, "string"), "blah4"));
265 ret3 = qobject_to(QNum, do_qmp_dispatch(false,
266 "{ 'execute': 'guest-get-time', 'arguments': { 'a': 66 } }"));
267 g_assert(qnum_get_try_int(ret3, &val));
268 g_assert_cmpint(val, ==, 66);
272 static void test_dispatch_cmd_deprecated(void)
274 #define cmd "{ 'execute': 'test-command-features1' }"
277 memset(&compat_policy, 0, sizeof(compat_policy));
280 ret = qobject_to(QDict, do_qmp_dispatch(false, cmd));
281 assert(ret && qdict_size(ret) == 0);
284 compat_policy.has_deprecated_input = true;
285 compat_policy.deprecated_input = COMPAT_POLICY_INPUT_ACCEPT;
286 ret = qobject_to(QDict, do_qmp_dispatch(false, cmd));
287 assert(ret && qdict_size(ret) == 0);
290 compat_policy.deprecated_input = COMPAT_POLICY_INPUT_REJECT;
291 do_qmp_dispatch_error(false, ERROR_CLASS_COMMAND_NOT_FOUND, cmd);
295 static void test_dispatch_cmd_arg_deprecated(void)
297 #define cmd "{ 'execute': 'test-features0'," \
298 " 'arguments': { 'fs1': { 'foo': 42 } } }"
301 memset(&compat_policy, 0, sizeof(compat_policy));
304 ret = qobject_to(QDict, do_qmp_dispatch(false, cmd));
305 assert(ret && qdict_size(ret) == 1);
308 compat_policy.has_deprecated_input = true;
309 compat_policy.deprecated_input = COMPAT_POLICY_INPUT_ACCEPT;
310 ret = qobject_to(QDict, do_qmp_dispatch(false, cmd));
311 assert(ret && qdict_size(ret) == 1);
314 compat_policy.deprecated_input = COMPAT_POLICY_INPUT_REJECT;
315 do_qmp_dispatch_error(false, ERROR_CLASS_GENERIC_ERROR, cmd);
319 static void test_dispatch_cmd_ret_deprecated(void)
321 #define cmd "{ 'execute': 'test-features0' }"
324 memset(&compat_policy, 0, sizeof(compat_policy));
327 ret = qobject_to(QDict, do_qmp_dispatch(false, cmd));
328 assert(ret && qdict_size(ret) == 1);
331 compat_policy.has_deprecated_output = true;
332 compat_policy.deprecated_output = COMPAT_POLICY_OUTPUT_ACCEPT;
333 ret = qobject_to(QDict, do_qmp_dispatch(false, cmd));
334 assert(ret && qdict_size(ret) == 1);
337 compat_policy.deprecated_output = COMPAT_POLICY_OUTPUT_HIDE;
338 ret = qobject_to(QDict, do_qmp_dispatch(false, cmd));
339 assert(ret && qdict_size(ret) == 0);
344 /* test generated dealloc functions for generated types */
345 static void test_dealloc_types(void)
347 UserDefOne *ud1test, *ud1a, *ud1b;
348 UserDefOneList *ud1list;
350 ud1test = g_new0(UserDefOne, 1);
351 ud1test->integer = 42;
352 ud1test->string = g_strdup("hi there 42");
354 qapi_free_UserDefOne(ud1test);
356 ud1a = g_new0(UserDefOne, 1);
358 ud1a->string = g_strdup("hi there 43");
360 ud1b = g_new0(UserDefOne, 1);
362 ud1b->string = g_strdup("hi there 44");
364 ud1list = g_new0(UserDefOneList, 1);
365 ud1list->value = ud1a;
366 ud1list->next = g_new0(UserDefOneList, 1);
367 ud1list->next->value = ud1b;
369 qapi_free_UserDefOneList(ud1list);
372 /* test generated deallocation on an object whose construction was prematurely
373 * terminated due to an error */
374 static void test_dealloc_partial(void)
376 static const char text[] = "don't leak me";
378 UserDefTwo *ud2 = NULL;
381 /* create partial object */
386 ud2_dict = qdict_new();
387 qdict_put_str(ud2_dict, "string0", text);
389 v = qobject_input_visitor_new(QOBJECT(ud2_dict));
390 visit_type_UserDefTwo(v, NULL, &ud2, &err);
392 qobject_unref(ud2_dict);
395 /* verify that visit_type_XXX() cleans up properly on error */
396 error_free_or_abort(&err);
399 /* Manually create a partial object, leaving ud2->dict1 at NULL */
400 ud2 = g_new0(UserDefTwo, 1);
401 ud2->string0 = g_strdup(text);
403 /* tear down partial object */
404 qapi_free_UserDefTwo(ud2);
408 int main(int argc, char **argv)
410 g_test_init(&argc, &argv, NULL);
412 g_test_add_func("/qmp/dispatch_cmd", test_dispatch_cmd);
413 g_test_add_func("/qmp/dispatch_cmd_oob", test_dispatch_cmd_oob);
414 g_test_add_func("/qmp/dispatch_cmd_failure", test_dispatch_cmd_failure);
415 g_test_add_func("/qmp/dispatch_cmd_io", test_dispatch_cmd_io);
416 g_test_add_func("/qmp/dispatch_cmd_success_response",
417 test_dispatch_cmd_success_response);
418 g_test_add_func("/qmp/dispatch_cmd_deprecated",
419 test_dispatch_cmd_deprecated);
420 g_test_add_func("/qmp/dispatch_cmd_arg_deprecated",
421 test_dispatch_cmd_arg_deprecated);
422 g_test_add_func("/qmp/dispatch_cmd_ret_deprecated",
423 test_dispatch_cmd_ret_deprecated);
424 g_test_add_func("/qmp/dealloc_types", test_dealloc_types);
425 g_test_add_func("/qmp/dealloc_partial", test_dealloc_partial);
427 test_qmp_init_marshal(&qmp_commands);