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",
135 static void dummy_class_init(ObjectClass
*cls
, void *data
)
137 object_class_property_add_bool(cls
, "bv",
141 object_class_property_add_str(cls
, "sv",
145 object_class_property_add_enum(cls
, "av",
154 static void dummy_finalize(Object
*obj
)
156 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
162 static const TypeInfo dummy_info
= {
164 .parent
= TYPE_OBJECT
,
165 .instance_size
= sizeof(DummyObject
),
166 .instance_init
= dummy_init
,
167 .instance_finalize
= dummy_finalize
,
168 .class_size
= sizeof(DummyObjectClass
),
169 .class_init
= dummy_class_init
,
170 .interfaces
= (InterfaceInfo
[]) {
171 { TYPE_USER_CREATABLE
},
178 * The following 3 object classes are used to
179 * simulate the kind of relationships seen in
180 * qdev, which result in complex object
181 * property destruction ordering.
183 * DummyDev has a 'bus' child to a DummyBus
184 * DummyBus has a 'backend' child to a DummyBackend
185 * DummyDev has a 'backend' link to DummyBackend
187 * When DummyDev is finalized, it unparents the
188 * DummyBackend, which unparents the DummyDev
189 * which deletes the 'backend' link from DummyDev
190 * to DummyBackend. This illustrates that the
191 * object_property_del_all() method needs to
192 * cope with the list of properties being changed
193 * while it iterates over them.
195 typedef struct DummyDev DummyDev
;
196 typedef struct DummyDevClass DummyDevClass
;
197 typedef struct DummyBus DummyBus
;
198 typedef struct DummyBusClass DummyBusClass
;
199 typedef struct DummyBackend DummyBackend
;
200 typedef struct DummyBackendClass DummyBackendClass
;
202 #define TYPE_DUMMY_DEV "qemu-dummy-dev"
203 #define TYPE_DUMMY_BUS "qemu-dummy-bus"
204 #define TYPE_DUMMY_BACKEND "qemu-dummy-backend"
206 #define DUMMY_DEV(obj) \
207 OBJECT_CHECK(DummyDev, (obj), TYPE_DUMMY_DEV)
208 #define DUMMY_BUS(obj) \
209 OBJECT_CHECK(DummyBus, (obj), TYPE_DUMMY_BUS)
210 #define DUMMY_BACKEND(obj) \
211 OBJECT_CHECK(DummyBackend, (obj), TYPE_DUMMY_BACKEND)
219 struct DummyDevClass
{
220 ObjectClass parent_class
;
226 DummyBackend
*backend
;
229 struct DummyBusClass
{
230 ObjectClass parent_class
;
233 struct DummyBackend
{
237 struct DummyBackendClass
{
238 ObjectClass parent_class
;
242 static void dummy_dev_finalize(Object
*obj
)
244 DummyDev
*dev
= DUMMY_DEV(obj
);
246 object_unref(OBJECT(dev
->bus
));
249 static void dummy_dev_init(Object
*obj
)
251 DummyDev
*dev
= DUMMY_DEV(obj
);
252 DummyBus
*bus
= DUMMY_BUS(object_new(TYPE_DUMMY_BUS
));
253 DummyBackend
*backend
= DUMMY_BACKEND(object_new(TYPE_DUMMY_BACKEND
));
255 object_property_add_child(obj
, "bus", OBJECT(bus
), NULL
);
257 object_property_add_child(OBJECT(bus
), "backend", OBJECT(backend
), NULL
);
258 bus
->backend
= backend
;
260 object_property_add_link(obj
, "backend", TYPE_DUMMY_BACKEND
,
261 (Object
**)&bus
->backend
, NULL
, 0, NULL
);
264 static void dummy_dev_unparent(Object
*obj
)
266 DummyDev
*dev
= DUMMY_DEV(obj
);
267 object_unparent(OBJECT(dev
->bus
));
270 static void dummy_dev_class_init(ObjectClass
*klass
, void *opaque
)
272 klass
->unparent
= dummy_dev_unparent
;
276 static void dummy_bus_finalize(Object
*obj
)
278 DummyBus
*bus
= DUMMY_BUS(obj
);
280 object_unref(OBJECT(bus
->backend
));
283 static void dummy_bus_init(Object
*obj
)
287 static void dummy_bus_unparent(Object
*obj
)
289 DummyBus
*bus
= DUMMY_BUS(obj
);
290 object_property_del(obj
->parent
, "backend", NULL
);
291 object_unparent(OBJECT(bus
->backend
));
294 static void dummy_bus_class_init(ObjectClass
*klass
, void *opaque
)
296 klass
->unparent
= dummy_bus_unparent
;
299 static void dummy_backend_init(Object
*obj
)
304 static const TypeInfo dummy_dev_info
= {
305 .name
= TYPE_DUMMY_DEV
,
306 .parent
= TYPE_OBJECT
,
307 .instance_size
= sizeof(DummyDev
),
308 .instance_init
= dummy_dev_init
,
309 .instance_finalize
= dummy_dev_finalize
,
310 .class_size
= sizeof(DummyDevClass
),
311 .class_init
= dummy_dev_class_init
,
314 static const TypeInfo dummy_bus_info
= {
315 .name
= TYPE_DUMMY_BUS
,
316 .parent
= TYPE_OBJECT
,
317 .instance_size
= sizeof(DummyBus
),
318 .instance_init
= dummy_bus_init
,
319 .instance_finalize
= dummy_bus_finalize
,
320 .class_size
= sizeof(DummyBusClass
),
321 .class_init
= dummy_bus_class_init
,
324 static const TypeInfo dummy_backend_info
= {
325 .name
= TYPE_DUMMY_BACKEND
,
326 .parent
= TYPE_OBJECT
,
327 .instance_size
= sizeof(DummyBackend
),
328 .instance_init
= dummy_backend_init
,
329 .class_size
= sizeof(DummyBackendClass
),
332 static QemuOptsList qemu_object_opts
= {
334 .implied_opt_name
= "qom-type",
335 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
342 static void test_dummy_createv(void)
345 Object
*parent
= object_get_objects_root();
346 DummyObject
*dobj
= DUMMY_OBJECT(
347 object_new_with_props(TYPE_DUMMY
,
352 "sv", "Hiss hiss hiss",
356 g_assert(err
== NULL
);
357 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
358 g_assert(dobj
->bv
== true);
359 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
361 g_assert(object_resolve_path_component(parent
, "dummy0")
364 object_unparent(OBJECT(dobj
));
368 static Object
*new_helper(Error
**errp
,
375 va_start(vargs
, parent
);
376 obj
= object_new_with_propv(TYPE_DUMMY
,
385 static void test_dummy_createlist(void)
388 Object
*parent
= object_get_objects_root();
389 DummyObject
*dobj
= DUMMY_OBJECT(
393 "sv", "Hiss hiss hiss",
397 g_assert(err
== NULL
);
398 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
399 g_assert(dobj
->bv
== true);
400 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
402 g_assert(object_resolve_path_component(parent
, "dummy0")
405 object_unparent(OBJECT(dobj
));
408 static void test_dummy_createcmdl(void)
413 const char *params
= TYPE_DUMMY \
415 "bv=yes,sv=Hiss hiss hiss,av=platypus";
417 qemu_add_opts(&qemu_object_opts
);
418 opts
= qemu_opts_parse(&qemu_object_opts
, params
, true, &err
);
419 g_assert(err
== NULL
);
422 dobj
= DUMMY_OBJECT(user_creatable_add_opts(opts
, &err
));
423 g_assert(err
== NULL
);
425 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
426 g_assert(dobj
->bv
== true);
427 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
429 user_creatable_del("dev0", &err
);
430 g_assert(err
== NULL
);
433 object_unref(OBJECT(dobj
));
436 * cmdline-parsing via qemu_opts_parse() results in a QemuOpts entry
437 * corresponding to the Object's ID to be added to the QemuOptsList
438 * for objects. To avoid having this entry conflict with future
439 * Objects using the same ID (which can happen in cases where
440 * qemu_opts_parse() is used to parse the object params, such as
441 * with hmp_object_add() at the time of this comment), we need to
442 * check for this in user_creatable_del() and remove the QemuOpts if
445 * The below check ensures this works as expected.
447 g_assert_null(qemu_opts_find(&qemu_object_opts
, "dev0"));
450 static void test_dummy_badenum(void)
453 Object
*parent
= object_get_objects_root();
455 object_new_with_props(TYPE_DUMMY
,
460 "sv", "Hiss hiss hiss",
464 g_assert(dobj
== NULL
);
465 g_assert(err
!= NULL
);
466 g_assert_cmpstr(error_get_pretty(err
), ==,
467 "Invalid parameter 'yeti'");
469 g_assert(object_resolve_path_component(parent
, "dummy0")
476 static void test_dummy_getenum(void)
480 Object
*parent
= object_get_objects_root();
481 DummyObject
*dobj
= DUMMY_OBJECT(
482 object_new_with_props(TYPE_DUMMY
,
489 g_assert(err
== NULL
);
490 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
492 val
= object_property_get_enum(OBJECT(dobj
),
496 g_assert(err
== NULL
);
497 g_assert(val
== DUMMY_PLATYPUS
);
499 /* A bad enum type name */
500 val
= object_property_get_enum(OBJECT(dobj
),
504 g_assert(err
!= NULL
);
508 /* A non-enum property name */
509 val
= object_property_get_enum(OBJECT(dobj
),
513 g_assert(err
!= NULL
);
516 object_unparent(OBJECT(dobj
));
520 static void test_dummy_iterator(void)
522 Object
*parent
= object_get_objects_root();
523 DummyObject
*dobj
= DUMMY_OBJECT(
524 object_new_with_props(TYPE_DUMMY
,
529 "sv", "Hiss hiss hiss",
533 ObjectProperty
*prop
;
534 ObjectPropertyIterator iter
;
535 bool seenbv
= false, seensv
= false, seenav
= false, seentype
;
537 object_property_iter_init(&iter
, OBJECT(dobj
));
538 while ((prop
= object_property_iter_next(&iter
))) {
539 if (g_str_equal(prop
->name
, "bv")) {
541 } else if (g_str_equal(prop
->name
, "sv")) {
543 } else if (g_str_equal(prop
->name
, "av")) {
545 } else if (g_str_equal(prop
->name
, "type")) {
546 /* This prop comes from the base Object class */
549 g_printerr("Found prop '%s'\n", prop
->name
);
550 g_assert_not_reached();
558 object_unparent(OBJECT(dobj
));
562 static void test_dummy_delchild(void)
564 Object
*parent
= object_get_objects_root();
565 DummyDev
*dev
= DUMMY_DEV(
566 object_new_with_props(TYPE_DUMMY_DEV
,
572 object_unparent(OBJECT(dev
));
575 static void test_qom_partial_path(void)
577 Object
*root
= object_get_objects_root();
578 Object
*cont1
= container_get(root
, "/cont1");
579 Object
*obj1
= object_new(TYPE_DUMMY
);
580 Object
*obj2a
= object_new(TYPE_DUMMY
);
581 Object
*obj2b
= object_new(TYPE_DUMMY
);
587 * /cont1/obj2 (obj2a)
590 object_property_add_child(cont1
, "obj1", obj1
, &error_abort
);
592 object_property_add_child(cont1
, "obj2", obj2a
, &error_abort
);
594 object_property_add_child(root
, "obj2", obj2b
, &error_abort
);
598 g_assert(!object_resolve_path_type("", TYPE_DUMMY
, &ambiguous
));
600 g_assert(!object_resolve_path_type("", TYPE_DUMMY
, NULL
));
603 g_assert(!object_resolve_path("obj2", &ambiguous
));
605 g_assert(!object_resolve_path("obj2", NULL
));
608 g_assert(object_resolve_path("obj1", &ambiguous
) == obj1
);
609 g_assert(!ambiguous
);
610 g_assert(object_resolve_path("obj1", NULL
) == obj1
);
612 object_unparent(obj2b
);
613 object_unparent(cont1
);
616 int main(int argc
, char **argv
)
618 g_test_init(&argc
, &argv
, NULL
);
620 module_call_init(MODULE_INIT_QOM
);
621 type_register_static(&dummy_info
);
622 type_register_static(&dummy_dev_info
);
623 type_register_static(&dummy_bus_info
);
624 type_register_static(&dummy_backend_info
);
626 g_test_add_func("/qom/proplist/createlist", test_dummy_createlist
);
627 g_test_add_func("/qom/proplist/createv", test_dummy_createv
);
628 g_test_add_func("/qom/proplist/createcmdline", test_dummy_createcmdl
);
629 g_test_add_func("/qom/proplist/badenum", test_dummy_badenum
);
630 g_test_add_func("/qom/proplist/getenum", test_dummy_getenum
);
631 g_test_add_func("/qom/proplist/iterator", test_dummy_iterator
);
632 g_test_add_func("/qom/proplist/delchild", test_dummy_delchild
);
633 g_test_add_func("/qom/resolve/partial", test_qom_partial_path
);