]> Git Repo - qemu.git/blob - tests/test-string-input-visitor.c
tests: Cover partial input visit of list
[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 <[email protected]> (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
127     /* Valid lists */
128
129     v = visitor_input_test_init(data, "1,2,0,2-4,20,5-9,1-8");
130     check_ilist(v, expect1, ARRAY_SIZE(expect1));
131
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);
147
148     /* Not a list */
149
150     v = visitor_input_test_init(data, "not an int list");
151
152     visit_type_int64List(v, NULL, &res, &err);
153     error_free_or_abort(&err);
154     g_assert(!res);
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);
176 }
177
178 static void test_visitor_in_bool(TestInputVisitorData *data,
179                                  const void *unused)
180 {
181     Error *err = NULL;
182     bool res = false;
183     Visitor *v;
184
185     v = visitor_input_test_init(data, "true");
186
187     visit_type_bool(v, NULL, &res, &err);
188     g_assert(!err);
189     g_assert_cmpint(res, ==, true);
190
191     v = visitor_input_test_init(data, "yes");
192
193     visit_type_bool(v, NULL, &res, &err);
194     g_assert(!err);
195     g_assert_cmpint(res, ==, true);
196
197     v = visitor_input_test_init(data, "on");
198
199     visit_type_bool(v, NULL, &res, &err);
200     g_assert(!err);
201     g_assert_cmpint(res, ==, true);
202
203     v = visitor_input_test_init(data, "false");
204
205     visit_type_bool(v, NULL, &res, &err);
206     g_assert(!err);
207     g_assert_cmpint(res, ==, false);
208
209     v = visitor_input_test_init(data, "no");
210
211     visit_type_bool(v, NULL, &res, &err);
212     g_assert(!err);
213     g_assert_cmpint(res, ==, false);
214
215     v = visitor_input_test_init(data, "off");
216
217     visit_type_bool(v, NULL, &res, &err);
218     g_assert(!err);
219     g_assert_cmpint(res, ==, false);
220 }
221
222 static void test_visitor_in_number(TestInputVisitorData *data,
223                                    const void *unused)
224 {
225     double res = 0, value = 3.14;
226     Error *err = NULL;
227     Visitor *v;
228
229     v = visitor_input_test_init(data, "3.14");
230
231     visit_type_number(v, NULL, &res, &err);
232     g_assert(!err);
233     g_assert_cmpfloat(res, ==, value);
234 }
235
236 static void test_visitor_in_string(TestInputVisitorData *data,
237                                    const void *unused)
238 {
239     char *res = NULL, *value = (char *) "Q E M U";
240     Error *err = NULL;
241     Visitor *v;
242
243     v = visitor_input_test_init(data, value);
244
245     visit_type_str(v, NULL, &res, &err);
246     g_assert(!err);
247     g_assert_cmpstr(res, ==, value);
248
249     g_free(res);
250 }
251
252 static void test_visitor_in_enum(TestInputVisitorData *data,
253                                  const void *unused)
254 {
255     Error *err = NULL;
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
264         visit_type_EnumOne(v, NULL, &res, &err);
265         g_assert(!err);
266         g_assert_cmpint(i, ==, res);
267     }
268 }
269
270 /* Try to crash the visitors */
271 static void test_visitor_in_fuzz(TestInputVisitorData *data,
272                                  const void *unused)
273 {
274     int64_t ires;
275     intList *ilres;
276     bool bres;
277     double nres;
278     char *sres;
279     EnumOne eres;
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);
298         visit_type_int(v, NULL, &ires, NULL);
299
300         v = visitor_input_test_init(data, buf);
301         visit_type_intList(v, NULL, &ilres, NULL);
302         qapi_free_intList(ilres);
303
304         v = visitor_input_test_init(data, buf);
305         visit_type_bool(v, NULL, &bres, NULL);
306
307         v = visitor_input_test_init(data, buf);
308         visit_type_number(v, NULL, &nres, NULL);
309
310         v = visitor_input_test_init(data, buf);
311         sres = NULL;
312         visit_type_str(v, NULL, &sres, NULL);
313         g_free(sres);
314
315         v = visitor_input_test_init(data, buf);
316         visit_type_EnumOne(v, NULL, &eres, NULL);
317     }
318 }
319
320 static 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
328 int 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);
336     input_visitor_test_add("/string-visitor/input/intList",
337                            &in_visitor_data, test_visitor_in_intList);
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);
346     input_visitor_test_add("/string-visitor/input/fuzz",
347                             &in_visitor_data, test_visitor_in_fuzz);
348
349     g_test_run();
350
351     return 0;
352 }
This page took 0.044949 seconds and 4 git commands to generate.