]> git.proxmox.com Git - mirror_qemu.git/blame - tests/test-qdev-global-props.c
qom: Swap 'name' next to visitor in ObjectPropertyAccessor
[mirror_qemu.git] / tests / test-qdev-global-props.c
CommitLineData
747b0cb4
EH
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 <glib.h>
26#include <stdint.h>
27
28#include "hw/qdev.h"
99a0b036
EH
29#include "qom/object.h"
30#include "qapi/visitor.h"
747b0cb4
EH
31
32
33#define TYPE_STATIC_PROPS "static_prop_type"
34#define STATIC_TYPE(obj) \
35 OBJECT_CHECK(MyType, (obj), TYPE_STATIC_PROPS)
36
37#define PROP_DEFAULT 100
38
39typedef struct MyType {
40 DeviceState parent_obj;
41
42 uint32_t prop1;
43 uint32_t prop2;
44} MyType;
45
46static Property static_props[] = {
47 DEFINE_PROP_UINT32("prop1", MyType, prop1, PROP_DEFAULT),
48 DEFINE_PROP_UINT32("prop2", MyType, prop2, PROP_DEFAULT),
49 DEFINE_PROP_END_OF_LIST()
50};
51
52static void static_prop_class_init(ObjectClass *oc, void *data)
53{
54 DeviceClass *dc = DEVICE_CLASS(oc);
55
56 dc->realize = NULL;
57 dc->props = static_props;
58}
59
60static const TypeInfo static_prop_type = {
61 .name = TYPE_STATIC_PROPS,
62 .parent = TYPE_DEVICE,
63 .instance_size = sizeof(MyType),
64 .class_init = static_prop_class_init,
65};
66
67/* Test simple static property setting to default value */
2177801a 68static void test_static_prop_subprocess(void)
747b0cb4
EH
69{
70 MyType *mt;
71
72 mt = STATIC_TYPE(object_new(TYPE_STATIC_PROPS));
73 qdev_init_nofail(DEVICE(mt));
74
75 g_assert_cmpuint(mt->prop1, ==, PROP_DEFAULT);
76}
77
2177801a
EH
78static void test_static_prop(void)
79{
80 g_test_trap_subprocess("/qdev/properties/static/default/subprocess", 0, 0);
81 g_test_trap_assert_passed();
82 g_test_trap_assert_stderr("");
83 g_test_trap_assert_stdout("");
84}
85
747b0cb4 86/* Test setting of static property using global properties */
2177801a 87static void test_static_globalprop_subprocess(void)
747b0cb4
EH
88{
89 MyType *mt;
90 static GlobalProperty props[] = {
91 { TYPE_STATIC_PROPS, "prop1", "200" },
92 {}
93 };
94
95 qdev_prop_register_global_list(props);
96
97 mt = STATIC_TYPE(object_new(TYPE_STATIC_PROPS));
98 qdev_init_nofail(DEVICE(mt));
99
100 g_assert_cmpuint(mt->prop1, ==, 200);
101 g_assert_cmpuint(mt->prop2, ==, PROP_DEFAULT);
102}
103
2177801a
EH
104static void test_static_globalprop(void)
105{
106 g_test_trap_subprocess("/qdev/properties/static/global/subprocess", 0, 0);
107 g_test_trap_assert_passed();
108 g_test_trap_assert_stderr("");
109 g_test_trap_assert_stdout("");
110}
111
99a0b036
EH
112#define TYPE_DYNAMIC_PROPS "dynamic-prop-type"
113#define DYNAMIC_TYPE(obj) \
114 OBJECT_CHECK(MyType, (obj), TYPE_DYNAMIC_PROPS)
115
08ac80cd
EH
116#define TYPE_UNUSED_HOTPLUG "hotplug-type"
117#define TYPE_UNUSED_NOHOTPLUG "nohotplug-type"
118
d7bce999
EB
119static void prop1_accessor(Object *obj, Visitor *v, const char *name,
120 void *opaque, Error **errp)
99a0b036
EH
121{
122 MyType *mt = DYNAMIC_TYPE(obj);
123
51e72bc1 124 visit_type_uint32(v, name, &mt->prop1, errp);
99a0b036
EH
125}
126
d7bce999
EB
127static void prop2_accessor(Object *obj, Visitor *v, const char *name,
128 void *opaque, Error **errp)
99a0b036
EH
129{
130 MyType *mt = DYNAMIC_TYPE(obj);
131
51e72bc1 132 visit_type_uint32(v, name, &mt->prop2, errp);
99a0b036
EH
133}
134
135static void dynamic_instance_init(Object *obj)
136{
137 object_property_add(obj, "prop1", "uint32", prop1_accessor, prop1_accessor,
138 NULL, NULL, NULL);
139 object_property_add(obj, "prop2", "uint32", prop2_accessor, prop2_accessor,
140 NULL, NULL, NULL);
141}
142
143static void dynamic_class_init(ObjectClass *oc, void *data)
144{
145 DeviceClass *dc = DEVICE_CLASS(oc);
146
147 dc->realize = NULL;
148}
149
150
151static const TypeInfo dynamic_prop_type = {
152 .name = TYPE_DYNAMIC_PROPS,
153 .parent = TYPE_DEVICE,
154 .instance_size = sizeof(MyType),
155 .instance_init = dynamic_instance_init,
156 .class_init = dynamic_class_init,
157};
158
08ac80cd
EH
159static void hotplug_class_init(ObjectClass *oc, void *data)
160{
161 DeviceClass *dc = DEVICE_CLASS(oc);
162
163 dc->realize = NULL;
164 dc->hotpluggable = true;
165}
166
167static const TypeInfo hotplug_type = {
168 .name = TYPE_UNUSED_HOTPLUG,
169 .parent = TYPE_DEVICE,
170 .instance_size = sizeof(MyType),
171 .instance_init = dynamic_instance_init,
172 .class_init = hotplug_class_init,
173};
174
175static void nohotplug_class_init(ObjectClass *oc, void *data)
176{
177 DeviceClass *dc = DEVICE_CLASS(oc);
178
179 dc->realize = NULL;
180 dc->hotpluggable = false;
181}
182
183static const TypeInfo nohotplug_type = {
184 .name = TYPE_UNUSED_NOHOTPLUG,
185 .parent = TYPE_DEVICE,
186 .instance_size = sizeof(MyType),
187 .instance_init = dynamic_instance_init,
188 .class_init = nohotplug_class_init,
189};
190
191#define TYPE_NONDEVICE "nondevice-type"
192
193static const TypeInfo nondevice_type = {
194 .name = TYPE_NONDEVICE,
195 .parent = TYPE_OBJECT,
196};
197
70e98b23 198/* Test setting of dynamic properties using global properties */
2177801a 199static void test_dynamic_globalprop_subprocess(void)
99a0b036
EH
200{
201 MyType *mt;
202 static GlobalProperty props[] = {
45de8173
EH
203 { TYPE_DYNAMIC_PROPS, "prop1", "101", true },
204 { TYPE_DYNAMIC_PROPS, "prop2", "102", true },
711e2f1e 205 { TYPE_DYNAMIC_PROPS"-bad", "prop3", "103", true },
b3ce84fe 206 { TYPE_UNUSED_HOTPLUG, "prop4", "104", true },
08ac80cd
EH
207 { TYPE_UNUSED_NOHOTPLUG, "prop5", "105", true },
208 { TYPE_NONDEVICE, "prop6", "106", true },
99a0b036
EH
209 {}
210 };
711e2f1e 211 int all_used;
99a0b036
EH
212
213 qdev_prop_register_global_list(props);
214
215 mt = DYNAMIC_TYPE(object_new(TYPE_DYNAMIC_PROPS));
216 qdev_init_nofail(DEVICE(mt));
217
218 g_assert_cmpuint(mt->prop1, ==, 101);
219 g_assert_cmpuint(mt->prop2, ==, 102);
d828c430 220 all_used = qdev_prop_check_globals();
711e2f1e 221 g_assert_cmpuint(all_used, ==, 1);
b3ce84fe
EH
222 g_assert(props[0].used);
223 g_assert(props[1].used);
224 g_assert(!props[2].used);
225 g_assert(!props[3].used);
226 g_assert(!props[4].used);
227 g_assert(!props[5].used);
99a0b036
EH
228}
229
2177801a
EH
230static void test_dynamic_globalprop(void)
231{
232 g_test_trap_subprocess("/qdev/properties/dynamic/global/subprocess", 0, 0);
233 g_test_trap_assert_passed();
234 g_test_trap_assert_stderr_unmatched("*prop1*");
235 g_test_trap_assert_stderr_unmatched("*prop2*");
b3ce84fe 236 g_test_trap_assert_stderr("*Warning: global dynamic-prop-type-bad.prop3 has invalid class name\n*");
08ac80cd 237 g_test_trap_assert_stderr_unmatched("*prop4*");
b3ce84fe
EH
238 g_test_trap_assert_stderr("*Warning: global nohotplug-type.prop5=105 not used\n*");
239 g_test_trap_assert_stderr("*Warning: global nondevice-type.prop6 has invalid class name\n*");
240 g_test_trap_assert_stdout("");
241}
242
243/* Test setting of dynamic properties using user_provided=false properties */
244static void test_dynamic_globalprop_nouser_subprocess(void)
245{
246 MyType *mt;
247 static GlobalProperty props[] = {
248 { TYPE_DYNAMIC_PROPS, "prop1", "101" },
249 { TYPE_DYNAMIC_PROPS, "prop2", "102" },
250 { TYPE_DYNAMIC_PROPS"-bad", "prop3", "103" },
251 { TYPE_UNUSED_HOTPLUG, "prop4", "104" },
252 { TYPE_UNUSED_NOHOTPLUG, "prop5", "105" },
253 { TYPE_NONDEVICE, "prop6", "106" },
254 {}
255 };
256 int all_used;
257
258 qdev_prop_register_global_list(props);
259
260 mt = DYNAMIC_TYPE(object_new(TYPE_DYNAMIC_PROPS));
261 qdev_init_nofail(DEVICE(mt));
262
263 g_assert_cmpuint(mt->prop1, ==, 101);
264 g_assert_cmpuint(mt->prop2, ==, 102);
265 all_used = qdev_prop_check_globals();
266 g_assert_cmpuint(all_used, ==, 0);
267 g_assert(props[0].used);
268 g_assert(props[1].used);
269 g_assert(!props[2].used);
270 g_assert(!props[3].used);
271 g_assert(!props[4].used);
272 g_assert(!props[5].used);
273}
274
275static void test_dynamic_globalprop_nouser(void)
276{
277 g_test_trap_subprocess("/qdev/properties/dynamic/global/nouser/subprocess", 0, 0);
278 g_test_trap_assert_passed();
279 g_test_trap_assert_stderr("");
2177801a
EH
280 g_test_trap_assert_stdout("");
281}
282
747b0cb4
EH
283int main(int argc, char **argv)
284{
285 g_test_init(&argc, &argv, NULL);
286
287 module_call_init(MODULE_INIT_QOM);
288 type_register_static(&static_prop_type);
99a0b036 289 type_register_static(&dynamic_prop_type);
08ac80cd
EH
290 type_register_static(&hotplug_type);
291 type_register_static(&nohotplug_type);
292 type_register_static(&nondevice_type);
747b0cb4 293
2177801a
EH
294 g_test_add_func("/qdev/properties/static/default/subprocess",
295 test_static_prop_subprocess);
296 g_test_add_func("/qdev/properties/static/default",
297 test_static_prop);
298
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);
303
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);
747b0cb4 308
b3ce84fe
EH
309 g_test_add_func("/qdev/properties/dynamic/global/nouser/subprocess",
310 test_dynamic_globalprop_nouser_subprocess);
311 g_test_add_func("/qdev/properties/dynamic/global/nouser",
312 test_dynamic_globalprop_nouser);
313
747b0cb4
EH
314 g_test_run();
315
316 return 0;
317}