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.
16 #include "qemu-common.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
, &res
, NULL
, &err
);
64 g_assert_cmpint(res
, ==, value
);
67 static void test_visitor_in_intList(TestInputVisitorData
*data
,
70 int64_t value
[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20};
71 int16List
*res
= NULL
, *tmp
;
75 v
= visitor_input_test_init(data
, "1,2,0,2-4,20,5-9,1-8");
77 visit_type_int16List(v
, &res
, NULL
, &error_abort
);
79 while (i
< sizeof(value
) / sizeof(value
[0])) {
81 g_assert_cmpint(tmp
->value
, ==, value
[i
++]);
94 static void test_visitor_in_bool(TestInputVisitorData
*data
,
101 v
= visitor_input_test_init(data
, "true");
103 visit_type_bool(v
, &res
, NULL
, &err
);
105 g_assert_cmpint(res
, ==, true);
106 visitor_input_teardown(data
, unused
);
108 v
= visitor_input_test_init(data
, "yes");
110 visit_type_bool(v
, &res
, NULL
, &err
);
112 g_assert_cmpint(res
, ==, true);
113 visitor_input_teardown(data
, unused
);
115 v
= visitor_input_test_init(data
, "on");
117 visit_type_bool(v
, &res
, NULL
, &err
);
119 g_assert_cmpint(res
, ==, true);
120 visitor_input_teardown(data
, unused
);
122 v
= visitor_input_test_init(data
, "false");
124 visit_type_bool(v
, &res
, NULL
, &err
);
126 g_assert_cmpint(res
, ==, false);
127 visitor_input_teardown(data
, unused
);
129 v
= visitor_input_test_init(data
, "no");
131 visit_type_bool(v
, &res
, NULL
, &err
);
133 g_assert_cmpint(res
, ==, false);
134 visitor_input_teardown(data
, unused
);
136 v
= visitor_input_test_init(data
, "off");
138 visit_type_bool(v
, &res
, NULL
, &err
);
140 g_assert_cmpint(res
, ==, false);
143 static void test_visitor_in_number(TestInputVisitorData
*data
,
146 double res
= 0, value
= 3.14;
150 v
= visitor_input_test_init(data
, "3.14");
152 visit_type_number(v
, &res
, NULL
, &err
);
154 g_assert_cmpfloat(res
, ==, value
);
157 static void test_visitor_in_string(TestInputVisitorData
*data
,
160 char *res
= NULL
, *value
= (char *) "Q E M U";
164 v
= visitor_input_test_init(data
, value
);
166 visit_type_str(v
, &res
, NULL
, &err
);
168 g_assert_cmpstr(res
, ==, value
);
173 static void test_visitor_in_enum(TestInputVisitorData
*data
,
180 for (i
= 0; EnumOne_lookup
[i
]; i
++) {
183 v
= visitor_input_test_init(data
, EnumOne_lookup
[i
]);
185 visit_type_EnumOne(v
, &res
, NULL
, &err
);
187 g_assert_cmpint(i
, ==, res
);
189 visitor_input_teardown(data
, NULL
);
195 /* Try to crash the visitors */
196 static void test_visitor_in_fuzz(TestInputVisitorData
*data
,
209 for (i
= 0; i
< 100; i
++) {
212 j
= g_test_rand_int_range(0, sizeof(buf
) - 1);
217 for (j
--; j
!= 0; j
--) {
218 buf
[j
- 1] = (char)g_test_rand_int_range(0, 256);
222 v
= visitor_input_test_init(data
, buf
);
223 visit_type_int(v
, &ires
, NULL
, NULL
);
224 visitor_input_teardown(data
, NULL
);
226 v
= visitor_input_test_init(data
, buf
);
227 visit_type_intList(v
, &ilres
, NULL
, NULL
);
228 visitor_input_teardown(data
, NULL
);
230 v
= visitor_input_test_init(data
, buf
);
231 visit_type_bool(v
, &bres
, NULL
, NULL
);
232 visitor_input_teardown(data
, NULL
);
234 v
= visitor_input_test_init(data
, buf
);
235 visit_type_number(v
, &nres
, NULL
, NULL
);
236 visitor_input_teardown(data
, NULL
);
238 v
= visitor_input_test_init(data
, buf
);
240 visit_type_str(v
, &sres
, NULL
, NULL
);
242 visitor_input_teardown(data
, NULL
);
244 v
= visitor_input_test_init(data
, buf
);
245 visit_type_EnumOne(v
, &eres
, NULL
, NULL
);
246 visitor_input_teardown(data
, NULL
);
250 static void input_visitor_test_add(const char *testpath
,
251 TestInputVisitorData
*data
,
252 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
254 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
255 visitor_input_teardown
);
258 int main(int argc
, char **argv
)
260 TestInputVisitorData in_visitor_data
;
262 g_test_init(&argc
, &argv
, NULL
);
264 input_visitor_test_add("/string-visitor/input/int",
265 &in_visitor_data
, test_visitor_in_int
);
266 input_visitor_test_add("/string-visitor/input/intList",
267 &in_visitor_data
, test_visitor_in_intList
);
268 input_visitor_test_add("/string-visitor/input/bool",
269 &in_visitor_data
, test_visitor_in_bool
);
270 input_visitor_test_add("/string-visitor/input/number",
271 &in_visitor_data
, test_visitor_in_number
);
272 input_visitor_test_add("/string-visitor/input/string",
273 &in_visitor_data
, test_visitor_in_string
);
274 input_visitor_test_add("/string-visitor/input/enum",
275 &in_visitor_data
, test_visitor_in_enum
);
276 input_visitor_test_add("/string-visitor/input/fuzz",
277 &in_visitor_data
, test_visitor_in_fuzz
);