blob: b6fb35b26eeb52be18bfd01317787be8f9bd0d93 [file] [log] [blame]
Andrew Jefferya607a0d2018-05-23 16:15:26 +09301#include "physical.hpp"
2
Andrew Jeffery280afaf2018-05-24 16:15:46 +09303#include <sys/param.h>
4
Andrew Jefferya607a0d2018-05-23 16:15:26 +09305#include <sdbusplus/bus.hpp>
6
Andrew Jeffery280afaf2018-05-24 16:15:46 +09307#include <gmock/gmock.h>
Andrew Jefferya607a0d2018-05-23 16:15:26 +09308#include <gtest/gtest.h>
9
Andrew Jeffery76544ab2023-02-06 18:11:30 +103010constexpr auto ledObj = "/foo/bar/led";
Andrew Jefferya607a0d2018-05-23 16:15:26 +093011
12using Action = sdbusplus::xyz::openbmc_project::Led::server::Physical::Action;
George Liu45eba6f2021-05-18 14:32:20 +080013namespace fs = std::filesystem;
Andrew Jefferya607a0d2018-05-23 16:15:26 +093014
Andrew Jefferyfeb990c2023-02-06 18:22:23 +103015fs::path createSandbox()
Andrew Jeffery280afaf2018-05-24 16:15:46 +093016{
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 Jeffery1978ed02023-02-06 18:24:55 +103023 std::array<char, MAXPATHLEN> buffer = {0};
Andrew Jeffery280afaf2018-05-24 16:15:46 +093024
Andrew Jeffery1978ed02023-02-06 18:24:55 +103025 strncpy(buffer.data(), tmplt, buffer.size() - 1);
26 auto* dir = mkdtemp(buffer.data());
27 if (dir == nullptr)
Andrew Jeffery280afaf2018-05-24 16:15:46 +093028 {
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 Jeffery1caab512023-02-06 18:28:50 +103041 return dir;
Andrew Jeffery280afaf2018-05-24 16:15:46 +093042}
43
44class MockLed : public phosphor::led::SysfsLed
45{
46 public:
47 /* Use a no-args ctor here to avoid headaches with {Nice,Strict}Mock */
Patrick Williams1c4503e2023-05-10 07:50:54 -050048 MockLed() : SysfsLed(createSandbox()) {}
Andrew Jefferyc813e3b2023-02-06 18:44:08 +103049 MockLed(MockLed& other) = delete;
50 MockLed(MockLed&& other) = delete;
51 MockLed& operator=(MockLed& other) = delete;
52 MockLed&& operator=(MockLed&& other) = delete;
Andrew Jeffery280afaf2018-05-24 16:15:46 +093053
Andrew Jeffery71246ca2023-02-06 18:45:19 +103054 ~MockLed() override
Andrew Jeffery280afaf2018-05-24 16:15:46 +093055 {
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 Jefferyaee9c2c2018-05-25 14:05:40 +093071using ::testing::InSequence;
Andrew Jeffery280afaf2018-05-24 16:15:46 +093072using ::testing::NiceMock;
73using ::testing::Return;
Andrew Jeffery280afaf2018-05-24 16:15:46 +093074
Andrew Jeffery56692702018-05-25 11:41:43 +093075TEST(Physical, ctor_none_trigger)
Andrew Jefferya607a0d2018-05-23 16:15:26 +093076{
Patrick Williamsff3d5382022-07-22 19:26:55 -050077 sdbusplus::bus_t bus = sdbusplus::bus::new_default();
Andrew Jeffery280afaf2018-05-24 16:15:46 +093078 /* NiceMock ignores calls to methods with no expectations defined */
79 NiceMock<MockLed> led;
80 ON_CALL(led, getTrigger()).WillByDefault(Return("none"));
Andrew Jeffery76544ab2023-02-06 18:11:30 +103081 phosphor::led::Physical phy(bus, ledObj, led);
Vishwanatha Subbannadb21bc02021-03-26 00:32:46 -050082 EXPECT_EQ(phy.state(), Action::Off);
Andrew Jefferya607a0d2018-05-23 16:15:26 +093083}
84
Vishwanatha Subbannaa8a275f2021-03-26 00:33:34 -050085TEST(Physical, ctor_maxbrightness_and_brightness_read_127)
86{
Patrick Williamsff3d5382022-07-22 19:26:55 -050087 sdbusplus::bus_t bus = sdbusplus::bus::new_default();
Vishwanatha Subbannaa8a275f2021-03-26 00:33:34 -050088 /* 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 Jeffery76544ab2023-02-06 18:11:30 +103093 phosphor::led::Physical phy(bus, ledObj, led);
Vishwanatha Subbannaa8a275f2021-03-26 00:33:34 -050094 EXPECT_EQ(phy.state(), Action::On);
95}
96
97TEST(Physical, ctor_maxbrightness_and_brightness_read_0)
98{
Patrick Williamsff3d5382022-07-22 19:26:55 -050099 sdbusplus::bus_t bus = sdbusplus::bus::new_default();
Vishwanatha Subbannaa8a275f2021-03-26 00:33:34 -0500100 /* 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 Jeffery76544ab2023-02-06 18:11:30 +1030105 phosphor::led::Physical phy(bus, ledObj, led);
Vishwanatha Subbannaa8a275f2021-03-26 00:33:34 -0500106 EXPECT_EQ(phy.state(), Action::Off);
107}
108
109TEST(Physical, ctor_only_maxbrightness_read_127)
110{
Patrick Williamsff3d5382022-07-22 19:26:55 -0500111 sdbusplus::bus_t bus = sdbusplus::bus::new_default();
Vishwanatha Subbannaa8a275f2021-03-26 00:33:34 -0500112 /* 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 Jeffery76544ab2023-02-06 18:11:30 +1030117 phosphor::led::Physical phy(bus, ledObj, led);
Vishwanatha Subbannaa8a275f2021-03-26 00:33:34 -0500118 EXPECT_EQ(phy.state(), Action::Off);
119}
120
121TEST(Physical, ctor_only_brightness_read_127)
122{
Patrick Williamsff3d5382022-07-22 19:26:55 -0500123 sdbusplus::bus_t bus = sdbusplus::bus::new_default();
Vishwanatha Subbannaa8a275f2021-03-26 00:33:34 -0500124 /* 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 Jeffery76544ab2023-02-06 18:11:30 +1030129 phosphor::led::Physical phy(bus, ledObj, led);
Vishwanatha Subbannaa8a275f2021-03-26 00:33:34 -0500130 EXPECT_EQ(phy.state(), Action::Off);
131}
132
Andrew Jeffery56692702018-05-25 11:41:43 +0930133TEST(Physical, ctor_timer_trigger)
134{
Patrick Williamsff3d5382022-07-22 19:26:55 -0500135 sdbusplus::bus_t bus = sdbusplus::bus::new_default();
Andrew Jeffery56692702018-05-25 11:41:43 +0930136 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 Jeffery76544ab2023-02-06 18:11:30 +1030140 phosphor::led::Physical phy(bus, ledObj, led);
George Liu001e2a32023-12-01 17:30:30 +0800141 EXPECT_EQ(phy.state(), Action::Blink);
Andrew Jeffery56692702018-05-25 11:41:43 +0930142}
143
Andrew Jefferya607a0d2018-05-23 16:15:26 +0930144TEST(Physical, off)
145{
Patrick Williamsff3d5382022-07-22 19:26:55 -0500146 sdbusplus::bus_t bus = sdbusplus::bus::new_default();
Andrew Jeffery280afaf2018-05-24 16:15:46 +0930147 NiceMock<MockLed> led;
Andrew Jeffery30726a02018-05-25 15:03:04 +0930148 ON_CALL(led, getMaxBrightness()).WillByDefault(Return(127));
149 EXPECT_CALL(led, getTrigger()).WillOnce(Return("none"));
Andrew Jeffery8e852282023-02-06 19:29:43 +1030150 EXPECT_CALL(led, getBrightness())
151 .WillOnce(Return(phosphor::led::deasserted));
152 EXPECT_CALL(led, setBrightness(phosphor::led::deasserted)).Times(0);
Andrew Jeffery76544ab2023-02-06 18:11:30 +1030153 phosphor::led::Physical phy(bus, ledObj, led);
Andrew Jeffery42e02d32018-05-24 13:34:05 +0930154 phy.state(Action::Off);
Vishwanatha Subbannadb21bc02021-03-26 00:32:46 -0500155 EXPECT_EQ(phy.state(), Action::Off);
Andrew Jefferya607a0d2018-05-23 16:15:26 +0930156}
157
158TEST(Physical, on)
159{
Andrew Jeffery5b1417b2019-03-18 17:20:37 +1030160 constexpr unsigned long asserted = 127;
161
Patrick Williamsff3d5382022-07-22 19:26:55 -0500162 sdbusplus::bus_t bus = sdbusplus::bus::new_default();
Andrew Jeffery280afaf2018-05-24 16:15:46 +0930163 NiceMock<MockLed> led;
Andrew Jeffery5b1417b2019-03-18 17:20:37 +1030164 ON_CALL(led, getMaxBrightness()).WillByDefault(Return(asserted));
Andrew Jeffery30726a02018-05-25 15:03:04 +0930165 EXPECT_CALL(led, getTrigger()).WillOnce(Return("none"));
166 EXPECT_CALL(led, setTrigger("none"));
Andrew Jeffery5b1417b2019-03-18 17:20:37 +1030167 EXPECT_CALL(led, setBrightness(asserted));
Andrew Jeffery76544ab2023-02-06 18:11:30 +1030168 phosphor::led::Physical phy(bus, ledObj, led);
Andrew Jeffery42e02d32018-05-24 13:34:05 +0930169 phy.state(Action::On);
Vishwanatha Subbannadb21bc02021-03-26 00:32:46 -0500170 EXPECT_EQ(phy.state(), Action::On);
Andrew Jefferya607a0d2018-05-23 16:15:26 +0930171}
172
173TEST(Physical, blink)
174{
Patrick Williamsff3d5382022-07-22 19:26:55 -0500175 sdbusplus::bus_t bus = sdbusplus::bus::new_default();
Andrew Jeffery280afaf2018-05-24 16:15:46 +0930176 NiceMock<MockLed> led;
Andrew Jeffery861e5622018-05-25 14:51:45 +0930177 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 Jeffery76544ab2023-02-06 18:11:30 +1030181 phosphor::led::Physical phy(bus, ledObj, led);
Andrew Jeffery42e02d32018-05-24 13:34:05 +0930182 phy.state(Action::Blink);
Vishwanatha Subbannadb21bc02021-03-26 00:32:46 -0500183 EXPECT_EQ(phy.state(), Action::Blink);
Andrew Jefferya607a0d2018-05-23 16:15:26 +0930184}
Andrew Jeffery04275e02018-05-25 13:04:39 +0930185
186TEST(Physical, ctor_none_trigger_asserted_brightness)
187{
Patrick Williamsff3d5382022-07-22 19:26:55 -0500188 sdbusplus::bus_t bus = sdbusplus::bus::new_default();
Andrew Jeffery04275e02018-05-25 13:04:39 +0930189 NiceMock<MockLed> led;
190 EXPECT_CALL(led, getTrigger()).WillRepeatedly(Return("none"));
Andrew Jeffery5b1417b2019-03-18 17:20:37 +1030191 EXPECT_CALL(led, getBrightness()).WillRepeatedly(Return(127));
Andrew Jeffery76544ab2023-02-06 18:11:30 +1030192 phosphor::led::Physical phy(bus, ledObj, led);
Vishwanatha Subbannadb21bc02021-03-26 00:32:46 -0500193 EXPECT_EQ(phy.state(), Action::Off);
Andrew Jeffery04275e02018-05-25 13:04:39 +0930194}
Andrew Jefferyaee9c2c2018-05-25 14:05:40 +0930195
196TEST(Physical, on_to_off)
197{
198 InSequence s;
Andrew Jeffery5b1417b2019-03-18 17:20:37 +1030199 constexpr unsigned long asserted = 127;
Andrew Jefferyaee9c2c2018-05-25 14:05:40 +0930200
201 auto bus = sdbusplus::bus::new_default();
202 NiceMock<MockLed> led;
Andrew Jeffery5b1417b2019-03-18 17:20:37 +1030203 ON_CALL(led, getMaxBrightness()).WillByDefault(Return(asserted));
Andrew Jefferyaee9c2c2018-05-25 14:05:40 +0930204 EXPECT_CALL(led, getTrigger()).Times(1).WillOnce(Return("none"));
Andrew Jeffery8e852282023-02-06 19:29:43 +1030205 constexpr auto deasserted = phosphor::led::deasserted;
Andrew Jefferyaee9c2c2018-05-25 14:05:40 +0930206 EXPECT_CALL(led, getBrightness()).WillOnce(Return(deasserted));
Andrew Jefferyaee9c2c2018-05-25 14:05:40 +0930207 EXPECT_CALL(led, setBrightness(asserted));
208 EXPECT_CALL(led, setBrightness(deasserted));
Andrew Jeffery76544ab2023-02-06 18:11:30 +1030209 phosphor::led::Physical phy(bus, ledObj, led);
Andrew Jefferyaee9c2c2018-05-25 14:05:40 +0930210 phy.state(Action::On);
Vishwanatha Subbannadb21bc02021-03-26 00:32:46 -0500211 EXPECT_EQ(phy.state(), Action::On);
Andrew Jefferyaee9c2c2018-05-25 14:05:40 +0930212 phy.state(Action::Off);
Vishwanatha Subbannadb21bc02021-03-26 00:32:46 -0500213 EXPECT_EQ(phy.state(), Action::Off);
Andrew Jefferyaee9c2c2018-05-25 14:05:40 +0930214}