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"
16 #include "qemu-common.h"
17 #include "qapi/error.h"
18 #include "qapi/string-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
{
24 StringInputVisitor
*siv
;
25 } TestInputVisitorData
;
27 static void visitor_input_teardown(TestInputVisitorData
*data
,
31 string_input_visitor_cleanup(data
->siv
);
36 /* This is provided instead of a test setup function so that the JSON
37 string used by the tests are kept in the test functions (and not
40 Visitor
*visitor_input_test_init(TestInputVisitorData
*data
,
45 data
->siv
= string_input_visitor_new(string
);
46 g_assert(data
->siv
!= NULL
);
48 v
= string_input_get_visitor(data
->siv
);
54 static void test_visitor_in_int(TestInputVisitorData
*data
,
57 int64_t res
= 0, value
= -42;
61 v
= visitor_input_test_init(data
, "-42");
63 visit_type_int(v
, NULL
, &res
, &err
);
65 g_assert_cmpint(res
, ==, value
);
67 visitor_input_teardown(data
, unused
);
69 v
= visitor_input_test_init(data
, "not an int");
71 visit_type_int(v
, NULL
, &res
, &err
);
72 error_free_or_abort(&err
);
75 static void test_visitor_in_intList(TestInputVisitorData
*data
,
78 int64_t value
[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20};
79 int16List
*res
= NULL
, *tmp
;
84 v
= visitor_input_test_init(data
, "1,2,0,2-4,20,5-9,1-8");
86 visit_type_int16List(v
, NULL
, &res
, &error_abort
);
88 while (i
< sizeof(value
) / sizeof(value
[0])) {
90 g_assert_cmpint(tmp
->value
, ==, value
[i
++]);
95 qapi_free_int16List(res
);
97 visitor_input_teardown(data
, unused
);
99 v
= visitor_input_test_init(data
, "not an int list");
101 visit_type_int16List(v
, NULL
, &res
, &err
);
102 error_free_or_abort(&err
);
106 static void test_visitor_in_bool(TestInputVisitorData
*data
,
113 v
= visitor_input_test_init(data
, "true");
115 visit_type_bool(v
, NULL
, &res
, &err
);
117 g_assert_cmpint(res
, ==, true);
118 visitor_input_teardown(data
, unused
);
120 v
= visitor_input_test_init(data
, "yes");
122 visit_type_bool(v
, NULL
, &res
, &err
);
124 g_assert_cmpint(res
, ==, true);
125 visitor_input_teardown(data
, unused
);
127 v
= visitor_input_test_init(data
, "on");
129 visit_type_bool(v
, NULL
, &res
, &err
);
131 g_assert_cmpint(res
, ==, true);
132 visitor_input_teardown(data
, unused
);
134 v
= visitor_input_test_init(data
, "false");
136 visit_type_bool(v
, NULL
, &res
, &err
);
138 g_assert_cmpint(res
, ==, false);
139 visitor_input_teardown(data
, unused
);
141 v
= visitor_input_test_init(data
, "no");
143 visit_type_bool(v
, NULL
, &res
, &err
);
145 g_assert_cmpint(res
, ==, false);
146 visitor_input_teardown(data
, unused
);
148 v
= visitor_input_test_init(data
, "off");
150 visit_type_bool(v
, NULL
, &res
, &err
);
152 g_assert_cmpint(res
, ==, false);
155 static void test_visitor_in_number(TestInputVisitorData
*data
,
158 double res
= 0, value
= 3.14;
162 v
= visitor_input_test_init(data
, "3.14");
164 visit_type_number(v
, NULL
, &res
, &err
);
166 g_assert_cmpfloat(res
, ==, value
);
169 static void test_visitor_in_string(TestInputVisitorData
*data
,
172 char *res
= NULL
, *value
= (char *) "Q E M U";
176 v
= visitor_input_test_init(data
, value
);
178 visit_type_str(v
, NULL
, &res
, &err
);
180 g_assert_cmpstr(res
, ==, value
);
185 static void test_visitor_in_enum(TestInputVisitorData
*data
,
192 for (i
= 0; EnumOne_lookup
[i
]; i
++) {
195 v
= visitor_input_test_init(data
, EnumOne_lookup
[i
]);
197 visit_type_EnumOne(v
, NULL
, &res
, &err
);
199 g_assert_cmpint(i
, ==, res
);
201 visitor_input_teardown(data
, NULL
);
207 /* Try to crash the visitors */
208 static void test_visitor_in_fuzz(TestInputVisitorData
*data
,
221 for (i
= 0; i
< 100; i
++) {
224 j
= g_test_rand_int_range(0, sizeof(buf
) - 1);
229 for (j
--; j
!= 0; j
--) {
230 buf
[j
- 1] = (char)g_test_rand_int_range(0, 256);
234 v
= visitor_input_test_init(data
, buf
);
235 visit_type_int(v
, NULL
, &ires
, NULL
);
236 visitor_input_teardown(data
, NULL
);
238 v
= visitor_input_test_init(data
, buf
);
239 visit_type_intList(v
, NULL
, &ilres
, NULL
);
240 visitor_input_teardown(data
, NULL
);
242 v
= visitor_input_test_init(data
, buf
);
243 visit_type_bool(v
, NULL
, &bres
, NULL
);
244 visitor_input_teardown(data
, NULL
);
246 v
= visitor_input_test_init(data
, buf
);
247 visit_type_number(v
, NULL
, &nres
, NULL
);
248 visitor_input_teardown(data
, NULL
);
250 v
= visitor_input_test_init(data
, buf
);
252 visit_type_str(v
, NULL
, &sres
, NULL
);
254 visitor_input_teardown(data
, NULL
);
256 v
= visitor_input_test_init(data
, buf
);
257 visit_type_EnumOne(v
, NULL
, &eres
, NULL
);
258 visitor_input_teardown(data
, NULL
);
262 static void input_visitor_test_add(const char *testpath
,
263 TestInputVisitorData
*data
,
264 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
266 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
267 visitor_input_teardown
);
270 int main(int argc
, char **argv
)
272 TestInputVisitorData in_visitor_data
;
274 g_test_init(&argc
, &argv
, NULL
);
276 input_visitor_test_add("/string-visitor/input/int",
277 &in_visitor_data
, test_visitor_in_int
);
278 input_visitor_test_add("/string-visitor/input/intList",
279 &in_visitor_data
, test_visitor_in_intList
);
280 input_visitor_test_add("/string-visitor/input/bool",
281 &in_visitor_data
, test_visitor_in_bool
);
282 input_visitor_test_add("/string-visitor/input/number",
283 &in_visitor_data
, test_visitor_in_number
);
284 input_visitor_test_add("/string-visitor/input/string",
285 &in_visitor_data
, test_visitor_in_string
);
286 input_visitor_test_add("/string-visitor/input/enum",
287 &in_visitor_data
, test_visitor_in_enum
);
288 input_visitor_test_add("/string-visitor/input/fuzz",
289 &in_visitor_data
, test_visitor_in_fuzz
);