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"
28 #define TYPE_DUMMY "qemu-dummy"
30 typedef struct DummyObject DummyObject
;
31 typedef struct DummyObjectClass DummyObjectClass
;
33 #define DUMMY_OBJECT(obj) \
34 OBJECT_CHECK(DummyObject, (obj), TYPE_DUMMY)
36 typedef enum DummyAnimal DummyAnimal
;
46 static const char *const dummy_animal_map
[DUMMY_LAST
+ 1] = {
47 [DUMMY_FROG
] = "frog",
48 [DUMMY_ALLIGATOR
] = "alligator",
49 [DUMMY_PLATYPUS
] = "platypus",
61 struct DummyObjectClass
{
62 ObjectClass parent_class
;
66 static void dummy_set_bv(Object
*obj
,
70 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
75 static bool dummy_get_bv(Object
*obj
,
78 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
84 static void dummy_set_av(Object
*obj
,
88 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
93 static int dummy_get_av(Object
*obj
,
96 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
102 static void dummy_set_sv(Object
*obj
,
106 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
109 dobj
->sv
= g_strdup(value
);
112 static char *dummy_get_sv(Object
*obj
,
115 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
117 return g_strdup(dobj
->sv
);
121 static void dummy_init(Object
*obj
)
123 object_property_add_bool(obj
, "bv",
130 static void dummy_class_init(ObjectClass
*cls
, void *data
)
132 object_class_property_add_bool(cls
, "bv",
136 object_class_property_add_str(cls
, "sv",
140 object_class_property_add_enum(cls
, "av",
149 static void dummy_finalize(Object
*obj
)
151 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
157 static const TypeInfo dummy_info
= {
159 .parent
= TYPE_OBJECT
,
160 .instance_size
= sizeof(DummyObject
),
161 .instance_init
= dummy_init
,
162 .instance_finalize
= dummy_finalize
,
163 .class_size
= sizeof(DummyObjectClass
),
164 .class_init
= dummy_class_init
,
169 * The following 3 object classes are used to
170 * simulate the kind of relationships seen in
171 * qdev, which result in complex object
172 * property destruction ordering.
174 * DummyDev has a 'bus' child to a DummyBus
175 * DummyBus has a 'backend' child to a DummyBackend
176 * DummyDev has a 'backend' link to DummyBackend
178 * When DummyDev is finalized, it unparents the
179 * DummyBackend, which unparents the DummyDev
180 * which deletes the 'backend' link from DummyDev
181 * to DummyBackend. This illustrates that the
182 * object_property_del_all() method needs to
183 * cope with the list of properties being changed
184 * while it iterates over them.
186 typedef struct DummyDev DummyDev
;
187 typedef struct DummyDevClass DummyDevClass
;
188 typedef struct DummyBus DummyBus
;
189 typedef struct DummyBusClass DummyBusClass
;
190 typedef struct DummyBackend DummyBackend
;
191 typedef struct DummyBackendClass DummyBackendClass
;
193 #define TYPE_DUMMY_DEV "qemu-dummy-dev"
194 #define TYPE_DUMMY_BUS "qemu-dummy-bus"
195 #define TYPE_DUMMY_BACKEND "qemu-dummy-backend"
197 #define DUMMY_DEV(obj) \
198 OBJECT_CHECK(DummyDev, (obj), TYPE_DUMMY_DEV)
199 #define DUMMY_BUS(obj) \
200 OBJECT_CHECK(DummyBus, (obj), TYPE_DUMMY_BUS)
201 #define DUMMY_BACKEND(obj) \
202 OBJECT_CHECK(DummyBackend, (obj), TYPE_DUMMY_BACKEND)
210 struct DummyDevClass
{
211 ObjectClass parent_class
;
217 DummyBackend
*backend
;
220 struct DummyBusClass
{
221 ObjectClass parent_class
;
224 struct DummyBackend
{
228 struct DummyBackendClass
{
229 ObjectClass parent_class
;
233 static void dummy_dev_finalize(Object
*obj
)
235 DummyDev
*dev
= DUMMY_DEV(obj
);
237 object_unref(OBJECT(dev
->bus
));
240 static void dummy_dev_init(Object
*obj
)
242 DummyDev
*dev
= DUMMY_DEV(obj
);
243 DummyBus
*bus
= DUMMY_BUS(object_new(TYPE_DUMMY_BUS
));
244 DummyBackend
*backend
= DUMMY_BACKEND(object_new(TYPE_DUMMY_BACKEND
));
246 object_property_add_child(obj
, "bus", OBJECT(bus
), NULL
);
248 object_property_add_child(OBJECT(bus
), "backend", OBJECT(backend
), NULL
);
249 bus
->backend
= backend
;
251 object_property_add_link(obj
, "backend", TYPE_DUMMY_BACKEND
,
252 (Object
**)&bus
->backend
, NULL
, 0, NULL
);
255 static void dummy_dev_unparent(Object
*obj
)
257 DummyDev
*dev
= DUMMY_DEV(obj
);
258 object_unparent(OBJECT(dev
->bus
));
261 static void dummy_dev_class_init(ObjectClass
*klass
, void *opaque
)
263 klass
->unparent
= dummy_dev_unparent
;
267 static void dummy_bus_finalize(Object
*obj
)
269 DummyBus
*bus
= DUMMY_BUS(obj
);
271 object_unref(OBJECT(bus
->backend
));
274 static void dummy_bus_init(Object
*obj
)
278 static void dummy_bus_unparent(Object
*obj
)
280 DummyBus
*bus
= DUMMY_BUS(obj
);
281 object_property_del(obj
->parent
, "backend", NULL
);
282 object_unparent(OBJECT(bus
->backend
));
285 static void dummy_bus_class_init(ObjectClass
*klass
, void *opaque
)
287 klass
->unparent
= dummy_bus_unparent
;
290 static void dummy_backend_init(Object
*obj
)
295 static const TypeInfo dummy_dev_info
= {
296 .name
= TYPE_DUMMY_DEV
,
297 .parent
= TYPE_OBJECT
,
298 .instance_size
= sizeof(DummyDev
),
299 .instance_init
= dummy_dev_init
,
300 .instance_finalize
= dummy_dev_finalize
,
301 .class_size
= sizeof(DummyDevClass
),
302 .class_init
= dummy_dev_class_init
,
305 static const TypeInfo dummy_bus_info
= {
306 .name
= TYPE_DUMMY_BUS
,
307 .parent
= TYPE_OBJECT
,
308 .instance_size
= sizeof(DummyBus
),
309 .instance_init
= dummy_bus_init
,
310 .instance_finalize
= dummy_bus_finalize
,
311 .class_size
= sizeof(DummyBusClass
),
312 .class_init
= dummy_bus_class_init
,
315 static const TypeInfo dummy_backend_info
= {
316 .name
= TYPE_DUMMY_BACKEND
,
317 .parent
= TYPE_OBJECT
,
318 .instance_size
= sizeof(DummyBackend
),
319 .instance_init
= dummy_backend_init
,
320 .class_size
= sizeof(DummyBackendClass
),
325 static void test_dummy_createv(void)
328 Object
*parent
= object_get_objects_root();
329 DummyObject
*dobj
= DUMMY_OBJECT(
330 object_new_with_props(TYPE_DUMMY
,
335 "sv", "Hiss hiss hiss",
339 g_assert(err
== NULL
);
340 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
341 g_assert(dobj
->bv
== true);
342 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
344 g_assert(object_resolve_path_component(parent
, "dummy0")
347 object_unparent(OBJECT(dobj
));
351 static Object
*new_helper(Error
**errp
,
358 va_start(vargs
, parent
);
359 obj
= object_new_with_propv(TYPE_DUMMY
,
368 static void test_dummy_createlist(void)
371 Object
*parent
= object_get_objects_root();
372 DummyObject
*dobj
= DUMMY_OBJECT(
376 "sv", "Hiss hiss hiss",
380 g_assert(err
== NULL
);
381 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
382 g_assert(dobj
->bv
== true);
383 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
385 g_assert(object_resolve_path_component(parent
, "dummy0")
388 object_unparent(OBJECT(dobj
));
391 static void test_dummy_badenum(void)
394 Object
*parent
= object_get_objects_root();
396 object_new_with_props(TYPE_DUMMY
,
401 "sv", "Hiss hiss hiss",
405 g_assert(dobj
== NULL
);
406 g_assert(err
!= NULL
);
407 g_assert_cmpstr(error_get_pretty(err
), ==,
408 "Invalid parameter 'yeti'");
410 g_assert(object_resolve_path_component(parent
, "dummy0")
417 static void test_dummy_getenum(void)
421 Object
*parent
= object_get_objects_root();
422 DummyObject
*dobj
= DUMMY_OBJECT(
423 object_new_with_props(TYPE_DUMMY
,
430 g_assert(err
== NULL
);
431 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
433 val
= object_property_get_enum(OBJECT(dobj
),
437 g_assert(err
== NULL
);
438 g_assert(val
== DUMMY_PLATYPUS
);
440 /* A bad enum type name */
441 val
= object_property_get_enum(OBJECT(dobj
),
445 g_assert(err
!= NULL
);
449 /* A non-enum property name */
450 val
= object_property_get_enum(OBJECT(dobj
),
454 g_assert(err
!= NULL
);
457 object_unparent(OBJECT(dobj
));
461 static void test_dummy_iterator(void)
463 Object
*parent
= object_get_objects_root();
464 DummyObject
*dobj
= DUMMY_OBJECT(
465 object_new_with_props(TYPE_DUMMY
,
470 "sv", "Hiss hiss hiss",
474 ObjectProperty
*prop
;
475 ObjectPropertyIterator iter
;
476 bool seenbv
= false, seensv
= false, seenav
= false, seentype
;
478 object_property_iter_init(&iter
, OBJECT(dobj
));
479 while ((prop
= object_property_iter_next(&iter
))) {
480 if (g_str_equal(prop
->name
, "bv")) {
482 } else if (g_str_equal(prop
->name
, "sv")) {
484 } else if (g_str_equal(prop
->name
, "av")) {
486 } else if (g_str_equal(prop
->name
, "type")) {
487 /* This prop comes from the base Object class */
490 g_printerr("Found prop '%s'\n", prop
->name
);
491 g_assert_not_reached();
499 object_unparent(OBJECT(dobj
));
503 static void test_dummy_delchild(void)
505 Object
*parent
= object_get_objects_root();
506 DummyDev
*dev
= DUMMY_DEV(
507 object_new_with_props(TYPE_DUMMY_DEV
,
513 object_unparent(OBJECT(dev
));
516 int main(int argc
, char **argv
)
518 g_test_init(&argc
, &argv
, NULL
);
520 module_call_init(MODULE_INIT_QOM
);
521 type_register_static(&dummy_info
);
522 type_register_static(&dummy_dev_info
);
523 type_register_static(&dummy_bus_info
);
524 type_register_static(&dummy_backend_info
);
526 g_test_add_func("/qom/proplist/createlist", test_dummy_createlist
);
527 g_test_add_func("/qom/proplist/createv", test_dummy_createv
);
528 g_test_add_func("/qom/proplist/badenum", test_dummy_badenum
);
529 g_test_add_func("/qom/proplist/getenum", test_dummy_getenum
);
530 g_test_add_func("/qom/proplist/iterator", test_dummy_iterator
);
531 g_test_add_func("/qom/proplist/delchild", test_dummy_delchild
);