Merge remote-tracking branch 'remotes/dgilbert-gitlab/tags/pull-migration-20210726a...
[qemu/armbru.git] / tests / unit / test-qdev-global-props.c
blobc8862cac5fef640379d097d16a8e7c8beca70139
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-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 typedef struct MyType MyType;
35 DECLARE_INSTANCE_CHECKER(MyType, STATIC_TYPE,
36 TYPE_STATIC_PROPS)
38 #define TYPE_SUBCLASS "static_prop_subtype"
40 #define PROP_DEFAULT 100
42 struct MyType {
43 DeviceState parent_obj;
45 uint32_t prop1;
46 uint32_t prop2;
49 static Property static_props[] = {
50 DEFINE_PROP_UINT32("prop1", MyType, prop1, PROP_DEFAULT),
51 DEFINE_PROP_UINT32("prop2", MyType, prop2, PROP_DEFAULT),
52 DEFINE_PROP_END_OF_LIST()
55 static void static_prop_class_init(ObjectClass *oc, void *data)
57 DeviceClass *dc = DEVICE_CLASS(oc);
59 dc->realize = NULL;
60 device_class_set_props(dc, static_props);
63 static const TypeInfo static_prop_type = {
64 .name = TYPE_STATIC_PROPS,
65 .parent = TYPE_DEVICE,
66 .instance_size = sizeof(MyType),
67 .class_init = static_prop_class_init,
70 static const TypeInfo subclass_type = {
71 .name = TYPE_SUBCLASS,
72 .parent = TYPE_STATIC_PROPS,
75 /* Test simple static property setting to default value */
76 static void test_static_prop_subprocess(void)
78 MyType *mt;
80 mt = STATIC_TYPE(object_new(TYPE_STATIC_PROPS));
81 qdev_realize(DEVICE(mt), NULL, &error_fatal);
83 g_assert_cmpuint(mt->prop1, ==, PROP_DEFAULT);
86 static void test_static_prop(void)
88 g_test_trap_subprocess("/qdev/properties/static/default/subprocess", 0, 0);
89 g_test_trap_assert_passed();
90 g_test_trap_assert_stderr("");
91 g_test_trap_assert_stdout("");
94 static void register_global_properties(GlobalProperty *props)
96 int i;
98 for (i = 0; props[i].driver != NULL; i++) {
99 qdev_prop_register_global(props + i);
104 /* Test setting of static property using global properties */
105 static void test_static_globalprop_subprocess(void)
107 MyType *mt;
108 static GlobalProperty props[] = {
109 { TYPE_STATIC_PROPS, "prop1", "200" },
113 register_global_properties(props);
115 mt = STATIC_TYPE(object_new(TYPE_STATIC_PROPS));
116 qdev_realize(DEVICE(mt), NULL, &error_fatal);
118 g_assert_cmpuint(mt->prop1, ==, 200);
119 g_assert_cmpuint(mt->prop2, ==, PROP_DEFAULT);
122 static void test_static_globalprop(void)
124 g_test_trap_subprocess("/qdev/properties/static/global/subprocess", 0, 0);
125 g_test_trap_assert_passed();
126 g_test_trap_assert_stderr("");
127 g_test_trap_assert_stdout("");
130 #define TYPE_DYNAMIC_PROPS "dynamic-prop-type"
131 DECLARE_INSTANCE_CHECKER(MyType, DYNAMIC_TYPE,
132 TYPE_DYNAMIC_PROPS)
134 #define TYPE_UNUSED_HOTPLUG "hotplug-type"
135 #define TYPE_UNUSED_NOHOTPLUG "nohotplug-type"
137 static void prop1_accessor(Object *obj, Visitor *v, const char *name,
138 void *opaque, Error **errp)
140 MyType *mt = DYNAMIC_TYPE(obj);
142 visit_type_uint32(v, name, &mt->prop1, errp);
145 static void prop2_accessor(Object *obj, Visitor *v, const char *name,
146 void *opaque, Error **errp)
148 MyType *mt = DYNAMIC_TYPE(obj);
150 visit_type_uint32(v, name, &mt->prop2, errp);
153 static void dynamic_instance_init(Object *obj)
155 object_property_add(obj, "prop1", "uint32", prop1_accessor, prop1_accessor,
156 NULL, NULL);
157 object_property_add(obj, "prop2", "uint32", prop2_accessor, prop2_accessor,
158 NULL, NULL);
161 static void dynamic_class_init(ObjectClass *oc, void *data)
163 DeviceClass *dc = DEVICE_CLASS(oc);
165 dc->realize = NULL;
169 static const TypeInfo dynamic_prop_type = {
170 .name = TYPE_DYNAMIC_PROPS,
171 .parent = TYPE_DEVICE,
172 .instance_size = sizeof(MyType),
173 .instance_init = dynamic_instance_init,
174 .class_init = dynamic_class_init,
177 static void hotplug_class_init(ObjectClass *oc, void *data)
179 DeviceClass *dc = DEVICE_CLASS(oc);
181 dc->realize = NULL;
182 dc->hotpluggable = true;
185 static const TypeInfo hotplug_type = {
186 .name = TYPE_UNUSED_HOTPLUG,
187 .parent = TYPE_DEVICE,
188 .instance_size = sizeof(MyType),
189 .instance_init = dynamic_instance_init,
190 .class_init = hotplug_class_init,
193 static void nohotplug_class_init(ObjectClass *oc, void *data)
195 DeviceClass *dc = DEVICE_CLASS(oc);
197 dc->realize = NULL;
198 dc->hotpluggable = false;
201 static const TypeInfo nohotplug_type = {
202 .name = TYPE_UNUSED_NOHOTPLUG,
203 .parent = TYPE_DEVICE,
204 .instance_size = sizeof(MyType),
205 .instance_init = dynamic_instance_init,
206 .class_init = nohotplug_class_init,
209 #define TYPE_NONDEVICE "nondevice-type"
211 static const TypeInfo nondevice_type = {
212 .name = TYPE_NONDEVICE,
213 .parent = TYPE_OBJECT,
216 /* Test setting of dynamic properties using global properties */
217 static void test_dynamic_globalprop_subprocess(void)
219 MyType *mt;
220 static GlobalProperty props[] = {
221 { TYPE_DYNAMIC_PROPS, "prop1", "101", },
222 { TYPE_DYNAMIC_PROPS, "prop2", "102", },
223 { TYPE_DYNAMIC_PROPS"-bad", "prop3", "103", },
224 { TYPE_UNUSED_HOTPLUG, "prop4", "104", },
225 { TYPE_UNUSED_NOHOTPLUG, "prop5", "105", },
226 { TYPE_NONDEVICE, "prop6", "106", },
229 int global_error;
231 register_global_properties(props);
233 mt = DYNAMIC_TYPE(object_new(TYPE_DYNAMIC_PROPS));
234 qdev_realize(DEVICE(mt), NULL, &error_fatal);
236 g_assert_cmpuint(mt->prop1, ==, 101);
237 g_assert_cmpuint(mt->prop2, ==, 102);
238 global_error = qdev_prop_check_globals();
239 g_assert_cmpuint(global_error, ==, 1);
240 g_assert(props[0].used);
241 g_assert(props[1].used);
242 g_assert(!props[2].used);
243 g_assert(!props[3].used);
244 g_assert(!props[4].used);
245 g_assert(!props[5].used);
248 static void test_dynamic_globalprop(void)
250 g_test_trap_subprocess("/qdev/properties/dynamic/global/subprocess", 0, 0);
251 g_test_trap_assert_passed();
252 g_test_trap_assert_stderr_unmatched("*prop1*");
253 g_test_trap_assert_stderr_unmatched("*prop2*");
254 g_test_trap_assert_stderr(
255 "*warning: global dynamic-prop-type-bad.prop3 has invalid class name*");
256 g_test_trap_assert_stderr_unmatched("*prop4*");
257 g_test_trap_assert_stderr(
258 "*warning: global nohotplug-type.prop5=105 not used*");
259 g_test_trap_assert_stderr(
260 "*warning: global nondevice-type.prop6 has invalid class name*");
261 g_test_trap_assert_stdout("");
264 /* Test if global props affecting subclasses are applied in the right order */
265 static void test_subclass_global_props(void)
267 MyType *mt;
268 /* Global properties must be applied in the order they were registered */
269 static GlobalProperty props[] = {
270 { TYPE_STATIC_PROPS, "prop1", "101" },
271 { TYPE_SUBCLASS, "prop1", "102" },
272 { TYPE_SUBCLASS, "prop2", "103" },
273 { TYPE_STATIC_PROPS, "prop2", "104" },
277 register_global_properties(props);
279 mt = STATIC_TYPE(object_new(TYPE_SUBCLASS));
280 qdev_realize(DEVICE(mt), NULL, &error_fatal);
282 g_assert_cmpuint(mt->prop1, ==, 102);
283 g_assert_cmpuint(mt->prop2, ==, 104);
286 int main(int argc, char **argv)
288 g_test_init(&argc, &argv, NULL);
290 module_call_init(MODULE_INIT_QOM);
291 type_register_static(&static_prop_type);
292 type_register_static(&subclass_type);
293 type_register_static(&dynamic_prop_type);
294 type_register_static(&hotplug_type);
295 type_register_static(&nohotplug_type);
296 type_register_static(&nondevice_type);
298 g_test_add_func("/qdev/properties/static/default/subprocess",
299 test_static_prop_subprocess);
300 g_test_add_func("/qdev/properties/static/default",
301 test_static_prop);
303 g_test_add_func("/qdev/properties/static/global/subprocess",
304 test_static_globalprop_subprocess);
305 g_test_add_func("/qdev/properties/static/global",
306 test_static_globalprop);
308 g_test_add_func("/qdev/properties/dynamic/global/subprocess",
309 test_dynamic_globalprop_subprocess);
310 g_test_add_func("/qdev/properties/dynamic/global",
311 test_dynamic_globalprop);
313 g_test_add_func("/qdev/properties/global/subclass",
314 test_subclass_global_props);
316 g_test_run();
318 return 0;