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 #define TYPE_UNUSED_HOTPLUG "hotplug-type" 117 #define TYPE_UNUSED_NOHOTPLUG "nohotplug-type" 118 119 static void prop1_accessor(Object *obj, 120 Visitor *v, 121 void *opaque, 122 const char *name, 123 Error **errp) 124 { 125 MyType *mt = DYNAMIC_TYPE(obj); 126 127 visit_type_uint32(v, &mt->prop1, name, errp); 128 } 129 130 static void prop2_accessor(Object *obj, 131 Visitor *v, 132 void *opaque, 133 const char *name, 134 Error **errp) 135 { 136 MyType *mt = DYNAMIC_TYPE(obj); 137 138 visit_type_uint32(v, &mt->prop2, name, errp); 139 } 140 141 static void dynamic_instance_init(Object *obj) 142 { 143 object_property_add(obj, "prop1", "uint32", prop1_accessor, prop1_accessor, 144 NULL, NULL, NULL); 145 object_property_add(obj, "prop2", "uint32", prop2_accessor, prop2_accessor, 146 NULL, NULL, NULL); 147 } 148 149 static void dynamic_class_init(ObjectClass *oc, void *data) 150 { 151 DeviceClass *dc = DEVICE_CLASS(oc); 152 153 dc->realize = NULL; 154 } 155 156 157 static const TypeInfo dynamic_prop_type = { 158 .name = TYPE_DYNAMIC_PROPS, 159 .parent = TYPE_DEVICE, 160 .instance_size = sizeof(MyType), 161 .instance_init = dynamic_instance_init, 162 .class_init = dynamic_class_init, 163 }; 164 165 static void hotplug_class_init(ObjectClass *oc, void *data) 166 { 167 DeviceClass *dc = DEVICE_CLASS(oc); 168 169 dc->realize = NULL; 170 dc->hotpluggable = true; 171 } 172 173 static const TypeInfo hotplug_type = { 174 .name = TYPE_UNUSED_HOTPLUG, 175 .parent = TYPE_DEVICE, 176 .instance_size = sizeof(MyType), 177 .instance_init = dynamic_instance_init, 178 .class_init = hotplug_class_init, 179 }; 180 181 static void nohotplug_class_init(ObjectClass *oc, void *data) 182 { 183 DeviceClass *dc = DEVICE_CLASS(oc); 184 185 dc->realize = NULL; 186 dc->hotpluggable = false; 187 } 188 189 static const TypeInfo nohotplug_type = { 190 .name = TYPE_UNUSED_NOHOTPLUG, 191 .parent = TYPE_DEVICE, 192 .instance_size = sizeof(MyType), 193 .instance_init = dynamic_instance_init, 194 .class_init = nohotplug_class_init, 195 }; 196 197 #define TYPE_NONDEVICE "nondevice-type" 198 199 static const TypeInfo nondevice_type = { 200 .name = TYPE_NONDEVICE, 201 .parent = TYPE_OBJECT, 202 }; 203 204 /* Test setting of dynamic properties using global properties */ 205 static void test_dynamic_globalprop_subprocess(void) 206 { 207 MyType *mt; 208 static GlobalProperty props[] = { 209 { TYPE_DYNAMIC_PROPS, "prop1", "101", true }, 210 { TYPE_DYNAMIC_PROPS, "prop2", "102", true }, 211 { TYPE_DYNAMIC_PROPS"-bad", "prop3", "103", true }, 212 /* .not_used=false to emulate what qdev_add_one_global() does: */ 213 { TYPE_UNUSED_HOTPLUG, "prop4", "104", false }, 214 { TYPE_UNUSED_NOHOTPLUG, "prop5", "105", true }, 215 { TYPE_NONDEVICE, "prop6", "106", true }, 216 {} 217 }; 218 int all_used; 219 220 qdev_prop_register_global_list(props); 221 222 mt = DYNAMIC_TYPE(object_new(TYPE_DYNAMIC_PROPS)); 223 qdev_init_nofail(DEVICE(mt)); 224 225 g_assert_cmpuint(mt->prop1, ==, 101); 226 g_assert_cmpuint(mt->prop2, ==, 102); 227 all_used = qdev_prop_check_globals(); 228 g_assert_cmpuint(all_used, ==, 1); 229 g_assert(!props[0].not_used); 230 g_assert(!props[1].not_used); 231 g_assert(props[2].not_used); 232 g_assert(!props[3].not_used); 233 g_assert(props[4].not_used); 234 g_assert(props[5].not_used); 235 } 236 237 static void test_dynamic_globalprop(void) 238 { 239 g_test_trap_subprocess("/qdev/properties/dynamic/global/subprocess", 0, 0); 240 g_test_trap_assert_passed(); 241 g_test_trap_assert_stderr_unmatched("*prop1*"); 242 g_test_trap_assert_stderr_unmatched("*prop2*"); 243 g_test_trap_assert_stderr("*Warning: \"-global dynamic-prop-type-bad.prop3=103\" not used\n*"); 244 g_test_trap_assert_stderr_unmatched("*prop4*"); 245 g_test_trap_assert_stderr("*Warning: \"-global nohotplug-type.prop5=105\" not used\n*"); 246 g_test_trap_assert_stderr("*Warning: \"-global nondevice-type.prop6=106\" not used\n*"); 247 g_test_trap_assert_stdout(""); 248 } 249 250 int main(int argc, char **argv) 251 { 252 g_test_init(&argc, &argv, NULL); 253 254 module_call_init(MODULE_INIT_QOM); 255 type_register_static(&static_prop_type); 256 type_register_static(&dynamic_prop_type); 257 type_register_static(&hotplug_type); 258 type_register_static(&nohotplug_type); 259 type_register_static(&nondevice_type); 260 261 g_test_add_func("/qdev/properties/static/default/subprocess", 262 test_static_prop_subprocess); 263 g_test_add_func("/qdev/properties/static/default", 264 test_static_prop); 265 266 g_test_add_func("/qdev/properties/static/global/subprocess", 267 test_static_globalprop_subprocess); 268 g_test_add_func("/qdev/properties/static/global", 269 test_static_globalprop); 270 271 g_test_add_func("/qdev/properties/dynamic/global/subprocess", 272 test_dynamic_globalprop_subprocess); 273 g_test_add_func("/qdev/properties/dynamic/global", 274 test_dynamic_globalprop); 275 276 g_test_run(); 277 278 return 0; 279 } 280