]> git.proxmox.com Git - mirror_qemu.git/blame - tests/test-qdev-global-props.c
hw/machine: Free old values of string properties
[mirror_qemu.git] / tests / test-qdev-global-props.c
CommitLineData
747b0cb4
EH
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"
99a0b036
EH
29#include "qom/object.h"
30#include "qapi/visitor.h"
747b0cb4
EH
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
39typedef struct MyType {
40 DeviceState parent_obj;
41
42 uint32_t prop1;
43 uint32_t prop2;
44} MyType;
45
46static 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
52static 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
60static 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 */
68static void test_static_prop(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/* Test setting of static property using global properties */
79static void test_static_globalprop(void)
80{
81 MyType *mt;
82 static GlobalProperty props[] = {
83 { TYPE_STATIC_PROPS, "prop1", "200" },
84 {}
85 };
86
87 qdev_prop_register_global_list(props);
88
89 mt = STATIC_TYPE(object_new(TYPE_STATIC_PROPS));
90 qdev_init_nofail(DEVICE(mt));
91
92 g_assert_cmpuint(mt->prop1, ==, 200);
93 g_assert_cmpuint(mt->prop2, ==, PROP_DEFAULT);
94}
95
99a0b036
EH
96#define TYPE_DYNAMIC_PROPS "dynamic-prop-type"
97#define DYNAMIC_TYPE(obj) \
98 OBJECT_CHECK(MyType, (obj), TYPE_DYNAMIC_PROPS)
99
100static void prop1_accessor(Object *obj,
101 Visitor *v,
102 void *opaque,
103 const char *name,
104 Error **errp)
105{
106 MyType *mt = DYNAMIC_TYPE(obj);
107
108 visit_type_uint32(v, &mt->prop1, name, errp);
109}
110
111static void prop2_accessor(Object *obj,
112 Visitor *v,
113 void *opaque,
114 const char *name,
115 Error **errp)
116{
117 MyType *mt = DYNAMIC_TYPE(obj);
118
119 visit_type_uint32(v, &mt->prop2, name, errp);
120}
121
122static void dynamic_instance_init(Object *obj)
123{
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);
128}
129
130static void dynamic_class_init(ObjectClass *oc, void *data)
131{
132 DeviceClass *dc = DEVICE_CLASS(oc);
133
134 dc->realize = NULL;
135}
136
137
138static 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,
144};
145
146/* Test setting of static property using global properties */
147static void test_dynamic_globalprop(void)
148{
149 MyType *mt;
150 static GlobalProperty props[] = {
151 { TYPE_DYNAMIC_PROPS, "prop1", "101" },
152 { TYPE_DYNAMIC_PROPS, "prop2", "102" },
711e2f1e 153 { TYPE_DYNAMIC_PROPS"-bad", "prop3", "103", true },
99a0b036
EH
154 {}
155 };
711e2f1e 156 int all_used;
99a0b036
EH
157
158 qdev_prop_register_global_list(props);
159
160 mt = DYNAMIC_TYPE(object_new(TYPE_DYNAMIC_PROPS));
161 qdev_init_nofail(DEVICE(mt));
162
163 g_assert_cmpuint(mt->prop1, ==, 101);
164 g_assert_cmpuint(mt->prop2, ==, 102);
711e2f1e
DS
165 all_used = qdev_prop_check_global();
166 g_assert_cmpuint(all_used, ==, 1);
99a0b036
EH
167}
168
747b0cb4
EH
169int main(int argc, char **argv)
170{
171 g_test_init(&argc, &argv, NULL);
172
173 module_call_init(MODULE_INIT_QOM);
174 type_register_static(&static_prop_type);
99a0b036 175 type_register_static(&dynamic_prop_type);
747b0cb4
EH
176
177 g_test_add_func("/qdev/properties/static/default", test_static_prop);
178 g_test_add_func("/qdev/properties/static/global", test_static_globalprop);
99a0b036 179 g_test_add_func("/qdev/properties/dynamic/global", test_dynamic_globalprop);
747b0cb4
EH
180
181 g_test_run();
182
183 return 0;
184}