spapr_pci_vfio: Add spapr-pci-vfio-host-bridge to support vfio
[qemu-kvm.git] / tests / test-qdev-global-props.c
blob2bef04c76f9556bd335ce6caacc8ff35aeb49844
1 /*
2 * Test code for qdev global-properties handling
4 * Copyright (c) 2012 Red Hat Inc.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include <glib.h>
26 #include <stdint.h>
28 #include "hw/qdev.h"
29 #include "qom/object.h"
30 #include "qapi/visitor.h"
33 #define TYPE_STATIC_PROPS "static_prop_type"
34 #define STATIC_TYPE(obj) \
35 OBJECT_CHECK(MyType, (obj), TYPE_STATIC_PROPS)
37 #define PROP_DEFAULT 100
39 typedef struct MyType {
40 DeviceState parent_obj;
42 uint32_t prop1;
43 uint32_t prop2;
44 } MyType;
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()
52 static void static_prop_class_init(ObjectClass *oc, void *data)
54 DeviceClass *dc = DEVICE_CLASS(oc);
56 dc->realize = NULL;
57 dc->props = static_props;
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,
67 /* Test simple static property setting to default value */
68 static void test_static_prop(void)
70 MyType *mt;
72 mt = STATIC_TYPE(object_new(TYPE_STATIC_PROPS));
73 qdev_init_nofail(DEVICE(mt));
75 g_assert_cmpuint(mt->prop1, ==, PROP_DEFAULT);
78 /* Test setting of static property using global properties */
79 static void test_static_globalprop(void)
81 MyType *mt;
82 static GlobalProperty props[] = {
83 { TYPE_STATIC_PROPS, "prop1", "200" },
87 qdev_prop_register_global_list(props);
89 mt = STATIC_TYPE(object_new(TYPE_STATIC_PROPS));
90 qdev_init_nofail(DEVICE(mt));
92 g_assert_cmpuint(mt->prop1, ==, 200);
93 g_assert_cmpuint(mt->prop2, ==, PROP_DEFAULT);
96 #define TYPE_DYNAMIC_PROPS "dynamic-prop-type"
97 #define DYNAMIC_TYPE(obj) \
98 OBJECT_CHECK(MyType, (obj), TYPE_DYNAMIC_PROPS)
100 static void prop1_accessor(Object *obj,
101 Visitor *v,
102 void *opaque,
103 const char *name,
104 Error **errp)
106 MyType *mt = DYNAMIC_TYPE(obj);
108 visit_type_uint32(v, &mt->prop1, name, errp);
111 static void prop2_accessor(Object *obj,
112 Visitor *v,
113 void *opaque,
114 const char *name,
115 Error **errp)
117 MyType *mt = DYNAMIC_TYPE(obj);
119 visit_type_uint32(v, &mt->prop2, name, errp);
122 static void dynamic_instance_init(Object *obj)
124 object_property_add(obj, "prop1", "uint32", prop1_accessor, prop1_accessor,
125 NULL, NULL, NULL);
126 object_property_add(obj, "prop2", "uint32", prop2_accessor, prop2_accessor,
127 NULL, NULL, NULL);
130 static void dynamic_class_init(ObjectClass *oc, void *data)
132 DeviceClass *dc = DEVICE_CLASS(oc);
134 dc->realize = NULL;
138 static const TypeInfo dynamic_prop_type = {
139 .name = TYPE_DYNAMIC_PROPS,
140 .parent = TYPE_DEVICE,
141 .instance_size = sizeof(MyType),
142 .instance_init = dynamic_instance_init,
143 .class_init = dynamic_class_init,
146 /* Test setting of static property using global properties */
147 static void test_dynamic_globalprop(void)
149 MyType *mt;
150 static GlobalProperty props[] = {
151 { TYPE_DYNAMIC_PROPS, "prop1", "101" },
152 { TYPE_DYNAMIC_PROPS, "prop2", "102" },
153 { TYPE_DYNAMIC_PROPS"-bad", "prop3", "103", true },
156 int all_used;
158 qdev_prop_register_global_list(props);
160 mt = DYNAMIC_TYPE(object_new(TYPE_DYNAMIC_PROPS));
161 qdev_init_nofail(DEVICE(mt));
163 g_assert_cmpuint(mt->prop1, ==, 101);
164 g_assert_cmpuint(mt->prop2, ==, 102);
165 all_used = qdev_prop_check_global();
166 g_assert_cmpuint(all_used, ==, 1);
169 int main(int argc, char **argv)
171 g_test_init(&argc, &argv, NULL);
173 module_call_init(MODULE_INIT_QOM);
174 type_register_static(&static_prop_type);
175 type_register_static(&dynamic_prop_type);
177 g_test_add_func("/qdev/properties/static/default", test_static_prop);
178 g_test_add_func("/qdev/properties/static/global", test_static_globalprop);
179 g_test_add_func("/qdev/properties/dynamic/global", test_dynamic_globalprop);
181 g_test_run();
183 return 0;