]>
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); | |
2d7799f2 PB |
66 | } |
67 | ||
3d089cea MA |
68 | static void check_ilist(Visitor *v, int64_t *expected, size_t n) |
69 | { | |
70 | int64List *res = NULL; | |
71 | int64List *tail; | |
72 | int i; | |
73 | ||
74 | visit_type_int64List(v, NULL, &res, &error_abort); | |
75 | tail = res; | |
76 | for (i = 0; i < n; i++) { | |
77 | g_assert(tail); | |
78 | g_assert_cmpint(tail->value, ==, expected[i]); | |
79 | tail = tail->next; | |
80 | } | |
81 | g_assert(!tail); | |
82 | ||
83 | qapi_free_int64List(res); | |
84 | } | |
85 | ||
86 | static void check_ulist(Visitor *v, uint64_t *expected, size_t n) | |
87 | { | |
88 | uint64List *res = NULL; | |
89 | uint64List *tail; | |
90 | int i; | |
91 | ||
92 | /* BUG: unsigned numbers above INT64_MAX don't work */ | |
93 | for (i = 0; i < n; i++) { | |
94 | if (expected[i] > INT64_MAX) { | |
95 | Error *err = NULL; | |
96 | visit_type_uint64List(v, NULL, &res, &err); | |
97 | error_free_or_abort(&err); | |
98 | return; | |
99 | } | |
100 | } | |
101 | ||
102 | visit_type_uint64List(v, NULL, &res, &error_abort); | |
103 | tail = res; | |
104 | for (i = 0; i < n; i++) { | |
105 | g_assert(tail); | |
106 | g_assert_cmpuint(tail->value, ==, expected[i]); | |
107 | tail = tail->next; | |
108 | } | |
109 | g_assert(!tail); | |
110 | ||
111 | qapi_free_uint64List(res); | |
112 | } | |
113 | ||
659268ff HT |
114 | static void test_visitor_in_intList(TestInputVisitorData *data, |
115 | const void *unused) | |
116 | { | |
3d089cea MA |
117 | /* Note: the visitor *sorts* ranges *unsigned* */ |
118 | int64_t expect1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20 }; | |
119 | int64_t expect2[] = { 32767, -32768, -32767 }; | |
120 | int64_t expect3[] = { INT64_MAX, INT64_MIN }; | |
121 | uint64_t expect4[] = { UINT64_MAX }; | |
73374683 | 122 | Error *err = NULL; |
3d089cea | 123 | int64List *res = NULL; |
9cb8ef36 | 124 | int64List *tail; |
659268ff | 125 | Visitor *v; |
a9416dc6 | 126 | int64_t val; |
3d089cea MA |
127 | |
128 | /* Valid lists */ | |
659268ff HT |
129 | |
130 | v = visitor_input_test_init(data, "1,2,0,2-4,20,5-9,1-8"); | |
3d089cea | 131 | check_ilist(v, expect1, ARRAY_SIZE(expect1)); |
659268ff | 132 | |
3d089cea MA |
133 | v = visitor_input_test_init(data, "32767,-32768--32767"); |
134 | check_ilist(v, expect2, ARRAY_SIZE(expect2)); | |
135 | ||
136 | v = visitor_input_test_init(data, | |
137 | "-9223372036854775808,9223372036854775807"); | |
138 | check_ilist(v, expect3, ARRAY_SIZE(expect3)); | |
139 | ||
140 | v = visitor_input_test_init(data, "18446744073709551615"); | |
141 | check_ulist(v, expect4, ARRAY_SIZE(expect4)); | |
142 | ||
143 | /* Empty list is invalid (weird) */ | |
144 | ||
145 | v = visitor_input_test_init(data, ""); | |
146 | visit_type_int64List(v, NULL, &res, &err); | |
147 | error_free_or_abort(&err); | |
659268ff | 148 | |
3d089cea | 149 | /* Not a list */ |
73374683 | 150 | |
73374683 MA |
151 | v = visitor_input_test_init(data, "not an int list"); |
152 | ||
3d089cea | 153 | visit_type_int64List(v, NULL, &res, &err); |
74f24cb6 EB |
154 | error_free_or_abort(&err); |
155 | g_assert(!res); | |
9cb8ef36 MA |
156 | |
157 | /* Unvisited list tail */ | |
158 | ||
159 | v = visitor_input_test_init(data, "0,2-3"); | |
160 | ||
161 | /* Would be simpler if the visitor genuinely supported virtual walks */ | |
162 | visit_start_list(v, NULL, (GenericList **)&res, sizeof(*res), | |
163 | &error_abort); | |
164 | tail = res; | |
165 | visit_type_int64(v, NULL, &tail->value, &error_abort); | |
166 | g_assert_cmpint(tail->value, ==, 0); | |
167 | tail = (int64List *)visit_next_list(v, (GenericList *)tail, sizeof(*res)); | |
168 | g_assert(tail); | |
169 | visit_type_int64(v, NULL, &tail->value, &error_abort); | |
170 | g_assert_cmpint(tail->value, ==, 2); | |
171 | tail = (int64List *)visit_next_list(v, (GenericList *)tail, sizeof(*res)); | |
172 | g_assert(tail); | |
a4a1c70d MA |
173 | |
174 | visit_check_list(v, &err); | |
175 | error_free_or_abort(&err); | |
9cb8ef36 | 176 | visit_end_list(v, (void **)&res); |
9cb8ef36 MA |
177 | |
178 | qapi_free_int64List(res); | |
a9416dc6 MA |
179 | |
180 | /* Visit beyond end of list */ | |
181 | v = visitor_input_test_init(data, "0"); | |
182 | ||
183 | visit_start_list(v, NULL, (GenericList **)&res, sizeof(*res), | |
184 | &error_abort); | |
185 | tail = res; | |
186 | visit_type_int64(v, NULL, &tail->value, &err); | |
187 | g_assert_cmpint(tail->value, ==, 0); | |
188 | visit_type_int64(v, NULL, &val, &err); | |
189 | g_assert_cmpint(val, ==, 1); /* BUG */ | |
190 | visit_check_list(v, &error_abort); | |
191 | visit_end_list(v, (void **)&res); | |
192 | ||
193 | qapi_free_int64List(res); | |
659268ff HT |
194 | } |
195 | ||
2d7799f2 PB |
196 | static void test_visitor_in_bool(TestInputVisitorData *data, |
197 | const void *unused) | |
198 | { | |
e940f543 | 199 | Error *err = NULL; |
2d7799f2 PB |
200 | bool res = false; |
201 | Visitor *v; | |
202 | ||
203 | v = visitor_input_test_init(data, "true"); | |
204 | ||
51e72bc1 | 205 | visit_type_bool(v, NULL, &res, &err); |
e940f543 | 206 | g_assert(!err); |
2d7799f2 | 207 | g_assert_cmpint(res, ==, true); |
2d7799f2 PB |
208 | |
209 | v = visitor_input_test_init(data, "yes"); | |
210 | ||
51e72bc1 | 211 | visit_type_bool(v, NULL, &res, &err); |
e940f543 | 212 | g_assert(!err); |
2d7799f2 | 213 | g_assert_cmpint(res, ==, true); |
2d7799f2 PB |
214 | |
215 | v = visitor_input_test_init(data, "on"); | |
216 | ||
51e72bc1 | 217 | visit_type_bool(v, NULL, &res, &err); |
e940f543 | 218 | g_assert(!err); |
2d7799f2 | 219 | g_assert_cmpint(res, ==, true); |
2d7799f2 PB |
220 | |
221 | v = visitor_input_test_init(data, "false"); | |
222 | ||
51e72bc1 | 223 | visit_type_bool(v, NULL, &res, &err); |
e940f543 | 224 | g_assert(!err); |
2d7799f2 | 225 | g_assert_cmpint(res, ==, false); |
2d7799f2 PB |
226 | |
227 | v = visitor_input_test_init(data, "no"); | |
228 | ||
51e72bc1 | 229 | visit_type_bool(v, NULL, &res, &err); |
e940f543 | 230 | g_assert(!err); |
2d7799f2 | 231 | g_assert_cmpint(res, ==, false); |
2d7799f2 PB |
232 | |
233 | v = visitor_input_test_init(data, "off"); | |
234 | ||
51e72bc1 | 235 | visit_type_bool(v, NULL, &res, &err); |
e940f543 | 236 | g_assert(!err); |
2d7799f2 PB |
237 | g_assert_cmpint(res, ==, false); |
238 | } | |
239 | ||
240 | static void test_visitor_in_number(TestInputVisitorData *data, | |
241 | const void *unused) | |
242 | { | |
243 | double res = 0, value = 3.14; | |
e940f543 | 244 | Error *err = NULL; |
2d7799f2 PB |
245 | Visitor *v; |
246 | ||
247 | v = visitor_input_test_init(data, "3.14"); | |
248 | ||
51e72bc1 | 249 | visit_type_number(v, NULL, &res, &err); |
e940f543 | 250 | g_assert(!err); |
2d7799f2 PB |
251 | g_assert_cmpfloat(res, ==, value); |
252 | } | |
253 | ||
254 | static void test_visitor_in_string(TestInputVisitorData *data, | |
255 | const void *unused) | |
256 | { | |
257 | char *res = NULL, *value = (char *) "Q E M U"; | |
e940f543 | 258 | Error *err = NULL; |
2d7799f2 PB |
259 | Visitor *v; |
260 | ||
261 | v = visitor_input_test_init(data, value); | |
262 | ||
51e72bc1 | 263 | visit_type_str(v, NULL, &res, &err); |
e940f543 | 264 | g_assert(!err); |
2d7799f2 PB |
265 | g_assert_cmpstr(res, ==, value); |
266 | ||
267 | g_free(res); | |
268 | } | |
269 | ||
270 | static void test_visitor_in_enum(TestInputVisitorData *data, | |
271 | const void *unused) | |
272 | { | |
e940f543 | 273 | Error *err = NULL; |
2d7799f2 PB |
274 | Visitor *v; |
275 | EnumOne i; | |
276 | ||
277 | for (i = 0; EnumOne_lookup[i]; i++) { | |
278 | EnumOne res = -1; | |
279 | ||
280 | v = visitor_input_test_init(data, EnumOne_lookup[i]); | |
281 | ||
51e72bc1 | 282 | visit_type_EnumOne(v, NULL, &res, &err); |
e940f543 | 283 | g_assert(!err); |
2d7799f2 | 284 | g_assert_cmpint(i, ==, res); |
2d7799f2 | 285 | } |
2d7799f2 PB |
286 | } |
287 | ||
3f0f31a0 BS |
288 | /* Try to crash the visitors */ |
289 | static void test_visitor_in_fuzz(TestInputVisitorData *data, | |
290 | const void *unused) | |
291 | { | |
292 | int64_t ires; | |
659268ff | 293 | intList *ilres; |
3f0f31a0 BS |
294 | bool bres; |
295 | double nres; | |
296 | char *sres; | |
297 | EnumOne eres; | |
3f0f31a0 BS |
298 | Visitor *v; |
299 | unsigned int i; | |
300 | char buf[10000]; | |
301 | ||
302 | for (i = 0; i < 100; i++) { | |
303 | unsigned int j; | |
304 | ||
305 | j = g_test_rand_int_range(0, sizeof(buf) - 1); | |
306 | ||
307 | buf[j] = '\0'; | |
308 | ||
309 | if (j != 0) { | |
310 | for (j--; j != 0; j--) { | |
311 | buf[j - 1] = (char)g_test_rand_int_range(0, 256); | |
312 | } | |
313 | } | |
314 | ||
315 | v = visitor_input_test_init(data, buf); | |
51e72bc1 | 316 | visit_type_int(v, NULL, &ires, NULL); |
3f0f31a0 | 317 | |
659268ff | 318 | v = visitor_input_test_init(data, buf); |
51e72bc1 | 319 | visit_type_intList(v, NULL, &ilres, NULL); |
bd794065 | 320 | qapi_free_intList(ilres); |
659268ff | 321 | |
3f0f31a0 | 322 | v = visitor_input_test_init(data, buf); |
51e72bc1 | 323 | visit_type_bool(v, NULL, &bres, NULL); |
3f0f31a0 BS |
324 | |
325 | v = visitor_input_test_init(data, buf); | |
51e72bc1 | 326 | visit_type_number(v, NULL, &nres, NULL); |
3f0f31a0 BS |
327 | |
328 | v = visitor_input_test_init(data, buf); | |
01845438 | 329 | sres = NULL; |
51e72bc1 | 330 | visit_type_str(v, NULL, &sres, NULL); |
3f0f31a0 BS |
331 | g_free(sres); |
332 | ||
333 | v = visitor_input_test_init(data, buf); | |
51e72bc1 | 334 | visit_type_EnumOne(v, NULL, &eres, NULL); |
3f0f31a0 BS |
335 | } |
336 | } | |
337 | ||
2d7799f2 PB |
338 | static void input_visitor_test_add(const char *testpath, |
339 | TestInputVisitorData *data, | |
340 | void (*test_func)(TestInputVisitorData *data, const void *user_data)) | |
341 | { | |
342 | g_test_add(testpath, TestInputVisitorData, data, NULL, test_func, | |
343 | visitor_input_teardown); | |
344 | } | |
345 | ||
346 | int main(int argc, char **argv) | |
347 | { | |
348 | TestInputVisitorData in_visitor_data; | |
349 | ||
350 | g_test_init(&argc, &argv, NULL); | |
351 | ||
352 | input_visitor_test_add("/string-visitor/input/int", | |
353 | &in_visitor_data, test_visitor_in_int); | |
659268ff HT |
354 | input_visitor_test_add("/string-visitor/input/intList", |
355 | &in_visitor_data, test_visitor_in_intList); | |
2d7799f2 PB |
356 | input_visitor_test_add("/string-visitor/input/bool", |
357 | &in_visitor_data, test_visitor_in_bool); | |
358 | input_visitor_test_add("/string-visitor/input/number", | |
359 | &in_visitor_data, test_visitor_in_number); | |
360 | input_visitor_test_add("/string-visitor/input/string", | |
361 | &in_visitor_data, test_visitor_in_string); | |
362 | input_visitor_test_add("/string-visitor/input/enum", | |
363 | &in_visitor_data, test_visitor_in_enum); | |
3f0f31a0 BS |
364 | input_visitor_test_add("/string-visitor/input/fuzz", |
365 | &in_visitor_data, test_visitor_in_fuzz); | |
2d7799f2 PB |
366 | |
367 | g_test_run(); | |
368 | ||
369 | return 0; | |
370 | } |