2 * Unit tests for parsing of KEY=VALUE,... strings
4 * Copyright (C) 2017 Red Hat Inc.
7 * Markus Armbruster <armbru@redhat.com>,
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"
14 #include "qemu/units.h"
15 #include "qapi/error.h"
16 #include "qapi/qmp/qdict.h"
17 #include "qapi/qmp/qlist.h"
18 #include "qapi/qmp/qstring.h"
19 #include "qapi/qobject-input-visitor.h"
20 #include "test-qapi-visit.h"
21 #include "qemu/cutils.h"
22 #include "qemu/option.h"
24 static void test_keyval_parse(void)
27 QDict
*qdict
, *sub_qdict
;
33 qdict
= keyval_parse("", NULL
, NULL
, &error_abort
);
34 g_assert_cmpuint(qdict_size(qdict
), ==, 0);
37 /* Empty key (qemu_opts_parse() accepts this) */
38 qdict
= keyval_parse("=val", NULL
, NULL
, &err
);
39 error_free_or_abort(&err
);
42 /* Empty key fragment */
43 qdict
= keyval_parse(".", NULL
, NULL
, &err
);
44 error_free_or_abort(&err
);
46 qdict
= keyval_parse("key.", NULL
, NULL
, &err
);
47 error_free_or_abort(&err
);
50 /* Invalid non-empty key (qemu_opts_parse() doesn't care) */
51 qdict
= keyval_parse("7up=val", NULL
, NULL
, &err
);
52 error_free_or_abort(&err
);
56 memset(long_key
, 'a', 127);
59 params
= g_strdup_printf("k.%s=v", long_key
);
60 qdict
= keyval_parse(params
+ 2, NULL
, NULL
, &err
);
61 error_free_or_abort(&err
);
64 /* Overlong key fragment */
65 qdict
= keyval_parse(params
, NULL
, NULL
, &err
);
66 error_free_or_abort(&err
);
70 /* Long key (qemu_opts_parse() accepts and truncates silently) */
71 params
= g_strdup_printf("k.%s=v", long_key
+ 1);
72 qdict
= keyval_parse(params
+ 2, NULL
, NULL
, &error_abort
);
73 g_assert_cmpuint(qdict_size(qdict
), ==, 1);
74 g_assert_cmpstr(qdict_get_try_str(qdict
, long_key
+ 1), ==, "v");
77 /* Long key fragment */
78 qdict
= keyval_parse(params
, NULL
, NULL
, &error_abort
);
79 g_assert_cmpuint(qdict_size(qdict
), ==, 1);
80 sub_qdict
= qdict_get_qdict(qdict
, "k");
82 g_assert_cmpuint(qdict_size(sub_qdict
), ==, 1);
83 g_assert_cmpstr(qdict_get_try_str(sub_qdict
, long_key
+ 1), ==, "v");
87 /* Crap after valid key */
88 qdict
= keyval_parse("key[0]=val", NULL
, NULL
, &err
);
89 error_free_or_abort(&err
);
92 /* Multiple keys, last one wins */
93 qdict
= keyval_parse("a=1,b=2,,x,a=3", NULL
, NULL
, &error_abort
);
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");
99 /* Even when it doesn't in qemu_opts_parse() */
100 qdict
= keyval_parse("id=foo,id=bar", NULL
, NULL
, &error_abort
);
101 g_assert_cmpuint(qdict_size(qdict
), ==, 1);
102 g_assert_cmpstr(qdict_get_try_str(qdict
, "id"), ==, "bar");
103 qobject_unref(qdict
);
106 qdict
= keyval_parse("a.b.c=1,a.b.c=2,d=3", NULL
, NULL
, &error_abort
);
107 g_assert_cmpuint(qdict_size(qdict
), ==, 2);
108 sub_qdict
= qdict_get_qdict(qdict
, "a");
110 g_assert_cmpuint(qdict_size(sub_qdict
), ==, 1);
111 sub_qdict
= qdict_get_qdict(sub_qdict
, "b");
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");
116 qobject_unref(qdict
);
118 /* Inconsistent dotted keys */
119 qdict
= keyval_parse("a.b=1,a=2", NULL
, NULL
, &err
);
120 error_free_or_abort(&err
);
122 qdict
= keyval_parse("a.b=1,a.b.c=2", NULL
, NULL
, &err
);
123 error_free_or_abort(&err
);
126 /* Trailing comma is ignored */
127 qdict
= keyval_parse("x=y,", NULL
, NULL
, &error_abort
);
128 g_assert_cmpuint(qdict_size(qdict
), ==, 1);
129 g_assert_cmpstr(qdict_get_try_str(qdict
, "x"), ==, "y");
130 qobject_unref(qdict
);
132 /* Except when it isn't */
133 qdict
= keyval_parse(",", NULL
, NULL
, &err
);
134 error_free_or_abort(&err
);
137 /* Value containing ,id= not misinterpreted as qemu_opts_parse() does */
138 qdict
= keyval_parse("x=,,id=bar", NULL
, NULL
, &error_abort
);
139 g_assert_cmpuint(qdict_size(qdict
), ==, 1);
140 g_assert_cmpstr(qdict_get_try_str(qdict
, "x"), ==, ",id=bar");
141 qobject_unref(qdict
);
143 /* Anti-social ID is left to caller (qemu_opts_parse() rejects it) */
144 qdict
= keyval_parse("id=666", NULL
, NULL
, &error_abort
);
145 g_assert_cmpuint(qdict_size(qdict
), ==, 1);
146 g_assert_cmpstr(qdict_get_try_str(qdict
, "id"), ==, "666");
147 qobject_unref(qdict
);
149 /* Implied value not supported (unlike qemu_opts_parse()) */
150 qdict
= keyval_parse("an,noaus,noaus=", NULL
, NULL
, &err
);
151 error_free_or_abort(&err
);
154 /* Implied value, key "no" (qemu_opts_parse(): negated empty key) */
155 qdict
= keyval_parse("no", NULL
, NULL
, &err
);
156 error_free_or_abort(&err
);
160 qdict
= keyval_parse("an,aus=off,noaus=", "implied", NULL
, &error_abort
);
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"), ==, "");
165 qobject_unref(qdict
);
167 /* Implied dotted key */
168 qdict
= keyval_parse("val", "eins.zwei", NULL
, &error_abort
);
169 g_assert_cmpuint(qdict_size(qdict
), ==, 1);
170 sub_qdict
= qdict_get_qdict(qdict
, "eins");
172 g_assert_cmpuint(qdict_size(sub_qdict
), ==, 1);
173 g_assert_cmpstr(qdict_get_try_str(sub_qdict
, "zwei"), ==, "val");
174 qobject_unref(qdict
);
176 /* Implied key with empty value (qemu_opts_parse() accepts this) */
177 qdict
= keyval_parse(",", "implied", NULL
, &err
);
178 error_free_or_abort(&err
);
181 /* Likewise (qemu_opts_parse(): implied key with comma value) */
182 qdict
= keyval_parse(",,,a=1", "implied", NULL
, &err
);
183 error_free_or_abort(&err
);
186 /* Implied key's value can't have comma (qemu_opts_parse(): it can) */
187 qdict
= keyval_parse("val,,ue", "implied", NULL
, &err
);
188 error_free_or_abort(&err
);
191 /* Empty key is not an implied key */
192 qdict
= keyval_parse("=val", "implied", NULL
, &err
);
193 error_free_or_abort(&err
);
196 /* "help" by itself, without implied key */
197 qdict
= keyval_parse("help", NULL
, &help
, &error_abort
);
198 g_assert_cmpuint(qdict_size(qdict
), ==, 0);
200 qobject_unref(qdict
);
202 /* "help" by itself, with implied key */
203 qdict
= keyval_parse("help", "implied", &help
, &error_abort
);
204 g_assert_cmpuint(qdict_size(qdict
), ==, 0);
206 qobject_unref(qdict
);
208 /* "help" when no help is available, without implied key */
209 qdict
= keyval_parse("help", NULL
, NULL
, &err
);
210 error_free_or_abort(&err
);
213 /* "help" when no help is available, with implied key */
214 qdict
= keyval_parse("help", "implied", NULL
, &err
);
215 error_free_or_abort(&err
);
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");
223 qobject_unref(qdict
);
225 /* "help" followed by crap, without implied key */
226 qdict
= keyval_parse("help.abc", NULL
, &help
, &err
);
227 error_free_or_abort(&err
);
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");
235 qobject_unref(qdict
);
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");
243 qobject_unref(qdict
);
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");
251 qobject_unref(qdict
);
254 static void check_list012(QList
*qlist
)
256 static const char *expected
[] = { "null", "eins", "zwei" };
261 for (i
= 0; i
< ARRAY_SIZE(expected
); i
++) {
262 qstr
= qobject_to(QString
, qlist_pop(qlist
));
264 g_assert_cmpstr(qstring_get_str(qstr
), ==, expected
[i
]);
267 g_assert(qlist_empty(qlist
));
270 static void test_keyval_parse_list(void)
273 QDict
*qdict
, *sub_qdict
;
275 /* Root can't be a list */
276 qdict
= keyval_parse("0=1", NULL
, NULL
, &err
);
277 error_free_or_abort(&err
);
280 /* List elements need not be in order */
281 qdict
= keyval_parse("list.0=null,list.2=zwei,list.1=eins", NULL
, NULL
,
283 g_assert_cmpint(qdict_size(qdict
), ==, 1);
284 check_list012(qdict_get_qlist(qdict
, "list"));
285 qobject_unref(qdict
);
287 /* Multiple indexes, last one wins */
288 qdict
= keyval_parse("list.1=goner,list.0=null,list.01=eins,list.2=zwei",
289 NULL
, NULL
, &error_abort
);
290 g_assert_cmpint(qdict_size(qdict
), ==, 1);
291 check_list012(qdict_get_qlist(qdict
, "list"));
292 qobject_unref(qdict
);
294 /* List at deeper nesting */
295 qdict
= keyval_parse("a.list.1=eins,a.list.00=null,a.list.2=zwei", NULL
,
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"));
301 qobject_unref(qdict
);
303 /* Inconsistent dotted keys: both list and dictionary */
304 qdict
= keyval_parse("a.b.c=1,a.b.0=2", NULL
, NULL
, &err
);
305 error_free_or_abort(&err
);
307 qdict
= keyval_parse("a.0.c=1,a.b.c=2", NULL
, NULL
, &err
);
308 error_free_or_abort(&err
);
311 /* Missing list indexes */
312 qdict
= keyval_parse("list.1=lonely", NULL
, NULL
, &err
);
313 error_free_or_abort(&err
);
315 qdict
= keyval_parse("list.0=null,list.2=eins,list.02=zwei", NULL
, NULL
,
317 error_free_or_abort(&err
);
321 static void test_keyval_visit_bool(void)
328 qdict
= keyval_parse("bool1=on,bool2=off", NULL
, NULL
, &error_abort
);
329 v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
330 qobject_unref(qdict
);
331 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
332 visit_type_bool(v
, "bool1", &b
, &error_abort
);
334 visit_type_bool(v
, "bool2", &b
, &error_abort
);
336 visit_check_struct(v
, &error_abort
);
337 visit_end_struct(v
, NULL
);
340 qdict
= keyval_parse("bool1=offer", NULL
, NULL
, &error_abort
);
341 v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
342 qobject_unref(qdict
);
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
);
350 static void test_keyval_visit_number(void)
357 /* Lower limit zero */
358 qdict
= keyval_parse("number1=0", NULL
, NULL
, &error_abort
);
359 v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
360 qobject_unref(qdict
);
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
);
368 /* Upper limit 2^64-1 */
369 qdict
= keyval_parse("number1=18446744073709551615,number2=-1", NULL
,
371 v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
372 qobject_unref(qdict
);
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
);
382 /* Above upper limit */
383 qdict
= keyval_parse("number1=18446744073709551616", NULL
, NULL
,
385 v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
386 qobject_unref(qdict
);
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
);
393 /* Below lower limit */
394 qdict
= keyval_parse("number1=-18446744073709551616", NULL
, NULL
,
396 v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
397 qobject_unref(qdict
);
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
);
405 qdict
= keyval_parse("number1=0x2a,number2=052", NULL
, NULL
, &error_abort
);
406 v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
407 qobject_unref(qdict
);
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
);
418 qdict
= keyval_parse("number1=3.14,number2=08", NULL
, NULL
, &error_abort
);
419 v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
420 qobject_unref(qdict
);
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
);
430 static void test_keyval_visit_size(void)
437 /* Lower limit zero */
438 qdict
= keyval_parse("sz1=0", NULL
, NULL
, &error_abort
);
439 v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
440 qobject_unref(qdict
);
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
);
448 /* Note: precision is 53 bits since we're parsing with strtod() */
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",
454 NULL
, NULL
, &error_abort
);
455 v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
456 qobject_unref(qdict
);
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
);
468 /* Close to signed upper limit 0x7ffffffffffffc00 (53 msbs set) */
469 qdict
= keyval_parse("sz1=9223372036854774784," /* 7ffffffffffffc00 */
470 "sz2=9223372036854775295", /* 7ffffffffffffdff */
471 NULL
, NULL
, &error_abort
);
472 v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
473 qobject_unref(qdict
);
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
);
483 /* Close to actual upper limit 0xfffffffffffff800 (53 msbs set) */
484 qdict
= keyval_parse("sz1=18446744073709549568," /* fffffffffffff800 */
485 "sz2=18446744073709550591", /* fffffffffffffbff */
486 NULL
, NULL
, &error_abort
);
487 v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
488 qobject_unref(qdict
);
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
);
499 qdict
= keyval_parse("sz1=-1,"
500 "sz2=18446744073709550592", /* fffffffffffffc00 */
501 NULL
, NULL
, &error_abort
);
502 v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
503 qobject_unref(qdict
);
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
);
513 qdict
= keyval_parse("sz1=8b,sz2=1.5k,sz3=2M,sz4=0.1G,sz5=16777215T",
514 NULL
, NULL
, &error_abort
);
515 v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
516 qobject_unref(qdict
);
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
);
523 g_assert_cmphex(sz
, ==, 2 * MiB
);
524 visit_type_size(v
, "sz4", &sz
, &error_abort
);
525 g_assert_cmphex(sz
, ==, GiB
/ 10);
526 visit_type_size(v
, "sz5", &sz
, &error_abort
);
527 g_assert_cmphex(sz
, ==, 16777215ULL * TiB
);
528 visit_check_struct(v
, &error_abort
);
529 visit_end_struct(v
, NULL
);
532 /* Beyond limit with suffix */
533 qdict
= keyval_parse("sz1=16777216T", NULL
, NULL
, &error_abort
);
534 v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
535 qobject_unref(qdict
);
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
);
543 qdict
= keyval_parse("sz1=0Z,sz2=16Gi", NULL
, NULL
, &error_abort
);
544 v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
545 qobject_unref(qdict
);
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
);
555 static void test_keyval_visit_dict(void)
562 qdict
= keyval_parse("a.b.c=1,a.b.c=2,d=3", NULL
, NULL
, &error_abort
);
563 v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
564 qobject_unref(qdict
);
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
);
580 qdict
= keyval_parse("a.b=", NULL
, NULL
, &error_abort
);
581 v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
582 qobject_unref(qdict
);
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
);
595 static void test_keyval_visit_list(void)
602 qdict
= keyval_parse("a.0=,a.1=I,a.2.0=II", NULL
, NULL
, &error_abort
);
603 /* TODO empty list */
604 v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
605 qobject_unref(qdict
);
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
, ==, "");
611 visit_type_str(v
, NULL
, &s
, &error_abort
);
612 g_assert_cmpstr(s
, ==, "I");
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");
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
);
626 qdict
= keyval_parse("a.0=,b.0.0=head", NULL
, NULL
, &error_abort
);
627 v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
628 qobject_unref(qdict
);
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");
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
);
648 static void test_keyval_visit_optional(void)
655 qdict
= keyval_parse("a.b=1", NULL
, NULL
, &error_abort
);
656 v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
657 qobject_unref(qdict
);
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
);
677 static void test_keyval_visit_alternate(void)
687 * Can't do scalar alternate variants other than string. You get
688 * the string variant if there is one, else an error.
689 * TODO make it work for unambiguous cases like AltEnumBool below
691 qdict
= keyval_parse("a=1,b=2,c=on", NULL
, NULL
, &error_abort
);
692 v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
693 qobject_unref(qdict
);
694 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
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
);
699 visit_type_AltNumEnum(v
, "b", &ane
, &err
);
700 error_free_or_abort(&err
);
701 visit_type_AltEnumBool(v
, "c", &aeb
, &err
);
702 error_free_or_abort(&err
);
703 visit_end_struct(v
, NULL
);
707 static void test_keyval_visit_any(void)
715 qdict
= keyval_parse("a.0=null,a.1=1", NULL
, NULL
, &error_abort
);
716 v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
717 qobject_unref(qdict
);
718 visit_start_struct(v
, NULL
, NULL
, 0, &error_abort
);
719 visit_type_any(v
, "a", &any
, &error_abort
);
720 qlist
= qobject_to(QList
, any
);
722 qstr
= qobject_to(QString
, qlist_pop(qlist
));
723 g_assert_cmpstr(qstring_get_str(qstr
), ==, "null");
725 qstr
= qobject_to(QString
, qlist_pop(qlist
));
726 g_assert_cmpstr(qstring_get_str(qstr
), ==, "1");
727 g_assert(qlist_empty(qlist
));
730 visit_check_struct(v
, &error_abort
);
731 visit_end_struct(v
, NULL
);
735 int main(int argc
, char *argv
[])
737 g_test_init(&argc
, &argv
, NULL
);
738 g_test_add_func("/keyval/keyval_parse", test_keyval_parse
);
739 g_test_add_func("/keyval/keyval_parse/list", test_keyval_parse_list
);
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
);
744 g_test_add_func("/keyval/visit/list", test_keyval_visit_list
);
745 g_test_add_func("/keyval/visit/optional", test_keyval_visit_optional
);
746 g_test_add_func("/keyval/visit/alternate", test_keyval_visit_alternate
);
747 g_test_add_func("/keyval/visit/any", test_keyval_visit_any
);