blob: 5d83d80ffc5b49b0e480ebfea15f4bcc80157e7e [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
29constexpr unsigned long MAX_BRIGHTNESS_VAL = 128;
30
31class FakeSysfsLed : public phosphor::led::SysfsLed
32{
33 public:
34 static FakeSysfsLed create()
35 {
36 static constexpr auto tmplt = "/tmp/FakeSysfsLed.XXXXXX";
37 char buffer[MAXPATHLEN] = {0};
38
39 strncpy(buffer, tmplt, sizeof(buffer) - 1);
40 char* dir = mkdtemp(buffer);
41 if (!dir)
42 throw std::system_error(errno, std::system_category());
43
44 return FakeSysfsLed(fs::path(dir));
45 }
46
47 ~FakeSysfsLed()
48 {
49 fs::remove_all(root);
50 }
51
52 private:
George Liu1f2b9322021-12-28 10:32:49 +080053 explicit FakeSysfsLed(fs::path&& path) : SysfsLed(std::move(path))
Andrew Jefferye185efb2018-05-24 12:59:06 +093054 {
Andrew Jeffery8e852282023-02-06 19:29:43 +103055 static constexpr auto attrs = {attrBrightness, attrTrigger, attrDelayOn,
56 attrDelayOff};
Andrew Jefferye185efb2018-05-24 12:59:06 +093057 for (const auto& attr : attrs)
58 {
59 fs::path p = root / attr;
60 std::ofstream f(p, std::ios::out);
61 f.exceptions(f.failbit);
62 }
63
Andrew Jeffery8e852282023-02-06 19:29:43 +103064 fs::path p = root / attrMaxBrightness;
Andrew Jefferye185efb2018-05-24 12:59:06 +093065 std::ofstream f(p, std::ios::out);
66 f.exceptions(f.failbit);
67 f << MAX_BRIGHTNESS_VAL;
68 }
69};
70
71TEST(Sysfs, getBrightness)
72{
73 constexpr unsigned long brightness = 127;
74 FakeSysfsLed fsl = FakeSysfsLed::create();
75
76 fsl.setBrightness(brightness);
77 ASSERT_EQ(brightness, fsl.getBrightness());
78}
79
80TEST(Sysfs, getMaxBrightness)
81{
82 FakeSysfsLed fsl = FakeSysfsLed::create();
83
84 ASSERT_EQ(MAX_BRIGHTNESS_VAL, fsl.getMaxBrightness());
85}
86
87TEST(Sysfs, getTrigger)
88{
89 constexpr auto trigger = "none";
90 FakeSysfsLed fsl = FakeSysfsLed::create();
91
92 fsl.setTrigger(trigger);
93 ASSERT_EQ(trigger, fsl.getTrigger());
94}
95
96TEST(Sysfs, getDelayOn)
97{
98 constexpr unsigned long delayOn = 250;
99 FakeSysfsLed fsl = FakeSysfsLed::create();
100
101 fsl.setDelayOn(delayOn);
102 ASSERT_EQ(delayOn, fsl.getDelayOn());
103}
104
105TEST(Sysfs, getDelayOff)
106{
107 constexpr unsigned long delayOff = 750;
108 FakeSysfsLed fsl = FakeSysfsLed::create();
109
110 fsl.setDelayOff(delayOff);
111 ASSERT_EQ(delayOff, fsl.getDelayOff());
112}