blob: 1cf7729dd470ed4e1653c66c454a77fbc8144a93 [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(
145 sdbusplus::bus::match::rules::member("InterfacesAdded") +
146 sdbusplus::bus::match::rules::path(path) +
147 sdbusplus::bus::match::rules::interface(
148 "org.freedesktop.DBus.ObjectManager"),
149 _));
Brad Bishop13fd8722017-05-15 12:44:01 -0400150 for (const auto& i : interfaces)
151 {
152 const auto& interface = i.first.get();
153 const auto& properties = i.second;
154 EXPECT_CALL(
155 dbus,
156 fwdAddMatch(
157 sdbusplus::bus::match::rules::member("PropertiesChanged") +
Brad Bishopd1eac882018-03-29 10:34:05 -0400158 sdbusplus::bus::match::rules::path(path) +
159 sdbusplus::bus::match::rules::argN(0, interface) +
160 sdbusplus::bus::match::rules::interface(
161 "org.freedesktop.DBus.Properties"),
Brad Bishop13fd8722017-05-15 12:44:01 -0400162 _));
163
164 PropertiesChanged<T> serviceResponse;
165 for (const auto& p : properties)
166 {
167 serviceResponse[p] = ExpectedValues<T>::get(ndx);
168 ++ndx;
169 }
170 Expect<T>::getProperties(dbus, path, interface)
Brad Bishopd1eac882018-03-29 10:34:05 -0400171 .WillOnce(Return(serviceResponse));
Brad Bishop13fd8722017-05-15 12:44:01 -0400172 }
173 }
174
175 watch.start();
176
177 ndx = 0;
178 for (auto s : storage)
179 {
Matt Spinlerabe43ab2018-02-19 13:34:43 -0600180 ASSERT_EQ(std::get<0>(s).empty(), false);
Brad Bishop13fd8722017-05-15 12:44:01 -0400181 ASSERT_EQ(any_ns::any_cast<T>(s), ExpectedValues<T>::get(ndx));
182 ++ndx;
183 }
184
185 // Make sure start logic only runs the first time.
186 watch.start();
187}
188
189TEST(PropertyWatchTest, TestStart)
190{
191 testStart<uint8_t>();
192 testStart<uint16_t>();
193 testStart<uint32_t>();
194 testStart<uint64_t>();
195 testStart<std::string>();
196}
197
198MockDBusInterface* MockDBusInterface::ptr = nullptr;