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_init(Object
*obj
)
235 DummyDev
*dev
= DUMMY_DEV(obj
);
236 DummyBus
*bus
= DUMMY_BUS(object_new(TYPE_DUMMY_BUS
));
237 DummyBackend
*backend
= DUMMY_BACKEND(object_new(TYPE_DUMMY_BACKEND
));
239 object_property_add_child(obj
, "bus", OBJECT(bus
), NULL
);
241 object_property_add_child(OBJECT(bus
), "backend", OBJECT(backend
), NULL
);
242 bus
->backend
= backend
;
244 object_property_add_link(obj
, "backend", TYPE_DUMMY_BACKEND
,
245 (Object
**)&bus
->backend
, NULL
, 0, NULL
);
248 static void dummy_dev_unparent(Object
*obj
)
250 DummyDev
*dev
= DUMMY_DEV(obj
);
251 object_unparent(OBJECT(dev
->bus
));
254 static void dummy_dev_class_init(ObjectClass
*klass
, void *opaque
)
256 klass
->unparent
= dummy_dev_unparent
;
260 static void dummy_bus_init(Object
*obj
)
264 static void dummy_bus_unparent(Object
*obj
)
266 DummyBus
*bus
= DUMMY_BUS(obj
);
267 object_property_del(obj
->parent
, "backend", NULL
);
268 object_unparent(OBJECT(bus
->backend
));
271 static void dummy_bus_class_init(ObjectClass
*klass
, void *opaque
)
273 klass
->unparent
= dummy_bus_unparent
;
276 static void dummy_backend_init(Object
*obj
)
281 static const TypeInfo dummy_dev_info
= {
282 .name
= TYPE_DUMMY_DEV
,
283 .parent
= TYPE_OBJECT
,
284 .instance_size
= sizeof(DummyDev
),
285 .instance_init
= dummy_dev_init
,
286 .class_size
= sizeof(DummyDevClass
),
287 .class_init
= dummy_dev_class_init
,
290 static const TypeInfo dummy_bus_info
= {
291 .name
= TYPE_DUMMY_BUS
,
292 .parent
= TYPE_OBJECT
,
293 .instance_size
= sizeof(DummyBus
),
294 .instance_init
= dummy_bus_init
,
295 .class_size
= sizeof(DummyBusClass
),
296 .class_init
= dummy_bus_class_init
,
299 static const TypeInfo dummy_backend_info
= {
300 .name
= TYPE_DUMMY_BACKEND
,
301 .parent
= TYPE_OBJECT
,
302 .instance_size
= sizeof(DummyBackend
),
303 .instance_init
= dummy_backend_init
,
304 .class_size
= sizeof(DummyBackendClass
),
309 static void test_dummy_createv(void)
312 Object
*parent
= object_get_objects_root();
313 DummyObject
*dobj
= DUMMY_OBJECT(
314 object_new_with_props(TYPE_DUMMY
,
319 "sv", "Hiss hiss hiss",
323 g_assert(err
== NULL
);
324 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
325 g_assert(dobj
->bv
== true);
326 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
328 g_assert(object_resolve_path_component(parent
, "dummy0")
331 object_unparent(OBJECT(dobj
));
335 static Object
*new_helper(Error
**errp
,
342 va_start(vargs
, parent
);
343 obj
= object_new_with_propv(TYPE_DUMMY
,
352 static void test_dummy_createlist(void)
355 Object
*parent
= object_get_objects_root();
356 DummyObject
*dobj
= DUMMY_OBJECT(
360 "sv", "Hiss hiss hiss",
364 g_assert(err
== NULL
);
365 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
366 g_assert(dobj
->bv
== true);
367 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
369 g_assert(object_resolve_path_component(parent
, "dummy0")
372 object_unparent(OBJECT(dobj
));
375 static void test_dummy_badenum(void)
378 Object
*parent
= object_get_objects_root();
380 object_new_with_props(TYPE_DUMMY
,
385 "sv", "Hiss hiss hiss",
389 g_assert(dobj
== NULL
);
390 g_assert(err
!= NULL
);
391 g_assert_cmpstr(error_get_pretty(err
), ==,
392 "Invalid parameter 'yeti'");
394 g_assert(object_resolve_path_component(parent
, "dummy0")
401 static void test_dummy_getenum(void)
405 Object
*parent
= object_get_objects_root();
406 DummyObject
*dobj
= DUMMY_OBJECT(
407 object_new_with_props(TYPE_DUMMY
,
414 g_assert(err
== NULL
);
415 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
417 val
= object_property_get_enum(OBJECT(dobj
),
421 g_assert(err
== NULL
);
422 g_assert(val
== DUMMY_PLATYPUS
);
424 /* A bad enum type name */
425 val
= object_property_get_enum(OBJECT(dobj
),
429 g_assert(err
!= NULL
);
433 /* A non-enum property name */
434 val
= object_property_get_enum(OBJECT(dobj
),
438 g_assert(err
!= NULL
);
441 object_unparent(OBJECT(dobj
));
445 static void test_dummy_iterator(void)
447 Object
*parent
= object_get_objects_root();
448 DummyObject
*dobj
= DUMMY_OBJECT(
449 object_new_with_props(TYPE_DUMMY
,
454 "sv", "Hiss hiss hiss",
458 ObjectProperty
*prop
;
459 ObjectPropertyIterator iter
;
460 bool seenbv
= false, seensv
= false, seenav
= false, seentype
;
462 object_property_iter_init(&iter
, OBJECT(dobj
));
463 while ((prop
= object_property_iter_next(&iter
))) {
464 if (g_str_equal(prop
->name
, "bv")) {
466 } else if (g_str_equal(prop
->name
, "sv")) {
468 } else if (g_str_equal(prop
->name
, "av")) {
470 } else if (g_str_equal(prop
->name
, "type")) {
471 /* This prop comes from the base Object class */
474 g_printerr("Found prop '%s'\n", prop
->name
);
475 g_assert_not_reached();
483 object_unparent(OBJECT(dobj
));
487 static void test_dummy_delchild(void)
489 Object
*parent
= object_get_objects_root();
490 DummyDev
*dev
= DUMMY_DEV(
491 object_new_with_props(TYPE_DUMMY_DEV
,
497 object_unparent(OBJECT(dev
));
500 int main(int argc
, char **argv
)
502 g_test_init(&argc
, &argv
, NULL
);
504 module_call_init(MODULE_INIT_QOM
);
505 type_register_static(&dummy_info
);
506 type_register_static(&dummy_dev_info
);
507 type_register_static(&dummy_bus_info
);
508 type_register_static(&dummy_backend_info
);
510 g_test_add_func("/qom/proplist/createlist", test_dummy_createlist
);
511 g_test_add_func("/qom/proplist/createv", test_dummy_createv
);
512 g_test_add_func("/qom/proplist/badenum", test_dummy_badenum
);
513 g_test_add_func("/qom/proplist/getenum", test_dummy_getenum
);
514 g_test_add_func("/qom/proplist/iterator", test_dummy_iterator
);
515 g_test_add_func("/qom/proplist/delchild", test_dummy_delchild
);