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 "qapi/qmp-input-visitor.h"
18 #include "test-qapi-types.h"
19 #include "test-qapi-visit.h"
20 #include "qemu-objects.h"
22 typedef struct TestInputVisitorData
{
25 } TestInputVisitorData
;
27 static void validate_teardown(TestInputVisitorData
*data
,
30 qobject_decref(data
->obj
);
34 qmp_input_visitor_cleanup(data
->qiv
);
39 /* This is provided instead of a test setup function so that the JSON
40 string used by the tests are kept in the test functions (and not
42 static GCC_FMT_ATTR(2, 3)
43 Visitor
*validate_test_init(TestInputVisitorData
*data
,
44 const char *json_string
, ...)
49 va_start(ap
, json_string
);
50 data
->obj
= qobject_from_jsonv(json_string
, &ap
);
53 g_assert(data
->obj
!= NULL
);
55 data
->qiv
= qmp_input_visitor_new_strict(data
->obj
);
56 g_assert(data
->qiv
!= NULL
);
58 v
= qmp_input_get_visitor(data
->qiv
);
64 typedef struct TestStruct
71 static void visit_type_TestStruct(Visitor
*v
, TestStruct
**obj
,
72 const char *name
, Error
**errp
)
74 visit_start_struct(v
, (void **)obj
, "TestStruct", name
, sizeof(TestStruct
),
77 visit_type_int(v
, &(*obj
)->integer
, "integer", errp
);
78 visit_type_bool(v
, &(*obj
)->boolean
, "boolean", errp
);
79 visit_type_str(v
, &(*obj
)->string
, "string", errp
);
81 visit_end_struct(v
, errp
);
84 static void test_validate_struct(TestInputVisitorData
*data
,
91 v
= validate_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
93 visit_type_TestStruct(v
, &p
, NULL
, &errp
);
94 g_assert(!error_is_set(&errp
));
99 static void test_validate_struct_nested(TestInputVisitorData
*data
,
102 UserDefNested
*udp
= NULL
;
106 v
= validate_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string' }, 'string2': 'string2'}}}");
108 visit_type_UserDefNested(v
, &udp
, NULL
, &errp
);
109 g_assert(!error_is_set(&errp
));
110 qapi_free_UserDefNested(udp
);
113 static void test_validate_list(TestInputVisitorData
*data
,
116 UserDefOneList
*head
= NULL
;
120 v
= validate_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
122 visit_type_UserDefOneList(v
, &head
, NULL
, &errp
);
123 g_assert(!error_is_set(&errp
));
124 qapi_free_UserDefOneList(head
);
127 static void test_validate_union(TestInputVisitorData
*data
,
130 UserDefUnion
*tmp
= NULL
;
134 v
= validate_test_init(data
, "{ 'type': 'b', 'data' : { 'integer': 42 } }");
136 visit_type_UserDefUnion(v
, &tmp
, NULL
, &errp
);
137 g_assert(!error_is_set(&errp
));
138 qapi_free_UserDefUnion(tmp
);
141 static void test_validate_fail_struct(TestInputVisitorData
*data
,
144 TestStruct
*p
= NULL
;
148 v
= validate_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo', 'extra': 42 }");
150 visit_type_TestStruct(v
, &p
, NULL
, &errp
);
151 g_assert(error_is_set(&errp
));
158 static void test_validate_fail_struct_nested(TestInputVisitorData
*data
,
161 UserDefNested
*udp
= NULL
;
165 v
= validate_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string', 'extra': [42, 23, {'foo':'bar'}] }, 'string2': 'string2'}}}");
167 visit_type_UserDefNested(v
, &udp
, NULL
, &errp
);
168 g_assert(error_is_set(&errp
));
169 qapi_free_UserDefNested(udp
);
172 static void test_validate_fail_list(TestInputVisitorData
*data
,
175 UserDefOneList
*head
= NULL
;
179 v
= validate_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44, 'extra': 'ggg' } ]");
181 visit_type_UserDefOneList(v
, &head
, NULL
, &errp
);
182 g_assert(error_is_set(&errp
));
183 qapi_free_UserDefOneList(head
);
186 static void test_validate_fail_union(TestInputVisitorData
*data
,
189 UserDefUnion
*tmp
= NULL
;
193 v
= validate_test_init(data
, "{ 'type': 'b', 'data' : { 'integer': 42 }, 'extra': 'yyy' }");
195 visit_type_UserDefUnion(v
, &tmp
, NULL
, &errp
);
196 g_assert(error_is_set(&errp
));
197 qapi_free_UserDefUnion(tmp
);
200 static void validate_test_add(const char *testpath
,
201 TestInputVisitorData
*data
,
202 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
204 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
208 int main(int argc
, char **argv
)
210 TestInputVisitorData testdata
;
212 g_test_init(&argc
, &argv
, NULL
);
214 validate_test_add("/visitor/input-strict/pass/struct",
215 &testdata
, test_validate_struct
);
216 validate_test_add("/visitor/input-strict/pass/struct-nested",
217 &testdata
, test_validate_struct_nested
);
218 validate_test_add("/visitor/input-strict/pass/list",
219 &testdata
, test_validate_list
);
220 validate_test_add("/visitor/input-strict/pass/union",
221 &testdata
, test_validate_union
);
222 validate_test_add("/visitor/input-strict/fail/struct",
223 &testdata
, test_validate_fail_struct
);
224 validate_test_add("/visitor/input-strict/fail/struct-nested",
225 &testdata
, test_validate_fail_struct_nested
);
226 validate_test_add("/visitor/input-strict/fail/list",
227 &testdata
, test_validate_fail_list
);
228 validate_test_add("/visitor/input-strict/fail/union",
229 &testdata
, test_validate_fail_union
);