]> git.proxmox.com Git - mirror_qemu.git/blame - tests/test-keyval.c
qemu-option: restrict qemu_opts_set to merge-lists QemuOpts
[mirror_qemu.git] / tests / test-keyval.c
CommitLineData
d454dbe0
MA
1/*
2 * Unit tests for parsing of KEY=VALUE,... strings
3 *
4 * Copyright (C) 2017 Red Hat Inc.
5 *
6 * Authors:
7 * Markus Armbruster <armbru@redhat.com>,
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"
d23b6caa 14#include "qemu/units.h"
d454dbe0 15#include "qapi/error.h"
452fcdbc 16#include "qapi/qmp/qdict.h"
47e6b297 17#include "qapi/qmp/qlist.h"
0b2c1bee 18#include "qapi/qmp/qstring.h"
9e3943f8 19#include "qapi/qobject-input-visitor.h"
599c156b 20#include "test-qapi-visit.h"
9e3943f8 21#include "qemu/cutils.h"
d454dbe0
MA
22#include "qemu/option.h"
23
24static void test_keyval_parse(void)
25{
26 Error *err = NULL;
27 QDict *qdict, *sub_qdict;
28 char long_key[129];
29 char *params;
8bf12c4f 30 bool help;
d454dbe0
MA
31
32 /* Nothing */
8bf12c4f 33 qdict = keyval_parse("", NULL, NULL, &error_abort);
d454dbe0 34 g_assert_cmpuint(qdict_size(qdict), ==, 0);
cb3e7f08 35 qobject_unref(qdict);
d454dbe0
MA
36
37 /* Empty key (qemu_opts_parse() accepts this) */
8bf12c4f 38 qdict = keyval_parse("=val", NULL, NULL, &err);
d454dbe0
MA
39 error_free_or_abort(&err);
40 g_assert(!qdict);
41
42 /* Empty key fragment */
8bf12c4f 43 qdict = keyval_parse(".", NULL, NULL, &err);
d454dbe0
MA
44 error_free_or_abort(&err);
45 g_assert(!qdict);
8bf12c4f 46 qdict = keyval_parse("key.", NULL, NULL, &err);
d454dbe0
MA
47 error_free_or_abort(&err);
48 g_assert(!qdict);
49
f7400483 50 /* Invalid non-empty key (qemu_opts_parse() doesn't care) */
8bf12c4f 51 qdict = keyval_parse("7up=val", NULL, NULL, &err);
f7400483
MA
52 error_free_or_abort(&err);
53 g_assert(!qdict);
54
d454dbe0
MA
55 /* Overlong key */
56 memset(long_key, 'a', 127);
57 long_key[127] = 'z';
58 long_key[128] = 0;
59 params = g_strdup_printf("k.%s=v", long_key);
8bf12c4f 60 qdict = keyval_parse(params + 2, NULL, NULL, &err);
d454dbe0
MA
61 error_free_or_abort(&err);
62 g_assert(!qdict);
63
64 /* Overlong key fragment */
8bf12c4f 65 qdict = keyval_parse(params, NULL, NULL, &err);
d454dbe0
MA
66 error_free_or_abort(&err);
67 g_assert(!qdict);
68 g_free(params);
69
70 /* Long key (qemu_opts_parse() accepts and truncates silently) */
71 params = g_strdup_printf("k.%s=v", long_key + 1);
8bf12c4f 72 qdict = keyval_parse(params + 2, NULL, NULL, &error_abort);
d454dbe0
MA
73 g_assert_cmpuint(qdict_size(qdict), ==, 1);
74 g_assert_cmpstr(qdict_get_try_str(qdict, long_key + 1), ==, "v");
cb3e7f08 75 qobject_unref(qdict);
d454dbe0
MA
76
77 /* Long key fragment */
8bf12c4f 78 qdict = keyval_parse(params, NULL, NULL, &error_abort);
d454dbe0
MA
79 g_assert_cmpuint(qdict_size(qdict), ==, 1);
80 sub_qdict = qdict_get_qdict(qdict, "k");
81 g_assert(sub_qdict);
82 g_assert_cmpuint(qdict_size(sub_qdict), ==, 1);
83 g_assert_cmpstr(qdict_get_try_str(sub_qdict, long_key + 1), ==, "v");
cb3e7f08 84 qobject_unref(qdict);
d454dbe0
MA
85 g_free(params);
86
f7400483 87 /* Crap after valid key */
8bf12c4f 88 qdict = keyval_parse("key[0]=val", NULL, NULL, &err);
f7400483
MA
89 error_free_or_abort(&err);
90 g_assert(!qdict);
91
d454dbe0 92 /* Multiple keys, last one wins */
8bf12c4f 93 qdict = keyval_parse("a=1,b=2,,x,a=3", NULL, NULL, &error_abort);
d454dbe0
MA
94 g_assert_cmpuint(qdict_size(qdict), ==, 2);
95 g_assert_cmpstr(qdict_get_try_str(qdict, "a"), ==, "3");
96 g_assert_cmpstr(qdict_get_try_str(qdict, "b"), ==, "2,x");
cb3e7f08 97 qobject_unref(qdict);
d454dbe0
MA
98
99 /* Even when it doesn't in qemu_opts_parse() */
8bf12c4f 100 qdict = keyval_parse("id=foo,id=bar", NULL, NULL, &error_abort);
d454dbe0
MA
101 g_assert_cmpuint(qdict_size(qdict), ==, 1);
102 g_assert_cmpstr(qdict_get_try_str(qdict, "id"), ==, "bar");
cb3e7f08 103 qobject_unref(qdict);
d454dbe0
MA
104
105 /* Dotted keys */
8bf12c4f 106 qdict = keyval_parse("a.b.c=1,a.b.c=2,d=3", NULL, NULL, &error_abort);
d454dbe0
MA
107 g_assert_cmpuint(qdict_size(qdict), ==, 2);
108 sub_qdict = qdict_get_qdict(qdict, "a");
109 g_assert(sub_qdict);
110 g_assert_cmpuint(qdict_size(sub_qdict), ==, 1);
111 sub_qdict = qdict_get_qdict(sub_qdict, "b");
112 g_assert(sub_qdict);
113 g_assert_cmpuint(qdict_size(sub_qdict), ==, 1);
114 g_assert_cmpstr(qdict_get_try_str(sub_qdict, "c"), ==, "2");
115 g_assert_cmpstr(qdict_get_try_str(qdict, "d"), ==, "3");
cb3e7f08 116 qobject_unref(qdict);
d454dbe0
MA
117
118 /* Inconsistent dotted keys */
8bf12c4f 119 qdict = keyval_parse("a.b=1,a=2", NULL, NULL, &err);
d454dbe0
MA
120 error_free_or_abort(&err);
121 g_assert(!qdict);
8bf12c4f 122 qdict = keyval_parse("a.b=1,a.b.c=2", NULL, NULL, &err);
d454dbe0
MA
123 error_free_or_abort(&err);
124 g_assert(!qdict);
125
126 /* Trailing comma is ignored */
8bf12c4f 127 qdict = keyval_parse("x=y,", NULL, NULL, &error_abort);
d454dbe0
MA
128 g_assert_cmpuint(qdict_size(qdict), ==, 1);
129 g_assert_cmpstr(qdict_get_try_str(qdict, "x"), ==, "y");
cb3e7f08 130 qobject_unref(qdict);
d454dbe0
MA
131
132 /* Except when it isn't */
8bf12c4f 133 qdict = keyval_parse(",", NULL, NULL, &err);
d454dbe0
MA
134 error_free_or_abort(&err);
135 g_assert(!qdict);
136
137 /* Value containing ,id= not misinterpreted as qemu_opts_parse() does */
8bf12c4f 138 qdict = keyval_parse("x=,,id=bar", NULL, NULL, &error_abort);
d454dbe0
MA
139 g_assert_cmpuint(qdict_size(qdict), ==, 1);
140 g_assert_cmpstr(qdict_get_try_str(qdict, "x"), ==, ",id=bar");
cb3e7f08 141 qobject_unref(qdict);
d454dbe0
MA
142
143 /* Anti-social ID is left to caller (qemu_opts_parse() rejects it) */
8bf12c4f 144 qdict = keyval_parse("id=666", NULL, NULL, &error_abort);
d454dbe0
MA
145 g_assert_cmpuint(qdict_size(qdict), ==, 1);
146 g_assert_cmpstr(qdict_get_try_str(qdict, "id"), ==, "666");
cb3e7f08 147 qobject_unref(qdict);
d454dbe0
MA
148
149 /* Implied value not supported (unlike qemu_opts_parse()) */
8bf12c4f 150 qdict = keyval_parse("an,noaus,noaus=", NULL, NULL, &err);
d454dbe0
MA
151 error_free_or_abort(&err);
152 g_assert(!qdict);
153
154 /* Implied value, key "no" (qemu_opts_parse(): negated empty key) */
8bf12c4f 155 qdict = keyval_parse("no", NULL, NULL, &err);
d454dbe0
MA
156 error_free_or_abort(&err);
157 g_assert(!qdict);
158
159 /* Implied key */
8bf12c4f 160 qdict = keyval_parse("an,aus=off,noaus=", "implied", NULL, &error_abort);
d454dbe0
MA
161 g_assert_cmpuint(qdict_size(qdict), ==, 3);
162 g_assert_cmpstr(qdict_get_try_str(qdict, "implied"), ==, "an");
163 g_assert_cmpstr(qdict_get_try_str(qdict, "aus"), ==, "off");
164 g_assert_cmpstr(qdict_get_try_str(qdict, "noaus"), ==, "");
cb3e7f08 165 qobject_unref(qdict);
d454dbe0
MA
166
167 /* Implied dotted key */
8bf12c4f 168 qdict = keyval_parse("val", "eins.zwei", NULL, &error_abort);
d454dbe0
MA
169 g_assert_cmpuint(qdict_size(qdict), ==, 1);
170 sub_qdict = qdict_get_qdict(qdict, "eins");
171 g_assert(sub_qdict);
172 g_assert_cmpuint(qdict_size(sub_qdict), ==, 1);
173 g_assert_cmpstr(qdict_get_try_str(sub_qdict, "zwei"), ==, "val");
cb3e7f08 174 qobject_unref(qdict);
d454dbe0
MA
175
176 /* Implied key with empty value (qemu_opts_parse() accepts this) */
8bf12c4f 177 qdict = keyval_parse(",", "implied", NULL, &err);
d454dbe0
MA
178 error_free_or_abort(&err);
179 g_assert(!qdict);
180
181 /* Likewise (qemu_opts_parse(): implied key with comma value) */
8bf12c4f 182 qdict = keyval_parse(",,,a=1", "implied", NULL, &err);
d454dbe0
MA
183 error_free_or_abort(&err);
184 g_assert(!qdict);
185
ce40cbf1 186 /* Implied key's value can't have comma (qemu_opts_parse(): it can) */
8bf12c4f 187 qdict = keyval_parse("val,,ue", "implied", NULL, &err);
7051ae6c
MA
188 error_free_or_abort(&err);
189 g_assert(!qdict);
ce40cbf1 190
d454dbe0 191 /* Empty key is not an implied key */
8bf12c4f 192 qdict = keyval_parse("=val", "implied", NULL, &err);
d454dbe0
MA
193 error_free_or_abort(&err);
194 g_assert(!qdict);
8bf12c4f
KW
195
196 /* "help" by itself, without implied key */
197 qdict = keyval_parse("help", NULL, &help, &error_abort);
198 g_assert_cmpuint(qdict_size(qdict), ==, 0);
199 g_assert(help);
200 qobject_unref(qdict);
201
202 /* "help" by itself, with implied key */
203 qdict = keyval_parse("help", "implied", &help, &error_abort);
204 g_assert_cmpuint(qdict_size(qdict), ==, 0);
205 g_assert(help);
206 qobject_unref(qdict);
207
208 /* "help" when no help is available, without implied key */
209 qdict = keyval_parse("help", NULL, NULL, &err);
210 error_free_or_abort(&err);
211 g_assert(!qdict);
212
213 /* "help" when no help is available, with implied key */
214 qdict = keyval_parse("help", "implied", NULL, &err);
215 error_free_or_abort(&err);
216 g_assert(!qdict);
217
218 /* Key "help" */
219 qdict = keyval_parse("help=on", NULL, &help, &error_abort);
220 g_assert_cmpuint(qdict_size(qdict), ==, 1);
221 g_assert_cmpstr(qdict_get_try_str(qdict, "help"), ==, "on");
222 g_assert(!help);
223 qobject_unref(qdict);
224
225 /* "help" followed by crap, without implied key */
226 qdict = keyval_parse("help.abc", NULL, &help, &err);
227 error_free_or_abort(&err);
228 g_assert(!qdict);
229
230 /* "help" followed by crap, with implied key */
231 qdict = keyval_parse("help.abc", "implied", &help, &err);
232 g_assert_cmpuint(qdict_size(qdict), ==, 1);
233 g_assert_cmpstr(qdict_get_try_str(qdict, "implied"), ==, "help.abc");
234 g_assert(!help);
235 qobject_unref(qdict);
236
237 /* "help" with other stuff, without implied key */
238 qdict = keyval_parse("number=42,help,foo=bar", NULL, &help, &error_abort);
239 g_assert_cmpuint(qdict_size(qdict), ==, 2);
240 g_assert_cmpstr(qdict_get_try_str(qdict, "number"), ==, "42");
241 g_assert_cmpstr(qdict_get_try_str(qdict, "foo"), ==, "bar");
242 g_assert(help);
243 qobject_unref(qdict);
244
245 /* "help" with other stuff, with implied key */
246 qdict = keyval_parse("val,help,foo=bar", "implied", &help, &error_abort);
247 g_assert_cmpuint(qdict_size(qdict), ==, 2);
248 g_assert_cmpstr(qdict_get_try_str(qdict, "implied"), ==, "val");
249 g_assert_cmpstr(qdict_get_try_str(qdict, "foo"), ==, "bar");
250 g_assert(help);
251 qobject_unref(qdict);
d454dbe0
MA
252}
253
0b2c1bee
MA
254static void check_list012(QList *qlist)
255{
256 static const char *expected[] = { "null", "eins", "zwei" };
257 int i;
258 QString *qstr;
259
260 g_assert(qlist);
261 for (i = 0; i < ARRAY_SIZE(expected); i++) {
7dc847eb 262 qstr = qobject_to(QString, qlist_pop(qlist));
0b2c1bee
MA
263 g_assert(qstr);
264 g_assert_cmpstr(qstring_get_str(qstr), ==, expected[i]);
cb3e7f08 265 qobject_unref(qstr);
0b2c1bee
MA
266 }
267 g_assert(qlist_empty(qlist));
268}
269
270static void test_keyval_parse_list(void)
271{
272 Error *err = NULL;
273 QDict *qdict, *sub_qdict;
274
275 /* Root can't be a list */
8bf12c4f 276 qdict = keyval_parse("0=1", NULL, NULL, &err);
0b2c1bee
MA
277 error_free_or_abort(&err);
278 g_assert(!qdict);
279
280 /* List elements need not be in order */
8bf12c4f
KW
281 qdict = keyval_parse("list.0=null,list.2=zwei,list.1=eins", NULL, NULL,
282 &error_abort);
0b2c1bee
MA
283 g_assert_cmpint(qdict_size(qdict), ==, 1);
284 check_list012(qdict_get_qlist(qdict, "list"));
cb3e7f08 285 qobject_unref(qdict);
0b2c1bee
MA
286
287 /* Multiple indexes, last one wins */
b2cd5b92 288 qdict = keyval_parse("list.1=goner,list.0=null,list.01=eins,list.2=zwei",
8bf12c4f 289 NULL, NULL, &error_abort);
0b2c1bee
MA
290 g_assert_cmpint(qdict_size(qdict), ==, 1);
291 check_list012(qdict_get_qlist(qdict, "list"));
cb3e7f08 292 qobject_unref(qdict);
0b2c1bee
MA
293
294 /* List at deeper nesting */
8bf12c4f 295 qdict = keyval_parse("a.list.1=eins,a.list.00=null,a.list.2=zwei", NULL,
0b2c1bee
MA
296 NULL, &error_abort);
297 g_assert_cmpint(qdict_size(qdict), ==, 1);
298 sub_qdict = qdict_get_qdict(qdict, "a");
299 g_assert_cmpint(qdict_size(sub_qdict), ==, 1);
300 check_list012(qdict_get_qlist(sub_qdict, "list"));
cb3e7f08 301 qobject_unref(qdict);
0b2c1bee
MA
302
303 /* Inconsistent dotted keys: both list and dictionary */
8bf12c4f 304 qdict = keyval_parse("a.b.c=1,a.b.0=2", NULL, NULL, &err);
0b2c1bee
MA
305 error_free_or_abort(&err);
306 g_assert(!qdict);
8bf12c4f 307 qdict = keyval_parse("a.0.c=1,a.b.c=2", NULL, NULL, &err);
0b2c1bee
MA
308 error_free_or_abort(&err);
309 g_assert(!qdict);
310
311 /* Missing list indexes */
8bf12c4f 312 qdict = keyval_parse("list.1=lonely", NULL, NULL, &err);
0b2c1bee
MA
313 error_free_or_abort(&err);
314 g_assert(!qdict);
8bf12c4f
KW
315 qdict = keyval_parse("list.0=null,list.2=eins,list.02=zwei", NULL, NULL,
316 &err);
0b2c1bee
MA
317 error_free_or_abort(&err);
318 g_assert(!qdict);
319}
320
9e3943f8
MA
321static void test_keyval_visit_bool(void)
322{
323 Error *err = NULL;
324 Visitor *v;
325 QDict *qdict;
326 bool b;
327
8bf12c4f 328 qdict = keyval_parse("bool1=on,bool2=off", NULL, NULL, &error_abort);
9e3943f8 329 v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
cb3e7f08 330 qobject_unref(qdict);
9e3943f8
MA
331 visit_start_struct(v, NULL, NULL, 0, &error_abort);
332 visit_type_bool(v, "bool1", &b, &error_abort);
333 g_assert(b);
334 visit_type_bool(v, "bool2", &b, &error_abort);
335 g_assert(!b);
336 visit_check_struct(v, &error_abort);
337 visit_end_struct(v, NULL);
338 visit_free(v);
339
8bf12c4f 340 qdict = keyval_parse("bool1=offer", NULL, NULL, &error_abort);
9e3943f8 341 v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
cb3e7f08 342 qobject_unref(qdict);
9e3943f8
MA
343 visit_start_struct(v, NULL, NULL, 0, &error_abort);
344 visit_type_bool(v, "bool1", &b, &err);
345 error_free_or_abort(&err);
346 visit_end_struct(v, NULL);
347 visit_free(v);
348}
349
350static void test_keyval_visit_number(void)
351{
352 Error *err = NULL;
353 Visitor *v;
354 QDict *qdict;
355 uint64_t u;
356
357 /* Lower limit zero */
8bf12c4f 358 qdict = keyval_parse("number1=0", NULL, NULL, &error_abort);
9e3943f8 359 v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
cb3e7f08 360 qobject_unref(qdict);
9e3943f8
MA
361 visit_start_struct(v, NULL, NULL, 0, &error_abort);
362 visit_type_uint64(v, "number1", &u, &error_abort);
363 g_assert_cmpuint(u, ==, 0);
364 visit_check_struct(v, &error_abort);
365 visit_end_struct(v, NULL);
366 visit_free(v);
367
368 /* Upper limit 2^64-1 */
8bf12c4f 369 qdict = keyval_parse("number1=18446744073709551615,number2=-1", NULL,
9e3943f8
MA
370 NULL, &error_abort);
371 v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
cb3e7f08 372 qobject_unref(qdict);
9e3943f8
MA
373 visit_start_struct(v, NULL, NULL, 0, &error_abort);
374 visit_type_uint64(v, "number1", &u, &error_abort);
375 g_assert_cmphex(u, ==, UINT64_MAX);
376 visit_type_uint64(v, "number2", &u, &error_abort);
377 g_assert_cmphex(u, ==, UINT64_MAX);
378 visit_check_struct(v, &error_abort);
379 visit_end_struct(v, NULL);
380 visit_free(v);
381
382 /* Above upper limit */
8bf12c4f
KW
383 qdict = keyval_parse("number1=18446744073709551616", NULL, NULL,
384 &error_abort);
9e3943f8 385 v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
cb3e7f08 386 qobject_unref(qdict);
9e3943f8
MA
387 visit_start_struct(v, NULL, NULL, 0, &error_abort);
388 visit_type_uint64(v, "number1", &u, &err);
389 error_free_or_abort(&err);
390 visit_end_struct(v, NULL);
391 visit_free(v);
392
393 /* Below lower limit */
8bf12c4f
KW
394 qdict = keyval_parse("number1=-18446744073709551616", NULL, NULL,
395 &error_abort);
9e3943f8 396 v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
cb3e7f08 397 qobject_unref(qdict);
9e3943f8
MA
398 visit_start_struct(v, NULL, NULL, 0, &error_abort);
399 visit_type_uint64(v, "number1", &u, &err);
400 error_free_or_abort(&err);
401 visit_end_struct(v, NULL);
402 visit_free(v);
403
404 /* Hex and octal */
8bf12c4f 405 qdict = keyval_parse("number1=0x2a,number2=052", NULL, NULL, &error_abort);
9e3943f8 406 v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
cb3e7f08 407 qobject_unref(qdict);
9e3943f8
MA
408 visit_start_struct(v, NULL, NULL, 0, &error_abort);
409 visit_type_uint64(v, "number1", &u, &error_abort);
410 g_assert_cmpuint(u, ==, 42);
411 visit_type_uint64(v, "number2", &u, &error_abort);
412 g_assert_cmpuint(u, ==, 42);
413 visit_check_struct(v, &error_abort);
414 visit_end_struct(v, NULL);
415 visit_free(v);
416
417 /* Trailing crap */
8bf12c4f 418 qdict = keyval_parse("number1=3.14,number2=08", NULL, NULL, &error_abort);
9e3943f8 419 v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
cb3e7f08 420 qobject_unref(qdict);
9e3943f8
MA
421 visit_start_struct(v, NULL, NULL, 0, &error_abort);
422 visit_type_uint64(v, "number1", &u, &err);
423 error_free_or_abort(&err);
424 visit_type_uint64(v, "number2", &u, &err);
425 error_free_or_abort(&err);
426 visit_end_struct(v, NULL);
427 visit_free(v);
428}
429
430static void test_keyval_visit_size(void)
431{
432 Error *err = NULL;
433 Visitor *v;
434 QDict *qdict;
435 uint64_t sz;
436
437 /* Lower limit zero */
8bf12c4f 438 qdict = keyval_parse("sz1=0", NULL, NULL, &error_abort);
9e3943f8 439 v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
cb3e7f08 440 qobject_unref(qdict);
9e3943f8
MA
441 visit_start_struct(v, NULL, NULL, 0, &error_abort);
442 visit_type_size(v, "sz1", &sz, &error_abort);
443 g_assert_cmpuint(sz, ==, 0);
444 visit_check_struct(v, &error_abort);
445 visit_end_struct(v, NULL);
446 visit_free(v);
447
448 /* Note: precision is 53 bits since we're parsing with strtod() */
449
450 /* Around limit of precision: 2^53-1, 2^53, 2^53+1 */
451 qdict = keyval_parse("sz1=9007199254740991,"
452 "sz2=9007199254740992,"
453 "sz3=9007199254740993",
8bf12c4f 454 NULL, NULL, &error_abort);
9e3943f8 455 v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
cb3e7f08 456 qobject_unref(qdict);
9e3943f8
MA
457 visit_start_struct(v, NULL, NULL, 0, &error_abort);
458 visit_type_size(v, "sz1", &sz, &error_abort);
459 g_assert_cmphex(sz, ==, 0x1fffffffffffff);
460 visit_type_size(v, "sz2", &sz, &error_abort);
461 g_assert_cmphex(sz, ==, 0x20000000000000);
462 visit_type_size(v, "sz3", &sz, &error_abort);
463 g_assert_cmphex(sz, ==, 0x20000000000000);
464 visit_check_struct(v, &error_abort);
465 visit_end_struct(v, NULL);
466 visit_free(v);
467
468 /* Close to signed upper limit 0x7ffffffffffffc00 (53 msbs set) */
469 qdict = keyval_parse("sz1=9223372036854774784," /* 7ffffffffffffc00 */
470 "sz2=9223372036854775295", /* 7ffffffffffffdff */
8bf12c4f 471 NULL, NULL, &error_abort);
9e3943f8 472 v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
cb3e7f08 473 qobject_unref(qdict);
9e3943f8
MA
474 visit_start_struct(v, NULL, NULL, 0, &error_abort);
475 visit_type_size(v, "sz1", &sz, &error_abort);
476 g_assert_cmphex(sz, ==, 0x7ffffffffffffc00);
477 visit_type_size(v, "sz2", &sz, &error_abort);
478 g_assert_cmphex(sz, ==, 0x7ffffffffffffc00);
479 visit_check_struct(v, &error_abort);
480 visit_end_struct(v, NULL);
481 visit_free(v);
482
483 /* Close to actual upper limit 0xfffffffffffff800 (53 msbs set) */
484 qdict = keyval_parse("sz1=18446744073709549568," /* fffffffffffff800 */
485 "sz2=18446744073709550591", /* fffffffffffffbff */
8bf12c4f 486 NULL, NULL, &error_abort);
9e3943f8 487 v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
cb3e7f08 488 qobject_unref(qdict);
9e3943f8
MA
489 visit_start_struct(v, NULL, NULL, 0, &error_abort);
490 visit_type_size(v, "sz1", &sz, &error_abort);
491 g_assert_cmphex(sz, ==, 0xfffffffffffff800);
492 visit_type_size(v, "sz2", &sz, &error_abort);
493 g_assert_cmphex(sz, ==, 0xfffffffffffff800);
494 visit_check_struct(v, &error_abort);
495 visit_end_struct(v, NULL);
496 visit_free(v);
497
498 /* Beyond limits */
499 qdict = keyval_parse("sz1=-1,"
500 "sz2=18446744073709550592", /* fffffffffffffc00 */
8bf12c4f 501 NULL, NULL, &error_abort);
9e3943f8 502 v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
cb3e7f08 503 qobject_unref(qdict);
9e3943f8
MA
504 visit_start_struct(v, NULL, NULL, 0, &error_abort);
505 visit_type_size(v, "sz1", &sz, &err);
506 error_free_or_abort(&err);
507 visit_type_size(v, "sz2", &sz, &err);
508 error_free_or_abort(&err);
509 visit_end_struct(v, NULL);
510 visit_free(v);
511
512 /* Suffixes */
513 qdict = keyval_parse("sz1=8b,sz2=1.5k,sz3=2M,sz4=0.1G,sz5=16777215T",
8bf12c4f 514 NULL, NULL, &error_abort);
9e3943f8 515 v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
cb3e7f08 516 qobject_unref(qdict);
9e3943f8
MA
517 visit_start_struct(v, NULL, NULL, 0, &error_abort);
518 visit_type_size(v, "sz1", &sz, &error_abort);
519 g_assert_cmpuint(sz, ==, 8);
520 visit_type_size(v, "sz2", &sz, &error_abort);
521 g_assert_cmpuint(sz, ==, 1536);
522 visit_type_size(v, "sz3", &sz, &error_abort);
d23b6caa 523 g_assert_cmphex(sz, ==, 2 * MiB);
9e3943f8 524 visit_type_size(v, "sz4", &sz, &error_abort);
d23b6caa 525 g_assert_cmphex(sz, ==, GiB / 10);
9e3943f8 526 visit_type_size(v, "sz5", &sz, &error_abort);
d23b6caa 527 g_assert_cmphex(sz, ==, 16777215ULL * TiB);
9e3943f8
MA
528 visit_check_struct(v, &error_abort);
529 visit_end_struct(v, NULL);
530 visit_free(v);
531
532 /* Beyond limit with suffix */
8bf12c4f 533 qdict = keyval_parse("sz1=16777216T", NULL, NULL, &error_abort);
9e3943f8 534 v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
cb3e7f08 535 qobject_unref(qdict);
9e3943f8
MA
536 visit_start_struct(v, NULL, NULL, 0, &error_abort);
537 visit_type_size(v, "sz1", &sz, &err);
538 error_free_or_abort(&err);
539 visit_end_struct(v, NULL);
540 visit_free(v);
541
542 /* Trailing crap */
8bf12c4f 543 qdict = keyval_parse("sz1=0Z,sz2=16Gi", NULL, NULL, &error_abort);
9e3943f8 544 v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
cb3e7f08 545 qobject_unref(qdict);
9e3943f8
MA
546 visit_start_struct(v, NULL, NULL, 0, &error_abort);
547 visit_type_size(v, "sz1", &sz, &err);
548 error_free_or_abort(&err);
549 visit_type_size(v, "sz2", &sz, &err);
550 error_free_or_abort(&err);
551 visit_end_struct(v, NULL);
552 visit_free(v);
553}
554
555static void test_keyval_visit_dict(void)
556{
557 Error *err = NULL;
558 Visitor *v;
559 QDict *qdict;
560 int64_t i;
561
8bf12c4f 562 qdict = keyval_parse("a.b.c=1,a.b.c=2,d=3", NULL, NULL, &error_abort);
9e3943f8 563 v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
cb3e7f08 564 qobject_unref(qdict);
9e3943f8
MA
565 visit_start_struct(v, NULL, NULL, 0, &error_abort);
566 visit_start_struct(v, "a", NULL, 0, &error_abort);
567 visit_start_struct(v, "b", NULL, 0, &error_abort);
568 visit_type_int(v, "c", &i, &error_abort);
569 g_assert_cmpint(i, ==, 2);
570 visit_check_struct(v, &error_abort);
571 visit_end_struct(v, NULL);
572 visit_check_struct(v, &error_abort);
573 visit_end_struct(v, NULL);
574 visit_type_int(v, "d", &i, &error_abort);
575 g_assert_cmpint(i, ==, 3);
576 visit_check_struct(v, &error_abort);
577 visit_end_struct(v, NULL);
578 visit_free(v);
579
8bf12c4f 580 qdict = keyval_parse("a.b=", NULL, NULL, &error_abort);
9e3943f8 581 v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
cb3e7f08 582 qobject_unref(qdict);
9e3943f8
MA
583 visit_start_struct(v, NULL, NULL, 0, &error_abort);
584 visit_start_struct(v, "a", NULL, 0, &error_abort);
585 visit_type_int(v, "c", &i, &err); /* a.c missing */
586 error_free_or_abort(&err);
587 visit_check_struct(v, &err);
588 error_free_or_abort(&err); /* a.b unexpected */
589 visit_end_struct(v, NULL);
590 visit_check_struct(v, &error_abort);
591 visit_end_struct(v, NULL);
592 visit_free(v);
593}
594
0b2c1bee
MA
595static void test_keyval_visit_list(void)
596{
597 Error *err = NULL;
598 Visitor *v;
599 QDict *qdict;
600 char *s;
601
8bf12c4f 602 qdict = keyval_parse("a.0=,a.1=I,a.2.0=II", NULL, NULL, &error_abort);
0b2c1bee
MA
603 /* TODO empty list */
604 v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
cb3e7f08 605 qobject_unref(qdict);
0b2c1bee
MA
606 visit_start_struct(v, NULL, NULL, 0, &error_abort);
607 visit_start_list(v, "a", NULL, 0, &error_abort);
608 visit_type_str(v, NULL, &s, &error_abort);
609 g_assert_cmpstr(s, ==, "");
610 g_free(s);
611 visit_type_str(v, NULL, &s, &error_abort);
612 g_assert_cmpstr(s, ==, "I");
613 g_free(s);
614 visit_start_list(v, NULL, NULL, 0, &error_abort);
615 visit_type_str(v, NULL, &s, &error_abort);
616 g_assert_cmpstr(s, ==, "II");
617 g_free(s);
618 visit_check_list(v, &error_abort);
619 visit_end_list(v, NULL);
620 visit_check_list(v, &error_abort);
621 visit_end_list(v, NULL);
622 visit_check_struct(v, &error_abort);
623 visit_end_struct(v, NULL);
624 visit_free(v);
625
8bf12c4f 626 qdict = keyval_parse("a.0=,b.0.0=head", NULL, NULL, &error_abort);
0b2c1bee 627 v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
cb3e7f08 628 qobject_unref(qdict);
0b2c1bee
MA
629 visit_start_struct(v, NULL, NULL, 0, &error_abort);
630 visit_start_list(v, "a", NULL, 0, &error_abort);
631 visit_check_list(v, &err); /* a[0] unexpected */
632 error_free_or_abort(&err);
633 visit_end_list(v, NULL);
634 visit_start_list(v, "b", NULL, 0, &error_abort);
635 visit_start_list(v, NULL, NULL, 0, &error_abort);
636 visit_type_str(v, NULL, &s, &error_abort);
637 g_assert_cmpstr(s, ==, "head");
638 g_free(s);
639 visit_type_str(v, NULL, &s, &err); /* b[0][1] missing */
640 error_free_or_abort(&err);
641 visit_end_list(v, NULL);
642 visit_end_list(v, NULL);
643 visit_check_struct(v, &error_abort);
644 visit_end_struct(v, NULL);
645 visit_free(v);
646}
647
9e3943f8
MA
648static void test_keyval_visit_optional(void)
649{
650 Visitor *v;
651 QDict *qdict;
652 bool present;
653 int64_t i;
654
8bf12c4f 655 qdict = keyval_parse("a.b=1", NULL, NULL, &error_abort);
9e3943f8 656 v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
cb3e7f08 657 qobject_unref(qdict);
9e3943f8
MA
658 visit_start_struct(v, NULL, NULL, 0, &error_abort);
659 visit_optional(v, "b", &present);
660 g_assert(!present); /* b missing */
661 visit_optional(v, "a", &present);
662 g_assert(present); /* a present */
663 visit_start_struct(v, "a", NULL, 0, &error_abort);
664 visit_optional(v, "b", &present);
665 g_assert(present); /* a.b present */
666 visit_type_int(v, "b", &i, &error_abort);
667 g_assert_cmpint(i, ==, 1);
668 visit_optional(v, "a", &present);
669 g_assert(!present); /* a.a missing */
670 visit_check_struct(v, &error_abort);
671 visit_end_struct(v, NULL);
672 visit_check_struct(v, &error_abort);
673 visit_end_struct(v, NULL);
674 visit_free(v);
675}
676
599c156b
MA
677static void test_keyval_visit_alternate(void)
678{
679 Error *err = NULL;
680 Visitor *v;
681 QDict *qdict;
c0644771 682 AltStrObj *aso;
7c877c80 683 AltNumEnum *ane;
c0644771 684 AltEnumBool *aeb;
599c156b
MA
685
686 /*
687 * Can't do scalar alternate variants other than string. You get
688 * the string variant if there is one, else an error.
c0644771 689 * TODO make it work for unambiguous cases like AltEnumBool below
599c156b 690 */
8bf12c4f 691 qdict = keyval_parse("a=1,b=2,c=on", NULL, NULL, &error_abort);
599c156b 692 v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
cb3e7f08 693 qobject_unref(qdict);
599c156b 694 visit_start_struct(v, NULL, NULL, 0, &error_abort);
c0644771
MA
695 visit_type_AltStrObj(v, "a", &aso, &error_abort);
696 g_assert_cmpint(aso->type, ==, QTYPE_QSTRING);
697 g_assert_cmpstr(aso->u.s, ==, "1");
698 qapi_free_AltStrObj(aso);
7c877c80 699 visit_type_AltNumEnum(v, "b", &ane, &err);
c0644771
MA
700 error_free_or_abort(&err);
701 visit_type_AltEnumBool(v, "c", &aeb, &err);
599c156b
MA
702 error_free_or_abort(&err);
703 visit_end_struct(v, NULL);
704 visit_free(v);
705}
706
707static void test_keyval_visit_any(void)
708{
709 Visitor *v;
710 QDict *qdict;
711 QObject *any;
712 QList *qlist;
713 QString *qstr;
714
8bf12c4f 715 qdict = keyval_parse("a.0=null,a.1=1", NULL, NULL, &error_abort);
599c156b 716 v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
cb3e7f08 717 qobject_unref(qdict);
599c156b
MA
718 visit_start_struct(v, NULL, NULL, 0, &error_abort);
719 visit_type_any(v, "a", &any, &error_abort);
7dc847eb 720 qlist = qobject_to(QList, any);
599c156b 721 g_assert(qlist);
7dc847eb 722 qstr = qobject_to(QString, qlist_pop(qlist));
599c156b 723 g_assert_cmpstr(qstring_get_str(qstr), ==, "null");
cb3e7f08 724 qobject_unref(qstr);
7dc847eb 725 qstr = qobject_to(QString, qlist_pop(qlist));
599c156b
MA
726 g_assert_cmpstr(qstring_get_str(qstr), ==, "1");
727 g_assert(qlist_empty(qlist));
cb3e7f08
MAL
728 qobject_unref(qstr);
729 qobject_unref(any);
599c156b
MA
730 visit_check_struct(v, &error_abort);
731 visit_end_struct(v, NULL);
732 visit_free(v);
733}
734
d454dbe0
MA
735int main(int argc, char *argv[])
736{
737 g_test_init(&argc, &argv, NULL);
738 g_test_add_func("/keyval/keyval_parse", test_keyval_parse);
0b2c1bee 739 g_test_add_func("/keyval/keyval_parse/list", test_keyval_parse_list);
9e3943f8
MA
740 g_test_add_func("/keyval/visit/bool", test_keyval_visit_bool);
741 g_test_add_func("/keyval/visit/number", test_keyval_visit_number);
742 g_test_add_func("/keyval/visit/size", test_keyval_visit_size);
743 g_test_add_func("/keyval/visit/dict", test_keyval_visit_dict);
0b2c1bee 744 g_test_add_func("/keyval/visit/list", test_keyval_visit_list);
9e3943f8 745 g_test_add_func("/keyval/visit/optional", test_keyval_visit_optional);
599c156b
MA
746 g_test_add_func("/keyval/visit/alternate", test_keyval_visit_alternate);
747 g_test_add_func("/keyval/visit/any", test_keyval_visit_any);
d454dbe0
MA
748 g_test_run();
749 return 0;
750}