xref: /openbmc/dbus-sensors/tests/test_Utils.cpp (revision 03d333e0)
1 #include "Utils.hpp"
2 
3 #include <array>
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
19         auto dir = std::to_array("./testDirXXXXXX");
20         testDir = mkdtemp(dir.data());
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         {
31             std::ofstream temp1Input{hwmon10 / "temp1_input"};
32             std::ofstream temp1Min{hwmon10 / "temp1_min"};
33             std::ofstream temp1Max{hwmon10 / "temp1_max"};
34             std::ofstream temp2Input{hwmon10 / "temp2_input"};
35         }
36         createPECIDir();
37     }
38 
39     ~TestUtils() override
40     {
41         fs::remove_all(testDir);
42     }
43 
44     TestUtils(const TestUtils&) = delete;
45     TestUtils(TestUtils&&) = delete;
46     TestUtils& operator=(const TestUtils&) = delete;
47     TestUtils& operator=(TestUtils&&) = delete;
48 
49     void createPECIDir()
50     {
51         peciDir = fs::path(testDir) / "peci";
52         auto peci0 = peciDir /
53                      "peci-0/device/0-30/peci-cputemp.0/hwmon/hwmon25";
54         fs::create_directories(peci0);
55         {
56             std::ofstream temp0Input{peci0 / "temp0_input"};
57             std::ofstream temp1Input{peci0 / "temp1_input"};
58             std::ofstream temp2Input{peci0 / "temp2_input"};
59             std::ofstream name{peci0 / "name"};
60         }
61         auto devDir = peciDir / "peci-0/peci_dev/peci-0";
62         fs::create_directories(devDir);
63         fs::create_directory_symlink("../../../peci-0", devDir / "device");
64         fs::create_directory_symlink("device/0-30", peciDir / "peci-0/0-30");
65 
66         // Let's keep this for debugging purpose
67         for (auto p = fs::recursive_directory_iterator(
68                  peciDir, fs::directory_options::follow_directory_symlink);
69              p != fs::recursive_directory_iterator(); ++p)
70         {
71             std::string path = p->path().string();
72             std::cerr << path << "\n";
73             if (p.depth() >= 6)
74             {
75                 p.disable_recursion_pending();
76             }
77         }
78     }
79 };
80 
81 TEST_F(TestUtils, findFiles_non_exist)
82 {
83     std::vector<fs::path> foundPaths;
84     auto ret = findFiles("non-exist", "", foundPaths);
85 
86     EXPECT_FALSE(ret);
87     EXPECT_TRUE(foundPaths.empty());
88 }
89 
90 TEST_F(TestUtils, findFiles_in_hwmon_no_match)
91 {
92     std::vector<fs::path> foundPaths;
93     auto ret = findFiles(hwmonDir, R"(in\d+_input)", foundPaths);
94 
95     EXPECT_TRUE(ret);
96     EXPECT_EQ(foundPaths.size(), 0U);
97 }
98 
99 TEST_F(TestUtils, findFiles_in_hwmon_match)
100 {
101     std::vector<fs::path> foundPaths;
102     auto ret = findFiles(hwmonDir, R"(temp\d+_input)", foundPaths);
103 
104     EXPECT_TRUE(ret);
105     EXPECT_EQ(foundPaths.size(), 2U);
106 }
107 
108 TEST_F(TestUtils, findFiles_in_peci_no_match)
109 {
110     std::vector<fs::path> foundPaths;
111     auto ret = findFiles(peciDir,
112                          R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/aaa$)",
113                          foundPaths, 6);
114 
115     EXPECT_TRUE(ret);
116     EXPECT_TRUE(foundPaths.empty());
117 }
118 
119 TEST_F(TestUtils, findFiles_in_peci_match)
120 {
121     std::vector<fs::path> foundPaths;
122     auto ret = findFiles(peciDir,
123                          R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
124                          foundPaths, 6);
125     EXPECT_TRUE(ret);
126     EXPECT_EQ(foundPaths.size(), 1U);
127 
128     foundPaths.clear();
129 
130     ret = findFiles(peciDir,
131                     R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)",
132                     foundPaths, 6);
133     EXPECT_TRUE(ret);
134     EXPECT_EQ(foundPaths.size(), 3U);
135 }
136 
137 TEST_F(TestUtils, findFiles_hwmonPath_end_with_slash)
138 {
139     std::string p = hwmonDir.string() + "/";
140     std::vector<fs::path> foundPaths;
141     auto ret = findFiles(p, R"(temp\d+_input)", foundPaths);
142 
143     EXPECT_TRUE(ret);
144     EXPECT_EQ(foundPaths.size(), 2U);
145 }
146 
147 TEST_F(TestUtils, findFiles_peciPath_end_with_slash)
148 {
149     std::string p = peciDir.string() + "/";
150     std::vector<fs::path> foundPaths;
151     auto ret =
152         findFiles(p, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)",
153                   foundPaths, 6);
154 
155     EXPECT_TRUE(ret);
156     EXPECT_EQ(foundPaths.size(), 3U);
157 }
158 
159 TEST_F(TestUtils, findFiles_in_sub_peci_match)
160 {
161     std::vector<fs::path> foundPaths;
162     auto ret = findFiles(peciDir / "peci-0",
163                          R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", foundPaths,
164                          5);
165     EXPECT_TRUE(ret);
166     EXPECT_EQ(foundPaths.size(), 1U);
167 
168     foundPaths.clear();
169 
170     ret = findFiles(peciDir / "peci-0",
171                     R"(\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)",
172                     foundPaths, 5);
173     EXPECT_TRUE(ret);
174     EXPECT_EQ(foundPaths.size(), 3U);
175 }
176 
177 TEST(GetDeviceBusAddrTest, DevNameInvalid)
178 {
179     size_t bus = 0;
180     size_t addr = 0;
181     std::string devName;
182 
183     auto ret = getDeviceBusAddr(devName, bus, addr);
184     EXPECT_FALSE(ret);
185 
186     devName = "NoHyphen";
187     ret = getDeviceBusAddr(devName, bus, addr);
188     EXPECT_FALSE(ret);
189 
190     devName = "pwm-fan";
191     ret = getDeviceBusAddr(devName, bus, addr);
192     EXPECT_FALSE(ret);
193 }
194 
195 TEST(GetDeviceBusAddrTest, BusInvalid)
196 {
197     size_t bus = 0;
198     size_t addr = 0;
199     std::string devName = "FF-00FF";
200 
201     auto ret = getDeviceBusAddr(devName, bus, addr);
202     EXPECT_FALSE(ret);
203 }
204 
205 TEST(GetDeviceBusAddrTest, AddrInvalid)
206 {
207     size_t bus = 0;
208     size_t addr = 0;
209     std::string devName = "12-fan";
210 
211     auto ret = getDeviceBusAddr(devName, bus, addr);
212     EXPECT_FALSE(ret);
213 }
214 
215 TEST(GetDeviceBusAddrTest, AllValid)
216 {
217     size_t bus = 0;
218     size_t addr = 0;
219     std::string devName = "12-00af";
220 
221     auto ret = getDeviceBusAddr(devName, bus, addr);
222     EXPECT_TRUE(ret);
223     EXPECT_EQ(bus, 12);
224     EXPECT_EQ(addr, 0xaf);
225 }
226