]> git.proxmox.com Git - mirror_qemu.git/blame - tests/test-string-input-visitor.c
qapi: Make input visitors detect unvisited list tails
[mirror_qemu.git] / tests / test-string-input-visitor.c
CommitLineData
2d7799f2
PB
1/*
2 * String Input Visitor unit-tests.
3 *
4 * Copyright (C) 2012 Red Hat Inc.
5 *
6 * Authors:
b3db211f 7 * Paolo Bonzini <pbonzini@redhat.com> (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
22typedef struct TestInputVisitorData {
7a0525c7 23 Visitor *v;
2d7799f2
PB
24} TestInputVisitorData;
25
26static 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()) */
38static
39Visitor *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
49static 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
68static 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
86static 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
114static 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;
3d089cea
MA
126
127 /* Valid lists */
659268ff
HT
128
129 v = visitor_input_test_init(data, "1,2,0,2-4,20,5-9,1-8");
3d089cea 130 check_ilist(v, expect1, ARRAY_SIZE(expect1));
659268ff 131
3d089cea
MA
132 v = visitor_input_test_init(data, "32767,-32768--32767");
133 check_ilist(v, expect2, ARRAY_SIZE(expect2));
134
135 v = visitor_input_test_init(data,
136 "-9223372036854775808,9223372036854775807");
137 check_ilist(v, expect3, ARRAY_SIZE(expect3));
138
139 v = visitor_input_test_init(data, "18446744073709551615");
140 check_ulist(v, expect4, ARRAY_SIZE(expect4));
141
142 /* Empty list is invalid (weird) */
143
144 v = visitor_input_test_init(data, "");
145 visit_type_int64List(v, NULL, &res, &err);
146 error_free_or_abort(&err);
659268ff 147
3d089cea 148 /* Not a list */
73374683 149
73374683
MA
150 v = visitor_input_test_init(data, "not an int list");
151
3d089cea 152 visit_type_int64List(v, NULL, &res, &err);
74f24cb6
EB
153 error_free_or_abort(&err);
154 g_assert(!res);
9cb8ef36
MA
155
156 /* Unvisited list tail */
157
158 v = visitor_input_test_init(data, "0,2-3");
159
160 /* Would be simpler if the visitor genuinely supported virtual walks */
161 visit_start_list(v, NULL, (GenericList **)&res, sizeof(*res),
162 &error_abort);
163 tail = res;
164 visit_type_int64(v, NULL, &tail->value, &error_abort);
165 g_assert_cmpint(tail->value, ==, 0);
166 tail = (int64List *)visit_next_list(v, (GenericList *)tail, sizeof(*res));
167 g_assert(tail);
168 visit_type_int64(v, NULL, &tail->value, &error_abort);
169 g_assert_cmpint(tail->value, ==, 2);
170 tail = (int64List *)visit_next_list(v, (GenericList *)tail, sizeof(*res));
171 g_assert(tail);
a4a1c70d
MA
172
173 visit_check_list(v, &err);
174 error_free_or_abort(&err);
9cb8ef36 175 visit_end_list(v, (void **)&res);
9cb8ef36
MA
176
177 qapi_free_int64List(res);
659268ff
HT
178}
179
2d7799f2
PB
180static void test_visitor_in_bool(TestInputVisitorData *data,
181 const void *unused)
182{
e940f543 183 Error *err = NULL;
2d7799f2
PB
184 bool res = false;
185 Visitor *v;
186
187 v = visitor_input_test_init(data, "true");
188
51e72bc1 189 visit_type_bool(v, NULL, &res, &err);
e940f543 190 g_assert(!err);
2d7799f2 191 g_assert_cmpint(res, ==, true);
2d7799f2
PB
192
193 v = visitor_input_test_init(data, "yes");
194
51e72bc1 195 visit_type_bool(v, NULL, &res, &err);
e940f543 196 g_assert(!err);
2d7799f2 197 g_assert_cmpint(res, ==, true);
2d7799f2
PB
198
199 v = visitor_input_test_init(data, "on");
200
51e72bc1 201 visit_type_bool(v, NULL, &res, &err);
e940f543 202 g_assert(!err);
2d7799f2 203 g_assert_cmpint(res, ==, true);
2d7799f2
PB
204
205 v = visitor_input_test_init(data, "false");
206
51e72bc1 207 visit_type_bool(v, NULL, &res, &err);
e940f543 208 g_assert(!err);
2d7799f2 209 g_assert_cmpint(res, ==, false);
2d7799f2
PB
210
211 v = visitor_input_test_init(data, "no");
212
51e72bc1 213 visit_type_bool(v, NULL, &res, &err);
e940f543 214 g_assert(!err);
2d7799f2 215 g_assert_cmpint(res, ==, false);
2d7799f2
PB
216
217 v = visitor_input_test_init(data, "off");
218
51e72bc1 219 visit_type_bool(v, NULL, &res, &err);
e940f543 220 g_assert(!err);
2d7799f2
PB
221 g_assert_cmpint(res, ==, false);
222}
223
224static void test_visitor_in_number(TestInputVisitorData *data,
225 const void *unused)
226{
227 double res = 0, value = 3.14;
e940f543 228 Error *err = NULL;
2d7799f2
PB
229 Visitor *v;
230
231 v = visitor_input_test_init(data, "3.14");
232
51e72bc1 233 visit_type_number(v, NULL, &res, &err);
e940f543 234 g_assert(!err);
2d7799f2
PB
235 g_assert_cmpfloat(res, ==, value);
236}
237
238static void test_visitor_in_string(TestInputVisitorData *data,
239 const void *unused)
240{
241 char *res = NULL, *value = (char *) "Q E M U";
e940f543 242 Error *err = NULL;
2d7799f2
PB
243 Visitor *v;
244
245 v = visitor_input_test_init(data, value);
246
51e72bc1 247 visit_type_str(v, NULL, &res, &err);
e940f543 248 g_assert(!err);
2d7799f2
PB
249 g_assert_cmpstr(res, ==, value);
250
251 g_free(res);
252}
253
254static void test_visitor_in_enum(TestInputVisitorData *data,
255 const void *unused)
256{
e940f543 257 Error *err = NULL;
2d7799f2
PB
258 Visitor *v;
259 EnumOne i;
260
261 for (i = 0; EnumOne_lookup[i]; i++) {
262 EnumOne res = -1;
263
264 v = visitor_input_test_init(data, EnumOne_lookup[i]);
265
51e72bc1 266 visit_type_EnumOne(v, NULL, &res, &err);
e940f543 267 g_assert(!err);
2d7799f2 268 g_assert_cmpint(i, ==, res);
2d7799f2 269 }
2d7799f2
PB
270}
271
3f0f31a0
BS
272/* Try to crash the visitors */
273static void test_visitor_in_fuzz(TestInputVisitorData *data,
274 const void *unused)
275{
276 int64_t ires;
659268ff 277 intList *ilres;
3f0f31a0
BS
278 bool bres;
279 double nres;
280 char *sres;
281 EnumOne eres;
3f0f31a0
BS
282 Visitor *v;
283 unsigned int i;
284 char buf[10000];
285
286 for (i = 0; i < 100; i++) {
287 unsigned int j;
288
289 j = g_test_rand_int_range(0, sizeof(buf) - 1);
290
291 buf[j] = '\0';
292
293 if (j != 0) {
294 for (j--; j != 0; j--) {
295 buf[j - 1] = (char)g_test_rand_int_range(0, 256);
296 }
297 }
298
299 v = visitor_input_test_init(data, buf);
51e72bc1 300 visit_type_int(v, NULL, &ires, NULL);
3f0f31a0 301
659268ff 302 v = visitor_input_test_init(data, buf);
51e72bc1 303 visit_type_intList(v, NULL, &ilres, NULL);
bd794065 304 qapi_free_intList(ilres);
659268ff 305
3f0f31a0 306 v = visitor_input_test_init(data, buf);
51e72bc1 307 visit_type_bool(v, NULL, &bres, NULL);
3f0f31a0
BS
308
309 v = visitor_input_test_init(data, buf);
51e72bc1 310 visit_type_number(v, NULL, &nres, NULL);
3f0f31a0
BS
311
312 v = visitor_input_test_init(data, buf);
01845438 313 sres = NULL;
51e72bc1 314 visit_type_str(v, NULL, &sres, NULL);
3f0f31a0
BS
315 g_free(sres);
316
317 v = visitor_input_test_init(data, buf);
51e72bc1 318 visit_type_EnumOne(v, NULL, &eres, NULL);
3f0f31a0
BS
319 }
320}
321
2d7799f2
PB
322static void input_visitor_test_add(const char *testpath,
323 TestInputVisitorData *data,
324 void (*test_func)(TestInputVisitorData *data, const void *user_data))
325{
326 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
327 visitor_input_teardown);
328}
329
330int main(int argc, char **argv)
331{
332 TestInputVisitorData in_visitor_data;
333
334 g_test_init(&argc, &argv, NULL);
335
336 input_visitor_test_add("/string-visitor/input/int",
337 &in_visitor_data, test_visitor_in_int);
659268ff
HT
338 input_visitor_test_add("/string-visitor/input/intList",
339 &in_visitor_data, test_visitor_in_intList);
2d7799f2
PB
340 input_visitor_test_add("/string-visitor/input/bool",
341 &in_visitor_data, test_visitor_in_bool);
342 input_visitor_test_add("/string-visitor/input/number",
343 &in_visitor_data, test_visitor_in_number);
344 input_visitor_test_add("/string-visitor/input/string",
345 &in_visitor_data, test_visitor_in_string);
346 input_visitor_test_add("/string-visitor/input/enum",
347 &in_visitor_data, test_visitor_in_enum);
3f0f31a0
BS
348 input_visitor_test_add("/string-visitor/input/fuzz",
349 &in_visitor_data, test_visitor_in_fuzz);
2d7799f2
PB
350
351 g_test_run();
352
353 return 0;
354}