1 #include "qemu/osdep.h"
2 #include "qemu-common.h"
3 #include "qapi/qmp/qdict.h"
4 #include "qapi/qmp/qnum.h"
5 #include "qapi/qmp/qstring.h"
6 #include "qapi/error.h"
7 #include "qemu/module.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"
13 static QmpCommandList qmp_commands
;
15 void qmp_user_def_cmd(Error
**errp
)
19 void qmp_test_flags_command(Error
**errp
)
23 Empty2
*qmp_user_def_cmd0(Error
**errp
)
25 return g_new0(Empty2
, 1);
28 void qmp_user_def_cmd1(UserDefOne
* ud1
, Error
**errp
)
32 UserDefTwo
*qmp_user_def_cmd2(UserDefOne
*ud1a
,
33 bool has_udb1
, UserDefOne
*ud1b
,
37 UserDefOne
*ud1c
= g_malloc0(sizeof(UserDefOne
));
38 UserDefOne
*ud1d
= g_malloc0(sizeof(UserDefOne
));
40 ud1c
->string
= strdup(ud1a
->string
);
41 ud1c
->integer
= ud1a
->integer
;
42 ud1d
->string
= strdup(has_udb1
? ud1b
->string
: "blah0");
43 ud1d
->integer
= has_udb1
? ud1b
->integer
: 0;
45 ret
= g_new0(UserDefTwo
, 1);
46 ret
->string0
= strdup("blah1");
47 ret
->dict1
= g_new0(UserDefTwoDict
, 1);
48 ret
->dict1
->string1
= strdup("blah2");
49 ret
->dict1
->dict2
= g_new0(UserDefTwoDictDict
, 1);
50 ret
->dict1
->dict2
->userdef
= ud1c
;
51 ret
->dict1
->dict2
->string
= strdup("blah3");
52 ret
->dict1
->dict3
= g_new0(UserDefTwoDictDict
, 1);
53 ret
->dict1
->has_dict3
= true;
54 ret
->dict1
->dict3
->userdef
= ud1d
;
55 ret
->dict1
->dict3
->string
= strdup("blah4");
60 int64_t qmp_guest_get_time(int64_t a
, bool has_b
, int64_t b
, Error
**errp
)
62 return a
+ (has_b
? b
: 0);
65 QObject
*qmp_guest_sync(QObject
*arg
, Error
**errp
)
70 void qmp_boxed_struct(UserDefZero
*arg
, Error
**errp
)
74 void qmp_boxed_union(UserDefNativeListUnion
*arg
, Error
**errp
)
78 __org_qemu_x_Union1
*qmp___org_qemu_x_command(__org_qemu_x_EnumList
*a
,
79 __org_qemu_x_StructList
*b
,
80 __org_qemu_x_Union2
*c
,
84 __org_qemu_x_Union1
*ret
= g_new0(__org_qemu_x_Union1
, 1);
86 ret
->type
= ORG_QEMU_X_UNION1_KIND___ORG_QEMU_X_BRANCH
;
87 ret
->u
.__org_qemu_x_branch
.data
= strdup("blah1");
89 /* Also test that 'wchar-t' was munged to 'q_wchar_t' */
90 if (b
&& b
->value
&& !b
->value
->has_q_wchar_t
) {
91 b
->value
->q_wchar_t
= 1;
97 /* test commands with no input and no return value */
98 static void test_dispatch_cmd(void)
100 QDict
*req
= qdict_new();
103 qdict_put_str(req
, "execute", "user_def_cmd");
105 resp
= qmp_dispatch(&qmp_commands
, QOBJECT(req
));
106 assert(resp
!= NULL
);
107 assert(!qdict_haskey(qobject_to(QDict
, resp
), "error"));
113 /* test commands that return an error due to invalid parameters */
114 static void test_dispatch_cmd_failure(void)
116 QDict
*req
= qdict_new();
117 QDict
*args
= qdict_new();
120 qdict_put_str(req
, "execute", "user_def_cmd2");
122 resp
= qmp_dispatch(&qmp_commands
, QOBJECT(req
));
123 assert(resp
!= NULL
);
124 assert(qdict_haskey(qobject_to(QDict
, resp
), "error"));
129 /* check that with extra arguments it throws an error */
131 qdict_put_int(args
, "a", 66);
132 qdict_put(req
, "arguments", args
);
134 qdict_put_str(req
, "execute", "user_def_cmd");
136 resp
= qmp_dispatch(&qmp_commands
, QOBJECT(req
));
137 assert(resp
!= NULL
);
138 assert(qdict_haskey(qobject_to(QDict
, resp
), "error"));
144 static QObject
*test_qmp_dispatch(QDict
*req
)
150 resp_obj
= qmp_dispatch(&qmp_commands
, QOBJECT(req
));
152 resp
= qobject_to(QDict
, resp_obj
);
153 assert(resp
&& !qdict_haskey(resp
, "error"));
154 ret
= qdict_get(resp
, "return");
157 qobject_unref(resp_obj
);
161 /* test commands that involve both input parameters and return values */
162 static void test_dispatch_cmd_io(void)
164 QDict
*req
= qdict_new();
165 QDict
*args
= qdict_new();
166 QDict
*args3
= qdict_new();
167 QDict
*ud1a
= qdict_new();
168 QDict
*ud1b
= qdict_new();
169 QDict
*ret
, *ret_dict
, *ret_dict_dict
, *ret_dict_dict_userdef
;
170 QDict
*ret_dict_dict2
, *ret_dict_dict2_userdef
;
174 qdict_put_int(ud1a
, "integer", 42);
175 qdict_put_str(ud1a
, "string", "hello");
176 qdict_put_int(ud1b
, "integer", 422);
177 qdict_put_str(ud1b
, "string", "hello2");
178 qdict_put(args
, "ud1a", ud1a
);
179 qdict_put(args
, "ud1b", ud1b
);
180 qdict_put(req
, "arguments", args
);
181 qdict_put_str(req
, "execute", "user_def_cmd2");
183 ret
= qobject_to(QDict
, test_qmp_dispatch(req
));
185 assert(!strcmp(qdict_get_str(ret
, "string0"), "blah1"));
186 ret_dict
= qdict_get_qdict(ret
, "dict1");
187 assert(!strcmp(qdict_get_str(ret_dict
, "string1"), "blah2"));
188 ret_dict_dict
= qdict_get_qdict(ret_dict
, "dict2");
189 ret_dict_dict_userdef
= qdict_get_qdict(ret_dict_dict
, "userdef");
190 assert(qdict_get_int(ret_dict_dict_userdef
, "integer") == 42);
191 assert(!strcmp(qdict_get_str(ret_dict_dict_userdef
, "string"), "hello"));
192 assert(!strcmp(qdict_get_str(ret_dict_dict
, "string"), "blah3"));
193 ret_dict_dict2
= qdict_get_qdict(ret_dict
, "dict3");
194 ret_dict_dict2_userdef
= qdict_get_qdict(ret_dict_dict2
, "userdef");
195 assert(qdict_get_int(ret_dict_dict2_userdef
, "integer") == 422);
196 assert(!strcmp(qdict_get_str(ret_dict_dict2_userdef
, "string"), "hello2"));
197 assert(!strcmp(qdict_get_str(ret_dict_dict2
, "string"), "blah4"));
200 qdict_put_int(args3
, "a", 66);
201 qdict_put(req
, "arguments", args3
);
202 qdict_put_str(req
, "execute", "guest-get-time");
204 ret3
= qobject_to(QNum
, test_qmp_dispatch(req
));
205 g_assert(qnum_get_try_int(ret3
, &val
));
206 g_assert_cmpint(val
, ==, 66);
212 /* test generated dealloc functions for generated types */
213 static void test_dealloc_types(void)
215 UserDefOne
*ud1test
, *ud1a
, *ud1b
;
216 UserDefOneList
*ud1list
;
218 ud1test
= g_malloc0(sizeof(UserDefOne
));
219 ud1test
->integer
= 42;
220 ud1test
->string
= g_strdup("hi there 42");
222 qapi_free_UserDefOne(ud1test
);
224 ud1a
= g_malloc0(sizeof(UserDefOne
));
226 ud1a
->string
= g_strdup("hi there 43");
228 ud1b
= g_malloc0(sizeof(UserDefOne
));
230 ud1b
->string
= g_strdup("hi there 44");
232 ud1list
= g_malloc0(sizeof(UserDefOneList
));
233 ud1list
->value
= ud1a
;
234 ud1list
->next
= g_malloc0(sizeof(UserDefOneList
));
235 ud1list
->next
->value
= ud1b
;
237 qapi_free_UserDefOneList(ud1list
);
240 /* test generated deallocation on an object whose construction was prematurely
241 * terminated due to an error */
242 static void test_dealloc_partial(void)
244 static const char text
[] = "don't leak me";
246 UserDefTwo
*ud2
= NULL
;
249 /* create partial object */
254 ud2_dict
= qdict_new();
255 qdict_put_str(ud2_dict
, "string0", text
);
257 v
= qobject_input_visitor_new(QOBJECT(ud2_dict
));
258 visit_type_UserDefTwo(v
, NULL
, &ud2
, &err
);
260 qobject_unref(ud2_dict
);
263 /* verify that visit_type_XXX() cleans up properly on error */
264 error_free_or_abort(&err
);
267 /* Manually create a partial object, leaving ud2->dict1 at NULL */
268 ud2
= g_new0(UserDefTwo
, 1);
269 ud2
->string0
= g_strdup(text
);
271 /* tear down partial object */
272 qapi_free_UserDefTwo(ud2
);
276 int main(int argc
, char **argv
)
278 g_test_init(&argc
, &argv
, NULL
);
280 g_test_add_func("/0.15/dispatch_cmd", test_dispatch_cmd
);
281 g_test_add_func("/0.15/dispatch_cmd_failure", test_dispatch_cmd_failure
);
282 g_test_add_func("/0.15/dispatch_cmd_io", test_dispatch_cmd_io
);
283 g_test_add_func("/0.15/dealloc_types", test_dealloc_types
);
284 g_test_add_func("/0.15/dealloc_partial", test_dealloc_partial
);
286 test_qmp_init_marshal(&qmp_commands
);