scripts/update-linux-headers: import pvrdma headers
[qemu/ar7.git] / tests / test-qmp-commands.c
blob24660d0868c2f8dce5ed7b28ba2654cc31344404
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 "test-qmp-commands.h"
7 #include "qapi/error.h"
8 #include "qemu/module.h"
9 #include "qapi/qobject-input-visitor.h"
10 #include "tests/test-qapi-types.h"
11 #include "tests/test-qapi-visit.h"
13 static QmpCommandList qmp_commands;
15 void qmp_user_def_cmd(Error **errp)
19 Empty2 *qmp_user_def_cmd0(Error **errp)
21 return g_new0(Empty2, 1);
24 void qmp_user_def_cmd1(UserDefOne * ud1, Error **errp)
28 UserDefTwo *qmp_user_def_cmd2(UserDefOne *ud1a,
29 bool has_udb1, UserDefOne *ud1b,
30 Error **errp)
32 UserDefTwo *ret;
33 UserDefOne *ud1c = g_malloc0(sizeof(UserDefOne));
34 UserDefOne *ud1d = g_malloc0(sizeof(UserDefOne));
36 ud1c->string = strdup(ud1a->string);
37 ud1c->integer = ud1a->integer;
38 ud1d->string = strdup(has_udb1 ? ud1b->string : "blah0");
39 ud1d->integer = has_udb1 ? ud1b->integer : 0;
41 ret = g_new0(UserDefTwo, 1);
42 ret->string0 = strdup("blah1");
43 ret->dict1 = g_new0(UserDefTwoDict, 1);
44 ret->dict1->string1 = strdup("blah2");
45 ret->dict1->dict2 = g_new0(UserDefTwoDictDict, 1);
46 ret->dict1->dict2->userdef = ud1c;
47 ret->dict1->dict2->string = strdup("blah3");
48 ret->dict1->dict3 = g_new0(UserDefTwoDictDict, 1);
49 ret->dict1->has_dict3 = true;
50 ret->dict1->dict3->userdef = ud1d;
51 ret->dict1->dict3->string = strdup("blah4");
53 return ret;
56 int64_t qmp_guest_get_time(int64_t a, bool has_b, int64_t b, Error **errp)
58 return a + (has_b ? b : 0);
61 QObject *qmp_guest_sync(QObject *arg, Error **errp)
63 return arg;
66 void qmp_boxed_struct(UserDefZero *arg, Error **errp)
70 void qmp_boxed_union(UserDefNativeListUnion *arg, Error **errp)
74 __org_qemu_x_Union1 *qmp___org_qemu_x_command(__org_qemu_x_EnumList *a,
75 __org_qemu_x_StructList *b,
76 __org_qemu_x_Union2 *c,
77 __org_qemu_x_Alt *d,
78 Error **errp)
80 __org_qemu_x_Union1 *ret = g_new0(__org_qemu_x_Union1, 1);
82 ret->type = ORG_QEMU_X_UNION1_KIND___ORG_QEMU_X_BRANCH;
83 ret->u.__org_qemu_x_branch.data = strdup("blah1");
85 /* Also test that 'wchar-t' was munged to 'q_wchar_t' */
86 if (b && b->value && !b->value->has_q_wchar_t) {
87 b->value->q_wchar_t = 1;
89 return ret;
93 /* test commands with no input and no return value */
94 static void test_dispatch_cmd(void)
96 QDict *req = qdict_new();
97 QObject *resp;
99 qdict_put_str(req, "execute", "user_def_cmd");
101 resp = qmp_dispatch(&qmp_commands, QOBJECT(req));
102 assert(resp != NULL);
103 assert(!qdict_haskey(qobject_to_qdict(resp), "error"));
105 qobject_decref(resp);
106 QDECREF(req);
109 /* test commands that return an error due to invalid parameters */
110 static void test_dispatch_cmd_failure(void)
112 QDict *req = qdict_new();
113 QDict *args = qdict_new();
114 QObject *resp;
116 qdict_put_str(req, "execute", "user_def_cmd2");
118 resp = qmp_dispatch(&qmp_commands, QOBJECT(req));
119 assert(resp != NULL);
120 assert(qdict_haskey(qobject_to_qdict(resp), "error"));
122 qobject_decref(resp);
123 QDECREF(req);
125 /* check that with extra arguments it throws an error */
126 req = qdict_new();
127 qdict_put_int(args, "a", 66);
128 qdict_put(req, "arguments", args);
130 qdict_put_str(req, "execute", "user_def_cmd");
132 resp = qmp_dispatch(&qmp_commands, QOBJECT(req));
133 assert(resp != NULL);
134 assert(qdict_haskey(qobject_to_qdict(resp), "error"));
136 qobject_decref(resp);
137 QDECREF(req);
140 static QObject *test_qmp_dispatch(QDict *req)
142 QObject *resp_obj;
143 QDict *resp;
144 QObject *ret;
146 resp_obj = qmp_dispatch(&qmp_commands, QOBJECT(req));
147 assert(resp_obj);
148 resp = qobject_to_qdict(resp_obj);
149 assert(resp && !qdict_haskey(resp, "error"));
150 ret = qdict_get(resp, "return");
151 assert(ret);
152 qobject_incref(ret);
153 qobject_decref(resp_obj);
154 return ret;
157 /* test commands that involve both input parameters and return values */
158 static void test_dispatch_cmd_io(void)
160 QDict *req = qdict_new();
161 QDict *args = qdict_new();
162 QDict *args3 = qdict_new();
163 QDict *ud1a = qdict_new();
164 QDict *ud1b = qdict_new();
165 QDict *ret, *ret_dict, *ret_dict_dict, *ret_dict_dict_userdef;
166 QDict *ret_dict_dict2, *ret_dict_dict2_userdef;
167 QNum *ret3;
168 int64_t val;
170 qdict_put_int(ud1a, "integer", 42);
171 qdict_put_str(ud1a, "string", "hello");
172 qdict_put_int(ud1b, "integer", 422);
173 qdict_put_str(ud1b, "string", "hello2");
174 qdict_put(args, "ud1a", ud1a);
175 qdict_put(args, "ud1b", ud1b);
176 qdict_put(req, "arguments", args);
177 qdict_put_str(req, "execute", "user_def_cmd2");
179 ret = qobject_to_qdict(test_qmp_dispatch(req));
181 assert(!strcmp(qdict_get_str(ret, "string0"), "blah1"));
182 ret_dict = qdict_get_qdict(ret, "dict1");
183 assert(!strcmp(qdict_get_str(ret_dict, "string1"), "blah2"));
184 ret_dict_dict = qdict_get_qdict(ret_dict, "dict2");
185 ret_dict_dict_userdef = qdict_get_qdict(ret_dict_dict, "userdef");
186 assert(qdict_get_int(ret_dict_dict_userdef, "integer") == 42);
187 assert(!strcmp(qdict_get_str(ret_dict_dict_userdef, "string"), "hello"));
188 assert(!strcmp(qdict_get_str(ret_dict_dict, "string"), "blah3"));
189 ret_dict_dict2 = qdict_get_qdict(ret_dict, "dict3");
190 ret_dict_dict2_userdef = qdict_get_qdict(ret_dict_dict2, "userdef");
191 assert(qdict_get_int(ret_dict_dict2_userdef, "integer") == 422);
192 assert(!strcmp(qdict_get_str(ret_dict_dict2_userdef, "string"), "hello2"));
193 assert(!strcmp(qdict_get_str(ret_dict_dict2, "string"), "blah4"));
194 QDECREF(ret);
196 qdict_put_int(args3, "a", 66);
197 qdict_put(req, "arguments", args3);
198 qdict_put_str(req, "execute", "guest-get-time");
200 ret3 = qobject_to_qnum(test_qmp_dispatch(req));
201 g_assert(qnum_get_try_int(ret3, &val));
202 g_assert_cmpint(val, ==, 66);
203 QDECREF(ret3);
205 QDECREF(req);
208 /* test generated dealloc functions for generated types */
209 static void test_dealloc_types(void)
211 UserDefOne *ud1test, *ud1a, *ud1b;
212 UserDefOneList *ud1list;
214 ud1test = g_malloc0(sizeof(UserDefOne));
215 ud1test->integer = 42;
216 ud1test->string = g_strdup("hi there 42");
218 qapi_free_UserDefOne(ud1test);
220 ud1a = g_malloc0(sizeof(UserDefOne));
221 ud1a->integer = 43;
222 ud1a->string = g_strdup("hi there 43");
224 ud1b = g_malloc0(sizeof(UserDefOne));
225 ud1b->integer = 44;
226 ud1b->string = g_strdup("hi there 44");
228 ud1list = g_malloc0(sizeof(UserDefOneList));
229 ud1list->value = ud1a;
230 ud1list->next = g_malloc0(sizeof(UserDefOneList));
231 ud1list->next->value = ud1b;
233 qapi_free_UserDefOneList(ud1list);
236 /* test generated deallocation on an object whose construction was prematurely
237 * terminated due to an error */
238 static void test_dealloc_partial(void)
240 static const char text[] = "don't leak me";
242 UserDefTwo *ud2 = NULL;
243 Error *err = NULL;
245 /* create partial object */
247 QDict *ud2_dict;
248 Visitor *v;
250 ud2_dict = qdict_new();
251 qdict_put_str(ud2_dict, "string0", text);
253 v = qobject_input_visitor_new(QOBJECT(ud2_dict));
254 visit_type_UserDefTwo(v, NULL, &ud2, &err);
255 visit_free(v);
256 QDECREF(ud2_dict);
259 /* verify that visit_type_XXX() cleans up properly on error */
260 error_free_or_abort(&err);
261 assert(!ud2);
263 /* Manually create a partial object, leaving ud2->dict1 at NULL */
264 ud2 = g_new0(UserDefTwo, 1);
265 ud2->string0 = g_strdup(text);
267 /* tear down partial object */
268 qapi_free_UserDefTwo(ud2);
272 int main(int argc, char **argv)
274 g_test_init(&argc, &argv, NULL);
276 g_test_add_func("/0.15/dispatch_cmd", test_dispatch_cmd);
277 g_test_add_func("/0.15/dispatch_cmd_failure", test_dispatch_cmd_failure);
278 g_test_add_func("/0.15/dispatch_cmd_io", test_dispatch_cmd_io);
279 g_test_add_func("/0.15/dealloc_types", test_dealloc_types);
280 g_test_add_func("/0.15/dealloc_partial", test_dealloc_partial);
282 test_qmp_init_marshal(&qmp_commands);
283 g_test_run();
285 return 0;