qemu/tests/unit/test-qdev-global-props.c
<<
>>
Prefs
   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 "qemu/osdep.h"
  26
  27#include "hw/qdev-properties.h"
  28#include "qom/object.h"
  29#include "qapi/error.h"
  30#include "qapi/visitor.h"
  31
  32
  33#define TYPE_STATIC_PROPS "static_prop_type"
  34typedef struct MyType MyType;
  35DECLARE_INSTANCE_CHECKER(MyType, STATIC_TYPE,
  36                         TYPE_STATIC_PROPS)
  37
  38#define TYPE_SUBCLASS "static_prop_subtype"
  39
  40#define PROP_DEFAULT 100
  41
  42struct MyType {
  43    DeviceState parent_obj;
  44
  45    uint32_t prop1;
  46    uint32_t prop2;
  47};
  48
  49static Property static_props[] = {
  50    DEFINE_PROP_UINT32("prop1", MyType, prop1, PROP_DEFAULT),
  51    DEFINE_PROP_UINT32("prop2", MyType, prop2, PROP_DEFAULT),
  52    DEFINE_PROP_END_OF_LIST()
  53};
  54
  55static void static_prop_class_init(ObjectClass *oc, void *data)
  56{
  57    DeviceClass *dc = DEVICE_CLASS(oc);
  58
  59    dc->realize = NULL;
  60    device_class_set_props(dc, static_props);
  61}
  62
  63static const TypeInfo static_prop_type = {
  64    .name = TYPE_STATIC_PROPS,
  65    .parent = TYPE_DEVICE,
  66    .instance_size = sizeof(MyType),
  67    .class_init = static_prop_class_init,
  68};
  69
  70static const TypeInfo subclass_type = {
  71    .name = TYPE_SUBCLASS,
  72    .parent = TYPE_STATIC_PROPS,
  73};
  74
  75/* Test simple static property setting to default value */
  76static void test_static_prop_subprocess(void)
  77{
  78    MyType *mt;
  79
  80    mt = STATIC_TYPE(object_new(TYPE_STATIC_PROPS));
  81    qdev_realize(DEVICE(mt), NULL, &error_fatal);
  82
  83    g_assert_cmpuint(mt->prop1, ==, PROP_DEFAULT);
  84}
  85
  86static void test_static_prop(void)
  87{
  88    g_test_trap_subprocess("/qdev/properties/static/default/subprocess", 0, 0);
  89    g_test_trap_assert_passed();
  90    g_test_trap_assert_stderr("");
  91    g_test_trap_assert_stdout("");
  92}
  93
  94static void register_global_properties(GlobalProperty *props)
  95{
  96    int i;
  97
  98    for (i = 0; props[i].driver != NULL; i++) {
  99        qdev_prop_register_global(props + i);
 100    }
 101}
 102
 103
 104/* Test setting of static property using global properties */
 105static void test_static_globalprop_subprocess(void)
 106{
 107    MyType *mt;
 108    static GlobalProperty props[] = {
 109        { TYPE_STATIC_PROPS, "prop1", "200" },
 110        {}
 111    };
 112
 113    register_global_properties(props);
 114
 115    mt = STATIC_TYPE(object_new(TYPE_STATIC_PROPS));
 116    qdev_realize(DEVICE(mt), NULL, &error_fatal);
 117
 118    g_assert_cmpuint(mt->prop1, ==, 200);
 119    g_assert_cmpuint(mt->prop2, ==, PROP_DEFAULT);
 120}
 121
 122static void test_static_globalprop(void)
 123{
 124    g_test_trap_subprocess("/qdev/properties/static/global/subprocess", 0, 0);
 125    g_test_trap_assert_passed();
 126    g_test_trap_assert_stderr("");
 127    g_test_trap_assert_stdout("");
 128}
 129
 130#define TYPE_DYNAMIC_PROPS "dynamic-prop-type"
 131DECLARE_INSTANCE_CHECKER(MyType, DYNAMIC_TYPE,
 132                         TYPE_DYNAMIC_PROPS)
 133
 134#define TYPE_UNUSED_HOTPLUG   "hotplug-type"
 135#define TYPE_UNUSED_NOHOTPLUG "nohotplug-type"
 136
 137static void prop1_accessor(Object *obj, Visitor *v, const char *name,
 138                           void *opaque, Error **errp)
 139{
 140    MyType *mt = DYNAMIC_TYPE(obj);
 141
 142    visit_type_uint32(v, name, &mt->prop1, errp);
 143}
 144
 145static void prop2_accessor(Object *obj, Visitor *v, const char *name,
 146                           void *opaque, Error **errp)
 147{
 148    MyType *mt = DYNAMIC_TYPE(obj);
 149
 150    visit_type_uint32(v, name, &mt->prop2, errp);
 151}
 152
 153static void dynamic_instance_init(Object *obj)
 154{
 155    object_property_add(obj, "prop1", "uint32", prop1_accessor, prop1_accessor,
 156                        NULL, NULL);
 157    object_property_add(obj, "prop2", "uint32", prop2_accessor, prop2_accessor,
 158                        NULL, NULL);
 159}
 160
 161static void dynamic_class_init(ObjectClass *oc, void *data)
 162{
 163    DeviceClass *dc = DEVICE_CLASS(oc);
 164
 165    dc->realize = NULL;
 166}
 167
 168
 169static const TypeInfo dynamic_prop_type = {
 170    .name = TYPE_DYNAMIC_PROPS,
 171    .parent = TYPE_DEVICE,
 172    .instance_size = sizeof(MyType),
 173    .instance_init = dynamic_instance_init,
 174    .class_init = dynamic_class_init,
 175};
 176
 177static void hotplug_class_init(ObjectClass *oc, void *data)
 178{
 179    DeviceClass *dc = DEVICE_CLASS(oc);
 180
 181    dc->realize = NULL;
 182    dc->hotpluggable = true;
 183}
 184
 185static const TypeInfo hotplug_type = {
 186    .name = TYPE_UNUSED_HOTPLUG,
 187    .parent = TYPE_DEVICE,
 188    .instance_size = sizeof(MyType),
 189    .instance_init = dynamic_instance_init,
 190    .class_init = hotplug_class_init,
 191};
 192
 193static void nohotplug_class_init(ObjectClass *oc, void *data)
 194{
 195    DeviceClass *dc = DEVICE_CLASS(oc);
 196
 197    dc->realize = NULL;
 198    dc->hotpluggable = false;
 199}
 200
 201static const TypeInfo nohotplug_type = {
 202    .name = TYPE_UNUSED_NOHOTPLUG,
 203    .parent = TYPE_DEVICE,
 204    .instance_size = sizeof(MyType),
 205    .instance_init = dynamic_instance_init,
 206    .class_init = nohotplug_class_init,
 207};
 208
 209#define TYPE_NONDEVICE "nondevice-type"
 210
 211static const TypeInfo nondevice_type = {
 212    .name = TYPE_NONDEVICE,
 213    .parent = TYPE_OBJECT,
 214};
 215
 216/* Test setting of dynamic properties using global properties */
 217static void test_dynamic_globalprop_subprocess(void)
 218{
 219    MyType *mt;
 220    static GlobalProperty props[] = {
 221        { TYPE_DYNAMIC_PROPS, "prop1", "101", },
 222        { TYPE_DYNAMIC_PROPS, "prop2", "102", },
 223        { TYPE_DYNAMIC_PROPS"-bad", "prop3", "103", },
 224        { TYPE_UNUSED_HOTPLUG, "prop4", "104", },
 225        { TYPE_UNUSED_NOHOTPLUG, "prop5", "105", },
 226        { TYPE_NONDEVICE, "prop6", "106", },
 227        {}
 228    };
 229    int global_error;
 230
 231    register_global_properties(props);
 232
 233    mt = DYNAMIC_TYPE(object_new(TYPE_DYNAMIC_PROPS));
 234    qdev_realize(DEVICE(mt), NULL, &error_fatal);
 235
 236    g_assert_cmpuint(mt->prop1, ==, 101);
 237    g_assert_cmpuint(mt->prop2, ==, 102);
 238    global_error = qdev_prop_check_globals();
 239    g_assert_cmpuint(global_error, ==, 1);
 240    g_assert(props[0].used);
 241    g_assert(props[1].used);
 242    g_assert(!props[2].used);
 243    g_assert(!props[3].used);
 244    g_assert(!props[4].used);
 245    g_assert(!props[5].used);
 246}
 247
 248static void test_dynamic_globalprop(void)
 249{
 250    g_test_trap_subprocess("/qdev/properties/dynamic/global/subprocess", 0, 0);
 251    g_test_trap_assert_passed();
 252    g_test_trap_assert_stderr_unmatched("*prop1*");
 253    g_test_trap_assert_stderr_unmatched("*prop2*");
 254    g_test_trap_assert_stderr(
 255        "*warning: global dynamic-prop-type-bad.prop3 has invalid class name*");
 256    g_test_trap_assert_stderr_unmatched("*prop4*");
 257    g_test_trap_assert_stderr(
 258        "*warning: global nohotplug-type.prop5=105 not used*");
 259    g_test_trap_assert_stderr(
 260        "*warning: global nondevice-type.prop6 has invalid class name*");
 261    g_test_trap_assert_stdout("");
 262}
 263
 264/* Test if global props affecting subclasses are applied in the right order */
 265static void test_subclass_global_props(void)
 266{
 267    MyType *mt;
 268    /* Global properties must be applied in the order they were registered */
 269    static GlobalProperty props[] = {
 270        { TYPE_STATIC_PROPS, "prop1", "101" },
 271        { TYPE_SUBCLASS,     "prop1", "102" },
 272        { TYPE_SUBCLASS,     "prop2", "103" },
 273        { TYPE_STATIC_PROPS, "prop2", "104" },
 274        {}
 275    };
 276
 277    register_global_properties(props);
 278
 279    mt = STATIC_TYPE(object_new(TYPE_SUBCLASS));
 280    qdev_realize(DEVICE(mt), NULL, &error_fatal);
 281
 282    g_assert_cmpuint(mt->prop1, ==, 102);
 283    g_assert_cmpuint(mt->prop2, ==, 104);
 284}
 285
 286int main(int argc, char **argv)
 287{
 288    g_test_init(&argc, &argv, NULL);
 289
 290    module_call_init(MODULE_INIT_QOM);
 291    type_register_static(&static_prop_type);
 292    type_register_static(&subclass_type);
 293    type_register_static(&dynamic_prop_type);
 294    type_register_static(&hotplug_type);
 295    type_register_static(&nohotplug_type);
 296    type_register_static(&nondevice_type);
 297
 298    g_test_add_func("/qdev/properties/static/default/subprocess",
 299                    test_static_prop_subprocess);
 300    g_test_add_func("/qdev/properties/static/default",
 301                    test_static_prop);
 302
 303    g_test_add_func("/qdev/properties/static/global/subprocess",
 304                    test_static_globalprop_subprocess);
 305    g_test_add_func("/qdev/properties/static/global",
 306                    test_static_globalprop);
 307
 308    g_test_add_func("/qdev/properties/dynamic/global/subprocess",
 309                    test_dynamic_globalprop_subprocess);
 310    g_test_add_func("/qdev/properties/dynamic/global",
 311                    test_dynamic_globalprop);
 312
 313    g_test_add_func("/qdev/properties/global/subclass",
 314                    test_subclass_global_props);
 315
 316    g_test_run();
 317
 318    return 0;
 319}
 320