cirrus: fix off-by-one in cirrus_bitblt_rop_bkwd_transp_*_16
[qemu.git] / tests / test-string-input-visitor.c
1 /*
2 * String Input Visitor unit-tests.
3 *
4 * Copyright (C) 2012 Red Hat Inc.
5 *
6 * Authors:
7 * Paolo Bonzini <pbonzini@redhat.com> (based on test-qobject-input-visitor)
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
13 #include "qemu/osdep.h"
14
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"
21
22 typedef struct TestInputVisitorData {
23 Visitor *v;
24 } TestInputVisitorData;
25
26 static void visitor_input_teardown(TestInputVisitorData *data,
27 const void *unused)
28 {
29 if (data->v) {
30 visit_free(data->v);
31 data->v = NULL;
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 {
42 visitor_input_teardown(data, NULL);
43
44 data->v = string_input_visitor_new(string);
45 g_assert(data->v);
46 return data->v;
47 }
48
49 static void test_visitor_in_int(TestInputVisitorData *data,
50 const void *unused)
51 {
52 int64_t res = 0, value = -42;
53 Error *err = NULL;
54 Visitor *v;
55
56 v = visitor_input_test_init(data, "-42");
57
58 visit_type_int(v, NULL, &res, &err);
59 g_assert(!err);
60 g_assert_cmpint(res, ==, value);
61
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);
66 }
67
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
114 static void test_visitor_in_intList(TestInputVisitorData *data,
115 const void *unused)
116 {
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 };
122 Error *err = NULL;
123 int64List *res = NULL;
124 int64List *tail;
125 Visitor *v;
126 int64_t val;
127
128 /* Valid lists */
129
130 v = visitor_input_test_init(data, "1,2,0,2-4,20,5-9,1-8");
131 check_ilist(v, expect1, ARRAY_SIZE(expect1));
132
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);
148
149 /* Not a list */
150
151 v = visitor_input_test_init(data, "not an int list");
152
153 visit_type_int64List(v, NULL, &res, &err);
154 error_free_or_abort(&err);
155 g_assert(!res);
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);
173
174 visit_check_list(v, &err);
175 error_free_or_abort(&err);
176 visit_end_list(v, (void **)&res);
177
178 qapi_free_int64List(res);
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);
194 }
195
196 static void test_visitor_in_bool(TestInputVisitorData *data,
197 const void *unused)
198 {
199 Error *err = NULL;
200 bool res = false;
201 Visitor *v;
202
203 v = visitor_input_test_init(data, "true");
204
205 visit_type_bool(v, NULL, &res, &err);
206 g_assert(!err);
207 g_assert_cmpint(res, ==, true);
208
209 v = visitor_input_test_init(data, "yes");
210
211 visit_type_bool(v, NULL, &res, &err);
212 g_assert(!err);
213 g_assert_cmpint(res, ==, true);
214
215 v = visitor_input_test_init(data, "on");
216
217 visit_type_bool(v, NULL, &res, &err);
218 g_assert(!err);
219 g_assert_cmpint(res, ==, true);
220
221 v = visitor_input_test_init(data, "false");
222
223 visit_type_bool(v, NULL, &res, &err);
224 g_assert(!err);
225 g_assert_cmpint(res, ==, false);
226
227 v = visitor_input_test_init(data, "no");
228
229 visit_type_bool(v, NULL, &res, &err);
230 g_assert(!err);
231 g_assert_cmpint(res, ==, false);
232
233 v = visitor_input_test_init(data, "off");
234
235 visit_type_bool(v, NULL, &res, &err);
236 g_assert(!err);
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;
244 Error *err = NULL;
245 Visitor *v;
246
247 v = visitor_input_test_init(data, "3.14");
248
249 visit_type_number(v, NULL, &res, &err);
250 g_assert(!err);
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";
258 Error *err = NULL;
259 Visitor *v;
260
261 v = visitor_input_test_init(data, value);
262
263 visit_type_str(v, NULL, &res, &err);
264 g_assert(!err);
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 {
273 Error *err = NULL;
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
282 visit_type_EnumOne(v, NULL, &res, &err);
283 g_assert(!err);
284 g_assert_cmpint(i, ==, res);
285 }
286 }
287
288 /* Try to crash the visitors */
289 static void test_visitor_in_fuzz(TestInputVisitorData *data,
290 const void *unused)
291 {
292 int64_t ires;
293 intList *ilres;
294 bool bres;
295 double nres;
296 char *sres;
297 EnumOne eres;
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);
316 visit_type_int(v, NULL, &ires, NULL);
317
318 v = visitor_input_test_init(data, buf);
319 visit_type_intList(v, NULL, &ilres, NULL);
320 qapi_free_intList(ilres);
321
322 v = visitor_input_test_init(data, buf);
323 visit_type_bool(v, NULL, &bres, NULL);
324
325 v = visitor_input_test_init(data, buf);
326 visit_type_number(v, NULL, &nres, NULL);
327
328 v = visitor_input_test_init(data, buf);
329 sres = NULL;
330 visit_type_str(v, NULL, &sres, NULL);
331 g_free(sres);
332
333 v = visitor_input_test_init(data, buf);
334 visit_type_EnumOne(v, NULL, &eres, NULL);
335 }
336 }
337
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);
354 input_visitor_test_add("/string-visitor/input/intList",
355 &in_visitor_data, test_visitor_in_intList);
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);
364 input_visitor_test_add("/string-visitor/input/fuzz",
365 &in_visitor_data, test_visitor_in_fuzz);
366
367 g_test_run();
368
369 return 0;
370 }