Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 1 | #include "Utils.hpp" |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 2 | |
Ed Tanous | 532c864 | 2022-01-14 09:48:15 -0800 | [diff] [blame] | 3 | #include <array> |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 4 | #include <filesystem> |
| 5 | #include <fstream> |
| 6 | |
| 7 | #include <gtest/gtest.h> |
| 8 | |
| 9 | namespace fs = std::filesystem; |
| 10 | class 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 Tanous | 532c864 | 2022-01-14 09:48:15 -0800 | [diff] [blame] | 19 | auto dir = std::to_array("./testDirXXXXXX"); |
| 20 | testDir = mkdtemp(dir.data()); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 21 | |
| 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 Tanous | b429f31 | 2022-06-27 16:09:53 -0700 | [diff] [blame] | 30 | { |
| 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 YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 36 | createPECIDir(); |
| 37 | } |
| 38 | |
| 39 | ~TestUtils() override |
| 40 | { |
| 41 | fs::remove_all(testDir); |
| 42 | } |
| 43 | |
Ed Tanous | 6529136 | 2022-01-14 09:43:43 -0800 | [diff] [blame] | 44 | TestUtils(const TestUtils&) = delete; |
| 45 | TestUtils(TestUtils&&) = delete; |
| 46 | TestUtils& operator=(const TestUtils&) = delete; |
| 47 | TestUtils& operator=(TestUtils&&) = delete; |
| 48 | |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 49 | void createPECIDir() |
| 50 | { |
| 51 | peciDir = fs::path(testDir) / "peci"; |
Patrick Williams | 779c96a | 2023-05-10 07:50:42 -0500 | [diff] [blame] | 52 | auto peci0 = peciDir / |
| 53 | "peci-0/device/0-30/peci-cputemp.0/hwmon/hwmon25"; |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 54 | fs::create_directories(peci0); |
Ed Tanous | b429f31 | 2022-06-27 16:09:53 -0700 | [diff] [blame] | 55 | { |
| 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 YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 61 | 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 Tanous | 99c4409 | 2022-01-14 09:59:09 -0800 | [diff] [blame] | 72 | std::cerr << path << "\n"; |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 73 | if (p.depth() >= 6) |
| 74 | { |
| 75 | p.disable_recursion_pending(); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | TEST_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 | |
| 90 | TEST_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 Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 96 | EXPECT_EQ(foundPaths.size(), 0U); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | TEST_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 Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 105 | EXPECT_EQ(foundPaths.size(), 2U); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | TEST_F(TestUtils, findFiles_in_peci_no_match) |
| 109 | { |
| 110 | std::vector<fs::path> foundPaths; |
Patrick Williams | 779c96a | 2023-05-10 07:50:42 -0500 | [diff] [blame] | 111 | auto ret = findFiles(peciDir, |
| 112 | R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/aaa$)", |
| 113 | foundPaths, 6); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 114 | |
| 115 | EXPECT_TRUE(ret); |
| 116 | EXPECT_TRUE(foundPaths.empty()); |
| 117 | } |
| 118 | |
| 119 | TEST_F(TestUtils, findFiles_in_peci_match) |
| 120 | { |
| 121 | std::vector<fs::path> foundPaths; |
Patrick Williams | 779c96a | 2023-05-10 07:50:42 -0500 | [diff] [blame] | 122 | auto ret = findFiles(peciDir, |
| 123 | R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", |
| 124 | foundPaths, 6); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 125 | EXPECT_TRUE(ret); |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 126 | EXPECT_EQ(foundPaths.size(), 1U); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 127 | |
| 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 Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 134 | EXPECT_EQ(foundPaths.size(), 3U); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | TEST_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 Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 144 | EXPECT_EQ(foundPaths.size(), 2U); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | TEST_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 Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 156 | EXPECT_EQ(foundPaths.size(), 3U); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 157 | } |
Lei YU | 0b207a6 | 2021-10-20 13:41:51 +0800 | [diff] [blame] | 158 | |
| 159 | TEST_F(TestUtils, findFiles_in_sub_peci_match) |
| 160 | { |
| 161 | std::vector<fs::path> foundPaths; |
Patrick Williams | 779c96a | 2023-05-10 07:50:42 -0500 | [diff] [blame] | 162 | auto ret = findFiles(peciDir / "peci-0", |
| 163 | R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", foundPaths, |
| 164 | 5); |
Lei YU | 0b207a6 | 2021-10-20 13:41:51 +0800 | [diff] [blame] | 165 | EXPECT_TRUE(ret); |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 166 | EXPECT_EQ(foundPaths.size(), 1U); |
Lei YU | 0b207a6 | 2021-10-20 13:41:51 +0800 | [diff] [blame] | 167 | |
| 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 Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 174 | EXPECT_EQ(foundPaths.size(), 3U); |
Lei YU | 0b207a6 | 2021-10-20 13:41:51 +0800 | [diff] [blame] | 175 | } |
Akshit Shah | 03d333e | 2023-08-23 22:14:28 +0000 | [diff] [blame] | 176 | |
| 177 | TEST(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 | |
| 195 | TEST(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 | |
| 205 | TEST(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 Tung | 278e177 | 2023-12-12 09:20:40 +0800 | [diff] [blame] | 215 | TEST(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 Shah | 03d333e | 2023-08-23 22:14:28 +0000 | [diff] [blame] | 227 | TEST(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 | } |