2 * QMP Input Visitor unit-tests (strict mode).
4 * Copyright (C) 2011-2012 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 UserDefNested
*udp
= NULL
;
123 v
= validate_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string' }, 'string2': 'string2'}}}");
125 visit_type_UserDefNested(v
, &udp
, NULL
, &err
);
127 qapi_free_UserDefNested(udp
);
130 static void test_validate_list(TestInputVisitorData
*data
,
133 UserDefOneList
*head
= NULL
;
137 v
= validate_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
139 visit_type_UserDefOneList(v
, &head
, NULL
, &err
);
141 qapi_free_UserDefOneList(head
);
144 static void test_validate_union(TestInputVisitorData
*data
,
147 UserDefUnion
*tmp
= NULL
;
151 v
= validate_test_init(data
, "{ 'type': 'b', 'integer': 41, 'data' : { 'integer': 42 } }");
153 visit_type_UserDefUnion(v
, &tmp
, NULL
, &err
);
155 qapi_free_UserDefUnion(tmp
);
158 static void test_validate_union_flat(TestInputVisitorData
*data
,
161 UserDefFlatUnion
*tmp
= NULL
;
165 v
= validate_test_init(data
,
166 "{ 'enum1': 'value1', "
168 "'boolean': true }");
169 /* TODO when generator bug is fixed, add 'integer': 41 */
171 visit_type_UserDefFlatUnion(v
, &tmp
, NULL
, &err
);
173 qapi_free_UserDefFlatUnion(tmp
);
176 static void test_validate_union_anon(TestInputVisitorData
*data
,
179 UserDefAnonUnion
*tmp
= NULL
;
183 v
= validate_test_init(data
, "42");
185 visit_type_UserDefAnonUnion(v
, &tmp
, NULL
, &err
);
187 qapi_free_UserDefAnonUnion(tmp
);
190 static void test_validate_fail_struct(TestInputVisitorData
*data
,
193 TestStruct
*p
= NULL
;
197 v
= validate_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
199 visit_type_TestStruct(v
, &p
, NULL
, &err
);
207 static void test_validate_fail_struct_nested(TestInputVisitorData
*data
,
210 UserDefNested
*udp
= NULL
;
214 v
= validate_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
216 visit_type_UserDefNested(v
, &udp
, NULL
, &err
);
218 qapi_free_UserDefNested(udp
);
221 static void test_validate_fail_list(TestInputVisitorData
*data
,
224 UserDefOneList
*head
= NULL
;
228 v
= validate_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
230 visit_type_UserDefOneList(v
, &head
, NULL
, &err
);
232 qapi_free_UserDefOneList(head
);
235 static void test_validate_fail_union(TestInputVisitorData
*data
,
238 UserDefUnion
*tmp
= NULL
;
242 v
= validate_test_init(data
, "{ 'type': 'b', 'data' : { 'integer': 42 } }");
244 visit_type_UserDefUnion(v
, &tmp
, NULL
, &err
);
246 qapi_free_UserDefUnion(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
);
260 qapi_free_UserDefFlatUnion(tmp
);
263 static void test_validate_fail_union_anon(TestInputVisitorData
*data
,
266 UserDefAnonUnion
*tmp
= NULL
;
270 v
= validate_test_init(data
, "3.14");
272 visit_type_UserDefAnonUnion(v
, &tmp
, NULL
, &err
);
274 qapi_free_UserDefAnonUnion(tmp
);
277 static void validate_test_add(const char *testpath
,
278 TestInputVisitorData
*data
,
279 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
281 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
285 int main(int argc
, char **argv
)
287 TestInputVisitorData testdata
;
289 g_test_init(&argc
, &argv
, NULL
);
291 validate_test_add("/visitor/input-strict/pass/struct",
292 &testdata
, test_validate_struct
);
293 validate_test_add("/visitor/input-strict/pass/struct-nested",
294 &testdata
, test_validate_struct_nested
);
295 validate_test_add("/visitor/input-strict/pass/list",
296 &testdata
, test_validate_list
);
297 validate_test_add("/visitor/input-strict/pass/union",
298 &testdata
, test_validate_union
);
299 validate_test_add("/visitor/input-strict/pass/union-flat",
300 &testdata
, test_validate_union_flat
);
301 validate_test_add("/visitor/input-strict/pass/union-anon",
302 &testdata
, test_validate_union_anon
);
303 validate_test_add("/visitor/input-strict/fail/struct",
304 &testdata
, test_validate_fail_struct
);
305 validate_test_add("/visitor/input-strict/fail/struct-nested",
306 &testdata
, test_validate_fail_struct_nested
);
307 validate_test_add("/visitor/input-strict/fail/list",
308 &testdata
, test_validate_fail_list
);
309 validate_test_add("/visitor/input-strict/fail/union",
310 &testdata
, test_validate_fail_union
);
311 validate_test_add("/visitor/input-strict/fail/union-flat",
312 &testdata
, test_validate_fail_union_flat
);
313 validate_test_add("/visitor/input-strict/fail/union-anon",
314 &testdata
, test_validate_fail_union_anon
);