| 1 | #include "qemu/osdep.h" |
| 2 | #include "hw/core/qdev-properties.h" |
| 3 | #include "qom/object.h" |
| 4 | #include "qapi/error.h" |
| 5 | #include "qapi/visitor.h" |
| 6 | |
| 7 | |
| 8 | #define TYPE_MY_DEV "my-dev" |
| 9 | typedef struct MyDev MyDev; |
| 10 | DECLARE_INSTANCE_CHECKER(MyDev, STATIC_TYPE, |
| 11 | TYPE_MY_DEV) |
| 12 | |
| 13 | struct MyDev { |
| 14 | DeviceState parent_obj; |
| 15 | |
| 16 | uint32_t prop_u32; |
| 17 | char *prop_string; |
| 18 | uint32_t *prop_array_u32; |
| 19 | uint32_t prop_array_u32_nb; |
| 20 | }; |
| 21 | |
| 22 | static const Property my_dev_props[] = { |
| 23 | DEFINE_PROP_UINT32("u32", MyDev, prop_u32, 100), |
| 24 | DEFINE_PROP_STRING("string", MyDev, prop_string), |
| 25 | DEFINE_PROP_ARRAY("array-u32", MyDev, prop_array_u32_nb, prop_array_u32, |
| 26 | qdev_prop_uint32, uint32_t), |
| 27 | }; |
| 28 | |
| 29 | static void my_dev_class_init(ObjectClass *oc, const void *data) |
| 30 | { |
| 31 | DeviceClass *dc = DEVICE_CLASS(oc); |
| 32 | |
| 33 | dc->realize = NULL; |
| 34 | device_class_set_props(dc, my_dev_props); |
| 35 | } |
| 36 | |
| 37 | static const TypeInfo my_dev_type_info = { |
| 38 | .name = TYPE_MY_DEV, |
| 39 | .parent = TYPE_DEVICE, |
| 40 | .instance_size = sizeof(MyDev), |
| 41 | .class_init = my_dev_class_init, |
| 42 | }; |
| 43 | |
| 44 | /* |
| 45 | * Initialize a fake machine, being prepared for future tests. |
| 46 | * |
| 47 | * Realization of anonymous qdev (with no parent object) requires both |
| 48 | * the machine object and its "unattached" container to be at least present. |
| 49 | */ |
| 50 | static void test_init_machine(void) |
| 51 | { |
| 52 | /* This is a fake machine - it doesn't need to be a machine object */ |
| 53 | Object *machine = object_property_add_new_container( |
| 54 | object_get_root(), "machine"); |
| 55 | |
| 56 | /* This container must exist for anonymous qdevs to realize() */ |
| 57 | object_property_add_new_container(machine, "unattached"); |
| 58 | } |
| 59 | |
| 60 | static void test_qdev_free_properties(void) |
| 61 | { |
| 62 | MyDev *mt; |
| 63 | |
| 64 | mt = STATIC_TYPE(object_new(TYPE_MY_DEV)); |
| 65 | object_set_props(OBJECT(mt), &error_fatal, |
| 66 | "string", "something", |
| 67 | "array-u32", "12,13", |
| 68 | NULL); |
| 69 | qdev_realize(DEVICE(mt), NULL, &error_fatal); |
| 70 | |
| 71 | g_assert_cmpuint(mt->prop_u32, ==, 100); |
| 72 | g_assert_cmpstr(mt->prop_string, ==, "something"); |
| 73 | g_assert_cmpuint(mt->prop_array_u32_nb, ==, 2); |
| 74 | g_assert_cmpuint(mt->prop_array_u32[0], ==, 12); |
| 75 | g_assert_cmpuint(mt->prop_array_u32[1], ==, 13); |
| 76 | |
| 77 | object_unparent(OBJECT(mt)); |
| 78 | object_unref(mt); |
| 79 | } |
| 80 | |
| 81 | static void test_qdev_double_realization(void) |
| 82 | { |
| 83 | MyDev *mt = STATIC_TYPE(object_new(TYPE_MY_DEV)); |
| 84 | |
| 85 | qdev_realize(DEVICE(mt), NULL, &error_fatal); |
| 86 | qdev_realize(DEVICE(mt), NULL, &error_fatal); |
| 87 | object_unparent(OBJECT(mt)); |
| 88 | object_unref(OBJECT(mt)); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | int main(int argc, char **argv) |
| 93 | { |
| 94 | g_test_init(&argc, &argv, NULL); |
| 95 | |
| 96 | module_call_init(MODULE_INIT_QOM); |
| 97 | type_register_static(&my_dev_type_info); |
| 98 | test_init_machine(); |
| 99 | |
| 100 | g_test_add_func("/qdev/free-properties", |
| 101 | test_qdev_free_properties); |
| 102 | |
| 103 | g_test_add_func("/qdev/double-realization", |
| 104 | test_qdev_double_realization); |
| 105 | |
| 106 | g_test_run(); |
| 107 | |
| 108 | return 0; |
| 109 | } |