blob: d4e9e98103e4023c037af8c91adf5c8badb062b1 [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 Venture0ef1faf2018-06-13 12:50:53 -07008#include <string>
James Feist1f802f52019-02-08 13:51:43 -08009#include <variant>
Patrick Venture0ef1faf2018-06-13 12:50:53 -070010
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070011#include <gmock/gmock.h>
12#include <gtest/gtest.h>
Patrick Venture0ef1faf2018-06-13 12:50:53 -070013
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070014using ::testing::_;
Patrick Venture0ef1faf2018-06-13 12:50:53 -070015using ::testing::InSequence;
16using ::testing::Invoke;
17using ::testing::IsNull;
18using ::testing::NotNull;
19using ::testing::Return;
20using ::testing::StrEq;
Patrick Venture0ef1faf2018-06-13 12:50:53 -070021
22std::string SensorIntf = "xyz.openbmc_project.Sensor.Value";
23
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070024TEST(DbusPassiveTest, FactoryFailsWithInvalidType)
25{
Patrick Venture0ef1faf2018-06-13 12:50:53 -070026 // Verify the type is checked by the factory.
27
28 sdbusplus::SdBusMock sdbus_mock;
29 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
30 std::string type = "invalid";
31 std::string id = "id";
32
33 DbusHelperMock helper;
James Feistf81f2882019-02-26 11:26:36 -080034 auto info = conf::SensorConfig();
Patrick Venture0ef1faf2018-06-13 12:50:53 -070035
James Feist98b704e2019-06-03 16:24:53 -070036 std::unique_ptr<ReadInterface> ri = DbusPassive::createDbusPassive(
37 bus_mock, type, id, &helper, &info, nullptr);
Patrick Venture0ef1faf2018-06-13 12:50:53 -070038
39 EXPECT_EQ(ri, nullptr);
40}
41
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070042TEST(DbusPassiveTest, BoringConstructorTest)
43{
Patrick Venturef8cb4642018-10-30 12:02:53 -070044 // Simply build the object, does no error checking.
Patrick Venture0ef1faf2018-06-13 12:50:53 -070045
46 sdbusplus::SdBusMock sdbus_mock;
47 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
48 std::string type = "invalid";
49 std::string id = "id";
50 std::string path = "/xyz/openbmc_project/sensors/unknown/id";
51
52 DbusHelperMock helper;
Patrick Venturef8cb4642018-10-30 12:02:53 -070053 struct SensorProperties properties;
Patrick Venture0ef1faf2018-06-13 12:50:53 -070054
James Feist98b704e2019-06-03 16:24:53 -070055 DbusPassive(bus_mock, type, id, &helper, properties, false, path, nullptr);
Patrick Venture0ef1faf2018-06-13 12:50:53 -070056 // Success
57}
58
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070059class DbusPassiveTestObj : public ::testing::Test
60{
61 protected:
62 DbusPassiveTestObj() :
63 sdbus_mock(),
64 bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))), helper()
65 {
Patrick Venture563a3562018-10-30 09:31:26 -070066 EXPECT_CALL(helper, getService(_, StrEq(SensorIntf), StrEq(path)))
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070067 .WillOnce(Return("asdf"));
Patrick Venture0ef1faf2018-06-13 12:50:53 -070068
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070069 EXPECT_CALL(helper,
Patrick Venture563a3562018-10-30 09:31:26 -070070 getProperties(_, StrEq("asdf"), StrEq(path), NotNull()))
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070071 .WillOnce(Invoke(
Patrick Venturee2ec0f62018-09-04 12:30:27 -070072 [&](sdbusplus::bus::bus& bus, const std::string& service,
73 const std::string& path, struct SensorProperties* prop) {
Patrick Venture0ef1faf2018-06-13 12:50:53 -070074 prop->scale = _scale;
75 prop->value = _value;
76 prop->unit = "x";
Patrick Venture6b9f5992019-09-10 09:18:28 -070077 prop->min = 0;
78 prop->max = 0;
Patrick Venture0ef1faf2018-06-13 12:50:53 -070079 }));
Patrick Venture563a3562018-10-30 09:31:26 -070080 EXPECT_CALL(helper, thresholdsAsserted(_, StrEq("asdf"), StrEq(path)))
James Feist36b7d8e2018-10-05 15:39:01 -070081 .WillOnce(Return(false));
Patrick Venture0ef1faf2018-06-13 12:50:53 -070082
James Feistf81f2882019-02-26 11:26:36 -080083 auto info = conf::SensorConfig();
James Feist98b704e2019-06-03 16:24:53 -070084 ri = DbusPassive::createDbusPassive(bus_mock, type, id, &helper, &info,
85 nullptr);
Patrick Venturee2ec0f62018-09-04 12:30:27 -070086 passive = reinterpret_cast<DbusPassive*>(ri.get());
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070087 EXPECT_FALSE(passive == nullptr);
88 }
Patrick Venture0ef1faf2018-06-13 12:50:53 -070089
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070090 sdbusplus::SdBusMock sdbus_mock;
91 sdbusplus::bus::bus bus_mock;
92 DbusHelperMock helper;
93 std::string type = "temp";
94 std::string id = "id";
95 std::string path = "/xyz/openbmc_project/sensors/temperature/id";
96 int64_t _scale = -3;
97 int64_t _value = 10;
Patrick Venture0ef1faf2018-06-13 12:50:53 -070098
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070099 std::unique_ptr<ReadInterface> ri;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700100 DbusPassive* passive;
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700101};
102
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700103TEST_F(DbusPassiveTestObj, ReadReturnsExpectedValues)
104{
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700105 // Verify read is returning the values.
106 ReadReturn v;
107 v.value = 0.01;
108 // TODO: updated is set when the value is created, so we can range check
109 // it.
110 ReadReturn r = passive->read();
111 EXPECT_EQ(v.value, r.value);
112}
113
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700114TEST_F(DbusPassiveTestObj, SetValueUpdatesValue)
115{
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700116 // Verify setvalue does as advertised.
117
118 double value = 0.01;
119 passive->setValue(value);
120
121 // TODO: updated is set when the value is set, so we can range check it.
122 ReadReturn r = passive->read();
123 EXPECT_EQ(value, r.value);
124}
125
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700126TEST_F(DbusPassiveTestObj, GetScaleReturnsExpectedValue)
127{
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700128 // Verify the scale is returned as expected.
129 EXPECT_EQ(_scale, passive->getScale());
130}
131
Patrick Venture563a3562018-10-30 09:31:26 -0700132TEST_F(DbusPassiveTestObj, getIDReturnsExpectedValue)
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700133{
Patrick Venture563a3562018-10-30 09:31:26 -0700134 // Verify getID returns the expected value.
135 EXPECT_EQ(id, passive->getID());
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700136}
137
Patrick Venture6b9f5992019-09-10 09:18:28 -0700138TEST_F(DbusPassiveTestObj, GetMinValueReturnsExpectedValue)
139{
140 EXPECT_DOUBLE_EQ(0, passive->getMin());
141}
142
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700143TEST_F(DbusPassiveTestObj, VerifyHandlesDbusSignal)
144{
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700145 // The dbus passive sensor listens for updates and if it's the Value
146 // property, it needs to handle it.
147
148 EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull()))
149 .WillOnce(Return(nullptr));
150 sdbusplus::message::message msg(nullptr, &sdbus_mock);
151
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700152 const char* Value = "Value";
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700153 int64_t xValue = 10000;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700154 const char* intf = "xyz.openbmc_project.Sensor.Value";
James Feist1f802f52019-02-08 13:51:43 -0800155 // string, std::map<std::string, std::variant<int64_t>>
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700156 // msg.read(msgSensor, msgData);
157
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700158 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull()))
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700159 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
160 const char** s = static_cast<const char**>(p);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700161 // Read the first parameter, the string.
162 *s = intf;
163 return 0;
164 }))
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700165 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
166 const char** s = static_cast<const char**>(p);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700167 *s = Value;
168 // Read the string in the pair (dictionary).
169 return 0;
170 }));
171
172 // std::map
173 EXPECT_CALL(sdbus_mock,
174 sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}")))
175 .WillOnce(Return(0));
176
177 // while !at_end()
178 EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0))
179 .WillOnce(Return(0))
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700180 .WillOnce(Return(1)); // So it exits the loop after reading one pair.
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700181
182 // std::pair
183 EXPECT_CALL(sdbus_mock,
184 sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv")))
185 .WillOnce(Return(0));
186
187 EXPECT_CALL(sdbus_mock,
188 sd_bus_message_verify_type(IsNull(), 'v', StrEq("x")))
189 .WillOnce(Return(1));
190 EXPECT_CALL(sdbus_mock,
191 sd_bus_message_enter_container(IsNull(), 'v', StrEq("x")))
192 .WillOnce(Return(0));
193
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700194 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'x', NotNull()))
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700195 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
196 int64_t* s = static_cast<int64_t*>(p);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700197 *s = xValue;
198 return 0;
199 }));
200
201 EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull()))
202 .WillOnce(Return(0)) /* variant. */
203 .WillOnce(Return(0)) /* std::pair */
204 .WillOnce(Return(0)); /* std::map */
205
Patrick Venture7af157b2018-10-30 11:24:40 -0700206 int rv = handleSensorValue(msg, passive);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700207 EXPECT_EQ(rv, 0); // It's always 0.
208
209 ReadReturn r = passive->read();
210 EXPECT_EQ(10, r.value);
211}
212
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700213TEST_F(DbusPassiveTestObj, VerifyIgnoresOtherPropertySignal)
214{
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700215 // The dbus passive sensor listens for updates and if it's the Value
216 // property, it needs to handle it. In this case, it won't be.
217
218 EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull()))
219 .WillOnce(Return(nullptr));
220 sdbusplus::message::message msg(nullptr, &sdbus_mock);
221
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700222 const char* Scale = "Scale";
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700223 int64_t xScale = -6;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700224 const char* intf = "xyz.openbmc_project.Sensor.Value";
James Feist1f802f52019-02-08 13:51:43 -0800225 // string, std::map<std::string, std::variant<int64_t>>
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700226 // msg.read(msgSensor, msgData);
227
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700228 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull()))
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700229 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
230 const char** s = static_cast<const char**>(p);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700231 // Read the first parameter, the string.
232 *s = intf;
233 return 0;
234 }))
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700235 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
236 const char** s = static_cast<const char**>(p);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700237 *s = Scale;
238 // Read the string in the pair (dictionary).
239 return 0;
240 }));
241
242 // std::map
243 EXPECT_CALL(sdbus_mock,
244 sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}")))
245 .WillOnce(Return(0));
246
247 // while !at_end()
248 EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0))
249 .WillOnce(Return(0))
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700250 .WillOnce(Return(1)); // So it exits the loop after reading one pair.
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700251
252 // std::pair
253 EXPECT_CALL(sdbus_mock,
254 sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv")))
255 .WillOnce(Return(0));
256
257 EXPECT_CALL(sdbus_mock,
258 sd_bus_message_verify_type(IsNull(), 'v', StrEq("x")))
259 .WillOnce(Return(1));
260 EXPECT_CALL(sdbus_mock,
261 sd_bus_message_enter_container(IsNull(), 'v', StrEq("x")))
262 .WillOnce(Return(0));
263
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700264 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'x', NotNull()))
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700265 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
266 int64_t* s = static_cast<int64_t*>(p);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700267 *s = xScale;
268 return 0;
269 }));
270
271 EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull()))
272 .WillOnce(Return(0)) /* variant. */
273 .WillOnce(Return(0)) /* std::pair */
274 .WillOnce(Return(0)); /* std::map */
275
Patrick Venture7af157b2018-10-30 11:24:40 -0700276 int rv = handleSensorValue(msg, passive);
Patrick Venture0ef1faf2018-06-13 12:50:53 -0700277 EXPECT_EQ(rv, 0); // It's always 0.
278
279 ReadReturn r = passive->read();
280 EXPECT_EQ(0.01, r.value);
281}
James Feist36b7d8e2018-10-05 15:39:01 -0700282TEST_F(DbusPassiveTestObj, VerifyCriticalThresholdAssert)
283{
284
285 // Verifies when a threshold is crossed the sensor goes into error state
286 EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull()))
287 .WillOnce(Return(nullptr));
288 sdbusplus::message::message msg(nullptr, &sdbus_mock);
289
290 const char* criticalAlarm = "CriticalAlarmHigh";
291 bool alarm = true;
292 const char* intf = "xyz.openbmc_project.Sensor.Threshold.Critical";
293
294 passive->setFailed(false);
295
296 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull()))
297 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
298 const char** s = static_cast<const char**>(p);
299 // Read the first parameter, the string.
300 *s = intf;
301 return 0;
302 }))
303 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
304 const char** s = static_cast<const char**>(p);
305 *s = criticalAlarm;
306 // Read the string in the pair (dictionary).
307 return 0;
308 }));
309
310 // std::map
311 EXPECT_CALL(sdbus_mock,
312 sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}")))
313 .WillOnce(Return(0));
314
315 // while !at_end()
316 EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0))
317 .WillOnce(Return(0))
318 .WillOnce(Return(1)); // So it exits the loop after reading one pair.
319
320 // std::pair
321 EXPECT_CALL(sdbus_mock,
322 sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv")))
323 .WillOnce(Return(0));
324
325 EXPECT_CALL(sdbus_mock,
326 sd_bus_message_verify_type(IsNull(), 'v', StrEq("x")))
327 .WillOnce(Return(0));
328 EXPECT_CALL(sdbus_mock,
329 sd_bus_message_verify_type(IsNull(), 'v', StrEq("d")))
330 .WillOnce(Return(0));
331 EXPECT_CALL(sdbus_mock,
332 sd_bus_message_verify_type(IsNull(), 'v', StrEq("b")))
333 .WillOnce(Return(1));
334 EXPECT_CALL(sdbus_mock,
335 sd_bus_message_enter_container(IsNull(), 'v', StrEq("b")))
336 .WillOnce(Return(0));
337
338 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'b', NotNull()))
339 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
340 bool* s = static_cast<bool*>(p);
341 *s = alarm;
342 return 0;
343 }));
344
345 EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull()))
346 .WillOnce(Return(0)) /* variant. */
347 .WillOnce(Return(0)) /* std::pair */
348 .WillOnce(Return(0)); /* std::map */
349
Patrick Venture7af157b2018-10-30 11:24:40 -0700350 int rv = handleSensorValue(msg, passive);
James Feist36b7d8e2018-10-05 15:39:01 -0700351 EXPECT_EQ(rv, 0); // It's always 0.
352 bool failed = passive->getFailed();
353 EXPECT_EQ(failed, true);
354}
355
356TEST_F(DbusPassiveTestObj, VerifyCriticalThresholdDeassert)
357{
358
359 // Verifies when a threshold is deasserted a failed sensor goes back into
360 // the normal state
361 EXPECT_CALL(sdbus_mock, sd_bus_message_ref(IsNull()))
362 .WillOnce(Return(nullptr));
363 sdbusplus::message::message msg(nullptr, &sdbus_mock);
364
365 const char* criticalAlarm = "CriticalAlarmHigh";
366 bool alarm = false;
367 const char* intf = "xyz.openbmc_project.Sensor.Threshold.Critical";
368
369 passive->setFailed(true);
370
371 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 's', NotNull()))
372 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
373 const char** s = static_cast<const char**>(p);
374 // Read the first parameter, the string.
375 *s = intf;
376 return 0;
377 }))
378 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
379 const char** s = static_cast<const char**>(p);
380 *s = criticalAlarm;
381 // Read the string in the pair (dictionary).
382 return 0;
383 }));
384
385 // std::map
386 EXPECT_CALL(sdbus_mock,
387 sd_bus_message_enter_container(IsNull(), 'a', StrEq("{sv}")))
388 .WillOnce(Return(0));
389
390 // while !at_end()
391 EXPECT_CALL(sdbus_mock, sd_bus_message_at_end(IsNull(), 0))
392 .WillOnce(Return(0))
393 .WillOnce(Return(1)); // So it exits the loop after reading one pair.
394
395 // std::pair
396 EXPECT_CALL(sdbus_mock,
397 sd_bus_message_enter_container(IsNull(), 'e', StrEq("sv")))
398 .WillOnce(Return(0));
399
400 EXPECT_CALL(sdbus_mock,
401 sd_bus_message_verify_type(IsNull(), 'v', StrEq("x")))
402 .WillOnce(Return(0));
403 EXPECT_CALL(sdbus_mock,
404 sd_bus_message_verify_type(IsNull(), 'v', StrEq("d")))
405 .WillOnce(Return(0));
406 EXPECT_CALL(sdbus_mock,
407 sd_bus_message_verify_type(IsNull(), 'v', StrEq("b")))
408 .WillOnce(Return(1));
409 EXPECT_CALL(sdbus_mock,
410 sd_bus_message_enter_container(IsNull(), 'v', StrEq("b")))
411 .WillOnce(Return(0));
412
413 EXPECT_CALL(sdbus_mock, sd_bus_message_read_basic(IsNull(), 'b', NotNull()))
414 .WillOnce(Invoke([&](sd_bus_message* m, char type, void* p) {
415 bool* s = static_cast<bool*>(p);
416 *s = alarm;
417 return 0;
418 }));
419
420 EXPECT_CALL(sdbus_mock, sd_bus_message_exit_container(IsNull()))
421 .WillOnce(Return(0)) /* variant. */
422 .WillOnce(Return(0)) /* std::pair */
423 .WillOnce(Return(0)); /* std::map */
424
Patrick Venture7af157b2018-10-30 11:24:40 -0700425 int rv = handleSensorValue(msg, passive);
James Feist36b7d8e2018-10-05 15:39:01 -0700426 EXPECT_EQ(rv, 0); // It's always 0.
427 bool failed = passive->getFailed();
428 EXPECT_EQ(failed, false);
Patrick Venture563a3562018-10-30 09:31:26 -0700429}
Patrick Venture6b9f5992019-09-10 09:18:28 -0700430
431void GetPropertiesMax3k(sdbusplus::bus::bus& bus, const std::string& service,
432 const std::string& path, SensorProperties* prop)
433{
434 prop->scale = -3;
435 prop->value = 10;
436 prop->unit = "x";
437 prop->min = 0;
438 prop->max = 3000;
439}
440
441using GetPropertiesFunction =
442 std::function<void(sdbusplus::bus::bus&, const std::string&,
443 const std::string&, SensorProperties*)>;
444
445// TODO: There is definitely a cleaner way to do this.
446class DbusPassiveTest3kMaxObj : public ::testing::Test
447{
448 protected:
449 DbusPassiveTest3kMaxObj() :
450 sdbus_mock(),
451 bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))), helper()
452 {
453 EXPECT_CALL(helper, getService(_, StrEq(SensorIntf), StrEq(path)))
454 .WillOnce(Return("asdf"));
455
456 EXPECT_CALL(helper,
457 getProperties(_, StrEq("asdf"), StrEq(path), NotNull()))
458 .WillOnce(_getProps);
459 EXPECT_CALL(helper, thresholdsAsserted(_, StrEq("asdf"), StrEq(path)))
460 .WillOnce(Return(false));
461
462 auto info = conf::SensorConfig();
463 ri = DbusPassive::createDbusPassive(bus_mock, type, id, &helper, &info,
464 nullptr);
465 passive = reinterpret_cast<DbusPassive*>(ri.get());
466 EXPECT_FALSE(passive == nullptr);
467 }
468
469 sdbusplus::SdBusMock sdbus_mock;
470 sdbusplus::bus::bus bus_mock;
471 DbusHelperMock helper;
472 std::string type = "temp";
473 std::string id = "id";
474 std::string path = "/xyz/openbmc_project/sensors/temperature/id";
475 int64_t _scale = -3;
476 int64_t _value = 10;
477
478 std::unique_ptr<ReadInterface> ri;
479 DbusPassive* passive;
480 GetPropertiesFunction _getProps = &GetPropertiesMax3k;
481};
482
483TEST_F(DbusPassiveTest3kMaxObj, ReadMinAndMaxReturnsExpected)
484{
485 EXPECT_DOUBLE_EQ(0, passive->getMin());
486 EXPECT_DOUBLE_EQ(3, passive->getMax());
487}
488
489class DbusPassiveTest3kMaxIgnoredObj : public ::testing::Test
490{
491 protected:
492 DbusPassiveTest3kMaxIgnoredObj() :
493 sdbus_mock(),
494 bus_mock(std::move(sdbusplus::get_mocked_new(&sdbus_mock))), helper()
495 {
496 EXPECT_CALL(helper, getService(_, StrEq(SensorIntf), StrEq(path)))
497 .WillOnce(Return("asdf"));
498
499 EXPECT_CALL(helper,
500 getProperties(_, StrEq("asdf"), StrEq(path), NotNull()))
501 .WillOnce(_getProps);
502 EXPECT_CALL(helper, thresholdsAsserted(_, StrEq("asdf"), StrEq(path)))
503 .WillOnce(Return(false));
504
505 auto info = conf::SensorConfig();
506 info.ignoreDbusMinMax = true;
507 ri = DbusPassive::createDbusPassive(bus_mock, type, id, &helper, &info,
508 nullptr);
509 passive = reinterpret_cast<DbusPassive*>(ri.get());
510 EXPECT_FALSE(passive == nullptr);
511 }
512
513 sdbusplus::SdBusMock sdbus_mock;
514 sdbusplus::bus::bus bus_mock;
515 DbusHelperMock helper;
516 std::string type = "temp";
517 std::string id = "id";
518 std::string path = "/xyz/openbmc_project/sensors/temperature/id";
519 int64_t _scale = -3;
520 int64_t _value = 10;
521
522 std::unique_ptr<ReadInterface> ri;
523 DbusPassive* passive;
524 GetPropertiesFunction _getProps = &GetPropertiesMax3k;
525};
526
527TEST_F(DbusPassiveTest3kMaxIgnoredObj, ReadMinAndMaxReturnsExpected)
528{
529 EXPECT_DOUBLE_EQ(0, passive->getMin());
530 EXPECT_DOUBLE_EQ(0, passive->getMax());
531}