s390x/css: introduce read-only property type for device ids
[qemu.git] / tests / test-qmp-commands.c
blob0f81a98be297de378e90121d23c72821f0a2019a
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,
28 Error **errp)
30 UserDefTwo *ret;
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");
51 return ret;
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)
61 return arg;
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,
75 __org_qemu_x_Alt *d,
76 Error **errp)
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;
87 return ret;
91 /* test commands with no input and no return value */
92 static void test_dispatch_cmd(void)
94 QDict *req = qdict_new();
95 QObject *resp;
97 qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("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);
104 QDECREF(req);
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();
112 QObject *resp;
114 qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("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);
121 QDECREF(req);
123 /* check that with extra arguments it throws an error */
124 req = qdict_new();
125 qdict_put(args, "a", qint_from_int(66));
126 qdict_put(req, "arguments", args);
128 qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("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);
135 QDECREF(req);
138 static QObject *test_qmp_dispatch(QDict *req)
140 QObject *resp_obj;
141 QDict *resp;
142 QObject *ret;
144 resp_obj = qmp_dispatch(&qmp_commands, QOBJECT(req));
145 assert(resp_obj);
146 resp = qobject_to_qdict(resp_obj);
147 assert(resp && !qdict_haskey(resp, "error"));
148 ret = qdict_get(resp, "return");
149 assert(ret);
150 qobject_incref(ret);
151 qobject_decref(resp_obj);
152 return ret;
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;
165 QInt *ret3;
167 qdict_put_obj(ud1a, "integer", QOBJECT(qint_from_int(42)));
168 qdict_put_obj(ud1a, "string", QOBJECT(qstring_from_str("hello")));
169 qdict_put_obj(ud1b, "integer", QOBJECT(qint_from_int(422)));
170 qdict_put_obj(ud1b, "string", QOBJECT(qstring_from_str("hello2")));
171 qdict_put_obj(args, "ud1a", QOBJECT(ud1a));
172 qdict_put_obj(args, "ud1b", QOBJECT(ud1b));
173 qdict_put_obj(req, "arguments", QOBJECT(args));
174 qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd2")));
176 ret = qobject_to_qdict(test_qmp_dispatch(req));
178 assert(!strcmp(qdict_get_str(ret, "string0"), "blah1"));
179 ret_dict = qdict_get_qdict(ret, "dict1");
180 assert(!strcmp(qdict_get_str(ret_dict, "string1"), "blah2"));
181 ret_dict_dict = qdict_get_qdict(ret_dict, "dict2");
182 ret_dict_dict_userdef = qdict_get_qdict(ret_dict_dict, "userdef");
183 assert(qdict_get_int(ret_dict_dict_userdef, "integer") == 42);
184 assert(!strcmp(qdict_get_str(ret_dict_dict_userdef, "string"), "hello"));
185 assert(!strcmp(qdict_get_str(ret_dict_dict, "string"), "blah3"));
186 ret_dict_dict2 = qdict_get_qdict(ret_dict, "dict3");
187 ret_dict_dict2_userdef = qdict_get_qdict(ret_dict_dict2, "userdef");
188 assert(qdict_get_int(ret_dict_dict2_userdef, "integer") == 422);
189 assert(!strcmp(qdict_get_str(ret_dict_dict2_userdef, "string"), "hello2"));
190 assert(!strcmp(qdict_get_str(ret_dict_dict2, "string"), "blah4"));
191 QDECREF(ret);
193 qdict_put(args3, "a", qint_from_int(66));
194 qdict_put(req, "arguments", args3);
195 qdict_put(req, "execute", qstring_from_str("guest-get-time"));
197 ret3 = qobject_to_qint(test_qmp_dispatch(req));
198 assert(qint_get_int(ret3) == 66);
199 QDECREF(ret3);
201 QDECREF(req);
204 /* test generated dealloc functions for generated types */
205 static void test_dealloc_types(void)
207 UserDefOne *ud1test, *ud1a, *ud1b;
208 UserDefOneList *ud1list;
210 ud1test = g_malloc0(sizeof(UserDefOne));
211 ud1test->integer = 42;
212 ud1test->string = g_strdup("hi there 42");
214 qapi_free_UserDefOne(ud1test);
216 ud1a = g_malloc0(sizeof(UserDefOne));
217 ud1a->integer = 43;
218 ud1a->string = g_strdup("hi there 43");
220 ud1b = g_malloc0(sizeof(UserDefOne));
221 ud1b->integer = 44;
222 ud1b->string = g_strdup("hi there 44");
224 ud1list = g_malloc0(sizeof(UserDefOneList));
225 ud1list->value = ud1a;
226 ud1list->next = g_malloc0(sizeof(UserDefOneList));
227 ud1list->next->value = ud1b;
229 qapi_free_UserDefOneList(ud1list);
232 /* test generated deallocation on an object whose construction was prematurely
233 * terminated due to an error */
234 static void test_dealloc_partial(void)
236 static const char text[] = "don't leak me";
238 UserDefTwo *ud2 = NULL;
239 Error *err = NULL;
241 /* create partial object */
243 QDict *ud2_dict;
244 Visitor *v;
246 ud2_dict = qdict_new();
247 qdict_put_obj(ud2_dict, "string0", QOBJECT(qstring_from_str(text)));
249 v = qobject_input_visitor_new(QOBJECT(ud2_dict));
250 visit_type_UserDefTwo(v, NULL, &ud2, &err);
251 visit_free(v);
252 QDECREF(ud2_dict);
255 /* verify that visit_type_XXX() cleans up properly on error */
256 error_free_or_abort(&err);
257 assert(!ud2);
259 /* Manually create a partial object, leaving ud2->dict1 at NULL */
260 ud2 = g_new0(UserDefTwo, 1);
261 ud2->string0 = g_strdup(text);
263 /* tear down partial object */
264 qapi_free_UserDefTwo(ud2);
268 int main(int argc, char **argv)
270 g_test_init(&argc, &argv, NULL);
272 g_test_add_func("/0.15/dispatch_cmd", test_dispatch_cmd);
273 g_test_add_func("/0.15/dispatch_cmd_failure", test_dispatch_cmd_failure);
274 g_test_add_func("/0.15/dispatch_cmd_io", test_dispatch_cmd_io);
275 g_test_add_func("/0.15/dealloc_types", test_dealloc_types);
276 g_test_add_func("/0.15/dealloc_partial", test_dealloc_partial);
278 test_qmp_init_marshal(&qmp_commands);
279 g_test_run();
281 return 0;