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