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 "qapi/qmp/qjson.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 visit_free(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
,
51 validate_teardown(data
, NULL
);
53 data
->obj
= qobject_from_jsonv(json_string
, ap
);
56 data
->qiv
= qmp_input_visitor_new(data
->obj
, true);
61 static GCC_FMT_ATTR(2, 3)
62 Visitor
*validate_test_init(TestInputVisitorData
*data
,
63 const char *json_string
, ...)
68 va_start(ap
, json_string
);
69 v
= validate_test_init_internal(data
, json_string
, &ap
);
74 /* similar to validate_test_init(), but does not expect a string
75 * literal/format json_string argument and so can be used for
76 * programatically generated strings (and we can't pass in programatically
77 * generated strings via %s format parameters since qobject_from_jsonv()
78 * will wrap those in double-quotes and treat the entire object as a
81 static Visitor
*validate_test_init_raw(TestInputVisitorData
*data
,
82 const char *json_string
)
84 return validate_test_init_internal(data
, json_string
, NULL
);
88 static void test_validate_struct(TestInputVisitorData
*data
,
94 v
= validate_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
96 visit_type_TestStruct(v
, NULL
, &p
, &error_abort
);
101 static void test_validate_struct_nested(TestInputVisitorData
*data
,
104 UserDefTwo
*udp
= NULL
;
107 v
= validate_test_init(data
, "{ 'string0': 'string0', "
108 "'dict1': { 'string1': 'string1', "
109 "'dict2': { 'userdef': { 'integer': 42, "
110 "'string': 'string' }, 'string': 'string2'}}}");
112 visit_type_UserDefTwo(v
, NULL
, &udp
, &error_abort
);
113 qapi_free_UserDefTwo(udp
);
116 static void test_validate_list(TestInputVisitorData
*data
,
119 UserDefOneList
*head
= NULL
;
122 v
= validate_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
124 visit_type_UserDefOneList(v
, NULL
, &head
, &error_abort
);
125 qapi_free_UserDefOneList(head
);
128 static void test_validate_union_native_list(TestInputVisitorData
*data
,
131 UserDefNativeListUnion
*tmp
= NULL
;
134 v
= validate_test_init(data
, "{ 'type': 'integer', 'data' : [ 1, 2 ] }");
136 visit_type_UserDefNativeListUnion(v
, NULL
, &tmp
, &error_abort
);
137 qapi_free_UserDefNativeListUnion(tmp
);
140 static void test_validate_union_flat(TestInputVisitorData
*data
,
143 UserDefFlatUnion
*tmp
= NULL
;
146 v
= validate_test_init(data
,
147 "{ 'enum1': 'value1', "
150 "'boolean': true }");
152 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &error_abort
);
153 qapi_free_UserDefFlatUnion(tmp
);
156 static void test_validate_alternate(TestInputVisitorData
*data
,
159 UserDefAlternate
*tmp
= NULL
;
162 v
= validate_test_init(data
, "42");
164 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
165 qapi_free_UserDefAlternate(tmp
);
168 static void test_validate_fail_struct(TestInputVisitorData
*data
,
171 TestStruct
*p
= NULL
;
175 v
= validate_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
177 visit_type_TestStruct(v
, NULL
, &p
, &err
);
178 error_free_or_abort(&err
);
182 static void test_validate_fail_struct_nested(TestInputVisitorData
*data
,
185 UserDefTwo
*udp
= NULL
;
189 v
= validate_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
191 visit_type_UserDefTwo(v
, NULL
, &udp
, &err
);
192 error_free_or_abort(&err
);
196 static void test_validate_fail_list(TestInputVisitorData
*data
,
199 UserDefOneList
*head
= NULL
;
203 v
= validate_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
205 visit_type_UserDefOneList(v
, NULL
, &head
, &err
);
206 error_free_or_abort(&err
);
210 static void test_validate_fail_union_native_list(TestInputVisitorData
*data
,
213 UserDefNativeListUnion
*tmp
= NULL
;
217 v
= validate_test_init(data
,
218 "{ 'type': 'integer', 'data' : [ 'string' ] }");
220 visit_type_UserDefNativeListUnion(v
, NULL
, &tmp
, &err
);
221 error_free_or_abort(&err
);
225 static void test_validate_fail_union_flat(TestInputVisitorData
*data
,
228 UserDefFlatUnion
*tmp
= NULL
;
232 v
= validate_test_init(data
, "{ 'string': 'c', 'integer': 41, 'boolean': true }");
234 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &err
);
235 error_free_or_abort(&err
);
239 static void test_validate_fail_union_flat_no_discrim(TestInputVisitorData
*data
,
242 UserDefFlatUnion2
*tmp
= NULL
;
246 /* test situation where discriminator field ('enum1' here) is missing */
247 v
= validate_test_init(data
, "{ 'integer': 42, 'string': 'c', 'string1': 'd', 'string2': 'e' }");
249 visit_type_UserDefFlatUnion2(v
, NULL
, &tmp
, &err
);
250 error_free_or_abort(&err
);
254 static void test_validate_fail_alternate(TestInputVisitorData
*data
,
257 UserDefAlternate
*tmp
;
261 v
= validate_test_init(data
, "3.14");
263 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &err
);
264 error_free_or_abort(&err
);
268 static void do_test_validate_qmp_introspect(TestInputVisitorData
*data
,
269 const char *schema_json
)
271 SchemaInfoList
*schema
= NULL
;
274 v
= validate_test_init_raw(data
, schema_json
);
276 visit_type_SchemaInfoList(v
, NULL
, &schema
, &error_abort
);
279 qapi_free_SchemaInfoList(schema
);
282 static void test_validate_qmp_introspect(TestInputVisitorData
*data
,
285 do_test_validate_qmp_introspect(data
, test_qmp_schema_json
);
286 do_test_validate_qmp_introspect(data
, qmp_schema_json
);
289 static void validate_test_add(const char *testpath
,
290 TestInputVisitorData
*data
,
291 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
293 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
297 int main(int argc
, char **argv
)
299 TestInputVisitorData testdata
;
301 g_test_init(&argc
, &argv
, NULL
);
303 validate_test_add("/visitor/input-strict/pass/struct",
304 &testdata
, test_validate_struct
);
305 validate_test_add("/visitor/input-strict/pass/struct-nested",
306 &testdata
, test_validate_struct_nested
);
307 validate_test_add("/visitor/input-strict/pass/list",
308 &testdata
, test_validate_list
);
309 validate_test_add("/visitor/input-strict/pass/union-flat",
310 &testdata
, test_validate_union_flat
);
311 validate_test_add("/visitor/input-strict/pass/alternate",
312 &testdata
, test_validate_alternate
);
313 validate_test_add("/visitor/input-strict/pass/union-native-list",
314 &testdata
, test_validate_union_native_list
);
315 validate_test_add("/visitor/input-strict/fail/struct",
316 &testdata
, test_validate_fail_struct
);
317 validate_test_add("/visitor/input-strict/fail/struct-nested",
318 &testdata
, test_validate_fail_struct_nested
);
319 validate_test_add("/visitor/input-strict/fail/list",
320 &testdata
, test_validate_fail_list
);
321 validate_test_add("/visitor/input-strict/fail/union-flat",
322 &testdata
, test_validate_fail_union_flat
);
323 validate_test_add("/visitor/input-strict/fail/union-flat-no-discriminator",
324 &testdata
, test_validate_fail_union_flat_no_discrim
);
325 validate_test_add("/visitor/input-strict/fail/alternate",
326 &testdata
, test_validate_fail_alternate
);
327 validate_test_add("/visitor/input-strict/fail/union-native-list",
328 &testdata
, test_validate_fail_union_native_list
);
329 validate_test_add("/visitor/input-strict/pass/qmp-introspect",
330 &testdata
, test_validate_qmp_introspect
);