tests: fix memory leak in test of string input visitor
[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_bool(TestInputVisitorData *data,
68 const void *unused)
69 {
70 Error *err = NULL;
71 bool res = false;
72 Visitor *v;
73
74 v = visitor_input_test_init(data, "true");
75
76 visit_type_bool(v, &res, NULL, &err);
77 g_assert(!err);
78 g_assert_cmpint(res, ==, true);
79 visitor_input_teardown(data, unused);
80
81 v = visitor_input_test_init(data, "yes");
82
83 visit_type_bool(v, &res, NULL, &err);
84 g_assert(!err);
85 g_assert_cmpint(res, ==, true);
86 visitor_input_teardown(data, unused);
87
88 v = visitor_input_test_init(data, "on");
89
90 visit_type_bool(v, &res, NULL, &err);
91 g_assert(!err);
92 g_assert_cmpint(res, ==, true);
93 visitor_input_teardown(data, unused);
94
95 v = visitor_input_test_init(data, "false");
96
97 visit_type_bool(v, &res, NULL, &err);
98 g_assert(!err);
99 g_assert_cmpint(res, ==, false);
100 visitor_input_teardown(data, unused);
101
102 v = visitor_input_test_init(data, "no");
103
104 visit_type_bool(v, &res, NULL, &err);
105 g_assert(!err);
106 g_assert_cmpint(res, ==, false);
107 visitor_input_teardown(data, unused);
108
109 v = visitor_input_test_init(data, "off");
110
111 visit_type_bool(v, &res, NULL, &err);
112 g_assert(!err);
113 g_assert_cmpint(res, ==, false);
114 }
115
116 static void test_visitor_in_number(TestInputVisitorData *data,
117 const void *unused)
118 {
119 double res = 0, value = 3.14;
120 Error *err = NULL;
121 Visitor *v;
122
123 v = visitor_input_test_init(data, "3.14");
124
125 visit_type_number(v, &res, NULL, &err);
126 g_assert(!err);
127 g_assert_cmpfloat(res, ==, value);
128 }
129
130 static void test_visitor_in_string(TestInputVisitorData *data,
131 const void *unused)
132 {
133 char *res = NULL, *value = (char *) "Q E M U";
134 Error *err = NULL;
135 Visitor *v;
136
137 v = visitor_input_test_init(data, value);
138
139 visit_type_str(v, &res, NULL, &err);
140 g_assert(!err);
141 g_assert_cmpstr(res, ==, value);
142
143 g_free(res);
144 }
145
146 static void test_visitor_in_enum(TestInputVisitorData *data,
147 const void *unused)
148 {
149 Error *err = NULL;
150 Visitor *v;
151 EnumOne i;
152
153 for (i = 0; EnumOne_lookup[i]; i++) {
154 EnumOne res = -1;
155
156 v = visitor_input_test_init(data, EnumOne_lookup[i]);
157
158 visit_type_EnumOne(v, &res, NULL, &err);
159 g_assert(!err);
160 g_assert_cmpint(i, ==, res);
161
162 visitor_input_teardown(data, NULL);
163 }
164
165 data->siv = NULL;
166 }
167
168 /* Try to crash the visitors */
169 static void test_visitor_in_fuzz(TestInputVisitorData *data,
170 const void *unused)
171 {
172 int64_t ires;
173 bool bres;
174 double nres;
175 char *sres;
176 EnumOne eres;
177 Visitor *v;
178 unsigned int i;
179 char buf[10000];
180
181 for (i = 0; i < 100; i++) {
182 unsigned int j;
183
184 j = g_test_rand_int_range(0, sizeof(buf) - 1);
185
186 buf[j] = '\0';
187
188 if (j != 0) {
189 for (j--; j != 0; j--) {
190 buf[j - 1] = (char)g_test_rand_int_range(0, 256);
191 }
192 }
193
194 v = visitor_input_test_init(data, buf);
195 visit_type_int(v, &ires, NULL, NULL);
196 visitor_input_teardown(data, NULL);
197
198 v = visitor_input_test_init(data, buf);
199 visit_type_bool(v, &bres, NULL, NULL);
200 visitor_input_teardown(data, NULL);
201
202 v = visitor_input_test_init(data, buf);
203 visit_type_number(v, &nres, NULL, NULL);
204 visitor_input_teardown(data, NULL);
205
206 v = visitor_input_test_init(data, buf);
207 sres = NULL;
208 visit_type_str(v, &sres, NULL, NULL);
209 g_free(sres);
210 visitor_input_teardown(data, NULL);
211
212 v = visitor_input_test_init(data, buf);
213 visit_type_EnumOne(v, &eres, NULL, NULL);
214 visitor_input_teardown(data, NULL);
215 }
216 }
217
218 static void input_visitor_test_add(const char *testpath,
219 TestInputVisitorData *data,
220 void (*test_func)(TestInputVisitorData *data, const void *user_data))
221 {
222 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
223 visitor_input_teardown);
224 }
225
226 int main(int argc, char **argv)
227 {
228 TestInputVisitorData in_visitor_data;
229
230 g_test_init(&argc, &argv, NULL);
231
232 input_visitor_test_add("/string-visitor/input/int",
233 &in_visitor_data, test_visitor_in_int);
234 input_visitor_test_add("/string-visitor/input/bool",
235 &in_visitor_data, test_visitor_in_bool);
236 input_visitor_test_add("/string-visitor/input/number",
237 &in_visitor_data, test_visitor_in_number);
238 input_visitor_test_add("/string-visitor/input/string",
239 &in_visitor_data, test_visitor_in_string);
240 input_visitor_test_add("/string-visitor/input/enum",
241 &in_visitor_data, test_visitor_in_enum);
242 input_visitor_test_add("/string-visitor/input/fuzz",
243 &in_visitor_data, test_visitor_in_fuzz);
244
245 g_test_run();
246
247 return 0;
248 }