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.
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
,
100 v
= validate_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
102 visit_type_TestStruct(v
, &p
, NULL
, &err
);
108 static void test_validate_struct_nested(TestInputVisitorData
*data
,
111 UserDefTwo
*udp
= NULL
;
115 v
= validate_test_init(data
, "{ 'string0': 'string0', "
116 "'dict1': { 'string1': 'string1', "
117 "'dict2': { 'userdef': { 'integer': 42, "
118 "'string': 'string' }, 'string': 'string2'}}}");
120 visit_type_UserDefTwo(v
, &udp
, NULL
, &err
);
122 qapi_free_UserDefTwo(udp
);
125 static void test_validate_list(TestInputVisitorData
*data
,
128 UserDefOneList
*head
= NULL
;
132 v
= validate_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
134 visit_type_UserDefOneList(v
, &head
, NULL
, &err
);
136 qapi_free_UserDefOneList(head
);
139 static void test_validate_union_native_list(TestInputVisitorData
*data
,
142 UserDefNativeListUnion
*tmp
= NULL
;
146 v
= validate_test_init(data
, "{ 'type': 'integer', 'data' : [ 1, 2 ] }");
148 visit_type_UserDefNativeListUnion(v
, &tmp
, NULL
, &err
);
150 qapi_free_UserDefNativeListUnion(tmp
);
153 static void test_validate_union_flat(TestInputVisitorData
*data
,
156 UserDefFlatUnion
*tmp
= NULL
;
160 v
= validate_test_init(data
,
161 "{ 'enum1': 'value1', "
164 "'boolean': true }");
166 visit_type_UserDefFlatUnion(v
, &tmp
, NULL
, &err
);
168 qapi_free_UserDefFlatUnion(tmp
);
171 static void test_validate_alternate(TestInputVisitorData
*data
,
174 UserDefAlternate
*tmp
= NULL
;
178 v
= validate_test_init(data
, "42");
180 visit_type_UserDefAlternate(v
, &tmp
, NULL
, &err
);
182 qapi_free_UserDefAlternate(tmp
);
185 static void test_validate_fail_struct(TestInputVisitorData
*data
,
188 TestStruct
*p
= NULL
;
192 v
= validate_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
194 visit_type_TestStruct(v
, &p
, NULL
, &err
);
203 static void test_validate_fail_struct_nested(TestInputVisitorData
*data
,
206 UserDefTwo
*udp
= NULL
;
210 v
= validate_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
212 visit_type_UserDefTwo(v
, &udp
, NULL
, &err
);
215 qapi_free_UserDefTwo(udp
);
218 static void test_validate_fail_list(TestInputVisitorData
*data
,
221 UserDefOneList
*head
= NULL
;
225 v
= validate_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
227 visit_type_UserDefOneList(v
, &head
, NULL
, &err
);
230 qapi_free_UserDefOneList(head
);
233 static void test_validate_fail_union_native_list(TestInputVisitorData
*data
,
236 UserDefNativeListUnion
*tmp
= NULL
;
240 v
= validate_test_init(data
,
241 "{ 'type': 'integer', 'data' : [ 'string' ] }");
243 visit_type_UserDefNativeListUnion(v
, &tmp
, NULL
, &err
);
246 qapi_free_UserDefNativeListUnion(tmp
);
249 static void test_validate_fail_union_flat(TestInputVisitorData
*data
,
252 UserDefFlatUnion
*tmp
= NULL
;
256 v
= validate_test_init(data
, "{ 'string': 'c', 'integer': 41, 'boolean': true }");
258 visit_type_UserDefFlatUnion(v
, &tmp
, NULL
, &err
);
261 qapi_free_UserDefFlatUnion(tmp
);
264 static void test_validate_fail_union_flat_no_discrim(TestInputVisitorData
*data
,
267 UserDefFlatUnion2
*tmp
= NULL
;
271 /* test situation where discriminator field ('enum1' here) is missing */
272 v
= validate_test_init(data
, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }");
274 visit_type_UserDefFlatUnion2(v
, &tmp
, NULL
, &err
);
277 qapi_free_UserDefFlatUnion2(tmp
);
280 static void test_validate_fail_alternate(TestInputVisitorData
*data
,
283 UserDefAlternate
*tmp
= NULL
;
287 v
= validate_test_init(data
, "3.14");
289 visit_type_UserDefAlternate(v
, &tmp
, NULL
, &err
);
292 qapi_free_UserDefAlternate(tmp
);
295 static void do_test_validate_qmp_introspect(TestInputVisitorData
*data
,
296 const char *schema_json
)
298 SchemaInfoList
*schema
= NULL
;
302 v
= validate_test_init_raw(data
, schema_json
);
304 visit_type_SchemaInfoList(v
, &schema
, NULL
, &err
);
306 fprintf(stderr
, "%s", error_get_pretty(err
));
311 qapi_free_SchemaInfoList(schema
);
314 static void test_validate_qmp_introspect(TestInputVisitorData
*data
,
317 do_test_validate_qmp_introspect(data
, test_qmp_schema_json
);
318 do_test_validate_qmp_introspect(data
, qmp_schema_json
);
321 static void validate_test_add(const char *testpath
,
322 TestInputVisitorData
*data
,
323 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
325 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
329 int main(int argc
, char **argv
)
331 TestInputVisitorData testdata
;
333 g_test_init(&argc
, &argv
, NULL
);
335 validate_test_add("/visitor/input-strict/pass/struct",
336 &testdata
, test_validate_struct
);
337 validate_test_add("/visitor/input-strict/pass/struct-nested",
338 &testdata
, test_validate_struct_nested
);
339 validate_test_add("/visitor/input-strict/pass/list",
340 &testdata
, test_validate_list
);
341 validate_test_add("/visitor/input-strict/pass/union-flat",
342 &testdata
, test_validate_union_flat
);
343 validate_test_add("/visitor/input-strict/pass/alternate",
344 &testdata
, test_validate_alternate
);
345 validate_test_add("/visitor/input-strict/pass/union-native-list",
346 &testdata
, test_validate_union_native_list
);
347 validate_test_add("/visitor/input-strict/fail/struct",
348 &testdata
, test_validate_fail_struct
);
349 validate_test_add("/visitor/input-strict/fail/struct-nested",
350 &testdata
, test_validate_fail_struct_nested
);
351 validate_test_add("/visitor/input-strict/fail/list",
352 &testdata
, test_validate_fail_list
);
353 validate_test_add("/visitor/input-strict/fail/union-flat",
354 &testdata
, test_validate_fail_union_flat
);
355 validate_test_add("/visitor/input-strict/fail/union-flat-no-discriminator",
356 &testdata
, test_validate_fail_union_flat_no_discrim
);
357 validate_test_add("/visitor/input-strict/fail/alternate",
358 &testdata
, test_validate_fail_alternate
);
359 validate_test_add("/visitor/input-strict/fail/union-native-list",
360 &testdata
, test_validate_fail_union_native_list
);
361 validate_test_add("/visitor/input-strict/pass/qmp-introspect",
362 &testdata
, test_validate_qmp_introspect
);