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_flat_no_discrim(TestInputVisitorData
*data
,
266 UserDefFlatUnion2
*tmp
= NULL
;
270 /* test situation where discriminator field ('enum1' here) is missing */
271 v
= validate_test_init(data
, "{ 'string': 'c', 'string1': 'd', 'string2': 'e' }");
273 visit_type_UserDefFlatUnion2(v
, &tmp
, NULL
, &err
);
275 qapi_free_UserDefFlatUnion2(tmp
);
278 static void test_validate_fail_union_anon(TestInputVisitorData
*data
,
281 UserDefAnonUnion
*tmp
= NULL
;
285 v
= validate_test_init(data
, "3.14");
287 visit_type_UserDefAnonUnion(v
, &tmp
, NULL
, &err
);
289 qapi_free_UserDefAnonUnion(tmp
);
292 static void validate_test_add(const char *testpath
,
293 TestInputVisitorData
*data
,
294 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
296 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
300 int main(int argc
, char **argv
)
302 TestInputVisitorData testdata
;
304 g_test_init(&argc
, &argv
, NULL
);
306 validate_test_add("/visitor/input-strict/pass/struct",
307 &testdata
, test_validate_struct
);
308 validate_test_add("/visitor/input-strict/pass/struct-nested",
309 &testdata
, test_validate_struct_nested
);
310 validate_test_add("/visitor/input-strict/pass/list",
311 &testdata
, test_validate_list
);
312 validate_test_add("/visitor/input-strict/pass/union",
313 &testdata
, test_validate_union
);
314 validate_test_add("/visitor/input-strict/pass/union-flat",
315 &testdata
, test_validate_union_flat
);
316 validate_test_add("/visitor/input-strict/pass/union-anon",
317 &testdata
, test_validate_union_anon
);
318 validate_test_add("/visitor/input-strict/fail/struct",
319 &testdata
, test_validate_fail_struct
);
320 validate_test_add("/visitor/input-strict/fail/struct-nested",
321 &testdata
, test_validate_fail_struct_nested
);
322 validate_test_add("/visitor/input-strict/fail/list",
323 &testdata
, test_validate_fail_list
);
324 validate_test_add("/visitor/input-strict/fail/union",
325 &testdata
, test_validate_fail_union
);
326 validate_test_add("/visitor/input-strict/fail/union-flat",
327 &testdata
, test_validate_fail_union_flat
);
328 validate_test_add("/visitor/input-strict/fail/union-flat-no-discriminator",
329 &testdata
, test_validate_fail_union_flat_no_discrim
);
330 validate_test_add("/visitor/input-strict/fail/union-anon",
331 &testdata
, test_validate_fail_union_anon
);