cirrus: fix off-by-one in cirrus_bitblt_rop_bkwd_transp_*_16
[qemu.git] / tests / test-qmp-commands.c
1 #include "qemu/osdep.h"
2 #include "qemu-common.h"
3 #include "qapi/qmp/types.h"
4 #include "test-qmp-commands.h"
5 #include "qapi/qmp/dispatch.h"
6 #include "qemu/module.h"
7 #include "qapi/qobject-input-visitor.h"
8 #include "tests/test-qapi-types.h"
9 #include "tests/test-qapi-visit.h"
10
11 static QmpCommandList qmp_commands;
12
13 void qmp_user_def_cmd(Error **errp)
14 {
15 }
16
17 Empty2 *qmp_user_def_cmd0(Error **errp)
18 {
19 return g_new0(Empty2, 1);
20 }
21
22 void qmp_user_def_cmd1(UserDefOne * ud1, Error **errp)
23 {
24 }
25
26 UserDefTwo *qmp_user_def_cmd2(UserDefOne *ud1a,
27 bool has_udb1, UserDefOne *ud1b,
28 Error **errp)
29 {
30 UserDefTwo *ret;
31 UserDefOne *ud1c = g_malloc0(sizeof(UserDefOne));
32 UserDefOne *ud1d = g_malloc0(sizeof(UserDefOne));
33
34 ud1c->string = strdup(ud1a->string);
35 ud1c->integer = ud1a->integer;
36 ud1d->string = strdup(has_udb1 ? ud1b->string : "blah0");
37 ud1d->integer = has_udb1 ? ud1b->integer : 0;
38
39 ret = g_new0(UserDefTwo, 1);
40 ret->string0 = strdup("blah1");
41 ret->dict1 = g_new0(UserDefTwoDict, 1);
42 ret->dict1->string1 = strdup("blah2");
43 ret->dict1->dict2 = g_new0(UserDefTwoDictDict, 1);
44 ret->dict1->dict2->userdef = ud1c;
45 ret->dict1->dict2->string = strdup("blah3");
46 ret->dict1->dict3 = g_new0(UserDefTwoDictDict, 1);
47 ret->dict1->has_dict3 = true;
48 ret->dict1->dict3->userdef = ud1d;
49 ret->dict1->dict3->string = strdup("blah4");
50
51 return ret;
52 }
53
54 int64_t qmp_guest_get_time(int64_t a, bool has_b, int64_t b, Error **errp)
55 {
56 return a + (has_b ? b : 0);
57 }
58
59 QObject *qmp_guest_sync(QObject *arg, Error **errp)
60 {
61 return arg;
62 }
63
64 void qmp_boxed_struct(UserDefZero *arg, Error **errp)
65 {
66 }
67
68 void qmp_boxed_union(UserDefNativeListUnion *arg, Error **errp)
69 {
70 }
71
72 __org_qemu_x_Union1 *qmp___org_qemu_x_command(__org_qemu_x_EnumList *a,
73 __org_qemu_x_StructList *b,
74 __org_qemu_x_Union2 *c,
75 __org_qemu_x_Alt *d,
76 Error **errp)
77 {
78 __org_qemu_x_Union1 *ret = g_new0(__org_qemu_x_Union1, 1);
79
80 ret->type = ORG_QEMU_X_UNION1_KIND___ORG_QEMU_X_BRANCH;
81 ret->u.__org_qemu_x_branch.data = strdup("blah1");
82
83 /* Also test that 'wchar-t' was munged to 'q_wchar_t' */
84 if (b && b->value && !b->value->has_q_wchar_t) {
85 b->value->q_wchar_t = 1;
86 }
87 return ret;
88 }
89
90
91 /* test commands with no input and no return value */
92 static void test_dispatch_cmd(void)
93 {
94 QDict *req = qdict_new();
95 QObject *resp;
96
97 qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd")));
98
99 resp = qmp_dispatch(&qmp_commands, QOBJECT(req));
100 assert(resp != NULL);
101 assert(!qdict_haskey(qobject_to_qdict(resp), "error"));
102
103 qobject_decref(resp);
104 QDECREF(req);
105 }
106
107 /* test commands that return an error due to invalid parameters */
108 static void test_dispatch_cmd_failure(void)
109 {
110 QDict *req = qdict_new();
111 QDict *args = qdict_new();
112 QObject *resp;
113
114 qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd2")));
115
116 resp = qmp_dispatch(&qmp_commands, QOBJECT(req));
117 assert(resp != NULL);
118 assert(qdict_haskey(qobject_to_qdict(resp), "error"));
119
120 qobject_decref(resp);
121 QDECREF(req);
122
123 /* check that with extra arguments it throws an error */
124 req = qdict_new();
125 qdict_put(args, "a", qint_from_int(66));
126 qdict_put(req, "arguments", args);
127
128 qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd")));
129
130 resp = qmp_dispatch(&qmp_commands, QOBJECT(req));
131 assert(resp != NULL);
132 assert(qdict_haskey(qobject_to_qdict(resp), "error"));
133
134 qobject_decref(resp);
135 QDECREF(req);
136 }
137
138 static QObject *test_qmp_dispatch(QDict *req)
139 {
140 QObject *resp_obj;
141 QDict *resp;
142 QObject *ret;
143
144 resp_obj = qmp_dispatch(&qmp_commands, QOBJECT(req));
145 assert(resp_obj);
146 resp = qobject_to_qdict(resp_obj);
147 assert(resp && !qdict_haskey(resp, "error"));
148 ret = qdict_get(resp, "return");
149 assert(ret);
150 qobject_incref(ret);
151 qobject_decref(resp_obj);
152 return ret;
153 }
154
155 /* test commands that involve both input parameters and return values */
156 static void test_dispatch_cmd_io(void)
157 {
158 QDict *req = qdict_new();
159 QDict *args = qdict_new();
160 QDict *args3 = qdict_new();
161 QDict *ud1a = qdict_new();
162 QDict *ud1b = qdict_new();
163 QDict *ret, *ret_dict, *ret_dict_dict, *ret_dict_dict_userdef;
164 QDict *ret_dict_dict2, *ret_dict_dict2_userdef;
165 QInt *ret3;
166
167 qdict_put_obj(ud1a, "integer", QOBJECT(qint_from_int(42)));
168 qdict_put_obj(ud1a, "string", QOBJECT(qstring_from_str("hello")));
169 qdict_put_obj(ud1b, "integer", QOBJECT(qint_from_int(422)));
170 qdict_put_obj(ud1b, "string", QOBJECT(qstring_from_str("hello2")));
171 qdict_put_obj(args, "ud1a", QOBJECT(ud1a));
172 qdict_put_obj(args, "ud1b", QOBJECT(ud1b));
173 qdict_put_obj(req, "arguments", QOBJECT(args));
174 qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd2")));
175
176 ret = qobject_to_qdict(test_qmp_dispatch(req));
177
178 assert(!strcmp(qdict_get_str(ret, "string0"), "blah1"));
179 ret_dict = qdict_get_qdict(ret, "dict1");
180 assert(!strcmp(qdict_get_str(ret_dict, "string1"), "blah2"));
181 ret_dict_dict = qdict_get_qdict(ret_dict, "dict2");
182 ret_dict_dict_userdef = qdict_get_qdict(ret_dict_dict, "userdef");
183 assert(qdict_get_int(ret_dict_dict_userdef, "integer") == 42);
184 assert(!strcmp(qdict_get_str(ret_dict_dict_userdef, "string"), "hello"));
185 assert(!strcmp(qdict_get_str(ret_dict_dict, "string"), "blah3"));
186 ret_dict_dict2 = qdict_get_qdict(ret_dict, "dict3");
187 ret_dict_dict2_userdef = qdict_get_qdict(ret_dict_dict2, "userdef");
188 assert(qdict_get_int(ret_dict_dict2_userdef, "integer") == 422);
189 assert(!strcmp(qdict_get_str(ret_dict_dict2_userdef, "string"), "hello2"));
190 assert(!strcmp(qdict_get_str(ret_dict_dict2, "string"), "blah4"));
191 QDECREF(ret);
192
193 qdict_put(args3, "a", qint_from_int(66));
194 qdict_put(req, "arguments", args3);
195 qdict_put(req, "execute", qstring_from_str("guest-get-time"));
196
197 ret3 = qobject_to_qint(test_qmp_dispatch(req));
198 assert(qint_get_int(ret3) == 66);
199 QDECREF(ret3);
200
201 QDECREF(req);
202 }
203
204 /* test generated dealloc functions for generated types */
205 static void test_dealloc_types(void)
206 {
207 UserDefOne *ud1test, *ud1a, *ud1b;
208 UserDefOneList *ud1list;
209
210 ud1test = g_malloc0(sizeof(UserDefOne));
211 ud1test->integer = 42;
212 ud1test->string = g_strdup("hi there 42");
213
214 qapi_free_UserDefOne(ud1test);
215
216 ud1a = g_malloc0(sizeof(UserDefOne));
217 ud1a->integer = 43;
218 ud1a->string = g_strdup("hi there 43");
219
220 ud1b = g_malloc0(sizeof(UserDefOne));
221 ud1b->integer = 44;
222 ud1b->string = g_strdup("hi there 44");
223
224 ud1list = g_malloc0(sizeof(UserDefOneList));
225 ud1list->value = ud1a;
226 ud1list->next = g_malloc0(sizeof(UserDefOneList));
227 ud1list->next->value = ud1b;
228
229 qapi_free_UserDefOneList(ud1list);
230 }
231
232 /* test generated deallocation on an object whose construction was prematurely
233 * terminated due to an error */
234 static void test_dealloc_partial(void)
235 {
236 static const char text[] = "don't leak me";
237
238 UserDefTwo *ud2 = NULL;
239 Error *err = NULL;
240
241 /* create partial object */
242 {
243 QDict *ud2_dict;
244 Visitor *v;
245
246 ud2_dict = qdict_new();
247 qdict_put_obj(ud2_dict, "string0", QOBJECT(qstring_from_str(text)));
248
249 v = qobject_input_visitor_new(QOBJECT(ud2_dict));
250 visit_type_UserDefTwo(v, NULL, &ud2, &err);
251 visit_free(v);
252 QDECREF(ud2_dict);
253 }
254
255 /* verify that visit_type_XXX() cleans up properly on error */
256 error_free_or_abort(&err);
257 assert(!ud2);
258
259 /* Manually create a partial object, leaving ud2->dict1 at NULL */
260 ud2 = g_new0(UserDefTwo, 1);
261 ud2->string0 = g_strdup(text);
262
263 /* tear down partial object */
264 qapi_free_UserDefTwo(ud2);
265 }
266
267
268 int main(int argc, char **argv)
269 {
270 g_test_init(&argc, &argv, NULL);
271
272 g_test_add_func("/0.15/dispatch_cmd", test_dispatch_cmd);
273 g_test_add_func("/0.15/dispatch_cmd_failure", test_dispatch_cmd_failure);
274 g_test_add_func("/0.15/dispatch_cmd_io", test_dispatch_cmd_io);
275 g_test_add_func("/0.15/dealloc_types", test_dealloc_types);
276 g_test_add_func("/0.15/dealloc_partial", test_dealloc_partial);
277
278 test_qmp_init_marshal(&qmp_commands);
279 g_test_run();
280
281 return 0;
282 }