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/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
;
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
);
58 device_class_set_props(dc
, 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)
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
)
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)
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
,
155 object_property_add(obj
, "prop2", "uint32", prop2_accessor
, prop2_accessor
,
159 static void dynamic_class_init(ObjectClass
*oc
, void *data
)
161 DeviceClass
*dc
= DEVICE_CLASS(oc
);
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
);
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
);
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)
218 static GlobalProperty props
[] = {
219 { TYPE_DYNAMIC_PROPS
, "prop1", "101", },
220 { TYPE_DYNAMIC_PROPS
, "prop2", "102", },
221 { TYPE_DYNAMIC_PROPS
"-bad", "prop3", "103", },
222 { TYPE_UNUSED_HOTPLUG
, "prop4", "104", },
223 { TYPE_UNUSED_NOHOTPLUG
, "prop5", "105", },
224 { TYPE_NONDEVICE
, "prop6", "106", },
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 if global props affecting subclasses are applied in the right order */
260 static void test_subclass_global_props(void)
263 /* Global properties must be applied in the order they were registered */
264 static GlobalProperty props
[] = {
265 { TYPE_STATIC_PROPS
, "prop1", "101" },
266 { TYPE_SUBCLASS
, "prop1", "102" },
267 { TYPE_SUBCLASS
, "prop2", "103" },
268 { TYPE_STATIC_PROPS
, "prop2", "104" },
272 register_global_properties(props
);
274 mt
= STATIC_TYPE(object_new(TYPE_SUBCLASS
));
275 qdev_init_nofail(DEVICE(mt
));
277 g_assert_cmpuint(mt
->prop1
, ==, 102);
278 g_assert_cmpuint(mt
->prop2
, ==, 104);
281 int main(int argc
, char **argv
)
283 g_test_init(&argc
, &argv
, NULL
);
285 module_call_init(MODULE_INIT_QOM
);
286 type_register_static(&static_prop_type
);
287 type_register_static(&subclass_type
);
288 type_register_static(&dynamic_prop_type
);
289 type_register_static(&hotplug_type
);
290 type_register_static(&nohotplug_type
);
291 type_register_static(&nondevice_type
);
293 g_test_add_func("/qdev/properties/static/default/subprocess",
294 test_static_prop_subprocess
);
295 g_test_add_func("/qdev/properties/static/default",
298 g_test_add_func("/qdev/properties/static/global/subprocess",
299 test_static_globalprop_subprocess
);
300 g_test_add_func("/qdev/properties/static/global",
301 test_static_globalprop
);
303 g_test_add_func("/qdev/properties/dynamic/global/subprocess",
304 test_dynamic_globalprop_subprocess
);
305 g_test_add_func("/qdev/properties/dynamic/global",
306 test_dynamic_globalprop
);
308 g_test_add_func("/qdev/properties/global/subclass",
309 test_subclass_global_props
);