block: move restarting of throttled reqs to block/throttle-groups.c
[qemu/ar7.git] / tests / check-qom-proplist.c
blobffffd872f240092335a0903deb4ee2143791531e
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"
22 #include <glib.h>
24 #include "qapi/error.h"
25 #include "qom/object.h"
26 #include "qemu/module.h"
29 #define TYPE_DUMMY "qemu-dummy"
31 typedef struct DummyObject DummyObject;
32 typedef struct DummyObjectClass DummyObjectClass;
34 #define DUMMY_OBJECT(obj) \
35 OBJECT_CHECK(DummyObject, (obj), TYPE_DUMMY)
37 typedef enum DummyAnimal DummyAnimal;
39 enum DummyAnimal {
40 DUMMY_FROG,
41 DUMMY_ALLIGATOR,
42 DUMMY_PLATYPUS,
44 DUMMY_LAST,
47 static const char *const dummy_animal_map[DUMMY_LAST + 1] = {
48 [DUMMY_FROG] = "frog",
49 [DUMMY_ALLIGATOR] = "alligator",
50 [DUMMY_PLATYPUS] = "platypus",
51 [DUMMY_LAST] = NULL,
54 struct DummyObject {
55 Object parent_obj;
57 bool bv;
58 DummyAnimal av;
59 char *sv;
62 struct DummyObjectClass {
63 ObjectClass parent_class;
67 static void dummy_set_bv(Object *obj,
68 bool value,
69 Error **errp)
71 DummyObject *dobj = DUMMY_OBJECT(obj);
73 dobj->bv = value;
76 static bool dummy_get_bv(Object *obj,
77 Error **errp)
79 DummyObject *dobj = DUMMY_OBJECT(obj);
81 return dobj->bv;
85 static void dummy_set_av(Object *obj,
86 int value,
87 Error **errp)
89 DummyObject *dobj = DUMMY_OBJECT(obj);
91 dobj->av = value;
94 static int dummy_get_av(Object *obj,
95 Error **errp)
97 DummyObject *dobj = DUMMY_OBJECT(obj);
99 return dobj->av;
103 static void dummy_set_sv(Object *obj,
104 const char *value,
105 Error **errp)
107 DummyObject *dobj = DUMMY_OBJECT(obj);
109 g_free(dobj->sv);
110 dobj->sv = g_strdup(value);
113 static char *dummy_get_sv(Object *obj,
114 Error **errp)
116 DummyObject *dobj = DUMMY_OBJECT(obj);
118 return g_strdup(dobj->sv);
122 static void dummy_init(Object *obj)
124 object_property_add_bool(obj, "bv",
125 dummy_get_bv,
126 dummy_set_bv,
127 NULL);
131 static void dummy_class_init(ObjectClass *cls, void *data)
133 object_class_property_add_bool(cls, "bv",
134 dummy_get_bv,
135 dummy_set_bv,
136 NULL);
137 object_class_property_add_str(cls, "sv",
138 dummy_get_sv,
139 dummy_set_sv,
140 NULL);
141 object_class_property_add_enum(cls, "av",
142 "DummyAnimal",
143 dummy_animal_map,
144 dummy_get_av,
145 dummy_set_av,
146 NULL);
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,
170 * The following 3 object classes are used to
171 * simulate the kind of relationships seen in
172 * qdev, which result in complex object
173 * property destruction ordering.
175 * DummyDev has a 'bus' child to a DummyBus
176 * DummyBus has a 'backend' child to a DummyBackend
177 * DummyDev has a 'backend' link to DummyBackend
179 * When DummyDev is finalized, it unparents the
180 * DummyBackend, which unparents the DummyDev
181 * which deletes the 'backend' link from DummyDev
182 * to DummyBackend. This illustrates that the
183 * object_property_del_all() method needs to
184 * cope with the list of properties being changed
185 * while it iterates over them.
187 typedef struct DummyDev DummyDev;
188 typedef struct DummyDevClass DummyDevClass;
189 typedef struct DummyBus DummyBus;
190 typedef struct DummyBusClass DummyBusClass;
191 typedef struct DummyBackend DummyBackend;
192 typedef struct DummyBackendClass DummyBackendClass;
194 #define TYPE_DUMMY_DEV "qemu-dummy-dev"
195 #define TYPE_DUMMY_BUS "qemu-dummy-bus"
196 #define TYPE_DUMMY_BACKEND "qemu-dummy-backend"
198 #define DUMMY_DEV(obj) \
199 OBJECT_CHECK(DummyDev, (obj), TYPE_DUMMY_DEV)
200 #define DUMMY_BUS(obj) \
201 OBJECT_CHECK(DummyBus, (obj), TYPE_DUMMY_BUS)
202 #define DUMMY_BACKEND(obj) \
203 OBJECT_CHECK(DummyBackend, (obj), TYPE_DUMMY_BACKEND)
205 struct DummyDev {
206 Object parent_obj;
208 DummyBus *bus;
211 struct DummyDevClass {
212 ObjectClass parent_class;
215 struct DummyBus {
216 Object parent_obj;
218 DummyBackend *backend;
221 struct DummyBusClass {
222 ObjectClass parent_class;
225 struct DummyBackend {
226 Object parent_obj;
229 struct DummyBackendClass {
230 ObjectClass parent_class;
234 static void dummy_dev_init(Object *obj)
236 DummyDev *dev = DUMMY_DEV(obj);
237 DummyBus *bus = DUMMY_BUS(object_new(TYPE_DUMMY_BUS));
238 DummyBackend *backend = DUMMY_BACKEND(object_new(TYPE_DUMMY_BACKEND));
240 object_property_add_child(obj, "bus", OBJECT(bus), NULL);
241 dev->bus = bus;
242 object_property_add_child(OBJECT(bus), "backend", OBJECT(backend), NULL);
243 bus->backend = backend;
245 object_property_add_link(obj, "backend", TYPE_DUMMY_BACKEND,
246 (Object **)&bus->backend, NULL, 0, NULL);
249 static void dummy_dev_unparent(Object *obj)
251 DummyDev *dev = DUMMY_DEV(obj);
252 object_unparent(OBJECT(dev->bus));
255 static void dummy_dev_class_init(ObjectClass *klass, void *opaque)
257 klass->unparent = dummy_dev_unparent;
261 static void dummy_bus_init(Object *obj)
265 static void dummy_bus_unparent(Object *obj)
267 DummyBus *bus = DUMMY_BUS(obj);
268 object_property_del(obj->parent, "backend", NULL);
269 object_unparent(OBJECT(bus->backend));
272 static void dummy_bus_class_init(ObjectClass *klass, void *opaque)
274 klass->unparent = dummy_bus_unparent;
277 static void dummy_backend_init(Object *obj)
282 static const TypeInfo dummy_dev_info = {
283 .name = TYPE_DUMMY_DEV,
284 .parent = TYPE_OBJECT,
285 .instance_size = sizeof(DummyDev),
286 .instance_init = dummy_dev_init,
287 .class_size = sizeof(DummyDevClass),
288 .class_init = dummy_dev_class_init,
291 static const TypeInfo dummy_bus_info = {
292 .name = TYPE_DUMMY_BUS,
293 .parent = TYPE_OBJECT,
294 .instance_size = sizeof(DummyBus),
295 .instance_init = dummy_bus_init,
296 .class_size = sizeof(DummyBusClass),
297 .class_init = dummy_bus_class_init,
300 static const TypeInfo dummy_backend_info = {
301 .name = TYPE_DUMMY_BACKEND,
302 .parent = TYPE_OBJECT,
303 .instance_size = sizeof(DummyBackend),
304 .instance_init = dummy_backend_init,
305 .class_size = sizeof(DummyBackendClass),
310 static void test_dummy_createv(void)
312 Error *err = NULL;
313 Object *parent = object_get_objects_root();
314 DummyObject *dobj = DUMMY_OBJECT(
315 object_new_with_props(TYPE_DUMMY,
316 parent,
317 "dummy0",
318 &err,
319 "bv", "yes",
320 "sv", "Hiss hiss hiss",
321 "av", "platypus",
322 NULL));
324 g_assert(err == NULL);
325 g_assert_cmpstr(dobj->sv, ==, "Hiss hiss hiss");
326 g_assert(dobj->bv == true);
327 g_assert(dobj->av == DUMMY_PLATYPUS);
329 g_assert(object_resolve_path_component(parent, "dummy0")
330 == OBJECT(dobj));
332 object_unparent(OBJECT(dobj));
336 static Object *new_helper(Error **errp,
337 Object *parent,
338 ...)
340 va_list vargs;
341 Object *obj;
343 va_start(vargs, parent);
344 obj = object_new_with_propv(TYPE_DUMMY,
345 parent,
346 "dummy0",
347 errp,
348 vargs);
349 va_end(vargs);
350 return obj;
353 static void test_dummy_createlist(void)
355 Error *err = NULL;
356 Object *parent = object_get_objects_root();
357 DummyObject *dobj = DUMMY_OBJECT(
358 new_helper(&err,
359 parent,
360 "bv", "yes",
361 "sv", "Hiss hiss hiss",
362 "av", "platypus",
363 NULL));
365 g_assert(err == NULL);
366 g_assert_cmpstr(dobj->sv, ==, "Hiss hiss hiss");
367 g_assert(dobj->bv == true);
368 g_assert(dobj->av == DUMMY_PLATYPUS);
370 g_assert(object_resolve_path_component(parent, "dummy0")
371 == OBJECT(dobj));
373 object_unparent(OBJECT(dobj));
376 static void test_dummy_badenum(void)
378 Error *err = NULL;
379 Object *parent = object_get_objects_root();
380 Object *dobj =
381 object_new_with_props(TYPE_DUMMY,
382 parent,
383 "dummy0",
384 &err,
385 "bv", "yes",
386 "sv", "Hiss hiss hiss",
387 "av", "yeti",
388 NULL);
390 g_assert(dobj == NULL);
391 g_assert(err != NULL);
392 g_assert_cmpstr(error_get_pretty(err), ==,
393 "Invalid parameter 'yeti'");
395 g_assert(object_resolve_path_component(parent, "dummy0")
396 == NULL);
398 error_free(err);
402 static void test_dummy_getenum(void)
404 Error *err = NULL;
405 int val;
406 Object *parent = object_get_objects_root();
407 DummyObject *dobj = DUMMY_OBJECT(
408 object_new_with_props(TYPE_DUMMY,
409 parent,
410 "dummy0",
411 &err,
412 "av", "platypus",
413 NULL));
415 g_assert(err == NULL);
416 g_assert(dobj->av == DUMMY_PLATYPUS);
418 val = object_property_get_enum(OBJECT(dobj),
419 "av",
420 "DummyAnimal",
421 &err);
422 g_assert(err == NULL);
423 g_assert(val == DUMMY_PLATYPUS);
425 /* A bad enum type name */
426 val = object_property_get_enum(OBJECT(dobj),
427 "av",
428 "BadAnimal",
429 &err);
430 g_assert(err != NULL);
431 error_free(err);
432 err = NULL;
434 /* A non-enum property name */
435 val = object_property_get_enum(OBJECT(dobj),
436 "iv",
437 "DummyAnimal",
438 &err);
439 g_assert(err != NULL);
440 error_free(err);
442 object_unparent(OBJECT(dobj));
446 static void test_dummy_iterator(void)
448 Object *parent = object_get_objects_root();
449 DummyObject *dobj = DUMMY_OBJECT(
450 object_new_with_props(TYPE_DUMMY,
451 parent,
452 "dummy0",
453 &error_abort,
454 "bv", "yes",
455 "sv", "Hiss hiss hiss",
456 "av", "platypus",
457 NULL));
459 ObjectProperty *prop;
460 ObjectPropertyIterator iter;
461 bool seenbv = false, seensv = false, seenav = false, seentype;
463 object_property_iter_init(&iter, OBJECT(dobj));
464 while ((prop = object_property_iter_next(&iter))) {
465 if (g_str_equal(prop->name, "bv")) {
466 seenbv = true;
467 } else if (g_str_equal(prop->name, "sv")) {
468 seensv = true;
469 } else if (g_str_equal(prop->name, "av")) {
470 seenav = true;
471 } else if (g_str_equal(prop->name, "type")) {
472 /* This prop comes from the base Object class */
473 seentype = true;
474 } else {
475 g_printerr("Found prop '%s'\n", prop->name);
476 g_assert_not_reached();
479 g_assert(seenbv);
480 g_assert(seenav);
481 g_assert(seensv);
482 g_assert(seentype);
484 object_unparent(OBJECT(dobj));
488 static void test_dummy_delchild(void)
490 Object *parent = object_get_objects_root();
491 DummyDev *dev = DUMMY_DEV(
492 object_new_with_props(TYPE_DUMMY_DEV,
493 parent,
494 "dev0",
495 &error_abort,
496 NULL));
498 object_unparent(OBJECT(dev));
501 int main(int argc, char **argv)
503 g_test_init(&argc, &argv, NULL);
505 module_call_init(MODULE_INIT_QOM);
506 type_register_static(&dummy_info);
507 type_register_static(&dummy_dev_info);
508 type_register_static(&dummy_bus_info);
509 type_register_static(&dummy_backend_info);
511 g_test_add_func("/qom/proplist/createlist", test_dummy_createlist);
512 g_test_add_func("/qom/proplist/createv", test_dummy_createv);
513 g_test_add_func("/qom/proplist/badenum", test_dummy_badenum);
514 g_test_add_func("/qom/proplist/getenum", test_dummy_getenum);
515 g_test_add_func("/qom/proplist/iterator", test_dummy_iterator);
516 g_test_add_func("/qom/proplist/delchild", test_dummy_delchild);
518 return g_test_run();