xref: /openbmc/dbus-sensors/tests/test_Utils.cpp (revision 99c4409a)
1d3204145SLei YU #include <Utils.hpp>
2d3204145SLei YU 
3532c8649SEd Tanous #include <array>
4d3204145SLei YU #include <filesystem>
5d3204145SLei YU #include <fstream>
6d3204145SLei YU 
7d3204145SLei YU #include <gtest/gtest.h>
8d3204145SLei YU 
9d3204145SLei YU namespace fs = std::filesystem;
10d3204145SLei YU class TestUtils : public testing::Test
11d3204145SLei YU {
12d3204145SLei YU   public:
13d3204145SLei YU     std::string testDir;
14d3204145SLei YU     fs::path hwmonDir;
15d3204145SLei YU     fs::path peciDir;
16d3204145SLei YU     TestUtils()
17d3204145SLei YU     {
18d3204145SLei YU         // Create test environment
19532c8649SEd Tanous         auto dir = std::to_array("./testDirXXXXXX");
20532c8649SEd Tanous         testDir = mkdtemp(dir.data());
21d3204145SLei YU 
22d3204145SLei YU         if (testDir.empty())
23d3204145SLei YU         {
24d3204145SLei YU             throw std::bad_alloc();
25d3204145SLei YU         }
26d3204145SLei YU         hwmonDir = fs::path(testDir) / "hwmon";
27d3204145SLei YU         fs::create_directory(hwmonDir);
28d3204145SLei YU         auto hwmon10 = hwmonDir / "hwmon10";
29d3204145SLei YU         fs::create_directory(hwmonDir / "hwmon10");
30d3204145SLei YU         std::ofstream{hwmon10 / "temp1_input"};
31d3204145SLei YU         std::ofstream{hwmon10 / "temp1_min"};
32d3204145SLei YU         std::ofstream{hwmon10 / "temp1_max"};
33d3204145SLei YU         std::ofstream{hwmon10 / "temp2_input"};
34d3204145SLei YU         createPECIDir();
35d3204145SLei YU     }
36d3204145SLei YU 
37d3204145SLei YU     ~TestUtils() override
38d3204145SLei YU     {
39d3204145SLei YU         fs::remove_all(testDir);
40d3204145SLei YU     }
41d3204145SLei YU 
4265291360SEd Tanous     TestUtils(const TestUtils&) = delete;
4365291360SEd Tanous     TestUtils(TestUtils&&) = delete;
4465291360SEd Tanous     TestUtils& operator=(const TestUtils&) = delete;
4565291360SEd Tanous     TestUtils& operator=(TestUtils&&) = delete;
4665291360SEd Tanous 
47d3204145SLei YU     void createPECIDir()
48d3204145SLei YU     {
49d3204145SLei YU         peciDir = fs::path(testDir) / "peci";
50d3204145SLei YU         auto peci0 =
51d3204145SLei YU             peciDir / "peci-0/device/0-30/peci-cputemp.0/hwmon/hwmon25";
52d3204145SLei YU         fs::create_directories(peci0);
53d3204145SLei YU         std::ofstream{peci0 / "temp0_input"};
54d3204145SLei YU         std::ofstream{peci0 / "temp1_input"};
55d3204145SLei YU         std::ofstream{peci0 / "temp2_input"};
56d3204145SLei YU         std::ofstream{peci0 / "name"};
57d3204145SLei YU         auto devDir = peciDir / "peci-0/peci_dev/peci-0";
58d3204145SLei YU         fs::create_directories(devDir);
59d3204145SLei YU         fs::create_directory_symlink("../../../peci-0", devDir / "device");
60d3204145SLei YU         fs::create_directory_symlink("device/0-30", peciDir / "peci-0/0-30");
61d3204145SLei YU 
62d3204145SLei YU         // Let's keep this for debugging purpose
63d3204145SLei YU         for (auto p = fs::recursive_directory_iterator(
64d3204145SLei YU                  peciDir, fs::directory_options::follow_directory_symlink);
65d3204145SLei YU              p != fs::recursive_directory_iterator(); ++p)
66d3204145SLei YU         {
67d3204145SLei YU             std::string path = p->path().string();
68*99c4409aSEd Tanous             std::cerr << path << "\n";
69d3204145SLei YU             if (p.depth() >= 6)
70d3204145SLei YU             {
71d3204145SLei YU                 p.disable_recursion_pending();
72d3204145SLei YU             }
73d3204145SLei YU         }
74d3204145SLei YU     }
75d3204145SLei YU };
76d3204145SLei YU 
77d3204145SLei YU TEST_F(TestUtils, findFiles_non_exist)
78d3204145SLei YU {
79d3204145SLei YU     std::vector<fs::path> foundPaths;
80d3204145SLei YU     auto ret = findFiles("non-exist", "", foundPaths);
81d3204145SLei YU 
82d3204145SLei YU     EXPECT_FALSE(ret);
83d3204145SLei YU     EXPECT_TRUE(foundPaths.empty());
84d3204145SLei YU }
85d3204145SLei YU 
86d3204145SLei YU TEST_F(TestUtils, findFiles_in_hwmon_no_match)
87d3204145SLei YU {
88d3204145SLei YU     std::vector<fs::path> foundPaths;
89d3204145SLei YU     auto ret = findFiles(hwmonDir, R"(in\d+_input)", foundPaths);
90d3204145SLei YU 
91d3204145SLei YU     EXPECT_TRUE(ret);
92d3204145SLei YU     EXPECT_EQ(foundPaths.size(), 0u);
93d3204145SLei YU }
94d3204145SLei YU 
95d3204145SLei YU TEST_F(TestUtils, findFiles_in_hwmon_match)
96d3204145SLei YU {
97d3204145SLei YU     std::vector<fs::path> foundPaths;
98d3204145SLei YU     auto ret = findFiles(hwmonDir, R"(temp\d+_input)", foundPaths);
99d3204145SLei YU 
100d3204145SLei YU     EXPECT_TRUE(ret);
101d3204145SLei YU     EXPECT_EQ(foundPaths.size(), 2u);
102d3204145SLei YU }
103d3204145SLei YU 
104d3204145SLei YU TEST_F(TestUtils, findFiles_in_peci_no_match)
105d3204145SLei YU {
106d3204145SLei YU     std::vector<fs::path> foundPaths;
107d3204145SLei YU     auto ret =
108d3204145SLei YU         findFiles(peciDir, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/aaa$)",
109d3204145SLei YU                   foundPaths, 6);
110d3204145SLei YU 
111d3204145SLei YU     EXPECT_TRUE(ret);
112d3204145SLei YU     EXPECT_TRUE(foundPaths.empty());
113d3204145SLei YU }
114d3204145SLei YU 
115d3204145SLei YU TEST_F(TestUtils, findFiles_in_peci_match)
116d3204145SLei YU {
117d3204145SLei YU     std::vector<fs::path> foundPaths;
118d3204145SLei YU     auto ret =
119d3204145SLei YU         findFiles(peciDir, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
120d3204145SLei YU                   foundPaths, 6);
121d3204145SLei YU     EXPECT_TRUE(ret);
122d3204145SLei YU     EXPECT_EQ(foundPaths.size(), 1u);
123d3204145SLei YU 
124d3204145SLei YU     foundPaths.clear();
125d3204145SLei YU 
126d3204145SLei YU     ret = findFiles(peciDir,
127d3204145SLei YU                     R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)",
128d3204145SLei YU                     foundPaths, 6);
129d3204145SLei YU     EXPECT_TRUE(ret);
130d3204145SLei YU     EXPECT_EQ(foundPaths.size(), 3u);
131d3204145SLei YU }
132d3204145SLei YU 
133d3204145SLei YU TEST_F(TestUtils, findFiles_hwmonPath_end_with_slash)
134d3204145SLei YU {
135d3204145SLei YU     std::string p = hwmonDir.string() + "/";
136d3204145SLei YU     std::vector<fs::path> foundPaths;
137d3204145SLei YU     auto ret = findFiles(p, R"(temp\d+_input)", foundPaths);
138d3204145SLei YU 
139d3204145SLei YU     EXPECT_TRUE(ret);
140d3204145SLei YU     EXPECT_EQ(foundPaths.size(), 2u);
141d3204145SLei YU }
142d3204145SLei YU 
143d3204145SLei YU TEST_F(TestUtils, findFiles_peciPath_end_with_slash)
144d3204145SLei YU {
145d3204145SLei YU     std::string p = peciDir.string() + "/";
146d3204145SLei YU     std::vector<fs::path> foundPaths;
147d3204145SLei YU     auto ret =
148d3204145SLei YU         findFiles(p, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)",
149d3204145SLei YU                   foundPaths, 6);
150d3204145SLei YU 
151d3204145SLei YU     EXPECT_TRUE(ret);
152d3204145SLei YU     EXPECT_EQ(foundPaths.size(), 3u);
153d3204145SLei YU }
1540b207a62SLei YU 
1550b207a62SLei YU TEST_F(TestUtils, findFiles_in_sub_peci_match)
1560b207a62SLei YU {
1570b207a62SLei YU     std::vector<fs::path> foundPaths;
1580b207a62SLei YU     auto ret =
1590b207a62SLei YU         findFiles(peciDir / "peci-0", R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
1600b207a62SLei YU                   foundPaths, 5);
1610b207a62SLei YU     EXPECT_TRUE(ret);
1620b207a62SLei YU     EXPECT_EQ(foundPaths.size(), 1u);
1630b207a62SLei YU 
1640b207a62SLei YU     foundPaths.clear();
1650b207a62SLei YU 
1660b207a62SLei YU     ret = findFiles(peciDir / "peci-0",
1670b207a62SLei YU                     R"(\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)",
1680b207a62SLei YU                     foundPaths, 5);
1690b207a62SLei YU     EXPECT_TRUE(ret);
1700b207a62SLei YU     EXPECT_EQ(foundPaths.size(), 3u);
1710b207a62SLei YU }
172