s390x: wrap flic savevm calls into vmstate
[qemu/ar7.git] / tests / test-string-input-visitor.c
blobd837ebedad0629b94450a7ef356930c52d71bf7a
1 /*
2 * String Input Visitor unit-tests.
4 * Copyright (C) 2012 Red Hat Inc.
6 * Authors:
7 * Paolo Bonzini <pbonzini@redhat.com> (based on test-qmp-input-visitor)
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.
13 #include "qemu/osdep.h"
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"
22 typedef struct TestInputVisitorData {
23 Visitor *v;
24 } TestInputVisitorData;
26 static void visitor_input_teardown(TestInputVisitorData *data,
27 const void *unused)
29 if (data->v) {
30 visit_free(data->v);
31 data->v = NULL;
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)
42 data->v = string_input_visitor_new(string);
43 g_assert(data->v);
44 return data->v;
47 static void test_visitor_in_int(TestInputVisitorData *data,
48 const void *unused)
50 int64_t res = 0, value = -42;
51 Error *err = NULL;
52 Visitor *v;
54 v = visitor_input_test_init(data, "-42");
56 visit_type_int(v, NULL, &res, &err);
57 g_assert(!err);
58 g_assert_cmpint(res, ==, value);
60 visitor_input_teardown(data, unused);
62 v = visitor_input_test_init(data, "not an int");
64 visit_type_int(v, NULL, &res, &err);
65 error_free_or_abort(&err);
68 static void test_visitor_in_intList(TestInputVisitorData *data,
69 const void *unused)
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;
77 v = visitor_input_test_init(data, "1,2,0,2-4,20,5-9,1-8");
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;
86 g_assert(!tmp);
88 qapi_free_int16List(res);
90 visitor_input_teardown(data, unused);
92 v = visitor_input_test_init(data, "not an int list");
94 visit_type_int16List(v, NULL, &res, &err);
95 error_free_or_abort(&err);
96 g_assert(!res);
99 static void test_visitor_in_bool(TestInputVisitorData *data,
100 const void *unused)
102 Error *err = NULL;
103 bool res = false;
104 Visitor *v;
106 v = visitor_input_test_init(data, "true");
108 visit_type_bool(v, NULL, &res, &err);
109 g_assert(!err);
110 g_assert_cmpint(res, ==, true);
111 visitor_input_teardown(data, unused);
113 v = visitor_input_test_init(data, "yes");
115 visit_type_bool(v, NULL, &res, &err);
116 g_assert(!err);
117 g_assert_cmpint(res, ==, true);
118 visitor_input_teardown(data, unused);
120 v = visitor_input_test_init(data, "on");
122 visit_type_bool(v, NULL, &res, &err);
123 g_assert(!err);
124 g_assert_cmpint(res, ==, true);
125 visitor_input_teardown(data, unused);
127 v = visitor_input_test_init(data, "false");
129 visit_type_bool(v, NULL, &res, &err);
130 g_assert(!err);
131 g_assert_cmpint(res, ==, false);
132 visitor_input_teardown(data, unused);
134 v = visitor_input_test_init(data, "no");
136 visit_type_bool(v, NULL, &res, &err);
137 g_assert(!err);
138 g_assert_cmpint(res, ==, false);
139 visitor_input_teardown(data, unused);
141 v = visitor_input_test_init(data, "off");
143 visit_type_bool(v, NULL, &res, &err);
144 g_assert(!err);
145 g_assert_cmpint(res, ==, false);
148 static void test_visitor_in_number(TestInputVisitorData *data,
149 const void *unused)
151 double res = 0, value = 3.14;
152 Error *err = NULL;
153 Visitor *v;
155 v = visitor_input_test_init(data, "3.14");
157 visit_type_number(v, NULL, &res, &err);
158 g_assert(!err);
159 g_assert_cmpfloat(res, ==, value);
162 static void test_visitor_in_string(TestInputVisitorData *data,
163 const void *unused)
165 char *res = NULL, *value = (char *) "Q E M U";
166 Error *err = NULL;
167 Visitor *v;
169 v = visitor_input_test_init(data, value);
171 visit_type_str(v, NULL, &res, &err);
172 g_assert(!err);
173 g_assert_cmpstr(res, ==, value);
175 g_free(res);
178 static void test_visitor_in_enum(TestInputVisitorData *data,
179 const void *unused)
181 Error *err = NULL;
182 Visitor *v;
183 EnumOne i;
185 for (i = 0; EnumOne_lookup[i]; i++) {
186 EnumOne res = -1;
188 v = visitor_input_test_init(data, EnumOne_lookup[i]);
190 visit_type_EnumOne(v, NULL, &res, &err);
191 g_assert(!err);
192 g_assert_cmpint(i, ==, res);
194 visitor_input_teardown(data, NULL);
198 /* Try to crash the visitors */
199 static void test_visitor_in_fuzz(TestInputVisitorData *data,
200 const void *unused)
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];
212 for (i = 0; i < 100; i++) {
213 unsigned int j;
215 j = g_test_rand_int_range(0, sizeof(buf) - 1);
217 buf[j] = '\0';
219 if (j != 0) {
220 for (j--; j != 0; j--) {
221 buf[j - 1] = (char)g_test_rand_int_range(0, 256);
225 v = visitor_input_test_init(data, buf);
226 visit_type_int(v, NULL, &ires, NULL);
227 visitor_input_teardown(data, NULL);
229 v = visitor_input_test_init(data, buf);
230 visit_type_intList(v, NULL, &ilres, NULL);
231 visitor_input_teardown(data, NULL);
233 v = visitor_input_test_init(data, buf);
234 visit_type_bool(v, NULL, &bres, NULL);
235 visitor_input_teardown(data, NULL);
237 v = visitor_input_test_init(data, buf);
238 visit_type_number(v, NULL, &nres, NULL);
239 visitor_input_teardown(data, NULL);
241 v = visitor_input_test_init(data, buf);
242 sres = NULL;
243 visit_type_str(v, NULL, &sres, NULL);
244 g_free(sres);
245 visitor_input_teardown(data, NULL);
247 v = visitor_input_test_init(data, buf);
248 visit_type_EnumOne(v, NULL, &eres, NULL);
249 visitor_input_teardown(data, NULL);
253 static void input_visitor_test_add(const char *testpath,
254 TestInputVisitorData *data,
255 void (*test_func)(TestInputVisitorData *data, const void *user_data))
257 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
258 visitor_input_teardown);
261 int main(int argc, char **argv)
263 TestInputVisitorData in_visitor_data;
265 g_test_init(&argc, &argv, NULL);
267 input_visitor_test_add("/string-visitor/input/int",
268 &in_visitor_data, test_visitor_in_int);
269 input_visitor_test_add("/string-visitor/input/intList",
270 &in_visitor_data, test_visitor_in_intList);
271 input_visitor_test_add("/string-visitor/input/bool",
272 &in_visitor_data, test_visitor_in_bool);
273 input_visitor_test_add("/string-visitor/input/number",
274 &in_visitor_data, test_visitor_in_number);
275 input_visitor_test_add("/string-visitor/input/string",
276 &in_visitor_data, test_visitor_in_string);
277 input_visitor_test_add("/string-visitor/input/enum",
278 &in_visitor_data, test_visitor_in_enum);
279 input_visitor_test_add("/string-visitor/input/fuzz",
280 &in_visitor_data, test_visitor_in_fuzz);
282 g_test_run();
284 return 0;