qapi: make string input visitor parse int 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 <pbonzini@redhat.com> (based on test-qmp-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 <glib.h>
14 #include <stdarg.h>
15
16 #include "qemu-common.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 StringInputVisitor *siv;
24 } TestInputVisitorData;
25
26 static void visitor_input_teardown(TestInputVisitorData *data,
27 const void *unused)
28 {
29 if (data->siv) {
30 string_input_visitor_cleanup(data->siv);
31 data->siv = 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 *v;
43
44 data->siv = string_input_visitor_new(string);
45 g_assert(data->siv != NULL);
46
47 v = string_input_get_visitor(data->siv);
48 g_assert(v != NULL);
49
50 return v;
51 }
52
53 static void test_visitor_in_int(TestInputVisitorData *data,
54 const void *unused)
55 {
56 int64_t res = 0, value = -42;
57 Error *err = NULL;
58 Visitor *v;
59
60 v = visitor_input_test_init(data, "-42");
61
62 visit_type_int(v, &res, NULL, &err);
63 g_assert(!err);
64 g_assert_cmpint(res, ==, value);
65 }
66
67 static void test_visitor_in_intList(TestInputVisitorData *data,
68 const void *unused)
69 {
70 int64_t value[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20};
71 int16List *res = NULL, *tmp;
72 Error *errp = NULL;
73 Visitor *v;
74 int i = 0;
75
76 v = visitor_input_test_init(data, "1,2,0,2-4,20,5-9,1-8");
77
78 visit_type_int16List(v, &res, NULL, &errp);
79 g_assert(errp == NULL);
80 tmp = res;
81 while (i < sizeof(value) / sizeof(value[0])) {
82 g_assert(tmp);
83 g_assert_cmpint(tmp->value, ==, value[i++]);
84 tmp = tmp->next;
85 }
86 g_assert(!tmp);
87
88 tmp = res;
89 while (tmp) {
90 res = res->next;
91 g_free(tmp);
92 tmp = res;
93 }
94 }
95
96 static void test_visitor_in_bool(TestInputVisitorData *data,
97 const void *unused)
98 {
99 Error *err = NULL;
100 bool res = false;
101 Visitor *v;
102
103 v = visitor_input_test_init(data, "true");
104
105 visit_type_bool(v, &res, NULL, &err);
106 g_assert(!err);
107 g_assert_cmpint(res, ==, true);
108 visitor_input_teardown(data, unused);
109
110 v = visitor_input_test_init(data, "yes");
111
112 visit_type_bool(v, &res, NULL, &err);
113 g_assert(!err);
114 g_assert_cmpint(res, ==, true);
115 visitor_input_teardown(data, unused);
116
117 v = visitor_input_test_init(data, "on");
118
119 visit_type_bool(v, &res, NULL, &err);
120 g_assert(!err);
121 g_assert_cmpint(res, ==, true);
122 visitor_input_teardown(data, unused);
123
124 v = visitor_input_test_init(data, "false");
125
126 visit_type_bool(v, &res, NULL, &err);
127 g_assert(!err);
128 g_assert_cmpint(res, ==, false);
129 visitor_input_teardown(data, unused);
130
131 v = visitor_input_test_init(data, "no");
132
133 visit_type_bool(v, &res, NULL, &err);
134 g_assert(!err);
135 g_assert_cmpint(res, ==, false);
136 visitor_input_teardown(data, unused);
137
138 v = visitor_input_test_init(data, "off");
139
140 visit_type_bool(v, &res, NULL, &err);
141 g_assert(!err);
142 g_assert_cmpint(res, ==, false);
143 }
144
145 static void test_visitor_in_number(TestInputVisitorData *data,
146 const void *unused)
147 {
148 double res = 0, value = 3.14;
149 Error *err = NULL;
150 Visitor *v;
151
152 v = visitor_input_test_init(data, "3.14");
153
154 visit_type_number(v, &res, NULL, &err);
155 g_assert(!err);
156 g_assert_cmpfloat(res, ==, value);
157 }
158
159 static void test_visitor_in_string(TestInputVisitorData *data,
160 const void *unused)
161 {
162 char *res = NULL, *value = (char *) "Q E M U";
163 Error *err = NULL;
164 Visitor *v;
165
166 v = visitor_input_test_init(data, value);
167
168 visit_type_str(v, &res, NULL, &err);
169 g_assert(!err);
170 g_assert_cmpstr(res, ==, value);
171
172 g_free(res);
173 }
174
175 static void test_visitor_in_enum(TestInputVisitorData *data,
176 const void *unused)
177 {
178 Error *err = NULL;
179 Visitor *v;
180 EnumOne i;
181
182 for (i = 0; EnumOne_lookup[i]; i++) {
183 EnumOne res = -1;
184
185 v = visitor_input_test_init(data, EnumOne_lookup[i]);
186
187 visit_type_EnumOne(v, &res, NULL, &err);
188 g_assert(!err);
189 g_assert_cmpint(i, ==, res);
190
191 visitor_input_teardown(data, NULL);
192 }
193
194 data->siv = NULL;
195 }
196
197 /* Try to crash the visitors */
198 static void test_visitor_in_fuzz(TestInputVisitorData *data,
199 const void *unused)
200 {
201 int64_t ires;
202 intList *ilres;
203 bool bres;
204 double nres;
205 char *sres;
206 EnumOne eres;
207 Visitor *v;
208 unsigned int i;
209 char buf[10000];
210
211 for (i = 0; i < 100; i++) {
212 unsigned int j;
213
214 j = g_test_rand_int_range(0, sizeof(buf) - 1);
215
216 buf[j] = '\0';
217
218 if (j != 0) {
219 for (j--; j != 0; j--) {
220 buf[j - 1] = (char)g_test_rand_int_range(0, 256);
221 }
222 }
223
224 v = visitor_input_test_init(data, buf);
225 visit_type_int(v, &ires, NULL, NULL);
226 visitor_input_teardown(data, NULL);
227
228 v = visitor_input_test_init(data, buf);
229 visit_type_intList(v, &ilres, NULL, NULL);
230 visitor_input_teardown(data, NULL);
231
232 v = visitor_input_test_init(data, buf);
233 visit_type_bool(v, &bres, NULL, NULL);
234 visitor_input_teardown(data, NULL);
235
236 v = visitor_input_test_init(data, buf);
237 visit_type_number(v, &nres, NULL, NULL);
238 visitor_input_teardown(data, NULL);
239
240 v = visitor_input_test_init(data, buf);
241 sres = NULL;
242 visit_type_str(v, &sres, NULL, NULL);
243 g_free(sres);
244 visitor_input_teardown(data, NULL);
245
246 v = visitor_input_test_init(data, buf);
247 visit_type_EnumOne(v, &eres, NULL, NULL);
248 visitor_input_teardown(data, NULL);
249 }
250 }
251
252 static void input_visitor_test_add(const char *testpath,
253 TestInputVisitorData *data,
254 void (*test_func)(TestInputVisitorData *data, const void *user_data))
255 {
256 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
257 visitor_input_teardown);
258 }
259
260 int main(int argc, char **argv)
261 {
262 TestInputVisitorData in_visitor_data;
263
264 g_test_init(&argc, &argv, NULL);
265
266 input_visitor_test_add("/string-visitor/input/int",
267 &in_visitor_data, test_visitor_in_int);
268 input_visitor_test_add("/string-visitor/input/intList",
269 &in_visitor_data, test_visitor_in_intList);
270 input_visitor_test_add("/string-visitor/input/bool",
271 &in_visitor_data, test_visitor_in_bool);
272 input_visitor_test_add("/string-visitor/input/number",
273 &in_visitor_data, test_visitor_in_number);
274 input_visitor_test_add("/string-visitor/input/string",
275 &in_visitor_data, test_visitor_in_string);
276 input_visitor_test_add("/string-visitor/input/enum",
277 &in_visitor_data, test_visitor_in_enum);
278 input_visitor_test_add("/string-visitor/input/fuzz",
279 &in_visitor_data, test_visitor_in_fuzz);
280
281 g_test_run();
282
283 return 0;
284 }