Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging
[qemu/qmp-unstable.git] / tests / test-qmp-commands.c
blob554e222b32d024e56e4147a27f075133c0aa5466
1 #include <glib.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/qmp-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 void qmp_user_def_cmd1(UserDefOne * ud1, Error **errp)
19 UserDefTwo *qmp_user_def_cmd2(UserDefOne *ud1a,
20 bool has_udb1, UserDefOne *ud1b,
21 Error **errp)
23 UserDefTwo *ret;
24 UserDefOne *ud1c = g_malloc0(sizeof(UserDefOne));
25 UserDefOne *ud1d = g_malloc0(sizeof(UserDefOne));
27 ud1c->string = strdup(ud1a->string);
28 ud1c->base = g_new0(UserDefZero, 1);
29 ud1c->base->integer = ud1a->base->integer;
30 ud1d->string = strdup(has_udb1 ? ud1b->string : "blah0");
31 ud1d->base = g_new0(UserDefZero, 1);
32 ud1d->base->integer = has_udb1 ? ud1b->base->integer : 0;
34 ret = g_malloc0(sizeof(UserDefTwo));
35 ret->string = strdup("blah1");
36 ret->dict.string = strdup("blah2");
37 ret->dict.dict.userdef = ud1c;
38 ret->dict.dict.string = strdup("blah3");
39 ret->dict.has_dict2 = true;
40 ret->dict.dict2.userdef = ud1d;
41 ret->dict.dict2.string = strdup("blah4");
43 return ret;
46 int64_t qmp_user_def_cmd3(int64_t a, bool has_b, int64_t b, Error **errp)
48 return a + (has_b ? b : 0);
51 /* test commands with no input and no return value */
52 static void test_dispatch_cmd(void)
54 QDict *req = qdict_new();
55 QObject *resp;
57 qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd")));
59 resp = qmp_dispatch(QOBJECT(req));
60 assert(resp != NULL);
61 assert(!qdict_haskey(qobject_to_qdict(resp), "error"));
63 qobject_decref(resp);
64 QDECREF(req);
67 /* test commands that return an error due to invalid parameters */
68 static void test_dispatch_cmd_error(void)
70 QDict *req = qdict_new();
71 QObject *resp;
73 qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd2")));
75 resp = qmp_dispatch(QOBJECT(req));
76 assert(resp != NULL);
77 assert(qdict_haskey(qobject_to_qdict(resp), "error"));
79 qobject_decref(resp);
80 QDECREF(req);
83 static QObject *test_qmp_dispatch(QDict *req)
85 QObject *resp_obj;
86 QDict *resp;
87 QObject *ret;
89 resp_obj = qmp_dispatch(QOBJECT(req));
90 assert(resp_obj);
91 resp = qobject_to_qdict(resp_obj);
92 assert(resp && !qdict_haskey(resp, "error"));
93 ret = qdict_get(resp, "return");
94 assert(ret);
95 qobject_incref(ret);
96 qobject_decref(resp_obj);
97 return ret;
100 /* test commands that involve both input parameters and return values */
101 static void test_dispatch_cmd_io(void)
103 QDict *req = qdict_new();
104 QDict *args = qdict_new();
105 QDict *args3 = qdict_new();
106 QDict *ud1a = qdict_new();
107 QDict *ud1b = qdict_new();
108 QDict *ret, *ret_dict, *ret_dict_dict, *ret_dict_dict_userdef;
109 QDict *ret_dict_dict2, *ret_dict_dict2_userdef;
110 QInt *ret3;
112 qdict_put_obj(ud1a, "integer", QOBJECT(qint_from_int(42)));
113 qdict_put_obj(ud1a, "string", QOBJECT(qstring_from_str("hello")));
114 qdict_put_obj(ud1b, "integer", QOBJECT(qint_from_int(422)));
115 qdict_put_obj(ud1b, "string", QOBJECT(qstring_from_str("hello2")));
116 qdict_put_obj(args, "ud1a", QOBJECT(ud1a));
117 qdict_put_obj(args, "ud1b", QOBJECT(ud1b));
118 qdict_put_obj(req, "arguments", QOBJECT(args));
119 qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd2")));
121 ret = qobject_to_qdict(test_qmp_dispatch(req));
123 assert(!strcmp(qdict_get_str(ret, "string"), "blah1"));
124 ret_dict = qdict_get_qdict(ret, "dict");
125 assert(!strcmp(qdict_get_str(ret_dict, "string"), "blah2"));
126 ret_dict_dict = qdict_get_qdict(ret_dict, "dict");
127 ret_dict_dict_userdef = qdict_get_qdict(ret_dict_dict, "userdef");
128 assert(qdict_get_int(ret_dict_dict_userdef, "integer") == 42);
129 assert(!strcmp(qdict_get_str(ret_dict_dict_userdef, "string"), "hello"));
130 assert(!strcmp(qdict_get_str(ret_dict_dict, "string"), "blah3"));
131 ret_dict_dict2 = qdict_get_qdict(ret_dict, "dict2");
132 ret_dict_dict2_userdef = qdict_get_qdict(ret_dict_dict2, "userdef");
133 assert(qdict_get_int(ret_dict_dict2_userdef, "integer") == 422);
134 assert(!strcmp(qdict_get_str(ret_dict_dict2_userdef, "string"), "hello2"));
135 assert(!strcmp(qdict_get_str(ret_dict_dict2, "string"), "blah4"));
136 QDECREF(ret);
138 qdict_put(args3, "a", qint_from_int(66));
139 qdict_put(req, "arguments", args3);
140 qdict_put(req, "execute", qstring_from_str("user_def_cmd3"));
142 ret3 = qobject_to_qint(test_qmp_dispatch(req));
143 assert(qint_get_int(ret3) == 66);
144 QDECREF(ret3);
146 QDECREF(req);
149 /* test generated dealloc functions for generated types */
150 static void test_dealloc_types(void)
152 UserDefOne *ud1test, *ud1a, *ud1b;
153 UserDefOneList *ud1list;
155 ud1test = g_malloc0(sizeof(UserDefOne));
156 ud1test->base = g_new0(UserDefZero, 1);
157 ud1test->base->integer = 42;
158 ud1test->string = g_strdup("hi there 42");
160 qapi_free_UserDefOne(ud1test);
162 ud1a = g_malloc0(sizeof(UserDefOne));
163 ud1a->base = g_new0(UserDefZero, 1);
164 ud1a->base->integer = 43;
165 ud1a->string = g_strdup("hi there 43");
167 ud1b = g_malloc0(sizeof(UserDefOne));
168 ud1b->base = g_new0(UserDefZero, 1);
169 ud1b->base->integer = 44;
170 ud1b->string = g_strdup("hi there 44");
172 ud1list = g_malloc0(sizeof(UserDefOneList));
173 ud1list->value = ud1a;
174 ud1list->next = g_malloc0(sizeof(UserDefOneList));
175 ud1list->next->value = ud1b;
177 qapi_free_UserDefOneList(ud1list);
180 /* test generated deallocation on an object whose construction was prematurely
181 * terminated due to an error */
182 static void test_dealloc_partial(void)
184 static const char text[] = "don't leak me";
186 UserDefTwo *ud2 = NULL;
187 Error *err = NULL;
189 /* create partial object */
191 QDict *ud2_dict;
192 QmpInputVisitor *qiv;
194 ud2_dict = qdict_new();
195 qdict_put_obj(ud2_dict, "string", QOBJECT(qstring_from_str(text)));
197 qiv = qmp_input_visitor_new(QOBJECT(ud2_dict));
198 visit_type_UserDefTwo(qmp_input_get_visitor(qiv), &ud2, NULL, &err);
199 qmp_input_visitor_cleanup(qiv);
200 QDECREF(ud2_dict);
203 /* verify partial success */
204 assert(ud2 != NULL);
205 assert(ud2->string != NULL);
206 assert(strcmp(ud2->string, text) == 0);
207 assert(ud2->dict.dict.userdef == NULL);
209 /* confirm & release construction error */
210 assert(err != NULL);
211 error_free(err);
213 /* tear down partial object */
214 qapi_free_UserDefTwo(ud2);
218 int main(int argc, char **argv)
220 g_test_init(&argc, &argv, NULL);
222 g_test_add_func("/0.15/dispatch_cmd", test_dispatch_cmd);
223 g_test_add_func("/0.15/dispatch_cmd_error", test_dispatch_cmd_error);
224 g_test_add_func("/0.15/dispatch_cmd_io", test_dispatch_cmd_io);
225 g_test_add_func("/0.15/dealloc_types", test_dealloc_types);
226 g_test_add_func("/0.15/dealloc_partial", test_dealloc_partial);
228 module_call_init(MODULE_INIT_QAPI);
229 g_test_run();
231 return 0;