ui/cocoa: Constify qkeycode translation arrays
[qemu/rayw.git] / tests / unit / check-qom-proplist.c
blobed341088d35f0c2cc2bba24dc8630b3ebe38b04a
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 "qapi/qobject-input-visitor.h"
25 #include "qapi/qmp/qdict.h"
26 #include "qapi/qmp/qobject.h"
27 #include "qom/object.h"
28 #include "qemu/module.h"
29 #include "qemu/option.h"
30 #include "qemu/config-file.h"
31 #include "qom/object_interfaces.h"
34 #define TYPE_DUMMY "qemu-dummy"
36 typedef struct DummyObject DummyObject;
37 typedef struct DummyObjectClass DummyObjectClass;
39 DECLARE_INSTANCE_CHECKER(DummyObject, DUMMY_OBJECT,
40 TYPE_DUMMY)
42 typedef enum DummyAnimal DummyAnimal;
44 enum DummyAnimal {
45 DUMMY_FROG,
46 DUMMY_ALLIGATOR,
47 DUMMY_PLATYPUS,
49 DUMMY_LAST,
52 const QEnumLookup dummy_animal_map = {
53 .array = (const char *const[]) {
54 [DUMMY_FROG] = "frog",
55 [DUMMY_ALLIGATOR] = "alligator",
56 [DUMMY_PLATYPUS] = "platypus",
58 .size = DUMMY_LAST
61 struct DummyObject {
62 Object parent_obj;
64 bool bv;
65 DummyAnimal av;
66 char *sv;
69 struct DummyObjectClass {
70 ObjectClass parent_class;
74 static void dummy_set_bv(Object *obj,
75 bool value,
76 Error **errp)
78 DummyObject *dobj = DUMMY_OBJECT(obj);
80 dobj->bv = value;
83 static bool dummy_get_bv(Object *obj,
84 Error **errp)
86 DummyObject *dobj = DUMMY_OBJECT(obj);
88 return dobj->bv;
92 static void dummy_set_av(Object *obj,
93 int value,
94 Error **errp)
96 DummyObject *dobj = DUMMY_OBJECT(obj);
98 dobj->av = value;
101 static int dummy_get_av(Object *obj,
102 Error **errp)
104 DummyObject *dobj = DUMMY_OBJECT(obj);
106 return dobj->av;
110 static void dummy_set_sv(Object *obj,
111 const char *value,
112 Error **errp)
114 DummyObject *dobj = DUMMY_OBJECT(obj);
116 g_free(dobj->sv);
117 dobj->sv = g_strdup(value);
120 static char *dummy_get_sv(Object *obj,
121 Error **errp)
123 DummyObject *dobj = DUMMY_OBJECT(obj);
125 return g_strdup(dobj->sv);
129 static void dummy_init(Object *obj)
131 object_property_add_bool(obj, "bv",
132 dummy_get_bv,
133 dummy_set_bv);
137 static void dummy_class_init(ObjectClass *cls, void *data)
139 object_class_property_add_str(cls, "sv",
140 dummy_get_sv,
141 dummy_set_sv);
142 object_class_property_add_enum(cls, "av",
143 "DummyAnimal",
144 &dummy_animal_map,
145 dummy_get_av,
146 dummy_set_av);
150 static void dummy_finalize(Object *obj)
152 DummyObject *dobj = DUMMY_OBJECT(obj);
154 g_free(dobj->sv);
158 static const TypeInfo dummy_info = {
159 .name = TYPE_DUMMY,
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,
166 .interfaces = (InterfaceInfo[]) {
167 { TYPE_USER_CREATABLE },
174 * The following 3 object classes are used to
175 * simulate the kind of relationships seen in
176 * qdev, which result in complex object
177 * property destruction ordering.
179 * DummyDev has a 'bus' child to a DummyBus
180 * DummyBus has a 'backend' child to a DummyBackend
181 * DummyDev has a 'backend' link to DummyBackend
183 * When DummyDev is finalized, it unparents the
184 * DummyBackend, which unparents the DummyDev
185 * which deletes the 'backend' link from DummyDev
186 * to DummyBackend. This illustrates that the
187 * object_property_del_all() method needs to
188 * cope with the list of properties being changed
189 * while it iterates over them.
191 typedef struct DummyDev DummyDev;
192 typedef struct DummyDevClass DummyDevClass;
193 typedef struct DummyBus DummyBus;
194 typedef struct DummyBusClass DummyBusClass;
195 typedef struct DummyBackend DummyBackend;
196 typedef struct DummyBackendClass DummyBackendClass;
198 #define TYPE_DUMMY_DEV "qemu-dummy-dev"
199 #define TYPE_DUMMY_BUS "qemu-dummy-bus"
200 #define TYPE_DUMMY_BACKEND "qemu-dummy-backend"
202 DECLARE_INSTANCE_CHECKER(DummyDev, DUMMY_DEV,
203 TYPE_DUMMY_DEV)
204 DECLARE_INSTANCE_CHECKER(DummyBus, DUMMY_BUS,
205 TYPE_DUMMY_BUS)
206 DECLARE_INSTANCE_CHECKER(DummyBackend, DUMMY_BACKEND,
207 TYPE_DUMMY_BACKEND)
209 struct DummyDev {
210 Object parent_obj;
212 DummyBus *bus;
215 struct DummyDevClass {
216 ObjectClass parent_class;
219 struct DummyBus {
220 Object parent_obj;
222 DummyBackend *backend;
225 struct DummyBusClass {
226 ObjectClass parent_class;
229 struct DummyBackend {
230 Object parent_obj;
233 struct DummyBackendClass {
234 ObjectClass parent_class;
238 static void dummy_dev_finalize(Object *obj)
240 DummyDev *dev = DUMMY_DEV(obj);
242 object_unref(OBJECT(dev->bus));
245 static void dummy_dev_init(Object *obj)
247 DummyDev *dev = DUMMY_DEV(obj);
248 DummyBus *bus = DUMMY_BUS(object_new(TYPE_DUMMY_BUS));
249 DummyBackend *backend = DUMMY_BACKEND(object_new(TYPE_DUMMY_BACKEND));
251 object_property_add_child(obj, "bus", OBJECT(bus));
252 dev->bus = bus;
253 object_property_add_child(OBJECT(bus), "backend", OBJECT(backend));
254 bus->backend = backend;
256 object_property_add_link(obj, "backend", TYPE_DUMMY_BACKEND,
257 (Object **)&bus->backend, NULL, 0);
260 static void dummy_dev_unparent(Object *obj)
262 DummyDev *dev = DUMMY_DEV(obj);
263 object_unparent(OBJECT(dev->bus));
266 static void dummy_dev_class_init(ObjectClass *klass, void *opaque)
268 klass->unparent = dummy_dev_unparent;
272 static void dummy_bus_finalize(Object *obj)
274 DummyBus *bus = DUMMY_BUS(obj);
276 object_unref(OBJECT(bus->backend));
279 static void dummy_bus_init(Object *obj)
283 static void dummy_bus_unparent(Object *obj)
285 DummyBus *bus = DUMMY_BUS(obj);
286 object_property_del(obj->parent, "backend");
287 object_unparent(OBJECT(bus->backend));
290 static void dummy_bus_class_init(ObjectClass *klass, void *opaque)
292 klass->unparent = dummy_bus_unparent;
295 static void dummy_backend_init(Object *obj)
300 static const TypeInfo dummy_dev_info = {
301 .name = TYPE_DUMMY_DEV,
302 .parent = TYPE_OBJECT,
303 .instance_size = sizeof(DummyDev),
304 .instance_init = dummy_dev_init,
305 .instance_finalize = dummy_dev_finalize,
306 .class_size = sizeof(DummyDevClass),
307 .class_init = dummy_dev_class_init,
310 static const TypeInfo dummy_bus_info = {
311 .name = TYPE_DUMMY_BUS,
312 .parent = TYPE_OBJECT,
313 .instance_size = sizeof(DummyBus),
314 .instance_init = dummy_bus_init,
315 .instance_finalize = dummy_bus_finalize,
316 .class_size = sizeof(DummyBusClass),
317 .class_init = dummy_bus_class_init,
320 static const TypeInfo dummy_backend_info = {
321 .name = TYPE_DUMMY_BACKEND,
322 .parent = TYPE_OBJECT,
323 .instance_size = sizeof(DummyBackend),
324 .instance_init = dummy_backend_init,
325 .class_size = sizeof(DummyBackendClass),
328 static QemuOptsList qemu_object_opts = {
329 .name = "object",
330 .implied_opt_name = "qom-type",
331 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
332 .desc = {
338 static void test_dummy_createv(void)
340 Error *err = NULL;
341 Object *parent = object_get_objects_root();
342 DummyObject *dobj = DUMMY_OBJECT(
343 object_new_with_props(TYPE_DUMMY,
344 parent,
345 "dummy0",
346 &err,
347 "bv", "yes",
348 "sv", "Hiss hiss hiss",
349 "av", "platypus",
350 NULL));
352 g_assert(err == NULL);
353 g_assert_cmpstr(dobj->sv, ==, "Hiss hiss hiss");
354 g_assert(dobj->bv == true);
355 g_assert(dobj->av == DUMMY_PLATYPUS);
357 g_assert(object_resolve_path_component(parent, "dummy0")
358 == OBJECT(dobj));
360 object_unparent(OBJECT(dobj));
364 static Object *new_helper(Error **errp,
365 Object *parent,
366 ...)
368 va_list vargs;
369 Object *obj;
371 va_start(vargs, parent);
372 obj = object_new_with_propv(TYPE_DUMMY,
373 parent,
374 "dummy0",
375 errp,
376 vargs);
377 va_end(vargs);
378 return obj;
381 static void test_dummy_createlist(void)
383 Error *err = NULL;
384 Object *parent = object_get_objects_root();
385 DummyObject *dobj = DUMMY_OBJECT(
386 new_helper(&err,
387 parent,
388 "bv", "yes",
389 "sv", "Hiss hiss hiss",
390 "av", "platypus",
391 NULL));
393 g_assert(err == NULL);
394 g_assert_cmpstr(dobj->sv, ==, "Hiss hiss hiss");
395 g_assert(dobj->bv == true);
396 g_assert(dobj->av == DUMMY_PLATYPUS);
398 g_assert(object_resolve_path_component(parent, "dummy0")
399 == OBJECT(dobj));
401 object_unparent(OBJECT(dobj));
404 static bool test_create_obj(QDict *qdict, Error **errp)
406 Visitor *v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
407 Object *obj = user_creatable_add_type(TYPE_DUMMY, "dev0", qdict, v, errp);
409 visit_free(v);
410 object_unref(obj);
411 return !!obj;
414 static void test_dummy_createcmdl(void)
416 QDict *qdict;
417 DummyObject *dobj;
418 Error *err = NULL;
419 bool created, help;
420 const char *params = "bv=yes,sv=Hiss hiss hiss,av=platypus";
422 /* Needed for user_creatable_del. */
423 qemu_add_opts(&qemu_object_opts);
425 qdict = keyval_parse(params, "qom-type", &help, &err);
426 g_assert(err == NULL);
427 g_assert(qdict);
428 g_assert(!help);
430 created = test_create_obj(qdict, &err);
431 g_assert(created);
432 g_assert(err == NULL);
433 qobject_unref(qdict);
435 dobj = DUMMY_OBJECT(object_resolve_path_component(object_get_objects_root(),
436 "dev0"));
437 g_assert(dobj);
438 g_assert_cmpstr(dobj->sv, ==, "Hiss hiss hiss");
439 g_assert(dobj->bv == true);
440 g_assert(dobj->av == DUMMY_PLATYPUS);
442 qdict = keyval_parse(params, "qom-type", &help, &err);
443 created = test_create_obj(qdict, &err);
444 g_assert(!created);
445 g_assert(err);
446 g_assert(object_resolve_path_component(object_get_objects_root(), "dev0")
447 == OBJECT(dobj));
448 qobject_unref(qdict);
449 error_free(err);
450 err = NULL;
452 qdict = keyval_parse(params, "qom-type", &help, &err);
453 user_creatable_del("dev0", &error_abort);
454 g_assert(object_resolve_path_component(object_get_objects_root(), "dev0")
455 == NULL);
457 created = test_create_obj(qdict, &err);
458 g_assert(created);
459 g_assert(err == NULL);
460 qobject_unref(qdict);
462 dobj = DUMMY_OBJECT(object_resolve_path_component(object_get_objects_root(),
463 "dev0"));
464 g_assert(dobj);
465 g_assert_cmpstr(dobj->sv, ==, "Hiss hiss hiss");
466 g_assert(dobj->bv == true);
467 g_assert(dobj->av == DUMMY_PLATYPUS);
468 g_assert(object_resolve_path_component(object_get_objects_root(), "dev0")
469 == OBJECT(dobj));
471 object_unparent(OBJECT(dobj));
474 static void test_dummy_badenum(void)
476 Error *err = NULL;
477 Object *parent = object_get_objects_root();
478 Object *dobj =
479 object_new_with_props(TYPE_DUMMY,
480 parent,
481 "dummy0",
482 &err,
483 "bv", "yes",
484 "sv", "Hiss hiss hiss",
485 "av", "yeti",
486 NULL);
488 g_assert(dobj == NULL);
489 g_assert(err != NULL);
490 g_assert_cmpstr(error_get_pretty(err), ==,
491 "Parameter 'av' does not accept value 'yeti'");
493 g_assert(object_resolve_path_component(parent, "dummy0")
494 == NULL);
496 error_free(err);
500 static void test_dummy_getenum(void)
502 Error *err = NULL;
503 int val;
504 Object *parent = object_get_objects_root();
505 DummyObject *dobj = DUMMY_OBJECT(
506 object_new_with_props(TYPE_DUMMY,
507 parent,
508 "dummy0",
509 &err,
510 "av", "platypus",
511 NULL));
513 g_assert(err == NULL);
514 g_assert(dobj->av == DUMMY_PLATYPUS);
516 val = object_property_get_enum(OBJECT(dobj),
517 "av",
518 "DummyAnimal",
519 &error_abort);
520 g_assert(val == DUMMY_PLATYPUS);
522 /* A bad enum type name */
523 val = object_property_get_enum(OBJECT(dobj),
524 "av",
525 "BadAnimal",
526 &err);
527 g_assert(val == -1);
528 error_free_or_abort(&err);
530 /* A non-enum property name */
531 val = object_property_get_enum(OBJECT(dobj),
532 "iv",
533 "DummyAnimal",
534 &err);
535 g_assert(val == -1);
536 error_free_or_abort(&err);
538 object_unparent(OBJECT(dobj));
542 static void test_dummy_prop_iterator(ObjectPropertyIterator *iter,
543 const char *expected[], int n)
545 ObjectProperty *prop;
546 int i;
548 while ((prop = object_property_iter_next(iter))) {
549 for (i = 0; i < n; i++) {
550 if (!g_strcmp0(prop->name, expected[i])) {
551 break;
554 g_assert(i < n);
555 expected[i] = NULL;
558 for (i = 0; i < n; i++) {
559 g_assert(!expected[i]);
563 static void test_dummy_iterator(void)
565 const char *expected[] = {
566 "type", /* inherited from TYPE_OBJECT */
567 "sv", "av", /* class properties */
568 "bv"}; /* instance property */
569 Object *parent = object_get_objects_root();
570 DummyObject *dobj = DUMMY_OBJECT(
571 object_new_with_props(TYPE_DUMMY,
572 parent,
573 "dummy0",
574 &error_abort,
575 "bv", "yes",
576 "sv", "Hiss hiss hiss",
577 "av", "platypus",
578 NULL));
579 ObjectPropertyIterator iter;
581 object_property_iter_init(&iter, OBJECT(dobj));
582 test_dummy_prop_iterator(&iter, expected, ARRAY_SIZE(expected));
583 object_unparent(OBJECT(dobj));
586 static void test_dummy_class_iterator(void)
588 const char *expected[] = { "type", "av", "sv" };
589 ObjectPropertyIterator iter;
590 ObjectClass *klass = object_class_by_name(TYPE_DUMMY);
592 object_class_property_iter_init(&iter, klass);
593 test_dummy_prop_iterator(&iter, expected, ARRAY_SIZE(expected));
596 static void test_dummy_delchild(void)
598 Object *parent = object_get_objects_root();
599 DummyDev *dev = DUMMY_DEV(
600 object_new_with_props(TYPE_DUMMY_DEV,
601 parent,
602 "dev0",
603 &error_abort,
604 NULL));
606 object_unparent(OBJECT(dev));
609 static void test_qom_partial_path(void)
611 Object *root = object_get_objects_root();
612 Object *cont1 = container_get(root, "/cont1");
613 Object *obj1 = object_new(TYPE_DUMMY);
614 Object *obj2a = object_new(TYPE_DUMMY);
615 Object *obj2b = object_new(TYPE_DUMMY);
616 bool ambiguous;
618 /* Objects created:
619 * /cont1
620 * /cont1/obj1
621 * /cont1/obj2 (obj2a)
622 * /obj2 (obj2b)
624 object_property_add_child(cont1, "obj1", obj1);
625 object_unref(obj1);
626 object_property_add_child(cont1, "obj2", obj2a);
627 object_unref(obj2a);
628 object_property_add_child(root, "obj2", obj2b);
629 object_unref(obj2b);
631 ambiguous = false;
632 g_assert(!object_resolve_path_type("", TYPE_DUMMY, &ambiguous));
633 g_assert(ambiguous);
634 g_assert(!object_resolve_path_type("", TYPE_DUMMY, NULL));
636 ambiguous = false;
637 g_assert(!object_resolve_path("obj2", &ambiguous));
638 g_assert(ambiguous);
639 g_assert(!object_resolve_path("obj2", NULL));
641 ambiguous = false;
642 g_assert(object_resolve_path("obj1", &ambiguous) == obj1);
643 g_assert(!ambiguous);
644 g_assert(object_resolve_path("obj1", NULL) == obj1);
646 object_unparent(obj2b);
647 object_unparent(cont1);
650 int main(int argc, char **argv)
652 g_test_init(&argc, &argv, NULL);
654 module_call_init(MODULE_INIT_QOM);
655 type_register_static(&dummy_info);
656 type_register_static(&dummy_dev_info);
657 type_register_static(&dummy_bus_info);
658 type_register_static(&dummy_backend_info);
660 g_test_add_func("/qom/proplist/createlist", test_dummy_createlist);
661 g_test_add_func("/qom/proplist/createv", test_dummy_createv);
662 g_test_add_func("/qom/proplist/createcmdline", test_dummy_createcmdl);
663 g_test_add_func("/qom/proplist/badenum", test_dummy_badenum);
664 g_test_add_func("/qom/proplist/getenum", test_dummy_getenum);
665 g_test_add_func("/qom/proplist/iterator", test_dummy_iterator);
666 g_test_add_func("/qom/proplist/class_iterator", test_dummy_class_iterator);
667 g_test_add_func("/qom/proplist/delchild", test_dummy_delchild);
668 g_test_add_func("/qom/resolve/partial", test_qom_partial_path);
670 return g_test_run();