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