linux-user: fix use of SIGRTMIN
[qemu/ar7.git] / tests / test-qmp-cmds.c
blob79507d9e543dfde56312083500e755bcdf5a0fa1
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 #include "test-qapi-init-commands.h"
12 static QmpCommandList qmp_commands;
14 #if defined(TEST_IF_STRUCT) && defined(TEST_IF_CMD)
15 UserDefThree *qmp_TestIfCmd(TestIfStruct *foo, Error **errp)
17 return NULL;
19 #endif
21 UserDefThree *qmp_TestCmdReturnDefThree(Error **errp)
23 return NULL;
26 void qmp_user_def_cmd(Error **errp)
30 void qmp_test_flags_command(Error **errp)
34 void qmp_cmd_success_response(Error **errp)
38 Empty2 *qmp_user_def_cmd0(Error **errp)
40 return g_new0(Empty2, 1);
43 void qmp_user_def_cmd1(UserDefOne * ud1, Error **errp)
47 void qmp_test_features(FeatureStruct0 *fs0, FeatureStruct1 *fs1,
48 FeatureStruct2 *fs2, FeatureStruct3 *fs3,
49 FeatureStruct4 *fs4, CondFeatureStruct1 *cfs1,
50 CondFeatureStruct2 *cfs2, CondFeatureStruct3 *cfs3,
51 Error **errp)
55 void qmp_test_command_features0(Error **errp)
59 void qmp_test_command_features1(Error **errp)
63 void qmp_test_command_features3(Error **errp)
67 void qmp_test_command_cond_features1(Error **errp)
71 void qmp_test_command_cond_features2(Error **errp)
75 void qmp_test_command_cond_features3(Error **errp)
79 UserDefTwo *qmp_user_def_cmd2(UserDefOne *ud1a,
80 bool has_udb1, UserDefOne *ud1b,
81 Error **errp)
83 UserDefTwo *ret;
84 UserDefOne *ud1c = g_malloc0(sizeof(UserDefOne));
85 UserDefOne *ud1d = g_malloc0(sizeof(UserDefOne));
87 ud1c->string = strdup(ud1a->string);
88 ud1c->integer = ud1a->integer;
89 ud1d->string = strdup(has_udb1 ? ud1b->string : "blah0");
90 ud1d->integer = has_udb1 ? 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->has_dict3 = true;
101 ret->dict1->dict3->userdef = ud1d;
102 ret->dict1->dict3->string = strdup("blah4");
104 return ret;
107 int64_t qmp_guest_get_time(int64_t a, bool has_b, int64_t b, Error **errp)
109 return a + (has_b ? b : 0);
112 QObject *qmp_guest_sync(QObject *arg, Error **errp)
114 return arg;
117 void qmp_boxed_struct(UserDefZero *arg, Error **errp)
121 void qmp_boxed_union(UserDefListUnion *arg, Error **errp)
125 void qmp_boxed_empty(Empty1 *arg, Error **errp)
129 __org_qemu_x_Union1 *qmp___org_qemu_x_command(__org_qemu_x_EnumList *a,
130 __org_qemu_x_StructList *b,
131 __org_qemu_x_Union2 *c,
132 __org_qemu_x_Alt *d,
133 Error **errp)
135 __org_qemu_x_Union1 *ret = g_new0(__org_qemu_x_Union1, 1);
137 ret->type = ORG_QEMU_X_UNION1_KIND___ORG_QEMU_X_BRANCH;
138 ret->u.__org_qemu_x_branch.data = strdup("blah1");
140 /* Also test that 'wchar-t' was munged to 'q_wchar_t' */
141 if (b && b->value && !b->value->has_q_wchar_t) {
142 b->value->q_wchar_t = 1;
144 return ret;
148 /* test commands with no input and no return value */
149 static void test_dispatch_cmd(void)
151 QDict *req = qdict_new();
152 QDict *resp;
154 qdict_put_str(req, "execute", "user_def_cmd");
156 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
157 assert(resp != NULL);
158 assert(!qdict_haskey(resp, "error"));
160 qobject_unref(resp);
161 qobject_unref(req);
164 static void test_dispatch_cmd_oob(void)
166 QDict *req = qdict_new();
167 QDict *resp;
169 qdict_put_str(req, "exec-oob", "test-flags-command");
171 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), true);
172 assert(resp != NULL);
173 assert(!qdict_haskey(resp, "error"));
175 qobject_unref(resp);
176 qobject_unref(req);
179 /* test commands that return an error due to invalid parameters */
180 static void test_dispatch_cmd_failure(void)
182 QDict *req = qdict_new();
183 QDict *args = qdict_new();
184 QDict *resp;
186 qdict_put_str(req, "execute", "user_def_cmd2");
188 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
189 assert(resp != NULL);
190 assert(qdict_haskey(resp, "error"));
192 qobject_unref(resp);
193 qobject_unref(req);
195 /* check that with extra arguments it throws an error */
196 req = qdict_new();
197 qdict_put_int(args, "a", 66);
198 qdict_put(req, "arguments", args);
200 qdict_put_str(req, "execute", "user_def_cmd");
202 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
203 assert(resp != NULL);
204 assert(qdict_haskey(resp, "error"));
206 qobject_unref(resp);
207 qobject_unref(req);
210 static void test_dispatch_cmd_success_response(void)
212 QDict *req = qdict_new();
213 QDict *resp;
215 qdict_put_str(req, "execute", "cmd-success-response");
216 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
217 g_assert_null(resp);
218 qobject_unref(req);
221 static QObject *test_qmp_dispatch(QDict *req)
223 QDict *resp;
224 QObject *ret;
226 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
227 assert(resp && !qdict_haskey(resp, "error"));
228 ret = qdict_get(resp, "return");
229 assert(ret);
230 qobject_ref(ret);
231 qobject_unref(resp);
232 return ret;
235 /* test commands that involve both input parameters and return values */
236 static void test_dispatch_cmd_io(void)
238 QDict *req = qdict_new();
239 QDict *args = qdict_new();
240 QDict *args3 = qdict_new();
241 QDict *ud1a = qdict_new();
242 QDict *ud1b = qdict_new();
243 QDict *ret, *ret_dict, *ret_dict_dict, *ret_dict_dict_userdef;
244 QDict *ret_dict_dict2, *ret_dict_dict2_userdef;
245 QNum *ret3;
246 int64_t val;
248 qdict_put_int(ud1a, "integer", 42);
249 qdict_put_str(ud1a, "string", "hello");
250 qdict_put_int(ud1b, "integer", 422);
251 qdict_put_str(ud1b, "string", "hello2");
252 qdict_put(args, "ud1a", ud1a);
253 qdict_put(args, "ud1b", ud1b);
254 qdict_put(req, "arguments", args);
255 qdict_put_str(req, "execute", "user_def_cmd2");
257 ret = qobject_to(QDict, test_qmp_dispatch(req));
259 assert(!strcmp(qdict_get_str(ret, "string0"), "blah1"));
260 ret_dict = qdict_get_qdict(ret, "dict1");
261 assert(!strcmp(qdict_get_str(ret_dict, "string1"), "blah2"));
262 ret_dict_dict = qdict_get_qdict(ret_dict, "dict2");
263 ret_dict_dict_userdef = qdict_get_qdict(ret_dict_dict, "userdef");
264 assert(qdict_get_int(ret_dict_dict_userdef, "integer") == 42);
265 assert(!strcmp(qdict_get_str(ret_dict_dict_userdef, "string"), "hello"));
266 assert(!strcmp(qdict_get_str(ret_dict_dict, "string"), "blah3"));
267 ret_dict_dict2 = qdict_get_qdict(ret_dict, "dict3");
268 ret_dict_dict2_userdef = qdict_get_qdict(ret_dict_dict2, "userdef");
269 assert(qdict_get_int(ret_dict_dict2_userdef, "integer") == 422);
270 assert(!strcmp(qdict_get_str(ret_dict_dict2_userdef, "string"), "hello2"));
271 assert(!strcmp(qdict_get_str(ret_dict_dict2, "string"), "blah4"));
272 qobject_unref(ret);
274 qdict_put_int(args3, "a", 66);
275 qdict_put(req, "arguments", args3);
276 qdict_put_str(req, "execute", "guest-get-time");
278 ret3 = qobject_to(QNum, test_qmp_dispatch(req));
279 g_assert(qnum_get_try_int(ret3, &val));
280 g_assert_cmpint(val, ==, 66);
281 qobject_unref(ret3);
283 qobject_unref(req);
286 /* test generated dealloc functions for generated types */
287 static void test_dealloc_types(void)
289 UserDefOne *ud1test, *ud1a, *ud1b;
290 UserDefOneList *ud1list;
292 ud1test = g_malloc0(sizeof(UserDefOne));
293 ud1test->integer = 42;
294 ud1test->string = g_strdup("hi there 42");
296 qapi_free_UserDefOne(ud1test);
298 ud1a = g_malloc0(sizeof(UserDefOne));
299 ud1a->integer = 43;
300 ud1a->string = g_strdup("hi there 43");
302 ud1b = g_malloc0(sizeof(UserDefOne));
303 ud1b->integer = 44;
304 ud1b->string = g_strdup("hi there 44");
306 ud1list = g_malloc0(sizeof(UserDefOneList));
307 ud1list->value = ud1a;
308 ud1list->next = g_malloc0(sizeof(UserDefOneList));
309 ud1list->next->value = ud1b;
311 qapi_free_UserDefOneList(ud1list);
314 /* test generated deallocation on an object whose construction was prematurely
315 * terminated due to an error */
316 static void test_dealloc_partial(void)
318 static const char text[] = "don't leak me";
320 UserDefTwo *ud2 = NULL;
321 Error *err = NULL;
323 /* create partial object */
325 QDict *ud2_dict;
326 Visitor *v;
328 ud2_dict = qdict_new();
329 qdict_put_str(ud2_dict, "string0", text);
331 v = qobject_input_visitor_new(QOBJECT(ud2_dict));
332 visit_type_UserDefTwo(v, NULL, &ud2, &err);
333 visit_free(v);
334 qobject_unref(ud2_dict);
337 /* verify that visit_type_XXX() cleans up properly on error */
338 error_free_or_abort(&err);
339 assert(!ud2);
341 /* Manually create a partial object, leaving ud2->dict1 at NULL */
342 ud2 = g_new0(UserDefTwo, 1);
343 ud2->string0 = g_strdup(text);
345 /* tear down partial object */
346 qapi_free_UserDefTwo(ud2);
350 int main(int argc, char **argv)
352 g_test_init(&argc, &argv, NULL);
354 g_test_add_func("/qmp/dispatch_cmd", test_dispatch_cmd);
355 g_test_add_func("/qmp/dispatch_cmd_oob", test_dispatch_cmd_oob);
356 g_test_add_func("/qmp/dispatch_cmd_failure", test_dispatch_cmd_failure);
357 g_test_add_func("/qmp/dispatch_cmd_io", test_dispatch_cmd_io);
358 g_test_add_func("/qmp/dispatch_cmd_success_response",
359 test_dispatch_cmd_success_response);
360 g_test_add_func("/qmp/dealloc_types", test_dealloc_types);
361 g_test_add_func("/qmp/dealloc_partial", test_dealloc_partial);
363 test_qmp_init_marshal(&qmp_commands);
364 g_test_run();
366 return 0;