blob: 1c5665802f8879b46f6881e23a0ffae65352ab19 [file] [log] [blame]
Brad Bishop13fd8722017-05-15 12:44:01 -04001#include "propertywatchtest.hpp"
2
Patrick Venture3d6d3182018-08-31 09:33:09 -07003#include "propertywatchimpl.hpp"
4
5#include <array>
6
Brad Bishop13fd8722017-05-15 12:44:01 -04007using namespace std::string_literals;
8using namespace phosphor::dbus::monitoring;
9
Brad Bishopd1eac882018-03-29 10:34:05 -040010const std::array<std::string, 4> paths = {
Brad Bishop13fd8722017-05-15 12:44:01 -040011 "/xyz/openbmc_project/testing/inst1"s,
12 "/xyz/openbmc_project/testing/inst2"s,
13 "/xyz/openbmc_project/testing/inst3"s,
14 "/xyz/openbmc_project/testing/inst4"s,
15};
16
Brad Bishopd1eac882018-03-29 10:34:05 -040017const std::array<std::string, 2> interfaces = {
Brad Bishop13fd8722017-05-15 12:44:01 -040018 "xyz.openbmc_project.Iface1"s,
19 "xyz.openbmc_project.Iface2"s,
20};
21
Brad Bishopd1eac882018-03-29 10:34:05 -040022const std::array<std::string, 2> properties = {
Brad Bishop13fd8722017-05-15 12:44:01 -040023 "Value1"s,
24 "Value2"s,
25};
26
27const std::string meta;
28
Brad Bishopd1eac882018-03-29 10:34:05 -040029std::array<std::tuple<any_ns::any, any_ns::any>, 8> storage = {};
Brad Bishop13fd8722017-05-15 12:44:01 -040030
Brad Bishopd1eac882018-03-29 10:34:05 -040031const PropertyIndex watchIndex = {
Brad Bishop13fd8722017-05-15 12:44:01 -040032 {
Brad Bishopd1eac882018-03-29 10:34:05 -040033 {PropertyIndex::key_type{paths[0], interfaces[0], properties[0]},
34 PropertyIndex::mapped_type{meta, meta, storage[0]}},
35 {PropertyIndex::key_type{paths[0], interfaces[1], properties[1]},
36 PropertyIndex::mapped_type{meta, meta, storage[1]}},
37 {PropertyIndex::key_type{paths[1], interfaces[0], properties[0]},
38 PropertyIndex::mapped_type{meta, meta, storage[2]}},
39 {PropertyIndex::key_type{paths[1], interfaces[1], properties[1]},
40 PropertyIndex::mapped_type{meta, meta, storage[3]}},
41 {PropertyIndex::key_type{paths[2], interfaces[0], properties[0]},
42 PropertyIndex::mapped_type{meta, meta, storage[4]}},
43 {PropertyIndex::key_type{paths[2], interfaces[1], properties[1]},
44 PropertyIndex::mapped_type{meta, meta, storage[5]}},
45 {PropertyIndex::key_type{paths[3], interfaces[0], properties[0]},
46 PropertyIndex::mapped_type{meta, meta, storage[6]}},
47 {PropertyIndex::key_type{paths[3], interfaces[1], properties[1]},
48 PropertyIndex::mapped_type{meta, meta, storage[7]}},
Brad Bishop13fd8722017-05-15 12:44:01 -040049 },
50};
51
Patrick Venture3d6d3182018-08-31 09:33:09 -070052template <typename T>
53struct ExpectedValues
Brad Bishopd1eac882018-03-29 10:34:05 -040054{
55};
Patrick Venture3d6d3182018-08-31 09:33:09 -070056template <>
57struct ExpectedValues<uint8_t>
Brad Bishop13fd8722017-05-15 12:44:01 -040058{
59 static auto& get(size_t i)
60 {
Brad Bishopd1eac882018-03-29 10:34:05 -040061 static const std::array<uint8_t, 8> values = {
Brad Bishop13fd8722017-05-15 12:44:01 -040062 {0, 1, 2, 3, 4, 5, 6, 7},
63 };
64 return values[i];
65 }
66};
67
Patrick Venture3d6d3182018-08-31 09:33:09 -070068template <>
69struct ExpectedValues<uint16_t>
Brad Bishop13fd8722017-05-15 12:44:01 -040070{
71 static auto& get(size_t i)
72 {
Brad Bishopd1eac882018-03-29 10:34:05 -040073 static const std::array<uint16_t, 8> values = {
Brad Bishop13fd8722017-05-15 12:44:01 -040074 {88, 77, 66, 55, 44, 33, 22, 11},
75 };
76 return values[i];
77 }
78};
79
Patrick Venture3d6d3182018-08-31 09:33:09 -070080template <>
81struct ExpectedValues<uint32_t>
Brad Bishop13fd8722017-05-15 12:44:01 -040082{
83 static auto& get(size_t i)
84 {
Brad Bishopd1eac882018-03-29 10:34:05 -040085 static const std::array<uint32_t, 8> values = {
Brad Bishop13fd8722017-05-15 12:44:01 -040086 {0xffffffff, 1, 3, 0, 5, 7, 9, 0xffffffff},
87 };
88 return values[i];
89 }
90};
91
Patrick Venture3d6d3182018-08-31 09:33:09 -070092template <>
93struct ExpectedValues<uint64_t>
Brad Bishop13fd8722017-05-15 12:44:01 -040094{
95 static auto& get(size_t i)
96 {
Brad Bishopd1eac882018-03-29 10:34:05 -040097 static const std::array<uint64_t, 8> values = {
Brad Bishop13fd8722017-05-15 12:44:01 -040098 {0xffffffffffffffff, 3, 7, 12234, 0, 3, 9, 0xffffffff},
99 };
100 return values[i];
101 }
102};
103
Patrick Venture3d6d3182018-08-31 09:33:09 -0700104template <>
105struct ExpectedValues<std::string>
Brad Bishop13fd8722017-05-15 12:44:01 -0400106{
107 static auto& get(size_t i)
108 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400109 static const std::array<std::string, 8> values = {
Brad Bishop13fd8722017-05-15 12:44:01 -0400110 {""s, "foo"s, "bar"s, "baz"s, "hello"s, "string", "\x2\x3", "\\"},
111 };
112 return values[i];
113 }
114};
115
Patrick Venture3d6d3182018-08-31 09:33:09 -0700116template <typename T>
117void testStart()
Brad Bishop13fd8722017-05-15 12:44:01 -0400118{
Brad Bishop13fd8722017-05-15 12:44:01 -0400119 using ::testing::_;
Patrick Ventureac803952018-08-31 09:28:31 -0700120 using ::testing::Return;
Brad Bishop13fd8722017-05-15 12:44:01 -0400121
122 MockDBusInterface dbus;
123 MockDBusInterface::instance(dbus);
124
125 const std::vector<std::string> expectedMapperInterfaces;
126 PropertyWatchOfType<T, MockDBusInterface> watch(watchIndex);
127
128 auto ndx = static_cast<size_t>(0);
129 for (const auto& o : convert(watchIndex))
130 {
131 const auto& path = o.first.get();
132 const auto& interfaces = o.second;
133 std::vector<std::string> mapperResponse;
Brad Bishopd1eac882018-03-29 10:34:05 -0400134 std::transform(interfaces.begin(), interfaces.end(),
135 std::back_inserter(mapperResponse),
136 // *INDENT-OFF*
137 [](const auto& item) { return item.first; });
Brad Bishop13fd8722017-05-15 12:44:01 -0400138 // *INDENT-ON*
Brad Bishopd1eac882018-03-29 10:34:05 -0400139 EXPECT_CALL(dbus, mapperGetObject(MAPPER_BUSNAME, MAPPER_PATH,
140 MAPPER_INTERFACE, "GetObject", path,
141 expectedMapperInterfaces))
142 .WillOnce(Return(GetObject({{"", mapperResponse}})));
Brad Bishop13fd8722017-05-15 12:44:01 -0400143 EXPECT_CALL(
Brad Bishopd1eac882018-03-29 10:34:05 -0400144 dbus, fwdAddMatch(
Matthew Barthf79fc092019-10-10 14:01:04 -0500145 sdbusplus::bus::match::rules::interfacesAdded(path), _));
Brad Bishop13fd8722017-05-15 12:44:01 -0400146 for (const auto& i : interfaces)
147 {
148 const auto& interface = i.first.get();
149 const auto& properties = i.second;
150 EXPECT_CALL(
151 dbus,
Matthew Barthf79fc092019-10-10 14:01:04 -0500152 fwdAddMatch(sdbusplus::bus::match::rules::propertiesChanged(
153 path, interface),
154 _));
Brad Bishop13fd8722017-05-15 12:44:01 -0400155
156 PropertiesChanged<T> serviceResponse;
157 for (const auto& p : properties)
158 {
159 serviceResponse[p] = ExpectedValues<T>::get(ndx);
160 ++ndx;
161 }
162 Expect<T>::getProperties(dbus, path, interface)
Brad Bishopd1eac882018-03-29 10:34:05 -0400163 .WillOnce(Return(serviceResponse));
Brad Bishop13fd8722017-05-15 12:44:01 -0400164 }
165 }
166
167 watch.start();
168
169 ndx = 0;
170 for (auto s : storage)
171 {
Matthew Barthf79fc092019-10-10 14:01:04 -0500172 ASSERT_EQ(std::get<valueIndex>(s).empty(), false);
173 ASSERT_EQ(any_ns::any_cast<T>(std::get<valueIndex>(s)),
174 ExpectedValues<T>::get(ndx));
Brad Bishop13fd8722017-05-15 12:44:01 -0400175 ++ndx;
176 }
177
178 // Make sure start logic only runs the first time.
179 watch.start();
180}
181
182TEST(PropertyWatchTest, TestStart)
183{
184 testStart<uint8_t>();
185 testStart<uint16_t>();
186 testStart<uint32_t>();
187 testStart<uint64_t>();
188 testStart<std::string>();
189}
190
191MockDBusInterface* MockDBusInterface::ptr = nullptr;