| Patrick Williams | 9d3ec7f | 2016-09-27 20:30:58 -0500 | [diff] [blame] | 1 | #include <sdbusplus/vtable.hpp> | 
| Patrick Venture | 95269db | 2018-08-31 09:19:17 -0700 | [diff] [blame] | 2 |  | 
| Patrick Williams | a14167b | 2017-04-28 15:54:39 -0500 | [diff] [blame] | 3 | #include <gtest/gtest.h> | 
| Patrick Williams | 9d3ec7f | 2016-09-27 20:30:58 -0500 | [diff] [blame] | 4 |  | 
| Patrick Williams | 127b8ab | 2020-05-21 15:24:19 -0500 | [diff] [blame] | 5 | extern "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 Williams | 9d3ec7f | 2016-09-27 20:30:58 -0500 | [diff] [blame] | 15 |  | 
| Patrick Williams | 127b8ab | 2020-05-21 15:24:19 -0500 | [diff] [blame] | 16 | extern const sd_bus_vtable example2[]; | 
|  | 17 | extern const size_t example2_size; | 
| Lei YU | d35c852 | 2019-12-13 14:48:03 +0800 | [diff] [blame] | 18 | } | 
|  | 19 |  | 
| Patrick Williams | a735ca5 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 20 | static const sdbusplus::vtable_t example[] = { | 
| Lei YU | d35c852 | 2019-12-13 14:48:03 +0800 | [diff] [blame] | 21 | 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 Williams | 9d3ec7f | 2016-09-27 20:30:58 -0500 | [diff] [blame] | 29 |  | 
| Patrick Williams | a14167b | 2017-04-28 15:54:39 -0500 | [diff] [blame] | 30 | TEST(VtableTest, SameSize) | 
| Patrick Williams | 9d3ec7f | 2016-09-27 20:30:58 -0500 | [diff] [blame] | 31 | { | 
| Patrick Williams | a14167b | 2017-04-28 15:54:39 -0500 | [diff] [blame] | 32 | ASSERT_EQ(sizeof(example), example2_size); | 
|  | 33 | } | 
| Patrick Williams | 9d3ec7f | 2016-09-27 20:30:58 -0500 | [diff] [blame] | 34 |  | 
| Lei YU | d35c852 | 2019-12-13 14:48:03 +0800 | [diff] [blame] | 35 | constexpr 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; | 
| Lei YU | d35c852 | 2019-12-13 14:48:03 +0800 | [diff] [blame] | 52 | case _SD_BUS_VTABLE_END: | 
|  | 53 | { | 
|  | 54 | // The union x shall be all zeros for END | 
|  | 55 | constexpr uint8_t allZeors[sizeof(t1.x)] = {0}; | 
|  | 56 | return memcmp(&t1.x, allZeors, sizeof(t1.x)) == 0 && | 
|  | 57 | memcmp(&t2.x, allZeors, sizeof(t2.x)) == 0; | 
| Lei YU | d35c852 | 2019-12-13 14:48:03 +0800 | [diff] [blame] | 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; | 
| Lei YU | d35c852 | 2019-12-13 14:48:03 +0800 | [diff] [blame] | 66 | case _SD_BUS_VTABLE_SIGNAL: | 
|  | 67 | return strcmp(t1.x.signal.member, t2.x.signal.member) == 0 && | 
|  | 68 | strcmp(t1.x.signal.signature, t2.x.signal.signature) == 0 && | 
|  | 69 | strcmp(t1.x.signal.names, t2.x.signal.names) == 0; | 
| Lei YU | d35c852 | 2019-12-13 14:48:03 +0800 | [diff] [blame] | 70 | case _SD_BUS_VTABLE_PROPERTY: | 
|  | 71 | case _SD_BUS_VTABLE_WRITABLE_PROPERTY: | 
|  | 72 | return strcmp(t1.x.property.member, t2.x.property.member) == 0 && | 
|  | 73 | strcmp(t1.x.property.signature, t2.x.property.signature) == | 
|  | 74 | 0 && | 
|  | 75 | t1.x.property.get == t2.x.property.get && | 
|  | 76 | t1.x.property.set == t2.x.property.set && | 
|  | 77 | t1.x.property.offset == t2.x.property.offset; | 
| Lei YU | d35c852 | 2019-12-13 14:48:03 +0800 | [diff] [blame] | 78 | } | 
|  | 79 | return false; | 
|  | 80 | } | 
|  | 81 |  | 
| Patrick Williams | a14167b | 2017-04-28 15:54:39 -0500 | [diff] [blame] | 82 | TEST(VtableTest, SameContent) | 
|  | 83 | { | 
| Lei YU | d35c852 | 2019-12-13 14:48:03 +0800 | [diff] [blame] | 84 | for (size_t i = 0; i < sizeof(example) / sizeof(example[0]); ++i) | 
|  | 85 | { | 
|  | 86 | EXPECT_EQ(example[i], example2[i]); | 
|  | 87 | } | 
| Patrick Williams | a14167b | 2017-04-28 15:54:39 -0500 | [diff] [blame] | 88 | } |