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