2 * Copyright (C) 2015 Red Hat, Inc.
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.
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.
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/>.
18 * Author: Daniel P. Berrange <berrange@redhat.com>
21 #include "qemu/osdep.h"
23 #include "qapi/error.h"
24 #include "qapi/qobject-input-visitor.h"
25 #include "qapi/qmp/qdict.h"
26 #include "qapi/qmp/qobject.h"
27 #include "qom/object.h"
28 #include "qemu/module.h"
29 #include "qemu/option.h"
30 #include "qemu/config-file.h"
31 #include "qom/object_interfaces.h"
34 #define TYPE_DUMMY "qemu-dummy"
36 typedef struct DummyObject DummyObject
;
37 typedef struct DummyObjectClass DummyObjectClass
;
39 DECLARE_INSTANCE_CHECKER(DummyObject
, DUMMY_OBJECT
,
42 typedef enum DummyAnimal DummyAnimal
;
52 const QEnumLookup dummy_animal_map
= {
53 .array
= (const char *const[]) {
54 [DUMMY_FROG
] = "frog",
55 [DUMMY_ALLIGATOR
] = "alligator",
56 [DUMMY_PLATYPUS
] = "platypus",
69 struct DummyObjectClass
{
70 ObjectClass parent_class
;
74 static void dummy_set_bv(Object
*obj
,
78 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
83 static bool dummy_get_bv(Object
*obj
,
86 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
92 static void dummy_set_av(Object
*obj
,
96 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
101 static int dummy_get_av(Object
*obj
,
104 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
110 static void dummy_set_sv(Object
*obj
,
114 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
117 dobj
->sv
= g_strdup(value
);
120 static char *dummy_get_sv(Object
*obj
,
123 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
125 return g_strdup(dobj
->sv
);
129 static void dummy_init(Object
*obj
)
131 object_property_add_bool(obj
, "bv",
137 static void dummy_class_init(ObjectClass
*cls
, void *data
)
139 object_class_property_add_str(cls
, "sv",
142 object_class_property_add_enum(cls
, "av",
150 static void dummy_finalize(Object
*obj
)
152 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
158 static const TypeInfo dummy_info
= {
160 .parent
= TYPE_OBJECT
,
161 .instance_size
= sizeof(DummyObject
),
162 .instance_init
= dummy_init
,
163 .instance_finalize
= dummy_finalize
,
164 .class_size
= sizeof(DummyObjectClass
),
165 .class_init
= dummy_class_init
,
166 .interfaces
= (InterfaceInfo
[]) {
167 { TYPE_USER_CREATABLE
},
174 * The following 3 object classes are used to
175 * simulate the kind of relationships seen in
176 * qdev, which result in complex object
177 * property destruction ordering.
179 * DummyDev has a 'bus' child to a DummyBus
180 * DummyBus has a 'backend' child to a DummyBackend
181 * DummyDev has a 'backend' link to DummyBackend
183 * When DummyDev is finalized, it unparents the
184 * DummyBackend, which unparents the DummyDev
185 * which deletes the 'backend' link from DummyDev
186 * to DummyBackend. This illustrates that the
187 * object_property_del_all() method needs to
188 * cope with the list of properties being changed
189 * while it iterates over them.
191 typedef struct DummyDev DummyDev
;
192 typedef struct DummyDevClass DummyDevClass
;
193 typedef struct DummyBus DummyBus
;
194 typedef struct DummyBusClass DummyBusClass
;
195 typedef struct DummyBackend DummyBackend
;
196 typedef struct DummyBackendClass DummyBackendClass
;
198 #define TYPE_DUMMY_DEV "qemu-dummy-dev"
199 #define TYPE_DUMMY_BUS "qemu-dummy-bus"
200 #define TYPE_DUMMY_BACKEND "qemu-dummy-backend"
202 DECLARE_INSTANCE_CHECKER(DummyDev
, DUMMY_DEV
,
204 DECLARE_INSTANCE_CHECKER(DummyBus
, DUMMY_BUS
,
206 DECLARE_INSTANCE_CHECKER(DummyBackend
, DUMMY_BACKEND
,
215 struct DummyDevClass
{
216 ObjectClass parent_class
;
222 DummyBackend
*backend
;
225 struct DummyBusClass
{
226 ObjectClass parent_class
;
229 struct DummyBackend
{
233 struct DummyBackendClass
{
234 ObjectClass parent_class
;
238 static void dummy_dev_finalize(Object
*obj
)
240 DummyDev
*dev
= DUMMY_DEV(obj
);
242 object_unref(OBJECT(dev
->bus
));
245 static void dummy_dev_init(Object
*obj
)
247 DummyDev
*dev
= DUMMY_DEV(obj
);
248 DummyBus
*bus
= DUMMY_BUS(object_new(TYPE_DUMMY_BUS
));
249 DummyBackend
*backend
= DUMMY_BACKEND(object_new(TYPE_DUMMY_BACKEND
));
251 object_property_add_child(obj
, "bus", OBJECT(bus
));
253 object_property_add_child(OBJECT(bus
), "backend", OBJECT(backend
));
254 bus
->backend
= backend
;
256 object_property_add_link(obj
, "backend", TYPE_DUMMY_BACKEND
,
257 (Object
**)&bus
->backend
, NULL
, 0);
260 static void dummy_dev_unparent(Object
*obj
)
262 DummyDev
*dev
= DUMMY_DEV(obj
);
263 object_unparent(OBJECT(dev
->bus
));
266 static void dummy_dev_class_init(ObjectClass
*klass
, void *opaque
)
268 klass
->unparent
= dummy_dev_unparent
;
272 static void dummy_bus_finalize(Object
*obj
)
274 DummyBus
*bus
= DUMMY_BUS(obj
);
276 object_unref(OBJECT(bus
->backend
));
279 static void dummy_bus_init(Object
*obj
)
283 static void dummy_bus_unparent(Object
*obj
)
285 DummyBus
*bus
= DUMMY_BUS(obj
);
286 object_property_del(obj
->parent
, "backend");
287 object_unparent(OBJECT(bus
->backend
));
290 static void dummy_bus_class_init(ObjectClass
*klass
, void *opaque
)
292 klass
->unparent
= dummy_bus_unparent
;
295 static void dummy_backend_init(Object
*obj
)
300 static const TypeInfo dummy_dev_info
= {
301 .name
= TYPE_DUMMY_DEV
,
302 .parent
= TYPE_OBJECT
,
303 .instance_size
= sizeof(DummyDev
),
304 .instance_init
= dummy_dev_init
,
305 .instance_finalize
= dummy_dev_finalize
,
306 .class_size
= sizeof(DummyDevClass
),
307 .class_init
= dummy_dev_class_init
,
310 static const TypeInfo dummy_bus_info
= {
311 .name
= TYPE_DUMMY_BUS
,
312 .parent
= TYPE_OBJECT
,
313 .instance_size
= sizeof(DummyBus
),
314 .instance_init
= dummy_bus_init
,
315 .instance_finalize
= dummy_bus_finalize
,
316 .class_size
= sizeof(DummyBusClass
),
317 .class_init
= dummy_bus_class_init
,
320 static const TypeInfo dummy_backend_info
= {
321 .name
= TYPE_DUMMY_BACKEND
,
322 .parent
= TYPE_OBJECT
,
323 .instance_size
= sizeof(DummyBackend
),
324 .instance_init
= dummy_backend_init
,
325 .class_size
= sizeof(DummyBackendClass
),
328 static QemuOptsList qemu_object_opts
= {
330 .implied_opt_name
= "qom-type",
331 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
338 static void test_dummy_createv(void)
341 Object
*parent
= object_get_objects_root();
342 DummyObject
*dobj
= DUMMY_OBJECT(
343 object_new_with_props(TYPE_DUMMY
,
348 "sv", "Hiss hiss hiss",
352 g_assert(err
== NULL
);
353 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
354 g_assert(dobj
->bv
== true);
355 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
357 g_assert(object_resolve_path_component(parent
, "dummy0")
360 object_unparent(OBJECT(dobj
));
364 static Object
*new_helper(Error
**errp
,
371 va_start(vargs
, parent
);
372 obj
= object_new_with_propv(TYPE_DUMMY
,
381 static void test_dummy_createlist(void)
384 Object
*parent
= object_get_objects_root();
385 DummyObject
*dobj
= DUMMY_OBJECT(
389 "sv", "Hiss hiss hiss",
393 g_assert(err
== NULL
);
394 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
395 g_assert(dobj
->bv
== true);
396 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
398 g_assert(object_resolve_path_component(parent
, "dummy0")
401 object_unparent(OBJECT(dobj
));
404 static bool test_create_obj(QDict
*qdict
, Error
**errp
)
406 Visitor
*v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
407 Object
*obj
= user_creatable_add_type(TYPE_DUMMY
, "dev0", qdict
, v
, errp
);
414 static void test_dummy_createcmdl(void)
420 const char *params
= "bv=yes,sv=Hiss hiss hiss,av=platypus";
422 /* Needed for user_creatable_del. */
423 qemu_add_opts(&qemu_object_opts
);
425 qdict
= keyval_parse(params
, "qom-type", &help
, &err
);
426 g_assert(err
== NULL
);
430 created
= test_create_obj(qdict
, &err
);
432 g_assert(err
== NULL
);
433 qobject_unref(qdict
);
435 dobj
= DUMMY_OBJECT(object_resolve_path_component(object_get_objects_root(),
438 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
439 g_assert(dobj
->bv
== true);
440 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
442 qdict
= keyval_parse(params
, "qom-type", &help
, &err
);
443 created
= test_create_obj(qdict
, &err
);
446 g_assert(object_resolve_path_component(object_get_objects_root(), "dev0")
448 qobject_unref(qdict
);
452 qdict
= keyval_parse(params
, "qom-type", &help
, &err
);
453 user_creatable_del("dev0", &error_abort
);
454 g_assert(object_resolve_path_component(object_get_objects_root(), "dev0")
457 created
= test_create_obj(qdict
, &err
);
459 g_assert(err
== NULL
);
460 qobject_unref(qdict
);
462 dobj
= DUMMY_OBJECT(object_resolve_path_component(object_get_objects_root(),
465 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
466 g_assert(dobj
->bv
== true);
467 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
468 g_assert(object_resolve_path_component(object_get_objects_root(), "dev0")
471 object_unparent(OBJECT(dobj
));
474 static void test_dummy_badenum(void)
477 Object
*parent
= object_get_objects_root();
479 object_new_with_props(TYPE_DUMMY
,
484 "sv", "Hiss hiss hiss",
488 g_assert(dobj
== NULL
);
489 g_assert(err
!= NULL
);
490 g_assert_cmpstr(error_get_pretty(err
), ==,
491 "Parameter 'av' does not accept value 'yeti'");
493 g_assert(object_resolve_path_component(parent
, "dummy0")
500 static void test_dummy_getenum(void)
504 Object
*parent
= object_get_objects_root();
505 DummyObject
*dobj
= DUMMY_OBJECT(
506 object_new_with_props(TYPE_DUMMY
,
513 g_assert(err
== NULL
);
514 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
516 val
= object_property_get_enum(OBJECT(dobj
),
520 g_assert(val
== DUMMY_PLATYPUS
);
522 /* A bad enum type name */
523 val
= object_property_get_enum(OBJECT(dobj
),
528 error_free_or_abort(&err
);
530 /* A non-enum property name */
531 val
= object_property_get_enum(OBJECT(dobj
),
536 error_free_or_abort(&err
);
538 object_unparent(OBJECT(dobj
));
542 static void test_dummy_prop_iterator(ObjectPropertyIterator
*iter
,
543 const char *expected
[], int n
)
545 ObjectProperty
*prop
;
548 while ((prop
= object_property_iter_next(iter
))) {
549 for (i
= 0; i
< n
; i
++) {
550 if (!g_strcmp0(prop
->name
, expected
[i
])) {
558 for (i
= 0; i
< n
; i
++) {
559 g_assert(!expected
[i
]);
563 static void test_dummy_iterator(void)
565 const char *expected
[] = {
566 "type", /* inherited from TYPE_OBJECT */
567 "sv", "av", /* class properties */
568 "bv"}; /* instance property */
569 Object
*parent
= object_get_objects_root();
570 DummyObject
*dobj
= DUMMY_OBJECT(
571 object_new_with_props(TYPE_DUMMY
,
576 "sv", "Hiss hiss hiss",
579 ObjectPropertyIterator iter
;
581 object_property_iter_init(&iter
, OBJECT(dobj
));
582 test_dummy_prop_iterator(&iter
, expected
, ARRAY_SIZE(expected
));
583 object_unparent(OBJECT(dobj
));
586 static void test_dummy_class_iterator(void)
588 const char *expected
[] = { "type", "av", "sv" };
589 ObjectPropertyIterator iter
;
590 ObjectClass
*klass
= object_class_by_name(TYPE_DUMMY
);
592 object_class_property_iter_init(&iter
, klass
);
593 test_dummy_prop_iterator(&iter
, expected
, ARRAY_SIZE(expected
));
596 static void test_dummy_delchild(void)
598 Object
*parent
= object_get_objects_root();
599 DummyDev
*dev
= DUMMY_DEV(
600 object_new_with_props(TYPE_DUMMY_DEV
,
606 object_unparent(OBJECT(dev
));
609 static void test_qom_partial_path(void)
611 Object
*root
= object_get_objects_root();
612 Object
*cont1
= container_get(root
, "/cont1");
613 Object
*obj1
= object_new(TYPE_DUMMY
);
614 Object
*obj2a
= object_new(TYPE_DUMMY
);
615 Object
*obj2b
= object_new(TYPE_DUMMY
);
621 * /cont1/obj2 (obj2a)
624 object_property_add_child(cont1
, "obj1", obj1
);
626 object_property_add_child(cont1
, "obj2", obj2a
);
628 object_property_add_child(root
, "obj2", obj2b
);
632 g_assert(!object_resolve_path_type("", TYPE_DUMMY
, &ambiguous
));
634 g_assert(!object_resolve_path_type("", TYPE_DUMMY
, NULL
));
637 g_assert(!object_resolve_path("obj2", &ambiguous
));
639 g_assert(!object_resolve_path("obj2", NULL
));
642 g_assert(object_resolve_path("obj1", &ambiguous
) == obj1
);
643 g_assert(!ambiguous
);
644 g_assert(object_resolve_path("obj1", NULL
) == obj1
);
646 object_unparent(obj2b
);
647 object_unparent(cont1
);
650 int main(int argc
, char **argv
)
652 g_test_init(&argc
, &argv
, NULL
);
654 module_call_init(MODULE_INIT_QOM
);
655 type_register_static(&dummy_info
);
656 type_register_static(&dummy_dev_info
);
657 type_register_static(&dummy_bus_info
);
658 type_register_static(&dummy_backend_info
);
660 g_test_add_func("/qom/proplist/createlist", test_dummy_createlist
);
661 g_test_add_func("/qom/proplist/createv", test_dummy_createv
);
662 g_test_add_func("/qom/proplist/createcmdline", test_dummy_createcmdl
);
663 g_test_add_func("/qom/proplist/badenum", test_dummy_badenum
);
664 g_test_add_func("/qom/proplist/getenum", test_dummy_getenum
);
665 g_test_add_func("/qom/proplist/iterator", test_dummy_iterator
);
666 g_test_add_func("/qom/proplist/class_iterator", test_dummy_class_iterator
);
667 g_test_add_func("/qom/proplist/delchild", test_dummy_delchild
);
668 g_test_add_func("/qom/resolve/partial", test_qom_partial_path
);