blob: d837ebedad0629b94450a7ef356930c52d71bf7a [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
Paolo Bonzini79ee7df2012-12-06 11:22:34 +010015#include "qemu-common.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010016#include "qapi/error.h"
Paolo Bonzini2d7799f2012-02-09 11:21:03 +010017#include "qapi/string-input-visitor.h"
18#include "test-qapi-types.h"
19#include "test-qapi-visit.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010020#include "qapi/qmp/types.h"
Paolo Bonzini2d7799f2012-02-09 11:21:03 +010021
22typedef struct TestInputVisitorData {
Eric Blake7a0525c2016-06-09 10:48:37 -060023 Visitor *v;
Paolo Bonzini2d7799f2012-02-09 11:21:03 +010024} TestInputVisitorData;
25
26static void visitor_input_teardown(TestInputVisitorData *data,
27 const void *unused)
28{
Eric Blake7a0525c2016-06-09 10:48:37 -060029 if (data->v) {
30 visit_free(data->v);
31 data->v = NULL;
Paolo Bonzini2d7799f2012-02-09 11:21:03 +010032 }
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()) */
38static
39Visitor *visitor_input_test_init(TestInputVisitorData *data,
40 const char *string)
41{
Eric Blake7a0525c2016-06-09 10:48:37 -060042 data->v = string_input_visitor_new(string);
43 g_assert(data->v);
44 return data->v;
Paolo Bonzini2d7799f2012-02-09 11:21:03 +010045}
46
47static void test_visitor_in_int(TestInputVisitorData *data,
48 const void *unused)
49{
50 int64_t res = 0, value = -42;
Markus Armbrustere940f542014-05-02 13:26:29 +020051 Error *err = NULL;
Paolo Bonzini2d7799f2012-02-09 11:21:03 +010052 Visitor *v;
53
54 v = visitor_input_test_init(data, "-42");
55
Eric Blake51e72bc2016-01-29 06:48:54 -070056 visit_type_int(v, NULL, &res, &err);
Markus Armbrustere940f542014-05-02 13:26:29 +020057 g_assert(!err);
Paolo Bonzini2d7799f2012-02-09 11:21:03 +010058 g_assert_cmpint(res, ==, value);
Markus Armbruster73374682016-04-28 15:45:29 -060059
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);
Paolo Bonzini2d7799f2012-02-09 11:21:03 +010066}
67
Hu Tao659268f2014-06-10 19:15:27 +080068static 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;
Markus Armbruster73374682016-04-28 15:45:29 -060073 Error *err = NULL;
Hu Tao659268f2014-06-10 19:15:27 +080074 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
Eric Blake51e72bc2016-01-29 06:48:54 -070079 visit_type_int16List(v, NULL, &res, &error_abort);
Hu Tao659268f2014-06-10 19:15:27 +080080 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
Eric Blake74f24cb2016-04-28 15:45:30 -060088 qapi_free_int16List(res);
Markus Armbruster73374682016-04-28 15:45:29 -060089
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);
Eric Blake74f24cb2016-04-28 15:45:30 -060095 error_free_or_abort(&err);
96 g_assert(!res);
Hu Tao659268f2014-06-10 19:15:27 +080097}
98
Paolo Bonzini2d7799f2012-02-09 11:21:03 +010099static void test_visitor_in_bool(TestInputVisitorData *data,
100 const void *unused)
101{
Markus Armbrustere940f542014-05-02 13:26:29 +0200102 Error *err = NULL;
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100103 bool res = false;
104 Visitor *v;
105
106 v = visitor_input_test_init(data, "true");
107
Eric Blake51e72bc2016-01-29 06:48:54 -0700108 visit_type_bool(v, NULL, &res, &err);
Markus Armbrustere940f542014-05-02 13:26:29 +0200109 g_assert(!err);
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100110 g_assert_cmpint(res, ==, true);
111 visitor_input_teardown(data, unused);
112
113 v = visitor_input_test_init(data, "yes");
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, "on");
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, "false");
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, ==, false);
132 visitor_input_teardown(data, unused);
133
134 v = visitor_input_test_init(data, "no");
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, "off");
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}
147
148static void test_visitor_in_number(TestInputVisitorData *data,
149 const void *unused)
150{
151 double res = 0, value = 3.14;
Markus Armbrustere940f542014-05-02 13:26:29 +0200152 Error *err = NULL;
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100153 Visitor *v;
154
155 v = visitor_input_test_init(data, "3.14");
156
Eric Blake51e72bc2016-01-29 06:48:54 -0700157 visit_type_number(v, NULL, &res, &err);
Markus Armbrustere940f542014-05-02 13:26:29 +0200158 g_assert(!err);
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100159 g_assert_cmpfloat(res, ==, value);
160}
161
162static void test_visitor_in_string(TestInputVisitorData *data,
163 const void *unused)
164{
165 char *res = NULL, *value = (char *) "Q E M U";
Markus Armbrustere940f542014-05-02 13:26:29 +0200166 Error *err = NULL;
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100167 Visitor *v;
168
169 v = visitor_input_test_init(data, value);
170
Eric Blake51e72bc2016-01-29 06:48:54 -0700171 visit_type_str(v, NULL, &res, &err);
Markus Armbrustere940f542014-05-02 13:26:29 +0200172 g_assert(!err);
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100173 g_assert_cmpstr(res, ==, value);
174
175 g_free(res);
176}
177
178static void test_visitor_in_enum(TestInputVisitorData *data,
179 const void *unused)
180{
Markus Armbrustere940f542014-05-02 13:26:29 +0200181 Error *err = NULL;
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100182 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
Eric Blake51e72bc2016-01-29 06:48:54 -0700190 visit_type_EnumOne(v, NULL, &res, &err);
Markus Armbrustere940f542014-05-02 13:26:29 +0200191 g_assert(!err);
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100192 g_assert_cmpint(i, ==, res);
193
194 visitor_input_teardown(data, NULL);
195 }
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100196}
197
Blue Swirl3f0f31a2013-01-19 15:43:53 +0000198/* Try to crash the visitors */
199static void test_visitor_in_fuzz(TestInputVisitorData *data,
200 const void *unused)
201{
202 int64_t ires;
Hu Tao659268f2014-06-10 19:15:27 +0800203 intList *ilres;
Blue Swirl3f0f31a2013-01-19 15:43:53 +0000204 bool bres;
205 double nres;
206 char *sres;
207 EnumOne eres;
Blue Swirl3f0f31a2013-01-19 15:43:53 +0000208 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);
Eric Blake51e72bc2016-01-29 06:48:54 -0700226 visit_type_int(v, NULL, &ires, NULL);
Hu Taocac124d2014-06-10 19:15:26 +0800227 visitor_input_teardown(data, NULL);
Blue Swirl3f0f31a2013-01-19 15:43:53 +0000228
229 v = visitor_input_test_init(data, buf);
Eric Blake51e72bc2016-01-29 06:48:54 -0700230 visit_type_intList(v, NULL, &ilres, NULL);
Hu Tao659268f2014-06-10 19:15:27 +0800231 visitor_input_teardown(data, NULL);
232
233 v = visitor_input_test_init(data, buf);
Eric Blake51e72bc2016-01-29 06:48:54 -0700234 visit_type_bool(v, NULL, &bres, NULL);
Blue Swirl3f0f31a2013-01-19 15:43:53 +0000235 visitor_input_teardown(data, NULL);
236
237 v = visitor_input_test_init(data, buf);
Eric Blake51e72bc2016-01-29 06:48:54 -0700238 visit_type_number(v, NULL, &nres, NULL);
Hu Taocac124d2014-06-10 19:15:26 +0800239 visitor_input_teardown(data, NULL);
Blue Swirl3f0f31a2013-01-19 15:43:53 +0000240
241 v = visitor_input_test_init(data, buf);
Peter Maydell01845432013-02-05 20:44:23 +0000242 sres = NULL;
Eric Blake51e72bc2016-01-29 06:48:54 -0700243 visit_type_str(v, NULL, &sres, NULL);
Blue Swirl3f0f31a2013-01-19 15:43:53 +0000244 g_free(sres);
Hu Taocac124d2014-06-10 19:15:26 +0800245 visitor_input_teardown(data, NULL);
Blue Swirl3f0f31a2013-01-19 15:43:53 +0000246
247 v = visitor_input_test_init(data, buf);
Eric Blake51e72bc2016-01-29 06:48:54 -0700248 visit_type_EnumOne(v, NULL, &eres, NULL);
Blue Swirl3f0f31a2013-01-19 15:43:53 +0000249 visitor_input_teardown(data, NULL);
250 }
251}
252
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100253static void input_visitor_test_add(const char *testpath,
254 TestInputVisitorData *data,
255 void (*test_func)(TestInputVisitorData *data, const void *user_data))
256{
257 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
258 visitor_input_teardown);
259}
260
261int main(int argc, char **argv)
262{
263 TestInputVisitorData in_visitor_data;
264
265 g_test_init(&argc, &argv, NULL);
266
267 input_visitor_test_add("/string-visitor/input/int",
268 &in_visitor_data, test_visitor_in_int);
Hu Tao659268f2014-06-10 19:15:27 +0800269 input_visitor_test_add("/string-visitor/input/intList",
270 &in_visitor_data, test_visitor_in_intList);
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100271 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);
Blue Swirl3f0f31a2013-01-19 15:43:53 +0000279 input_visitor_test_add("/string-visitor/input/fuzz",
280 &in_visitor_data, test_visitor_in_fuzz);
Paolo Bonzini2d7799f2012-02-09 11:21:03 +0100281
282 g_test_run();
283
284 return 0;
285}