cirrus: fix off-by-one in cirrus_bitblt_rop_bkwd_transp_*_16
[qemu.git] / tests / check-qom-proplist.c
1 /*
2 * Copyright (C) 2015 Red Hat, Inc.
3 *
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.
8 *
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.
13 *
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/>.
17 *
18 * Author: Daniel P. Berrange <berrange@redhat.com>
19 */
20
21 #include "qemu/osdep.h"
22
23 #include "qapi/error.h"
24 #include "qom/object.h"
25 #include "qemu/module.h"
26
27
28 #define TYPE_DUMMY "qemu-dummy"
29
30 typedef struct DummyObject DummyObject;
31 typedef struct DummyObjectClass DummyObjectClass;
32
33 #define DUMMY_OBJECT(obj) \
34 OBJECT_CHECK(DummyObject, (obj), TYPE_DUMMY)
35
36 typedef enum DummyAnimal DummyAnimal;
37
38 enum DummyAnimal {
39 DUMMY_FROG,
40 DUMMY_ALLIGATOR,
41 DUMMY_PLATYPUS,
42
43 DUMMY_LAST,
44 };
45
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,
51 };
52
53 struct DummyObject {
54 Object parent_obj;
55
56 bool bv;
57 DummyAnimal av;
58 char *sv;
59 };
60
61 struct DummyObjectClass {
62 ObjectClass parent_class;
63 };
64
65
66 static void dummy_set_bv(Object *obj,
67 bool value,
68 Error **errp)
69 {
70 DummyObject *dobj = DUMMY_OBJECT(obj);
71
72 dobj->bv = value;
73 }
74
75 static bool dummy_get_bv(Object *obj,
76 Error **errp)
77 {
78 DummyObject *dobj = DUMMY_OBJECT(obj);
79
80 return dobj->bv;
81 }
82
83
84 static void dummy_set_av(Object *obj,
85 int value,
86 Error **errp)
87 {
88 DummyObject *dobj = DUMMY_OBJECT(obj);
89
90 dobj->av = value;
91 }
92
93 static int dummy_get_av(Object *obj,
94 Error **errp)
95 {
96 DummyObject *dobj = DUMMY_OBJECT(obj);
97
98 return dobj->av;
99 }
100
101
102 static void dummy_set_sv(Object *obj,
103 const char *value,
104 Error **errp)
105 {
106 DummyObject *dobj = DUMMY_OBJECT(obj);
107
108 g_free(dobj->sv);
109 dobj->sv = g_strdup(value);
110 }
111
112 static char *dummy_get_sv(Object *obj,
113 Error **errp)
114 {
115 DummyObject *dobj = DUMMY_OBJECT(obj);
116
117 return g_strdup(dobj->sv);
118 }
119
120
121 static void dummy_init(Object *obj)
122 {
123 object_property_add_bool(obj, "bv",
124 dummy_get_bv,
125 dummy_set_bv,
126 NULL);
127 }
128
129
130 static void dummy_class_init(ObjectClass *cls, void *data)
131 {
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);
146 }
147
148
149 static void dummy_finalize(Object *obj)
150 {
151 DummyObject *dobj = DUMMY_OBJECT(obj);
152
153 g_free(dobj->sv);
154 }
155
156
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,
165 };
166
167
168 /*
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.
173 *
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
177 *
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.
185 */
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;
192
193 #define TYPE_DUMMY_DEV "qemu-dummy-dev"
194 #define TYPE_DUMMY_BUS "qemu-dummy-bus"
195 #define TYPE_DUMMY_BACKEND "qemu-dummy-backend"
196
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)
203
204 struct DummyDev {
205 Object parent_obj;
206
207 DummyBus *bus;
208 };
209
210 struct DummyDevClass {
211 ObjectClass parent_class;
212 };
213
214 struct DummyBus {
215 Object parent_obj;
216
217 DummyBackend *backend;
218 };
219
220 struct DummyBusClass {
221 ObjectClass parent_class;
222 };
223
224 struct DummyBackend {
225 Object parent_obj;
226 };
227
228 struct DummyBackendClass {
229 ObjectClass parent_class;
230 };
231
232
233 static void dummy_dev_finalize(Object *obj)
234 {
235 DummyDev *dev = DUMMY_DEV(obj);
236
237 object_unref(OBJECT(dev->bus));
238 }
239
240 static void dummy_dev_init(Object *obj)
241 {
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));
245
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;
250
251 object_property_add_link(obj, "backend", TYPE_DUMMY_BACKEND,
252 (Object **)&bus->backend, NULL, 0, NULL);
253 }
254
255 static void dummy_dev_unparent(Object *obj)
256 {
257 DummyDev *dev = DUMMY_DEV(obj);
258 object_unparent(OBJECT(dev->bus));
259 }
260
261 static void dummy_dev_class_init(ObjectClass *klass, void *opaque)
262 {
263 klass->unparent = dummy_dev_unparent;
264 }
265
266
267 static void dummy_bus_finalize(Object *obj)
268 {
269 DummyBus *bus = DUMMY_BUS(obj);
270
271 object_unref(OBJECT(bus->backend));
272 }
273
274 static void dummy_bus_init(Object *obj)
275 {
276 }
277
278 static void dummy_bus_unparent(Object *obj)
279 {
280 DummyBus *bus = DUMMY_BUS(obj);
281 object_property_del(obj->parent, "backend", NULL);
282 object_unparent(OBJECT(bus->backend));
283 }
284
285 static void dummy_bus_class_init(ObjectClass *klass, void *opaque)
286 {
287 klass->unparent = dummy_bus_unparent;
288 }
289
290 static void dummy_backend_init(Object *obj)
291 {
292 }
293
294
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,
303 };
304
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,
313 };
314
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),
321 };
322
323
324
325 static void test_dummy_createv(void)
326 {
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));
338
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);
343
344 g_assert(object_resolve_path_component(parent, "dummy0")
345 == OBJECT(dobj));
346
347 object_unparent(OBJECT(dobj));
348 }
349
350
351 static Object *new_helper(Error **errp,
352 Object *parent,
353 ...)
354 {
355 va_list vargs;
356 Object *obj;
357
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;
366 }
367
368 static void test_dummy_createlist(void)
369 {
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));
379
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);
384
385 g_assert(object_resolve_path_component(parent, "dummy0")
386 == OBJECT(dobj));
387
388 object_unparent(OBJECT(dobj));
389 }
390
391 static void test_dummy_badenum(void)
392 {
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);
404
405 g_assert(dobj == NULL);
406 g_assert(err != NULL);
407 g_assert_cmpstr(error_get_pretty(err), ==,
408 "Invalid parameter 'yeti'");
409
410 g_assert(object_resolve_path_component(parent, "dummy0")
411 == NULL);
412
413 error_free(err);
414 }
415
416
417 static void test_dummy_getenum(void)
418 {
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));
429
430 g_assert(err == NULL);
431 g_assert(dobj->av == DUMMY_PLATYPUS);
432
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);
439
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;
448
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);
456
457 object_unparent(OBJECT(dobj));
458 }
459
460
461 static void test_dummy_iterator(void)
462 {
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));
473
474 ObjectProperty *prop;
475 ObjectPropertyIterator iter;
476 bool seenbv = false, seensv = false, seenav = false, seentype;
477
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();
492 }
493 }
494 g_assert(seenbv);
495 g_assert(seenav);
496 g_assert(seensv);
497 g_assert(seentype);
498
499 object_unparent(OBJECT(dobj));
500 }
501
502
503 static void test_dummy_delchild(void)
504 {
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));
512
513 object_unparent(OBJECT(dev));
514 }
515
516 int main(int argc, char **argv)
517 {
518 g_test_init(&argc, &argv, NULL);
519
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);
525
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);
532
533 return g_test_run();
534 }