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"
16 #include "qemu-common.h"
17 #include "qapi/error.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(data
->obj
, true);
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
);
187 static void test_validate_fail_struct_nested(TestInputVisitorData
*data
,
190 UserDefTwo
*udp
= NULL
;
194 v
= validate_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
196 visit_type_UserDefTwo(v
, NULL
, &udp
, &err
);
197 error_free_or_abort(&err
);
201 static void test_validate_fail_list(TestInputVisitorData
*data
,
204 UserDefOneList
*head
= NULL
;
208 v
= validate_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
210 visit_type_UserDefOneList(v
, NULL
, &head
, &err
);
211 error_free_or_abort(&err
);
215 static void test_validate_fail_union_native_list(TestInputVisitorData
*data
,
218 UserDefNativeListUnion
*tmp
= NULL
;
222 v
= validate_test_init(data
,
223 "{ 'type': 'integer', 'data' : [ 'string' ] }");
225 visit_type_UserDefNativeListUnion(v
, NULL
, &tmp
, &err
);
226 error_free_or_abort(&err
);
230 static void test_validate_fail_union_flat(TestInputVisitorData
*data
,
233 UserDefFlatUnion
*tmp
= NULL
;
237 v
= validate_test_init(data
, "{ 'string': 'c', 'integer': 41, 'boolean': true }");
239 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &err
);
240 error_free_or_abort(&err
);
244 static void test_validate_fail_union_flat_no_discrim(TestInputVisitorData
*data
,
247 UserDefFlatUnion2
*tmp
= NULL
;
251 /* test situation where discriminator field ('enum1' here) is missing */
252 v
= validate_test_init(data
, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }");
254 visit_type_UserDefFlatUnion2(v
, NULL
, &tmp
, &err
);
255 error_free_or_abort(&err
);
259 static void test_validate_fail_alternate(TestInputVisitorData
*data
,
262 UserDefAlternate
*tmp
;
266 v
= validate_test_init(data
, "3.14");
268 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &err
);
269 error_free_or_abort(&err
);
273 static void do_test_validate_qmp_introspect(TestInputVisitorData
*data
,
274 const char *schema_json
)
276 SchemaInfoList
*schema
= NULL
;
279 v
= validate_test_init_raw(data
, schema_json
);
281 visit_type_SchemaInfoList(v
, NULL
, &schema
, &error_abort
);
284 qapi_free_SchemaInfoList(schema
);
287 static void test_validate_qmp_introspect(TestInputVisitorData
*data
,
290 do_test_validate_qmp_introspect(data
, test_qmp_schema_json
);
291 do_test_validate_qmp_introspect(data
, qmp_schema_json
);
294 static void validate_test_add(const char *testpath
,
295 TestInputVisitorData
*data
,
296 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
298 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
302 int main(int argc
, char **argv
)
304 TestInputVisitorData testdata
;
306 g_test_init(&argc
, &argv
, NULL
);
308 validate_test_add("/visitor/input-strict/pass/struct",
309 &testdata
, test_validate_struct
);
310 validate_test_add("/visitor/input-strict/pass/struct-nested",
311 &testdata
, test_validate_struct_nested
);
312 validate_test_add("/visitor/input-strict/pass/list",
313 &testdata
, test_validate_list
);
314 validate_test_add("/visitor/input-strict/pass/union-flat",
315 &testdata
, test_validate_union_flat
);
316 validate_test_add("/visitor/input-strict/pass/alternate",
317 &testdata
, test_validate_alternate
);
318 validate_test_add("/visitor/input-strict/pass/union-native-list",
319 &testdata
, test_validate_union_native_list
);
320 validate_test_add("/visitor/input-strict/fail/struct",
321 &testdata
, test_validate_fail_struct
);
322 validate_test_add("/visitor/input-strict/fail/struct-nested",
323 &testdata
, test_validate_fail_struct_nested
);
324 validate_test_add("/visitor/input-strict/fail/list",
325 &testdata
, test_validate_fail_list
);
326 validate_test_add("/visitor/input-strict/fail/union-flat",
327 &testdata
, test_validate_fail_union_flat
);
328 validate_test_add("/visitor/input-strict/fail/union-flat-no-discriminator",
329 &testdata
, test_validate_fail_union_flat_no_discrim
);
330 validate_test_add("/visitor/input-strict/fail/alternate",
331 &testdata
, test_validate_fail_alternate
);
332 validate_test_add("/visitor/input-strict/fail/union-native-list",
333 &testdata
, test_validate_fail_union_native_list
);
334 validate_test_add("/visitor/input-strict/pass/qmp-introspect",
335 &testdata
, test_validate_qmp_introspect
);