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
)
75 visit_start_struct(v
, (void **)obj
, "TestStruct", name
, sizeof(TestStruct
),
78 visit_type_int(v
, &(*obj
)->integer
, "integer", errp
);
79 visit_type_bool(v
, &(*obj
)->boolean
, "boolean", errp
);
80 visit_type_str(v
, &(*obj
)->string
, "string", errp
);
82 visit_end_struct(v
, errp
);
85 static void test_validate_struct(TestInputVisitorData
*data
,
92 v
= validate_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
94 visit_type_TestStruct(v
, &p
, NULL
, &err
);
100 static void test_validate_struct_nested(TestInputVisitorData
*data
,
103 UserDefNested
*udp
= NULL
;
107 v
= validate_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string' }, 'string2': 'string2'}}}");
109 visit_type_UserDefNested(v
, &udp
, NULL
, &err
);
111 qapi_free_UserDefNested(udp
);
114 static void test_validate_list(TestInputVisitorData
*data
,
117 UserDefOneList
*head
= NULL
;
121 v
= validate_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
123 visit_type_UserDefOneList(v
, &head
, NULL
, &err
);
125 qapi_free_UserDefOneList(head
);
128 static void test_validate_union(TestInputVisitorData
*data
,
131 UserDefUnion
*tmp
= NULL
;
135 v
= validate_test_init(data
, "{ 'type': 'b', 'integer': 41, 'data' : { 'integer': 42 } }");
137 visit_type_UserDefUnion(v
, &tmp
, NULL
, &err
);
139 qapi_free_UserDefUnion(tmp
);
142 static void test_validate_union_flat(TestInputVisitorData
*data
,
145 UserDefFlatUnion
*tmp
= NULL
;
149 v
= validate_test_init(data
,
150 "{ 'enum1': 'value1', "
152 "'boolean': true }");
153 /* TODO when generator bug is fixed, add 'integer': 41 */
155 visit_type_UserDefFlatUnion(v
, &tmp
, NULL
, &err
);
157 qapi_free_UserDefFlatUnion(tmp
);
160 static void test_validate_union_anon(TestInputVisitorData
*data
,
163 UserDefAnonUnion
*tmp
= NULL
;
167 v
= validate_test_init(data
, "42");
169 visit_type_UserDefAnonUnion(v
, &tmp
, NULL
, &err
);
171 qapi_free_UserDefAnonUnion(tmp
);
174 static void test_validate_fail_struct(TestInputVisitorData
*data
,
177 TestStruct
*p
= NULL
;
181 v
= validate_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
183 visit_type_TestStruct(v
, &p
, NULL
, &err
);
191 static void test_validate_fail_struct_nested(TestInputVisitorData
*data
,
194 UserDefNested
*udp
= NULL
;
198 v
= validate_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
200 visit_type_UserDefNested(v
, &udp
, NULL
, &err
);
202 qapi_free_UserDefNested(udp
);
205 static void test_validate_fail_list(TestInputVisitorData
*data
,
208 UserDefOneList
*head
= NULL
;
212 v
= validate_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
214 visit_type_UserDefOneList(v
, &head
, NULL
, &err
);
216 qapi_free_UserDefOneList(head
);
219 static void test_validate_fail_union(TestInputVisitorData
*data
,
222 UserDefUnion
*tmp
= NULL
;
226 v
= validate_test_init(data
, "{ 'type': 'b', 'data' : { 'integer': 42 } }");
228 visit_type_UserDefUnion(v
, &tmp
, NULL
, &err
);
230 qapi_free_UserDefUnion(tmp
);
233 static void test_validate_fail_union_flat(TestInputVisitorData
*data
,
236 UserDefFlatUnion
*tmp
= NULL
;
240 v
= validate_test_init(data
, "{ 'string': 'c', 'integer': 41, 'boolean': true }");
242 visit_type_UserDefFlatUnion(v
, &tmp
, NULL
, &err
);
244 qapi_free_UserDefFlatUnion(tmp
);
247 static void test_validate_fail_union_anon(TestInputVisitorData
*data
,
250 UserDefAnonUnion
*tmp
= NULL
;
254 v
= validate_test_init(data
, "3.14");
256 visit_type_UserDefAnonUnion(v
, &tmp
, NULL
, &err
);
258 qapi_free_UserDefAnonUnion(tmp
);
261 static void validate_test_add(const char *testpath
,
262 TestInputVisitorData
*data
,
263 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
265 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
269 int main(int argc
, char **argv
)
271 TestInputVisitorData testdata
;
273 g_test_init(&argc
, &argv
, NULL
);
275 validate_test_add("/visitor/input-strict/pass/struct",
276 &testdata
, test_validate_struct
);
277 validate_test_add("/visitor/input-strict/pass/struct-nested",
278 &testdata
, test_validate_struct_nested
);
279 validate_test_add("/visitor/input-strict/pass/list",
280 &testdata
, test_validate_list
);
281 validate_test_add("/visitor/input-strict/pass/union",
282 &testdata
, test_validate_union
);
283 validate_test_add("/visitor/input-strict/pass/union-flat",
284 &testdata
, test_validate_union_flat
);
285 validate_test_add("/visitor/input-strict/pass/union-anon",
286 &testdata
, test_validate_union_anon
);
287 validate_test_add("/visitor/input-strict/fail/struct",
288 &testdata
, test_validate_fail_struct
);
289 validate_test_add("/visitor/input-strict/fail/struct-nested",
290 &testdata
, test_validate_fail_struct_nested
);
291 validate_test_add("/visitor/input-strict/fail/list",
292 &testdata
, test_validate_fail_list
);
293 validate_test_add("/visitor/input-strict/fail/union",
294 &testdata
, test_validate_fail_union
);
295 validate_test_add("/visitor/input-strict/fail/union-flat",
296 &testdata
, test_validate_fail_union_flat
);
297 validate_test_add("/visitor/input-strict/fail/union-anon",
298 &testdata
, test_validate_fail_union_anon
);