xref: /qemu/tests/unit/test-qdev-global-props.c (revision 2177801a4899bf29108b3d471417a5b4d701ec29)
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"
29 #include "qom/object.h"
30 #include "qapi/visitor.h"
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 
39 typedef struct MyType {
40     DeviceState parent_obj;
41 
42     uint32_t prop1;
43     uint32_t prop2;
44 } MyType;
45 
46 static 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 
52 static 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 
60 static 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 */
68 static void test_static_prop_subprocess(void)
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 
78 static 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 
86 /* Test setting of static property using global properties */
87 static void test_static_globalprop_subprocess(void)
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 
104 static 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 
112 #define TYPE_DYNAMIC_PROPS "dynamic-prop-type"
113 #define DYNAMIC_TYPE(obj) \
114     OBJECT_CHECK(MyType, (obj), TYPE_DYNAMIC_PROPS)
115 
116 static void prop1_accessor(Object *obj,
117                            Visitor *v,
118                            void *opaque,
119                            const char *name,
120                            Error **errp)
121 {
122     MyType *mt = DYNAMIC_TYPE(obj);
123 
124     visit_type_uint32(v, &mt->prop1, name, errp);
125 }
126 
127 static void prop2_accessor(Object *obj,
128                            Visitor *v,
129                            void *opaque,
130                            const char *name,
131                            Error **errp)
132 {
133     MyType *mt = DYNAMIC_TYPE(obj);
134 
135     visit_type_uint32(v, &mt->prop2, name, errp);
136 }
137 
138 static void dynamic_instance_init(Object *obj)
139 {
140     object_property_add(obj, "prop1", "uint32", prop1_accessor, prop1_accessor,
141                         NULL, NULL, NULL);
142     object_property_add(obj, "prop2", "uint32", prop2_accessor, prop2_accessor,
143                         NULL, NULL, NULL);
144 }
145 
146 static void dynamic_class_init(ObjectClass *oc, void *data)
147 {
148     DeviceClass *dc = DEVICE_CLASS(oc);
149 
150     dc->realize = NULL;
151 }
152 
153 
154 static const TypeInfo dynamic_prop_type = {
155     .name = TYPE_DYNAMIC_PROPS,
156     .parent = TYPE_DEVICE,
157     .instance_size = sizeof(MyType),
158     .instance_init = dynamic_instance_init,
159     .class_init = dynamic_class_init,
160 };
161 
162 /* Test setting of dynamic properties using global properties */
163 static void test_dynamic_globalprop_subprocess(void)
164 {
165     MyType *mt;
166     static GlobalProperty props[] = {
167         { TYPE_DYNAMIC_PROPS, "prop1", "101" },
168         { TYPE_DYNAMIC_PROPS, "prop2", "102" },
169         { TYPE_DYNAMIC_PROPS"-bad", "prop3", "103", true },
170         {}
171     };
172     int all_used;
173 
174     qdev_prop_register_global_list(props);
175 
176     mt = DYNAMIC_TYPE(object_new(TYPE_DYNAMIC_PROPS));
177     qdev_init_nofail(DEVICE(mt));
178 
179     g_assert_cmpuint(mt->prop1, ==, 101);
180     g_assert_cmpuint(mt->prop2, ==, 102);
181     all_used = qdev_prop_check_global();
182     g_assert_cmpuint(all_used, ==, 1);
183 }
184 
185 static void test_dynamic_globalprop(void)
186 {
187     g_test_trap_subprocess("/qdev/properties/dynamic/global/subprocess", 0, 0);
188     g_test_trap_assert_passed();
189     g_test_trap_assert_stderr_unmatched("*prop1*");
190     g_test_trap_assert_stderr_unmatched("*prop2*");
191     g_test_trap_assert_stderr("*Warning: \"-global dynamic-prop-type-bad.prop3=103\" not used\n*");
192     g_test_trap_assert_stdout("");
193 }
194 
195 int main(int argc, char **argv)
196 {
197     g_test_init(&argc, &argv, NULL);
198 
199     module_call_init(MODULE_INIT_QOM);
200     type_register_static(&static_prop_type);
201     type_register_static(&dynamic_prop_type);
202 
203     g_test_add_func("/qdev/properties/static/default/subprocess",
204                     test_static_prop_subprocess);
205     g_test_add_func("/qdev/properties/static/default",
206                     test_static_prop);
207 
208     g_test_add_func("/qdev/properties/static/global/subprocess",
209                     test_static_globalprop_subprocess);
210     g_test_add_func("/qdev/properties/static/global",
211                     test_static_globalprop);
212 
213     g_test_add_func("/qdev/properties/dynamic/global/subprocess",
214                     test_dynamic_globalprop_subprocess);
215     g_test_add_func("/qdev/properties/dynamic/global",
216                     test_dynamic_globalprop);
217 
218     g_test_run();
219 
220     return 0;
221 }
222