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
{
24 } TestInputVisitorData
;
26 static void visitor_input_teardown(TestInputVisitorData
*data
,
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
,
42 data
->v
= string_input_visitor_new(string
);
47 static void test_visitor_in_int(TestInputVisitorData
*data
,
50 int64_t res
= 0, value
= -42;
54 v
= visitor_input_test_init(data
, "-42");
56 visit_type_int(v
, NULL
, &res
, &err
);
58 g_assert_cmpint(res
, ==, value
);
60 visitor_input_teardown(data
, unused
);
62 v
= visitor_input_test_init(data
, "not an int");
64 visit_type_int(v
, NULL
, &res
, &err
);
65 error_free_or_abort(&err
);
68 static void test_visitor_in_intList(TestInputVisitorData
*data
,
71 int64_t value
[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20};
72 int16List
*res
= NULL
, *tmp
;
77 v
= visitor_input_test_init(data
, "1,2,0,2-4,20,5-9,1-8");
79 visit_type_int16List(v
, NULL
, &res
, &error_abort
);
81 while (i
< sizeof(value
) / sizeof(value
[0])) {
83 g_assert_cmpint(tmp
->value
, ==, value
[i
++]);
88 qapi_free_int16List(res
);
90 visitor_input_teardown(data
, unused
);
92 v
= visitor_input_test_init(data
, "not an int list");
94 visit_type_int16List(v
, NULL
, &res
, &err
);
95 error_free_or_abort(&err
);
99 static void test_visitor_in_bool(TestInputVisitorData
*data
,
106 v
= visitor_input_test_init(data
, "true");
108 visit_type_bool(v
, NULL
, &res
, &err
);
110 g_assert_cmpint(res
, ==, true);
111 visitor_input_teardown(data
, unused
);
113 v
= visitor_input_test_init(data
, "yes");
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
, "on");
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
, "false");
129 visit_type_bool(v
, NULL
, &res
, &err
);
131 g_assert_cmpint(res
, ==, false);
132 visitor_input_teardown(data
, unused
);
134 v
= visitor_input_test_init(data
, "no");
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
, "off");
143 visit_type_bool(v
, NULL
, &res
, &err
);
145 g_assert_cmpint(res
, ==, false);
148 static void test_visitor_in_number(TestInputVisitorData
*data
,
151 double res
= 0, value
= 3.14;
155 v
= visitor_input_test_init(data
, "3.14");
157 visit_type_number(v
, NULL
, &res
, &err
);
159 g_assert_cmpfloat(res
, ==, value
);
162 static void test_visitor_in_string(TestInputVisitorData
*data
,
165 char *res
= NULL
, *value
= (char *) "Q E M U";
169 v
= visitor_input_test_init(data
, value
);
171 visit_type_str(v
, NULL
, &res
, &err
);
173 g_assert_cmpstr(res
, ==, value
);
178 static void test_visitor_in_enum(TestInputVisitorData
*data
,
185 for (i
= 0; EnumOne_lookup
[i
]; i
++) {
188 v
= visitor_input_test_init(data
, EnumOne_lookup
[i
]);
190 visit_type_EnumOne(v
, NULL
, &res
, &err
);
192 g_assert_cmpint(i
, ==, res
);
194 visitor_input_teardown(data
, NULL
);
198 /* Try to crash the visitors */
199 static void test_visitor_in_fuzz(TestInputVisitorData
*data
,
212 for (i
= 0; i
< 100; i
++) {
215 j
= g_test_rand_int_range(0, sizeof(buf
) - 1);
220 for (j
--; j
!= 0; j
--) {
221 buf
[j
- 1] = (char)g_test_rand_int_range(0, 256);
225 v
= visitor_input_test_init(data
, buf
);
226 visit_type_int(v
, NULL
, &ires
, NULL
);
227 visitor_input_teardown(data
, NULL
);
229 v
= visitor_input_test_init(data
, buf
);
230 visit_type_intList(v
, NULL
, &ilres
, NULL
);
231 visitor_input_teardown(data
, NULL
);
233 v
= visitor_input_test_init(data
, buf
);
234 visit_type_bool(v
, NULL
, &bres
, NULL
);
235 visitor_input_teardown(data
, NULL
);
237 v
= visitor_input_test_init(data
, buf
);
238 visit_type_number(v
, NULL
, &nres
, NULL
);
239 visitor_input_teardown(data
, NULL
);
241 v
= visitor_input_test_init(data
, buf
);
243 visit_type_str(v
, NULL
, &sres
, NULL
);
245 visitor_input_teardown(data
, NULL
);
247 v
= visitor_input_test_init(data
, buf
);
248 visit_type_EnumOne(v
, NULL
, &eres
, NULL
);
249 visitor_input_teardown(data
, NULL
);
253 static void input_visitor_test_add(const char *testpath
,
254 TestInputVisitorData
*data
,
255 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
257 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
258 visitor_input_teardown
);
261 int main(int argc
, char **argv
)
263 TestInputVisitorData in_visitor_data
;
265 g_test_init(&argc
, &argv
, NULL
);
267 input_visitor_test_add("/string-visitor/input/int",
268 &in_visitor_data
, test_visitor_in_int
);
269 input_visitor_test_add("/string-visitor/input/intList",
270 &in_visitor_data
, test_visitor_in_intList
);
271 input_visitor_test_add("/string-visitor/input/bool",
272 &in_visitor_data
, test_visitor_in_bool
);
273 input_visitor_test_add("/string-visitor/input/number",
274 &in_visitor_data
, test_visitor_in_number
);
275 input_visitor_test_add("/string-visitor/input/string",
276 &in_visitor_data
, test_visitor_in_string
);
277 input_visitor_test_add("/string-visitor/input/enum",
278 &in_visitor_data
, test_visitor_in_enum
);
279 input_visitor_test_add("/string-visitor/input/fuzz",
280 &in_visitor_data
, test_visitor_in_fuzz
);