Brad Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 1 | #include "propertywatchtest.hpp" |
| 2 | |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 3 | #include "propertywatchimpl.hpp" |
| 4 | |
| 5 | #include <array> |
| 6 | |
Brad Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 7 | using namespace std::string_literals; |
| 8 | using namespace phosphor::dbus::monitoring; |
| 9 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 10 | const std::array<std::string, 4> paths = { |
Brad Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 11 | "/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 Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 17 | const std::array<std::string, 2> interfaces = { |
Brad Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 18 | "xyz.openbmc_project.Iface1"s, |
| 19 | "xyz.openbmc_project.Iface2"s, |
| 20 | }; |
| 21 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 22 | const std::array<std::string, 2> properties = { |
Brad Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 23 | "Value1"s, |
| 24 | "Value2"s, |
| 25 | }; |
| 26 | |
| 27 | const std::string meta; |
| 28 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 29 | std::array<std::tuple<any_ns::any, any_ns::any>, 8> storage = {}; |
Brad Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 30 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 31 | const PropertyIndex watchIndex = { |
Brad Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 32 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 33 | {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 Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 49 | }, |
| 50 | }; |
| 51 | |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 52 | template <typename T> |
| 53 | struct ExpectedValues |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 54 | { |
| 55 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 56 | template <> |
| 57 | struct ExpectedValues<uint8_t> |
Brad Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 58 | { |
| 59 | static auto& get(size_t i) |
| 60 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 61 | static const std::array<uint8_t, 8> values = { |
Brad Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 62 | {0, 1, 2, 3, 4, 5, 6, 7}, |
| 63 | }; |
| 64 | return values[i]; |
| 65 | } |
| 66 | }; |
| 67 | |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 68 | template <> |
| 69 | struct ExpectedValues<uint16_t> |
Brad Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 70 | { |
| 71 | static auto& get(size_t i) |
| 72 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 73 | static const std::array<uint16_t, 8> values = { |
Brad Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 74 | {88, 77, 66, 55, 44, 33, 22, 11}, |
| 75 | }; |
| 76 | return values[i]; |
| 77 | } |
| 78 | }; |
| 79 | |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 80 | template <> |
| 81 | struct ExpectedValues<uint32_t> |
Brad Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 82 | { |
| 83 | static auto& get(size_t i) |
| 84 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 85 | static const std::array<uint32_t, 8> values = { |
Brad Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 86 | {0xffffffff, 1, 3, 0, 5, 7, 9, 0xffffffff}, |
| 87 | }; |
| 88 | return values[i]; |
| 89 | } |
| 90 | }; |
| 91 | |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 92 | template <> |
| 93 | struct ExpectedValues<uint64_t> |
Brad Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 94 | { |
| 95 | static auto& get(size_t i) |
| 96 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 97 | static const std::array<uint64_t, 8> values = { |
Brad Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 98 | {0xffffffffffffffff, 3, 7, 12234, 0, 3, 9, 0xffffffff}, |
| 99 | }; |
| 100 | return values[i]; |
| 101 | } |
| 102 | }; |
| 103 | |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 104 | template <> |
| 105 | struct ExpectedValues<std::string> |
Brad Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 106 | { |
| 107 | static auto& get(size_t i) |
| 108 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 109 | static const std::array<std::string, 8> values = { |
Brad Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 110 | {""s, "foo"s, "bar"s, "baz"s, "hello"s, "string", "\x2\x3", "\\"}, |
| 111 | }; |
| 112 | return values[i]; |
| 113 | } |
| 114 | }; |
| 115 | |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 116 | template <typename T> |
| 117 | void testStart() |
Brad Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 118 | { |
Brad Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 119 | using ::testing::_; |
Patrick Venture | ac80395 | 2018-08-31 09:28:31 -0700 | [diff] [blame] | 120 | using ::testing::Return; |
Brad Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 121 | |
| 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 Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 134 | std::transform(interfaces.begin(), interfaces.end(), |
| 135 | std::back_inserter(mapperResponse), |
| 136 | // *INDENT-OFF* |
| 137 | [](const auto& item) { return item.first; }); |
Brad Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 138 | // *INDENT-ON* |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 139 | EXPECT_CALL(dbus, mapperGetObject(MAPPER_BUSNAME, MAPPER_PATH, |
| 140 | MAPPER_INTERFACE, "GetObject", path, |
| 141 | expectedMapperInterfaces)) |
| 142 | .WillOnce(Return(GetObject({{"", mapperResponse}}))); |
Brad Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 143 | EXPECT_CALL( |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 144 | dbus, fwdAddMatch( |
Matthew Barth | f79fc09 | 2019-10-10 14:01:04 -0500 | [diff] [blame^] | 145 | sdbusplus::bus::match::rules::interfacesAdded(path), _)); |
Brad Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 146 | 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 Barth | f79fc09 | 2019-10-10 14:01:04 -0500 | [diff] [blame^] | 152 | fwdAddMatch(sdbusplus::bus::match::rules::propertiesChanged( |
| 153 | path, interface), |
| 154 | _)); |
Brad Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 155 | |
| 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 Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 163 | .WillOnce(Return(serviceResponse)); |
Brad Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | watch.start(); |
| 168 | |
| 169 | ndx = 0; |
| 170 | for (auto s : storage) |
| 171 | { |
Matthew Barth | f79fc09 | 2019-10-10 14:01:04 -0500 | [diff] [blame^] | 172 | 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 Bishop | 13fd872 | 2017-05-15 12:44:01 -0400 | [diff] [blame] | 175 | ++ndx; |
| 176 | } |
| 177 | |
| 178 | // Make sure start logic only runs the first time. |
| 179 | watch.start(); |
| 180 | } |
| 181 | |
| 182 | TEST(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 | |
| 191 | MockDBusInterface* MockDBusInterface::ptr = nullptr; |