1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
|
/*
* QemuOpts unit-tests.
*
* Copyright (C) 2014 Leandro Dorileo <l@dorileo.org>
*
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
* See the COPYING.LIB file in the top-level directory.
*/
#include "qemu/osdep.h"
#include "qemu/units.h"
#include "qemu/option.h"
#include "qemu/option_int.h"
#include "qapi/error.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qstring.h"
#include "qemu/config-file.h"
static QemuOptsList opts_list_01 = {
.name = "opts_list_01",
.head = QTAILQ_HEAD_INITIALIZER(opts_list_01.head),
.desc = {
{
.name = "str1",
.type = QEMU_OPT_STRING,
.help = "Help texts are preserved in qemu_opts_append",
.def_value_str = "default",
},{
.name = "str2",
.type = QEMU_OPT_STRING,
},{
.name = "str3",
.type = QEMU_OPT_STRING,
},{
.name = "number1",
.type = QEMU_OPT_NUMBER,
.help = "Having help texts only for some options is okay",
},{
.name = "number2",
.type = QEMU_OPT_NUMBER,
},
{ /* end of list */ }
},
};
static QemuOptsList opts_list_02 = {
.name = "opts_list_02",
.head = QTAILQ_HEAD_INITIALIZER(opts_list_02.head),
.desc = {
{
.name = "str1",
.type = QEMU_OPT_STRING,
},{
.name = "str2",
.type = QEMU_OPT_STRING,
},{
.name = "bool1",
.type = QEMU_OPT_BOOL,
},{
.name = "bool2",
.type = QEMU_OPT_BOOL,
},{
.name = "size1",
.type = QEMU_OPT_SIZE,
},{
.name = "size2",
.type = QEMU_OPT_SIZE,
},{
.name = "size3",
.type = QEMU_OPT_SIZE,
},
{ /* end of list */ }
},
};
static QemuOptsList opts_list_03 = {
.name = "opts_list_03",
.implied_opt_name = "implied",
.head = QTAILQ_HEAD_INITIALIZER(opts_list_03.head),
.desc = {
/* no elements => accept any params */
{ /* end of list */ }
},
};
static QemuOptsList opts_list_04 = {
.name = "opts_list_04",
.head = QTAILQ_HEAD_INITIALIZER(opts_list_04.head),
.merge_lists = true,
.desc = {
{
.name = "str3",
.type = QEMU_OPT_STRING,
},
{ /* end of list */ }
},
};
static void register_opts(void)
{
qemu_add_opts(&opts_list_01);
qemu_add_opts(&opts_list_02);
qemu_add_opts(&opts_list_03);
qemu_add_opts(&opts_list_04);
}
static void test_find_unknown_opts(void)
{
QemuOptsList *list;
Error *err = NULL;
/* should not return anything, we don't have an "unknown" option */
list = qemu_find_opts_err("unknown", &err);
g_assert(list == NULL);
error_free_or_abort(&err);
}
static void test_qemu_find_opts(void)
{
QemuOptsList *list;
/* we have an "opts_list_01" option, should return it */
list = qemu_find_opts("opts_list_01");
g_assert(list != NULL);
g_assert_cmpstr(list->name, ==, "opts_list_01");
}
static void test_qemu_opts_create(void)
{
QemuOptsList *list;
QemuOpts *opts;
list = qemu_find_opts("opts_list_01");
g_assert(list != NULL);
g_assert(QTAILQ_EMPTY(&list->head));
g_assert_cmpstr(list->name, ==, "opts_list_01");
/* should not find anything at this point */
opts = qemu_opts_find(list, NULL);
g_assert(opts == NULL);
/* create the opts */
opts = qemu_opts_create(list, NULL, 0, &error_abort);
g_assert(opts != NULL);
g_assert(!QTAILQ_EMPTY(&list->head));
/* now we've create the opts, must find it */
opts = qemu_opts_find(list, NULL);
g_assert(opts != NULL);
qemu_opts_del(opts);
/* should not find anything at this point */
opts = qemu_opts_find(list, NULL);
g_assert(opts == NULL);
}
static void test_qemu_opt_get(void)
{
QemuOptsList *list;
QemuOpts *opts;
const char *opt = NULL;
list = qemu_find_opts("opts_list_01");
g_assert(list != NULL);
g_assert(QTAILQ_EMPTY(&list->head));
g_assert_cmpstr(list->name, ==, "opts_list_01");
/* should not find anything at this point */
opts = qemu_opts_find(list, NULL);
g_assert(opts == NULL);
/* create the opts */
opts = qemu_opts_create(list, NULL, 0, &error_abort);
g_assert(opts != NULL);
g_assert(!QTAILQ_EMPTY(&list->head));
/* haven't set anything to str2 yet */
opt = qemu_opt_get(opts, "str2");
g_assert(opt == NULL);
qemu_opt_set(opts, "str2", "value", &error_abort);
/* now we have set str2, should know about it */
opt = qemu_opt_get(opts, "str2");
g_assert_cmpstr(opt, ==, "value");
qemu_opt_set(opts, "str2", "value2", &error_abort);
/* having reset the value, the returned should be the reset one */
opt = qemu_opt_get(opts, "str2");
g_assert_cmpstr(opt, ==, "value2");
qemu_opts_del(opts);
/* should not find anything at this point */
opts = qemu_opts_find(list, NULL);
g_assert(opts == NULL);
}
static void test_qemu_opt_get_bool(void)
{
QemuOptsList *list;
QemuOpts *opts;
bool opt;
list = qemu_find_opts("opts_list_02");
g_assert(list != NULL);
g_assert(QTAILQ_EMPTY(&list->head));
g_assert_cmpstr(list->name, ==, "opts_list_02");
/* should not find anything at this point */
opts = qemu_opts_find(list, NULL);
g_assert(opts == NULL);
/* create the opts */
opts = qemu_opts_create(list, NULL, 0, &error_abort);
g_assert(opts != NULL);
g_assert(!QTAILQ_EMPTY(&list->head));
/* haven't set anything to bool1 yet, so defval should be returned */
opt = qemu_opt_get_bool(opts, "bool1", false);
g_assert(opt == false);
qemu_opt_set_bool(opts, "bool1", true, &error_abort);
/* now we have set bool1, should know about it */
opt = qemu_opt_get_bool(opts, "bool1", false);
g_assert(opt == true);
/* having reset the value, opt should be the reset one not defval */
qemu_opt_set_bool(opts, "bool1", false, &error_abort);
opt = qemu_opt_get_bool(opts, "bool1", true);
g_assert(opt == false);
qemu_opts_del(opts);
/* should not find anything at this point */
opts = qemu_opts_find(list, NULL);
g_assert(opts == NULL);
}
static void test_qemu_opt_get_number(void)
{
QemuOptsList *list;
QemuOpts *opts;
uint64_t opt;
list = qemu_find_opts("opts_list_01");
g_assert(list != NULL);
g_assert(QTAILQ_EMPTY(&list->head));
g_assert_cmpstr(list->name, ==, "opts_list_01");
/* should not find anything at this point */
opts = qemu_opts_find(list, NULL);
g_assert(opts == NULL);
/* create the opts */
opts = qemu_opts_create(list, NULL, 0, &error_abort);
g_assert(opts != NULL);
g_assert(!QTAILQ_EMPTY(&list->head));
/* haven't set anything to number1 yet, so defval should be returned */
opt = qemu_opt_get_number(opts, "number1", 5);
g_assert(opt == 5);
qemu_opt_set_number(opts, "number1", 10, &error_abort);
/* now we have set number1, should know about it */
opt = qemu_opt_get_number(opts, "number1", 5);
g_assert(opt == 10);
/* having reset it, the returned should be the reset one not defval */
qemu_opt_set_number(opts, "number1", 15, &error_abort);
opt = qemu_opt_get_number(opts, "number1", 5);
g_assert(opt == 15);
qemu_opts_del(opts);
/* should not find anything at this point */
opts = qemu_opts_find(list, NULL);
g_assert(opts == NULL);
}
static void test_qemu_opt_get_size(void)
{
QemuOptsList *list;
QemuOpts *opts;
uint64_t opt;
QDict *dict;
list = qemu_find_opts("opts_list_02");
g_assert(list != NULL);
g_assert(QTAILQ_EMPTY(&list->head));
g_assert_cmpstr(list->name, ==, "opts_list_02");
/* should not find anything at this point */
opts = qemu_opts_find(list, NULL);
g_assert(opts == NULL);
/* create the opts */
opts = qemu_opts_create(list, NULL, 0, &error_abort);
g_assert(opts != NULL);
g_assert(!QTAILQ_EMPTY(&list->head));
/* haven't set anything to size1 yet, so defval should be returned */
opt = qemu_opt_get_size(opts, "size1", 5);
g_assert(opt == 5);
dict = qdict_new();
g_assert(dict != NULL);
qdict_put_str(dict, "size1", "10");
qemu_opts_absorb_qdict(opts, dict, &error_abort);
g_assert(error_abort == NULL);
/* now we have set size1, should know about it */
opt = qemu_opt_get_size(opts, "size1", 5);
g_assert(opt == 10);
/* reset value */
qdict_put_str(dict, "size1", "15");
qemu_opts_absorb_qdict(opts, dict, &error_abort);
g_assert(error_abort == NULL);
/* test the reset value */
opt = qemu_opt_get_size(opts, "size1", 5);
g_assert(opt == 15);
qdict_del(dict, "size1");
g_free(dict);
qemu_opts_del(opts);
/* should not find anything at this point */
opts = qemu_opts_find(list, NULL);
g_assert(opts == NULL);
}
static void test_qemu_opt_unset(void)
{
QemuOpts *opts;
const char *value;
int ret;
/* dynamically initialized (parsed) opts */
opts = qemu_opts_parse(&opts_list_03, "key=value", false, NULL);
g_assert(opts != NULL);
/* check default/parsed value */
value = qemu_opt_get(opts, "key");
g_assert_cmpstr(value, ==, "value");
/* reset it to value2 */
qemu_opt_set(opts, "key", "value2", &error_abort);
value = qemu_opt_get(opts, "key");
g_assert_cmpstr(value, ==, "value2");
/* unset, valid only for "accept any" */
ret = qemu_opt_unset(opts, "key");
g_assert(ret == 0);
/* after reset the value should be the parsed/default one */
value = qemu_opt_get(opts, "key");
g_assert_cmpstr(value, ==, "value");
qemu_opts_del(opts);
}
static void test_qemu_opts_reset(void)
{
QemuOptsList *list;
QemuOpts *opts;
uint64_t opt;
list = qemu_find_opts("opts_list_01");
g_assert(list != NULL);
g_assert(QTAILQ_EMPTY(&list->head));
g_assert_cmpstr(list->name, ==, "opts_list_01");
/* should not find anything at this point */
opts = qemu_opts_find(list, NULL);
g_assert(opts == NULL);
/* create the opts */
opts = qemu_opts_create(list, NULL, 0, &error_abort);
g_assert(opts != NULL);
g_assert(!QTAILQ_EMPTY(&list->head));
/* haven't set anything to number1 yet, so defval should be returned */
opt = qemu_opt_get_number(opts, "number1", 5);
g_assert(opt == 5);
qemu_opt_set_number(opts, "number1", 10, &error_abort);
/* now we have set number1, should know about it */
opt = qemu_opt_get_number(opts, "number1", 5);
g_assert(opt == 10);
qemu_opts_reset(list);
/* should not find anything at this point */
opts = qemu_opts_find(list, NULL);
g_assert(opts == NULL);
}
static void test_qemu_opts_set(void)
{
QemuOptsList *list;
QemuOpts *opts;
const char *opt;
list = qemu_find_opts("opts_list_04");
g_assert(list != NULL);
g_assert(QTAILQ_EMPTY(&list->head));
g_assert_cmpstr(list->name, ==, "opts_list_04");
/* should not find anything at this point */
opts = qemu_opts_find(list, NULL);
g_assert(opts == NULL);
/* implicitly create opts and set str3 value */
qemu_opts_set(list, "str3", "value", &error_abort);
g_assert(!QTAILQ_EMPTY(&list->head));
/* get the just created opts */
opts = qemu_opts_find(list, NULL);
g_assert(opts != NULL);
/* check the str3 value */
opt = qemu_opt_get(opts, "str3");
g_assert_cmpstr(opt, ==, "value");
qemu_opts_del(opts);
/* should not find anything at this point */
opts = qemu_opts_find(list, NULL);
g_assert(opts == NULL);
}
static int opts_count_iter(void *opaque, const char *name, const char *value,
Error **errp)
{
(*(size_t *)opaque)++;
return 0;
}
static size_t opts_count(QemuOpts *opts)
{
size_t n = 0;
qemu_opt_foreach(opts, opts_count_iter, &n, NULL);
return n;
}
static void test_opts_parse(void)
{
Error *err = NULL;
QemuOpts *opts;
/* Nothing */
opts = qemu_opts_parse(&opts_list_03, "", false, &error_abort);
g_assert_cmpuint(opts_count(opts), ==, 0);
/* Empty key */
opts = qemu_opts_parse(&opts_list_03, "=val", false, &error_abort);
g_assert_cmpuint(opts_count(opts), ==, 1);
g_assert_cmpstr(qemu_opt_get(opts, ""), ==, "val");
/* Multiple keys, last one wins */
opts = qemu_opts_parse(&opts_list_03, "a=1,b=2,,x,a=3",
false, &error_abort);
g_assert_cmpuint(opts_count(opts), ==, 3);
g_assert_cmpstr(qemu_opt_get(opts, "a"), ==, "3");
g_assert_cmpstr(qemu_opt_get(opts, "b"), ==, "2,x");
/* Except when it doesn't */
opts = qemu_opts_parse(&opts_list_03, "id=foo,id=bar",
false, &error_abort);
g_assert_cmpuint(opts_count(opts), ==, 0);
g_assert_cmpstr(qemu_opts_id(opts), ==, "foo");
/* TODO Cover low-level access to repeated keys */
/* Trailing comma is ignored */
opts = qemu_opts_parse(&opts_list_03, "x=y,", false, &error_abort);
g_assert_cmpuint(opts_count(opts), ==, 1);
g_assert_cmpstr(qemu_opt_get(opts, "x"), ==, "y");
/* Except when it isn't */
opts = qemu_opts_parse(&opts_list_03, ",", false, &error_abort);
g_assert_cmpuint(opts_count(opts), ==, 1);
g_assert_cmpstr(qemu_opt_get(opts, ""), ==, "on");
/* Duplicate ID */
opts = qemu_opts_parse(&opts_list_03, "x=y,id=foo", false, &err);
error_free_or_abort(&err);
g_assert(!opts);
/* TODO Cover .merge_lists = true */
/* Buggy ID recognition (fixed) */
opts = qemu_opts_parse(&opts_list_03, "x=,,id=bar", false, &error_abort);
g_assert_cmpuint(opts_count(opts), ==, 1);
g_assert(!qemu_opts_id(opts));
g_assert_cmpstr(qemu_opt_get(opts, "x"), ==, ",id=bar");
/* Anti-social ID */
opts = qemu_opts_parse(&opts_list_01, "id=666", false, &err);
error_free_or_abort(&err);
g_assert(!opts);
/* Implied value (qemu_opts_parse warns but accepts it) */
opts = qemu_opts_parse(&opts_list_03, "an,noaus,noaus=",
false, &error_abort);
g_assert_cmpuint(opts_count(opts), ==, 3);
g_assert_cmpstr(qemu_opt_get(opts, "an"), ==, "on");
g_assert_cmpstr(qemu_opt_get(opts, "aus"), ==, "off");
g_assert_cmpstr(qemu_opt_get(opts, "noaus"), ==, "");
/* Implied value, negated empty key */
opts = qemu_opts_parse(&opts_list_03, "no", false, &error_abort);
g_assert_cmpuint(opts_count(opts), ==, 1);
g_assert_cmpstr(qemu_opt_get(opts, ""), ==, "off");
/* Implied key */
opts = qemu_opts_parse(&opts_list_03, "an,noaus,noaus=", true,
&error_abort);
g_assert_cmpuint(opts_count(opts), ==, 3);
g_assert_cmpstr(qemu_opt_get(opts, "implied"), ==, "an");
g_assert_cmpstr(qemu_opt_get(opts, "aus"), ==, "off");
g_assert_cmpstr(qemu_opt_get(opts, "noaus"), ==, "");
/* Implied key with empty value */
opts = qemu_opts_parse(&opts_list_03, ",", true, &error_abort);
g_assert_cmpuint(opts_count(opts), ==, 1);
g_assert_cmpstr(qemu_opt_get(opts, "implied"), ==, "");
/* Implied key with comma value */
opts = qemu_opts_parse(&opts_list_03, ",,,a=1", true, &error_abort);
g_assert_cmpuint(opts_count(opts), ==, 2);
g_assert_cmpstr(qemu_opt_get(opts, "implied"), ==, ",");
g_assert_cmpstr(qemu_opt_get(opts, "a"), ==, "1");
/* Empty key is not an implied key */
opts = qemu_opts_parse(&opts_list_03, "=val", true, &error_abort);
g_assert_cmpuint(opts_count(opts), ==, 1);
g_assert_cmpstr(qemu_opt_get(opts, ""), ==, "val");
/* Unknown key */
opts = qemu_opts_parse(&opts_list_01, "nonexistent=", false, &err);
error_free_or_abort(&err);
g_assert(!opts);
qemu_opts_reset(&opts_list_01);
qemu_opts_reset(&opts_list_03);
}
static void test_opts_parse_bool(void)
{
Error *err = NULL;
QemuOpts *opts;
opts = qemu_opts_parse(&opts_list_02, "bool1=on,bool2=off",
false, &error_abort);
g_assert_cmpuint(opts_count(opts), ==, 2);
g_assert(qemu_opt_get_bool(opts, "bool1", false));
g_assert(!qemu_opt_get_bool(opts, "bool2", true));
opts = qemu_opts_parse(&opts_list_02, "bool1=offer", false, &err);
error_free_or_abort(&err);
g_assert(!opts);
qemu_opts_reset(&opts_list_02);
}
static void test_opts_parse_number(void)
{
Error *err = NULL;
QemuOpts *opts;
/* Lower limit zero */
opts = qemu_opts_parse(&opts_list_01, "number1=0", false, &error_abort);
g_assert_cmpuint(opts_count(opts), ==, 1);
g_assert_cmpuint(qemu_opt_get_number(opts, "number1", 1), ==, 0);
/* Upper limit 2^64-1 */
opts = qemu_opts_parse(&opts_list_01,
"number1=18446744073709551615,number2=-1",
false, &error_abort);
g_assert_cmpuint(opts_count(opts), ==, 2);
g_assert_cmphex(qemu_opt_get_number(opts, "number1", 1), ==, UINT64_MAX);
g_assert_cmphex(qemu_opt_get_number(opts, "number2", 0), ==, UINT64_MAX);
/* Above upper limit */
opts = qemu_opts_parse(&opts_list_01, "number1=18446744073709551616",
false, &err);
error_free_or_abort(&err);
g_assert(!opts);
/* Below lower limit */
opts = qemu_opts_parse(&opts_list_01, "number1=-18446744073709551616",
false, &err);
error_free_or_abort(&err);
g_assert(!opts);
/* Hex and octal */
opts = qemu_opts_parse(&opts_list_01, "number1=0x2a,number2=052",
false, &error_abort);
g_assert_cmpuint(opts_count(opts), ==, 2);
g_assert_cmpuint(qemu_opt_get_number(opts, "number1", 1), ==, 42);
g_assert_cmpuint(qemu_opt_get_number(opts, "number2", 0), ==, 42);
/* Invalid */
opts = qemu_opts_parse(&opts_list_01, "number1=", false, &err);
error_free_or_abort(&err);
g_assert(!opts);
opts = qemu_opts_parse(&opts_list_01, "number1=eins", false, &err);
error_free_or_abort(&err);
g_assert(!opts);
/* Leading whitespace */
opts = qemu_opts_parse(&opts_list_01, "number1= \t42",
false, &error_abort);
g_assert_cmpuint(opts_count(opts), ==, 1);
g_assert_cmpuint(qemu_opt_get_number(opts, "number1", 1), ==, 42);
/* Trailing crap */
opts = qemu_opts_parse(&opts_list_01, "number1=3.14", false, &err);
error_free_or_abort(&err);
g_assert(!opts);
opts = qemu_opts_parse(&opts_list_01, "number1=08", false, &err);
error_free_or_abort(&err);
g_assert(!opts);
opts = qemu_opts_parse(&opts_list_01, "number1=0 ", false, &err);
error_free_or_abort(&err);
g_assert(!opts);
qemu_opts_reset(&opts_list_01);
}
static void test_opts_parse_size(void)
{
Error *err = NULL;
QemuOpts *opts;
/* Lower limit zero */
opts = qemu_opts_parse(&opts_list_02, "size1=0", false, &error_abort);
g_assert_cmpuint(opts_count(opts), ==, 1);
g_assert_cmpuint(qemu_opt_get_size(opts, "size1", 1), ==, 0);
/* Note: precision is 53 bits since we're parsing with strtod() */
/* Around limit of precision: 2^53-1, 2^53, 2^54 */
opts = qemu_opts_parse(&opts_list_02,
"size1=9007199254740991,"
"size2=9007199254740992,"
"size3=9007199254740993",
false, &error_abort);
g_assert_cmpuint(opts_count(opts), ==, 3);
g_assert_cmphex(qemu_opt_get_size(opts, "size1", 1),
==, 0x1fffffffffffff);
g_assert_cmphex(qemu_opt_get_size(opts, "size2", 1),
==, 0x20000000000000);
g_assert_cmphex(qemu_opt_get_size(opts, "size3", 1),
==, 0x20000000000000);
/* Close to signed upper limit 0x7ffffffffffffc00 (53 msbs set) */
opts = qemu_opts_parse(&opts_list_02,
"size1=9223372036854774784," /* 7ffffffffffffc00 */
"size2=9223372036854775295", /* 7ffffffffffffdff */
false, &error_abort);
g_assert_cmpuint(opts_count(opts), ==, 2);
g_assert_cmphex(qemu_opt_get_size(opts, "size1", 1),
==, 0x7ffffffffffffc00);
g_assert_cmphex(qemu_opt_get_size(opts, "size2", 1),
==, 0x7ffffffffffffc00);
/* Close to actual upper limit 0xfffffffffffff800 (53 msbs set) */
opts = qemu_opts_parse(&opts_list_02,
"size1=18446744073709549568," /* fffffffffffff800 */
"size2=18446744073709550591", /* fffffffffffffbff */
false, &error_abort);
g_assert_cmpuint(opts_count(opts), ==, 2);
g_assert_cmphex(qemu_opt_get_size(opts, "size1", 1),
==, 0xfffffffffffff800);
g_assert_cmphex(qemu_opt_get_size(opts, "size2", 1),
==, 0xfffffffffffff800);
/* Beyond limits */
opts = qemu_opts_parse(&opts_list_02, "size1=-1", false, &err);
error_free_or_abort(&err);
g_assert(!opts);
opts = qemu_opts_parse(&opts_list_02,
"size1=18446744073709550592", /* fffffffffffffc00 */
false, &err);
error_free_or_abort(&err);
g_assert(!opts);
/* Suffixes */
opts = qemu_opts_parse(&opts_list_02, "size1=8b,size2=1.5k,size3=2M",
false, &error_abort);
g_assert_cmpuint(opts_count(opts), ==, 3);
g_assert_cmphex(qemu_opt_get_size(opts, "size1", 0), ==, 8);
g_assert_cmphex(qemu_opt_get_size(opts, "size2", 0), ==, 1536);
g_assert_cmphex(qemu_opt_get_size(opts, "size3", 0), ==, 2 * MiB);
opts = qemu_opts_parse(&opts_list_02, "size1=0.1G,size2=16777215T",
false, &error_abort);
g_assert_cmpuint(opts_count(opts), ==, 2);
g_assert_cmphex(qemu_opt_get_size(opts, "size1", 0), ==, GiB / 10);
g_assert_cmphex(qemu_opt_get_size(opts, "size2", 0), ==, 16777215ULL * TiB);
/* Beyond limit with suffix */
opts = qemu_opts_parse(&opts_list_02, "size1=16777216T",
false, &err);
error_free_or_abort(&err);
g_assert(!opts);
/* Trailing crap */
opts = qemu_opts_parse(&opts_list_02, "size1=16E", false, &err);
error_free_or_abort(&err);
g_assert(!opts);
opts = qemu_opts_parse(&opts_list_02, "size1=16Gi", false, &err);
error_free_or_abort(&err);
g_assert(!opts);
qemu_opts_reset(&opts_list_02);
}
static void test_has_help_option(void)
{
static const struct {
const char *params;
/* expected value of qemu_opt_has_help_opt() with implied=false */
bool expect;
/* expected value of qemu_opt_has_help_opt() with implied=true */
bool expect_implied;
} test[] = {
{ "help", true, false },
{ "?", true, false },
{ "helpme", false, false },
{ "?me", false, false },
{ "a,help", true, true },
{ "a,?", true, true },
{ "a=0,help,b", true, true },
{ "a=0,?,b", true, true },
{ "help,b=1", true, false },
{ "?,b=1", true, false },
{ "a,b,,help", true, true },
{ "a,b,,?", true, true },
};
int i;
QemuOpts *opts;
for (i = 0; i < ARRAY_SIZE(test); i++) {
g_assert_cmpint(has_help_option(test[i].params),
==, test[i].expect);
opts = qemu_opts_parse(&opts_list_03, test[i].params, false,
&error_abort);
g_assert_cmpint(qemu_opt_has_help_opt(opts),
==, test[i].expect);
qemu_opts_del(opts);
opts = qemu_opts_parse(&opts_list_03, test[i].params, true,
&error_abort);
g_assert_cmpint(qemu_opt_has_help_opt(opts),
==, test[i].expect_implied);
qemu_opts_del(opts);
}
}
static void append_verify_list_01(QemuOptDesc *desc, bool with_overlapping)
{
int i = 0;
if (with_overlapping) {
g_assert_cmpstr(desc[i].name, ==, "str1");
g_assert_cmpint(desc[i].type, ==, QEMU_OPT_STRING);
g_assert_cmpstr(desc[i].help, ==,
"Help texts are preserved in qemu_opts_append");
g_assert_cmpstr(desc[i].def_value_str, ==, "default");
i++;
g_assert_cmpstr(desc[i].name, ==, "str2");
g_assert_cmpint(desc[i].type, ==, QEMU_OPT_STRING);
g_assert_cmpstr(desc[i].help, ==, NULL);
g_assert_cmpstr(desc[i].def_value_str, ==, NULL);
i++;
}
g_assert_cmpstr(desc[i].name, ==, "str3");
g_assert_cmpint(desc[i].type, ==, QEMU_OPT_STRING);
g_assert_cmpstr(desc[i].help, ==, NULL);
g_assert_cmpstr(desc[i].def_value_str, ==, NULL);
i++;
g_assert_cmpstr(desc[i].name, ==, "number1");
g_assert_cmpint(desc[i].type, ==, QEMU_OPT_NUMBER);
g_assert_cmpstr(desc[i].help, ==,
"Having help texts only for some options is okay");
g_assert_cmpstr(desc[i].def_value_str, ==, NULL);
i++;
g_assert_cmpstr(desc[i].name, ==, "number2");
g_assert_cmpint(desc[i].type, ==, QEMU_OPT_NUMBER);
g_assert_cmpstr(desc[i].help, ==, NULL);
g_assert_cmpstr(desc[i].def_value_str, ==, NULL);
i++;
g_assert_cmpstr(desc[i].name, ==, NULL);
}
static void append_verify_list_02(QemuOptDesc *desc)
{
int i = 0;
g_assert_cmpstr(desc[i].name, ==, "str1");
g_assert_cmpint(desc[i].type, ==, QEMU_OPT_STRING);
g_assert_cmpstr(desc[i].help, ==, NULL);
g_assert_cmpstr(desc[i].def_value_str, ==, NULL);
i++;
g_assert_cmpstr(desc[i].name, ==, "str2");
g_assert_cmpint(desc[i].type, ==, QEMU_OPT_STRING);
g_assert_cmpstr(desc[i].help, ==, NULL);
g_assert_cmpstr(desc[i].def_value_str, ==, NULL);
i++;
g_assert_cmpstr(desc[i].name, ==, "bool1");
g_assert_cmpint(desc[i].type, ==, QEMU_OPT_BOOL);
g_assert_cmpstr(desc[i].help, ==, NULL);
g_assert_cmpstr(desc[i].def_value_str, ==, NULL);
i++;
g_assert_cmpstr(desc[i].name, ==, "bool2");
g_assert_cmpint(desc[i].type, ==, QEMU_OPT_BOOL);
g_assert_cmpstr(desc[i].help, ==, NULL);
g_assert_cmpstr(desc[i].def_value_str, ==, NULL);
i++;
g_assert_cmpstr(desc[i].name, ==, "size1");
g_assert_cmpint(desc[i].type, ==, QEMU_OPT_SIZE);
g_assert_cmpstr(desc[i].help, ==, NULL);
g_assert_cmpstr(desc[i].def_value_str, ==, NULL);
i++;
g_assert_cmpstr(desc[i].name, ==, "size2");
g_assert_cmpint(desc[i].type, ==, QEMU_OPT_SIZE);
g_assert_cmpstr(desc[i].help, ==, NULL);
g_assert_cmpstr(desc[i].def_value_str, ==, NULL);
i++;
g_assert_cmpstr(desc[i].name, ==, "size3");
g_assert_cmpint(desc[i].type, ==, QEMU_OPT_SIZE);
g_assert_cmpstr(desc[i].help, ==, NULL);
g_assert_cmpstr(desc[i].def_value_str, ==, NULL);
}
static void test_opts_append_to_null(void)
{
QemuOptsList *merged;
merged = qemu_opts_append(NULL, &opts_list_01);
g_assert(merged != &opts_list_01);
g_assert_cmpstr(merged->name, ==, NULL);
g_assert_cmpstr(merged->implied_opt_name, ==, NULL);
g_assert_false(merged->merge_lists);
append_verify_list_01(merged->desc, true);
qemu_opts_free(merged);
}
static void test_opts_append(void)
{
QemuOptsList *first, *merged;
first = qemu_opts_append(NULL, &opts_list_02);
merged = qemu_opts_append(first, &opts_list_01);
g_assert(first != &opts_list_02);
g_assert(merged != &opts_list_01);
g_assert_cmpstr(merged->name, ==, NULL);
g_assert_cmpstr(merged->implied_opt_name, ==, NULL);
g_assert_false(merged->merge_lists);
append_verify_list_02(&merged->desc[0]);
append_verify_list_01(&merged->desc[7], false);
qemu_opts_free(merged);
}
static void test_opts_to_qdict_basic(void)
{
QemuOpts *opts;
QDict *dict;
opts = qemu_opts_parse(&opts_list_01, "str1=foo,str2=,str3=bar,number1=42",
false, &error_abort);
g_assert(opts != NULL);
dict = qemu_opts_to_qdict(opts, NULL);
g_assert(dict != NULL);
g_assert_cmpstr(qdict_get_str(dict, "str1"), ==, "foo");
g_assert_cmpstr(qdict_get_str(dict, "str2"), ==, "");
g_assert_cmpstr(qdict_get_str(dict, "str3"), ==, "bar");
g_assert_cmpstr(qdict_get_str(dict, "number1"), ==, "42");
g_assert_false(qdict_haskey(dict, "number2"));
qobject_unref(dict);
qemu_opts_del(opts);
}
static void test_opts_to_qdict_filtered(void)
{
QemuOptsList *first, *merged;
QemuOpts *opts;
QDict *dict;
first = qemu_opts_append(NULL, &opts_list_02);
merged = qemu_opts_append(first, &opts_list_01);
opts = qemu_opts_parse(merged,
"str1=foo,str2=,str3=bar,bool1=off,number1=42",
false, &error_abort);
g_assert(opts != NULL);
/* Convert to QDict without deleting from opts */
dict = qemu_opts_to_qdict_filtered(opts, NULL, &opts_list_01, false);
g_assert(dict != NULL);
g_assert_cmpstr(qdict_get_str(dict, "str1"), ==, "foo");
g_assert_cmpstr(qdict_get_str(dict, "str2"), ==, "");
g_assert_cmpstr(qdict_get_str(dict, "str3"), ==, "bar");
g_assert_cmpstr(qdict_get_str(dict, "number1"), ==, "42");
g_assert_false(qdict_haskey(dict, "number2"));
g_assert_false(qdict_haskey(dict, "bool1"));
qobject_unref(dict);
dict = qemu_opts_to_qdict_filtered(opts, NULL, &opts_list_02, false);
g_assert(dict != NULL);
g_assert_cmpstr(qdict_get_str(dict, "str1"), ==, "foo");
g_assert_cmpstr(qdict_get_str(dict, "str2"), ==, "");
g_assert_cmpstr(qdict_get_str(dict, "bool1"), ==, "off");
g_assert_false(qdict_haskey(dict, "str3"));
g_assert_false(qdict_haskey(dict, "number1"));
g_assert_false(qdict_haskey(dict, "number2"));
qobject_unref(dict);
/* Now delete converted options from opts */
dict = qemu_opts_to_qdict_filtered(opts, NULL, &opts_list_01, true);
g_assert(dict != NULL);
g_assert_cmpstr(qdict_get_str(dict, "str1"), ==, "foo");
g_assert_cmpstr(qdict_get_str(dict, "str2"), ==, "");
g_assert_cmpstr(qdict_get_str(dict, "str3"), ==, "bar");
g_assert_cmpstr(qdict_get_str(dict, "number1"), ==, "42");
g_assert_false(qdict_haskey(dict, "number2"));
g_assert_false(qdict_haskey(dict, "bool1"));
qobject_unref(dict);
dict = qemu_opts_to_qdict_filtered(opts, NULL, &opts_list_02, true);
g_assert(dict != NULL);
g_assert_cmpstr(qdict_get_str(dict, "bool1"), ==, "off");
g_assert_false(qdict_haskey(dict, "str1"));
g_assert_false(qdict_haskey(dict, "str2"));
g_assert_false(qdict_haskey(dict, "str3"));
g_assert_false(qdict_haskey(dict, "number1"));
g_assert_false(qdict_haskey(dict, "number2"));
qobject_unref(dict);
g_assert_true(QTAILQ_EMPTY(&opts->head));
qemu_opts_del(opts);
qemu_opts_free(merged);
}
static void test_opts_to_qdict_duplicates(void)
{
QemuOpts *opts;
QemuOpt *opt;
QDict *dict;
opts = qemu_opts_parse(&opts_list_03, "foo=a,foo=b", false, &error_abort);
g_assert(opts != NULL);
/* Verify that opts has two options with the same name */
opt = QTAILQ_FIRST(&opts->head);
g_assert_cmpstr(opt->name, ==, "foo");
g_assert_cmpstr(opt->str , ==, "a");
opt = QTAILQ_NEXT(opt, next);
g_assert_cmpstr(opt->name, ==, "foo");
g_assert_cmpstr(opt->str , ==, "b");
opt = QTAILQ_NEXT(opt, next);
g_assert(opt == NULL);
/* In the conversion to QDict, the last one wins */
dict = qemu_opts_to_qdict(opts, NULL);
g_assert(dict != NULL);
g_assert_cmpstr(qdict_get_str(dict, "foo"), ==, "b");
qobject_unref(dict);
/* The last one still wins if entries are deleted, and both are deleted */
dict = qemu_opts_to_qdict_filtered(opts, NULL, NULL, true);
g_assert(dict != NULL);
g_assert_cmpstr(qdict_get_str(dict, "foo"), ==, "b");
qobject_unref(dict);
g_assert_true(QTAILQ_EMPTY(&opts->head));
qemu_opts_del(opts);
}
int main(int argc, char *argv[])
{
register_opts();
g_test_init(&argc, &argv, NULL);
g_test_add_func("/qemu-opts/find_unknown_opts", test_find_unknown_opts);
g_test_add_func("/qemu-opts/find_opts", test_qemu_find_opts);
g_test_add_func("/qemu-opts/opts_create", test_qemu_opts_create);
g_test_add_func("/qemu-opts/opt_get", test_qemu_opt_get);
g_test_add_func("/qemu-opts/opt_get_bool", test_qemu_opt_get_bool);
g_test_add_func("/qemu-opts/opt_get_number", test_qemu_opt_get_number);
g_test_add_func("/qemu-opts/opt_get_size", test_qemu_opt_get_size);
g_test_add_func("/qemu-opts/opt_unset", test_qemu_opt_unset);
g_test_add_func("/qemu-opts/opts_reset", test_qemu_opts_reset);
g_test_add_func("/qemu-opts/opts_set", test_qemu_opts_set);
g_test_add_func("/qemu-opts/opts_parse/general", test_opts_parse);
g_test_add_func("/qemu-opts/opts_parse/bool", test_opts_parse_bool);
g_test_add_func("/qemu-opts/opts_parse/number", test_opts_parse_number);
g_test_add_func("/qemu-opts/opts_parse/size", test_opts_parse_size);
g_test_add_func("/qemu-opts/has_help_option", test_has_help_option);
g_test_add_func("/qemu-opts/append_to_null", test_opts_append_to_null);
g_test_add_func("/qemu-opts/append", test_opts_append);
g_test_add_func("/qemu-opts/to_qdict/basic", test_opts_to_qdict_basic);
g_test_add_func("/qemu-opts/to_qdict/filtered", test_opts_to_qdict_filtered);
g_test_add_func("/qemu-opts/to_qdict/duplicates", test_opts_to_qdict_duplicates);
g_test_run();
return 0;
}
|