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