xref: /openbmc/dbus-sensors/tests/test_Utils.cpp (revision e73bd0a1)
1*e73bd0a1SAndrew Jeffery #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");
30b429f31dSEd Tanous         {
31b429f31dSEd Tanous             std::ofstream temp1Input{hwmon10 / "temp1_input"};
32b429f31dSEd Tanous             std::ofstream temp1Min{hwmon10 / "temp1_min"};
33b429f31dSEd Tanous             std::ofstream temp1Max{hwmon10 / "temp1_max"};
34b429f31dSEd Tanous             std::ofstream temp2Input{hwmon10 / "temp2_input"};
35b429f31dSEd Tanous         }
36d3204145SLei YU         createPECIDir();
37d3204145SLei YU     }
38d3204145SLei YU 
39d3204145SLei YU     ~TestUtils() override
40d3204145SLei YU     {
41d3204145SLei YU         fs::remove_all(testDir);
42d3204145SLei YU     }
43d3204145SLei YU 
4465291360SEd Tanous     TestUtils(const TestUtils&) = delete;
4565291360SEd Tanous     TestUtils(TestUtils&&) = delete;
4665291360SEd Tanous     TestUtils& operator=(const TestUtils&) = delete;
4765291360SEd Tanous     TestUtils& operator=(TestUtils&&) = delete;
4865291360SEd Tanous 
49d3204145SLei YU     void createPECIDir()
50d3204145SLei YU     {
51d3204145SLei YU         peciDir = fs::path(testDir) / "peci";
52d3204145SLei YU         auto peci0 =
53d3204145SLei YU             peciDir / "peci-0/device/0-30/peci-cputemp.0/hwmon/hwmon25";
54d3204145SLei YU         fs::create_directories(peci0);
55b429f31dSEd Tanous         {
56b429f31dSEd Tanous             std::ofstream temp0Input{peci0 / "temp0_input"};
57b429f31dSEd Tanous             std::ofstream temp1Input{peci0 / "temp1_input"};
58b429f31dSEd Tanous             std::ofstream temp2Input{peci0 / "temp2_input"};
59b429f31dSEd Tanous             std::ofstream name{peci0 / "name"};
60b429f31dSEd Tanous         }
61d3204145SLei YU         auto devDir = peciDir / "peci-0/peci_dev/peci-0";
62d3204145SLei YU         fs::create_directories(devDir);
63d3204145SLei YU         fs::create_directory_symlink("../../../peci-0", devDir / "device");
64d3204145SLei YU         fs::create_directory_symlink("device/0-30", peciDir / "peci-0/0-30");
65d3204145SLei YU 
66d3204145SLei YU         // Let's keep this for debugging purpose
67d3204145SLei YU         for (auto p = fs::recursive_directory_iterator(
68d3204145SLei YU                  peciDir, fs::directory_options::follow_directory_symlink);
69d3204145SLei YU              p != fs::recursive_directory_iterator(); ++p)
70d3204145SLei YU         {
71d3204145SLei YU             std::string path = p->path().string();
7299c4409aSEd Tanous             std::cerr << path << "\n";
73d3204145SLei YU             if (p.depth() >= 6)
74d3204145SLei YU             {
75d3204145SLei YU                 p.disable_recursion_pending();
76d3204145SLei YU             }
77d3204145SLei YU         }
78d3204145SLei YU     }
79d3204145SLei YU };
80d3204145SLei YU 
81d3204145SLei YU TEST_F(TestUtils, findFiles_non_exist)
82d3204145SLei YU {
83d3204145SLei YU     std::vector<fs::path> foundPaths;
84d3204145SLei YU     auto ret = findFiles("non-exist", "", foundPaths);
85d3204145SLei YU 
86d3204145SLei YU     EXPECT_FALSE(ret);
87d3204145SLei YU     EXPECT_TRUE(foundPaths.empty());
88d3204145SLei YU }
89d3204145SLei YU 
90d3204145SLei YU TEST_F(TestUtils, findFiles_in_hwmon_no_match)
91d3204145SLei YU {
92d3204145SLei YU     std::vector<fs::path> foundPaths;
93d3204145SLei YU     auto ret = findFiles(hwmonDir, R"(in\d+_input)", foundPaths);
94d3204145SLei YU 
95d3204145SLei YU     EXPECT_TRUE(ret);
962049bd26SEd Tanous     EXPECT_EQ(foundPaths.size(), 0U);
97d3204145SLei YU }
98d3204145SLei YU 
99d3204145SLei YU TEST_F(TestUtils, findFiles_in_hwmon_match)
100d3204145SLei YU {
101d3204145SLei YU     std::vector<fs::path> foundPaths;
102d3204145SLei YU     auto ret = findFiles(hwmonDir, R"(temp\d+_input)", foundPaths);
103d3204145SLei YU 
104d3204145SLei YU     EXPECT_TRUE(ret);
1052049bd26SEd Tanous     EXPECT_EQ(foundPaths.size(), 2U);
106d3204145SLei YU }
107d3204145SLei YU 
108d3204145SLei YU TEST_F(TestUtils, findFiles_in_peci_no_match)
109d3204145SLei YU {
110d3204145SLei YU     std::vector<fs::path> foundPaths;
111d3204145SLei YU     auto ret =
112d3204145SLei YU         findFiles(peciDir, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/aaa$)",
113d3204145SLei YU                   foundPaths, 6);
114d3204145SLei YU 
115d3204145SLei YU     EXPECT_TRUE(ret);
116d3204145SLei YU     EXPECT_TRUE(foundPaths.empty());
117d3204145SLei YU }
118d3204145SLei YU 
119d3204145SLei YU TEST_F(TestUtils, findFiles_in_peci_match)
120d3204145SLei YU {
121d3204145SLei YU     std::vector<fs::path> foundPaths;
122d3204145SLei YU     auto ret =
123d3204145SLei YU         findFiles(peciDir, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
124d3204145SLei YU                   foundPaths, 6);
125d3204145SLei YU     EXPECT_TRUE(ret);
1262049bd26SEd Tanous     EXPECT_EQ(foundPaths.size(), 1U);
127d3204145SLei YU 
128d3204145SLei YU     foundPaths.clear();
129d3204145SLei YU 
130d3204145SLei YU     ret = findFiles(peciDir,
131d3204145SLei YU                     R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)",
132d3204145SLei YU                     foundPaths, 6);
133d3204145SLei YU     EXPECT_TRUE(ret);
1342049bd26SEd Tanous     EXPECT_EQ(foundPaths.size(), 3U);
135d3204145SLei YU }
136d3204145SLei YU 
137d3204145SLei YU TEST_F(TestUtils, findFiles_hwmonPath_end_with_slash)
138d3204145SLei YU {
139d3204145SLei YU     std::string p = hwmonDir.string() + "/";
140d3204145SLei YU     std::vector<fs::path> foundPaths;
141d3204145SLei YU     auto ret = findFiles(p, R"(temp\d+_input)", foundPaths);
142d3204145SLei YU 
143d3204145SLei YU     EXPECT_TRUE(ret);
1442049bd26SEd Tanous     EXPECT_EQ(foundPaths.size(), 2U);
145d3204145SLei YU }
146d3204145SLei YU 
147d3204145SLei YU TEST_F(TestUtils, findFiles_peciPath_end_with_slash)
148d3204145SLei YU {
149d3204145SLei YU     std::string p = peciDir.string() + "/";
150d3204145SLei YU     std::vector<fs::path> foundPaths;
151d3204145SLei YU     auto ret =
152d3204145SLei YU         findFiles(p, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)",
153d3204145SLei YU                   foundPaths, 6);
154d3204145SLei YU 
155d3204145SLei YU     EXPECT_TRUE(ret);
1562049bd26SEd Tanous     EXPECT_EQ(foundPaths.size(), 3U);
157d3204145SLei YU }
1580b207a62SLei YU 
1590b207a62SLei YU TEST_F(TestUtils, findFiles_in_sub_peci_match)
1600b207a62SLei YU {
1610b207a62SLei YU     std::vector<fs::path> foundPaths;
1620b207a62SLei YU     auto ret =
1630b207a62SLei YU         findFiles(peciDir / "peci-0", R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
1640b207a62SLei YU                   foundPaths, 5);
1650b207a62SLei YU     EXPECT_TRUE(ret);
1662049bd26SEd Tanous     EXPECT_EQ(foundPaths.size(), 1U);
1670b207a62SLei YU 
1680b207a62SLei YU     foundPaths.clear();
1690b207a62SLei YU 
1700b207a62SLei YU     ret = findFiles(peciDir / "peci-0",
1710b207a62SLei YU                     R"(\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)",
1720b207a62SLei YU                     foundPaths, 5);
1730b207a62SLei YU     EXPECT_TRUE(ret);
1742049bd26SEd Tanous     EXPECT_EQ(foundPaths.size(), 3U);
1750b207a62SLei YU }
176