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