blob: 3db67275cd4ea96af6097f1735b50348888a9f2f [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>
Ed Tanouseacbfdd2024-04-04 12:00:24 -07004#include <cstddef>
5#include <cstdint>
6#include <cstdlib>
Lei YUd3204142021-11-16 14:57:27 +08007#include <filesystem>
8#include <fstream>
Ed Tanouseacbfdd2024-04-04 12:00:24 -07009#include <iostream>
10#include <new>
11#include <string>
12#include <vector>
Lei YUd3204142021-11-16 14:57:27 +080013
14#include <gtest/gtest.h>
15
Lei YUd3204142021-11-16 14:57:27 +080016class TestUtils : public testing::Test
17{
18 public:
19 std::string testDir;
Ed Tanous2e466962025-01-30 10:59:49 -080020 std::filesystem::path hwmonDir;
21 std::filesystem::path peciDir;
Lei YUd3204142021-11-16 14:57:27 +080022 TestUtils()
23 {
24 // Create test environment
Ed Tanous532c8642022-01-14 09:48:15 -080025 auto dir = std::to_array("./testDirXXXXXX");
26 testDir = mkdtemp(dir.data());
Lei YUd3204142021-11-16 14:57:27 +080027
28 if (testDir.empty())
29 {
30 throw std::bad_alloc();
31 }
Ed Tanous2e466962025-01-30 10:59:49 -080032 hwmonDir = std::filesystem::path(testDir) / "hwmon";
33 std::filesystem::create_directory(hwmonDir);
Lei YUd3204142021-11-16 14:57:27 +080034 auto hwmon10 = hwmonDir / "hwmon10";
Ed Tanous2e466962025-01-30 10:59:49 -080035 std::filesystem::create_directory(hwmonDir / "hwmon10");
Ed Tanousb429f312022-06-27 16:09:53 -070036 {
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 }
Lei YUd3204142021-11-16 14:57:27 +080042 createPECIDir();
43 }
44
45 ~TestUtils() override
46 {
Ed Tanous2e466962025-01-30 10:59:49 -080047 std::filesystem::remove_all(testDir);
Lei YUd3204142021-11-16 14:57:27 +080048 }
49
Ed Tanous65291362022-01-14 09:43:43 -080050 TestUtils(const TestUtils&) = delete;
51 TestUtils(TestUtils&&) = delete;
52 TestUtils& operator=(const TestUtils&) = delete;
53 TestUtils& operator=(TestUtils&&) = delete;
54
Lei YUd3204142021-11-16 14:57:27 +080055 void createPECIDir()
56 {
Ed Tanous2e466962025-01-30 10:59:49 -080057 peciDir = std::filesystem::path(testDir) / "peci";
Patrick Williams779c96a2023-05-10 07:50:42 -050058 auto peci0 = peciDir /
59 "peci-0/device/0-30/peci-cputemp.0/hwmon/hwmon25";
Ed Tanous2e466962025-01-30 10:59:49 -080060 std::filesystem::create_directories(peci0);
Ed Tanousb429f312022-06-27 16:09:53 -070061 {
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 }
Lei YUd3204142021-11-16 14:57:27 +080067 auto devDir = peciDir / "peci-0/peci_dev/peci-0";
Ed Tanous2e466962025-01-30 10:59:49 -080068 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");
Lei YUd3204142021-11-16 14:57:27 +080073
74 // Let's keep this for debugging purpose
Ed Tanous2e466962025-01-30 10:59:49 -080075 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)
Lei YUd3204142021-11-16 14:57:27 +080079 {
80 std::string path = p->path().string();
Ed Tanous99c44092022-01-14 09:59:09 -080081 std::cerr << path << "\n";
Lei YUd3204142021-11-16 14:57:27 +080082 if (p.depth() >= 6)
83 {
84 p.disable_recursion_pending();
85 }
86 }
87 }
88};
89
90TEST_F(TestUtils, findFiles_non_exist)
91{
Ed Tanous2e466962025-01-30 10:59:49 -080092 std::vector<std::filesystem::path> foundPaths;
Lei YUd3204142021-11-16 14:57:27 +080093 auto ret = findFiles("non-exist", "", foundPaths);
94
95 EXPECT_FALSE(ret);
96 EXPECT_TRUE(foundPaths.empty());
97}
98
99TEST_F(TestUtils, findFiles_in_hwmon_no_match)
100{
Ed Tanous2e466962025-01-30 10:59:49 -0800101 std::vector<std::filesystem::path> foundPaths;
Lei YUd3204142021-11-16 14:57:27 +0800102 auto ret = findFiles(hwmonDir, R"(in\d+_input)", foundPaths);
103
104 EXPECT_TRUE(ret);
Ed Tanous2049bd22022-07-09 07:20:26 -0700105 EXPECT_EQ(foundPaths.size(), 0U);
Lei YUd3204142021-11-16 14:57:27 +0800106}
107
108TEST_F(TestUtils, findFiles_in_hwmon_match)
109{
Ed Tanous2e466962025-01-30 10:59:49 -0800110 std::vector<std::filesystem::path> foundPaths;
Lei YUd3204142021-11-16 14:57:27 +0800111 auto ret = findFiles(hwmonDir, R"(temp\d+_input)", foundPaths);
112
113 EXPECT_TRUE(ret);
Ed Tanous2049bd22022-07-09 07:20:26 -0700114 EXPECT_EQ(foundPaths.size(), 2U);
Lei YUd3204142021-11-16 14:57:27 +0800115}
116
117TEST_F(TestUtils, findFiles_in_peci_no_match)
118{
Ed Tanous2e466962025-01-30 10:59:49 -0800119 std::vector<std::filesystem::path> foundPaths;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400120 auto ret =
121 findFiles(peciDir, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/aaa$)",
122 foundPaths, 6);
Lei YUd3204142021-11-16 14:57:27 +0800123
124 EXPECT_TRUE(ret);
125 EXPECT_TRUE(foundPaths.empty());
126}
127
128TEST_F(TestUtils, findFiles_in_peci_match)
129{
Ed Tanous2e466962025-01-30 10:59:49 -0800130 std::vector<std::filesystem::path> foundPaths;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400131 auto ret =
132 findFiles(peciDir, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
133 foundPaths, 6);
Lei YUd3204142021-11-16 14:57:27 +0800134 EXPECT_TRUE(ret);
Ed Tanous2049bd22022-07-09 07:20:26 -0700135 EXPECT_EQ(foundPaths.size(), 1U);
Lei YUd3204142021-11-16 14:57:27 +0800136
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);
Ed Tanous2049bd22022-07-09 07:20:26 -0700143 EXPECT_EQ(foundPaths.size(), 3U);
Lei YUd3204142021-11-16 14:57:27 +0800144}
145
146TEST_F(TestUtils, findFiles_hwmonPath_end_with_slash)
147{
148 std::string p = hwmonDir.string() + "/";
Ed Tanous2e466962025-01-30 10:59:49 -0800149 std::vector<std::filesystem::path> foundPaths;
Lei YUd3204142021-11-16 14:57:27 +0800150 auto ret = findFiles(p, R"(temp\d+_input)", foundPaths);
151
152 EXPECT_TRUE(ret);
Ed Tanous2049bd22022-07-09 07:20:26 -0700153 EXPECT_EQ(foundPaths.size(), 2U);
Lei YUd3204142021-11-16 14:57:27 +0800154}
155
156TEST_F(TestUtils, findFiles_peciPath_end_with_slash)
157{
158 std::string p = peciDir.string() + "/";
Ed Tanous2e466962025-01-30 10:59:49 -0800159 std::vector<std::filesystem::path> foundPaths;
Lei YUd3204142021-11-16 14:57:27 +0800160 auto ret =
161 findFiles(p, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)",
162 foundPaths, 6);
163
164 EXPECT_TRUE(ret);
Ed Tanous2049bd22022-07-09 07:20:26 -0700165 EXPECT_EQ(foundPaths.size(), 3U);
Lei YUd3204142021-11-16 14:57:27 +0800166}
Lei YU0b207a62021-10-20 13:41:51 +0800167
168TEST_F(TestUtils, findFiles_in_sub_peci_match)
169{
Ed Tanous2e466962025-01-30 10:59:49 -0800170 std::vector<std::filesystem::path> foundPaths;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400171 auto ret =
172 findFiles(peciDir / "peci-0", R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
173 foundPaths, 5);
Lei YU0b207a62021-10-20 13:41:51 +0800174 EXPECT_TRUE(ret);
Ed Tanous2049bd22022-07-09 07:20:26 -0700175 EXPECT_EQ(foundPaths.size(), 1U);
Lei YU0b207a62021-10-20 13:41:51 +0800176
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);
Ed Tanous2049bd22022-07-09 07:20:26 -0700183 EXPECT_EQ(foundPaths.size(), 3U);
Lei YU0b207a62021-10-20 13:41:51 +0800184}
Akshit Shah03d333e2023-08-23 22:14:28 +0000185
186TEST(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
204TEST(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
214TEST(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
Tom Tung278e1772023-12-12 09:20:40 +0800224TEST(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
Akshit Shah03d333e2023-08-23 22:14:28 +0000236TEST(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}