]>
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; |
659268ff | 124 | Visitor *v; |
3d089cea MA |
125 | |
126 | /* Valid lists */ | |
659268ff HT |
127 | |
128 | v = visitor_input_test_init(data, "1,2,0,2-4,20,5-9,1-8"); | |
3d089cea | 129 | check_ilist(v, expect1, ARRAY_SIZE(expect1)); |
659268ff | 130 | |
3d089cea MA |
131 | v = visitor_input_test_init(data, "32767,-32768--32767"); |
132 | check_ilist(v, expect2, ARRAY_SIZE(expect2)); | |
133 | ||
134 | v = visitor_input_test_init(data, | |
135 | "-9223372036854775808,9223372036854775807"); | |
136 | check_ilist(v, expect3, ARRAY_SIZE(expect3)); | |
137 | ||
138 | v = visitor_input_test_init(data, "18446744073709551615"); | |
139 | check_ulist(v, expect4, ARRAY_SIZE(expect4)); | |
140 | ||
141 | /* Empty list is invalid (weird) */ | |
142 | ||
143 | v = visitor_input_test_init(data, ""); | |
144 | visit_type_int64List(v, NULL, &res, &err); | |
145 | error_free_or_abort(&err); | |
659268ff | 146 | |
3d089cea | 147 | /* Not a list */ |
73374683 | 148 | |
73374683 MA |
149 | v = visitor_input_test_init(data, "not an int list"); |
150 | ||
3d089cea | 151 | visit_type_int64List(v, NULL, &res, &err); |
74f24cb6 EB |
152 | error_free_or_abort(&err); |
153 | g_assert(!res); | |
659268ff HT |
154 | } |
155 | ||
2d7799f2 PB |
156 | static void test_visitor_in_bool(TestInputVisitorData *data, |
157 | const void *unused) | |
158 | { | |
e940f543 | 159 | Error *err = NULL; |
2d7799f2 PB |
160 | bool res = false; |
161 | Visitor *v; | |
162 | ||
163 | v = visitor_input_test_init(data, "true"); | |
164 | ||
51e72bc1 | 165 | visit_type_bool(v, NULL, &res, &err); |
e940f543 | 166 | g_assert(!err); |
2d7799f2 | 167 | g_assert_cmpint(res, ==, true); |
2d7799f2 PB |
168 | |
169 | v = visitor_input_test_init(data, "yes"); | |
170 | ||
51e72bc1 | 171 | visit_type_bool(v, NULL, &res, &err); |
e940f543 | 172 | g_assert(!err); |
2d7799f2 | 173 | g_assert_cmpint(res, ==, true); |
2d7799f2 PB |
174 | |
175 | v = visitor_input_test_init(data, "on"); | |
176 | ||
51e72bc1 | 177 | visit_type_bool(v, NULL, &res, &err); |
e940f543 | 178 | g_assert(!err); |
2d7799f2 | 179 | g_assert_cmpint(res, ==, true); |
2d7799f2 PB |
180 | |
181 | v = visitor_input_test_init(data, "false"); | |
182 | ||
51e72bc1 | 183 | visit_type_bool(v, NULL, &res, &err); |
e940f543 | 184 | g_assert(!err); |
2d7799f2 | 185 | g_assert_cmpint(res, ==, false); |
2d7799f2 PB |
186 | |
187 | v = visitor_input_test_init(data, "no"); | |
188 | ||
51e72bc1 | 189 | visit_type_bool(v, NULL, &res, &err); |
e940f543 | 190 | g_assert(!err); |
2d7799f2 | 191 | g_assert_cmpint(res, ==, false); |
2d7799f2 PB |
192 | |
193 | v = visitor_input_test_init(data, "off"); | |
194 | ||
51e72bc1 | 195 | visit_type_bool(v, NULL, &res, &err); |
e940f543 | 196 | g_assert(!err); |
2d7799f2 PB |
197 | g_assert_cmpint(res, ==, false); |
198 | } | |
199 | ||
200 | static void test_visitor_in_number(TestInputVisitorData *data, | |
201 | const void *unused) | |
202 | { | |
203 | double res = 0, value = 3.14; | |
e940f543 | 204 | Error *err = NULL; |
2d7799f2 PB |
205 | Visitor *v; |
206 | ||
207 | v = visitor_input_test_init(data, "3.14"); | |
208 | ||
51e72bc1 | 209 | visit_type_number(v, NULL, &res, &err); |
e940f543 | 210 | g_assert(!err); |
2d7799f2 PB |
211 | g_assert_cmpfloat(res, ==, value); |
212 | } | |
213 | ||
214 | static void test_visitor_in_string(TestInputVisitorData *data, | |
215 | const void *unused) | |
216 | { | |
217 | char *res = NULL, *value = (char *) "Q E M U"; | |
e940f543 | 218 | Error *err = NULL; |
2d7799f2 PB |
219 | Visitor *v; |
220 | ||
221 | v = visitor_input_test_init(data, value); | |
222 | ||
51e72bc1 | 223 | visit_type_str(v, NULL, &res, &err); |
e940f543 | 224 | g_assert(!err); |
2d7799f2 PB |
225 | g_assert_cmpstr(res, ==, value); |
226 | ||
227 | g_free(res); | |
228 | } | |
229 | ||
230 | static void test_visitor_in_enum(TestInputVisitorData *data, | |
231 | const void *unused) | |
232 | { | |
e940f543 | 233 | Error *err = NULL; |
2d7799f2 PB |
234 | Visitor *v; |
235 | EnumOne i; | |
236 | ||
237 | for (i = 0; EnumOne_lookup[i]; i++) { | |
238 | EnumOne res = -1; | |
239 | ||
240 | v = visitor_input_test_init(data, EnumOne_lookup[i]); | |
241 | ||
51e72bc1 | 242 | visit_type_EnumOne(v, NULL, &res, &err); |
e940f543 | 243 | g_assert(!err); |
2d7799f2 | 244 | g_assert_cmpint(i, ==, res); |
2d7799f2 | 245 | } |
2d7799f2 PB |
246 | } |
247 | ||
3f0f31a0 BS |
248 | /* Try to crash the visitors */ |
249 | static void test_visitor_in_fuzz(TestInputVisitorData *data, | |
250 | const void *unused) | |
251 | { | |
252 | int64_t ires; | |
659268ff | 253 | intList *ilres; |
3f0f31a0 BS |
254 | bool bres; |
255 | double nres; | |
256 | char *sres; | |
257 | EnumOne eres; | |
3f0f31a0 BS |
258 | Visitor *v; |
259 | unsigned int i; | |
260 | char buf[10000]; | |
261 | ||
262 | for (i = 0; i < 100; i++) { | |
263 | unsigned int j; | |
264 | ||
265 | j = g_test_rand_int_range(0, sizeof(buf) - 1); | |
266 | ||
267 | buf[j] = '\0'; | |
268 | ||
269 | if (j != 0) { | |
270 | for (j--; j != 0; j--) { | |
271 | buf[j - 1] = (char)g_test_rand_int_range(0, 256); | |
272 | } | |
273 | } | |
274 | ||
275 | v = visitor_input_test_init(data, buf); | |
51e72bc1 | 276 | visit_type_int(v, NULL, &ires, NULL); |
3f0f31a0 | 277 | |
659268ff | 278 | v = visitor_input_test_init(data, buf); |
51e72bc1 | 279 | visit_type_intList(v, NULL, &ilres, NULL); |
bd794065 | 280 | qapi_free_intList(ilres); |
659268ff | 281 | |
3f0f31a0 | 282 | v = visitor_input_test_init(data, buf); |
51e72bc1 | 283 | visit_type_bool(v, NULL, &bres, NULL); |
3f0f31a0 BS |
284 | |
285 | v = visitor_input_test_init(data, buf); | |
51e72bc1 | 286 | visit_type_number(v, NULL, &nres, NULL); |
3f0f31a0 BS |
287 | |
288 | v = visitor_input_test_init(data, buf); | |
01845438 | 289 | sres = NULL; |
51e72bc1 | 290 | visit_type_str(v, NULL, &sres, NULL); |
3f0f31a0 BS |
291 | g_free(sres); |
292 | ||
293 | v = visitor_input_test_init(data, buf); | |
51e72bc1 | 294 | visit_type_EnumOne(v, NULL, &eres, NULL); |
3f0f31a0 BS |
295 | } |
296 | } | |
297 | ||
2d7799f2 PB |
298 | static void input_visitor_test_add(const char *testpath, |
299 | TestInputVisitorData *data, | |
300 | void (*test_func)(TestInputVisitorData *data, const void *user_data)) | |
301 | { | |
302 | g_test_add(testpath, TestInputVisitorData, data, NULL, test_func, | |
303 | visitor_input_teardown); | |
304 | } | |
305 | ||
306 | int main(int argc, char **argv) | |
307 | { | |
308 | TestInputVisitorData in_visitor_data; | |
309 | ||
310 | g_test_init(&argc, &argv, NULL); | |
311 | ||
312 | input_visitor_test_add("/string-visitor/input/int", | |
313 | &in_visitor_data, test_visitor_in_int); | |
659268ff HT |
314 | input_visitor_test_add("/string-visitor/input/intList", |
315 | &in_visitor_data, test_visitor_in_intList); | |
2d7799f2 PB |
316 | input_visitor_test_add("/string-visitor/input/bool", |
317 | &in_visitor_data, test_visitor_in_bool); | |
318 | input_visitor_test_add("/string-visitor/input/number", | |
319 | &in_visitor_data, test_visitor_in_number); | |
320 | input_visitor_test_add("/string-visitor/input/string", | |
321 | &in_visitor_data, test_visitor_in_string); | |
322 | input_visitor_test_add("/string-visitor/input/enum", | |
323 | &in_visitor_data, test_visitor_in_enum); | |
3f0f31a0 BS |
324 | input_visitor_test_add("/string-visitor/input/fuzz", |
325 | &in_visitor_data, test_visitor_in_fuzz); | |
2d7799f2 PB |
326 | |
327 | g_test_run(); | |
328 | ||
329 | return 0; | |
330 | } |