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