2 * String Input Visitor unit-tests.
4 * Copyright (C) 2012 Red Hat Inc.
7 * Paolo Bonzini <pbonzini@redhat.com> (based on test-qobject-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-visit.h"
20 typedef struct TestInputVisitorData
{
22 } TestInputVisitorData
;
24 static void visitor_input_teardown(TestInputVisitorData
*data
,
33 /* This is provided instead of a test setup function so that the JSON
34 string used by the tests are kept in the test functions (and not
37 Visitor
*visitor_input_test_init(TestInputVisitorData
*data
,
40 visitor_input_teardown(data
, NULL
);
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 v
= visitor_input_test_init(data
, "not an int");
62 visit_type_int(v
, NULL
, &res
, &err
);
63 error_free_or_abort(&err
);
65 v
= visitor_input_test_init(data
, "");
67 visit_type_int(v
, NULL
, &res
, &err
);
68 error_free_or_abort(&err
);
71 static void check_ilist(Visitor
*v
, int64_t *expected
, size_t n
)
73 int64List
*res
= NULL
;
77 visit_type_int64List(v
, NULL
, &res
, &error_abort
);
79 for (i
= 0; i
< n
; i
++) {
81 g_assert_cmpint(tail
->value
, ==, expected
[i
]);
86 qapi_free_int64List(res
);
89 static void check_ulist(Visitor
*v
, uint64_t *expected
, size_t n
)
91 uint64List
*res
= NULL
;
95 /* BUG: unsigned numbers above INT64_MAX don't work */
96 for (i
= 0; i
< n
; i
++) {
97 if (expected
[i
] > INT64_MAX
) {
99 visit_type_uint64List(v
, NULL
, &res
, &err
);
100 error_free_or_abort(&err
);
105 visit_type_uint64List(v
, NULL
, &res
, &error_abort
);
107 for (i
= 0; i
< n
; i
++) {
109 g_assert_cmpuint(tail
->value
, ==, expected
[i
]);
114 qapi_free_uint64List(res
);
117 static void test_visitor_in_intList(TestInputVisitorData
*data
,
120 /* Note: the visitor *sorts* ranges *unsigned* */
121 int64_t expect1
[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20 };
122 int64_t expect2
[] = { 32767, -32768, -32767 };
123 int64_t expect3
[] = { INT64_MAX
, INT64_MIN
};
124 uint64_t expect4
[] = { UINT64_MAX
};
126 int64List
*res
= NULL
;
133 v
= visitor_input_test_init(data
, "1,2,0,2-4,20,5-9,1-8");
134 check_ilist(v
, expect1
, ARRAY_SIZE(expect1
));
136 v
= visitor_input_test_init(data
, "32767,-32768--32767");
137 check_ilist(v
, expect2
, ARRAY_SIZE(expect2
));
139 v
= visitor_input_test_init(data
,
140 "-9223372036854775808,9223372036854775807");
141 check_ilist(v
, expect3
, ARRAY_SIZE(expect3
));
143 v
= visitor_input_test_init(data
, "18446744073709551615");
144 check_ulist(v
, expect4
, ARRAY_SIZE(expect4
));
148 v
= visitor_input_test_init(data
, "");
149 visit_type_int64List(v
, NULL
, &res
, &error_abort
);
154 v
= visitor_input_test_init(data
, "not an int list");
156 visit_type_int64List(v
, NULL
, &res
, &err
);
157 error_free_or_abort(&err
);
160 /* Unvisited list tail */
162 v
= visitor_input_test_init(data
, "0,2-3");
164 /* Would be simpler if the visitor genuinely supported virtual walks */
165 visit_start_list(v
, NULL
, (GenericList
**)&res
, sizeof(*res
),
168 visit_type_int64(v
, NULL
, &tail
->value
, &error_abort
);
169 g_assert_cmpint(tail
->value
, ==, 0);
170 tail
= (int64List
*)visit_next_list(v
, (GenericList
*)tail
, sizeof(*res
));
172 visit_type_int64(v
, NULL
, &tail
->value
, &error_abort
);
173 g_assert_cmpint(tail
->value
, ==, 2);
174 tail
= (int64List
*)visit_next_list(v
, (GenericList
*)tail
, sizeof(*res
));
177 visit_check_list(v
, &err
);
178 error_free_or_abort(&err
);
179 visit_end_list(v
, (void **)&res
);
181 qapi_free_int64List(res
);
183 /* Visit beyond end of list */
184 v
= visitor_input_test_init(data
, "0");
186 visit_start_list(v
, NULL
, (GenericList
**)&res
, sizeof(*res
),
189 visit_type_int64(v
, NULL
, &tail
->value
, &err
);
190 g_assert_cmpint(tail
->value
, ==, 0);
191 visit_type_int64(v
, NULL
, &val
, &err
);
192 g_assert_cmpint(val
, ==, 1); /* BUG */
193 visit_check_list(v
, &error_abort
);
194 visit_end_list(v
, (void **)&res
);
196 qapi_free_int64List(res
);
199 static void test_visitor_in_bool(TestInputVisitorData
*data
,
206 v
= visitor_input_test_init(data
, "true");
208 visit_type_bool(v
, NULL
, &res
, &err
);
210 g_assert_cmpint(res
, ==, true);
212 v
= visitor_input_test_init(data
, "yes");
214 visit_type_bool(v
, NULL
, &res
, &err
);
216 g_assert_cmpint(res
, ==, true);
218 v
= visitor_input_test_init(data
, "on");
220 visit_type_bool(v
, NULL
, &res
, &err
);
222 g_assert_cmpint(res
, ==, true);
224 v
= visitor_input_test_init(data
, "false");
226 visit_type_bool(v
, NULL
, &res
, &err
);
228 g_assert_cmpint(res
, ==, false);
230 v
= visitor_input_test_init(data
, "no");
232 visit_type_bool(v
, NULL
, &res
, &err
);
234 g_assert_cmpint(res
, ==, false);
236 v
= visitor_input_test_init(data
, "off");
238 visit_type_bool(v
, NULL
, &res
, &err
);
240 g_assert_cmpint(res
, ==, false);
243 static void test_visitor_in_number(TestInputVisitorData
*data
,
246 double res
= 0, value
= 3.14;
250 v
= visitor_input_test_init(data
, "3.14");
252 visit_type_number(v
, NULL
, &res
, &err
);
254 g_assert_cmpfloat(res
, ==, value
);
257 static void test_visitor_in_string(TestInputVisitorData
*data
,
260 char *res
= NULL
, *value
= (char *) "Q E M U";
264 v
= visitor_input_test_init(data
, value
);
266 visit_type_str(v
, NULL
, &res
, &err
);
268 g_assert_cmpstr(res
, ==, value
);
273 static void test_visitor_in_enum(TestInputVisitorData
*data
,
280 for (i
= 0; i
< ENUM_ONE__MAX
; i
++) {
283 v
= visitor_input_test_init(data
, EnumOne_str(i
));
285 visit_type_EnumOne(v
, NULL
, &res
, &err
);
287 g_assert_cmpint(i
, ==, res
);
291 /* Try to crash the visitors */
292 static void test_visitor_in_fuzz(TestInputVisitorData
*data
,
305 for (i
= 0; i
< 100; i
++) {
308 j
= g_test_rand_int_range(0, sizeof(buf
) - 1);
313 for (j
--; j
!= 0; j
--) {
314 buf
[j
- 1] = (char)g_test_rand_int_range(0, 256);
318 v
= visitor_input_test_init(data
, buf
);
319 visit_type_int(v
, NULL
, &ires
, NULL
);
321 v
= visitor_input_test_init(data
, buf
);
322 visit_type_intList(v
, NULL
, &ilres
, NULL
);
323 qapi_free_intList(ilres
);
325 v
= visitor_input_test_init(data
, buf
);
326 visit_type_bool(v
, NULL
, &bres
, NULL
);
328 v
= visitor_input_test_init(data
, buf
);
329 visit_type_number(v
, NULL
, &nres
, NULL
);
331 v
= visitor_input_test_init(data
, buf
);
333 visit_type_str(v
, NULL
, &sres
, NULL
);
336 v
= visitor_input_test_init(data
, buf
);
337 visit_type_EnumOne(v
, NULL
, &eres
, NULL
);
341 static void input_visitor_test_add(const char *testpath
,
342 TestInputVisitorData
*data
,
343 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
345 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
346 visitor_input_teardown
);
349 int main(int argc
, char **argv
)
351 TestInputVisitorData in_visitor_data
;
353 g_test_init(&argc
, &argv
, NULL
);
355 input_visitor_test_add("/string-visitor/input/int",
356 &in_visitor_data
, test_visitor_in_int
);
357 input_visitor_test_add("/string-visitor/input/intList",
358 &in_visitor_data
, test_visitor_in_intList
);
359 input_visitor_test_add("/string-visitor/input/bool",
360 &in_visitor_data
, test_visitor_in_bool
);
361 input_visitor_test_add("/string-visitor/input/number",
362 &in_visitor_data
, test_visitor_in_number
);
363 input_visitor_test_add("/string-visitor/input/string",
364 &in_visitor_data
, test_visitor_in_string
);
365 input_visitor_test_add("/string-visitor/input/enum",
366 &in_visitor_data
, test_visitor_in_enum
);
367 input_visitor_test_add("/string-visitor/input/fuzz",
368 &in_visitor_data
, test_visitor_in_fuzz
);