Patrick Williams | ea56ec3 | 2021-07-14 07:41:47 -0500 | [diff] [blame] | 1 | #include <sdbusplus/bus.hpp> |
William A. Kennington III | 293c8a2 | 2022-09-02 14:35:54 -0700 | [diff] [blame] | 2 | #include <server/Test/server.hpp> |
Patrick Williams | ea56ec3 | 2021-07-14 07:41:47 -0500 | [diff] [blame] | 3 | |
| 4 | #include <gtest/gtest.h> |
| 5 | |
| 6 | using TestIf = sdbusplus::server::server::Test; |
| 7 | |
| 8 | struct Object : public ::testing::Test |
| 9 | { |
Patrick Williams | 0f282c4 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 10 | sdbusplus::bus_t bus = sdbusplus::bus::new_default(); |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 11 | sdbusplus::message_t msg = bus.new_method_call( |
Patrick Williams | ea56ec3 | 2021-07-14 07:41:47 -0500 | [diff] [blame] | 12 | "xyz.openbmc_project.sdbusplus.test.Object", |
| 13 | "/xyz/openbmc_project/sdbusplus/test/object", |
| 14 | "xyz.openbmc_project.sdbusplus.test.Object", "Unused"); |
| 15 | |
| 16 | using variant_t = |
| 17 | std::variant<TestIf::EnumOne, std::string, TestIf::EnumTwo>; |
| 18 | |
| 19 | template <typename V, typename T> |
| 20 | void run_test(const T& value) |
| 21 | { |
| 22 | const V data = value; |
| 23 | msg.append(data); |
| 24 | ASSERT_EQ(0, sd_bus_message_seal(msg.get(), 0, 0)); |
| 25 | |
| 26 | V new_data = {}; |
| 27 | msg.read(new_data); |
| 28 | |
| 29 | EXPECT_EQ(data, new_data); |
| 30 | } |
| 31 | |
| 32 | template <typename V1, typename V2, typename T> |
| 33 | void run_test_throw_bad_enum(const T& value) |
| 34 | { |
| 35 | const V1 data = value; |
| 36 | msg.append(data); |
| 37 | ASSERT_EQ(0, sd_bus_message_seal(msg.get(), 0, 0)); |
| 38 | |
| 39 | V2 new_data = {}; |
| 40 | EXPECT_THROW(msg.read(new_data), |
| 41 | sdbusplus::exception::InvalidEnumString); |
| 42 | } |
| 43 | }; |
| 44 | |
Patrick Williams | 87b6a8d | 2021-07-15 09:27:34 -0500 | [diff] [blame] | 45 | static_assert( |
| 46 | sdbusplus::message::details::has_convert_from_string_v<TestIf::EnumOne>, |
| 47 | "EnumOne does not have convert_from_string!"); |
| 48 | static_assert( |
| 49 | sdbusplus::message::details::has_convert_from_string_v<TestIf::EnumTwo>, |
| 50 | "EnumTwo does not have convert_from_string!"); |
| 51 | static_assert(!sdbusplus::message::details::has_convert_from_string_v<size_t>, |
| 52 | "size_t unexpectedly has a convert_from_string!"); |
Patrick Williams | a22dbf4 | 2021-07-16 09:49:11 -0500 | [diff] [blame] | 53 | static_assert(sdbusplus::message::details::has_convert_from_string_v< |
| 54 | TestIf::PropertiesVariant>, |
| 55 | "TestIf::PropertiesVariant does not convert_from_string!"); |
Patrick Williams | 87b6a8d | 2021-07-15 09:27:34 -0500 | [diff] [blame] | 56 | |
Patrick Williams | ea56ec3 | 2021-07-14 07:41:47 -0500 | [diff] [blame] | 57 | TEST_F(Object, PlainEnumOne) |
| 58 | { |
| 59 | run_test<TestIf::EnumOne>(TestIf::EnumOne::OneA); |
| 60 | } |
| 61 | |
| 62 | TEST_F(Object, PlainEnumTwo) |
| 63 | { |
| 64 | run_test<TestIf::EnumTwo>(TestIf::EnumTwo::TwoB); |
| 65 | } |
| 66 | |
| 67 | TEST_F(Object, EnumOneAsEnumTwoThrows) |
| 68 | { |
| 69 | run_test_throw_bad_enum<TestIf::EnumOne, TestIf::EnumTwo>( |
| 70 | TestIf::EnumOne::OneA); |
| 71 | } |
| 72 | |
| 73 | TEST_F(Object, EnumTwoAsEnumOneThrows) |
| 74 | { |
| 75 | run_test_throw_bad_enum<TestIf::EnumTwo, TestIf::EnumOne>( |
| 76 | TestIf::EnumTwo::TwoB); |
| 77 | } |
| 78 | |
Patrick Williams | a22dbf4 | 2021-07-16 09:49:11 -0500 | [diff] [blame] | 79 | TEST_F(Object, VariantAsString) |
Patrick Williams | ea56ec3 | 2021-07-14 07:41:47 -0500 | [diff] [blame] | 80 | { |
| 81 | run_test<variant_t>(std::string("Hello")); |
| 82 | } |
| 83 | |
| 84 | TEST_F(Object, VariantAsEnumOne) |
| 85 | { |
| 86 | run_test<variant_t>(TestIf::EnumOne::OneA); |
| 87 | } |
| 88 | |
Patrick Williams | a22dbf4 | 2021-07-16 09:49:11 -0500 | [diff] [blame] | 89 | TEST_F(Object, VariantAsEnumTwo) |
Patrick Williams | ea56ec3 | 2021-07-14 07:41:47 -0500 | [diff] [blame] | 90 | { |
| 91 | run_test<variant_t>(TestIf::EnumTwo::TwoB); |
| 92 | } |