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"
26 #include "qemu/option.h"
27 #include "qemu/config-file.h"
28 #include "qom/object_interfaces.h"
31 #define TYPE_DUMMY "qemu-dummy"
33 typedef struct DummyObject DummyObject
;
34 typedef struct DummyObjectClass DummyObjectClass
;
36 #define DUMMY_OBJECT(obj) \
37 OBJECT_CHECK(DummyObject, (obj), TYPE_DUMMY)
39 typedef enum DummyAnimal DummyAnimal
;
49 const QEnumLookup dummy_animal_map
= {
50 .array
= (const char *const[]) {
51 [DUMMY_FROG
] = "frog",
52 [DUMMY_ALLIGATOR
] = "alligator",
53 [DUMMY_PLATYPUS
] = "platypus",
66 struct DummyObjectClass
{
67 ObjectClass parent_class
;
71 static void dummy_set_bv(Object
*obj
,
75 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
80 static bool dummy_get_bv(Object
*obj
,
83 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
89 static void dummy_set_av(Object
*obj
,
93 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
98 static int dummy_get_av(Object
*obj
,
101 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
107 static void dummy_set_sv(Object
*obj
,
111 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
114 dobj
->sv
= g_strdup(value
);
117 static char *dummy_get_sv(Object
*obj
,
120 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
122 return g_strdup(dobj
->sv
);
126 static void dummy_init(Object
*obj
)
128 object_property_add_bool(obj
, "bv",
134 static void dummy_class_init(ObjectClass
*cls
, void *data
)
136 object_class_property_add_str(cls
, "sv",
139 object_class_property_add_enum(cls
, "av",
147 static void dummy_finalize(Object
*obj
)
149 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
155 static const TypeInfo dummy_info
= {
157 .parent
= TYPE_OBJECT
,
158 .instance_size
= sizeof(DummyObject
),
159 .instance_init
= dummy_init
,
160 .instance_finalize
= dummy_finalize
,
161 .class_size
= sizeof(DummyObjectClass
),
162 .class_init
= dummy_class_init
,
163 .interfaces
= (InterfaceInfo
[]) {
164 { TYPE_USER_CREATABLE
},
171 * The following 3 object classes are used to
172 * simulate the kind of relationships seen in
173 * qdev, which result in complex object
174 * property destruction ordering.
176 * DummyDev has a 'bus' child to a DummyBus
177 * DummyBus has a 'backend' child to a DummyBackend
178 * DummyDev has a 'backend' link to DummyBackend
180 * When DummyDev is finalized, it unparents the
181 * DummyBackend, which unparents the DummyDev
182 * which deletes the 'backend' link from DummyDev
183 * to DummyBackend. This illustrates that the
184 * object_property_del_all() method needs to
185 * cope with the list of properties being changed
186 * while it iterates over them.
188 typedef struct DummyDev DummyDev
;
189 typedef struct DummyDevClass DummyDevClass
;
190 typedef struct DummyBus DummyBus
;
191 typedef struct DummyBusClass DummyBusClass
;
192 typedef struct DummyBackend DummyBackend
;
193 typedef struct DummyBackendClass DummyBackendClass
;
195 #define TYPE_DUMMY_DEV "qemu-dummy-dev"
196 #define TYPE_DUMMY_BUS "qemu-dummy-bus"
197 #define TYPE_DUMMY_BACKEND "qemu-dummy-backend"
199 #define DUMMY_DEV(obj) \
200 OBJECT_CHECK(DummyDev, (obj), TYPE_DUMMY_DEV)
201 #define DUMMY_BUS(obj) \
202 OBJECT_CHECK(DummyBus, (obj), TYPE_DUMMY_BUS)
203 #define DUMMY_BACKEND(obj) \
204 OBJECT_CHECK(DummyBackend, (obj), TYPE_DUMMY_BACKEND)
212 struct DummyDevClass
{
213 ObjectClass parent_class
;
219 DummyBackend
*backend
;
222 struct DummyBusClass
{
223 ObjectClass parent_class
;
226 struct DummyBackend
{
230 struct DummyBackendClass
{
231 ObjectClass parent_class
;
235 static void dummy_dev_finalize(Object
*obj
)
237 DummyDev
*dev
= DUMMY_DEV(obj
);
239 object_unref(OBJECT(dev
->bus
));
242 static void dummy_dev_init(Object
*obj
)
244 DummyDev
*dev
= DUMMY_DEV(obj
);
245 DummyBus
*bus
= DUMMY_BUS(object_new(TYPE_DUMMY_BUS
));
246 DummyBackend
*backend
= DUMMY_BACKEND(object_new(TYPE_DUMMY_BACKEND
));
248 object_property_add_child(obj
, "bus", OBJECT(bus
));
250 object_property_add_child(OBJECT(bus
), "backend", OBJECT(backend
));
251 bus
->backend
= backend
;
253 object_property_add_link(obj
, "backend", TYPE_DUMMY_BACKEND
,
254 (Object
**)&bus
->backend
, NULL
, 0);
257 static void dummy_dev_unparent(Object
*obj
)
259 DummyDev
*dev
= DUMMY_DEV(obj
);
260 object_unparent(OBJECT(dev
->bus
));
263 static void dummy_dev_class_init(ObjectClass
*klass
, void *opaque
)
265 klass
->unparent
= dummy_dev_unparent
;
269 static void dummy_bus_finalize(Object
*obj
)
271 DummyBus
*bus
= DUMMY_BUS(obj
);
273 object_unref(OBJECT(bus
->backend
));
276 static void dummy_bus_init(Object
*obj
)
280 static void dummy_bus_unparent(Object
*obj
)
282 DummyBus
*bus
= DUMMY_BUS(obj
);
283 object_property_del(obj
->parent
, "backend");
284 object_unparent(OBJECT(bus
->backend
));
287 static void dummy_bus_class_init(ObjectClass
*klass
, void *opaque
)
289 klass
->unparent
= dummy_bus_unparent
;
292 static void dummy_backend_init(Object
*obj
)
297 static const TypeInfo dummy_dev_info
= {
298 .name
= TYPE_DUMMY_DEV
,
299 .parent
= TYPE_OBJECT
,
300 .instance_size
= sizeof(DummyDev
),
301 .instance_init
= dummy_dev_init
,
302 .instance_finalize
= dummy_dev_finalize
,
303 .class_size
= sizeof(DummyDevClass
),
304 .class_init
= dummy_dev_class_init
,
307 static const TypeInfo dummy_bus_info
= {
308 .name
= TYPE_DUMMY_BUS
,
309 .parent
= TYPE_OBJECT
,
310 .instance_size
= sizeof(DummyBus
),
311 .instance_init
= dummy_bus_init
,
312 .instance_finalize
= dummy_bus_finalize
,
313 .class_size
= sizeof(DummyBusClass
),
314 .class_init
= dummy_bus_class_init
,
317 static const TypeInfo dummy_backend_info
= {
318 .name
= TYPE_DUMMY_BACKEND
,
319 .parent
= TYPE_OBJECT
,
320 .instance_size
= sizeof(DummyBackend
),
321 .instance_init
= dummy_backend_init
,
322 .class_size
= sizeof(DummyBackendClass
),
325 static QemuOptsList qemu_object_opts
= {
327 .implied_opt_name
= "qom-type",
328 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
335 static void test_dummy_createv(void)
338 Object
*parent
= object_get_objects_root();
339 DummyObject
*dobj
= DUMMY_OBJECT(
340 object_new_with_props(TYPE_DUMMY
,
345 "sv", "Hiss hiss hiss",
349 g_assert(err
== NULL
);
350 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
351 g_assert(dobj
->bv
== true);
352 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
354 g_assert(object_resolve_path_component(parent
, "dummy0")
357 object_unparent(OBJECT(dobj
));
361 static Object
*new_helper(Error
**errp
,
368 va_start(vargs
, parent
);
369 obj
= object_new_with_propv(TYPE_DUMMY
,
378 static void test_dummy_createlist(void)
381 Object
*parent
= object_get_objects_root();
382 DummyObject
*dobj
= DUMMY_OBJECT(
386 "sv", "Hiss hiss hiss",
390 g_assert(err
== NULL
);
391 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
392 g_assert(dobj
->bv
== true);
393 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
395 g_assert(object_resolve_path_component(parent
, "dummy0")
398 object_unparent(OBJECT(dobj
));
401 static void test_dummy_createcmdl(void)
406 const char *params
= TYPE_DUMMY \
408 "bv=yes,sv=Hiss hiss hiss,av=platypus";
410 qemu_add_opts(&qemu_object_opts
);
411 opts
= qemu_opts_parse(&qemu_object_opts
, params
, true, &err
);
412 g_assert(err
== NULL
);
415 dobj
= DUMMY_OBJECT(user_creatable_add_opts(opts
, &err
));
416 g_assert(err
== NULL
);
418 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
419 g_assert(dobj
->bv
== true);
420 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
422 user_creatable_del("dev0", &error_abort
);
424 object_unref(OBJECT(dobj
));
427 * cmdline-parsing via qemu_opts_parse() results in a QemuOpts entry
428 * corresponding to the Object's ID to be added to the QemuOptsList
429 * for objects. To avoid having this entry conflict with future
430 * Objects using the same ID (which can happen in cases where
431 * qemu_opts_parse() is used to parse the object params, such as
432 * with hmp_object_add() at the time of this comment), we need to
433 * check for this in user_creatable_del() and remove the QemuOpts if
436 * The below check ensures this works as expected.
438 g_assert_null(qemu_opts_find(&qemu_object_opts
, "dev0"));
441 static void test_dummy_badenum(void)
444 Object
*parent
= object_get_objects_root();
446 object_new_with_props(TYPE_DUMMY
,
451 "sv", "Hiss hiss hiss",
455 g_assert(dobj
== NULL
);
456 g_assert(err
!= NULL
);
457 g_assert_cmpstr(error_get_pretty(err
), ==,
458 "Invalid parameter 'yeti'");
460 g_assert(object_resolve_path_component(parent
, "dummy0")
467 static void test_dummy_getenum(void)
471 Object
*parent
= object_get_objects_root();
472 DummyObject
*dobj
= DUMMY_OBJECT(
473 object_new_with_props(TYPE_DUMMY
,
480 g_assert(err
== NULL
);
481 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
483 val
= object_property_get_enum(OBJECT(dobj
),
487 g_assert(val
== DUMMY_PLATYPUS
);
489 /* A bad enum type name */
490 val
= object_property_get_enum(OBJECT(dobj
),
494 error_free_or_abort(&err
);
496 /* A non-enum property name */
497 val
= object_property_get_enum(OBJECT(dobj
),
501 error_free_or_abort(&err
);
503 object_unparent(OBJECT(dobj
));
507 static void test_dummy_prop_iterator(ObjectPropertyIterator
*iter
,
508 const char *expected
[], int n
)
510 ObjectProperty
*prop
;
513 while ((prop
= object_property_iter_next(iter
))) {
514 for (i
= 0; i
< n
; i
++) {
515 if (!g_strcmp0(prop
->name
, expected
[i
])) {
523 for (i
= 0; i
< n
; i
++) {
524 g_assert(!expected
[i
]);
528 static void test_dummy_iterator(void)
530 const char *expected
[] = {
531 "type", /* inherited from TYPE_OBJECT */
532 "sv", "av", /* class properties */
533 "bv"}; /* instance property */
534 Object
*parent
= object_get_objects_root();
535 DummyObject
*dobj
= DUMMY_OBJECT(
536 object_new_with_props(TYPE_DUMMY
,
541 "sv", "Hiss hiss hiss",
544 ObjectPropertyIterator iter
;
546 object_property_iter_init(&iter
, OBJECT(dobj
));
547 test_dummy_prop_iterator(&iter
, expected
, ARRAY_SIZE(expected
));
548 object_unparent(OBJECT(dobj
));
551 static void test_dummy_class_iterator(void)
553 const char *expected
[] = { "type", "av", "sv" };
554 ObjectPropertyIterator iter
;
555 ObjectClass
*klass
= object_class_by_name(TYPE_DUMMY
);
557 object_class_property_iter_init(&iter
, klass
);
558 test_dummy_prop_iterator(&iter
, expected
, ARRAY_SIZE(expected
));
561 static void test_dummy_delchild(void)
563 Object
*parent
= object_get_objects_root();
564 DummyDev
*dev
= DUMMY_DEV(
565 object_new_with_props(TYPE_DUMMY_DEV
,
571 object_unparent(OBJECT(dev
));
574 static void test_qom_partial_path(void)
576 Object
*root
= object_get_objects_root();
577 Object
*cont1
= container_get(root
, "/cont1");
578 Object
*obj1
= object_new(TYPE_DUMMY
);
579 Object
*obj2a
= object_new(TYPE_DUMMY
);
580 Object
*obj2b
= object_new(TYPE_DUMMY
);
586 * /cont1/obj2 (obj2a)
589 object_property_add_child(cont1
, "obj1", obj1
);
591 object_property_add_child(cont1
, "obj2", obj2a
);
593 object_property_add_child(root
, "obj2", obj2b
);
597 g_assert(!object_resolve_path_type("", TYPE_DUMMY
, &ambiguous
));
599 g_assert(!object_resolve_path_type("", TYPE_DUMMY
, NULL
));
602 g_assert(!object_resolve_path("obj2", &ambiguous
));
604 g_assert(!object_resolve_path("obj2", NULL
));
607 g_assert(object_resolve_path("obj1", &ambiguous
) == obj1
);
608 g_assert(!ambiguous
);
609 g_assert(object_resolve_path("obj1", NULL
) == obj1
);
611 object_unparent(obj2b
);
612 object_unparent(cont1
);
615 int main(int argc
, char **argv
)
617 g_test_init(&argc
, &argv
, NULL
);
619 module_call_init(MODULE_INIT_QOM
);
620 type_register_static(&dummy_info
);
621 type_register_static(&dummy_dev_info
);
622 type_register_static(&dummy_bus_info
);
623 type_register_static(&dummy_backend_info
);
625 g_test_add_func("/qom/proplist/createlist", test_dummy_createlist
);
626 g_test_add_func("/qom/proplist/createv", test_dummy_createv
);
627 g_test_add_func("/qom/proplist/createcmdline", test_dummy_createcmdl
);
628 g_test_add_func("/qom/proplist/badenum", test_dummy_badenum
);
629 g_test_add_func("/qom/proplist/getenum", test_dummy_getenum
);
630 g_test_add_func("/qom/proplist/iterator", test_dummy_iterator
);
631 g_test_add_func("/qom/proplist/class_iterator", test_dummy_class_iterator
);
632 g_test_add_func("/qom/proplist/delchild", test_dummy_delchild
);
633 g_test_add_func("/qom/resolve/partial", test_qom_partial_path
);