]> git.proxmox.com Git - mirror_qemu.git/blob - tests/check-qom-proplist.c
tests/check-qom-proplist: Improve iterator coverage
[mirror_qemu.git] / tests / check-qom-proplist.c
1 /*
2 * Copyright (C) 2015 Red Hat, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
17 *
18 * Author: Daniel P. Berrange <berrange@redhat.com>
19 */
20
21 #include "qemu/osdep.h"
22
23 #include "qapi/error.h"
24 #include "qom/object.h"
25 #include "qemu/module.h"
26 #include "qemu/option.h"
27 #include "qemu/config-file.h"
28 #include "qom/object_interfaces.h"
29
30
31 #define TYPE_DUMMY "qemu-dummy"
32
33 typedef struct DummyObject DummyObject;
34 typedef struct DummyObjectClass DummyObjectClass;
35
36 #define DUMMY_OBJECT(obj) \
37 OBJECT_CHECK(DummyObject, (obj), TYPE_DUMMY)
38
39 typedef enum DummyAnimal DummyAnimal;
40
41 enum DummyAnimal {
42 DUMMY_FROG,
43 DUMMY_ALLIGATOR,
44 DUMMY_PLATYPUS,
45
46 DUMMY_LAST,
47 };
48
49 const QEnumLookup dummy_animal_map = {
50 .array = (const char *const[]) {
51 [DUMMY_FROG] = "frog",
52 [DUMMY_ALLIGATOR] = "alligator",
53 [DUMMY_PLATYPUS] = "platypus",
54 },
55 .size = DUMMY_LAST
56 };
57
58 struct DummyObject {
59 Object parent_obj;
60
61 bool bv;
62 DummyAnimal av;
63 char *sv;
64 };
65
66 struct DummyObjectClass {
67 ObjectClass parent_class;
68 };
69
70
71 static void dummy_set_bv(Object *obj,
72 bool value,
73 Error **errp)
74 {
75 DummyObject *dobj = DUMMY_OBJECT(obj);
76
77 dobj->bv = value;
78 }
79
80 static bool dummy_get_bv(Object *obj,
81 Error **errp)
82 {
83 DummyObject *dobj = DUMMY_OBJECT(obj);
84
85 return dobj->bv;
86 }
87
88
89 static void dummy_set_av(Object *obj,
90 int value,
91 Error **errp)
92 {
93 DummyObject *dobj = DUMMY_OBJECT(obj);
94
95 dobj->av = value;
96 }
97
98 static int dummy_get_av(Object *obj,
99 Error **errp)
100 {
101 DummyObject *dobj = DUMMY_OBJECT(obj);
102
103 return dobj->av;
104 }
105
106
107 static void dummy_set_sv(Object *obj,
108 const char *value,
109 Error **errp)
110 {
111 DummyObject *dobj = DUMMY_OBJECT(obj);
112
113 g_free(dobj->sv);
114 dobj->sv = g_strdup(value);
115 }
116
117 static char *dummy_get_sv(Object *obj,
118 Error **errp)
119 {
120 DummyObject *dobj = DUMMY_OBJECT(obj);
121
122 return g_strdup(dobj->sv);
123 }
124
125
126 static void dummy_init(Object *obj)
127 {
128 Error *err = NULL;
129
130 object_property_add_bool(obj, "bv",
131 dummy_get_bv,
132 dummy_set_bv,
133 NULL);
134 /* duplicate: */
135 object_property_add_str(obj, "sv",
136 dummy_get_sv,
137 dummy_set_sv,
138 &err);
139 error_free_or_abort(&err);
140 }
141
142
143 static void dummy_class_init(ObjectClass *cls, void *data)
144 {
145 object_class_property_add_str(cls, "sv",
146 dummy_get_sv,
147 dummy_set_sv,
148 NULL);
149 object_class_property_add_enum(cls, "av",
150 "DummyAnimal",
151 &dummy_animal_map,
152 dummy_get_av,
153 dummy_set_av,
154 NULL);
155 }
156
157
158 static void dummy_finalize(Object *obj)
159 {
160 DummyObject *dobj = DUMMY_OBJECT(obj);
161
162 g_free(dobj->sv);
163 }
164
165
166 static const TypeInfo dummy_info = {
167 .name = TYPE_DUMMY,
168 .parent = TYPE_OBJECT,
169 .instance_size = sizeof(DummyObject),
170 .instance_init = dummy_init,
171 .instance_finalize = dummy_finalize,
172 .class_size = sizeof(DummyObjectClass),
173 .class_init = dummy_class_init,
174 .interfaces = (InterfaceInfo[]) {
175 { TYPE_USER_CREATABLE },
176 { }
177 }
178 };
179
180
181 /*
182 * The following 3 object classes are used to
183 * simulate the kind of relationships seen in
184 * qdev, which result in complex object
185 * property destruction ordering.
186 *
187 * DummyDev has a 'bus' child to a DummyBus
188 * DummyBus has a 'backend' child to a DummyBackend
189 * DummyDev has a 'backend' link to DummyBackend
190 *
191 * When DummyDev is finalized, it unparents the
192 * DummyBackend, which unparents the DummyDev
193 * which deletes the 'backend' link from DummyDev
194 * to DummyBackend. This illustrates that the
195 * object_property_del_all() method needs to
196 * cope with the list of properties being changed
197 * while it iterates over them.
198 */
199 typedef struct DummyDev DummyDev;
200 typedef struct DummyDevClass DummyDevClass;
201 typedef struct DummyBus DummyBus;
202 typedef struct DummyBusClass DummyBusClass;
203 typedef struct DummyBackend DummyBackend;
204 typedef struct DummyBackendClass DummyBackendClass;
205
206 #define TYPE_DUMMY_DEV "qemu-dummy-dev"
207 #define TYPE_DUMMY_BUS "qemu-dummy-bus"
208 #define TYPE_DUMMY_BACKEND "qemu-dummy-backend"
209
210 #define DUMMY_DEV(obj) \
211 OBJECT_CHECK(DummyDev, (obj), TYPE_DUMMY_DEV)
212 #define DUMMY_BUS(obj) \
213 OBJECT_CHECK(DummyBus, (obj), TYPE_DUMMY_BUS)
214 #define DUMMY_BACKEND(obj) \
215 OBJECT_CHECK(DummyBackend, (obj), TYPE_DUMMY_BACKEND)
216
217 struct DummyDev {
218 Object parent_obj;
219
220 DummyBus *bus;
221 };
222
223 struct DummyDevClass {
224 ObjectClass parent_class;
225 };
226
227 struct DummyBus {
228 Object parent_obj;
229
230 DummyBackend *backend;
231 };
232
233 struct DummyBusClass {
234 ObjectClass parent_class;
235 };
236
237 struct DummyBackend {
238 Object parent_obj;
239 };
240
241 struct DummyBackendClass {
242 ObjectClass parent_class;
243 };
244
245
246 static void dummy_dev_finalize(Object *obj)
247 {
248 DummyDev *dev = DUMMY_DEV(obj);
249
250 object_unref(OBJECT(dev->bus));
251 }
252
253 static void dummy_dev_init(Object *obj)
254 {
255 DummyDev *dev = DUMMY_DEV(obj);
256 DummyBus *bus = DUMMY_BUS(object_new(TYPE_DUMMY_BUS));
257 DummyBackend *backend = DUMMY_BACKEND(object_new(TYPE_DUMMY_BACKEND));
258
259 object_property_add_child(obj, "bus", OBJECT(bus), NULL);
260 dev->bus = bus;
261 object_property_add_child(OBJECT(bus), "backend", OBJECT(backend), NULL);
262 bus->backend = backend;
263
264 object_property_add_link(obj, "backend", TYPE_DUMMY_BACKEND,
265 (Object **)&bus->backend, NULL, 0, NULL);
266 }
267
268 static void dummy_dev_unparent(Object *obj)
269 {
270 DummyDev *dev = DUMMY_DEV(obj);
271 object_unparent(OBJECT(dev->bus));
272 }
273
274 static void dummy_dev_class_init(ObjectClass *klass, void *opaque)
275 {
276 klass->unparent = dummy_dev_unparent;
277 }
278
279
280 static void dummy_bus_finalize(Object *obj)
281 {
282 DummyBus *bus = DUMMY_BUS(obj);
283
284 object_unref(OBJECT(bus->backend));
285 }
286
287 static void dummy_bus_init(Object *obj)
288 {
289 }
290
291 static void dummy_bus_unparent(Object *obj)
292 {
293 DummyBus *bus = DUMMY_BUS(obj);
294 object_property_del(obj->parent, "backend", NULL);
295 object_unparent(OBJECT(bus->backend));
296 }
297
298 static void dummy_bus_class_init(ObjectClass *klass, void *opaque)
299 {
300 klass->unparent = dummy_bus_unparent;
301 }
302
303 static void dummy_backend_init(Object *obj)
304 {
305 }
306
307
308 static const TypeInfo dummy_dev_info = {
309 .name = TYPE_DUMMY_DEV,
310 .parent = TYPE_OBJECT,
311 .instance_size = sizeof(DummyDev),
312 .instance_init = dummy_dev_init,
313 .instance_finalize = dummy_dev_finalize,
314 .class_size = sizeof(DummyDevClass),
315 .class_init = dummy_dev_class_init,
316 };
317
318 static const TypeInfo dummy_bus_info = {
319 .name = TYPE_DUMMY_BUS,
320 .parent = TYPE_OBJECT,
321 .instance_size = sizeof(DummyBus),
322 .instance_init = dummy_bus_init,
323 .instance_finalize = dummy_bus_finalize,
324 .class_size = sizeof(DummyBusClass),
325 .class_init = dummy_bus_class_init,
326 };
327
328 static const TypeInfo dummy_backend_info = {
329 .name = TYPE_DUMMY_BACKEND,
330 .parent = TYPE_OBJECT,
331 .instance_size = sizeof(DummyBackend),
332 .instance_init = dummy_backend_init,
333 .class_size = sizeof(DummyBackendClass),
334 };
335
336 static QemuOptsList qemu_object_opts = {
337 .name = "object",
338 .implied_opt_name = "qom-type",
339 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
340 .desc = {
341 { }
342 },
343 };
344
345
346 static void test_dummy_createv(void)
347 {
348 Error *err = NULL;
349 Object *parent = object_get_objects_root();
350 DummyObject *dobj = DUMMY_OBJECT(
351 object_new_with_props(TYPE_DUMMY,
352 parent,
353 "dummy0",
354 &err,
355 "bv", "yes",
356 "sv", "Hiss hiss hiss",
357 "av", "platypus",
358 NULL));
359
360 g_assert(err == NULL);
361 g_assert_cmpstr(dobj->sv, ==, "Hiss hiss hiss");
362 g_assert(dobj->bv == true);
363 g_assert(dobj->av == DUMMY_PLATYPUS);
364
365 g_assert(object_resolve_path_component(parent, "dummy0")
366 == OBJECT(dobj));
367
368 object_unparent(OBJECT(dobj));
369 }
370
371
372 static Object *new_helper(Error **errp,
373 Object *parent,
374 ...)
375 {
376 va_list vargs;
377 Object *obj;
378
379 va_start(vargs, parent);
380 obj = object_new_with_propv(TYPE_DUMMY,
381 parent,
382 "dummy0",
383 errp,
384 vargs);
385 va_end(vargs);
386 return obj;
387 }
388
389 static void test_dummy_createlist(void)
390 {
391 Error *err = NULL;
392 Object *parent = object_get_objects_root();
393 DummyObject *dobj = DUMMY_OBJECT(
394 new_helper(&err,
395 parent,
396 "bv", "yes",
397 "sv", "Hiss hiss hiss",
398 "av", "platypus",
399 NULL));
400
401 g_assert(err == NULL);
402 g_assert_cmpstr(dobj->sv, ==, "Hiss hiss hiss");
403 g_assert(dobj->bv == true);
404 g_assert(dobj->av == DUMMY_PLATYPUS);
405
406 g_assert(object_resolve_path_component(parent, "dummy0")
407 == OBJECT(dobj));
408
409 object_unparent(OBJECT(dobj));
410 }
411
412 static void test_dummy_createcmdl(void)
413 {
414 QemuOpts *opts;
415 DummyObject *dobj;
416 Error *err = NULL;
417 const char *params = TYPE_DUMMY \
418 ",id=dev0," \
419 "bv=yes,sv=Hiss hiss hiss,av=platypus";
420
421 qemu_add_opts(&qemu_object_opts);
422 opts = qemu_opts_parse(&qemu_object_opts, params, true, &err);
423 g_assert(err == NULL);
424 g_assert(opts);
425
426 dobj = DUMMY_OBJECT(user_creatable_add_opts(opts, &err));
427 g_assert(err == NULL);
428 g_assert(dobj);
429 g_assert_cmpstr(dobj->sv, ==, "Hiss hiss hiss");
430 g_assert(dobj->bv == true);
431 g_assert(dobj->av == DUMMY_PLATYPUS);
432
433 user_creatable_del("dev0", &err);
434 g_assert(err == NULL);
435 error_free(err);
436
437 object_unref(OBJECT(dobj));
438
439 /*
440 * cmdline-parsing via qemu_opts_parse() results in a QemuOpts entry
441 * corresponding to the Object's ID to be added to the QemuOptsList
442 * for objects. To avoid having this entry conflict with future
443 * Objects using the same ID (which can happen in cases where
444 * qemu_opts_parse() is used to parse the object params, such as
445 * with hmp_object_add() at the time of this comment), we need to
446 * check for this in user_creatable_del() and remove the QemuOpts if
447 * it is present.
448 *
449 * The below check ensures this works as expected.
450 */
451 g_assert_null(qemu_opts_find(&qemu_object_opts, "dev0"));
452 }
453
454 static void test_dummy_badenum(void)
455 {
456 Error *err = NULL;
457 Object *parent = object_get_objects_root();
458 Object *dobj =
459 object_new_with_props(TYPE_DUMMY,
460 parent,
461 "dummy0",
462 &err,
463 "bv", "yes",
464 "sv", "Hiss hiss hiss",
465 "av", "yeti",
466 NULL);
467
468 g_assert(dobj == NULL);
469 g_assert(err != NULL);
470 g_assert_cmpstr(error_get_pretty(err), ==,
471 "Invalid parameter 'yeti'");
472
473 g_assert(object_resolve_path_component(parent, "dummy0")
474 == NULL);
475
476 error_free(err);
477 }
478
479
480 static void test_dummy_getenum(void)
481 {
482 Error *err = NULL;
483 int val;
484 Object *parent = object_get_objects_root();
485 DummyObject *dobj = DUMMY_OBJECT(
486 object_new_with_props(TYPE_DUMMY,
487 parent,
488 "dummy0",
489 &err,
490 "av", "platypus",
491 NULL));
492
493 g_assert(err == NULL);
494 g_assert(dobj->av == DUMMY_PLATYPUS);
495
496 val = object_property_get_enum(OBJECT(dobj),
497 "av",
498 "DummyAnimal",
499 &err);
500 g_assert(err == NULL);
501 g_assert(val == DUMMY_PLATYPUS);
502
503 /* A bad enum type name */
504 val = object_property_get_enum(OBJECT(dobj),
505 "av",
506 "BadAnimal",
507 &err);
508 g_assert(err != NULL);
509 error_free(err);
510 err = NULL;
511
512 /* A non-enum property name */
513 val = object_property_get_enum(OBJECT(dobj),
514 "iv",
515 "DummyAnimal",
516 &err);
517 g_assert(err != NULL);
518 error_free(err);
519
520 object_unparent(OBJECT(dobj));
521 }
522
523
524 static void test_dummy_prop_iterator(ObjectPropertyIterator *iter,
525 const char *expected[], int n)
526 {
527 ObjectProperty *prop;
528 int i;
529
530 while ((prop = object_property_iter_next(iter))) {
531 for (i = 0; i < n; i++) {
532 if (!g_strcmp0(prop->name, expected[i])) {
533 break;
534 }
535 }
536 g_assert(i < n);
537 expected[i] = NULL;
538 }
539
540 for (i = 0; i < n; i++) {
541 g_assert(!expected[i]);
542 }
543 }
544
545 static void test_dummy_iterator(void)
546 {
547 const char *expected[] = {
548 "type", /* inherited from TYPE_OBJECT */
549 "sv", "av", /* class properties */
550 "bv"}; /* instance property */
551 Object *parent = object_get_objects_root();
552 DummyObject *dobj = DUMMY_OBJECT(
553 object_new_with_props(TYPE_DUMMY,
554 parent,
555 "dummy0",
556 &error_abort,
557 "bv", "yes",
558 "sv", "Hiss hiss hiss",
559 "av", "platypus",
560 NULL));
561 ObjectPropertyIterator iter;
562
563 object_property_iter_init(&iter, OBJECT(dobj));
564 test_dummy_prop_iterator(&iter, expected, ARRAY_SIZE(expected));
565 object_unparent(OBJECT(dobj));
566 }
567
568 static void test_dummy_class_iterator(void)
569 {
570 const char *expected[] = { "type", "av", "sv" };
571 ObjectPropertyIterator iter;
572 ObjectClass *klass = object_class_by_name(TYPE_DUMMY);
573
574 object_class_property_iter_init(&iter, klass);
575 test_dummy_prop_iterator(&iter, expected, ARRAY_SIZE(expected));
576 }
577
578 static void test_dummy_delchild(void)
579 {
580 Object *parent = object_get_objects_root();
581 DummyDev *dev = DUMMY_DEV(
582 object_new_with_props(TYPE_DUMMY_DEV,
583 parent,
584 "dev0",
585 &error_abort,
586 NULL));
587
588 object_unparent(OBJECT(dev));
589 }
590
591 static void test_qom_partial_path(void)
592 {
593 Object *root = object_get_objects_root();
594 Object *cont1 = container_get(root, "/cont1");
595 Object *obj1 = object_new(TYPE_DUMMY);
596 Object *obj2a = object_new(TYPE_DUMMY);
597 Object *obj2b = object_new(TYPE_DUMMY);
598 bool ambiguous;
599
600 /* Objects created:
601 * /cont1
602 * /cont1/obj1
603 * /cont1/obj2 (obj2a)
604 * /obj2 (obj2b)
605 */
606 object_property_add_child(cont1, "obj1", obj1, &error_abort);
607 object_unref(obj1);
608 object_property_add_child(cont1, "obj2", obj2a, &error_abort);
609 object_unref(obj2a);
610 object_property_add_child(root, "obj2", obj2b, &error_abort);
611 object_unref(obj2b);
612
613 ambiguous = false;
614 g_assert(!object_resolve_path_type("", TYPE_DUMMY, &ambiguous));
615 g_assert(ambiguous);
616 g_assert(!object_resolve_path_type("", TYPE_DUMMY, NULL));
617
618 ambiguous = false;
619 g_assert(!object_resolve_path("obj2", &ambiguous));
620 g_assert(ambiguous);
621 g_assert(!object_resolve_path("obj2", NULL));
622
623 ambiguous = false;
624 g_assert(object_resolve_path("obj1", &ambiguous) == obj1);
625 g_assert(!ambiguous);
626 g_assert(object_resolve_path("obj1", NULL) == obj1);
627
628 object_unparent(obj2b);
629 object_unparent(cont1);
630 }
631
632 int main(int argc, char **argv)
633 {
634 g_test_init(&argc, &argv, NULL);
635
636 module_call_init(MODULE_INIT_QOM);
637 type_register_static(&dummy_info);
638 type_register_static(&dummy_dev_info);
639 type_register_static(&dummy_bus_info);
640 type_register_static(&dummy_backend_info);
641
642 g_test_add_func("/qom/proplist/createlist", test_dummy_createlist);
643 g_test_add_func("/qom/proplist/createv", test_dummy_createv);
644 g_test_add_func("/qom/proplist/createcmdline", test_dummy_createcmdl);
645 g_test_add_func("/qom/proplist/badenum", test_dummy_badenum);
646 g_test_add_func("/qom/proplist/getenum", test_dummy_getenum);
647 g_test_add_func("/qom/proplist/iterator", test_dummy_iterator);
648 g_test_add_func("/qom/proplist/class_iterator", test_dummy_class_iterator);
649 g_test_add_func("/qom/proplist/delchild", test_dummy_delchild);
650 g_test_add_func("/qom/resolve/partial", test_qom_partial_path);
651
652 return g_test_run();
653 }