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 | |
George Liu | 6198435 | 2025-02-24 14:47:34 +0800 | [diff] [blame^] | 3 | #include <phosphor-logging/lg2.hpp> |
| 4 | |
Ed Tanous | 532c864 | 2022-01-14 09:48:15 -0800 | [diff] [blame] | 5 | #include <array> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 6 | #include <cstddef> |
| 7 | #include <cstdint> |
| 8 | #include <cstdlib> |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 9 | #include <filesystem> |
| 10 | #include <fstream> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 11 | #include <new> |
| 12 | #include <string> |
| 13 | #include <vector> |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 14 | |
| 15 | #include <gtest/gtest.h> |
| 16 | |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 17 | class TestUtils : public testing::Test |
| 18 | { |
| 19 | public: |
| 20 | std::string testDir; |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 21 | std::filesystem::path hwmonDir; |
| 22 | std::filesystem::path peciDir; |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 23 | TestUtils() |
| 24 | { |
| 25 | // Create test environment |
Ed Tanous | 532c864 | 2022-01-14 09:48:15 -0800 | [diff] [blame] | 26 | auto dir = std::to_array("./testDirXXXXXX"); |
| 27 | testDir = mkdtemp(dir.data()); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 28 | |
| 29 | if (testDir.empty()) |
| 30 | { |
| 31 | throw std::bad_alloc(); |
| 32 | } |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 33 | hwmonDir = std::filesystem::path(testDir) / "hwmon"; |
| 34 | std::filesystem::create_directory(hwmonDir); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 35 | auto hwmon10 = hwmonDir / "hwmon10"; |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 36 | std::filesystem::create_directory(hwmonDir / "hwmon10"); |
Ed Tanous | b429f31 | 2022-06-27 16:09:53 -0700 | [diff] [blame] | 37 | { |
| 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 YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 43 | createPECIDir(); |
| 44 | } |
| 45 | |
| 46 | ~TestUtils() override |
| 47 | { |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 48 | std::filesystem::remove_all(testDir); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 49 | } |
| 50 | |
Ed Tanous | 6529136 | 2022-01-14 09:43:43 -0800 | [diff] [blame] | 51 | TestUtils(const TestUtils&) = delete; |
| 52 | TestUtils(TestUtils&&) = delete; |
| 53 | TestUtils& operator=(const TestUtils&) = delete; |
| 54 | TestUtils& operator=(TestUtils&&) = delete; |
| 55 | |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 56 | void createPECIDir() |
| 57 | { |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 58 | peciDir = std::filesystem::path(testDir) / "peci"; |
Patrick Williams | 779c96a | 2023-05-10 07:50:42 -0500 | [diff] [blame] | 59 | auto peci0 = peciDir / |
| 60 | "peci-0/device/0-30/peci-cputemp.0/hwmon/hwmon25"; |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 61 | std::filesystem::create_directories(peci0); |
Ed Tanous | b429f31 | 2022-06-27 16:09:53 -0700 | [diff] [blame] | 62 | { |
| 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 YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 68 | auto devDir = peciDir / "peci-0/peci_dev/peci-0"; |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 69 | 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 YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 74 | |
| 75 | // Let's keep this for debugging purpose |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 76 | 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 YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 80 | { |
| 81 | std::string path = p->path().string(); |
George Liu | 6198435 | 2025-02-24 14:47:34 +0800 | [diff] [blame^] | 82 | lg2::info("{PATH}", "PATH", path); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 83 | if (p.depth() >= 6) |
| 84 | { |
| 85 | p.disable_recursion_pending(); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | TEST_F(TestUtils, findFiles_non_exist) |
| 92 | { |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 93 | std::vector<std::filesystem::path> foundPaths; |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 94 | auto ret = findFiles("non-exist", "", foundPaths); |
| 95 | |
| 96 | EXPECT_FALSE(ret); |
| 97 | EXPECT_TRUE(foundPaths.empty()); |
| 98 | } |
| 99 | |
| 100 | TEST_F(TestUtils, findFiles_in_hwmon_no_match) |
| 101 | { |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 102 | std::vector<std::filesystem::path> foundPaths; |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 103 | auto ret = findFiles(hwmonDir, R"(in\d+_input)", foundPaths); |
| 104 | |
| 105 | EXPECT_TRUE(ret); |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 106 | EXPECT_EQ(foundPaths.size(), 0U); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | TEST_F(TestUtils, findFiles_in_hwmon_match) |
| 110 | { |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 111 | std::vector<std::filesystem::path> foundPaths; |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 112 | auto ret = findFiles(hwmonDir, R"(temp\d+_input)", foundPaths); |
| 113 | |
| 114 | EXPECT_TRUE(ret); |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 115 | EXPECT_EQ(foundPaths.size(), 2U); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | TEST_F(TestUtils, findFiles_in_peci_no_match) |
| 119 | { |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 120 | std::vector<std::filesystem::path> foundPaths; |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 121 | auto ret = |
| 122 | findFiles(peciDir, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/aaa$)", |
| 123 | foundPaths, 6); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 124 | |
| 125 | EXPECT_TRUE(ret); |
| 126 | EXPECT_TRUE(foundPaths.empty()); |
| 127 | } |
| 128 | |
| 129 | TEST_F(TestUtils, findFiles_in_peci_match) |
| 130 | { |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 131 | std::vector<std::filesystem::path> foundPaths; |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 132 | auto ret = |
| 133 | findFiles(peciDir, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", |
| 134 | foundPaths, 6); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 135 | EXPECT_TRUE(ret); |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 136 | EXPECT_EQ(foundPaths.size(), 1U); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 137 | |
| 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 Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 144 | EXPECT_EQ(foundPaths.size(), 3U); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | TEST_F(TestUtils, findFiles_hwmonPath_end_with_slash) |
| 148 | { |
| 149 | std::string p = hwmonDir.string() + "/"; |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 150 | std::vector<std::filesystem::path> foundPaths; |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 151 | auto ret = findFiles(p, R"(temp\d+_input)", foundPaths); |
| 152 | |
| 153 | EXPECT_TRUE(ret); |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 154 | EXPECT_EQ(foundPaths.size(), 2U); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | TEST_F(TestUtils, findFiles_peciPath_end_with_slash) |
| 158 | { |
| 159 | std::string p = peciDir.string() + "/"; |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 160 | std::vector<std::filesystem::path> foundPaths; |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 161 | 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 Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 166 | EXPECT_EQ(foundPaths.size(), 3U); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 167 | } |
Lei YU | 0b207a6 | 2021-10-20 13:41:51 +0800 | [diff] [blame] | 168 | |
| 169 | TEST_F(TestUtils, findFiles_in_sub_peci_match) |
| 170 | { |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 171 | std::vector<std::filesystem::path> foundPaths; |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 172 | auto ret = |
| 173 | findFiles(peciDir / "peci-0", R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", |
| 174 | foundPaths, 5); |
Lei YU | 0b207a6 | 2021-10-20 13:41:51 +0800 | [diff] [blame] | 175 | EXPECT_TRUE(ret); |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 176 | EXPECT_EQ(foundPaths.size(), 1U); |
Lei YU | 0b207a6 | 2021-10-20 13:41:51 +0800 | [diff] [blame] | 177 | |
| 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 Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 184 | EXPECT_EQ(foundPaths.size(), 3U); |
Lei YU | 0b207a6 | 2021-10-20 13:41:51 +0800 | [diff] [blame] | 185 | } |
Akshit Shah | 03d333e | 2023-08-23 22:14:28 +0000 | [diff] [blame] | 186 | |
| 187 | TEST(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 | |
| 205 | TEST(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 | |
| 215 | TEST(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 Tung | 278e177 | 2023-12-12 09:20:40 +0800 | [diff] [blame] | 225 | TEST(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 Shah | 03d333e | 2023-08-23 22:14:28 +0000 | [diff] [blame] | 237 | TEST(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 | } |