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
25 #include "qemu/osdep.h"
27 #include "hw/qdev-properties.h"
28 #include "qom/object.h"
29 #include "qapi/error.h"
30 #include "qapi/visitor.h"
33 #define TYPE_STATIC_PROPS "static_prop_type"
34 #define STATIC_TYPE(obj) \
35 OBJECT_CHECK(MyType, (obj), TYPE_STATIC_PROPS)
37 #define TYPE_SUBCLASS "static_prop_subtype"
39 #define PROP_DEFAULT 100
41 typedef struct MyType
{
42 DeviceState parent_obj
;
48 static Property static_props
[] = {
49 DEFINE_PROP_UINT32("prop1", MyType
, prop1
, PROP_DEFAULT
),
50 DEFINE_PROP_UINT32("prop2", MyType
, prop2
, PROP_DEFAULT
),
51 DEFINE_PROP_END_OF_LIST()
54 static void static_prop_class_init(ObjectClass
*oc
, void *data
)
56 DeviceClass
*dc
= DEVICE_CLASS(oc
);
59 device_class_set_props(dc
, static_props
);
62 static const TypeInfo static_prop_type
= {
63 .name
= TYPE_STATIC_PROPS
,
64 .parent
= TYPE_DEVICE
,
65 .instance_size
= sizeof(MyType
),
66 .class_init
= static_prop_class_init
,
69 static const TypeInfo subclass_type
= {
70 .name
= TYPE_SUBCLASS
,
71 .parent
= TYPE_STATIC_PROPS
,
74 /* Test simple static property setting to default value */
75 static void test_static_prop_subprocess(void)
79 mt
= STATIC_TYPE(object_new(TYPE_STATIC_PROPS
));
80 qdev_realize(DEVICE(mt
), NULL
, &error_fatal
);
82 g_assert_cmpuint(mt
->prop1
, ==, PROP_DEFAULT
);
85 static void test_static_prop(void)
87 g_test_trap_subprocess("/qdev/properties/static/default/subprocess", 0, 0);
88 g_test_trap_assert_passed();
89 g_test_trap_assert_stderr("");
90 g_test_trap_assert_stdout("");
93 static void register_global_properties(GlobalProperty
*props
)
97 for (i
= 0; props
[i
].driver
!= NULL
; i
++) {
98 qdev_prop_register_global(props
+ i
);
103 /* Test setting of static property using global properties */
104 static void test_static_globalprop_subprocess(void)
107 static GlobalProperty props
[] = {
108 { TYPE_STATIC_PROPS
, "prop1", "200" },
112 register_global_properties(props
);
114 mt
= STATIC_TYPE(object_new(TYPE_STATIC_PROPS
));
115 qdev_realize(DEVICE(mt
), NULL
, &error_fatal
);
117 g_assert_cmpuint(mt
->prop1
, ==, 200);
118 g_assert_cmpuint(mt
->prop2
, ==, PROP_DEFAULT
);
121 static void test_static_globalprop(void)
123 g_test_trap_subprocess("/qdev/properties/static/global/subprocess", 0, 0);
124 g_test_trap_assert_passed();
125 g_test_trap_assert_stderr("");
126 g_test_trap_assert_stdout("");
129 #define TYPE_DYNAMIC_PROPS "dynamic-prop-type"
130 #define DYNAMIC_TYPE(obj) \
131 OBJECT_CHECK(MyType, (obj), TYPE_DYNAMIC_PROPS)
133 #define TYPE_UNUSED_HOTPLUG "hotplug-type"
134 #define TYPE_UNUSED_NOHOTPLUG "nohotplug-type"
136 static void prop1_accessor(Object
*obj
, Visitor
*v
, const char *name
,
137 void *opaque
, Error
**errp
)
139 MyType
*mt
= DYNAMIC_TYPE(obj
);
141 visit_type_uint32(v
, name
, &mt
->prop1
, errp
);
144 static void prop2_accessor(Object
*obj
, Visitor
*v
, const char *name
,
145 void *opaque
, Error
**errp
)
147 MyType
*mt
= DYNAMIC_TYPE(obj
);
149 visit_type_uint32(v
, name
, &mt
->prop2
, errp
);
152 static void dynamic_instance_init(Object
*obj
)
154 object_property_add(obj
, "prop1", "uint32", prop1_accessor
, prop1_accessor
,
156 object_property_add(obj
, "prop2", "uint32", prop2_accessor
, prop2_accessor
,
160 static void dynamic_class_init(ObjectClass
*oc
, void *data
)
162 DeviceClass
*dc
= DEVICE_CLASS(oc
);
168 static const TypeInfo dynamic_prop_type
= {
169 .name
= TYPE_DYNAMIC_PROPS
,
170 .parent
= TYPE_DEVICE
,
171 .instance_size
= sizeof(MyType
),
172 .instance_init
= dynamic_instance_init
,
173 .class_init
= dynamic_class_init
,
176 static void hotplug_class_init(ObjectClass
*oc
, void *data
)
178 DeviceClass
*dc
= DEVICE_CLASS(oc
);
181 dc
->hotpluggable
= true;
184 static const TypeInfo hotplug_type
= {
185 .name
= TYPE_UNUSED_HOTPLUG
,
186 .parent
= TYPE_DEVICE
,
187 .instance_size
= sizeof(MyType
),
188 .instance_init
= dynamic_instance_init
,
189 .class_init
= hotplug_class_init
,
192 static void nohotplug_class_init(ObjectClass
*oc
, void *data
)
194 DeviceClass
*dc
= DEVICE_CLASS(oc
);
197 dc
->hotpluggable
= false;
200 static const TypeInfo nohotplug_type
= {
201 .name
= TYPE_UNUSED_NOHOTPLUG
,
202 .parent
= TYPE_DEVICE
,
203 .instance_size
= sizeof(MyType
),
204 .instance_init
= dynamic_instance_init
,
205 .class_init
= nohotplug_class_init
,
208 #define TYPE_NONDEVICE "nondevice-type"
210 static const TypeInfo nondevice_type
= {
211 .name
= TYPE_NONDEVICE
,
212 .parent
= TYPE_OBJECT
,
215 /* Test setting of dynamic properties using global properties */
216 static void test_dynamic_globalprop_subprocess(void)
219 static GlobalProperty props
[] = {
220 { TYPE_DYNAMIC_PROPS
, "prop1", "101", },
221 { TYPE_DYNAMIC_PROPS
, "prop2", "102", },
222 { TYPE_DYNAMIC_PROPS
"-bad", "prop3", "103", },
223 { TYPE_UNUSED_HOTPLUG
, "prop4", "104", },
224 { TYPE_UNUSED_NOHOTPLUG
, "prop5", "105", },
225 { TYPE_NONDEVICE
, "prop6", "106", },
230 register_global_properties(props
);
232 mt
= DYNAMIC_TYPE(object_new(TYPE_DYNAMIC_PROPS
));
233 qdev_realize(DEVICE(mt
), NULL
, &error_fatal
);
235 g_assert_cmpuint(mt
->prop1
, ==, 101);
236 g_assert_cmpuint(mt
->prop2
, ==, 102);
237 global_error
= qdev_prop_check_globals();
238 g_assert_cmpuint(global_error
, ==, 1);
239 g_assert(props
[0].used
);
240 g_assert(props
[1].used
);
241 g_assert(!props
[2].used
);
242 g_assert(!props
[3].used
);
243 g_assert(!props
[4].used
);
244 g_assert(!props
[5].used
);
247 static void test_dynamic_globalprop(void)
249 g_test_trap_subprocess("/qdev/properties/dynamic/global/subprocess", 0, 0);
250 g_test_trap_assert_passed();
251 g_test_trap_assert_stderr_unmatched("*prop1*");
252 g_test_trap_assert_stderr_unmatched("*prop2*");
253 g_test_trap_assert_stderr("*warning: global dynamic-prop-type-bad.prop3 has invalid class name\n*");
254 g_test_trap_assert_stderr_unmatched("*prop4*");
255 g_test_trap_assert_stderr("*warning: global nohotplug-type.prop5=105 not used\n*");
256 g_test_trap_assert_stderr("*warning: global nondevice-type.prop6 has invalid class name\n*");
257 g_test_trap_assert_stdout("");
260 /* Test if global props affecting subclasses are applied in the right order */
261 static void test_subclass_global_props(void)
264 /* Global properties must be applied in the order they were registered */
265 static GlobalProperty props
[] = {
266 { TYPE_STATIC_PROPS
, "prop1", "101" },
267 { TYPE_SUBCLASS
, "prop1", "102" },
268 { TYPE_SUBCLASS
, "prop2", "103" },
269 { TYPE_STATIC_PROPS
, "prop2", "104" },
273 register_global_properties(props
);
275 mt
= STATIC_TYPE(object_new(TYPE_SUBCLASS
));
276 qdev_realize(DEVICE(mt
), NULL
, &error_fatal
);
278 g_assert_cmpuint(mt
->prop1
, ==, 102);
279 g_assert_cmpuint(mt
->prop2
, ==, 104);
282 int main(int argc
, char **argv
)
284 g_test_init(&argc
, &argv
, NULL
);
286 module_call_init(MODULE_INIT_QOM
);
287 type_register_static(&static_prop_type
);
288 type_register_static(&subclass_type
);
289 type_register_static(&dynamic_prop_type
);
290 type_register_static(&hotplug_type
);
291 type_register_static(&nohotplug_type
);
292 type_register_static(&nondevice_type
);
294 g_test_add_func("/qdev/properties/static/default/subprocess",
295 test_static_prop_subprocess
);
296 g_test_add_func("/qdev/properties/static/default",
299 g_test_add_func("/qdev/properties/static/global/subprocess",
300 test_static_globalprop_subprocess
);
301 g_test_add_func("/qdev/properties/static/global",
302 test_static_globalprop
);
304 g_test_add_func("/qdev/properties/dynamic/global/subprocess",
305 test_dynamic_globalprop_subprocess
);
306 g_test_add_func("/qdev/properties/dynamic/global",
307 test_dynamic_globalprop
);
309 g_test_add_func("/qdev/properties/global/subclass",
310 test_subclass_global_props
);