blob: 29c73dc378a42f6b7a0fbe7019b6a6d591cb792d [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) {
Patrick Williamse1dbb592023-10-20 11:19:22 -050082 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 Williamse1dbb592023-10-20 11:19:22 -0500171 const char** s = static_cast<const char**>(p);
172 // 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 Williamse1dbb592023-10-20 11:19:22 -0500178 const char** s = static_cast<const char**>(p);
179 *s = Value;
180 // Read the string in the pair (dictionary).
181 return 0;
182 }));
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700183
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 Williamse1dbb592023-10-20 11:19:22 -0500209 int64_t* s = static_cast<int64_t*>(p);
210 *s = xValue;
211 return 0;
212 }));
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700213
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 Williamse1dbb592023-10-20 11:19:22 -0500244 const char** s = static_cast<const char**>(p);
245 // 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 Williamse1dbb592023-10-20 11:19:22 -0500251 const char** s = static_cast<const char**>(p);
252 *s = Scale;
253 // Read the string in the pair (dictionary).
254 return 0;
255 }));
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700256
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 Williamse1dbb592023-10-20 11:19:22 -0500282 int64_t* s = static_cast<int64_t*>(p);
283 *s = xScale;
284 return 0;
285 }));
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700286
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{
James Feist36b7d8e2018-10-05 15:39:01 -0700301 // Verifies when a threshold is crossed the sensor goes into error state
302 EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull()))
303 .WillOnce(Return(nullptr));
Patrick Williamsb228bc32022-07-22 19:26:56 -0500304 sdbusplus::message_t msg(nullptr, &sdbus_mock);
James Feist36b7d8e2018-10-05 15:39:01 -0700305
306 const char* criticalAlarm = "CriticalAlarmHigh";
307 bool alarm = true;
308 const char* intf = "xyz.openbmc_project.Sensor.Threshold.Critical";
309
310 passive->setFailed(false);
311
312 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull()))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800313 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
314 [[maybe_unused]] char type, void* p) {
Patrick Williamse1dbb592023-10-20 11:19:22 -0500315 const char** s = static_cast<const char**>(p);
316 // Read the first parameter, the string.
317 *s = intf;
318 return 0;
319 }))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800320 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
321 [[maybe_unused]] char type, void* p) {
Patrick Williamse1dbb592023-10-20 11:19:22 -0500322 const char** s = static_cast<const char**>(p);
323 *s = criticalAlarm;
324 // Read the string in the pair (dictionary).
325 return 0;
326 }));
James Feist36b7d8e2018-10-05 15:39:01 -0700327
328 // std::map
329 EXPECT_CALL(sdbus_mock,
330 sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}")))
331 .WillOnce(Return(0));
332
333 // while !at_end()
334 EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0))
335 .WillOnce(Return(0))
336 .WillOnce(Return(1)); // So it exits the loop after reading one pair.
337
338 // std::pair
339 EXPECT_CALL(sdbus_mock,
340 sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv")))
341 .WillOnce(Return(0));
342
343 EXPECT_CALL(sdbus_mock,
344 sd_bus_message_verify_type(IsNull(), 'v', StrEq("x")))
345 .WillOnce(Return(0));
346 EXPECT_CALL(sdbus_mock,
347 sd_bus_message_verify_type(IsNull(), 'v', StrEq("d")))
348 .WillOnce(Return(0));
349 EXPECT_CALL(sdbus_mock,
350 sd_bus_message_verify_type(IsNull(), 'v', StrEq("b")))
351 .WillOnce(Return(1));
352 EXPECT_CALL(sdbus_mock,
353 sd_bus_message_enter_container(IsNull(), 'v', StrEq("b")))
354 .WillOnce(Return(0));
355
356 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'b', NotNull()))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800357 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
358 [[maybe_unused]] char type, void* p) {
Patrick Williamse1dbb592023-10-20 11:19:22 -0500359 bool* s = static_cast<bool*>(p);
360 *s = alarm;
361 return 0;
362 }));
James Feist36b7d8e2018-10-05 15:39:01 -0700363
364 EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull()))
365 .WillOnce(Return(0)) /* variant. */
366 .WillOnce(Return(0)) /* std::pair */
367 .WillOnce(Return(0)); /* std::map */
368
Patrick Venture7af157b2018-10-30 11:24:40 -0700369 int rv = handleSensorValue(msg, passive);
James Feist36b7d8e2018-10-05 15:39:01 -0700370 EXPECT_EQ(rv, 0); // It's always 0.
371 bool failed = passive->getFailed();
372 EXPECT_EQ(failed, true);
373}
374
375TEST_F(DbusPassiveTestObj, VerifyCriticalThresholdDeassert)
376{
James Feist36b7d8e2018-10-05 15:39:01 -0700377 // Verifies when a threshold is deasserted a failed sensor goes back into
378 // the normal state
379 EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull()))
380 .WillOnce(Return(nullptr));
Patrick Williamsb228bc32022-07-22 19:26:56 -0500381 sdbusplus::message_t msg(nullptr, &sdbus_mock);
James Feist36b7d8e2018-10-05 15:39:01 -0700382
383 const char* criticalAlarm = "CriticalAlarmHigh";
384 bool alarm = false;
385 const char* intf = "xyz.openbmc_project.Sensor.Threshold.Critical";
386
387 passive->setFailed(true);
388
389 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull()))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800390 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
391 [[maybe_unused]] char type, void* p) {
Patrick Williamse1dbb592023-10-20 11:19:22 -0500392 const char** s = static_cast<const char**>(p);
393 // Read the first parameter, the string.
394 *s = intf;
395 return 0;
396 }))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800397 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
398 [[maybe_unused]] char type, void* p) {
Patrick Williamse1dbb592023-10-20 11:19:22 -0500399 const char** s = static_cast<const char**>(p);
400 *s = criticalAlarm;
401 // Read the string in the pair (dictionary).
402 return 0;
403 }));
James Feist36b7d8e2018-10-05 15:39:01 -0700404
405 // std::map
406 EXPECT_CALL(sdbus_mock,
407 sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}")))
408 .WillOnce(Return(0));
409
410 // while !at_end()
411 EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0))
412 .WillOnce(Return(0))
413 .WillOnce(Return(1)); // So it exits the loop after reading one pair.
414
415 // std::pair
416 EXPECT_CALL(sdbus_mock,
417 sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv")))
418 .WillOnce(Return(0));
419
420 EXPECT_CALL(sdbus_mock,
421 sd_bus_message_verify_type(IsNull(), 'v', StrEq("x")))
422 .WillOnce(Return(0));
423 EXPECT_CALL(sdbus_mock,
424 sd_bus_message_verify_type(IsNull(), 'v', StrEq("d")))
425 .WillOnce(Return(0));
426 EXPECT_CALL(sdbus_mock,
427 sd_bus_message_verify_type(IsNull(), 'v', StrEq("b")))
428 .WillOnce(Return(1));
429 EXPECT_CALL(sdbus_mock,
430 sd_bus_message_enter_container(IsNull(), 'v', StrEq("b")))
431 .WillOnce(Return(0));
432
433 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'b', NotNull()))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800434 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
435 [[maybe_unused]] char type, void* p) {
Patrick Williamse1dbb592023-10-20 11:19:22 -0500436 bool* s = static_cast<bool*>(p);
437 *s = alarm;
438 return 0;
439 }));
James Feist36b7d8e2018-10-05 15:39:01 -0700440
441 EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull()))
442 .WillOnce(Return(0)) /* variant. */
443 .WillOnce(Return(0)) /* std::pair */
444 .WillOnce(Return(0)); /* std::map */
445
Patrick Venture7af157b2018-10-30 11:24:40 -0700446 int rv = handleSensorValue(msg, passive);
James Feist36b7d8e2018-10-05 15:39:01 -0700447 EXPECT_EQ(rv, 0); // It's always 0.
448 bool failed = passive->getFailed();
449 EXPECT_EQ(failed, false);
Patrick Venture563a3562018-10-30 09:31:26 -0700450}
Patrick Venture6b9f5992019-09-10 09:18:28 -0700451
Alex.Song8f73ad72021-10-07 00:18:27 +0800452TEST_F(DbusPassiveTestObj, VerifyAvailableDeassert)
453{
Alex.Song8f73ad72021-10-07 00:18:27 +0800454 // Verifies when Availble is deasserted && unavailableAsFailed == true,
455 // the sensor goes into error state
456 EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull()))
457 .WillOnce(Return(nullptr));
Patrick Williamsb228bc32022-07-22 19:26:56 -0500458 sdbusplus::message_t msg(nullptr, &sdbus_mock);
Alex.Song8f73ad72021-10-07 00:18:27 +0800459
460 const char* property = "Available";
461 bool asserted = false;
462 const char* intf = "xyz.openbmc_project.State.Decorator.Availability";
463
464 passive->setAvailable(true);
465
466 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull()))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800467 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
468 [[maybe_unused]] char type, void* p) {
Patrick Williamse1dbb592023-10-20 11:19:22 -0500469 const char** s = static_cast<const char**>(p);
470 // Read the first parameter, the string.
471 *s = intf;
472 return 0;
473 }))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800474 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
475 [[maybe_unused]] char type, void* p) {
Patrick Williamse1dbb592023-10-20 11:19:22 -0500476 const char** s = static_cast<const char**>(p);
477 *s = property;
478 // Read the string in the pair (dictionary).
479 return 0;
480 }));
Alex.Song8f73ad72021-10-07 00:18:27 +0800481
482 // std::map
483 EXPECT_CALL(sdbus_mock,
484 sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}")))
485 .WillOnce(Return(0));
486
487 // while !at_end()
488 EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0))
489 .WillOnce(Return(0))
490 .WillOnce(Return(1)); // So it exits the loop after reading one pair.
491
492 // std::pair
493 EXPECT_CALL(sdbus_mock,
494 sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv")))
495 .WillOnce(Return(0));
496
497 EXPECT_CALL(sdbus_mock,
498 sd_bus_message_verify_type(IsNull(), 'v', StrEq("x")))
499 .WillOnce(Return(0));
500 EXPECT_CALL(sdbus_mock,
501 sd_bus_message_verify_type(IsNull(), 'v', StrEq("d")))
502 .WillOnce(Return(0));
503 EXPECT_CALL(sdbus_mock,
504 sd_bus_message_verify_type(IsNull(), 'v', StrEq("b")))
505 .WillOnce(Return(1));
506 EXPECT_CALL(sdbus_mock,
507 sd_bus_message_enter_container(IsNull(), 'v', StrEq("b")))
508 .WillOnce(Return(0));
509
510 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'b', NotNull()))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800511 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
512 [[maybe_unused]] char type, void* p) {
Patrick Williamse1dbb592023-10-20 11:19:22 -0500513 bool* s = static_cast<bool*>(p);
514 *s = asserted;
515 return 0;
516 }));
Alex.Song8f73ad72021-10-07 00:18:27 +0800517
518 EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull()))
519 .WillOnce(Return(0)) /* variant. */
520 .WillOnce(Return(0)) /* std::pair */
521 .WillOnce(Return(0)); /* std::map */
522
523 int rv = handleSensorValue(msg, passive);
524 EXPECT_EQ(rv, 0); // It's always 0.
525 bool failed = passive->getFailed();
526 EXPECT_EQ(failed, true);
527}
528
529TEST_F(DbusPassiveTestObj, VerifyAvailableAssert)
530{
Alex.Song8f73ad72021-10-07 00:18:27 +0800531 // Verifies when Availble is asserted && unavailableAsFailed == true,
532 // an error sensor goes back to normal state
533 EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull()))
534 .WillOnce(Return(nullptr));
Patrick Williamsb228bc32022-07-22 19:26:56 -0500535 sdbusplus::message_t msg(nullptr, &sdbus_mock);
Alex.Song8f73ad72021-10-07 00:18:27 +0800536
537 const char* property = "Available";
538 bool asserted = true;
539 const char* intf = "xyz.openbmc_project.State.Decorator.Availability";
540
541 passive->setAvailable(false);
542 bool failed = passive->getFailed();
543 EXPECT_EQ(failed, true);
544
545 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull()))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800546 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
547 [[maybe_unused]] char type, void* p) {
Patrick Williamse1dbb592023-10-20 11:19:22 -0500548 const char** s = static_cast<const char**>(p);
549 // Read the first parameter, the string.
550 *s = intf;
551 return 0;
552 }))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800553 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
554 [[maybe_unused]] char type, void* p) {
Patrick Williamse1dbb592023-10-20 11:19:22 -0500555 const char** s = static_cast<const char**>(p);
556 *s = property;
557 // Read the string in the pair (dictionary).
558 return 0;
559 }));
Alex.Song8f73ad72021-10-07 00:18:27 +0800560
561 // std::map
562 EXPECT_CALL(sdbus_mock,
563 sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}")))
564 .WillOnce(Return(0));
565
566 // while !at_end()
567 EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0))
568 .WillOnce(Return(0))
569 .WillOnce(Return(1)); // So it exits the loop after reading one pair.
570
571 // std::pair
572 EXPECT_CALL(sdbus_mock,
573 sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv")))
574 .WillOnce(Return(0));
575
576 EXPECT_CALL(sdbus_mock,
577 sd_bus_message_verify_type(IsNull(), 'v', StrEq("x")))
578 .WillOnce(Return(0));
579 EXPECT_CALL(sdbus_mock,
580 sd_bus_message_verify_type(IsNull(), 'v', StrEq("d")))
581 .WillOnce(Return(0));
582 EXPECT_CALL(sdbus_mock,
583 sd_bus_message_verify_type(IsNull(), 'v', StrEq("b")))
584 .WillOnce(Return(1));
585 EXPECT_CALL(sdbus_mock,
586 sd_bus_message_enter_container(IsNull(), 'v', StrEq("b")))
587 .WillOnce(Return(0));
588
589 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'b', NotNull()))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800590 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
591 [[maybe_unused]] char type, void* p) {
Patrick Williamse1dbb592023-10-20 11:19:22 -0500592 bool* s = static_cast<bool*>(p);
593 *s = asserted;
594 return 0;
595 }));
Alex.Song8f73ad72021-10-07 00:18:27 +0800596
597 EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull()))
598 .WillOnce(Return(0)) /* variant. */
599 .WillOnce(Return(0)) /* std::pair */
600 .WillOnce(Return(0)); /* std::map */
601
602 int rv = handleSensorValue(msg, passive);
603 EXPECT_EQ(rv, 0); // It's always 0.
604 failed = passive->getFailed();
605 EXPECT_EQ(failed, false);
606}
607
608class DbusPassiveTestUnaSensorNotAsFailedObj : public ::testing::Test
609{
610 protected:
611 DbusPassiveTestUnaSensorNotAsFailedObj() :
612 sdbus_mock(),
613 bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))),
614 helper(std::make_unique<DbusHelperMock>())
615 {
616 EXPECT_CALL(*helper, getService(StrEq(SensorIntf), StrEq(path)))
617 .WillOnce(Return("asdf"));
618
619 EXPECT_CALL(*helper,
620 getProperties(StrEq("asdf"), StrEq(path), NotNull()))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800621 .WillOnce(Invoke([&]([[maybe_unused]] const std::string& service,
622 [[maybe_unused]] const std::string& path,
623 SensorProperties* prop) {
Patrick Williamse1dbb592023-10-20 11:19:22 -0500624 prop->scale = _scale;
625 prop->value = _value;
626 prop->unit = "x";
627 prop->min = 0;
628 prop->max = 0;
629 prop->available = true;
630 }));
Alex.Song8f73ad72021-10-07 00:18:27 +0800631 EXPECT_CALL(*helper, thresholdsAsserted(StrEq("asdf"), StrEq(path)))
632 .WillOnce(Return(false));
633
634 auto info = conf::SensorConfig();
635 info.unavailableAsFailed = false;
636 ri = DbusPassive::createDbusPassive(bus_mock, type, id,
637 std::move(helper), &info, nullptr);
638 passive = reinterpret_cast<DbusPassive*>(ri.get());
639 EXPECT_FALSE(passive == nullptr);
640 }
641
642 sdbusplus::SdBusMock sdbus_mock;
Patrick Williamsb228bc32022-07-22 19:26:56 -0500643 sdbusplus::bus_t bus_mock;
Alex.Song8f73ad72021-10-07 00:18:27 +0800644 std::unique_ptr<DbusHelperMock> helper;
645 std::string type = "temp";
646 std::string id = "id";
647 std::string path = "/xyz/openbmc_project/sensors/temperature/id";
648 int64_t _scale = -3;
649 int64_t _value = 10;
650
651 std::unique_ptr<ReadInterface> ri;
652 DbusPassive* passive;
653};
654
655TEST_F(DbusPassiveTestUnaSensorNotAsFailedObj, VerifyAvailableDeassert)
656{
Alex.Song8f73ad72021-10-07 00:18:27 +0800657 // Verifies when Availble is deasserted && unavailableAsFailed == false,
658 // the sensor remains at OK state but reading goes to NaN.
659 EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull()))
660 .WillOnce(Return(nullptr));
Patrick Williamsb228bc32022-07-22 19:26:56 -0500661 sdbusplus::message_t msg(nullptr, &sdbus_mock);
Alex.Song8f73ad72021-10-07 00:18:27 +0800662
663 const char* property = "Available";
664 bool asserted = false;
665 const char* intf = "xyz.openbmc_project.State.Decorator.Availability";
666
667 passive->setAvailable(true);
668
669 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull()))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800670 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
671 [[maybe_unused]] char type, void* p) {
Patrick Williamse1dbb592023-10-20 11:19:22 -0500672 const char** s = static_cast<const char**>(p);
673 // Read the first parameter, the string.
674 *s = intf;
675 return 0;
676 }))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800677 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
678 [[maybe_unused]] char type, void* p) {
Patrick Williamse1dbb592023-10-20 11:19:22 -0500679 const char** s = static_cast<const char**>(p);
680 *s = property;
681 // Read the string in the pair (dictionary).
682 return 0;
683 }));
Alex.Song8f73ad72021-10-07 00:18:27 +0800684
685 // std::map
686 EXPECT_CALL(sdbus_mock,
687 sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}")))
688 .WillOnce(Return(0));
689
690 // while !at_end()
691 EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0))
692 .WillOnce(Return(0))
693 .WillOnce(Return(1)); // So it exits the loop after reading one pair.
694
695 // std::pair
696 EXPECT_CALL(sdbus_mock,
697 sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv")))
698 .WillOnce(Return(0));
699
700 EXPECT_CALL(sdbus_mock,
701 sd_bus_message_verify_type(IsNull(), 'v', StrEq("x")))
702 .WillOnce(Return(0));
703 EXPECT_CALL(sdbus_mock,
704 sd_bus_message_verify_type(IsNull(), 'v', StrEq("d")))
705 .WillOnce(Return(0));
706 EXPECT_CALL(sdbus_mock,
707 sd_bus_message_verify_type(IsNull(), 'v', StrEq("b")))
708 .WillOnce(Return(1));
709 EXPECT_CALL(sdbus_mock,
710 sd_bus_message_enter_container(IsNull(), 'v', StrEq("b")))
711 .WillOnce(Return(0));
712
713 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'b', NotNull()))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800714 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
715 [[maybe_unused]] char type, void* p) {
Patrick Williamse1dbb592023-10-20 11:19:22 -0500716 bool* s = static_cast<bool*>(p);
717 *s = asserted;
718 return 0;
719 }));
Alex.Song8f73ad72021-10-07 00:18:27 +0800720
721 EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull()))
722 .WillOnce(Return(0)) /* variant. */
723 .WillOnce(Return(0)) /* std::pair */
724 .WillOnce(Return(0)); /* std::map */
725
726 int rv = handleSensorValue(msg, passive);
727 EXPECT_EQ(rv, 0); // It's always 0.
728 bool failed = passive->getFailed();
729 EXPECT_EQ(failed, false);
730 ReadReturn r = passive->read();
731 EXPECT_FALSE(std::isfinite(r.value));
732}
733
734TEST_F(DbusPassiveTestUnaSensorNotAsFailedObj, VerifyAvailableAssert)
735{
Alex.Song8f73ad72021-10-07 00:18:27 +0800736 // Verifies when a sensor's state goes from unavailble to available
737 // && unavailableAsFailed == false, this sensor remains at OK state.
738 EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull()))
739 .WillOnce(Return(nullptr));
Patrick Williamsb228bc32022-07-22 19:26:56 -0500740 sdbusplus::message_t msg(nullptr, &sdbus_mock);
Alex.Song8f73ad72021-10-07 00:18:27 +0800741
742 const char* property = "Available";
743 bool asserted = true;
744 const char* intf = "xyz.openbmc_project.State.Decorator.Availability";
745
746 passive->setAvailable(false);
747 bool failed = passive->getFailed();
748 EXPECT_EQ(failed, false);
749
750 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull()))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800751 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
752 [[maybe_unused]] char type, void* p) {
Patrick Williamse1dbb592023-10-20 11:19:22 -0500753 const char** s = static_cast<const char**>(p);
754 // Read the first parameter, the string.
755 *s = intf;
756 return 0;
757 }))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800758 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
759 [[maybe_unused]] char type, void* p) {
Patrick Williamse1dbb592023-10-20 11:19:22 -0500760 const char** s = static_cast<const char**>(p);
761 *s = property;
762 // Read the string in the pair (dictionary).
763 return 0;
764 }));
Alex.Song8f73ad72021-10-07 00:18:27 +0800765
766 // std::map
767 EXPECT_CALL(sdbus_mock,
768 sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}")))
769 .WillOnce(Return(0));
770
771 // while !at_end()
772 EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0))
773 .WillOnce(Return(0))
774 .WillOnce(Return(1)); // So it exits the loop after reading one pair.
775
776 // std::pair
777 EXPECT_CALL(sdbus_mock,
778 sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv")))
779 .WillOnce(Return(0));
780
781 EXPECT_CALL(sdbus_mock,
782 sd_bus_message_verify_type(IsNull(), 'v', StrEq("x")))
783 .WillOnce(Return(0));
784 EXPECT_CALL(sdbus_mock,
785 sd_bus_message_verify_type(IsNull(), 'v', StrEq("d")))
786 .WillOnce(Return(0));
787 EXPECT_CALL(sdbus_mock,
788 sd_bus_message_verify_type(IsNull(), 'v', StrEq("b")))
789 .WillOnce(Return(1));
790 EXPECT_CALL(sdbus_mock,
791 sd_bus_message_enter_container(IsNull(), 'v', StrEq("b")))
792 .WillOnce(Return(0));
793
794 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'b', NotNull()))
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800795 .WillOnce(Invoke([&]([[maybe_unused]] sd_bus_message* m,
796 [[maybe_unused]] char type, void* p) {
Patrick Williamse1dbb592023-10-20 11:19:22 -0500797 bool* s = static_cast<bool*>(p);
798 *s = asserted;
799 return 0;
800 }));
Alex.Song8f73ad72021-10-07 00:18:27 +0800801
802 EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull()))
803 .WillOnce(Return(0)) /* variant. */
804 .WillOnce(Return(0)) /* std::pair */
805 .WillOnce(Return(0)); /* std::map */
806
807 int rv = handleSensorValue(msg, passive);
808 EXPECT_EQ(rv, 0); // It's always 0.
809 failed = passive->getFailed();
810 EXPECT_EQ(failed, false);
811}
812
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800813void GetPropertiesMax3k([[maybe_unused]] const std::string& service,
814 [[maybe_unused]] const std::string& path,
Patrick Venture9b936922020-08-10 11:28:39 -0700815 SensorProperties* prop)
Patrick Venture6b9f5992019-09-10 09:18:28 -0700816{
817 prop->scale = -3;
818 prop->value = 10;
819 prop->unit = "x";
820 prop->min = 0;
821 prop->max = 3000;
822}
823
Patrick Venture9b936922020-08-10 11:28:39 -0700824using GetPropertiesFunction = std::function<void(
825 const std::string&, const std::string&, SensorProperties*)>;
Patrick Venture6b9f5992019-09-10 09:18:28 -0700826
827// TODO: There is definitely a cleaner way to do this.
828class DbusPassiveTest3kMaxObj : public ::testing::Test
829{
830 protected:
831 DbusPassiveTest3kMaxObj() :
832 sdbus_mock(),
Patrick Venture8729eb92020-08-10 10:38:44 -0700833 bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))),
834 helper(std::make_unique<DbusHelperMock>())
Patrick Venture6b9f5992019-09-10 09:18:28 -0700835 {
Patrick Venture9b936922020-08-10 11:28:39 -0700836 EXPECT_CALL(*helper, getService(StrEq(SensorIntf), StrEq(path)))
Patrick Venture6b9f5992019-09-10 09:18:28 -0700837 .WillOnce(Return("asdf"));
838
Patrick Venture8729eb92020-08-10 10:38:44 -0700839 EXPECT_CALL(*helper,
Patrick Venture9b936922020-08-10 11:28:39 -0700840 getProperties(StrEq("asdf"), StrEq(path), NotNull()))
Patrick Venture6b9f5992019-09-10 09:18:28 -0700841 .WillOnce(_getProps);
Patrick Venture9b936922020-08-10 11:28:39 -0700842 EXPECT_CALL(*helper, thresholdsAsserted(StrEq("asdf"), StrEq(path)))
Patrick Venture6b9f5992019-09-10 09:18:28 -0700843 .WillOnce(Return(false));
844
845 auto info = conf::SensorConfig();
Patrick Venture8729eb92020-08-10 10:38:44 -0700846 ri = DbusPassive::createDbusPassive(bus_mock, type, id,
847 std::move(helper), &info, nullptr);
Patrick Venture6b9f5992019-09-10 09:18:28 -0700848 passive = reinterpret_cast<DbusPassive*>(ri.get());
849 EXPECT_FALSE(passive == nullptr);
850 }
851
852 sdbusplus::SdBusMock sdbus_mock;
Patrick Williamsb228bc32022-07-22 19:26:56 -0500853 sdbusplus::bus_t bus_mock;
Patrick Venture8729eb92020-08-10 10:38:44 -0700854 std::unique_ptr<DbusHelperMock> helper;
Patrick Venture6b9f5992019-09-10 09:18:28 -0700855 std::string type = "temp";
856 std::string id = "id";
857 std::string path = "/xyz/openbmc_project/sensors/temperature/id";
858 int64_t _scale = -3;
859 int64_t _value = 10;
860
861 std::unique_ptr<ReadInterface> ri;
862 DbusPassive* passive;
863 GetPropertiesFunction _getProps = &GetPropertiesMax3k;
864};
865
866TEST_F(DbusPassiveTest3kMaxObj, ReadMinAndMaxReturnsExpected)
867{
868 EXPECT_DOUBLE_EQ(0, passive->getMin());
869 EXPECT_DOUBLE_EQ(3, passive->getMax());
870}
871
872class DbusPassiveTest3kMaxIgnoredObj : public ::testing::Test
873{
874 protected:
875 DbusPassiveTest3kMaxIgnoredObj() :
876 sdbus_mock(),
Patrick Venture8729eb92020-08-10 10:38:44 -0700877 bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))),
878 helper(std::make_unique<DbusHelperMock>())
Patrick Venture6b9f5992019-09-10 09:18:28 -0700879 {
Patrick Venture9b936922020-08-10 11:28:39 -0700880 EXPECT_CALL(*helper, getService(StrEq(SensorIntf), StrEq(path)))
Patrick Venture6b9f5992019-09-10 09:18:28 -0700881 .WillOnce(Return("asdf"));
882
Patrick Venture8729eb92020-08-10 10:38:44 -0700883 EXPECT_CALL(*helper,
Patrick Venture9b936922020-08-10 11:28:39 -0700884 getProperties(StrEq("asdf"), StrEq(path), NotNull()))
Patrick Venture6b9f5992019-09-10 09:18:28 -0700885 .WillOnce(_getProps);
Patrick Venture9b936922020-08-10 11:28:39 -0700886 EXPECT_CALL(*helper, thresholdsAsserted(StrEq("asdf"), StrEq(path)))
Patrick Venture6b9f5992019-09-10 09:18:28 -0700887 .WillOnce(Return(false));
888
889 auto info = conf::SensorConfig();
890 info.ignoreDbusMinMax = true;
Patrick Venture8729eb92020-08-10 10:38:44 -0700891 ri = DbusPassive::createDbusPassive(bus_mock, type, id,
892 std::move(helper), &info, nullptr);
Patrick Venture6b9f5992019-09-10 09:18:28 -0700893 passive = reinterpret_cast<DbusPassive*>(ri.get());
894 EXPECT_FALSE(passive == nullptr);
895 }
896
897 sdbusplus::SdBusMock sdbus_mock;
Patrick Williamsb228bc32022-07-22 19:26:56 -0500898 sdbusplus::bus_t bus_mock;
Patrick Venture8729eb92020-08-10 10:38:44 -0700899 std::unique_ptr<DbusHelperMock> helper;
Patrick Venture6b9f5992019-09-10 09:18:28 -0700900 std::string type = "temp";
901 std::string id = "id";
902 std::string path = "/xyz/openbmc_project/sensors/temperature/id";
903 int64_t _scale = -3;
904 int64_t _value = 10;
905
906 std::unique_ptr<ReadInterface> ri;
907 DbusPassive* passive;
908 GetPropertiesFunction _getProps = &GetPropertiesMax3k;
909};
910
911TEST_F(DbusPassiveTest3kMaxIgnoredObj, ReadMinAndMaxReturnsExpected)
912{
913 EXPECT_DOUBLE_EQ(0, passive->getMin());
914 EXPECT_DOUBLE_EQ(0, passive->getMax());
915}
Patrick Venturea0764872020-08-08 07:48:43 -0700916
917} // namespace
918} // namespace pid_control