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/keyval.h"
31 #include "qemu/config-file.h"
32 #include "qom/object_interfaces.h"
35 #define TYPE_DUMMY "qemu-dummy"
37 typedef struct DummyObject DummyObject
;
38 typedef struct DummyObjectClass DummyObjectClass
;
40 DECLARE_INSTANCE_CHECKER(DummyObject
, DUMMY_OBJECT
,
43 typedef enum DummyAnimal DummyAnimal
;
53 const QEnumLookup dummy_animal_map
= {
54 .array
= (const char *const[]) {
55 [DUMMY_FROG
] = "frog",
56 [DUMMY_ALLIGATOR
] = "alligator",
57 [DUMMY_PLATYPUS
] = "platypus",
70 struct DummyObjectClass
{
71 ObjectClass parent_class
;
75 static void dummy_set_bv(Object
*obj
,
79 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
84 static bool dummy_get_bv(Object
*obj
,
87 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
93 static void dummy_set_av(Object
*obj
,
97 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
102 static int dummy_get_av(Object
*obj
,
105 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
111 static void dummy_set_sv(Object
*obj
,
115 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
118 dobj
->sv
= g_strdup(value
);
121 static char *dummy_get_sv(Object
*obj
,
124 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
126 return g_strdup(dobj
->sv
);
130 static void dummy_init(Object
*obj
)
132 object_property_add_bool(obj
, "bv",
138 static void dummy_class_init(ObjectClass
*cls
, void *data
)
140 object_class_property_add_str(cls
, "sv",
143 object_class_property_add_enum(cls
, "av",
151 static void dummy_finalize(Object
*obj
)
153 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
159 static const TypeInfo dummy_info
= {
161 .parent
= TYPE_OBJECT
,
162 .instance_size
= sizeof(DummyObject
),
163 .instance_init
= dummy_init
,
164 .instance_finalize
= dummy_finalize
,
165 .class_size
= sizeof(DummyObjectClass
),
166 .class_init
= dummy_class_init
,
167 .interfaces
= (InterfaceInfo
[]) {
168 { TYPE_USER_CREATABLE
},
175 * The following 3 object classes are used to
176 * simulate the kind of relationships seen in
177 * qdev, which result in complex object
178 * property destruction ordering.
180 * DummyDev has a 'bus' child to a DummyBus
181 * DummyBus has a 'backend' child to a DummyBackend
182 * DummyDev has a 'backend' link to DummyBackend
184 * When DummyDev is finalized, it unparents the
185 * DummyBackend, which unparents the DummyDev
186 * which deletes the 'backend' link from DummyDev
187 * to DummyBackend. This illustrates that the
188 * object_property_del_all() method needs to
189 * cope with the list of properties being changed
190 * while it iterates over them.
192 typedef struct DummyDev DummyDev
;
193 typedef struct DummyDevClass DummyDevClass
;
194 typedef struct DummyBus DummyBus
;
195 typedef struct DummyBusClass DummyBusClass
;
196 typedef struct DummyBackend DummyBackend
;
197 typedef struct DummyBackendClass DummyBackendClass
;
199 #define TYPE_DUMMY_DEV "qemu-dummy-dev"
200 #define TYPE_DUMMY_BUS "qemu-dummy-bus"
201 #define TYPE_DUMMY_BACKEND "qemu-dummy-backend"
203 DECLARE_INSTANCE_CHECKER(DummyDev
, DUMMY_DEV
,
205 DECLARE_INSTANCE_CHECKER(DummyBus
, DUMMY_BUS
,
207 DECLARE_INSTANCE_CHECKER(DummyBackend
, DUMMY_BACKEND
,
216 struct DummyDevClass
{
217 ObjectClass parent_class
;
223 DummyBackend
*backend
;
226 struct DummyBusClass
{
227 ObjectClass parent_class
;
230 struct DummyBackend
{
234 struct DummyBackendClass
{
235 ObjectClass parent_class
;
239 static void dummy_dev_finalize(Object
*obj
)
241 DummyDev
*dev
= DUMMY_DEV(obj
);
243 object_unref(OBJECT(dev
->bus
));
246 static void dummy_dev_init(Object
*obj
)
248 DummyDev
*dev
= DUMMY_DEV(obj
);
249 DummyBus
*bus
= DUMMY_BUS(object_new(TYPE_DUMMY_BUS
));
250 DummyBackend
*backend
= DUMMY_BACKEND(object_new(TYPE_DUMMY_BACKEND
));
252 object_property_add_child(obj
, "bus", OBJECT(bus
));
254 object_property_add_child(OBJECT(bus
), "backend", OBJECT(backend
));
255 bus
->backend
= backend
;
257 object_property_add_link(obj
, "backend", TYPE_DUMMY_BACKEND
,
258 (Object
**)&bus
->backend
, NULL
, 0);
261 static void dummy_dev_unparent(Object
*obj
)
263 DummyDev
*dev
= DUMMY_DEV(obj
);
264 object_unparent(OBJECT(dev
->bus
));
267 static void dummy_dev_class_init(ObjectClass
*klass
, void *opaque
)
269 klass
->unparent
= dummy_dev_unparent
;
273 static void dummy_bus_finalize(Object
*obj
)
275 DummyBus
*bus
= DUMMY_BUS(obj
);
277 object_unref(OBJECT(bus
->backend
));
280 static void dummy_bus_init(Object
*obj
)
284 static void dummy_bus_unparent(Object
*obj
)
286 DummyBus
*bus
= DUMMY_BUS(obj
);
287 object_property_del(obj
->parent
, "backend");
288 object_unparent(OBJECT(bus
->backend
));
291 static void dummy_bus_class_init(ObjectClass
*klass
, void *opaque
)
293 klass
->unparent
= dummy_bus_unparent
;
296 static void dummy_backend_init(Object
*obj
)
301 static const TypeInfo dummy_dev_info
= {
302 .name
= TYPE_DUMMY_DEV
,
303 .parent
= TYPE_OBJECT
,
304 .instance_size
= sizeof(DummyDev
),
305 .instance_init
= dummy_dev_init
,
306 .instance_finalize
= dummy_dev_finalize
,
307 .class_size
= sizeof(DummyDevClass
),
308 .class_init
= dummy_dev_class_init
,
311 static const TypeInfo dummy_bus_info
= {
312 .name
= TYPE_DUMMY_BUS
,
313 .parent
= TYPE_OBJECT
,
314 .instance_size
= sizeof(DummyBus
),
315 .instance_init
= dummy_bus_init
,
316 .instance_finalize
= dummy_bus_finalize
,
317 .class_size
= sizeof(DummyBusClass
),
318 .class_init
= dummy_bus_class_init
,
321 static const TypeInfo dummy_backend_info
= {
322 .name
= TYPE_DUMMY_BACKEND
,
323 .parent
= TYPE_OBJECT
,
324 .instance_size
= sizeof(DummyBackend
),
325 .instance_init
= dummy_backend_init
,
326 .class_size
= sizeof(DummyBackendClass
),
329 static QemuOptsList qemu_object_opts
= {
331 .implied_opt_name
= "qom-type",
332 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
339 static void test_dummy_createv(void)
342 Object
*parent
= object_get_objects_root();
343 DummyObject
*dobj
= DUMMY_OBJECT(
344 object_new_with_props(TYPE_DUMMY
,
349 "sv", "Hiss hiss hiss",
353 g_assert(err
== NULL
);
354 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
355 g_assert(dobj
->bv
== true);
356 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
358 g_assert(object_resolve_path_component(parent
, "dummy0")
361 object_unparent(OBJECT(dobj
));
365 static Object
*new_helper(Error
**errp
,
372 va_start(vargs
, parent
);
373 obj
= object_new_with_propv(TYPE_DUMMY
,
382 static void test_dummy_createlist(void)
385 Object
*parent
= object_get_objects_root();
386 DummyObject
*dobj
= DUMMY_OBJECT(
390 "sv", "Hiss hiss hiss",
394 g_assert(err
== NULL
);
395 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
396 g_assert(dobj
->bv
== true);
397 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
399 g_assert(object_resolve_path_component(parent
, "dummy0")
402 object_unparent(OBJECT(dobj
));
405 static bool test_create_obj(QDict
*qdict
, Error
**errp
)
407 Visitor
*v
= qobject_input_visitor_new_keyval(QOBJECT(qdict
));
408 Object
*obj
= user_creatable_add_type(TYPE_DUMMY
, "dev0", qdict
, v
, errp
);
415 static void test_dummy_createcmdl(void)
421 const char *params
= "bv=yes,sv=Hiss hiss hiss,av=platypus";
423 /* Needed for user_creatable_del. */
424 qemu_add_opts(&qemu_object_opts
);
426 qdict
= keyval_parse(params
, "qom-type", &help
, &err
);
427 g_assert(err
== NULL
);
431 created
= test_create_obj(qdict
, &err
);
433 g_assert(err
== NULL
);
434 qobject_unref(qdict
);
436 dobj
= DUMMY_OBJECT(object_resolve_path_component(object_get_objects_root(),
439 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
440 g_assert(dobj
->bv
== true);
441 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
443 qdict
= keyval_parse(params
, "qom-type", &help
, &err
);
444 created
= test_create_obj(qdict
, &err
);
447 g_assert(object_resolve_path_component(object_get_objects_root(), "dev0")
449 qobject_unref(qdict
);
453 qdict
= keyval_parse(params
, "qom-type", &help
, &err
);
454 user_creatable_del("dev0", &error_abort
);
455 g_assert(object_resolve_path_component(object_get_objects_root(), "dev0")
458 created
= test_create_obj(qdict
, &err
);
460 g_assert(err
== NULL
);
461 qobject_unref(qdict
);
463 dobj
= DUMMY_OBJECT(object_resolve_path_component(object_get_objects_root(),
466 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
467 g_assert(dobj
->bv
== true);
468 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
469 g_assert(object_resolve_path_component(object_get_objects_root(), "dev0")
472 object_unparent(OBJECT(dobj
));
475 static void test_dummy_badenum(void)
478 Object
*parent
= object_get_objects_root();
480 object_new_with_props(TYPE_DUMMY
,
485 "sv", "Hiss hiss hiss",
489 g_assert(dobj
== NULL
);
490 g_assert(err
!= NULL
);
491 g_assert_cmpstr(error_get_pretty(err
), ==,
492 "Parameter 'av' does not accept value 'yeti'");
494 g_assert(object_resolve_path_component(parent
, "dummy0")
501 static void test_dummy_getenum(void)
505 Object
*parent
= object_get_objects_root();
506 DummyObject
*dobj
= DUMMY_OBJECT(
507 object_new_with_props(TYPE_DUMMY
,
514 g_assert(err
== NULL
);
515 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
517 val
= object_property_get_enum(OBJECT(dobj
),
521 g_assert(val
== DUMMY_PLATYPUS
);
523 /* A bad enum type name */
524 val
= object_property_get_enum(OBJECT(dobj
),
529 error_free_or_abort(&err
);
531 /* A non-enum property name */
532 val
= object_property_get_enum(OBJECT(dobj
),
537 error_free_or_abort(&err
);
539 object_unparent(OBJECT(dobj
));
543 static void test_dummy_prop_iterator(ObjectPropertyIterator
*iter
,
544 const char *expected
[], int n
)
546 ObjectProperty
*prop
;
549 while ((prop
= object_property_iter_next(iter
))) {
550 for (i
= 0; i
< n
; i
++) {
551 if (!g_strcmp0(prop
->name
, expected
[i
])) {
559 for (i
= 0; i
< n
; i
++) {
560 g_assert(!expected
[i
]);
564 static void test_dummy_iterator(void)
566 const char *expected
[] = {
567 "type", /* inherited from TYPE_OBJECT */
568 "sv", "av", /* class properties */
569 "bv"}; /* instance property */
570 Object
*parent
= object_get_objects_root();
571 DummyObject
*dobj
= DUMMY_OBJECT(
572 object_new_with_props(TYPE_DUMMY
,
577 "sv", "Hiss hiss hiss",
580 ObjectPropertyIterator iter
;
582 object_property_iter_init(&iter
, OBJECT(dobj
));
583 test_dummy_prop_iterator(&iter
, expected
, ARRAY_SIZE(expected
));
584 object_unparent(OBJECT(dobj
));
587 static void test_dummy_class_iterator(void)
589 const char *expected
[] = { "type", "av", "sv" };
590 ObjectPropertyIterator iter
;
591 ObjectClass
*klass
= object_class_by_name(TYPE_DUMMY
);
593 object_class_property_iter_init(&iter
, klass
);
594 test_dummy_prop_iterator(&iter
, expected
, ARRAY_SIZE(expected
));
597 static void test_dummy_delchild(void)
599 Object
*parent
= object_get_objects_root();
600 DummyDev
*dev
= DUMMY_DEV(
601 object_new_with_props(TYPE_DUMMY_DEV
,
607 object_unparent(OBJECT(dev
));
610 static void test_qom_partial_path(void)
612 Object
*root
= object_get_objects_root();
613 Object
*cont1
= container_get(root
, "/cont1");
614 Object
*obj1
= object_new(TYPE_DUMMY
);
615 Object
*obj2a
= object_new(TYPE_DUMMY
);
616 Object
*obj2b
= object_new(TYPE_DUMMY
);
622 * /cont1/obj2 (obj2a)
625 object_property_add_child(cont1
, "obj1", obj1
);
627 object_property_add_child(cont1
, "obj2", obj2a
);
629 object_property_add_child(root
, "obj2", obj2b
);
633 g_assert(!object_resolve_path_type("", TYPE_DUMMY
, &ambiguous
));
635 g_assert(!object_resolve_path_type("", TYPE_DUMMY
, NULL
));
638 g_assert(!object_resolve_path("obj2", &ambiguous
));
640 g_assert(!object_resolve_path("obj2", NULL
));
643 g_assert(object_resolve_path("obj1", &ambiguous
) == obj1
);
644 g_assert(!ambiguous
);
645 g_assert(object_resolve_path("obj1", NULL
) == obj1
);
647 object_unparent(obj2b
);
648 object_unparent(cont1
);
651 int main(int argc
, char **argv
)
653 g_test_init(&argc
, &argv
, NULL
);
655 module_call_init(MODULE_INIT_QOM
);
656 type_register_static(&dummy_info
);
657 type_register_static(&dummy_dev_info
);
658 type_register_static(&dummy_bus_info
);
659 type_register_static(&dummy_backend_info
);
661 g_test_add_func("/qom/proplist/createlist", test_dummy_createlist
);
662 g_test_add_func("/qom/proplist/createv", test_dummy_createv
);
663 g_test_add_func("/qom/proplist/createcmdline", test_dummy_createcmdl
);
664 g_test_add_func("/qom/proplist/badenum", test_dummy_badenum
);
665 g_test_add_func("/qom/proplist/getenum", test_dummy_getenum
);
666 g_test_add_func("/qom/proplist/iterator", test_dummy_iterator
);
667 g_test_add_func("/qom/proplist/class_iterator", test_dummy_class_iterator
);
668 g_test_add_func("/qom/proplist/delchild", test_dummy_delchild
);
669 g_test_add_func("/qom/resolve/partial", test_qom_partial_path
);