xref: /openbmc/dbus-sensors/tests/test_Utils.cpp (revision 278e177f)
1e73bd0a1SAndrew Jeffery #include "Utils.hpp"
2d3204145SLei YU 
3532c8649SEd Tanous #include <array>
4d3204145SLei YU #include <filesystem>
5d3204145SLei YU #include <fstream>
6d3204145SLei YU 
7d3204145SLei YU #include <gtest/gtest.h>
8d3204145SLei YU 
9d3204145SLei YU namespace fs = std::filesystem;
10d3204145SLei YU class TestUtils : public testing::Test
11d3204145SLei YU {
12d3204145SLei YU   public:
13d3204145SLei YU     std::string testDir;
14d3204145SLei YU     fs::path hwmonDir;
15d3204145SLei YU     fs::path peciDir;
16d3204145SLei YU     TestUtils()
17d3204145SLei YU     {
18d3204145SLei YU         // Create test environment
19532c8649SEd Tanous         auto dir = std::to_array("./testDirXXXXXX");
20532c8649SEd Tanous         testDir = mkdtemp(dir.data());
21d3204145SLei YU 
22d3204145SLei YU         if (testDir.empty())
23d3204145SLei YU         {
24d3204145SLei YU             throw std::bad_alloc();
25d3204145SLei YU         }
26d3204145SLei YU         hwmonDir = fs::path(testDir) / "hwmon";
27d3204145SLei YU         fs::create_directory(hwmonDir);
28d3204145SLei YU         auto hwmon10 = hwmonDir / "hwmon10";
29d3204145SLei YU         fs::create_directory(hwmonDir / "hwmon10");
30b429f31dSEd Tanous         {
31b429f31dSEd Tanous             std::ofstream temp1Input{hwmon10 / "temp1_input"};
32b429f31dSEd Tanous             std::ofstream temp1Min{hwmon10 / "temp1_min"};
33b429f31dSEd Tanous             std::ofstream temp1Max{hwmon10 / "temp1_max"};
34b429f31dSEd Tanous             std::ofstream temp2Input{hwmon10 / "temp2_input"};
35b429f31dSEd Tanous         }
36d3204145SLei YU         createPECIDir();
37d3204145SLei YU     }
38d3204145SLei YU 
39d3204145SLei YU     ~TestUtils() override
40d3204145SLei YU     {
41d3204145SLei YU         fs::remove_all(testDir);
42d3204145SLei YU     }
43d3204145SLei YU 
4465291360SEd Tanous     TestUtils(const TestUtils&) = delete;
4565291360SEd Tanous     TestUtils(TestUtils&&) = delete;
4665291360SEd Tanous     TestUtils& operator=(const TestUtils&) = delete;
4765291360SEd Tanous     TestUtils& operator=(TestUtils&&) = delete;
4865291360SEd Tanous 
49d3204145SLei YU     void createPECIDir()
50d3204145SLei YU     {
51d3204145SLei YU         peciDir = fs::path(testDir) / "peci";
52779c96a2SPatrick Williams         auto peci0 = peciDir /
53779c96a2SPatrick Williams                      "peci-0/device/0-30/peci-cputemp.0/hwmon/hwmon25";
54d3204145SLei YU         fs::create_directories(peci0);
55b429f31dSEd Tanous         {
56b429f31dSEd Tanous             std::ofstream temp0Input{peci0 / "temp0_input"};
57b429f31dSEd Tanous             std::ofstream temp1Input{peci0 / "temp1_input"};
58b429f31dSEd Tanous             std::ofstream temp2Input{peci0 / "temp2_input"};
59b429f31dSEd Tanous             std::ofstream name{peci0 / "name"};
60b429f31dSEd Tanous         }
61d3204145SLei YU         auto devDir = peciDir / "peci-0/peci_dev/peci-0";
62d3204145SLei YU         fs::create_directories(devDir);
63d3204145SLei YU         fs::create_directory_symlink("../../../peci-0", devDir / "device");
64d3204145SLei YU         fs::create_directory_symlink("device/0-30", peciDir / "peci-0/0-30");
65d3204145SLei YU 
66d3204145SLei YU         // Let's keep this for debugging purpose
67d3204145SLei YU         for (auto p = fs::recursive_directory_iterator(
68d3204145SLei YU                  peciDir, fs::directory_options::follow_directory_symlink);
69d3204145SLei YU              p != fs::recursive_directory_iterator(); ++p)
70d3204145SLei YU         {
71d3204145SLei YU             std::string path = p->path().string();
7299c4409aSEd Tanous             std::cerr << path << "\n";
73d3204145SLei YU             if (p.depth() >= 6)
74d3204145SLei YU             {
75d3204145SLei YU                 p.disable_recursion_pending();
76d3204145SLei YU             }
77d3204145SLei YU         }
78d3204145SLei YU     }
79d3204145SLei YU };
80d3204145SLei YU 
81d3204145SLei YU TEST_F(TestUtils, findFiles_non_exist)
82d3204145SLei YU {
83d3204145SLei YU     std::vector<fs::path> foundPaths;
84d3204145SLei YU     auto ret = findFiles("non-exist", "", foundPaths);
85d3204145SLei YU 
86d3204145SLei YU     EXPECT_FALSE(ret);
87d3204145SLei YU     EXPECT_TRUE(foundPaths.empty());
88d3204145SLei YU }
89d3204145SLei YU 
90d3204145SLei YU TEST_F(TestUtils, findFiles_in_hwmon_no_match)
91d3204145SLei YU {
92d3204145SLei YU     std::vector<fs::path> foundPaths;
93d3204145SLei YU     auto ret = findFiles(hwmonDir, R"(in\d+_input)", foundPaths);
94d3204145SLei YU 
95d3204145SLei YU     EXPECT_TRUE(ret);
962049bd26SEd Tanous     EXPECT_EQ(foundPaths.size(), 0U);
97d3204145SLei YU }
98d3204145SLei YU 
99d3204145SLei YU TEST_F(TestUtils, findFiles_in_hwmon_match)
100d3204145SLei YU {
101d3204145SLei YU     std::vector<fs::path> foundPaths;
102d3204145SLei YU     auto ret = findFiles(hwmonDir, R"(temp\d+_input)", foundPaths);
103d3204145SLei YU 
104d3204145SLei YU     EXPECT_TRUE(ret);
1052049bd26SEd Tanous     EXPECT_EQ(foundPaths.size(), 2U);
106d3204145SLei YU }
107d3204145SLei YU 
108d3204145SLei YU TEST_F(TestUtils, findFiles_in_peci_no_match)
109d3204145SLei YU {
110d3204145SLei YU     std::vector<fs::path> foundPaths;
111779c96a2SPatrick Williams     auto ret = findFiles(peciDir,
112779c96a2SPatrick Williams                          R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/aaa$)",
113d3204145SLei YU                          foundPaths, 6);
114d3204145SLei YU 
115d3204145SLei YU     EXPECT_TRUE(ret);
116d3204145SLei YU     EXPECT_TRUE(foundPaths.empty());
117d3204145SLei YU }
118d3204145SLei YU 
119d3204145SLei YU TEST_F(TestUtils, findFiles_in_peci_match)
120d3204145SLei YU {
121d3204145SLei YU     std::vector<fs::path> foundPaths;
122779c96a2SPatrick Williams     auto ret = findFiles(peciDir,
123779c96a2SPatrick Williams                          R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
124d3204145SLei YU                          foundPaths, 6);
125d3204145SLei YU     EXPECT_TRUE(ret);
1262049bd26SEd Tanous     EXPECT_EQ(foundPaths.size(), 1U);
127d3204145SLei YU 
128d3204145SLei YU     foundPaths.clear();
129d3204145SLei YU 
130d3204145SLei YU     ret = findFiles(peciDir,
131d3204145SLei YU                     R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)",
132d3204145SLei YU                     foundPaths, 6);
133d3204145SLei YU     EXPECT_TRUE(ret);
1342049bd26SEd Tanous     EXPECT_EQ(foundPaths.size(), 3U);
135d3204145SLei YU }
136d3204145SLei YU 
137d3204145SLei YU TEST_F(TestUtils, findFiles_hwmonPath_end_with_slash)
138d3204145SLei YU {
139d3204145SLei YU     std::string p = hwmonDir.string() + "/";
140d3204145SLei YU     std::vector<fs::path> foundPaths;
141d3204145SLei YU     auto ret = findFiles(p, R"(temp\d+_input)", foundPaths);
142d3204145SLei YU 
143d3204145SLei YU     EXPECT_TRUE(ret);
1442049bd26SEd Tanous     EXPECT_EQ(foundPaths.size(), 2U);
145d3204145SLei YU }
146d3204145SLei YU 
147d3204145SLei YU TEST_F(TestUtils, findFiles_peciPath_end_with_slash)
148d3204145SLei YU {
149d3204145SLei YU     std::string p = peciDir.string() + "/";
150d3204145SLei YU     std::vector<fs::path> foundPaths;
151d3204145SLei YU     auto ret =
152d3204145SLei YU         findFiles(p, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)",
153d3204145SLei YU                   foundPaths, 6);
154d3204145SLei YU 
155d3204145SLei YU     EXPECT_TRUE(ret);
1562049bd26SEd Tanous     EXPECT_EQ(foundPaths.size(), 3U);
157d3204145SLei YU }
1580b207a62SLei YU 
1590b207a62SLei YU TEST_F(TestUtils, findFiles_in_sub_peci_match)
1600b207a62SLei YU {
1610b207a62SLei YU     std::vector<fs::path> foundPaths;
162779c96a2SPatrick Williams     auto ret = findFiles(peciDir / "peci-0",
163779c96a2SPatrick Williams                          R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", foundPaths,
164779c96a2SPatrick Williams                          5);
1650b207a62SLei YU     EXPECT_TRUE(ret);
1662049bd26SEd Tanous     EXPECT_EQ(foundPaths.size(), 1U);
1670b207a62SLei YU 
1680b207a62SLei YU     foundPaths.clear();
1690b207a62SLei YU 
1700b207a62SLei YU     ret = findFiles(peciDir / "peci-0",
1710b207a62SLei YU                     R"(\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)",
1720b207a62SLei YU                     foundPaths, 5);
1730b207a62SLei YU     EXPECT_TRUE(ret);
1742049bd26SEd Tanous     EXPECT_EQ(foundPaths.size(), 3U);
1750b207a62SLei YU }
17603d333e0SAkshit Shah 
17703d333e0SAkshit Shah TEST(GetDeviceBusAddrTest, DevNameInvalid)
17803d333e0SAkshit Shah {
17903d333e0SAkshit Shah     size_t bus = 0;
18003d333e0SAkshit Shah     size_t addr = 0;
18103d333e0SAkshit Shah     std::string devName;
18203d333e0SAkshit Shah 
18303d333e0SAkshit Shah     auto ret = getDeviceBusAddr(devName, bus, addr);
18403d333e0SAkshit Shah     EXPECT_FALSE(ret);
18503d333e0SAkshit Shah 
18603d333e0SAkshit Shah     devName = "NoHyphen";
18703d333e0SAkshit Shah     ret = getDeviceBusAddr(devName, bus, addr);
18803d333e0SAkshit Shah     EXPECT_FALSE(ret);
18903d333e0SAkshit Shah 
19003d333e0SAkshit Shah     devName = "pwm-fan";
19103d333e0SAkshit Shah     ret = getDeviceBusAddr(devName, bus, addr);
19203d333e0SAkshit Shah     EXPECT_FALSE(ret);
19303d333e0SAkshit Shah }
19403d333e0SAkshit Shah 
19503d333e0SAkshit Shah TEST(GetDeviceBusAddrTest, BusInvalid)
19603d333e0SAkshit Shah {
19703d333e0SAkshit Shah     size_t bus = 0;
19803d333e0SAkshit Shah     size_t addr = 0;
19903d333e0SAkshit Shah     std::string devName = "FF-00FF";
20003d333e0SAkshit Shah 
20103d333e0SAkshit Shah     auto ret = getDeviceBusAddr(devName, bus, addr);
20203d333e0SAkshit Shah     EXPECT_FALSE(ret);
20303d333e0SAkshit Shah }
20403d333e0SAkshit Shah 
20503d333e0SAkshit Shah TEST(GetDeviceBusAddrTest, AddrInvalid)
20603d333e0SAkshit Shah {
20703d333e0SAkshit Shah     size_t bus = 0;
20803d333e0SAkshit Shah     size_t addr = 0;
20903d333e0SAkshit Shah     std::string devName = "12-fan";
21003d333e0SAkshit Shah 
21103d333e0SAkshit Shah     auto ret = getDeviceBusAddr(devName, bus, addr);
21203d333e0SAkshit Shah     EXPECT_FALSE(ret);
21303d333e0SAkshit Shah }
21403d333e0SAkshit Shah 
215*278e177fSTom Tung TEST(GetDeviceBusAddrTest, I3CBusAddrValid)
216*278e177fSTom Tung {
217*278e177fSTom Tung     uint64_t bus = 0;
218*278e177fSTom Tung     uint64_t provisionedId = 0;
219*278e177fSTom Tung     std::string devName = "0-22400000001";
220*278e177fSTom Tung 
221*278e177fSTom Tung     auto ret = getDeviceBusAddr(devName, bus, provisionedId);
222*278e177fSTom Tung     EXPECT_TRUE(ret);
223*278e177fSTom Tung     EXPECT_EQ(bus, 0);
224*278e177fSTom Tung     EXPECT_EQ(provisionedId, 0x22400000001);
225*278e177fSTom Tung }
226*278e177fSTom Tung 
22703d333e0SAkshit Shah TEST(GetDeviceBusAddrTest, AllValid)
22803d333e0SAkshit Shah {
22903d333e0SAkshit Shah     size_t bus = 0;
23003d333e0SAkshit Shah     size_t addr = 0;
23103d333e0SAkshit Shah     std::string devName = "12-00af";
23203d333e0SAkshit Shah 
23303d333e0SAkshit Shah     auto ret = getDeviceBusAddr(devName, bus, addr);
23403d333e0SAkshit Shah     EXPECT_TRUE(ret);
23503d333e0SAkshit Shah     EXPECT_EQ(bus, 12);
23603d333e0SAkshit Shah     EXPECT_EQ(addr, 0xaf);
23703d333e0SAkshit Shah }
238