James Feist | 75eb769 | 2019-02-25 12:50:02 -0800 | [diff] [blame] | 1 | #include "conf.hpp" |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 2 | #include "dbus/dbuspassive.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 3 | #include "test/dbushelper_mock.hpp" |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 4 | |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 5 | #include <sdbusplus/test/sdbus_mock.hpp> |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 6 | |
| 7 | #include <functional> |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 8 | #include <memory> |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 9 | #include <string> |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 10 | #include <variant> |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 11 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 12 | #include <gmock/gmock.h> |
| 13 | #include <gtest/gtest.h> |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 14 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 15 | namespace pid_control |
| 16 | { |
| 17 | namespace |
| 18 | { |
| 19 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 20 | using ::testing::_; |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 21 | using ::testing::InSequence; |
| 22 | using ::testing::Invoke; |
| 23 | using ::testing::IsNull; |
| 24 | using ::testing::NotNull; |
| 25 | using ::testing::Return; |
| 26 | using ::testing::StrEq; |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 27 | |
| 28 | std::string SensorIntf = "xyz.openbmc_project.Sensor.Value"; |
| 29 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 30 | TEST(DbusPassiveTest, FactoryFailsWithInvalidType) |
| 31 | { |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 32 | // Verify the type is checked by the factory. |
| 33 | |
| 34 | sdbusplus::SdBusMock sdbus_mock; |
| 35 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 36 | std::string type = "invalid"; |
| 37 | std::string id = "id"; |
| 38 | |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 39 | auto helper = std::make_unique<DbusHelperMock>(); |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 40 | auto info = conf::SensorConfig(); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 41 | |
James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 42 | std::unique_ptr<ReadInterface> ri = DbusPassive::createDbusPassive( |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 43 | bus_mock, type, id, std::move(helper), &info, nullptr); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 44 | |
| 45 | EXPECT_EQ(ri, nullptr); |
| 46 | } |
| 47 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 48 | TEST(DbusPassiveTest, BoringConstructorTest) |
| 49 | { |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 50 | // Simply build the object, does no error checking. |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 51 | |
| 52 | sdbusplus::SdBusMock sdbus_mock; |
| 53 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 54 | std::string type = "invalid"; |
| 55 | std::string id = "id"; |
| 56 | std::string path = "/xyz/openbmc_project/sensors/unknown/id"; |
| 57 | |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 58 | auto helper = std::make_unique<DbusHelperMock>(); |
Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 59 | SensorProperties properties; |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 60 | |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 61 | DbusPassive(bus_mock, type, id, std::move(helper), properties, false, path, |
| 62 | nullptr); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 63 | // Success |
| 64 | } |
| 65 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 66 | class DbusPassiveTestObj : public ::testing::Test |
| 67 | { |
| 68 | protected: |
| 69 | DbusPassiveTestObj() : |
| 70 | sdbus_mock(), |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 71 | bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))), |
| 72 | helper(std::make_unique<DbusHelperMock>()) |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 73 | { |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 74 | EXPECT_CALL(*helper, getService(StrEq(SensorIntf), StrEq(path))) |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 75 | .WillOnce(Return("asdf")); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 76 | |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 77 | EXPECT_CALL(*helper, |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 78 | getProperties(StrEq("asdf"), StrEq(path), NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 79 | .WillOnce(Invoke([&]([[maybe_unused]] const std::string& service, |
| 80 | [[maybe_unused]] const std::string& path, |
| 81 | SensorProperties* prop) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 82 | prop->scale = _scale; |
| 83 | prop->value = _value; |
| 84 | prop->unit = "x"; |
| 85 | prop->min = 0; |
| 86 | prop->max = 0; |
| 87 | prop->available = true; |
| 88 | })); |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 89 | EXPECT_CALL(*helper, thresholdsAsserted(StrEq("asdf"), StrEq(path))) |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 90 | .WillOnce(Return(false)); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 91 | |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 92 | auto info = conf::SensorConfig(); |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 93 | info.unavailableAsFailed = true; |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 94 | ri = DbusPassive::createDbusPassive(bus_mock, type, id, |
| 95 | std::move(helper), &info, nullptr); |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 96 | passive = reinterpret_cast<DbusPassive*>(ri.get()); |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 97 | EXPECT_FALSE(passive == nullptr); |
| 98 | } |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 99 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 100 | sdbusplus::SdBusMock sdbus_mock; |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 101 | sdbusplus::bus_t bus_mock; |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 102 | std::unique_ptr<DbusHelperMock> helper; |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 103 | std::string type = "temp"; |
| 104 | std::string id = "id"; |
| 105 | std::string path = "/xyz/openbmc_project/sensors/temperature/id"; |
| 106 | int64_t _scale = -3; |
| 107 | int64_t _value = 10; |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 108 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 109 | std::unique_ptr<ReadInterface> ri; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 110 | DbusPassive* passive; |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 113 | TEST_F(DbusPassiveTestObj, ReadReturnsExpectedValues) |
| 114 | { |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 115 | // Verify read is returning the values. |
| 116 | ReadReturn v; |
| 117 | v.value = 0.01; |
| 118 | // TODO: updated is set when the value is created, so we can range check |
| 119 | // it. |
| 120 | ReadReturn r = passive->read(); |
| 121 | EXPECT_EQ(v.value, r.value); |
| 122 | } |
| 123 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 124 | TEST_F(DbusPassiveTestObj, SetValueUpdatesValue) |
| 125 | { |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 126 | // Verify setvalue does as advertised. |
| 127 | |
| 128 | double value = 0.01; |
| 129 | passive->setValue(value); |
| 130 | |
| 131 | // TODO: updated is set when the value is set, so we can range check it. |
| 132 | ReadReturn r = passive->read(); |
| 133 | EXPECT_EQ(value, r.value); |
| 134 | } |
| 135 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 136 | TEST_F(DbusPassiveTestObj, GetScaleReturnsExpectedValue) |
| 137 | { |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 138 | // Verify the scale is returned as expected. |
| 139 | EXPECT_EQ(_scale, passive->getScale()); |
| 140 | } |
| 141 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 142 | TEST_F(DbusPassiveTestObj, getIDReturnsExpectedValue) |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 143 | { |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 144 | // Verify getID returns the expected value. |
| 145 | EXPECT_EQ(id, passive->getID()); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 148 | TEST_F(DbusPassiveTestObj, GetMinValueReturnsExpectedValue) |
| 149 | { |
| 150 | EXPECT_DOUBLE_EQ(0, passive->getMin()); |
| 151 | } |
| 152 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 153 | TEST_F(DbusPassiveTestObj, VerifyHandlesDbusSignal) |
| 154 | { |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 155 | // The dbus passive sensor listens for updates and if it's the Value |
| 156 | // property, it needs to handle it. |
| 157 | |
| 158 | EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull())) |
| 159 | .WillOnce(Return(nullptr)); |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 160 | sdbusplus::message_t msg(nullptr, &sdbus_mock); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 161 | |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 162 | const char* Value = "Value"; |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 163 | int64_t xValue = 10000; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 164 | const char* intf = "xyz.openbmc_project.Sensor.Value"; |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 165 | // string, std::map<std::string, std::variant<int64_t>> |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 166 | // msg.read(msgSensor, msgData); |
| 167 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 168 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 169 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 170 | [[maybe_unused]] char type, void* p) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 171 | const char** s = static_cast<const char**>(p); |
| 172 | // Read the first parameter, the string. |
| 173 | *s = intf; |
| 174 | return 0; |
| 175 | })) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 176 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 177 | [[maybe_unused]] char type, void* p) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 178 | const char** s = static_cast<const char**>(p); |
| 179 | *s = Value; |
| 180 | // Read the string in the pair (dictionary). |
| 181 | return 0; |
| 182 | })); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 183 | |
| 184 | // std::map |
| 185 | EXPECT_CALL(sdbus_mock, |
| 186 | sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}"))) |
| 187 | .WillOnce(Return(0)); |
| 188 | |
| 189 | // while !at_end() |
| 190 | EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0)) |
| 191 | .WillOnce(Return(0)) |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 192 | .WillOnce(Return(1)); // So it exits the loop after reading one pair. |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 193 | |
| 194 | // std::pair |
| 195 | EXPECT_CALL(sdbus_mock, |
| 196 | sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv"))) |
| 197 | .WillOnce(Return(0)); |
| 198 | |
| 199 | EXPECT_CALL(sdbus_mock, |
| 200 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("x"))) |
| 201 | .WillOnce(Return(1)); |
| 202 | EXPECT_CALL(sdbus_mock, |
| 203 | sd_bus_message_enter_container(IsNull(), 'v', StrEq("x"))) |
| 204 | .WillOnce(Return(0)); |
| 205 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 206 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'x', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 207 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 208 | [[maybe_unused]] char type, void* p) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 209 | int64_t* s = static_cast<int64_t*>(p); |
| 210 | *s = xValue; |
| 211 | return 0; |
| 212 | })); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 213 | |
| 214 | EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull())) |
| 215 | .WillOnce(Return(0)) /* variant. */ |
| 216 | .WillOnce(Return(0)) /* std::pair */ |
| 217 | .WillOnce(Return(0)); /* std::map */ |
| 218 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 219 | int rv = handleSensorValue(msg, passive); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 220 | EXPECT_EQ(rv, 0); // It's always 0. |
| 221 | |
| 222 | ReadReturn r = passive->read(); |
| 223 | EXPECT_EQ(10, r.value); |
| 224 | } |
| 225 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 226 | TEST_F(DbusPassiveTestObj, VerifyIgnoresOtherPropertySignal) |
| 227 | { |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 228 | // The dbus passive sensor listens for updates and if it's the Value |
| 229 | // property, it needs to handle it. In this case, it won't be. |
| 230 | |
| 231 | EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull())) |
| 232 | .WillOnce(Return(nullptr)); |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 233 | sdbusplus::message_t msg(nullptr, &sdbus_mock); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 234 | |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 235 | const char* Scale = "Scale"; |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 236 | int64_t xScale = -6; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 237 | const char* intf = "xyz.openbmc_project.Sensor.Value"; |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 238 | // string, std::map<std::string, std::variant<int64_t>> |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 239 | // msg.read(msgSensor, msgData); |
| 240 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 241 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 242 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 243 | [[maybe_unused]] char type, void* p) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 244 | const char** s = static_cast<const char**>(p); |
| 245 | // Read the first parameter, the string. |
| 246 | *s = intf; |
| 247 | return 0; |
| 248 | })) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 249 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 250 | [[maybe_unused]] char type, void* p) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 251 | const char** s = static_cast<const char**>(p); |
| 252 | *s = Scale; |
| 253 | // Read the string in the pair (dictionary). |
| 254 | return 0; |
| 255 | })); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 256 | |
| 257 | // std::map |
| 258 | EXPECT_CALL(sdbus_mock, |
| 259 | sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}"))) |
| 260 | .WillOnce(Return(0)); |
| 261 | |
| 262 | // while !at_end() |
| 263 | EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0)) |
| 264 | .WillOnce(Return(0)) |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 265 | .WillOnce(Return(1)); // So it exits the loop after reading one pair. |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 266 | |
| 267 | // std::pair |
| 268 | EXPECT_CALL(sdbus_mock, |
| 269 | sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv"))) |
| 270 | .WillOnce(Return(0)); |
| 271 | |
| 272 | EXPECT_CALL(sdbus_mock, |
| 273 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("x"))) |
| 274 | .WillOnce(Return(1)); |
| 275 | EXPECT_CALL(sdbus_mock, |
| 276 | sd_bus_message_enter_container(IsNull(), 'v', StrEq("x"))) |
| 277 | .WillOnce(Return(0)); |
| 278 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 279 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'x', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 280 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 281 | [[maybe_unused]] char type, void* p) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 282 | int64_t* s = static_cast<int64_t*>(p); |
| 283 | *s = xScale; |
| 284 | return 0; |
| 285 | })); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 286 | |
| 287 | EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull())) |
| 288 | .WillOnce(Return(0)) /* variant. */ |
| 289 | .WillOnce(Return(0)) /* std::pair */ |
| 290 | .WillOnce(Return(0)); /* std::map */ |
| 291 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 292 | int rv = handleSensorValue(msg, passive); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 293 | EXPECT_EQ(rv, 0); // It's always 0. |
| 294 | |
| 295 | ReadReturn r = passive->read(); |
| 296 | EXPECT_EQ(0.01, r.value); |
| 297 | } |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 298 | |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 299 | TEST_F(DbusPassiveTestObj, VerifyCriticalThresholdAssert) |
| 300 | { |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 301 | // Verifies when a threshold is crossed the sensor goes into error state |
| 302 | EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull())) |
| 303 | .WillOnce(Return(nullptr)); |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 304 | sdbusplus::message_t msg(nullptr, &sdbus_mock); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 305 | |
| 306 | const char* criticalAlarm = "CriticalAlarmHigh"; |
| 307 | bool alarm = true; |
| 308 | const char* intf = "xyz.openbmc_project.Sensor.Threshold.Critical"; |
| 309 | |
| 310 | passive->setFailed(false); |
| 311 | |
| 312 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 313 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 314 | [[maybe_unused]] char type, void* p) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 315 | const char** s = static_cast<const char**>(p); |
| 316 | // Read the first parameter, the string. |
| 317 | *s = intf; |
| 318 | return 0; |
| 319 | })) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 320 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 321 | [[maybe_unused]] char type, void* p) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 322 | const char** s = static_cast<const char**>(p); |
| 323 | *s = criticalAlarm; |
| 324 | // Read the string in the pair (dictionary). |
| 325 | return 0; |
| 326 | })); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 327 | |
| 328 | // std::map |
| 329 | EXPECT_CALL(sdbus_mock, |
| 330 | sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}"))) |
| 331 | .WillOnce(Return(0)); |
| 332 | |
| 333 | // while !at_end() |
| 334 | EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0)) |
| 335 | .WillOnce(Return(0)) |
| 336 | .WillOnce(Return(1)); // So it exits the loop after reading one pair. |
| 337 | |
| 338 | // std::pair |
| 339 | EXPECT_CALL(sdbus_mock, |
| 340 | sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv"))) |
| 341 | .WillOnce(Return(0)); |
| 342 | |
| 343 | EXPECT_CALL(sdbus_mock, |
| 344 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("x"))) |
| 345 | .WillOnce(Return(0)); |
| 346 | EXPECT_CALL(sdbus_mock, |
| 347 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("d"))) |
| 348 | .WillOnce(Return(0)); |
| 349 | EXPECT_CALL(sdbus_mock, |
| 350 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("b"))) |
| 351 | .WillOnce(Return(1)); |
| 352 | EXPECT_CALL(sdbus_mock, |
| 353 | sd_bus_message_enter_container(IsNull(), 'v', StrEq("b"))) |
| 354 | .WillOnce(Return(0)); |
| 355 | |
| 356 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'b', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 357 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 358 | [[maybe_unused]] char type, void* p) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 359 | bool* s = static_cast<bool*>(p); |
| 360 | *s = alarm; |
| 361 | return 0; |
| 362 | })); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 363 | |
| 364 | EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull())) |
| 365 | .WillOnce(Return(0)) /* variant. */ |
| 366 | .WillOnce(Return(0)) /* std::pair */ |
| 367 | .WillOnce(Return(0)); /* std::map */ |
| 368 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 369 | int rv = handleSensorValue(msg, passive); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 370 | EXPECT_EQ(rv, 0); // It's always 0. |
| 371 | bool failed = passive->getFailed(); |
| 372 | EXPECT_EQ(failed, true); |
| 373 | } |
| 374 | |
| 375 | TEST_F(DbusPassiveTestObj, VerifyCriticalThresholdDeassert) |
| 376 | { |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 377 | // Verifies when a threshold is deasserted a failed sensor goes back into |
| 378 | // the normal state |
| 379 | EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull())) |
| 380 | .WillOnce(Return(nullptr)); |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 381 | sdbusplus::message_t msg(nullptr, &sdbus_mock); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 382 | |
| 383 | const char* criticalAlarm = "CriticalAlarmHigh"; |
| 384 | bool alarm = false; |
| 385 | const char* intf = "xyz.openbmc_project.Sensor.Threshold.Critical"; |
| 386 | |
| 387 | passive->setFailed(true); |
| 388 | |
| 389 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 390 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 391 | [[maybe_unused]] char type, void* p) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 392 | const char** s = static_cast<const char**>(p); |
| 393 | // Read the first parameter, the string. |
| 394 | *s = intf; |
| 395 | return 0; |
| 396 | })) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 397 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 398 | [[maybe_unused]] char type, void* p) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 399 | const char** s = static_cast<const char**>(p); |
| 400 | *s = criticalAlarm; |
| 401 | // Read the string in the pair (dictionary). |
| 402 | return 0; |
| 403 | })); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 404 | |
| 405 | // std::map |
| 406 | EXPECT_CALL(sdbus_mock, |
| 407 | sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}"))) |
| 408 | .WillOnce(Return(0)); |
| 409 | |
| 410 | // while !at_end() |
| 411 | EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0)) |
| 412 | .WillOnce(Return(0)) |
| 413 | .WillOnce(Return(1)); // So it exits the loop after reading one pair. |
| 414 | |
| 415 | // std::pair |
| 416 | EXPECT_CALL(sdbus_mock, |
| 417 | sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv"))) |
| 418 | .WillOnce(Return(0)); |
| 419 | |
| 420 | EXPECT_CALL(sdbus_mock, |
| 421 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("x"))) |
| 422 | .WillOnce(Return(0)); |
| 423 | EXPECT_CALL(sdbus_mock, |
| 424 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("d"))) |
| 425 | .WillOnce(Return(0)); |
| 426 | EXPECT_CALL(sdbus_mock, |
| 427 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("b"))) |
| 428 | .WillOnce(Return(1)); |
| 429 | EXPECT_CALL(sdbus_mock, |
| 430 | sd_bus_message_enter_container(IsNull(), 'v', StrEq("b"))) |
| 431 | .WillOnce(Return(0)); |
| 432 | |
| 433 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'b', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 434 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 435 | [[maybe_unused]] char type, void* p) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 436 | bool* s = static_cast<bool*>(p); |
| 437 | *s = alarm; |
| 438 | return 0; |
| 439 | })); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 440 | |
| 441 | EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull())) |
| 442 | .WillOnce(Return(0)) /* variant. */ |
| 443 | .WillOnce(Return(0)) /* std::pair */ |
| 444 | .WillOnce(Return(0)); /* std::map */ |
| 445 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 446 | int rv = handleSensorValue(msg, passive); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 447 | EXPECT_EQ(rv, 0); // It's always 0. |
| 448 | bool failed = passive->getFailed(); |
| 449 | EXPECT_EQ(failed, false); |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 450 | } |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 451 | |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 452 | TEST_F(DbusPassiveTestObj, VerifyAvailableDeassert) |
| 453 | { |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 454 | // Verifies when Availble is deasserted && unavailableAsFailed == true, |
| 455 | // the sensor goes into error state |
| 456 | EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull())) |
| 457 | .WillOnce(Return(nullptr)); |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 458 | sdbusplus::message_t msg(nullptr, &sdbus_mock); |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 459 | |
| 460 | const char* property = "Available"; |
| 461 | bool asserted = false; |
| 462 | const char* intf = "xyz.openbmc_project.State.Decorator.Availability"; |
| 463 | |
| 464 | passive->setAvailable(true); |
| 465 | |
| 466 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 467 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 468 | [[maybe_unused]] char type, void* p) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 469 | const char** s = static_cast<const char**>(p); |
| 470 | // Read the first parameter, the string. |
| 471 | *s = intf; |
| 472 | return 0; |
| 473 | })) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 474 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 475 | [[maybe_unused]] char type, void* p) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 476 | const char** s = static_cast<const char**>(p); |
| 477 | *s = property; |
| 478 | // Read the string in the pair (dictionary). |
| 479 | return 0; |
| 480 | })); |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 481 | |
| 482 | // std::map |
| 483 | EXPECT_CALL(sdbus_mock, |
| 484 | sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}"))) |
| 485 | .WillOnce(Return(0)); |
| 486 | |
| 487 | // while !at_end() |
| 488 | EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0)) |
| 489 | .WillOnce(Return(0)) |
| 490 | .WillOnce(Return(1)); // So it exits the loop after reading one pair. |
| 491 | |
| 492 | // std::pair |
| 493 | EXPECT_CALL(sdbus_mock, |
| 494 | sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv"))) |
| 495 | .WillOnce(Return(0)); |
| 496 | |
| 497 | EXPECT_CALL(sdbus_mock, |
| 498 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("x"))) |
| 499 | .WillOnce(Return(0)); |
| 500 | EXPECT_CALL(sdbus_mock, |
| 501 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("d"))) |
| 502 | .WillOnce(Return(0)); |
| 503 | EXPECT_CALL(sdbus_mock, |
| 504 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("b"))) |
| 505 | .WillOnce(Return(1)); |
| 506 | EXPECT_CALL(sdbus_mock, |
| 507 | sd_bus_message_enter_container(IsNull(), 'v', StrEq("b"))) |
| 508 | .WillOnce(Return(0)); |
| 509 | |
| 510 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'b', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 511 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 512 | [[maybe_unused]] char type, void* p) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 513 | bool* s = static_cast<bool*>(p); |
| 514 | *s = asserted; |
| 515 | return 0; |
| 516 | })); |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 517 | |
| 518 | EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull())) |
| 519 | .WillOnce(Return(0)) /* variant. */ |
| 520 | .WillOnce(Return(0)) /* std::pair */ |
| 521 | .WillOnce(Return(0)); /* std::map */ |
| 522 | |
| 523 | int rv = handleSensorValue(msg, passive); |
| 524 | EXPECT_EQ(rv, 0); // It's always 0. |
| 525 | bool failed = passive->getFailed(); |
| 526 | EXPECT_EQ(failed, true); |
| 527 | } |
| 528 | |
| 529 | TEST_F(DbusPassiveTestObj, VerifyAvailableAssert) |
| 530 | { |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 531 | // Verifies when Availble is asserted && unavailableAsFailed == true, |
| 532 | // an error sensor goes back to normal state |
| 533 | EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull())) |
| 534 | .WillOnce(Return(nullptr)); |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 535 | sdbusplus::message_t msg(nullptr, &sdbus_mock); |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 536 | |
| 537 | const char* property = "Available"; |
| 538 | bool asserted = true; |
| 539 | const char* intf = "xyz.openbmc_project.State.Decorator.Availability"; |
| 540 | |
| 541 | passive->setAvailable(false); |
| 542 | bool failed = passive->getFailed(); |
| 543 | EXPECT_EQ(failed, true); |
| 544 | |
| 545 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 546 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 547 | [[maybe_unused]] char type, void* p) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 548 | const char** s = static_cast<const char**>(p); |
| 549 | // Read the first parameter, the string. |
| 550 | *s = intf; |
| 551 | return 0; |
| 552 | })) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 553 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 554 | [[maybe_unused]] char type, void* p) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 555 | const char** s = static_cast<const char**>(p); |
| 556 | *s = property; |
| 557 | // Read the string in the pair (dictionary). |
| 558 | return 0; |
| 559 | })); |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 560 | |
| 561 | // std::map |
| 562 | EXPECT_CALL(sdbus_mock, |
| 563 | sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}"))) |
| 564 | .WillOnce(Return(0)); |
| 565 | |
| 566 | // while !at_end() |
| 567 | EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0)) |
| 568 | .WillOnce(Return(0)) |
| 569 | .WillOnce(Return(1)); // So it exits the loop after reading one pair. |
| 570 | |
| 571 | // std::pair |
| 572 | EXPECT_CALL(sdbus_mock, |
| 573 | sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv"))) |
| 574 | .WillOnce(Return(0)); |
| 575 | |
| 576 | EXPECT_CALL(sdbus_mock, |
| 577 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("x"))) |
| 578 | .WillOnce(Return(0)); |
| 579 | EXPECT_CALL(sdbus_mock, |
| 580 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("d"))) |
| 581 | .WillOnce(Return(0)); |
| 582 | EXPECT_CALL(sdbus_mock, |
| 583 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("b"))) |
| 584 | .WillOnce(Return(1)); |
| 585 | EXPECT_CALL(sdbus_mock, |
| 586 | sd_bus_message_enter_container(IsNull(), 'v', StrEq("b"))) |
| 587 | .WillOnce(Return(0)); |
| 588 | |
| 589 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'b', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 590 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 591 | [[maybe_unused]] char type, void* p) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 592 | bool* s = static_cast<bool*>(p); |
| 593 | *s = asserted; |
| 594 | return 0; |
| 595 | })); |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 596 | |
| 597 | EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull())) |
| 598 | .WillOnce(Return(0)) /* variant. */ |
| 599 | .WillOnce(Return(0)) /* std::pair */ |
| 600 | .WillOnce(Return(0)); /* std::map */ |
| 601 | |
| 602 | int rv = handleSensorValue(msg, passive); |
| 603 | EXPECT_EQ(rv, 0); // It's always 0. |
| 604 | failed = passive->getFailed(); |
| 605 | EXPECT_EQ(failed, false); |
| 606 | } |
| 607 | |
| 608 | class DbusPassiveTestUnaSensorNotAsFailedObj : public ::testing::Test |
| 609 | { |
| 610 | protected: |
| 611 | DbusPassiveTestUnaSensorNotAsFailedObj() : |
| 612 | sdbus_mock(), |
| 613 | bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))), |
| 614 | helper(std::make_unique<DbusHelperMock>()) |
| 615 | { |
| 616 | EXPECT_CALL(*helper, getService(StrEq(SensorIntf), StrEq(path))) |
| 617 | .WillOnce(Return("asdf")); |
| 618 | |
| 619 | EXPECT_CALL(*helper, |
| 620 | getProperties(StrEq("asdf"), StrEq(path), NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 621 | .WillOnce(Invoke([&]([[maybe_unused]] const std::string& service, |
| 622 | [[maybe_unused]] const std::string& path, |
| 623 | SensorProperties* prop) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 624 | prop->scale = _scale; |
| 625 | prop->value = _value; |
| 626 | prop->unit = "x"; |
| 627 | prop->min = 0; |
| 628 | prop->max = 0; |
| 629 | prop->available = true; |
| 630 | })); |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 631 | EXPECT_CALL(*helper, thresholdsAsserted(StrEq("asdf"), StrEq(path))) |
| 632 | .WillOnce(Return(false)); |
| 633 | |
| 634 | auto info = conf::SensorConfig(); |
| 635 | info.unavailableAsFailed = false; |
| 636 | ri = DbusPassive::createDbusPassive(bus_mock, type, id, |
| 637 | std::move(helper), &info, nullptr); |
| 638 | passive = reinterpret_cast<DbusPassive*>(ri.get()); |
| 639 | EXPECT_FALSE(passive == nullptr); |
| 640 | } |
| 641 | |
| 642 | sdbusplus::SdBusMock sdbus_mock; |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 643 | sdbusplus::bus_t bus_mock; |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 644 | std::unique_ptr<DbusHelperMock> helper; |
| 645 | std::string type = "temp"; |
| 646 | std::string id = "id"; |
| 647 | std::string path = "/xyz/openbmc_project/sensors/temperature/id"; |
| 648 | int64_t _scale = -3; |
| 649 | int64_t _value = 10; |
| 650 | |
| 651 | std::unique_ptr<ReadInterface> ri; |
| 652 | DbusPassive* passive; |
| 653 | }; |
| 654 | |
| 655 | TEST_F(DbusPassiveTestUnaSensorNotAsFailedObj, VerifyAvailableDeassert) |
| 656 | { |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 657 | // Verifies when Availble is deasserted && unavailableAsFailed == false, |
| 658 | // the sensor remains at OK state but reading goes to NaN. |
| 659 | EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull())) |
| 660 | .WillOnce(Return(nullptr)); |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 661 | sdbusplus::message_t msg(nullptr, &sdbus_mock); |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 662 | |
| 663 | const char* property = "Available"; |
| 664 | bool asserted = false; |
| 665 | const char* intf = "xyz.openbmc_project.State.Decorator.Availability"; |
| 666 | |
| 667 | passive->setAvailable(true); |
| 668 | |
| 669 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 670 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 671 | [[maybe_unused]] char type, void* p) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 672 | const char** s = static_cast<const char**>(p); |
| 673 | // Read the first parameter, the string. |
| 674 | *s = intf; |
| 675 | return 0; |
| 676 | })) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 677 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 678 | [[maybe_unused]] char type, void* p) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 679 | const char** s = static_cast<const char**>(p); |
| 680 | *s = property; |
| 681 | // Read the string in the pair (dictionary). |
| 682 | return 0; |
| 683 | })); |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 684 | |
| 685 | // std::map |
| 686 | EXPECT_CALL(sdbus_mock, |
| 687 | sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}"))) |
| 688 | .WillOnce(Return(0)); |
| 689 | |
| 690 | // while !at_end() |
| 691 | EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0)) |
| 692 | .WillOnce(Return(0)) |
| 693 | .WillOnce(Return(1)); // So it exits the loop after reading one pair. |
| 694 | |
| 695 | // std::pair |
| 696 | EXPECT_CALL(sdbus_mock, |
| 697 | sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv"))) |
| 698 | .WillOnce(Return(0)); |
| 699 | |
| 700 | EXPECT_CALL(sdbus_mock, |
| 701 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("x"))) |
| 702 | .WillOnce(Return(0)); |
| 703 | EXPECT_CALL(sdbus_mock, |
| 704 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("d"))) |
| 705 | .WillOnce(Return(0)); |
| 706 | EXPECT_CALL(sdbus_mock, |
| 707 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("b"))) |
| 708 | .WillOnce(Return(1)); |
| 709 | EXPECT_CALL(sdbus_mock, |
| 710 | sd_bus_message_enter_container(IsNull(), 'v', StrEq("b"))) |
| 711 | .WillOnce(Return(0)); |
| 712 | |
| 713 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'b', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 714 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 715 | [[maybe_unused]] char type, void* p) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 716 | bool* s = static_cast<bool*>(p); |
| 717 | *s = asserted; |
| 718 | return 0; |
| 719 | })); |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 720 | |
| 721 | EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull())) |
| 722 | .WillOnce(Return(0)) /* variant. */ |
| 723 | .WillOnce(Return(0)) /* std::pair */ |
| 724 | .WillOnce(Return(0)); /* std::map */ |
| 725 | |
| 726 | int rv = handleSensorValue(msg, passive); |
| 727 | EXPECT_EQ(rv, 0); // It's always 0. |
| 728 | bool failed = passive->getFailed(); |
| 729 | EXPECT_EQ(failed, false); |
| 730 | ReadReturn r = passive->read(); |
| 731 | EXPECT_FALSE(std::isfinite(r.value)); |
| 732 | } |
| 733 | |
| 734 | TEST_F(DbusPassiveTestUnaSensorNotAsFailedObj, VerifyAvailableAssert) |
| 735 | { |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 736 | // Verifies when a sensor's state goes from unavailble to available |
| 737 | // && unavailableAsFailed == false, this sensor remains at OK state. |
| 738 | EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull())) |
| 739 | .WillOnce(Return(nullptr)); |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 740 | sdbusplus::message_t msg(nullptr, &sdbus_mock); |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 741 | |
| 742 | const char* property = "Available"; |
| 743 | bool asserted = true; |
| 744 | const char* intf = "xyz.openbmc_project.State.Decorator.Availability"; |
| 745 | |
| 746 | passive->setAvailable(false); |
| 747 | bool failed = passive->getFailed(); |
| 748 | EXPECT_EQ(failed, false); |
| 749 | |
| 750 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 751 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 752 | [[maybe_unused]] char type, void* p) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 753 | const char** s = static_cast<const char**>(p); |
| 754 | // Read the first parameter, the string. |
| 755 | *s = intf; |
| 756 | return 0; |
| 757 | })) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 758 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 759 | [[maybe_unused]] char type, void* p) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 760 | const char** s = static_cast<const char**>(p); |
| 761 | *s = property; |
| 762 | // Read the string in the pair (dictionary). |
| 763 | return 0; |
| 764 | })); |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 765 | |
| 766 | // std::map |
| 767 | EXPECT_CALL(sdbus_mock, |
| 768 | sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}"))) |
| 769 | .WillOnce(Return(0)); |
| 770 | |
| 771 | // while !at_end() |
| 772 | EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0)) |
| 773 | .WillOnce(Return(0)) |
| 774 | .WillOnce(Return(1)); // So it exits the loop after reading one pair. |
| 775 | |
| 776 | // std::pair |
| 777 | EXPECT_CALL(sdbus_mock, |
| 778 | sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv"))) |
| 779 | .WillOnce(Return(0)); |
| 780 | |
| 781 | EXPECT_CALL(sdbus_mock, |
| 782 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("x"))) |
| 783 | .WillOnce(Return(0)); |
| 784 | EXPECT_CALL(sdbus_mock, |
| 785 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("d"))) |
| 786 | .WillOnce(Return(0)); |
| 787 | EXPECT_CALL(sdbus_mock, |
| 788 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("b"))) |
| 789 | .WillOnce(Return(1)); |
| 790 | EXPECT_CALL(sdbus_mock, |
| 791 | sd_bus_message_enter_container(IsNull(), 'v', StrEq("b"))) |
| 792 | .WillOnce(Return(0)); |
| 793 | |
| 794 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'b', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 795 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 796 | [[maybe_unused]] char type, void* p) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 797 | bool* s = static_cast<bool*>(p); |
| 798 | *s = asserted; |
| 799 | return 0; |
| 800 | })); |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 801 | |
| 802 | EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull())) |
| 803 | .WillOnce(Return(0)) /* variant. */ |
| 804 | .WillOnce(Return(0)) /* std::pair */ |
| 805 | .WillOnce(Return(0)); /* std::map */ |
| 806 | |
| 807 | int rv = handleSensorValue(msg, passive); |
| 808 | EXPECT_EQ(rv, 0); // It's always 0. |
| 809 | failed = passive->getFailed(); |
| 810 | EXPECT_EQ(failed, false); |
| 811 | } |
| 812 | |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 813 | void GetPropertiesMax3k([[maybe_unused]] const std::string& service, |
| 814 | [[maybe_unused]] const std::string& path, |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 815 | SensorProperties* prop) |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 816 | { |
| 817 | prop->scale = -3; |
| 818 | prop->value = 10; |
| 819 | prop->unit = "x"; |
| 820 | prop->min = 0; |
| 821 | prop->max = 3000; |
| 822 | } |
| 823 | |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 824 | using GetPropertiesFunction = std::function<void( |
| 825 | const std::string&, const std::string&, SensorProperties*)>; |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 826 | |
| 827 | // TODO: There is definitely a cleaner way to do this. |
| 828 | class DbusPassiveTest3kMaxObj : public ::testing::Test |
| 829 | { |
| 830 | protected: |
| 831 | DbusPassiveTest3kMaxObj() : |
| 832 | sdbus_mock(), |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 833 | bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))), |
| 834 | helper(std::make_unique<DbusHelperMock>()) |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 835 | { |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 836 | EXPECT_CALL(*helper, getService(StrEq(SensorIntf), StrEq(path))) |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 837 | .WillOnce(Return("asdf")); |
| 838 | |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 839 | EXPECT_CALL(*helper, |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 840 | getProperties(StrEq("asdf"), StrEq(path), NotNull())) |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 841 | .WillOnce(_getProps); |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 842 | EXPECT_CALL(*helper, thresholdsAsserted(StrEq("asdf"), StrEq(path))) |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 843 | .WillOnce(Return(false)); |
| 844 | |
| 845 | auto info = conf::SensorConfig(); |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 846 | ri = DbusPassive::createDbusPassive(bus_mock, type, id, |
| 847 | std::move(helper), &info, nullptr); |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 848 | passive = reinterpret_cast<DbusPassive*>(ri.get()); |
| 849 | EXPECT_FALSE(passive == nullptr); |
| 850 | } |
| 851 | |
| 852 | sdbusplus::SdBusMock sdbus_mock; |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 853 | sdbusplus::bus_t bus_mock; |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 854 | std::unique_ptr<DbusHelperMock> helper; |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 855 | std::string type = "temp"; |
| 856 | std::string id = "id"; |
| 857 | std::string path = "/xyz/openbmc_project/sensors/temperature/id"; |
| 858 | int64_t _scale = -3; |
| 859 | int64_t _value = 10; |
| 860 | |
| 861 | std::unique_ptr<ReadInterface> ri; |
| 862 | DbusPassive* passive; |
| 863 | GetPropertiesFunction _getProps = &GetPropertiesMax3k; |
| 864 | }; |
| 865 | |
| 866 | TEST_F(DbusPassiveTest3kMaxObj, ReadMinAndMaxReturnsExpected) |
| 867 | { |
| 868 | EXPECT_DOUBLE_EQ(0, passive->getMin()); |
| 869 | EXPECT_DOUBLE_EQ(3, passive->getMax()); |
| 870 | } |
| 871 | |
| 872 | class DbusPassiveTest3kMaxIgnoredObj : public ::testing::Test |
| 873 | { |
| 874 | protected: |
| 875 | DbusPassiveTest3kMaxIgnoredObj() : |
| 876 | sdbus_mock(), |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 877 | bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))), |
| 878 | helper(std::make_unique<DbusHelperMock>()) |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 879 | { |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 880 | EXPECT_CALL(*helper, getService(StrEq(SensorIntf), StrEq(path))) |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 881 | .WillOnce(Return("asdf")); |
| 882 | |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 883 | EXPECT_CALL(*helper, |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 884 | getProperties(StrEq("asdf"), StrEq(path), NotNull())) |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 885 | .WillOnce(_getProps); |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 886 | EXPECT_CALL(*helper, thresholdsAsserted(StrEq("asdf"), StrEq(path))) |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 887 | .WillOnce(Return(false)); |
| 888 | |
| 889 | auto info = conf::SensorConfig(); |
| 890 | info.ignoreDbusMinMax = true; |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 891 | ri = DbusPassive::createDbusPassive(bus_mock, type, id, |
| 892 | std::move(helper), &info, nullptr); |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 893 | passive = reinterpret_cast<DbusPassive*>(ri.get()); |
| 894 | EXPECT_FALSE(passive == nullptr); |
| 895 | } |
| 896 | |
| 897 | sdbusplus::SdBusMock sdbus_mock; |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 898 | sdbusplus::bus_t bus_mock; |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 899 | std::unique_ptr<DbusHelperMock> helper; |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 900 | std::string type = "temp"; |
| 901 | std::string id = "id"; |
| 902 | std::string path = "/xyz/openbmc_project/sensors/temperature/id"; |
| 903 | int64_t _scale = -3; |
| 904 | int64_t _value = 10; |
| 905 | |
| 906 | std::unique_ptr<ReadInterface> ri; |
| 907 | DbusPassive* passive; |
| 908 | GetPropertiesFunction _getProps = &GetPropertiesMax3k; |
| 909 | }; |
| 910 | |
| 911 | TEST_F(DbusPassiveTest3kMaxIgnoredObj, ReadMinAndMaxReturnsExpected) |
| 912 | { |
| 913 | EXPECT_DOUBLE_EQ(0, passive->getMin()); |
| 914 | EXPECT_DOUBLE_EQ(0, passive->getMax()); |
| 915 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 916 | |
| 917 | } // namespace |
| 918 | } // namespace pid_control |