Merge remote-tracking branch 'remotes/ehabkost/tags/x86-next-pull-request' into staging
[qemu/ar7.git] / tests / test-qmp-cmds.c
blobab389f42da8304757c653039a855c9310fff8e89
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 UserDefTwo *qmp_user_def_cmd2(UserDefOne *ud1a,
55 bool has_udb1, UserDefOne *ud1b,
56 Error **errp)
58 UserDefTwo *ret;
59 UserDefOne *ud1c = g_malloc0(sizeof(UserDefOne));
60 UserDefOne *ud1d = g_malloc0(sizeof(UserDefOne));
62 ud1c->string = strdup(ud1a->string);
63 ud1c->integer = ud1a->integer;
64 ud1d->string = strdup(has_udb1 ? ud1b->string : "blah0");
65 ud1d->integer = has_udb1 ? ud1b->integer : 0;
67 ret = g_new0(UserDefTwo, 1);
68 ret->string0 = strdup("blah1");
69 ret->dict1 = g_new0(UserDefTwoDict, 1);
70 ret->dict1->string1 = strdup("blah2");
71 ret->dict1->dict2 = g_new0(UserDefTwoDictDict, 1);
72 ret->dict1->dict2->userdef = ud1c;
73 ret->dict1->dict2->string = strdup("blah3");
74 ret->dict1->dict3 = g_new0(UserDefTwoDictDict, 1);
75 ret->dict1->has_dict3 = true;
76 ret->dict1->dict3->userdef = ud1d;
77 ret->dict1->dict3->string = strdup("blah4");
79 return ret;
82 int64_t qmp_guest_get_time(int64_t a, bool has_b, int64_t b, Error **errp)
84 return a + (has_b ? b : 0);
87 QObject *qmp_guest_sync(QObject *arg, Error **errp)
89 return arg;
92 void qmp_boxed_struct(UserDefZero *arg, Error **errp)
96 void qmp_boxed_union(UserDefListUnion *arg, Error **errp)
100 __org_qemu_x_Union1 *qmp___org_qemu_x_command(__org_qemu_x_EnumList *a,
101 __org_qemu_x_StructList *b,
102 __org_qemu_x_Union2 *c,
103 __org_qemu_x_Alt *d,
104 Error **errp)
106 __org_qemu_x_Union1 *ret = g_new0(__org_qemu_x_Union1, 1);
108 ret->type = ORG_QEMU_X_UNION1_KIND___ORG_QEMU_X_BRANCH;
109 ret->u.__org_qemu_x_branch.data = strdup("blah1");
111 /* Also test that 'wchar-t' was munged to 'q_wchar_t' */
112 if (b && b->value && !b->value->has_q_wchar_t) {
113 b->value->q_wchar_t = 1;
115 return ret;
119 /* test commands with no input and no return value */
120 static void test_dispatch_cmd(void)
122 QDict *req = qdict_new();
123 QDict *resp;
125 qdict_put_str(req, "execute", "user_def_cmd");
127 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
128 assert(resp != NULL);
129 assert(!qdict_haskey(resp, "error"));
131 qobject_unref(resp);
132 qobject_unref(req);
135 static void test_dispatch_cmd_oob(void)
137 QDict *req = qdict_new();
138 QDict *resp;
140 qdict_put_str(req, "exec-oob", "test-flags-command");
142 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), true);
143 assert(resp != NULL);
144 assert(!qdict_haskey(resp, "error"));
146 qobject_unref(resp);
147 qobject_unref(req);
150 /* test commands that return an error due to invalid parameters */
151 static void test_dispatch_cmd_failure(void)
153 QDict *req = qdict_new();
154 QDict *args = qdict_new();
155 QDict *resp;
157 qdict_put_str(req, "execute", "user_def_cmd2");
159 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
160 assert(resp != NULL);
161 assert(qdict_haskey(resp, "error"));
163 qobject_unref(resp);
164 qobject_unref(req);
166 /* check that with extra arguments it throws an error */
167 req = qdict_new();
168 qdict_put_int(args, "a", 66);
169 qdict_put(req, "arguments", args);
171 qdict_put_str(req, "execute", "user_def_cmd");
173 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
174 assert(resp != NULL);
175 assert(qdict_haskey(resp, "error"));
177 qobject_unref(resp);
178 qobject_unref(req);
181 static void test_dispatch_cmd_success_response(void)
183 QDict *req = qdict_new();
184 QDict *resp;
186 qdict_put_str(req, "execute", "cmd-success-response");
187 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
188 g_assert_null(resp);
189 qobject_unref(req);
192 static QObject *test_qmp_dispatch(QDict *req)
194 QDict *resp;
195 QObject *ret;
197 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
198 assert(resp && !qdict_haskey(resp, "error"));
199 ret = qdict_get(resp, "return");
200 assert(ret);
201 qobject_ref(ret);
202 qobject_unref(resp);
203 return ret;
206 /* test commands that involve both input parameters and return values */
207 static void test_dispatch_cmd_io(void)
209 QDict *req = qdict_new();
210 QDict *args = qdict_new();
211 QDict *args3 = qdict_new();
212 QDict *ud1a = qdict_new();
213 QDict *ud1b = qdict_new();
214 QDict *ret, *ret_dict, *ret_dict_dict, *ret_dict_dict_userdef;
215 QDict *ret_dict_dict2, *ret_dict_dict2_userdef;
216 QNum *ret3;
217 int64_t val;
219 qdict_put_int(ud1a, "integer", 42);
220 qdict_put_str(ud1a, "string", "hello");
221 qdict_put_int(ud1b, "integer", 422);
222 qdict_put_str(ud1b, "string", "hello2");
223 qdict_put(args, "ud1a", ud1a);
224 qdict_put(args, "ud1b", ud1b);
225 qdict_put(req, "arguments", args);
226 qdict_put_str(req, "execute", "user_def_cmd2");
228 ret = qobject_to(QDict, test_qmp_dispatch(req));
230 assert(!strcmp(qdict_get_str(ret, "string0"), "blah1"));
231 ret_dict = qdict_get_qdict(ret, "dict1");
232 assert(!strcmp(qdict_get_str(ret_dict, "string1"), "blah2"));
233 ret_dict_dict = qdict_get_qdict(ret_dict, "dict2");
234 ret_dict_dict_userdef = qdict_get_qdict(ret_dict_dict, "userdef");
235 assert(qdict_get_int(ret_dict_dict_userdef, "integer") == 42);
236 assert(!strcmp(qdict_get_str(ret_dict_dict_userdef, "string"), "hello"));
237 assert(!strcmp(qdict_get_str(ret_dict_dict, "string"), "blah3"));
238 ret_dict_dict2 = qdict_get_qdict(ret_dict, "dict3");
239 ret_dict_dict2_userdef = qdict_get_qdict(ret_dict_dict2, "userdef");
240 assert(qdict_get_int(ret_dict_dict2_userdef, "integer") == 422);
241 assert(!strcmp(qdict_get_str(ret_dict_dict2_userdef, "string"), "hello2"));
242 assert(!strcmp(qdict_get_str(ret_dict_dict2, "string"), "blah4"));
243 qobject_unref(ret);
245 qdict_put_int(args3, "a", 66);
246 qdict_put(req, "arguments", args3);
247 qdict_put_str(req, "execute", "guest-get-time");
249 ret3 = qobject_to(QNum, test_qmp_dispatch(req));
250 g_assert(qnum_get_try_int(ret3, &val));
251 g_assert_cmpint(val, ==, 66);
252 qobject_unref(ret3);
254 qobject_unref(req);
257 /* test generated dealloc functions for generated types */
258 static void test_dealloc_types(void)
260 UserDefOne *ud1test, *ud1a, *ud1b;
261 UserDefOneList *ud1list;
263 ud1test = g_malloc0(sizeof(UserDefOne));
264 ud1test->integer = 42;
265 ud1test->string = g_strdup("hi there 42");
267 qapi_free_UserDefOne(ud1test);
269 ud1a = g_malloc0(sizeof(UserDefOne));
270 ud1a->integer = 43;
271 ud1a->string = g_strdup("hi there 43");
273 ud1b = g_malloc0(sizeof(UserDefOne));
274 ud1b->integer = 44;
275 ud1b->string = g_strdup("hi there 44");
277 ud1list = g_malloc0(sizeof(UserDefOneList));
278 ud1list->value = ud1a;
279 ud1list->next = g_malloc0(sizeof(UserDefOneList));
280 ud1list->next->value = ud1b;
282 qapi_free_UserDefOneList(ud1list);
285 /* test generated deallocation on an object whose construction was prematurely
286 * terminated due to an error */
287 static void test_dealloc_partial(void)
289 static const char text[] = "don't leak me";
291 UserDefTwo *ud2 = NULL;
292 Error *err = NULL;
294 /* create partial object */
296 QDict *ud2_dict;
297 Visitor *v;
299 ud2_dict = qdict_new();
300 qdict_put_str(ud2_dict, "string0", text);
302 v = qobject_input_visitor_new(QOBJECT(ud2_dict));
303 visit_type_UserDefTwo(v, NULL, &ud2, &err);
304 visit_free(v);
305 qobject_unref(ud2_dict);
308 /* verify that visit_type_XXX() cleans up properly on error */
309 error_free_or_abort(&err);
310 assert(!ud2);
312 /* Manually create a partial object, leaving ud2->dict1 at NULL */
313 ud2 = g_new0(UserDefTwo, 1);
314 ud2->string0 = g_strdup(text);
316 /* tear down partial object */
317 qapi_free_UserDefTwo(ud2);
321 int main(int argc, char **argv)
323 g_test_init(&argc, &argv, NULL);
325 g_test_add_func("/qmp/dispatch_cmd", test_dispatch_cmd);
326 g_test_add_func("/qmp/dispatch_cmd_oob", test_dispatch_cmd_oob);
327 g_test_add_func("/qmp/dispatch_cmd_failure", test_dispatch_cmd_failure);
328 g_test_add_func("/qmp/dispatch_cmd_io", test_dispatch_cmd_io);
329 g_test_add_func("/qmp/dispatch_cmd_success_response",
330 test_dispatch_cmd_success_response);
331 g_test_add_func("/qmp/dealloc_types", test_dealloc_types);
332 g_test_add_func("/qmp/dealloc_partial", test_dealloc_partial);
334 test_qmp_init_marshal(&qmp_commands);
335 g_test_run();
337 return 0;