blob: 87fe51163c335c0ca7e4176ce54c8294712ed139 [file] [log] [blame]
Patrick Williamsea56ec32021-07-14 07:41:47 -05001#include <sdbusplus/bus.hpp>
William A. Kennington III293c8a22022-09-02 14:35:54 -07002#include <server/Test/server.hpp>
Patrick Williamsea56ec32021-07-14 07:41:47 -05003
4#include <gtest/gtest.h>
5
6using TestIf = sdbusplus::server::server::Test;
7
8struct Object : public ::testing::Test
9{
Patrick Williams0f282c42021-11-19 11:36:18 -060010 sdbusplus::bus_t bus = sdbusplus::bus::new_default();
Patrick Williams10d7aa12021-11-19 11:36:18 -060011 sdbusplus::message_t msg = bus.new_method_call(
Patrick Williamsea56ec32021-07-14 07:41:47 -050012 "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 Williams87b6a8d2021-07-15 09:27:34 -050045static_assert(
46 sdbusplus::message::details::has_convert_from_string_v<TestIf::EnumOne>,
47 "EnumOne does not have convert_from_string!");
48static_assert(
49 sdbusplus::message::details::has_convert_from_string_v<TestIf::EnumTwo>,
50 "EnumTwo does not have convert_from_string!");
51static_assert(!sdbusplus::message::details::has_convert_from_string_v<size_t>,
52 "size_t unexpectedly has a convert_from_string!");
Patrick Williamsa22dbf42021-07-16 09:49:11 -050053static_assert(sdbusplus::message::details::has_convert_from_string_v<
54 TestIf::PropertiesVariant>,
55 "TestIf::PropertiesVariant does not convert_from_string!");
Patrick Williams87b6a8d2021-07-15 09:27:34 -050056
Patrick Williamsea56ec32021-07-14 07:41:47 -050057TEST_F(Object, PlainEnumOne)
58{
59 run_test<TestIf::EnumOne>(TestIf::EnumOne::OneA);
60}
61
62TEST_F(Object, PlainEnumTwo)
63{
64 run_test<TestIf::EnumTwo>(TestIf::EnumTwo::TwoB);
65}
66
67TEST_F(Object, EnumOneAsEnumTwoThrows)
68{
69 run_test_throw_bad_enum<TestIf::EnumOne, TestIf::EnumTwo>(
70 TestIf::EnumOne::OneA);
71}
72
73TEST_F(Object, EnumTwoAsEnumOneThrows)
74{
75 run_test_throw_bad_enum<TestIf::EnumTwo, TestIf::EnumOne>(
76 TestIf::EnumTwo::TwoB);
77}
78
Patrick Williamsa22dbf42021-07-16 09:49:11 -050079TEST_F(Object, VariantAsString)
Patrick Williamsea56ec32021-07-14 07:41:47 -050080{
81 run_test<variant_t>(std::string("Hello"));
82}
83
84TEST_F(Object, VariantAsEnumOne)
85{
86 run_test<variant_t>(TestIf::EnumOne::OneA);
87}
88
Patrick Williamsa22dbf42021-07-16 09:49:11 -050089TEST_F(Object, VariantAsEnumTwo)
Patrick Williamsea56ec32021-07-14 07:41:47 -050090{
91 run_test<variant_t>(TestIf::EnumTwo::TwoB);
92}