1 /*
2  * QOM interface test.
3  *
4  * Copyright (C) 2013 Red Hat Inc.
5  *
6  * Authors:
7  *  Igor Mammedov <imammedo@redhat.com>
8  *
9  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10  * See the COPYING.LIB file in the top-level directory.
11  */
12 #include "qemu/osdep.h"
13 
14 #include "qom/object.h"
15 #include "qemu/module.h"
16 
17 
18 #define TYPE_TEST_IF "test-interface"
19 typedef struct TestIfClass TestIfClass;
20 DECLARE_CLASS_CHECKERS(TestIfClass, TEST_IF,
21                        TYPE_TEST_IF)
22 #define TEST_IF(obj) \
23      INTERFACE_CHECK(TestIf, (obj), TYPE_TEST_IF)
24 
25 typedef struct TestIf TestIf;
26 
27 struct TestIfClass {
28     InterfaceClass parent_class;
29 
30     uint32_t test;
31 };
32 
33 static const TypeInfo test_if_info = {
34     .name          = TYPE_TEST_IF,
35     .parent        = TYPE_INTERFACE,
36     .class_size = sizeof(TestIfClass),
37 };
38 
39 #define PATTERN 0xFAFBFCFD
40 
41 static void test_class_init(ObjectClass *oc, void *data)
42 {
43     TestIfClass *tc = TEST_IF_CLASS(oc);
44 
45     g_assert(tc);
46     tc->test = PATTERN;
47 }
48 
49 #define TYPE_DIRECT_IMPL "direct-impl"
50 
51 static const TypeInfo direct_impl_info = {
52     .name = TYPE_DIRECT_IMPL,
53     .parent = TYPE_OBJECT,
54     .class_init = test_class_init,
55     .interfaces = (InterfaceInfo[]) {
56         { TYPE_TEST_IF },
57         { }
58     }
59 };
60 
61 #define TYPE_INTERMEDIATE_IMPL "intermediate-impl"
62 
63 static const TypeInfo intermediate_impl_info = {
64     .name = TYPE_INTERMEDIATE_IMPL,
65     .parent = TYPE_DIRECT_IMPL,
66 };
67 
68 static void test_interface_impl(const char *type)
69 {
70     Object *obj = object_new(type);
71     TestIf *iobj = TEST_IF(obj);
72     TestIfClass *ioc = TEST_IF_GET_CLASS(iobj);
73 
74     g_assert(iobj);
75     g_assert(ioc->test == PATTERN);
76     object_unref(obj);
77 }
78 
79 static void interface_direct_test(void)
80 {
81     test_interface_impl(TYPE_DIRECT_IMPL);
82 }
83 
84 static void interface_intermediate_test(void)
85 {
86     test_interface_impl(TYPE_INTERMEDIATE_IMPL);
87 }
88 
89 int main(int argc, char **argv)
90 {
91     g_test_init(&argc, &argv, NULL);
92 
93     module_call_init(MODULE_INIT_QOM);
94     type_register_static(&test_if_info);
95     type_register_static(&direct_impl_info);
96     type_register_static(&intermediate_impl_info);
97 
98     g_test_add_func("/qom/interface/direct_impl", interface_direct_test);
99     g_test_add_func("/qom/interface/intermediate_impl",
100                     interface_intermediate_test);
101 
102     return g_test_run();
103 }
104