blob: c949daa19ef03a02e43859f940bf471856b61026 [file] [log] [blame]
Andrew Jefferye185efb2018-05-24 12:59:06 +09301/**
2 * Copyright © 2019 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "sysfs.hpp"
18
19#include <sys/param.h>
20
21#include <cerrno>
22#include <cstdlib>
23#include <fstream>
24
25#include <gtest/gtest.h>
26
George Liu45eba6f2021-05-18 14:32:20 +080027namespace fs = std::filesystem;
Andrew Jefferye185efb2018-05-24 12:59:06 +093028
Andrew Jeffery59986452023-02-06 18:52:43 +103029constexpr unsigned long maxBrightnessVal = 128;
Andrew Jefferye185efb2018-05-24 12:59:06 +093030
31class FakeSysfsLed : public phosphor::led::SysfsLed
32{
33 public:
Andrew Jefferyb934a852023-02-06 18:57:30 +103034 FakeSysfsLed(FakeSysfsLed& other) = delete;
35 FakeSysfsLed(FakeSysfsLed&& other) = delete;
36 FakeSysfsLed& operator=(FakeSysfsLed& other) = delete;
37 FakeSysfsLed&& operator=(FakeSysfsLed&& other) = delete;
38
Andrew Jefferye185efb2018-05-24 12:59:06 +093039 static FakeSysfsLed create()
40 {
41 static constexpr auto tmplt = "/tmp/FakeSysfsLed.XXXXXX";
Andrew Jeffery41567272023-02-06 19:14:28 +103042 std::array<char, MAXPATHLEN> buffer = {0};
Andrew Jefferye185efb2018-05-24 12:59:06 +093043
Andrew Jeffery41567272023-02-06 19:14:28 +103044 strncpy(buffer.data(), tmplt, buffer.size() - 1);
45 char* dir = mkdtemp(buffer.data());
46 if (dir == nullptr)
Andrew Jefferyea808902023-02-06 19:17:14 +103047 {
Andrew Jefferye185efb2018-05-24 12:59:06 +093048 throw std::system_error(errno, std::system_category());
Andrew Jefferyea808902023-02-06 19:17:14 +103049 }
Andrew Jefferye185efb2018-05-24 12:59:06 +093050
51 return FakeSysfsLed(fs::path(dir));
52 }
53
Andrew Jeffery26085c22023-02-06 19:18:56 +103054 ~FakeSysfsLed() override
Andrew Jefferye185efb2018-05-24 12:59:06 +093055 {
56 fs::remove_all(root);
57 }
58
59 private:
George Liu1f2b9322021-12-28 10:32:49 +080060 explicit FakeSysfsLed(fs::path&& path) : SysfsLed(std::move(path))
Andrew Jefferye185efb2018-05-24 12:59:06 +093061 {
Andrew Jeffery8e852282023-02-06 19:29:43 +103062 static constexpr auto attrs = {attrBrightness, attrTrigger, attrDelayOn,
63 attrDelayOff};
Andrew Jefferye185efb2018-05-24 12:59:06 +093064 for (const auto& attr : attrs)
65 {
66 fs::path p = root / attr;
67 std::ofstream f(p, std::ios::out);
Andrew Jefferyf97abd12023-02-06 19:32:47 +103068 f.exceptions(std::ofstream::failbit);
Andrew Jefferye185efb2018-05-24 12:59:06 +093069 }
70
Andrew Jeffery8e852282023-02-06 19:29:43 +103071 fs::path p = root / attrMaxBrightness;
Andrew Jefferye185efb2018-05-24 12:59:06 +093072 std::ofstream f(p, std::ios::out);
Andrew Jefferyf97abd12023-02-06 19:32:47 +103073 f.exceptions(std::ofstream::failbit);
Andrew Jeffery59986452023-02-06 18:52:43 +103074 f << maxBrightnessVal;
Andrew Jefferye185efb2018-05-24 12:59:06 +093075 }
76};
77
78TEST(Sysfs, getBrightness)
79{
80 constexpr unsigned long brightness = 127;
81 FakeSysfsLed fsl = FakeSysfsLed::create();
82
83 fsl.setBrightness(brightness);
84 ASSERT_EQ(brightness, fsl.getBrightness());
85}
86
87TEST(Sysfs, getMaxBrightness)
88{
89 FakeSysfsLed fsl = FakeSysfsLed::create();
90
Andrew Jeffery59986452023-02-06 18:52:43 +103091 ASSERT_EQ(maxBrightnessVal, fsl.getMaxBrightness());
Andrew Jefferye185efb2018-05-24 12:59:06 +093092}
93
94TEST(Sysfs, getTrigger)
95{
Andrew Jefferye185efb2018-05-24 12:59:06 +093096 FakeSysfsLed fsl = FakeSysfsLed::create();
97
George Liu001e2a32023-12-01 17:30:30 +080098 // We need to set the complete attribute value in UT, Because the LED driver
99 // is not called in UT to automatically set `none` to `[none] xxx yyy`
100 fsl.setTrigger("[none] timer heartbeat default-on");
101 ASSERT_EQ("none", fsl.getTrigger());
102}
103
104TEST(Sysfs, getTriggerBlink)
105{
106 FakeSysfsLed fsl = FakeSysfsLed::create();
107 fsl.setTrigger("none [timer] heartbeat default-on");
108 ASSERT_EQ("timer", fsl.getTrigger());
Andrew Jefferye185efb2018-05-24 12:59:06 +0930109}
110
111TEST(Sysfs, getDelayOn)
112{
113 constexpr unsigned long delayOn = 250;
114 FakeSysfsLed fsl = FakeSysfsLed::create();
115
116 fsl.setDelayOn(delayOn);
117 ASSERT_EQ(delayOn, fsl.getDelayOn());
118}
119
120TEST(Sysfs, getDelayOff)
121{
122 constexpr unsigned long delayOff = 750;
123 FakeSysfsLed fsl = FakeSysfsLed::create();
124
125 fsl.setDelayOff(delayOff);
126 ASSERT_EQ(delayOff, fsl.getDelayOff());
127}