blob: a04b4ecc2476c6c731ea6b6d51d4475b9ceea426 [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 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()))
79 .WillOnce(
80 Invoke([&](const std::string& service, const std::string& path,
81 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 Venture9b936922020-08-10 11:28:39 -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
Patrick Venture9b936922020-08-10 11:28:39 -0700439void GetPropertiesMax3k(const std::string& service, const std::string& path,
440 SensorProperties* prop)
Patrick Venture6b9f5992019-09-10 09:18:28 -0700441{
442 prop->scale = -3;
443 prop->value = 10;
444 prop->unit = "x";
445 prop->min = 0;
446 prop->max = 3000;
447}
448
Patrick Venture9b936922020-08-10 11:28:39 -0700449using GetPropertiesFunction = std::function<void(
450 const std::string&, const std::string&, SensorProperties*)>;
Patrick Venture6b9f5992019-09-10 09:18:28 -0700451
452// TODO: There is definitely a cleaner way to do this.
453class DbusPassiveTest3kMaxObj : public ::testing::Test
454{
455 protected:
456 DbusPassiveTest3kMaxObj() :
457 sdbus_mock(),
Patrick Venture8729eb92020-08-10 10:38:44 -0700458 bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))),
459 helper(std::make_unique<DbusHelperMock>())
Patrick Venture6b9f5992019-09-10 09:18:28 -0700460 {
Patrick Venture9b936922020-08-10 11:28:39 -0700461 EXPECT_CALL(*helper, getService(StrEq(SensorIntf), StrEq(path)))
Patrick Venture6b9f5992019-09-10 09:18:28 -0700462 .WillOnce(Return("asdf"));
463
Patrick Venture8729eb92020-08-10 10:38:44 -0700464 EXPECT_CALL(*helper,
Patrick Venture9b936922020-08-10 11:28:39 -0700465 getProperties(StrEq("asdf"), StrEq(path), NotNull()))
Patrick Venture6b9f5992019-09-10 09:18:28 -0700466 .WillOnce(_getProps);
Patrick Venture9b936922020-08-10 11:28:39 -0700467 EXPECT_CALL(*helper, thresholdsAsserted(StrEq("asdf"), StrEq(path)))
Patrick Venture6b9f5992019-09-10 09:18:28 -0700468 .WillOnce(Return(false));
469
470 auto info = conf::SensorConfig();
Patrick Venture8729eb92020-08-10 10:38:44 -0700471 ri = DbusPassive::createDbusPassive(bus_mock, type, id,
472 std::move(helper), &info, nullptr);
Patrick Venture6b9f5992019-09-10 09:18:28 -0700473 passive = reinterpret_cast<DbusPassive*>(ri.get());
474 EXPECT_FALSE(passive == nullptr);
475 }
476
477 sdbusplus::SdBusMock sdbus_mock;
478 sdbusplus::bus::bus bus_mock;
Patrick Venture8729eb92020-08-10 10:38:44 -0700479 std::unique_ptr<DbusHelperMock> helper;
Patrick Venture6b9f5992019-09-10 09:18:28 -0700480 std::string type = "temp";
481 std::string id = "id";
482 std::string path = "/xyz/openbmc_project/sensors/temperature/id";
483 int64_t _scale = -3;
484 int64_t _value = 10;
485
486 std::unique_ptr<ReadInterface> ri;
487 DbusPassive* passive;
488 GetPropertiesFunction _getProps = &GetPropertiesMax3k;
489};
490
491TEST_F(DbusPassiveTest3kMaxObj, ReadMinAndMaxReturnsExpected)
492{
493 EXPECT_DOUBLE_EQ(0, passive->getMin());
494 EXPECT_DOUBLE_EQ(3, passive->getMax());
495}
496
497class DbusPassiveTest3kMaxIgnoredObj : public ::testing::Test
498{
499 protected:
500 DbusPassiveTest3kMaxIgnoredObj() :
501 sdbus_mock(),
Patrick Venture8729eb92020-08-10 10:38:44 -0700502 bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))),
503 helper(std::make_unique<DbusHelperMock>())
Patrick Venture6b9f5992019-09-10 09:18:28 -0700504 {
Patrick Venture9b936922020-08-10 11:28:39 -0700505 EXPECT_CALL(*helper, getService(StrEq(SensorIntf), StrEq(path)))
Patrick Venture6b9f5992019-09-10 09:18:28 -0700506 .WillOnce(Return("asdf"));
507
Patrick Venture8729eb92020-08-10 10:38:44 -0700508 EXPECT_CALL(*helper,
Patrick Venture9b936922020-08-10 11:28:39 -0700509 getProperties(StrEq("asdf"), StrEq(path), NotNull()))
Patrick Venture6b9f5992019-09-10 09:18:28 -0700510 .WillOnce(_getProps);
Patrick Venture9b936922020-08-10 11:28:39 -0700511 EXPECT_CALL(*helper, thresholdsAsserted(StrEq("asdf"), StrEq(path)))
Patrick Venture6b9f5992019-09-10 09:18:28 -0700512 .WillOnce(Return(false));
513
514 auto info = conf::SensorConfig();
515 info.ignoreDbusMinMax = true;
Patrick Venture8729eb92020-08-10 10:38:44 -0700516 ri = DbusPassive::createDbusPassive(bus_mock, type, id,
517 std::move(helper), &info, nullptr);
Patrick Venture6b9f5992019-09-10 09:18:28 -0700518 passive = reinterpret_cast<DbusPassive*>(ri.get());
519 EXPECT_FALSE(passive == nullptr);
520 }
521
522 sdbusplus::SdBusMock sdbus_mock;
523 sdbusplus::bus::bus bus_mock;
Patrick Venture8729eb92020-08-10 10:38:44 -0700524 std::unique_ptr<DbusHelperMock> helper;
Patrick Venture6b9f5992019-09-10 09:18:28 -0700525 std::string type = "temp";
526 std::string id = "id";
527 std::string path = "/xyz/openbmc_project/sensors/temperature/id";
528 int64_t _scale = -3;
529 int64_t _value = 10;
530
531 std::unique_ptr<ReadInterface> ri;
532 DbusPassive* passive;
533 GetPropertiesFunction _getProps = &GetPropertiesMax3k;
534};
535
536TEST_F(DbusPassiveTest3kMaxIgnoredObj, ReadMinAndMaxReturnsExpected)
537{
538 EXPECT_DOUBLE_EQ(0, passive->getMin());
539 EXPECT_DOUBLE_EQ(0, passive->getMax());
540}
Patrick Venturea0764872020-08-08 07:48:43 -0700541
542} // namespace
543} // namespace pid_control