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