]>
Commit | Line | Data |
---|---|---|
69ed8366 | 1 | #include <glib.h> |
79ee7df8 | 2 | #include "qemu-common.h" |
7b1b5d19 | 3 | #include "qapi/qmp/types.h" |
69ed8366 | 4 | #include "test-qmp-commands.h" |
7b1b5d19 | 5 | #include "qapi/qmp/dispatch.h" |
1de7afc9 | 6 | #include "qemu/module.h" |
d98150f0 LE |
7 | #include "qapi/qmp-input-visitor.h" |
8 | #include "tests/test-qapi-types.h" | |
9 | #include "tests/test-qapi-visit.h" | |
69ed8366 MR |
10 | |
11 | void qmp_user_def_cmd(Error **errp) | |
12 | { | |
13 | } | |
14 | ||
15 | void qmp_user_def_cmd1(UserDefOne * ud1, Error **errp) | |
16 | { | |
17 | } | |
18 | ||
ab22ad96 MA |
19 | UserDefTwo *qmp_user_def_cmd2(UserDefOne *ud1a, |
20 | bool has_udb1, UserDefOne *ud1b, | |
21 | Error **errp) | |
69ed8366 MR |
22 | { |
23 | UserDefTwo *ret; | |
7267c094 AL |
24 | UserDefOne *ud1c = g_malloc0(sizeof(UserDefOne)); |
25 | UserDefOne *ud1d = g_malloc0(sizeof(UserDefOne)); | |
69ed8366 MR |
26 | |
27 | ud1c->string = strdup(ud1a->string); | |
28 | ud1c->integer = ud1a->integer; | |
ab22ad96 MA |
29 | ud1d->string = strdup(has_udb1 ? ud1b->string : "blah0"); |
30 | ud1d->integer = has_udb1 ? ud1b->integer : 0; | |
69ed8366 | 31 | |
7267c094 | 32 | ret = g_malloc0(sizeof(UserDefTwo)); |
69ed8366 MR |
33 | ret->string = strdup("blah1"); |
34 | ret->dict.string = strdup("blah2"); | |
35 | ret->dict.dict.userdef = ud1c; | |
36 | ret->dict.dict.string = strdup("blah3"); | |
37 | ret->dict.has_dict2 = true; | |
38 | ret->dict.dict2.userdef = ud1d; | |
39 | ret->dict.dict2.string = strdup("blah4"); | |
40 | ||
41 | return ret; | |
42 | } | |
43 | ||
c2216a8a MA |
44 | int64_t qmp_user_def_cmd3(int64_t a, bool has_b, int64_t b, Error **errp) |
45 | { | |
46 | return a + (has_b ? b : 0); | |
47 | } | |
48 | ||
69ed8366 MR |
49 | /* test commands with no input and no return value */ |
50 | static void test_dispatch_cmd(void) | |
51 | { | |
52 | QDict *req = qdict_new(); | |
53 | QObject *resp; | |
54 | ||
55 | qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd"))); | |
56 | ||
57 | resp = qmp_dispatch(QOBJECT(req)); | |
58 | assert(resp != NULL); | |
59 | assert(!qdict_haskey(qobject_to_qdict(resp), "error")); | |
69ed8366 MR |
60 | |
61 | qobject_decref(resp); | |
62 | QDECREF(req); | |
63 | } | |
64 | ||
65 | /* test commands that return an error due to invalid parameters */ | |
66 | static void test_dispatch_cmd_error(void) | |
67 | { | |
68 | QDict *req = qdict_new(); | |
69 | QObject *resp; | |
70 | ||
71 | qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd2"))); | |
72 | ||
73 | resp = qmp_dispatch(QOBJECT(req)); | |
74 | assert(resp != NULL); | |
75 | assert(qdict_haskey(qobject_to_qdict(resp), "error")); | |
69ed8366 MR |
76 | |
77 | qobject_decref(resp); | |
78 | QDECREF(req); | |
79 | } | |
80 | ||
357765fe MA |
81 | static QObject *test_qmp_dispatch(QDict *req) |
82 | { | |
83 | QObject *resp_obj; | |
84 | QDict *resp; | |
85 | QObject *ret; | |
86 | ||
87 | resp_obj = qmp_dispatch(QOBJECT(req)); | |
88 | assert(resp_obj); | |
89 | resp = qobject_to_qdict(resp_obj); | |
90 | assert(resp && !qdict_haskey(resp, "error")); | |
91 | ret = qdict_get(resp, "return"); | |
92 | assert(ret); | |
93 | qobject_incref(ret); | |
94 | qobject_decref(resp_obj); | |
95 | return ret; | |
96 | } | |
97 | ||
69ed8366 MR |
98 | /* test commands that involve both input parameters and return values */ |
99 | static void test_dispatch_cmd_io(void) | |
100 | { | |
101 | QDict *req = qdict_new(); | |
102 | QDict *args = qdict_new(); | |
c2216a8a | 103 | QDict *args3 = qdict_new(); |
69ed8366 MR |
104 | QDict *ud1a = qdict_new(); |
105 | QDict *ud1b = qdict_new(); | |
357765fe MA |
106 | QDict *ret, *ret_dict, *ret_dict_dict, *ret_dict_dict_userdef; |
107 | QDict *ret_dict_dict2, *ret_dict_dict2_userdef; | |
c2216a8a | 108 | QInt *ret3; |
69ed8366 MR |
109 | |
110 | qdict_put_obj(ud1a, "integer", QOBJECT(qint_from_int(42))); | |
111 | qdict_put_obj(ud1a, "string", QOBJECT(qstring_from_str("hello"))); | |
112 | qdict_put_obj(ud1b, "integer", QOBJECT(qint_from_int(422))); | |
113 | qdict_put_obj(ud1b, "string", QOBJECT(qstring_from_str("hello2"))); | |
114 | qdict_put_obj(args, "ud1a", QOBJECT(ud1a)); | |
115 | qdict_put_obj(args, "ud1b", QOBJECT(ud1b)); | |
116 | qdict_put_obj(req, "arguments", QOBJECT(args)); | |
69ed8366 MR |
117 | qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd2"))); |
118 | ||
357765fe MA |
119 | ret = qobject_to_qdict(test_qmp_dispatch(req)); |
120 | ||
121 | assert(!strcmp(qdict_get_str(ret, "string"), "blah1")); | |
122 | ret_dict = qdict_get_qdict(ret, "dict"); | |
123 | assert(!strcmp(qdict_get_str(ret_dict, "string"), "blah2")); | |
124 | ret_dict_dict = qdict_get_qdict(ret_dict, "dict"); | |
125 | ret_dict_dict_userdef = qdict_get_qdict(ret_dict_dict, "userdef"); | |
126 | assert(qdict_get_int(ret_dict_dict_userdef, "integer") == 42); | |
127 | assert(!strcmp(qdict_get_str(ret_dict_dict_userdef, "string"), "hello")); | |
128 | assert(!strcmp(qdict_get_str(ret_dict_dict, "string"), "blah3")); | |
129 | ret_dict_dict2 = qdict_get_qdict(ret_dict, "dict2"); | |
130 | ret_dict_dict2_userdef = qdict_get_qdict(ret_dict_dict2, "userdef"); | |
131 | assert(qdict_get_int(ret_dict_dict2_userdef, "integer") == 422); | |
132 | assert(!strcmp(qdict_get_str(ret_dict_dict2_userdef, "string"), "hello2")); | |
133 | assert(!strcmp(qdict_get_str(ret_dict_dict2, "string"), "blah4")); | |
134 | QDECREF(ret); | |
c2216a8a MA |
135 | |
136 | qdict_put(args3, "a", qint_from_int(66)); | |
137 | qdict_put(req, "arguments", args3); | |
138 | qdict_put(req, "execute", qstring_from_str("user_def_cmd3")); | |
139 | ||
140 | ret3 = qobject_to_qint(test_qmp_dispatch(req)); | |
141 | assert(qint_get_int(ret3) == 66); | |
142 | QDECREF(ret); | |
143 | ||
69ed8366 MR |
144 | QDECREF(req); |
145 | } | |
146 | ||
5cd5f0d0 MR |
147 | /* test generated dealloc functions for generated types */ |
148 | static void test_dealloc_types(void) | |
149 | { | |
150 | UserDefOne *ud1test, *ud1a, *ud1b; | |
151 | UserDefOneList *ud1list; | |
152 | ||
153 | ud1test = g_malloc0(sizeof(UserDefOne)); | |
154 | ud1test->integer = 42; | |
155 | ud1test->string = g_strdup("hi there 42"); | |
156 | ||
157 | qapi_free_UserDefOne(ud1test); | |
158 | ||
159 | ud1a = g_malloc0(sizeof(UserDefOne)); | |
160 | ud1a->integer = 43; | |
161 | ud1a->string = g_strdup("hi there 43"); | |
162 | ||
163 | ud1b = g_malloc0(sizeof(UserDefOne)); | |
164 | ud1b->integer = 44; | |
165 | ud1b->string = g_strdup("hi there 44"); | |
166 | ||
167 | ud1list = g_malloc0(sizeof(UserDefOneList)); | |
168 | ud1list->value = ud1a; | |
169 | ud1list->next = g_malloc0(sizeof(UserDefOneList)); | |
170 | ud1list->next->value = ud1b; | |
171 | ||
172 | qapi_free_UserDefOneList(ud1list); | |
173 | } | |
174 | ||
d98150f0 LE |
175 | /* test generated deallocation on an object whose construction was prematurely |
176 | * terminated due to an error */ | |
177 | static void test_dealloc_partial(void) | |
178 | { | |
179 | static const char text[] = "don't leak me"; | |
180 | ||
181 | UserDefTwo *ud2 = NULL; | |
182 | Error *err = NULL; | |
183 | ||
184 | /* create partial object */ | |
185 | { | |
186 | QDict *ud2_dict; | |
187 | QmpInputVisitor *qiv; | |
188 | ||
189 | ud2_dict = qdict_new(); | |
190 | qdict_put_obj(ud2_dict, "string", QOBJECT(qstring_from_str(text))); | |
191 | ||
192 | qiv = qmp_input_visitor_new(QOBJECT(ud2_dict)); | |
193 | visit_type_UserDefTwo(qmp_input_get_visitor(qiv), &ud2, NULL, &err); | |
194 | qmp_input_visitor_cleanup(qiv); | |
195 | QDECREF(ud2_dict); | |
196 | } | |
197 | ||
198 | /* verify partial success */ | |
199 | assert(ud2 != NULL); | |
200 | assert(ud2->string != NULL); | |
201 | assert(strcmp(ud2->string, text) == 0); | |
202 | assert(ud2->dict.dict.userdef == NULL); | |
203 | ||
204 | /* confirm & release construction error */ | |
205 | assert(err != NULL); | |
206 | error_free(err); | |
207 | ||
208 | /* tear down partial object */ | |
209 | qapi_free_UserDefTwo(ud2); | |
210 | } | |
211 | ||
212 | ||
69ed8366 MR |
213 | int main(int argc, char **argv) |
214 | { | |
215 | g_test_init(&argc, &argv, NULL); | |
216 | ||
217 | g_test_add_func("/0.15/dispatch_cmd", test_dispatch_cmd); | |
218 | g_test_add_func("/0.15/dispatch_cmd_error", test_dispatch_cmd_error); | |
219 | g_test_add_func("/0.15/dispatch_cmd_io", test_dispatch_cmd_io); | |
5cd5f0d0 | 220 | g_test_add_func("/0.15/dealloc_types", test_dealloc_types); |
d98150f0 | 221 | g_test_add_func("/0.15/dealloc_partial", test_dealloc_partial); |
69ed8366 MR |
222 | |
223 | module_call_init(MODULE_INIT_QAPI); | |
224 | g_test_run(); | |
225 | ||
226 | return 0; | |
227 | } |