2 * QMP Input Visitor unit-tests (strict mode).
4 * Copyright (C) 2011-2012, 2015 Red Hat Inc.
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 * Paolo Bonzini <pbonzini@redhat.com>
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.
14 #include "qemu/osdep.h"
17 #include "qemu-common.h"
18 #include "qapi/error.h"
19 #include "qapi/qmp-input-visitor.h"
20 #include "test-qapi-types.h"
21 #include "test-qapi-visit.h"
22 #include "qapi/qmp/types.h"
23 #include "test-qmp-introspect.h"
24 #include "qmp-introspect.h"
25 #include "qapi-visit.h"
27 typedef struct TestInputVisitorData
{
30 } TestInputVisitorData
;
32 static void validate_teardown(TestInputVisitorData
*data
,
35 qobject_decref(data
->obj
);
39 qmp_input_visitor_cleanup(data
->qiv
);
44 /* The various test_init functions are provided instead of a test setup
45 function so that the JSON string used by the tests are kept in the test
46 functions (and not in main()). */
47 static Visitor
*validate_test_init_internal(TestInputVisitorData
*data
,
48 const char *json_string
,
53 validate_teardown(data
, NULL
);
55 data
->obj
= qobject_from_jsonv(json_string
, ap
);
58 data
->qiv
= qmp_input_visitor_new_strict(data
->obj
);
61 v
= qmp_input_get_visitor(data
->qiv
);
67 static GCC_FMT_ATTR(2, 3)
68 Visitor
*validate_test_init(TestInputVisitorData
*data
,
69 const char *json_string
, ...)
74 va_start(ap
, json_string
);
75 v
= validate_test_init_internal(data
, json_string
, &ap
);
80 /* similar to validate_test_init(), but does not expect a string
81 * literal/format json_string argument and so can be used for
82 * programatically generated strings (and we can't pass in programatically
83 * generated strings via %s format parameters since qobject_from_jsonv()
84 * will wrap those in double-quotes and treat the entire object as a
87 static Visitor
*validate_test_init_raw(TestInputVisitorData
*data
,
88 const char *json_string
)
90 return validate_test_init_internal(data
, json_string
, NULL
);
94 static void test_validate_struct(TestInputVisitorData
*data
,
100 v
= validate_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
102 visit_type_TestStruct(v
, NULL
, &p
, &error_abort
);
107 static void test_validate_struct_nested(TestInputVisitorData
*data
,
110 UserDefTwo
*udp
= NULL
;
113 v
= validate_test_init(data
, "{ 'string0': 'string0', "
114 "'dict1': { 'string1': 'string1', "
115 "'dict2': { 'userdef': { 'integer': 42, "
116 "'string': 'string' }, 'string': 'string2'}}}");
118 visit_type_UserDefTwo(v
, NULL
, &udp
, &error_abort
);
119 qapi_free_UserDefTwo(udp
);
122 static void test_validate_list(TestInputVisitorData
*data
,
125 UserDefOneList
*head
= NULL
;
128 v
= validate_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
130 visit_type_UserDefOneList(v
, NULL
, &head
, &error_abort
);
131 qapi_free_UserDefOneList(head
);
134 static void test_validate_union_native_list(TestInputVisitorData
*data
,
137 UserDefNativeListUnion
*tmp
= NULL
;
140 v
= validate_test_init(data
, "{ 'type': 'integer', 'data' : [ 1, 2 ] }");
142 visit_type_UserDefNativeListUnion(v
, NULL
, &tmp
, &error_abort
);
143 qapi_free_UserDefNativeListUnion(tmp
);
146 static void test_validate_union_flat(TestInputVisitorData
*data
,
149 UserDefFlatUnion
*tmp
= NULL
;
152 v
= validate_test_init(data
,
153 "{ 'enum1': 'value1', "
156 "'boolean': true }");
158 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &error_abort
);
159 qapi_free_UserDefFlatUnion(tmp
);
162 static void test_validate_alternate(TestInputVisitorData
*data
,
165 UserDefAlternate
*tmp
= NULL
;
168 v
= validate_test_init(data
, "42");
170 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
171 qapi_free_UserDefAlternate(tmp
);
174 static void test_validate_fail_struct(TestInputVisitorData
*data
,
177 TestStruct
*p
= NULL
;
181 v
= validate_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
183 visit_type_TestStruct(v
, NULL
, &p
, &err
);
184 error_free_or_abort(&err
);
191 static void test_validate_fail_struct_nested(TestInputVisitorData
*data
,
194 UserDefTwo
*udp
= NULL
;
198 v
= validate_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
200 visit_type_UserDefTwo(v
, NULL
, &udp
, &err
);
201 error_free_or_abort(&err
);
202 qapi_free_UserDefTwo(udp
);
205 static void test_validate_fail_list(TestInputVisitorData
*data
,
208 UserDefOneList
*head
= NULL
;
212 v
= validate_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
214 visit_type_UserDefOneList(v
, NULL
, &head
, &err
);
215 error_free_or_abort(&err
);
216 qapi_free_UserDefOneList(head
);
219 static void test_validate_fail_union_native_list(TestInputVisitorData
*data
,
222 UserDefNativeListUnion
*tmp
= NULL
;
226 v
= validate_test_init(data
,
227 "{ 'type': 'integer', 'data' : [ 'string' ] }");
229 visit_type_UserDefNativeListUnion(v
, NULL
, &tmp
, &err
);
230 error_free_or_abort(&err
);
231 qapi_free_UserDefNativeListUnion(tmp
);
234 static void test_validate_fail_union_flat(TestInputVisitorData
*data
,
237 UserDefFlatUnion
*tmp
= NULL
;
241 v
= validate_test_init(data
, "{ 'string': 'c', 'integer': 41, 'boolean': true }");
243 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &err
);
244 error_free_or_abort(&err
);
245 qapi_free_UserDefFlatUnion(tmp
);
248 static void test_validate_fail_union_flat_no_discrim(TestInputVisitorData
*data
,
251 UserDefFlatUnion2
*tmp
= NULL
;
255 /* test situation where discriminator field ('enum1' here) is missing */
256 v
= validate_test_init(data
, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }");
258 visit_type_UserDefFlatUnion2(v
, NULL
, &tmp
, &err
);
259 error_free_or_abort(&err
);
260 qapi_free_UserDefFlatUnion2(tmp
);
263 static void test_validate_fail_alternate(TestInputVisitorData
*data
,
266 UserDefAlternate
*tmp
= NULL
;
270 v
= validate_test_init(data
, "3.14");
272 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &err
);
273 error_free_or_abort(&err
);
274 qapi_free_UserDefAlternate(tmp
);
277 static void do_test_validate_qmp_introspect(TestInputVisitorData
*data
,
278 const char *schema_json
)
280 SchemaInfoList
*schema
= NULL
;
283 v
= validate_test_init_raw(data
, schema_json
);
285 visit_type_SchemaInfoList(v
, NULL
, &schema
, &error_abort
);
288 qapi_free_SchemaInfoList(schema
);
291 static void test_validate_qmp_introspect(TestInputVisitorData
*data
,
294 do_test_validate_qmp_introspect(data
, test_qmp_schema_json
);
295 do_test_validate_qmp_introspect(data
, qmp_schema_json
);
298 static void validate_test_add(const char *testpath
,
299 TestInputVisitorData
*data
,
300 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
302 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
306 int main(int argc
, char **argv
)
308 TestInputVisitorData testdata
;
310 g_test_init(&argc
, &argv
, NULL
);
312 validate_test_add("/visitor/input-strict/pass/struct",
313 &testdata
, test_validate_struct
);
314 validate_test_add("/visitor/input-strict/pass/struct-nested",
315 &testdata
, test_validate_struct_nested
);
316 validate_test_add("/visitor/input-strict/pass/list",
317 &testdata
, test_validate_list
);
318 validate_test_add("/visitor/input-strict/pass/union-flat",
319 &testdata
, test_validate_union_flat
);
320 validate_test_add("/visitor/input-strict/pass/alternate",
321 &testdata
, test_validate_alternate
);
322 validate_test_add("/visitor/input-strict/pass/union-native-list",
323 &testdata
, test_validate_union_native_list
);
324 validate_test_add("/visitor/input-strict/fail/struct",
325 &testdata
, test_validate_fail_struct
);
326 validate_test_add("/visitor/input-strict/fail/struct-nested",
327 &testdata
, test_validate_fail_struct_nested
);
328 validate_test_add("/visitor/input-strict/fail/list",
329 &testdata
, test_validate_fail_list
);
330 validate_test_add("/visitor/input-strict/fail/union-flat",
331 &testdata
, test_validate_fail_union_flat
);
332 validate_test_add("/visitor/input-strict/fail/union-flat-no-discriminator",
333 &testdata
, test_validate_fail_union_flat_no_discrim
);
334 validate_test_add("/visitor/input-strict/fail/alternate",
335 &testdata
, test_validate_fail_alternate
);
336 validate_test_add("/visitor/input-strict/fail/union-native-list",
337 &testdata
, test_validate_fail_union_native_list
);
338 validate_test_add("/visitor/input-strict/pass/qmp-introspect",
339 &testdata
, test_validate_qmp_introspect
);