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/qmp-input-visitor.h"
19 #include "test-qapi-types.h"
20 #include "test-qapi-visit.h"
21 #include "qapi/qmp/types.h"
22 #include "test-qmp-introspect.h"
23 #include "qmp-introspect.h"
24 #include "qapi-visit.h"
26 typedef struct TestInputVisitorData
{
29 } TestInputVisitorData
;
31 static void validate_teardown(TestInputVisitorData
*data
,
34 qobject_decref(data
->obj
);
38 qmp_input_visitor_cleanup(data
->qiv
);
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
,
52 validate_teardown(data
, NULL
);
54 data
->obj
= qobject_from_jsonv(json_string
, ap
);
57 data
->qiv
= qmp_input_visitor_new_strict(data
->obj
);
60 v
= qmp_input_get_visitor(data
->qiv
);
66 static GCC_FMT_ATTR(2, 3)
67 Visitor
*validate_test_init(TestInputVisitorData
*data
,
68 const char *json_string
, ...)
73 va_start(ap
, json_string
);
74 v
= validate_test_init_internal(data
, json_string
, &ap
);
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
86 static Visitor
*validate_test_init_raw(TestInputVisitorData
*data
,
87 const char *json_string
)
89 return validate_test_init_internal(data
, json_string
, NULL
);
93 static void test_validate_struct(TestInputVisitorData
*data
,
99 v
= validate_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
101 visit_type_TestStruct(v
, NULL
, &p
, &error_abort
);
106 static void test_validate_struct_nested(TestInputVisitorData
*data
,
109 UserDefTwo
*udp
= NULL
;
112 v
= validate_test_init(data
, "{ 'string0': 'string0', "
113 "'dict1': { 'string1': 'string1', "
114 "'dict2': { 'userdef': { 'integer': 42, "
115 "'string': 'string' }, 'string': 'string2'}}}");
117 visit_type_UserDefTwo(v
, NULL
, &udp
, &error_abort
);
118 qapi_free_UserDefTwo(udp
);
121 static void test_validate_list(TestInputVisitorData
*data
,
124 UserDefOneList
*head
= NULL
;
127 v
= validate_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
129 visit_type_UserDefOneList(v
, NULL
, &head
, &error_abort
);
130 qapi_free_UserDefOneList(head
);
133 static void test_validate_union_native_list(TestInputVisitorData
*data
,
136 UserDefNativeListUnion
*tmp
= NULL
;
139 v
= validate_test_init(data
, "{ 'type': 'integer', 'data' : [ 1, 2 ] }");
141 visit_type_UserDefNativeListUnion(v
, NULL
, &tmp
, &error_abort
);
142 qapi_free_UserDefNativeListUnion(tmp
);
145 static void test_validate_union_flat(TestInputVisitorData
*data
,
148 UserDefFlatUnion
*tmp
= NULL
;
151 v
= validate_test_init(data
,
152 "{ 'enum1': 'value1', "
155 "'boolean': true }");
157 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &error_abort
);
158 qapi_free_UserDefFlatUnion(tmp
);
161 static void test_validate_alternate(TestInputVisitorData
*data
,
164 UserDefAlternate
*tmp
= NULL
;
167 v
= validate_test_init(data
, "42");
169 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
170 qapi_free_UserDefAlternate(tmp
);
173 static void test_validate_fail_struct(TestInputVisitorData
*data
,
176 TestStruct
*p
= NULL
;
180 v
= validate_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
182 visit_type_TestStruct(v
, NULL
, &p
, &err
);
183 error_free_or_abort(&err
);
190 static void test_validate_fail_struct_nested(TestInputVisitorData
*data
,
193 UserDefTwo
*udp
= NULL
;
197 v
= validate_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
199 visit_type_UserDefTwo(v
, NULL
, &udp
, &err
);
200 error_free_or_abort(&err
);
201 qapi_free_UserDefTwo(udp
);
204 static void test_validate_fail_list(TestInputVisitorData
*data
,
207 UserDefOneList
*head
= NULL
;
211 v
= validate_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
213 visit_type_UserDefOneList(v
, NULL
, &head
, &err
);
214 error_free_or_abort(&err
);
215 qapi_free_UserDefOneList(head
);
218 static void test_validate_fail_union_native_list(TestInputVisitorData
*data
,
221 UserDefNativeListUnion
*tmp
= NULL
;
225 v
= validate_test_init(data
,
226 "{ 'type': 'integer', 'data' : [ 'string' ] }");
228 visit_type_UserDefNativeListUnion(v
, NULL
, &tmp
, &err
);
229 error_free_or_abort(&err
);
230 qapi_free_UserDefNativeListUnion(tmp
);
233 static void test_validate_fail_union_flat(TestInputVisitorData
*data
,
236 UserDefFlatUnion
*tmp
= NULL
;
240 v
= validate_test_init(data
, "{ 'string': 'c', 'integer': 41, 'boolean': true }");
242 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &err
);
243 error_free_or_abort(&err
);
244 qapi_free_UserDefFlatUnion(tmp
);
247 static void test_validate_fail_union_flat_no_discrim(TestInputVisitorData
*data
,
250 UserDefFlatUnion2
*tmp
= NULL
;
254 /* test situation where discriminator field ('enum1' here) is missing */
255 v
= validate_test_init(data
, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }");
257 visit_type_UserDefFlatUnion2(v
, NULL
, &tmp
, &err
);
258 error_free_or_abort(&err
);
259 qapi_free_UserDefFlatUnion2(tmp
);
262 static void test_validate_fail_alternate(TestInputVisitorData
*data
,
265 UserDefAlternate
*tmp
= NULL
;
269 v
= validate_test_init(data
, "3.14");
271 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &err
);
272 error_free_or_abort(&err
);
273 qapi_free_UserDefAlternate(tmp
);
276 static void do_test_validate_qmp_introspect(TestInputVisitorData
*data
,
277 const char *schema_json
)
279 SchemaInfoList
*schema
= NULL
;
282 v
= validate_test_init_raw(data
, schema_json
);
284 visit_type_SchemaInfoList(v
, NULL
, &schema
, &error_abort
);
287 qapi_free_SchemaInfoList(schema
);
290 static void test_validate_qmp_introspect(TestInputVisitorData
*data
,
293 do_test_validate_qmp_introspect(data
, test_qmp_schema_json
);
294 do_test_validate_qmp_introspect(data
, qmp_schema_json
);
297 static void validate_test_add(const char *testpath
,
298 TestInputVisitorData
*data
,
299 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
301 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
305 int main(int argc
, char **argv
)
307 TestInputVisitorData testdata
;
309 g_test_init(&argc
, &argv
, NULL
);
311 validate_test_add("/visitor/input-strict/pass/struct",
312 &testdata
, test_validate_struct
);
313 validate_test_add("/visitor/input-strict/pass/struct-nested",
314 &testdata
, test_validate_struct_nested
);
315 validate_test_add("/visitor/input-strict/pass/list",
316 &testdata
, test_validate_list
);
317 validate_test_add("/visitor/input-strict/pass/union-flat",
318 &testdata
, test_validate_union_flat
);
319 validate_test_add("/visitor/input-strict/pass/alternate",
320 &testdata
, test_validate_alternate
);
321 validate_test_add("/visitor/input-strict/pass/union-native-list",
322 &testdata
, test_validate_union_native_list
);
323 validate_test_add("/visitor/input-strict/fail/struct",
324 &testdata
, test_validate_fail_struct
);
325 validate_test_add("/visitor/input-strict/fail/struct-nested",
326 &testdata
, test_validate_fail_struct_nested
);
327 validate_test_add("/visitor/input-strict/fail/list",
328 &testdata
, test_validate_fail_list
);
329 validate_test_add("/visitor/input-strict/fail/union-flat",
330 &testdata
, test_validate_fail_union_flat
);
331 validate_test_add("/visitor/input-strict/fail/union-flat-no-discriminator",
332 &testdata
, test_validate_fail_union_flat_no_discrim
);
333 validate_test_add("/visitor/input-strict/fail/alternate",
334 &testdata
, test_validate_fail_alternate
);
335 validate_test_add("/visitor/input-strict/fail/union-native-list",
336 &testdata
, test_validate_fail_union_native_list
);
337 validate_test_add("/visitor/input-strict/pass/qmp-introspect",
338 &testdata
, test_validate_qmp_introspect
);