sdhci.c: Limit the maximum block size
[qemu/ar7.git] / tests / test-qemu-opts.c
blob0c1136d1b2aceb976964d8b464585c94cf702f51
1 /*
2 * QemuOpts unit-tests.
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.
8 */
10 #include "qapi/error.h"
11 #include "qapi/qmp/qstring.h"
12 #include "qemu/config-file.h"
14 #include <glib.h>
15 #include <string.h>
17 static QemuOptsList opts_list_01 = {
18 .name = "opts_list_01",
19 .head = QTAILQ_HEAD_INITIALIZER(opts_list_01.head),
20 .desc = {
22 .name = "str1",
23 .type = QEMU_OPT_STRING,
24 },{
25 .name = "str2",
26 .type = QEMU_OPT_STRING,
27 },{
28 .name = "str3",
29 .type = QEMU_OPT_STRING,
30 },{
31 .name = "number1",
32 .type = QEMU_OPT_NUMBER,
34 { /* end of list */ }
38 static QemuOptsList opts_list_02 = {
39 .name = "opts_list_02",
40 .head = QTAILQ_HEAD_INITIALIZER(opts_list_02.head),
41 .desc = {
43 .name = "str1",
44 .type = QEMU_OPT_STRING,
45 },{
46 .name = "bool1",
47 .type = QEMU_OPT_BOOL,
48 },{
49 .name = "str2",
50 .type = QEMU_OPT_STRING,
51 },{
52 .name = "size1",
53 .type = QEMU_OPT_SIZE,
55 { /* end of list */ }
59 static QemuOptsList opts_list_03 = {
60 .name = "opts_list_03",
61 .head = QTAILQ_HEAD_INITIALIZER(opts_list_03.head),
62 .desc = {
63 /* no elements => accept any params */
64 { /* end of list */ }
68 static void register_opts(void)
70 qemu_add_opts(&opts_list_01);
71 qemu_add_opts(&opts_list_02);
72 qemu_add_opts(&opts_list_03);
75 static void test_find_unknown_opts(void)
77 QemuOptsList *list;
78 Error *err = NULL;
80 /* should not return anything, we don't have an "unknown" option */
81 list = qemu_find_opts_err("unknown", &err);
82 g_assert(list == NULL);
83 g_assert(err);
84 error_free(err);
87 static void test_qemu_find_opts(void)
89 QemuOptsList *list;
91 /* we have an "opts_list_01" option, should return it */
92 list = qemu_find_opts("opts_list_01");
93 g_assert(list != NULL);
94 g_assert_cmpstr(list->name, ==, "opts_list_01");
97 static void test_qemu_opts_create(void)
99 QemuOptsList *list;
100 QemuOpts *opts;
102 list = qemu_find_opts("opts_list_01");
103 g_assert(list != NULL);
104 g_assert(QTAILQ_EMPTY(&list->head));
105 g_assert_cmpstr(list->name, ==, "opts_list_01");
107 /* should not find anything at this point */
108 opts = qemu_opts_find(list, NULL);
109 g_assert(opts == NULL);
111 /* create the opts */
112 opts = qemu_opts_create(list, NULL, 0, &error_abort);
113 g_assert(opts != NULL);
114 g_assert(!QTAILQ_EMPTY(&list->head));
116 /* now we've create the opts, must find it */
117 opts = qemu_opts_find(list, NULL);
118 g_assert(opts != NULL);
120 qemu_opts_del(opts);
122 /* should not find anything at this point */
123 opts = qemu_opts_find(list, NULL);
124 g_assert(opts == NULL);
127 static void test_qemu_opt_get(void)
129 QemuOptsList *list;
130 QemuOpts *opts;
131 const char *opt = NULL;
133 list = qemu_find_opts("opts_list_01");
134 g_assert(list != NULL);
135 g_assert(QTAILQ_EMPTY(&list->head));
136 g_assert_cmpstr(list->name, ==, "opts_list_01");
138 /* should not find anything at this point */
139 opts = qemu_opts_find(list, NULL);
140 g_assert(opts == NULL);
142 /* create the opts */
143 opts = qemu_opts_create(list, NULL, 0, &error_abort);
144 g_assert(opts != NULL);
145 g_assert(!QTAILQ_EMPTY(&list->head));
147 /* haven't set anything to str2 yet */
148 opt = qemu_opt_get(opts, "str2");
149 g_assert(opt == NULL);
151 qemu_opt_set(opts, "str2", "value", &error_abort);
153 /* now we have set str2, should know about it */
154 opt = qemu_opt_get(opts, "str2");
155 g_assert_cmpstr(opt, ==, "value");
157 qemu_opt_set(opts, "str2", "value2", &error_abort);
159 /* having reset the value, the returned should be the reset one */
160 opt = qemu_opt_get(opts, "str2");
161 g_assert_cmpstr(opt, ==, "value2");
163 qemu_opts_del(opts);
165 /* should not find anything at this point */
166 opts = qemu_opts_find(list, NULL);
167 g_assert(opts == NULL);
170 static void test_qemu_opt_get_bool(void)
172 Error *err = NULL;
173 QemuOptsList *list;
174 QemuOpts *opts;
175 bool opt;
177 list = qemu_find_opts("opts_list_02");
178 g_assert(list != NULL);
179 g_assert(QTAILQ_EMPTY(&list->head));
180 g_assert_cmpstr(list->name, ==, "opts_list_02");
182 /* should not find anything at this point */
183 opts = qemu_opts_find(list, NULL);
184 g_assert(opts == NULL);
186 /* create the opts */
187 opts = qemu_opts_create(list, NULL, 0, &error_abort);
188 g_assert(opts != NULL);
189 g_assert(!QTAILQ_EMPTY(&list->head));
191 /* haven't set anything to bool1 yet, so defval should be returned */
192 opt = qemu_opt_get_bool(opts, "bool1", false);
193 g_assert(opt == false);
195 qemu_opt_set_bool(opts, "bool1", true, &err);
196 g_assert(!err);
198 /* now we have set bool1, should know about it */
199 opt = qemu_opt_get_bool(opts, "bool1", false);
200 g_assert(opt == true);
202 /* having reset the value, opt should be the reset one not defval */
203 qemu_opt_set_bool(opts, "bool1", false, &err);
204 g_assert(!err);
206 opt = qemu_opt_get_bool(opts, "bool1", true);
207 g_assert(opt == false);
209 qemu_opts_del(opts);
211 /* should not find anything at this point */
212 opts = qemu_opts_find(list, NULL);
213 g_assert(opts == NULL);
216 static void test_qemu_opt_get_number(void)
218 Error *err = NULL;
219 QemuOptsList *list;
220 QemuOpts *opts;
221 uint64_t opt;
223 list = qemu_find_opts("opts_list_01");
224 g_assert(list != NULL);
225 g_assert(QTAILQ_EMPTY(&list->head));
226 g_assert_cmpstr(list->name, ==, "opts_list_01");
228 /* should not find anything at this point */
229 opts = qemu_opts_find(list, NULL);
230 g_assert(opts == NULL);
232 /* create the opts */
233 opts = qemu_opts_create(list, NULL, 0, &error_abort);
234 g_assert(opts != NULL);
235 g_assert(!QTAILQ_EMPTY(&list->head));
237 /* haven't set anything to number1 yet, so defval should be returned */
238 opt = qemu_opt_get_number(opts, "number1", 5);
239 g_assert(opt == 5);
241 qemu_opt_set_number(opts, "number1", 10, &err);
242 g_assert(!err);
244 /* now we have set number1, should know about it */
245 opt = qemu_opt_get_number(opts, "number1", 5);
246 g_assert(opt == 10);
248 /* having reset it, the returned should be the reset one not defval */
249 qemu_opt_set_number(opts, "number1", 15, &err);
250 g_assert(!err);
252 opt = qemu_opt_get_number(opts, "number1", 5);
253 g_assert(opt == 15);
255 qemu_opts_del(opts);
257 /* should not find anything at this point */
258 opts = qemu_opts_find(list, NULL);
259 g_assert(opts == NULL);
262 static void test_qemu_opt_get_size(void)
264 QemuOptsList *list;
265 QemuOpts *opts;
266 uint64_t opt;
267 QDict *dict;
269 list = qemu_find_opts("opts_list_02");
270 g_assert(list != NULL);
271 g_assert(QTAILQ_EMPTY(&list->head));
272 g_assert_cmpstr(list->name, ==, "opts_list_02");
274 /* should not find anything at this point */
275 opts = qemu_opts_find(list, NULL);
276 g_assert(opts == NULL);
278 /* create the opts */
279 opts = qemu_opts_create(list, NULL, 0, &error_abort);
280 g_assert(opts != NULL);
281 g_assert(!QTAILQ_EMPTY(&list->head));
283 /* haven't set anything to size1 yet, so defval should be returned */
284 opt = qemu_opt_get_size(opts, "size1", 5);
285 g_assert(opt == 5);
287 dict = qdict_new();
288 g_assert(dict != NULL);
290 qdict_put(dict, "size1", qstring_from_str("10"));
292 qemu_opts_absorb_qdict(opts, dict, &error_abort);
293 g_assert(error_abort == NULL);
295 /* now we have set size1, should know about it */
296 opt = qemu_opt_get_size(opts, "size1", 5);
297 g_assert(opt == 10);
299 /* reset value */
300 qdict_put(dict, "size1", qstring_from_str("15"));
302 qemu_opts_absorb_qdict(opts, dict, &error_abort);
303 g_assert(error_abort == NULL);
305 /* test the reset value */
306 opt = qemu_opt_get_size(opts, "size1", 5);
307 g_assert(opt == 15);
309 qdict_del(dict, "size1");
310 g_free(dict);
312 qemu_opts_del(opts);
314 /* should not find anything at this point */
315 opts = qemu_opts_find(list, NULL);
316 g_assert(opts == NULL);
319 static void test_qemu_opt_unset(void)
321 QemuOpts *opts;
322 const char *value;
323 int ret;
325 /* dynamically initialized (parsed) opts */
326 opts = qemu_opts_parse(&opts_list_03, "key=value", false, NULL);
327 g_assert(opts != NULL);
329 /* check default/parsed value */
330 value = qemu_opt_get(opts, "key");
331 g_assert_cmpstr(value, ==, "value");
333 /* reset it to value2 */
334 qemu_opt_set(opts, "key", "value2", &error_abort);
336 value = qemu_opt_get(opts, "key");
337 g_assert_cmpstr(value, ==, "value2");
339 /* unset, valid only for "accept any" */
340 ret = qemu_opt_unset(opts, "key");
341 g_assert(ret == 0);
343 /* after reset the value should be the parsed/default one */
344 value = qemu_opt_get(opts, "key");
345 g_assert_cmpstr(value, ==, "value");
347 qemu_opts_del(opts);
350 static void test_qemu_opts_reset(void)
352 Error *err = NULL;
353 QemuOptsList *list;
354 QemuOpts *opts;
355 uint64_t opt;
357 list = qemu_find_opts("opts_list_01");
358 g_assert(list != NULL);
359 g_assert(QTAILQ_EMPTY(&list->head));
360 g_assert_cmpstr(list->name, ==, "opts_list_01");
362 /* should not find anything at this point */
363 opts = qemu_opts_find(list, NULL);
364 g_assert(opts == NULL);
366 /* create the opts */
367 opts = qemu_opts_create(list, NULL, 0, &error_abort);
368 g_assert(opts != NULL);
369 g_assert(!QTAILQ_EMPTY(&list->head));
371 /* haven't set anything to number1 yet, so defval should be returned */
372 opt = qemu_opt_get_number(opts, "number1", 5);
373 g_assert(opt == 5);
375 qemu_opt_set_number(opts, "number1", 10, &err);
376 g_assert(!err);
378 /* now we have set number1, should know about it */
379 opt = qemu_opt_get_number(opts, "number1", 5);
380 g_assert(opt == 10);
382 qemu_opts_reset(list);
384 /* should not find anything at this point */
385 opts = qemu_opts_find(list, NULL);
386 g_assert(opts == NULL);
389 static void test_qemu_opts_set(void)
391 Error *err = NULL;
392 QemuOptsList *list;
393 QemuOpts *opts;
394 const char *opt;
396 list = qemu_find_opts("opts_list_01");
397 g_assert(list != NULL);
398 g_assert(QTAILQ_EMPTY(&list->head));
399 g_assert_cmpstr(list->name, ==, "opts_list_01");
401 /* should not find anything at this point */
402 opts = qemu_opts_find(list, NULL);
403 g_assert(opts == NULL);
405 /* implicitly create opts and set str3 value */
406 qemu_opts_set(list, NULL, "str3", "value", &err);
407 g_assert(!err);
408 g_assert(!QTAILQ_EMPTY(&list->head));
410 /* get the just created opts */
411 opts = qemu_opts_find(list, NULL);
412 g_assert(opts != NULL);
414 /* check the str3 value */
415 opt = qemu_opt_get(opts, "str3");
416 g_assert_cmpstr(opt, ==, "value");
418 qemu_opts_del(opts);
420 /* should not find anything at this point */
421 opts = qemu_opts_find(list, NULL);
422 g_assert(opts == NULL);
425 int main(int argc, char *argv[])
427 register_opts();
428 g_test_init(&argc, &argv, NULL);
429 g_test_add_func("/qemu-opts/find_unknown_opts", test_find_unknown_opts);
430 g_test_add_func("/qemu-opts/find_opts", test_qemu_find_opts);
431 g_test_add_func("/qemu-opts/opts_create", test_qemu_opts_create);
432 g_test_add_func("/qemu-opts/opt_get", test_qemu_opt_get);
433 g_test_add_func("/qemu-opts/opt_get_bool", test_qemu_opt_get_bool);
434 g_test_add_func("/qemu-opts/opt_get_number", test_qemu_opt_get_number);
435 g_test_add_func("/qemu-opts/opt_get_size", test_qemu_opt_get_size);
436 g_test_add_func("/qemu-opts/opt_unset", test_qemu_opt_unset);
437 g_test_add_func("/qemu-opts/opts_reset", test_qemu_opts_reset);
438 g_test_add_func("/qemu-opts/opts_set", test_qemu_opts_set);
439 g_test_run();
440 return 0;