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 | |
Andrew Jeffery | 76544ab | 2023-02-06 18:11:30 +1030 | [diff] [blame] | 10 | constexpr auto ledObj = "/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; |
George Liu | 45eba6f | 2021-05-18 14:32:20 +0800 | [diff] [blame] | 13 | namespace fs = std::filesystem; |
Andrew Jeffery | a607a0d | 2018-05-23 16:15:26 +0930 | [diff] [blame] | 14 | |
Andrew Jeffery | feb990c | 2023-02-06 18:22:23 +1030 | [diff] [blame] | 15 | fs::path createSandbox() |
Andrew Jeffery | 280afaf | 2018-05-24 16:15:46 +0930 | [diff] [blame] | 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"; |
Andrew Jeffery | 1978ed0 | 2023-02-06 18:24:55 +1030 | [diff] [blame] | 23 | std::array<char, MAXPATHLEN> buffer = {0}; |
Andrew Jeffery | 280afaf | 2018-05-24 16:15:46 +0930 | [diff] [blame] | 24 | |
Andrew Jeffery | 1978ed0 | 2023-02-06 18:24:55 +1030 | [diff] [blame] | 25 | strncpy(buffer.data(), tmplt, buffer.size() - 1); |
| 26 | auto* dir = mkdtemp(buffer.data()); |
| 27 | if (dir == nullptr) |
Andrew Jeffery | 280afaf | 2018-05-24 16:15:46 +0930 | [diff] [blame] | 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 | |
Andrew Jeffery | 1caab51 | 2023-02-06 18:28:50 +1030 | [diff] [blame] | 41 | return dir; |
Andrew Jeffery | 280afaf | 2018-05-24 16:15:46 +0930 | [diff] [blame] | 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 */ |
Patrick Williams | 1c4503e | 2023-05-10 07:50:54 -0500 | [diff] [blame] | 48 | MockLed() : SysfsLed(createSandbox()) {} |
Andrew Jeffery | c813e3b | 2023-02-06 18:44:08 +1030 | [diff] [blame] | 49 | MockLed(MockLed& other) = delete; |
| 50 | MockLed(MockLed&& other) = delete; |
| 51 | MockLed& operator=(MockLed& other) = delete; |
| 52 | MockLed&& operator=(MockLed&& other) = delete; |
Andrew Jeffery | 280afaf | 2018-05-24 16:15:46 +0930 | [diff] [blame] | 53 | |
Andrew Jeffery | 71246ca | 2023-02-06 18:45:19 +1030 | [diff] [blame] | 54 | ~MockLed() override |
Andrew Jeffery | 280afaf | 2018-05-24 16:15:46 +0930 | [diff] [blame] | 55 | { |
| 56 | chmod(root.c_str(), S_IRUSR | S_IWUSR | S_IXUSR); |
| 57 | fs::remove_all(root); |
| 58 | } |
| 59 | |
| 60 | MOCK_METHOD0(getBrightness, unsigned long()); |
| 61 | MOCK_METHOD1(setBrightness, void(unsigned long value)); |
| 62 | MOCK_METHOD0(getMaxBrightness, unsigned long()); |
| 63 | MOCK_METHOD0(getTrigger, std::string()); |
| 64 | MOCK_METHOD1(setTrigger, void(const std::string& trigger)); |
| 65 | MOCK_METHOD0(getDelayOn, unsigned long()); |
| 66 | MOCK_METHOD1(setDelayOn, void(unsigned long ms)); |
| 67 | MOCK_METHOD0(getDelayOff, unsigned long()); |
| 68 | MOCK_METHOD1(setDelayOff, void(unsigned long ms)); |
| 69 | }; |
| 70 | |
Andrew Jeffery | aee9c2c | 2018-05-25 14:05:40 +0930 | [diff] [blame] | 71 | using ::testing::InSequence; |
Andrew Jeffery | 280afaf | 2018-05-24 16:15:46 +0930 | [diff] [blame] | 72 | using ::testing::NiceMock; |
| 73 | using ::testing::Return; |
Andrew Jeffery | 280afaf | 2018-05-24 16:15:46 +0930 | [diff] [blame] | 74 | |
Andrew Jeffery | 5669270 | 2018-05-25 11:41:43 +0930 | [diff] [blame] | 75 | TEST(Physical, ctor_none_trigger) |
Andrew Jeffery | a607a0d | 2018-05-23 16:15:26 +0930 | [diff] [blame] | 76 | { |
Patrick Williams | ff3d538 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 77 | sdbusplus::bus_t bus = sdbusplus::bus::new_default(); |
Andrew Jeffery | 280afaf | 2018-05-24 16:15:46 +0930 | [diff] [blame] | 78 | /* NiceMock ignores calls to methods with no expectations defined */ |
| 79 | NiceMock<MockLed> led; |
| 80 | ON_CALL(led, getTrigger()).WillByDefault(Return("none")); |
Andrew Jeffery | 76544ab | 2023-02-06 18:11:30 +1030 | [diff] [blame] | 81 | phosphor::led::Physical phy(bus, ledObj, led); |
Vishwanatha Subbanna | db21bc0 | 2021-03-26 00:32:46 -0500 | [diff] [blame] | 82 | EXPECT_EQ(phy.state(), Action::Off); |
Andrew Jeffery | a607a0d | 2018-05-23 16:15:26 +0930 | [diff] [blame] | 83 | } |
| 84 | |
Vishwanatha Subbanna | a8a275f | 2021-03-26 00:33:34 -0500 | [diff] [blame] | 85 | TEST(Physical, ctor_maxbrightness_and_brightness_read_127) |
| 86 | { |
Patrick Williams | ff3d538 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 87 | sdbusplus::bus_t bus = sdbusplus::bus::new_default(); |
Vishwanatha Subbanna | a8a275f | 2021-03-26 00:33:34 -0500 | [diff] [blame] | 88 | /* NiceMock ignores calls to methods with no expectations defined */ |
| 89 | NiceMock<MockLed> led; |
| 90 | EXPECT_CALL(led, getTrigger()).WillRepeatedly(Return("none")); |
| 91 | EXPECT_CALL(led, getBrightness()).WillOnce(Return(127)); |
| 92 | EXPECT_CALL(led, getMaxBrightness()).WillOnce(Return(127)); |
Andrew Jeffery | 76544ab | 2023-02-06 18:11:30 +1030 | [diff] [blame] | 93 | phosphor::led::Physical phy(bus, ledObj, led); |
Vishwanatha Subbanna | a8a275f | 2021-03-26 00:33:34 -0500 | [diff] [blame] | 94 | EXPECT_EQ(phy.state(), Action::On); |
| 95 | } |
| 96 | |
| 97 | TEST(Physical, ctor_maxbrightness_and_brightness_read_0) |
| 98 | { |
Patrick Williams | ff3d538 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 99 | sdbusplus::bus_t bus = sdbusplus::bus::new_default(); |
Vishwanatha Subbanna | a8a275f | 2021-03-26 00:33:34 -0500 | [diff] [blame] | 100 | /* NiceMock ignores calls to methods with no expectations defined */ |
| 101 | NiceMock<MockLed> led; |
| 102 | EXPECT_CALL(led, getTrigger()).WillRepeatedly(Return("none")); |
| 103 | EXPECT_CALL(led, getBrightness()).WillOnce(Return(0)); |
| 104 | EXPECT_CALL(led, getMaxBrightness()).WillOnce(Return(0)); |
Andrew Jeffery | 76544ab | 2023-02-06 18:11:30 +1030 | [diff] [blame] | 105 | phosphor::led::Physical phy(bus, ledObj, led); |
Vishwanatha Subbanna | a8a275f | 2021-03-26 00:33:34 -0500 | [diff] [blame] | 106 | EXPECT_EQ(phy.state(), Action::Off); |
| 107 | } |
| 108 | |
| 109 | TEST(Physical, ctor_only_maxbrightness_read_127) |
| 110 | { |
Patrick Williams | ff3d538 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 111 | sdbusplus::bus_t bus = sdbusplus::bus::new_default(); |
Vishwanatha Subbanna | a8a275f | 2021-03-26 00:33:34 -0500 | [diff] [blame] | 112 | /* NiceMock ignores calls to methods with no expectations defined */ |
| 113 | NiceMock<MockLed> led; |
| 114 | EXPECT_CALL(led, getTrigger()).WillRepeatedly(Return("none")); |
| 115 | EXPECT_CALL(led, getBrightness()).WillOnce(Return(0)); |
| 116 | EXPECT_CALL(led, getMaxBrightness()).WillOnce(Return(127)); |
Andrew Jeffery | 76544ab | 2023-02-06 18:11:30 +1030 | [diff] [blame] | 117 | phosphor::led::Physical phy(bus, ledObj, led); |
Vishwanatha Subbanna | a8a275f | 2021-03-26 00:33:34 -0500 | [diff] [blame] | 118 | EXPECT_EQ(phy.state(), Action::Off); |
| 119 | } |
| 120 | |
| 121 | TEST(Physical, ctor_only_brightness_read_127) |
| 122 | { |
Patrick Williams | ff3d538 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 123 | sdbusplus::bus_t bus = sdbusplus::bus::new_default(); |
Vishwanatha Subbanna | a8a275f | 2021-03-26 00:33:34 -0500 | [diff] [blame] | 124 | /* NiceMock ignores calls to methods with no expectations defined */ |
| 125 | NiceMock<MockLed> led; |
| 126 | EXPECT_CALL(led, getTrigger()).WillRepeatedly(Return("none")); |
| 127 | EXPECT_CALL(led, getBrightness()).WillOnce(Return(127)); |
| 128 | EXPECT_CALL(led, getMaxBrightness()).WillOnce(Return(0)); |
Andrew Jeffery | 76544ab | 2023-02-06 18:11:30 +1030 | [diff] [blame] | 129 | phosphor::led::Physical phy(bus, ledObj, led); |
Vishwanatha Subbanna | a8a275f | 2021-03-26 00:33:34 -0500 | [diff] [blame] | 130 | EXPECT_EQ(phy.state(), Action::Off); |
| 131 | } |
| 132 | |
Andrew Jeffery | 5669270 | 2018-05-25 11:41:43 +0930 | [diff] [blame] | 133 | TEST(Physical, ctor_timer_trigger) |
| 134 | { |
Patrick Williams | ff3d538 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 135 | sdbusplus::bus_t bus = sdbusplus::bus::new_default(); |
Andrew Jeffery | 5669270 | 2018-05-25 11:41:43 +0930 | [diff] [blame] | 136 | NiceMock<MockLed> led; |
| 137 | EXPECT_CALL(led, getTrigger()).WillOnce(Return("timer")); |
| 138 | EXPECT_CALL(led, getDelayOn()).WillOnce(Return(500)); |
| 139 | EXPECT_CALL(led, getDelayOff()).WillOnce(Return(500)); |
Andrew Jeffery | 76544ab | 2023-02-06 18:11:30 +1030 | [diff] [blame] | 140 | phosphor::led::Physical phy(bus, ledObj, led); |
George Liu | 001e2a3 | 2023-12-01 17:30:30 +0800 | [diff] [blame] | 141 | EXPECT_EQ(phy.state(), Action::Blink); |
Andrew Jeffery | 5669270 | 2018-05-25 11:41:43 +0930 | [diff] [blame] | 142 | } |
| 143 | |
Andrew Jeffery | a607a0d | 2018-05-23 16:15:26 +0930 | [diff] [blame] | 144 | TEST(Physical, off) |
| 145 | { |
Patrick Williams | ff3d538 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 146 | sdbusplus::bus_t bus = sdbusplus::bus::new_default(); |
Andrew Jeffery | 280afaf | 2018-05-24 16:15:46 +0930 | [diff] [blame] | 147 | NiceMock<MockLed> led; |
Andrew Jeffery | 30726a0 | 2018-05-25 15:03:04 +0930 | [diff] [blame] | 148 | ON_CALL(led, getMaxBrightness()).WillByDefault(Return(127)); |
| 149 | EXPECT_CALL(led, getTrigger()).WillOnce(Return("none")); |
Andrew Jeffery | 8e85228 | 2023-02-06 19:29:43 +1030 | [diff] [blame] | 150 | EXPECT_CALL(led, getBrightness()) |
| 151 | .WillOnce(Return(phosphor::led::deasserted)); |
| 152 | EXPECT_CALL(led, setBrightness(phosphor::led::deasserted)).Times(0); |
Andrew Jeffery | 76544ab | 2023-02-06 18:11:30 +1030 | [diff] [blame] | 153 | phosphor::led::Physical phy(bus, ledObj, led); |
Andrew Jeffery | 42e02d3 | 2018-05-24 13:34:05 +0930 | [diff] [blame] | 154 | phy.state(Action::Off); |
Vishwanatha Subbanna | db21bc0 | 2021-03-26 00:32:46 -0500 | [diff] [blame] | 155 | EXPECT_EQ(phy.state(), Action::Off); |
Andrew Jeffery | a607a0d | 2018-05-23 16:15:26 +0930 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | TEST(Physical, on) |
| 159 | { |
Andrew Jeffery | 5b1417b | 2019-03-18 17:20:37 +1030 | [diff] [blame] | 160 | constexpr unsigned long asserted = 127; |
| 161 | |
Patrick Williams | ff3d538 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 162 | sdbusplus::bus_t bus = sdbusplus::bus::new_default(); |
Andrew Jeffery | 280afaf | 2018-05-24 16:15:46 +0930 | [diff] [blame] | 163 | NiceMock<MockLed> led; |
Andrew Jeffery | 5b1417b | 2019-03-18 17:20:37 +1030 | [diff] [blame] | 164 | ON_CALL(led, getMaxBrightness()).WillByDefault(Return(asserted)); |
Andrew Jeffery | 30726a0 | 2018-05-25 15:03:04 +0930 | [diff] [blame] | 165 | EXPECT_CALL(led, getTrigger()).WillOnce(Return("none")); |
| 166 | EXPECT_CALL(led, setTrigger("none")); |
Andrew Jeffery | 5b1417b | 2019-03-18 17:20:37 +1030 | [diff] [blame] | 167 | EXPECT_CALL(led, setBrightness(asserted)); |
Andrew Jeffery | 76544ab | 2023-02-06 18:11:30 +1030 | [diff] [blame] | 168 | phosphor::led::Physical phy(bus, ledObj, led); |
Andrew Jeffery | 42e02d3 | 2018-05-24 13:34:05 +0930 | [diff] [blame] | 169 | phy.state(Action::On); |
Vishwanatha Subbanna | db21bc0 | 2021-03-26 00:32:46 -0500 | [diff] [blame] | 170 | EXPECT_EQ(phy.state(), Action::On); |
Andrew Jeffery | a607a0d | 2018-05-23 16:15:26 +0930 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | TEST(Physical, blink) |
| 174 | { |
Patrick Williams | ff3d538 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 175 | sdbusplus::bus_t bus = sdbusplus::bus::new_default(); |
Andrew Jeffery | 280afaf | 2018-05-24 16:15:46 +0930 | [diff] [blame] | 176 | NiceMock<MockLed> led; |
Andrew Jeffery | 861e562 | 2018-05-25 14:51:45 +0930 | [diff] [blame] | 177 | EXPECT_CALL(led, getTrigger()).WillOnce(Return("none")); |
| 178 | EXPECT_CALL(led, setTrigger("timer")); |
| 179 | EXPECT_CALL(led, setDelayOn(500)); |
| 180 | EXPECT_CALL(led, setDelayOff(500)); |
Andrew Jeffery | 76544ab | 2023-02-06 18:11:30 +1030 | [diff] [blame] | 181 | phosphor::led::Physical phy(bus, ledObj, led); |
Andrew Jeffery | 42e02d3 | 2018-05-24 13:34:05 +0930 | [diff] [blame] | 182 | phy.state(Action::Blink); |
Vishwanatha Subbanna | db21bc0 | 2021-03-26 00:32:46 -0500 | [diff] [blame] | 183 | EXPECT_EQ(phy.state(), Action::Blink); |
Andrew Jeffery | a607a0d | 2018-05-23 16:15:26 +0930 | [diff] [blame] | 184 | } |
Andrew Jeffery | 04275e0 | 2018-05-25 13:04:39 +0930 | [diff] [blame] | 185 | |
| 186 | TEST(Physical, ctor_none_trigger_asserted_brightness) |
| 187 | { |
Patrick Williams | ff3d538 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 188 | sdbusplus::bus_t bus = sdbusplus::bus::new_default(); |
Andrew Jeffery | 04275e0 | 2018-05-25 13:04:39 +0930 | [diff] [blame] | 189 | NiceMock<MockLed> led; |
| 190 | EXPECT_CALL(led, getTrigger()).WillRepeatedly(Return("none")); |
Andrew Jeffery | 5b1417b | 2019-03-18 17:20:37 +1030 | [diff] [blame] | 191 | EXPECT_CALL(led, getBrightness()).WillRepeatedly(Return(127)); |
Andrew Jeffery | 76544ab | 2023-02-06 18:11:30 +1030 | [diff] [blame] | 192 | phosphor::led::Physical phy(bus, ledObj, led); |
Vishwanatha Subbanna | db21bc0 | 2021-03-26 00:32:46 -0500 | [diff] [blame] | 193 | EXPECT_EQ(phy.state(), Action::Off); |
Andrew Jeffery | 04275e0 | 2018-05-25 13:04:39 +0930 | [diff] [blame] | 194 | } |
Andrew Jeffery | aee9c2c | 2018-05-25 14:05:40 +0930 | [diff] [blame] | 195 | |
| 196 | TEST(Physical, on_to_off) |
| 197 | { |
| 198 | InSequence s; |
Andrew Jeffery | 5b1417b | 2019-03-18 17:20:37 +1030 | [diff] [blame] | 199 | constexpr unsigned long asserted = 127; |
Andrew Jeffery | aee9c2c | 2018-05-25 14:05:40 +0930 | [diff] [blame] | 200 | |
| 201 | auto bus = sdbusplus::bus::new_default(); |
| 202 | NiceMock<MockLed> led; |
Andrew Jeffery | 5b1417b | 2019-03-18 17:20:37 +1030 | [diff] [blame] | 203 | ON_CALL(led, getMaxBrightness()).WillByDefault(Return(asserted)); |
Andrew Jeffery | aee9c2c | 2018-05-25 14:05:40 +0930 | [diff] [blame] | 204 | EXPECT_CALL(led, getTrigger()).Times(1).WillOnce(Return("none")); |
Andrew Jeffery | 8e85228 | 2023-02-06 19:29:43 +1030 | [diff] [blame] | 205 | constexpr auto deasserted = phosphor::led::deasserted; |
Andrew Jeffery | aee9c2c | 2018-05-25 14:05:40 +0930 | [diff] [blame] | 206 | EXPECT_CALL(led, getBrightness()).WillOnce(Return(deasserted)); |
Andrew Jeffery | aee9c2c | 2018-05-25 14:05:40 +0930 | [diff] [blame] | 207 | EXPECT_CALL(led, setBrightness(asserted)); |
| 208 | EXPECT_CALL(led, setBrightness(deasserted)); |
Andrew Jeffery | 76544ab | 2023-02-06 18:11:30 +1030 | [diff] [blame] | 209 | phosphor::led::Physical phy(bus, ledObj, led); |
Andrew Jeffery | aee9c2c | 2018-05-25 14:05:40 +0930 | [diff] [blame] | 210 | phy.state(Action::On); |
Vishwanatha Subbanna | db21bc0 | 2021-03-26 00:32:46 -0500 | [diff] [blame] | 211 | EXPECT_EQ(phy.state(), Action::On); |
Andrew Jeffery | aee9c2c | 2018-05-25 14:05:40 +0930 | [diff] [blame] | 212 | phy.state(Action::Off); |
Vishwanatha Subbanna | db21bc0 | 2021-03-26 00:32:46 -0500 | [diff] [blame] | 213 | EXPECT_EQ(phy.state(), Action::Off); |
Andrew Jeffery | aee9c2c | 2018-05-25 14:05:40 +0930 | [diff] [blame] | 214 | } |