blob: 59a4e1307f3176345445d01130306d31bf5d3cdf [file] [log] [blame]
Andrew Jefferye73bd0a2023-01-25 10:39:57 +10301#include "Utils.hpp"
Lei YUd3204142021-11-16 14:57:27 +08002
Ed Tanous532c8642022-01-14 09:48:15 -08003#include <array>
Lei YUd3204142021-11-16 14:57:27 +08004#include <filesystem>
5#include <fstream>
6
7#include <gtest/gtest.h>
8
9namespace fs = std::filesystem;
10class 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
Ed Tanous532c8642022-01-14 09:48:15 -080019 auto dir = std::to_array("./testDirXXXXXX");
20 testDir = mkdtemp(dir.data());
Lei YUd3204142021-11-16 14:57:27 +080021
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");
Ed Tanousb429f312022-06-27 16:09:53 -070030 {
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 }
Lei YUd3204142021-11-16 14:57:27 +080036 createPECIDir();
37 }
38
39 ~TestUtils() override
40 {
41 fs::remove_all(testDir);
42 }
43
Ed Tanous65291362022-01-14 09:43:43 -080044 TestUtils(const TestUtils&) = delete;
45 TestUtils(TestUtils&&) = delete;
46 TestUtils& operator=(const TestUtils&) = delete;
47 TestUtils& operator=(TestUtils&&) = delete;
48
Lei YUd3204142021-11-16 14:57:27 +080049 void createPECIDir()
50 {
51 peciDir = fs::path(testDir) / "peci";
Patrick Williams779c96a2023-05-10 07:50:42 -050052 auto peci0 = peciDir /
53 "peci-0/device/0-30/peci-cputemp.0/hwmon/hwmon25";
Lei YUd3204142021-11-16 14:57:27 +080054 fs::create_directories(peci0);
Ed Tanousb429f312022-06-27 16:09:53 -070055 {
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 }
Lei YUd3204142021-11-16 14:57:27 +080061 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();
Ed Tanous99c44092022-01-14 09:59:09 -080072 std::cerr << path << "\n";
Lei YUd3204142021-11-16 14:57:27 +080073 if (p.depth() >= 6)
74 {
75 p.disable_recursion_pending();
76 }
77 }
78 }
79};
80
81TEST_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
90TEST_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);
Ed Tanous2049bd22022-07-09 07:20:26 -070096 EXPECT_EQ(foundPaths.size(), 0U);
Lei YUd3204142021-11-16 14:57:27 +080097}
98
99TEST_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);
Ed Tanous2049bd22022-07-09 07:20:26 -0700105 EXPECT_EQ(foundPaths.size(), 2U);
Lei YUd3204142021-11-16 14:57:27 +0800106}
107
108TEST_F(TestUtils, findFiles_in_peci_no_match)
109{
110 std::vector<fs::path> foundPaths;
Patrick Williams779c96a2023-05-10 07:50:42 -0500111 auto ret = findFiles(peciDir,
112 R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/aaa$)",
113 foundPaths, 6);
Lei YUd3204142021-11-16 14:57:27 +0800114
115 EXPECT_TRUE(ret);
116 EXPECT_TRUE(foundPaths.empty());
117}
118
119TEST_F(TestUtils, findFiles_in_peci_match)
120{
121 std::vector<fs::path> foundPaths;
Patrick Williams779c96a2023-05-10 07:50:42 -0500122 auto ret = findFiles(peciDir,
123 R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
124 foundPaths, 6);
Lei YUd3204142021-11-16 14:57:27 +0800125 EXPECT_TRUE(ret);
Ed Tanous2049bd22022-07-09 07:20:26 -0700126 EXPECT_EQ(foundPaths.size(), 1U);
Lei YUd3204142021-11-16 14:57:27 +0800127
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);
Ed Tanous2049bd22022-07-09 07:20:26 -0700134 EXPECT_EQ(foundPaths.size(), 3U);
Lei YUd3204142021-11-16 14:57:27 +0800135}
136
137TEST_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);
Ed Tanous2049bd22022-07-09 07:20:26 -0700144 EXPECT_EQ(foundPaths.size(), 2U);
Lei YUd3204142021-11-16 14:57:27 +0800145}
146
147TEST_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);
Ed Tanous2049bd22022-07-09 07:20:26 -0700156 EXPECT_EQ(foundPaths.size(), 3U);
Lei YUd3204142021-11-16 14:57:27 +0800157}
Lei YU0b207a62021-10-20 13:41:51 +0800158
159TEST_F(TestUtils, findFiles_in_sub_peci_match)
160{
161 std::vector<fs::path> foundPaths;
Patrick Williams779c96a2023-05-10 07:50:42 -0500162 auto ret = findFiles(peciDir / "peci-0",
163 R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", foundPaths,
164 5);
Lei YU0b207a62021-10-20 13:41:51 +0800165 EXPECT_TRUE(ret);
Ed Tanous2049bd22022-07-09 07:20:26 -0700166 EXPECT_EQ(foundPaths.size(), 1U);
Lei YU0b207a62021-10-20 13:41:51 +0800167
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);
Ed Tanous2049bd22022-07-09 07:20:26 -0700174 EXPECT_EQ(foundPaths.size(), 3U);
Lei YU0b207a62021-10-20 13:41:51 +0800175}
Akshit Shah03d333e2023-08-23 22:14:28 +0000176
177TEST(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
195TEST(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
205TEST(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
Tom Tung278e1772023-12-12 09:20:40 +0800215TEST(GetDeviceBusAddrTest, I3CBusAddrValid)
216{
217 uint64_t bus = 0;
218 uint64_t provisionedId = 0;
219 std::string devName = "0-22400000001";
220
221 auto ret = getDeviceBusAddr(devName, bus, provisionedId);
222 EXPECT_TRUE(ret);
223 EXPECT_EQ(bus, 0);
224 EXPECT_EQ(provisionedId, 0x22400000001);
225}
226
Akshit Shah03d333e2023-08-23 22:14:28 +0000227TEST(GetDeviceBusAddrTest, AllValid)
228{
229 size_t bus = 0;
230 size_t addr = 0;
231 std::string devName = "12-00af";
232
233 auto ret = getDeviceBusAddr(devName, bus, addr);
234 EXPECT_TRUE(ret);
235 EXPECT_EQ(bus, 12);
236 EXPECT_EQ(addr, 0xaf);
237}