]>
Commit | Line | Data |
---|---|---|
e38ac962 PB |
1 | /* |
2 | * QMP Input Visitor unit-tests (strict mode). | |
3 | * | |
805017b7 | 4 | * Copyright (C) 2011-2012, 2015 Red Hat Inc. |
e38ac962 PB |
5 | * |
6 | * Authors: | |
7 | * Luiz Capitulino <lcapitulino@redhat.com> | |
8 | * Paolo Bonzini <pbonzini@redhat.com> | |
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 | ||
681c28a3 | 14 | #include "qemu/osdep.h" |
e38ac962 | 15 | #include <glib.h> |
e38ac962 | 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" |
39a18158 MA |
22 | #include "test-qmp-introspect.h" |
23 | #include "qmp-introspect.h" | |
24 | #include "qapi-visit.h" | |
e38ac962 PB |
25 | |
26 | typedef struct TestInputVisitorData { | |
27 | QObject *obj; | |
28 | QmpInputVisitor *qiv; | |
29 | } TestInputVisitorData; | |
30 | ||
31 | static void validate_teardown(TestInputVisitorData *data, | |
32 | const void *unused) | |
33 | { | |
34 | qobject_decref(data->obj); | |
35 | data->obj = NULL; | |
36 | ||
37 | if (data->qiv) { | |
38 | qmp_input_visitor_cleanup(data->qiv); | |
39 | data->qiv = NULL; | |
40 | } | |
41 | } | |
42 | ||
0920a171 EB |
43 | /* The various test_init functions are provided instead of a test setup |
44 | function so that the JSON string used by the tests are kept in the test | |
45 | functions (and not in main()). */ | |
46 | static Visitor *validate_test_init_internal(TestInputVisitorData *data, | |
47 | const char *json_string, | |
48 | va_list *ap) | |
49 | { | |
50 | Visitor *v; | |
51 | ||
b18f1141 EB |
52 | validate_teardown(data, NULL); |
53 | ||
0920a171 EB |
54 | data->obj = qobject_from_jsonv(json_string, ap); |
55 | g_assert(data->obj); | |
56 | ||
57 | data->qiv = qmp_input_visitor_new_strict(data->obj); | |
58 | g_assert(data->qiv); | |
59 | ||
60 | v = qmp_input_get_visitor(data->qiv); | |
61 | g_assert(v); | |
62 | ||
63 | return v; | |
64 | } | |
65 | ||
e38ac962 PB |
66 | static GCC_FMT_ATTR(2, 3) |
67 | Visitor *validate_test_init(TestInputVisitorData *data, | |
68 | const char *json_string, ...) | |
69 | { | |
70 | Visitor *v; | |
71 | va_list ap; | |
72 | ||
73 | va_start(ap, json_string); | |
0920a171 | 74 | v = validate_test_init_internal(data, json_string, &ap); |
e38ac962 | 75 | va_end(ap); |
e38ac962 PB |
76 | return v; |
77 | } | |
78 | ||
39a18158 MA |
79 | /* similar to validate_test_init(), but does not expect a string |
80 | * literal/format json_string argument and so can be used for | |
81 | * programatically generated strings (and we can't pass in programatically | |
82 | * generated strings via %s format parameters since qobject_from_jsonv() | |
83 | * will wrap those in double-quotes and treat the entire object as a | |
84 | * string) | |
85 | */ | |
86 | static Visitor *validate_test_init_raw(TestInputVisitorData *data, | |
87 | const char *json_string) | |
88 | { | |
0920a171 | 89 | return validate_test_init_internal(data, json_string, NULL); |
39a18158 MA |
90 | } |
91 | ||
e38ac962 PB |
92 | |
93 | static void test_validate_struct(TestInputVisitorData *data, | |
94 | const void *unused) | |
95 | { | |
96 | TestStruct *p = NULL; | |
e38ac962 PB |
97 | Visitor *v; |
98 | ||
99 | v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }"); | |
100 | ||
51e72bc1 | 101 | visit_type_TestStruct(v, NULL, &p, &error_abort); |
e38ac962 PB |
102 | g_free(p->string); |
103 | g_free(p); | |
104 | } | |
105 | ||
106 | static void test_validate_struct_nested(TestInputVisitorData *data, | |
107 | const void *unused) | |
108 | { | |
b6fcf32d | 109 | UserDefTwo *udp = NULL; |
e38ac962 PB |
110 | Visitor *v; |
111 | ||
b6fcf32d EB |
112 | v = validate_test_init(data, "{ 'string0': 'string0', " |
113 | "'dict1': { 'string1': 'string1', " | |
114 | "'dict2': { 'userdef': { 'integer': 42, " | |
115 | "'string': 'string' }, 'string': 'string2'}}}"); | |
e38ac962 | 116 | |
51e72bc1 | 117 | visit_type_UserDefTwo(v, NULL, &udp, &error_abort); |
b6fcf32d | 118 | qapi_free_UserDefTwo(udp); |
e38ac962 PB |
119 | } |
120 | ||
121 | static void test_validate_list(TestInputVisitorData *data, | |
122 | const void *unused) | |
123 | { | |
124 | UserDefOneList *head = NULL; | |
e38ac962 PB |
125 | Visitor *v; |
126 | ||
127 | v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]"); | |
128 | ||
51e72bc1 | 129 | visit_type_UserDefOneList(v, NULL, &head, &error_abort); |
e38ac962 PB |
130 | qapi_free_UserDefOneList(head); |
131 | } | |
132 | ||
805017b7 EB |
133 | static void test_validate_union_native_list(TestInputVisitorData *data, |
134 | const void *unused) | |
e38ac962 | 135 | { |
805017b7 | 136 | UserDefNativeListUnion *tmp = NULL; |
e38ac962 | 137 | Visitor *v; |
e38ac962 | 138 | |
805017b7 | 139 | v = validate_test_init(data, "{ 'type': 'integer', 'data' : [ 1, 2 ] }"); |
e38ac962 | 140 | |
51e72bc1 | 141 | visit_type_UserDefNativeListUnion(v, NULL, &tmp, &error_abort); |
805017b7 | 142 | qapi_free_UserDefNativeListUnion(tmp); |
e38ac962 PB |
143 | } |
144 | ||
2fc00432 MA |
145 | static void test_validate_union_flat(TestInputVisitorData *data, |
146 | const void *unused) | |
147 | { | |
148 | UserDefFlatUnion *tmp = NULL; | |
149 | Visitor *v; | |
2fc00432 | 150 | |
5223070c WX |
151 | v = validate_test_init(data, |
152 | "{ 'enum1': 'value1', " | |
441cbac0 | 153 | "'integer': 41, " |
5223070c WX |
154 | "'string': 'str', " |
155 | "'boolean': true }"); | |
2fc00432 | 156 | |
51e72bc1 | 157 | visit_type_UserDefFlatUnion(v, NULL, &tmp, &error_abort); |
2fc00432 MA |
158 | qapi_free_UserDefFlatUnion(tmp); |
159 | } | |
160 | ||
ab045267 EB |
161 | static void test_validate_alternate(TestInputVisitorData *data, |
162 | const void *unused) | |
2c38b600 | 163 | { |
ab045267 | 164 | UserDefAlternate *tmp = NULL; |
2c38b600 | 165 | Visitor *v; |
2c38b600 MA |
166 | |
167 | v = validate_test_init(data, "42"); | |
168 | ||
51e72bc1 | 169 | visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort); |
ab045267 | 170 | qapi_free_UserDefAlternate(tmp); |
2c38b600 MA |
171 | } |
172 | ||
e38ac962 PB |
173 | static void test_validate_fail_struct(TestInputVisitorData *data, |
174 | const void *unused) | |
175 | { | |
176 | TestStruct *p = NULL; | |
e940f543 | 177 | Error *err = NULL; |
e38ac962 PB |
178 | Visitor *v; |
179 | ||
180 | v = validate_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }"); | |
181 | ||
51e72bc1 | 182 | visit_type_TestStruct(v, NULL, &p, &err); |
a12a5a1a | 183 | error_free_or_abort(&err); |
e38ac962 PB |
184 | if (p) { |
185 | g_free(p->string); | |
186 | } | |
187 | g_free(p); | |
188 | } | |
189 | ||
190 | static void test_validate_fail_struct_nested(TestInputVisitorData *data, | |
191 | const void *unused) | |
192 | { | |
b6fcf32d | 193 | UserDefTwo *udp = NULL; |
e940f543 | 194 | Error *err = NULL; |
e38ac962 PB |
195 | Visitor *v; |
196 | ||
197 | v = validate_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}"); | |
198 | ||
51e72bc1 | 199 | visit_type_UserDefTwo(v, NULL, &udp, &err); |
a12a5a1a | 200 | error_free_or_abort(&err); |
b6fcf32d | 201 | qapi_free_UserDefTwo(udp); |
e38ac962 PB |
202 | } |
203 | ||
204 | static void test_validate_fail_list(TestInputVisitorData *data, | |
205 | const void *unused) | |
206 | { | |
207 | UserDefOneList *head = NULL; | |
e940f543 | 208 | Error *err = NULL; |
e38ac962 PB |
209 | Visitor *v; |
210 | ||
211 | v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]"); | |
212 | ||
51e72bc1 | 213 | visit_type_UserDefOneList(v, NULL, &head, &err); |
a12a5a1a | 214 | error_free_or_abort(&err); |
e38ac962 PB |
215 | qapi_free_UserDefOneList(head); |
216 | } | |
217 | ||
805017b7 EB |
218 | static void test_validate_fail_union_native_list(TestInputVisitorData *data, |
219 | const void *unused) | |
e38ac962 | 220 | { |
805017b7 | 221 | UserDefNativeListUnion *tmp = NULL; |
e940f543 | 222 | Error *err = NULL; |
e38ac962 PB |
223 | Visitor *v; |
224 | ||
805017b7 EB |
225 | v = validate_test_init(data, |
226 | "{ 'type': 'integer', 'data' : [ 'string' ] }"); | |
e38ac962 | 227 | |
51e72bc1 | 228 | visit_type_UserDefNativeListUnion(v, NULL, &tmp, &err); |
a12a5a1a | 229 | error_free_or_abort(&err); |
805017b7 | 230 | qapi_free_UserDefNativeListUnion(tmp); |
e38ac962 PB |
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 | ||
51e72bc1 | 242 | visit_type_UserDefFlatUnion(v, NULL, &tmp, &err); |
a12a5a1a | 243 | error_free_or_abort(&err); |
2fc00432 MA |
244 | qapi_free_UserDefFlatUnion(tmp); |
245 | } | |
246 | ||
cb55111b MR |
247 | static void test_validate_fail_union_flat_no_discrim(TestInputVisitorData *data, |
248 | const void *unused) | |
249 | { | |
250 | UserDefFlatUnion2 *tmp = NULL; | |
251 | Error *err = NULL; | |
252 | Visitor *v; | |
253 | ||
254 | /* test situation where discriminator field ('enum1' here) is missing */ | |
441cbac0 | 255 | v = validate_test_init(data, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }"); |
cb55111b | 256 | |
51e72bc1 | 257 | visit_type_UserDefFlatUnion2(v, NULL, &tmp, &err); |
a12a5a1a | 258 | error_free_or_abort(&err); |
cb55111b MR |
259 | qapi_free_UserDefFlatUnion2(tmp); |
260 | } | |
261 | ||
ab045267 EB |
262 | static void test_validate_fail_alternate(TestInputVisitorData *data, |
263 | const void *unused) | |
2c38b600 | 264 | { |
ab045267 | 265 | UserDefAlternate *tmp = NULL; |
2c38b600 | 266 | Visitor *v; |
e940f543 | 267 | Error *err = NULL; |
2c38b600 MA |
268 | |
269 | v = validate_test_init(data, "3.14"); | |
270 | ||
51e72bc1 | 271 | visit_type_UserDefAlternate(v, NULL, &tmp, &err); |
a12a5a1a | 272 | error_free_or_abort(&err); |
ab045267 | 273 | qapi_free_UserDefAlternate(tmp); |
2c38b600 MA |
274 | } |
275 | ||
39a18158 MA |
276 | static void do_test_validate_qmp_introspect(TestInputVisitorData *data, |
277 | const char *schema_json) | |
278 | { | |
279 | SchemaInfoList *schema = NULL; | |
39a18158 MA |
280 | Visitor *v; |
281 | ||
282 | v = validate_test_init_raw(data, schema_json); | |
283 | ||
51e72bc1 | 284 | visit_type_SchemaInfoList(v, NULL, &schema, &error_abort); |
39a18158 MA |
285 | g_assert(schema); |
286 | ||
287 | qapi_free_SchemaInfoList(schema); | |
288 | } | |
289 | ||
290 | static void test_validate_qmp_introspect(TestInputVisitorData *data, | |
291 | const void *unused) | |
292 | { | |
293 | do_test_validate_qmp_introspect(data, test_qmp_schema_json); | |
294 | do_test_validate_qmp_introspect(data, qmp_schema_json); | |
295 | } | |
296 | ||
e38ac962 PB |
297 | static void validate_test_add(const char *testpath, |
298 | TestInputVisitorData *data, | |
299 | void (*test_func)(TestInputVisitorData *data, const void *user_data)) | |
300 | { | |
301 | g_test_add(testpath, TestInputVisitorData, data, NULL, test_func, | |
302 | validate_teardown); | |
303 | } | |
304 | ||
305 | int main(int argc, char **argv) | |
306 | { | |
307 | TestInputVisitorData testdata; | |
308 | ||
309 | g_test_init(&argc, &argv, NULL); | |
310 | ||
311 | validate_test_add("/visitor/input-strict/pass/struct", | |
805017b7 | 312 | &testdata, test_validate_struct); |
e38ac962 | 313 | validate_test_add("/visitor/input-strict/pass/struct-nested", |
805017b7 | 314 | &testdata, test_validate_struct_nested); |
e38ac962 | 315 | validate_test_add("/visitor/input-strict/pass/list", |
805017b7 | 316 | &testdata, test_validate_list); |
2fc00432 | 317 | validate_test_add("/visitor/input-strict/pass/union-flat", |
805017b7 | 318 | &testdata, test_validate_union_flat); |
ab045267 EB |
319 | validate_test_add("/visitor/input-strict/pass/alternate", |
320 | &testdata, test_validate_alternate); | |
805017b7 EB |
321 | validate_test_add("/visitor/input-strict/pass/union-native-list", |
322 | &testdata, test_validate_union_native_list); | |
e38ac962 | 323 | validate_test_add("/visitor/input-strict/fail/struct", |
805017b7 | 324 | &testdata, test_validate_fail_struct); |
e38ac962 | 325 | validate_test_add("/visitor/input-strict/fail/struct-nested", |
805017b7 | 326 | &testdata, test_validate_fail_struct_nested); |
e38ac962 | 327 | validate_test_add("/visitor/input-strict/fail/list", |
805017b7 | 328 | &testdata, test_validate_fail_list); |
2fc00432 | 329 | validate_test_add("/visitor/input-strict/fail/union-flat", |
805017b7 | 330 | &testdata, test_validate_fail_union_flat); |
cb55111b | 331 | validate_test_add("/visitor/input-strict/fail/union-flat-no-discriminator", |
805017b7 | 332 | &testdata, test_validate_fail_union_flat_no_discrim); |
ab045267 EB |
333 | validate_test_add("/visitor/input-strict/fail/alternate", |
334 | &testdata, test_validate_fail_alternate); | |
805017b7 EB |
335 | validate_test_add("/visitor/input-strict/fail/union-native-list", |
336 | &testdata, test_validate_fail_union_native_list); | |
39a18158 MA |
337 | validate_test_add("/visitor/input-strict/pass/qmp-introspect", |
338 | &testdata, test_validate_qmp_introspect); | |
e38ac962 PB |
339 | |
340 | g_test_run(); | |
341 | ||
342 | return 0; | |
343 | } |