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/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 device_class_set_props(dc, 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); 155 object_property_add(obj, "prop2", "uint32", prop2_accessor, prop2_accessor, 156 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", }, 220 { TYPE_DYNAMIC_PROPS, "prop2", "102", }, 221 { TYPE_DYNAMIC_PROPS"-bad", "prop3", "103", }, 222 { TYPE_UNUSED_HOTPLUG, "prop4", "104", }, 223 { TYPE_UNUSED_NOHOTPLUG, "prop5", "105", }, 224 { TYPE_NONDEVICE, "prop6", "106", }, 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 if global props affecting subclasses are applied in the right order */ 260 static void test_subclass_global_props(void) 261 { 262 MyType *mt; 263 /* Global properties must be applied in the order they were registered */ 264 static GlobalProperty props[] = { 265 { TYPE_STATIC_PROPS, "prop1", "101" }, 266 { TYPE_SUBCLASS, "prop1", "102" }, 267 { TYPE_SUBCLASS, "prop2", "103" }, 268 { TYPE_STATIC_PROPS, "prop2", "104" }, 269 {} 270 }; 271 272 register_global_properties(props); 273 274 mt = STATIC_TYPE(object_new(TYPE_SUBCLASS)); 275 qdev_init_nofail(DEVICE(mt)); 276 277 g_assert_cmpuint(mt->prop1, ==, 102); 278 g_assert_cmpuint(mt->prop2, ==, 104); 279 } 280 281 int main(int argc, char **argv) 282 { 283 g_test_init(&argc, &argv, NULL); 284 285 module_call_init(MODULE_INIT_QOM); 286 type_register_static(&static_prop_type); 287 type_register_static(&subclass_type); 288 type_register_static(&dynamic_prop_type); 289 type_register_static(&hotplug_type); 290 type_register_static(&nohotplug_type); 291 type_register_static(&nondevice_type); 292 293 g_test_add_func("/qdev/properties/static/default/subprocess", 294 test_static_prop_subprocess); 295 g_test_add_func("/qdev/properties/static/default", 296 test_static_prop); 297 298 g_test_add_func("/qdev/properties/static/global/subprocess", 299 test_static_globalprop_subprocess); 300 g_test_add_func("/qdev/properties/static/global", 301 test_static_globalprop); 302 303 g_test_add_func("/qdev/properties/dynamic/global/subprocess", 304 test_dynamic_globalprop_subprocess); 305 g_test_add_func("/qdev/properties/dynamic/global", 306 test_dynamic_globalprop); 307 308 g_test_add_func("/qdev/properties/global/subclass", 309 test_subclass_global_props); 310 311 g_test_run(); 312 313 return 0; 314 } 315