]>
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 | |
79ee7df8 | 16 | #include "qemu-common.h" |
da34e65c | 17 | #include "qapi/error.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 | ||
fc471c18 | 57 | data->qiv = qmp_input_visitor_new(data->obj, true); |
0920a171 EB |
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); |
68ab47e4 | 184 | g_assert(!p); |
e38ac962 PB |
185 | } |
186 | ||
187 | static void test_validate_fail_struct_nested(TestInputVisitorData *data, | |
188 | const void *unused) | |
189 | { | |
b6fcf32d | 190 | UserDefTwo *udp = NULL; |
e940f543 | 191 | Error *err = NULL; |
e38ac962 PB |
192 | Visitor *v; |
193 | ||
194 | v = validate_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}"); | |
195 | ||
51e72bc1 | 196 | visit_type_UserDefTwo(v, NULL, &udp, &err); |
a12a5a1a | 197 | error_free_or_abort(&err); |
68ab47e4 | 198 | g_assert(!udp); |
e38ac962 PB |
199 | } |
200 | ||
201 | static void test_validate_fail_list(TestInputVisitorData *data, | |
202 | const void *unused) | |
203 | { | |
204 | UserDefOneList *head = NULL; | |
e940f543 | 205 | Error *err = NULL; |
e38ac962 PB |
206 | Visitor *v; |
207 | ||
208 | v = validate_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]"); | |
209 | ||
51e72bc1 | 210 | visit_type_UserDefOneList(v, NULL, &head, &err); |
a12a5a1a | 211 | error_free_or_abort(&err); |
68ab47e4 | 212 | g_assert(!head); |
e38ac962 PB |
213 | } |
214 | ||
805017b7 EB |
215 | static void test_validate_fail_union_native_list(TestInputVisitorData *data, |
216 | const void *unused) | |
e38ac962 | 217 | { |
805017b7 | 218 | UserDefNativeListUnion *tmp = NULL; |
e940f543 | 219 | Error *err = NULL; |
e38ac962 PB |
220 | Visitor *v; |
221 | ||
805017b7 EB |
222 | v = validate_test_init(data, |
223 | "{ 'type': 'integer', 'data' : [ 'string' ] }"); | |
e38ac962 | 224 | |
51e72bc1 | 225 | visit_type_UserDefNativeListUnion(v, NULL, &tmp, &err); |
a12a5a1a | 226 | error_free_or_abort(&err); |
68ab47e4 | 227 | g_assert(!tmp); |
e38ac962 PB |
228 | } |
229 | ||
2fc00432 MA |
230 | static void test_validate_fail_union_flat(TestInputVisitorData *data, |
231 | const void *unused) | |
232 | { | |
233 | UserDefFlatUnion *tmp = NULL; | |
e940f543 | 234 | Error *err = NULL; |
2fc00432 MA |
235 | Visitor *v; |
236 | ||
237 | v = validate_test_init(data, "{ 'string': 'c', 'integer': 41, 'boolean': true }"); | |
238 | ||
51e72bc1 | 239 | visit_type_UserDefFlatUnion(v, NULL, &tmp, &err); |
a12a5a1a | 240 | error_free_or_abort(&err); |
68ab47e4 | 241 | g_assert(!tmp); |
2fc00432 MA |
242 | } |
243 | ||
cb55111b MR |
244 | static void test_validate_fail_union_flat_no_discrim(TestInputVisitorData *data, |
245 | const void *unused) | |
246 | { | |
247 | UserDefFlatUnion2 *tmp = NULL; | |
248 | Error *err = NULL; | |
249 | Visitor *v; | |
250 | ||
251 | /* test situation where discriminator field ('enum1' here) is missing */ | |
441cbac0 | 252 | v = validate_test_init(data, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }"); |
cb55111b | 253 | |
51e72bc1 | 254 | visit_type_UserDefFlatUnion2(v, NULL, &tmp, &err); |
a12a5a1a | 255 | error_free_or_abort(&err); |
68ab47e4 | 256 | g_assert(!tmp); |
cb55111b MR |
257 | } |
258 | ||
ab045267 EB |
259 | static void test_validate_fail_alternate(TestInputVisitorData *data, |
260 | const void *unused) | |
2c38b600 | 261 | { |
e58d695e | 262 | UserDefAlternate *tmp; |
2c38b600 | 263 | Visitor *v; |
e940f543 | 264 | Error *err = NULL; |
2c38b600 MA |
265 | |
266 | v = validate_test_init(data, "3.14"); | |
267 | ||
51e72bc1 | 268 | visit_type_UserDefAlternate(v, NULL, &tmp, &err); |
a12a5a1a | 269 | error_free_or_abort(&err); |
68ab47e4 | 270 | g_assert(!tmp); |
2c38b600 MA |
271 | } |
272 | ||
39a18158 MA |
273 | static void do_test_validate_qmp_introspect(TestInputVisitorData *data, |
274 | const char *schema_json) | |
275 | { | |
276 | SchemaInfoList *schema = NULL; | |
39a18158 MA |
277 | Visitor *v; |
278 | ||
279 | v = validate_test_init_raw(data, schema_json); | |
280 | ||
51e72bc1 | 281 | visit_type_SchemaInfoList(v, NULL, &schema, &error_abort); |
39a18158 MA |
282 | g_assert(schema); |
283 | ||
284 | qapi_free_SchemaInfoList(schema); | |
285 | } | |
286 | ||
287 | static void test_validate_qmp_introspect(TestInputVisitorData *data, | |
288 | const void *unused) | |
289 | { | |
290 | do_test_validate_qmp_introspect(data, test_qmp_schema_json); | |
291 | do_test_validate_qmp_introspect(data, qmp_schema_json); | |
292 | } | |
293 | ||
e38ac962 PB |
294 | static void validate_test_add(const char *testpath, |
295 | TestInputVisitorData *data, | |
296 | void (*test_func)(TestInputVisitorData *data, const void *user_data)) | |
297 | { | |
298 | g_test_add(testpath, TestInputVisitorData, data, NULL, test_func, | |
299 | validate_teardown); | |
300 | } | |
301 | ||
302 | int main(int argc, char **argv) | |
303 | { | |
304 | TestInputVisitorData testdata; | |
305 | ||
306 | g_test_init(&argc, &argv, NULL); | |
307 | ||
308 | validate_test_add("/visitor/input-strict/pass/struct", | |
805017b7 | 309 | &testdata, test_validate_struct); |
e38ac962 | 310 | validate_test_add("/visitor/input-strict/pass/struct-nested", |
805017b7 | 311 | &testdata, test_validate_struct_nested); |
e38ac962 | 312 | validate_test_add("/visitor/input-strict/pass/list", |
805017b7 | 313 | &testdata, test_validate_list); |
2fc00432 | 314 | validate_test_add("/visitor/input-strict/pass/union-flat", |
805017b7 | 315 | &testdata, test_validate_union_flat); |
ab045267 EB |
316 | validate_test_add("/visitor/input-strict/pass/alternate", |
317 | &testdata, test_validate_alternate); | |
805017b7 EB |
318 | validate_test_add("/visitor/input-strict/pass/union-native-list", |
319 | &testdata, test_validate_union_native_list); | |
e38ac962 | 320 | validate_test_add("/visitor/input-strict/fail/struct", |
805017b7 | 321 | &testdata, test_validate_fail_struct); |
e38ac962 | 322 | validate_test_add("/visitor/input-strict/fail/struct-nested", |
805017b7 | 323 | &testdata, test_validate_fail_struct_nested); |
e38ac962 | 324 | validate_test_add("/visitor/input-strict/fail/list", |
805017b7 | 325 | &testdata, test_validate_fail_list); |
2fc00432 | 326 | validate_test_add("/visitor/input-strict/fail/union-flat", |
805017b7 | 327 | &testdata, test_validate_fail_union_flat); |
cb55111b | 328 | validate_test_add("/visitor/input-strict/fail/union-flat-no-discriminator", |
805017b7 | 329 | &testdata, test_validate_fail_union_flat_no_discrim); |
ab045267 EB |
330 | validate_test_add("/visitor/input-strict/fail/alternate", |
331 | &testdata, test_validate_fail_alternate); | |
805017b7 EB |
332 | validate_test_add("/visitor/input-strict/fail/union-native-list", |
333 | &testdata, test_validate_fail_union_native_list); | |
39a18158 MA |
334 | validate_test_add("/visitor/input-strict/pass/qmp-introspect", |
335 | &testdata, test_validate_qmp_introspect); | |
e38ac962 PB |
336 | |
337 | g_test_run(); | |
338 | ||
339 | return 0; | |
340 | } |