1 #include <sdbusplus/bus.hpp> 2 #include <server/Test/server.hpp> 3 4 #include <gtest/gtest.h> 5 6 using TestIf = sdbusplus::server::server::Test; 7 8 struct Object : public ::testing::Test 9 { 10 sdbusplus::bus_t bus = sdbusplus::bus::new_default(); 11 sdbusplus::message_t msg = bus.new_method_call( 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 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!"); 53 static_assert(sdbusplus::message::details::has_convert_from_string_v< 54 TestIf::PropertiesVariant>, 55 "TestIf::PropertiesVariant does not convert_from_string!"); 56 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 79 TEST_F(Object, VariantAsString) 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 89 TEST_F(Object, VariantAsEnumTwo) 90 { 91 run_test<variant_t>(TestIf::EnumTwo::TwoB); 92 } 93