2 * Copyright (C) 2015 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
18 * Author: Daniel P. Berrange <berrange@redhat.com>
23 #include "qom/object.h"
24 #include "qemu/module.h"
27 #define TYPE_DUMMY "qemu-dummy"
29 typedef struct DummyObject DummyObject
;
30 typedef struct DummyObjectClass DummyObjectClass
;
32 #define DUMMY_OBJECT(obj) \
33 OBJECT_CHECK(DummyObject, (obj), TYPE_DUMMY)
35 typedef enum DummyAnimal DummyAnimal
;
45 static const char *const dummy_animal_map
[DUMMY_LAST
+ 1] = {
46 [DUMMY_FROG
] = "frog",
47 [DUMMY_ALLIGATOR
] = "alligator",
48 [DUMMY_PLATYPUS
] = "platypus",
60 struct DummyObjectClass
{
61 ObjectClass parent_class
;
65 static void dummy_set_bv(Object
*obj
,
69 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
74 static bool dummy_get_bv(Object
*obj
,
77 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
83 static void dummy_set_av(Object
*obj
,
87 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
92 static int dummy_get_av(Object
*obj
,
95 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
101 static void dummy_set_sv(Object
*obj
,
105 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
108 dobj
->sv
= g_strdup(value
);
111 static char *dummy_get_sv(Object
*obj
,
114 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
116 return g_strdup(dobj
->sv
);
120 static void dummy_init(Object
*obj
)
122 object_property_add_bool(obj
, "bv",
126 object_property_add_str(obj
, "sv",
130 object_property_add_enum(obj
, "av",
138 static void dummy_finalize(Object
*obj
)
140 DummyObject
*dobj
= DUMMY_OBJECT(obj
);
146 static const TypeInfo dummy_info
= {
148 .parent
= TYPE_OBJECT
,
149 .instance_size
= sizeof(DummyObject
),
150 .instance_init
= dummy_init
,
151 .instance_finalize
= dummy_finalize
,
152 .class_size
= sizeof(DummyObjectClass
),
155 static void test_dummy_createv(void)
158 Object
*parent
= object_get_objects_root();
159 DummyObject
*dobj
= DUMMY_OBJECT(
160 object_new_with_props(TYPE_DUMMY
,
165 "sv", "Hiss hiss hiss",
169 g_assert(err
== NULL
);
170 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
171 g_assert(dobj
->bv
== true);
172 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
174 g_assert(object_resolve_path_component(parent
, "dummy0")
177 object_unparent(OBJECT(dobj
));
181 static Object
*new_helper(Error
**errp
,
188 va_start(vargs
, parent
);
189 obj
= object_new_with_propv(TYPE_DUMMY
,
198 static void test_dummy_createlist(void)
201 Object
*parent
= object_get_objects_root();
202 DummyObject
*dobj
= DUMMY_OBJECT(
206 "sv", "Hiss hiss hiss",
210 g_assert(err
== NULL
);
211 g_assert_cmpstr(dobj
->sv
, ==, "Hiss hiss hiss");
212 g_assert(dobj
->bv
== true);
213 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
215 g_assert(object_resolve_path_component(parent
, "dummy0")
218 object_unparent(OBJECT(dobj
));
221 static void test_dummy_badenum(void)
224 Object
*parent
= object_get_objects_root();
226 object_new_with_props(TYPE_DUMMY
,
231 "sv", "Hiss hiss hiss",
235 g_assert(dobj
== NULL
);
236 g_assert(err
!= NULL
);
237 g_assert_cmpstr(error_get_pretty(err
), ==,
238 "Invalid parameter 'yeti'");
240 g_assert(object_resolve_path_component(parent
, "dummy0")
247 static void test_dummy_getenum(void)
251 Object
*parent
= object_get_objects_root();
252 DummyObject
*dobj
= DUMMY_OBJECT(
253 object_new_with_props(TYPE_DUMMY
,
260 g_assert(err
== NULL
);
261 g_assert(dobj
->av
== DUMMY_PLATYPUS
);
263 val
= object_property_get_enum(OBJECT(dobj
),
267 g_assert(err
== NULL
);
268 g_assert(val
== DUMMY_PLATYPUS
);
270 /* A bad enum type name */
271 val
= object_property_get_enum(OBJECT(dobj
),
275 g_assert(err
!= NULL
);
279 /* A non-enum property name */
280 val
= object_property_get_enum(OBJECT(dobj
),
284 g_assert(err
!= NULL
);
289 int main(int argc
, char **argv
)
291 g_test_init(&argc
, &argv
, NULL
);
293 module_call_init(MODULE_INIT_QOM
);
294 type_register_static(&dummy_info
);
296 g_test_add_func("/qom/proplist/createlist", test_dummy_createlist
);
297 g_test_add_func("/qom/proplist/createv", test_dummy_createv
);
298 g_test_add_func("/qom/proplist/badenum", test_dummy_badenum
);
299 g_test_add_func("/qom/proplist/getenum", test_dummy_getenum
);