blob: b317074dbffbc66a2adbb6d245f02401ef79c76b [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>
Matthew Barth39c82152019-10-17 15:47:29 -05006#include <functional>
Patrick Venture3d6d3182018-08-31 09:33:09 -07007
Brad Bishop13fd8722017-05-15 12:44:01 -04008using namespace std::string_literals;
9using namespace phosphor::dbus::monitoring;
10
Brad Bishopd1eac882018-03-29 10:34:05 -040011const std::array<std::string, 4> paths = {
Brad Bishop13fd8722017-05-15 12:44:01 -040012 "/xyz/openbmc_project/testing/inst1"s,
13 "/xyz/openbmc_project/testing/inst2"s,
14 "/xyz/openbmc_project/testing/inst3"s,
15 "/xyz/openbmc_project/testing/inst4"s,
16};
17
Brad Bishopd1eac882018-03-29 10:34:05 -040018const std::array<std::string, 2> interfaces = {
Brad Bishop13fd8722017-05-15 12:44:01 -040019 "xyz.openbmc_project.Iface1"s,
20 "xyz.openbmc_project.Iface2"s,
21};
22
Brad Bishopd1eac882018-03-29 10:34:05 -040023const std::array<std::string, 2> properties = {
Brad Bishop13fd8722017-05-15 12:44:01 -040024 "Value1"s,
25 "Value2"s,
26};
27
28const std::string meta;
29
Brad Bishopd1eac882018-03-29 10:34:05 -040030std::array<std::tuple<any_ns::any, any_ns::any>, 8> storage = {};
Brad Bishop13fd8722017-05-15 12:44:01 -040031
Brad Bishopd1eac882018-03-29 10:34:05 -040032const PropertyIndex watchIndex = {
Brad Bishop13fd8722017-05-15 12:44:01 -040033 {
Brad Bishopd1eac882018-03-29 10:34:05 -040034 {PropertyIndex::key_type{paths[0], interfaces[0], properties[0]},
35 PropertyIndex::mapped_type{meta, meta, storage[0]}},
36 {PropertyIndex::key_type{paths[0], interfaces[1], properties[1]},
37 PropertyIndex::mapped_type{meta, meta, storage[1]}},
38 {PropertyIndex::key_type{paths[1], interfaces[0], properties[0]},
39 PropertyIndex::mapped_type{meta, meta, storage[2]}},
40 {PropertyIndex::key_type{paths[1], interfaces[1], properties[1]},
41 PropertyIndex::mapped_type{meta, meta, storage[3]}},
42 {PropertyIndex::key_type{paths[2], interfaces[0], properties[0]},
43 PropertyIndex::mapped_type{meta, meta, storage[4]}},
44 {PropertyIndex::key_type{paths[2], interfaces[1], properties[1]},
45 PropertyIndex::mapped_type{meta, meta, storage[5]}},
46 {PropertyIndex::key_type{paths[3], interfaces[0], properties[0]},
47 PropertyIndex::mapped_type{meta, meta, storage[6]}},
48 {PropertyIndex::key_type{paths[3], interfaces[1], properties[1]},
49 PropertyIndex::mapped_type{meta, meta, storage[7]}},
Brad Bishop13fd8722017-05-15 12:44:01 -040050 },
51};
52
Patrick Venture3d6d3182018-08-31 09:33:09 -070053template <typename T>
Matthew Barth52881362019-10-17 15:40:43 -050054struct Values
Brad Bishopd1eac882018-03-29 10:34:05 -040055{
56};
Patrick Venture3d6d3182018-08-31 09:33:09 -070057template <>
Matthew Barth52881362019-10-17 15:40:43 -050058struct Values<uint8_t>
Brad Bishop13fd8722017-05-15 12:44:01 -040059{
60 static auto& get(size_t i)
61 {
Brad Bishopd1eac882018-03-29 10:34:05 -040062 static const std::array<uint8_t, 8> values = {
Brad Bishop13fd8722017-05-15 12:44:01 -040063 {0, 1, 2, 3, 4, 5, 6, 7},
64 };
65 return values[i];
66 }
67};
68
Patrick Venture3d6d3182018-08-31 09:33:09 -070069template <>
Matthew Barth52881362019-10-17 15:40:43 -050070struct Values<uint16_t>
Brad Bishop13fd8722017-05-15 12:44:01 -040071{
72 static auto& get(size_t i)
73 {
Brad Bishopd1eac882018-03-29 10:34:05 -040074 static const std::array<uint16_t, 8> values = {
Brad Bishop13fd8722017-05-15 12:44:01 -040075 {88, 77, 66, 55, 44, 33, 22, 11},
76 };
77 return values[i];
78 }
79};
80
Patrick Venture3d6d3182018-08-31 09:33:09 -070081template <>
Matthew Barth52881362019-10-17 15:40:43 -050082struct Values<uint32_t>
Brad Bishop13fd8722017-05-15 12:44:01 -040083{
84 static auto& get(size_t i)
85 {
Brad Bishopd1eac882018-03-29 10:34:05 -040086 static const std::array<uint32_t, 8> values = {
Brad Bishop13fd8722017-05-15 12:44:01 -040087 {0xffffffff, 1, 3, 0, 5, 7, 9, 0xffffffff},
88 };
89 return values[i];
90 }
91};
92
Patrick Venture3d6d3182018-08-31 09:33:09 -070093template <>
Matthew Barth52881362019-10-17 15:40:43 -050094struct Values<uint64_t>
Brad Bishop13fd8722017-05-15 12:44:01 -040095{
96 static auto& get(size_t i)
97 {
Brad Bishopd1eac882018-03-29 10:34:05 -040098 static const std::array<uint64_t, 8> values = {
Brad Bishop13fd8722017-05-15 12:44:01 -040099 {0xffffffffffffffff, 3, 7, 12234, 0, 3, 9, 0xffffffff},
100 };
101 return values[i];
102 }
103};
104
Patrick Venture3d6d3182018-08-31 09:33:09 -0700105template <>
Matthew Barth52881362019-10-17 15:40:43 -0500106struct Values<std::string>
Brad Bishop13fd8722017-05-15 12:44:01 -0400107{
108 static auto& get(size_t i)
109 {
Brad Bishopd1eac882018-03-29 10:34:05 -0400110 static const std::array<std::string, 8> values = {
Brad Bishop13fd8722017-05-15 12:44:01 -0400111 {""s, "foo"s, "bar"s, "baz"s, "hello"s, "string", "\x2\x3", "\\"},
112 };
113 return values[i];
114 }
115};
116
Patrick Venture3d6d3182018-08-31 09:33:09 -0700117template <typename T>
Matthew Barth39c82152019-10-17 15:47:29 -0500118void nonFilteredCheck(const any_ns::any& value, const size_t ndx)
119{
120 ASSERT_EQ(value.empty(), false);
121 ASSERT_EQ(any_ns::any_cast<T>(value), Values<T>::get(ndx));
122}
123
124template <typename T>
125void testStart(
126 std::function<void(const any_ns::any&, const size_t)>&& checkState)
Brad Bishop13fd8722017-05-15 12:44:01 -0400127{
Brad Bishop13fd8722017-05-15 12:44:01 -0400128 using ::testing::_;
Patrick Ventureac803952018-08-31 09:28:31 -0700129 using ::testing::Return;
Brad Bishop13fd8722017-05-15 12:44:01 -0400130
131 MockDBusInterface dbus;
132 MockDBusInterface::instance(dbus);
133
134 const std::vector<std::string> expectedMapperInterfaces;
Matthew Barthefe01582019-09-09 15:22:37 -0500135 PropertyWatchOfType<T, MockDBusInterface> watch(watchIndex);
Brad Bishop13fd8722017-05-15 12:44:01 -0400136
137 auto ndx = static_cast<size_t>(0);
138 for (const auto& o : convert(watchIndex))
139 {
140 const auto& path = o.first.get();
141 const auto& interfaces = o.second;
142 std::vector<std::string> mapperResponse;
Brad Bishopd1eac882018-03-29 10:34:05 -0400143 std::transform(interfaces.begin(), interfaces.end(),
144 std::back_inserter(mapperResponse),
145 // *INDENT-OFF*
146 [](const auto& item) { return item.first; });
Brad Bishop13fd8722017-05-15 12:44:01 -0400147 // *INDENT-ON*
Brad Bishopd1eac882018-03-29 10:34:05 -0400148 EXPECT_CALL(dbus, mapperGetObject(MAPPER_BUSNAME, MAPPER_PATH,
149 MAPPER_INTERFACE, "GetObject", path,
150 expectedMapperInterfaces))
151 .WillOnce(Return(GetObject({{"", mapperResponse}})));
Brad Bishop13fd8722017-05-15 12:44:01 -0400152 EXPECT_CALL(
Brad Bishopd1eac882018-03-29 10:34:05 -0400153 dbus, fwdAddMatch(
Matthew Barthf79fc092019-10-10 14:01:04 -0500154 sdbusplus::bus::match::rules::interfacesAdded(path), _));
Brad Bishop13fd8722017-05-15 12:44:01 -0400155 for (const auto& i : interfaces)
156 {
157 const auto& interface = i.first.get();
158 const auto& properties = i.second;
159 EXPECT_CALL(
160 dbus,
Matthew Barthf79fc092019-10-10 14:01:04 -0500161 fwdAddMatch(sdbusplus::bus::match::rules::propertiesChanged(
162 path, interface),
163 _));
Brad Bishop13fd8722017-05-15 12:44:01 -0400164
165 PropertiesChanged<T> serviceResponse;
166 for (const auto& p : properties)
167 {
Matthew Barth52881362019-10-17 15:40:43 -0500168 serviceResponse[p] = Values<T>::get(ndx);
Brad Bishop13fd8722017-05-15 12:44:01 -0400169 ++ndx;
170 }
171 Expect<T>::getProperties(dbus, path, interface)
Brad Bishopd1eac882018-03-29 10:34:05 -0400172 .WillOnce(Return(serviceResponse));
Brad Bishop13fd8722017-05-15 12:44:01 -0400173 }
174 }
175
176 watch.start();
177
178 ndx = 0;
179 for (auto s : storage)
180 {
Matthew Barth39c82152019-10-17 15:47:29 -0500181 checkState(std::get<valueIndex>(s), ndx);
Brad Bishop13fd8722017-05-15 12:44:01 -0400182 ++ndx;
183 }
184
185 // Make sure start logic only runs the first time.
186 watch.start();
187}
188
189TEST(PropertyWatchTest, TestStart)
190{
Matthew Barth39c82152019-10-17 15:47:29 -0500191 testStart<uint8_t>(nonFilteredCheck<uint8_t>);
192 testStart<uint16_t>(nonFilteredCheck<uint16_t>);
193 testStart<uint32_t>(nonFilteredCheck<uint32_t>);
194 testStart<uint64_t>(nonFilteredCheck<uint64_t>);
195 testStart<std::string>(nonFilteredCheck<std::string>);
Brad Bishop13fd8722017-05-15 12:44:01 -0400196}
197
198MockDBusInterface* MockDBusInterface::ptr = nullptr;