i8257: rename struct dma_regs to I8257Regs
[qemu/ar7.git] / tests / check-qom-proplist.c
blob448d270b68a7526aaee66953a201bd001f25a0a3
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 <glib.h>
23 #include "qom/object.h"
24 #include "qemu/module.h"
27 #define TYPE_DUMMY "qemu-dummy"
29 typedef struct DummyObject DummyObject;
30 typedef struct DummyObjectClass DummyObjectClass;
32 #define DUMMY_OBJECT(obj) \
33 OBJECT_CHECK(DummyObject, (obj), TYPE_DUMMY)
35 typedef enum DummyAnimal DummyAnimal;
37 enum DummyAnimal {
38 DUMMY_FROG,
39 DUMMY_ALLIGATOR,
40 DUMMY_PLATYPUS,
42 DUMMY_LAST,
45 static const char *const dummy_animal_map[DUMMY_LAST + 1] = {
46 [DUMMY_FROG] = "frog",
47 [DUMMY_ALLIGATOR] = "alligator",
48 [DUMMY_PLATYPUS] = "platypus",
49 [DUMMY_LAST] = NULL,
52 struct DummyObject {
53 Object parent_obj;
55 bool bv;
56 DummyAnimal av;
57 char *sv;
60 struct DummyObjectClass {
61 ObjectClass parent_class;
65 static void dummy_set_bv(Object *obj,
66 bool value,
67 Error **errp)
69 DummyObject *dobj = DUMMY_OBJECT(obj);
71 dobj->bv = value;
74 static bool dummy_get_bv(Object *obj,
75 Error **errp)
77 DummyObject *dobj = DUMMY_OBJECT(obj);
79 return dobj->bv;
83 static void dummy_set_av(Object *obj,
84 int value,
85 Error **errp)
87 DummyObject *dobj = DUMMY_OBJECT(obj);
89 dobj->av = value;
92 static int dummy_get_av(Object *obj,
93 Error **errp)
95 DummyObject *dobj = DUMMY_OBJECT(obj);
97 return dobj->av;
101 static void dummy_set_sv(Object *obj,
102 const char *value,
103 Error **errp)
105 DummyObject *dobj = DUMMY_OBJECT(obj);
107 g_free(dobj->sv);
108 dobj->sv = g_strdup(value);
111 static char *dummy_get_sv(Object *obj,
112 Error **errp)
114 DummyObject *dobj = DUMMY_OBJECT(obj);
116 return g_strdup(dobj->sv);
120 static void dummy_init(Object *obj)
122 object_property_add_bool(obj, "bv",
123 dummy_get_bv,
124 dummy_set_bv,
125 NULL);
129 static void dummy_class_init(ObjectClass *cls, void *data)
131 object_class_property_add_bool(cls, "bv",
132 dummy_get_bv,
133 dummy_set_bv,
134 NULL);
135 object_class_property_add_str(cls, "sv",
136 dummy_get_sv,
137 dummy_set_sv,
138 NULL);
139 object_class_property_add_enum(cls, "av",
140 "DummyAnimal",
141 dummy_animal_map,
142 dummy_get_av,
143 dummy_set_av,
144 NULL);
148 static void dummy_finalize(Object *obj)
150 DummyObject *dobj = DUMMY_OBJECT(obj);
152 g_free(dobj->sv);
156 static const TypeInfo dummy_info = {
157 .name = TYPE_DUMMY,
158 .parent = TYPE_OBJECT,
159 .instance_size = sizeof(DummyObject),
160 .instance_init = dummy_init,
161 .instance_finalize = dummy_finalize,
162 .class_size = sizeof(DummyObjectClass),
163 .class_init = dummy_class_init,
168 * The following 3 object classes are used to
169 * simulate the kind of relationships seen in
170 * qdev, which result in complex object
171 * property destruction ordering.
173 * DummyDev has a 'bus' child to a DummyBus
174 * DummyBus has a 'backend' child to a DummyBackend
175 * DummyDev has a 'backend' link to DummyBackend
177 * When DummyDev is finalized, it unparents the
178 * DummyBackend, which unparents the DummyDev
179 * which deletes the 'backend' link from DummyDev
180 * to DummyBackend. This illustrates that the
181 * object_property_del_all() method needs to
182 * cope with the list of properties being changed
183 * while it iterates over them.
185 typedef struct DummyDev DummyDev;
186 typedef struct DummyDevClass DummyDevClass;
187 typedef struct DummyBus DummyBus;
188 typedef struct DummyBusClass DummyBusClass;
189 typedef struct DummyBackend DummyBackend;
190 typedef struct DummyBackendClass DummyBackendClass;
192 #define TYPE_DUMMY_DEV "qemu-dummy-dev"
193 #define TYPE_DUMMY_BUS "qemu-dummy-bus"
194 #define TYPE_DUMMY_BACKEND "qemu-dummy-backend"
196 #define DUMMY_DEV(obj) \
197 OBJECT_CHECK(DummyDev, (obj), TYPE_DUMMY_DEV)
198 #define DUMMY_BUS(obj) \
199 OBJECT_CHECK(DummyBus, (obj), TYPE_DUMMY_BUS)
200 #define DUMMY_BACKEND(obj) \
201 OBJECT_CHECK(DummyBackend, (obj), TYPE_DUMMY_BACKEND)
203 struct DummyDev {
204 Object parent_obj;
206 DummyBus *bus;
209 struct DummyDevClass {
210 ObjectClass parent_class;
213 struct DummyBus {
214 Object parent_obj;
216 DummyBackend *backend;
219 struct DummyBusClass {
220 ObjectClass parent_class;
223 struct DummyBackend {
224 Object parent_obj;
227 struct DummyBackendClass {
228 ObjectClass parent_class;
232 static void dummy_dev_init(Object *obj)
234 DummyDev *dev = DUMMY_DEV(obj);
235 DummyBus *bus = DUMMY_BUS(object_new(TYPE_DUMMY_BUS));
236 DummyBackend *backend = DUMMY_BACKEND(object_new(TYPE_DUMMY_BACKEND));
238 object_property_add_child(obj, "bus", OBJECT(bus), NULL);
239 dev->bus = bus;
240 object_property_add_child(OBJECT(bus), "backend", OBJECT(backend), NULL);
241 bus->backend = backend;
243 object_property_add_link(obj, "backend", TYPE_DUMMY_BACKEND,
244 (Object **)&bus->backend, NULL, 0, NULL);
247 static void dummy_dev_unparent(Object *obj)
249 DummyDev *dev = DUMMY_DEV(obj);
250 object_unparent(OBJECT(dev->bus));
253 static void dummy_dev_class_init(ObjectClass *klass, void *opaque)
255 klass->unparent = dummy_dev_unparent;
259 static void dummy_bus_init(Object *obj)
263 static void dummy_bus_unparent(Object *obj)
265 DummyBus *bus = DUMMY_BUS(obj);
266 object_property_del(obj->parent, "backend", NULL);
267 object_unparent(OBJECT(bus->backend));
270 static void dummy_bus_class_init(ObjectClass *klass, void *opaque)
272 klass->unparent = dummy_bus_unparent;
275 static void dummy_backend_init(Object *obj)
280 static const TypeInfo dummy_dev_info = {
281 .name = TYPE_DUMMY_DEV,
282 .parent = TYPE_OBJECT,
283 .instance_size = sizeof(DummyDev),
284 .instance_init = dummy_dev_init,
285 .class_size = sizeof(DummyDevClass),
286 .class_init = dummy_dev_class_init,
289 static const TypeInfo dummy_bus_info = {
290 .name = TYPE_DUMMY_BUS,
291 .parent = TYPE_OBJECT,
292 .instance_size = sizeof(DummyBus),
293 .instance_init = dummy_bus_init,
294 .class_size = sizeof(DummyBusClass),
295 .class_init = dummy_bus_class_init,
298 static const TypeInfo dummy_backend_info = {
299 .name = TYPE_DUMMY_BACKEND,
300 .parent = TYPE_OBJECT,
301 .instance_size = sizeof(DummyBackend),
302 .instance_init = dummy_backend_init,
303 .class_size = sizeof(DummyBackendClass),
308 static void test_dummy_createv(void)
310 Error *err = NULL;
311 Object *parent = object_get_objects_root();
312 DummyObject *dobj = DUMMY_OBJECT(
313 object_new_with_props(TYPE_DUMMY,
314 parent,
315 "dummy0",
316 &err,
317 "bv", "yes",
318 "sv", "Hiss hiss hiss",
319 "av", "platypus",
320 NULL));
322 g_assert(err == NULL);
323 g_assert_cmpstr(dobj->sv, ==, "Hiss hiss hiss");
324 g_assert(dobj->bv == true);
325 g_assert(dobj->av == DUMMY_PLATYPUS);
327 g_assert(object_resolve_path_component(parent, "dummy0")
328 == OBJECT(dobj));
330 object_unparent(OBJECT(dobj));
334 static Object *new_helper(Error **errp,
335 Object *parent,
336 ...)
338 va_list vargs;
339 Object *obj;
341 va_start(vargs, parent);
342 obj = object_new_with_propv(TYPE_DUMMY,
343 parent,
344 "dummy0",
345 errp,
346 vargs);
347 va_end(vargs);
348 return obj;
351 static void test_dummy_createlist(void)
353 Error *err = NULL;
354 Object *parent = object_get_objects_root();
355 DummyObject *dobj = DUMMY_OBJECT(
356 new_helper(&err,
357 parent,
358 "bv", "yes",
359 "sv", "Hiss hiss hiss",
360 "av", "platypus",
361 NULL));
363 g_assert(err == NULL);
364 g_assert_cmpstr(dobj->sv, ==, "Hiss hiss hiss");
365 g_assert(dobj->bv == true);
366 g_assert(dobj->av == DUMMY_PLATYPUS);
368 g_assert(object_resolve_path_component(parent, "dummy0")
369 == OBJECT(dobj));
371 object_unparent(OBJECT(dobj));
374 static void test_dummy_badenum(void)
376 Error *err = NULL;
377 Object *parent = object_get_objects_root();
378 Object *dobj =
379 object_new_with_props(TYPE_DUMMY,
380 parent,
381 "dummy0",
382 &err,
383 "bv", "yes",
384 "sv", "Hiss hiss hiss",
385 "av", "yeti",
386 NULL);
388 g_assert(dobj == NULL);
389 g_assert(err != NULL);
390 g_assert_cmpstr(error_get_pretty(err), ==,
391 "Invalid parameter 'yeti'");
393 g_assert(object_resolve_path_component(parent, "dummy0")
394 == NULL);
396 error_free(err);
400 static void test_dummy_getenum(void)
402 Error *err = NULL;
403 int val;
404 Object *parent = object_get_objects_root();
405 DummyObject *dobj = DUMMY_OBJECT(
406 object_new_with_props(TYPE_DUMMY,
407 parent,
408 "dummy0",
409 &err,
410 "av", "platypus",
411 NULL));
413 g_assert(err == NULL);
414 g_assert(dobj->av == DUMMY_PLATYPUS);
416 val = object_property_get_enum(OBJECT(dobj),
417 "av",
418 "DummyAnimal",
419 &err);
420 g_assert(err == NULL);
421 g_assert(val == DUMMY_PLATYPUS);
423 /* A bad enum type name */
424 val = object_property_get_enum(OBJECT(dobj),
425 "av",
426 "BadAnimal",
427 &err);
428 g_assert(err != NULL);
429 error_free(err);
430 err = NULL;
432 /* A non-enum property name */
433 val = object_property_get_enum(OBJECT(dobj),
434 "iv",
435 "DummyAnimal",
436 &err);
437 g_assert(err != NULL);
438 error_free(err);
440 object_unparent(OBJECT(dobj));
444 static void test_dummy_iterator(void)
446 Object *parent = object_get_objects_root();
447 DummyObject *dobj = DUMMY_OBJECT(
448 object_new_with_props(TYPE_DUMMY,
449 parent,
450 "dummy0",
451 &error_abort,
452 "bv", "yes",
453 "sv", "Hiss hiss hiss",
454 "av", "platypus",
455 NULL));
457 ObjectProperty *prop;
458 ObjectPropertyIterator iter;
459 bool seenbv = false, seensv = false, seenav = false, seentype;
461 object_property_iter_init(&iter, OBJECT(dobj));
462 while ((prop = object_property_iter_next(&iter))) {
463 if (g_str_equal(prop->name, "bv")) {
464 seenbv = true;
465 } else if (g_str_equal(prop->name, "sv")) {
466 seensv = true;
467 } else if (g_str_equal(prop->name, "av")) {
468 seenav = true;
469 } else if (g_str_equal(prop->name, "type")) {
470 /* This prop comes from the base Object class */
471 seentype = true;
472 } else {
473 g_printerr("Found prop '%s'\n", prop->name);
474 g_assert_not_reached();
477 g_assert(seenbv);
478 g_assert(seenav);
479 g_assert(seensv);
480 g_assert(seentype);
482 object_unparent(OBJECT(dobj));
486 static void test_dummy_delchild(void)
488 Object *parent = object_get_objects_root();
489 DummyDev *dev = DUMMY_DEV(
490 object_new_with_props(TYPE_DUMMY_DEV,
491 parent,
492 "dev0",
493 &error_abort,
494 NULL));
496 object_unparent(OBJECT(dev));
499 int main(int argc, char **argv)
501 g_test_init(&argc, &argv, NULL);
503 module_call_init(MODULE_INIT_QOM);
504 type_register_static(&dummy_info);
505 type_register_static(&dummy_dev_info);
506 type_register_static(&dummy_bus_info);
507 type_register_static(&dummy_backend_info);
509 g_test_add_func("/qom/proplist/createlist", test_dummy_createlist);
510 g_test_add_func("/qom/proplist/createv", test_dummy_createv);
511 g_test_add_func("/qom/proplist/badenum", test_dummy_badenum);
512 g_test_add_func("/qom/proplist/getenum", test_dummy_getenum);
513 g_test_add_func("/qom/proplist/iterator", test_dummy_iterator);
514 g_test_add_func("/qom/proplist/delchild", test_dummy_delchild);
516 return g_test_run();