ppc/pnv: Add support for POWER8+ LPC Controller
[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 v = visitor_input_test_init(data, "");
68
69 visit_type_int(v, NULL, &res, &err);
70 error_free_or_abort(&err);
71 }
72
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
119 static void test_visitor_in_intList(TestInputVisitorData *data,
120 const void *unused)
121 {
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 };
127 Error *err = NULL;
128 int64List *res = NULL;
129 int64List *tail;
130 Visitor *v;
131 int64_t val;
132
133 /* Valid lists */
134
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));
137
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
148 /* Empty list */
149
150 v = visitor_input_test_init(data, "");
151 visit_type_int64List(v, NULL, &res, &error_abort);
152 g_assert(!res);
153
154 /* Not a list */
155
156 v = visitor_input_test_init(data, "not an int list");
157
158 visit_type_int64List(v, NULL, &res, &err);
159 error_free_or_abort(&err);
160 g_assert(!res);
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);
178
179 visit_check_list(v, &err);
180 error_free_or_abort(&err);
181 visit_end_list(v, (void **)&res);
182
183 qapi_free_int64List(res);
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);
199 }
200
201 static void test_visitor_in_bool(TestInputVisitorData *data,
202 const void *unused)
203 {
204 Error *err = NULL;
205 bool res = false;
206 Visitor *v;
207
208 v = visitor_input_test_init(data, "true");
209
210 visit_type_bool(v, NULL, &res, &err);
211 g_assert(!err);
212 g_assert_cmpint(res, ==, true);
213
214 v = visitor_input_test_init(data, "yes");
215
216 visit_type_bool(v, NULL, &res, &err);
217 g_assert(!err);
218 g_assert_cmpint(res, ==, true);
219
220 v = visitor_input_test_init(data, "on");
221
222 visit_type_bool(v, NULL, &res, &err);
223 g_assert(!err);
224 g_assert_cmpint(res, ==, true);
225
226 v = visitor_input_test_init(data, "false");
227
228 visit_type_bool(v, NULL, &res, &err);
229 g_assert(!err);
230 g_assert_cmpint(res, ==, false);
231
232 v = visitor_input_test_init(data, "no");
233
234 visit_type_bool(v, NULL, &res, &err);
235 g_assert(!err);
236 g_assert_cmpint(res, ==, false);
237
238 v = visitor_input_test_init(data, "off");
239
240 visit_type_bool(v, NULL, &res, &err);
241 g_assert(!err);
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;
249 Error *err = NULL;
250 Visitor *v;
251
252 v = visitor_input_test_init(data, "3.14");
253
254 visit_type_number(v, NULL, &res, &err);
255 g_assert(!err);
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";
263 Error *err = NULL;
264 Visitor *v;
265
266 v = visitor_input_test_init(data, value);
267
268 visit_type_str(v, NULL, &res, &err);
269 g_assert(!err);
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 {
278 Error *err = NULL;
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
287 visit_type_EnumOne(v, NULL, &res, &err);
288 g_assert(!err);
289 g_assert_cmpint(i, ==, res);
290 }
291 }
292
293 /* Try to crash the visitors */
294 static void test_visitor_in_fuzz(TestInputVisitorData *data,
295 const void *unused)
296 {
297 int64_t ires;
298 intList *ilres;
299 bool bres;
300 double nres;
301 char *sres;
302 EnumOne eres;
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);
321 visit_type_int(v, NULL, &ires, NULL);
322
323 v = visitor_input_test_init(data, buf);
324 visit_type_intList(v, NULL, &ilres, NULL);
325 qapi_free_intList(ilres);
326
327 v = visitor_input_test_init(data, buf);
328 visit_type_bool(v, NULL, &bres, NULL);
329
330 v = visitor_input_test_init(data, buf);
331 visit_type_number(v, NULL, &nres, NULL);
332
333 v = visitor_input_test_init(data, buf);
334 sres = NULL;
335 visit_type_str(v, NULL, &sres, NULL);
336 g_free(sres);
337
338 v = visitor_input_test_init(data, buf);
339 visit_type_EnumOne(v, NULL, &eres, NULL);
340 }
341 }
342
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);
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);
371
372 g_test_run();
373
374 return 0;
375 }