Tomáš Pecka | efd8843 | 2020-06-17 15:31:39 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Tomáš Pecka <tomas.pecka@fit.cvut.cz> |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include "trompeloeil_doctest.h" |
| 9 | #include <filesystem> |
| 10 | #include <fstream> |
Tomáš Pecka | 261c886 | 2020-11-05 11:23:08 +0100 | [diff] [blame^] | 11 | #include "health/outputs/LedSysfsDriver.h" |
Tomáš Pecka | efd8843 | 2020-06-17 15:31:39 +0200 | [diff] [blame] | 12 | #include "test_log_setup.h" |
| 13 | #include "tests/configure.cmake.h" |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | uint32_t readFile(const std::filesystem::path& path) |
| 18 | { |
| 19 | std::ifstream ifs(path); |
| 20 | uint32_t ret; |
| 21 | if (!(ifs >> ret)) { |
| 22 | throw std::invalid_argument("Failed reading '" + std::string(path) + "'."); |
| 23 | } |
| 24 | return ret; |
| 25 | } |
| 26 | |
| 27 | /** @short Remove directory tree at 'rootDir' path (if exists) */ |
| 28 | void removeDirectoryTreeIfExists(const std::filesystem::path& rootDir) |
| 29 | { |
| 30 | if (std::filesystem::exists(rootDir)) { |
| 31 | std::filesystem::remove_all(rootDir); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | } |
| 36 | |
| 37 | TEST_CASE("SysFS LED driver") |
| 38 | { |
| 39 | using namespace std::literals; |
| 40 | TEST_INIT_LOGS; |
| 41 | |
| 42 | auto fakeSysfsDir = std::filesystem::path {CMAKE_CURRENT_BINARY_DIR + "/tests/led/"s}; |
| 43 | auto fakeBrightnessFile = fakeSysfsDir / "brightness"; |
| 44 | removeDirectoryTreeIfExists(fakeSysfsDir); |
| 45 | |
| 46 | SECTION("Basic usage test") |
| 47 | { |
| 48 | std::filesystem::copy(std::string(CMAKE_CURRENT_SOURCE_DIR) + "/tests/sysfs/led/1/"s, fakeSysfsDir, std::filesystem::copy_options::recursive); |
Tomáš Pecka | 261c886 | 2020-11-05 11:23:08 +0100 | [diff] [blame^] | 49 | velia::health::LedSysfsDriver led(fakeSysfsDir); |
Tomáš Pecka | efd8843 | 2020-06-17 15:31:39 +0200 | [diff] [blame] | 50 | |
| 51 | led.set(0); |
| 52 | REQUIRE(readFile(fakeBrightnessFile) == 0); |
| 53 | |
| 54 | led.set(1); |
| 55 | REQUIRE(readFile(fakeBrightnessFile) == 1); |
| 56 | |
| 57 | led.set(42); |
| 58 | REQUIRE(readFile(fakeBrightnessFile) == 42); |
| 59 | |
| 60 | led.set(0); |
| 61 | REQUIRE(readFile(fakeBrightnessFile) == 0); |
| 62 | } |
| 63 | |
| 64 | SECTION("Invalid directory") |
| 65 | { |
| 66 | std::filesystem::copy(std::string(CMAKE_CURRENT_SOURCE_DIR) + "/tests/sysfs/led/2/"s, fakeSysfsDir, std::filesystem::copy_options::recursive); |
Tomáš Pecka | 261c886 | 2020-11-05 11:23:08 +0100 | [diff] [blame^] | 67 | REQUIRE_THROWS_AS(velia::health::LedSysfsDriver(fakeSysfsDir), std::invalid_argument); |
Tomáš Pecka | efd8843 | 2020-06-17 15:31:39 +0200 | [diff] [blame] | 68 | } |
| 69 | } |