linux-headers: update
[qemu.git] / tests / check-qom-proplist.c
bloba16cefca73ee680373f99a5783fa42d2f1b83f08
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"
28 #define TYPE_DUMMY "qemu-dummy"
30 typedef struct DummyObject DummyObject;
31 typedef struct DummyObjectClass DummyObjectClass;
33 #define DUMMY_OBJECT(obj) \
34 OBJECT_CHECK(DummyObject, (obj), TYPE_DUMMY)
36 typedef enum DummyAnimal DummyAnimal;
38 enum DummyAnimal {
39 DUMMY_FROG,
40 DUMMY_ALLIGATOR,
41 DUMMY_PLATYPUS,
43 DUMMY_LAST,
46 static const char *const dummy_animal_map[DUMMY_LAST + 1] = {
47 [DUMMY_FROG] = "frog",
48 [DUMMY_ALLIGATOR] = "alligator",
49 [DUMMY_PLATYPUS] = "platypus",
50 [DUMMY_LAST] = NULL,
53 struct DummyObject {
54 Object parent_obj;
56 bool bv;
57 DummyAnimal av;
58 char *sv;
61 struct DummyObjectClass {
62 ObjectClass parent_class;
66 static void dummy_set_bv(Object *obj,
67 bool value,
68 Error **errp)
70 DummyObject *dobj = DUMMY_OBJECT(obj);
72 dobj->bv = value;
75 static bool dummy_get_bv(Object *obj,
76 Error **errp)
78 DummyObject *dobj = DUMMY_OBJECT(obj);
80 return dobj->bv;
84 static void dummy_set_av(Object *obj,
85 int value,
86 Error **errp)
88 DummyObject *dobj = DUMMY_OBJECT(obj);
90 dobj->av = value;
93 static int dummy_get_av(Object *obj,
94 Error **errp)
96 DummyObject *dobj = DUMMY_OBJECT(obj);
98 return dobj->av;
102 static void dummy_set_sv(Object *obj,
103 const char *value,
104 Error **errp)
106 DummyObject *dobj = DUMMY_OBJECT(obj);
108 g_free(dobj->sv);
109 dobj->sv = g_strdup(value);
112 static char *dummy_get_sv(Object *obj,
113 Error **errp)
115 DummyObject *dobj = DUMMY_OBJECT(obj);
117 return g_strdup(dobj->sv);
121 static void dummy_init(Object *obj)
123 object_property_add_bool(obj, "bv",
124 dummy_get_bv,
125 dummy_set_bv,
126 NULL);
130 static void dummy_class_init(ObjectClass *cls, void *data)
132 object_class_property_add_bool(cls, "bv",
133 dummy_get_bv,
134 dummy_set_bv,
135 NULL);
136 object_class_property_add_str(cls, "sv",
137 dummy_get_sv,
138 dummy_set_sv,
139 NULL);
140 object_class_property_add_enum(cls, "av",
141 "DummyAnimal",
142 dummy_animal_map,
143 dummy_get_av,
144 dummy_set_av,
145 NULL);
149 static void dummy_finalize(Object *obj)
151 DummyObject *dobj = DUMMY_OBJECT(obj);
153 g_free(dobj->sv);
157 static const TypeInfo dummy_info = {
158 .name = TYPE_DUMMY,
159 .parent = TYPE_OBJECT,
160 .instance_size = sizeof(DummyObject),
161 .instance_init = dummy_init,
162 .instance_finalize = dummy_finalize,
163 .class_size = sizeof(DummyObjectClass),
164 .class_init = dummy_class_init,
169 * The following 3 object classes are used to
170 * simulate the kind of relationships seen in
171 * qdev, which result in complex object
172 * property destruction ordering.
174 * DummyDev has a 'bus' child to a DummyBus
175 * DummyBus has a 'backend' child to a DummyBackend
176 * DummyDev has a 'backend' link to DummyBackend
178 * When DummyDev is finalized, it unparents the
179 * DummyBackend, which unparents the DummyDev
180 * which deletes the 'backend' link from DummyDev
181 * to DummyBackend. This illustrates that the
182 * object_property_del_all() method needs to
183 * cope with the list of properties being changed
184 * while it iterates over them.
186 typedef struct DummyDev DummyDev;
187 typedef struct DummyDevClass DummyDevClass;
188 typedef struct DummyBus DummyBus;
189 typedef struct DummyBusClass DummyBusClass;
190 typedef struct DummyBackend DummyBackend;
191 typedef struct DummyBackendClass DummyBackendClass;
193 #define TYPE_DUMMY_DEV "qemu-dummy-dev"
194 #define TYPE_DUMMY_BUS "qemu-dummy-bus"
195 #define TYPE_DUMMY_BACKEND "qemu-dummy-backend"
197 #define DUMMY_DEV(obj) \
198 OBJECT_CHECK(DummyDev, (obj), TYPE_DUMMY_DEV)
199 #define DUMMY_BUS(obj) \
200 OBJECT_CHECK(DummyBus, (obj), TYPE_DUMMY_BUS)
201 #define DUMMY_BACKEND(obj) \
202 OBJECT_CHECK(DummyBackend, (obj), TYPE_DUMMY_BACKEND)
204 struct DummyDev {
205 Object parent_obj;
207 DummyBus *bus;
210 struct DummyDevClass {
211 ObjectClass parent_class;
214 struct DummyBus {
215 Object parent_obj;
217 DummyBackend *backend;
220 struct DummyBusClass {
221 ObjectClass parent_class;
224 struct DummyBackend {
225 Object parent_obj;
228 struct DummyBackendClass {
229 ObjectClass parent_class;
233 static void dummy_dev_finalize(Object *obj)
235 DummyDev *dev = DUMMY_DEV(obj);
237 object_unref(OBJECT(dev->bus));
240 static void dummy_dev_init(Object *obj)
242 DummyDev *dev = DUMMY_DEV(obj);
243 DummyBus *bus = DUMMY_BUS(object_new(TYPE_DUMMY_BUS));
244 DummyBackend *backend = DUMMY_BACKEND(object_new(TYPE_DUMMY_BACKEND));
246 object_property_add_child(obj, "bus", OBJECT(bus), NULL);
247 dev->bus = bus;
248 object_property_add_child(OBJECT(bus), "backend", OBJECT(backend), NULL);
249 bus->backend = backend;
251 object_property_add_link(obj, "backend", TYPE_DUMMY_BACKEND,
252 (Object **)&bus->backend, NULL, 0, NULL);
255 static void dummy_dev_unparent(Object *obj)
257 DummyDev *dev = DUMMY_DEV(obj);
258 object_unparent(OBJECT(dev->bus));
261 static void dummy_dev_class_init(ObjectClass *klass, void *opaque)
263 klass->unparent = dummy_dev_unparent;
267 static void dummy_bus_finalize(Object *obj)
269 DummyBus *bus = DUMMY_BUS(obj);
271 object_unref(OBJECT(bus->backend));
274 static void dummy_bus_init(Object *obj)
278 static void dummy_bus_unparent(Object *obj)
280 DummyBus *bus = DUMMY_BUS(obj);
281 object_property_del(obj->parent, "backend", NULL);
282 object_unparent(OBJECT(bus->backend));
285 static void dummy_bus_class_init(ObjectClass *klass, void *opaque)
287 klass->unparent = dummy_bus_unparent;
290 static void dummy_backend_init(Object *obj)
295 static const TypeInfo dummy_dev_info = {
296 .name = TYPE_DUMMY_DEV,
297 .parent = TYPE_OBJECT,
298 .instance_size = sizeof(DummyDev),
299 .instance_init = dummy_dev_init,
300 .instance_finalize = dummy_dev_finalize,
301 .class_size = sizeof(DummyDevClass),
302 .class_init = dummy_dev_class_init,
305 static const TypeInfo dummy_bus_info = {
306 .name = TYPE_DUMMY_BUS,
307 .parent = TYPE_OBJECT,
308 .instance_size = sizeof(DummyBus),
309 .instance_init = dummy_bus_init,
310 .instance_finalize = dummy_bus_finalize,
311 .class_size = sizeof(DummyBusClass),
312 .class_init = dummy_bus_class_init,
315 static const TypeInfo dummy_backend_info = {
316 .name = TYPE_DUMMY_BACKEND,
317 .parent = TYPE_OBJECT,
318 .instance_size = sizeof(DummyBackend),
319 .instance_init = dummy_backend_init,
320 .class_size = sizeof(DummyBackendClass),
325 static void test_dummy_createv(void)
327 Error *err = NULL;
328 Object *parent = object_get_objects_root();
329 DummyObject *dobj = DUMMY_OBJECT(
330 object_new_with_props(TYPE_DUMMY,
331 parent,
332 "dummy0",
333 &err,
334 "bv", "yes",
335 "sv", "Hiss hiss hiss",
336 "av", "platypus",
337 NULL));
339 g_assert(err == NULL);
340 g_assert_cmpstr(dobj->sv, ==, "Hiss hiss hiss");
341 g_assert(dobj->bv == true);
342 g_assert(dobj->av == DUMMY_PLATYPUS);
344 g_assert(object_resolve_path_component(parent, "dummy0")
345 == OBJECT(dobj));
347 object_unparent(OBJECT(dobj));
351 static Object *new_helper(Error **errp,
352 Object *parent,
353 ...)
355 va_list vargs;
356 Object *obj;
358 va_start(vargs, parent);
359 obj = object_new_with_propv(TYPE_DUMMY,
360 parent,
361 "dummy0",
362 errp,
363 vargs);
364 va_end(vargs);
365 return obj;
368 static void test_dummy_createlist(void)
370 Error *err = NULL;
371 Object *parent = object_get_objects_root();
372 DummyObject *dobj = DUMMY_OBJECT(
373 new_helper(&err,
374 parent,
375 "bv", "yes",
376 "sv", "Hiss hiss hiss",
377 "av", "platypus",
378 NULL));
380 g_assert(err == NULL);
381 g_assert_cmpstr(dobj->sv, ==, "Hiss hiss hiss");
382 g_assert(dobj->bv == true);
383 g_assert(dobj->av == DUMMY_PLATYPUS);
385 g_assert(object_resolve_path_component(parent, "dummy0")
386 == OBJECT(dobj));
388 object_unparent(OBJECT(dobj));
391 static void test_dummy_badenum(void)
393 Error *err = NULL;
394 Object *parent = object_get_objects_root();
395 Object *dobj =
396 object_new_with_props(TYPE_DUMMY,
397 parent,
398 "dummy0",
399 &err,
400 "bv", "yes",
401 "sv", "Hiss hiss hiss",
402 "av", "yeti",
403 NULL);
405 g_assert(dobj == NULL);
406 g_assert(err != NULL);
407 g_assert_cmpstr(error_get_pretty(err), ==,
408 "Invalid parameter 'yeti'");
410 g_assert(object_resolve_path_component(parent, "dummy0")
411 == NULL);
413 error_free(err);
417 static void test_dummy_getenum(void)
419 Error *err = NULL;
420 int val;
421 Object *parent = object_get_objects_root();
422 DummyObject *dobj = DUMMY_OBJECT(
423 object_new_with_props(TYPE_DUMMY,
424 parent,
425 "dummy0",
426 &err,
427 "av", "platypus",
428 NULL));
430 g_assert(err == NULL);
431 g_assert(dobj->av == DUMMY_PLATYPUS);
433 val = object_property_get_enum(OBJECT(dobj),
434 "av",
435 "DummyAnimal",
436 &err);
437 g_assert(err == NULL);
438 g_assert(val == DUMMY_PLATYPUS);
440 /* A bad enum type name */
441 val = object_property_get_enum(OBJECT(dobj),
442 "av",
443 "BadAnimal",
444 &err);
445 g_assert(err != NULL);
446 error_free(err);
447 err = NULL;
449 /* A non-enum property name */
450 val = object_property_get_enum(OBJECT(dobj),
451 "iv",
452 "DummyAnimal",
453 &err);
454 g_assert(err != NULL);
455 error_free(err);
457 object_unparent(OBJECT(dobj));
461 static void test_dummy_iterator(void)
463 Object *parent = object_get_objects_root();
464 DummyObject *dobj = DUMMY_OBJECT(
465 object_new_with_props(TYPE_DUMMY,
466 parent,
467 "dummy0",
468 &error_abort,
469 "bv", "yes",
470 "sv", "Hiss hiss hiss",
471 "av", "platypus",
472 NULL));
474 ObjectProperty *prop;
475 ObjectPropertyIterator iter;
476 bool seenbv = false, seensv = false, seenav = false, seentype;
478 object_property_iter_init(&iter, OBJECT(dobj));
479 while ((prop = object_property_iter_next(&iter))) {
480 if (g_str_equal(prop->name, "bv")) {
481 seenbv = true;
482 } else if (g_str_equal(prop->name, "sv")) {
483 seensv = true;
484 } else if (g_str_equal(prop->name, "av")) {
485 seenav = true;
486 } else if (g_str_equal(prop->name, "type")) {
487 /* This prop comes from the base Object class */
488 seentype = true;
489 } else {
490 g_printerr("Found prop '%s'\n", prop->name);
491 g_assert_not_reached();
494 g_assert(seenbv);
495 g_assert(seenav);
496 g_assert(seensv);
497 g_assert(seentype);
499 object_unparent(OBJECT(dobj));
503 static void test_dummy_delchild(void)
505 Object *parent = object_get_objects_root();
506 DummyDev *dev = DUMMY_DEV(
507 object_new_with_props(TYPE_DUMMY_DEV,
508 parent,
509 "dev0",
510 &error_abort,
511 NULL));
513 object_unparent(OBJECT(dev));
516 int main(int argc, char **argv)
518 g_test_init(&argc, &argv, NULL);
520 module_call_init(MODULE_INIT_QOM);
521 type_register_static(&dummy_info);
522 type_register_static(&dummy_dev_info);
523 type_register_static(&dummy_bus_info);
524 type_register_static(&dummy_backend_info);
526 g_test_add_func("/qom/proplist/createlist", test_dummy_createlist);
527 g_test_add_func("/qom/proplist/createv", test_dummy_createv);
528 g_test_add_func("/qom/proplist/badenum", test_dummy_badenum);
529 g_test_add_func("/qom/proplist/getenum", test_dummy_getenum);
530 g_test_add_func("/qom/proplist/iterator", test_dummy_iterator);
531 g_test_add_func("/qom/proplist/delchild", test_dummy_delchild);
533 return g_test_run();