2 * Options Visitor unit-tests.
4 * Copyright (C) 2013 Red Hat, Inc.
7 * Laszlo Ersek <lersek@redhat.com> (based on test-string-output-visitor)
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.
13 #include "qemu/osdep.h"
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() */
21 static QemuOptsList userdef_opts
= {
23 .head
= QTAILQ_HEAD_INITIALIZER(userdef_opts
.head
),
24 .desc
= { { 0 } } /* validated with OptsVisitor */
27 /* fixture (= glib test case context) and test case manipulation */
29 typedef struct OptsVisitorFixture
{
30 UserDefOptions
*userdef
;
36 setup_fixture(OptsVisitorFixture
*f
, gconstpointer test_data
)
38 const char *opts_string
= test_data
;
42 opts
= qemu_opts_parse(qemu_find_opts("userdef"), opts_string
, false,
44 g_assert(opts
!= NULL
);
46 ov
= opts_visitor_new(opts
);
47 visit_type_UserDefOptions(opts_get_visitor(ov
), NULL
, &f
->userdef
,
49 opts_visitor_cleanup(ov
);
55 teardown_fixture(OptsVisitorFixture
*f
, gconstpointer test_data
)
57 qapi_free_UserDefOptions(f
->userdef
);
63 add_test(const char *testpath
,
64 void (*test_func
)(OptsVisitorFixture
*f
, gconstpointer test_data
),
65 gconstpointer test_data
)
67 g_test_add(testpath
, OptsVisitorFixture
, test_data
, setup_fixture
,
68 test_func
, teardown_fixture
);
71 /* test output evaluation */
74 expect_ok(OptsVisitorFixture
*f
, gconstpointer test_data
)
76 g_assert(f
->err
== NULL
);
77 g_assert(f
->userdef
!= NULL
);
82 expect_fail(OptsVisitorFixture
*f
, gconstpointer test_data
)
84 g_assert(f
->err
!= NULL
);
86 /* The error message is printed when this test utility is invoked directly
87 * (ie. without gtester) and the --verbose flag is passed:
89 * tests/test-opts-visitor --verbose
91 g_test_message("'%s': %s", (const char *)test_data
,
92 error_get_pretty(f
->err
));
97 test_value(OptsVisitorFixture
*f
, gconstpointer test_data
)
99 uint64_t magic
, bitval
;
104 expect_ok(f
, test_data
);
107 for (i64
= f
->userdef
->i64
; i64
!= NULL
; i64
= i64
->next
) {
108 g_assert(-16 <= i64
->value
&& i64
->value
< 64-16);
109 bitval
= 1ull << (i64
->value
+ 16);
110 g_assert((magic
& bitval
) == 0);
113 g_assert(magic
== 0xDEADBEEF);
116 for (u64
= f
->userdef
->u64
; u64
!= NULL
; u64
= u64
->next
) {
117 g_assert(u64
->value
< 64);
118 bitval
= 1ull << u64
->value
;
119 g_assert((magic
& bitval
) == 0);
122 g_assert(magic
== 0xBADC0FFEE0DDF00DULL
);
125 for (u16
= f
->userdef
->u16
; u16
!= NULL
; u16
= u16
->next
) {
126 g_assert(u16
->value
< 64);
127 bitval
= 1ull << u16
->value
;
128 g_assert((magic
& bitval
) == 0);
131 g_assert(magic
== 0xD15EA5E);
136 expect_i64_min(OptsVisitorFixture
*f
, gconstpointer test_data
)
138 expect_ok(f
, test_data
);
139 g_assert(f
->userdef
->has_i64
);
140 g_assert(f
->userdef
->i64
->next
== NULL
);
141 g_assert(f
->userdef
->i64
->value
== INT64_MIN
);
146 expect_i64_max(OptsVisitorFixture
*f
, gconstpointer test_data
)
148 expect_ok(f
, test_data
);
149 g_assert(f
->userdef
->has_i64
);
150 g_assert(f
->userdef
->i64
->next
== NULL
);
151 g_assert(f
->userdef
->i64
->value
== INT64_MAX
);
156 expect_zero(OptsVisitorFixture
*f
, gconstpointer test_data
)
158 expect_ok(f
, test_data
);
159 g_assert(f
->userdef
->has_u64
);
160 g_assert(f
->userdef
->u64
->next
== NULL
);
161 g_assert(f
->userdef
->u64
->value
== 0);
166 expect_u64_max(OptsVisitorFixture
*f
, gconstpointer test_data
)
168 expect_ok(f
, test_data
);
169 g_assert(f
->userdef
->has_u64
);
170 g_assert(f
->userdef
->u64
->next
== NULL
);
171 g_assert(f
->userdef
->u64
->value
== UINT64_MAX
);
177 main(int argc
, char **argv
)
179 g_test_init(&argc
, &argv
, NULL
);
181 qemu_add_opts(&userdef_opts
);
183 /* Three hexadecimal magic numbers, "dead beef", "bad coffee, odd food" and
185 * <http://en.wikipedia.org/wiki/Magic_number_%28programming%29>, were
186 * converted to binary and dissected into bit ranges. Each magic number is
187 * going to be recomposed using the lists called "i64", "u64" and "u16",
190 * (Note that these types pertain to the individual bit shift counts, not
191 * the magic numbers themselves; the intent is to exercise opts_type_int()
192 * and opts_type_uint64().)
194 * The "i64" shift counts have been decreased by 16 (decimal) in order to
195 * test negative values as well. Finally, the full list of QemuOpt elements
196 * has been permuted with "shuf".
198 * Both "i64" and "u64" have some (distinct) single-element ranges
199 * represented as both "a" and "a-a". "u16" is a special case of "i64" (see
200 * visit_type_uint16()), so it wouldn't add a separate test in this regard.
203 add_test("/visitor/opts/flatten/value", &test_value
,
204 "i64=-1-0,u64=12-16,u64=2-3,i64=-11--9,u64=57,u16=9,i64=5-5,"
205 "u16=1-4,u16=20,u64=63-63,i64=-16--13,u64=50-52,i64=14-15,u16=11,"
206 "i64=7,u16=18,i64=2-3,u16=6,u64=54-55,u64=0,u64=18-20,u64=33-43,"
207 "i64=9-12,u16=26-27,u64=59-61,u16=13-16,u64=29-31,u64=22-23,"
210 add_test("/visitor/opts/i64/val1/errno", &expect_fail
,
211 "i64=0x8000000000000000");
212 add_test("/visitor/opts/i64/val1/empty", &expect_fail
, "i64=");
213 add_test("/visitor/opts/i64/val1/trailing", &expect_fail
, "i64=5z");
214 add_test("/visitor/opts/i64/nonlist", &expect_fail
, "i64x=5-6");
215 add_test("/visitor/opts/i64/val2/errno", &expect_fail
,
216 "i64=0x7fffffffffffffff-0x8000000000000000");
217 add_test("/visitor/opts/i64/val2/empty", &expect_fail
, "i64=5-");
218 add_test("/visitor/opts/i64/val2/trailing", &expect_fail
, "i64=5-6z");
219 add_test("/visitor/opts/i64/range/empty", &expect_fail
, "i64=6-5");
220 add_test("/visitor/opts/i64/range/minval", &expect_i64_min
,
221 "i64=-0x8000000000000000--0x8000000000000000");
222 add_test("/visitor/opts/i64/range/maxval", &expect_i64_max
,
223 "i64=0x7fffffffffffffff-0x7fffffffffffffff");
225 add_test("/visitor/opts/u64/val1/errno", &expect_fail
, "u64=-1");
226 add_test("/visitor/opts/u64/val1/empty", &expect_fail
, "u64=");
227 add_test("/visitor/opts/u64/val1/trailing", &expect_fail
, "u64=5z");
228 add_test("/visitor/opts/u64/nonlist", &expect_fail
, "u64x=5-6");
229 add_test("/visitor/opts/u64/val2/errno", &expect_fail
,
230 "u64=0xffffffffffffffff-0x10000000000000000");
231 add_test("/visitor/opts/u64/val2/empty", &expect_fail
, "u64=5-");
232 add_test("/visitor/opts/u64/val2/trailing", &expect_fail
, "u64=5-6z");
233 add_test("/visitor/opts/u64/range/empty", &expect_fail
, "u64=6-5");
234 add_test("/visitor/opts/u64/range/minval", &expect_zero
, "u64=0-0");
235 add_test("/visitor/opts/u64/range/maxval", &expect_u64_max
,
236 "u64=0xffffffffffffffff-0xffffffffffffffff");
238 /* Test maximum range sizes. The macro value is open-coded here
239 * *intentionally*; the test case must use concrete values by design. If
240 * OPTS_VISITOR_RANGE_MAX is changed, the following values need to be
241 * recalculated as well. The assert and this comment should help with it.
243 g_assert(OPTS_VISITOR_RANGE_MAX
== 65536);
245 /* The unsigned case is simple, a u64-u64 difference can always be
246 * represented as a u64.
248 add_test("/visitor/opts/u64/range/max", &expect_ok
, "u64=0-65535");
249 add_test("/visitor/opts/u64/range/2big", &expect_fail
, "u64=0-65536");
251 /* The same cannot be said about an i64-i64 difference. */
252 add_test("/visitor/opts/i64/range/max/pos/a", &expect_ok
,
253 "i64=0x7fffffffffff0000-0x7fffffffffffffff");
254 add_test("/visitor/opts/i64/range/max/pos/b", &expect_ok
,
255 "i64=0x7ffffffffffeffff-0x7ffffffffffffffe");
256 add_test("/visitor/opts/i64/range/2big/pos", &expect_fail
,
257 "i64=0x7ffffffffffeffff-0x7fffffffffffffff");
258 add_test("/visitor/opts/i64/range/max/neg/a", &expect_ok
,
259 "i64=-0x8000000000000000--0x7fffffffffff0001");
260 add_test("/visitor/opts/i64/range/max/neg/b", &expect_ok
,
261 "i64=-0x7fffffffffffffff--0x7fffffffffff0000");
262 add_test("/visitor/opts/i64/range/2big/neg", &expect_fail
,
263 "i64=-0x8000000000000000--0x7fffffffffff0000");
264 add_test("/visitor/opts/i64/range/2big/full", &expect_fail
,
265 "i64=-0x8000000000000000-0x7fffffffffffffff");