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