1e73bd0a1SAndrew Jeffery #include "Utils.hpp"
2d3204145SLei YU
3532c8649SEd Tanous #include <array>
4eacbfdd1SEd Tanous #include <cstddef>
5eacbfdd1SEd Tanous #include <cstdint>
6eacbfdd1SEd Tanous #include <cstdlib>
7d3204145SLei YU #include <filesystem>
8d3204145SLei YU #include <fstream>
9eacbfdd1SEd Tanous #include <iostream>
10eacbfdd1SEd Tanous #include <new>
11eacbfdd1SEd Tanous #include <string>
12eacbfdd1SEd Tanous #include <vector>
13d3204145SLei YU
14d3204145SLei YU #include <gtest/gtest.h>
15d3204145SLei YU
16d3204145SLei YU namespace fs = std::filesystem;
17d3204145SLei YU class TestUtils : public testing::Test
18d3204145SLei YU {
19d3204145SLei YU public:
20d3204145SLei YU std::string testDir;
21d3204145SLei YU fs::path hwmonDir;
22d3204145SLei YU fs::path peciDir;
TestUtils()23d3204145SLei YU TestUtils()
24d3204145SLei YU {
25d3204145SLei YU // Create test environment
26532c8649SEd Tanous auto dir = std::to_array("./testDirXXXXXX");
27532c8649SEd Tanous testDir = mkdtemp(dir.data());
28d3204145SLei YU
29d3204145SLei YU if (testDir.empty())
30d3204145SLei YU {
31d3204145SLei YU throw std::bad_alloc();
32d3204145SLei YU }
33d3204145SLei YU hwmonDir = fs::path(testDir) / "hwmon";
34d3204145SLei YU fs::create_directory(hwmonDir);
35d3204145SLei YU auto hwmon10 = hwmonDir / "hwmon10";
36d3204145SLei YU fs::create_directory(hwmonDir / "hwmon10");
37b429f31dSEd Tanous {
38b429f31dSEd Tanous std::ofstream temp1Input{hwmon10 / "temp1_input"};
39b429f31dSEd Tanous std::ofstream temp1Min{hwmon10 / "temp1_min"};
40b429f31dSEd Tanous std::ofstream temp1Max{hwmon10 / "temp1_max"};
41b429f31dSEd Tanous std::ofstream temp2Input{hwmon10 / "temp2_input"};
42b429f31dSEd Tanous }
43d3204145SLei YU createPECIDir();
44d3204145SLei YU }
45d3204145SLei YU
~TestUtils()46d3204145SLei YU ~TestUtils() override
47d3204145SLei YU {
48d3204145SLei YU fs::remove_all(testDir);
49d3204145SLei YU }
50d3204145SLei YU
5165291360SEd Tanous TestUtils(const TestUtils&) = delete;
5265291360SEd Tanous TestUtils(TestUtils&&) = delete;
5365291360SEd Tanous TestUtils& operator=(const TestUtils&) = delete;
5465291360SEd Tanous TestUtils& operator=(TestUtils&&) = delete;
5565291360SEd Tanous
createPECIDir()56d3204145SLei YU void createPECIDir()
57d3204145SLei YU {
58d3204145SLei YU peciDir = fs::path(testDir) / "peci";
59779c96a2SPatrick Williams auto peci0 = peciDir /
60779c96a2SPatrick Williams "peci-0/device/0-30/peci-cputemp.0/hwmon/hwmon25";
61d3204145SLei YU fs::create_directories(peci0);
62b429f31dSEd Tanous {
63b429f31dSEd Tanous std::ofstream temp0Input{peci0 / "temp0_input"};
64b429f31dSEd Tanous std::ofstream temp1Input{peci0 / "temp1_input"};
65b429f31dSEd Tanous std::ofstream temp2Input{peci0 / "temp2_input"};
66b429f31dSEd Tanous std::ofstream name{peci0 / "name"};
67b429f31dSEd Tanous }
68d3204145SLei YU auto devDir = peciDir / "peci-0/peci_dev/peci-0";
69d3204145SLei YU fs::create_directories(devDir);
70d3204145SLei YU fs::create_directory_symlink("../../../peci-0", devDir / "device");
71d3204145SLei YU fs::create_directory_symlink("device/0-30", peciDir / "peci-0/0-30");
72d3204145SLei YU
73d3204145SLei YU // Let's keep this for debugging purpose
74d3204145SLei YU for (auto p = fs::recursive_directory_iterator(
75d3204145SLei YU peciDir, fs::directory_options::follow_directory_symlink);
76d3204145SLei YU p != fs::recursive_directory_iterator(); ++p)
77d3204145SLei YU {
78d3204145SLei YU std::string path = p->path().string();
7999c4409aSEd Tanous std::cerr << path << "\n";
80d3204145SLei YU if (p.depth() >= 6)
81d3204145SLei YU {
82d3204145SLei YU p.disable_recursion_pending();
83d3204145SLei YU }
84d3204145SLei YU }
85d3204145SLei YU }
86d3204145SLei YU };
87d3204145SLei YU
TEST_F(TestUtils,findFiles_non_exist)88d3204145SLei YU TEST_F(TestUtils, findFiles_non_exist)
89d3204145SLei YU {
90d3204145SLei YU std::vector<fs::path> foundPaths;
91d3204145SLei YU auto ret = findFiles("non-exist", "", foundPaths);
92d3204145SLei YU
93d3204145SLei YU EXPECT_FALSE(ret);
94d3204145SLei YU EXPECT_TRUE(foundPaths.empty());
95d3204145SLei YU }
96d3204145SLei YU
TEST_F(TestUtils,findFiles_in_hwmon_no_match)97d3204145SLei YU TEST_F(TestUtils, findFiles_in_hwmon_no_match)
98d3204145SLei YU {
99d3204145SLei YU std::vector<fs::path> foundPaths;
100d3204145SLei YU auto ret = findFiles(hwmonDir, R"(in\d+_input)", foundPaths);
101d3204145SLei YU
102d3204145SLei YU EXPECT_TRUE(ret);
1032049bd26SEd Tanous EXPECT_EQ(foundPaths.size(), 0U);
104d3204145SLei YU }
105d3204145SLei YU
TEST_F(TestUtils,findFiles_in_hwmon_match)106d3204145SLei YU TEST_F(TestUtils, findFiles_in_hwmon_match)
107d3204145SLei YU {
108d3204145SLei YU std::vector<fs::path> foundPaths;
109d3204145SLei YU auto ret = findFiles(hwmonDir, R"(temp\d+_input)", foundPaths);
110d3204145SLei YU
111d3204145SLei YU EXPECT_TRUE(ret);
1122049bd26SEd Tanous EXPECT_EQ(foundPaths.size(), 2U);
113d3204145SLei YU }
114d3204145SLei YU
TEST_F(TestUtils,findFiles_in_peci_no_match)115d3204145SLei YU TEST_F(TestUtils, findFiles_in_peci_no_match)
116d3204145SLei YU {
117d3204145SLei YU std::vector<fs::path> foundPaths;
118*2aaf7175SPatrick Williams auto ret =
119*2aaf7175SPatrick Williams findFiles(peciDir, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/aaa$)",
120d3204145SLei YU foundPaths, 6);
121d3204145SLei YU
122d3204145SLei YU EXPECT_TRUE(ret);
123d3204145SLei YU EXPECT_TRUE(foundPaths.empty());
124d3204145SLei YU }
125d3204145SLei YU
TEST_F(TestUtils,findFiles_in_peci_match)126d3204145SLei YU TEST_F(TestUtils, findFiles_in_peci_match)
127d3204145SLei YU {
128d3204145SLei YU std::vector<fs::path> foundPaths;
129*2aaf7175SPatrick Williams auto ret =
130*2aaf7175SPatrick Williams findFiles(peciDir, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
131d3204145SLei YU foundPaths, 6);
132d3204145SLei YU EXPECT_TRUE(ret);
1332049bd26SEd Tanous EXPECT_EQ(foundPaths.size(), 1U);
134d3204145SLei YU
135d3204145SLei YU foundPaths.clear();
136d3204145SLei YU
137d3204145SLei YU ret = findFiles(peciDir,
138d3204145SLei YU R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)",
139d3204145SLei YU foundPaths, 6);
140d3204145SLei YU EXPECT_TRUE(ret);
1412049bd26SEd Tanous EXPECT_EQ(foundPaths.size(), 3U);
142d3204145SLei YU }
143d3204145SLei YU
TEST_F(TestUtils,findFiles_hwmonPath_end_with_slash)144d3204145SLei YU TEST_F(TestUtils, findFiles_hwmonPath_end_with_slash)
145d3204145SLei YU {
146d3204145SLei YU std::string p = hwmonDir.string() + "/";
147d3204145SLei YU std::vector<fs::path> foundPaths;
148d3204145SLei YU auto ret = findFiles(p, R"(temp\d+_input)", foundPaths);
149d3204145SLei YU
150d3204145SLei YU EXPECT_TRUE(ret);
1512049bd26SEd Tanous EXPECT_EQ(foundPaths.size(), 2U);
152d3204145SLei YU }
153d3204145SLei YU
TEST_F(TestUtils,findFiles_peciPath_end_with_slash)154d3204145SLei YU TEST_F(TestUtils, findFiles_peciPath_end_with_slash)
155d3204145SLei YU {
156d3204145SLei YU std::string p = peciDir.string() + "/";
157d3204145SLei YU std::vector<fs::path> foundPaths;
158d3204145SLei YU auto ret =
159d3204145SLei YU findFiles(p, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)",
160d3204145SLei YU foundPaths, 6);
161d3204145SLei YU
162d3204145SLei YU EXPECT_TRUE(ret);
1632049bd26SEd Tanous EXPECT_EQ(foundPaths.size(), 3U);
164d3204145SLei YU }
1650b207a62SLei YU
TEST_F(TestUtils,findFiles_in_sub_peci_match)1660b207a62SLei YU TEST_F(TestUtils, findFiles_in_sub_peci_match)
1670b207a62SLei YU {
1680b207a62SLei YU std::vector<fs::path> foundPaths;
169*2aaf7175SPatrick Williams auto ret =
170*2aaf7175SPatrick Williams findFiles(peciDir / "peci-0", R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
171*2aaf7175SPatrick Williams foundPaths, 5);
1720b207a62SLei YU EXPECT_TRUE(ret);
1732049bd26SEd Tanous EXPECT_EQ(foundPaths.size(), 1U);
1740b207a62SLei YU
1750b207a62SLei YU foundPaths.clear();
1760b207a62SLei YU
1770b207a62SLei YU ret = findFiles(peciDir / "peci-0",
1780b207a62SLei YU R"(\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)",
1790b207a62SLei YU foundPaths, 5);
1800b207a62SLei YU EXPECT_TRUE(ret);
1812049bd26SEd Tanous EXPECT_EQ(foundPaths.size(), 3U);
1820b207a62SLei YU }
18303d333e0SAkshit Shah
TEST(GetDeviceBusAddrTest,DevNameInvalid)18403d333e0SAkshit Shah TEST(GetDeviceBusAddrTest, DevNameInvalid)
18503d333e0SAkshit Shah {
18603d333e0SAkshit Shah size_t bus = 0;
18703d333e0SAkshit Shah size_t addr = 0;
18803d333e0SAkshit Shah std::string devName;
18903d333e0SAkshit Shah
19003d333e0SAkshit Shah auto ret = getDeviceBusAddr(devName, bus, addr);
19103d333e0SAkshit Shah EXPECT_FALSE(ret);
19203d333e0SAkshit Shah
19303d333e0SAkshit Shah devName = "NoHyphen";
19403d333e0SAkshit Shah ret = getDeviceBusAddr(devName, bus, addr);
19503d333e0SAkshit Shah EXPECT_FALSE(ret);
19603d333e0SAkshit Shah
19703d333e0SAkshit Shah devName = "pwm-fan";
19803d333e0SAkshit Shah ret = getDeviceBusAddr(devName, bus, addr);
19903d333e0SAkshit Shah EXPECT_FALSE(ret);
20003d333e0SAkshit Shah }
20103d333e0SAkshit Shah
TEST(GetDeviceBusAddrTest,BusInvalid)20203d333e0SAkshit Shah TEST(GetDeviceBusAddrTest, BusInvalid)
20303d333e0SAkshit Shah {
20403d333e0SAkshit Shah size_t bus = 0;
20503d333e0SAkshit Shah size_t addr = 0;
20603d333e0SAkshit Shah std::string devName = "FF-00FF";
20703d333e0SAkshit Shah
20803d333e0SAkshit Shah auto ret = getDeviceBusAddr(devName, bus, addr);
20903d333e0SAkshit Shah EXPECT_FALSE(ret);
21003d333e0SAkshit Shah }
21103d333e0SAkshit Shah
TEST(GetDeviceBusAddrTest,AddrInvalid)21203d333e0SAkshit Shah TEST(GetDeviceBusAddrTest, AddrInvalid)
21303d333e0SAkshit Shah {
21403d333e0SAkshit Shah size_t bus = 0;
21503d333e0SAkshit Shah size_t addr = 0;
21603d333e0SAkshit Shah std::string devName = "12-fan";
21703d333e0SAkshit Shah
21803d333e0SAkshit Shah auto ret = getDeviceBusAddr(devName, bus, addr);
21903d333e0SAkshit Shah EXPECT_FALSE(ret);
22003d333e0SAkshit Shah }
22103d333e0SAkshit Shah
TEST(GetDeviceBusAddrTest,I3CBusAddrValid)222278e177fSTom Tung TEST(GetDeviceBusAddrTest, I3CBusAddrValid)
223278e177fSTom Tung {
224278e177fSTom Tung uint64_t bus = 0;
225278e177fSTom Tung uint64_t provisionedId = 0;
226278e177fSTom Tung std::string devName = "0-22400000001";
227278e177fSTom Tung
228278e177fSTom Tung auto ret = getDeviceBusAddr(devName, bus, provisionedId);
229278e177fSTom Tung EXPECT_TRUE(ret);
230278e177fSTom Tung EXPECT_EQ(bus, 0);
231278e177fSTom Tung EXPECT_EQ(provisionedId, 0x22400000001);
232278e177fSTom Tung }
233278e177fSTom Tung
TEST(GetDeviceBusAddrTest,AllValid)23403d333e0SAkshit Shah TEST(GetDeviceBusAddrTest, AllValid)
23503d333e0SAkshit Shah {
23603d333e0SAkshit Shah size_t bus = 0;
23703d333e0SAkshit Shah size_t addr = 0;
23803d333e0SAkshit Shah std::string devName = "12-00af";
23903d333e0SAkshit Shah
24003d333e0SAkshit Shah auto ret = getDeviceBusAddr(devName, bus, addr);
24103d333e0SAkshit Shah EXPECT_TRUE(ret);
24203d333e0SAkshit Shah EXPECT_EQ(bus, 12);
24303d333e0SAkshit Shah EXPECT_EQ(addr, 0xaf);
24403d333e0SAkshit Shah }
245