xref: /openbmc/qemu/tests/unit/test-qmp-cmds.c (revision 19f4ed36)
1 #include "qemu/osdep.h"
2 #include "qapi/compat-policy.h"
3 #include "qapi/qmp/qdict.h"
4 #include "qapi/qmp/qjson.h"
5 #include "qapi/qmp/qnum.h"
6 #include "qapi/qmp/qstring.h"
7 #include "qapi/error.h"
8 #include "qapi/qobject-input-visitor.h"
9 #include "tests/test-qapi-types.h"
10 #include "tests/test-qapi-visit.h"
11 #include "test-qapi-commands.h"
12 #include "test-qapi-init-commands.h"
13 
14 static QmpCommandList qmp_commands;
15 
16 UserDefThree *qmp_test_cmd_return_def_three(Error **errp)
17 {
18     return NULL;
19 }
20 
21 void qmp_user_def_cmd(Error **errp)
22 {
23 }
24 
25 void qmp_test_flags_command(Error **errp)
26 {
27 }
28 
29 void qmp_cmd_success_response(Error **errp)
30 {
31 }
32 
33 void qmp_coroutine_cmd(Error **errp)
34 {
35 }
36 
37 Empty2 *qmp_user_def_cmd0(Error **errp)
38 {
39     return g_new0(Empty2, 1);
40 }
41 
42 void qmp_user_def_cmd1(UserDefOne * ud1, Error **errp)
43 {
44 }
45 
46 FeatureStruct1 *qmp_test_features0(bool has_fs0, FeatureStruct0 *fs0,
47                                    bool has_fs1, FeatureStruct1 *fs1,
48                                    bool has_fs2, FeatureStruct2 *fs2,
49                                    bool has_fs3, FeatureStruct3 *fs3,
50                                    bool has_fs4, FeatureStruct4 *fs4,
51                                    bool has_cfs1, CondFeatureStruct1 *cfs1,
52                                    bool has_cfs2, CondFeatureStruct2 *cfs2,
53                                    bool has_cfs3, CondFeatureStruct3 *cfs3,
54                                    Error **errp)
55 {
56     return g_new0(FeatureStruct1, 1);
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 static QObject *do_qmp_dispatch(bool allow_oob, const char *template, ...)
149 {
150     va_list ap;
151     QDict *req, *resp;
152     QObject *ret;
153 
154     va_start(ap, template);
155     req = qdict_from_vjsonf_nofail(template, ap);
156     va_end(ap);
157 
158     resp = qmp_dispatch(&qmp_commands, QOBJECT(req), allow_oob, NULL);
159     g_assert(resp);
160     ret = qdict_get(resp, "return");
161     g_assert(ret);
162     g_assert(qdict_size(resp) == 1);
163 
164     qobject_ref(ret);
165     qobject_unref(resp);
166     qobject_unref(req);
167     return ret;
168 }
169 
170 static void do_qmp_dispatch_error(bool allow_oob, ErrorClass cls,
171                                   const char *template, ...)
172 {
173     va_list ap;
174     QDict *req, *resp;
175     QDict *error;
176 
177     va_start(ap, template);
178     req = qdict_from_vjsonf_nofail(template, ap);
179     va_end(ap);
180 
181     resp = qmp_dispatch(&qmp_commands, QOBJECT(req), allow_oob, NULL);
182     g_assert(resp);
183     error = qdict_get_qdict(resp, "error");
184     g_assert(error);
185     g_assert_cmpstr(qdict_get_try_str(error, "class"),
186                     ==, QapiErrorClass_str(cls));
187     g_assert(qdict_get_try_str(error, "desc"));
188     g_assert(qdict_size(error) == 2);
189     g_assert(qdict_size(resp) == 1);
190 
191     qobject_unref(resp);
192     qobject_unref(req);
193 }
194 
195 /* test commands with no input and no return value */
196 static void test_dispatch_cmd(void)
197 {
198     QDict *ret;
199 
200     ret = qobject_to(QDict,
201                      do_qmp_dispatch(false,
202                                      "{ 'execute': 'user-def-cmd' }"));
203     assert(ret && qdict_size(ret) == 0);
204     qobject_unref(ret);
205 }
206 
207 static void test_dispatch_cmd_oob(void)
208 {
209     QDict *ret;
210 
211     ret = qobject_to(QDict,
212                      do_qmp_dispatch(true,
213                                      "{ 'exec-oob': 'test-flags-command' }"));
214     assert(ret && qdict_size(ret) == 0);
215     qobject_unref(ret);
216 }
217 
218 /* test commands that return an error due to invalid parameters */
219 static void test_dispatch_cmd_failure(void)
220 {
221     /* missing arguments */
222     do_qmp_dispatch_error(false, ERROR_CLASS_GENERIC_ERROR,
223                           "{ 'execute': 'user-def-cmd2' }");
224 
225     /* extra arguments */
226     do_qmp_dispatch_error(false, ERROR_CLASS_GENERIC_ERROR,
227                           "{ 'execute': 'user-def-cmd',"
228                           " 'arguments': { 'a': 66 } }");
229 }
230 
231 static void test_dispatch_cmd_success_response(void)
232 {
233     QDict *req = qdict_new();
234     QDict *resp;
235 
236     qdict_put_str(req, "execute", "cmd-success-response");
237     resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false, NULL);
238     g_assert_null(resp);
239     qobject_unref(req);
240 }
241 
242 /* test commands that involve both input parameters and return values */
243 static void test_dispatch_cmd_io(void)
244 {
245     QDict *ret, *ret_dict, *ret_dict_dict, *ret_dict_dict_userdef;
246     QDict *ret_dict_dict2, *ret_dict_dict2_userdef;
247     QNum *ret3;
248     int64_t val;
249 
250     ret = qobject_to(QDict, do_qmp_dispatch(false,
251         "{ 'execute': 'user-def-cmd2', 'arguments': {"
252         " 'ud1a': { 'integer': 42, 'string': 'hello' },"
253         " 'ud1b': { 'integer': 422, 'string': 'hello2' } } }"));
254 
255     assert(!strcmp(qdict_get_str(ret, "string0"), "blah1"));
256     ret_dict = qdict_get_qdict(ret, "dict1");
257     assert(!strcmp(qdict_get_str(ret_dict, "string1"), "blah2"));
258     ret_dict_dict = qdict_get_qdict(ret_dict, "dict2");
259     ret_dict_dict_userdef = qdict_get_qdict(ret_dict_dict, "userdef");
260     assert(qdict_get_int(ret_dict_dict_userdef, "integer") == 42);
261     assert(!strcmp(qdict_get_str(ret_dict_dict_userdef, "string"), "hello"));
262     assert(!strcmp(qdict_get_str(ret_dict_dict, "string"), "blah3"));
263     ret_dict_dict2 = qdict_get_qdict(ret_dict, "dict3");
264     ret_dict_dict2_userdef = qdict_get_qdict(ret_dict_dict2, "userdef");
265     assert(qdict_get_int(ret_dict_dict2_userdef, "integer") == 422);
266     assert(!strcmp(qdict_get_str(ret_dict_dict2_userdef, "string"), "hello2"));
267     assert(!strcmp(qdict_get_str(ret_dict_dict2, "string"), "blah4"));
268     qobject_unref(ret);
269 
270     ret3 = qobject_to(QNum, do_qmp_dispatch(false,
271         "{ 'execute': 'guest-get-time', 'arguments': { 'a': 66 } }"));
272     g_assert(qnum_get_try_int(ret3, &val));
273     g_assert_cmpint(val, ==, 66);
274     qobject_unref(ret3);
275 }
276 
277 static void test_dispatch_cmd_deprecated(void)
278 {
279     const char *cmd = "{ 'execute': 'test-command-features1' }";
280     QDict *ret;
281 
282     memset(&compat_policy, 0, sizeof(compat_policy));
283 
284     /* accept */
285     ret = qobject_to(QDict, do_qmp_dispatch(false, cmd));
286     assert(ret && qdict_size(ret) == 0);
287     qobject_unref(ret);
288 
289     compat_policy.has_deprecated_input = true;
290     compat_policy.deprecated_input = COMPAT_POLICY_INPUT_ACCEPT;
291     ret = qobject_to(QDict, do_qmp_dispatch(false, cmd));
292     assert(ret && qdict_size(ret) == 0);
293     qobject_unref(ret);
294 
295     compat_policy.deprecated_input = COMPAT_POLICY_INPUT_REJECT;
296     do_qmp_dispatch_error(false, ERROR_CLASS_COMMAND_NOT_FOUND, cmd);
297 }
298 
299 static void test_dispatch_cmd_arg_deprecated(void)
300 {
301     const char *cmd = "{ 'execute': 'test-features0',"
302         " 'arguments': { 'fs1': { 'foo': 42 } } }";
303     QDict *ret;
304 
305     memset(&compat_policy, 0, sizeof(compat_policy));
306 
307     /* accept */
308     ret = qobject_to(QDict, do_qmp_dispatch(false, cmd));
309     assert(ret && qdict_size(ret) == 1);
310     qobject_unref(ret);
311 
312     compat_policy.has_deprecated_input = true;
313     compat_policy.deprecated_input = COMPAT_POLICY_INPUT_ACCEPT;
314     ret = qobject_to(QDict, do_qmp_dispatch(false, cmd));
315     assert(ret && qdict_size(ret) == 1);
316     qobject_unref(ret);
317 
318     compat_policy.deprecated_input = COMPAT_POLICY_INPUT_REJECT;
319     do_qmp_dispatch_error(false, ERROR_CLASS_GENERIC_ERROR, cmd);
320 }
321 
322 static void test_dispatch_cmd_ret_deprecated(void)
323 {
324     const char *cmd = "{ 'execute': 'test-features0' }";
325     QDict *ret;
326 
327     memset(&compat_policy, 0, sizeof(compat_policy));
328 
329     /* default accept */
330     ret = qobject_to(QDict, do_qmp_dispatch(false, cmd));
331     assert(ret && qdict_size(ret) == 1);
332     qobject_unref(ret);
333 
334     compat_policy.has_deprecated_output = true;
335     compat_policy.deprecated_output = COMPAT_POLICY_OUTPUT_ACCEPT;
336     ret = qobject_to(QDict, do_qmp_dispatch(false, cmd));
337     assert(ret && qdict_size(ret) == 1);
338     qobject_unref(ret);
339 
340     compat_policy.deprecated_output = COMPAT_POLICY_OUTPUT_HIDE;
341     ret = qobject_to(QDict, do_qmp_dispatch(false, cmd));
342     assert(ret && qdict_size(ret) == 0);
343     qobject_unref(ret);
344 }
345 
346 /* test generated dealloc functions for generated types */
347 static void test_dealloc_types(void)
348 {
349     UserDefOne *ud1test, *ud1a, *ud1b;
350     UserDefOneList *ud1list;
351 
352     ud1test = g_malloc0(sizeof(UserDefOne));
353     ud1test->integer = 42;
354     ud1test->string = g_strdup("hi there 42");
355 
356     qapi_free_UserDefOne(ud1test);
357 
358     ud1a = g_malloc0(sizeof(UserDefOne));
359     ud1a->integer = 43;
360     ud1a->string = g_strdup("hi there 43");
361 
362     ud1b = g_malloc0(sizeof(UserDefOne));
363     ud1b->integer = 44;
364     ud1b->string = g_strdup("hi there 44");
365 
366     ud1list = g_malloc0(sizeof(UserDefOneList));
367     ud1list->value = ud1a;
368     ud1list->next = g_malloc0(sizeof(UserDefOneList));
369     ud1list->next->value = ud1b;
370 
371     qapi_free_UserDefOneList(ud1list);
372 }
373 
374 /* test generated deallocation on an object whose construction was prematurely
375  * terminated due to an error */
376 static void test_dealloc_partial(void)
377 {
378     static const char text[] = "don't leak me";
379 
380     UserDefTwo *ud2 = NULL;
381     Error *err = NULL;
382 
383     /* create partial object */
384     {
385         QDict *ud2_dict;
386         Visitor *v;
387 
388         ud2_dict = qdict_new();
389         qdict_put_str(ud2_dict, "string0", text);
390 
391         v = qobject_input_visitor_new(QOBJECT(ud2_dict));
392         visit_type_UserDefTwo(v, NULL, &ud2, &err);
393         visit_free(v);
394         qobject_unref(ud2_dict);
395     }
396 
397     /* verify that visit_type_XXX() cleans up properly on error */
398     error_free_or_abort(&err);
399     assert(!ud2);
400 
401     /* Manually create a partial object, leaving ud2->dict1 at NULL */
402     ud2 = g_new0(UserDefTwo, 1);
403     ud2->string0 = g_strdup(text);
404 
405     /* tear down partial object */
406     qapi_free_UserDefTwo(ud2);
407 }
408 
409 
410 int main(int argc, char **argv)
411 {
412     g_test_init(&argc, &argv, NULL);
413 
414     g_test_add_func("/qmp/dispatch_cmd", test_dispatch_cmd);
415     g_test_add_func("/qmp/dispatch_cmd_oob", test_dispatch_cmd_oob);
416     g_test_add_func("/qmp/dispatch_cmd_failure", test_dispatch_cmd_failure);
417     g_test_add_func("/qmp/dispatch_cmd_io", test_dispatch_cmd_io);
418     g_test_add_func("/qmp/dispatch_cmd_success_response",
419                     test_dispatch_cmd_success_response);
420     g_test_add_func("/qmp/dispatch_cmd_deprecated",
421                     test_dispatch_cmd_deprecated);
422     g_test_add_func("/qmp/dispatch_cmd_arg_deprecated",
423                     test_dispatch_cmd_arg_deprecated);
424     g_test_add_func("/qmp/dispatch_cmd_ret_deprecated",
425                     test_dispatch_cmd_ret_deprecated);
426     g_test_add_func("/qmp/dealloc_types", test_dealloc_types);
427     g_test_add_func("/qmp/dealloc_partial", test_dealloc_partial);
428 
429     test_qmp_init_marshal(&qmp_commands);
430     g_test_run();
431 
432     return 0;
433 }
434