2 * String Input Visitor unit-tests.
4 * Copyright (C) 2012 Red Hat Inc.
7 * Paolo Bonzini <pbonzini@redhat.com> (based on test-qmp-input-visitor)
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
15 #include "qemu-common.h"
16 #include "qapi/error.h"
17 #include "qapi/string-input-visitor.h"
18 #include "test-qapi-types.h"
19 #include "test-qapi-visit.h"
20 #include "qapi/qmp/types.h"
22 typedef struct TestInputVisitorData
{
23 StringInputVisitor
*siv
;
24 } TestInputVisitorData
;
26 static void visitor_input_teardown(TestInputVisitorData
*data
,
30 string_input_visitor_cleanup(data
->siv
);
35 /* This is provided instead of a test setup function so that the JSON
36 string used by the tests are kept in the test functions (and not
39 Visitor
*visitor_input_test_init(TestInputVisitorData
*data
,
44 data
->siv
= string_input_visitor_new(string
);
45 g_assert(data
->siv
!= NULL
);
47 v
= string_input_get_visitor(data
->siv
);
53 static void test_visitor_in_int(TestInputVisitorData
*data
,
56 int64_t res
= 0, value
= -42;
60 v
= visitor_input_test_init(data
, "-42");
62 visit_type_int(v
, NULL
, &res
, &err
);
64 g_assert_cmpint(res
, ==, value
);
66 visitor_input_teardown(data
, unused
);
68 v
= visitor_input_test_init(data
, "not an int");
70 visit_type_int(v
, NULL
, &res
, &err
);
71 error_free_or_abort(&err
);
74 static void test_visitor_in_intList(TestInputVisitorData
*data
,
77 int64_t value
[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20};
78 int16List
*res
= NULL
, *tmp
;
83 v
= visitor_input_test_init(data
, "1,2,0,2-4,20,5-9,1-8");
85 visit_type_int16List(v
, NULL
, &res
, &error_abort
);
87 while (i
< sizeof(value
) / sizeof(value
[0])) {
89 g_assert_cmpint(tmp
->value
, ==, value
[i
++]);
94 qapi_free_int16List(res
);
96 visitor_input_teardown(data
, unused
);
98 v
= visitor_input_test_init(data
, "not an int list");
100 visit_type_int16List(v
, NULL
, &res
, &err
);
101 error_free_or_abort(&err
);
105 static void test_visitor_in_bool(TestInputVisitorData
*data
,
112 v
= visitor_input_test_init(data
, "true");
114 visit_type_bool(v
, NULL
, &res
, &err
);
116 g_assert_cmpint(res
, ==, true);
117 visitor_input_teardown(data
, unused
);
119 v
= visitor_input_test_init(data
, "yes");
121 visit_type_bool(v
, NULL
, &res
, &err
);
123 g_assert_cmpint(res
, ==, true);
124 visitor_input_teardown(data
, unused
);
126 v
= visitor_input_test_init(data
, "on");
128 visit_type_bool(v
, NULL
, &res
, &err
);
130 g_assert_cmpint(res
, ==, true);
131 visitor_input_teardown(data
, unused
);
133 v
= visitor_input_test_init(data
, "false");
135 visit_type_bool(v
, NULL
, &res
, &err
);
137 g_assert_cmpint(res
, ==, false);
138 visitor_input_teardown(data
, unused
);
140 v
= visitor_input_test_init(data
, "no");
142 visit_type_bool(v
, NULL
, &res
, &err
);
144 g_assert_cmpint(res
, ==, false);
145 visitor_input_teardown(data
, unused
);
147 v
= visitor_input_test_init(data
, "off");
149 visit_type_bool(v
, NULL
, &res
, &err
);
151 g_assert_cmpint(res
, ==, false);
154 static void test_visitor_in_number(TestInputVisitorData
*data
,
157 double res
= 0, value
= 3.14;
161 v
= visitor_input_test_init(data
, "3.14");
163 visit_type_number(v
, NULL
, &res
, &err
);
165 g_assert_cmpfloat(res
, ==, value
);
168 static void test_visitor_in_string(TestInputVisitorData
*data
,
171 char *res
= NULL
, *value
= (char *) "Q E M U";
175 v
= visitor_input_test_init(data
, value
);
177 visit_type_str(v
, NULL
, &res
, &err
);
179 g_assert_cmpstr(res
, ==, value
);
184 static void test_visitor_in_enum(TestInputVisitorData
*data
,
191 for (i
= 0; EnumOne_lookup
[i
]; i
++) {
194 v
= visitor_input_test_init(data
, EnumOne_lookup
[i
]);
196 visit_type_EnumOne(v
, NULL
, &res
, &err
);
198 g_assert_cmpint(i
, ==, res
);
200 visitor_input_teardown(data
, NULL
);
206 /* Try to crash the visitors */
207 static void test_visitor_in_fuzz(TestInputVisitorData
*data
,
220 for (i
= 0; i
< 100; i
++) {
223 j
= g_test_rand_int_range(0, sizeof(buf
) - 1);
228 for (j
--; j
!= 0; j
--) {
229 buf
[j
- 1] = (char)g_test_rand_int_range(0, 256);
233 v
= visitor_input_test_init(data
, buf
);
234 visit_type_int(v
, NULL
, &ires
, NULL
);
235 visitor_input_teardown(data
, NULL
);
237 v
= visitor_input_test_init(data
, buf
);
238 visit_type_intList(v
, NULL
, &ilres
, NULL
);
239 visitor_input_teardown(data
, NULL
);
241 v
= visitor_input_test_init(data
, buf
);
242 visit_type_bool(v
, NULL
, &bres
, NULL
);
243 visitor_input_teardown(data
, NULL
);
245 v
= visitor_input_test_init(data
, buf
);
246 visit_type_number(v
, NULL
, &nres
, NULL
);
247 visitor_input_teardown(data
, NULL
);
249 v
= visitor_input_test_init(data
, buf
);
251 visit_type_str(v
, NULL
, &sres
, NULL
);
253 visitor_input_teardown(data
, NULL
);
255 v
= visitor_input_test_init(data
, buf
);
256 visit_type_EnumOne(v
, NULL
, &eres
, NULL
);
257 visitor_input_teardown(data
, NULL
);
261 static void input_visitor_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
,
266 visitor_input_teardown
);
269 int main(int argc
, char **argv
)
271 TestInputVisitorData in_visitor_data
;
273 g_test_init(&argc
, &argv
, NULL
);
275 input_visitor_test_add("/string-visitor/input/int",
276 &in_visitor_data
, test_visitor_in_int
);
277 input_visitor_test_add("/string-visitor/input/intList",
278 &in_visitor_data
, test_visitor_in_intList
);
279 input_visitor_test_add("/string-visitor/input/bool",
280 &in_visitor_data
, test_visitor_in_bool
);
281 input_visitor_test_add("/string-visitor/input/number",
282 &in_visitor_data
, test_visitor_in_number
);
283 input_visitor_test_add("/string-visitor/input/string",
284 &in_visitor_data
, test_visitor_in_string
);
285 input_visitor_test_add("/string-visitor/input/enum",
286 &in_visitor_data
, test_visitor_in_enum
);
287 input_visitor_test_add("/string-visitor/input/fuzz",
288 &in_visitor_data
, test_visitor_in_fuzz
);