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) { |
| 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 Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 171 | const char** s = static_cast<const char**>(p); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 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 Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 178 | const char** s = static_cast<const char**>(p); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 179 | *s = Value; |
| 180 | // Read the string in the pair (dictionary). |
| 181 | return 0; |
| 182 | })); |
| 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 Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 209 | int64_t* s = static_cast<int64_t*>(p); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 210 | *s = xValue; |
| 211 | return 0; |
| 212 | })); |
| 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 Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 244 | const char** s = static_cast<const char**>(p); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 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 Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 251 | const char** s = static_cast<const char**>(p); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 252 | *s = Scale; |
| 253 | // Read the string in the pair (dictionary). |
| 254 | return 0; |
| 255 | })); |
| 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 Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 282 | int64_t* s = static_cast<int64_t*>(p); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 283 | *s = xScale; |
| 284 | return 0; |
| 285 | })); |
| 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 | { |
| 301 | |
| 302 | // Verifies when a threshold is crossed the sensor goes into error state |
| 303 | EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull())) |
| 304 | .WillOnce(Return(nullptr)); |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 305 | sdbusplus::message_t msg(nullptr, &sdbus_mock); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 306 | |
| 307 | const char* criticalAlarm = "CriticalAlarmHigh"; |
| 308 | bool alarm = true; |
| 309 | const char* intf = "xyz.openbmc_project.Sensor.Threshold.Critical"; |
| 310 | |
| 311 | passive->setFailed(false); |
| 312 | |
| 313 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 314 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 315 | [[maybe_unused]] char type, void* p) { |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 316 | const char** s = static_cast<const char**>(p); |
| 317 | // Read the first parameter, the string. |
| 318 | *s = intf; |
| 319 | return 0; |
| 320 | })) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 321 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 322 | [[maybe_unused]] char type, void* p) { |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 323 | const char** s = static_cast<const char**>(p); |
| 324 | *s = criticalAlarm; |
| 325 | // Read the string in the pair (dictionary). |
| 326 | return 0; |
| 327 | })); |
| 328 | |
| 329 | // std::map |
| 330 | EXPECT_CALL(sdbus_mock, |
| 331 | sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}"))) |
| 332 | .WillOnce(Return(0)); |
| 333 | |
| 334 | // while !at_end() |
| 335 | EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0)) |
| 336 | .WillOnce(Return(0)) |
| 337 | .WillOnce(Return(1)); // So it exits the loop after reading one pair. |
| 338 | |
| 339 | // std::pair |
| 340 | EXPECT_CALL(sdbus_mock, |
| 341 | sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv"))) |
| 342 | .WillOnce(Return(0)); |
| 343 | |
| 344 | EXPECT_CALL(sdbus_mock, |
| 345 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("x"))) |
| 346 | .WillOnce(Return(0)); |
| 347 | EXPECT_CALL(sdbus_mock, |
| 348 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("d"))) |
| 349 | .WillOnce(Return(0)); |
| 350 | EXPECT_CALL(sdbus_mock, |
| 351 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("b"))) |
| 352 | .WillOnce(Return(1)); |
| 353 | EXPECT_CALL(sdbus_mock, |
| 354 | sd_bus_message_enter_container(IsNull(), 'v', StrEq("b"))) |
| 355 | .WillOnce(Return(0)); |
| 356 | |
| 357 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'b', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 358 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 359 | [[maybe_unused]] char type, void* p) { |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 360 | bool* s = static_cast<bool*>(p); |
| 361 | *s = alarm; |
| 362 | return 0; |
| 363 | })); |
| 364 | |
| 365 | EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull())) |
| 366 | .WillOnce(Return(0)) /* variant. */ |
| 367 | .WillOnce(Return(0)) /* std::pair */ |
| 368 | .WillOnce(Return(0)); /* std::map */ |
| 369 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 370 | int rv = handleSensorValue(msg, passive); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 371 | EXPECT_EQ(rv, 0); // It's always 0. |
| 372 | bool failed = passive->getFailed(); |
| 373 | EXPECT_EQ(failed, true); |
| 374 | } |
| 375 | |
| 376 | TEST_F(DbusPassiveTestObj, VerifyCriticalThresholdDeassert) |
| 377 | { |
| 378 | |
| 379 | // Verifies when a threshold is deasserted a failed sensor goes back into |
| 380 | // the normal state |
| 381 | EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull())) |
| 382 | .WillOnce(Return(nullptr)); |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 383 | sdbusplus::message_t msg(nullptr, &sdbus_mock); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 384 | |
| 385 | const char* criticalAlarm = "CriticalAlarmHigh"; |
| 386 | bool alarm = false; |
| 387 | const char* intf = "xyz.openbmc_project.Sensor.Threshold.Critical"; |
| 388 | |
| 389 | passive->setFailed(true); |
| 390 | |
| 391 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 392 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 393 | [[maybe_unused]] char type, void* p) { |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 394 | const char** s = static_cast<const char**>(p); |
| 395 | // Read the first parameter, the string. |
| 396 | *s = intf; |
| 397 | return 0; |
| 398 | })) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 399 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 400 | [[maybe_unused]] char type, void* p) { |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 401 | const char** s = static_cast<const char**>(p); |
| 402 | *s = criticalAlarm; |
| 403 | // Read the string in the pair (dictionary). |
| 404 | return 0; |
| 405 | })); |
| 406 | |
| 407 | // std::map |
| 408 | EXPECT_CALL(sdbus_mock, |
| 409 | sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}"))) |
| 410 | .WillOnce(Return(0)); |
| 411 | |
| 412 | // while !at_end() |
| 413 | EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0)) |
| 414 | .WillOnce(Return(0)) |
| 415 | .WillOnce(Return(1)); // So it exits the loop after reading one pair. |
| 416 | |
| 417 | // std::pair |
| 418 | EXPECT_CALL(sdbus_mock, |
| 419 | sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv"))) |
| 420 | .WillOnce(Return(0)); |
| 421 | |
| 422 | EXPECT_CALL(sdbus_mock, |
| 423 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("x"))) |
| 424 | .WillOnce(Return(0)); |
| 425 | EXPECT_CALL(sdbus_mock, |
| 426 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("d"))) |
| 427 | .WillOnce(Return(0)); |
| 428 | EXPECT_CALL(sdbus_mock, |
| 429 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("b"))) |
| 430 | .WillOnce(Return(1)); |
| 431 | EXPECT_CALL(sdbus_mock, |
| 432 | sd_bus_message_enter_container(IsNull(), 'v', StrEq("b"))) |
| 433 | .WillOnce(Return(0)); |
| 434 | |
| 435 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'b', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 436 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 437 | [[maybe_unused]] char type, void* p) { |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 438 | bool* s = static_cast<bool*>(p); |
| 439 | *s = alarm; |
| 440 | return 0; |
| 441 | })); |
| 442 | |
| 443 | EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull())) |
| 444 | .WillOnce(Return(0)) /* variant. */ |
| 445 | .WillOnce(Return(0)) /* std::pair */ |
| 446 | .WillOnce(Return(0)); /* std::map */ |
| 447 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 448 | int rv = handleSensorValue(msg, passive); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 449 | EXPECT_EQ(rv, 0); // It's always 0. |
| 450 | bool failed = passive->getFailed(); |
| 451 | EXPECT_EQ(failed, false); |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 452 | } |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 453 | |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 454 | TEST_F(DbusPassiveTestObj, VerifyAvailableDeassert) |
| 455 | { |
| 456 | |
| 457 | // Verifies when Availble is deasserted && unavailableAsFailed == true, |
| 458 | // the sensor goes into error state |
| 459 | EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull())) |
| 460 | .WillOnce(Return(nullptr)); |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 461 | sdbusplus::message_t msg(nullptr, &sdbus_mock); |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 462 | |
| 463 | const char* property = "Available"; |
| 464 | bool asserted = false; |
| 465 | const char* intf = "xyz.openbmc_project.State.Decorator.Availability"; |
| 466 | |
| 467 | passive->setAvailable(true); |
| 468 | |
| 469 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 470 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 471 | [[maybe_unused]] char type, void* p) { |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 472 | const char** s = static_cast<const char**>(p); |
| 473 | // Read the first parameter, the string. |
| 474 | *s = intf; |
| 475 | return 0; |
| 476 | })) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 477 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 478 | [[maybe_unused]] char type, void* p) { |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 479 | const char** s = static_cast<const char**>(p); |
| 480 | *s = property; |
| 481 | // Read the string in the pair (dictionary). |
| 482 | return 0; |
| 483 | })); |
| 484 | |
| 485 | // std::map |
| 486 | EXPECT_CALL(sdbus_mock, |
| 487 | sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}"))) |
| 488 | .WillOnce(Return(0)); |
| 489 | |
| 490 | // while !at_end() |
| 491 | EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0)) |
| 492 | .WillOnce(Return(0)) |
| 493 | .WillOnce(Return(1)); // So it exits the loop after reading one pair. |
| 494 | |
| 495 | // std::pair |
| 496 | EXPECT_CALL(sdbus_mock, |
| 497 | sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv"))) |
| 498 | .WillOnce(Return(0)); |
| 499 | |
| 500 | EXPECT_CALL(sdbus_mock, |
| 501 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("x"))) |
| 502 | .WillOnce(Return(0)); |
| 503 | EXPECT_CALL(sdbus_mock, |
| 504 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("d"))) |
| 505 | .WillOnce(Return(0)); |
| 506 | EXPECT_CALL(sdbus_mock, |
| 507 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("b"))) |
| 508 | .WillOnce(Return(1)); |
| 509 | EXPECT_CALL(sdbus_mock, |
| 510 | sd_bus_message_enter_container(IsNull(), 'v', StrEq("b"))) |
| 511 | .WillOnce(Return(0)); |
| 512 | |
| 513 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'b', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 514 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 515 | [[maybe_unused]] char type, void* p) { |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 516 | bool* s = static_cast<bool*>(p); |
| 517 | *s = asserted; |
| 518 | return 0; |
| 519 | })); |
| 520 | |
| 521 | EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull())) |
| 522 | .WillOnce(Return(0)) /* variant. */ |
| 523 | .WillOnce(Return(0)) /* std::pair */ |
| 524 | .WillOnce(Return(0)); /* std::map */ |
| 525 | |
| 526 | int rv = handleSensorValue(msg, passive); |
| 527 | EXPECT_EQ(rv, 0); // It's always 0. |
| 528 | bool failed = passive->getFailed(); |
| 529 | EXPECT_EQ(failed, true); |
| 530 | } |
| 531 | |
| 532 | TEST_F(DbusPassiveTestObj, VerifyAvailableAssert) |
| 533 | { |
| 534 | |
| 535 | // Verifies when Availble is asserted && unavailableAsFailed == true, |
| 536 | // an error sensor goes back to normal state |
| 537 | EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull())) |
| 538 | .WillOnce(Return(nullptr)); |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 539 | sdbusplus::message_t msg(nullptr, &sdbus_mock); |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 540 | |
| 541 | const char* property = "Available"; |
| 542 | bool asserted = true; |
| 543 | const char* intf = "xyz.openbmc_project.State.Decorator.Availability"; |
| 544 | |
| 545 | passive->setAvailable(false); |
| 546 | bool failed = passive->getFailed(); |
| 547 | EXPECT_EQ(failed, true); |
| 548 | |
| 549 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 550 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 551 | [[maybe_unused]] char type, void* p) { |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 552 | const char** s = static_cast<const char**>(p); |
| 553 | // Read the first parameter, the string. |
| 554 | *s = intf; |
| 555 | return 0; |
| 556 | })) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 557 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 558 | [[maybe_unused]] char type, void* p) { |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 559 | const char** s = static_cast<const char**>(p); |
| 560 | *s = property; |
| 561 | // Read the string in the pair (dictionary). |
| 562 | return 0; |
| 563 | })); |
| 564 | |
| 565 | // std::map |
| 566 | EXPECT_CALL(sdbus_mock, |
| 567 | sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}"))) |
| 568 | .WillOnce(Return(0)); |
| 569 | |
| 570 | // while !at_end() |
| 571 | EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0)) |
| 572 | .WillOnce(Return(0)) |
| 573 | .WillOnce(Return(1)); // So it exits the loop after reading one pair. |
| 574 | |
| 575 | // std::pair |
| 576 | EXPECT_CALL(sdbus_mock, |
| 577 | sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv"))) |
| 578 | .WillOnce(Return(0)); |
| 579 | |
| 580 | EXPECT_CALL(sdbus_mock, |
| 581 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("x"))) |
| 582 | .WillOnce(Return(0)); |
| 583 | EXPECT_CALL(sdbus_mock, |
| 584 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("d"))) |
| 585 | .WillOnce(Return(0)); |
| 586 | EXPECT_CALL(sdbus_mock, |
| 587 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("b"))) |
| 588 | .WillOnce(Return(1)); |
| 589 | EXPECT_CALL(sdbus_mock, |
| 590 | sd_bus_message_enter_container(IsNull(), 'v', StrEq("b"))) |
| 591 | .WillOnce(Return(0)); |
| 592 | |
| 593 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'b', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 594 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 595 | [[maybe_unused]] char type, void* p) { |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 596 | bool* s = static_cast<bool*>(p); |
| 597 | *s = asserted; |
| 598 | return 0; |
| 599 | })); |
| 600 | |
| 601 | EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull())) |
| 602 | .WillOnce(Return(0)) /* variant. */ |
| 603 | .WillOnce(Return(0)) /* std::pair */ |
| 604 | .WillOnce(Return(0)); /* std::map */ |
| 605 | |
| 606 | int rv = handleSensorValue(msg, passive); |
| 607 | EXPECT_EQ(rv, 0); // It's always 0. |
| 608 | failed = passive->getFailed(); |
| 609 | EXPECT_EQ(failed, false); |
| 610 | } |
| 611 | |
| 612 | class DbusPassiveTestUnaSensorNotAsFailedObj : public ::testing::Test |
| 613 | { |
| 614 | protected: |
| 615 | DbusPassiveTestUnaSensorNotAsFailedObj() : |
| 616 | sdbus_mock(), |
| 617 | bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))), |
| 618 | helper(std::make_unique<DbusHelperMock>()) |
| 619 | { |
| 620 | EXPECT_CALL(*helper, getService(StrEq(SensorIntf), StrEq(path))) |
| 621 | .WillOnce(Return("asdf")); |
| 622 | |
| 623 | EXPECT_CALL(*helper, |
| 624 | getProperties(StrEq("asdf"), StrEq(path), NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 625 | .WillOnce(Invoke([&]([[maybe_unused]] const std::string& service, |
| 626 | [[maybe_unused]] const std::string& path, |
| 627 | SensorProperties* prop) { |
| 628 | prop->scale = _scale; |
| 629 | prop->value = _value; |
| 630 | prop->unit = "x"; |
| 631 | prop->min = 0; |
| 632 | prop->max = 0; |
| 633 | prop->available = true; |
| 634 | })); |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 635 | EXPECT_CALL(*helper, thresholdsAsserted(StrEq("asdf"), StrEq(path))) |
| 636 | .WillOnce(Return(false)); |
| 637 | |
| 638 | auto info = conf::SensorConfig(); |
| 639 | info.unavailableAsFailed = false; |
| 640 | ri = DbusPassive::createDbusPassive(bus_mock, type, id, |
| 641 | std::move(helper), &info, nullptr); |
| 642 | passive = reinterpret_cast<DbusPassive*>(ri.get()); |
| 643 | EXPECT_FALSE(passive == nullptr); |
| 644 | } |
| 645 | |
| 646 | sdbusplus::SdBusMock sdbus_mock; |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 647 | sdbusplus::bus_t bus_mock; |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 648 | std::unique_ptr<DbusHelperMock> helper; |
| 649 | std::string type = "temp"; |
| 650 | std::string id = "id"; |
| 651 | std::string path = "/xyz/openbmc_project/sensors/temperature/id"; |
| 652 | int64_t _scale = -3; |
| 653 | int64_t _value = 10; |
| 654 | |
| 655 | std::unique_ptr<ReadInterface> ri; |
| 656 | DbusPassive* passive; |
| 657 | }; |
| 658 | |
| 659 | TEST_F(DbusPassiveTestUnaSensorNotAsFailedObj, VerifyAvailableDeassert) |
| 660 | { |
| 661 | |
| 662 | // Verifies when Availble is deasserted && unavailableAsFailed == false, |
| 663 | // the sensor remains at OK state but reading goes to NaN. |
| 664 | EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull())) |
| 665 | .WillOnce(Return(nullptr)); |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 666 | sdbusplus::message_t msg(nullptr, &sdbus_mock); |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 667 | |
| 668 | const char* property = "Available"; |
| 669 | bool asserted = false; |
| 670 | const char* intf = "xyz.openbmc_project.State.Decorator.Availability"; |
| 671 | |
| 672 | passive->setAvailable(true); |
| 673 | |
| 674 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 675 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 676 | [[maybe_unused]] char type, void* p) { |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 677 | const char** s = static_cast<const char**>(p); |
| 678 | // Read the first parameter, the string. |
| 679 | *s = intf; |
| 680 | return 0; |
| 681 | })) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 682 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 683 | [[maybe_unused]] char type, void* p) { |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 684 | const char** s = static_cast<const char**>(p); |
| 685 | *s = property; |
| 686 | // Read the string in the pair (dictionary). |
| 687 | return 0; |
| 688 | })); |
| 689 | |
| 690 | // std::map |
| 691 | EXPECT_CALL(sdbus_mock, |
| 692 | sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}"))) |
| 693 | .WillOnce(Return(0)); |
| 694 | |
| 695 | // while !at_end() |
| 696 | EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0)) |
| 697 | .WillOnce(Return(0)) |
| 698 | .WillOnce(Return(1)); // So it exits the loop after reading one pair. |
| 699 | |
| 700 | // std::pair |
| 701 | EXPECT_CALL(sdbus_mock, |
| 702 | sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv"))) |
| 703 | .WillOnce(Return(0)); |
| 704 | |
| 705 | EXPECT_CALL(sdbus_mock, |
| 706 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("x"))) |
| 707 | .WillOnce(Return(0)); |
| 708 | EXPECT_CALL(sdbus_mock, |
| 709 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("d"))) |
| 710 | .WillOnce(Return(0)); |
| 711 | EXPECT_CALL(sdbus_mock, |
| 712 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("b"))) |
| 713 | .WillOnce(Return(1)); |
| 714 | EXPECT_CALL(sdbus_mock, |
| 715 | sd_bus_message_enter_container(IsNull(), 'v', StrEq("b"))) |
| 716 | .WillOnce(Return(0)); |
| 717 | |
| 718 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'b', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 719 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 720 | [[maybe_unused]] char type, void* p) { |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 721 | bool* s = static_cast<bool*>(p); |
| 722 | *s = asserted; |
| 723 | return 0; |
| 724 | })); |
| 725 | |
| 726 | EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull())) |
| 727 | .WillOnce(Return(0)) /* variant. */ |
| 728 | .WillOnce(Return(0)) /* std::pair */ |
| 729 | .WillOnce(Return(0)); /* std::map */ |
| 730 | |
| 731 | int rv = handleSensorValue(msg, passive); |
| 732 | EXPECT_EQ(rv, 0); // It's always 0. |
| 733 | bool failed = passive->getFailed(); |
| 734 | EXPECT_EQ(failed, false); |
| 735 | ReadReturn r = passive->read(); |
| 736 | EXPECT_FALSE(std::isfinite(r.value)); |
| 737 | } |
| 738 | |
| 739 | TEST_F(DbusPassiveTestUnaSensorNotAsFailedObj, VerifyAvailableAssert) |
| 740 | { |
| 741 | |
| 742 | // Verifies when a sensor's state goes from unavailble to available |
| 743 | // && unavailableAsFailed == false, this sensor remains at OK state. |
| 744 | EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull())) |
| 745 | .WillOnce(Return(nullptr)); |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 746 | sdbusplus::message_t msg(nullptr, &sdbus_mock); |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 747 | |
| 748 | const char* property = "Available"; |
| 749 | bool asserted = true; |
| 750 | const char* intf = "xyz.openbmc_project.State.Decorator.Availability"; |
| 751 | |
| 752 | passive->setAvailable(false); |
| 753 | bool failed = passive->getFailed(); |
| 754 | EXPECT_EQ(failed, false); |
| 755 | |
| 756 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 757 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 758 | [[maybe_unused]] char type, void* p) { |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 759 | const char** s = static_cast<const char**>(p); |
| 760 | // Read the first parameter, the string. |
| 761 | *s = intf; |
| 762 | return 0; |
| 763 | })) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 764 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 765 | [[maybe_unused]] char type, void* p) { |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 766 | const char** s = static_cast<const char**>(p); |
| 767 | *s = property; |
| 768 | // Read the string in the pair (dictionary). |
| 769 | return 0; |
| 770 | })); |
| 771 | |
| 772 | // std::map |
| 773 | EXPECT_CALL(sdbus_mock, |
| 774 | sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}"))) |
| 775 | .WillOnce(Return(0)); |
| 776 | |
| 777 | // while !at_end() |
| 778 | EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0)) |
| 779 | .WillOnce(Return(0)) |
| 780 | .WillOnce(Return(1)); // So it exits the loop after reading one pair. |
| 781 | |
| 782 | // std::pair |
| 783 | EXPECT_CALL(sdbus_mock, |
| 784 | sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv"))) |
| 785 | .WillOnce(Return(0)); |
| 786 | |
| 787 | EXPECT_CALL(sdbus_mock, |
| 788 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("x"))) |
| 789 | .WillOnce(Return(0)); |
| 790 | EXPECT_CALL(sdbus_mock, |
| 791 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("d"))) |
| 792 | .WillOnce(Return(0)); |
| 793 | EXPECT_CALL(sdbus_mock, |
| 794 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("b"))) |
| 795 | .WillOnce(Return(1)); |
| 796 | EXPECT_CALL(sdbus_mock, |
| 797 | sd_bus_message_enter_container(IsNull(), 'v', StrEq("b"))) |
| 798 | .WillOnce(Return(0)); |
| 799 | |
| 800 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'b', NotNull())) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 801 | .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m, |
| 802 | [[maybe_unused]] char type, void* p) { |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 803 | bool* s = static_cast<bool*>(p); |
| 804 | *s = asserted; |
| 805 | return 0; |
| 806 | })); |
| 807 | |
| 808 | EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull())) |
| 809 | .WillOnce(Return(0)) /* variant. */ |
| 810 | .WillOnce(Return(0)) /* std::pair */ |
| 811 | .WillOnce(Return(0)); /* std::map */ |
| 812 | |
| 813 | int rv = handleSensorValue(msg, passive); |
| 814 | EXPECT_EQ(rv, 0); // It's always 0. |
| 815 | failed = passive->getFailed(); |
| 816 | EXPECT_EQ(failed, false); |
| 817 | } |
| 818 | |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 819 | void GetPropertiesMax3k([[maybe_unused]] const std::string& service, |
| 820 | [[maybe_unused]] const std::string& path, |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 821 | SensorProperties* prop) |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 822 | { |
| 823 | prop->scale = -3; |
| 824 | prop->value = 10; |
| 825 | prop->unit = "x"; |
| 826 | prop->min = 0; |
| 827 | prop->max = 3000; |
| 828 | } |
| 829 | |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 830 | using GetPropertiesFunction = std::function<void( |
| 831 | const std::string&, const std::string&, SensorProperties*)>; |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 832 | |
| 833 | // TODO: There is definitely a cleaner way to do this. |
| 834 | class DbusPassiveTest3kMaxObj : public ::testing::Test |
| 835 | { |
| 836 | protected: |
| 837 | DbusPassiveTest3kMaxObj() : |
| 838 | sdbus_mock(), |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 839 | bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))), |
| 840 | helper(std::make_unique<DbusHelperMock>()) |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 841 | { |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 842 | EXPECT_CALL(*helper, getService(StrEq(SensorIntf), StrEq(path))) |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 843 | .WillOnce(Return("asdf")); |
| 844 | |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 845 | EXPECT_CALL(*helper, |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 846 | getProperties(StrEq("asdf"), StrEq(path), NotNull())) |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 847 | .WillOnce(_getProps); |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 848 | EXPECT_CALL(*helper, thresholdsAsserted(StrEq("asdf"), StrEq(path))) |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 849 | .WillOnce(Return(false)); |
| 850 | |
| 851 | auto info = conf::SensorConfig(); |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 852 | ri = DbusPassive::createDbusPassive(bus_mock, type, id, |
| 853 | std::move(helper), &info, nullptr); |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 854 | passive = reinterpret_cast<DbusPassive*>(ri.get()); |
| 855 | EXPECT_FALSE(passive == nullptr); |
| 856 | } |
| 857 | |
| 858 | sdbusplus::SdBusMock sdbus_mock; |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 859 | sdbusplus::bus_t bus_mock; |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 860 | std::unique_ptr<DbusHelperMock> helper; |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 861 | std::string type = "temp"; |
| 862 | std::string id = "id"; |
| 863 | std::string path = "/xyz/openbmc_project/sensors/temperature/id"; |
| 864 | int64_t _scale = -3; |
| 865 | int64_t _value = 10; |
| 866 | |
| 867 | std::unique_ptr<ReadInterface> ri; |
| 868 | DbusPassive* passive; |
| 869 | GetPropertiesFunction _getProps = &GetPropertiesMax3k; |
| 870 | }; |
| 871 | |
| 872 | TEST_F(DbusPassiveTest3kMaxObj, ReadMinAndMaxReturnsExpected) |
| 873 | { |
| 874 | EXPECT_DOUBLE_EQ(0, passive->getMin()); |
| 875 | EXPECT_DOUBLE_EQ(3, passive->getMax()); |
| 876 | } |
| 877 | |
| 878 | class DbusPassiveTest3kMaxIgnoredObj : public ::testing::Test |
| 879 | { |
| 880 | protected: |
| 881 | DbusPassiveTest3kMaxIgnoredObj() : |
| 882 | sdbus_mock(), |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 883 | bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))), |
| 884 | helper(std::make_unique<DbusHelperMock>()) |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 885 | { |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 886 | EXPECT_CALL(*helper, getService(StrEq(SensorIntf), StrEq(path))) |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 887 | .WillOnce(Return("asdf")); |
| 888 | |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 889 | EXPECT_CALL(*helper, |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 890 | getProperties(StrEq("asdf"), StrEq(path), NotNull())) |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 891 | .WillOnce(_getProps); |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 892 | EXPECT_CALL(*helper, thresholdsAsserted(StrEq("asdf"), StrEq(path))) |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 893 | .WillOnce(Return(false)); |
| 894 | |
| 895 | auto info = conf::SensorConfig(); |
| 896 | info.ignoreDbusMinMax = true; |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 897 | ri = DbusPassive::createDbusPassive(bus_mock, type, id, |
| 898 | std::move(helper), &info, nullptr); |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 899 | passive = reinterpret_cast<DbusPassive*>(ri.get()); |
| 900 | EXPECT_FALSE(passive == nullptr); |
| 901 | } |
| 902 | |
| 903 | sdbusplus::SdBusMock sdbus_mock; |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 904 | sdbusplus::bus_t bus_mock; |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 905 | std::unique_ptr<DbusHelperMock> helper; |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 906 | std::string type = "temp"; |
| 907 | std::string id = "id"; |
| 908 | std::string path = "/xyz/openbmc_project/sensors/temperature/id"; |
| 909 | int64_t _scale = -3; |
| 910 | int64_t _value = 10; |
| 911 | |
| 912 | std::unique_ptr<ReadInterface> ri; |
| 913 | DbusPassive* passive; |
| 914 | GetPropertiesFunction _getProps = &GetPropertiesMax3k; |
| 915 | }; |
| 916 | |
| 917 | TEST_F(DbusPassiveTest3kMaxIgnoredObj, ReadMinAndMaxReturnsExpected) |
| 918 | { |
| 919 | EXPECT_DOUBLE_EQ(0, passive->getMin()); |
| 920 | EXPECT_DOUBLE_EQ(0, passive->getMax()); |
| 921 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 922 | |
| 923 | } // namespace |
| 924 | } // namespace pid_control |