]>
Commit | Line | Data |
---|---|---|
2d7799f2 PB |
1 | /* |
2 | * String Input Visitor unit-tests. | |
3 | * | |
4 | * Copyright (C) 2012 Red Hat Inc. | |
5 | * | |
6 | * Authors: | |
b3db211f | 7 | * Paolo Bonzini <[email protected]> (based on test-qobject-input-visitor) |
2d7799f2 PB |
8 | * |
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. | |
11 | */ | |
12 | ||
681c28a3 | 13 | #include "qemu/osdep.h" |
2d7799f2 | 14 | |
79ee7df8 | 15 | #include "qemu-common.h" |
da34e65c | 16 | #include "qapi/error.h" |
2d7799f2 PB |
17 | #include "qapi/string-input-visitor.h" |
18 | #include "test-qapi-types.h" | |
19 | #include "test-qapi-visit.h" | |
7b1b5d19 | 20 | #include "qapi/qmp/types.h" |
2d7799f2 PB |
21 | |
22 | typedef struct TestInputVisitorData { | |
7a0525c7 | 23 | Visitor *v; |
2d7799f2 PB |
24 | } TestInputVisitorData; |
25 | ||
26 | static void visitor_input_teardown(TestInputVisitorData *data, | |
27 | const void *unused) | |
28 | { | |
7a0525c7 EB |
29 | if (data->v) { |
30 | visit_free(data->v); | |
31 | data->v = NULL; | |
2d7799f2 PB |
32 | } |
33 | } | |
34 | ||
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 | |
37 | int main()) */ | |
38 | static | |
39 | Visitor *visitor_input_test_init(TestInputVisitorData *data, | |
40 | const char *string) | |
41 | { | |
0f721d16 MA |
42 | visitor_input_teardown(data, NULL); |
43 | ||
7a0525c7 EB |
44 | data->v = string_input_visitor_new(string); |
45 | g_assert(data->v); | |
46 | return data->v; | |
2d7799f2 PB |
47 | } |
48 | ||
49 | static void test_visitor_in_int(TestInputVisitorData *data, | |
50 | const void *unused) | |
51 | { | |
52 | int64_t res = 0, value = -42; | |
e940f543 | 53 | Error *err = NULL; |
2d7799f2 PB |
54 | Visitor *v; |
55 | ||
56 | v = visitor_input_test_init(data, "-42"); | |
57 | ||
51e72bc1 | 58 | visit_type_int(v, NULL, &res, &err); |
e940f543 | 59 | g_assert(!err); |
2d7799f2 | 60 | g_assert_cmpint(res, ==, value); |
73374683 | 61 | |
73374683 MA |
62 | v = visitor_input_test_init(data, "not an int"); |
63 | ||
64 | visit_type_int(v, NULL, &res, &err); | |
65 | error_free_or_abort(&err); | |
d2788227 MA |
66 | |
67 | v = visitor_input_test_init(data, ""); | |
68 | ||
69 | visit_type_int(v, NULL, &res, &err); | |
70 | error_free_or_abort(&err); | |
2d7799f2 PB |
71 | } |
72 | ||
3d089cea MA |
73 | static void check_ilist(Visitor *v, int64_t *expected, size_t n) |
74 | { | |
75 | int64List *res = NULL; | |
76 | int64List *tail; | |
77 | int i; | |
78 | ||
79 | visit_type_int64List(v, NULL, &res, &error_abort); | |
80 | tail = res; | |
81 | for (i = 0; i < n; i++) { | |
82 | g_assert(tail); | |
83 | g_assert_cmpint(tail->value, ==, expected[i]); | |
84 | tail = tail->next; | |
85 | } | |
86 | g_assert(!tail); | |
87 | ||
88 | qapi_free_int64List(res); | |
89 | } | |
90 | ||
91 | static void check_ulist(Visitor *v, uint64_t *expected, size_t n) | |
92 | { | |
93 | uint64List *res = NULL; | |
94 | uint64List *tail; | |
95 | int i; | |
96 | ||
97 | /* BUG: unsigned numbers above INT64_MAX don't work */ | |
98 | for (i = 0; i < n; i++) { | |
99 | if (expected[i] > INT64_MAX) { | |
100 | Error *err = NULL; | |
101 | visit_type_uint64List(v, NULL, &res, &err); | |
102 | error_free_or_abort(&err); | |
103 | return; | |
104 | } | |
105 | } | |
106 | ||
107 | visit_type_uint64List(v, NULL, &res, &error_abort); | |
108 | tail = res; | |
109 | for (i = 0; i < n; i++) { | |
110 | g_assert(tail); | |
111 | g_assert_cmpuint(tail->value, ==, expected[i]); | |
112 | tail = tail->next; | |
113 | } | |
114 | g_assert(!tail); | |
115 | ||
116 | qapi_free_uint64List(res); | |
117 | } | |
118 | ||
659268ff HT |
119 | static void test_visitor_in_intList(TestInputVisitorData *data, |
120 | const void *unused) | |
121 | { | |
3d089cea MA |
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 }; | |
73374683 | 127 | Error *err = NULL; |
3d089cea | 128 | int64List *res = NULL; |
9cb8ef36 | 129 | int64List *tail; |
659268ff | 130 | Visitor *v; |
a9416dc6 | 131 | int64_t val; |
3d089cea MA |
132 | |
133 | /* Valid lists */ | |
659268ff HT |
134 | |
135 | v = visitor_input_test_init(data, "1,2,0,2-4,20,5-9,1-8"); | |
3d089cea | 136 | check_ilist(v, expect1, ARRAY_SIZE(expect1)); |
659268ff | 137 | |
3d089cea MA |
138 | v = visitor_input_test_init(data, "32767,-32768--32767"); |
139 | check_ilist(v, expect2, ARRAY_SIZE(expect2)); | |
140 | ||
141 | v = visitor_input_test_init(data, | |
142 | "-9223372036854775808,9223372036854775807"); | |
143 | check_ilist(v, expect3, ARRAY_SIZE(expect3)); | |
144 | ||
145 | v = visitor_input_test_init(data, "18446744073709551615"); | |
146 | check_ulist(v, expect4, ARRAY_SIZE(expect4)); | |
147 | ||
d2788227 | 148 | /* Empty list */ |
3d089cea MA |
149 | |
150 | v = visitor_input_test_init(data, ""); | |
d2788227 MA |
151 | visit_type_int64List(v, NULL, &res, &error_abort); |
152 | g_assert(!res); | |
659268ff | 153 | |
3d089cea | 154 | /* Not a list */ |
73374683 | 155 | |
73374683 MA |
156 | v = visitor_input_test_init(data, "not an int list"); |
157 | ||
3d089cea | 158 | visit_type_int64List(v, NULL, &res, &err); |
74f24cb6 EB |
159 | error_free_or_abort(&err); |
160 | g_assert(!res); | |
9cb8ef36 MA |
161 | |
162 | /* Unvisited list tail */ | |
163 | ||
164 | v = visitor_input_test_init(data, "0,2-3"); | |
165 | ||
166 | /* Would be simpler if the visitor genuinely supported virtual walks */ | |
167 | visit_start_list(v, NULL, (GenericList **)&res, sizeof(*res), | |
168 | &error_abort); | |
169 | tail = 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)); | |
173 | g_assert(tail); | |
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)); | |
177 | g_assert(tail); | |
a4a1c70d MA |
178 | |
179 | visit_check_list(v, &err); | |
180 | error_free_or_abort(&err); | |
9cb8ef36 | 181 | visit_end_list(v, (void **)&res); |
9cb8ef36 MA |
182 | |
183 | qapi_free_int64List(res); | |
a9416dc6 MA |
184 | |
185 | /* Visit beyond end of list */ | |
186 | v = visitor_input_test_init(data, "0"); | |
187 | ||
188 | visit_start_list(v, NULL, (GenericList **)&res, sizeof(*res), | |
189 | &error_abort); | |
190 | tail = 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); | |
197 | ||
198 | qapi_free_int64List(res); | |
659268ff HT |
199 | } |
200 | ||
2d7799f2 PB |
201 | static void test_visitor_in_bool(TestInputVisitorData *data, |
202 | const void *unused) | |
203 | { | |
e940f543 | 204 | Error *err = NULL; |
2d7799f2 PB |
205 | bool res = false; |
206 | Visitor *v; | |
207 | ||
208 | v = visitor_input_test_init(data, "true"); | |
209 | ||
51e72bc1 | 210 | visit_type_bool(v, NULL, &res, &err); |
e940f543 | 211 | g_assert(!err); |
2d7799f2 | 212 | g_assert_cmpint(res, ==, true); |
2d7799f2 PB |
213 | |
214 | v = visitor_input_test_init(data, "yes"); | |
215 | ||
51e72bc1 | 216 | visit_type_bool(v, NULL, &res, &err); |
e940f543 | 217 | g_assert(!err); |
2d7799f2 | 218 | g_assert_cmpint(res, ==, true); |
2d7799f2 PB |
219 | |
220 | v = visitor_input_test_init(data, "on"); | |
221 | ||
51e72bc1 | 222 | visit_type_bool(v, NULL, &res, &err); |
e940f543 | 223 | g_assert(!err); |
2d7799f2 | 224 | g_assert_cmpint(res, ==, true); |
2d7799f2 PB |
225 | |
226 | v = visitor_input_test_init(data, "false"); | |
227 | ||
51e72bc1 | 228 | visit_type_bool(v, NULL, &res, &err); |
e940f543 | 229 | g_assert(!err); |
2d7799f2 | 230 | g_assert_cmpint(res, ==, false); |
2d7799f2 PB |
231 | |
232 | v = visitor_input_test_init(data, "no"); | |
233 | ||
51e72bc1 | 234 | visit_type_bool(v, NULL, &res, &err); |
e940f543 | 235 | g_assert(!err); |
2d7799f2 | 236 | g_assert_cmpint(res, ==, false); |
2d7799f2 PB |
237 | |
238 | v = visitor_input_test_init(data, "off"); | |
239 | ||
51e72bc1 | 240 | visit_type_bool(v, NULL, &res, &err); |
e940f543 | 241 | g_assert(!err); |
2d7799f2 PB |
242 | g_assert_cmpint(res, ==, false); |
243 | } | |
244 | ||
245 | static void test_visitor_in_number(TestInputVisitorData *data, | |
246 | const void *unused) | |
247 | { | |
248 | double res = 0, value = 3.14; | |
e940f543 | 249 | Error *err = NULL; |
2d7799f2 PB |
250 | Visitor *v; |
251 | ||
252 | v = visitor_input_test_init(data, "3.14"); | |
253 | ||
51e72bc1 | 254 | visit_type_number(v, NULL, &res, &err); |
e940f543 | 255 | g_assert(!err); |
2d7799f2 PB |
256 | g_assert_cmpfloat(res, ==, value); |
257 | } | |
258 | ||
259 | static void test_visitor_in_string(TestInputVisitorData *data, | |
260 | const void *unused) | |
261 | { | |
262 | char *res = NULL, *value = (char *) "Q E M U"; | |
e940f543 | 263 | Error *err = NULL; |
2d7799f2 PB |
264 | Visitor *v; |
265 | ||
266 | v = visitor_input_test_init(data, value); | |
267 | ||
51e72bc1 | 268 | visit_type_str(v, NULL, &res, &err); |
e940f543 | 269 | g_assert(!err); |
2d7799f2 PB |
270 | g_assert_cmpstr(res, ==, value); |
271 | ||
272 | g_free(res); | |
273 | } | |
274 | ||
275 | static void test_visitor_in_enum(TestInputVisitorData *data, | |
276 | const void *unused) | |
277 | { | |
e940f543 | 278 | Error *err = NULL; |
2d7799f2 PB |
279 | Visitor *v; |
280 | EnumOne i; | |
281 | ||
282 | for (i = 0; EnumOne_lookup[i]; i++) { | |
283 | EnumOne res = -1; | |
284 | ||
285 | v = visitor_input_test_init(data, EnumOne_lookup[i]); | |
286 | ||
51e72bc1 | 287 | visit_type_EnumOne(v, NULL, &res, &err); |
e940f543 | 288 | g_assert(!err); |
2d7799f2 | 289 | g_assert_cmpint(i, ==, res); |
2d7799f2 | 290 | } |
2d7799f2 PB |
291 | } |
292 | ||
3f0f31a0 BS |
293 | /* Try to crash the visitors */ |
294 | static void test_visitor_in_fuzz(TestInputVisitorData *data, | |
295 | const void *unused) | |
296 | { | |
297 | int64_t ires; | |
659268ff | 298 | intList *ilres; |
3f0f31a0 BS |
299 | bool bres; |
300 | double nres; | |
301 | char *sres; | |
302 | EnumOne eres; | |
3f0f31a0 BS |
303 | Visitor *v; |
304 | unsigned int i; | |
305 | char buf[10000]; | |
306 | ||
307 | for (i = 0; i < 100; i++) { | |
308 | unsigned int j; | |
309 | ||
310 | j = g_test_rand_int_range(0, sizeof(buf) - 1); | |
311 | ||
312 | buf[j] = '\0'; | |
313 | ||
314 | if (j != 0) { | |
315 | for (j--; j != 0; j--) { | |
316 | buf[j - 1] = (char)g_test_rand_int_range(0, 256); | |
317 | } | |
318 | } | |
319 | ||
320 | v = visitor_input_test_init(data, buf); | |
51e72bc1 | 321 | visit_type_int(v, NULL, &ires, NULL); |
3f0f31a0 | 322 | |
659268ff | 323 | v = visitor_input_test_init(data, buf); |
51e72bc1 | 324 | visit_type_intList(v, NULL, &ilres, NULL); |
bd794065 | 325 | qapi_free_intList(ilres); |
659268ff | 326 | |
3f0f31a0 | 327 | v = visitor_input_test_init(data, buf); |
51e72bc1 | 328 | visit_type_bool(v, NULL, &bres, NULL); |
3f0f31a0 BS |
329 | |
330 | v = visitor_input_test_init(data, buf); | |
51e72bc1 | 331 | visit_type_number(v, NULL, &nres, NULL); |
3f0f31a0 BS |
332 | |
333 | v = visitor_input_test_init(data, buf); | |
01845438 | 334 | sres = NULL; |
51e72bc1 | 335 | visit_type_str(v, NULL, &sres, NULL); |
3f0f31a0 BS |
336 | g_free(sres); |
337 | ||
338 | v = visitor_input_test_init(data, buf); | |
51e72bc1 | 339 | visit_type_EnumOne(v, NULL, &eres, NULL); |
3f0f31a0 BS |
340 | } |
341 | } | |
342 | ||
2d7799f2 PB |
343 | static void input_visitor_test_add(const char *testpath, |
344 | TestInputVisitorData *data, | |
345 | void (*test_func)(TestInputVisitorData *data, const void *user_data)) | |
346 | { | |
347 | g_test_add(testpath, TestInputVisitorData, data, NULL, test_func, | |
348 | visitor_input_teardown); | |
349 | } | |
350 | ||
351 | int main(int argc, char **argv) | |
352 | { | |
353 | TestInputVisitorData in_visitor_data; | |
354 | ||
355 | g_test_init(&argc, &argv, NULL); | |
356 | ||
357 | input_visitor_test_add("/string-visitor/input/int", | |
358 | &in_visitor_data, test_visitor_in_int); | |
659268ff HT |
359 | input_visitor_test_add("/string-visitor/input/intList", |
360 | &in_visitor_data, test_visitor_in_intList); | |
2d7799f2 PB |
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); | |
3f0f31a0 BS |
369 | input_visitor_test_add("/string-visitor/input/fuzz", |
370 | &in_visitor_data, test_visitor_in_fuzz); | |
2d7799f2 PB |
371 | |
372 | g_test_run(); | |
373 | ||
374 | return 0; | |
375 | } |