Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 1 | #include <Utils.hpp> |
| 2 | |
Ed Tanous | 532c864 | 2022-01-14 09:48:15 -0800 | [diff] [blame^] | 3 | #include <array> |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 4 | #include <filesystem> |
| 5 | #include <fstream> |
| 6 | |
| 7 | #include <gtest/gtest.h> |
| 8 | |
| 9 | namespace fs = std::filesystem; |
| 10 | class TestUtils : public testing::Test |
| 11 | { |
| 12 | public: |
| 13 | std::string testDir; |
| 14 | fs::path hwmonDir; |
| 15 | fs::path peciDir; |
| 16 | TestUtils() |
| 17 | { |
| 18 | // Create test environment |
Ed Tanous | 532c864 | 2022-01-14 09:48:15 -0800 | [diff] [blame^] | 19 | auto dir = std::to_array("./testDirXXXXXX"); |
| 20 | testDir = mkdtemp(dir.data()); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 21 | |
| 22 | if (testDir.empty()) |
| 23 | { |
| 24 | throw std::bad_alloc(); |
| 25 | } |
| 26 | hwmonDir = fs::path(testDir) / "hwmon"; |
| 27 | fs::create_directory(hwmonDir); |
| 28 | auto hwmon10 = hwmonDir / "hwmon10"; |
| 29 | fs::create_directory(hwmonDir / "hwmon10"); |
| 30 | std::ofstream{hwmon10 / "temp1_input"}; |
| 31 | std::ofstream{hwmon10 / "temp1_min"}; |
| 32 | std::ofstream{hwmon10 / "temp1_max"}; |
| 33 | std::ofstream{hwmon10 / "temp2_input"}; |
| 34 | createPECIDir(); |
| 35 | } |
| 36 | |
| 37 | ~TestUtils() override |
| 38 | { |
| 39 | fs::remove_all(testDir); |
| 40 | } |
| 41 | |
| 42 | void createPECIDir() |
| 43 | { |
| 44 | peciDir = fs::path(testDir) / "peci"; |
| 45 | auto peci0 = |
| 46 | peciDir / "peci-0/device/0-30/peci-cputemp.0/hwmon/hwmon25"; |
| 47 | fs::create_directories(peci0); |
| 48 | std::ofstream{peci0 / "temp0_input"}; |
| 49 | std::ofstream{peci0 / "temp1_input"}; |
| 50 | std::ofstream{peci0 / "temp2_input"}; |
| 51 | std::ofstream{peci0 / "name"}; |
| 52 | auto devDir = peciDir / "peci-0/peci_dev/peci-0"; |
| 53 | fs::create_directories(devDir); |
| 54 | fs::create_directory_symlink("../../../peci-0", devDir / "device"); |
| 55 | fs::create_directory_symlink("device/0-30", peciDir / "peci-0/0-30"); |
| 56 | |
| 57 | // Let's keep this for debugging purpose |
| 58 | for (auto p = fs::recursive_directory_iterator( |
| 59 | peciDir, fs::directory_options::follow_directory_symlink); |
| 60 | p != fs::recursive_directory_iterator(); ++p) |
| 61 | { |
| 62 | std::string path = p->path().string(); |
| 63 | fprintf(stderr, "%s\n", path.c_str()); |
| 64 | if (p.depth() >= 6) |
| 65 | { |
| 66 | p.disable_recursion_pending(); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | TEST_F(TestUtils, findFiles_non_exist) |
| 73 | { |
| 74 | std::vector<fs::path> foundPaths; |
| 75 | auto ret = findFiles("non-exist", "", foundPaths); |
| 76 | |
| 77 | EXPECT_FALSE(ret); |
| 78 | EXPECT_TRUE(foundPaths.empty()); |
| 79 | } |
| 80 | |
| 81 | TEST_F(TestUtils, findFiles_in_hwmon_no_match) |
| 82 | { |
| 83 | std::vector<fs::path> foundPaths; |
| 84 | auto ret = findFiles(hwmonDir, R"(in\d+_input)", foundPaths); |
| 85 | |
| 86 | EXPECT_TRUE(ret); |
| 87 | EXPECT_EQ(foundPaths.size(), 0u); |
| 88 | } |
| 89 | |
| 90 | TEST_F(TestUtils, findFiles_in_hwmon_match) |
| 91 | { |
| 92 | std::vector<fs::path> foundPaths; |
| 93 | auto ret = findFiles(hwmonDir, R"(temp\d+_input)", foundPaths); |
| 94 | |
| 95 | EXPECT_TRUE(ret); |
| 96 | EXPECT_EQ(foundPaths.size(), 2u); |
| 97 | } |
| 98 | |
| 99 | TEST_F(TestUtils, findFiles_in_peci_no_match) |
| 100 | { |
| 101 | std::vector<fs::path> foundPaths; |
| 102 | auto ret = |
| 103 | findFiles(peciDir, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/aaa$)", |
| 104 | foundPaths, 6); |
| 105 | |
| 106 | EXPECT_TRUE(ret); |
| 107 | EXPECT_TRUE(foundPaths.empty()); |
| 108 | } |
| 109 | |
| 110 | TEST_F(TestUtils, findFiles_in_peci_match) |
| 111 | { |
| 112 | std::vector<fs::path> foundPaths; |
| 113 | auto ret = |
| 114 | findFiles(peciDir, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", |
| 115 | foundPaths, 6); |
| 116 | EXPECT_TRUE(ret); |
| 117 | EXPECT_EQ(foundPaths.size(), 1u); |
| 118 | |
| 119 | foundPaths.clear(); |
| 120 | |
| 121 | ret = findFiles(peciDir, |
| 122 | R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)", |
| 123 | foundPaths, 6); |
| 124 | EXPECT_TRUE(ret); |
| 125 | EXPECT_EQ(foundPaths.size(), 3u); |
| 126 | } |
| 127 | |
| 128 | TEST_F(TestUtils, findFiles_hwmonPath_end_with_slash) |
| 129 | { |
| 130 | std::string p = hwmonDir.string() + "/"; |
| 131 | std::vector<fs::path> foundPaths; |
| 132 | auto ret = findFiles(p, R"(temp\d+_input)", foundPaths); |
| 133 | |
| 134 | EXPECT_TRUE(ret); |
| 135 | EXPECT_EQ(foundPaths.size(), 2u); |
| 136 | } |
| 137 | |
| 138 | TEST_F(TestUtils, findFiles_peciPath_end_with_slash) |
| 139 | { |
| 140 | std::string p = peciDir.string() + "/"; |
| 141 | std::vector<fs::path> foundPaths; |
| 142 | auto ret = |
| 143 | findFiles(p, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)", |
| 144 | foundPaths, 6); |
| 145 | |
| 146 | EXPECT_TRUE(ret); |
| 147 | EXPECT_EQ(foundPaths.size(), 3u); |
| 148 | } |
Lei YU | 0b207a6 | 2021-10-20 13:41:51 +0800 | [diff] [blame] | 149 | |
| 150 | TEST_F(TestUtils, findFiles_in_sub_peci_match) |
| 151 | { |
| 152 | std::vector<fs::path> foundPaths; |
| 153 | auto ret = |
| 154 | findFiles(peciDir / "peci-0", R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", |
| 155 | foundPaths, 5); |
| 156 | EXPECT_TRUE(ret); |
| 157 | EXPECT_EQ(foundPaths.size(), 1u); |
| 158 | |
| 159 | foundPaths.clear(); |
| 160 | |
| 161 | ret = findFiles(peciDir / "peci-0", |
| 162 | R"(\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)", |
| 163 | foundPaths, 5); |
| 164 | EXPECT_TRUE(ret); |
| 165 | EXPECT_EQ(foundPaths.size(), 3u); |
| 166 | } |