error: Use error_prepend() where it makes obvious sense
[qemu.git] / tests / check-qom-proplist.c
blobe674c0fa897f5dc3969416cff8337b6c3fa06250
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);
126 object_property_add_str(obj, "sv",
127 dummy_get_sv,
128 dummy_set_sv,
129 NULL);
130 object_property_add_enum(obj, "av",
131 "DummyAnimal",
132 dummy_animal_map,
133 dummy_get_av,
134 dummy_set_av,
135 NULL);
138 static void dummy_finalize(Object *obj)
140 DummyObject *dobj = DUMMY_OBJECT(obj);
142 g_free(dobj->sv);
146 static const TypeInfo dummy_info = {
147 .name = TYPE_DUMMY,
148 .parent = TYPE_OBJECT,
149 .instance_size = sizeof(DummyObject),
150 .instance_init = dummy_init,
151 .instance_finalize = dummy_finalize,
152 .class_size = sizeof(DummyObjectClass),
157 * The following 3 object classes are used to
158 * simulate the kind of relationships seen in
159 * qdev, which result in complex object
160 * property destruction ordering.
162 * DummyDev has a 'bus' child to a DummyBus
163 * DummyBus has a 'backend' child to a DummyBackend
164 * DummyDev has a 'backend' link to DummyBackend
166 * When DummyDev is finalized, it unparents the
167 * DummyBackend, which unparents the DummyDev
168 * which deletes the 'backend' link from DummyDev
169 * to DummyBackend. This illustrates that the
170 * object_property_del_all() method needs to
171 * cope with the list of properties being changed
172 * while it iterates over them.
174 typedef struct DummyDev DummyDev;
175 typedef struct DummyDevClass DummyDevClass;
176 typedef struct DummyBus DummyBus;
177 typedef struct DummyBusClass DummyBusClass;
178 typedef struct DummyBackend DummyBackend;
179 typedef struct DummyBackendClass DummyBackendClass;
181 #define TYPE_DUMMY_DEV "qemu-dummy-dev"
182 #define TYPE_DUMMY_BUS "qemu-dummy-bus"
183 #define TYPE_DUMMY_BACKEND "qemu-dummy-backend"
185 #define DUMMY_DEV(obj) \
186 OBJECT_CHECK(DummyDev, (obj), TYPE_DUMMY_DEV)
187 #define DUMMY_BUS(obj) \
188 OBJECT_CHECK(DummyBus, (obj), TYPE_DUMMY_BUS)
189 #define DUMMY_BACKEND(obj) \
190 OBJECT_CHECK(DummyBackend, (obj), TYPE_DUMMY_BACKEND)
192 struct DummyDev {
193 Object parent_obj;
195 DummyBus *bus;
198 struct DummyDevClass {
199 ObjectClass parent_class;
202 struct DummyBus {
203 Object parent_obj;
205 DummyBackend *backend;
208 struct DummyBusClass {
209 ObjectClass parent_class;
212 struct DummyBackend {
213 Object parent_obj;
216 struct DummyBackendClass {
217 ObjectClass parent_class;
221 static void dummy_dev_init(Object *obj)
223 DummyDev *dev = DUMMY_DEV(obj);
224 DummyBus *bus = DUMMY_BUS(object_new(TYPE_DUMMY_BUS));
225 DummyBackend *backend = DUMMY_BACKEND(object_new(TYPE_DUMMY_BACKEND));
227 object_property_add_child(obj, "bus", OBJECT(bus), NULL);
228 dev->bus = bus;
229 object_property_add_child(OBJECT(bus), "backend", OBJECT(backend), NULL);
230 bus->backend = backend;
232 object_property_add_link(obj, "backend", TYPE_DUMMY_BACKEND,
233 (Object **)&bus->backend, NULL, 0, NULL);
236 static void dummy_dev_unparent(Object *obj)
238 DummyDev *dev = DUMMY_DEV(obj);
239 object_unparent(OBJECT(dev->bus));
242 static void dummy_dev_class_init(ObjectClass *klass, void *opaque)
244 klass->unparent = dummy_dev_unparent;
248 static void dummy_bus_init(Object *obj)
252 static void dummy_bus_unparent(Object *obj)
254 DummyBus *bus = DUMMY_BUS(obj);
255 object_property_del(obj->parent, "backend", NULL);
256 object_unparent(OBJECT(bus->backend));
259 static void dummy_bus_class_init(ObjectClass *klass, void *opaque)
261 klass->unparent = dummy_bus_unparent;
264 static void dummy_backend_init(Object *obj)
269 static const TypeInfo dummy_dev_info = {
270 .name = TYPE_DUMMY_DEV,
271 .parent = TYPE_OBJECT,
272 .instance_size = sizeof(DummyDev),
273 .instance_init = dummy_dev_init,
274 .class_size = sizeof(DummyDevClass),
275 .class_init = dummy_dev_class_init,
278 static const TypeInfo dummy_bus_info = {
279 .name = TYPE_DUMMY_BUS,
280 .parent = TYPE_OBJECT,
281 .instance_size = sizeof(DummyBus),
282 .instance_init = dummy_bus_init,
283 .class_size = sizeof(DummyBusClass),
284 .class_init = dummy_bus_class_init,
287 static const TypeInfo dummy_backend_info = {
288 .name = TYPE_DUMMY_BACKEND,
289 .parent = TYPE_OBJECT,
290 .instance_size = sizeof(DummyBackend),
291 .instance_init = dummy_backend_init,
292 .class_size = sizeof(DummyBackendClass),
297 static void test_dummy_createv(void)
299 Error *err = NULL;
300 Object *parent = object_get_objects_root();
301 DummyObject *dobj = DUMMY_OBJECT(
302 object_new_with_props(TYPE_DUMMY,
303 parent,
304 "dummy0",
305 &err,
306 "bv", "yes",
307 "sv", "Hiss hiss hiss",
308 "av", "platypus",
309 NULL));
311 g_assert(err == NULL);
312 g_assert_cmpstr(dobj->sv, ==, "Hiss hiss hiss");
313 g_assert(dobj->bv == true);
314 g_assert(dobj->av == DUMMY_PLATYPUS);
316 g_assert(object_resolve_path_component(parent, "dummy0")
317 == OBJECT(dobj));
319 object_unparent(OBJECT(dobj));
323 static Object *new_helper(Error **errp,
324 Object *parent,
325 ...)
327 va_list vargs;
328 Object *obj;
330 va_start(vargs, parent);
331 obj = object_new_with_propv(TYPE_DUMMY,
332 parent,
333 "dummy0",
334 errp,
335 vargs);
336 va_end(vargs);
337 return obj;
340 static void test_dummy_createlist(void)
342 Error *err = NULL;
343 Object *parent = object_get_objects_root();
344 DummyObject *dobj = DUMMY_OBJECT(
345 new_helper(&err,
346 parent,
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));
363 static void test_dummy_badenum(void)
365 Error *err = NULL;
366 Object *parent = object_get_objects_root();
367 Object *dobj =
368 object_new_with_props(TYPE_DUMMY,
369 parent,
370 "dummy0",
371 &err,
372 "bv", "yes",
373 "sv", "Hiss hiss hiss",
374 "av", "yeti",
375 NULL);
377 g_assert(dobj == NULL);
378 g_assert(err != NULL);
379 g_assert_cmpstr(error_get_pretty(err), ==,
380 "Invalid parameter 'yeti'");
382 g_assert(object_resolve_path_component(parent, "dummy0")
383 == NULL);
385 error_free(err);
389 static void test_dummy_getenum(void)
391 Error *err = NULL;
392 int val;
393 Object *parent = object_get_objects_root();
394 DummyObject *dobj = DUMMY_OBJECT(
395 object_new_with_props(TYPE_DUMMY,
396 parent,
397 "dummy0",
398 &err,
399 "av", "platypus",
400 NULL));
402 g_assert(err == NULL);
403 g_assert(dobj->av == DUMMY_PLATYPUS);
405 val = object_property_get_enum(OBJECT(dobj),
406 "av",
407 "DummyAnimal",
408 &err);
409 g_assert(err == NULL);
410 g_assert(val == DUMMY_PLATYPUS);
412 /* A bad enum type name */
413 val = object_property_get_enum(OBJECT(dobj),
414 "av",
415 "BadAnimal",
416 &err);
417 g_assert(err != NULL);
418 error_free(err);
419 err = NULL;
421 /* A non-enum property name */
422 val = object_property_get_enum(OBJECT(dobj),
423 "iv",
424 "DummyAnimal",
425 &err);
426 g_assert(err != NULL);
427 error_free(err);
429 object_unparent(OBJECT(dobj));
433 static void test_dummy_iterator(void)
435 Object *parent = object_get_objects_root();
436 DummyObject *dobj = DUMMY_OBJECT(
437 object_new_with_props(TYPE_DUMMY,
438 parent,
439 "dummy0",
440 &error_abort,
441 "bv", "yes",
442 "sv", "Hiss hiss hiss",
443 "av", "platypus",
444 NULL));
446 ObjectProperty *prop;
447 ObjectPropertyIterator *iter;
448 bool seenbv = false, seensv = false, seenav = false, seentype;
450 iter = object_property_iter_init(OBJECT(dobj));
451 while ((prop = object_property_iter_next(iter))) {
452 if (g_str_equal(prop->name, "bv")) {
453 seenbv = true;
454 } else if (g_str_equal(prop->name, "sv")) {
455 seensv = true;
456 } else if (g_str_equal(prop->name, "av")) {
457 seenav = true;
458 } else if (g_str_equal(prop->name, "type")) {
459 /* This prop comes from the base Object class */
460 seentype = true;
461 } else {
462 g_printerr("Found prop '%s'\n", prop->name);
463 g_assert_not_reached();
466 object_property_iter_free(iter);
467 g_assert(seenbv);
468 g_assert(seenav);
469 g_assert(seensv);
470 g_assert(seentype);
472 object_unparent(OBJECT(dobj));
476 static void test_dummy_delchild(void)
478 Object *parent = object_get_objects_root();
479 DummyDev *dev = DUMMY_DEV(
480 object_new_with_props(TYPE_DUMMY_DEV,
481 parent,
482 "dev0",
483 &error_abort,
484 NULL));
486 object_unparent(OBJECT(dev));
489 int main(int argc, char **argv)
491 g_test_init(&argc, &argv, NULL);
493 module_call_init(MODULE_INIT_QOM);
494 type_register_static(&dummy_info);
495 type_register_static(&dummy_dev_info);
496 type_register_static(&dummy_bus_info);
497 type_register_static(&dummy_backend_info);
499 g_test_add_func("/qom/proplist/createlist", test_dummy_createlist);
500 g_test_add_func("/qom/proplist/createv", test_dummy_createv);
501 g_test_add_func("/qom/proplist/badenum", test_dummy_badenum);
502 g_test_add_func("/qom/proplist/getenum", test_dummy_getenum);
503 g_test_add_func("/qom/proplist/iterator", test_dummy_iterator);
504 g_test_add_func("/qom/proplist/delchild", test_dummy_delchild);
506 return g_test_run();