]> git.proxmox.com Git - mirror_qemu.git/blame - tests/test-string-input-visitor.c
test-qobject-input-visitor: Cover missing nested struct member
[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);
172 visit_end_list(v, (void **)&res);
173 /* BUG: unvisited tail not reported; actually not reportable by design */
174
175 qapi_free_int64List(res);
659268ff
HT
176}
177
2d7799f2
PB
178static void test_visitor_in_bool(TestInputVisitorData *data,
179 const void *unused)
180{
e940f543 181 Error *err = NULL;
2d7799f2
PB
182 bool res = false;
183 Visitor *v;
184
185 v = visitor_input_test_init(data, "true");
186
51e72bc1 187 visit_type_bool(v, NULL, &res, &err);
e940f543 188 g_assert(!err);
2d7799f2 189 g_assert_cmpint(res, ==, true);
2d7799f2
PB
190
191 v = visitor_input_test_init(data, "yes");
192
51e72bc1 193 visit_type_bool(v, NULL, &res, &err);
e940f543 194 g_assert(!err);
2d7799f2 195 g_assert_cmpint(res, ==, true);
2d7799f2
PB
196
197 v = visitor_input_test_init(data, "on");
198
51e72bc1 199 visit_type_bool(v, NULL, &res, &err);
e940f543 200 g_assert(!err);
2d7799f2 201 g_assert_cmpint(res, ==, true);
2d7799f2
PB
202
203 v = visitor_input_test_init(data, "false");
204
51e72bc1 205 visit_type_bool(v, NULL, &res, &err);
e940f543 206 g_assert(!err);
2d7799f2 207 g_assert_cmpint(res, ==, false);
2d7799f2
PB
208
209 v = visitor_input_test_init(data, "no");
210
51e72bc1 211 visit_type_bool(v, NULL, &res, &err);
e940f543 212 g_assert(!err);
2d7799f2 213 g_assert_cmpint(res, ==, false);
2d7799f2
PB
214
215 v = visitor_input_test_init(data, "off");
216
51e72bc1 217 visit_type_bool(v, NULL, &res, &err);
e940f543 218 g_assert(!err);
2d7799f2
PB
219 g_assert_cmpint(res, ==, false);
220}
221
222static void test_visitor_in_number(TestInputVisitorData *data,
223 const void *unused)
224{
225 double res = 0, value = 3.14;
e940f543 226 Error *err = NULL;
2d7799f2
PB
227 Visitor *v;
228
229 v = visitor_input_test_init(data, "3.14");
230
51e72bc1 231 visit_type_number(v, NULL, &res, &err);
e940f543 232 g_assert(!err);
2d7799f2
PB
233 g_assert_cmpfloat(res, ==, value);
234}
235
236static void test_visitor_in_string(TestInputVisitorData *data,
237 const void *unused)
238{
239 char *res = NULL, *value = (char *) "Q E M U";
e940f543 240 Error *err = NULL;
2d7799f2
PB
241 Visitor *v;
242
243 v = visitor_input_test_init(data, value);
244
51e72bc1 245 visit_type_str(v, NULL, &res, &err);
e940f543 246 g_assert(!err);
2d7799f2
PB
247 g_assert_cmpstr(res, ==, value);
248
249 g_free(res);
250}
251
252static void test_visitor_in_enum(TestInputVisitorData *data,
253 const void *unused)
254{
e940f543 255 Error *err = NULL;
2d7799f2
PB
256 Visitor *v;
257 EnumOne i;
258
259 for (i = 0; EnumOne_lookup[i]; i++) {
260 EnumOne res = -1;
261
262 v = visitor_input_test_init(data, EnumOne_lookup[i]);
263
51e72bc1 264 visit_type_EnumOne(v, NULL, &res, &err);
e940f543 265 g_assert(!err);
2d7799f2 266 g_assert_cmpint(i, ==, res);
2d7799f2 267 }
2d7799f2
PB
268}
269
3f0f31a0
BS
270/* Try to crash the visitors */
271static void test_visitor_in_fuzz(TestInputVisitorData *data,
272 const void *unused)
273{
274 int64_t ires;
659268ff 275 intList *ilres;
3f0f31a0
BS
276 bool bres;
277 double nres;
278 char *sres;
279 EnumOne eres;
3f0f31a0
BS
280 Visitor *v;
281 unsigned int i;
282 char buf[10000];
283
284 for (i = 0; i < 100; i++) {
285 unsigned int j;
286
287 j = g_test_rand_int_range(0, sizeof(buf) - 1);
288
289 buf[j] = '\0';
290
291 if (j != 0) {
292 for (j--; j != 0; j--) {
293 buf[j - 1] = (char)g_test_rand_int_range(0, 256);
294 }
295 }
296
297 v = visitor_input_test_init(data, buf);
51e72bc1 298 visit_type_int(v, NULL, &ires, NULL);
3f0f31a0 299
659268ff 300 v = visitor_input_test_init(data, buf);
51e72bc1 301 visit_type_intList(v, NULL, &ilres, NULL);
bd794065 302 qapi_free_intList(ilres);
659268ff 303
3f0f31a0 304 v = visitor_input_test_init(data, buf);
51e72bc1 305 visit_type_bool(v, NULL, &bres, NULL);
3f0f31a0
BS
306
307 v = visitor_input_test_init(data, buf);
51e72bc1 308 visit_type_number(v, NULL, &nres, NULL);
3f0f31a0
BS
309
310 v = visitor_input_test_init(data, buf);
01845438 311 sres = NULL;
51e72bc1 312 visit_type_str(v, NULL, &sres, NULL);
3f0f31a0
BS
313 g_free(sres);
314
315 v = visitor_input_test_init(data, buf);
51e72bc1 316 visit_type_EnumOne(v, NULL, &eres, NULL);
3f0f31a0
BS
317 }
318}
319
2d7799f2
PB
320static void input_visitor_test_add(const char *testpath,
321 TestInputVisitorData *data,
322 void (*test_func)(TestInputVisitorData *data, const void *user_data))
323{
324 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
325 visitor_input_teardown);
326}
327
328int main(int argc, char **argv)
329{
330 TestInputVisitorData in_visitor_data;
331
332 g_test_init(&argc, &argv, NULL);
333
334 input_visitor_test_add("/string-visitor/input/int",
335 &in_visitor_data, test_visitor_in_int);
659268ff
HT
336 input_visitor_test_add("/string-visitor/input/intList",
337 &in_visitor_data, test_visitor_in_intList);
2d7799f2
PB
338 input_visitor_test_add("/string-visitor/input/bool",
339 &in_visitor_data, test_visitor_in_bool);
340 input_visitor_test_add("/string-visitor/input/number",
341 &in_visitor_data, test_visitor_in_number);
342 input_visitor_test_add("/string-visitor/input/string",
343 &in_visitor_data, test_visitor_in_string);
344 input_visitor_test_add("/string-visitor/input/enum",
345 &in_visitor_data, test_visitor_in_enum);
3f0f31a0
BS
346 input_visitor_test_add("/string-visitor/input/fuzz",
347 &in_visitor_data, test_visitor_in_fuzz);
2d7799f2
PB
348
349 g_test_run();
350
351 return 0;
352}