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"
11 static QmpCommandList qmp_commands
;
13 void qmp_user_def_cmd(Error
**errp
)
17 Empty2
*qmp_user_def_cmd0(Error
**errp
)
19 return g_new0(Empty2
, 1);
22 void qmp_user_def_cmd1(UserDefOne
* ud1
, Error
**errp
)
26 UserDefTwo
*qmp_user_def_cmd2(UserDefOne
*ud1a
,
27 bool has_udb1
, UserDefOne
*ud1b
,
31 UserDefOne
*ud1c
= g_malloc0(sizeof(UserDefOne
));
32 UserDefOne
*ud1d
= g_malloc0(sizeof(UserDefOne
));
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;
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");
54 int64_t qmp_guest_get_time(int64_t a
, bool has_b
, int64_t b
, Error
**errp
)
56 return a
+ (has_b
? b
: 0);
59 QObject
*qmp_guest_sync(QObject
*arg
, Error
**errp
)
64 void qmp_boxed_struct(UserDefZero
*arg
, Error
**errp
)
68 void qmp_boxed_union(UserDefNativeListUnion
*arg
, Error
**errp
)
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
,
78 __org_qemu_x_Union1
*ret
= g_new0(__org_qemu_x_Union1
, 1);
80 ret
->type
= ORG_QEMU_X_UNION1_KIND___ORG_QEMU_X_BRANCH
;
81 ret
->u
.__org_qemu_x_branch
.data
= strdup("blah1");
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;
91 /* test commands with no input and no return value */
92 static void test_dispatch_cmd(void)
94 QDict
*req
= qdict_new();
97 qdict_put_str(req
, "execute", "user_def_cmd");
99 resp
= qmp_dispatch(&qmp_commands
, QOBJECT(req
));
100 assert(resp
!= NULL
);
101 assert(!qdict_haskey(qobject_to_qdict(resp
), "error"));
103 qobject_decref(resp
);
107 /* test commands that return an error due to invalid parameters */
108 static void test_dispatch_cmd_failure(void)
110 QDict
*req
= qdict_new();
111 QDict
*args
= qdict_new();
114 qdict_put_str(req
, "execute", "user_def_cmd2");
116 resp
= qmp_dispatch(&qmp_commands
, QOBJECT(req
));
117 assert(resp
!= NULL
);
118 assert(qdict_haskey(qobject_to_qdict(resp
), "error"));
120 qobject_decref(resp
);
123 /* check that with extra arguments it throws an error */
125 qdict_put_int(args
, "a", 66);
126 qdict_put(req
, "arguments", args
);
128 qdict_put_str(req
, "execute", "user_def_cmd");
130 resp
= qmp_dispatch(&qmp_commands
, QOBJECT(req
));
131 assert(resp
!= NULL
);
132 assert(qdict_haskey(qobject_to_qdict(resp
), "error"));
134 qobject_decref(resp
);
138 static QObject
*test_qmp_dispatch(QDict
*req
)
144 resp_obj
= qmp_dispatch(&qmp_commands
, QOBJECT(req
));
146 resp
= qobject_to_qdict(resp_obj
);
147 assert(resp
&& !qdict_haskey(resp
, "error"));
148 ret
= qdict_get(resp
, "return");
151 qobject_decref(resp_obj
);
155 /* test commands that involve both input parameters and return values */
156 static void test_dispatch_cmd_io(void)
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
;
168 qdict_put_int(ud1a
, "integer", 42);
169 qdict_put_str(ud1a
, "string", "hello");
170 qdict_put_int(ud1b
, "integer", 422);
171 qdict_put_str(ud1b
, "string", "hello2");
172 qdict_put(args
, "ud1a", ud1a
);
173 qdict_put(args
, "ud1b", ud1b
);
174 qdict_put(req
, "arguments", args
);
175 qdict_put_str(req
, "execute", "user_def_cmd2");
177 ret
= qobject_to_qdict(test_qmp_dispatch(req
));
179 assert(!strcmp(qdict_get_str(ret
, "string0"), "blah1"));
180 ret_dict
= qdict_get_qdict(ret
, "dict1");
181 assert(!strcmp(qdict_get_str(ret_dict
, "string1"), "blah2"));
182 ret_dict_dict
= qdict_get_qdict(ret_dict
, "dict2");
183 ret_dict_dict_userdef
= qdict_get_qdict(ret_dict_dict
, "userdef");
184 assert(qdict_get_int(ret_dict_dict_userdef
, "integer") == 42);
185 assert(!strcmp(qdict_get_str(ret_dict_dict_userdef
, "string"), "hello"));
186 assert(!strcmp(qdict_get_str(ret_dict_dict
, "string"), "blah3"));
187 ret_dict_dict2
= qdict_get_qdict(ret_dict
, "dict3");
188 ret_dict_dict2_userdef
= qdict_get_qdict(ret_dict_dict2
, "userdef");
189 assert(qdict_get_int(ret_dict_dict2_userdef
, "integer") == 422);
190 assert(!strcmp(qdict_get_str(ret_dict_dict2_userdef
, "string"), "hello2"));
191 assert(!strcmp(qdict_get_str(ret_dict_dict2
, "string"), "blah4"));
194 qdict_put_int(args3
, "a", 66);
195 qdict_put(req
, "arguments", args3
);
196 qdict_put_str(req
, "execute", "guest-get-time");
198 ret3
= qobject_to_qnum(test_qmp_dispatch(req
));
199 g_assert(qnum_get_try_int(ret3
, &val
));
200 g_assert_cmpint(val
, ==, 66);
206 /* test generated dealloc functions for generated types */
207 static void test_dealloc_types(void)
209 UserDefOne
*ud1test
, *ud1a
, *ud1b
;
210 UserDefOneList
*ud1list
;
212 ud1test
= g_malloc0(sizeof(UserDefOne
));
213 ud1test
->integer
= 42;
214 ud1test
->string
= g_strdup("hi there 42");
216 qapi_free_UserDefOne(ud1test
);
218 ud1a
= g_malloc0(sizeof(UserDefOne
));
220 ud1a
->string
= g_strdup("hi there 43");
222 ud1b
= g_malloc0(sizeof(UserDefOne
));
224 ud1b
->string
= g_strdup("hi there 44");
226 ud1list
= g_malloc0(sizeof(UserDefOneList
));
227 ud1list
->value
= ud1a
;
228 ud1list
->next
= g_malloc0(sizeof(UserDefOneList
));
229 ud1list
->next
->value
= ud1b
;
231 qapi_free_UserDefOneList(ud1list
);
234 /* test generated deallocation on an object whose construction was prematurely
235 * terminated due to an error */
236 static void test_dealloc_partial(void)
238 static const char text
[] = "don't leak me";
240 UserDefTwo
*ud2
= NULL
;
243 /* create partial object */
248 ud2_dict
= qdict_new();
249 qdict_put_str(ud2_dict
, "string0", text
);
251 v
= qobject_input_visitor_new(QOBJECT(ud2_dict
));
252 visit_type_UserDefTwo(v
, NULL
, &ud2
, &err
);
257 /* verify that visit_type_XXX() cleans up properly on error */
258 error_free_or_abort(&err
);
261 /* Manually create a partial object, leaving ud2->dict1 at NULL */
262 ud2
= g_new0(UserDefTwo
, 1);
263 ud2
->string0
= g_strdup(text
);
265 /* tear down partial object */
266 qapi_free_UserDefTwo(ud2
);
270 int main(int argc
, char **argv
)
272 g_test_init(&argc
, &argv
, NULL
);
274 g_test_add_func("/0.15/dispatch_cmd", test_dispatch_cmd
);
275 g_test_add_func("/0.15/dispatch_cmd_failure", test_dispatch_cmd_failure
);
276 g_test_add_func("/0.15/dispatch_cmd_io", test_dispatch_cmd_io
);
277 g_test_add_func("/0.15/dealloc_types", test_dealloc_types
);
278 g_test_add_func("/0.15/dealloc_partial", test_dealloc_partial
);
280 test_qmp_init_marshal(&qmp_commands
);