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
);
432 * cmdline-parsing via qemu_opts_parse() results in a QemuOpts entry
433 * corresponding to the Object's ID to be added to the QemuOptsList
434 * for objects. To avoid having this entry conflict with future
435 * Objects using the same ID (which can happen in cases where
436 * qemu_opts_parse() is used to parse the object params, such as
437 * with hmp_object_add() at the time of this comment), we need to
438 * check for this in user_creatable_del() and remove the QemuOpts if
441 * The below check ensures this works as expected.
443 g_assert_null(qemu_opts_find(&qemu_object_opts
, "dev0"));
446 static void test_dummy_badenum(void)
449 Object
*parent
= object_get_objects_root();
451 object_new_with_props(TYPE_DUMMY
,
456 "sv", "Hiss hiss hiss",
460 g_assert(dobj
== NULL
);
461 g_assert(err
!= NULL
);
462 g_assert_cmpstr(error_get_pretty(err
), ==,
463 "Invalid parameter 'yeti'");
465 g_assert(object_resolve_path_component(parent
, "dummy0")
472 static void test_dummy_getenum(void)
476 Object
*parent
= object_get_objects_root();
477 DummyObject
*dobj
= DUMMY_OBJECT(
478 object_new_with_props(TYPE_DUMMY
,
485 g_assert(err
== NULL
);
486 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
488 val
= object_property_get_enum(OBJECT(dobj
),
492 g_assert(err
== NULL
);
493 g_assert(val
== DUMMY_PLATYPUS
);
495 /* A bad enum type name */
496 val
= object_property_get_enum(OBJECT(dobj
),
500 g_assert(err
!= NULL
);
504 /* A non-enum property name */
505 val
= object_property_get_enum(OBJECT(dobj
),
509 g_assert(err
!= NULL
);
512 object_unparent(OBJECT(dobj
));
516 static void test_dummy_iterator(void)
518 Object
*parent
= object_get_objects_root();
519 DummyObject
*dobj
= DUMMY_OBJECT(
520 object_new_with_props(TYPE_DUMMY
,
525 "sv", "Hiss hiss hiss",
529 ObjectProperty
*prop
;
530 ObjectPropertyIterator iter
;
531 bool seenbv
= false, seensv
= false, seenav
= false, seentype
;
533 object_property_iter_init(&iter
, OBJECT(dobj
));
534 while ((prop
= object_property_iter_next(&iter
))) {
535 if (g_str_equal(prop
->name
, "bv")) {
537 } else if (g_str_equal(prop
->name
, "sv")) {
539 } else if (g_str_equal(prop
->name
, "av")) {
541 } else if (g_str_equal(prop
->name
, "type")) {
542 /* This prop comes from the base Object class */
545 g_printerr("Found prop '%s'\n", prop
->name
);
546 g_assert_not_reached();
554 object_unparent(OBJECT(dobj
));
558 static void test_dummy_delchild(void)
560 Object
*parent
= object_get_objects_root();
561 DummyDev
*dev
= DUMMY_DEV(
562 object_new_with_props(TYPE_DUMMY_DEV
,
568 object_unparent(OBJECT(dev
));
571 int main(int argc
, char **argv
)
573 g_test_init(&argc
, &argv
, NULL
);
575 module_call_init(MODULE_INIT_QOM
);
576 type_register_static(&dummy_info
);
577 type_register_static(&dummy_dev_info
);
578 type_register_static(&dummy_bus_info
);
579 type_register_static(&dummy_backend_info
);
581 g_test_add_func("/qom/proplist/createlist", test_dummy_createlist
);
582 g_test_add_func("/qom/proplist/createv", test_dummy_createv
);
583 g_test_add_func("/qom/proplist/createcmdline", test_dummy_createcmdl
);
584 g_test_add_func("/qom/proplist/badenum", test_dummy_badenum
);
585 g_test_add_func("/qom/proplist/getenum", test_dummy_getenum
);
586 g_test_add_func("/qom/proplist/iterator", test_dummy_iterator
);
587 g_test_add_func("/qom/proplist/delchild", test_dummy_delchild
);