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
, &error_abort
);
57 g_assert_cmpint(res
, ==, value
);
59 v
= visitor_input_test_init(data
, "not an int");
61 visit_type_int(v
, NULL
, &res
, &err
);
62 error_free_or_abort(&err
);
64 v
= visitor_input_test_init(data
, "");
66 visit_type_int(v
, NULL
, &res
, &err
);
67 error_free_or_abort(&err
);
70 static void check_ilist(Visitor
*v
, int64_t *expected
, size_t n
)
72 int64List
*res
= NULL
;
76 visit_type_int64List(v
, NULL
, &res
, &error_abort
);
78 for (i
= 0; i
< n
; i
++) {
80 g_assert_cmpint(tail
->value
, ==, expected
[i
]);
85 qapi_free_int64List(res
);
88 static void check_ulist(Visitor
*v
, uint64_t *expected
, size_t n
)
90 uint64List
*res
= NULL
;
94 visit_type_uint64List(v
, NULL
, &res
, &error_abort
);
96 for (i
= 0; i
< n
; i
++) {
98 g_assert_cmpuint(tail
->value
, ==, expected
[i
]);
103 qapi_free_uint64List(res
);
106 static void test_visitor_in_intList(TestInputVisitorData
*data
,
109 int64_t expect1
[] = { 1, 2, 0, 2, 3, 4, 20, 5, 6, 7,
110 8, 9, 1, 2, 3, 4, 5, 6, 7, 8 };
111 int64_t expect2
[] = { 32767, -32768, -32767 };
112 int64_t expect3
[] = { INT64_MIN
, INT64_MAX
};
113 int64_t expect4
[] = { 1 };
114 int64_t expect5
[] = { INT64_MAX
- 2, INT64_MAX
- 1, INT64_MAX
};
116 int64List
*res
= NULL
;
122 v
= visitor_input_test_init(data
, "1,2,0,2-4,20,5-9,1-8");
123 check_ilist(v
, expect1
, ARRAY_SIZE(expect1
));
125 v
= visitor_input_test_init(data
, "32767,-32768--32767");
126 check_ilist(v
, expect2
, ARRAY_SIZE(expect2
));
128 v
= visitor_input_test_init(data
,
129 "-9223372036854775808,9223372036854775807");
130 check_ilist(v
, expect3
, ARRAY_SIZE(expect3
));
132 v
= visitor_input_test_init(data
, "1-1");
133 check_ilist(v
, expect4
, ARRAY_SIZE(expect4
));
135 v
= visitor_input_test_init(data
,
136 "9223372036854775805-9223372036854775807");
137 check_ilist(v
, expect5
, ARRAY_SIZE(expect5
));
139 /* Value too large */
141 v
= visitor_input_test_init(data
, "9223372036854775808");
142 visit_type_int64List(v
, NULL
, &res
, &err
);
143 error_free_or_abort(&err
);
146 /* Value too small */
148 v
= visitor_input_test_init(data
, "-9223372036854775809");
149 visit_type_int64List(v
, NULL
, &res
, &err
);
150 error_free_or_abort(&err
);
153 /* Range not ascending */
155 v
= visitor_input_test_init(data
, "3-1");
156 visit_type_int64List(v
, NULL
, &res
, &err
);
157 error_free_or_abort(&err
);
160 v
= visitor_input_test_init(data
, "9223372036854775807-0");
161 visit_type_int64List(v
, NULL
, &res
, &err
);
162 error_free_or_abort(&err
);
165 /* Range too big (65536 is the limit against DOS attacks) */
167 v
= visitor_input_test_init(data
, "0-65536");
168 visit_type_int64List(v
, NULL
, &res
, &err
);
169 error_free_or_abort(&err
);
174 v
= visitor_input_test_init(data
, "");
175 visit_type_int64List(v
, NULL
, &res
, &error_abort
);
180 v
= visitor_input_test_init(data
, "not an int list");
182 visit_type_int64List(v
, NULL
, &res
, &err
);
183 error_free_or_abort(&err
);
186 /* Unvisited list tail */
188 v
= visitor_input_test_init(data
, "0,2-3");
190 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
191 visit_type_int64(v
, NULL
, &val
, &error_abort
);
192 g_assert_cmpint(val
, ==, 0);
193 visit_type_int64(v
, NULL
, &val
, &error_abort
);
194 g_assert_cmpint(val
, ==, 2);
196 visit_check_list(v
, &err
);
197 error_free_or_abort(&err
);
198 visit_end_list(v
, NULL
);
200 /* Visit beyond end of list */
202 v
= visitor_input_test_init(data
, "0");
204 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
205 visit_type_int64(v
, NULL
, &val
, &err
);
206 g_assert_cmpint(val
, ==, 0);
207 visit_type_int64(v
, NULL
, &val
, &err
);
208 error_free_or_abort(&err
);
210 visit_check_list(v
, &error_abort
);
211 visit_end_list(v
, NULL
);
214 static void test_visitor_in_uintList(TestInputVisitorData
*data
,
217 uint64_t expect1
[] = { 1, 2, 0, 2, 3, 4, 20, 5, 6, 7,
218 8, 9, 1, 2, 3, 4, 5, 6, 7, 8 };
219 uint64_t expect2
[] = { 32767, -32768, -32767 };
220 uint64_t expect3
[] = { INT64_MIN
, INT64_MAX
};
221 uint64_t expect4
[] = { 1 };
222 uint64_t expect5
[] = { UINT64_MAX
};
223 uint64_t expect6
[] = { UINT64_MAX
- 2, UINT64_MAX
- 1, UINT64_MAX
};
225 uint64List
*res
= NULL
;
231 v
= visitor_input_test_init(data
, "1,2,0,2-4,20,5-9,1-8");
232 check_ulist(v
, expect1
, ARRAY_SIZE(expect1
));
234 v
= visitor_input_test_init(data
, "32767,-32768--32767");
235 check_ulist(v
, expect2
, ARRAY_SIZE(expect2
));
237 v
= visitor_input_test_init(data
,
238 "-9223372036854775808,9223372036854775807");
239 check_ulist(v
, expect3
, ARRAY_SIZE(expect3
));
241 v
= visitor_input_test_init(data
, "1-1");
242 check_ulist(v
, expect4
, ARRAY_SIZE(expect4
));
244 v
= visitor_input_test_init(data
, "18446744073709551615");
245 check_ulist(v
, expect5
, ARRAY_SIZE(expect5
));
247 v
= visitor_input_test_init(data
,
248 "18446744073709551613-18446744073709551615");
249 check_ulist(v
, expect6
, ARRAY_SIZE(expect6
));
251 /* Value too large */
253 v
= visitor_input_test_init(data
, "18446744073709551616");
254 visit_type_uint64List(v
, NULL
, &res
, &err
);
255 error_free_or_abort(&err
);
258 /* Value too small */
260 v
= visitor_input_test_init(data
, "-18446744073709551616");
261 visit_type_uint64List(v
, NULL
, &res
, &err
);
262 error_free_or_abort(&err
);
265 /* Range not ascending */
267 v
= visitor_input_test_init(data
, "3-1");
268 visit_type_uint64List(v
, NULL
, &res
, &err
);
269 error_free_or_abort(&err
);
272 v
= visitor_input_test_init(data
, "18446744073709551615-0");
273 visit_type_uint64List(v
, NULL
, &res
, &err
);
274 error_free_or_abort(&err
);
277 /* Range too big (65536 is the limit against DOS attacks) */
279 v
= visitor_input_test_init(data
, "0-65536");
280 visit_type_uint64List(v
, NULL
, &res
, &err
);
281 error_free_or_abort(&err
);
286 v
= visitor_input_test_init(data
, "");
287 visit_type_uint64List(v
, NULL
, &res
, &error_abort
);
292 v
= visitor_input_test_init(data
, "not an uint list");
294 visit_type_uint64List(v
, NULL
, &res
, &err
);
295 error_free_or_abort(&err
);
298 /* Unvisited list tail */
300 v
= visitor_input_test_init(data
, "0,2-3");
302 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
303 visit_type_uint64(v
, NULL
, &val
, &error_abort
);
304 g_assert_cmpuint(val
, ==, 0);
305 visit_type_uint64(v
, NULL
, &val
, &error_abort
);
306 g_assert_cmpuint(val
, ==, 2);
308 visit_check_list(v
, &err
);
309 error_free_or_abort(&err
);
310 visit_end_list(v
, NULL
);
312 /* Visit beyond end of list */
314 v
= visitor_input_test_init(data
, "0");
316 visit_start_list(v
, NULL
, NULL
, 0, &error_abort
);
317 visit_type_uint64(v
, NULL
, &val
, &err
);
318 g_assert_cmpuint(val
, ==, 0);
319 visit_type_uint64(v
, NULL
, &val
, &err
);
320 error_free_or_abort(&err
);
322 visit_check_list(v
, &error_abort
);
323 visit_end_list(v
, NULL
);
326 static void test_visitor_in_bool(TestInputVisitorData
*data
,
332 v
= visitor_input_test_init(data
, "true");
334 visit_type_bool(v
, NULL
, &res
, &error_abort
);
335 g_assert_cmpint(res
, ==, true);
337 v
= visitor_input_test_init(data
, "yes");
339 visit_type_bool(v
, NULL
, &res
, &error_abort
);
340 g_assert_cmpint(res
, ==, true);
342 v
= visitor_input_test_init(data
, "on");
344 visit_type_bool(v
, NULL
, &res
, &error_abort
);
345 g_assert_cmpint(res
, ==, true);
347 v
= visitor_input_test_init(data
, "false");
349 visit_type_bool(v
, NULL
, &res
, &error_abort
);
350 g_assert_cmpint(res
, ==, false);
352 v
= visitor_input_test_init(data
, "no");
354 visit_type_bool(v
, NULL
, &res
, &error_abort
);
355 g_assert_cmpint(res
, ==, false);
357 v
= visitor_input_test_init(data
, "off");
359 visit_type_bool(v
, NULL
, &res
, &error_abort
);
360 g_assert_cmpint(res
, ==, false);
363 static void test_visitor_in_number(TestInputVisitorData
*data
,
366 double res
= 0, value
= 3.14;
370 v
= visitor_input_test_init(data
, "3.14");
372 visit_type_number(v
, NULL
, &res
, &error_abort
);
373 g_assert_cmpfloat(res
, ==, value
);
375 /* NaN and infinity has to be rejected */
377 v
= visitor_input_test_init(data
, "NaN");
379 visit_type_number(v
, NULL
, &res
, &err
);
380 error_free_or_abort(&err
);
382 v
= visitor_input_test_init(data
, "inf");
384 visit_type_number(v
, NULL
, &res
, &err
);
385 error_free_or_abort(&err
);
389 static void test_visitor_in_string(TestInputVisitorData
*data
,
392 char *res
= NULL
, *value
= (char *) "Q E M U";
395 v
= visitor_input_test_init(data
, value
);
397 visit_type_str(v
, NULL
, &res
, &error_abort
);
398 g_assert_cmpstr(res
, ==, value
);
403 static void test_visitor_in_enum(TestInputVisitorData
*data
,
409 for (i
= 0; i
< ENUM_ONE__MAX
; i
++) {
412 v
= visitor_input_test_init(data
, EnumOne_str(i
));
414 visit_type_EnumOne(v
, NULL
, &res
, &error_abort
);
415 g_assert_cmpint(i
, ==, res
);
419 /* Try to crash the visitors */
420 static void test_visitor_in_fuzz(TestInputVisitorData
*data
,
433 for (i
= 0; i
< 100; i
++) {
436 j
= g_test_rand_int_range(0, sizeof(buf
) - 1);
440 for (k
= 0; k
!= j
; k
++) {
441 buf
[k
] = (char)g_test_rand_int_range(0, 256);
444 v
= visitor_input_test_init(data
, buf
);
445 visit_type_int(v
, NULL
, &ires
, NULL
);
447 v
= visitor_input_test_init(data
, buf
);
448 visit_type_intList(v
, NULL
, &ilres
, NULL
);
449 qapi_free_intList(ilres
);
451 v
= visitor_input_test_init(data
, buf
);
452 visit_type_bool(v
, NULL
, &bres
, NULL
);
454 v
= visitor_input_test_init(data
, buf
);
455 visit_type_number(v
, NULL
, &nres
, NULL
);
457 v
= visitor_input_test_init(data
, buf
);
459 visit_type_str(v
, NULL
, &sres
, NULL
);
462 v
= visitor_input_test_init(data
, buf
);
463 visit_type_EnumOne(v
, NULL
, &eres
, NULL
);
467 static void input_visitor_test_add(const char *testpath
,
468 TestInputVisitorData
*data
,
469 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
471 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
472 visitor_input_teardown
);
475 int main(int argc
, char **argv
)
477 TestInputVisitorData in_visitor_data
;
479 g_test_init(&argc
, &argv
, NULL
);
481 input_visitor_test_add("/string-visitor/input/int",
482 &in_visitor_data
, test_visitor_in_int
);
483 input_visitor_test_add("/string-visitor/input/intList",
484 &in_visitor_data
, test_visitor_in_intList
);
485 input_visitor_test_add("/string-visitor/input/uintList",
486 &in_visitor_data
, test_visitor_in_uintList
);
487 input_visitor_test_add("/string-visitor/input/bool",
488 &in_visitor_data
, test_visitor_in_bool
);
489 input_visitor_test_add("/string-visitor/input/number",
490 &in_visitor_data
, test_visitor_in_number
);
491 input_visitor_test_add("/string-visitor/input/string",
492 &in_visitor_data
, test_visitor_in_string
);
493 input_visitor_test_add("/string-visitor/input/enum",
494 &in_visitor_data
, test_visitor_in_enum
);
495 input_visitor_test_add("/string-visitor/input/fuzz",
496 &in_visitor_data
, test_visitor_in_fuzz
);