migration/vmstate: fix array of ptr with nullptrs
[qemu.git] / tests / test-qmp-commands.c
blobff944811e8c0828226196d1dcef82e5f632f4fd4
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 void qmp_user_def_cmd(Error **errp)
15 Empty2 *qmp_user_def_cmd0(Error **errp)
17 return g_new0(Empty2, 1);
20 void qmp_user_def_cmd1(UserDefOne * ud1, Error **errp)
24 UserDefTwo *qmp_user_def_cmd2(UserDefOne *ud1a,
25 bool has_udb1, UserDefOne *ud1b,
26 Error **errp)
28 UserDefTwo *ret;
29 UserDefOne *ud1c = g_malloc0(sizeof(UserDefOne));
30 UserDefOne *ud1d = g_malloc0(sizeof(UserDefOne));
32 ud1c->string = strdup(ud1a->string);
33 ud1c->integer = ud1a->integer;
34 ud1d->string = strdup(has_udb1 ? ud1b->string : "blah0");
35 ud1d->integer = has_udb1 ? ud1b->integer : 0;
37 ret = g_new0(UserDefTwo, 1);
38 ret->string0 = strdup("blah1");
39 ret->dict1 = g_new0(UserDefTwoDict, 1);
40 ret->dict1->string1 = strdup("blah2");
41 ret->dict1->dict2 = g_new0(UserDefTwoDictDict, 1);
42 ret->dict1->dict2->userdef = ud1c;
43 ret->dict1->dict2->string = strdup("blah3");
44 ret->dict1->dict3 = g_new0(UserDefTwoDictDict, 1);
45 ret->dict1->has_dict3 = true;
46 ret->dict1->dict3->userdef = ud1d;
47 ret->dict1->dict3->string = strdup("blah4");
49 return ret;
52 int64_t qmp_guest_get_time(int64_t a, bool has_b, int64_t b, Error **errp)
54 return a + (has_b ? b : 0);
57 QObject *qmp_guest_sync(QObject *arg, Error **errp)
59 return arg;
62 void qmp_boxed_struct(UserDefZero *arg, Error **errp)
66 void qmp_boxed_union(UserDefNativeListUnion *arg, Error **errp)
70 __org_qemu_x_Union1 *qmp___org_qemu_x_command(__org_qemu_x_EnumList *a,
71 __org_qemu_x_StructList *b,
72 __org_qemu_x_Union2 *c,
73 __org_qemu_x_Alt *d,
74 Error **errp)
76 __org_qemu_x_Union1 *ret = g_new0(__org_qemu_x_Union1, 1);
78 ret->type = ORG_QEMU_X_UNION1_KIND___ORG_QEMU_X_BRANCH;
79 ret->u.__org_qemu_x_branch.data = strdup("blah1");
81 /* Also test that 'wchar-t' was munged to 'q_wchar_t' */
82 if (b && b->value && !b->value->has_q_wchar_t) {
83 b->value->q_wchar_t = 1;
85 return ret;
89 /* test commands with no input and no return value */
90 static void test_dispatch_cmd(void)
92 QDict *req = qdict_new();
93 QObject *resp;
95 qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd")));
97 resp = qmp_dispatch(QOBJECT(req));
98 assert(resp != NULL);
99 assert(!qdict_haskey(qobject_to_qdict(resp), "error"));
101 qobject_decref(resp);
102 QDECREF(req);
105 /* test commands that return an error due to invalid parameters */
106 static void test_dispatch_cmd_failure(void)
108 QDict *req = qdict_new();
109 QDict *args = qdict_new();
110 QObject *resp;
112 qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd2")));
114 resp = qmp_dispatch(QOBJECT(req));
115 assert(resp != NULL);
116 assert(qdict_haskey(qobject_to_qdict(resp), "error"));
118 qobject_decref(resp);
119 QDECREF(req);
121 /* check that with extra arguments it throws an error */
122 req = qdict_new();
123 qdict_put(args, "a", qint_from_int(66));
124 qdict_put(req, "arguments", args);
126 qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd")));
128 resp = qmp_dispatch(QOBJECT(req));
129 assert(resp != NULL);
130 assert(qdict_haskey(qobject_to_qdict(resp), "error"));
132 qobject_decref(resp);
133 QDECREF(req);
136 static QObject *test_qmp_dispatch(QDict *req)
138 QObject *resp_obj;
139 QDict *resp;
140 QObject *ret;
142 resp_obj = qmp_dispatch(QOBJECT(req));
143 assert(resp_obj);
144 resp = qobject_to_qdict(resp_obj);
145 assert(resp && !qdict_haskey(resp, "error"));
146 ret = qdict_get(resp, "return");
147 assert(ret);
148 qobject_incref(ret);
149 qobject_decref(resp_obj);
150 return ret;
153 /* test commands that involve both input parameters and return values */
154 static void test_dispatch_cmd_io(void)
156 QDict *req = qdict_new();
157 QDict *args = qdict_new();
158 QDict *args3 = qdict_new();
159 QDict *ud1a = qdict_new();
160 QDict *ud1b = qdict_new();
161 QDict *ret, *ret_dict, *ret_dict_dict, *ret_dict_dict_userdef;
162 QDict *ret_dict_dict2, *ret_dict_dict2_userdef;
163 QInt *ret3;
165 qdict_put_obj(ud1a, "integer", QOBJECT(qint_from_int(42)));
166 qdict_put_obj(ud1a, "string", QOBJECT(qstring_from_str("hello")));
167 qdict_put_obj(ud1b, "integer", QOBJECT(qint_from_int(422)));
168 qdict_put_obj(ud1b, "string", QOBJECT(qstring_from_str("hello2")));
169 qdict_put_obj(args, "ud1a", QOBJECT(ud1a));
170 qdict_put_obj(args, "ud1b", QOBJECT(ud1b));
171 qdict_put_obj(req, "arguments", QOBJECT(args));
172 qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd2")));
174 ret = qobject_to_qdict(test_qmp_dispatch(req));
176 assert(!strcmp(qdict_get_str(ret, "string0"), "blah1"));
177 ret_dict = qdict_get_qdict(ret, "dict1");
178 assert(!strcmp(qdict_get_str(ret_dict, "string1"), "blah2"));
179 ret_dict_dict = qdict_get_qdict(ret_dict, "dict2");
180 ret_dict_dict_userdef = qdict_get_qdict(ret_dict_dict, "userdef");
181 assert(qdict_get_int(ret_dict_dict_userdef, "integer") == 42);
182 assert(!strcmp(qdict_get_str(ret_dict_dict_userdef, "string"), "hello"));
183 assert(!strcmp(qdict_get_str(ret_dict_dict, "string"), "blah3"));
184 ret_dict_dict2 = qdict_get_qdict(ret_dict, "dict3");
185 ret_dict_dict2_userdef = qdict_get_qdict(ret_dict_dict2, "userdef");
186 assert(qdict_get_int(ret_dict_dict2_userdef, "integer") == 422);
187 assert(!strcmp(qdict_get_str(ret_dict_dict2_userdef, "string"), "hello2"));
188 assert(!strcmp(qdict_get_str(ret_dict_dict2, "string"), "blah4"));
189 QDECREF(ret);
191 qdict_put(args3, "a", qint_from_int(66));
192 qdict_put(req, "arguments", args3);
193 qdict_put(req, "execute", qstring_from_str("guest-get-time"));
195 ret3 = qobject_to_qint(test_qmp_dispatch(req));
196 assert(qint_get_int(ret3) == 66);
197 QDECREF(ret3);
199 QDECREF(req);
202 /* test generated dealloc functions for generated types */
203 static void test_dealloc_types(void)
205 UserDefOne *ud1test, *ud1a, *ud1b;
206 UserDefOneList *ud1list;
208 ud1test = g_malloc0(sizeof(UserDefOne));
209 ud1test->integer = 42;
210 ud1test->string = g_strdup("hi there 42");
212 qapi_free_UserDefOne(ud1test);
214 ud1a = g_malloc0(sizeof(UserDefOne));
215 ud1a->integer = 43;
216 ud1a->string = g_strdup("hi there 43");
218 ud1b = g_malloc0(sizeof(UserDefOne));
219 ud1b->integer = 44;
220 ud1b->string = g_strdup("hi there 44");
222 ud1list = g_malloc0(sizeof(UserDefOneList));
223 ud1list->value = ud1a;
224 ud1list->next = g_malloc0(sizeof(UserDefOneList));
225 ud1list->next->value = ud1b;
227 qapi_free_UserDefOneList(ud1list);
230 /* test generated deallocation on an object whose construction was prematurely
231 * terminated due to an error */
232 static void test_dealloc_partial(void)
234 static const char text[] = "don't leak me";
236 UserDefTwo *ud2 = NULL;
237 Error *err = NULL;
239 /* create partial object */
241 QDict *ud2_dict;
242 Visitor *v;
244 ud2_dict = qdict_new();
245 qdict_put_obj(ud2_dict, "string0", QOBJECT(qstring_from_str(text)));
247 v = qobject_input_visitor_new(QOBJECT(ud2_dict), true);
248 visit_type_UserDefTwo(v, NULL, &ud2, &err);
249 visit_free(v);
250 QDECREF(ud2_dict);
253 /* verify that visit_type_XXX() cleans up properly on error */
254 error_free_or_abort(&err);
255 assert(!ud2);
257 /* Manually create a partial object, leaving ud2->dict1 at NULL */
258 ud2 = g_new0(UserDefTwo, 1);
259 ud2->string0 = g_strdup(text);
261 /* tear down partial object */
262 qapi_free_UserDefTwo(ud2);
266 int main(int argc, char **argv)
268 g_test_init(&argc, &argv, NULL);
270 g_test_add_func("/0.15/dispatch_cmd", test_dispatch_cmd);
271 g_test_add_func("/0.15/dispatch_cmd_failure", test_dispatch_cmd_failure);
272 g_test_add_func("/0.15/dispatch_cmd_io", test_dispatch_cmd_io);
273 g_test_add_func("/0.15/dealloc_types", test_dealloc_types);
274 g_test_add_func("/0.15/dealloc_partial", test_dealloc_partial);
276 module_call_init(MODULE_INIT_QAPI);
277 g_test_run();
279 return 0;