xref: /qemu/tests/unit/test-qdev-global-props.c (revision e6add65b9c823f00f63a78571581ae0c30867b3a)
1 /*
2  *  Test code for qdev global-properties handling
3  *
4  *  Copyright (c) 2012 Red Hat Inc.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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.
23  */
24 
25 #include "qemu/osdep.h"
26 
27 #include "hw/qdev.h"
28 #include "qom/object.h"
29 #include "qapi/visitor.h"
30 
31 
32 #define TYPE_STATIC_PROPS "static_prop_type"
33 #define STATIC_TYPE(obj) \
34     OBJECT_CHECK(MyType, (obj), TYPE_STATIC_PROPS)
35 
36 #define TYPE_SUBCLASS "static_prop_subtype"
37 
38 #define PROP_DEFAULT 100
39 
40 typedef struct MyType {
41     DeviceState parent_obj;
42 
43     uint32_t prop1;
44     uint32_t prop2;
45 } MyType;
46 
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()
51 };
52 
53 static void static_prop_class_init(ObjectClass *oc, void *data)
54 {
55     DeviceClass *dc = DEVICE_CLASS(oc);
56 
57     dc->realize = NULL;
58     dc->props = static_props;
59 }
60 
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,
66 };
67 
68 static const TypeInfo subclass_type = {
69     .name = TYPE_SUBCLASS,
70     .parent = TYPE_STATIC_PROPS,
71 };
72 
73 /* Test simple static property setting to default value */
74 static void test_static_prop_subprocess(void)
75 {
76     MyType *mt;
77 
78     mt = STATIC_TYPE(object_new(TYPE_STATIC_PROPS));
79     qdev_init_nofail(DEVICE(mt));
80 
81     g_assert_cmpuint(mt->prop1, ==, PROP_DEFAULT);
82 }
83 
84 static void test_static_prop(void)
85 {
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("");
90 }
91 
92 static void register_global_properties(GlobalProperty *props)
93 {
94     int i;
95 
96     for (i = 0; props[i].driver != NULL; i++) {
97         qdev_prop_register_global(props + i);
98     }
99 }
100 
101 
102 /* Test setting of static property using global properties */
103 static void test_static_globalprop_subprocess(void)
104 {
105     MyType *mt;
106     static GlobalProperty props[] = {
107         { TYPE_STATIC_PROPS, "prop1", "200" },
108         {}
109     };
110 
111     register_global_properties(props);
112 
113     mt = STATIC_TYPE(object_new(TYPE_STATIC_PROPS));
114     qdev_init_nofail(DEVICE(mt));
115 
116     g_assert_cmpuint(mt->prop1, ==, 200);
117     g_assert_cmpuint(mt->prop2, ==, PROP_DEFAULT);
118 }
119 
120 static void test_static_globalprop(void)
121 {
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("");
126 }
127 
128 #define TYPE_DYNAMIC_PROPS "dynamic-prop-type"
129 #define DYNAMIC_TYPE(obj) \
130     OBJECT_CHECK(MyType, (obj), TYPE_DYNAMIC_PROPS)
131 
132 #define TYPE_UNUSED_HOTPLUG   "hotplug-type"
133 #define TYPE_UNUSED_NOHOTPLUG "nohotplug-type"
134 
135 static void prop1_accessor(Object *obj, Visitor *v, const char *name,
136                            void *opaque, Error **errp)
137 {
138     MyType *mt = DYNAMIC_TYPE(obj);
139 
140     visit_type_uint32(v, name, &mt->prop1, errp);
141 }
142 
143 static void prop2_accessor(Object *obj, Visitor *v, const char *name,
144                            void *opaque, Error **errp)
145 {
146     MyType *mt = DYNAMIC_TYPE(obj);
147 
148     visit_type_uint32(v, name, &mt->prop2, errp);
149 }
150 
151 static void dynamic_instance_init(Object *obj)
152 {
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);
157 }
158 
159 static void dynamic_class_init(ObjectClass *oc, void *data)
160 {
161     DeviceClass *dc = DEVICE_CLASS(oc);
162 
163     dc->realize = NULL;
164 }
165 
166 
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,
173 };
174 
175 static void hotplug_class_init(ObjectClass *oc, void *data)
176 {
177     DeviceClass *dc = DEVICE_CLASS(oc);
178 
179     dc->realize = NULL;
180     dc->hotpluggable = true;
181 }
182 
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,
189 };
190 
191 static void nohotplug_class_init(ObjectClass *oc, void *data)
192 {
193     DeviceClass *dc = DEVICE_CLASS(oc);
194 
195     dc->realize = NULL;
196     dc->hotpluggable = false;
197 }
198 
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,
205 };
206 
207 #define TYPE_NONDEVICE "nondevice-type"
208 
209 static const TypeInfo nondevice_type = {
210     .name = TYPE_NONDEVICE,
211     .parent = TYPE_OBJECT,
212 };
213 
214 /* Test setting of dynamic properties using global properties */
215 static void test_dynamic_globalprop_subprocess(void)
216 {
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 },
225         {}
226     };
227     int global_error;
228 
229     register_global_properties(props);
230 
231     mt = DYNAMIC_TYPE(object_new(TYPE_DYNAMIC_PROPS));
232     qdev_init_nofail(DEVICE(mt));
233 
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);
244 }
245 
246 static void test_dynamic_globalprop(void)
247 {
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("");
257 }
258 
259 /* Test setting of dynamic properties using user_provided=false properties */
260 static void test_dynamic_globalprop_nouser_subprocess(void)
261 {
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" },
270         {}
271     };
272     int global_error;
273 
274     register_global_properties(props);
275 
276     mt = DYNAMIC_TYPE(object_new(TYPE_DYNAMIC_PROPS));
277     qdev_init_nofail(DEVICE(mt));
278 
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);
289 }
290 
291 static void test_dynamic_globalprop_nouser(void)
292 {
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("");
297 }
298 
299 /* Test if global props affecting subclasses are applied in the right order */
300 static void test_subclass_global_props(void)
301 {
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" },
309         {}
310     };
311 
312     register_global_properties(props);
313 
314     mt = STATIC_TYPE(object_new(TYPE_SUBCLASS));
315     qdev_init_nofail(DEVICE(mt));
316 
317     g_assert_cmpuint(mt->prop1, ==, 102);
318     g_assert_cmpuint(mt->prop2, ==, 104);
319 }
320 
321 int main(int argc, char **argv)
322 {
323     g_test_init(&argc, &argv, NULL);
324 
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);
332 
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);
337 
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);
342 
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);
347 
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);
352 
353     g_test_add_func("/qdev/properties/global/subclass",
354                     test_subclass_global_props);
355 
356     g_test_run();
357 
358     return 0;
359 }
360