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 visit_type_uint64List(v
, NULL
, &res
, &error_abort
);
97 for (i
= 0; i
< n
; i
++) {
99 g_assert_cmpuint(tail
->value
, ==, expected
[i
]);
104 qapi_free_uint64List(res
);
107 static void test_visitor_in_intList(TestInputVisitorData
*data
,
110 int64_t expect1
[] = { 1, 2, 0, 2, 3, 4, 20, 5, 6, 7,
111 8, 9, 1, 2, 3, 4, 5, 6, 7, 8 };
112 int64_t expect2
[] = { 32767, -32768, -32767 };
113 int64_t expect3
[] = { INT64_MIN
, INT64_MAX
};
114 int64_t expect4
[] = { 1 };
115 int64_t expect5
[] = { INT64_MAX
- 2, INT64_MAX
- 1, INT64_MAX
};
117 int64List
*res
= NULL
;
123 v
= visitor_input_test_init(data
, "1,2,0,2-4,20,5-9,1-8");
124 check_ilist(v
, expect1
, ARRAY_SIZE(expect1
));
126 v
= visitor_input_test_init(data
, "32767,-32768--32767");
127 check_ilist(v
, expect2
, ARRAY_SIZE(expect2
));
129 v
= visitor_input_test_init(data
,
130 "-9223372036854775808,9223372036854775807");
131 check_ilist(v
, expect3
, ARRAY_SIZE(expect3
));
133 v
= visitor_input_test_init(data
, "1-1");
134 check_ilist(v
, expect4
, ARRAY_SIZE(expect4
));
136 v
= visitor_input_test_init(data
,
137 "9223372036854775805-9223372036854775807");
138 check_ilist(v
, expect5
, ARRAY_SIZE(expect5
));
140 /* Value too large */
142 v
= visitor_input_test_init(data
, "9223372036854775808");
143 visit_type_int64List(v
, NULL
, &res
, &err
);
144 error_free_or_abort(&err
);
147 /* Value too small */
149 v
= visitor_input_test_init(data
, "-9223372036854775809");
150 visit_type_int64List(v
, NULL
, &res
, &err
);
151 error_free_or_abort(&err
);
154 /* Range not ascending */
156 v
= visitor_input_test_init(data
, "3-1");
157 visit_type_int64List(v
, NULL
, &res
, &err
);
158 error_free_or_abort(&err
);
161 v
= visitor_input_test_init(data
, "9223372036854775807-0");
162 visit_type_int64List(v
, NULL
, &res
, &err
);
163 error_free_or_abort(&err
);
166 /* Range too big (65536 is the limit against DOS attacks) */
168 v
= visitor_input_test_init(data
, "0-65536");
169 visit_type_int64List(v
, NULL
, &res
, &err
);
170 error_free_or_abort(&err
);
175 v
= visitor_input_test_init(data
, "");
176 visit_type_int64List(v
, NULL
, &res
, &error_abort
);
181 v
= visitor_input_test_init(data
, "not an int list");
183 visit_type_int64List(v
, NULL
, &res
, &err
);
184 error_free_or_abort(&err
);
187 /* Unvisited list tail */
189 v
= visitor_input_test_init(data
, "0,2-3");
191 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
192 visit_type_int64(v
, NULL
, &val
, &error_abort
);
193 g_assert_cmpint(val
, ==, 0);
194 visit_type_int64(v
, NULL
, &val
, &error_abort
);
195 g_assert_cmpint(val
, ==, 2);
197 visit_check_list(v
, &err
);
198 error_free_or_abort(&err
);
199 visit_end_list(v
, NULL
);
201 /* Visit beyond end of list */
203 v
= visitor_input_test_init(data
, "0");
205 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
206 visit_type_int64(v
, NULL
, &val
, &err
);
207 g_assert_cmpint(val
, ==, 0);
208 visit_type_int64(v
, NULL
, &val
, &err
);
209 error_free_or_abort(&err
);
211 visit_check_list(v
, &error_abort
);
212 visit_end_list(v
, NULL
);
215 static void test_visitor_in_uintList(TestInputVisitorData
*data
,
218 uint64_t expect1
[] = { 1, 2, 0, 2, 3, 4, 20, 5, 6, 7,
219 8, 9, 1, 2, 3, 4, 5, 6, 7, 8 };
220 uint64_t expect2
[] = { 32767, -32768, -32767 };
221 uint64_t expect3
[] = { INT64_MIN
, INT64_MAX
};
222 uint64_t expect4
[] = { 1 };
223 uint64_t expect5
[] = { UINT64_MAX
};
224 uint64_t expect6
[] = { UINT64_MAX
- 2, UINT64_MAX
- 1, UINT64_MAX
};
226 uint64List
*res
= NULL
;
232 v
= visitor_input_test_init(data
, "1,2,0,2-4,20,5-9,1-8");
233 check_ulist(v
, expect1
, ARRAY_SIZE(expect1
));
235 v
= visitor_input_test_init(data
, "32767,-32768--32767");
236 check_ulist(v
, expect2
, ARRAY_SIZE(expect2
));
238 v
= visitor_input_test_init(data
,
239 "-9223372036854775808,9223372036854775807");
240 check_ulist(v
, expect3
, ARRAY_SIZE(expect3
));
242 v
= visitor_input_test_init(data
, "1-1");
243 check_ulist(v
, expect4
, ARRAY_SIZE(expect4
));
245 v
= visitor_input_test_init(data
, "18446744073709551615");
246 check_ulist(v
, expect5
, ARRAY_SIZE(expect5
));
248 v
= visitor_input_test_init(data
,
249 "18446744073709551613-18446744073709551615");
250 check_ulist(v
, expect6
, ARRAY_SIZE(expect6
));
252 /* Value too large */
254 v
= visitor_input_test_init(data
, "18446744073709551616");
255 visit_type_uint64List(v
, NULL
, &res
, &err
);
256 error_free_or_abort(&err
);
259 /* Value too small */
261 v
= visitor_input_test_init(data
, "-18446744073709551616");
262 visit_type_uint64List(v
, NULL
, &res
, &err
);
263 error_free_or_abort(&err
);
266 /* Range not ascending */
268 v
= visitor_input_test_init(data
, "3-1");
269 visit_type_uint64List(v
, NULL
, &res
, &err
);
270 error_free_or_abort(&err
);
273 v
= visitor_input_test_init(data
, "18446744073709551615-0");
274 visit_type_uint64List(v
, NULL
, &res
, &err
);
275 error_free_or_abort(&err
);
278 /* Range too big (65536 is the limit against DOS attacks) */
280 v
= visitor_input_test_init(data
, "0-65536");
281 visit_type_uint64List(v
, NULL
, &res
, &err
);
282 error_free_or_abort(&err
);
287 v
= visitor_input_test_init(data
, "");
288 visit_type_uint64List(v
, NULL
, &res
, &error_abort
);
293 v
= visitor_input_test_init(data
, "not an uint list");
295 visit_type_uint64List(v
, NULL
, &res
, &err
);
296 error_free_or_abort(&err
);
299 /* Unvisited list tail */
301 v
= visitor_input_test_init(data
, "0,2-3");
303 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
304 visit_type_uint64(v
, NULL
, &val
, &error_abort
);
305 g_assert_cmpuint(val
, ==, 0);
306 visit_type_uint64(v
, NULL
, &val
, &error_abort
);
307 g_assert_cmpuint(val
, ==, 2);
309 visit_check_list(v
, &err
);
310 error_free_or_abort(&err
);
311 visit_end_list(v
, NULL
);
313 /* Visit beyond end of list */
315 v
= visitor_input_test_init(data
, "0");
317 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
318 visit_type_uint64(v
, NULL
, &val
, &err
);
319 g_assert_cmpuint(val
, ==, 0);
320 visit_type_uint64(v
, NULL
, &val
, &err
);
321 error_free_or_abort(&err
);
323 visit_check_list(v
, &error_abort
);
324 visit_end_list(v
, NULL
);
327 static void test_visitor_in_bool(TestInputVisitorData
*data
,
334 v
= visitor_input_test_init(data
, "true");
336 visit_type_bool(v
, NULL
, &res
, &err
);
338 g_assert_cmpint(res
, ==, true);
340 v
= visitor_input_test_init(data
, "yes");
342 visit_type_bool(v
, NULL
, &res
, &err
);
344 g_assert_cmpint(res
, ==, true);
346 v
= visitor_input_test_init(data
, "on");
348 visit_type_bool(v
, NULL
, &res
, &err
);
350 g_assert_cmpint(res
, ==, true);
352 v
= visitor_input_test_init(data
, "false");
354 visit_type_bool(v
, NULL
, &res
, &err
);
356 g_assert_cmpint(res
, ==, false);
358 v
= visitor_input_test_init(data
, "no");
360 visit_type_bool(v
, NULL
, &res
, &err
);
362 g_assert_cmpint(res
, ==, false);
364 v
= visitor_input_test_init(data
, "off");
366 visit_type_bool(v
, NULL
, &res
, &err
);
368 g_assert_cmpint(res
, ==, false);
371 static void test_visitor_in_number(TestInputVisitorData
*data
,
374 double res
= 0, value
= 3.14;
378 v
= visitor_input_test_init(data
, "3.14");
380 visit_type_number(v
, NULL
, &res
, &err
);
382 g_assert_cmpfloat(res
, ==, value
);
384 /* NaN and infinity has to be rejected */
386 v
= visitor_input_test_init(data
, "NaN");
388 visit_type_number(v
, NULL
, &res
, &err
);
389 error_free_or_abort(&err
);
391 v
= visitor_input_test_init(data
, "inf");
393 visit_type_number(v
, NULL
, &res
, &err
);
394 error_free_or_abort(&err
);
398 static void test_visitor_in_string(TestInputVisitorData
*data
,
401 char *res
= NULL
, *value
= (char *) "Q E M U";
405 v
= visitor_input_test_init(data
, value
);
407 visit_type_str(v
, NULL
, &res
, &err
);
409 g_assert_cmpstr(res
, ==, value
);
414 static void test_visitor_in_enum(TestInputVisitorData
*data
,
421 for (i
= 0; i
< ENUM_ONE__MAX
; i
++) {
424 v
= visitor_input_test_init(data
, EnumOne_str(i
));
426 visit_type_EnumOne(v
, NULL
, &res
, &err
);
428 g_assert_cmpint(i
, ==, res
);
432 /* Try to crash the visitors */
433 static void test_visitor_in_fuzz(TestInputVisitorData
*data
,
446 for (i
= 0; i
< 100; i
++) {
449 j
= g_test_rand_int_range(0, sizeof(buf
) - 1);
453 for (k
= 0; k
!= j
; k
++) {
454 buf
[k
] = (char)g_test_rand_int_range(0, 256);
457 v
= visitor_input_test_init(data
, buf
);
458 visit_type_int(v
, NULL
, &ires
, NULL
);
460 v
= visitor_input_test_init(data
, buf
);
461 visit_type_intList(v
, NULL
, &ilres
, NULL
);
462 qapi_free_intList(ilres
);
464 v
= visitor_input_test_init(data
, buf
);
465 visit_type_bool(v
, NULL
, &bres
, NULL
);
467 v
= visitor_input_test_init(data
, buf
);
468 visit_type_number(v
, NULL
, &nres
, NULL
);
470 v
= visitor_input_test_init(data
, buf
);
472 visit_type_str(v
, NULL
, &sres
, NULL
);
475 v
= visitor_input_test_init(data
, buf
);
476 visit_type_EnumOne(v
, NULL
, &eres
, NULL
);
480 static void input_visitor_test_add(const char *testpath
,
481 TestInputVisitorData
*data
,
482 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
484 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
485 visitor_input_teardown
);
488 int main(int argc
, char **argv
)
490 TestInputVisitorData in_visitor_data
;
492 g_test_init(&argc
, &argv
, NULL
);
494 input_visitor_test_add("/string-visitor/input/int",
495 &in_visitor_data
, test_visitor_in_int
);
496 input_visitor_test_add("/string-visitor/input/intList",
497 &in_visitor_data
, test_visitor_in_intList
);
498 input_visitor_test_add("/string-visitor/input/uintList",
499 &in_visitor_data
, test_visitor_in_uintList
);
500 input_visitor_test_add("/string-visitor/input/bool",
501 &in_visitor_data
, test_visitor_in_bool
);
502 input_visitor_test_add("/string-visitor/input/number",
503 &in_visitor_data
, test_visitor_in_number
);
504 input_visitor_test_add("/string-visitor/input/string",
505 &in_visitor_data
, test_visitor_in_string
);
506 input_visitor_test_add("/string-visitor/input/enum",
507 &in_visitor_data
, test_visitor_in_enum
);
508 input_visitor_test_add("/string-visitor/input/fuzz",
509 &in_visitor_data
, test_visitor_in_fuzz
);