1 #include "Utils.hpp"
2
3 #include <phosphor-logging/lg2.hpp>
4
5 #include <array>
6 #include <cstddef>
7 #include <cstdint>
8 #include <cstdlib>
9 #include <filesystem>
10 #include <fstream>
11 #include <new>
12 #include <string>
13 #include <vector>
14
15 #include <gtest/gtest.h>
16
17 class TestUtils : public testing::Test
18 {
19 public:
20 std::string testDir;
21 std::filesystem::path hwmonDir;
22 std::filesystem::path peciDir;
TestUtils()23 TestUtils()
24 {
25 // Create test environment
26 auto dir = std::to_array("./testDirXXXXXX");
27 testDir = mkdtemp(dir.data());
28
29 if (testDir.empty())
30 {
31 throw std::bad_alloc();
32 }
33 hwmonDir = std::filesystem::path(testDir) / "hwmon";
34 std::filesystem::create_directory(hwmonDir);
35 auto hwmon10 = hwmonDir / "hwmon10";
36 std::filesystem::create_directory(hwmonDir / "hwmon10");
37 {
38 std::ofstream temp1Input{hwmon10 / "temp1_input"};
39 std::ofstream temp1Min{hwmon10 / "temp1_min"};
40 std::ofstream temp1Max{hwmon10 / "temp1_max"};
41 std::ofstream temp2Input{hwmon10 / "temp2_input"};
42 }
43 createPECIDir();
44 }
45
~TestUtils()46 ~TestUtils() override
47 {
48 std::filesystem::remove_all(testDir);
49 }
50
51 TestUtils(const TestUtils&) = delete;
52 TestUtils(TestUtils&&) = delete;
53 TestUtils& operator=(const TestUtils&) = delete;
54 TestUtils& operator=(TestUtils&&) = delete;
55
createPECIDir()56 void createPECIDir()
57 {
58 peciDir = std::filesystem::path(testDir) / "peci";
59 auto peci0 = peciDir /
60 "peci-0/device/0-30/peci-cputemp.0/hwmon/hwmon25";
61 std::filesystem::create_directories(peci0);
62 {
63 std::ofstream temp0Input{peci0 / "temp0_input"};
64 std::ofstream temp1Input{peci0 / "temp1_input"};
65 std::ofstream temp2Input{peci0 / "temp2_input"};
66 std::ofstream name{peci0 / "name"};
67 }
68 auto devDir = peciDir / "peci-0/peci_dev/peci-0";
69 std::filesystem::create_directories(devDir);
70 std::filesystem::create_directory_symlink("../../../peci-0",
71 devDir / "device");
72 std::filesystem::create_directory_symlink("device/0-30",
73 peciDir / "peci-0/0-30");
74
75 // Let's keep this for debugging purpose
76 for (auto p = std::filesystem::recursive_directory_iterator(
77 peciDir,
78 std::filesystem::directory_options::follow_directory_symlink);
79 p != std::filesystem::recursive_directory_iterator(); ++p)
80 {
81 std::string path = p->path().string();
82 lg2::info("{PATH}", "PATH", path);
83 if (p.depth() >= 6)
84 {
85 p.disable_recursion_pending();
86 }
87 }
88 }
89 };
90
TEST_F(TestUtils,findFiles_non_exist)91 TEST_F(TestUtils, findFiles_non_exist)
92 {
93 std::vector<std::filesystem::path> foundPaths;
94 auto ret = findFiles("non-exist", "", foundPaths);
95
96 EXPECT_FALSE(ret);
97 EXPECT_TRUE(foundPaths.empty());
98 }
99
TEST_F(TestUtils,findFiles_in_hwmon_no_match)100 TEST_F(TestUtils, findFiles_in_hwmon_no_match)
101 {
102 std::vector<std::filesystem::path> foundPaths;
103 auto ret = findFiles(hwmonDir, R"(in\d+_input)", foundPaths);
104
105 EXPECT_TRUE(ret);
106 EXPECT_EQ(foundPaths.size(), 0U);
107 }
108
TEST_F(TestUtils,findFiles_in_hwmon_match)109 TEST_F(TestUtils, findFiles_in_hwmon_match)
110 {
111 std::vector<std::filesystem::path> foundPaths;
112 auto ret = findFiles(hwmonDir, R"(temp\d+_input)", foundPaths);
113
114 EXPECT_TRUE(ret);
115 EXPECT_EQ(foundPaths.size(), 2U);
116 }
117
TEST_F(TestUtils,findFiles_in_peci_no_match)118 TEST_F(TestUtils, findFiles_in_peci_no_match)
119 {
120 std::vector<std::filesystem::path> foundPaths;
121 auto ret =
122 findFiles(peciDir, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/aaa$)",
123 foundPaths, 6);
124
125 EXPECT_TRUE(ret);
126 EXPECT_TRUE(foundPaths.empty());
127 }
128
TEST_F(TestUtils,findFiles_in_peci_match)129 TEST_F(TestUtils, findFiles_in_peci_match)
130 {
131 std::vector<std::filesystem::path> foundPaths;
132 auto ret =
133 findFiles(peciDir, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
134 foundPaths, 6);
135 EXPECT_TRUE(ret);
136 EXPECT_EQ(foundPaths.size(), 1U);
137
138 foundPaths.clear();
139
140 ret = findFiles(peciDir,
141 R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)",
142 foundPaths, 6);
143 EXPECT_TRUE(ret);
144 EXPECT_EQ(foundPaths.size(), 3U);
145 }
146
TEST_F(TestUtils,findFiles_hwmonPath_end_with_slash)147 TEST_F(TestUtils, findFiles_hwmonPath_end_with_slash)
148 {
149 std::string p = hwmonDir.string() + "/";
150 std::vector<std::filesystem::path> foundPaths;
151 auto ret = findFiles(p, R"(temp\d+_input)", foundPaths);
152
153 EXPECT_TRUE(ret);
154 EXPECT_EQ(foundPaths.size(), 2U);
155 }
156
TEST_F(TestUtils,findFiles_peciPath_end_with_slash)157 TEST_F(TestUtils, findFiles_peciPath_end_with_slash)
158 {
159 std::string p = peciDir.string() + "/";
160 std::vector<std::filesystem::path> foundPaths;
161 auto ret =
162 findFiles(p, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)",
163 foundPaths, 6);
164
165 EXPECT_TRUE(ret);
166 EXPECT_EQ(foundPaths.size(), 3U);
167 }
168
TEST_F(TestUtils,findFiles_in_sub_peci_match)169 TEST_F(TestUtils, findFiles_in_sub_peci_match)
170 {
171 std::vector<std::filesystem::path> foundPaths;
172 auto ret =
173 findFiles(peciDir / "peci-0", R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
174 foundPaths, 5);
175 EXPECT_TRUE(ret);
176 EXPECT_EQ(foundPaths.size(), 1U);
177
178 foundPaths.clear();
179
180 ret = findFiles(peciDir / "peci-0",
181 R"(\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)",
182 foundPaths, 5);
183 EXPECT_TRUE(ret);
184 EXPECT_EQ(foundPaths.size(), 3U);
185 }
186
TEST(GetDeviceBusAddrTest,DevNameInvalid)187 TEST(GetDeviceBusAddrTest, DevNameInvalid)
188 {
189 size_t bus = 0;
190 size_t addr = 0;
191 std::string devName;
192
193 auto ret = getDeviceBusAddr(devName, bus, addr);
194 EXPECT_FALSE(ret);
195
196 devName = "NoHyphen";
197 ret = getDeviceBusAddr(devName, bus, addr);
198 EXPECT_FALSE(ret);
199
200 devName = "pwm-fan";
201 ret = getDeviceBusAddr(devName, bus, addr);
202 EXPECT_FALSE(ret);
203 }
204
TEST(GetDeviceBusAddrTest,BusInvalid)205 TEST(GetDeviceBusAddrTest, BusInvalid)
206 {
207 size_t bus = 0;
208 size_t addr = 0;
209 std::string devName = "FF-00FF";
210
211 auto ret = getDeviceBusAddr(devName, bus, addr);
212 EXPECT_FALSE(ret);
213 }
214
TEST(GetDeviceBusAddrTest,AddrInvalid)215 TEST(GetDeviceBusAddrTest, AddrInvalid)
216 {
217 size_t bus = 0;
218 size_t addr = 0;
219 std::string devName = "12-fan";
220
221 auto ret = getDeviceBusAddr(devName, bus, addr);
222 EXPECT_FALSE(ret);
223 }
224
TEST(GetDeviceBusAddrTest,I3CBusAddrValid)225 TEST(GetDeviceBusAddrTest, I3CBusAddrValid)
226 {
227 uint64_t bus = 0;
228 uint64_t provisionedId = 0;
229 std::string devName = "0-22400000001";
230
231 auto ret = getDeviceBusAddr(devName, bus, provisionedId);
232 EXPECT_TRUE(ret);
233 EXPECT_EQ(bus, 0);
234 EXPECT_EQ(provisionedId, 0x22400000001);
235 }
236
TEST(GetDeviceBusAddrTest,AllValid)237 TEST(GetDeviceBusAddrTest, AllValid)
238 {
239 size_t bus = 0;
240 size_t addr = 0;
241 std::string devName = "12-00af";
242
243 auto ret = getDeviceBusAddr(devName, bus, addr);
244 EXPECT_TRUE(ret);
245 EXPECT_EQ(bus, 12);
246 EXPECT_EQ(addr, 0xaf);
247 }
248