4 * Copyright (C) 2014 Leandro Dorileo <l@dorileo.org>
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 "qapi/error.h"
12 #include "qapi/qmp/qstring.h"
13 #include "qemu/config-file.h"
16 static QemuOptsList opts_list_01
= {
17 .name
= "opts_list_01",
18 .head
= QTAILQ_HEAD_INITIALIZER(opts_list_01
.head
),
22 .type
= QEMU_OPT_STRING
,
25 .type
= QEMU_OPT_STRING
,
28 .type
= QEMU_OPT_STRING
,
31 .type
= QEMU_OPT_NUMBER
,
37 static QemuOptsList opts_list_02
= {
38 .name
= "opts_list_02",
39 .head
= QTAILQ_HEAD_INITIALIZER(opts_list_02
.head
),
43 .type
= QEMU_OPT_STRING
,
46 .type
= QEMU_OPT_BOOL
,
49 .type
= QEMU_OPT_STRING
,
52 .type
= QEMU_OPT_SIZE
,
58 static QemuOptsList opts_list_03
= {
59 .name
= "opts_list_03",
60 .head
= QTAILQ_HEAD_INITIALIZER(opts_list_03
.head
),
62 /* no elements => accept any params */
67 static void register_opts(void)
69 qemu_add_opts(&opts_list_01
);
70 qemu_add_opts(&opts_list_02
);
71 qemu_add_opts(&opts_list_03
);
74 static void test_find_unknown_opts(void)
79 /* should not return anything, we don't have an "unknown" option */
80 list
= qemu_find_opts_err("unknown", &err
);
81 g_assert(list
== NULL
);
86 static void test_qemu_find_opts(void)
90 /* we have an "opts_list_01" option, should return it */
91 list
= qemu_find_opts("opts_list_01");
92 g_assert(list
!= NULL
);
93 g_assert_cmpstr(list
->name
, ==, "opts_list_01");
96 static void test_qemu_opts_create(void)
101 list
= qemu_find_opts("opts_list_01");
102 g_assert(list
!= NULL
);
103 g_assert(QTAILQ_EMPTY(&list
->head
));
104 g_assert_cmpstr(list
->name
, ==, "opts_list_01");
106 /* should not find anything at this point */
107 opts
= qemu_opts_find(list
, NULL
);
108 g_assert(opts
== NULL
);
110 /* create the opts */
111 opts
= qemu_opts_create(list
, NULL
, 0, &error_abort
);
112 g_assert(opts
!= NULL
);
113 g_assert(!QTAILQ_EMPTY(&list
->head
));
115 /* now we've create the opts, must find it */
116 opts
= qemu_opts_find(list
, NULL
);
117 g_assert(opts
!= NULL
);
121 /* should not find anything at this point */
122 opts
= qemu_opts_find(list
, NULL
);
123 g_assert(opts
== NULL
);
126 static void test_qemu_opt_get(void)
130 const char *opt
= NULL
;
132 list
= qemu_find_opts("opts_list_01");
133 g_assert(list
!= NULL
);
134 g_assert(QTAILQ_EMPTY(&list
->head
));
135 g_assert_cmpstr(list
->name
, ==, "opts_list_01");
137 /* should not find anything at this point */
138 opts
= qemu_opts_find(list
, NULL
);
139 g_assert(opts
== NULL
);
141 /* create the opts */
142 opts
= qemu_opts_create(list
, NULL
, 0, &error_abort
);
143 g_assert(opts
!= NULL
);
144 g_assert(!QTAILQ_EMPTY(&list
->head
));
146 /* haven't set anything to str2 yet */
147 opt
= qemu_opt_get(opts
, "str2");
148 g_assert(opt
== NULL
);
150 qemu_opt_set(opts
, "str2", "value", &error_abort
);
152 /* now we have set str2, should know about it */
153 opt
= qemu_opt_get(opts
, "str2");
154 g_assert_cmpstr(opt
, ==, "value");
156 qemu_opt_set(opts
, "str2", "value2", &error_abort
);
158 /* having reset the value, the returned should be the reset one */
159 opt
= qemu_opt_get(opts
, "str2");
160 g_assert_cmpstr(opt
, ==, "value2");
164 /* should not find anything at this point */
165 opts
= qemu_opts_find(list
, NULL
);
166 g_assert(opts
== NULL
);
169 static void test_qemu_opt_get_bool(void)
176 list
= qemu_find_opts("opts_list_02");
177 g_assert(list
!= NULL
);
178 g_assert(QTAILQ_EMPTY(&list
->head
));
179 g_assert_cmpstr(list
->name
, ==, "opts_list_02");
181 /* should not find anything at this point */
182 opts
= qemu_opts_find(list
, NULL
);
183 g_assert(opts
== NULL
);
185 /* create the opts */
186 opts
= qemu_opts_create(list
, NULL
, 0, &error_abort
);
187 g_assert(opts
!= NULL
);
188 g_assert(!QTAILQ_EMPTY(&list
->head
));
190 /* haven't set anything to bool1 yet, so defval should be returned */
191 opt
= qemu_opt_get_bool(opts
, "bool1", false);
192 g_assert(opt
== false);
194 qemu_opt_set_bool(opts
, "bool1", true, &err
);
197 /* now we have set bool1, should know about it */
198 opt
= qemu_opt_get_bool(opts
, "bool1", false);
199 g_assert(opt
== true);
201 /* having reset the value, opt should be the reset one not defval */
202 qemu_opt_set_bool(opts
, "bool1", false, &err
);
205 opt
= qemu_opt_get_bool(opts
, "bool1", true);
206 g_assert(opt
== false);
210 /* should not find anything at this point */
211 opts
= qemu_opts_find(list
, NULL
);
212 g_assert(opts
== NULL
);
215 static void test_qemu_opt_get_number(void)
222 list
= qemu_find_opts("opts_list_01");
223 g_assert(list
!= NULL
);
224 g_assert(QTAILQ_EMPTY(&list
->head
));
225 g_assert_cmpstr(list
->name
, ==, "opts_list_01");
227 /* should not find anything at this point */
228 opts
= qemu_opts_find(list
, NULL
);
229 g_assert(opts
== NULL
);
231 /* create the opts */
232 opts
= qemu_opts_create(list
, NULL
, 0, &error_abort
);
233 g_assert(opts
!= NULL
);
234 g_assert(!QTAILQ_EMPTY(&list
->head
));
236 /* haven't set anything to number1 yet, so defval should be returned */
237 opt
= qemu_opt_get_number(opts
, "number1", 5);
240 qemu_opt_set_number(opts
, "number1", 10, &err
);
243 /* now we have set number1, should know about it */
244 opt
= qemu_opt_get_number(opts
, "number1", 5);
247 /* having reset it, the returned should be the reset one not defval */
248 qemu_opt_set_number(opts
, "number1", 15, &err
);
251 opt
= qemu_opt_get_number(opts
, "number1", 5);
256 /* should not find anything at this point */
257 opts
= qemu_opts_find(list
, NULL
);
258 g_assert(opts
== NULL
);
261 static void test_qemu_opt_get_size(void)
268 list
= qemu_find_opts("opts_list_02");
269 g_assert(list
!= NULL
);
270 g_assert(QTAILQ_EMPTY(&list
->head
));
271 g_assert_cmpstr(list
->name
, ==, "opts_list_02");
273 /* should not find anything at this point */
274 opts
= qemu_opts_find(list
, NULL
);
275 g_assert(opts
== NULL
);
277 /* create the opts */
278 opts
= qemu_opts_create(list
, NULL
, 0, &error_abort
);
279 g_assert(opts
!= NULL
);
280 g_assert(!QTAILQ_EMPTY(&list
->head
));
282 /* haven't set anything to size1 yet, so defval should be returned */
283 opt
= qemu_opt_get_size(opts
, "size1", 5);
287 g_assert(dict
!= NULL
);
289 qdict_put(dict
, "size1", qstring_from_str("10"));
291 qemu_opts_absorb_qdict(opts
, dict
, &error_abort
);
292 g_assert(error_abort
== NULL
);
294 /* now we have set size1, should know about it */
295 opt
= qemu_opt_get_size(opts
, "size1", 5);
299 qdict_put(dict
, "size1", qstring_from_str("15"));
301 qemu_opts_absorb_qdict(opts
, dict
, &error_abort
);
302 g_assert(error_abort
== NULL
);
304 /* test the reset value */
305 opt
= qemu_opt_get_size(opts
, "size1", 5);
308 qdict_del(dict
, "size1");
313 /* should not find anything at this point */
314 opts
= qemu_opts_find(list
, NULL
);
315 g_assert(opts
== NULL
);
318 static void test_qemu_opt_unset(void)
324 /* dynamically initialized (parsed) opts */
325 opts
= qemu_opts_parse(&opts_list_03
, "key=value", false, NULL
);
326 g_assert(opts
!= NULL
);
328 /* check default/parsed value */
329 value
= qemu_opt_get(opts
, "key");
330 g_assert_cmpstr(value
, ==, "value");
332 /* reset it to value2 */
333 qemu_opt_set(opts
, "key", "value2", &error_abort
);
335 value
= qemu_opt_get(opts
, "key");
336 g_assert_cmpstr(value
, ==, "value2");
338 /* unset, valid only for "accept any" */
339 ret
= qemu_opt_unset(opts
, "key");
342 /* after reset the value should be the parsed/default one */
343 value
= qemu_opt_get(opts
, "key");
344 g_assert_cmpstr(value
, ==, "value");
349 static void test_qemu_opts_reset(void)
356 list
= qemu_find_opts("opts_list_01");
357 g_assert(list
!= NULL
);
358 g_assert(QTAILQ_EMPTY(&list
->head
));
359 g_assert_cmpstr(list
->name
, ==, "opts_list_01");
361 /* should not find anything at this point */
362 opts
= qemu_opts_find(list
, NULL
);
363 g_assert(opts
== NULL
);
365 /* create the opts */
366 opts
= qemu_opts_create(list
, NULL
, 0, &error_abort
);
367 g_assert(opts
!= NULL
);
368 g_assert(!QTAILQ_EMPTY(&list
->head
));
370 /* haven't set anything to number1 yet, so defval should be returned */
371 opt
= qemu_opt_get_number(opts
, "number1", 5);
374 qemu_opt_set_number(opts
, "number1", 10, &err
);
377 /* now we have set number1, should know about it */
378 opt
= qemu_opt_get_number(opts
, "number1", 5);
381 qemu_opts_reset(list
);
383 /* should not find anything at this point */
384 opts
= qemu_opts_find(list
, NULL
);
385 g_assert(opts
== NULL
);
388 static void test_qemu_opts_set(void)
395 list
= qemu_find_opts("opts_list_01");
396 g_assert(list
!= NULL
);
397 g_assert(QTAILQ_EMPTY(&list
->head
));
398 g_assert_cmpstr(list
->name
, ==, "opts_list_01");
400 /* should not find anything at this point */
401 opts
= qemu_opts_find(list
, NULL
);
402 g_assert(opts
== NULL
);
404 /* implicitly create opts and set str3 value */
405 qemu_opts_set(list
, NULL
, "str3", "value", &err
);
407 g_assert(!QTAILQ_EMPTY(&list
->head
));
409 /* get the just created opts */
410 opts
= qemu_opts_find(list
, NULL
);
411 g_assert(opts
!= NULL
);
413 /* check the str3 value */
414 opt
= qemu_opt_get(opts
, "str3");
415 g_assert_cmpstr(opt
, ==, "value");
419 /* should not find anything at this point */
420 opts
= qemu_opts_find(list
, NULL
);
421 g_assert(opts
== NULL
);
424 int main(int argc
, char *argv
[])
427 g_test_init(&argc
, &argv
, NULL
);
428 g_test_add_func("/qemu-opts/find_unknown_opts", test_find_unknown_opts
);
429 g_test_add_func("/qemu-opts/find_opts", test_qemu_find_opts
);
430 g_test_add_func("/qemu-opts/opts_create", test_qemu_opts_create
);
431 g_test_add_func("/qemu-opts/opt_get", test_qemu_opt_get
);
432 g_test_add_func("/qemu-opts/opt_get_bool", test_qemu_opt_get_bool
);
433 g_test_add_func("/qemu-opts/opt_get_number", test_qemu_opt_get_number
);
434 g_test_add_func("/qemu-opts/opt_get_size", test_qemu_opt_get_size
);
435 g_test_add_func("/qemu-opts/opt_unset", test_qemu_opt_unset
);
436 g_test_add_func("/qemu-opts/opts_reset", test_qemu_opts_reset
);
437 g_test_add_func("/qemu-opts/opts_set", test_qemu_opts_set
);