Merge remote-tracking branch 'remotes/armbru/tags/pull-monitor-2018-12-12' into staging
[qemu/ar7.git] / tests / test-qdev-global-props.c
blobb1eb5054426a2564f1a42b168079cc7bb754448c
1 /*
2 * Test code for qdev global-properties handling
4 * Copyright (c) 2012 Red Hat Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
27 #include "hw/qdev.h"
28 #include "qom/object.h"
29 #include "qapi/visitor.h"
32 #define TYPE_STATIC_PROPS "static_prop_type"
33 #define STATIC_TYPE(obj) \
34 OBJECT_CHECK(MyType, (obj), TYPE_STATIC_PROPS)
36 #define TYPE_SUBCLASS "static_prop_subtype"
38 #define PROP_DEFAULT 100
40 typedef struct MyType {
41 DeviceState parent_obj;
43 uint32_t prop1;
44 uint32_t prop2;
45 } MyType;
47 static Property static_props[] = {
48 DEFINE_PROP_UINT32("prop1", MyType, prop1, PROP_DEFAULT),
49 DEFINE_PROP_UINT32("prop2", MyType, prop2, PROP_DEFAULT),
50 DEFINE_PROP_END_OF_LIST()
53 static void static_prop_class_init(ObjectClass *oc, void *data)
55 DeviceClass *dc = DEVICE_CLASS(oc);
57 dc->realize = NULL;
58 dc->props = static_props;
61 static const TypeInfo static_prop_type = {
62 .name = TYPE_STATIC_PROPS,
63 .parent = TYPE_DEVICE,
64 .instance_size = sizeof(MyType),
65 .class_init = static_prop_class_init,
68 static const TypeInfo subclass_type = {
69 .name = TYPE_SUBCLASS,
70 .parent = TYPE_STATIC_PROPS,
73 /* Test simple static property setting to default value */
74 static void test_static_prop_subprocess(void)
76 MyType *mt;
78 mt = STATIC_TYPE(object_new(TYPE_STATIC_PROPS));
79 qdev_init_nofail(DEVICE(mt));
81 g_assert_cmpuint(mt->prop1, ==, PROP_DEFAULT);
84 static void test_static_prop(void)
86 g_test_trap_subprocess("/qdev/properties/static/default/subprocess", 0, 0);
87 g_test_trap_assert_passed();
88 g_test_trap_assert_stderr("");
89 g_test_trap_assert_stdout("");
92 static void register_global_properties(GlobalProperty *props)
94 int i;
96 for (i = 0; props[i].driver != NULL; i++) {
97 qdev_prop_register_global(props + i);
102 /* Test setting of static property using global properties */
103 static void test_static_globalprop_subprocess(void)
105 MyType *mt;
106 static GlobalProperty props[] = {
107 { TYPE_STATIC_PROPS, "prop1", "200" },
111 register_global_properties(props);
113 mt = STATIC_TYPE(object_new(TYPE_STATIC_PROPS));
114 qdev_init_nofail(DEVICE(mt));
116 g_assert_cmpuint(mt->prop1, ==, 200);
117 g_assert_cmpuint(mt->prop2, ==, PROP_DEFAULT);
120 static void test_static_globalprop(void)
122 g_test_trap_subprocess("/qdev/properties/static/global/subprocess", 0, 0);
123 g_test_trap_assert_passed();
124 g_test_trap_assert_stderr("");
125 g_test_trap_assert_stdout("");
128 #define TYPE_DYNAMIC_PROPS "dynamic-prop-type"
129 #define DYNAMIC_TYPE(obj) \
130 OBJECT_CHECK(MyType, (obj), TYPE_DYNAMIC_PROPS)
132 #define TYPE_UNUSED_HOTPLUG "hotplug-type"
133 #define TYPE_UNUSED_NOHOTPLUG "nohotplug-type"
135 static void prop1_accessor(Object *obj, Visitor *v, const char *name,
136 void *opaque, Error **errp)
138 MyType *mt = DYNAMIC_TYPE(obj);
140 visit_type_uint32(v, name, &mt->prop1, errp);
143 static void prop2_accessor(Object *obj, Visitor *v, const char *name,
144 void *opaque, Error **errp)
146 MyType *mt = DYNAMIC_TYPE(obj);
148 visit_type_uint32(v, name, &mt->prop2, errp);
151 static void dynamic_instance_init(Object *obj)
153 object_property_add(obj, "prop1", "uint32", prop1_accessor, prop1_accessor,
154 NULL, NULL, NULL);
155 object_property_add(obj, "prop2", "uint32", prop2_accessor, prop2_accessor,
156 NULL, NULL, NULL);
159 static void dynamic_class_init(ObjectClass *oc, void *data)
161 DeviceClass *dc = DEVICE_CLASS(oc);
163 dc->realize = NULL;
167 static const TypeInfo dynamic_prop_type = {
168 .name = TYPE_DYNAMIC_PROPS,
169 .parent = TYPE_DEVICE,
170 .instance_size = sizeof(MyType),
171 .instance_init = dynamic_instance_init,
172 .class_init = dynamic_class_init,
175 static void hotplug_class_init(ObjectClass *oc, void *data)
177 DeviceClass *dc = DEVICE_CLASS(oc);
179 dc->realize = NULL;
180 dc->hotpluggable = true;
183 static const TypeInfo hotplug_type = {
184 .name = TYPE_UNUSED_HOTPLUG,
185 .parent = TYPE_DEVICE,
186 .instance_size = sizeof(MyType),
187 .instance_init = dynamic_instance_init,
188 .class_init = hotplug_class_init,
191 static void nohotplug_class_init(ObjectClass *oc, void *data)
193 DeviceClass *dc = DEVICE_CLASS(oc);
195 dc->realize = NULL;
196 dc->hotpluggable = false;
199 static const TypeInfo nohotplug_type = {
200 .name = TYPE_UNUSED_NOHOTPLUG,
201 .parent = TYPE_DEVICE,
202 .instance_size = sizeof(MyType),
203 .instance_init = dynamic_instance_init,
204 .class_init = nohotplug_class_init,
207 #define TYPE_NONDEVICE "nondevice-type"
209 static const TypeInfo nondevice_type = {
210 .name = TYPE_NONDEVICE,
211 .parent = TYPE_OBJECT,
214 /* Test setting of dynamic properties using global properties */
215 static void test_dynamic_globalprop_subprocess(void)
217 MyType *mt;
218 static GlobalProperty props[] = {
219 { TYPE_DYNAMIC_PROPS, "prop1", "101", true },
220 { TYPE_DYNAMIC_PROPS, "prop2", "102", true },
221 { TYPE_DYNAMIC_PROPS"-bad", "prop3", "103", true },
222 { TYPE_UNUSED_HOTPLUG, "prop4", "104", true },
223 { TYPE_UNUSED_NOHOTPLUG, "prop5", "105", true },
224 { TYPE_NONDEVICE, "prop6", "106", true },
227 int global_error;
229 register_global_properties(props);
231 mt = DYNAMIC_TYPE(object_new(TYPE_DYNAMIC_PROPS));
232 qdev_init_nofail(DEVICE(mt));
234 g_assert_cmpuint(mt->prop1, ==, 101);
235 g_assert_cmpuint(mt->prop2, ==, 102);
236 global_error = qdev_prop_check_globals();
237 g_assert_cmpuint(global_error, ==, 1);
238 g_assert(props[0].used);
239 g_assert(props[1].used);
240 g_assert(!props[2].used);
241 g_assert(!props[3].used);
242 g_assert(!props[4].used);
243 g_assert(!props[5].used);
246 static void test_dynamic_globalprop(void)
248 g_test_trap_subprocess("/qdev/properties/dynamic/global/subprocess", 0, 0);
249 g_test_trap_assert_passed();
250 g_test_trap_assert_stderr_unmatched("*prop1*");
251 g_test_trap_assert_stderr_unmatched("*prop2*");
252 g_test_trap_assert_stderr("*warning: global dynamic-prop-type-bad.prop3 has invalid class name\n*");
253 g_test_trap_assert_stderr_unmatched("*prop4*");
254 g_test_trap_assert_stderr("*warning: global nohotplug-type.prop5=105 not used\n*");
255 g_test_trap_assert_stderr("*warning: global nondevice-type.prop6 has invalid class name\n*");
256 g_test_trap_assert_stdout("");
259 /* Test setting of dynamic properties using user_provided=false properties */
260 static void test_dynamic_globalprop_nouser_subprocess(void)
262 MyType *mt;
263 static GlobalProperty props[] = {
264 { TYPE_DYNAMIC_PROPS, "prop1", "101" },
265 { TYPE_DYNAMIC_PROPS, "prop2", "102" },
266 { TYPE_DYNAMIC_PROPS"-bad", "prop3", "103" },
267 { TYPE_UNUSED_HOTPLUG, "prop4", "104" },
268 { TYPE_UNUSED_NOHOTPLUG, "prop5", "105" },
269 { TYPE_NONDEVICE, "prop6", "106" },
272 int global_error;
274 register_global_properties(props);
276 mt = DYNAMIC_TYPE(object_new(TYPE_DYNAMIC_PROPS));
277 qdev_init_nofail(DEVICE(mt));
279 g_assert_cmpuint(mt->prop1, ==, 101);
280 g_assert_cmpuint(mt->prop2, ==, 102);
281 global_error = qdev_prop_check_globals();
282 g_assert_cmpuint(global_error, ==, 0);
283 g_assert(props[0].used);
284 g_assert(props[1].used);
285 g_assert(!props[2].used);
286 g_assert(!props[3].used);
287 g_assert(!props[4].used);
288 g_assert(!props[5].used);
291 static void test_dynamic_globalprop_nouser(void)
293 g_test_trap_subprocess("/qdev/properties/dynamic/global/nouser/subprocess", 0, 0);
294 g_test_trap_assert_passed();
295 g_test_trap_assert_stderr("");
296 g_test_trap_assert_stdout("");
299 /* Test if global props affecting subclasses are applied in the right order */
300 static void test_subclass_global_props(void)
302 MyType *mt;
303 /* Global properties must be applied in the order they were registered */
304 static GlobalProperty props[] = {
305 { TYPE_STATIC_PROPS, "prop1", "101" },
306 { TYPE_SUBCLASS, "prop1", "102" },
307 { TYPE_SUBCLASS, "prop2", "103" },
308 { TYPE_STATIC_PROPS, "prop2", "104" },
312 register_global_properties(props);
314 mt = STATIC_TYPE(object_new(TYPE_SUBCLASS));
315 qdev_init_nofail(DEVICE(mt));
317 g_assert_cmpuint(mt->prop1, ==, 102);
318 g_assert_cmpuint(mt->prop2, ==, 104);
321 int main(int argc, char **argv)
323 g_test_init(&argc, &argv, NULL);
325 module_call_init(MODULE_INIT_QOM);
326 type_register_static(&static_prop_type);
327 type_register_static(&subclass_type);
328 type_register_static(&dynamic_prop_type);
329 type_register_static(&hotplug_type);
330 type_register_static(&nohotplug_type);
331 type_register_static(&nondevice_type);
333 g_test_add_func("/qdev/properties/static/default/subprocess",
334 test_static_prop_subprocess);
335 g_test_add_func("/qdev/properties/static/default",
336 test_static_prop);
338 g_test_add_func("/qdev/properties/static/global/subprocess",
339 test_static_globalprop_subprocess);
340 g_test_add_func("/qdev/properties/static/global",
341 test_static_globalprop);
343 g_test_add_func("/qdev/properties/dynamic/global/subprocess",
344 test_dynamic_globalprop_subprocess);
345 g_test_add_func("/qdev/properties/dynamic/global",
346 test_dynamic_globalprop);
348 g_test_add_func("/qdev/properties/dynamic/global/nouser/subprocess",
349 test_dynamic_globalprop_nouser_subprocess);
350 g_test_add_func("/qdev/properties/dynamic/global/nouser",
351 test_dynamic_globalprop_nouser);
353 g_test_add_func("/qdev/properties/global/subclass",
354 test_subclass_global_props);
356 g_test_run();
358 return 0;