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 | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 8 | #include <string> |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 9 | #include <variant> |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 10 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 11 | #include <gmock/gmock.h> |
| 12 | #include <gtest/gtest.h> |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 13 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 14 | using ::testing::_; |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 15 | using ::testing::InSequence; |
| 16 | using ::testing::Invoke; |
| 17 | using ::testing::IsNull; |
| 18 | using ::testing::NotNull; |
| 19 | using ::testing::Return; |
| 20 | using ::testing::StrEq; |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 21 | |
| 22 | std::string SensorIntf = "xyz.openbmc_project.Sensor.Value"; |
| 23 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 24 | TEST(DbusPassiveTest, FactoryFailsWithInvalidType) |
| 25 | { |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 26 | // Verify the type is checked by the factory. |
| 27 | |
| 28 | sdbusplus::SdBusMock sdbus_mock; |
| 29 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 30 | std::string type = "invalid"; |
| 31 | std::string id = "id"; |
| 32 | |
| 33 | DbusHelperMock helper; |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 34 | auto info = conf::SensorConfig(); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 35 | |
James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 36 | std::unique_ptr<ReadInterface> ri = DbusPassive::createDbusPassive( |
| 37 | bus_mock, type, id, &helper, &info, nullptr); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 38 | |
| 39 | EXPECT_EQ(ri, nullptr); |
| 40 | } |
| 41 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 42 | TEST(DbusPassiveTest, BoringConstructorTest) |
| 43 | { |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 44 | // Simply build the object, does no error checking. |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 45 | |
| 46 | sdbusplus::SdBusMock sdbus_mock; |
| 47 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 48 | std::string type = "invalid"; |
| 49 | std::string id = "id"; |
| 50 | std::string path = "/xyz/openbmc_project/sensors/unknown/id"; |
| 51 | |
| 52 | DbusHelperMock helper; |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 53 | struct SensorProperties properties; |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 54 | |
James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 55 | DbusPassive(bus_mock, type, id, &helper, properties, false, path, nullptr); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 56 | // Success |
| 57 | } |
| 58 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 59 | class DbusPassiveTestObj : public ::testing::Test |
| 60 | { |
| 61 | protected: |
| 62 | DbusPassiveTestObj() : |
| 63 | sdbus_mock(), |
| 64 | bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))), helper() |
| 65 | { |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 66 | EXPECT_CALL(helper, getService(_, StrEq(SensorIntf), StrEq(path))) |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 67 | .WillOnce(Return("asdf")); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 68 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 69 | EXPECT_CALL(helper, |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 70 | getProperties(_, StrEq("asdf"), StrEq(path), NotNull())) |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 71 | .WillOnce(Invoke( |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 72 | [&](sdbusplus::bus::bus& bus, const std::string& service, |
| 73 | const std::string& path, struct SensorProperties* prop) { |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 74 | prop->scale = _scale; |
| 75 | prop->value = _value; |
| 76 | prop->unit = "x"; |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 77 | prop->min = 0; |
| 78 | prop->max = 0; |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 79 | })); |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 80 | EXPECT_CALL(helper, thresholdsAsserted(_, StrEq("asdf"), StrEq(path))) |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 81 | .WillOnce(Return(false)); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 82 | |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 83 | auto info = conf::SensorConfig(); |
James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 84 | ri = DbusPassive::createDbusPassive(bus_mock, type, id, &helper, &info, |
| 85 | nullptr); |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 86 | passive = reinterpret_cast<DbusPassive*>(ri.get()); |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 87 | EXPECT_FALSE(passive == nullptr); |
| 88 | } |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 89 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 90 | sdbusplus::SdBusMock sdbus_mock; |
| 91 | sdbusplus::bus::bus bus_mock; |
| 92 | DbusHelperMock helper; |
| 93 | std::string type = "temp"; |
| 94 | std::string id = "id"; |
| 95 | std::string path = "/xyz/openbmc_project/sensors/temperature/id"; |
| 96 | int64_t _scale = -3; |
| 97 | int64_t _value = 10; |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 98 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 99 | std::unique_ptr<ReadInterface> ri; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 100 | DbusPassive* passive; |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 103 | TEST_F(DbusPassiveTestObj, ReadReturnsExpectedValues) |
| 104 | { |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 105 | // Verify read is returning the values. |
| 106 | ReadReturn v; |
| 107 | v.value = 0.01; |
| 108 | // TODO: updated is set when the value is created, so we can range check |
| 109 | // it. |
| 110 | ReadReturn r = passive->read(); |
| 111 | EXPECT_EQ(v.value, r.value); |
| 112 | } |
| 113 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 114 | TEST_F(DbusPassiveTestObj, SetValueUpdatesValue) |
| 115 | { |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 116 | // Verify setvalue does as advertised. |
| 117 | |
| 118 | double value = 0.01; |
| 119 | passive->setValue(value); |
| 120 | |
| 121 | // TODO: updated is set when the value is set, so we can range check it. |
| 122 | ReadReturn r = passive->read(); |
| 123 | EXPECT_EQ(value, r.value); |
| 124 | } |
| 125 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 126 | TEST_F(DbusPassiveTestObj, GetScaleReturnsExpectedValue) |
| 127 | { |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 128 | // Verify the scale is returned as expected. |
| 129 | EXPECT_EQ(_scale, passive->getScale()); |
| 130 | } |
| 131 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 132 | TEST_F(DbusPassiveTestObj, getIDReturnsExpectedValue) |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 133 | { |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 134 | // Verify getID returns the expected value. |
| 135 | EXPECT_EQ(id, passive->getID()); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 138 | TEST_F(DbusPassiveTestObj, GetMinValueReturnsExpectedValue) |
| 139 | { |
| 140 | EXPECT_DOUBLE_EQ(0, passive->getMin()); |
| 141 | } |
| 142 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 143 | TEST_F(DbusPassiveTestObj, VerifyHandlesDbusSignal) |
| 144 | { |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 145 | // The dbus passive sensor listens for updates and if it's the Value |
| 146 | // property, it needs to handle it. |
| 147 | |
| 148 | EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull())) |
| 149 | .WillOnce(Return(nullptr)); |
| 150 | sdbusplus::message::message msg(nullptr, &sdbus_mock); |
| 151 | |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 152 | const char* Value = "Value"; |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 153 | int64_t xValue = 10000; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 154 | const char* intf = "xyz.openbmc_project.Sensor.Value"; |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 155 | // string, std::map<std::string, std::variant<int64_t>> |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 156 | // msg.read(msgSensor, msgData); |
| 157 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 158 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull())) |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 159 | .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) { |
| 160 | const char** s = static_cast<const char**>(p); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 161 | // Read the first parameter, the string. |
| 162 | *s = intf; |
| 163 | return 0; |
| 164 | })) |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 165 | .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) { |
| 166 | const char** s = static_cast<const char**>(p); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 167 | *s = Value; |
| 168 | // Read the string in the pair (dictionary). |
| 169 | return 0; |
| 170 | })); |
| 171 | |
| 172 | // std::map |
| 173 | EXPECT_CALL(sdbus_mock, |
| 174 | sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}"))) |
| 175 | .WillOnce(Return(0)); |
| 176 | |
| 177 | // while !at_end() |
| 178 | EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0)) |
| 179 | .WillOnce(Return(0)) |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 180 | .WillOnce(Return(1)); // So it exits the loop after reading one pair. |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 181 | |
| 182 | // std::pair |
| 183 | EXPECT_CALL(sdbus_mock, |
| 184 | sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv"))) |
| 185 | .WillOnce(Return(0)); |
| 186 | |
| 187 | EXPECT_CALL(sdbus_mock, |
| 188 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("x"))) |
| 189 | .WillOnce(Return(1)); |
| 190 | EXPECT_CALL(sdbus_mock, |
| 191 | sd_bus_message_enter_container(IsNull(), 'v', StrEq("x"))) |
| 192 | .WillOnce(Return(0)); |
| 193 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 194 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'x', NotNull())) |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 195 | .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) { |
| 196 | int64_t* s = static_cast<int64_t*>(p); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 197 | *s = xValue; |
| 198 | return 0; |
| 199 | })); |
| 200 | |
| 201 | EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull())) |
| 202 | .WillOnce(Return(0)) /* variant. */ |
| 203 | .WillOnce(Return(0)) /* std::pair */ |
| 204 | .WillOnce(Return(0)); /* std::map */ |
| 205 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 206 | int rv = handleSensorValue(msg, passive); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 207 | EXPECT_EQ(rv, 0); // It's always 0. |
| 208 | |
| 209 | ReadReturn r = passive->read(); |
| 210 | EXPECT_EQ(10, r.value); |
| 211 | } |
| 212 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 213 | TEST_F(DbusPassiveTestObj, VerifyIgnoresOtherPropertySignal) |
| 214 | { |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 215 | // The dbus passive sensor listens for updates and if it's the Value |
| 216 | // property, it needs to handle it. In this case, it won't be. |
| 217 | |
| 218 | EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull())) |
| 219 | .WillOnce(Return(nullptr)); |
| 220 | sdbusplus::message::message msg(nullptr, &sdbus_mock); |
| 221 | |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 222 | const char* Scale = "Scale"; |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 223 | int64_t xScale = -6; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 224 | const char* intf = "xyz.openbmc_project.Sensor.Value"; |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 225 | // string, std::map<std::string, std::variant<int64_t>> |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 226 | // msg.read(msgSensor, msgData); |
| 227 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 228 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull())) |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 229 | .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) { |
| 230 | const char** s = static_cast<const char**>(p); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 231 | // Read the first parameter, the string. |
| 232 | *s = intf; |
| 233 | return 0; |
| 234 | })) |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 235 | .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) { |
| 236 | const char** s = static_cast<const char**>(p); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 237 | *s = Scale; |
| 238 | // Read the string in the pair (dictionary). |
| 239 | return 0; |
| 240 | })); |
| 241 | |
| 242 | // std::map |
| 243 | EXPECT_CALL(sdbus_mock, |
| 244 | sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}"))) |
| 245 | .WillOnce(Return(0)); |
| 246 | |
| 247 | // while !at_end() |
| 248 | EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0)) |
| 249 | .WillOnce(Return(0)) |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 250 | .WillOnce(Return(1)); // So it exits the loop after reading one pair. |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 251 | |
| 252 | // std::pair |
| 253 | EXPECT_CALL(sdbus_mock, |
| 254 | sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv"))) |
| 255 | .WillOnce(Return(0)); |
| 256 | |
| 257 | EXPECT_CALL(sdbus_mock, |
| 258 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("x"))) |
| 259 | .WillOnce(Return(1)); |
| 260 | EXPECT_CALL(sdbus_mock, |
| 261 | sd_bus_message_enter_container(IsNull(), 'v', StrEq("x"))) |
| 262 | .WillOnce(Return(0)); |
| 263 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 264 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'x', NotNull())) |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 265 | .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) { |
| 266 | int64_t* s = static_cast<int64_t*>(p); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 267 | *s = xScale; |
| 268 | return 0; |
| 269 | })); |
| 270 | |
| 271 | EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull())) |
| 272 | .WillOnce(Return(0)) /* variant. */ |
| 273 | .WillOnce(Return(0)) /* std::pair */ |
| 274 | .WillOnce(Return(0)); /* std::map */ |
| 275 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 276 | int rv = handleSensorValue(msg, passive); |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 277 | EXPECT_EQ(rv, 0); // It's always 0. |
| 278 | |
| 279 | ReadReturn r = passive->read(); |
| 280 | EXPECT_EQ(0.01, r.value); |
| 281 | } |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 282 | TEST_F(DbusPassiveTestObj, VerifyCriticalThresholdAssert) |
| 283 | { |
| 284 | |
| 285 | // Verifies when a threshold is crossed the sensor goes into error state |
| 286 | EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull())) |
| 287 | .WillOnce(Return(nullptr)); |
| 288 | sdbusplus::message::message msg(nullptr, &sdbus_mock); |
| 289 | |
| 290 | const char* criticalAlarm = "CriticalAlarmHigh"; |
| 291 | bool alarm = true; |
| 292 | const char* intf = "xyz.openbmc_project.Sensor.Threshold.Critical"; |
| 293 | |
| 294 | passive->setFailed(false); |
| 295 | |
| 296 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull())) |
| 297 | .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) { |
| 298 | const char** s = static_cast<const char**>(p); |
| 299 | // Read the first parameter, the string. |
| 300 | *s = intf; |
| 301 | return 0; |
| 302 | })) |
| 303 | .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) { |
| 304 | const char** s = static_cast<const char**>(p); |
| 305 | *s = criticalAlarm; |
| 306 | // Read the string in the pair (dictionary). |
| 307 | return 0; |
| 308 | })); |
| 309 | |
| 310 | // std::map |
| 311 | EXPECT_CALL(sdbus_mock, |
| 312 | sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}"))) |
| 313 | .WillOnce(Return(0)); |
| 314 | |
| 315 | // while !at_end() |
| 316 | EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0)) |
| 317 | .WillOnce(Return(0)) |
| 318 | .WillOnce(Return(1)); // So it exits the loop after reading one pair. |
| 319 | |
| 320 | // std::pair |
| 321 | EXPECT_CALL(sdbus_mock, |
| 322 | sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv"))) |
| 323 | .WillOnce(Return(0)); |
| 324 | |
| 325 | EXPECT_CALL(sdbus_mock, |
| 326 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("x"))) |
| 327 | .WillOnce(Return(0)); |
| 328 | EXPECT_CALL(sdbus_mock, |
| 329 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("d"))) |
| 330 | .WillOnce(Return(0)); |
| 331 | EXPECT_CALL(sdbus_mock, |
| 332 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("b"))) |
| 333 | .WillOnce(Return(1)); |
| 334 | EXPECT_CALL(sdbus_mock, |
| 335 | sd_bus_message_enter_container(IsNull(), 'v', StrEq("b"))) |
| 336 | .WillOnce(Return(0)); |
| 337 | |
| 338 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'b', NotNull())) |
| 339 | .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) { |
| 340 | bool* s = static_cast<bool*>(p); |
| 341 | *s = alarm; |
| 342 | return 0; |
| 343 | })); |
| 344 | |
| 345 | EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull())) |
| 346 | .WillOnce(Return(0)) /* variant. */ |
| 347 | .WillOnce(Return(0)) /* std::pair */ |
| 348 | .WillOnce(Return(0)); /* std::map */ |
| 349 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 350 | int rv = handleSensorValue(msg, passive); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 351 | EXPECT_EQ(rv, 0); // It's always 0. |
| 352 | bool failed = passive->getFailed(); |
| 353 | EXPECT_EQ(failed, true); |
| 354 | } |
| 355 | |
| 356 | TEST_F(DbusPassiveTestObj, VerifyCriticalThresholdDeassert) |
| 357 | { |
| 358 | |
| 359 | // Verifies when a threshold is deasserted a failed sensor goes back into |
| 360 | // the normal state |
| 361 | EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull())) |
| 362 | .WillOnce(Return(nullptr)); |
| 363 | sdbusplus::message::message msg(nullptr, &sdbus_mock); |
| 364 | |
| 365 | const char* criticalAlarm = "CriticalAlarmHigh"; |
| 366 | bool alarm = false; |
| 367 | const char* intf = "xyz.openbmc_project.Sensor.Threshold.Critical"; |
| 368 | |
| 369 | passive->setFailed(true); |
| 370 | |
| 371 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull())) |
| 372 | .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) { |
| 373 | const char** s = static_cast<const char**>(p); |
| 374 | // Read the first parameter, the string. |
| 375 | *s = intf; |
| 376 | return 0; |
| 377 | })) |
| 378 | .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) { |
| 379 | const char** s = static_cast<const char**>(p); |
| 380 | *s = criticalAlarm; |
| 381 | // Read the string in the pair (dictionary). |
| 382 | return 0; |
| 383 | })); |
| 384 | |
| 385 | // std::map |
| 386 | EXPECT_CALL(sdbus_mock, |
| 387 | sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}"))) |
| 388 | .WillOnce(Return(0)); |
| 389 | |
| 390 | // while !at_end() |
| 391 | EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0)) |
| 392 | .WillOnce(Return(0)) |
| 393 | .WillOnce(Return(1)); // So it exits the loop after reading one pair. |
| 394 | |
| 395 | // std::pair |
| 396 | EXPECT_CALL(sdbus_mock, |
| 397 | sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv"))) |
| 398 | .WillOnce(Return(0)); |
| 399 | |
| 400 | EXPECT_CALL(sdbus_mock, |
| 401 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("x"))) |
| 402 | .WillOnce(Return(0)); |
| 403 | EXPECT_CALL(sdbus_mock, |
| 404 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("d"))) |
| 405 | .WillOnce(Return(0)); |
| 406 | EXPECT_CALL(sdbus_mock, |
| 407 | sd_bus_message_verify_type(IsNull(), 'v', StrEq("b"))) |
| 408 | .WillOnce(Return(1)); |
| 409 | EXPECT_CALL(sdbus_mock, |
| 410 | sd_bus_message_enter_container(IsNull(), 'v', StrEq("b"))) |
| 411 | .WillOnce(Return(0)); |
| 412 | |
| 413 | EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'b', NotNull())) |
| 414 | .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) { |
| 415 | bool* s = static_cast<bool*>(p); |
| 416 | *s = alarm; |
| 417 | return 0; |
| 418 | })); |
| 419 | |
| 420 | EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull())) |
| 421 | .WillOnce(Return(0)) /* variant. */ |
| 422 | .WillOnce(Return(0)) /* std::pair */ |
| 423 | .WillOnce(Return(0)); /* std::map */ |
| 424 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 425 | int rv = handleSensorValue(msg, passive); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 426 | EXPECT_EQ(rv, 0); // It's always 0. |
| 427 | bool failed = passive->getFailed(); |
| 428 | EXPECT_EQ(failed, false); |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 429 | } |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 430 | |
| 431 | void GetPropertiesMax3k(sdbusplus::bus::bus& bus, const std::string& service, |
| 432 | const std::string& path, SensorProperties* prop) |
| 433 | { |
| 434 | prop->scale = -3; |
| 435 | prop->value = 10; |
| 436 | prop->unit = "x"; |
| 437 | prop->min = 0; |
| 438 | prop->max = 3000; |
| 439 | } |
| 440 | |
| 441 | using GetPropertiesFunction = |
| 442 | std::function<void(sdbusplus::bus::bus&, const std::string&, |
| 443 | const std::string&, SensorProperties*)>; |
| 444 | |
| 445 | // TODO: There is definitely a cleaner way to do this. |
| 446 | class DbusPassiveTest3kMaxObj : public ::testing::Test |
| 447 | { |
| 448 | protected: |
| 449 | DbusPassiveTest3kMaxObj() : |
| 450 | sdbus_mock(), |
| 451 | bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))), helper() |
| 452 | { |
| 453 | EXPECT_CALL(helper, getService(_, StrEq(SensorIntf), StrEq(path))) |
| 454 | .WillOnce(Return("asdf")); |
| 455 | |
| 456 | EXPECT_CALL(helper, |
| 457 | getProperties(_, StrEq("asdf"), StrEq(path), NotNull())) |
| 458 | .WillOnce(_getProps); |
| 459 | EXPECT_CALL(helper, thresholdsAsserted(_, StrEq("asdf"), StrEq(path))) |
| 460 | .WillOnce(Return(false)); |
| 461 | |
| 462 | auto info = conf::SensorConfig(); |
| 463 | ri = DbusPassive::createDbusPassive(bus_mock, type, id, &helper, &info, |
| 464 | nullptr); |
| 465 | passive = reinterpret_cast<DbusPassive*>(ri.get()); |
| 466 | EXPECT_FALSE(passive == nullptr); |
| 467 | } |
| 468 | |
| 469 | sdbusplus::SdBusMock sdbus_mock; |
| 470 | sdbusplus::bus::bus bus_mock; |
| 471 | DbusHelperMock helper; |
| 472 | std::string type = "temp"; |
| 473 | std::string id = "id"; |
| 474 | std::string path = "/xyz/openbmc_project/sensors/temperature/id"; |
| 475 | int64_t _scale = -3; |
| 476 | int64_t _value = 10; |
| 477 | |
| 478 | std::unique_ptr<ReadInterface> ri; |
| 479 | DbusPassive* passive; |
| 480 | GetPropertiesFunction _getProps = &GetPropertiesMax3k; |
| 481 | }; |
| 482 | |
| 483 | TEST_F(DbusPassiveTest3kMaxObj, ReadMinAndMaxReturnsExpected) |
| 484 | { |
| 485 | EXPECT_DOUBLE_EQ(0, passive->getMin()); |
| 486 | EXPECT_DOUBLE_EQ(3, passive->getMax()); |
| 487 | } |
| 488 | |
| 489 | class DbusPassiveTest3kMaxIgnoredObj : public ::testing::Test |
| 490 | { |
| 491 | protected: |
| 492 | DbusPassiveTest3kMaxIgnoredObj() : |
| 493 | sdbus_mock(), |
| 494 | bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))), helper() |
| 495 | { |
| 496 | EXPECT_CALL(helper, getService(_, StrEq(SensorIntf), StrEq(path))) |
| 497 | .WillOnce(Return("asdf")); |
| 498 | |
| 499 | EXPECT_CALL(helper, |
| 500 | getProperties(_, StrEq("asdf"), StrEq(path), NotNull())) |
| 501 | .WillOnce(_getProps); |
| 502 | EXPECT_CALL(helper, thresholdsAsserted(_, StrEq("asdf"), StrEq(path))) |
| 503 | .WillOnce(Return(false)); |
| 504 | |
| 505 | auto info = conf::SensorConfig(); |
| 506 | info.ignoreDbusMinMax = true; |
| 507 | ri = DbusPassive::createDbusPassive(bus_mock, type, id, &helper, &info, |
| 508 | nullptr); |
| 509 | passive = reinterpret_cast<DbusPassive*>(ri.get()); |
| 510 | EXPECT_FALSE(passive == nullptr); |
| 511 | } |
| 512 | |
| 513 | sdbusplus::SdBusMock sdbus_mock; |
| 514 | sdbusplus::bus::bus bus_mock; |
| 515 | DbusHelperMock helper; |
| 516 | std::string type = "temp"; |
| 517 | std::string id = "id"; |
| 518 | std::string path = "/xyz/openbmc_project/sensors/temperature/id"; |
| 519 | int64_t _scale = -3; |
| 520 | int64_t _value = 10; |
| 521 | |
| 522 | std::unique_ptr<ReadInterface> ri; |
| 523 | DbusPassive* passive; |
| 524 | GetPropertiesFunction _getProps = &GetPropertiesMax3k; |
| 525 | }; |
| 526 | |
| 527 | TEST_F(DbusPassiveTest3kMaxIgnoredObj, ReadMinAndMaxReturnsExpected) |
| 528 | { |
| 529 | EXPECT_DOUBLE_EQ(0, passive->getMin()); |
| 530 | EXPECT_DOUBLE_EQ(0, passive->getMax()); |
| 531 | } |