]>
Commit | Line | Data |
---|---|---|
e38ac962 PB |
1 | /* |
2 | * QMP Input Visitor unit-tests (strict mode). | |
3 | * | |
4 | * Copyright (C) 2011-2012 Red Hat Inc. | |
5 | * | |
6 | * Authors: | |
7 | * Luiz Capitulino <[email protected]> | |
8 | * Paolo Bonzini <[email protected]> | |
9 | * | |
10 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
11 | * See the COPYING file in the top-level directory. | |
12 | */ | |
13 | ||
14 | #include <glib.h> | |
15 | #include <stdarg.h> | |
16 | ||
79ee7df8 | 17 | #include "qemu-common.h" |
e38ac962 PB |
18 | #include "qapi/qmp-input-visitor.h" |
19 | #include "test-qapi-types.h" | |
20 | #include "test-qapi-visit.h" | |
7b1b5d19 | 21 | #include "qapi/qmp/types.h" |
e38ac962 PB |
22 | |
23 | typedef struct TestInputVisitorData { | |
24 | QObject *obj; | |
25 | QmpInputVisitor *qiv; | |
26 | } TestInputVisitorData; | |
27 | ||
28 | static void validate_teardown(TestInputVisitorData *data, | |
29 | const void *unused) | |
30 | { | |
31 | qobject_decref(data->obj); | |
32 | data->obj = NULL; | |
33 | ||
34 | if (data->qiv) { | |
35 | qmp_input_visitor_cleanup(data->qiv); | |
36 | data->qiv = NULL; | |
37 | } | |
38 | } | |
39 | ||
40 | /* This is provided instead of a test setup function so that the JSON | |
41 | string used by the tests are kept in the test functions (and not | |
42 | int main()) */ | |
43 | static GCC_FMT_ATTR(2, 3) | |
44 | Visitor *validate_test_init(TestInputVisitorData *data, | |
45 | const char *json_string, ...) | |
46 | { | |
47 | Visitor *v; | |
48 | va_list ap; | |
49 | ||
50 | va_start(ap, json_string); | |
51 | data->obj = qobject_from_jsonv(json_string, &ap); | |
52 | va_end(ap); | |
53 | ||
54 | g_assert(data->obj != NULL); | |
55 | ||
56 | data->qiv = qmp_input_visitor_new_strict(data->obj); | |
57 | g_assert(data->qiv != NULL); | |
58 | ||
59 | v = qmp_input_get_visitor(data->qiv); | |
60 | g_assert(v != NULL); | |
61 | ||
62 | return v; | |
63 | } | |
64 | ||
65 | typedef struct TestStruct | |
66 | { | |
67 | int64_t integer; | |
68 | bool boolean; | |
69 | char *string; | |
70 | } TestStruct; | |
71 | ||
72 | static void visit_type_TestStruct(Visitor *v, TestStruct **obj, | |
73 | const char *name, Error **errp) | |
74 | { | |
75 | visit_start_struct(v, (void **)obj, "TestStruct", name, sizeof(TestStruct), | |
76 | errp); | |
77 | ||
78 | visit_type_int(v, &(*obj)->integer, "integer", errp); | |
79 | visit_type_bool(v, &(*obj)->boolean, "boolean", errp); | |
80 | visit_type_str(v, &(*obj)->string, "string", errp); | |
81 | ||
82 | visit_end_struct(v, errp); | |
83 | } | |
84 | ||
85 | static void test_validate_struct(TestInputVisitorData *data, | |
86 | const void *unused) | |
87 | { | |
88 | TestStruct *p = NULL; | |
e940f543 | 89 | Error *err = NULL; |
e38ac962 PB |
90 | Visitor *v; |
91 | ||
92 | v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }"); | |
93 | ||
e940f543 MA |
94 | visit_type_TestStruct(v, &p, NULL, &err); |
95 | g_assert(!err); | |
e38ac962 PB |
96 | g_free(p->string); |
97 | g_free(p); | |
98 | } | |
99 | ||
100 | static void test_validate_struct_nested(TestInputVisitorData *data, | |
101 | const void *unused) | |
102 | { | |
103 | UserDefNested *udp = NULL; | |
e940f543 | 104 | Error *err = NULL; |
e38ac962 PB |
105 | Visitor *v; |
106 | ||
107 | v = validate_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string' }, 'string2': 'string2'}}}"); | |
108 | ||
e940f543 MA |
109 | visit_type_UserDefNested(v, &udp, NULL, &err); |
110 | g_assert(!err); | |
e38ac962 PB |
111 | qapi_free_UserDefNested(udp); |
112 | } | |
113 | ||
114 | static void test_validate_list(TestInputVisitorData *data, | |
115 | const void *unused) | |
116 | { | |
117 | UserDefOneList *head = NULL; | |
e940f543 | 118 | Error *err = NULL; |
e38ac962 PB |
119 | Visitor *v; |
120 | ||
121 | v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]"); | |
122 | ||
e940f543 MA |
123 | visit_type_UserDefOneList(v, &head, NULL, &err); |
124 | g_assert(!err); | |
e38ac962 PB |
125 | qapi_free_UserDefOneList(head); |
126 | } | |
127 | ||
128 | static void test_validate_union(TestInputVisitorData *data, | |
129 | const void *unused) | |
130 | { | |
131 | UserDefUnion *tmp = NULL; | |
132 | Visitor *v; | |
e940f543 | 133 | Error *err = NULL; |
e38ac962 | 134 | |
7ad993b4 | 135 | v = validate_test_init(data, "{ 'type': 'b', 'integer': 41, 'data' : { 'integer': 42 } }"); |
e38ac962 | 136 | |
e940f543 MA |
137 | visit_type_UserDefUnion(v, &tmp, NULL, &err); |
138 | g_assert(!err); | |
e38ac962 PB |
139 | qapi_free_UserDefUnion(tmp); |
140 | } | |
141 | ||
2fc00432 MA |
142 | static void test_validate_union_flat(TestInputVisitorData *data, |
143 | const void *unused) | |
144 | { | |
145 | UserDefFlatUnion *tmp = NULL; | |
146 | Visitor *v; | |
e940f543 | 147 | Error *err = NULL; |
2fc00432 | 148 | |
5223070c WX |
149 | v = validate_test_init(data, |
150 | "{ 'enum1': 'value1', " | |
151 | "'string': 'str', " | |
152 | "'boolean': true }"); | |
2fc00432 MA |
153 | /* TODO when generator bug is fixed, add 'integer': 41 */ |
154 | ||
e940f543 MA |
155 | visit_type_UserDefFlatUnion(v, &tmp, NULL, &err); |
156 | g_assert(!err); | |
2fc00432 MA |
157 | qapi_free_UserDefFlatUnion(tmp); |
158 | } | |
159 | ||
2c38b600 MA |
160 | static void test_validate_union_anon(TestInputVisitorData *data, |
161 | const void *unused) | |
162 | { | |
163 | UserDefAnonUnion *tmp = NULL; | |
164 | Visitor *v; | |
e940f543 | 165 | Error *err = NULL; |
2c38b600 MA |
166 | |
167 | v = validate_test_init(data, "42"); | |
168 | ||
e940f543 MA |
169 | visit_type_UserDefAnonUnion(v, &tmp, NULL, &err); |
170 | g_assert(!err); | |
2c38b600 MA |
171 | qapi_free_UserDefAnonUnion(tmp); |
172 | } | |
173 | ||
e38ac962 PB |
174 | static void test_validate_fail_struct(TestInputVisitorData *data, |
175 | const void *unused) | |
176 | { | |
177 | TestStruct *p = NULL; | |
e940f543 | 178 | Error *err = NULL; |
e38ac962 PB |
179 | Visitor *v; |
180 | ||
181 | v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }"); | |
182 | ||
e940f543 MA |
183 | visit_type_TestStruct(v, &p, NULL, &err); |
184 | g_assert(err); | |
e38ac962 PB |
185 | if (p) { |
186 | g_free(p->string); | |
187 | } | |
188 | g_free(p); | |
189 | } | |
190 | ||
191 | static void test_validate_fail_struct_nested(TestInputVisitorData *data, | |
192 | const void *unused) | |
193 | { | |
194 | UserDefNested *udp = NULL; | |
e940f543 | 195 | Error *err = NULL; |
e38ac962 PB |
196 | Visitor *v; |
197 | ||
198 | v = validate_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}"); | |
199 | ||
e940f543 MA |
200 | visit_type_UserDefNested(v, &udp, NULL, &err); |
201 | g_assert(err); | |
e38ac962 PB |
202 | qapi_free_UserDefNested(udp); |
203 | } | |
204 | ||
205 | static void test_validate_fail_list(TestInputVisitorData *data, | |
206 | const void *unused) | |
207 | { | |
208 | UserDefOneList *head = NULL; | |
e940f543 | 209 | Error *err = NULL; |
e38ac962 PB |
210 | Visitor *v; |
211 | ||
212 | v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]"); | |
213 | ||
e940f543 MA |
214 | visit_type_UserDefOneList(v, &head, NULL, &err); |
215 | g_assert(err); | |
e38ac962 PB |
216 | qapi_free_UserDefOneList(head); |
217 | } | |
218 | ||
219 | static void test_validate_fail_union(TestInputVisitorData *data, | |
220 | const void *unused) | |
221 | { | |
222 | UserDefUnion *tmp = NULL; | |
e940f543 | 223 | Error *err = NULL; |
e38ac962 PB |
224 | Visitor *v; |
225 | ||
7ad993b4 | 226 | v = validate_test_init(data, "{ 'type': 'b', 'data' : { 'integer': 42 } }"); |
e38ac962 | 227 | |
e940f543 MA |
228 | visit_type_UserDefUnion(v, &tmp, NULL, &err); |
229 | g_assert(err); | |
e38ac962 PB |
230 | qapi_free_UserDefUnion(tmp); |
231 | } | |
232 | ||
2fc00432 MA |
233 | static void test_validate_fail_union_flat(TestInputVisitorData *data, |
234 | const void *unused) | |
235 | { | |
236 | UserDefFlatUnion *tmp = NULL; | |
e940f543 | 237 | Error *err = NULL; |
2fc00432 MA |
238 | Visitor *v; |
239 | ||
240 | v = validate_test_init(data, "{ 'string': 'c', 'integer': 41, 'boolean': true }"); | |
241 | ||
e940f543 MA |
242 | visit_type_UserDefFlatUnion(v, &tmp, NULL, &err); |
243 | g_assert(err); | |
2fc00432 MA |
244 | qapi_free_UserDefFlatUnion(tmp); |
245 | } | |
246 | ||
2c38b600 MA |
247 | static void test_validate_fail_union_anon(TestInputVisitorData *data, |
248 | const void *unused) | |
249 | { | |
250 | UserDefAnonUnion *tmp = NULL; | |
251 | Visitor *v; | |
e940f543 | 252 | Error *err = NULL; |
2c38b600 MA |
253 | |
254 | v = validate_test_init(data, "3.14"); | |
255 | ||
e940f543 MA |
256 | visit_type_UserDefAnonUnion(v, &tmp, NULL, &err); |
257 | g_assert(err); | |
2c38b600 MA |
258 | qapi_free_UserDefAnonUnion(tmp); |
259 | } | |
260 | ||
e38ac962 PB |
261 | static void validate_test_add(const char *testpath, |
262 | TestInputVisitorData *data, | |
263 | void (*test_func)(TestInputVisitorData *data, const void *user_data)) | |
264 | { | |
265 | g_test_add(testpath, TestInputVisitorData, data, NULL, test_func, | |
266 | validate_teardown); | |
267 | } | |
268 | ||
269 | int main(int argc, char **argv) | |
270 | { | |
271 | TestInputVisitorData testdata; | |
272 | ||
273 | g_test_init(&argc, &argv, NULL); | |
274 | ||
275 | validate_test_add("/visitor/input-strict/pass/struct", | |
276 | &testdata, test_validate_struct); | |
277 | validate_test_add("/visitor/input-strict/pass/struct-nested", | |
278 | &testdata, test_validate_struct_nested); | |
279 | validate_test_add("/visitor/input-strict/pass/list", | |
280 | &testdata, test_validate_list); | |
281 | validate_test_add("/visitor/input-strict/pass/union", | |
282 | &testdata, test_validate_union); | |
2fc00432 MA |
283 | validate_test_add("/visitor/input-strict/pass/union-flat", |
284 | &testdata, test_validate_union_flat); | |
2c38b600 MA |
285 | validate_test_add("/visitor/input-strict/pass/union-anon", |
286 | &testdata, test_validate_union_anon); | |
e38ac962 PB |
287 | validate_test_add("/visitor/input-strict/fail/struct", |
288 | &testdata, test_validate_fail_struct); | |
289 | validate_test_add("/visitor/input-strict/fail/struct-nested", | |
290 | &testdata, test_validate_fail_struct_nested); | |
291 | validate_test_add("/visitor/input-strict/fail/list", | |
292 | &testdata, test_validate_fail_list); | |
293 | validate_test_add("/visitor/input-strict/fail/union", | |
294 | &testdata, test_validate_fail_union); | |
2fc00432 MA |
295 | validate_test_add("/visitor/input-strict/fail/union-flat", |
296 | &testdata, test_validate_fail_union_flat); | |
2c38b600 MA |
297 | validate_test_add("/visitor/input-strict/fail/union-anon", |
298 | &testdata, test_validate_fail_union_anon); | |
e38ac962 PB |
299 | |
300 | g_test_run(); | |
301 | ||
302 | return 0; | |
303 | } |