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>
23 #include "qom/object.h"
24 #include "qemu/module.h"
27 #define TYPE_DUMMY "qemu-dummy"
29 typedef struct DummyObject DummyObject
;
30 typedef struct DummyObjectClass DummyObjectClass
;
32 #define DUMMY_OBJECT(obj) \
33 OBJECT_CHECK(DummyObject, (obj), TYPE_DUMMY)
35 typedef enum DummyAnimal DummyAnimal
;
45 static const char *const dummy_animal_map
[DUMMY_LAST
+ 1] = {
46 [DUMMY_FROG
] = "frog",
47 [DUMMY_ALLIGATOR
] = "alligator",
48 [DUMMY_PLATYPUS
] = "platypus",
60 struct DummyObjectClass
{
61 ObjectClass parent_class
;
65 static void dummy_set_bv(Object
*obj
,
69 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
74 static bool dummy_get_bv(Object
*obj
,
77 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
83 static void dummy_set_av(Object
*obj
,
87 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
92 static int dummy_get_av(Object
*obj
,
95 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
101 static void dummy_set_sv(Object
*obj
,
105 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
108 dobj
->sv
= g_strdup(value
);
111 static char *dummy_get_sv(Object
*obj
,
114 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
116 return g_strdup(dobj
->sv
);
120 static void dummy_init(Object
*obj
)
122 object_property_add_bool(obj
, "bv",
126 object_property_add_str(obj
, "sv",
130 object_property_add_enum(obj
, "av",
138 static void dummy_finalize(Object
*obj
)
140 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
146 static const TypeInfo dummy_info
= {
148 .parent
= TYPE_OBJECT
,
149 .instance_size
= sizeof(DummyObject
),
150 .instance_init
= dummy_init
,
151 .instance_finalize
= dummy_finalize
,
152 .class_size
= sizeof(DummyObjectClass
),
157 * The following 3 object classes are used to
158 * simulate the kind of relationships seen in
159 * qdev, which result in complex object
160 * property destruction ordering.
162 * DummyDev has a 'bus' child to a DummyBus
163 * DummyBus has a 'backend' child to a DummyBackend
164 * DummyDev has a 'backend' link to DummyBackend
166 * When DummyDev is finalized, it unparents the
167 * DummyBackend, which unparents the DummyDev
168 * which deletes the 'backend' link from DummyDev
169 * to DummyBackend. This illustrates that the
170 * object_property_del_all() method needs to
171 * cope with the list of properties being changed
172 * while it iterates over them.
174 typedef struct DummyDev DummyDev
;
175 typedef struct DummyDevClass DummyDevClass
;
176 typedef struct DummyBus DummyBus
;
177 typedef struct DummyBusClass DummyBusClass
;
178 typedef struct DummyBackend DummyBackend
;
179 typedef struct DummyBackendClass DummyBackendClass
;
181 #define TYPE_DUMMY_DEV "qemu-dummy-dev"
182 #define TYPE_DUMMY_BUS "qemu-dummy-bus"
183 #define TYPE_DUMMY_BACKEND "qemu-dummy-backend"
185 #define DUMMY_DEV(obj) \
186 OBJECT_CHECK(DummyDev, (obj), TYPE_DUMMY_DEV)
187 #define DUMMY_BUS(obj) \
188 OBJECT_CHECK(DummyBus, (obj), TYPE_DUMMY_BUS)
189 #define DUMMY_BACKEND(obj) \
190 OBJECT_CHECK(DummyBackend, (obj), TYPE_DUMMY_BACKEND)
198 struct DummyDevClass
{
199 ObjectClass parent_class
;
205 DummyBackend
*backend
;
208 struct DummyBusClass
{
209 ObjectClass parent_class
;
212 struct DummyBackend
{
216 struct DummyBackendClass
{
217 ObjectClass parent_class
;
221 static void dummy_dev_init(Object
*obj
)
223 DummyDev
*dev
= DUMMY_DEV(obj
);
224 DummyBus
*bus
= DUMMY_BUS(object_new(TYPE_DUMMY_BUS
));
225 DummyBackend
*backend
= DUMMY_BACKEND(object_new(TYPE_DUMMY_BACKEND
));
227 object_property_add_child(obj
, "bus", OBJECT(bus
), NULL
);
229 object_property_add_child(OBJECT(bus
), "backend", OBJECT(backend
), NULL
);
230 bus
->backend
= backend
;
232 object_property_add_link(obj
, "backend", TYPE_DUMMY_BACKEND
,
233 (Object
**)&bus
->backend
, NULL
, 0, NULL
);
236 static void dummy_dev_unparent(Object
*obj
)
238 DummyDev
*dev
= DUMMY_DEV(obj
);
239 object_unparent(OBJECT(dev
->bus
));
242 static void dummy_dev_class_init(ObjectClass
*klass
, void *opaque
)
244 klass
->unparent
= dummy_dev_unparent
;
248 static void dummy_bus_init(Object
*obj
)
252 static void dummy_bus_unparent(Object
*obj
)
254 DummyBus
*bus
= DUMMY_BUS(obj
);
255 object_property_del(obj
->parent
, "backend", NULL
);
256 object_unparent(OBJECT(bus
->backend
));
259 static void dummy_bus_class_init(ObjectClass
*klass
, void *opaque
)
261 klass
->unparent
= dummy_bus_unparent
;
264 static void dummy_backend_init(Object
*obj
)
269 static const TypeInfo dummy_dev_info
= {
270 .name
= TYPE_DUMMY_DEV
,
271 .parent
= TYPE_OBJECT
,
272 .instance_size
= sizeof(DummyDev
),
273 .instance_init
= dummy_dev_init
,
274 .class_size
= sizeof(DummyDevClass
),
275 .class_init
= dummy_dev_class_init
,
278 static const TypeInfo dummy_bus_info
= {
279 .name
= TYPE_DUMMY_BUS
,
280 .parent
= TYPE_OBJECT
,
281 .instance_size
= sizeof(DummyBus
),
282 .instance_init
= dummy_bus_init
,
283 .class_size
= sizeof(DummyBusClass
),
284 .class_init
= dummy_bus_class_init
,
287 static const TypeInfo dummy_backend_info
= {
288 .name
= TYPE_DUMMY_BACKEND
,
289 .parent
= TYPE_OBJECT
,
290 .instance_size
= sizeof(DummyBackend
),
291 .instance_init
= dummy_backend_init
,
292 .class_size
= sizeof(DummyBackendClass
),
297 static void test_dummy_createv(void)
300 Object
*parent
= object_get_objects_root();
301 DummyObject
*dobj
= DUMMY_OBJECT(
302 object_new_with_props(TYPE_DUMMY
,
307 "sv", "Hiss hiss hiss",
311 g_assert(err
== NULL
);
312 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
313 g_assert(dobj
->bv
== true);
314 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
316 g_assert(object_resolve_path_component(parent
, "dummy0")
319 object_unparent(OBJECT(dobj
));
323 static Object
*new_helper(Error
**errp
,
330 va_start(vargs
, parent
);
331 obj
= object_new_with_propv(TYPE_DUMMY
,
340 static void test_dummy_createlist(void)
343 Object
*parent
= object_get_objects_root();
344 DummyObject
*dobj
= DUMMY_OBJECT(
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
));
363 static void test_dummy_badenum(void)
366 Object
*parent
= object_get_objects_root();
368 object_new_with_props(TYPE_DUMMY
,
373 "sv", "Hiss hiss hiss",
377 g_assert(dobj
== NULL
);
378 g_assert(err
!= NULL
);
379 g_assert_cmpstr(error_get_pretty(err
), ==,
380 "Invalid parameter 'yeti'");
382 g_assert(object_resolve_path_component(parent
, "dummy0")
389 static void test_dummy_getenum(void)
393 Object
*parent
= object_get_objects_root();
394 DummyObject
*dobj
= DUMMY_OBJECT(
395 object_new_with_props(TYPE_DUMMY
,
402 g_assert(err
== NULL
);
403 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
405 val
= object_property_get_enum(OBJECT(dobj
),
409 g_assert(err
== NULL
);
410 g_assert(val
== DUMMY_PLATYPUS
);
412 /* A bad enum type name */
413 val
= object_property_get_enum(OBJECT(dobj
),
417 g_assert(err
!= NULL
);
421 /* A non-enum property name */
422 val
= object_property_get_enum(OBJECT(dobj
),
426 g_assert(err
!= NULL
);
429 object_unparent(OBJECT(dobj
));
433 static void test_dummy_iterator(void)
435 Object
*parent
= object_get_objects_root();
436 DummyObject
*dobj
= DUMMY_OBJECT(
437 object_new_with_props(TYPE_DUMMY
,
442 "sv", "Hiss hiss hiss",
446 ObjectProperty
*prop
;
447 ObjectPropertyIterator
*iter
;
448 bool seenbv
= false, seensv
= false, seenav
= false, seentype
;
450 iter
= object_property_iter_init(OBJECT(dobj
));
451 while ((prop
= object_property_iter_next(iter
))) {
452 if (g_str_equal(prop
->name
, "bv")) {
454 } else if (g_str_equal(prop
->name
, "sv")) {
456 } else if (g_str_equal(prop
->name
, "av")) {
458 } else if (g_str_equal(prop
->name
, "type")) {
459 /* This prop comes from the base Object class */
462 g_printerr("Found prop '%s'\n", prop
->name
);
463 g_assert_not_reached();
466 object_property_iter_free(iter
);
472 object_unparent(OBJECT(dobj
));
476 static void test_dummy_delchild(void)
478 Object
*parent
= object_get_objects_root();
479 DummyDev
*dev
= DUMMY_DEV(
480 object_new_with_props(TYPE_DUMMY_DEV
,
486 object_unparent(OBJECT(dev
));
489 int main(int argc
, char **argv
)
491 g_test_init(&argc
, &argv
, NULL
);
493 module_call_init(MODULE_INIT_QOM
);
494 type_register_static(&dummy_info
);
495 type_register_static(&dummy_dev_info
);
496 type_register_static(&dummy_bus_info
);
497 type_register_static(&dummy_backend_info
);
499 g_test_add_func("/qom/proplist/createlist", test_dummy_createlist
);
500 g_test_add_func("/qom/proplist/createv", test_dummy_createv
);
501 g_test_add_func("/qom/proplist/badenum", test_dummy_badenum
);
502 g_test_add_func("/qom/proplist/getenum", test_dummy_getenum
);
503 g_test_add_func("/qom/proplist/iterator", test_dummy_iterator
);
504 g_test_add_func("/qom/proplist/delchild", test_dummy_delchild
);