1
2
3
4
5
6
7
8
9
10
11
12
13#include "qemu/osdep.h"
14#include <glib.h>
15
16#include "qemu/config-file.h"
17#include "qemu/option.h"
18#include "qapi/error.h"
19#include "qapi/opts-visitor.h"
20#include "test-qapi-visit.h"
21
22static QemuOptsList userdef_opts = {
23 .name = "userdef",
24 .head = QTAILQ_HEAD_INITIALIZER(userdef_opts.head),
25 .desc = { { 0 } }
26};
27
28
29
30typedef struct OptsVisitorFixture {
31 UserDefOptions *userdef;
32 Error *err;
33} OptsVisitorFixture;
34
35
36static void
37setup_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
55static void
56teardown_fixture(OptsVisitorFixture *f, gconstpointer test_data)
57{
58 qapi_free_UserDefOptions(f->userdef);
59 error_free(f->err);
60}
61
62
63static void
64add_test(const char *testpath,
65 void (*test_func)(OptsVisitorFixture *f, gconstpointer test_data),
66 gconstpointer test_data)
67{
68 g_test_add(testpath, OptsVisitorFixture, test_data, setup_fixture,
69 test_func, teardown_fixture);
70}
71
72
73
74static void
75expect_ok(OptsVisitorFixture *f, gconstpointer test_data)
76{
77 g_assert(f->err == NULL);
78 g_assert(f->userdef != NULL);
79}
80
81
82static void
83expect_fail(OptsVisitorFixture *f, gconstpointer test_data)
84{
85 g_assert(f->err != NULL);
86
87
88
89
90
91
92 g_test_message("'%s': %s", (const char *)test_data,
93 error_get_pretty(f->err));
94}
95
96
97static void
98test_value(OptsVisitorFixture *f, gconstpointer test_data)
99{
100 uint64_t magic, bitval;
101 intList *i64;
102 uint64List *u64;
103 uint16List *u16;
104
105 expect_ok(f, test_data);
106
107 magic = 0;
108 for (i64 = f->userdef->i64; i64 != NULL; i64 = i64->next) {
109 g_assert(-16 <= i64->value && i64->value < 64-16);
110 bitval = 1ull << (i64->value + 16);
111 g_assert((magic & bitval) == 0);
112 magic |= bitval;
113 }
114 g_assert(magic == 0xDEADBEEF);
115
116 magic = 0;
117 for (u64 = f->userdef->u64; u64 != NULL; u64 = u64->next) {
118 g_assert(u64->value < 64);
119 bitval = 1ull << u64->value;
120 g_assert((magic & bitval) == 0);
121 magic |= bitval;
122 }
123 g_assert(magic == 0xBADC0FFEE0DDF00DULL);
124
125 magic = 0;
126 for (u16 = f->userdef->u16; u16 != NULL; u16 = u16->next) {
127 g_assert(u16->value < 64);
128 bitval = 1ull << u16->value;
129 g_assert((magic & bitval) == 0);
130 magic |= bitval;
131 }
132 g_assert(magic == 0xD15EA5E);
133}
134
135
136static void
137expect_i64_min(OptsVisitorFixture *f, gconstpointer test_data)
138{
139 expect_ok(f, test_data);
140 g_assert(f->userdef->has_i64);
141 g_assert(f->userdef->i64->next == NULL);
142 g_assert(f->userdef->i64->value == INT64_MIN);
143}
144
145
146static void
147expect_i64_max(OptsVisitorFixture *f, gconstpointer test_data)
148{
149 expect_ok(f, test_data);
150 g_assert(f->userdef->has_i64);
151 g_assert(f->userdef->i64->next == NULL);
152 g_assert(f->userdef->i64->value == INT64_MAX);
153}
154
155
156static void
157expect_zero(OptsVisitorFixture *f, gconstpointer test_data)
158{
159 expect_ok(f, test_data);
160 g_assert(f->userdef->has_u64);
161 g_assert(f->userdef->u64->next == NULL);
162 g_assert(f->userdef->u64->value == 0);
163}
164
165
166static void
167expect_u64_max(OptsVisitorFixture *f, gconstpointer test_data)
168{
169 expect_ok(f, test_data);
170 g_assert(f->userdef->has_u64);
171 g_assert(f->userdef->u64->next == NULL);
172 g_assert(f->userdef->u64->value == UINT64_MAX);
173}
174
175
176
177int
178main(int argc, char **argv)
179{
180 g_test_init(&argc, &argv, NULL);
181
182 qemu_add_opts(&userdef_opts);
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204 add_test("/visitor/opts/flatten/value", &test_value,
205 "i64=-1-0,u64=12-16,u64=2-3,i64=-11--9,u64=57,u16=9,i64=5-5,"
206 "u16=1-4,u16=20,u64=63-63,i64=-16--13,u64=50-52,i64=14-15,u16=11,"
207 "i64=7,u16=18,i64=2-3,u16=6,u64=54-55,u64=0,u64=18-20,u64=33-43,"
208 "i64=9-12,u16=26-27,u64=59-61,u16=13-16,u64=29-31,u64=22-23,"
209 "u16=24,i64=-7--3");
210
211 add_test("/visitor/opts/i64/val1/errno", &expect_fail,
212 "i64=0x8000000000000000");
213 add_test("/visitor/opts/i64/val1/empty", &expect_fail, "i64=");
214 add_test("/visitor/opts/i64/val1/trailing", &expect_fail, "i64=5z");
215 add_test("/visitor/opts/i64/nonlist", &expect_fail, "i64x=5-6");
216 add_test("/visitor/opts/i64/val2/errno", &expect_fail,
217 "i64=0x7fffffffffffffff-0x8000000000000000");
218 add_test("/visitor/opts/i64/val2/empty", &expect_fail, "i64=5-");
219 add_test("/visitor/opts/i64/val2/trailing", &expect_fail, "i64=5-6z");
220 add_test("/visitor/opts/i64/range/empty", &expect_fail, "i64=6-5");
221 add_test("/visitor/opts/i64/range/minval", &expect_i64_min,
222 "i64=-0x8000000000000000--0x8000000000000000");
223 add_test("/visitor/opts/i64/range/maxval", &expect_i64_max,
224 "i64=0x7fffffffffffffff-0x7fffffffffffffff");
225
226 add_test("/visitor/opts/u64/val1/errno", &expect_fail, "u64=-1");
227 add_test("/visitor/opts/u64/val1/empty", &expect_fail, "u64=");
228 add_test("/visitor/opts/u64/val1/trailing", &expect_fail, "u64=5z");
229 add_test("/visitor/opts/u64/nonlist", &expect_fail, "u64x=5-6");
230 add_test("/visitor/opts/u64/val2/errno", &expect_fail,
231 "u64=0xffffffffffffffff-0x10000000000000000");
232 add_test("/visitor/opts/u64/val2/empty", &expect_fail, "u64=5-");
233 add_test("/visitor/opts/u64/val2/trailing", &expect_fail, "u64=5-6z");
234 add_test("/visitor/opts/u64/range/empty", &expect_fail, "u64=6-5");
235 add_test("/visitor/opts/u64/range/minval", &expect_zero, "u64=0-0");
236 add_test("/visitor/opts/u64/range/maxval", &expect_u64_max,
237 "u64=0xffffffffffffffff-0xffffffffffffffff");
238
239
240
241
242
243
244 g_assert(OPTS_VISITOR_RANGE_MAX == 65536);
245
246
247
248
249 add_test("/visitor/opts/u64/range/max", &expect_ok, "u64=0-65535");
250 add_test("/visitor/opts/u64/range/2big", &expect_fail, "u64=0-65536");
251
252
253 add_test("/visitor/opts/i64/range/max/pos/a", &expect_ok,
254 "i64=0x7fffffffffff0000-0x7fffffffffffffff");
255 add_test("/visitor/opts/i64/range/max/pos/b", &expect_ok,
256 "i64=0x7ffffffffffeffff-0x7ffffffffffffffe");
257 add_test("/visitor/opts/i64/range/2big/pos", &expect_fail,
258 "i64=0x7ffffffffffeffff-0x7fffffffffffffff");
259 add_test("/visitor/opts/i64/range/max/neg/a", &expect_ok,
260 "i64=-0x8000000000000000--0x7fffffffffff0001");
261 add_test("/visitor/opts/i64/range/max/neg/b", &expect_ok,
262 "i64=-0x7fffffffffffffff--0x7fffffffffff0000");
263 add_test("/visitor/opts/i64/range/2big/neg", &expect_fail,
264 "i64=-0x8000000000000000--0x7fffffffffff0000");
265 add_test("/visitor/opts/i64/range/2big/full", &expect_fail,
266 "i64=-0x8000000000000000-0x7fffffffffffffff");
267
268 g_test_run();
269 return 0;
270}
271