xref: /qemu/tests/unit/test-qdev-global-props.c (revision 513823e7521a09ed7ad1e32e6454bac3b2cbf52d)
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-properties.h"
28 #include "qom/object.h"
29 #include "qapi/error.h"
30 #include "qapi/visitor.h"
31 
32 
33 #define TYPE_STATIC_PROPS "static_prop_type"
34 typedef struct MyType MyType;
35 DECLARE_INSTANCE_CHECKER(MyType, STATIC_TYPE,
36                          TYPE_STATIC_PROPS)
37 
38 #define TYPE_SUBCLASS "static_prop_subtype"
39 
40 #define PROP_DEFAULT 100
41 
42 struct MyType {
43     DeviceState parent_obj;
44 
45     uint32_t prop1;
46     uint32_t prop2;
47 };
48 
49 static const Property static_props[] = {
50     DEFINE_PROP_UINT32("prop1", MyType, prop1, PROP_DEFAULT),
51     DEFINE_PROP_UINT32("prop2", MyType, prop2, PROP_DEFAULT),
52 };
53 
54 static void static_prop_class_init(ObjectClass *oc, void *data)
55 {
56     DeviceClass *dc = DEVICE_CLASS(oc);
57 
58     dc->realize = NULL;
59     device_class_set_props(dc, static_props);
60 }
61 
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,
67 };
68 
69 static const TypeInfo subclass_type = {
70     .name = TYPE_SUBCLASS,
71     .parent = TYPE_STATIC_PROPS,
72 };
73 
74 /*
75  * Initialize a fake machine, being prepared for future tests.
76  *
77  * All the tests later (even if to be run in subprocesses.. which will
78  * inherit the global states of the parent process) will try to create qdev
79  * and realize the device.
80  *
81  * Realization of such anonymous qdev (with no parent object) requires both
82  * the machine object and its "unattached" container to be at least present.
83  */
84 static void test_init_machine(void)
85 {
86     /* This is a fake machine - it doesn't need to be a machine object */
87     Object *machine = object_property_add_new_container(
88         object_get_root(), "machine");
89 
90     /* This container must exist for anonymous qdevs to realize() */
91     object_property_add_new_container(machine, "unattached");
92 }
93 
94 /* Test simple static property setting to default value */
95 static void test_static_prop_subprocess(void)
96 {
97     MyType *mt;
98 
99     mt = STATIC_TYPE(object_new(TYPE_STATIC_PROPS));
100     qdev_realize(DEVICE(mt), NULL, &error_fatal);
101 
102     g_assert_cmpuint(mt->prop1, ==, PROP_DEFAULT);
103 }
104 
105 static void test_static_prop(void)
106 {
107     g_test_trap_subprocess("/qdev/properties/static/default/subprocess", 0, 0);
108     g_test_trap_assert_passed();
109     g_test_trap_assert_stderr("");
110     g_test_trap_assert_stdout("");
111 }
112 
113 static void register_global_properties(GlobalProperty *props)
114 {
115     int i;
116 
117     for (i = 0; props[i].driver != NULL; i++) {
118         qdev_prop_register_global(props + i);
119     }
120 }
121 
122 
123 /* Test setting of static property using global properties */
124 static void test_static_globalprop_subprocess(void)
125 {
126     MyType *mt;
127     static GlobalProperty props[] = {
128         { TYPE_STATIC_PROPS, "prop1", "200" },
129         {}
130     };
131 
132     register_global_properties(props);
133 
134     mt = STATIC_TYPE(object_new(TYPE_STATIC_PROPS));
135     qdev_realize(DEVICE(mt), NULL, &error_fatal);
136 
137     g_assert_cmpuint(mt->prop1, ==, 200);
138     g_assert_cmpuint(mt->prop2, ==, PROP_DEFAULT);
139 }
140 
141 static void test_static_globalprop(void)
142 {
143     g_test_trap_subprocess("/qdev/properties/static/global/subprocess", 0, 0);
144     g_test_trap_assert_passed();
145     g_test_trap_assert_stderr("");
146     g_test_trap_assert_stdout("");
147 }
148 
149 #define TYPE_DYNAMIC_PROPS "dynamic-prop-type"
150 DECLARE_INSTANCE_CHECKER(MyType, DYNAMIC_TYPE,
151                          TYPE_DYNAMIC_PROPS)
152 
153 #define TYPE_UNUSED_HOTPLUG   "hotplug-type"
154 #define TYPE_UNUSED_NOHOTPLUG "nohotplug-type"
155 
156 static void prop1_accessor(Object *obj, Visitor *v, const char *name,
157                            void *opaque, Error **errp)
158 {
159     MyType *mt = DYNAMIC_TYPE(obj);
160 
161     visit_type_uint32(v, name, &mt->prop1, errp);
162 }
163 
164 static void prop2_accessor(Object *obj, Visitor *v, const char *name,
165                            void *opaque, Error **errp)
166 {
167     MyType *mt = DYNAMIC_TYPE(obj);
168 
169     visit_type_uint32(v, name, &mt->prop2, errp);
170 }
171 
172 static void dynamic_instance_init(Object *obj)
173 {
174     object_property_add(obj, "prop1", "uint32", prop1_accessor, prop1_accessor,
175                         NULL, NULL);
176     object_property_add(obj, "prop2", "uint32", prop2_accessor, prop2_accessor,
177                         NULL, NULL);
178 }
179 
180 static void dynamic_class_init(ObjectClass *oc, void *data)
181 {
182     DeviceClass *dc = DEVICE_CLASS(oc);
183 
184     dc->realize = NULL;
185 }
186 
187 
188 static const TypeInfo dynamic_prop_type = {
189     .name = TYPE_DYNAMIC_PROPS,
190     .parent = TYPE_DEVICE,
191     .instance_size = sizeof(MyType),
192     .instance_init = dynamic_instance_init,
193     .class_init = dynamic_class_init,
194 };
195 
196 static void hotplug_class_init(ObjectClass *oc, void *data)
197 {
198     DeviceClass *dc = DEVICE_CLASS(oc);
199 
200     dc->realize = NULL;
201     dc->hotpluggable = true;
202 }
203 
204 static const TypeInfo hotplug_type = {
205     .name = TYPE_UNUSED_HOTPLUG,
206     .parent = TYPE_DEVICE,
207     .instance_size = sizeof(MyType),
208     .instance_init = dynamic_instance_init,
209     .class_init = hotplug_class_init,
210 };
211 
212 static void nohotplug_class_init(ObjectClass *oc, void *data)
213 {
214     DeviceClass *dc = DEVICE_CLASS(oc);
215 
216     dc->realize = NULL;
217     dc->hotpluggable = false;
218 }
219 
220 static const TypeInfo nohotplug_type = {
221     .name = TYPE_UNUSED_NOHOTPLUG,
222     .parent = TYPE_DEVICE,
223     .instance_size = sizeof(MyType),
224     .instance_init = dynamic_instance_init,
225     .class_init = nohotplug_class_init,
226 };
227 
228 #define TYPE_NONDEVICE "nondevice-type"
229 
230 static const TypeInfo nondevice_type = {
231     .name = TYPE_NONDEVICE,
232     .parent = TYPE_OBJECT,
233 };
234 
235 /* Test setting of dynamic properties using global properties */
236 static void test_dynamic_globalprop_subprocess(void)
237 {
238     MyType *mt;
239     static GlobalProperty props[] = {
240         { TYPE_DYNAMIC_PROPS, "prop1", "101", },
241         { TYPE_DYNAMIC_PROPS, "prop2", "102", },
242         { TYPE_DYNAMIC_PROPS"-bad", "prop3", "103", },
243         { TYPE_UNUSED_HOTPLUG, "prop4", "104", },
244         { TYPE_UNUSED_NOHOTPLUG, "prop5", "105", },
245         { TYPE_NONDEVICE, "prop6", "106", },
246         {}
247     };
248     int global_error;
249 
250     register_global_properties(props);
251 
252     mt = DYNAMIC_TYPE(object_new(TYPE_DYNAMIC_PROPS));
253     qdev_realize(DEVICE(mt), NULL, &error_fatal);
254 
255     g_assert_cmpuint(mt->prop1, ==, 101);
256     g_assert_cmpuint(mt->prop2, ==, 102);
257     global_error = qdev_prop_check_globals();
258     g_assert_cmpuint(global_error, ==, 1);
259     g_assert(props[0].used);
260     g_assert(props[1].used);
261     g_assert(!props[2].used);
262     g_assert(!props[3].used);
263     g_assert(!props[4].used);
264     g_assert(!props[5].used);
265 }
266 
267 static void test_dynamic_globalprop(void)
268 {
269     g_test_trap_subprocess("/qdev/properties/dynamic/global/subprocess", 0, 0);
270     g_test_trap_assert_passed();
271     g_test_trap_assert_stderr_unmatched("*prop1*");
272     g_test_trap_assert_stderr_unmatched("*prop2*");
273     g_test_trap_assert_stderr(
274         "*warning: global dynamic-prop-type-bad.prop3 has invalid class name*");
275     g_test_trap_assert_stderr_unmatched("*prop4*");
276     g_test_trap_assert_stderr(
277         "*warning: global nohotplug-type.prop5=105 not used*");
278     g_test_trap_assert_stderr(
279         "*warning: global nondevice-type.prop6 has invalid class name*");
280     g_test_trap_assert_stdout("");
281 }
282 
283 /* Test if global props affecting subclasses are applied in the right order */
284 static void test_subclass_global_props(void)
285 {
286     MyType *mt;
287     /* Global properties must be applied in the order they were registered */
288     static GlobalProperty props[] = {
289         { TYPE_STATIC_PROPS, "prop1", "101" },
290         { TYPE_SUBCLASS,     "prop1", "102" },
291         { TYPE_SUBCLASS,     "prop2", "103" },
292         { TYPE_STATIC_PROPS, "prop2", "104" },
293         {}
294     };
295 
296     register_global_properties(props);
297 
298     mt = STATIC_TYPE(object_new(TYPE_SUBCLASS));
299     qdev_realize(DEVICE(mt), NULL, &error_fatal);
300 
301     g_assert_cmpuint(mt->prop1, ==, 102);
302     g_assert_cmpuint(mt->prop2, ==, 104);
303 }
304 
305 int main(int argc, char **argv)
306 {
307     g_test_init(&argc, &argv, NULL);
308 
309     module_call_init(MODULE_INIT_QOM);
310     type_register_static(&static_prop_type);
311     type_register_static(&subclass_type);
312     type_register_static(&dynamic_prop_type);
313     type_register_static(&hotplug_type);
314     type_register_static(&nohotplug_type);
315     type_register_static(&nondevice_type);
316 
317     test_init_machine();
318 
319     g_test_add_func("/qdev/properties/static/default/subprocess",
320                     test_static_prop_subprocess);
321     g_test_add_func("/qdev/properties/static/default",
322                     test_static_prop);
323 
324     g_test_add_func("/qdev/properties/static/global/subprocess",
325                     test_static_globalprop_subprocess);
326     g_test_add_func("/qdev/properties/static/global",
327                     test_static_globalprop);
328 
329     g_test_add_func("/qdev/properties/dynamic/global/subprocess",
330                     test_dynamic_globalprop_subprocess);
331     g_test_add_func("/qdev/properties/dynamic/global",
332                     test_dynamic_globalprop);
333 
334     g_test_add_func("/qdev/properties/global/subclass",
335                     test_subclass_global_props);
336 
337     g_test_run();
338 
339     return 0;
340 }
341