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
, &errp
);
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
, &errp
);
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
, &errp
);
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', 'data' : { 'integer': 42 } }");
137 visit_type_UserDefUnion(v
, &tmp
, NULL
, &errp
);
139 qapi_free_UserDefUnion(tmp
);
142 static void test_validate_fail_struct(TestInputVisitorData
*data
,
145 TestStruct
*p
= NULL
;
149 v
= validate_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
151 visit_type_TestStruct(v
, &p
, NULL
, &errp
);
159 static void test_validate_fail_struct_nested(TestInputVisitorData
*data
,
162 UserDefNested
*udp
= NULL
;
166 v
= validate_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
168 visit_type_UserDefNested(v
, &udp
, NULL
, &errp
);
170 qapi_free_UserDefNested(udp
);
173 static void test_validate_fail_list(TestInputVisitorData
*data
,
176 UserDefOneList
*head
= NULL
;
180 v
= validate_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
182 visit_type_UserDefOneList(v
, &head
, NULL
, &errp
);
184 qapi_free_UserDefOneList(head
);
187 static void test_validate_fail_union(TestInputVisitorData
*data
,
190 UserDefUnion
*tmp
= NULL
;
194 v
= validate_test_init(data
, "{ 'type': 'b', 'data' : { 'integer': 42 }, 'extra': 'yyy' }");
196 visit_type_UserDefUnion(v
, &tmp
, NULL
, &errp
);
198 qapi_free_UserDefUnion(tmp
);
201 static void validate_test_add(const char *testpath
,
202 TestInputVisitorData
*data
,
203 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
205 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
209 int main(int argc
, char **argv
)
211 TestInputVisitorData testdata
;
213 g_test_init(&argc
, &argv
, NULL
);
215 validate_test_add("/visitor/input-strict/pass/struct",
216 &testdata
, test_validate_struct
);
217 validate_test_add("/visitor/input-strict/pass/struct-nested",
218 &testdata
, test_validate_struct_nested
);
219 validate_test_add("/visitor/input-strict/pass/list",
220 &testdata
, test_validate_list
);
221 validate_test_add("/visitor/input-strict/pass/union",
222 &testdata
, test_validate_union
);
223 validate_test_add("/visitor/input-strict/fail/struct",
224 &testdata
, test_validate_fail_struct
);
225 validate_test_add("/visitor/input-strict/fail/struct-nested",
226 &testdata
, test_validate_fail_struct_nested
);
227 validate_test_add("/visitor/input-strict/fail/list",
228 &testdata
, test_validate_fail_list
);
229 validate_test_add("/visitor/input-strict/fail/union",
230 &testdata
, test_validate_fail_union
);