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 | |
Ed Tanous | 6529136 | 2022-01-14 09:43:43 -0800 | [diff] [blame^] | 42 | TestUtils(const TestUtils&) = delete; |
| 43 | TestUtils(TestUtils&&) = delete; |
| 44 | TestUtils& operator=(const TestUtils&) = delete; |
| 45 | TestUtils& operator=(TestUtils&&) = delete; |
| 46 | |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 47 | void createPECIDir() |
| 48 | { |
| 49 | peciDir = fs::path(testDir) / "peci"; |
| 50 | auto peci0 = |
| 51 | peciDir / "peci-0/device/0-30/peci-cputemp.0/hwmon/hwmon25"; |
| 52 | fs::create_directories(peci0); |
| 53 | std::ofstream{peci0 / "temp0_input"}; |
| 54 | std::ofstream{peci0 / "temp1_input"}; |
| 55 | std::ofstream{peci0 / "temp2_input"}; |
| 56 | std::ofstream{peci0 / "name"}; |
| 57 | auto devDir = peciDir / "peci-0/peci_dev/peci-0"; |
| 58 | fs::create_directories(devDir); |
| 59 | fs::create_directory_symlink("../../../peci-0", devDir / "device"); |
| 60 | fs::create_directory_symlink("device/0-30", peciDir / "peci-0/0-30"); |
| 61 | |
| 62 | // Let's keep this for debugging purpose |
| 63 | for (auto p = fs::recursive_directory_iterator( |
| 64 | peciDir, fs::directory_options::follow_directory_symlink); |
| 65 | p != fs::recursive_directory_iterator(); ++p) |
| 66 | { |
| 67 | std::string path = p->path().string(); |
| 68 | fprintf(stderr, "%s\n", path.c_str()); |
| 69 | if (p.depth() >= 6) |
| 70 | { |
| 71 | p.disable_recursion_pending(); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | TEST_F(TestUtils, findFiles_non_exist) |
| 78 | { |
| 79 | std::vector<fs::path> foundPaths; |
| 80 | auto ret = findFiles("non-exist", "", foundPaths); |
| 81 | |
| 82 | EXPECT_FALSE(ret); |
| 83 | EXPECT_TRUE(foundPaths.empty()); |
| 84 | } |
| 85 | |
| 86 | TEST_F(TestUtils, findFiles_in_hwmon_no_match) |
| 87 | { |
| 88 | std::vector<fs::path> foundPaths; |
| 89 | auto ret = findFiles(hwmonDir, R"(in\d+_input)", foundPaths); |
| 90 | |
| 91 | EXPECT_TRUE(ret); |
| 92 | EXPECT_EQ(foundPaths.size(), 0u); |
| 93 | } |
| 94 | |
| 95 | TEST_F(TestUtils, findFiles_in_hwmon_match) |
| 96 | { |
| 97 | std::vector<fs::path> foundPaths; |
| 98 | auto ret = findFiles(hwmonDir, R"(temp\d+_input)", foundPaths); |
| 99 | |
| 100 | EXPECT_TRUE(ret); |
| 101 | EXPECT_EQ(foundPaths.size(), 2u); |
| 102 | } |
| 103 | |
| 104 | TEST_F(TestUtils, findFiles_in_peci_no_match) |
| 105 | { |
| 106 | std::vector<fs::path> foundPaths; |
| 107 | auto ret = |
| 108 | findFiles(peciDir, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/aaa$)", |
| 109 | foundPaths, 6); |
| 110 | |
| 111 | EXPECT_TRUE(ret); |
| 112 | EXPECT_TRUE(foundPaths.empty()); |
| 113 | } |
| 114 | |
| 115 | TEST_F(TestUtils, findFiles_in_peci_match) |
| 116 | { |
| 117 | std::vector<fs::path> foundPaths; |
| 118 | auto ret = |
| 119 | findFiles(peciDir, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", |
| 120 | foundPaths, 6); |
| 121 | EXPECT_TRUE(ret); |
| 122 | EXPECT_EQ(foundPaths.size(), 1u); |
| 123 | |
| 124 | foundPaths.clear(); |
| 125 | |
| 126 | ret = findFiles(peciDir, |
| 127 | R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)", |
| 128 | foundPaths, 6); |
| 129 | EXPECT_TRUE(ret); |
| 130 | EXPECT_EQ(foundPaths.size(), 3u); |
| 131 | } |
| 132 | |
| 133 | TEST_F(TestUtils, findFiles_hwmonPath_end_with_slash) |
| 134 | { |
| 135 | std::string p = hwmonDir.string() + "/"; |
| 136 | std::vector<fs::path> foundPaths; |
| 137 | auto ret = findFiles(p, R"(temp\d+_input)", foundPaths); |
| 138 | |
| 139 | EXPECT_TRUE(ret); |
| 140 | EXPECT_EQ(foundPaths.size(), 2u); |
| 141 | } |
| 142 | |
| 143 | TEST_F(TestUtils, findFiles_peciPath_end_with_slash) |
| 144 | { |
| 145 | std::string p = peciDir.string() + "/"; |
| 146 | std::vector<fs::path> foundPaths; |
| 147 | auto ret = |
| 148 | findFiles(p, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)", |
| 149 | foundPaths, 6); |
| 150 | |
| 151 | EXPECT_TRUE(ret); |
| 152 | EXPECT_EQ(foundPaths.size(), 3u); |
| 153 | } |
Lei YU | 0b207a6 | 2021-10-20 13:41:51 +0800 | [diff] [blame] | 154 | |
| 155 | TEST_F(TestUtils, findFiles_in_sub_peci_match) |
| 156 | { |
| 157 | std::vector<fs::path> foundPaths; |
| 158 | auto ret = |
| 159 | findFiles(peciDir / "peci-0", R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", |
| 160 | foundPaths, 5); |
| 161 | EXPECT_TRUE(ret); |
| 162 | EXPECT_EQ(foundPaths.size(), 1u); |
| 163 | |
| 164 | foundPaths.clear(); |
| 165 | |
| 166 | ret = findFiles(peciDir / "peci-0", |
| 167 | R"(\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)", |
| 168 | foundPaths, 5); |
| 169 | EXPECT_TRUE(ret); |
| 170 | EXPECT_EQ(foundPaths.size(), 3u); |
| 171 | } |