test-char: start a /char/serial test
[qemu/armbru.git] / tests / check-qom-proplist.c
blob8e432e9ab6dfdd50b74226580a7ac72dc86dc71f
1 /*
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;
41 enum DummyAnimal {
42 DUMMY_FROG,
43 DUMMY_ALLIGATOR,
44 DUMMY_PLATYPUS,
46 DUMMY_LAST,
49 static const char *const dummy_animal_map[DUMMY_LAST + 1] = {
50 [DUMMY_FROG] = "frog",
51 [DUMMY_ALLIGATOR] = "alligator",
52 [DUMMY_PLATYPUS] = "platypus",
53 [DUMMY_LAST] = NULL,
56 struct DummyObject {
57 Object parent_obj;
59 bool bv;
60 DummyAnimal av;
61 char *sv;
64 struct DummyObjectClass {
65 ObjectClass parent_class;
69 static void dummy_set_bv(Object *obj,
70 bool value,
71 Error **errp)
73 DummyObject *dobj = DUMMY_OBJECT(obj);
75 dobj->bv = value;
78 static bool dummy_get_bv(Object *obj,
79 Error **errp)
81 DummyObject *dobj = DUMMY_OBJECT(obj);
83 return dobj->bv;
87 static void dummy_set_av(Object *obj,
88 int value,
89 Error **errp)
91 DummyObject *dobj = DUMMY_OBJECT(obj);
93 dobj->av = value;
96 static int dummy_get_av(Object *obj,
97 Error **errp)
99 DummyObject *dobj = DUMMY_OBJECT(obj);
101 return dobj->av;
105 static void dummy_set_sv(Object *obj,
106 const char *value,
107 Error **errp)
109 DummyObject *dobj = DUMMY_OBJECT(obj);
111 g_free(dobj->sv);
112 dobj->sv = g_strdup(value);
115 static char *dummy_get_sv(Object *obj,
116 Error **errp)
118 DummyObject *dobj = DUMMY_OBJECT(obj);
120 return g_strdup(dobj->sv);
124 static void dummy_init(Object *obj)
126 object_property_add_bool(obj, "bv",
127 dummy_get_bv,
128 dummy_set_bv,
129 NULL);
133 static void dummy_class_init(ObjectClass *cls, void *data)
135 object_class_property_add_bool(cls, "bv",
136 dummy_get_bv,
137 dummy_set_bv,
138 NULL);
139 object_class_property_add_str(cls, "sv",
140 dummy_get_sv,
141 dummy_set_sv,
142 NULL);
143 object_class_property_add_enum(cls, "av",
144 "DummyAnimal",
145 dummy_animal_map,
146 dummy_get_av,
147 dummy_set_av,
148 NULL);
152 static void dummy_finalize(Object *obj)
154 DummyObject *dobj = DUMMY_OBJECT(obj);
156 g_free(dobj->sv);
160 static const TypeInfo dummy_info = {
161 .name = TYPE_DUMMY,
162 .parent = TYPE_OBJECT,
163 .instance_size = sizeof(DummyObject),
164 .instance_init = dummy_init,
165 .instance_finalize = dummy_finalize,
166 .class_size = sizeof(DummyObjectClass),
167 .class_init = dummy_class_init,
168 .interfaces = (InterfaceInfo[]) {
169 { TYPE_USER_CREATABLE },
176 * The following 3 object classes are used to
177 * simulate the kind of relationships seen in
178 * qdev, which result in complex object
179 * property destruction ordering.
181 * DummyDev has a 'bus' child to a DummyBus
182 * DummyBus has a 'backend' child to a DummyBackend
183 * DummyDev has a 'backend' link to DummyBackend
185 * When DummyDev is finalized, it unparents the
186 * DummyBackend, which unparents the DummyDev
187 * which deletes the 'backend' link from DummyDev
188 * to DummyBackend. This illustrates that the
189 * object_property_del_all() method needs to
190 * cope with the list of properties being changed
191 * while it iterates over them.
193 typedef struct DummyDev DummyDev;
194 typedef struct DummyDevClass DummyDevClass;
195 typedef struct DummyBus DummyBus;
196 typedef struct DummyBusClass DummyBusClass;
197 typedef struct DummyBackend DummyBackend;
198 typedef struct DummyBackendClass DummyBackendClass;
200 #define TYPE_DUMMY_DEV "qemu-dummy-dev"
201 #define TYPE_DUMMY_BUS "qemu-dummy-bus"
202 #define TYPE_DUMMY_BACKEND "qemu-dummy-backend"
204 #define DUMMY_DEV(obj) \
205 OBJECT_CHECK(DummyDev, (obj), TYPE_DUMMY_DEV)
206 #define DUMMY_BUS(obj) \
207 OBJECT_CHECK(DummyBus, (obj), TYPE_DUMMY_BUS)
208 #define DUMMY_BACKEND(obj) \
209 OBJECT_CHECK(DummyBackend, (obj), TYPE_DUMMY_BACKEND)
211 struct DummyDev {
212 Object parent_obj;
214 DummyBus *bus;
217 struct DummyDevClass {
218 ObjectClass parent_class;
221 struct DummyBus {
222 Object parent_obj;
224 DummyBackend *backend;
227 struct DummyBusClass {
228 ObjectClass parent_class;
231 struct DummyBackend {
232 Object parent_obj;
235 struct DummyBackendClass {
236 ObjectClass parent_class;
240 static void dummy_dev_finalize(Object *obj)
242 DummyDev *dev = DUMMY_DEV(obj);
244 object_unref(OBJECT(dev->bus));
247 static void dummy_dev_init(Object *obj)
249 DummyDev *dev = DUMMY_DEV(obj);
250 DummyBus *bus = DUMMY_BUS(object_new(TYPE_DUMMY_BUS));
251 DummyBackend *backend = DUMMY_BACKEND(object_new(TYPE_DUMMY_BACKEND));
253 object_property_add_child(obj, "bus", OBJECT(bus), NULL);
254 dev->bus = bus;
255 object_property_add_child(OBJECT(bus), "backend", OBJECT(backend), NULL);
256 bus->backend = backend;
258 object_property_add_link(obj, "backend", TYPE_DUMMY_BACKEND,
259 (Object **)&bus->backend, NULL, 0, NULL);
262 static void dummy_dev_unparent(Object *obj)
264 DummyDev *dev = DUMMY_DEV(obj);
265 object_unparent(OBJECT(dev->bus));
268 static void dummy_dev_class_init(ObjectClass *klass, void *opaque)
270 klass->unparent = dummy_dev_unparent;
274 static void dummy_bus_finalize(Object *obj)
276 DummyBus *bus = DUMMY_BUS(obj);
278 object_unref(OBJECT(bus->backend));
281 static void dummy_bus_init(Object *obj)
285 static void dummy_bus_unparent(Object *obj)
287 DummyBus *bus = DUMMY_BUS(obj);
288 object_property_del(obj->parent, "backend", NULL);
289 object_unparent(OBJECT(bus->backend));
292 static void dummy_bus_class_init(ObjectClass *klass, void *opaque)
294 klass->unparent = dummy_bus_unparent;
297 static void dummy_backend_init(Object *obj)
302 static const TypeInfo dummy_dev_info = {
303 .name = TYPE_DUMMY_DEV,
304 .parent = TYPE_OBJECT,
305 .instance_size = sizeof(DummyDev),
306 .instance_init = dummy_dev_init,
307 .instance_finalize = dummy_dev_finalize,
308 .class_size = sizeof(DummyDevClass),
309 .class_init = dummy_dev_class_init,
312 static const TypeInfo dummy_bus_info = {
313 .name = TYPE_DUMMY_BUS,
314 .parent = TYPE_OBJECT,
315 .instance_size = sizeof(DummyBus),
316 .instance_init = dummy_bus_init,
317 .instance_finalize = dummy_bus_finalize,
318 .class_size = sizeof(DummyBusClass),
319 .class_init = dummy_bus_class_init,
322 static const TypeInfo dummy_backend_info = {
323 .name = TYPE_DUMMY_BACKEND,
324 .parent = TYPE_OBJECT,
325 .instance_size = sizeof(DummyBackend),
326 .instance_init = dummy_backend_init,
327 .class_size = sizeof(DummyBackendClass),
330 static QemuOptsList qemu_object_opts = {
331 .name = "object",
332 .implied_opt_name = "qom-type",
333 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
334 .desc = {
340 static void test_dummy_createv(void)
342 Error *err = NULL;
343 Object *parent = object_get_objects_root();
344 DummyObject *dobj = DUMMY_OBJECT(
345 object_new_with_props(TYPE_DUMMY,
346 parent,
347 "dummy0",
348 &err,
349 "bv", "yes",
350 "sv", "Hiss hiss hiss",
351 "av", "platypus",
352 NULL));
354 g_assert(err == NULL);
355 g_assert_cmpstr(dobj->sv, ==, "Hiss hiss hiss");
356 g_assert(dobj->bv == true);
357 g_assert(dobj->av == DUMMY_PLATYPUS);
359 g_assert(object_resolve_path_component(parent, "dummy0")
360 == OBJECT(dobj));
362 object_unparent(OBJECT(dobj));
366 static Object *new_helper(Error **errp,
367 Object *parent,
368 ...)
370 va_list vargs;
371 Object *obj;
373 va_start(vargs, parent);
374 obj = object_new_with_propv(TYPE_DUMMY,
375 parent,
376 "dummy0",
377 errp,
378 vargs);
379 va_end(vargs);
380 return obj;
383 static void test_dummy_createlist(void)
385 Error *err = NULL;
386 Object *parent = object_get_objects_root();
387 DummyObject *dobj = DUMMY_OBJECT(
388 new_helper(&err,
389 parent,
390 "bv", "yes",
391 "sv", "Hiss hiss hiss",
392 "av", "platypus",
393 NULL));
395 g_assert(err == NULL);
396 g_assert_cmpstr(dobj->sv, ==, "Hiss hiss hiss");
397 g_assert(dobj->bv == true);
398 g_assert(dobj->av == DUMMY_PLATYPUS);
400 g_assert(object_resolve_path_component(parent, "dummy0")
401 == OBJECT(dobj));
403 object_unparent(OBJECT(dobj));
406 static void test_dummy_createcmdl(void)
408 QemuOpts *opts;
409 DummyObject *dobj;
410 Error *err = NULL;
411 const char *params = TYPE_DUMMY \
412 ",id=dev0," \
413 "bv=yes,sv=Hiss hiss hiss,av=platypus";
415 qemu_add_opts(&qemu_object_opts);
416 opts = qemu_opts_parse(&qemu_object_opts, params, true, &err);
417 g_assert(err == NULL);
418 g_assert(opts);
420 dobj = DUMMY_OBJECT(user_creatable_add_opts(opts, &err));
421 g_assert(err == NULL);
422 g_assert(dobj);
423 g_assert_cmpstr(dobj->sv, ==, "Hiss hiss hiss");
424 g_assert(dobj->bv == true);
425 g_assert(dobj->av == DUMMY_PLATYPUS);
427 user_creatable_del("dev0", &err);
428 g_assert(err == NULL);
429 error_free(err);
432 * cmdline-parsing via qemu_opts_parse() results in a QemuOpts entry
433 * corresponding to the Object's ID to be added to the QemuOptsList
434 * for objects. To avoid having this entry conflict with future
435 * Objects using the same ID (which can happen in cases where
436 * qemu_opts_parse() is used to parse the object params, such as
437 * with hmp_object_add() at the time of this comment), we need to
438 * check for this in user_creatable_del() and remove the QemuOpts if
439 * it is present.
441 * The below check ensures this works as expected.
443 g_assert_null(qemu_opts_find(&qemu_object_opts, "dev0"));
446 static void test_dummy_badenum(void)
448 Error *err = NULL;
449 Object *parent = object_get_objects_root();
450 Object *dobj =
451 object_new_with_props(TYPE_DUMMY,
452 parent,
453 "dummy0",
454 &err,
455 "bv", "yes",
456 "sv", "Hiss hiss hiss",
457 "av", "yeti",
458 NULL);
460 g_assert(dobj == NULL);
461 g_assert(err != NULL);
462 g_assert_cmpstr(error_get_pretty(err), ==,
463 "Invalid parameter 'yeti'");
465 g_assert(object_resolve_path_component(parent, "dummy0")
466 == NULL);
468 error_free(err);
472 static void test_dummy_getenum(void)
474 Error *err = NULL;
475 int val;
476 Object *parent = object_get_objects_root();
477 DummyObject *dobj = DUMMY_OBJECT(
478 object_new_with_props(TYPE_DUMMY,
479 parent,
480 "dummy0",
481 &err,
482 "av", "platypus",
483 NULL));
485 g_assert(err == NULL);
486 g_assert(dobj->av == DUMMY_PLATYPUS);
488 val = object_property_get_enum(OBJECT(dobj),
489 "av",
490 "DummyAnimal",
491 &err);
492 g_assert(err == NULL);
493 g_assert(val == DUMMY_PLATYPUS);
495 /* A bad enum type name */
496 val = object_property_get_enum(OBJECT(dobj),
497 "av",
498 "BadAnimal",
499 &err);
500 g_assert(err != NULL);
501 error_free(err);
502 err = NULL;
504 /* A non-enum property name */
505 val = object_property_get_enum(OBJECT(dobj),
506 "iv",
507 "DummyAnimal",
508 &err);
509 g_assert(err != NULL);
510 error_free(err);
512 object_unparent(OBJECT(dobj));
516 static void test_dummy_iterator(void)
518 Object *parent = object_get_objects_root();
519 DummyObject *dobj = DUMMY_OBJECT(
520 object_new_with_props(TYPE_DUMMY,
521 parent,
522 "dummy0",
523 &error_abort,
524 "bv", "yes",
525 "sv", "Hiss hiss hiss",
526 "av", "platypus",
527 NULL));
529 ObjectProperty *prop;
530 ObjectPropertyIterator iter;
531 bool seenbv = false, seensv = false, seenav = false, seentype;
533 object_property_iter_init(&iter, OBJECT(dobj));
534 while ((prop = object_property_iter_next(&iter))) {
535 if (g_str_equal(prop->name, "bv")) {
536 seenbv = true;
537 } else if (g_str_equal(prop->name, "sv")) {
538 seensv = true;
539 } else if (g_str_equal(prop->name, "av")) {
540 seenav = true;
541 } else if (g_str_equal(prop->name, "type")) {
542 /* This prop comes from the base Object class */
543 seentype = true;
544 } else {
545 g_printerr("Found prop '%s'\n", prop->name);
546 g_assert_not_reached();
549 g_assert(seenbv);
550 g_assert(seenav);
551 g_assert(seensv);
552 g_assert(seentype);
554 object_unparent(OBJECT(dobj));
558 static void test_dummy_delchild(void)
560 Object *parent = object_get_objects_root();
561 DummyDev *dev = DUMMY_DEV(
562 object_new_with_props(TYPE_DUMMY_DEV,
563 parent,
564 "dev0",
565 &error_abort,
566 NULL));
568 object_unparent(OBJECT(dev));
571 int main(int argc, char **argv)
573 g_test_init(&argc, &argv, NULL);
575 module_call_init(MODULE_INIT_QOM);
576 type_register_static(&dummy_info);
577 type_register_static(&dummy_dev_info);
578 type_register_static(&dummy_bus_info);
579 type_register_static(&dummy_backend_info);
581 g_test_add_func("/qom/proplist/createlist", test_dummy_createlist);
582 g_test_add_func("/qom/proplist/createv", test_dummy_createv);
583 g_test_add_func("/qom/proplist/createcmdline", test_dummy_createcmdl);
584 g_test_add_func("/qom/proplist/badenum", test_dummy_badenum);
585 g_test_add_func("/qom/proplist/getenum", test_dummy_getenum);
586 g_test_add_func("/qom/proplist/iterator", test_dummy_iterator);
587 g_test_add_func("/qom/proplist/delchild", test_dummy_delchild);
589 return g_test_run();