]> git.proxmox.com Git - mirror_qemu.git/blob - tests/test-opts-visitor.c
tests: Clean up includes
[mirror_qemu.git] / tests / test-opts-visitor.c
1 /*
2 * Options Visitor unit-tests.
3 *
4 * Copyright (C) 2013 Red Hat, Inc.
5 *
6 * Authors:
7 * Laszlo Ersek <lersek@redhat.com> (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
13 #include "qemu/osdep.h"
14 #include <glib.h>
15
16 #include "qemu/config-file.h" /* qemu_add_opts() */
17 #include "qemu/option.h" /* qemu_opts_parse() */
18 #include "qapi/opts-visitor.h" /* opts_visitor_new() */
19 #include "test-qapi-visit.h" /* visit_type_UserDefOptions() */
20 #include "qapi/dealloc-visitor.h" /* qapi_dealloc_visitor_new() */
21
22 static QemuOptsList userdef_opts = {
23 .name = "userdef",
24 .head = QTAILQ_HEAD_INITIALIZER(userdef_opts.head),
25 .desc = { { 0 } } /* validated with OptsVisitor */
26 };
27
28 /* fixture (= glib test case context) and test case manipulation */
29
30 typedef struct OptsVisitorFixture {
31 UserDefOptions *userdef;
32 Error *err;
33 } OptsVisitorFixture;
34
35
36 static void
37 setup_fixture(OptsVisitorFixture *f, gconstpointer test_data)
38 {
39 const char *opts_string = test_data;
40 QemuOpts *opts;
41 OptsVisitor *ov;
42
43 opts = qemu_opts_parse(qemu_find_opts("userdef"), opts_string, false,
44 NULL);
45 g_assert(opts != NULL);
46
47 ov = opts_visitor_new(opts);
48 visit_type_UserDefOptions(opts_get_visitor(ov), NULL, &f->userdef,
49 &f->err);
50 opts_visitor_cleanup(ov);
51 qemu_opts_del(opts);
52 }
53
54
55 static void
56 teardown_fixture(OptsVisitorFixture *f, gconstpointer test_data)
57 {
58 if (f->userdef != NULL) {
59 QapiDeallocVisitor *dv;
60
61 dv = qapi_dealloc_visitor_new();
62 visit_type_UserDefOptions(qapi_dealloc_get_visitor(dv), NULL,
63 &f->userdef, NULL);
64 qapi_dealloc_visitor_cleanup(dv);
65 }
66 error_free(f->err);
67 }
68
69
70 static void
71 add_test(const char *testpath,
72 void (*test_func)(OptsVisitorFixture *f, gconstpointer test_data),
73 gconstpointer test_data)
74 {
75 g_test_add(testpath, OptsVisitorFixture, test_data, setup_fixture,
76 test_func, teardown_fixture);
77 }
78
79 /* test output evaluation */
80
81 static void
82 expect_ok(OptsVisitorFixture *f, gconstpointer test_data)
83 {
84 g_assert(f->err == NULL);
85 g_assert(f->userdef != NULL);
86 }
87
88
89 static void
90 expect_fail(OptsVisitorFixture *f, gconstpointer test_data)
91 {
92 g_assert(f->err != NULL);
93
94 /* The error message is printed when this test utility is invoked directly
95 * (ie. without gtester) and the --verbose flag is passed:
96 *
97 * tests/test-opts-visitor --verbose
98 */
99 g_test_message("'%s': %s", (const char *)test_data,
100 error_get_pretty(f->err));
101 }
102
103
104 static void
105 test_value(OptsVisitorFixture *f, gconstpointer test_data)
106 {
107 uint64_t magic, bitval;
108 intList *i64;
109 uint64List *u64;
110 uint16List *u16;
111
112 expect_ok(f, test_data);
113
114 magic = 0;
115 for (i64 = f->userdef->i64; i64 != NULL; i64 = i64->next) {
116 g_assert(-16 <= i64->value && i64->value < 64-16);
117 bitval = 1ull << (i64->value + 16);
118 g_assert((magic & bitval) == 0);
119 magic |= bitval;
120 }
121 g_assert(magic == 0xDEADBEEF);
122
123 magic = 0;
124 for (u64 = f->userdef->u64; u64 != NULL; u64 = u64->next) {
125 g_assert(u64->value < 64);
126 bitval = 1ull << u64->value;
127 g_assert((magic & bitval) == 0);
128 magic |= bitval;
129 }
130 g_assert(magic == 0xBADC0FFEE0DDF00DULL);
131
132 magic = 0;
133 for (u16 = f->userdef->u16; u16 != NULL; u16 = u16->next) {
134 g_assert(u16->value < 64);
135 bitval = 1ull << u16->value;
136 g_assert((magic & bitval) == 0);
137 magic |= bitval;
138 }
139 g_assert(magic == 0xD15EA5E);
140 }
141
142
143 static void
144 expect_i64_min(OptsVisitorFixture *f, gconstpointer test_data)
145 {
146 expect_ok(f, test_data);
147 g_assert(f->userdef->has_i64);
148 g_assert(f->userdef->i64->next == NULL);
149 g_assert(f->userdef->i64->value == INT64_MIN);
150 }
151
152
153 static void
154 expect_i64_max(OptsVisitorFixture *f, gconstpointer test_data)
155 {
156 expect_ok(f, test_data);
157 g_assert(f->userdef->has_i64);
158 g_assert(f->userdef->i64->next == NULL);
159 g_assert(f->userdef->i64->value == INT64_MAX);
160 }
161
162
163 static void
164 expect_zero(OptsVisitorFixture *f, gconstpointer test_data)
165 {
166 expect_ok(f, test_data);
167 g_assert(f->userdef->has_u64);
168 g_assert(f->userdef->u64->next == NULL);
169 g_assert(f->userdef->u64->value == 0);
170 }
171
172
173 static void
174 expect_u64_max(OptsVisitorFixture *f, gconstpointer test_data)
175 {
176 expect_ok(f, test_data);
177 g_assert(f->userdef->has_u64);
178 g_assert(f->userdef->u64->next == NULL);
179 g_assert(f->userdef->u64->value == UINT64_MAX);
180 }
181
182 /* test cases */
183
184 int
185 main(int argc, char **argv)
186 {
187 g_test_init(&argc, &argv, NULL);
188
189 qemu_add_opts(&userdef_opts);
190
191 /* Three hexadecimal magic numbers, "dead beef", "bad coffee, odd food" and
192 * "disease", from
193 * <http://en.wikipedia.org/wiki/Magic_number_%28programming%29>, were
194 * converted to binary and dissected into bit ranges. Each magic number is
195 * going to be recomposed using the lists called "i64", "u64" and "u16",
196 * respectively.
197 *
198 * (Note that these types pertain to the individual bit shift counts, not
199 * the magic numbers themselves; the intent is to exercise opts_type_int()
200 * and opts_type_uint64().)
201 *
202 * The "i64" shift counts have been decreased by 16 (decimal) in order to
203 * test negative values as well. Finally, the full list of QemuOpt elements
204 * has been permuted with "shuf".
205 *
206 * Both "i64" and "u64" have some (distinct) single-element ranges
207 * represented as both "a" and "a-a". "u16" is a special case of "i64" (see
208 * visit_type_uint16()), so it wouldn't add a separate test in this regard.
209 */
210
211 add_test("/visitor/opts/flatten/value", &test_value,
212 "i64=-1-0,u64=12-16,u64=2-3,i64=-11--9,u64=57,u16=9,i64=5-5,"
213 "u16=1-4,u16=20,u64=63-63,i64=-16--13,u64=50-52,i64=14-15,u16=11,"
214 "i64=7,u16=18,i64=2-3,u16=6,u64=54-55,u64=0,u64=18-20,u64=33-43,"
215 "i64=9-12,u16=26-27,u64=59-61,u16=13-16,u64=29-31,u64=22-23,"
216 "u16=24,i64=-7--3");
217
218 add_test("/visitor/opts/i64/val1/errno", &expect_fail,
219 "i64=0x8000000000000000");
220 add_test("/visitor/opts/i64/val1/empty", &expect_fail, "i64=");
221 add_test("/visitor/opts/i64/val1/trailing", &expect_fail, "i64=5z");
222 add_test("/visitor/opts/i64/nonlist", &expect_fail, "i64x=5-6");
223 add_test("/visitor/opts/i64/val2/errno", &expect_fail,
224 "i64=0x7fffffffffffffff-0x8000000000000000");
225 add_test("/visitor/opts/i64/val2/empty", &expect_fail, "i64=5-");
226 add_test("/visitor/opts/i64/val2/trailing", &expect_fail, "i64=5-6z");
227 add_test("/visitor/opts/i64/range/empty", &expect_fail, "i64=6-5");
228 add_test("/visitor/opts/i64/range/minval", &expect_i64_min,
229 "i64=-0x8000000000000000--0x8000000000000000");
230 add_test("/visitor/opts/i64/range/maxval", &expect_i64_max,
231 "i64=0x7fffffffffffffff-0x7fffffffffffffff");
232
233 add_test("/visitor/opts/u64/val1/errno", &expect_fail, "u64=-1");
234 add_test("/visitor/opts/u64/val1/empty", &expect_fail, "u64=");
235 add_test("/visitor/opts/u64/val1/trailing", &expect_fail, "u64=5z");
236 add_test("/visitor/opts/u64/nonlist", &expect_fail, "u64x=5-6");
237 add_test("/visitor/opts/u64/val2/errno", &expect_fail,
238 "u64=0xffffffffffffffff-0x10000000000000000");
239 add_test("/visitor/opts/u64/val2/empty", &expect_fail, "u64=5-");
240 add_test("/visitor/opts/u64/val2/trailing", &expect_fail, "u64=5-6z");
241 add_test("/visitor/opts/u64/range/empty", &expect_fail, "u64=6-5");
242 add_test("/visitor/opts/u64/range/minval", &expect_zero, "u64=0-0");
243 add_test("/visitor/opts/u64/range/maxval", &expect_u64_max,
244 "u64=0xffffffffffffffff-0xffffffffffffffff");
245
246 /* Test maximum range sizes. The macro value is open-coded here
247 * *intentionally*; the test case must use concrete values by design. If
248 * OPTS_VISITOR_RANGE_MAX is changed, the following values need to be
249 * recalculated as well. The assert and this comment should help with it.
250 */
251 g_assert(OPTS_VISITOR_RANGE_MAX == 65536);
252
253 /* The unsigned case is simple, a u64-u64 difference can always be
254 * represented as a u64.
255 */
256 add_test("/visitor/opts/u64/range/max", &expect_ok, "u64=0-65535");
257 add_test("/visitor/opts/u64/range/2big", &expect_fail, "u64=0-65536");
258
259 /* The same cannot be said about an i64-i64 difference. */
260 add_test("/visitor/opts/i64/range/max/pos/a", &expect_ok,
261 "i64=0x7fffffffffff0000-0x7fffffffffffffff");
262 add_test("/visitor/opts/i64/range/max/pos/b", &expect_ok,
263 "i64=0x7ffffffffffeffff-0x7ffffffffffffffe");
264 add_test("/visitor/opts/i64/range/2big/pos", &expect_fail,
265 "i64=0x7ffffffffffeffff-0x7fffffffffffffff");
266 add_test("/visitor/opts/i64/range/max/neg/a", &expect_ok,
267 "i64=-0x8000000000000000--0x7fffffffffff0001");
268 add_test("/visitor/opts/i64/range/max/neg/b", &expect_ok,
269 "i64=-0x7fffffffffffffff--0x7fffffffffff0000");
270 add_test("/visitor/opts/i64/range/2big/neg", &expect_fail,
271 "i64=-0x8000000000000000--0x7fffffffffff0000");
272 add_test("/visitor/opts/i64/range/2big/full", &expect_fail,
273 "i64=-0x8000000000000000-0x7fffffffffffffff");
274
275 g_test_run();
276 return 0;
277 }