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"
24 #include "qapi/error.h"
25 #include "qom/object.h"
26 #include "qemu/module.h"
29 #define TYPE_DUMMY "qemu-dummy"
31 typedef struct DummyObject DummyObject
;
32 typedef struct DummyObjectClass DummyObjectClass
;
34 #define DUMMY_OBJECT(obj) \
35 OBJECT_CHECK(DummyObject, (obj), TYPE_DUMMY)
37 typedef enum DummyAnimal DummyAnimal
;
47 static const char *const dummy_animal_map
[DUMMY_LAST
+ 1] = {
48 [DUMMY_FROG
] = "frog",
49 [DUMMY_ALLIGATOR
] = "alligator",
50 [DUMMY_PLATYPUS
] = "platypus",
62 struct DummyObjectClass
{
63 ObjectClass parent_class
;
67 static void dummy_set_bv(Object
*obj
,
71 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
76 static bool dummy_get_bv(Object
*obj
,
79 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
85 static void dummy_set_av(Object
*obj
,
89 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
94 static int dummy_get_av(Object
*obj
,
97 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
103 static void dummy_set_sv(Object
*obj
,
107 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
110 dobj
->sv
= g_strdup(value
);
113 static char *dummy_get_sv(Object
*obj
,
116 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
118 return g_strdup(dobj
->sv
);
122 static void dummy_init(Object
*obj
)
124 object_property_add_bool(obj
, "bv",
131 static void dummy_class_init(ObjectClass
*cls
, void *data
)
133 object_class_property_add_bool(cls
, "bv",
137 object_class_property_add_str(cls
, "sv",
141 object_class_property_add_enum(cls
, "av",
150 static void dummy_finalize(Object
*obj
)
152 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
158 static const TypeInfo dummy_info
= {
160 .parent
= TYPE_OBJECT
,
161 .instance_size
= sizeof(DummyObject
),
162 .instance_init
= dummy_init
,
163 .instance_finalize
= dummy_finalize
,
164 .class_size
= sizeof(DummyObjectClass
),
165 .class_init
= dummy_class_init
,
170 * The following 3 object classes are used to
171 * simulate the kind of relationships seen in
172 * qdev, which result in complex object
173 * property destruction ordering.
175 * DummyDev has a 'bus' child to a DummyBus
176 * DummyBus has a 'backend' child to a DummyBackend
177 * DummyDev has a 'backend' link to DummyBackend
179 * When DummyDev is finalized, it unparents the
180 * DummyBackend, which unparents the DummyDev
181 * which deletes the 'backend' link from DummyDev
182 * to DummyBackend. This illustrates that the
183 * object_property_del_all() method needs to
184 * cope with the list of properties being changed
185 * while it iterates over them.
187 typedef struct DummyDev DummyDev
;
188 typedef struct DummyDevClass DummyDevClass
;
189 typedef struct DummyBus DummyBus
;
190 typedef struct DummyBusClass DummyBusClass
;
191 typedef struct DummyBackend DummyBackend
;
192 typedef struct DummyBackendClass DummyBackendClass
;
194 #define TYPE_DUMMY_DEV "qemu-dummy-dev"
195 #define TYPE_DUMMY_BUS "qemu-dummy-bus"
196 #define TYPE_DUMMY_BACKEND "qemu-dummy-backend"
198 #define DUMMY_DEV(obj) \
199 OBJECT_CHECK(DummyDev, (obj), TYPE_DUMMY_DEV)
200 #define DUMMY_BUS(obj) \
201 OBJECT_CHECK(DummyBus, (obj), TYPE_DUMMY_BUS)
202 #define DUMMY_BACKEND(obj) \
203 OBJECT_CHECK(DummyBackend, (obj), TYPE_DUMMY_BACKEND)
211 struct DummyDevClass
{
212 ObjectClass parent_class
;
218 DummyBackend
*backend
;
221 struct DummyBusClass
{
222 ObjectClass parent_class
;
225 struct DummyBackend
{
229 struct DummyBackendClass
{
230 ObjectClass parent_class
;
234 static void dummy_dev_init(Object
*obj
)
236 DummyDev
*dev
= DUMMY_DEV(obj
);
237 DummyBus
*bus
= DUMMY_BUS(object_new(TYPE_DUMMY_BUS
));
238 DummyBackend
*backend
= DUMMY_BACKEND(object_new(TYPE_DUMMY_BACKEND
));
240 object_property_add_child(obj
, "bus", OBJECT(bus
), NULL
);
242 object_property_add_child(OBJECT(bus
), "backend", OBJECT(backend
), NULL
);
243 bus
->backend
= backend
;
245 object_property_add_link(obj
, "backend", TYPE_DUMMY_BACKEND
,
246 (Object
**)&bus
->backend
, NULL
, 0, NULL
);
249 static void dummy_dev_unparent(Object
*obj
)
251 DummyDev
*dev
= DUMMY_DEV(obj
);
252 object_unparent(OBJECT(dev
->bus
));
255 static void dummy_dev_class_init(ObjectClass
*klass
, void *opaque
)
257 klass
->unparent
= dummy_dev_unparent
;
261 static void dummy_bus_init(Object
*obj
)
265 static void dummy_bus_unparent(Object
*obj
)
267 DummyBus
*bus
= DUMMY_BUS(obj
);
268 object_property_del(obj
->parent
, "backend", NULL
);
269 object_unparent(OBJECT(bus
->backend
));
272 static void dummy_bus_class_init(ObjectClass
*klass
, void *opaque
)
274 klass
->unparent
= dummy_bus_unparent
;
277 static void dummy_backend_init(Object
*obj
)
282 static const TypeInfo dummy_dev_info
= {
283 .name
= TYPE_DUMMY_DEV
,
284 .parent
= TYPE_OBJECT
,
285 .instance_size
= sizeof(DummyDev
),
286 .instance_init
= dummy_dev_init
,
287 .class_size
= sizeof(DummyDevClass
),
288 .class_init
= dummy_dev_class_init
,
291 static const TypeInfo dummy_bus_info
= {
292 .name
= TYPE_DUMMY_BUS
,
293 .parent
= TYPE_OBJECT
,
294 .instance_size
= sizeof(DummyBus
),
295 .instance_init
= dummy_bus_init
,
296 .class_size
= sizeof(DummyBusClass
),
297 .class_init
= dummy_bus_class_init
,
300 static const TypeInfo dummy_backend_info
= {
301 .name
= TYPE_DUMMY_BACKEND
,
302 .parent
= TYPE_OBJECT
,
303 .instance_size
= sizeof(DummyBackend
),
304 .instance_init
= dummy_backend_init
,
305 .class_size
= sizeof(DummyBackendClass
),
310 static void test_dummy_createv(void)
313 Object
*parent
= object_get_objects_root();
314 DummyObject
*dobj
= DUMMY_OBJECT(
315 object_new_with_props(TYPE_DUMMY
,
320 "sv", "Hiss hiss hiss",
324 g_assert(err
== NULL
);
325 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
326 g_assert(dobj
->bv
== true);
327 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
329 g_assert(object_resolve_path_component(parent
, "dummy0")
332 object_unparent(OBJECT(dobj
));
336 static Object
*new_helper(Error
**errp
,
343 va_start(vargs
, parent
);
344 obj
= object_new_with_propv(TYPE_DUMMY
,
353 static void test_dummy_createlist(void)
356 Object
*parent
= object_get_objects_root();
357 DummyObject
*dobj
= DUMMY_OBJECT(
361 "sv", "Hiss hiss hiss",
365 g_assert(err
== NULL
);
366 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
367 g_assert(dobj
->bv
== true);
368 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
370 g_assert(object_resolve_path_component(parent
, "dummy0")
373 object_unparent(OBJECT(dobj
));
376 static void test_dummy_badenum(void)
379 Object
*parent
= object_get_objects_root();
381 object_new_with_props(TYPE_DUMMY
,
386 "sv", "Hiss hiss hiss",
390 g_assert(dobj
== NULL
);
391 g_assert(err
!= NULL
);
392 g_assert_cmpstr(error_get_pretty(err
), ==,
393 "Invalid parameter 'yeti'");
395 g_assert(object_resolve_path_component(parent
, "dummy0")
402 static void test_dummy_getenum(void)
406 Object
*parent
= object_get_objects_root();
407 DummyObject
*dobj
= DUMMY_OBJECT(
408 object_new_with_props(TYPE_DUMMY
,
415 g_assert(err
== NULL
);
416 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
418 val
= object_property_get_enum(OBJECT(dobj
),
422 g_assert(err
== NULL
);
423 g_assert(val
== DUMMY_PLATYPUS
);
425 /* A bad enum type name */
426 val
= object_property_get_enum(OBJECT(dobj
),
430 g_assert(err
!= NULL
);
434 /* A non-enum property name */
435 val
= object_property_get_enum(OBJECT(dobj
),
439 g_assert(err
!= NULL
);
442 object_unparent(OBJECT(dobj
));
446 static void test_dummy_iterator(void)
448 Object
*parent
= object_get_objects_root();
449 DummyObject
*dobj
= DUMMY_OBJECT(
450 object_new_with_props(TYPE_DUMMY
,
455 "sv", "Hiss hiss hiss",
459 ObjectProperty
*prop
;
460 ObjectPropertyIterator iter
;
461 bool seenbv
= false, seensv
= false, seenav
= false, seentype
;
463 object_property_iter_init(&iter
, OBJECT(dobj
));
464 while ((prop
= object_property_iter_next(&iter
))) {
465 if (g_str_equal(prop
->name
, "bv")) {
467 } else if (g_str_equal(prop
->name
, "sv")) {
469 } else if (g_str_equal(prop
->name
, "av")) {
471 } else if (g_str_equal(prop
->name
, "type")) {
472 /* This prop comes from the base Object class */
475 g_printerr("Found prop '%s'\n", prop
->name
);
476 g_assert_not_reached();
484 object_unparent(OBJECT(dobj
));
488 static void test_dummy_delchild(void)
490 Object
*parent
= object_get_objects_root();
491 DummyDev
*dev
= DUMMY_DEV(
492 object_new_with_props(TYPE_DUMMY_DEV
,
498 object_unparent(OBJECT(dev
));
501 int main(int argc
, char **argv
)
503 g_test_init(&argc
, &argv
, NULL
);
505 module_call_init(MODULE_INIT_QOM
);
506 type_register_static(&dummy_info
);
507 type_register_static(&dummy_dev_info
);
508 type_register_static(&dummy_bus_info
);
509 type_register_static(&dummy_backend_info
);
511 g_test_add_func("/qom/proplist/createlist", test_dummy_createlist
);
512 g_test_add_func("/qom/proplist/createv", test_dummy_createv
);
513 g_test_add_func("/qom/proplist/badenum", test_dummy_badenum
);
514 g_test_add_func("/qom/proplist/getenum", test_dummy_getenum
);
515 g_test_add_func("/qom/proplist/iterator", test_dummy_iterator
);
516 g_test_add_func("/qom/proplist/delchild", test_dummy_delchild
);