]> Git Repo - qemu.git/blob - tests/test-qmp-cmds.c
qapi: Fix code generation for empty modules
[qemu.git] / tests / test-qmp-cmds.c
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"
11
12 static QmpCommandList qmp_commands;
13
14 #if defined(TEST_IF_STRUCT) && defined(TEST_IF_CMD)
15 UserDefThree *qmp_TestIfCmd(TestIfStruct *foo, Error **errp)
16 {
17     return NULL;
18 }
19 #endif
20
21 UserDefThree *qmp_TestCmdReturnDefThree(Error **errp)
22 {
23     return NULL;
24 }
25
26 void qmp_user_def_cmd(Error **errp)
27 {
28 }
29
30 void qmp_test_flags_command(Error **errp)
31 {
32 }
33
34 void qmp_cmd_success_response(Error **errp)
35 {
36 }
37
38 Empty2 *qmp_user_def_cmd0(Error **errp)
39 {
40     return g_new0(Empty2, 1);
41 }
42
43 void qmp_user_def_cmd1(UserDefOne * ud1, Error **errp)
44 {
45 }
46
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)
52 {
53 }
54
55 void qmp_test_command_features0(Error **errp)
56 {
57 }
58
59 void qmp_test_command_features1(Error **errp)
60 {
61 }
62
63 void qmp_test_command_features3(Error **errp)
64 {
65 }
66
67 void qmp_test_command_cond_features1(Error **errp)
68 {
69 }
70
71 void qmp_test_command_cond_features2(Error **errp)
72 {
73 }
74
75 void qmp_test_command_cond_features3(Error **errp)
76 {
77 }
78
79 UserDefTwo *qmp_user_def_cmd2(UserDefOne *ud1a,
80                               bool has_udb1, UserDefOne *ud1b,
81                               Error **errp)
82 {
83     UserDefTwo *ret;
84     UserDefOne *ud1c = g_malloc0(sizeof(UserDefOne));
85     UserDefOne *ud1d = g_malloc0(sizeof(UserDefOne));
86
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;
91
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");
103
104     return ret;
105 }
106
107 int64_t qmp_guest_get_time(int64_t a, bool has_b, int64_t b, Error **errp)
108 {
109     return a + (has_b ? b : 0);
110 }
111
112 QObject *qmp_guest_sync(QObject *arg, Error **errp)
113 {
114     return arg;
115 }
116
117 void qmp_boxed_struct(UserDefZero *arg, Error **errp)
118 {
119 }
120
121 void qmp_boxed_union(UserDefListUnion *arg, Error **errp)
122 {
123 }
124
125 void qmp_boxed_empty(Empty1 *arg, Error **errp)
126 {
127 }
128
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)
134 {
135     __org_qemu_x_Union1 *ret = g_new0(__org_qemu_x_Union1, 1);
136
137     ret->type = ORG_QEMU_X_UNION1_KIND___ORG_QEMU_X_BRANCH;
138     ret->u.__org_qemu_x_branch.data = strdup("blah1");
139
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;
143     }
144     return ret;
145 }
146
147
148 /* test commands with no input and no return value */
149 static void test_dispatch_cmd(void)
150 {
151     QDict *req = qdict_new();
152     QDict *resp;
153
154     qdict_put_str(req, "execute", "user_def_cmd");
155
156     resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
157     assert(resp != NULL);
158     assert(!qdict_haskey(resp, "error"));
159
160     qobject_unref(resp);
161     qobject_unref(req);
162 }
163
164 static void test_dispatch_cmd_oob(void)
165 {
166     QDict *req = qdict_new();
167     QDict *resp;
168
169     qdict_put_str(req, "exec-oob", "test-flags-command");
170
171     resp = qmp_dispatch(&qmp_commands, QOBJECT(req), true);
172     assert(resp != NULL);
173     assert(!qdict_haskey(resp, "error"));
174
175     qobject_unref(resp);
176     qobject_unref(req);
177 }
178
179 /* test commands that return an error due to invalid parameters */
180 static void test_dispatch_cmd_failure(void)
181 {
182     QDict *req = qdict_new();
183     QDict *args = qdict_new();
184     QDict *resp;
185
186     qdict_put_str(req, "execute", "user_def_cmd2");
187
188     resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
189     assert(resp != NULL);
190     assert(qdict_haskey(resp, "error"));
191
192     qobject_unref(resp);
193     qobject_unref(req);
194
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);
199
200     qdict_put_str(req, "execute", "user_def_cmd");
201
202     resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false);
203     assert(resp != NULL);
204     assert(qdict_haskey(resp, "error"));
205
206     qobject_unref(resp);
207     qobject_unref(req);
208 }
209
210 static void test_dispatch_cmd_success_response(void)
211 {
212     QDict *req = qdict_new();
213     QDict *resp;
214
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);
219 }
220
221 static QObject *test_qmp_dispatch(QDict *req)
222 {
223     QDict *resp;
224     QObject *ret;
225
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;
233 }
234
235 /* test commands that involve both input parameters and return values */
236 static void test_dispatch_cmd_io(void)
237 {
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;
247
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");
256
257     ret = qobject_to(QDict, test_qmp_dispatch(req));
258
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);
273
274     qdict_put_int(args3, "a", 66);
275     qdict_put(req, "arguments", args3);
276     qdict_put_str(req, "execute", "guest-get-time");
277
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);
282
283     qobject_unref(req);
284 }
285
286 /* test generated dealloc functions for generated types */
287 static void test_dealloc_types(void)
288 {
289     UserDefOne *ud1test, *ud1a, *ud1b;
290     UserDefOneList *ud1list;
291
292     ud1test = g_malloc0(sizeof(UserDefOne));
293     ud1test->integer = 42;
294     ud1test->string = g_strdup("hi there 42");
295
296     qapi_free_UserDefOne(ud1test);
297
298     ud1a = g_malloc0(sizeof(UserDefOne));
299     ud1a->integer = 43;
300     ud1a->string = g_strdup("hi there 43");
301
302     ud1b = g_malloc0(sizeof(UserDefOne));
303     ud1b->integer = 44;
304     ud1b->string = g_strdup("hi there 44");
305
306     ud1list = g_malloc0(sizeof(UserDefOneList));
307     ud1list->value = ud1a;
308     ud1list->next = g_malloc0(sizeof(UserDefOneList));
309     ud1list->next->value = ud1b;
310
311     qapi_free_UserDefOneList(ud1list);
312 }
313
314 /* test generated deallocation on an object whose construction was prematurely
315  * terminated due to an error */
316 static void test_dealloc_partial(void)
317 {
318     static const char text[] = "don't leak me";
319
320     UserDefTwo *ud2 = NULL;
321     Error *err = NULL;
322
323     /* create partial object */
324     {
325         QDict *ud2_dict;
326         Visitor *v;
327
328         ud2_dict = qdict_new();
329         qdict_put_str(ud2_dict, "string0", text);
330
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);
335     }
336
337     /* verify that visit_type_XXX() cleans up properly on error */
338     error_free_or_abort(&err);
339     assert(!ud2);
340
341     /* Manually create a partial object, leaving ud2->dict1 at NULL */
342     ud2 = g_new0(UserDefTwo, 1);
343     ud2->string0 = g_strdup(text);
344
345     /* tear down partial object */
346     qapi_free_UserDefTwo(ud2);
347 }
348
349
350 int main(int argc, char **argv)
351 {
352     g_test_init(&argc, &argv, NULL);
353
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);
362
363     test_qmp_init_marshal(&qmp_commands);
364     g_test_run();
365
366     return 0;
367 }
This page took 0.040647 seconds and 4 git commands to generate.