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> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 4 | #include <cstddef> |
| 5 | #include <cstdint> |
| 6 | #include <cstdlib> |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 7 | #include <filesystem> |
| 8 | #include <fstream> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 9 | #include <iostream> |
| 10 | #include <new> |
| 11 | #include <string> |
| 12 | #include <vector> |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 13 | |
| 14 | #include <gtest/gtest.h> |
| 15 | |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 16 | class TestUtils : public testing::Test |
| 17 | { |
| 18 | public: |
| 19 | std::string testDir; |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame^] | 20 | std::filesystem::path hwmonDir; |
| 21 | std::filesystem::path peciDir; |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 22 | TestUtils() |
| 23 | { |
| 24 | // Create test environment |
Ed Tanous | 532c864 | 2022-01-14 09:48:15 -0800 | [diff] [blame] | 25 | auto dir = std::to_array("./testDirXXXXXX"); |
| 26 | testDir = mkdtemp(dir.data()); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 27 | |
| 28 | if (testDir.empty()) |
| 29 | { |
| 30 | throw std::bad_alloc(); |
| 31 | } |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame^] | 32 | hwmonDir = std::filesystem::path(testDir) / "hwmon"; |
| 33 | std::filesystem::create_directory(hwmonDir); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 34 | auto hwmon10 = hwmonDir / "hwmon10"; |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame^] | 35 | std::filesystem::create_directory(hwmonDir / "hwmon10"); |
Ed Tanous | b429f31 | 2022-06-27 16:09:53 -0700 | [diff] [blame] | 36 | { |
| 37 | std::ofstream temp1Input{hwmon10 / "temp1_input"}; |
| 38 | std::ofstream temp1Min{hwmon10 / "temp1_min"}; |
| 39 | std::ofstream temp1Max{hwmon10 / "temp1_max"}; |
| 40 | std::ofstream temp2Input{hwmon10 / "temp2_input"}; |
| 41 | } |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 42 | createPECIDir(); |
| 43 | } |
| 44 | |
| 45 | ~TestUtils() override |
| 46 | { |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame^] | 47 | std::filesystem::remove_all(testDir); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 48 | } |
| 49 | |
Ed Tanous | 6529136 | 2022-01-14 09:43:43 -0800 | [diff] [blame] | 50 | TestUtils(const TestUtils&) = delete; |
| 51 | TestUtils(TestUtils&&) = delete; |
| 52 | TestUtils& operator=(const TestUtils&) = delete; |
| 53 | TestUtils& operator=(TestUtils&&) = delete; |
| 54 | |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 55 | void createPECIDir() |
| 56 | { |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame^] | 57 | peciDir = std::filesystem::path(testDir) / "peci"; |
Patrick Williams | 779c96a | 2023-05-10 07:50:42 -0500 | [diff] [blame] | 58 | auto peci0 = peciDir / |
| 59 | "peci-0/device/0-30/peci-cputemp.0/hwmon/hwmon25"; |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame^] | 60 | std::filesystem::create_directories(peci0); |
Ed Tanous | b429f31 | 2022-06-27 16:09:53 -0700 | [diff] [blame] | 61 | { |
| 62 | std::ofstream temp0Input{peci0 / "temp0_input"}; |
| 63 | std::ofstream temp1Input{peci0 / "temp1_input"}; |
| 64 | std::ofstream temp2Input{peci0 / "temp2_input"}; |
| 65 | std::ofstream name{peci0 / "name"}; |
| 66 | } |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 67 | auto devDir = peciDir / "peci-0/peci_dev/peci-0"; |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame^] | 68 | std::filesystem::create_directories(devDir); |
| 69 | std::filesystem::create_directory_symlink("../../../peci-0", |
| 70 | devDir / "device"); |
| 71 | std::filesystem::create_directory_symlink("device/0-30", |
| 72 | peciDir / "peci-0/0-30"); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 73 | |
| 74 | // Let's keep this for debugging purpose |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame^] | 75 | for (auto p = std::filesystem::recursive_directory_iterator( |
| 76 | peciDir, |
| 77 | std::filesystem::directory_options::follow_directory_symlink); |
| 78 | p != std::filesystem::recursive_directory_iterator(); ++p) |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 79 | { |
| 80 | std::string path = p->path().string(); |
Ed Tanous | 99c4409 | 2022-01-14 09:59:09 -0800 | [diff] [blame] | 81 | std::cerr << path << "\n"; |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 82 | if (p.depth() >= 6) |
| 83 | { |
| 84 | p.disable_recursion_pending(); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | TEST_F(TestUtils, findFiles_non_exist) |
| 91 | { |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame^] | 92 | std::vector<std::filesystem::path> foundPaths; |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 93 | auto ret = findFiles("non-exist", "", foundPaths); |
| 94 | |
| 95 | EXPECT_FALSE(ret); |
| 96 | EXPECT_TRUE(foundPaths.empty()); |
| 97 | } |
| 98 | |
| 99 | TEST_F(TestUtils, findFiles_in_hwmon_no_match) |
| 100 | { |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame^] | 101 | std::vector<std::filesystem::path> foundPaths; |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 102 | auto ret = findFiles(hwmonDir, R"(in\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(), 0U); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | TEST_F(TestUtils, findFiles_in_hwmon_match) |
| 109 | { |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame^] | 110 | std::vector<std::filesystem::path> foundPaths; |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 111 | auto ret = findFiles(hwmonDir, R"(temp\d+_input)", foundPaths); |
| 112 | |
| 113 | EXPECT_TRUE(ret); |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 114 | EXPECT_EQ(foundPaths.size(), 2U); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | TEST_F(TestUtils, findFiles_in_peci_no_match) |
| 118 | { |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame^] | 119 | std::vector<std::filesystem::path> foundPaths; |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 120 | auto ret = |
| 121 | findFiles(peciDir, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/aaa$)", |
| 122 | foundPaths, 6); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 123 | |
| 124 | EXPECT_TRUE(ret); |
| 125 | EXPECT_TRUE(foundPaths.empty()); |
| 126 | } |
| 127 | |
| 128 | TEST_F(TestUtils, findFiles_in_peci_match) |
| 129 | { |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame^] | 130 | std::vector<std::filesystem::path> foundPaths; |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 131 | auto ret = |
| 132 | findFiles(peciDir, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", |
| 133 | foundPaths, 6); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 134 | EXPECT_TRUE(ret); |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 135 | EXPECT_EQ(foundPaths.size(), 1U); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 136 | |
| 137 | foundPaths.clear(); |
| 138 | |
| 139 | ret = findFiles(peciDir, |
| 140 | R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)", |
| 141 | foundPaths, 6); |
| 142 | EXPECT_TRUE(ret); |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 143 | EXPECT_EQ(foundPaths.size(), 3U); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | TEST_F(TestUtils, findFiles_hwmonPath_end_with_slash) |
| 147 | { |
| 148 | std::string p = hwmonDir.string() + "/"; |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame^] | 149 | std::vector<std::filesystem::path> foundPaths; |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 150 | auto ret = findFiles(p, R"(temp\d+_input)", foundPaths); |
| 151 | |
| 152 | EXPECT_TRUE(ret); |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 153 | EXPECT_EQ(foundPaths.size(), 2U); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | TEST_F(TestUtils, findFiles_peciPath_end_with_slash) |
| 157 | { |
| 158 | std::string p = peciDir.string() + "/"; |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame^] | 159 | std::vector<std::filesystem::path> foundPaths; |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 160 | auto ret = |
| 161 | findFiles(p, R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)", |
| 162 | foundPaths, 6); |
| 163 | |
| 164 | EXPECT_TRUE(ret); |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 165 | EXPECT_EQ(foundPaths.size(), 3U); |
Lei YU | d320414 | 2021-11-16 14:57:27 +0800 | [diff] [blame] | 166 | } |
Lei YU | 0b207a6 | 2021-10-20 13:41:51 +0800 | [diff] [blame] | 167 | |
| 168 | TEST_F(TestUtils, findFiles_in_sub_peci_match) |
| 169 | { |
Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame^] | 170 | std::vector<std::filesystem::path> foundPaths; |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 171 | auto ret = |
| 172 | findFiles(peciDir / "peci-0", R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", |
| 173 | foundPaths, 5); |
Lei YU | 0b207a6 | 2021-10-20 13:41:51 +0800 | [diff] [blame] | 174 | EXPECT_TRUE(ret); |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 175 | EXPECT_EQ(foundPaths.size(), 1U); |
Lei YU | 0b207a6 | 2021-10-20 13:41:51 +0800 | [diff] [blame] | 176 | |
| 177 | foundPaths.clear(); |
| 178 | |
| 179 | ret = findFiles(peciDir / "peci-0", |
| 180 | R"(\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)", |
| 181 | foundPaths, 5); |
| 182 | EXPECT_TRUE(ret); |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 183 | EXPECT_EQ(foundPaths.size(), 3U); |
Lei YU | 0b207a6 | 2021-10-20 13:41:51 +0800 | [diff] [blame] | 184 | } |
Akshit Shah | 03d333e | 2023-08-23 22:14:28 +0000 | [diff] [blame] | 185 | |
| 186 | TEST(GetDeviceBusAddrTest, DevNameInvalid) |
| 187 | { |
| 188 | size_t bus = 0; |
| 189 | size_t addr = 0; |
| 190 | std::string devName; |
| 191 | |
| 192 | auto ret = getDeviceBusAddr(devName, bus, addr); |
| 193 | EXPECT_FALSE(ret); |
| 194 | |
| 195 | devName = "NoHyphen"; |
| 196 | ret = getDeviceBusAddr(devName, bus, addr); |
| 197 | EXPECT_FALSE(ret); |
| 198 | |
| 199 | devName = "pwm-fan"; |
| 200 | ret = getDeviceBusAddr(devName, bus, addr); |
| 201 | EXPECT_FALSE(ret); |
| 202 | } |
| 203 | |
| 204 | TEST(GetDeviceBusAddrTest, BusInvalid) |
| 205 | { |
| 206 | size_t bus = 0; |
| 207 | size_t addr = 0; |
| 208 | std::string devName = "FF-00FF"; |
| 209 | |
| 210 | auto ret = getDeviceBusAddr(devName, bus, addr); |
| 211 | EXPECT_FALSE(ret); |
| 212 | } |
| 213 | |
| 214 | TEST(GetDeviceBusAddrTest, AddrInvalid) |
| 215 | { |
| 216 | size_t bus = 0; |
| 217 | size_t addr = 0; |
| 218 | std::string devName = "12-fan"; |
| 219 | |
| 220 | auto ret = getDeviceBusAddr(devName, bus, addr); |
| 221 | EXPECT_FALSE(ret); |
| 222 | } |
| 223 | |
Tom Tung | 278e177 | 2023-12-12 09:20:40 +0800 | [diff] [blame] | 224 | TEST(GetDeviceBusAddrTest, I3CBusAddrValid) |
| 225 | { |
| 226 | uint64_t bus = 0; |
| 227 | uint64_t provisionedId = 0; |
| 228 | std::string devName = "0-22400000001"; |
| 229 | |
| 230 | auto ret = getDeviceBusAddr(devName, bus, provisionedId); |
| 231 | EXPECT_TRUE(ret); |
| 232 | EXPECT_EQ(bus, 0); |
| 233 | EXPECT_EQ(provisionedId, 0x22400000001); |
| 234 | } |
| 235 | |
Akshit Shah | 03d333e | 2023-08-23 22:14:28 +0000 | [diff] [blame] | 236 | TEST(GetDeviceBusAddrTest, AllValid) |
| 237 | { |
| 238 | size_t bus = 0; |
| 239 | size_t addr = 0; |
| 240 | std::string devName = "12-00af"; |
| 241 | |
| 242 | auto ret = getDeviceBusAddr(devName, bus, addr); |
| 243 | EXPECT_TRUE(ret); |
| 244 | EXPECT_EQ(bus, 12); |
| 245 | EXPECT_EQ(addr, 0xaf); |
| 246 | } |