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"
23 typedef struct TestInputVisitorData
{
26 } TestInputVisitorData
;
28 static void validate_teardown(TestInputVisitorData
*data
,
31 qobject_decref(data
->obj
);
35 qmp_input_visitor_cleanup(data
->qiv
);
40 /* This is provided instead of a test setup function so that the JSON
41 string used by the tests are kept in the test functions (and not
43 static GCC_FMT_ATTR(2, 3)
44 Visitor
*validate_test_init(TestInputVisitorData
*data
,
45 const char *json_string
, ...)
50 va_start(ap
, json_string
);
51 data
->obj
= qobject_from_jsonv(json_string
, &ap
);
54 g_assert(data
->obj
!= NULL
);
56 data
->qiv
= qmp_input_visitor_new_strict(data
->obj
);
57 g_assert(data
->qiv
!= NULL
);
59 v
= qmp_input_get_visitor(data
->qiv
);
65 typedef struct TestStruct
72 static void visit_type_TestStruct(Visitor
*v
, TestStruct
**obj
,
73 const char *name
, Error
**errp
)
77 visit_start_struct(v
, (void **)obj
, "TestStruct", name
, sizeof(TestStruct
),
83 visit_type_int(v
, &(*obj
)->integer
, "integer", &err
);
87 visit_type_bool(v
, &(*obj
)->boolean
, "boolean", &err
);
91 visit_type_str(v
, &(*obj
)->string
, "string", &err
);
94 error_propagate(errp
, err
);
96 visit_end_struct(v
, &err
);
98 error_propagate(errp
, err
);
101 static void test_validate_struct(TestInputVisitorData
*data
,
104 TestStruct
*p
= NULL
;
108 v
= validate_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
110 visit_type_TestStruct(v
, &p
, NULL
, &err
);
116 static void test_validate_struct_nested(TestInputVisitorData
*data
,
119 UserDefTwo
*udp
= NULL
;
123 v
= validate_test_init(data
, "{ 'string0': 'string0', "
124 "'dict1': { 'string1': 'string1', "
125 "'dict2': { 'userdef': { 'integer': 42, "
126 "'string': 'string' }, 'string': 'string2'}}}");
128 visit_type_UserDefTwo(v
, &udp
, NULL
, &err
);
130 qapi_free_UserDefTwo(udp
);
133 static void test_validate_list(TestInputVisitorData
*data
,
136 UserDefOneList
*head
= NULL
;
140 v
= validate_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
142 visit_type_UserDefOneList(v
, &head
, NULL
, &err
);
144 qapi_free_UserDefOneList(head
);
147 static void test_validate_union_native_list(TestInputVisitorData
*data
,
150 UserDefNativeListUnion
*tmp
= NULL
;
154 v
= validate_test_init(data
, "{ 'type': 'integer', 'data' : [ 1, 2 ] }");
156 visit_type_UserDefNativeListUnion(v
, &tmp
, NULL
, &err
);
158 qapi_free_UserDefNativeListUnion(tmp
);
161 static void test_validate_union_flat(TestInputVisitorData
*data
,
164 UserDefFlatUnion
*tmp
= NULL
;
168 v
= validate_test_init(data
,
169 "{ 'enum1': 'value1', "
171 "'boolean': true }");
172 /* TODO when generator bug is fixed, add 'integer': 41 */
174 visit_type_UserDefFlatUnion(v
, &tmp
, NULL
, &err
);
176 qapi_free_UserDefFlatUnion(tmp
);
179 static void test_validate_alternate(TestInputVisitorData
*data
,
182 UserDefAlternate
*tmp
= NULL
;
186 v
= validate_test_init(data
, "42");
188 visit_type_UserDefAlternate(v
, &tmp
, NULL
, &err
);
190 qapi_free_UserDefAlternate(tmp
);
193 static void test_validate_fail_struct(TestInputVisitorData
*data
,
196 TestStruct
*p
= NULL
;
200 v
= validate_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
202 visit_type_TestStruct(v
, &p
, NULL
, &err
);
210 static void test_validate_fail_struct_nested(TestInputVisitorData
*data
,
213 UserDefTwo
*udp
= NULL
;
217 v
= validate_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
219 visit_type_UserDefTwo(v
, &udp
, NULL
, &err
);
221 qapi_free_UserDefTwo(udp
);
224 static void test_validate_fail_list(TestInputVisitorData
*data
,
227 UserDefOneList
*head
= NULL
;
231 v
= validate_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
233 visit_type_UserDefOneList(v
, &head
, NULL
, &err
);
235 qapi_free_UserDefOneList(head
);
238 static void test_validate_fail_union_native_list(TestInputVisitorData
*data
,
241 UserDefNativeListUnion
*tmp
= NULL
;
245 v
= validate_test_init(data
,
246 "{ 'type': 'integer', 'data' : [ 'string' ] }");
248 visit_type_UserDefNativeListUnion(v
, &tmp
, NULL
, &err
);
250 qapi_free_UserDefNativeListUnion(tmp
);
253 static void test_validate_fail_union_flat(TestInputVisitorData
*data
,
256 UserDefFlatUnion
*tmp
= NULL
;
260 v
= validate_test_init(data
, "{ 'string': 'c', 'integer': 41, 'boolean': true }");
262 visit_type_UserDefFlatUnion(v
, &tmp
, NULL
, &err
);
264 qapi_free_UserDefFlatUnion(tmp
);
267 static void test_validate_fail_union_flat_no_discrim(TestInputVisitorData
*data
,
270 UserDefFlatUnion2
*tmp
= NULL
;
274 /* test situation where discriminator field ('enum1' here) is missing */
275 v
= validate_test_init(data
, "{ 'string': 'c', 'string1': 'd', 'string2': 'e' }");
277 visit_type_UserDefFlatUnion2(v
, &tmp
, NULL
, &err
);
279 qapi_free_UserDefFlatUnion2(tmp
);
282 static void test_validate_fail_alternate(TestInputVisitorData
*data
,
285 UserDefAlternate
*tmp
= NULL
;
289 v
= validate_test_init(data
, "3.14");
291 visit_type_UserDefAlternate(v
, &tmp
, NULL
, &err
);
293 qapi_free_UserDefAlternate(tmp
);
296 static void validate_test_add(const char *testpath
,
297 TestInputVisitorData
*data
,
298 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
300 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
304 int main(int argc
, char **argv
)
306 TestInputVisitorData testdata
;
308 g_test_init(&argc
, &argv
, NULL
);
310 validate_test_add("/visitor/input-strict/pass/struct",
311 &testdata
, test_validate_struct
);
312 validate_test_add("/visitor/input-strict/pass/struct-nested",
313 &testdata
, test_validate_struct_nested
);
314 validate_test_add("/visitor/input-strict/pass/list",
315 &testdata
, test_validate_list
);
316 validate_test_add("/visitor/input-strict/pass/union-flat",
317 &testdata
, test_validate_union_flat
);
318 validate_test_add("/visitor/input-strict/pass/alternate",
319 &testdata
, test_validate_alternate
);
320 validate_test_add("/visitor/input-strict/pass/union-native-list",
321 &testdata
, test_validate_union_native_list
);
322 validate_test_add("/visitor/input-strict/fail/struct",
323 &testdata
, test_validate_fail_struct
);
324 validate_test_add("/visitor/input-strict/fail/struct-nested",
325 &testdata
, test_validate_fail_struct_nested
);
326 validate_test_add("/visitor/input-strict/fail/list",
327 &testdata
, test_validate_fail_list
);
328 validate_test_add("/visitor/input-strict/fail/union-flat",
329 &testdata
, test_validate_fail_union_flat
);
330 validate_test_add("/visitor/input-strict/fail/union-flat-no-discriminator",
331 &testdata
, test_validate_fail_union_flat_no_discrim
);
332 validate_test_add("/visitor/input-strict/fail/alternate",
333 &testdata
, test_validate_fail_alternate
);
334 validate_test_add("/visitor/input-strict/fail/union-native-list",
335 &testdata
, test_validate_fail_union_native_list
);