Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 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 | |
George Liu | 45eba6f | 2021-05-18 14:32:20 +0800 | [diff] [blame] | 27 | namespace fs = std::filesystem; |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 28 | |
Andrew Jeffery | 5998645 | 2023-02-06 18:52:43 +1030 | [diff] [blame] | 29 | constexpr unsigned long maxBrightnessVal = 128; |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 30 | |
| 31 | class FakeSysfsLed : public phosphor::led::SysfsLed |
| 32 | { |
| 33 | public: |
Andrew Jeffery | b934a85 | 2023-02-06 18:57:30 +1030 | [diff] [blame^] | 34 | FakeSysfsLed(FakeSysfsLed& other) = delete; |
| 35 | FakeSysfsLed(FakeSysfsLed&& other) = delete; |
| 36 | FakeSysfsLed& operator=(FakeSysfsLed& other) = delete; |
| 37 | FakeSysfsLed&& operator=(FakeSysfsLed&& other) = delete; |
| 38 | |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 39 | static FakeSysfsLed create() |
| 40 | { |
| 41 | static constexpr auto tmplt = "/tmp/FakeSysfsLed.XXXXXX"; |
| 42 | char buffer[MAXPATHLEN] = {0}; |
| 43 | |
| 44 | strncpy(buffer, tmplt, sizeof(buffer) - 1); |
| 45 | char* dir = mkdtemp(buffer); |
| 46 | if (!dir) |
| 47 | throw std::system_error(errno, std::system_category()); |
| 48 | |
| 49 | return FakeSysfsLed(fs::path(dir)); |
| 50 | } |
| 51 | |
| 52 | ~FakeSysfsLed() |
| 53 | { |
| 54 | fs::remove_all(root); |
| 55 | } |
| 56 | |
| 57 | private: |
George Liu | 1f2b932 | 2021-12-28 10:32:49 +0800 | [diff] [blame] | 58 | explicit FakeSysfsLed(fs::path&& path) : SysfsLed(std::move(path)) |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 59 | { |
Andrew Jeffery | 8e85228 | 2023-02-06 19:29:43 +1030 | [diff] [blame] | 60 | static constexpr auto attrs = {attrBrightness, attrTrigger, attrDelayOn, |
| 61 | attrDelayOff}; |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 62 | for (const auto& attr : attrs) |
| 63 | { |
| 64 | fs::path p = root / attr; |
| 65 | std::ofstream f(p, std::ios::out); |
| 66 | f.exceptions(f.failbit); |
| 67 | } |
| 68 | |
Andrew Jeffery | 8e85228 | 2023-02-06 19:29:43 +1030 | [diff] [blame] | 69 | fs::path p = root / attrMaxBrightness; |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 70 | std::ofstream f(p, std::ios::out); |
| 71 | f.exceptions(f.failbit); |
Andrew Jeffery | 5998645 | 2023-02-06 18:52:43 +1030 | [diff] [blame] | 72 | f << maxBrightnessVal; |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 73 | } |
| 74 | }; |
| 75 | |
| 76 | TEST(Sysfs, getBrightness) |
| 77 | { |
| 78 | constexpr unsigned long brightness = 127; |
| 79 | FakeSysfsLed fsl = FakeSysfsLed::create(); |
| 80 | |
| 81 | fsl.setBrightness(brightness); |
| 82 | ASSERT_EQ(brightness, fsl.getBrightness()); |
| 83 | } |
| 84 | |
| 85 | TEST(Sysfs, getMaxBrightness) |
| 86 | { |
| 87 | FakeSysfsLed fsl = FakeSysfsLed::create(); |
| 88 | |
Andrew Jeffery | 5998645 | 2023-02-06 18:52:43 +1030 | [diff] [blame] | 89 | ASSERT_EQ(maxBrightnessVal, fsl.getMaxBrightness()); |
Andrew Jeffery | e185efb | 2018-05-24 12:59:06 +0930 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | TEST(Sysfs, getTrigger) |
| 93 | { |
| 94 | constexpr auto trigger = "none"; |
| 95 | FakeSysfsLed fsl = FakeSysfsLed::create(); |
| 96 | |
| 97 | fsl.setTrigger(trigger); |
| 98 | ASSERT_EQ(trigger, fsl.getTrigger()); |
| 99 | } |
| 100 | |
| 101 | TEST(Sysfs, getDelayOn) |
| 102 | { |
| 103 | constexpr unsigned long delayOn = 250; |
| 104 | FakeSysfsLed fsl = FakeSysfsLed::create(); |
| 105 | |
| 106 | fsl.setDelayOn(delayOn); |
| 107 | ASSERT_EQ(delayOn, fsl.getDelayOn()); |
| 108 | } |
| 109 | |
| 110 | TEST(Sysfs, getDelayOff) |
| 111 | { |
| 112 | constexpr unsigned long delayOff = 750; |
| 113 | FakeSysfsLed fsl = FakeSysfsLed::create(); |
| 114 | |
| 115 | fsl.setDelayOff(delayOff); |
| 116 | ASSERT_EQ(delayOff, fsl.getDelayOff()); |
| 117 | } |