6 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
7 * See the COPYING.LIB file in the top-level directory.
10 #include "qemu/osdep.h"
11 #include "qemu/units.h"
12 #include "qemu/option.h"
13 #include "qemu/option_int.h"
14 #include "qapi/error.h"
15 #include "qapi/qmp/qdict.h"
16 #include "qapi/qmp/qstring.h"
17 #include "qemu/config-file.h"
20 static QemuOptsList opts_list_01 = {
21 .name = "opts_list_01",
22 .head = QTAILQ_HEAD_INITIALIZER(opts_list_01.head),
26 .type = QEMU_OPT_STRING,
27 .help = "Help texts are preserved in qemu_opts_append",
28 .def_value_str = "default",
31 .type = QEMU_OPT_STRING,
34 .type = QEMU_OPT_STRING,
37 .type = QEMU_OPT_NUMBER,
38 .help = "Having help texts only for some options is okay",
41 .type = QEMU_OPT_NUMBER,
47 static QemuOptsList opts_list_02 = {
48 .name = "opts_list_02",
49 .head = QTAILQ_HEAD_INITIALIZER(opts_list_02.head),
53 .type = QEMU_OPT_STRING,
56 .type = QEMU_OPT_STRING,
59 .type = QEMU_OPT_BOOL,
62 .type = QEMU_OPT_BOOL,
65 .type = QEMU_OPT_SIZE,
68 .type = QEMU_OPT_SIZE,
71 .type = QEMU_OPT_SIZE,
77 static QemuOptsList opts_list_03 = {
78 .name = "opts_list_03",
79 .implied_opt_name = "implied",
80 .head = QTAILQ_HEAD_INITIALIZER(opts_list_03.head),
82 /* no elements => accept any params */
87 static QemuOptsList opts_list_04 = {
88 .name = "opts_list_04",
89 .head = QTAILQ_HEAD_INITIALIZER(opts_list_04.head),
94 .type = QEMU_OPT_STRING,
100 static void register_opts(void)
102 qemu_add_opts(&opts_list_01);
103 qemu_add_opts(&opts_list_02);
104 qemu_add_opts(&opts_list_03);
105 qemu_add_opts(&opts_list_04);
108 static void test_find_unknown_opts(void)
113 /* should not return anything, we don't have an "unknown" option */
114 list = qemu_find_opts_err("unknown", &err);
115 g_assert(list == NULL);
116 error_free_or_abort(&err);
119 static void test_qemu_find_opts(void)
123 /* we have an "opts_list_01" option, should return it */
124 list = qemu_find_opts("opts_list_01");
125 g_assert(list != NULL);
126 g_assert_cmpstr(list->name, ==, "opts_list_01");
129 static void test_qemu_opts_create(void)
134 list = qemu_find_opts("opts_list_01");
135 g_assert(list != NULL);
136 g_assert(QTAILQ_EMPTY(&list->head));
137 g_assert_cmpstr(list->name, ==, "opts_list_01");
139 /* should not find anything at this point */
140 opts = qemu_opts_find(list, NULL);
141 g_assert(opts == NULL);
143 /* create the opts */
144 opts = qemu_opts_create(list, NULL, 0, &error_abort);
145 g_assert(opts != NULL);
146 g_assert(!QTAILQ_EMPTY(&list->head));
148 /* now we've create the opts, must find it */
149 opts = qemu_opts_find(list, NULL);
150 g_assert(opts != NULL);
154 /* should not find anything at this point */
155 opts = qemu_opts_find(list, NULL);
156 g_assert(opts == NULL);
159 static void test_qemu_opt_get(void)
163 const char *opt = NULL;
165 list = qemu_find_opts("opts_list_01");
166 g_assert(list != NULL);
167 g_assert(QTAILQ_EMPTY(&list->head));
168 g_assert_cmpstr(list->name, ==, "opts_list_01");
170 /* should not find anything at this point */
171 opts = qemu_opts_find(list, NULL);
172 g_assert(opts == NULL);
174 /* create the opts */
175 opts = qemu_opts_create(list, NULL, 0, &error_abort);
176 g_assert(opts != NULL);
177 g_assert(!QTAILQ_EMPTY(&list->head));
179 /* haven't set anything to str2 yet */
180 opt = qemu_opt_get(opts, "str2");
181 g_assert(opt == NULL);
183 qemu_opt_set(opts, "str2", "value", &error_abort);
185 /* now we have set str2, should know about it */
186 opt = qemu_opt_get(opts, "str2");
187 g_assert_cmpstr(opt, ==, "value");
189 qemu_opt_set(opts, "str2", "value2", &error_abort);
191 /* having reset the value, the returned should be the reset one */
192 opt = qemu_opt_get(opts, "str2");
193 g_assert_cmpstr(opt, ==, "value2");
197 /* should not find anything at this point */
198 opts = qemu_opts_find(list, NULL);
199 g_assert(opts == NULL);
202 static void test_qemu_opt_get_bool(void)
208 list = qemu_find_opts("opts_list_02");
209 g_assert(list != NULL);
210 g_assert(QTAILQ_EMPTY(&list->head));
211 g_assert_cmpstr(list->name, ==, "opts_list_02");
213 /* should not find anything at this point */
214 opts = qemu_opts_find(list, NULL);
215 g_assert(opts == NULL);
217 /* create the opts */
218 opts = qemu_opts_create(list, NULL, 0, &error_abort);
219 g_assert(opts != NULL);
220 g_assert(!QTAILQ_EMPTY(&list->head));
222 /* haven't set anything to bool1 yet, so defval should be returned */
223 opt = qemu_opt_get_bool(opts, "bool1", false);
224 g_assert(opt == false);
226 qemu_opt_set_bool(opts, "bool1", true, &error_abort);
228 /* now we have set bool1, should know about it */
229 opt = qemu_opt_get_bool(opts, "bool1", false);
230 g_assert(opt == true);
232 /* having reset the value, opt should be the reset one not defval */
233 qemu_opt_set_bool(opts, "bool1", false, &error_abort);
235 opt = qemu_opt_get_bool(opts, "bool1", true);
236 g_assert(opt == false);
240 /* should not find anything at this point */
241 opts = qemu_opts_find(list, NULL);
242 g_assert(opts == NULL);
245 static void test_qemu_opt_get_number(void)
251 list = qemu_find_opts("opts_list_01");
252 g_assert(list != NULL);
253 g_assert(QTAILQ_EMPTY(&list->head));
254 g_assert_cmpstr(list->name, ==, "opts_list_01");
256 /* should not find anything at this point */
257 opts = qemu_opts_find(list, NULL);
258 g_assert(opts == NULL);
260 /* create the opts */
261 opts = qemu_opts_create(list, NULL, 0, &error_abort);
262 g_assert(opts != NULL);
263 g_assert(!QTAILQ_EMPTY(&list->head));
265 /* haven't set anything to number1 yet, so defval should be returned */
266 opt = qemu_opt_get_number(opts, "number1", 5);
269 qemu_opt_set_number(opts, "number1", 10, &error_abort);
271 /* now we have set number1, should know about it */
272 opt = qemu_opt_get_number(opts, "number1", 5);
275 /* having reset it, the returned should be the reset one not defval */
276 qemu_opt_set_number(opts, "number1", 15, &error_abort);
278 opt = qemu_opt_get_number(opts, "number1", 5);
283 /* should not find anything at this point */
284 opts = qemu_opts_find(list, NULL);
285 g_assert(opts == NULL);
288 static void test_qemu_opt_get_size(void)
295 list = qemu_find_opts("opts_list_02");
296 g_assert(list != NULL);
297 g_assert(QTAILQ_EMPTY(&list->head));
298 g_assert_cmpstr(list->name, ==, "opts_list_02");
300 /* should not find anything at this point */
301 opts = qemu_opts_find(list, NULL);
302 g_assert(opts == NULL);
304 /* create the opts */
305 opts = qemu_opts_create(list, NULL, 0, &error_abort);
306 g_assert(opts != NULL);
307 g_assert(!QTAILQ_EMPTY(&list->head));
309 /* haven't set anything to size1 yet, so defval should be returned */
310 opt = qemu_opt_get_size(opts, "size1", 5);
314 g_assert(dict != NULL);
316 qdict_put_str(dict, "size1", "10");
318 qemu_opts_absorb_qdict(opts, dict, &error_abort);
319 g_assert(error_abort == NULL);
321 /* now we have set size1, should know about it */
322 opt = qemu_opt_get_size(opts, "size1", 5);
326 qdict_put_str(dict, "size1", "15");
328 qemu_opts_absorb_qdict(opts, dict, &error_abort);
329 g_assert(error_abort == NULL);
331 /* test the reset value */
332 opt = qemu_opt_get_size(opts, "size1", 5);
335 qdict_del(dict, "size1");
340 /* should not find anything at this point */
341 opts = qemu_opts_find(list, NULL);
342 g_assert(opts == NULL);
345 static void test_qemu_opt_unset(void)
351 /* dynamically initialized (parsed) opts */
352 opts = qemu_opts_parse(&opts_list_03, "key=value", false, NULL);
353 g_assert(opts != NULL);
355 /* check default/parsed value */
356 value = qemu_opt_get(opts, "key");
357 g_assert_cmpstr(value, ==, "value");
359 /* reset it to value2 */
360 qemu_opt_set(opts, "key", "value2", &error_abort);
362 value = qemu_opt_get(opts, "key");
363 g_assert_cmpstr(value, ==, "value2");
365 /* unset, valid only for "accept any" */
366 ret = qemu_opt_unset(opts, "key");
369 /* after reset the value should be the parsed/default one */
370 value = qemu_opt_get(opts, "key");
371 g_assert_cmpstr(value, ==, "value");
376 static void test_qemu_opts_reset(void)
382 list = qemu_find_opts("opts_list_01");
383 g_assert(list != NULL);
384 g_assert(QTAILQ_EMPTY(&list->head));
385 g_assert_cmpstr(list->name, ==, "opts_list_01");
387 /* should not find anything at this point */
388 opts = qemu_opts_find(list, NULL);
389 g_assert(opts == NULL);
391 /* create the opts */
392 opts = qemu_opts_create(list, NULL, 0, &error_abort);
393 g_assert(opts != NULL);
394 g_assert(!QTAILQ_EMPTY(&list->head));
396 /* haven't set anything to number1 yet, so defval should be returned */
397 opt = qemu_opt_get_number(opts, "number1", 5);
400 qemu_opt_set_number(opts, "number1", 10, &error_abort);
402 /* now we have set number1, should know about it */
403 opt = qemu_opt_get_number(opts, "number1", 5);
406 qemu_opts_reset(list);
408 /* should not find anything at this point */
409 opts = qemu_opts_find(list, NULL);
410 g_assert(opts == NULL);
413 static int opts_count_iter(void *opaque, const char *name, const char *value,
416 (*(size_t *)opaque)++;
420 static size_t opts_count(QemuOpts *opts)
424 qemu_opt_foreach(opts, opts_count_iter, &n, NULL);
428 static void test_opts_parse(void)
434 opts = qemu_opts_parse(&opts_list_03, "", false, &error_abort);
435 g_assert_cmpuint(opts_count(opts), ==, 0);
438 opts = qemu_opts_parse(&opts_list_03, "=val", false, &error_abort);
439 g_assert_cmpuint(opts_count(opts), ==, 1);
440 g_assert_cmpstr(qemu_opt_get(opts, ""), ==, "val");
442 /* Multiple keys, last one wins */
443 opts = qemu_opts_parse(&opts_list_03, "a=1,b=2,,x,a=3",
444 false, &error_abort);
445 g_assert_cmpuint(opts_count(opts), ==, 3);
446 g_assert_cmpstr(qemu_opt_get(opts, "a"), ==, "3");
447 g_assert_cmpstr(qemu_opt_get(opts, "b"), ==, "2,x");
449 /* Except when it doesn't */
450 opts = qemu_opts_parse(&opts_list_03, "id=foo,id=bar",
451 false, &error_abort);
452 g_assert_cmpuint(opts_count(opts), ==, 0);
453 g_assert_cmpstr(qemu_opts_id(opts), ==, "foo");
455 /* TODO Cover low-level access to repeated keys */
457 /* Trailing comma is ignored */
458 opts = qemu_opts_parse(&opts_list_03, "x=y,", false, &error_abort);
459 g_assert_cmpuint(opts_count(opts), ==, 1);
460 g_assert_cmpstr(qemu_opt_get(opts, "x"), ==, "y");
462 /* Except when it isn't */
463 opts = qemu_opts_parse(&opts_list_03, ",", false, &error_abort);
464 g_assert_cmpuint(opts_count(opts), ==, 1);
465 g_assert_cmpstr(qemu_opt_get(opts, ""), ==, "on");
468 opts = qemu_opts_parse(&opts_list_03, "x=y,id=foo", false, &err);
469 error_free_or_abort(&err);
471 /* TODO Cover .merge_lists = true */
473 /* Buggy ID recognition (fixed) */
474 opts = qemu_opts_parse(&opts_list_03, "x=,,id=bar", false, &error_abort);
475 g_assert_cmpuint(opts_count(opts), ==, 1);
476 g_assert(!qemu_opts_id(opts));
477 g_assert_cmpstr(qemu_opt_get(opts, "x"), ==, ",id=bar");
480 opts = qemu_opts_parse(&opts_list_01, "id=666", false, &err);
481 error_free_or_abort(&err);
484 /* Implied value (qemu_opts_parse warns but accepts it) */
485 opts = qemu_opts_parse(&opts_list_03, "an,noaus,noaus=",
486 false, &error_abort);
487 g_assert_cmpuint(opts_count(opts), ==, 3);
488 g_assert_cmpstr(qemu_opt_get(opts, "an"), ==, "on");
489 g_assert_cmpstr(qemu_opt_get(opts, "aus"), ==, "off");
490 g_assert_cmpstr(qemu_opt_get(opts, "noaus"), ==, "");
492 /* Implied value, negated empty key */
493 opts = qemu_opts_parse(&opts_list_03, "no", false, &error_abort);
494 g_assert_cmpuint(opts_count(opts), ==, 1);
495 g_assert_cmpstr(qemu_opt_get(opts, ""), ==, "off");
498 opts = qemu_opts_parse(&opts_list_03, "an,noaus,noaus=", true,
500 g_assert_cmpuint(opts_count(opts), ==, 3);
501 g_assert_cmpstr(qemu_opt_get(opts, "implied"), ==, "an");
502 g_assert_cmpstr(qemu_opt_get(opts, "aus"), ==, "off");
503 g_assert_cmpstr(qemu_opt_get(opts, "noaus"), ==, "");
505 /* Implied key with empty value */
506 opts = qemu_opts_parse(&opts_list_03, ",", true, &error_abort);
507 g_assert_cmpuint(opts_count(opts), ==, 1);
508 g_assert_cmpstr(qemu_opt_get(opts, "implied"), ==, "");
510 /* Implied key with comma value */
511 opts = qemu_opts_parse(&opts_list_03, ",,,a=1", true, &error_abort);
512 g_assert_cmpuint(opts_count(opts), ==, 2);
513 g_assert_cmpstr(qemu_opt_get(opts, "implied"), ==, ",");
514 g_assert_cmpstr(qemu_opt_get(opts, "a"), ==, "1");
516 /* Empty key is not an implied key */
517 opts = qemu_opts_parse(&opts_list_03, "=val", true, &error_abort);
518 g_assert_cmpuint(opts_count(opts), ==, 1);
519 g_assert_cmpstr(qemu_opt_get(opts, ""), ==, "val");
522 opts = qemu_opts_parse(&opts_list_01, "nonexistent=", false, &err);
523 error_free_or_abort(&err);
526 qemu_opts_reset(&opts_list_01);
527 qemu_opts_reset(&opts_list_03);
530 static void test_opts_parse_bool(void)
535 opts = qemu_opts_parse(&opts_list_02, "bool1=on,bool2=off",
536 false, &error_abort);
537 g_assert_cmpuint(opts_count(opts), ==, 2);
538 g_assert(qemu_opt_get_bool(opts, "bool1", false));
539 g_assert(!qemu_opt_get_bool(opts, "bool2", true));
541 opts = qemu_opts_parse(&opts_list_02, "bool1=offer", false, &err);
542 error_free_or_abort(&err);
545 qemu_opts_reset(&opts_list_02);
548 static void test_opts_parse_number(void)
553 /* Lower limit zero */
554 opts = qemu_opts_parse(&opts_list_01, "number1=0", false, &error_abort);
555 g_assert_cmpuint(opts_count(opts), ==, 1);
556 g_assert_cmpuint(qemu_opt_get_number(opts, "number1", 1), ==, 0);
558 /* Upper limit 2^64-1 */
559 opts = qemu_opts_parse(&opts_list_01,
560 "number1=18446744073709551615,number2=-1",
561 false, &error_abort);
562 g_assert_cmpuint(opts_count(opts), ==, 2);
563 g_assert_cmphex(qemu_opt_get_number(opts, "number1", 1), ==, UINT64_MAX);
564 g_assert_cmphex(qemu_opt_get_number(opts, "number2", 0), ==, UINT64_MAX);
566 /* Above upper limit */
567 opts = qemu_opts_parse(&opts_list_01, "number1=18446744073709551616",
569 error_free_or_abort(&err);
572 /* Below lower limit */
573 opts = qemu_opts_parse(&opts_list_01, "number1=-18446744073709551616",
575 error_free_or_abort(&err);
579 opts = qemu_opts_parse(&opts_list_01, "number1=0x2a,number2=052",
580 false, &error_abort);
581 g_assert_cmpuint(opts_count(opts), ==, 2);
582 g_assert_cmpuint(qemu_opt_get_number(opts, "number1", 1), ==, 42);
583 g_assert_cmpuint(qemu_opt_get_number(opts, "number2", 0), ==, 42);
586 opts = qemu_opts_parse(&opts_list_01, "number1=", false, &err);
587 error_free_or_abort(&err);
589 opts = qemu_opts_parse(&opts_list_01, "number1=eins", false, &err);
590 error_free_or_abort(&err);
593 /* Leading whitespace */
594 opts = qemu_opts_parse(&opts_list_01, "number1= \t42",
595 false, &error_abort);
596 g_assert_cmpuint(opts_count(opts), ==, 1);
597 g_assert_cmpuint(qemu_opt_get_number(opts, "number1", 1), ==, 42);
600 opts = qemu_opts_parse(&opts_list_01, "number1=3.14", false, &err);
601 error_free_or_abort(&err);
603 opts = qemu_opts_parse(&opts_list_01, "number1=08", false, &err);
604 error_free_or_abort(&err);
606 opts = qemu_opts_parse(&opts_list_01, "number1=0 ", false, &err);
607 error_free_or_abort(&err);
610 qemu_opts_reset(&opts_list_01);
613 static void test_opts_parse_size(void)
618 /* Lower limit zero */
619 opts = qemu_opts_parse(&opts_list_02, "size1=0", false, &error_abort);
620 g_assert_cmpuint(opts_count(opts), ==, 1);
621 g_assert_cmpuint(qemu_opt_get_size(opts, "size1", 1), ==, 0);
623 /* Note: full 64 bits of precision */
625 /* Around double limit of precision: 2^53-1, 2^53, 2^53+1 */
626 opts = qemu_opts_parse(&opts_list_02,
627 "size1=9007199254740991,"
628 "size2=9007199254740992,"
629 "size3=9007199254740993",
630 false, &error_abort);
631 g_assert_cmpuint(opts_count(opts), ==, 3);
632 g_assert_cmphex(qemu_opt_get_size(opts, "size1", 1),
633 ==, 0x1fffffffffffff);
634 g_assert_cmphex(qemu_opt_get_size(opts, "size2", 1),
635 ==, 0x20000000000000);
636 g_assert_cmphex(qemu_opt_get_size(opts, "size3", 1),
637 ==, 0x20000000000001);
639 /* Close to signed int limit: 2^63-1, 2^63, 2^63+1 */
640 opts = qemu_opts_parse(&opts_list_02,
641 "size1=9223372036854775807," /* 7fffffffffffffff */
642 "size2=9223372036854775808," /* 8000000000000000 */
643 "size3=9223372036854775809", /* 8000000000000001 */
644 false, &error_abort);
645 g_assert_cmpuint(opts_count(opts), ==, 3);
646 g_assert_cmphex(qemu_opt_get_size(opts, "size1", 1),
647 ==, 0x7fffffffffffffff);
648 g_assert_cmphex(qemu_opt_get_size(opts, "size2", 1),
649 ==, 0x8000000000000000);
650 g_assert_cmphex(qemu_opt_get_size(opts, "size3", 1),
651 ==, 0x8000000000000001);
653 /* Close to actual upper limit 0xfffffffffffff800 (53 msbs set) */
654 opts = qemu_opts_parse(&opts_list_02,
655 "size1=18446744073709549568," /* fffffffffffff800 */
656 "size2=18446744073709550591", /* fffffffffffffbff */
657 false, &error_abort);
658 g_assert_cmpuint(opts_count(opts), ==, 2);
659 g_assert_cmphex(qemu_opt_get_size(opts, "size1", 1),
660 ==, 0xfffffffffffff800);
661 g_assert_cmphex(qemu_opt_get_size(opts, "size2", 1),
662 ==, 0xfffffffffffffbff);
664 /* Actual limit, 2^64-1 */
665 opts = qemu_opts_parse(&opts_list_02,
666 "size1=18446744073709551615", /* ffffffffffffffff */
667 false, &error_abort);
668 g_assert_cmpuint(opts_count(opts), ==, 1);
669 g_assert_cmphex(qemu_opt_get_size(opts, "size1", 1),
670 ==, 0xffffffffffffffff);
673 opts = qemu_opts_parse(&opts_list_02, "size1=-1", false, &err);
674 error_free_or_abort(&err);
676 opts = qemu_opts_parse(&opts_list_02,
677 "size1=18446744073709551616", /* 2^64 */
679 error_free_or_abort(&err);
683 opts = qemu_opts_parse(&opts_list_02, "size1=8b,size2=1.5k,size3=2M",
684 false, &error_abort);
685 g_assert_cmpuint(opts_count(opts), ==, 3);
686 g_assert_cmphex(qemu_opt_get_size(opts, "size1", 0), ==, 8);
687 g_assert_cmphex(qemu_opt_get_size(opts, "size2", 0), ==, 1536);
688 g_assert_cmphex(qemu_opt_get_size(opts, "size3", 0), ==, 2 * MiB);
689 opts = qemu_opts_parse(&opts_list_02, "size1=0.1G,size2=16777215T",
690 false, &error_abort);
691 g_assert_cmpuint(opts_count(opts), ==, 2);
692 g_assert_cmphex(qemu_opt_get_size(opts, "size1", 0), ==, GiB / 10);
693 g_assert_cmphex(qemu_opt_get_size(opts, "size2", 0), ==, 16777215ULL * TiB);
695 /* Beyond limit with suffix */
696 opts = qemu_opts_parse(&opts_list_02, "size1=16777216T",
698 error_free_or_abort(&err);
702 opts = qemu_opts_parse(&opts_list_02, "size1=16E", false, &err);
703 error_free_or_abort(&err);
705 opts = qemu_opts_parse(&opts_list_02, "size1=16Gi", false, &err);
706 error_free_or_abort(&err);
709 qemu_opts_reset(&opts_list_02);
712 static void test_has_help_option(void)
714 static const struct {
716 /* expected value of qemu_opt_has_help_opt() with implied=false */
718 /* expected value of qemu_opt_has_help_opt() with implied=true */
721 { "help", true, false },
722 { "?", true, false },
723 { "helpme", false, false },
724 { "?me", false, false },
725 { "a,help", true, true },
726 { "a,?", true, true },
727 { "a=0,help,b", true, true },
728 { "a=0,?,b", true, true },
729 { "help,b=1", true, false },
730 { "?,b=1", true, false },
731 { "a,b,,help", true, true },
732 { "a,b,,?", true, true },
737 for (i = 0; i < ARRAY_SIZE(test); i++) {
738 g_assert_cmpint(has_help_option(test[i].params),
740 opts = qemu_opts_parse(&opts_list_03, test[i].params, false,
742 g_assert_cmpint(qemu_opt_has_help_opt(opts),
745 opts = qemu_opts_parse(&opts_list_03, test[i].params, true,
747 g_assert_cmpint(qemu_opt_has_help_opt(opts),
748 ==, test[i].expect_implied);
753 static void append_verify_list_01(QemuOptDesc *desc, bool with_overlapping)
757 if (with_overlapping) {
758 g_assert_cmpstr(desc[i].name, ==, "str1");
759 g_assert_cmpint(desc[i].type, ==, QEMU_OPT_STRING);
760 g_assert_cmpstr(desc[i].help, ==,
761 "Help texts are preserved in qemu_opts_append");
762 g_assert_cmpstr(desc[i].def_value_str, ==, "default");
765 g_assert_cmpstr(desc[i].name, ==, "str2");
766 g_assert_cmpint(desc[i].type, ==, QEMU_OPT_STRING);
767 g_assert_cmpstr(desc[i].help, ==, NULL);
768 g_assert_cmpstr(desc[i].def_value_str, ==, NULL);
772 g_assert_cmpstr(desc[i].name, ==, "str3");
773 g_assert_cmpint(desc[i].type, ==, QEMU_OPT_STRING);
774 g_assert_cmpstr(desc[i].help, ==, NULL);
775 g_assert_cmpstr(desc[i].def_value_str, ==, NULL);
778 g_assert_cmpstr(desc[i].name, ==, "number1");
779 g_assert_cmpint(desc[i].type, ==, QEMU_OPT_NUMBER);
780 g_assert_cmpstr(desc[i].help, ==,
781 "Having help texts only for some options is okay");
782 g_assert_cmpstr(desc[i].def_value_str, ==, NULL);
785 g_assert_cmpstr(desc[i].name, ==, "number2");
786 g_assert_cmpint(desc[i].type, ==, QEMU_OPT_NUMBER);
787 g_assert_cmpstr(desc[i].help, ==, NULL);
788 g_assert_cmpstr(desc[i].def_value_str, ==, NULL);
791 g_assert_cmpstr(desc[i].name, ==, NULL);
794 static void append_verify_list_02(QemuOptDesc *desc)
798 g_assert_cmpstr(desc[i].name, ==, "str1");
799 g_assert_cmpint(desc[i].type, ==, QEMU_OPT_STRING);
800 g_assert_cmpstr(desc[i].help, ==, NULL);
801 g_assert_cmpstr(desc[i].def_value_str, ==, NULL);
804 g_assert_cmpstr(desc[i].name, ==, "str2");
805 g_assert_cmpint(desc[i].type, ==, QEMU_OPT_STRING);
806 g_assert_cmpstr(desc[i].help, ==, NULL);
807 g_assert_cmpstr(desc[i].def_value_str, ==, NULL);
810 g_assert_cmpstr(desc[i].name, ==, "bool1");
811 g_assert_cmpint(desc[i].type, ==, QEMU_OPT_BOOL);
812 g_assert_cmpstr(desc[i].help, ==, NULL);
813 g_assert_cmpstr(desc[i].def_value_str, ==, NULL);
816 g_assert_cmpstr(desc[i].name, ==, "bool2");
817 g_assert_cmpint(desc[i].type, ==, QEMU_OPT_BOOL);
818 g_assert_cmpstr(desc[i].help, ==, NULL);
819 g_assert_cmpstr(desc[i].def_value_str, ==, NULL);
822 g_assert_cmpstr(desc[i].name, ==, "size1");
823 g_assert_cmpint(desc[i].type, ==, QEMU_OPT_SIZE);
824 g_assert_cmpstr(desc[i].help, ==, NULL);
825 g_assert_cmpstr(desc[i].def_value_str, ==, NULL);
828 g_assert_cmpstr(desc[i].name, ==, "size2");
829 g_assert_cmpint(desc[i].type, ==, QEMU_OPT_SIZE);
830 g_assert_cmpstr(desc[i].help, ==, NULL);
831 g_assert_cmpstr(desc[i].def_value_str, ==, NULL);
834 g_assert_cmpstr(desc[i].name, ==, "size3");
835 g_assert_cmpint(desc[i].type, ==, QEMU_OPT_SIZE);
836 g_assert_cmpstr(desc[i].help, ==, NULL);
837 g_assert_cmpstr(desc[i].def_value_str, ==, NULL);
840 static void test_opts_append_to_null(void)
842 QemuOptsList *merged;
844 merged = qemu_opts_append(NULL, &opts_list_01);
845 g_assert(merged != &opts_list_01);
847 g_assert_cmpstr(merged->name, ==, NULL);
848 g_assert_cmpstr(merged->implied_opt_name, ==, NULL);
849 g_assert_false(merged->merge_lists);
851 append_verify_list_01(merged->desc, true);
853 qemu_opts_free(merged);
856 static void test_opts_append(void)
858 QemuOptsList *first, *merged;
860 first = qemu_opts_append(NULL, &opts_list_02);
861 merged = qemu_opts_append(first, &opts_list_01);
862 g_assert(first != &opts_list_02);
863 g_assert(merged != &opts_list_01);
865 g_assert_cmpstr(merged->name, ==, NULL);
866 g_assert_cmpstr(merged->implied_opt_name, ==, NULL);
867 g_assert_false(merged->merge_lists);
869 append_verify_list_02(&merged->desc[0]);
870 append_verify_list_01(&merged->desc[7], false);
872 qemu_opts_free(merged);
875 static void test_opts_to_qdict_basic(void)
880 opts = qemu_opts_parse(&opts_list_01, "str1=foo,str2=,str3=bar,number1=42",
881 false, &error_abort);
882 g_assert(opts != NULL);
884 dict = qemu_opts_to_qdict(opts, NULL);
885 g_assert(dict != NULL);
887 g_assert_cmpstr(qdict_get_str(dict, "str1"), ==, "foo");
888 g_assert_cmpstr(qdict_get_str(dict, "str2"), ==, "");
889 g_assert_cmpstr(qdict_get_str(dict, "str3"), ==, "bar");
890 g_assert_cmpstr(qdict_get_str(dict, "number1"), ==, "42");
891 g_assert_false(qdict_haskey(dict, "number2"));
897 static void test_opts_to_qdict_filtered(void)
899 QemuOptsList *first, *merged;
903 first = qemu_opts_append(NULL, &opts_list_02);
904 merged = qemu_opts_append(first, &opts_list_01);
906 opts = qemu_opts_parse(merged,
907 "str1=foo,str2=,str3=bar,bool1=off,number1=42",
908 false, &error_abort);
909 g_assert(opts != NULL);
911 /* Convert to QDict without deleting from opts */
912 dict = qemu_opts_to_qdict_filtered(opts, NULL, &opts_list_01, false);
913 g_assert(dict != NULL);
914 g_assert_cmpstr(qdict_get_str(dict, "str1"), ==, "foo");
915 g_assert_cmpstr(qdict_get_str(dict, "str2"), ==, "");
916 g_assert_cmpstr(qdict_get_str(dict, "str3"), ==, "bar");
917 g_assert_cmpstr(qdict_get_str(dict, "number1"), ==, "42");
918 g_assert_false(qdict_haskey(dict, "number2"));
919 g_assert_false(qdict_haskey(dict, "bool1"));
922 dict = qemu_opts_to_qdict_filtered(opts, NULL, &opts_list_02, false);
923 g_assert(dict != NULL);
924 g_assert_cmpstr(qdict_get_str(dict, "str1"), ==, "foo");
925 g_assert_cmpstr(qdict_get_str(dict, "str2"), ==, "");
926 g_assert_cmpstr(qdict_get_str(dict, "bool1"), ==, "off");
927 g_assert_false(qdict_haskey(dict, "str3"));
928 g_assert_false(qdict_haskey(dict, "number1"));
929 g_assert_false(qdict_haskey(dict, "number2"));
932 /* Now delete converted options from opts */
933 dict = qemu_opts_to_qdict_filtered(opts, NULL, &opts_list_01, true);
934 g_assert(dict != NULL);
935 g_assert_cmpstr(qdict_get_str(dict, "str1"), ==, "foo");
936 g_assert_cmpstr(qdict_get_str(dict, "str2"), ==, "");
937 g_assert_cmpstr(qdict_get_str(dict, "str3"), ==, "bar");
938 g_assert_cmpstr(qdict_get_str(dict, "number1"), ==, "42");
939 g_assert_false(qdict_haskey(dict, "number2"));
940 g_assert_false(qdict_haskey(dict, "bool1"));
943 dict = qemu_opts_to_qdict_filtered(opts, NULL, &opts_list_02, true);
944 g_assert(dict != NULL);
945 g_assert_cmpstr(qdict_get_str(dict, "bool1"), ==, "off");
946 g_assert_false(qdict_haskey(dict, "str1"));
947 g_assert_false(qdict_haskey(dict, "str2"));
948 g_assert_false(qdict_haskey(dict, "str3"));
949 g_assert_false(qdict_haskey(dict, "number1"));
950 g_assert_false(qdict_haskey(dict, "number2"));
953 g_assert_true(QTAILQ_EMPTY(&opts->head));
956 qemu_opts_free(merged);
959 static void test_opts_to_qdict_duplicates(void)
965 opts = qemu_opts_parse(&opts_list_03, "foo=a,foo=b", false, &error_abort);
966 g_assert(opts != NULL);
968 /* Verify that opts has two options with the same name */
969 opt = QTAILQ_FIRST(&opts->head);
970 g_assert_cmpstr(opt->name, ==, "foo");
971 g_assert_cmpstr(opt->str , ==, "a");
973 opt = QTAILQ_NEXT(opt, next);
974 g_assert_cmpstr(opt->name, ==, "foo");
975 g_assert_cmpstr(opt->str , ==, "b");
977 opt = QTAILQ_NEXT(opt, next);
978 g_assert(opt == NULL);
980 /* In the conversion to QDict, the last one wins */
981 dict = qemu_opts_to_qdict(opts, NULL);
982 g_assert(dict != NULL);
983 g_assert_cmpstr(qdict_get_str(dict, "foo"), ==, "b");
986 /* The last one still wins if entries are deleted, and both are deleted */
987 dict = qemu_opts_to_qdict_filtered(opts, NULL, NULL, true);
988 g_assert(dict != NULL);
989 g_assert_cmpstr(qdict_get_str(dict, "foo"), ==, "b");
992 g_assert_true(QTAILQ_EMPTY(&opts->head));
997 int main(int argc, char *argv[])
1000 g_test_init(&argc, &argv, NULL);
1001 g_test_add_func("/qemu-opts/find_unknown_opts", test_find_unknown_opts);
1002 g_test_add_func("/qemu-opts/find_opts", test_qemu_find_opts);
1003 g_test_add_func("/qemu-opts/opts_create", test_qemu_opts_create);
1004 g_test_add_func("/qemu-opts/opt_get", test_qemu_opt_get);
1005 g_test_add_func("/qemu-opts/opt_get_bool", test_qemu_opt_get_bool);
1006 g_test_add_func("/qemu-opts/opt_get_number", test_qemu_opt_get_number);
1007 g_test_add_func("/qemu-opts/opt_get_size", test_qemu_opt_get_size);
1008 g_test_add_func("/qemu-opts/opt_unset", test_qemu_opt_unset);
1009 g_test_add_func("/qemu-opts/opts_reset", test_qemu_opts_reset);
1010 g_test_add_func("/qemu-opts/opts_parse/general", test_opts_parse);
1011 g_test_add_func("/qemu-opts/opts_parse/bool", test_opts_parse_bool);
1012 g_test_add_func("/qemu-opts/opts_parse/number", test_opts_parse_number);
1013 g_test_add_func("/qemu-opts/opts_parse/size", test_opts_parse_size);
1014 g_test_add_func("/qemu-opts/has_help_option", test_has_help_option);
1015 g_test_add_func("/qemu-opts/append_to_null", test_opts_append_to_null);
1016 g_test_add_func("/qemu-opts/append", test_opts_append);
1017 g_test_add_func("/qemu-opts/to_qdict/basic", test_opts_to_qdict_basic);
1018 g_test_add_func("/qemu-opts/to_qdict/filtered", test_opts_to_qdict_filtered);
1019 g_test_add_func("/qemu-opts/to_qdict/duplicates", test_opts_to_qdict_duplicates);