target/arm: Conditionalize some asserts on aarch32 support
[qemu/ar7.git] / tests / test-qmp-cmds.c
blob4ab2b6e5cea9e3efce3e561380d7116b25d35466
1 #include "qemu/osdep.h"
2 #include "qemu-common.h"
3 #include "qapi/qmp/qdict.h"
4 #include "qapi/qmp/qnum.h"
5 #include "qapi/qmp/qstring.h"
6 #include "qapi/error.h"
7 #include "qemu/module.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"
13 static QmpCommandList qmp_commands;
15 #if defined(TEST_IF_STRUCT) && defined(TEST_IF_CMD)
16 UserDefThree *qmp_TestIfCmd(TestIfStruct *foo, Error **errp)
18 return NULL;
20 #endif
22 UserDefThree *qmp_TestCmdReturnDefThree(Error **errp)
24 return NULL;
27 void qmp_user_def_cmd(Error **errp)
31 void qmp_test_flags_command(Error **errp)
35 void qmp_cmd_success_response(Error **errp)
39 Empty2 *qmp_user_def_cmd0(Error **errp)
41 return g_new0(Empty2, 1);
44 void qmp_user_def_cmd1(UserDefOne * ud1, Error **errp)
48 UserDefTwo *qmp_user_def_cmd2(UserDefOne *ud1a,
49 bool has_udb1, UserDefOne *ud1b,
50 Error **errp)
52 UserDefTwo *ret;
53 UserDefOne *ud1c = g_malloc0(sizeof(UserDefOne));
54 UserDefOne *ud1d = g_malloc0(sizeof(UserDefOne));
56 ud1c->string = strdup(ud1a->string);
57 ud1c->integer = ud1a->integer;
58 ud1d->string = strdup(has_udb1 ? ud1b->string : "blah0");
59 ud1d->integer = has_udb1 ? ud1b->integer : 0;
61 ret = g_new0(UserDefTwo, 1);
62 ret->string0 = strdup("blah1");
63 ret->dict1 = g_new0(UserDefTwoDict, 1);
64 ret->dict1->string1 = strdup("blah2");
65 ret->dict1->dict2 = g_new0(UserDefTwoDictDict, 1);
66 ret->dict1->dict2->userdef = ud1c;
67 ret->dict1->dict2->string = strdup("blah3");
68 ret->dict1->dict3 = g_new0(UserDefTwoDictDict, 1);
69 ret->dict1->has_dict3 = true;
70 ret->dict1->dict3->userdef = ud1d;
71 ret->dict1->dict3->string = strdup("blah4");
73 return ret;
76 int64_t qmp_guest_get_time(int64_t a, bool has_b, int64_t b, Error **errp)
78 return a + (has_b ? b : 0);
81 QObject *qmp_guest_sync(QObject *arg, Error **errp)
83 return arg;
86 void qmp_boxed_struct(UserDefZero *arg, Error **errp)
90 void qmp_boxed_union(UserDefNativeListUnion *arg, Error **errp)
94 __org_qemu_x_Union1 *qmp___org_qemu_x_command(__org_qemu_x_EnumList *a,
95 __org_qemu_x_StructList *b,
96 __org_qemu_x_Union2 *c,
97 __org_qemu_x_Alt *d,
98 Error **errp)
100 __org_qemu_x_Union1 *ret = g_new0(__org_qemu_x_Union1, 1);
102 ret->type = ORG_QEMU_X_UNION1_KIND___ORG_QEMU_X_BRANCH;
103 ret->u.__org_qemu_x_branch.data = strdup("blah1");
105 /* Also test that 'wchar-t' was munged to 'q_wchar_t' */
106 if (b && b->value && !b->value->has_q_wchar_t) {
107 b->value->q_wchar_t = 1;
109 return ret;
113 /* test commands with no input and no return value */
114 static void test_dispatch_cmd(void)
116 QDict *req = qdict_new();
117 QDict *resp;
119 qdict_put_str(req, "execute", "user_def_cmd");
121 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
122 assert(resp != NULL);
123 assert(!qdict_haskey(resp, "error"));
125 qobject_unref(resp);
126 qobject_unref(req);
129 /* test commands that return an error due to invalid parameters */
130 static void test_dispatch_cmd_failure(void)
132 QDict *req = qdict_new();
133 QDict *args = qdict_new();
134 QDict *resp;
136 qdict_put_str(req, "execute", "user_def_cmd2");
138 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
139 assert(resp != NULL);
140 assert(qdict_haskey(resp, "error"));
142 qobject_unref(resp);
143 qobject_unref(req);
145 /* check that with extra arguments it throws an error */
146 req = qdict_new();
147 qdict_put_int(args, "a", 66);
148 qdict_put(req, "arguments", args);
150 qdict_put_str(req, "execute", "user_def_cmd");
152 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
153 assert(resp != NULL);
154 assert(qdict_haskey(resp, "error"));
156 qobject_unref(resp);
157 qobject_unref(req);
160 static void test_dispatch_cmd_success_response(void)
162 QDict *req = qdict_new();
163 QDict *resp;
165 qdict_put_str(req, "execute", "cmd-success-response");
166 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
167 g_assert_null(resp);
168 qobject_unref(req);
171 static QObject *test_qmp_dispatch(QDict *req)
173 QDict *resp;
174 QObject *ret;
176 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
177 assert(resp && !qdict_haskey(resp, "error"));
178 ret = qdict_get(resp, "return");
179 assert(ret);
180 qobject_ref(ret);
181 qobject_unref(resp);
182 return ret;
185 /* test commands that involve both input parameters and return values */
186 static void test_dispatch_cmd_io(void)
188 QDict *req = qdict_new();
189 QDict *args = qdict_new();
190 QDict *args3 = qdict_new();
191 QDict *ud1a = qdict_new();
192 QDict *ud1b = qdict_new();
193 QDict *ret, *ret_dict, *ret_dict_dict, *ret_dict_dict_userdef;
194 QDict *ret_dict_dict2, *ret_dict_dict2_userdef;
195 QNum *ret3;
196 int64_t val;
198 qdict_put_int(ud1a, "integer", 42);
199 qdict_put_str(ud1a, "string", "hello");
200 qdict_put_int(ud1b, "integer", 422);
201 qdict_put_str(ud1b, "string", "hello2");
202 qdict_put(args, "ud1a", ud1a);
203 qdict_put(args, "ud1b", ud1b);
204 qdict_put(req, "arguments", args);
205 qdict_put_str(req, "execute", "user_def_cmd2");
207 ret = qobject_to(QDict, test_qmp_dispatch(req));
209 assert(!strcmp(qdict_get_str(ret, "string0"), "blah1"));
210 ret_dict = qdict_get_qdict(ret, "dict1");
211 assert(!strcmp(qdict_get_str(ret_dict, "string1"), "blah2"));
212 ret_dict_dict = qdict_get_qdict(ret_dict, "dict2");
213 ret_dict_dict_userdef = qdict_get_qdict(ret_dict_dict, "userdef");
214 assert(qdict_get_int(ret_dict_dict_userdef, "integer") == 42);
215 assert(!strcmp(qdict_get_str(ret_dict_dict_userdef, "string"), "hello"));
216 assert(!strcmp(qdict_get_str(ret_dict_dict, "string"), "blah3"));
217 ret_dict_dict2 = qdict_get_qdict(ret_dict, "dict3");
218 ret_dict_dict2_userdef = qdict_get_qdict(ret_dict_dict2, "userdef");
219 assert(qdict_get_int(ret_dict_dict2_userdef, "integer") == 422);
220 assert(!strcmp(qdict_get_str(ret_dict_dict2_userdef, "string"), "hello2"));
221 assert(!strcmp(qdict_get_str(ret_dict_dict2, "string"), "blah4"));
222 qobject_unref(ret);
224 qdict_put_int(args3, "a", 66);
225 qdict_put(req, "arguments", args3);
226 qdict_put_str(req, "execute", "guest-get-time");
228 ret3 = qobject_to(QNum, test_qmp_dispatch(req));
229 g_assert(qnum_get_try_int(ret3, &val));
230 g_assert_cmpint(val, ==, 66);
231 qobject_unref(ret3);
233 qobject_unref(req);
236 /* test generated dealloc functions for generated types */
237 static void test_dealloc_types(void)
239 UserDefOne *ud1test, *ud1a, *ud1b;
240 UserDefOneList *ud1list;
242 ud1test = g_malloc0(sizeof(UserDefOne));
243 ud1test->integer = 42;
244 ud1test->string = g_strdup("hi there 42");
246 qapi_free_UserDefOne(ud1test);
248 ud1a = g_malloc0(sizeof(UserDefOne));
249 ud1a->integer = 43;
250 ud1a->string = g_strdup("hi there 43");
252 ud1b = g_malloc0(sizeof(UserDefOne));
253 ud1b->integer = 44;
254 ud1b->string = g_strdup("hi there 44");
256 ud1list = g_malloc0(sizeof(UserDefOneList));
257 ud1list->value = ud1a;
258 ud1list->next = g_malloc0(sizeof(UserDefOneList));
259 ud1list->next->value = ud1b;
261 qapi_free_UserDefOneList(ud1list);
264 /* test generated deallocation on an object whose construction was prematurely
265 * terminated due to an error */
266 static void test_dealloc_partial(void)
268 static const char text[] = "don't leak me";
270 UserDefTwo *ud2 = NULL;
271 Error *err = NULL;
273 /* create partial object */
275 QDict *ud2_dict;
276 Visitor *v;
278 ud2_dict = qdict_new();
279 qdict_put_str(ud2_dict, "string0", text);
281 v = qobject_input_visitor_new(QOBJECT(ud2_dict));
282 visit_type_UserDefTwo(v, NULL, &ud2, &err);
283 visit_free(v);
284 qobject_unref(ud2_dict);
287 /* verify that visit_type_XXX() cleans up properly on error */
288 error_free_or_abort(&err);
289 assert(!ud2);
291 /* Manually create a partial object, leaving ud2->dict1 at NULL */
292 ud2 = g_new0(UserDefTwo, 1);
293 ud2->string0 = g_strdup(text);
295 /* tear down partial object */
296 qapi_free_UserDefTwo(ud2);
300 int main(int argc, char **argv)
302 g_test_init(&argc, &argv, NULL);
304 g_test_add_func("/qmp/dispatch_cmd", test_dispatch_cmd);
305 g_test_add_func("/qmp/dispatch_cmd_failure", test_dispatch_cmd_failure);
306 g_test_add_func("/qmp/dispatch_cmd_io", test_dispatch_cmd_io);
307 g_test_add_func("/qmp/dispatch_cmd_success_response",
308 test_dispatch_cmd_success_response);
309 g_test_add_func("/qmp/dealloc_types", test_dealloc_types);
310 g_test_add_func("/qmp/dealloc_partial", test_dealloc_partial);
312 test_qmp_init_marshal(&qmp_commands);
313 g_test_run();
315 return 0;