tests/boot_linux_console: Fetch assets from Debian snapshot archives
[qemu/ar7.git] / tests / test-qmp-cmds.c
blob27b0afe55a4dcc0f86bab545fc87d43fba742857
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"
11 static QmpCommandList qmp_commands;
13 #if defined(TEST_IF_STRUCT) && defined(TEST_IF_CMD)
14 UserDefThree *qmp_TestIfCmd(TestIfStruct *foo, Error **errp)
16 return NULL;
18 #endif
20 UserDefThree *qmp_TestCmdReturnDefThree(Error **errp)
22 return NULL;
25 void qmp_user_def_cmd(Error **errp)
29 void qmp_test_flags_command(Error **errp)
33 void qmp_cmd_success_response(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 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)
54 void qmp_test_command_features0(Error **errp)
58 void qmp_test_command_features1(Error **errp)
62 void qmp_test_command_features3(Error **errp)
66 void qmp_test_command_cond_features1(Error **errp)
70 void qmp_test_command_cond_features2(Error **errp)
74 void qmp_test_command_cond_features3(Error **errp)
78 UserDefTwo *qmp_user_def_cmd2(UserDefOne *ud1a,
79 bool has_udb1, UserDefOne *ud1b,
80 Error **errp)
82 UserDefTwo *ret;
83 UserDefOne *ud1c = g_malloc0(sizeof(UserDefOne));
84 UserDefOne *ud1d = g_malloc0(sizeof(UserDefOne));
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;
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");
103 return ret;
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)
113 return arg;
116 void qmp_boxed_struct(UserDefZero *arg, Error **errp)
120 void qmp_boxed_union(UserDefListUnion *arg, Error **errp)
124 void qmp_boxed_empty(Empty1 *arg, Error **errp)
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)
134 __org_qemu_x_Union1 *ret = g_new0(__org_qemu_x_Union1, 1);
136 ret->type = ORG_QEMU_X_UNION1_KIND___ORG_QEMU_X_BRANCH;
137 ret->u.__org_qemu_x_branch.data = strdup("blah1");
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;
143 return ret;
147 /* test commands with no input and no return value */
148 static void test_dispatch_cmd(void)
150 QDict *req = qdict_new();
151 QDict *resp;
153 qdict_put_str(req, "execute", "user_def_cmd");
155 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
156 assert(resp != NULL);
157 assert(!qdict_haskey(resp, "error"));
159 qobject_unref(resp);
160 qobject_unref(req);
163 static void test_dispatch_cmd_oob(void)
165 QDict *req = qdict_new();
166 QDict *resp;
168 qdict_put_str(req, "exec-oob", "test-flags-command");
170 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), true);
171 assert(resp != NULL);
172 assert(!qdict_haskey(resp, "error"));
174 qobject_unref(resp);
175 qobject_unref(req);
178 /* test commands that return an error due to invalid parameters */
179 static void test_dispatch_cmd_failure(void)
181 QDict *req = qdict_new();
182 QDict *args = qdict_new();
183 QDict *resp;
185 qdict_put_str(req, "execute", "user_def_cmd2");
187 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
188 assert(resp != NULL);
189 assert(qdict_haskey(resp, "error"));
191 qobject_unref(resp);
192 qobject_unref(req);
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);
199 qdict_put_str(req, "execute", "user_def_cmd");
201 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
202 assert(resp != NULL);
203 assert(qdict_haskey(resp, "error"));
205 qobject_unref(resp);
206 qobject_unref(req);
209 static void test_dispatch_cmd_success_response(void)
211 QDict *req = qdict_new();
212 QDict *resp;
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);
220 static QObject *test_qmp_dispatch(QDict *req)
222 QDict *resp;
223 QObject *ret;
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;
234 /* test commands that involve both input parameters and return values */
235 static void test_dispatch_cmd_io(void)
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;
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");
256 ret = qobject_to(QDict, test_qmp_dispatch(req));
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);
273 qdict_put_int(args3, "a", 66);
274 qdict_put(req, "arguments", args3);
275 qdict_put_str(req, "execute", "guest-get-time");
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);
282 qobject_unref(req);
285 /* test generated dealloc functions for generated types */
286 static void test_dealloc_types(void)
288 UserDefOne *ud1test, *ud1a, *ud1b;
289 UserDefOneList *ud1list;
291 ud1test = g_malloc0(sizeof(UserDefOne));
292 ud1test->integer = 42;
293 ud1test->string = g_strdup("hi there 42");
295 qapi_free_UserDefOne(ud1test);
297 ud1a = g_malloc0(sizeof(UserDefOne));
298 ud1a->integer = 43;
299 ud1a->string = g_strdup("hi there 43");
301 ud1b = g_malloc0(sizeof(UserDefOne));
302 ud1b->integer = 44;
303 ud1b->string = g_strdup("hi there 44");
305 ud1list = g_malloc0(sizeof(UserDefOneList));
306 ud1list->value = ud1a;
307 ud1list->next = g_malloc0(sizeof(UserDefOneList));
308 ud1list->next->value = ud1b;
310 qapi_free_UserDefOneList(ud1list);
313 /* test generated deallocation on an object whose construction was prematurely
314 * terminated due to an error */
315 static void test_dealloc_partial(void)
317 static const char text[] = "don't leak me";
319 UserDefTwo *ud2 = NULL;
320 Error *err = NULL;
322 /* create partial object */
324 QDict *ud2_dict;
325 Visitor *v;
327 ud2_dict = qdict_new();
328 qdict_put_str(ud2_dict, "string0", text);
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);
336 /* verify that visit_type_XXX() cleans up properly on error */
337 error_free_or_abort(&err);
338 assert(!ud2);
340 /* Manually create a partial object, leaving ud2->dict1 at NULL */
341 ud2 = g_new0(UserDefTwo, 1);
342 ud2->string0 = g_strdup(text);
344 /* tear down partial object */
345 qapi_free_UserDefTwo(ud2);
349 int main(int argc, char **argv)
351 g_test_init(&argc, &argv, NULL);
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);
362 test_qmp_init_marshal(&qmp_commands);
363 g_test_run();
365 return 0;