xref: /openbmc/qemu/tests/unit/test-qmp-cmds.c (revision 5e437d3c)
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 #if defined(TEST_IF_STRUCT) && defined(TEST_IF_CMD)
17 UserDefThree *qmp_TestIfCmd(TestIfStruct *foo, Error **errp)
18 {
19     return NULL;
20 }
21 #endif
22 
23 UserDefThree *qmp_TestCmdReturnDefThree(Error **errp)
24 {
25     return NULL;
26 }
27 
28 void qmp_user_def_cmd(Error **errp)
29 {
30 }
31 
32 void qmp_test_flags_command(Error **errp)
33 {
34 }
35 
36 void qmp_cmd_success_response(Error **errp)
37 {
38 }
39 
40 void qmp_coroutine_cmd(Error **errp)
41 {
42 }
43 
44 Empty2 *qmp_user_def_cmd0(Error **errp)
45 {
46     return g_new0(Empty2, 1);
47 }
48 
49 void qmp_user_def_cmd1(UserDefOne * ud1, Error **errp)
50 {
51 }
52 
53 FeatureStruct1 *qmp_test_features0(bool has_fs0, FeatureStruct0 *fs0,
54                                    bool has_fs1, FeatureStruct1 *fs1,
55                                    bool has_fs2, FeatureStruct2 *fs2,
56                                    bool has_fs3, FeatureStruct3 *fs3,
57                                    bool has_fs4, FeatureStruct4 *fs4,
58                                    bool has_cfs1, CondFeatureStruct1 *cfs1,
59                                    bool has_cfs2, CondFeatureStruct2 *cfs2,
60                                    bool has_cfs3, CondFeatureStruct3 *cfs3,
61                                    Error **errp)
62 {
63     return g_new0(FeatureStruct1, 1);
64 }
65 
66 void qmp_test_command_features1(Error **errp)
67 {
68 }
69 
70 void qmp_test_command_features3(Error **errp)
71 {
72 }
73 
74 void qmp_test_command_cond_features1(Error **errp)
75 {
76 }
77 
78 void qmp_test_command_cond_features2(Error **errp)
79 {
80 }
81 
82 void qmp_test_command_cond_features3(Error **errp)
83 {
84 }
85 
86 UserDefTwo *qmp_user_def_cmd2(UserDefOne *ud1a,
87                               bool has_udb1, UserDefOne *ud1b,
88                               Error **errp)
89 {
90     UserDefTwo *ret;
91     UserDefOne *ud1c = g_malloc0(sizeof(UserDefOne));
92     UserDefOne *ud1d = g_malloc0(sizeof(UserDefOne));
93 
94     ud1c->string = strdup(ud1a->string);
95     ud1c->integer = ud1a->integer;
96     ud1d->string = strdup(has_udb1 ? ud1b->string : "blah0");
97     ud1d->integer = has_udb1 ? ud1b->integer : 0;
98 
99     ret = g_new0(UserDefTwo, 1);
100     ret->string0 = strdup("blah1");
101     ret->dict1 = g_new0(UserDefTwoDict, 1);
102     ret->dict1->string1 = strdup("blah2");
103     ret->dict1->dict2 = g_new0(UserDefTwoDictDict, 1);
104     ret->dict1->dict2->userdef = ud1c;
105     ret->dict1->dict2->string = strdup("blah3");
106     ret->dict1->dict3 = g_new0(UserDefTwoDictDict, 1);
107     ret->dict1->has_dict3 = true;
108     ret->dict1->dict3->userdef = ud1d;
109     ret->dict1->dict3->string = strdup("blah4");
110 
111     return ret;
112 }
113 
114 int64_t qmp_guest_get_time(int64_t a, bool has_b, int64_t b, Error **errp)
115 {
116     return a + (has_b ? b : 0);
117 }
118 
119 QObject *qmp_guest_sync(QObject *arg, Error **errp)
120 {
121     return arg;
122 }
123 
124 void qmp_boxed_struct(UserDefZero *arg, Error **errp)
125 {
126 }
127 
128 void qmp_boxed_union(UserDefListUnion *arg, Error **errp)
129 {
130 }
131 
132 void qmp_boxed_empty(Empty1 *arg, Error **errp)
133 {
134 }
135 
136 __org_qemu_x_Union1 *qmp___org_qemu_x_command(__org_qemu_x_EnumList *a,
137                                               __org_qemu_x_StructList *b,
138                                               __org_qemu_x_Union2 *c,
139                                               __org_qemu_x_Alt *d,
140                                               Error **errp)
141 {
142     __org_qemu_x_Union1 *ret = g_new0(__org_qemu_x_Union1, 1);
143 
144     ret->type = ORG_QEMU_X_UNION1_KIND___ORG_QEMU_X_BRANCH;
145     ret->u.__org_qemu_x_branch.data = strdup("blah1");
146 
147     /* Also test that 'wchar-t' was munged to 'q_wchar_t' */
148     if (b && b->value && !b->value->has_q_wchar_t) {
149         b->value->q_wchar_t = 1;
150     }
151     return ret;
152 }
153 
154 
155 static QObject *do_qmp_dispatch(bool allow_oob, const char *template, ...)
156 {
157     va_list ap;
158     QDict *req, *resp;
159     QObject *ret;
160 
161     va_start(ap, template);
162     req = qdict_from_vjsonf_nofail(template, ap);
163     va_end(ap);
164 
165     resp = qmp_dispatch(&qmp_commands, QOBJECT(req), allow_oob, NULL);
166     g_assert(resp);
167     ret = qdict_get(resp, "return");
168     g_assert(ret);
169     g_assert(qdict_size(resp) == 1);
170 
171     qobject_ref(ret);
172     qobject_unref(resp);
173     qobject_unref(req);
174     return ret;
175 }
176 
177 static void do_qmp_dispatch_error(bool allow_oob, ErrorClass cls,
178                                   const char *template, ...)
179 {
180     va_list ap;
181     QDict *req, *resp;
182     QDict *error;
183 
184     va_start(ap, template);
185     req = qdict_from_vjsonf_nofail(template, ap);
186     va_end(ap);
187 
188     resp = qmp_dispatch(&qmp_commands, QOBJECT(req), allow_oob, NULL);
189     g_assert(resp);
190     error = qdict_get_qdict(resp, "error");
191     g_assert(error);
192     g_assert_cmpstr(qdict_get_try_str(error, "class"),
193                     ==, QapiErrorClass_str(cls));
194     g_assert(qdict_get_try_str(error, "desc"));
195     g_assert(qdict_size(error) == 2);
196     g_assert(qdict_size(resp) == 1);
197 
198     qobject_unref(resp);
199     qobject_unref(req);
200 }
201 
202 /* test commands with no input and no return value */
203 static void test_dispatch_cmd(void)
204 {
205     QDict *ret;
206 
207     ret = qobject_to(QDict,
208                      do_qmp_dispatch(false,
209                                      "{ 'execute': 'user_def_cmd' }"));
210     assert(ret && qdict_size(ret) == 0);
211     qobject_unref(ret);
212 }
213 
214 static void test_dispatch_cmd_oob(void)
215 {
216     QDict *ret;
217 
218     ret = qobject_to(QDict,
219                      do_qmp_dispatch(true,
220                                      "{ 'exec-oob': 'test-flags-command' }"));
221     assert(ret && qdict_size(ret) == 0);
222     qobject_unref(ret);
223 }
224 
225 /* test commands that return an error due to invalid parameters */
226 static void test_dispatch_cmd_failure(void)
227 {
228     /* missing arguments */
229     do_qmp_dispatch_error(false, ERROR_CLASS_GENERIC_ERROR,
230                           "{ 'execute': 'user_def_cmd2' }");
231 
232     /* extra arguments */
233     do_qmp_dispatch_error(false, ERROR_CLASS_GENERIC_ERROR,
234                           "{ 'execute': 'user_def_cmd',"
235                           " 'arguments': { 'a': 66 } }");
236 }
237 
238 static void test_dispatch_cmd_success_response(void)
239 {
240     QDict *req = qdict_new();
241     QDict *resp;
242 
243     qdict_put_str(req, "execute", "cmd-success-response");
244     resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false, NULL);
245     g_assert_null(resp);
246     qobject_unref(req);
247 }
248 
249 /* test commands that involve both input parameters and return values */
250 static void test_dispatch_cmd_io(void)
251 {
252     QDict *ret, *ret_dict, *ret_dict_dict, *ret_dict_dict_userdef;
253     QDict *ret_dict_dict2, *ret_dict_dict2_userdef;
254     QNum *ret3;
255     int64_t val;
256 
257     ret = qobject_to(QDict, do_qmp_dispatch(false,
258         "{ 'execute': 'user_def_cmd2', 'arguments': {"
259         " 'ud1a': { 'integer': 42, 'string': 'hello' },"
260         " 'ud1b': { 'integer': 422, 'string': 'hello2' } } }"));
261 
262     assert(!strcmp(qdict_get_str(ret, "string0"), "blah1"));
263     ret_dict = qdict_get_qdict(ret, "dict1");
264     assert(!strcmp(qdict_get_str(ret_dict, "string1"), "blah2"));
265     ret_dict_dict = qdict_get_qdict(ret_dict, "dict2");
266     ret_dict_dict_userdef = qdict_get_qdict(ret_dict_dict, "userdef");
267     assert(qdict_get_int(ret_dict_dict_userdef, "integer") == 42);
268     assert(!strcmp(qdict_get_str(ret_dict_dict_userdef, "string"), "hello"));
269     assert(!strcmp(qdict_get_str(ret_dict_dict, "string"), "blah3"));
270     ret_dict_dict2 = qdict_get_qdict(ret_dict, "dict3");
271     ret_dict_dict2_userdef = qdict_get_qdict(ret_dict_dict2, "userdef");
272     assert(qdict_get_int(ret_dict_dict2_userdef, "integer") == 422);
273     assert(!strcmp(qdict_get_str(ret_dict_dict2_userdef, "string"), "hello2"));
274     assert(!strcmp(qdict_get_str(ret_dict_dict2, "string"), "blah4"));
275     qobject_unref(ret);
276 
277     ret3 = qobject_to(QNum, do_qmp_dispatch(false,
278         "{ 'execute': 'guest-get-time', 'arguments': { 'a': 66 } }"));
279     g_assert(qnum_get_try_int(ret3, &val));
280     g_assert_cmpint(val, ==, 66);
281     qobject_unref(ret3);
282 }
283 
284 static void test_dispatch_cmd_deprecated(void)
285 {
286     const char *cmd = "{ 'execute': 'test-command-features1' }";
287     QDict *ret;
288 
289     memset(&compat_policy, 0, sizeof(compat_policy));
290 
291     /* accept */
292     ret = qobject_to(QDict, do_qmp_dispatch(false, cmd));
293     assert(ret && qdict_size(ret) == 0);
294     qobject_unref(ret);
295 
296     compat_policy.has_deprecated_input = true;
297     compat_policy.deprecated_input = COMPAT_POLICY_INPUT_ACCEPT;
298     ret = qobject_to(QDict, do_qmp_dispatch(false, cmd));
299     assert(ret && qdict_size(ret) == 0);
300     qobject_unref(ret);
301 
302     compat_policy.deprecated_input = COMPAT_POLICY_INPUT_REJECT;
303     do_qmp_dispatch_error(false, ERROR_CLASS_COMMAND_NOT_FOUND, cmd);
304 }
305 
306 static void test_dispatch_cmd_arg_deprecated(void)
307 {
308     const char *cmd = "{ 'execute': 'test-features0',"
309         " 'arguments': { 'fs1': { 'foo': 42 } } }";
310     QDict *ret;
311 
312     memset(&compat_policy, 0, sizeof(compat_policy));
313 
314     /* accept */
315     ret = qobject_to(QDict, do_qmp_dispatch(false, cmd));
316     assert(ret && qdict_size(ret) == 1);
317     qobject_unref(ret);
318 
319     compat_policy.has_deprecated_input = true;
320     compat_policy.deprecated_input = COMPAT_POLICY_INPUT_ACCEPT;
321     ret = qobject_to(QDict, do_qmp_dispatch(false, cmd));
322     assert(ret && qdict_size(ret) == 1);
323     qobject_unref(ret);
324 
325     compat_policy.deprecated_input = COMPAT_POLICY_INPUT_REJECT;
326     do_qmp_dispatch_error(false, ERROR_CLASS_GENERIC_ERROR, cmd);
327 }
328 
329 static void test_dispatch_cmd_ret_deprecated(void)
330 {
331     const char *cmd = "{ 'execute': 'test-features0' }";
332     QDict *ret;
333 
334     memset(&compat_policy, 0, sizeof(compat_policy));
335 
336     /* default accept */
337     ret = qobject_to(QDict, do_qmp_dispatch(false, cmd));
338     assert(ret && qdict_size(ret) == 1);
339     qobject_unref(ret);
340 
341     compat_policy.has_deprecated_output = true;
342     compat_policy.deprecated_output = COMPAT_POLICY_OUTPUT_ACCEPT;
343     ret = qobject_to(QDict, do_qmp_dispatch(false, cmd));
344     assert(ret && qdict_size(ret) == 1);
345     qobject_unref(ret);
346 
347     compat_policy.deprecated_output = COMPAT_POLICY_OUTPUT_HIDE;
348     ret = qobject_to(QDict, do_qmp_dispatch(false, cmd));
349     assert(ret && qdict_size(ret) == 0);
350     qobject_unref(ret);
351 }
352 
353 /* test generated dealloc functions for generated types */
354 static void test_dealloc_types(void)
355 {
356     UserDefOne *ud1test, *ud1a, *ud1b;
357     UserDefOneList *ud1list;
358 
359     ud1test = g_malloc0(sizeof(UserDefOne));
360     ud1test->integer = 42;
361     ud1test->string = g_strdup("hi there 42");
362 
363     qapi_free_UserDefOne(ud1test);
364 
365     ud1a = g_malloc0(sizeof(UserDefOne));
366     ud1a->integer = 43;
367     ud1a->string = g_strdup("hi there 43");
368 
369     ud1b = g_malloc0(sizeof(UserDefOne));
370     ud1b->integer = 44;
371     ud1b->string = g_strdup("hi there 44");
372 
373     ud1list = g_malloc0(sizeof(UserDefOneList));
374     ud1list->value = ud1a;
375     ud1list->next = g_malloc0(sizeof(UserDefOneList));
376     ud1list->next->value = ud1b;
377 
378     qapi_free_UserDefOneList(ud1list);
379 }
380 
381 /* test generated deallocation on an object whose construction was prematurely
382  * terminated due to an error */
383 static void test_dealloc_partial(void)
384 {
385     static const char text[] = "don't leak me";
386 
387     UserDefTwo *ud2 = NULL;
388     Error *err = NULL;
389 
390     /* create partial object */
391     {
392         QDict *ud2_dict;
393         Visitor *v;
394 
395         ud2_dict = qdict_new();
396         qdict_put_str(ud2_dict, "string0", text);
397 
398         v = qobject_input_visitor_new(QOBJECT(ud2_dict));
399         visit_type_UserDefTwo(v, NULL, &ud2, &err);
400         visit_free(v);
401         qobject_unref(ud2_dict);
402     }
403 
404     /* verify that visit_type_XXX() cleans up properly on error */
405     error_free_or_abort(&err);
406     assert(!ud2);
407 
408     /* Manually create a partial object, leaving ud2->dict1 at NULL */
409     ud2 = g_new0(UserDefTwo, 1);
410     ud2->string0 = g_strdup(text);
411 
412     /* tear down partial object */
413     qapi_free_UserDefTwo(ud2);
414 }
415 
416 
417 int main(int argc, char **argv)
418 {
419     g_test_init(&argc, &argv, NULL);
420 
421     g_test_add_func("/qmp/dispatch_cmd", test_dispatch_cmd);
422     g_test_add_func("/qmp/dispatch_cmd_oob", test_dispatch_cmd_oob);
423     g_test_add_func("/qmp/dispatch_cmd_failure", test_dispatch_cmd_failure);
424     g_test_add_func("/qmp/dispatch_cmd_io", test_dispatch_cmd_io);
425     g_test_add_func("/qmp/dispatch_cmd_success_response",
426                     test_dispatch_cmd_success_response);
427     g_test_add_func("/qmp/dispatch_cmd_deprecated",
428                     test_dispatch_cmd_deprecated);
429     g_test_add_func("/qmp/dispatch_cmd_arg_deprecated",
430                     test_dispatch_cmd_arg_deprecated);
431     g_test_add_func("/qmp/dispatch_cmd_ret_deprecated",
432                     test_dispatch_cmd_ret_deprecated);
433     g_test_add_func("/qmp/dealloc_types", test_dealloc_types);
434     g_test_add_func("/qmp/dealloc_partial", test_dealloc_partial);
435 
436     test_qmp_init_marshal(&qmp_commands);
437     g_test_run();
438 
439     return 0;
440 }
441