blob: bab3da49377ef1416737bce8bebd73e96b9fcf4a [file] [log] [blame]
Patrick Williams9d3ec7f2016-09-27 20:30:58 -05001#include <sdbusplus/vtable.hpp>
Patrick Venture95269db2018-08-31 09:19:17 -07002
Patrick Williamsa14167b2017-04-28 15:54:39 -05003#include <gtest/gtest.h>
Patrick Williams9d3ec7f2016-09-27 20:30:58 -05004
Lei YUd35c8522019-12-13 14:48:03 +08005extern "C" {
6int test_handler(sd_bus_message* m, void* userdata, sd_bus_error* ret_error);
7int test_get(sd_bus* bus, const char* path, const char* interface,
8 const char* property, sd_bus_message* reply, void* userdata,
9 sd_bus_error* ret_error);
10int test_set(sd_bus* bus, const char* path, const char* interface,
11 const char* property, sd_bus_message* value, void* userdata,
12 sd_bus_error* ret_error);
Patrick Williams9d3ec7f2016-09-27 20:30:58 -050013
14extern const sd_bus_vtable example2[];
15extern const size_t example2_size;
Lei YUd35c8522019-12-13 14:48:03 +080016}
17
18static const sdbusplus::vtable::vtable_t example[] = {
19 sdbusplus::vtable::start(),
20 sdbusplus::vtable::method("1", "2", "3", &test_handler, 0),
21 sdbusplus::vtable::signal("5", "6"),
22 sdbusplus::vtable::property("7", "8", &test_get,
23 sdbusplus::vtable::property_::const_),
24 sdbusplus::vtable::property("10", "11", &test_get, &test_set),
25 sdbusplus::vtable::property_o("14", "15", 16),
26 sdbusplus::vtable::end()};
Patrick Williams9d3ec7f2016-09-27 20:30:58 -050027
Patrick Williamsa14167b2017-04-28 15:54:39 -050028TEST(VtableTest, SameSize)
Patrick Williams9d3ec7f2016-09-27 20:30:58 -050029{
Patrick Williamsa14167b2017-04-28 15:54:39 -050030 ASSERT_EQ(sizeof(example), example2_size);
31}
Patrick Williams9d3ec7f2016-09-27 20:30:58 -050032
Lei YUd35c8522019-12-13 14:48:03 +080033constexpr bool operator==(const sd_bus_vtable& t1, const sd_bus_vtable& t2)
34{
35 if (t1.type != t2.type || t1.flags != t2.flags)
36 {
37 return false;
38 }
39
40 switch (t1.type)
41 {
42 case _SD_BUS_VTABLE_START:
43 return t1.x.start.element_size == t2.x.start.element_size &&
44 t1.x.start.features == t2.x.start.features;
45 // FIXME: In systemd 243, there is the new vtable_format_reference
46 // member, but current CI is using libsystemd 242, so we can not
47 // check it yet.
48 // && t1.x.start.vtable_format_reference
49 // == t2.x.start.vtable_format_reference;
50 break;
51 case _SD_BUS_VTABLE_END:
52 {
53 // The union x shall be all zeros for END
54 constexpr uint8_t allZeors[sizeof(t1.x)] = {0};
55 return memcmp(&t1.x, allZeors, sizeof(t1.x)) == 0 &&
56 memcmp(&t2.x, allZeors, sizeof(t2.x)) == 0;
57 break;
58 }
59 case _SD_BUS_VTABLE_METHOD:
60 return strcmp(t1.x.method.member, t2.x.method.member) == 0 &&
61 strcmp(t1.x.method.signature, t2.x.method.signature) == 0 &&
62 strcmp(t1.x.method.result, t2.x.method.result) == 0 &&
63 t1.x.method.handler == t2.x.method.handler &&
64 t1.x.method.offset == t2.x.method.offset &&
65 strcmp(t1.x.method.names, t2.x.method.names) == 0;
66 break;
67 case _SD_BUS_VTABLE_SIGNAL:
68 return strcmp(t1.x.signal.member, t2.x.signal.member) == 0 &&
69 strcmp(t1.x.signal.signature, t2.x.signal.signature) == 0 &&
70 strcmp(t1.x.signal.names, t2.x.signal.names) == 0;
71 break;
72 case _SD_BUS_VTABLE_PROPERTY:
73 case _SD_BUS_VTABLE_WRITABLE_PROPERTY:
74 return strcmp(t1.x.property.member, t2.x.property.member) == 0 &&
75 strcmp(t1.x.property.signature, t2.x.property.signature) ==
76 0 &&
77 t1.x.property.get == t2.x.property.get &&
78 t1.x.property.set == t2.x.property.set &&
79 t1.x.property.offset == t2.x.property.offset;
80 break;
81 }
82 return false;
83}
84
Patrick Williamsa14167b2017-04-28 15:54:39 -050085TEST(VtableTest, SameContent)
86{
Lei YUd35c8522019-12-13 14:48:03 +080087 for (size_t i = 0; i < sizeof(example) / sizeof(example[0]); ++i)
88 {
89 EXPECT_EQ(example[i], example2[i]);
90 }
Patrick Williamsa14167b2017-04-28 15:54:39 -050091}