Andrew Jeffery | a607a0d | 2018-05-23 16:15:26 +0930 | [diff] [blame] | 1 | #include "physical.hpp" |
| 2 | |
Andrew Jeffery | 280afaf | 2018-05-24 16:15:46 +0930 | [diff] [blame] | 3 | #include <sys/param.h> |
| 4 | |
Andrew Jeffery | a607a0d | 2018-05-23 16:15:26 +0930 | [diff] [blame] | 5 | #include <sdbusplus/bus.hpp> |
| 6 | |
Andrew Jeffery | 280afaf | 2018-05-24 16:15:46 +0930 | [diff] [blame] | 7 | #include <gmock/gmock.h> |
Andrew Jeffery | a607a0d | 2018-05-23 16:15:26 +0930 | [diff] [blame] | 8 | #include <gtest/gtest.h> |
| 9 | |
| 10 | constexpr auto LED_OBJ = "/foo/bar/led"; |
Andrew Jeffery | a607a0d | 2018-05-23 16:15:26 +0930 | [diff] [blame] | 11 | |
| 12 | using Action = sdbusplus::xyz::openbmc_project::Led::server::Physical::Action; |
Andrew Jeffery | 42e02d3 | 2018-05-24 13:34:05 +0930 | [diff] [blame] | 13 | namespace fs = std::experimental::filesystem; |
Andrew Jeffery | a607a0d | 2018-05-23 16:15:26 +0930 | [diff] [blame] | 14 | |
Andrew Jeffery | 280afaf | 2018-05-24 16:15:46 +0930 | [diff] [blame] | 15 | fs::path create_sandbox() |
| 16 | { |
| 17 | /* If your tests need to touch the filesystem, always use mkdtemp() or |
| 18 | * mkstemp() for creating directories and files. Tests can be run in |
| 19 | * parallel with `make -j`, and if use the same path in multiple tests they |
| 20 | * will stomp on eachother and likely fail. |
| 21 | */ |
| 22 | static constexpr auto tmplt = "/tmp/MockLed.XXXXXX"; |
| 23 | char buffer[MAXPATHLEN] = {0}; |
| 24 | |
| 25 | strncpy(buffer, tmplt, sizeof(buffer) - 1); |
| 26 | auto dir = mkdtemp(buffer); |
| 27 | if (!dir) |
| 28 | { |
| 29 | throw std::system_error(errno, std::system_category()); |
| 30 | } |
| 31 | |
| 32 | /* We want to limit behaviours to mocks, and if methods aren't mocked they |
| 33 | * may fall back to their base class implementation. Stop read/write to |
| 34 | * directory to prevent streams from creating files. |
| 35 | */ |
| 36 | if (chmod(dir, S_IXUSR | S_IXGRP) == -1) |
| 37 | { |
| 38 | throw std::system_error(errno, std::system_category()); |
| 39 | } |
| 40 | |
| 41 | return fs::path(dir); |
| 42 | } |
| 43 | |
| 44 | class MockLed : public phosphor::led::SysfsLed |
| 45 | { |
| 46 | public: |
| 47 | /* Use a no-args ctor here to avoid headaches with {Nice,Strict}Mock */ |
| 48 | MockLed() : SysfsLed(create_sandbox()) |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | virtual ~MockLed() |
| 53 | { |
| 54 | chmod(root.c_str(), S_IRUSR | S_IWUSR | S_IXUSR); |
| 55 | fs::remove_all(root); |
| 56 | } |
| 57 | |
| 58 | MOCK_METHOD0(getBrightness, unsigned long()); |
| 59 | MOCK_METHOD1(setBrightness, void(unsigned long value)); |
| 60 | MOCK_METHOD0(getMaxBrightness, unsigned long()); |
| 61 | MOCK_METHOD0(getTrigger, std::string()); |
| 62 | MOCK_METHOD1(setTrigger, void(const std::string& trigger)); |
| 63 | MOCK_METHOD0(getDelayOn, unsigned long()); |
| 64 | MOCK_METHOD1(setDelayOn, void(unsigned long ms)); |
| 65 | MOCK_METHOD0(getDelayOff, unsigned long()); |
| 66 | MOCK_METHOD1(setDelayOff, void(unsigned long ms)); |
| 67 | }; |
| 68 | |
Andrew Jeffery | aee9c2c | 2018-05-25 14:05:40 +0930 | [diff] [blame] | 69 | using ::testing::InSequence; |
Andrew Jeffery | 280afaf | 2018-05-24 16:15:46 +0930 | [diff] [blame] | 70 | using ::testing::NiceMock; |
| 71 | using ::testing::Return; |
| 72 | using ::testing::Throw; |
| 73 | |
Andrew Jeffery | 5669270 | 2018-05-25 11:41:43 +0930 | [diff] [blame] | 74 | TEST(Physical, ctor_none_trigger) |
Andrew Jeffery | a607a0d | 2018-05-23 16:15:26 +0930 | [diff] [blame] | 75 | { |
| 76 | sdbusplus::bus::bus bus = sdbusplus::bus::new_default(); |
Andrew Jeffery | 280afaf | 2018-05-24 16:15:46 +0930 | [diff] [blame] | 77 | /* NiceMock ignores calls to methods with no expectations defined */ |
| 78 | NiceMock<MockLed> led; |
| 79 | ON_CALL(led, getTrigger()).WillByDefault(Return("none")); |
Andrew Jeffery | 42e02d3 | 2018-05-24 13:34:05 +0930 | [diff] [blame] | 80 | phosphor::led::Physical phy(bus, LED_OBJ, led); |
Andrew Jeffery | a607a0d | 2018-05-23 16:15:26 +0930 | [diff] [blame] | 81 | } |
| 82 | |
Andrew Jeffery | 5669270 | 2018-05-25 11:41:43 +0930 | [diff] [blame] | 83 | TEST(Physical, ctor_timer_trigger) |
| 84 | { |
| 85 | sdbusplus::bus::bus bus = sdbusplus::bus::new_default(); |
| 86 | NiceMock<MockLed> led; |
| 87 | EXPECT_CALL(led, getTrigger()).WillOnce(Return("timer")); |
| 88 | EXPECT_CALL(led, getDelayOn()).WillOnce(Return(500)); |
| 89 | EXPECT_CALL(led, getDelayOff()).WillOnce(Return(500)); |
| 90 | phosphor::led::Physical phy(bus, LED_OBJ, led); |
| 91 | } |
| 92 | |
Andrew Jeffery | a607a0d | 2018-05-23 16:15:26 +0930 | [diff] [blame] | 93 | TEST(Physical, off) |
| 94 | { |
| 95 | sdbusplus::bus::bus bus = sdbusplus::bus::new_default(); |
Andrew Jeffery | 280afaf | 2018-05-24 16:15:46 +0930 | [diff] [blame] | 96 | NiceMock<MockLed> led; |
Andrew Jeffery | 30726a0 | 2018-05-25 15:03:04 +0930 | [diff] [blame] | 97 | ON_CALL(led, getMaxBrightness()).WillByDefault(Return(127)); |
| 98 | EXPECT_CALL(led, getTrigger()).WillOnce(Return("none")); |
| 99 | EXPECT_CALL(led, getBrightness()).WillOnce(Return(phosphor::led::DEASSERT)); |
| 100 | EXPECT_CALL(led, setBrightness(phosphor::led::DEASSERT)).Times(0); |
Andrew Jeffery | 42e02d3 | 2018-05-24 13:34:05 +0930 | [diff] [blame] | 101 | phosphor::led::Physical phy(bus, LED_OBJ, led); |
| 102 | phy.state(Action::Off); |
Andrew Jeffery | a607a0d | 2018-05-23 16:15:26 +0930 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | TEST(Physical, on) |
| 106 | { |
Andrew Jeffery | 5b1417b | 2019-03-18 17:20:37 +1030 | [diff] [blame^] | 107 | constexpr unsigned long asserted = 127; |
| 108 | |
Andrew Jeffery | a607a0d | 2018-05-23 16:15:26 +0930 | [diff] [blame] | 109 | sdbusplus::bus::bus bus = sdbusplus::bus::new_default(); |
Andrew Jeffery | 280afaf | 2018-05-24 16:15:46 +0930 | [diff] [blame] | 110 | NiceMock<MockLed> led; |
Andrew Jeffery | 5b1417b | 2019-03-18 17:20:37 +1030 | [diff] [blame^] | 111 | ON_CALL(led, getMaxBrightness()).WillByDefault(Return(asserted)); |
Andrew Jeffery | 30726a0 | 2018-05-25 15:03:04 +0930 | [diff] [blame] | 112 | EXPECT_CALL(led, getTrigger()).WillOnce(Return("none")); |
| 113 | EXPECT_CALL(led, setTrigger("none")); |
Andrew Jeffery | 5b1417b | 2019-03-18 17:20:37 +1030 | [diff] [blame^] | 114 | EXPECT_CALL(led, setBrightness(asserted)); |
Andrew Jeffery | 42e02d3 | 2018-05-24 13:34:05 +0930 | [diff] [blame] | 115 | phosphor::led::Physical phy(bus, LED_OBJ, led); |
| 116 | phy.state(Action::On); |
Andrew Jeffery | a607a0d | 2018-05-23 16:15:26 +0930 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | TEST(Physical, blink) |
| 120 | { |
| 121 | sdbusplus::bus::bus bus = sdbusplus::bus::new_default(); |
Andrew Jeffery | 280afaf | 2018-05-24 16:15:46 +0930 | [diff] [blame] | 122 | NiceMock<MockLed> led; |
Andrew Jeffery | 861e562 | 2018-05-25 14:51:45 +0930 | [diff] [blame] | 123 | EXPECT_CALL(led, getTrigger()).WillOnce(Return("none")); |
| 124 | EXPECT_CALL(led, setTrigger("timer")); |
| 125 | EXPECT_CALL(led, setDelayOn(500)); |
| 126 | EXPECT_CALL(led, setDelayOff(500)); |
Andrew Jeffery | 42e02d3 | 2018-05-24 13:34:05 +0930 | [diff] [blame] | 127 | phosphor::led::Physical phy(bus, LED_OBJ, led); |
| 128 | phy.state(Action::Blink); |
Andrew Jeffery | a607a0d | 2018-05-23 16:15:26 +0930 | [diff] [blame] | 129 | } |
Andrew Jeffery | 04275e0 | 2018-05-25 13:04:39 +0930 | [diff] [blame] | 130 | |
| 131 | TEST(Physical, ctor_none_trigger_asserted_brightness) |
| 132 | { |
| 133 | sdbusplus::bus::bus bus = sdbusplus::bus::new_default(); |
| 134 | NiceMock<MockLed> led; |
| 135 | EXPECT_CALL(led, getTrigger()).WillRepeatedly(Return("none")); |
Andrew Jeffery | 5b1417b | 2019-03-18 17:20:37 +1030 | [diff] [blame^] | 136 | EXPECT_CALL(led, getBrightness()).WillRepeatedly(Return(127)); |
Andrew Jeffery | 04275e0 | 2018-05-25 13:04:39 +0930 | [diff] [blame] | 137 | phosphor::led::Physical phy(bus, LED_OBJ, led); |
| 138 | } |
Andrew Jeffery | aee9c2c | 2018-05-25 14:05:40 +0930 | [diff] [blame] | 139 | |
| 140 | TEST(Physical, on_to_off) |
| 141 | { |
| 142 | InSequence s; |
Andrew Jeffery | 5b1417b | 2019-03-18 17:20:37 +1030 | [diff] [blame^] | 143 | constexpr unsigned long asserted = 127; |
Andrew Jeffery | aee9c2c | 2018-05-25 14:05:40 +0930 | [diff] [blame] | 144 | |
| 145 | auto bus = sdbusplus::bus::new_default(); |
| 146 | NiceMock<MockLed> led; |
Andrew Jeffery | 5b1417b | 2019-03-18 17:20:37 +1030 | [diff] [blame^] | 147 | ON_CALL(led, getMaxBrightness()).WillByDefault(Return(asserted)); |
Andrew Jeffery | aee9c2c | 2018-05-25 14:05:40 +0930 | [diff] [blame] | 148 | EXPECT_CALL(led, getTrigger()).Times(1).WillOnce(Return("none")); |
| 149 | constexpr auto deasserted = phosphor::led::DEASSERT; |
| 150 | EXPECT_CALL(led, getBrightness()).WillOnce(Return(deasserted)); |
Andrew Jeffery | aee9c2c | 2018-05-25 14:05:40 +0930 | [diff] [blame] | 151 | EXPECT_CALL(led, setBrightness(asserted)); |
| 152 | EXPECT_CALL(led, setBrightness(deasserted)); |
| 153 | phosphor::led::Physical phy(bus, LED_OBJ, led); |
| 154 | phy.state(Action::On); |
| 155 | phy.state(Action::Off); |
| 156 | } |