xref: /openbmc/dbus-sensors/tests/test_Utils.cpp (revision 532c8649)
1d3204145SLei YU #include <Utils.hpp>
2d3204145SLei YU 
3*532c8649SEd 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
19*532c8649SEd Tanous         auto dir = std::to_array("./testDirXXXXXX");
20*532c8649SEd 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 
42d3204145SLei YU     void createPECIDir()
43d3204145SLei YU     {
44d3204145SLei YU         peciDir = fs::path(testDir) / "peci";
45d3204145SLei YU         auto peci0 =
46d3204145SLei YU             peciDir / "peci-0/device/0-30/peci-cputemp.0/hwmon/hwmon25";
47d3204145SLei YU         fs::create_directories(peci0);
48d3204145SLei YU         std::ofstream{peci0 / "temp0_input"};
49d3204145SLei YU         std::ofstream{peci0 / "temp1_input"};
50d3204145SLei YU         std::ofstream{peci0 / "temp2_input"};
51d3204145SLei YU         std::ofstream{peci0 / "name"};
52d3204145SLei YU         auto devDir = peciDir / "peci-0/peci_dev/peci-0";
53d3204145SLei YU         fs::create_directories(devDir);
54d3204145SLei YU         fs::create_directory_symlink("../../../peci-0", devDir / "device");
55d3204145SLei YU         fs::create_directory_symlink("device/0-30", peciDir / "peci-0/0-30");
56d3204145SLei YU 
57d3204145SLei YU         // Let's keep this for debugging purpose
58d3204145SLei YU         for (auto p = fs::recursive_directory_iterator(
59d3204145SLei YU                  peciDir, fs::directory_options::follow_directory_symlink);
60d3204145SLei YU              p != fs::recursive_directory_iterator(); ++p)
61d3204145SLei YU         {
62d3204145SLei YU             std::string path = p->path().string();
63d3204145SLei YU             fprintf(stderr, "%s\n", path.c_str());
64d3204145SLei YU             if (p.depth() >= 6)
65d3204145SLei YU             {
66d3204145SLei YU                 p.disable_recursion_pending();
67d3204145SLei YU             }
68d3204145SLei YU         }
69d3204145SLei YU     }
70d3204145SLei YU };
71d3204145SLei YU 
72d3204145SLei YU TEST_F(TestUtils, findFiles_non_exist)
73d3204145SLei YU {
74d3204145SLei YU     std::vector<fs::path> foundPaths;
75d3204145SLei YU     auto ret = findFiles("non-exist", "", foundPaths);
76d3204145SLei YU 
77d3204145SLei YU     EXPECT_FALSE(ret);
78d3204145SLei YU     EXPECT_TRUE(foundPaths.empty());
79d3204145SLei YU }
80d3204145SLei YU 
81d3204145SLei YU TEST_F(TestUtils, findFiles_in_hwmon_no_match)
82d3204145SLei YU {
83d3204145SLei YU     std::vector<fs::path> foundPaths;
84d3204145SLei YU     auto ret = findFiles(hwmonDir, R"(in\d+_input)", foundPaths);
85d3204145SLei YU 
86d3204145SLei YU     EXPECT_TRUE(ret);
87d3204145SLei YU     EXPECT_EQ(foundPaths.size(), 0u);
88d3204145SLei YU }
89d3204145SLei YU 
90d3204145SLei YU TEST_F(TestUtils, findFiles_in_hwmon_match)
91d3204145SLei YU {
92d3204145SLei YU     std::vector<fs::path> foundPaths;
93d3204145SLei YU     auto ret = findFiles(hwmonDir, R"(temp\d+_input)", foundPaths);
94d3204145SLei YU 
95d3204145SLei YU     EXPECT_TRUE(ret);
96d3204145SLei YU     EXPECT_EQ(foundPaths.size(), 2u);
97d3204145SLei YU }
98d3204145SLei YU 
99d3204145SLei YU TEST_F(TestUtils, findFiles_in_peci_no_match)
100d3204145SLei YU {
101d3204145SLei YU     std::vector<fs::path> foundPaths;
102d3204145SLei YU     auto ret =
103d3204145SLei YU         findFiles(peciDir, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/aaa$)",
104d3204145SLei YU                   foundPaths, 6);
105d3204145SLei YU 
106d3204145SLei YU     EXPECT_TRUE(ret);
107d3204145SLei YU     EXPECT_TRUE(foundPaths.empty());
108d3204145SLei YU }
109d3204145SLei YU 
110d3204145SLei YU TEST_F(TestUtils, findFiles_in_peci_match)
111d3204145SLei YU {
112d3204145SLei YU     std::vector<fs::path> foundPaths;
113d3204145SLei YU     auto ret =
114d3204145SLei YU         findFiles(peciDir, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
115d3204145SLei YU                   foundPaths, 6);
116d3204145SLei YU     EXPECT_TRUE(ret);
117d3204145SLei YU     EXPECT_EQ(foundPaths.size(), 1u);
118d3204145SLei YU 
119d3204145SLei YU     foundPaths.clear();
120d3204145SLei YU 
121d3204145SLei YU     ret = findFiles(peciDir,
122d3204145SLei YU                     R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)",
123d3204145SLei YU                     foundPaths, 6);
124d3204145SLei YU     EXPECT_TRUE(ret);
125d3204145SLei YU     EXPECT_EQ(foundPaths.size(), 3u);
126d3204145SLei YU }
127d3204145SLei YU 
128d3204145SLei YU TEST_F(TestUtils, findFiles_hwmonPath_end_with_slash)
129d3204145SLei YU {
130d3204145SLei YU     std::string p = hwmonDir.string() + "/";
131d3204145SLei YU     std::vector<fs::path> foundPaths;
132d3204145SLei YU     auto ret = findFiles(p, R"(temp\d+_input)", foundPaths);
133d3204145SLei YU 
134d3204145SLei YU     EXPECT_TRUE(ret);
135d3204145SLei YU     EXPECT_EQ(foundPaths.size(), 2u);
136d3204145SLei YU }
137d3204145SLei YU 
138d3204145SLei YU TEST_F(TestUtils, findFiles_peciPath_end_with_slash)
139d3204145SLei YU {
140d3204145SLei YU     std::string p = peciDir.string() + "/";
141d3204145SLei YU     std::vector<fs::path> foundPaths;
142d3204145SLei YU     auto ret =
143d3204145SLei YU         findFiles(p, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)",
144d3204145SLei YU                   foundPaths, 6);
145d3204145SLei YU 
146d3204145SLei YU     EXPECT_TRUE(ret);
147d3204145SLei YU     EXPECT_EQ(foundPaths.size(), 3u);
148d3204145SLei YU }
1490b207a62SLei YU 
1500b207a62SLei YU TEST_F(TestUtils, findFiles_in_sub_peci_match)
1510b207a62SLei YU {
1520b207a62SLei YU     std::vector<fs::path> foundPaths;
1530b207a62SLei YU     auto ret =
1540b207a62SLei YU         findFiles(peciDir / "peci-0", R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
1550b207a62SLei YU                   foundPaths, 5);
1560b207a62SLei YU     EXPECT_TRUE(ret);
1570b207a62SLei YU     EXPECT_EQ(foundPaths.size(), 1u);
1580b207a62SLei YU 
1590b207a62SLei YU     foundPaths.clear();
1600b207a62SLei YU 
1610b207a62SLei YU     ret = findFiles(peciDir / "peci-0",
1620b207a62SLei YU                     R"(\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)",
1630b207a62SLei YU                     foundPaths, 5);
1640b207a62SLei YU     EXPECT_TRUE(ret);
1650b207a62SLei YU     EXPECT_EQ(foundPaths.size(), 3u);
1660b207a62SLei YU }
167