blob: 40e68c413283026497f25b4551e3d4ef3b84d947 [file] [log] [blame]
James Feist75eb7692019-02-25 12:50:02 -08001#include "conf.hpp"
Patrick Venture0ef1faf2018-06-13 12:50:53 -07002#include "dbus/dbuspassive.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07003#include "test/dbushelper_mock.hpp"
Patrick Venture0ef1faf2018-06-13 12:50:53 -07004
Patrick Venture0ef1faf2018-06-13 12:50:53 -07005#include <sdbusplus/test/sdbus_mock.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -07006
7#include <functional>
Patrick Venture8729eb92020-08-10 10:38:44 -07008#include <memory>
Patrick Venture0ef1faf2018-06-13 12:50:53 -07009#include <string>
James Feist1f802f52019-02-08 13:51:43 -080010#include <variant>
Patrick Venture0ef1faf2018-06-13 12:50:53 -070011
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070012#include <gmock/gmock.h>
13#include <gtest/gtest.h>
Patrick Venture0ef1faf2018-06-13 12:50:53 -070014
Patrick Venturea0764872020-08-08 07:48:43 -070015namespace pid_control
16{
17namespace
18{
19
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070020using ::testing::_;
Patrick Venture0ef1faf2018-06-13 12:50:53 -070021using ::testing::InSequence;
22using ::testing::Invoke;
23using ::testing::IsNull;
24using ::testing::NotNull;
25using ::testing::Return;
26using ::testing::StrEq;
Patrick Venture0ef1faf2018-06-13 12:50:53 -070027
28std::string SensorIntf = "xyz.openbmc_project.Sensor.Value";
29
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070030TEST(DbusPassiveTest, FactoryFailsWithInvalidType)
31{
Patrick Venture0ef1faf2018-06-13 12:50:53 -070032 // 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 Venture8729eb92020-08-10 10:38:44 -070039 auto helper = std::make_unique<DbusHelperMock>();
James Feistf81f2882019-02-26 11:26:36 -080040 auto info = conf::SensorConfig();
Patrick Venture0ef1faf2018-06-13 12:50:53 -070041
James Feist98b704e2019-06-03 16:24:53 -070042 std::unique_ptr<ReadInterface> ri = DbusPassive::createDbusPassive(
Patrick Venture8729eb92020-08-10 10:38:44 -070043 bus_mock, type, id, std::move(helper), &info, nullptr);
Patrick Venture0ef1faf2018-06-13 12:50:53 -070044
45 EXPECT_EQ(ri, nullptr);
46}
47
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070048TEST(DbusPassiveTest, BoringConstructorTest)
49{
Patrick Venturef8cb4642018-10-30 12:02:53 -070050 // Simply build the object, does no error checking.
Patrick Venture0ef1faf2018-06-13 12:50:53 -070051
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 Venture8729eb92020-08-10 10:38:44 -070058 auto helper = std::make_unique<DbusHelperMock>();
Patrick Venture1df9e872020-10-08 15:35:01 -070059 SensorProperties properties;
Patrick Venture0ef1faf2018-06-13 12:50:53 -070060
Patrick Venture8729eb92020-08-10 10:38:44 -070061 DbusPassive(bus_mock, type, id, std::move(helper), properties, false, path,
62 nullptr);
Patrick Venture0ef1faf2018-06-13 12:50:53 -070063 // Success
64}
65
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070066class DbusPassiveTestObj : public ::testing::Test
67{
68 protected:
69 DbusPassiveTestObj() :
70 sdbus_mock(),
Patrick Venture8729eb92020-08-10 10:38:44 -070071 bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))),
72 helper(std::make_unique<DbusHelperMock>())
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070073 {
Patrick Venture9b936922020-08-10 11:28:39 -070074 EXPECT_CALL(*helper, getService(StrEq(SensorIntf), StrEq(path)))
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070075 .WillOnce(Return("asdf"));
Patrick Venture0ef1faf2018-06-13 12:50:53 -070076
Patrick Venture8729eb92020-08-10 10:38:44 -070077 EXPECT_CALL(*helper,
Patrick Venture9b936922020-08-10 11:28:39 -070078 getProperties(StrEq("asdf"), StrEq(path), NotNull()))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +080079 .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 Venture9b936922020-08-10 11:28:39 -070089 EXPECT_CALL(*helper, thresholdsAsserted(StrEq("asdf"), StrEq(path)))
James Feist36b7d8e2018-10-05 15:39:01 -070090 .WillOnce(Return(false));
Patrick Venture0ef1faf2018-06-13 12:50:53 -070091
James Feistf81f2882019-02-26 11:26:36 -080092 auto info = conf::SensorConfig();
Alex.Song8f73ad72021-10-07 00:18:27 +080093 info.unavailableAsFailed = true;
Patrick Venture8729eb92020-08-10 10:38:44 -070094 ri = DbusPassive::createDbusPassive(bus_mock, type, id,
95 std::move(helper), &info, nullptr);
Patrick Venturee2ec0f62018-09-04 12:30:27 -070096 passive = reinterpret_cast<DbusPassive*>(ri.get());
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070097 EXPECT_FALSE(passive == nullptr);
98 }
Patrick Venture0ef1faf2018-06-13 12:50:53 -070099
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700100 sdbusplus::SdBusMock sdbus_mock;
Patrick Williamsb228bc32022-07-22 19:26:56 -0500101 sdbusplus::bus_t bus_mock;
Patrick Venture8729eb92020-08-10 10:38:44 -0700102 std::unique_ptr<DbusHelperMock> helper;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700103 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 Venture0ef1faf2018-06-13 12:50:53 -0700108
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700109 std::unique_ptr<ReadInterface> ri;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700110 DbusPassive* passive;
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700111};
112
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700113TEST_F(DbusPassiveTestObj, ReadReturnsExpectedValues)
114{
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700115 // 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 Ventureda4a5dd2018-08-31 09:42:48 -0700124TEST_F(DbusPassiveTestObj, SetValueUpdatesValue)
125{
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700126 // 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 Ventureda4a5dd2018-08-31 09:42:48 -0700136TEST_F(DbusPassiveTestObj, GetScaleReturnsExpectedValue)
137{
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700138 // Verify the scale is returned as expected.
139 EXPECT_EQ(_scale, passive->getScale());
140}
141
Patrick Venture563a3562018-10-30 09:31:26 -0700142TEST_F(DbusPassiveTestObj, getIDReturnsExpectedValue)
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700143{
Patrick Venture563a3562018-10-30 09:31:26 -0700144 // Verify getID returns the expected value.
145 EXPECT_EQ(id, passive->getID());
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700146}
147
Patrick Venture6b9f5992019-09-10 09:18:28 -0700148TEST_F(DbusPassiveTestObj, GetMinValueReturnsExpectedValue)
149{
150 EXPECT_DOUBLE_EQ(0, passive->getMin());
151}
152
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700153TEST_F(DbusPassiveTestObj, VerifyHandlesDbusSignal)
154{
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700155 // 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 Williamsb228bc32022-07-22 19:26:56 -0500160 sdbusplus::message_t msg(nullptr, &sdbus_mock);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700161
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700162 const char* Value = "Value";
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700163 int64_t xValue = 10000;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700164 const char* intf = "xyz.openbmc_project.Sensor.Value";
James Feist1f802f52019-02-08 13:51:43 -0800165 // string, std::map<std::string, std::variant<int64_t>>
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700166 // msg.read(msgSensor, msgData);
167
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700168 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull()))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800169 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
170 [[maybe_unused]] char type, void* p) {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700171 const char** s = static_cast<const char**>(p);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700172 // Read the first parameter, the string.
173 *s = intf;
174 return 0;
175 }))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800176 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
177 [[maybe_unused]] char type, void* p) {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700178 const char** s = static_cast<const char**>(p);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700179 *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 Ventureda4a5dd2018-08-31 09:42:48 -0700192 .WillOnce(Return(1)); // So it exits the loop after reading one pair.
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700193
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 Ventureda4a5dd2018-08-31 09:42:48 -0700206 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'x', NotNull()))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800207 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
208 [[maybe_unused]] char type, void* p) {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700209 int64_t* s = static_cast<int64_t*>(p);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700210 *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 Venture7af157b2018-10-30 11:24:40 -0700219 int rv = handleSensorValue(msg, passive);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700220 EXPECT_EQ(rv, 0); // It's always 0.
221
222 ReadReturn r = passive->read();
223 EXPECT_EQ(10, r.value);
224}
225
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700226TEST_F(DbusPassiveTestObj, VerifyIgnoresOtherPropertySignal)
227{
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700228 // 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 Williamsb228bc32022-07-22 19:26:56 -0500233 sdbusplus::message_t msg(nullptr, &sdbus_mock);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700234
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700235 const char* Scale = "Scale";
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700236 int64_t xScale = -6;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700237 const char* intf = "xyz.openbmc_project.Sensor.Value";
James Feist1f802f52019-02-08 13:51:43 -0800238 // string, std::map<std::string, std::variant<int64_t>>
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700239 // msg.read(msgSensor, msgData);
240
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700241 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull()))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800242 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
243 [[maybe_unused]] char type, void* p) {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700244 const char** s = static_cast<const char**>(p);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700245 // Read the first parameter, the string.
246 *s = intf;
247 return 0;
248 }))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800249 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
250 [[maybe_unused]] char type, void* p) {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700251 const char** s = static_cast<const char**>(p);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700252 *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 Ventureda4a5dd2018-08-31 09:42:48 -0700265 .WillOnce(Return(1)); // So it exits the loop after reading one pair.
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700266
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 Ventureda4a5dd2018-08-31 09:42:48 -0700279 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'x', NotNull()))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800280 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
281 [[maybe_unused]] char type, void* p) {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700282 int64_t* s = static_cast<int64_t*>(p);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700283 *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 Venture7af157b2018-10-30 11:24:40 -0700292 int rv = handleSensorValue(msg, passive);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700293 EXPECT_EQ(rv, 0); // It's always 0.
294
295 ReadReturn r = passive->read();
296 EXPECT_EQ(0.01, r.value);
297}
Alex.Song8f73ad72021-10-07 00:18:27 +0800298
James Feist36b7d8e2018-10-05 15:39:01 -0700299TEST_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 Williamsb228bc32022-07-22 19:26:56 -0500305 sdbusplus::message_t msg(nullptr, &sdbus_mock);
James Feist36b7d8e2018-10-05 15:39:01 -0700306
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.Wua1ae4fa2022-10-28 17:38:35 +0800314 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
315 [[maybe_unused]] char type, void* p) {
James Feist36b7d8e2018-10-05 15:39:01 -0700316 const char** s = static_cast<const char**>(p);
317 // Read the first parameter, the string.
318 *s = intf;
319 return 0;
320 }))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800321 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
322 [[maybe_unused]] char type, void* p) {
James Feist36b7d8e2018-10-05 15:39:01 -0700323 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.Wua1ae4fa2022-10-28 17:38:35 +0800358 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
359 [[maybe_unused]] char type, void* p) {
James Feist36b7d8e2018-10-05 15:39:01 -0700360 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 Venture7af157b2018-10-30 11:24:40 -0700370 int rv = handleSensorValue(msg, passive);
James Feist36b7d8e2018-10-05 15:39:01 -0700371 EXPECT_EQ(rv, 0); // It's always 0.
372 bool failed = passive->getFailed();
373 EXPECT_EQ(failed, true);
374}
375
376TEST_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 Williamsb228bc32022-07-22 19:26:56 -0500383 sdbusplus::message_t msg(nullptr, &sdbus_mock);
James Feist36b7d8e2018-10-05 15:39:01 -0700384
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.Wua1ae4fa2022-10-28 17:38:35 +0800392 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
393 [[maybe_unused]] char type, void* p) {
James Feist36b7d8e2018-10-05 15:39:01 -0700394 const char** s = static_cast<const char**>(p);
395 // Read the first parameter, the string.
396 *s = intf;
397 return 0;
398 }))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800399 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
400 [[maybe_unused]] char type, void* p) {
James Feist36b7d8e2018-10-05 15:39:01 -0700401 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.Wua1ae4fa2022-10-28 17:38:35 +0800436 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
437 [[maybe_unused]] char type, void* p) {
James Feist36b7d8e2018-10-05 15:39:01 -0700438 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 Venture7af157b2018-10-30 11:24:40 -0700448 int rv = handleSensorValue(msg, passive);
James Feist36b7d8e2018-10-05 15:39:01 -0700449 EXPECT_EQ(rv, 0); // It's always 0.
450 bool failed = passive->getFailed();
451 EXPECT_EQ(failed, false);
Patrick Venture563a3562018-10-30 09:31:26 -0700452}
Patrick Venture6b9f5992019-09-10 09:18:28 -0700453
Alex.Song8f73ad72021-10-07 00:18:27 +0800454TEST_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 Williamsb228bc32022-07-22 19:26:56 -0500461 sdbusplus::message_t msg(nullptr, &sdbus_mock);
Alex.Song8f73ad72021-10-07 00:18:27 +0800462
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.Wua1ae4fa2022-10-28 17:38:35 +0800470 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
471 [[maybe_unused]] char type, void* p) {
Alex.Song8f73ad72021-10-07 00:18:27 +0800472 const char** s = static_cast<const char**>(p);
473 // Read the first parameter, the string.
474 *s = intf;
475 return 0;
476 }))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800477 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
478 [[maybe_unused]] char type, void* p) {
Alex.Song8f73ad72021-10-07 00:18:27 +0800479 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.Wua1ae4fa2022-10-28 17:38:35 +0800514 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
515 [[maybe_unused]] char type, void* p) {
Alex.Song8f73ad72021-10-07 00:18:27 +0800516 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
532TEST_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 Williamsb228bc32022-07-22 19:26:56 -0500539 sdbusplus::message_t msg(nullptr, &sdbus_mock);
Alex.Song8f73ad72021-10-07 00:18:27 +0800540
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.Wua1ae4fa2022-10-28 17:38:35 +0800550 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
551 [[maybe_unused]] char type, void* p) {
Alex.Song8f73ad72021-10-07 00:18:27 +0800552 const char** s = static_cast<const char**>(p);
553 // Read the first parameter, the string.
554 *s = intf;
555 return 0;
556 }))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800557 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
558 [[maybe_unused]] char type, void* p) {
Alex.Song8f73ad72021-10-07 00:18:27 +0800559 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.Wua1ae4fa2022-10-28 17:38:35 +0800594 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
595 [[maybe_unused]] char type, void* p) {
Alex.Song8f73ad72021-10-07 00:18:27 +0800596 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
612class 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.Wua1ae4fa2022-10-28 17:38:35 +0800625 .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.Song8f73ad72021-10-07 00:18:27 +0800635 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 Williamsb228bc32022-07-22 19:26:56 -0500647 sdbusplus::bus_t bus_mock;
Alex.Song8f73ad72021-10-07 00:18:27 +0800648 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
659TEST_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 Williamsb228bc32022-07-22 19:26:56 -0500666 sdbusplus::message_t msg(nullptr, &sdbus_mock);
Alex.Song8f73ad72021-10-07 00:18:27 +0800667
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.Wua1ae4fa2022-10-28 17:38:35 +0800675 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
676 [[maybe_unused]] char type, void* p) {
Alex.Song8f73ad72021-10-07 00:18:27 +0800677 const char** s = static_cast<const char**>(p);
678 // Read the first parameter, the string.
679 *s = intf;
680 return 0;
681 }))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800682 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
683 [[maybe_unused]] char type, void* p) {
Alex.Song8f73ad72021-10-07 00:18:27 +0800684 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.Wua1ae4fa2022-10-28 17:38:35 +0800719 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
720 [[maybe_unused]] char type, void* p) {
Alex.Song8f73ad72021-10-07 00:18:27 +0800721 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
739TEST_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 Williamsb228bc32022-07-22 19:26:56 -0500746 sdbusplus::message_t msg(nullptr, &sdbus_mock);
Alex.Song8f73ad72021-10-07 00:18:27 +0800747
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.Wua1ae4fa2022-10-28 17:38:35 +0800757 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
758 [[maybe_unused]] char type, void* p) {
Alex.Song8f73ad72021-10-07 00:18:27 +0800759 const char** s = static_cast<const char**>(p);
760 // Read the first parameter, the string.
761 *s = intf;
762 return 0;
763 }))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800764 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
765 [[maybe_unused]] char type, void* p) {
Alex.Song8f73ad72021-10-07 00:18:27 +0800766 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.Wua1ae4fa2022-10-28 17:38:35 +0800801 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
802 [[maybe_unused]] char type, void* p) {
Alex.Song8f73ad72021-10-07 00:18:27 +0800803 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.Wua1ae4fa2022-10-28 17:38:35 +0800819void GetPropertiesMax3k([[maybe_unused]] const std::string& service,
820 [[maybe_unused]] const std::string& path,
Patrick Venture9b936922020-08-10 11:28:39 -0700821 SensorProperties* prop)
Patrick Venture6b9f5992019-09-10 09:18:28 -0700822{
823 prop->scale = -3;
824 prop->value = 10;
825 prop->unit = "x";
826 prop->min = 0;
827 prop->max = 3000;
828}
829
Patrick Venture9b936922020-08-10 11:28:39 -0700830using GetPropertiesFunction = std::function<void(
831 const std::string&, const std::string&, SensorProperties*)>;
Patrick Venture6b9f5992019-09-10 09:18:28 -0700832
833// TODO: There is definitely a cleaner way to do this.
834class DbusPassiveTest3kMaxObj : public ::testing::Test
835{
836 protected:
837 DbusPassiveTest3kMaxObj() :
838 sdbus_mock(),
Patrick Venture8729eb92020-08-10 10:38:44 -0700839 bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))),
840 helper(std::make_unique<DbusHelperMock>())
Patrick Venture6b9f5992019-09-10 09:18:28 -0700841 {
Patrick Venture9b936922020-08-10 11:28:39 -0700842 EXPECT_CALL(*helper, getService(StrEq(SensorIntf), StrEq(path)))
Patrick Venture6b9f5992019-09-10 09:18:28 -0700843 .WillOnce(Return("asdf"));
844
Patrick Venture8729eb92020-08-10 10:38:44 -0700845 EXPECT_CALL(*helper,
Patrick Venture9b936922020-08-10 11:28:39 -0700846 getProperties(StrEq("asdf"), StrEq(path), NotNull()))
Patrick Venture6b9f5992019-09-10 09:18:28 -0700847 .WillOnce(_getProps);
Patrick Venture9b936922020-08-10 11:28:39 -0700848 EXPECT_CALL(*helper, thresholdsAsserted(StrEq("asdf"), StrEq(path)))
Patrick Venture6b9f5992019-09-10 09:18:28 -0700849 .WillOnce(Return(false));
850
851 auto info = conf::SensorConfig();
Patrick Venture8729eb92020-08-10 10:38:44 -0700852 ri = DbusPassive::createDbusPassive(bus_mock, type, id,
853 std::move(helper), &info, nullptr);
Patrick Venture6b9f5992019-09-10 09:18:28 -0700854 passive = reinterpret_cast<DbusPassive*>(ri.get());
855 EXPECT_FALSE(passive == nullptr);
856 }
857
858 sdbusplus::SdBusMock sdbus_mock;
Patrick Williamsb228bc32022-07-22 19:26:56 -0500859 sdbusplus::bus_t bus_mock;
Patrick Venture8729eb92020-08-10 10:38:44 -0700860 std::unique_ptr<DbusHelperMock> helper;
Patrick Venture6b9f5992019-09-10 09:18:28 -0700861 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
872TEST_F(DbusPassiveTest3kMaxObj, ReadMinAndMaxReturnsExpected)
873{
874 EXPECT_DOUBLE_EQ(0, passive->getMin());
875 EXPECT_DOUBLE_EQ(3, passive->getMax());
876}
877
878class DbusPassiveTest3kMaxIgnoredObj : public ::testing::Test
879{
880 protected:
881 DbusPassiveTest3kMaxIgnoredObj() :
882 sdbus_mock(),
Patrick Venture8729eb92020-08-10 10:38:44 -0700883 bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))),
884 helper(std::make_unique<DbusHelperMock>())
Patrick Venture6b9f5992019-09-10 09:18:28 -0700885 {
Patrick Venture9b936922020-08-10 11:28:39 -0700886 EXPECT_CALL(*helper, getService(StrEq(SensorIntf), StrEq(path)))
Patrick Venture6b9f5992019-09-10 09:18:28 -0700887 .WillOnce(Return("asdf"));
888
Patrick Venture8729eb92020-08-10 10:38:44 -0700889 EXPECT_CALL(*helper,
Patrick Venture9b936922020-08-10 11:28:39 -0700890 getProperties(StrEq("asdf"), StrEq(path), NotNull()))
Patrick Venture6b9f5992019-09-10 09:18:28 -0700891 .WillOnce(_getProps);
Patrick Venture9b936922020-08-10 11:28:39 -0700892 EXPECT_CALL(*helper, thresholdsAsserted(StrEq("asdf"), StrEq(path)))
Patrick Venture6b9f5992019-09-10 09:18:28 -0700893 .WillOnce(Return(false));
894
895 auto info = conf::SensorConfig();
896 info.ignoreDbusMinMax = true;
Patrick Venture8729eb92020-08-10 10:38:44 -0700897 ri = DbusPassive::createDbusPassive(bus_mock, type, id,
898 std::move(helper), &info, nullptr);
Patrick Venture6b9f5992019-09-10 09:18:28 -0700899 passive = reinterpret_cast<DbusPassive*>(ri.get());
900 EXPECT_FALSE(passive == nullptr);
901 }
902
903 sdbusplus::SdBusMock sdbus_mock;
Patrick Williamsb228bc32022-07-22 19:26:56 -0500904 sdbusplus::bus_t bus_mock;
Patrick Venture8729eb92020-08-10 10:38:44 -0700905 std::unique_ptr<DbusHelperMock> helper;
Patrick Venture6b9f5992019-09-10 09:18:28 -0700906 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
917TEST_F(DbusPassiveTest3kMaxIgnoredObj, ReadMinAndMaxReturnsExpected)
918{
919 EXPECT_DOUBLE_EQ(0, passive->getMin());
920 EXPECT_DOUBLE_EQ(0, passive->getMax());
921}
Patrick Venturea0764872020-08-08 07:48:43 -0700922
923} // namespace
924} // namespace pid_control