blob: 2ddfac4dadada6ba45ad4bae4715cb44da1ce636 [file] [log] [blame]
Patrick Williamsea56ec32021-07-14 07:41:47 -05001#include "Test/server.hpp"
2
3#include <sdbusplus/bus.hpp>
4
5#include <gtest/gtest.h>
6
7using TestIf = sdbusplus::server::server::Test;
8
9struct Object : public ::testing::Test
10{
Patrick Williams0f282c42021-11-19 11:36:18 -060011 sdbusplus::bus_t bus = sdbusplus::bus::new_default();
Patrick Williams10d7aa12021-11-19 11:36:18 -060012 sdbusplus::message_t msg = bus.new_method_call(
Patrick Williamsea56ec32021-07-14 07:41:47 -050013 "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
Patrick Williams87b6a8d2021-07-15 09:27:34 -050046static_assert(
47 sdbusplus::message::details::has_convert_from_string_v<TestIf::EnumOne>,
48 "EnumOne does not have convert_from_string!");
49static_assert(
50 sdbusplus::message::details::has_convert_from_string_v<TestIf::EnumTwo>,
51 "EnumTwo does not have convert_from_string!");
52static_assert(!sdbusplus::message::details::has_convert_from_string_v<size_t>,
53 "size_t unexpectedly has a convert_from_string!");
Patrick Williamsa22dbf42021-07-16 09:49:11 -050054static_assert(sdbusplus::message::details::has_convert_from_string_v<
55 TestIf::PropertiesVariant>,
56 "TestIf::PropertiesVariant does not convert_from_string!");
Patrick Williams87b6a8d2021-07-15 09:27:34 -050057
Patrick Williamsea56ec32021-07-14 07:41:47 -050058TEST_F(Object, PlainEnumOne)
59{
60 run_test<TestIf::EnumOne>(TestIf::EnumOne::OneA);
61}
62
63TEST_F(Object, PlainEnumTwo)
64{
65 run_test<TestIf::EnumTwo>(TestIf::EnumTwo::TwoB);
66}
67
68TEST_F(Object, EnumOneAsEnumTwoThrows)
69{
70 run_test_throw_bad_enum<TestIf::EnumOne, TestIf::EnumTwo>(
71 TestIf::EnumOne::OneA);
72}
73
74TEST_F(Object, EnumTwoAsEnumOneThrows)
75{
76 run_test_throw_bad_enum<TestIf::EnumTwo, TestIf::EnumOne>(
77 TestIf::EnumTwo::TwoB);
78}
79
Patrick Williamsa22dbf42021-07-16 09:49:11 -050080TEST_F(Object, VariantAsString)
Patrick Williamsea56ec32021-07-14 07:41:47 -050081{
82 run_test<variant_t>(std::string("Hello"));
83}
84
85TEST_F(Object, VariantAsEnumOne)
86{
87 run_test<variant_t>(TestIf::EnumOne::OneA);
88}
89
Patrick Williamsa22dbf42021-07-16 09:49:11 -050090TEST_F(Object, VariantAsEnumTwo)
Patrick Williamsea56ec32021-07-14 07:41:47 -050091{
92 run_test<variant_t>(TestIf::EnumTwo::TwoB);
93}