blob: 092f5cf76b43097b978211db4b729163abb4e487 [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
Patrick Williams127b8ab2020-05-21 15:24:19 -05005extern "C"
6{
Patrick Williams6db88382023-10-20 11:18:17 -05007int test_handler(sd_bus_message* m, void* userdata, sd_bus_error* ret_error);
8int test_get(sd_bus* bus, const char* path, const char* interface,
9 const char* property, sd_bus_message* reply, void* userdata,
10 sd_bus_error* ret_error);
11int test_set(sd_bus* bus, const char* path, const char* interface,
12 const char* property, sd_bus_message* value, void* userdata,
13 sd_bus_error* ret_error);
Patrick Williams9d3ec7f2016-09-27 20:30:58 -050014
Patrick Williams6db88382023-10-20 11:18:17 -050015extern const sd_bus_vtable example2[];
16extern const size_t example2_size;
Lei YUd35c8522019-12-13 14:48:03 +080017}
18
Patrick Williamsa735ca52021-11-19 11:36:18 -060019static const sdbusplus::vtable_t example[] = {
Lei YUd35c8522019-12-13 14:48:03 +080020 sdbusplus::vtable::start(),
21 sdbusplus::vtable::method("1", "2", "3", &test_handler, 0),
22 sdbusplus::vtable::signal("5", "6"),
23 sdbusplus::vtable::property("7", "8", &test_get,
24 sdbusplus::vtable::property_::const_),
25 sdbusplus::vtable::property("10", "11", &test_get, &test_set),
Ed Tanousda48d202023-03-08 09:49:55 -080026 sdbusplus::vtable::property_o("14", "15", &test_get, 16),
Lei YUd35c8522019-12-13 14:48:03 +080027 sdbusplus::vtable::end()};
Patrick Williams9d3ec7f2016-09-27 20:30:58 -050028
Patrick Williamsa14167b2017-04-28 15:54:39 -050029TEST(VtableTest, SameSize)
Patrick Williams9d3ec7f2016-09-27 20:30:58 -050030{
Patrick Williamsa14167b2017-04-28 15:54:39 -050031 ASSERT_EQ(sizeof(example), example2_size);
32}
Patrick Williams9d3ec7f2016-09-27 20:30:58 -050033
Lei YUd35c8522019-12-13 14:48:03 +080034constexpr bool operator==(const sd_bus_vtable& t1, const sd_bus_vtable& t2)
35{
36 if (t1.type != t2.type || t1.flags != t2.flags)
37 {
38 return false;
39 }
40
41 switch (t1.type)
42 {
43 case _SD_BUS_VTABLE_START:
44 return t1.x.start.element_size == t2.x.start.element_size &&
45 t1.x.start.features == t2.x.start.features;
46 // FIXME: In systemd 243, there is the new vtable_format_reference
47 // member, but current CI is using libsystemd 242, so we can not
48 // check it yet.
49 // && t1.x.start.vtable_format_reference
50 // == t2.x.start.vtable_format_reference;
Lei YUd35c8522019-12-13 14:48:03 +080051 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;
Lei YUd35c8522019-12-13 14:48:03 +080057 }
58 case _SD_BUS_VTABLE_METHOD:
59 return strcmp(t1.x.method.member, t2.x.method.member) == 0 &&
60 strcmp(t1.x.method.signature, t2.x.method.signature) == 0 &&
61 strcmp(t1.x.method.result, t2.x.method.result) == 0 &&
62 t1.x.method.handler == t2.x.method.handler &&
63 t1.x.method.offset == t2.x.method.offset &&
64 strcmp(t1.x.method.names, t2.x.method.names) == 0;
Lei YUd35c8522019-12-13 14:48:03 +080065 case _SD_BUS_VTABLE_SIGNAL:
66 return strcmp(t1.x.signal.member, t2.x.signal.member) == 0 &&
67 strcmp(t1.x.signal.signature, t2.x.signal.signature) == 0 &&
68 strcmp(t1.x.signal.names, t2.x.signal.names) == 0;
Lei YUd35c8522019-12-13 14:48:03 +080069 case _SD_BUS_VTABLE_PROPERTY:
70 case _SD_BUS_VTABLE_WRITABLE_PROPERTY:
71 return strcmp(t1.x.property.member, t2.x.property.member) == 0 &&
72 strcmp(t1.x.property.signature, t2.x.property.signature) ==
73 0 &&
74 t1.x.property.get == t2.x.property.get &&
75 t1.x.property.set == t2.x.property.set &&
76 t1.x.property.offset == t2.x.property.offset;
Lei YUd35c8522019-12-13 14:48:03 +080077 }
78 return false;
79}
80
Patrick Williamsa14167b2017-04-28 15:54:39 -050081TEST(VtableTest, SameContent)
82{
Lei YUd35c8522019-12-13 14:48:03 +080083 for (size_t i = 0; i < sizeof(example) / sizeof(example[0]); ++i)
84 {
85 EXPECT_EQ(example[i], example2[i]);
86 }
Patrick Williamsa14167b2017-04-28 15:54:39 -050087}