xref: /openbmc/phosphor-led-sysfs/test/sysfs.cpp (revision eaa1c89f)
1 /**
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 
27 namespace fs = std::filesystem;
28 
29 constexpr unsigned long maxBrightnessVal = 128;
30 
31 class FakeSysfsLed : public phosphor::led::SysfsLed
32 {
33   public:
34     FakeSysfsLed(FakeSysfsLed& other) = delete;
35     FakeSysfsLed(FakeSysfsLed&& other) = delete;
36     FakeSysfsLed& operator=(FakeSysfsLed& other) = delete;
37     FakeSysfsLed&& operator=(FakeSysfsLed&& other) = delete;
38 
39     static FakeSysfsLed create()
40     {
41         static constexpr auto tmplt = "/tmp/FakeSysfsLed.XXXXXX";
42         std::array<char, MAXPATHLEN> buffer = {0};
43 
44         strncpy(buffer.data(), tmplt, buffer.size() - 1);
45         char* dir = mkdtemp(buffer.data());
46         if (dir == nullptr)
47         {
48             throw std::system_error(errno, std::system_category());
49         }
50 
51         return FakeSysfsLed(fs::path(dir));
52     }
53 
54     ~FakeSysfsLed() override
55     {
56         fs::remove_all(root);
57     }
58 
59   private:
60     explicit FakeSysfsLed(fs::path&& path) : SysfsLed(std::move(path))
61     {
62         static constexpr auto attrs = {attrBrightness, attrTrigger, attrDelayOn,
63                                        attrDelayOff};
64         for (const auto& attr : attrs)
65         {
66             fs::path p = root / attr;
67             std::ofstream f(p, std::ios::out);
68             f.exceptions(std::ofstream::failbit);
69         }
70 
71         fs::path p = root / attrMaxBrightness;
72         std::ofstream f(p, std::ios::out);
73         f.exceptions(std::ofstream::failbit);
74         f << maxBrightnessVal;
75     }
76 };
77 
78 TEST(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 
87 TEST(Sysfs, getMaxBrightness)
88 {
89     FakeSysfsLed fsl = FakeSysfsLed::create();
90 
91     ASSERT_EQ(maxBrightnessVal, fsl.getMaxBrightness());
92 }
93 
94 TEST(Sysfs, getTrigger)
95 {
96     constexpr auto trigger = "none";
97     FakeSysfsLed fsl = FakeSysfsLed::create();
98 
99     fsl.setTrigger(trigger);
100     ASSERT_EQ(trigger, fsl.getTrigger());
101 }
102 
103 TEST(Sysfs, getDelayOn)
104 {
105     constexpr unsigned long delayOn = 250;
106     FakeSysfsLed fsl = FakeSysfsLed::create();
107 
108     fsl.setDelayOn(delayOn);
109     ASSERT_EQ(delayOn, fsl.getDelayOn());
110 }
111 
112 TEST(Sysfs, getDelayOff)
113 {
114     constexpr unsigned long delayOff = 750;
115     FakeSysfsLed fsl = FakeSysfsLed::create();
116 
117     fsl.setDelayOff(delayOff);
118     ASSERT_EQ(delayOff, fsl.getDelayOff());
119 }
120