blob: 5a5692022264355ed1e7758d251f82b2cbcba190 [file] [log] [blame]
Paolo Bonzini2d7799f2012-02-09 11:21:03 +01001/*
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
Peter Maydell681c28a2016-02-08 18:08:51 +000013#include "qemu/osdep.h"
Paolo Bonzini2d7799f2012-02-09 11:21:03 +010014#include <glib.h>
Paolo Bonzini2d7799f2012-02-09 11:21:03 +010015
Paolo Bonzini79ee7df2012-12-06 11:22:34 +010016#include "qemu-common.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010017#include "qapi/error.h"
Paolo Bonzini2d7799f2012-02-09 11:21:03 +010018#include "qapi/string-input-visitor.h"
19#include "test-qapi-types.h"
20#include "test-qapi-visit.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010021#include "qapi/qmp/types.h"
Paolo Bonzini2d7799f2012-02-09 11:21:03 +010022
23typedef struct TestInputVisitorData {
24 StringInputVisitor *siv;
25} TestInputVisitorData;
26
27static void visitor_input_teardown(TestInputVisitorData *data,
28 const void *unused)
29{
30 if (data->siv) {
31 string_input_visitor_cleanup(data->siv);
32 data->siv = NULL;
33 }
34}
35
36/* This is provided instead of a test setup function so that the JSON
37 string used by the tests are kept in the test functions (and not
38 int main()) */
39static
40Visitor *visitor_input_test_init(TestInputVisitorData *data,
41 const char *string)
42{
43 Visitor *v;
44
45 data->siv = string_input_visitor_new(string);
46 g_assert(data->siv != NULL);
47
48 v = string_input_get_visitor(data->siv);
49 g_assert(v != NULL);
50
51 return v;
52}
53
54static void test_visitor_in_int(TestInputVisitorData *data,
55 const void *unused)
56{
57 int64_t res = 0, value = -42;
Markus Armbrustere940f542014-05-02 13:26:29 +020058 Error *err = NULL;
Paolo Bonzini2d7799f2012-02-09 11:21:03 +010059 Visitor *v;
60
61 v = visitor_input_test_init(data, "-42");
62
Eric Blake51e72bc2016-01-29 06:48:54 -070063 visit_type_int(v, NULL, &res, &err);
Markus Armbrustere940f542014-05-02 13:26:29 +020064 g_assert(!err);
Paolo Bonzini2d7799f2012-02-09 11:21:03 +010065 g_assert_cmpint(res, ==, value);
Markus Armbruster73374682016-04-28 15:45:29 -060066
67 visitor_input_teardown(data, unused);
68
69 v = visitor_input_test_init(data, "not an int");
70
71 visit_type_int(v, NULL, &res, &err);
72 error_free_or_abort(&err);
Paolo Bonzini2d7799f2012-02-09 11:21:03 +010073}
74
Hu Tao659268f2014-06-10 19:15:27 +080075static void test_visitor_in_intList(TestInputVisitorData *data,
76 const void *unused)
77{
78 int64_t value[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20};
79 int16List *res = NULL, *tmp;
Markus Armbruster73374682016-04-28 15:45:29 -060080 Error *err = NULL;
Hu Tao659268f2014-06-10 19:15:27 +080081 Visitor *v;
82 int i = 0;
83
84 v = visitor_input_test_init(data, "1,2,0,2-4,20,5-9,1-8");
85
Eric Blake51e72bc2016-01-29 06:48:54 -070086 visit_type_int16List(v, NULL, &res, &error_abort);
Hu Tao659268f2014-06-10 19:15:27 +080087 tmp = res;
88 while (i < sizeof(value) / sizeof(value[0])) {
89 g_assert(tmp);
90 g_assert_cmpint(tmp->value, ==, value[i++]);
91 tmp = tmp->next;
92 }
93 g_assert(!tmp);
94
Eric Blake74f24cb2016-04-28 15:45:30 -060095 qapi_free_int16List(res);
Markus Armbruster73374682016-04-28 15:45:29 -060096
97 visitor_input_teardown(data, unused);
98
99 v = visitor_input_test_init(data, "not an int list");
100
101 visit_type_int16List(v, NULL, &res, &err);
Eric Blake74f24cb2016-04-28 15:45:30 -0600102 error_free_or_abort(&err);
103 g_assert(!res);
Hu Tao659268f2014-06-10 19:15:27 +0800104}
105
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100106static void test_visitor_in_bool(TestInputVisitorData *data,
107 const void *unused)
108{
Markus Armbrustere940f542014-05-02 13:26:29 +0200109 Error *err = NULL;
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100110 bool res = false;
111 Visitor *v;
112
113 v = visitor_input_test_init(data, "true");
114
Eric Blake51e72bc2016-01-29 06:48:54 -0700115 visit_type_bool(v, NULL, &res, &err);
Markus Armbrustere940f542014-05-02 13:26:29 +0200116 g_assert(!err);
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100117 g_assert_cmpint(res, ==, true);
118 visitor_input_teardown(data, unused);
119
120 v = visitor_input_test_init(data, "yes");
121
Eric Blake51e72bc2016-01-29 06:48:54 -0700122 visit_type_bool(v, NULL, &res, &err);
Markus Armbrustere940f542014-05-02 13:26:29 +0200123 g_assert(!err);
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100124 g_assert_cmpint(res, ==, true);
125 visitor_input_teardown(data, unused);
126
127 v = visitor_input_test_init(data, "on");
128
Eric Blake51e72bc2016-01-29 06:48:54 -0700129 visit_type_bool(v, NULL, &res, &err);
Markus Armbrustere940f542014-05-02 13:26:29 +0200130 g_assert(!err);
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100131 g_assert_cmpint(res, ==, true);
132 visitor_input_teardown(data, unused);
133
134 v = visitor_input_test_init(data, "false");
135
Eric Blake51e72bc2016-01-29 06:48:54 -0700136 visit_type_bool(v, NULL, &res, &err);
Markus Armbrustere940f542014-05-02 13:26:29 +0200137 g_assert(!err);
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100138 g_assert_cmpint(res, ==, false);
139 visitor_input_teardown(data, unused);
140
141 v = visitor_input_test_init(data, "no");
142
Eric Blake51e72bc2016-01-29 06:48:54 -0700143 visit_type_bool(v, NULL, &res, &err);
Markus Armbrustere940f542014-05-02 13:26:29 +0200144 g_assert(!err);
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100145 g_assert_cmpint(res, ==, false);
146 visitor_input_teardown(data, unused);
147
148 v = visitor_input_test_init(data, "off");
149
Eric Blake51e72bc2016-01-29 06:48:54 -0700150 visit_type_bool(v, NULL, &res, &err);
Markus Armbrustere940f542014-05-02 13:26:29 +0200151 g_assert(!err);
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100152 g_assert_cmpint(res, ==, false);
153}
154
155static void test_visitor_in_number(TestInputVisitorData *data,
156 const void *unused)
157{
158 double res = 0, value = 3.14;
Markus Armbrustere940f542014-05-02 13:26:29 +0200159 Error *err = NULL;
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100160 Visitor *v;
161
162 v = visitor_input_test_init(data, "3.14");
163
Eric Blake51e72bc2016-01-29 06:48:54 -0700164 visit_type_number(v, NULL, &res, &err);
Markus Armbrustere940f542014-05-02 13:26:29 +0200165 g_assert(!err);
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100166 g_assert_cmpfloat(res, ==, value);
167}
168
169static void test_visitor_in_string(TestInputVisitorData *data,
170 const void *unused)
171{
172 char *res = NULL, *value = (char *) "Q E M U";
Markus Armbrustere940f542014-05-02 13:26:29 +0200173 Error *err = NULL;
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100174 Visitor *v;
175
176 v = visitor_input_test_init(data, value);
177
Eric Blake51e72bc2016-01-29 06:48:54 -0700178 visit_type_str(v, NULL, &res, &err);
Markus Armbrustere940f542014-05-02 13:26:29 +0200179 g_assert(!err);
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100180 g_assert_cmpstr(res, ==, value);
181
182 g_free(res);
183}
184
185static void test_visitor_in_enum(TestInputVisitorData *data,
186 const void *unused)
187{
Markus Armbrustere940f542014-05-02 13:26:29 +0200188 Error *err = NULL;
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100189 Visitor *v;
190 EnumOne i;
191
192 for (i = 0; EnumOne_lookup[i]; i++) {
193 EnumOne res = -1;
194
195 v = visitor_input_test_init(data, EnumOne_lookup[i]);
196
Eric Blake51e72bc2016-01-29 06:48:54 -0700197 visit_type_EnumOne(v, NULL, &res, &err);
Markus Armbrustere940f542014-05-02 13:26:29 +0200198 g_assert(!err);
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100199 g_assert_cmpint(i, ==, res);
200
201 visitor_input_teardown(data, NULL);
202 }
203
204 data->siv = NULL;
205}
206
Blue Swirl3f0f31a2013-01-19 15:43:53 +0000207/* Try to crash the visitors */
208static void test_visitor_in_fuzz(TestInputVisitorData *data,
209 const void *unused)
210{
211 int64_t ires;
Hu Tao659268f2014-06-10 19:15:27 +0800212 intList *ilres;
Blue Swirl3f0f31a2013-01-19 15:43:53 +0000213 bool bres;
214 double nres;
215 char *sres;
216 EnumOne eres;
Blue Swirl3f0f31a2013-01-19 15:43:53 +0000217 Visitor *v;
218 unsigned int i;
219 char buf[10000];
220
221 for (i = 0; i < 100; i++) {
222 unsigned int j;
223
224 j = g_test_rand_int_range(0, sizeof(buf) - 1);
225
226 buf[j] = '\0';
227
228 if (j != 0) {
229 for (j--; j != 0; j--) {
230 buf[j - 1] = (char)g_test_rand_int_range(0, 256);
231 }
232 }
233
234 v = visitor_input_test_init(data, buf);
Eric Blake51e72bc2016-01-29 06:48:54 -0700235 visit_type_int(v, NULL, &ires, NULL);
Hu Taocac124d2014-06-10 19:15:26 +0800236 visitor_input_teardown(data, NULL);
Blue Swirl3f0f31a2013-01-19 15:43:53 +0000237
238 v = visitor_input_test_init(data, buf);
Eric Blake51e72bc2016-01-29 06:48:54 -0700239 visit_type_intList(v, NULL, &ilres, NULL);
Hu Tao659268f2014-06-10 19:15:27 +0800240 visitor_input_teardown(data, NULL);
241
242 v = visitor_input_test_init(data, buf);
Eric Blake51e72bc2016-01-29 06:48:54 -0700243 visit_type_bool(v, NULL, &bres, NULL);
Blue Swirl3f0f31a2013-01-19 15:43:53 +0000244 visitor_input_teardown(data, NULL);
245
246 v = visitor_input_test_init(data, buf);
Eric Blake51e72bc2016-01-29 06:48:54 -0700247 visit_type_number(v, NULL, &nres, NULL);
Hu Taocac124d2014-06-10 19:15:26 +0800248 visitor_input_teardown(data, NULL);
Blue Swirl3f0f31a2013-01-19 15:43:53 +0000249
250 v = visitor_input_test_init(data, buf);
Peter Maydell01845432013-02-05 20:44:23 +0000251 sres = NULL;
Eric Blake51e72bc2016-01-29 06:48:54 -0700252 visit_type_str(v, NULL, &sres, NULL);
Blue Swirl3f0f31a2013-01-19 15:43:53 +0000253 g_free(sres);
Hu Taocac124d2014-06-10 19:15:26 +0800254 visitor_input_teardown(data, NULL);
Blue Swirl3f0f31a2013-01-19 15:43:53 +0000255
256 v = visitor_input_test_init(data, buf);
Eric Blake51e72bc2016-01-29 06:48:54 -0700257 visit_type_EnumOne(v, NULL, &eres, NULL);
Blue Swirl3f0f31a2013-01-19 15:43:53 +0000258 visitor_input_teardown(data, NULL);
259 }
260}
261
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100262static void input_visitor_test_add(const char *testpath,
263 TestInputVisitorData *data,
264 void (*test_func)(TestInputVisitorData *data, const void *user_data))
265{
266 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
267 visitor_input_teardown);
268}
269
270int main(int argc, char **argv)
271{
272 TestInputVisitorData in_visitor_data;
273
274 g_test_init(&argc, &argv, NULL);
275
276 input_visitor_test_add("/string-visitor/input/int",
277 &in_visitor_data, test_visitor_in_int);
Hu Tao659268f2014-06-10 19:15:27 +0800278 input_visitor_test_add("/string-visitor/input/intList",
279 &in_visitor_data, test_visitor_in_intList);
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100280 input_visitor_test_add("/string-visitor/input/bool",
281 &in_visitor_data, test_visitor_in_bool);
282 input_visitor_test_add("/string-visitor/input/number",
283 &in_visitor_data, test_visitor_in_number);
284 input_visitor_test_add("/string-visitor/input/string",
285 &in_visitor_data, test_visitor_in_string);
286 input_visitor_test_add("/string-visitor/input/enum",
287 &in_visitor_data, test_visitor_in_enum);
Blue Swirl3f0f31a2013-01-19 15:43:53 +0000288 input_visitor_test_add("/string-visitor/input/fuzz",
289 &in_visitor_data, test_visitor_in_fuzz);
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100290
291 g_test_run();
292
293 return 0;
294}