blob: 957a9e90a3a4cd984acf6a62d2d8585679c74e72 [file] [log] [blame]
Andrew Jefferye73bd0a2023-01-25 10:39:57 +10301#include "Utils.hpp"
Lei YUd3204142021-11-16 14:57:27 +08002
George Liu61984352025-02-24 14:47:34 +08003#include <phosphor-logging/lg2.hpp>
4
Ed Tanous532c8642022-01-14 09:48:15 -08005#include <array>
Ed Tanouseacbfdd2024-04-04 12:00:24 -07006#include <cstddef>
7#include <cstdint>
8#include <cstdlib>
Lei YUd3204142021-11-16 14:57:27 +08009#include <filesystem>
10#include <fstream>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070011#include <new>
12#include <string>
13#include <vector>
Lei YUd3204142021-11-16 14:57:27 +080014
15#include <gtest/gtest.h>
16
Lei YUd3204142021-11-16 14:57:27 +080017class TestUtils : public testing::Test
18{
19 public:
20 std::string testDir;
Ed Tanous2e466962025-01-30 10:59:49 -080021 std::filesystem::path hwmonDir;
22 std::filesystem::path peciDir;
Lei YUd3204142021-11-16 14:57:27 +080023 TestUtils()
24 {
25 // Create test environment
Ed Tanous532c8642022-01-14 09:48:15 -080026 auto dir = std::to_array("./testDirXXXXXX");
27 testDir = mkdtemp(dir.data());
Lei YUd3204142021-11-16 14:57:27 +080028
29 if (testDir.empty())
30 {
31 throw std::bad_alloc();
32 }
Ed Tanous2e466962025-01-30 10:59:49 -080033 hwmonDir = std::filesystem::path(testDir) / "hwmon";
34 std::filesystem::create_directory(hwmonDir);
Lei YUd3204142021-11-16 14:57:27 +080035 auto hwmon10 = hwmonDir / "hwmon10";
Ed Tanous2e466962025-01-30 10:59:49 -080036 std::filesystem::create_directory(hwmonDir / "hwmon10");
Ed Tanousb429f312022-06-27 16:09:53 -070037 {
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 }
Lei YUd3204142021-11-16 14:57:27 +080043 createPECIDir();
44 }
45
46 ~TestUtils() override
47 {
Ed Tanous2e466962025-01-30 10:59:49 -080048 std::filesystem::remove_all(testDir);
Lei YUd3204142021-11-16 14:57:27 +080049 }
50
Ed Tanous65291362022-01-14 09:43:43 -080051 TestUtils(const TestUtils&) = delete;
52 TestUtils(TestUtils&&) = delete;
53 TestUtils& operator=(const TestUtils&) = delete;
54 TestUtils& operator=(TestUtils&&) = delete;
55
Lei YUd3204142021-11-16 14:57:27 +080056 void createPECIDir()
57 {
Ed Tanous2e466962025-01-30 10:59:49 -080058 peciDir = std::filesystem::path(testDir) / "peci";
Patrick Williams779c96a2023-05-10 07:50:42 -050059 auto peci0 = peciDir /
60 "peci-0/device/0-30/peci-cputemp.0/hwmon/hwmon25";
Ed Tanous2e466962025-01-30 10:59:49 -080061 std::filesystem::create_directories(peci0);
Ed Tanousb429f312022-06-27 16:09:53 -070062 {
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 }
Lei YUd3204142021-11-16 14:57:27 +080068 auto devDir = peciDir / "peci-0/peci_dev/peci-0";
Ed Tanous2e466962025-01-30 10:59:49 -080069 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");
Lei YUd3204142021-11-16 14:57:27 +080074
75 // Let's keep this for debugging purpose
Ed Tanous2e466962025-01-30 10:59:49 -080076 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)
Lei YUd3204142021-11-16 14:57:27 +080080 {
81 std::string path = p->path().string();
George Liu61984352025-02-24 14:47:34 +080082 lg2::info("{PATH}", "PATH", path);
Lei YUd3204142021-11-16 14:57:27 +080083 if (p.depth() >= 6)
84 {
85 p.disable_recursion_pending();
86 }
87 }
88 }
89};
90
91TEST_F(TestUtils, findFiles_non_exist)
92{
Ed Tanous2e466962025-01-30 10:59:49 -080093 std::vector<std::filesystem::path> foundPaths;
Lei YUd3204142021-11-16 14:57:27 +080094 auto ret = findFiles("non-exist", "", foundPaths);
95
96 EXPECT_FALSE(ret);
97 EXPECT_TRUE(foundPaths.empty());
98}
99
100TEST_F(TestUtils, findFiles_in_hwmon_no_match)
101{
Ed Tanous2e466962025-01-30 10:59:49 -0800102 std::vector<std::filesystem::path> foundPaths;
Lei YUd3204142021-11-16 14:57:27 +0800103 auto ret = findFiles(hwmonDir, R"(in\d+_input)", foundPaths);
104
105 EXPECT_TRUE(ret);
Ed Tanous2049bd22022-07-09 07:20:26 -0700106 EXPECT_EQ(foundPaths.size(), 0U);
Lei YUd3204142021-11-16 14:57:27 +0800107}
108
109TEST_F(TestUtils, findFiles_in_hwmon_match)
110{
Ed Tanous2e466962025-01-30 10:59:49 -0800111 std::vector<std::filesystem::path> foundPaths;
Lei YUd3204142021-11-16 14:57:27 +0800112 auto ret = findFiles(hwmonDir, R"(temp\d+_input)", foundPaths);
113
114 EXPECT_TRUE(ret);
Ed Tanous2049bd22022-07-09 07:20:26 -0700115 EXPECT_EQ(foundPaths.size(), 2U);
Lei YUd3204142021-11-16 14:57:27 +0800116}
117
118TEST_F(TestUtils, findFiles_in_peci_no_match)
119{
Ed Tanous2e466962025-01-30 10:59:49 -0800120 std::vector<std::filesystem::path> foundPaths;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400121 auto ret =
122 findFiles(peciDir, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/aaa$)",
123 foundPaths, 6);
Lei YUd3204142021-11-16 14:57:27 +0800124
125 EXPECT_TRUE(ret);
126 EXPECT_TRUE(foundPaths.empty());
127}
128
129TEST_F(TestUtils, findFiles_in_peci_match)
130{
Ed Tanous2e466962025-01-30 10:59:49 -0800131 std::vector<std::filesystem::path> foundPaths;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400132 auto ret =
133 findFiles(peciDir, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
134 foundPaths, 6);
Lei YUd3204142021-11-16 14:57:27 +0800135 EXPECT_TRUE(ret);
Ed Tanous2049bd22022-07-09 07:20:26 -0700136 EXPECT_EQ(foundPaths.size(), 1U);
Lei YUd3204142021-11-16 14:57:27 +0800137
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);
Ed Tanous2049bd22022-07-09 07:20:26 -0700144 EXPECT_EQ(foundPaths.size(), 3U);
Lei YUd3204142021-11-16 14:57:27 +0800145}
146
147TEST_F(TestUtils, findFiles_hwmonPath_end_with_slash)
148{
149 std::string p = hwmonDir.string() + "/";
Ed Tanous2e466962025-01-30 10:59:49 -0800150 std::vector<std::filesystem::path> foundPaths;
Lei YUd3204142021-11-16 14:57:27 +0800151 auto ret = findFiles(p, R"(temp\d+_input)", foundPaths);
152
153 EXPECT_TRUE(ret);
Ed Tanous2049bd22022-07-09 07:20:26 -0700154 EXPECT_EQ(foundPaths.size(), 2U);
Lei YUd3204142021-11-16 14:57:27 +0800155}
156
157TEST_F(TestUtils, findFiles_peciPath_end_with_slash)
158{
159 std::string p = peciDir.string() + "/";
Ed Tanous2e466962025-01-30 10:59:49 -0800160 std::vector<std::filesystem::path> foundPaths;
Lei YUd3204142021-11-16 14:57:27 +0800161 auto ret =
162 findFiles(p, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)",
163 foundPaths, 6);
164
165 EXPECT_TRUE(ret);
Ed Tanous2049bd22022-07-09 07:20:26 -0700166 EXPECT_EQ(foundPaths.size(), 3U);
Lei YUd3204142021-11-16 14:57:27 +0800167}
Lei YU0b207a62021-10-20 13:41:51 +0800168
169TEST_F(TestUtils, findFiles_in_sub_peci_match)
170{
Ed Tanous2e466962025-01-30 10:59:49 -0800171 std::vector<std::filesystem::path> foundPaths;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400172 auto ret =
173 findFiles(peciDir / "peci-0", R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
174 foundPaths, 5);
Lei YU0b207a62021-10-20 13:41:51 +0800175 EXPECT_TRUE(ret);
Ed Tanous2049bd22022-07-09 07:20:26 -0700176 EXPECT_EQ(foundPaths.size(), 1U);
Lei YU0b207a62021-10-20 13:41:51 +0800177
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);
Ed Tanous2049bd22022-07-09 07:20:26 -0700184 EXPECT_EQ(foundPaths.size(), 3U);
Lei YU0b207a62021-10-20 13:41:51 +0800185}
Akshit Shah03d333e2023-08-23 22:14:28 +0000186
187TEST(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
205TEST(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
215TEST(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
Tom Tung278e1772023-12-12 09:20:40 +0800225TEST(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
Akshit Shah03d333e2023-08-23 22:14:28 +0000237TEST(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}