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",
129 static void dummy_class_init(ObjectClass
*cls
, void *data
)
131 object_class_property_add_bool(cls
, "bv",
135 object_class_property_add_str(cls
, "sv",
139 object_class_property_add_enum(cls
, "av",
148 static void dummy_finalize(Object
*obj
)
150 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
156 static const TypeInfo dummy_info
= {
158 .parent
= TYPE_OBJECT
,
159 .instance_size
= sizeof(DummyObject
),
160 .instance_init
= dummy_init
,
161 .instance_finalize
= dummy_finalize
,
162 .class_size
= sizeof(DummyObjectClass
),
163 .class_init
= dummy_class_init
,
168 * The following 3 object classes are used to
169 * simulate the kind of relationships seen in
170 * qdev, which result in complex object
171 * property destruction ordering.
173 * DummyDev has a 'bus' child to a DummyBus
174 * DummyBus has a 'backend' child to a DummyBackend
175 * DummyDev has a 'backend' link to DummyBackend
177 * When DummyDev is finalized, it unparents the
178 * DummyBackend, which unparents the DummyDev
179 * which deletes the 'backend' link from DummyDev
180 * to DummyBackend. This illustrates that the
181 * object_property_del_all() method needs to
182 * cope with the list of properties being changed
183 * while it iterates over them.
185 typedef struct DummyDev DummyDev
;
186 typedef struct DummyDevClass DummyDevClass
;
187 typedef struct DummyBus DummyBus
;
188 typedef struct DummyBusClass DummyBusClass
;
189 typedef struct DummyBackend DummyBackend
;
190 typedef struct DummyBackendClass DummyBackendClass
;
192 #define TYPE_DUMMY_DEV "qemu-dummy-dev"
193 #define TYPE_DUMMY_BUS "qemu-dummy-bus"
194 #define TYPE_DUMMY_BACKEND "qemu-dummy-backend"
196 #define DUMMY_DEV(obj) \
197 OBJECT_CHECK(DummyDev, (obj), TYPE_DUMMY_DEV)
198 #define DUMMY_BUS(obj) \
199 OBJECT_CHECK(DummyBus, (obj), TYPE_DUMMY_BUS)
200 #define DUMMY_BACKEND(obj) \
201 OBJECT_CHECK(DummyBackend, (obj), TYPE_DUMMY_BACKEND)
209 struct DummyDevClass
{
210 ObjectClass parent_class
;
216 DummyBackend
*backend
;
219 struct DummyBusClass
{
220 ObjectClass parent_class
;
223 struct DummyBackend
{
227 struct DummyBackendClass
{
228 ObjectClass parent_class
;
232 static void dummy_dev_init(Object
*obj
)
234 DummyDev
*dev
= DUMMY_DEV(obj
);
235 DummyBus
*bus
= DUMMY_BUS(object_new(TYPE_DUMMY_BUS
));
236 DummyBackend
*backend
= DUMMY_BACKEND(object_new(TYPE_DUMMY_BACKEND
));
238 object_property_add_child(obj
, "bus", OBJECT(bus
), NULL
);
240 object_property_add_child(OBJECT(bus
), "backend", OBJECT(backend
), NULL
);
241 bus
->backend
= backend
;
243 object_property_add_link(obj
, "backend", TYPE_DUMMY_BACKEND
,
244 (Object
**)&bus
->backend
, NULL
, 0, NULL
);
247 static void dummy_dev_unparent(Object
*obj
)
249 DummyDev
*dev
= DUMMY_DEV(obj
);
250 object_unparent(OBJECT(dev
->bus
));
253 static void dummy_dev_class_init(ObjectClass
*klass
, void *opaque
)
255 klass
->unparent
= dummy_dev_unparent
;
259 static void dummy_bus_init(Object
*obj
)
263 static void dummy_bus_unparent(Object
*obj
)
265 DummyBus
*bus
= DUMMY_BUS(obj
);
266 object_property_del(obj
->parent
, "backend", NULL
);
267 object_unparent(OBJECT(bus
->backend
));
270 static void dummy_bus_class_init(ObjectClass
*klass
, void *opaque
)
272 klass
->unparent
= dummy_bus_unparent
;
275 static void dummy_backend_init(Object
*obj
)
280 static const TypeInfo dummy_dev_info
= {
281 .name
= TYPE_DUMMY_DEV
,
282 .parent
= TYPE_OBJECT
,
283 .instance_size
= sizeof(DummyDev
),
284 .instance_init
= dummy_dev_init
,
285 .class_size
= sizeof(DummyDevClass
),
286 .class_init
= dummy_dev_class_init
,
289 static const TypeInfo dummy_bus_info
= {
290 .name
= TYPE_DUMMY_BUS
,
291 .parent
= TYPE_OBJECT
,
292 .instance_size
= sizeof(DummyBus
),
293 .instance_init
= dummy_bus_init
,
294 .class_size
= sizeof(DummyBusClass
),
295 .class_init
= dummy_bus_class_init
,
298 static const TypeInfo dummy_backend_info
= {
299 .name
= TYPE_DUMMY_BACKEND
,
300 .parent
= TYPE_OBJECT
,
301 .instance_size
= sizeof(DummyBackend
),
302 .instance_init
= dummy_backend_init
,
303 .class_size
= sizeof(DummyBackendClass
),
308 static void test_dummy_createv(void)
311 Object
*parent
= object_get_objects_root();
312 DummyObject
*dobj
= DUMMY_OBJECT(
313 object_new_with_props(TYPE_DUMMY
,
318 "sv", "Hiss hiss hiss",
322 g_assert(err
== NULL
);
323 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
324 g_assert(dobj
->bv
== true);
325 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
327 g_assert(object_resolve_path_component(parent
, "dummy0")
330 object_unparent(OBJECT(dobj
));
334 static Object
*new_helper(Error
**errp
,
341 va_start(vargs
, parent
);
342 obj
= object_new_with_propv(TYPE_DUMMY
,
351 static void test_dummy_createlist(void)
354 Object
*parent
= object_get_objects_root();
355 DummyObject
*dobj
= DUMMY_OBJECT(
359 "sv", "Hiss hiss hiss",
363 g_assert(err
== NULL
);
364 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
365 g_assert(dobj
->bv
== true);
366 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
368 g_assert(object_resolve_path_component(parent
, "dummy0")
371 object_unparent(OBJECT(dobj
));
374 static void test_dummy_badenum(void)
377 Object
*parent
= object_get_objects_root();
379 object_new_with_props(TYPE_DUMMY
,
384 "sv", "Hiss hiss hiss",
388 g_assert(dobj
== NULL
);
389 g_assert(err
!= NULL
);
390 g_assert_cmpstr(error_get_pretty(err
), ==,
391 "Invalid parameter 'yeti'");
393 g_assert(object_resolve_path_component(parent
, "dummy0")
400 static void test_dummy_getenum(void)
404 Object
*parent
= object_get_objects_root();
405 DummyObject
*dobj
= DUMMY_OBJECT(
406 object_new_with_props(TYPE_DUMMY
,
413 g_assert(err
== NULL
);
414 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
416 val
= object_property_get_enum(OBJECT(dobj
),
420 g_assert(err
== NULL
);
421 g_assert(val
== DUMMY_PLATYPUS
);
423 /* A bad enum type name */
424 val
= object_property_get_enum(OBJECT(dobj
),
428 g_assert(err
!= NULL
);
432 /* A non-enum property name */
433 val
= object_property_get_enum(OBJECT(dobj
),
437 g_assert(err
!= NULL
);
440 object_unparent(OBJECT(dobj
));
444 static void test_dummy_iterator(void)
446 Object
*parent
= object_get_objects_root();
447 DummyObject
*dobj
= DUMMY_OBJECT(
448 object_new_with_props(TYPE_DUMMY
,
453 "sv", "Hiss hiss hiss",
457 ObjectProperty
*prop
;
458 ObjectPropertyIterator iter
;
459 bool seenbv
= false, seensv
= false, seenav
= false, seentype
;
461 object_property_iter_init(&iter
, OBJECT(dobj
));
462 while ((prop
= object_property_iter_next(&iter
))) {
463 if (g_str_equal(prop
->name
, "bv")) {
465 } else if (g_str_equal(prop
->name
, "sv")) {
467 } else if (g_str_equal(prop
->name
, "av")) {
469 } else if (g_str_equal(prop
->name
, "type")) {
470 /* This prop comes from the base Object class */
473 g_printerr("Found prop '%s'\n", prop
->name
);
474 g_assert_not_reached();
482 object_unparent(OBJECT(dobj
));
486 static void test_dummy_delchild(void)
488 Object
*parent
= object_get_objects_root();
489 DummyDev
*dev
= DUMMY_DEV(
490 object_new_with_props(TYPE_DUMMY_DEV
,
496 object_unparent(OBJECT(dev
));
499 int main(int argc
, char **argv
)
501 g_test_init(&argc
, &argv
, NULL
);
503 module_call_init(MODULE_INIT_QOM
);
504 type_register_static(&dummy_info
);
505 type_register_static(&dummy_dev_info
);
506 type_register_static(&dummy_bus_info
);
507 type_register_static(&dummy_backend_info
);
509 g_test_add_func("/qom/proplist/createlist", test_dummy_createlist
);
510 g_test_add_func("/qom/proplist/createv", test_dummy_createv
);
511 g_test_add_func("/qom/proplist/badenum", test_dummy_badenum
);
512 g_test_add_func("/qom/proplist/getenum", test_dummy_getenum
);
513 g_test_add_func("/qom/proplist/iterator", test_dummy_iterator
);
514 g_test_add_func("/qom/proplist/delchild", test_dummy_delchild
);