]>
Commit | Line | Data |
---|---|---|
3953e3a5 LE |
1 | /* |
2 | * Options Visitor unit-tests. | |
3 | * | |
4 | * Copyright (C) 2013 Red Hat, Inc. | |
5 | * | |
6 | * Authors: | |
7 | * Laszlo Ersek <[email protected]> (based on test-string-output-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 | ||
681c28a3 | 13 | #include "qemu/osdep.h" |
3953e3a5 LE |
14 | |
15 | #include "qemu/config-file.h" /* qemu_add_opts() */ | |
16 | #include "qemu/option.h" /* qemu_opts_parse() */ | |
da34e65c | 17 | #include "qapi/error.h" |
3953e3a5 LE |
18 | #include "qapi/opts-visitor.h" /* opts_visitor_new() */ |
19 | #include "test-qapi-visit.h" /* visit_type_UserDefOptions() */ | |
3953e3a5 LE |
20 | |
21 | static QemuOptsList userdef_opts = { | |
22 | .name = "userdef", | |
23 | .head = QTAILQ_HEAD_INITIALIZER(userdef_opts.head), | |
24 | .desc = { { 0 } } /* validated with OptsVisitor */ | |
25 | }; | |
26 | ||
27 | /* fixture (= glib test case context) and test case manipulation */ | |
28 | ||
29 | typedef struct OptsVisitorFixture { | |
30 | UserDefOptions *userdef; | |
31 | Error *err; | |
32 | } OptsVisitorFixture; | |
33 | ||
34 | ||
35 | static void | |
36 | setup_fixture(OptsVisitorFixture *f, gconstpointer test_data) | |
37 | { | |
38 | const char *opts_string = test_data; | |
39 | QemuOpts *opts; | |
09204eac | 40 | Visitor *v; |
3953e3a5 | 41 | |
70b94331 MA |
42 | opts = qemu_opts_parse(qemu_find_opts("userdef"), opts_string, false, |
43 | NULL); | |
3953e3a5 LE |
44 | g_assert(opts != NULL); |
45 | ||
09204eac EB |
46 | v = opts_visitor_new(opts); |
47 | visit_type_UserDefOptions(v, NULL, &f->userdef, &f->err); | |
48 | visit_free(v); | |
3953e3a5 LE |
49 | qemu_opts_del(opts); |
50 | } | |
51 | ||
52 | ||
53 | static void | |
54 | teardown_fixture(OptsVisitorFixture *f, gconstpointer test_data) | |
55 | { | |
96a1616c | 56 | qapi_free_UserDefOptions(f->userdef); |
3953e3a5 LE |
57 | error_free(f->err); |
58 | } | |
59 | ||
60 | ||
61 | static void | |
62 | add_test(const char *testpath, | |
63 | void (*test_func)(OptsVisitorFixture *f, gconstpointer test_data), | |
64 | gconstpointer test_data) | |
65 | { | |
66 | g_test_add(testpath, OptsVisitorFixture, test_data, setup_fixture, | |
67 | test_func, teardown_fixture); | |
68 | } | |
69 | ||
70 | /* test output evaluation */ | |
71 | ||
72 | static void | |
73 | expect_ok(OptsVisitorFixture *f, gconstpointer test_data) | |
74 | { | |
75 | g_assert(f->err == NULL); | |
76 | g_assert(f->userdef != NULL); | |
77 | } | |
78 | ||
79 | ||
80 | static void | |
81 | expect_fail(OptsVisitorFixture *f, gconstpointer test_data) | |
82 | { | |
83 | g_assert(f->err != NULL); | |
84 | ||
85 | /* The error message is printed when this test utility is invoked directly | |
86 | * (ie. without gtester) and the --verbose flag is passed: | |
87 | * | |
88 | * tests/test-opts-visitor --verbose | |
89 | */ | |
90 | g_test_message("'%s': %s", (const char *)test_data, | |
91 | error_get_pretty(f->err)); | |
92 | } | |
93 | ||
94 | ||
95 | static void | |
96 | test_value(OptsVisitorFixture *f, gconstpointer test_data) | |
97 | { | |
98 | uint64_t magic, bitval; | |
99 | intList *i64; | |
100 | uint64List *u64; | |
101 | uint16List *u16; | |
102 | ||
103 | expect_ok(f, test_data); | |
104 | ||
105 | magic = 0; | |
106 | for (i64 = f->userdef->i64; i64 != NULL; i64 = i64->next) { | |
107 | g_assert(-16 <= i64->value && i64->value < 64-16); | |
108 | bitval = 1ull << (i64->value + 16); | |
109 | g_assert((magic & bitval) == 0); | |
110 | magic |= bitval; | |
111 | } | |
112 | g_assert(magic == 0xDEADBEEF); | |
113 | ||
114 | magic = 0; | |
115 | for (u64 = f->userdef->u64; u64 != NULL; u64 = u64->next) { | |
116 | g_assert(u64->value < 64); | |
117 | bitval = 1ull << u64->value; | |
118 | g_assert((magic & bitval) == 0); | |
119 | magic |= bitval; | |
120 | } | |
5cb6be2c | 121 | g_assert(magic == 0xBADC0FFEE0DDF00DULL); |
3953e3a5 LE |
122 | |
123 | magic = 0; | |
124 | for (u16 = f->userdef->u16; u16 != NULL; u16 = u16->next) { | |
125 | g_assert(u16->value < 64); | |
126 | bitval = 1ull << u16->value; | |
127 | g_assert((magic & bitval) == 0); | |
128 | magic |= bitval; | |
129 | } | |
130 | g_assert(magic == 0xD15EA5E); | |
131 | } | |
132 | ||
133 | ||
134 | static void | |
135 | expect_i64_min(OptsVisitorFixture *f, gconstpointer test_data) | |
136 | { | |
137 | expect_ok(f, test_data); | |
138 | g_assert(f->userdef->has_i64); | |
139 | g_assert(f->userdef->i64->next == NULL); | |
140 | g_assert(f->userdef->i64->value == INT64_MIN); | |
141 | } | |
142 | ||
143 | ||
144 | static void | |
145 | expect_i64_max(OptsVisitorFixture *f, gconstpointer test_data) | |
146 | { | |
147 | expect_ok(f, test_data); | |
148 | g_assert(f->userdef->has_i64); | |
149 | g_assert(f->userdef->i64->next == NULL); | |
150 | g_assert(f->userdef->i64->value == INT64_MAX); | |
151 | } | |
152 | ||
153 | ||
154 | static void | |
155 | expect_zero(OptsVisitorFixture *f, gconstpointer test_data) | |
156 | { | |
157 | expect_ok(f, test_data); | |
158 | g_assert(f->userdef->has_u64); | |
159 | g_assert(f->userdef->u64->next == NULL); | |
160 | g_assert(f->userdef->u64->value == 0); | |
161 | } | |
162 | ||
163 | ||
164 | static void | |
165 | expect_u64_max(OptsVisitorFixture *f, gconstpointer test_data) | |
166 | { | |
167 | expect_ok(f, test_data); | |
168 | g_assert(f->userdef->has_u64); | |
169 | g_assert(f->userdef->u64->next == NULL); | |
170 | g_assert(f->userdef->u64->value == UINT64_MAX); | |
171 | } | |
172 | ||
173 | /* test cases */ | |
174 | ||
9cb8ef36 MA |
175 | static void |
176 | test_opts_range_unvisited(void) | |
177 | { | |
178 | intList *list = NULL; | |
179 | intList *tail; | |
180 | QemuOpts *opts; | |
181 | Visitor *v; | |
182 | ||
183 | opts = qemu_opts_parse(qemu_find_opts("userdef"), "ilist=0-2", false, | |
184 | &error_abort); | |
185 | ||
186 | v = opts_visitor_new(opts); | |
187 | ||
188 | visit_start_struct(v, NULL, NULL, 0, &error_abort); | |
189 | ||
190 | /* Would be simpler if the visitor genuinely supported virtual walks */ | |
191 | visit_start_list(v, "ilist", (GenericList **)&list, sizeof(*list), | |
192 | &error_abort); | |
193 | tail = list; | |
194 | visit_type_int(v, NULL, &tail->value, &error_abort); | |
195 | g_assert_cmpint(tail->value, ==, 0); | |
196 | tail = (intList *)visit_next_list(v, (GenericList *)tail, sizeof(*list)); | |
197 | g_assert(tail); | |
198 | visit_type_int(v, NULL, &tail->value, &error_abort); | |
199 | g_assert_cmpint(tail->value, ==, 1); | |
200 | tail = (intList *)visit_next_list(v, (GenericList *)tail, sizeof(*list)); | |
201 | g_assert(tail); | |
a4a1c70d | 202 | visit_check_list(v, &error_abort); /* BUG: unvisited tail not reported */ |
9cb8ef36 | 203 | visit_end_list(v, (void **)&list); |
9cb8ef36 MA |
204 | |
205 | visit_check_struct(v, &error_abort); | |
206 | visit_end_struct(v, NULL); | |
207 | ||
208 | qapi_free_intList(list); | |
209 | visit_free(v); | |
210 | qemu_opts_del(opts); | |
211 | } | |
212 | ||
a9416dc6 MA |
213 | static void |
214 | test_opts_range_beyond(void) | |
215 | { | |
216 | Error *err = NULL; | |
217 | intList *list = NULL; | |
218 | intList *tail; | |
219 | QemuOpts *opts; | |
220 | Visitor *v; | |
221 | int64_t val; | |
222 | ||
223 | opts = qemu_opts_parse(qemu_find_opts("userdef"), "ilist=0", false, | |
224 | &error_abort); | |
225 | ||
226 | v = opts_visitor_new(opts); | |
227 | ||
228 | visit_start_struct(v, NULL, NULL, 0, &error_abort); | |
229 | ||
230 | /* Would be simpler if the visitor genuinely supported virtual walks */ | |
231 | visit_start_list(v, "ilist", (GenericList **)&list, sizeof(*list), | |
232 | &error_abort); | |
233 | tail = list; | |
234 | visit_type_int(v, NULL, &tail->value, &error_abort); | |
235 | g_assert_cmpint(tail->value, ==, 0); | |
236 | tail = (intList *)visit_next_list(v, (GenericList *)tail, sizeof(*tail)); | |
237 | g_assert(!tail); | |
238 | visit_type_int(v, NULL, &val, &err); | |
239 | error_free_or_abort(&err); | |
240 | visit_end_list(v, (void **)&list); | |
241 | ||
242 | visit_check_struct(v, &error_abort); | |
243 | visit_end_struct(v, NULL); | |
244 | ||
245 | qapi_free_intList(list); | |
246 | visit_free(v); | |
247 | qemu_opts_del(opts); | |
248 | } | |
249 | ||
3953e3a5 LE |
250 | int |
251 | main(int argc, char **argv) | |
252 | { | |
253 | g_test_init(&argc, &argv, NULL); | |
254 | ||
255 | qemu_add_opts(&userdef_opts); | |
256 | ||
257 | /* Three hexadecimal magic numbers, "dead beef", "bad coffee, odd food" and | |
258 | * "disease", from | |
259 | * <http://en.wikipedia.org/wiki/Magic_number_%28programming%29>, were | |
260 | * converted to binary and dissected into bit ranges. Each magic number is | |
261 | * going to be recomposed using the lists called "i64", "u64" and "u16", | |
262 | * respectively. | |
263 | * | |
264 | * (Note that these types pertain to the individual bit shift counts, not | |
265 | * the magic numbers themselves; the intent is to exercise opts_type_int() | |
266 | * and opts_type_uint64().) | |
267 | * | |
268 | * The "i64" shift counts have been decreased by 16 (decimal) in order to | |
269 | * test negative values as well. Finally, the full list of QemuOpt elements | |
270 | * has been permuted with "shuf". | |
271 | * | |
272 | * Both "i64" and "u64" have some (distinct) single-element ranges | |
273 | * represented as both "a" and "a-a". "u16" is a special case of "i64" (see | |
274 | * visit_type_uint16()), so it wouldn't add a separate test in this regard. | |
275 | */ | |
276 | ||
277 | add_test("/visitor/opts/flatten/value", &test_value, | |
278 | "i64=-1-0,u64=12-16,u64=2-3,i64=-11--9,u64=57,u16=9,i64=5-5," | |
279 | "u16=1-4,u16=20,u64=63-63,i64=-16--13,u64=50-52,i64=14-15,u16=11," | |
280 | "i64=7,u16=18,i64=2-3,u16=6,u64=54-55,u64=0,u64=18-20,u64=33-43," | |
281 | "i64=9-12,u16=26-27,u64=59-61,u16=13-16,u64=29-31,u64=22-23," | |
282 | "u16=24,i64=-7--3"); | |
283 | ||
284 | add_test("/visitor/opts/i64/val1/errno", &expect_fail, | |
285 | "i64=0x8000000000000000"); | |
286 | add_test("/visitor/opts/i64/val1/empty", &expect_fail, "i64="); | |
287 | add_test("/visitor/opts/i64/val1/trailing", &expect_fail, "i64=5z"); | |
288 | add_test("/visitor/opts/i64/nonlist", &expect_fail, "i64x=5-6"); | |
289 | add_test("/visitor/opts/i64/val2/errno", &expect_fail, | |
290 | "i64=0x7fffffffffffffff-0x8000000000000000"); | |
291 | add_test("/visitor/opts/i64/val2/empty", &expect_fail, "i64=5-"); | |
292 | add_test("/visitor/opts/i64/val2/trailing", &expect_fail, "i64=5-6z"); | |
293 | add_test("/visitor/opts/i64/range/empty", &expect_fail, "i64=6-5"); | |
294 | add_test("/visitor/opts/i64/range/minval", &expect_i64_min, | |
295 | "i64=-0x8000000000000000--0x8000000000000000"); | |
296 | add_test("/visitor/opts/i64/range/maxval", &expect_i64_max, | |
297 | "i64=0x7fffffffffffffff-0x7fffffffffffffff"); | |
298 | ||
299 | add_test("/visitor/opts/u64/val1/errno", &expect_fail, "u64=-1"); | |
300 | add_test("/visitor/opts/u64/val1/empty", &expect_fail, "u64="); | |
301 | add_test("/visitor/opts/u64/val1/trailing", &expect_fail, "u64=5z"); | |
302 | add_test("/visitor/opts/u64/nonlist", &expect_fail, "u64x=5-6"); | |
303 | add_test("/visitor/opts/u64/val2/errno", &expect_fail, | |
304 | "u64=0xffffffffffffffff-0x10000000000000000"); | |
305 | add_test("/visitor/opts/u64/val2/empty", &expect_fail, "u64=5-"); | |
306 | add_test("/visitor/opts/u64/val2/trailing", &expect_fail, "u64=5-6z"); | |
307 | add_test("/visitor/opts/u64/range/empty", &expect_fail, "u64=6-5"); | |
308 | add_test("/visitor/opts/u64/range/minval", &expect_zero, "u64=0-0"); | |
309 | add_test("/visitor/opts/u64/range/maxval", &expect_u64_max, | |
310 | "u64=0xffffffffffffffff-0xffffffffffffffff"); | |
311 | ||
312 | /* Test maximum range sizes. The macro value is open-coded here | |
313 | * *intentionally*; the test case must use concrete values by design. If | |
314 | * OPTS_VISITOR_RANGE_MAX is changed, the following values need to be | |
315 | * recalculated as well. The assert and this comment should help with it. | |
316 | */ | |
317 | g_assert(OPTS_VISITOR_RANGE_MAX == 65536); | |
318 | ||
319 | /* The unsigned case is simple, a u64-u64 difference can always be | |
320 | * represented as a u64. | |
321 | */ | |
322 | add_test("/visitor/opts/u64/range/max", &expect_ok, "u64=0-65535"); | |
323 | add_test("/visitor/opts/u64/range/2big", &expect_fail, "u64=0-65536"); | |
324 | ||
325 | /* The same cannot be said about an i64-i64 difference. */ | |
326 | add_test("/visitor/opts/i64/range/max/pos/a", &expect_ok, | |
327 | "i64=0x7fffffffffff0000-0x7fffffffffffffff"); | |
328 | add_test("/visitor/opts/i64/range/max/pos/b", &expect_ok, | |
329 | "i64=0x7ffffffffffeffff-0x7ffffffffffffffe"); | |
330 | add_test("/visitor/opts/i64/range/2big/pos", &expect_fail, | |
331 | "i64=0x7ffffffffffeffff-0x7fffffffffffffff"); | |
332 | add_test("/visitor/opts/i64/range/max/neg/a", &expect_ok, | |
333 | "i64=-0x8000000000000000--0x7fffffffffff0001"); | |
334 | add_test("/visitor/opts/i64/range/max/neg/b", &expect_ok, | |
335 | "i64=-0x7fffffffffffffff--0x7fffffffffff0000"); | |
336 | add_test("/visitor/opts/i64/range/2big/neg", &expect_fail, | |
337 | "i64=-0x8000000000000000--0x7fffffffffff0000"); | |
338 | add_test("/visitor/opts/i64/range/2big/full", &expect_fail, | |
339 | "i64=-0x8000000000000000-0x7fffffffffffffff"); | |
340 | ||
9cb8ef36 MA |
341 | g_test_add_func("/visitor/opts/range/unvisited", |
342 | test_opts_range_unvisited); | |
a9416dc6 MA |
343 | g_test_add_func("/visitor/opts/range/beyond", |
344 | test_opts_range_beyond); | |
9cb8ef36 | 345 | |
3953e3a5 LE |
346 | g_test_run(); |
347 | return 0; | |
348 | } |