aspeed/timer: Add AST2600 support
[qemu.git] / tests / test-qmp-cmds.c
blob36fdf5b1154f8275da77be04e8e159e4e04747b6
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 void qmp_boxed_empty(Empty1 *arg, Error **errp)
104 __org_qemu_x_Union1 *qmp___org_qemu_x_command(__org_qemu_x_EnumList *a,
105 __org_qemu_x_StructList *b,
106 __org_qemu_x_Union2 *c,
107 __org_qemu_x_Alt *d,
108 Error **errp)
110 __org_qemu_x_Union1 *ret = g_new0(__org_qemu_x_Union1, 1);
112 ret->type = ORG_QEMU_X_UNION1_KIND___ORG_QEMU_X_BRANCH;
113 ret->u.__org_qemu_x_branch.data = strdup("blah1");
115 /* Also test that 'wchar-t' was munged to 'q_wchar_t' */
116 if (b && b->value && !b->value->has_q_wchar_t) {
117 b->value->q_wchar_t = 1;
119 return ret;
123 /* test commands with no input and no return value */
124 static void test_dispatch_cmd(void)
126 QDict *req = qdict_new();
127 QDict *resp;
129 qdict_put_str(req, "execute", "user_def_cmd");
131 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
132 assert(resp != NULL);
133 assert(!qdict_haskey(resp, "error"));
135 qobject_unref(resp);
136 qobject_unref(req);
139 static void test_dispatch_cmd_oob(void)
141 QDict *req = qdict_new();
142 QDict *resp;
144 qdict_put_str(req, "exec-oob", "test-flags-command");
146 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), true);
147 assert(resp != NULL);
148 assert(!qdict_haskey(resp, "error"));
150 qobject_unref(resp);
151 qobject_unref(req);
154 /* test commands that return an error due to invalid parameters */
155 static void test_dispatch_cmd_failure(void)
157 QDict *req = qdict_new();
158 QDict *args = qdict_new();
159 QDict *resp;
161 qdict_put_str(req, "execute", "user_def_cmd2");
163 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
164 assert(resp != NULL);
165 assert(qdict_haskey(resp, "error"));
167 qobject_unref(resp);
168 qobject_unref(req);
170 /* check that with extra arguments it throws an error */
171 req = qdict_new();
172 qdict_put_int(args, "a", 66);
173 qdict_put(req, "arguments", args);
175 qdict_put_str(req, "execute", "user_def_cmd");
177 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
178 assert(resp != NULL);
179 assert(qdict_haskey(resp, "error"));
181 qobject_unref(resp);
182 qobject_unref(req);
185 static void test_dispatch_cmd_success_response(void)
187 QDict *req = qdict_new();
188 QDict *resp;
190 qdict_put_str(req, "execute", "cmd-success-response");
191 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
192 g_assert_null(resp);
193 qobject_unref(req);
196 static QObject *test_qmp_dispatch(QDict *req)
198 QDict *resp;
199 QObject *ret;
201 resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
202 assert(resp && !qdict_haskey(resp, "error"));
203 ret = qdict_get(resp, "return");
204 assert(ret);
205 qobject_ref(ret);
206 qobject_unref(resp);
207 return ret;
210 /* test commands that involve both input parameters and return values */
211 static void test_dispatch_cmd_io(void)
213 QDict *req = qdict_new();
214 QDict *args = qdict_new();
215 QDict *args3 = qdict_new();
216 QDict *ud1a = qdict_new();
217 QDict *ud1b = qdict_new();
218 QDict *ret, *ret_dict, *ret_dict_dict, *ret_dict_dict_userdef;
219 QDict *ret_dict_dict2, *ret_dict_dict2_userdef;
220 QNum *ret3;
221 int64_t val;
223 qdict_put_int(ud1a, "integer", 42);
224 qdict_put_str(ud1a, "string", "hello");
225 qdict_put_int(ud1b, "integer", 422);
226 qdict_put_str(ud1b, "string", "hello2");
227 qdict_put(args, "ud1a", ud1a);
228 qdict_put(args, "ud1b", ud1b);
229 qdict_put(req, "arguments", args);
230 qdict_put_str(req, "execute", "user_def_cmd2");
232 ret = qobject_to(QDict, test_qmp_dispatch(req));
234 assert(!strcmp(qdict_get_str(ret, "string0"), "blah1"));
235 ret_dict = qdict_get_qdict(ret, "dict1");
236 assert(!strcmp(qdict_get_str(ret_dict, "string1"), "blah2"));
237 ret_dict_dict = qdict_get_qdict(ret_dict, "dict2");
238 ret_dict_dict_userdef = qdict_get_qdict(ret_dict_dict, "userdef");
239 assert(qdict_get_int(ret_dict_dict_userdef, "integer") == 42);
240 assert(!strcmp(qdict_get_str(ret_dict_dict_userdef, "string"), "hello"));
241 assert(!strcmp(qdict_get_str(ret_dict_dict, "string"), "blah3"));
242 ret_dict_dict2 = qdict_get_qdict(ret_dict, "dict3");
243 ret_dict_dict2_userdef = qdict_get_qdict(ret_dict_dict2, "userdef");
244 assert(qdict_get_int(ret_dict_dict2_userdef, "integer") == 422);
245 assert(!strcmp(qdict_get_str(ret_dict_dict2_userdef, "string"), "hello2"));
246 assert(!strcmp(qdict_get_str(ret_dict_dict2, "string"), "blah4"));
247 qobject_unref(ret);
249 qdict_put_int(args3, "a", 66);
250 qdict_put(req, "arguments", args3);
251 qdict_put_str(req, "execute", "guest-get-time");
253 ret3 = qobject_to(QNum, test_qmp_dispatch(req));
254 g_assert(qnum_get_try_int(ret3, &val));
255 g_assert_cmpint(val, ==, 66);
256 qobject_unref(ret3);
258 qobject_unref(req);
261 /* test generated dealloc functions for generated types */
262 static void test_dealloc_types(void)
264 UserDefOne *ud1test, *ud1a, *ud1b;
265 UserDefOneList *ud1list;
267 ud1test = g_malloc0(sizeof(UserDefOne));
268 ud1test->integer = 42;
269 ud1test->string = g_strdup("hi there 42");
271 qapi_free_UserDefOne(ud1test);
273 ud1a = g_malloc0(sizeof(UserDefOne));
274 ud1a->integer = 43;
275 ud1a->string = g_strdup("hi there 43");
277 ud1b = g_malloc0(sizeof(UserDefOne));
278 ud1b->integer = 44;
279 ud1b->string = g_strdup("hi there 44");
281 ud1list = g_malloc0(sizeof(UserDefOneList));
282 ud1list->value = ud1a;
283 ud1list->next = g_malloc0(sizeof(UserDefOneList));
284 ud1list->next->value = ud1b;
286 qapi_free_UserDefOneList(ud1list);
289 /* test generated deallocation on an object whose construction was prematurely
290 * terminated due to an error */
291 static void test_dealloc_partial(void)
293 static const char text[] = "don't leak me";
295 UserDefTwo *ud2 = NULL;
296 Error *err = NULL;
298 /* create partial object */
300 QDict *ud2_dict;
301 Visitor *v;
303 ud2_dict = qdict_new();
304 qdict_put_str(ud2_dict, "string0", text);
306 v = qobject_input_visitor_new(QOBJECT(ud2_dict));
307 visit_type_UserDefTwo(v, NULL, &ud2, &err);
308 visit_free(v);
309 qobject_unref(ud2_dict);
312 /* verify that visit_type_XXX() cleans up properly on error */
313 error_free_or_abort(&err);
314 assert(!ud2);
316 /* Manually create a partial object, leaving ud2->dict1 at NULL */
317 ud2 = g_new0(UserDefTwo, 1);
318 ud2->string0 = g_strdup(text);
320 /* tear down partial object */
321 qapi_free_UserDefTwo(ud2);
325 int main(int argc, char **argv)
327 g_test_init(&argc, &argv, NULL);
329 g_test_add_func("/qmp/dispatch_cmd", test_dispatch_cmd);
330 g_test_add_func("/qmp/dispatch_cmd_oob", test_dispatch_cmd_oob);
331 g_test_add_func("/qmp/dispatch_cmd_failure", test_dispatch_cmd_failure);
332 g_test_add_func("/qmp/dispatch_cmd_io", test_dispatch_cmd_io);
333 g_test_add_func("/qmp/dispatch_cmd_success_response",
334 test_dispatch_cmd_success_response);
335 g_test_add_func("/qmp/dealloc_types", test_dealloc_types);
336 g_test_add_func("/qmp/dealloc_partial", test_dealloc_partial);
338 test_qmp_init_marshal(&qmp_commands);
339 g_test_run();
341 return 0;