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 static const char *const dummy_animal_map
[DUMMY_LAST
+ 1] = {
50 [DUMMY_FROG
] = "frog",
51 [DUMMY_ALLIGATOR
] = "alligator",
52 [DUMMY_PLATYPUS
] = "platypus",
64 struct DummyObjectClass
{
65 ObjectClass parent_class
;
69 static void dummy_set_bv(Object
*obj
,
73 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
78 static bool dummy_get_bv(Object
*obj
,
81 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
87 static void dummy_set_av(Object
*obj
,
91 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
96 static int dummy_get_av(Object
*obj
,
99 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
105 static void dummy_set_sv(Object
*obj
,
109 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
112 dobj
->sv
= g_strdup(value
);
115 static char *dummy_get_sv(Object
*obj
,
118 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
120 return g_strdup(dobj
->sv
);
124 static void dummy_init(Object
*obj
)
126 object_property_add_bool(obj
, "bv",
133 static void dummy_class_init(ObjectClass
*cls
, void *data
)
135 object_class_property_add_bool(cls
, "bv",
139 object_class_property_add_str(cls
, "sv",
143 object_class_property_add_enum(cls
, "av",
152 static void dummy_finalize(Object
*obj
)
154 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
160 static const TypeInfo dummy_info
= {
162 .parent
= TYPE_OBJECT
,
163 .instance_size
= sizeof(DummyObject
),
164 .instance_init
= dummy_init
,
165 .instance_finalize
= dummy_finalize
,
166 .class_size
= sizeof(DummyObjectClass
),
167 .class_init
= dummy_class_init
,
168 .interfaces
= (InterfaceInfo
[]) {
169 { TYPE_USER_CREATABLE
},
176 * The following 3 object classes are used to
177 * simulate the kind of relationships seen in
178 * qdev, which result in complex object
179 * property destruction ordering.
181 * DummyDev has a 'bus' child to a DummyBus
182 * DummyBus has a 'backend' child to a DummyBackend
183 * DummyDev has a 'backend' link to DummyBackend
185 * When DummyDev is finalized, it unparents the
186 * DummyBackend, which unparents the DummyDev
187 * which deletes the 'backend' link from DummyDev
188 * to DummyBackend. This illustrates that the
189 * object_property_del_all() method needs to
190 * cope with the list of properties being changed
191 * while it iterates over them.
193 typedef struct DummyDev DummyDev
;
194 typedef struct DummyDevClass DummyDevClass
;
195 typedef struct DummyBus DummyBus
;
196 typedef struct DummyBusClass DummyBusClass
;
197 typedef struct DummyBackend DummyBackend
;
198 typedef struct DummyBackendClass DummyBackendClass
;
200 #define TYPE_DUMMY_DEV "qemu-dummy-dev"
201 #define TYPE_DUMMY_BUS "qemu-dummy-bus"
202 #define TYPE_DUMMY_BACKEND "qemu-dummy-backend"
204 #define DUMMY_DEV(obj) \
205 OBJECT_CHECK(DummyDev, (obj), TYPE_DUMMY_DEV)
206 #define DUMMY_BUS(obj) \
207 OBJECT_CHECK(DummyBus, (obj), TYPE_DUMMY_BUS)
208 #define DUMMY_BACKEND(obj) \
209 OBJECT_CHECK(DummyBackend, (obj), TYPE_DUMMY_BACKEND)
217 struct DummyDevClass
{
218 ObjectClass parent_class
;
224 DummyBackend
*backend
;
227 struct DummyBusClass
{
228 ObjectClass parent_class
;
231 struct DummyBackend
{
235 struct DummyBackendClass
{
236 ObjectClass parent_class
;
240 static void dummy_dev_finalize(Object
*obj
)
242 DummyDev
*dev
= DUMMY_DEV(obj
);
244 object_unref(OBJECT(dev
->bus
));
247 static void dummy_dev_init(Object
*obj
)
249 DummyDev
*dev
= DUMMY_DEV(obj
);
250 DummyBus
*bus
= DUMMY_BUS(object_new(TYPE_DUMMY_BUS
));
251 DummyBackend
*backend
= DUMMY_BACKEND(object_new(TYPE_DUMMY_BACKEND
));
253 object_property_add_child(obj
, "bus", OBJECT(bus
), NULL
);
255 object_property_add_child(OBJECT(bus
), "backend", OBJECT(backend
), NULL
);
256 bus
->backend
= backend
;
258 object_property_add_link(obj
, "backend", TYPE_DUMMY_BACKEND
,
259 (Object
**)&bus
->backend
, NULL
, 0, NULL
);
262 static void dummy_dev_unparent(Object
*obj
)
264 DummyDev
*dev
= DUMMY_DEV(obj
);
265 object_unparent(OBJECT(dev
->bus
));
268 static void dummy_dev_class_init(ObjectClass
*klass
, void *opaque
)
270 klass
->unparent
= dummy_dev_unparent
;
274 static void dummy_bus_finalize(Object
*obj
)
276 DummyBus
*bus
= DUMMY_BUS(obj
);
278 object_unref(OBJECT(bus
->backend
));
281 static void dummy_bus_init(Object
*obj
)
285 static void dummy_bus_unparent(Object
*obj
)
287 DummyBus
*bus
= DUMMY_BUS(obj
);
288 object_property_del(obj
->parent
, "backend", NULL
);
289 object_unparent(OBJECT(bus
->backend
));
292 static void dummy_bus_class_init(ObjectClass
*klass
, void *opaque
)
294 klass
->unparent
= dummy_bus_unparent
;
297 static void dummy_backend_init(Object
*obj
)
302 static const TypeInfo dummy_dev_info
= {
303 .name
= TYPE_DUMMY_DEV
,
304 .parent
= TYPE_OBJECT
,
305 .instance_size
= sizeof(DummyDev
),
306 .instance_init
= dummy_dev_init
,
307 .instance_finalize
= dummy_dev_finalize
,
308 .class_size
= sizeof(DummyDevClass
),
309 .class_init
= dummy_dev_class_init
,
312 static const TypeInfo dummy_bus_info
= {
313 .name
= TYPE_DUMMY_BUS
,
314 .parent
= TYPE_OBJECT
,
315 .instance_size
= sizeof(DummyBus
),
316 .instance_init
= dummy_bus_init
,
317 .instance_finalize
= dummy_bus_finalize
,
318 .class_size
= sizeof(DummyBusClass
),
319 .class_init
= dummy_bus_class_init
,
322 static const TypeInfo dummy_backend_info
= {
323 .name
= TYPE_DUMMY_BACKEND
,
324 .parent
= TYPE_OBJECT
,
325 .instance_size
= sizeof(DummyBackend
),
326 .instance_init
= dummy_backend_init
,
327 .class_size
= sizeof(DummyBackendClass
),
330 static QemuOptsList qemu_object_opts
= {
332 .implied_opt_name
= "qom-type",
333 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
340 static void test_dummy_createv(void)
343 Object
*parent
= object_get_objects_root();
344 DummyObject
*dobj
= DUMMY_OBJECT(
345 object_new_with_props(TYPE_DUMMY
,
350 "sv", "Hiss hiss hiss",
354 g_assert(err
== NULL
);
355 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
356 g_assert(dobj
->bv
== true);
357 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
359 g_assert(object_resolve_path_component(parent
, "dummy0")
362 object_unparent(OBJECT(dobj
));
366 static Object
*new_helper(Error
**errp
,
373 va_start(vargs
, parent
);
374 obj
= object_new_with_propv(TYPE_DUMMY
,
383 static void test_dummy_createlist(void)
386 Object
*parent
= object_get_objects_root();
387 DummyObject
*dobj
= DUMMY_OBJECT(
391 "sv", "Hiss hiss hiss",
395 g_assert(err
== NULL
);
396 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
397 g_assert(dobj
->bv
== true);
398 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
400 g_assert(object_resolve_path_component(parent
, "dummy0")
403 object_unparent(OBJECT(dobj
));
406 static void test_dummy_createcmdl(void)
411 const char *params
= TYPE_DUMMY \
413 "bv=yes,sv=Hiss hiss hiss,av=platypus";
415 qemu_add_opts(&qemu_object_opts
);
416 opts
= qemu_opts_parse(&qemu_object_opts
, params
, true, &err
);
417 g_assert(err
== NULL
);
420 dobj
= DUMMY_OBJECT(user_creatable_add_opts(opts
, &err
));
421 g_assert(err
== NULL
);
423 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
424 g_assert(dobj
->bv
== true);
425 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
427 user_creatable_del("dev0", &err
);
428 g_assert(err
== NULL
);
431 object_unref(OBJECT(dobj
));
434 * cmdline-parsing via qemu_opts_parse() results in a QemuOpts entry
435 * corresponding to the Object's ID to be added to the QemuOptsList
436 * for objects. To avoid having this entry conflict with future
437 * Objects using the same ID (which can happen in cases where
438 * qemu_opts_parse() is used to parse the object params, such as
439 * with hmp_object_add() at the time of this comment), we need to
440 * check for this in user_creatable_del() and remove the QemuOpts if
443 * The below check ensures this works as expected.
445 g_assert_null(qemu_opts_find(&qemu_object_opts
, "dev0"));
448 static void test_dummy_badenum(void)
451 Object
*parent
= object_get_objects_root();
453 object_new_with_props(TYPE_DUMMY
,
458 "sv", "Hiss hiss hiss",
462 g_assert(dobj
== NULL
);
463 g_assert(err
!= NULL
);
464 g_assert_cmpstr(error_get_pretty(err
), ==,
465 "Invalid parameter 'yeti'");
467 g_assert(object_resolve_path_component(parent
, "dummy0")
474 static void test_dummy_getenum(void)
478 Object
*parent
= object_get_objects_root();
479 DummyObject
*dobj
= DUMMY_OBJECT(
480 object_new_with_props(TYPE_DUMMY
,
487 g_assert(err
== NULL
);
488 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
490 val
= object_property_get_enum(OBJECT(dobj
),
494 g_assert(err
== NULL
);
495 g_assert(val
== DUMMY_PLATYPUS
);
497 /* A bad enum type name */
498 val
= object_property_get_enum(OBJECT(dobj
),
502 g_assert(err
!= NULL
);
506 /* A non-enum property name */
507 val
= object_property_get_enum(OBJECT(dobj
),
511 g_assert(err
!= NULL
);
514 object_unparent(OBJECT(dobj
));
518 static void test_dummy_iterator(void)
520 Object
*parent
= object_get_objects_root();
521 DummyObject
*dobj
= DUMMY_OBJECT(
522 object_new_with_props(TYPE_DUMMY
,
527 "sv", "Hiss hiss hiss",
531 ObjectProperty
*prop
;
532 ObjectPropertyIterator iter
;
533 bool seenbv
= false, seensv
= false, seenav
= false, seentype
;
535 object_property_iter_init(&iter
, OBJECT(dobj
));
536 while ((prop
= object_property_iter_next(&iter
))) {
537 if (g_str_equal(prop
->name
, "bv")) {
539 } else if (g_str_equal(prop
->name
, "sv")) {
541 } else if (g_str_equal(prop
->name
, "av")) {
543 } else if (g_str_equal(prop
->name
, "type")) {
544 /* This prop comes from the base Object class */
547 g_printerr("Found prop '%s'\n", prop
->name
);
548 g_assert_not_reached();
556 object_unparent(OBJECT(dobj
));
560 static void test_dummy_delchild(void)
562 Object
*parent
= object_get_objects_root();
563 DummyDev
*dev
= DUMMY_DEV(
564 object_new_with_props(TYPE_DUMMY_DEV
,
570 object_unparent(OBJECT(dev
));
573 static void test_qom_partial_path(void)
575 Object
*root
= object_get_objects_root();
576 Object
*cont1
= container_get(root
, "/cont1");
577 Object
*obj1
= object_new(TYPE_DUMMY
);
578 Object
*obj2a
= object_new(TYPE_DUMMY
);
579 Object
*obj2b
= object_new(TYPE_DUMMY
);
585 * /cont1/obj2 (obj2a)
588 object_property_add_child(cont1
, "obj1", obj1
, &error_abort
);
590 object_property_add_child(cont1
, "obj2", obj2a
, &error_abort
);
592 object_property_add_child(root
, "obj2", obj2b
, &error_abort
);
596 g_assert(!object_resolve_path_type("", TYPE_DUMMY
, &ambiguous
));
598 g_assert(!object_resolve_path_type("", TYPE_DUMMY
, NULL
));
601 g_assert(!object_resolve_path("obj2", &ambiguous
));
603 g_assert(!object_resolve_path("obj2", NULL
));
606 g_assert(object_resolve_path("obj1", &ambiguous
) == obj1
);
607 g_assert(!ambiguous
);
608 g_assert(object_resolve_path("obj1", NULL
) == obj1
);
610 object_unparent(obj2b
);
611 object_unparent(cont1
);
614 int main(int argc
, char **argv
)
616 g_test_init(&argc
, &argv
, NULL
);
618 module_call_init(MODULE_INIT_QOM
);
619 type_register_static(&dummy_info
);
620 type_register_static(&dummy_dev_info
);
621 type_register_static(&dummy_bus_info
);
622 type_register_static(&dummy_backend_info
);
624 g_test_add_func("/qom/proplist/createlist", test_dummy_createlist
);
625 g_test_add_func("/qom/proplist/createv", test_dummy_createv
);
626 g_test_add_func("/qom/proplist/createcmdline", test_dummy_createcmdl
);
627 g_test_add_func("/qom/proplist/badenum", test_dummy_badenum
);
628 g_test_add_func("/qom/proplist/getenum", test_dummy_getenum
);
629 g_test_add_func("/qom/proplist/iterator", test_dummy_iterator
);
630 g_test_add_func("/qom/proplist/delchild", test_dummy_delchild
);
631 g_test_add_func("/qom/resolve/partial", test_qom_partial_path
);