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-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 visitor_input_teardown(data
, NULL
);
44 data
->v
= string_input_visitor_new(string
);
49 static void test_visitor_in_int(TestInputVisitorData
*data
,
52 int64_t res
= 0, value
= -42;
56 v
= visitor_input_test_init(data
, "-42");
58 visit_type_int(v
, NULL
, &res
, &err
);
60 g_assert_cmpint(res
, ==, value
);
62 v
= visitor_input_test_init(data
, "not an int");
64 visit_type_int(v
, NULL
, &res
, &err
);
65 error_free_or_abort(&err
);
67 v
= visitor_input_test_init(data
, "");
69 visit_type_int(v
, NULL
, &res
, &err
);
70 error_free_or_abort(&err
);
73 static void check_ilist(Visitor
*v
, int64_t *expected
, size_t n
)
75 int64List
*res
= NULL
;
79 visit_type_int64List(v
, NULL
, &res
, &error_abort
);
81 for (i
= 0; i
< n
; i
++) {
83 g_assert_cmpint(tail
->value
, ==, expected
[i
]);
88 qapi_free_int64List(res
);
91 static void check_ulist(Visitor
*v
, uint64_t *expected
, size_t n
)
93 uint64List
*res
= NULL
;
97 /* BUG: unsigned numbers above INT64_MAX don't work */
98 for (i
= 0; i
< n
; i
++) {
99 if (expected
[i
] > INT64_MAX
) {
101 visit_type_uint64List(v
, NULL
, &res
, &err
);
102 error_free_or_abort(&err
);
107 visit_type_uint64List(v
, NULL
, &res
, &error_abort
);
109 for (i
= 0; i
< n
; i
++) {
111 g_assert_cmpuint(tail
->value
, ==, expected
[i
]);
116 qapi_free_uint64List(res
);
119 static void test_visitor_in_intList(TestInputVisitorData
*data
,
122 /* Note: the visitor *sorts* ranges *unsigned* */
123 int64_t expect1
[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20 };
124 int64_t expect2
[] = { 32767, -32768, -32767 };
125 int64_t expect3
[] = { INT64_MAX
, INT64_MIN
};
126 uint64_t expect4
[] = { UINT64_MAX
};
128 int64List
*res
= NULL
;
135 v
= visitor_input_test_init(data
, "1,2,0,2-4,20,5-9,1-8");
136 check_ilist(v
, expect1
, ARRAY_SIZE(expect1
));
138 v
= visitor_input_test_init(data
, "32767,-32768--32767");
139 check_ilist(v
, expect2
, ARRAY_SIZE(expect2
));
141 v
= visitor_input_test_init(data
,
142 "-9223372036854775808,9223372036854775807");
143 check_ilist(v
, expect3
, ARRAY_SIZE(expect3
));
145 v
= visitor_input_test_init(data
, "18446744073709551615");
146 check_ulist(v
, expect4
, ARRAY_SIZE(expect4
));
150 v
= visitor_input_test_init(data
, "");
151 visit_type_int64List(v
, NULL
, &res
, &error_abort
);
156 v
= visitor_input_test_init(data
, "not an int list");
158 visit_type_int64List(v
, NULL
, &res
, &err
);
159 error_free_or_abort(&err
);
162 /* Unvisited list tail */
164 v
= visitor_input_test_init(data
, "0,2-3");
166 /* Would be simpler if the visitor genuinely supported virtual walks */
167 visit_start_list(v
, NULL
, (GenericList
**)&res
, sizeof(*res
),
170 visit_type_int64(v
, NULL
, &tail
->value
, &error_abort
);
171 g_assert_cmpint(tail
->value
, ==, 0);
172 tail
= (int64List
*)visit_next_list(v
, (GenericList
*)tail
, sizeof(*res
));
174 visit_type_int64(v
, NULL
, &tail
->value
, &error_abort
);
175 g_assert_cmpint(tail
->value
, ==, 2);
176 tail
= (int64List
*)visit_next_list(v
, (GenericList
*)tail
, sizeof(*res
));
179 visit_check_list(v
, &err
);
180 error_free_or_abort(&err
);
181 visit_end_list(v
, (void **)&res
);
183 qapi_free_int64List(res
);
185 /* Visit beyond end of list */
186 v
= visitor_input_test_init(data
, "0");
188 visit_start_list(v
, NULL
, (GenericList
**)&res
, sizeof(*res
),
191 visit_type_int64(v
, NULL
, &tail
->value
, &err
);
192 g_assert_cmpint(tail
->value
, ==, 0);
193 visit_type_int64(v
, NULL
, &val
, &err
);
194 g_assert_cmpint(val
, ==, 1); /* BUG */
195 visit_check_list(v
, &error_abort
);
196 visit_end_list(v
, (void **)&res
);
198 qapi_free_int64List(res
);
201 static void test_visitor_in_bool(TestInputVisitorData
*data
,
208 v
= visitor_input_test_init(data
, "true");
210 visit_type_bool(v
, NULL
, &res
, &err
);
212 g_assert_cmpint(res
, ==, true);
214 v
= visitor_input_test_init(data
, "yes");
216 visit_type_bool(v
, NULL
, &res
, &err
);
218 g_assert_cmpint(res
, ==, true);
220 v
= visitor_input_test_init(data
, "on");
222 visit_type_bool(v
, NULL
, &res
, &err
);
224 g_assert_cmpint(res
, ==, true);
226 v
= visitor_input_test_init(data
, "false");
228 visit_type_bool(v
, NULL
, &res
, &err
);
230 g_assert_cmpint(res
, ==, false);
232 v
= visitor_input_test_init(data
, "no");
234 visit_type_bool(v
, NULL
, &res
, &err
);
236 g_assert_cmpint(res
, ==, false);
238 v
= visitor_input_test_init(data
, "off");
240 visit_type_bool(v
, NULL
, &res
, &err
);
242 g_assert_cmpint(res
, ==, false);
245 static void test_visitor_in_number(TestInputVisitorData
*data
,
248 double res
= 0, value
= 3.14;
252 v
= visitor_input_test_init(data
, "3.14");
254 visit_type_number(v
, NULL
, &res
, &err
);
256 g_assert_cmpfloat(res
, ==, value
);
259 static void test_visitor_in_string(TestInputVisitorData
*data
,
262 char *res
= NULL
, *value
= (char *) "Q E M U";
266 v
= visitor_input_test_init(data
, value
);
268 visit_type_str(v
, NULL
, &res
, &err
);
270 g_assert_cmpstr(res
, ==, value
);
275 static void test_visitor_in_enum(TestInputVisitorData
*data
,
282 for (i
= 0; EnumOne_lookup
[i
]; i
++) {
285 v
= visitor_input_test_init(data
, EnumOne_lookup
[i
]);
287 visit_type_EnumOne(v
, NULL
, &res
, &err
);
289 g_assert_cmpint(i
, ==, res
);
293 /* Try to crash the visitors */
294 static void test_visitor_in_fuzz(TestInputVisitorData
*data
,
307 for (i
= 0; i
< 100; i
++) {
310 j
= g_test_rand_int_range(0, sizeof(buf
) - 1);
315 for (j
--; j
!= 0; j
--) {
316 buf
[j
- 1] = (char)g_test_rand_int_range(0, 256);
320 v
= visitor_input_test_init(data
, buf
);
321 visit_type_int(v
, NULL
, &ires
, NULL
);
323 v
= visitor_input_test_init(data
, buf
);
324 visit_type_intList(v
, NULL
, &ilres
, NULL
);
325 qapi_free_intList(ilres
);
327 v
= visitor_input_test_init(data
, buf
);
328 visit_type_bool(v
, NULL
, &bres
, NULL
);
330 v
= visitor_input_test_init(data
, buf
);
331 visit_type_number(v
, NULL
, &nres
, NULL
);
333 v
= visitor_input_test_init(data
, buf
);
335 visit_type_str(v
, NULL
, &sres
, NULL
);
338 v
= visitor_input_test_init(data
, buf
);
339 visit_type_EnumOne(v
, NULL
, &eres
, NULL
);
343 static void input_visitor_test_add(const char *testpath
,
344 TestInputVisitorData
*data
,
345 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
347 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
348 visitor_input_teardown
);
351 int main(int argc
, char **argv
)
353 TestInputVisitorData in_visitor_data
;
355 g_test_init(&argc
, &argv
, NULL
);
357 input_visitor_test_add("/string-visitor/input/int",
358 &in_visitor_data
, test_visitor_in_int
);
359 input_visitor_test_add("/string-visitor/input/intList",
360 &in_visitor_data
, test_visitor_in_intList
);
361 input_visitor_test_add("/string-visitor/input/bool",
362 &in_visitor_data
, test_visitor_in_bool
);
363 input_visitor_test_add("/string-visitor/input/number",
364 &in_visitor_data
, test_visitor_in_number
);
365 input_visitor_test_add("/string-visitor/input/string",
366 &in_visitor_data
, test_visitor_in_string
);
367 input_visitor_test_add("/string-visitor/input/enum",
368 &in_visitor_data
, test_visitor_in_enum
);
369 input_visitor_test_add("/string-visitor/input/fuzz",
370 &in_visitor_data
, test_visitor_in_fuzz
);