blob: 8eb341609825e1b4cef2680d3d0611dd368a55b8 [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 Venturef8cb4642018-10-30 12:02:53 -070059 struct 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 Venture8729eb92020-08-10 10:38:44 -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 Venture563a3562018-10-30 09:31:26 -070078 getProperties(_, StrEq("asdf"), StrEq(path), NotNull()))
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070079 .WillOnce(Invoke(
Patrick Venturee2ec0f62018-09-04 12:30:27 -070080 [&](sdbusplus::bus::bus& bus, const std::string& service,
81 const std::string& path, struct SensorProperties* prop) {
Patrick Venture0ef1faf2018-06-13 12:50:53 -070082 prop->scale = _scale;
83 prop->value = _value;
84 prop->unit = "x";
Patrick Venture6b9f5992019-09-10 09:18:28 -070085 prop->min = 0;
86 prop->max = 0;
Patrick Venture0ef1faf2018-06-13 12:50:53 -070087 }));
Patrick Venture8729eb92020-08-10 10:38:44 -070088 EXPECT_CALL(*helper, thresholdsAsserted(_, StrEq("asdf"), StrEq(path)))
James Feist36b7d8e2018-10-05 15:39:01 -070089 .WillOnce(Return(false));
Patrick Venture0ef1faf2018-06-13 12:50:53 -070090
James Feistf81f2882019-02-26 11:26:36 -080091 auto info = conf::SensorConfig();
Patrick Venture8729eb92020-08-10 10:38:44 -070092 ri = DbusPassive::createDbusPassive(bus_mock, type, id,
93 std::move(helper), &info, nullptr);
Patrick Venturee2ec0f62018-09-04 12:30:27 -070094 passive = reinterpret_cast<DbusPassive*>(ri.get());
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070095 EXPECT_FALSE(passive == nullptr);
96 }
Patrick Venture0ef1faf2018-06-13 12:50:53 -070097
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070098 sdbusplus::SdBusMock sdbus_mock;
99 sdbusplus::bus::bus bus_mock;
Patrick Venture8729eb92020-08-10 10:38:44 -0700100 std::unique_ptr<DbusHelperMock> helper;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700101 std::string type = "temp";
102 std::string id = "id";
103 std::string path = "/xyz/openbmc_project/sensors/temperature/id";
104 int64_t _scale = -3;
105 int64_t _value = 10;
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700106
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700107 std::unique_ptr<ReadInterface> ri;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700108 DbusPassive* passive;
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700109};
110
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700111TEST_F(DbusPassiveTestObj, ReadReturnsExpectedValues)
112{
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700113 // Verify read is returning the values.
114 ReadReturn v;
115 v.value = 0.01;
116 // TODO: updated is set when the value is created, so we can range check
117 // it.
118 ReadReturn r = passive->read();
119 EXPECT_EQ(v.value, r.value);
120}
121
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700122TEST_F(DbusPassiveTestObj, SetValueUpdatesValue)
123{
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700124 // Verify setvalue does as advertised.
125
126 double value = 0.01;
127 passive->setValue(value);
128
129 // TODO: updated is set when the value is set, so we can range check it.
130 ReadReturn r = passive->read();
131 EXPECT_EQ(value, r.value);
132}
133
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700134TEST_F(DbusPassiveTestObj, GetScaleReturnsExpectedValue)
135{
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700136 // Verify the scale is returned as expected.
137 EXPECT_EQ(_scale, passive->getScale());
138}
139
Patrick Venture563a3562018-10-30 09:31:26 -0700140TEST_F(DbusPassiveTestObj, getIDReturnsExpectedValue)
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700141{
Patrick Venture563a3562018-10-30 09:31:26 -0700142 // Verify getID returns the expected value.
143 EXPECT_EQ(id, passive->getID());
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700144}
145
Patrick Venture6b9f5992019-09-10 09:18:28 -0700146TEST_F(DbusPassiveTestObj, GetMinValueReturnsExpectedValue)
147{
148 EXPECT_DOUBLE_EQ(0, passive->getMin());
149}
150
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700151TEST_F(DbusPassiveTestObj, VerifyHandlesDbusSignal)
152{
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700153 // The dbus passive sensor listens for updates and if it's the Value
154 // property, it needs to handle it.
155
156 EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull()))
157 .WillOnce(Return(nullptr));
158 sdbusplus::message::message msg(nullptr, &sdbus_mock);
159
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700160 const char* Value = "Value";
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700161 int64_t xValue = 10000;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700162 const char* intf = "xyz.openbmc_project.Sensor.Value";
James Feist1f802f52019-02-08 13:51:43 -0800163 // string, std::map<std::string, std::variant<int64_t>>
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700164 // msg.read(msgSensor, msgData);
165
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700166 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull()))
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700167 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
168 const char** s = static_cast<const char**>(p);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700169 // Read the first parameter, the string.
170 *s = intf;
171 return 0;
172 }))
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700173 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
174 const char** s = static_cast<const char**>(p);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700175 *s = Value;
176 // Read the string in the pair (dictionary).
177 return 0;
178 }));
179
180 // std::map
181 EXPECT_CALL(sdbus_mock,
182 sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}")))
183 .WillOnce(Return(0));
184
185 // while !at_end()
186 EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0))
187 .WillOnce(Return(0))
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700188 .WillOnce(Return(1)); // So it exits the loop after reading one pair.
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700189
190 // std::pair
191 EXPECT_CALL(sdbus_mock,
192 sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv")))
193 .WillOnce(Return(0));
194
195 EXPECT_CALL(sdbus_mock,
196 sd_bus_message_verify_type(IsNull(), 'v', StrEq("x")))
197 .WillOnce(Return(1));
198 EXPECT_CALL(sdbus_mock,
199 sd_bus_message_enter_container(IsNull(), 'v', StrEq("x")))
200 .WillOnce(Return(0));
201
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700202 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'x', NotNull()))
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700203 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
204 int64_t* s = static_cast<int64_t*>(p);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700205 *s = xValue;
206 return 0;
207 }));
208
209 EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull()))
210 .WillOnce(Return(0)) /* variant. */
211 .WillOnce(Return(0)) /* std::pair */
212 .WillOnce(Return(0)); /* std::map */
213
Patrick Venture7af157b2018-10-30 11:24:40 -0700214 int rv = handleSensorValue(msg, passive);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700215 EXPECT_EQ(rv, 0); // It's always 0.
216
217 ReadReturn r = passive->read();
218 EXPECT_EQ(10, r.value);
219}
220
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700221TEST_F(DbusPassiveTestObj, VerifyIgnoresOtherPropertySignal)
222{
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700223 // The dbus passive sensor listens for updates and if it's the Value
224 // property, it needs to handle it. In this case, it won't be.
225
226 EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull()))
227 .WillOnce(Return(nullptr));
228 sdbusplus::message::message msg(nullptr, &sdbus_mock);
229
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700230 const char* Scale = "Scale";
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700231 int64_t xScale = -6;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700232 const char* intf = "xyz.openbmc_project.Sensor.Value";
James Feist1f802f52019-02-08 13:51:43 -0800233 // string, std::map<std::string, std::variant<int64_t>>
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700234 // msg.read(msgSensor, msgData);
235
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700236 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull()))
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700237 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
238 const char** s = static_cast<const char**>(p);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700239 // Read the first parameter, the string.
240 *s = intf;
241 return 0;
242 }))
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700243 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
244 const char** s = static_cast<const char**>(p);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700245 *s = Scale;
246 // Read the string in the pair (dictionary).
247 return 0;
248 }));
249
250 // std::map
251 EXPECT_CALL(sdbus_mock,
252 sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}")))
253 .WillOnce(Return(0));
254
255 // while !at_end()
256 EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0))
257 .WillOnce(Return(0))
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700258 .WillOnce(Return(1)); // So it exits the loop after reading one pair.
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700259
260 // std::pair
261 EXPECT_CALL(sdbus_mock,
262 sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv")))
263 .WillOnce(Return(0));
264
265 EXPECT_CALL(sdbus_mock,
266 sd_bus_message_verify_type(IsNull(), 'v', StrEq("x")))
267 .WillOnce(Return(1));
268 EXPECT_CALL(sdbus_mock,
269 sd_bus_message_enter_container(IsNull(), 'v', StrEq("x")))
270 .WillOnce(Return(0));
271
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700272 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'x', NotNull()))
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700273 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
274 int64_t* s = static_cast<int64_t*>(p);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700275 *s = xScale;
276 return 0;
277 }));
278
279 EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull()))
280 .WillOnce(Return(0)) /* variant. */
281 .WillOnce(Return(0)) /* std::pair */
282 .WillOnce(Return(0)); /* std::map */
283
Patrick Venture7af157b2018-10-30 11:24:40 -0700284 int rv = handleSensorValue(msg, passive);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700285 EXPECT_EQ(rv, 0); // It's always 0.
286
287 ReadReturn r = passive->read();
288 EXPECT_EQ(0.01, r.value);
289}
James Feist36b7d8e2018-10-05 15:39:01 -0700290TEST_F(DbusPassiveTestObj, VerifyCriticalThresholdAssert)
291{
292
293 // Verifies when a threshold is crossed the sensor goes into error state
294 EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull()))
295 .WillOnce(Return(nullptr));
296 sdbusplus::message::message msg(nullptr, &sdbus_mock);
297
298 const char* criticalAlarm = "CriticalAlarmHigh";
299 bool alarm = true;
300 const char* intf = "xyz.openbmc_project.Sensor.Threshold.Critical";
301
302 passive->setFailed(false);
303
304 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull()))
305 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
306 const char** s = static_cast<const char**>(p);
307 // Read the first parameter, the string.
308 *s = intf;
309 return 0;
310 }))
311 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
312 const char** s = static_cast<const char**>(p);
313 *s = criticalAlarm;
314 // Read the string in the pair (dictionary).
315 return 0;
316 }));
317
318 // std::map
319 EXPECT_CALL(sdbus_mock,
320 sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}")))
321 .WillOnce(Return(0));
322
323 // while !at_end()
324 EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0))
325 .WillOnce(Return(0))
326 .WillOnce(Return(1)); // So it exits the loop after reading one pair.
327
328 // std::pair
329 EXPECT_CALL(sdbus_mock,
330 sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv")))
331 .WillOnce(Return(0));
332
333 EXPECT_CALL(sdbus_mock,
334 sd_bus_message_verify_type(IsNull(), 'v', StrEq("x")))
335 .WillOnce(Return(0));
336 EXPECT_CALL(sdbus_mock,
337 sd_bus_message_verify_type(IsNull(), 'v', StrEq("d")))
338 .WillOnce(Return(0));
339 EXPECT_CALL(sdbus_mock,
340 sd_bus_message_verify_type(IsNull(), 'v', StrEq("b")))
341 .WillOnce(Return(1));
342 EXPECT_CALL(sdbus_mock,
343 sd_bus_message_enter_container(IsNull(), 'v', StrEq("b")))
344 .WillOnce(Return(0));
345
346 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'b', NotNull()))
347 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
348 bool* s = static_cast<bool*>(p);
349 *s = alarm;
350 return 0;
351 }));
352
353 EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull()))
354 .WillOnce(Return(0)) /* variant. */
355 .WillOnce(Return(0)) /* std::pair */
356 .WillOnce(Return(0)); /* std::map */
357
Patrick Venture7af157b2018-10-30 11:24:40 -0700358 int rv = handleSensorValue(msg, passive);
James Feist36b7d8e2018-10-05 15:39:01 -0700359 EXPECT_EQ(rv, 0); // It's always 0.
360 bool failed = passive->getFailed();
361 EXPECT_EQ(failed, true);
362}
363
364TEST_F(DbusPassiveTestObj, VerifyCriticalThresholdDeassert)
365{
366
367 // Verifies when a threshold is deasserted a failed sensor goes back into
368 // the normal state
369 EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull()))
370 .WillOnce(Return(nullptr));
371 sdbusplus::message::message msg(nullptr, &sdbus_mock);
372
373 const char* criticalAlarm = "CriticalAlarmHigh";
374 bool alarm = false;
375 const char* intf = "xyz.openbmc_project.Sensor.Threshold.Critical";
376
377 passive->setFailed(true);
378
379 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull()))
380 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
381 const char** s = static_cast<const char**>(p);
382 // Read the first parameter, the string.
383 *s = intf;
384 return 0;
385 }))
386 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
387 const char** s = static_cast<const char**>(p);
388 *s = criticalAlarm;
389 // Read the string in the pair (dictionary).
390 return 0;
391 }));
392
393 // std::map
394 EXPECT_CALL(sdbus_mock,
395 sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}")))
396 .WillOnce(Return(0));
397
398 // while !at_end()
399 EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0))
400 .WillOnce(Return(0))
401 .WillOnce(Return(1)); // So it exits the loop after reading one pair.
402
403 // std::pair
404 EXPECT_CALL(sdbus_mock,
405 sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv")))
406 .WillOnce(Return(0));
407
408 EXPECT_CALL(sdbus_mock,
409 sd_bus_message_verify_type(IsNull(), 'v', StrEq("x")))
410 .WillOnce(Return(0));
411 EXPECT_CALL(sdbus_mock,
412 sd_bus_message_verify_type(IsNull(), 'v', StrEq("d")))
413 .WillOnce(Return(0));
414 EXPECT_CALL(sdbus_mock,
415 sd_bus_message_verify_type(IsNull(), 'v', StrEq("b")))
416 .WillOnce(Return(1));
417 EXPECT_CALL(sdbus_mock,
418 sd_bus_message_enter_container(IsNull(), 'v', StrEq("b")))
419 .WillOnce(Return(0));
420
421 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'b', NotNull()))
422 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
423 bool* s = static_cast<bool*>(p);
424 *s = alarm;
425 return 0;
426 }));
427
428 EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull()))
429 .WillOnce(Return(0)) /* variant. */
430 .WillOnce(Return(0)) /* std::pair */
431 .WillOnce(Return(0)); /* std::map */
432
Patrick Venture7af157b2018-10-30 11:24:40 -0700433 int rv = handleSensorValue(msg, passive);
James Feist36b7d8e2018-10-05 15:39:01 -0700434 EXPECT_EQ(rv, 0); // It's always 0.
435 bool failed = passive->getFailed();
436 EXPECT_EQ(failed, false);
Patrick Venture563a3562018-10-30 09:31:26 -0700437}
Patrick Venture6b9f5992019-09-10 09:18:28 -0700438
439void GetPropertiesMax3k(sdbusplus::bus::bus& bus, const std::string& service,
440 const std::string& path, SensorProperties* prop)
441{
442 prop->scale = -3;
443 prop->value = 10;
444 prop->unit = "x";
445 prop->min = 0;
446 prop->max = 3000;
447}
448
449using GetPropertiesFunction =
450 std::function<void(sdbusplus::bus::bus&, const std::string&,
451 const std::string&, SensorProperties*)>;
452
453// TODO: There is definitely a cleaner way to do this.
454class DbusPassiveTest3kMaxObj : public ::testing::Test
455{
456 protected:
457 DbusPassiveTest3kMaxObj() :
458 sdbus_mock(),
Patrick Venture8729eb92020-08-10 10:38:44 -0700459 bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))),
460 helper(std::make_unique<DbusHelperMock>())
Patrick Venture6b9f5992019-09-10 09:18:28 -0700461 {
Patrick Venture8729eb92020-08-10 10:38:44 -0700462 EXPECT_CALL(*helper, getService(_, StrEq(SensorIntf), StrEq(path)))
Patrick Venture6b9f5992019-09-10 09:18:28 -0700463 .WillOnce(Return("asdf"));
464
Patrick Venture8729eb92020-08-10 10:38:44 -0700465 EXPECT_CALL(*helper,
Patrick Venture6b9f5992019-09-10 09:18:28 -0700466 getProperties(_, StrEq("asdf"), StrEq(path), NotNull()))
467 .WillOnce(_getProps);
Patrick Venture8729eb92020-08-10 10:38:44 -0700468 EXPECT_CALL(*helper, thresholdsAsserted(_, StrEq("asdf"), StrEq(path)))
Patrick Venture6b9f5992019-09-10 09:18:28 -0700469 .WillOnce(Return(false));
470
471 auto info = conf::SensorConfig();
Patrick Venture8729eb92020-08-10 10:38:44 -0700472 ri = DbusPassive::createDbusPassive(bus_mock, type, id,
473 std::move(helper), &info, nullptr);
Patrick Venture6b9f5992019-09-10 09:18:28 -0700474 passive = reinterpret_cast<DbusPassive*>(ri.get());
475 EXPECT_FALSE(passive == nullptr);
476 }
477
478 sdbusplus::SdBusMock sdbus_mock;
479 sdbusplus::bus::bus bus_mock;
Patrick Venture8729eb92020-08-10 10:38:44 -0700480 std::unique_ptr<DbusHelperMock> helper;
Patrick Venture6b9f5992019-09-10 09:18:28 -0700481 std::string type = "temp";
482 std::string id = "id";
483 std::string path = "/xyz/openbmc_project/sensors/temperature/id";
484 int64_t _scale = -3;
485 int64_t _value = 10;
486
487 std::unique_ptr<ReadInterface> ri;
488 DbusPassive* passive;
489 GetPropertiesFunction _getProps = &GetPropertiesMax3k;
490};
491
492TEST_F(DbusPassiveTest3kMaxObj, ReadMinAndMaxReturnsExpected)
493{
494 EXPECT_DOUBLE_EQ(0, passive->getMin());
495 EXPECT_DOUBLE_EQ(3, passive->getMax());
496}
497
498class DbusPassiveTest3kMaxIgnoredObj : public ::testing::Test
499{
500 protected:
501 DbusPassiveTest3kMaxIgnoredObj() :
502 sdbus_mock(),
Patrick Venture8729eb92020-08-10 10:38:44 -0700503 bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))),
504 helper(std::make_unique<DbusHelperMock>())
Patrick Venture6b9f5992019-09-10 09:18:28 -0700505 {
Patrick Venture8729eb92020-08-10 10:38:44 -0700506 EXPECT_CALL(*helper, getService(_, StrEq(SensorIntf), StrEq(path)))
Patrick Venture6b9f5992019-09-10 09:18:28 -0700507 .WillOnce(Return("asdf"));
508
Patrick Venture8729eb92020-08-10 10:38:44 -0700509 EXPECT_CALL(*helper,
Patrick Venture6b9f5992019-09-10 09:18:28 -0700510 getProperties(_, StrEq("asdf"), StrEq(path), NotNull()))
511 .WillOnce(_getProps);
Patrick Venture8729eb92020-08-10 10:38:44 -0700512 EXPECT_CALL(*helper, thresholdsAsserted(_, StrEq("asdf"), StrEq(path)))
Patrick Venture6b9f5992019-09-10 09:18:28 -0700513 .WillOnce(Return(false));
514
515 auto info = conf::SensorConfig();
516 info.ignoreDbusMinMax = true;
Patrick Venture8729eb92020-08-10 10:38:44 -0700517 ri = DbusPassive::createDbusPassive(bus_mock, type, id,
518 std::move(helper), &info, nullptr);
Patrick Venture6b9f5992019-09-10 09:18:28 -0700519 passive = reinterpret_cast<DbusPassive*>(ri.get());
520 EXPECT_FALSE(passive == nullptr);
521 }
522
523 sdbusplus::SdBusMock sdbus_mock;
524 sdbusplus::bus::bus bus_mock;
Patrick Venture8729eb92020-08-10 10:38:44 -0700525 std::unique_ptr<DbusHelperMock> helper;
Patrick Venture6b9f5992019-09-10 09:18:28 -0700526 std::string type = "temp";
527 std::string id = "id";
528 std::string path = "/xyz/openbmc_project/sensors/temperature/id";
529 int64_t _scale = -3;
530 int64_t _value = 10;
531
532 std::unique_ptr<ReadInterface> ri;
533 DbusPassive* passive;
534 GetPropertiesFunction _getProps = &GetPropertiesMax3k;
535};
536
537TEST_F(DbusPassiveTest3kMaxIgnoredObj, ReadMinAndMaxReturnsExpected)
538{
539 EXPECT_DOUBLE_EQ(0, passive->getMin());
540 EXPECT_DOUBLE_EQ(0, passive->getMax());
541}
Patrick Venturea0764872020-08-08 07:48:43 -0700542
543} // namespace
544} // namespace pid_control