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 "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"
31 #define TYPE_DUMMY "qemu-dummy"
33 typedef struct DummyObject DummyObject
;
34 typedef struct DummyObjectClass DummyObjectClass
;
36 #define DUMMY_OBJECT(obj) \
37 OBJECT_CHECK(DummyObject, (obj), TYPE_DUMMY)
39 typedef enum DummyAnimal DummyAnimal
;
49 const QEnumLookup dummy_animal_map
= {
50 .array
= (const char *const[]) {
51 [DUMMY_FROG
] = "frog",
52 [DUMMY_ALLIGATOR
] = "alligator",
53 [DUMMY_PLATYPUS
] = "platypus",
66 struct DummyObjectClass
{
67 ObjectClass parent_class
;
71 static void dummy_set_bv(Object
*obj
,
75 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
80 static bool dummy_get_bv(Object
*obj
,
83 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
89 static void dummy_set_av(Object
*obj
,
93 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
98 static int dummy_get_av(Object
*obj
,
101 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
107 static void dummy_set_sv(Object
*obj
,
111 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
114 dobj
->sv
= g_strdup(value
);
117 static char *dummy_get_sv(Object
*obj
,
120 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
122 return g_strdup(dobj
->sv
);
126 static void dummy_init(Object
*obj
)
130 object_property_add_bool(obj
, "bv",
134 error_free_or_abort(&err
);
138 static void dummy_class_init(ObjectClass
*cls
, void *data
)
140 object_class_property_add_bool(cls
, "bv",
144 object_class_property_add_str(cls
, "sv",
148 object_class_property_add_enum(cls
, "av",
157 static void dummy_finalize(Object
*obj
)
159 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
165 static const TypeInfo dummy_info
= {
167 .parent
= TYPE_OBJECT
,
168 .instance_size
= sizeof(DummyObject
),
169 .instance_init
= dummy_init
,
170 .instance_finalize
= dummy_finalize
,
171 .class_size
= sizeof(DummyObjectClass
),
172 .class_init
= dummy_class_init
,
173 .interfaces
= (InterfaceInfo
[]) {
174 { TYPE_USER_CREATABLE
},
181 * The following 3 object classes are used to
182 * simulate the kind of relationships seen in
183 * qdev, which result in complex object
184 * property destruction ordering.
186 * DummyDev has a 'bus' child to a DummyBus
187 * DummyBus has a 'backend' child to a DummyBackend
188 * DummyDev has a 'backend' link to DummyBackend
190 * When DummyDev is finalized, it unparents the
191 * DummyBackend, which unparents the DummyDev
192 * which deletes the 'backend' link from DummyDev
193 * to DummyBackend. This illustrates that the
194 * object_property_del_all() method needs to
195 * cope with the list of properties being changed
196 * while it iterates over them.
198 typedef struct DummyDev DummyDev
;
199 typedef struct DummyDevClass DummyDevClass
;
200 typedef struct DummyBus DummyBus
;
201 typedef struct DummyBusClass DummyBusClass
;
202 typedef struct DummyBackend DummyBackend
;
203 typedef struct DummyBackendClass DummyBackendClass
;
205 #define TYPE_DUMMY_DEV "qemu-dummy-dev"
206 #define TYPE_DUMMY_BUS "qemu-dummy-bus"
207 #define TYPE_DUMMY_BACKEND "qemu-dummy-backend"
209 #define DUMMY_DEV(obj) \
210 OBJECT_CHECK(DummyDev, (obj), TYPE_DUMMY_DEV)
211 #define DUMMY_BUS(obj) \
212 OBJECT_CHECK(DummyBus, (obj), TYPE_DUMMY_BUS)
213 #define DUMMY_BACKEND(obj) \
214 OBJECT_CHECK(DummyBackend, (obj), TYPE_DUMMY_BACKEND)
222 struct DummyDevClass
{
223 ObjectClass parent_class
;
229 DummyBackend
*backend
;
232 struct DummyBusClass
{
233 ObjectClass parent_class
;
236 struct DummyBackend
{
240 struct DummyBackendClass
{
241 ObjectClass parent_class
;
245 static void dummy_dev_finalize(Object
*obj
)
247 DummyDev
*dev
= DUMMY_DEV(obj
);
249 object_unref(OBJECT(dev
->bus
));
252 static void dummy_dev_init(Object
*obj
)
254 DummyDev
*dev
= DUMMY_DEV(obj
);
255 DummyBus
*bus
= DUMMY_BUS(object_new(TYPE_DUMMY_BUS
));
256 DummyBackend
*backend
= DUMMY_BACKEND(object_new(TYPE_DUMMY_BACKEND
));
258 object_property_add_child(obj
, "bus", OBJECT(bus
), NULL
);
260 object_property_add_child(OBJECT(bus
), "backend", OBJECT(backend
), NULL
);
261 bus
->backend
= backend
;
263 object_property_add_link(obj
, "backend", TYPE_DUMMY_BACKEND
,
264 (Object
**)&bus
->backend
, NULL
, 0, NULL
);
267 static void dummy_dev_unparent(Object
*obj
)
269 DummyDev
*dev
= DUMMY_DEV(obj
);
270 object_unparent(OBJECT(dev
->bus
));
273 static void dummy_dev_class_init(ObjectClass
*klass
, void *opaque
)
275 klass
->unparent
= dummy_dev_unparent
;
279 static void dummy_bus_finalize(Object
*obj
)
281 DummyBus
*bus
= DUMMY_BUS(obj
);
283 object_unref(OBJECT(bus
->backend
));
286 static void dummy_bus_init(Object
*obj
)
290 static void dummy_bus_unparent(Object
*obj
)
292 DummyBus
*bus
= DUMMY_BUS(obj
);
293 object_property_del(obj
->parent
, "backend", NULL
);
294 object_unparent(OBJECT(bus
->backend
));
297 static void dummy_bus_class_init(ObjectClass
*klass
, void *opaque
)
299 klass
->unparent
= dummy_bus_unparent
;
302 static void dummy_backend_init(Object
*obj
)
307 static const TypeInfo dummy_dev_info
= {
308 .name
= TYPE_DUMMY_DEV
,
309 .parent
= TYPE_OBJECT
,
310 .instance_size
= sizeof(DummyDev
),
311 .instance_init
= dummy_dev_init
,
312 .instance_finalize
= dummy_dev_finalize
,
313 .class_size
= sizeof(DummyDevClass
),
314 .class_init
= dummy_dev_class_init
,
317 static const TypeInfo dummy_bus_info
= {
318 .name
= TYPE_DUMMY_BUS
,
319 .parent
= TYPE_OBJECT
,
320 .instance_size
= sizeof(DummyBus
),
321 .instance_init
= dummy_bus_init
,
322 .instance_finalize
= dummy_bus_finalize
,
323 .class_size
= sizeof(DummyBusClass
),
324 .class_init
= dummy_bus_class_init
,
327 static const TypeInfo dummy_backend_info
= {
328 .name
= TYPE_DUMMY_BACKEND
,
329 .parent
= TYPE_OBJECT
,
330 .instance_size
= sizeof(DummyBackend
),
331 .instance_init
= dummy_backend_init
,
332 .class_size
= sizeof(DummyBackendClass
),
335 static QemuOptsList qemu_object_opts
= {
337 .implied_opt_name
= "qom-type",
338 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
345 static void test_dummy_createv(void)
348 Object
*parent
= object_get_objects_root();
349 DummyObject
*dobj
= DUMMY_OBJECT(
350 object_new_with_props(TYPE_DUMMY
,
355 "sv", "Hiss hiss hiss",
359 g_assert(err
== NULL
);
360 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
361 g_assert(dobj
->bv
== true);
362 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
364 g_assert(object_resolve_path_component(parent
, "dummy0")
367 object_unparent(OBJECT(dobj
));
371 static Object
*new_helper(Error
**errp
,
378 va_start(vargs
, parent
);
379 obj
= object_new_with_propv(TYPE_DUMMY
,
388 static void test_dummy_createlist(void)
391 Object
*parent
= object_get_objects_root();
392 DummyObject
*dobj
= DUMMY_OBJECT(
396 "sv", "Hiss hiss hiss",
400 g_assert(err
== NULL
);
401 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
402 g_assert(dobj
->bv
== true);
403 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
405 g_assert(object_resolve_path_component(parent
, "dummy0")
408 object_unparent(OBJECT(dobj
));
411 static void test_dummy_createcmdl(void)
416 const char *params
= TYPE_DUMMY \
418 "bv=yes,sv=Hiss hiss hiss,av=platypus";
420 qemu_add_opts(&qemu_object_opts
);
421 opts
= qemu_opts_parse(&qemu_object_opts
, params
, true, &err
);
422 g_assert(err
== NULL
);
425 dobj
= DUMMY_OBJECT(user_creatable_add_opts(opts
, &err
));
426 g_assert(err
== NULL
);
428 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
429 g_assert(dobj
->bv
== true);
430 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
432 user_creatable_del("dev0", &err
);
433 g_assert(err
== NULL
);
436 object_unref(OBJECT(dobj
));
439 * cmdline-parsing via qemu_opts_parse() results in a QemuOpts entry
440 * corresponding to the Object's ID to be added to the QemuOptsList
441 * for objects. To avoid having this entry conflict with future
442 * Objects using the same ID (which can happen in cases where
443 * qemu_opts_parse() is used to parse the object params, such as
444 * with hmp_object_add() at the time of this comment), we need to
445 * check for this in user_creatable_del() and remove the QemuOpts if
448 * The below check ensures this works as expected.
450 g_assert_null(qemu_opts_find(&qemu_object_opts
, "dev0"));
453 static void test_dummy_badenum(void)
456 Object
*parent
= object_get_objects_root();
458 object_new_with_props(TYPE_DUMMY
,
463 "sv", "Hiss hiss hiss",
467 g_assert(dobj
== NULL
);
468 g_assert(err
!= NULL
);
469 g_assert_cmpstr(error_get_pretty(err
), ==,
470 "Invalid parameter 'yeti'");
472 g_assert(object_resolve_path_component(parent
, "dummy0")
479 static void test_dummy_getenum(void)
483 Object
*parent
= object_get_objects_root();
484 DummyObject
*dobj
= DUMMY_OBJECT(
485 object_new_with_props(TYPE_DUMMY
,
492 g_assert(err
== NULL
);
493 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
495 val
= object_property_get_enum(OBJECT(dobj
),
499 g_assert(err
== NULL
);
500 g_assert(val
== DUMMY_PLATYPUS
);
502 /* A bad enum type name */
503 val
= object_property_get_enum(OBJECT(dobj
),
507 g_assert(err
!= NULL
);
511 /* A non-enum property name */
512 val
= object_property_get_enum(OBJECT(dobj
),
516 g_assert(err
!= NULL
);
519 object_unparent(OBJECT(dobj
));
523 static void test_dummy_prop_iterator(ObjectPropertyIterator
*iter
)
525 bool seenbv
= false, seensv
= false, seenav
= false, seentype
= false;
526 ObjectProperty
*prop
;
528 while ((prop
= object_property_iter_next(iter
))) {
529 if (!seenbv
&& g_str_equal(prop
->name
, "bv")) {
531 } else if (!seensv
&& g_str_equal(prop
->name
, "sv")) {
533 } else if (!seenav
&& g_str_equal(prop
->name
, "av")) {
535 } else if (!seentype
&& g_str_equal(prop
->name
, "type")) {
536 /* This prop comes from the base Object class */
539 g_printerr("Found prop '%s'\n", prop
->name
);
540 g_assert_not_reached();
549 static void test_dummy_iterator(void)
551 Object
*parent
= object_get_objects_root();
552 DummyObject
*dobj
= DUMMY_OBJECT(
553 object_new_with_props(TYPE_DUMMY
,
558 "sv", "Hiss hiss hiss",
561 ObjectPropertyIterator iter
;
563 object_property_iter_init(&iter
, OBJECT(dobj
));
564 test_dummy_prop_iterator(&iter
);
565 object_unparent(OBJECT(dobj
));
568 static void test_dummy_class_iterator(void)
570 ObjectPropertyIterator iter
;
571 ObjectClass
*klass
= object_class_by_name(TYPE_DUMMY
);
573 object_class_property_iter_init(&iter
, klass
);
574 test_dummy_prop_iterator(&iter
);
577 static void test_dummy_delchild(void)
579 Object
*parent
= object_get_objects_root();
580 DummyDev
*dev
= DUMMY_DEV(
581 object_new_with_props(TYPE_DUMMY_DEV
,
587 object_unparent(OBJECT(dev
));
590 static void test_qom_partial_path(void)
592 Object
*root
= object_get_objects_root();
593 Object
*cont1
= container_get(root
, "/cont1");
594 Object
*obj1
= object_new(TYPE_DUMMY
);
595 Object
*obj2a
= object_new(TYPE_DUMMY
);
596 Object
*obj2b
= object_new(TYPE_DUMMY
);
602 * /cont1/obj2 (obj2a)
605 object_property_add_child(cont1
, "obj1", obj1
, &error_abort
);
607 object_property_add_child(cont1
, "obj2", obj2a
, &error_abort
);
609 object_property_add_child(root
, "obj2", obj2b
, &error_abort
);
613 g_assert(!object_resolve_path_type("", TYPE_DUMMY
, &ambiguous
));
615 g_assert(!object_resolve_path_type("", TYPE_DUMMY
, NULL
));
618 g_assert(!object_resolve_path("obj2", &ambiguous
));
620 g_assert(!object_resolve_path("obj2", NULL
));
623 g_assert(object_resolve_path("obj1", &ambiguous
) == obj1
);
624 g_assert(!ambiguous
);
625 g_assert(object_resolve_path("obj1", NULL
) == obj1
);
627 object_unparent(obj2b
);
628 object_unparent(cont1
);
631 int main(int argc
, char **argv
)
633 g_test_init(&argc
, &argv
, NULL
);
635 module_call_init(MODULE_INIT_QOM
);
636 type_register_static(&dummy_info
);
637 type_register_static(&dummy_dev_info
);
638 type_register_static(&dummy_bus_info
);
639 type_register_static(&dummy_backend_info
);
641 g_test_add_func("/qom/proplist/createlist", test_dummy_createlist
);
642 g_test_add_func("/qom/proplist/createv", test_dummy_createv
);
643 g_test_add_func("/qom/proplist/createcmdline", test_dummy_createcmdl
);
644 g_test_add_func("/qom/proplist/badenum", test_dummy_badenum
);
645 g_test_add_func("/qom/proplist/getenum", test_dummy_getenum
);
646 g_test_add_func("/qom/proplist/iterator", test_dummy_iterator
);
647 g_test_add_func("/qom/proplist/class_iterator", test_dummy_class_iterator
);
648 g_test_add_func("/qom/proplist/delchild", test_dummy_delchild
);
649 g_test_add_func("/qom/resolve/partial", test_qom_partial_path
);