Brad Bishop | e45d8c7 | 2022-05-25 15:12:53 -0400 | [diff] [blame] | 1 | #include "fru_utils.hpp" |
Patrick Venture | a62466c | 2021-02-08 14:55:18 -0800 | [diff] [blame] | 2 | |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 3 | #include <algorithm> |
Patrick Venture | a62466c | 2021-02-08 14:55:18 -0800 | [diff] [blame] | 4 | #include <array> |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 5 | #include <iterator> |
Patrick Venture | a62466c | 2021-02-08 14:55:18 -0800 | [diff] [blame] | 6 | |
| 7 | #include "gtest/gtest.h" |
| 8 | |
| 9 | extern "C" |
| 10 | { |
| 11 | // Include for I2C_SMBUS_BLOCK_MAX |
| 12 | #include <linux/i2c.h> |
| 13 | } |
| 14 | |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 15 | static constexpr size_t blockSize = I2C_SMBUS_BLOCK_MAX; |
| 16 | |
Patrick Venture | a62466c | 2021-02-08 14:55:18 -0800 | [diff] [blame] | 17 | TEST(ValidateHeaderTest, InvalidFruVersionReturnsFalse) |
| 18 | { |
| 19 | // Validates the FruVersion is checked for the only legal value. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 20 | constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = { |
Patrick Venture | a62466c | 2021-02-08 14:55:18 -0800 | [diff] [blame] | 21 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 22 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 23 | EXPECT_FALSE(validateHeader(fruHeader)); |
Patrick Venture | a62466c | 2021-02-08 14:55:18 -0800 | [diff] [blame] | 24 | } |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 25 | |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 26 | TEST(ValidateHeaderTest, InvalidReservedReturnsFalse) |
| 27 | { |
| 28 | // Validates the reserved bit(7:4) of first bytes. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 29 | constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = { |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 30 | 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 31 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 32 | EXPECT_FALSE(validateHeader(fruHeader)); |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | TEST(ValidateHeaderTest, InvalidPaddingReturnsFalse) |
| 36 | { |
| 37 | // Validates the padding byte (7th byte). |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 38 | constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = { |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 39 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00}; |
| 40 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 41 | EXPECT_FALSE(validateHeader(fruHeader)); |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | TEST(ValidateHeaderTest, InvalidChecksumReturnsFalse) |
| 45 | { |
| 46 | // Validates the checksum, check for incorrect value. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 47 | constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = { |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 48 | 0x01, 0x00, 0x01, 0x02, 0x03, 0x04, 0x00, 0x00}; |
| 49 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 50 | EXPECT_FALSE(validateHeader(fruHeader)); |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | TEST(ValidateHeaderTest, ValidChecksumReturnsTrue) |
| 54 | { |
| 55 | // Validates the checksum, check for correct value. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 56 | constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = { |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 57 | 0x01, 0x00, 0x01, 0x02, 0x03, 0x04, 0x00, 0xf5}; |
| 58 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 59 | EXPECT_TRUE(validateHeader(fruHeader)); |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 62 | TEST(VerifyOffsetTest, EmptyFruDataReturnsFalse) |
| 63 | { |
| 64 | // Validates the FruData size is checked for non empty. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 65 | std::vector<uint8_t> fruData = {}; |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 66 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 67 | EXPECT_FALSE(verifyOffset(fruData, fruAreas::fruAreaChassis, 0)); |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | TEST(VerifyOffsetTest, AreaOutOfRangeReturnsFalse) |
| 71 | { |
| 72 | // Validates the FruArea value, check if it is within range. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 73 | const std::vector<uint8_t> fruData = {0x01, 0x00, 0x00, 0x00, 0x00, |
| 74 | 0x00, 0x00, 0x00, 0x00}; |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 75 | |
| 76 | unsigned int areaOutOfRange = 8; |
| 77 | EXPECT_FALSE( |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 78 | verifyOffset(fruData, static_cast<fruAreas>(areaOutOfRange), 0)); |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | TEST(VerifyOffsetTest, OverlapNextAreaReturnsFalse) |
| 82 | { |
| 83 | // Validates the Overlap of offsets with overlapped values. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 84 | const std::vector<uint8_t> fruData = {0x01, 0x00, 0x01, 0x02, 0x03, |
| 85 | 0x04, 0x00, 0x00, 0x00}; |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 86 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 87 | EXPECT_FALSE(verifyOffset(fruData, fruAreas::fruAreaChassis, 2)); |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | TEST(VerifyOffsetTest, OverlapPrevAreaReturnsFalse) |
| 91 | { |
| 92 | // Validates the Overlap of offsets with overlapped values. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 93 | const std::vector<uint8_t> fruData = {0x01, 0x00, 0x01, 0x03, 0x02, |
| 94 | 0x07, 0x00, 0x00, 0x00}; |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 95 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 96 | EXPECT_FALSE(verifyOffset(fruData, fruAreas::fruAreaProduct, 2)); |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | TEST(VerifyOffsetTest, ValidInputDataNoOverlapReturnsTrue) |
| 100 | { |
| 101 | // Validates all inputs with expected value and no overlap. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 102 | const std::vector<uint8_t> fruData = {0x01, 0x00, 0x01, 0x02, 0x03, |
| 103 | 0x04, 0x00, 0x00, 0x00}; |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 104 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 105 | EXPECT_TRUE(verifyOffset(fruData, fruAreas::fruAreaChassis, 1)); |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 106 | } |
Andrew Jeffery | 8dacf6a | 2021-08-02 22:17:18 +0930 | [diff] [blame] | 107 | |
| 108 | TEST(VerifyChecksumTest, EmptyInput) |
| 109 | { |
| 110 | std::vector<uint8_t> data = {}; |
| 111 | |
| 112 | EXPECT_EQ(calculateChecksum(data), 0); |
| 113 | } |
| 114 | |
| 115 | TEST(VerifyChecksumTest, SingleOneInput) |
| 116 | { |
| 117 | std::vector<uint8_t> data(1, 1); |
| 118 | |
| 119 | EXPECT_EQ(calculateChecksum(data), 255); |
| 120 | } |
| 121 | |
| 122 | TEST(VerifyChecksumTest, AllOneInput) |
| 123 | { |
| 124 | std::vector<uint8_t> data(256, 1); |
| 125 | |
| 126 | EXPECT_EQ(calculateChecksum(data), 0); |
| 127 | } |
| 128 | |
| 129 | TEST(VerifyChecksumTest, WrapBoundaryLow) |
| 130 | { |
Ed Tanous | fbce8e2 | 2021-09-02 15:29:12 -0700 | [diff] [blame] | 131 | std::vector<uint8_t> data = {255, 0}; |
Andrew Jeffery | 8dacf6a | 2021-08-02 22:17:18 +0930 | [diff] [blame] | 132 | |
| 133 | EXPECT_EQ(calculateChecksum(data), 1); |
| 134 | } |
| 135 | |
| 136 | TEST(VerifyChecksumTest, WrapBoundaryExact) |
| 137 | { |
Ed Tanous | fbce8e2 | 2021-09-02 15:29:12 -0700 | [diff] [blame] | 138 | std::vector<uint8_t> data = {255, 1}; |
Andrew Jeffery | 8dacf6a | 2021-08-02 22:17:18 +0930 | [diff] [blame] | 139 | |
| 140 | EXPECT_EQ(calculateChecksum(data), 0); |
| 141 | } |
| 142 | |
| 143 | TEST(VerifyChecksumTest, WrapBoundaryHigh) |
| 144 | { |
Ed Tanous | fbce8e2 | 2021-09-02 15:29:12 -0700 | [diff] [blame] | 145 | std::vector<uint8_t> data = {255, 2}; |
Andrew Jeffery | 8dacf6a | 2021-08-02 22:17:18 +0930 | [diff] [blame] | 146 | |
| 147 | EXPECT_EQ(calculateChecksum(data), 255); |
| 148 | } |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 149 | |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 150 | int64_t getDataTempl(const std::vector<uint8_t>& data, off_t offset, |
Zev Weiss | 1525e85 | 2022-03-22 22:27:43 +0000 | [diff] [blame] | 151 | size_t length, uint8_t* outBuf) |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 152 | { |
Zev Weiss | 1525e85 | 2022-03-22 22:27:43 +0000 | [diff] [blame] | 153 | if (offset >= static_cast<off_t>(data.size())) |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 154 | { |
| 155 | return 0; |
| 156 | } |
| 157 | |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 158 | uint16_t idx = offset; |
| 159 | // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) |
| 160 | for (; idx < std::min(data.size(), offset + length); ++idx, ++outBuf) |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 161 | { |
| 162 | *outBuf = data[idx]; |
| 163 | } |
| 164 | |
| 165 | return idx - offset; |
| 166 | } |
| 167 | |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 168 | TEST(FRUReaderTest, ReadData) |
| 169 | { |
| 170 | std::vector<uint8_t> data = {}; |
Patrick Williams | 849f13a | 2024-12-18 15:43:19 -0500 | [diff] [blame^] | 171 | data.reserve(blockSize * 2); |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 172 | for (size_t i = 0; i < blockSize * 2; i++) |
| 173 | { |
| 174 | data.push_back(i); |
| 175 | } |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 176 | std::array<uint8_t, blockSize * 2> rdbuf{}; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 177 | auto getData = [&data](auto o, auto l, auto* b) { |
| 178 | return getDataTempl(data, o, l, b); |
| 179 | }; |
| 180 | FRUReader reader(getData); |
| 181 | |
| 182 | EXPECT_EQ(reader.read(0, data.size(), rdbuf.data()), |
| 183 | static_cast<ssize_t>(data.size())); |
| 184 | EXPECT_TRUE(std::equal(rdbuf.begin(), rdbuf.end(), data.begin())); |
| 185 | for (size_t i = 0; i < blockSize * 2; i++) |
| 186 | { |
| 187 | EXPECT_EQ(reader.read(i, 1, rdbuf.data()), 1); |
| 188 | EXPECT_EQ(rdbuf[i], i); |
| 189 | } |
| 190 | EXPECT_EQ(reader.read(blockSize - 1, 2, rdbuf.data()), 2); |
| 191 | EXPECT_EQ(rdbuf[0], blockSize - 1); |
| 192 | EXPECT_EQ(rdbuf[1], blockSize); |
| 193 | } |
| 194 | |
| 195 | TEST(FRUReaderTest, StartPastUnknownEOF) |
| 196 | { |
| 197 | const std::vector<uint8_t> data = {}; |
| 198 | auto getData = [&data](auto o, auto l, auto* b) { |
| 199 | return getDataTempl(data, o, l, b); |
| 200 | }; |
| 201 | FRUReader reader(getData); |
| 202 | |
| 203 | EXPECT_EQ(reader.read(1, 1, nullptr), 0); |
| 204 | } |
| 205 | |
| 206 | TEST(FRUReaderTest, StartPastKnownEOF) |
| 207 | { |
| 208 | std::vector<uint8_t> data = {}; |
| 209 | data.resize(blockSize / 2); |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 210 | std::array<uint8_t, blockSize> blockData{}; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 211 | auto getData = [&data](auto o, auto l, auto* b) { |
| 212 | return getDataTempl(data, o, l, b); |
| 213 | }; |
| 214 | FRUReader reader(getData); |
| 215 | |
| 216 | EXPECT_EQ(reader.read(0, blockSize, blockData.data()), |
| 217 | static_cast<ssize_t>(data.size())); |
| 218 | EXPECT_EQ(reader.read(data.size(), 1, nullptr), 0); |
| 219 | EXPECT_EQ(reader.read(data.size() + 1, 1, nullptr), 0); |
| 220 | EXPECT_EQ(reader.read(blockSize, 1, nullptr), 0); |
| 221 | EXPECT_EQ(reader.read(blockSize + 1, 1, nullptr), 0); |
| 222 | } |
| 223 | |
| 224 | TEST(FRUReaderTest, DecreasingEOF) |
| 225 | { |
| 226 | const std::vector<uint8_t> data = {}; |
| 227 | auto getData = [&data](auto o, auto l, auto* b) { |
| 228 | return getDataTempl(data, o, l, b); |
| 229 | }; |
| 230 | FRUReader reader(getData); |
| 231 | |
| 232 | EXPECT_EQ(reader.read(blockSize * 2, 1, nullptr), 0); |
| 233 | EXPECT_EQ(reader.read(blockSize + (blockSize / 2), 1, nullptr), 0); |
| 234 | EXPECT_EQ(reader.read(blockSize, 1, nullptr), 0); |
| 235 | EXPECT_EQ(reader.read(blockSize / 2, 1, nullptr), 0); |
| 236 | EXPECT_EQ(reader.read(0, 1, nullptr), 0); |
| 237 | } |
| 238 | |
| 239 | TEST(FRUReaderTest, CacheHit) |
| 240 | { |
| 241 | std::vector<uint8_t> data = {'X'}; |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 242 | std::array<uint8_t, blockSize> read1{}; |
| 243 | std::array<uint8_t, blockSize> read2{}; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 244 | auto getData = [&data](auto o, auto l, auto* b) { |
| 245 | return getDataTempl(data, o, l, b); |
| 246 | }; |
| 247 | FRUReader reader(getData); |
| 248 | |
| 249 | // cache hit should return the same data for the second read even if we |
| 250 | // change it behind the FRUReader's back after the first |
| 251 | EXPECT_EQ(reader.read(0, blockSize, read1.data()), 1); |
| 252 | data[0] = 'Y'; |
| 253 | EXPECT_EQ(reader.read(0, blockSize, read2.data()), 1); |
| 254 | EXPECT_EQ(read1[0], read2[0]); |
| 255 | } |
| 256 | |
| 257 | TEST(FRUReaderTest, ReadPastKnownEnd) |
| 258 | { |
| 259 | const std::vector<uint8_t> data = {'X', 'Y'}; |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 260 | std::array<uint8_t, blockSize> rdbuf{}; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 261 | auto getData = [&data](auto o, auto l, auto* b) { |
| 262 | return getDataTempl(data, o, l, b); |
| 263 | }; |
| 264 | FRUReader reader(getData); |
| 265 | |
| 266 | EXPECT_EQ(reader.read(0, data.size(), rdbuf.data()), |
| 267 | static_cast<ssize_t>(data.size())); |
| 268 | EXPECT_EQ(rdbuf[0], 'X'); |
| 269 | EXPECT_EQ(rdbuf[1], 'Y'); |
| 270 | EXPECT_EQ(reader.read(1, data.size(), rdbuf.data()), |
| 271 | static_cast<ssize_t>(data.size() - 1)); |
| 272 | EXPECT_EQ(rdbuf[0], 'Y'); |
| 273 | } |
| 274 | |
| 275 | TEST(FRUReaderTest, MultiBlockRead) |
| 276 | { |
| 277 | std::vector<uint8_t> data = {}; |
| 278 | data.resize(blockSize, 'X'); |
| 279 | data.resize(2 * blockSize, 'Y'); |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 280 | std::array<uint8_t, 2 * blockSize> rdbuf{}; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 281 | auto getData = [&data](auto o, auto l, auto* b) { |
| 282 | return getDataTempl(data, o, l, b); |
| 283 | }; |
| 284 | FRUReader reader(getData); |
| 285 | |
| 286 | EXPECT_EQ(reader.read(0, 2 * blockSize, rdbuf.data()), |
| 287 | static_cast<ssize_t>(2 * blockSize)); |
| 288 | EXPECT_TRUE(std::equal(rdbuf.begin(), rdbuf.end(), data.begin())); |
| 289 | } |
| 290 | |
| 291 | TEST(FRUReaderTest, ShrinkingEEPROM) |
| 292 | { |
| 293 | std::vector<uint8_t> data = {}; |
| 294 | data.resize(3 * blockSize, 'X'); |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 295 | std::array<uint8_t, blockSize> rdbuf{}; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 296 | auto getData = [&data](auto o, auto l, auto* b) { |
| 297 | return getDataTempl(data, o, l, b); |
| 298 | }; |
| 299 | FRUReader reader(getData); |
| 300 | |
| 301 | EXPECT_EQ(reader.read(data.size() - 1, 2, rdbuf.data()), 1); |
| 302 | data.resize(blockSize); |
| 303 | EXPECT_EQ(reader.read(data.size() - 1, 2, rdbuf.data()), 1); |
| 304 | } |
| 305 | |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 306 | TEST(FindFRUHeaderTest, InvalidHeader) |
| 307 | { |
| 308 | const std::vector<uint8_t> data = {255, 16}; |
Zev Weiss | 1525e85 | 2022-03-22 22:27:43 +0000 | [diff] [blame] | 309 | off_t offset = 0; |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 310 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData{}; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 311 | auto getData = [&data](auto o, auto l, auto* b) { |
| 312 | return getDataTempl(data, o, l, b); |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 313 | }; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 314 | FRUReader reader(getData); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 315 | |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 316 | EXPECT_FALSE(findFRUHeader(reader, "error", blockData, offset)); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | TEST(FindFRUHeaderTest, NoData) |
| 320 | { |
| 321 | const std::vector<uint8_t> data = {}; |
Zev Weiss | 1525e85 | 2022-03-22 22:27:43 +0000 | [diff] [blame] | 322 | off_t offset = 0; |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 323 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData{}; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 324 | auto getData = [&data](auto o, auto l, auto* b) { |
| 325 | return getDataTempl(data, o, l, b); |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 326 | }; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 327 | FRUReader reader(getData); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 328 | |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 329 | EXPECT_FALSE(findFRUHeader(reader, "error", blockData, offset)); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | TEST(FindFRUHeaderTest, ValidHeader) |
| 333 | { |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 334 | const std::vector<uint8_t> data = {0x01, 0x00, 0x01, 0x02, |
| 335 | 0x03, 0x04, 0x00, 0xf5}; |
Zev Weiss | 1525e85 | 2022-03-22 22:27:43 +0000 | [diff] [blame] | 336 | off_t offset = 0; |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 337 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData{}; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 338 | auto getData = [&data](auto o, auto l, auto* b) { |
| 339 | return getDataTempl(data, o, l, b); |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 340 | }; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 341 | FRUReader reader(getData); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 342 | |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 343 | EXPECT_TRUE(findFRUHeader(reader, "error", blockData, offset)); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 344 | EXPECT_EQ(0, offset); |
| 345 | } |
| 346 | |
| 347 | TEST(FindFRUHeaderTest, TyanInvalidHeader) |
| 348 | { |
| 349 | std::vector<uint8_t> data = {'$', 'T', 'Y', 'A', 'N', '$', 0, 0}; |
| 350 | data.resize(0x6000 + I2C_SMBUS_BLOCK_MAX); |
Zev Weiss | 1525e85 | 2022-03-22 22:27:43 +0000 | [diff] [blame] | 351 | off_t offset = 0; |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 352 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData{}; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 353 | auto getData = [&data](auto o, auto l, auto* b) { |
| 354 | return getDataTempl(data, o, l, b); |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 355 | }; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 356 | FRUReader reader(getData); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 357 | |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 358 | EXPECT_FALSE(findFRUHeader(reader, "error", blockData, offset)); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | TEST(FindFRUHeaderTest, TyanNoData) |
| 362 | { |
| 363 | const std::vector<uint8_t> data = {'$', 'T', 'Y', 'A', 'N', '$', 0, 0}; |
Zev Weiss | 1525e85 | 2022-03-22 22:27:43 +0000 | [diff] [blame] | 364 | off_t offset = 0; |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 365 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData{}; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 366 | auto getData = [&data](auto o, auto l, auto* b) { |
| 367 | return getDataTempl(data, o, l, b); |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 368 | }; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 369 | FRUReader reader(getData); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 370 | |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 371 | EXPECT_FALSE(findFRUHeader(reader, "error", blockData, offset)); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | TEST(FindFRUHeaderTest, TyanValidHeader) |
| 375 | { |
| 376 | std::vector<uint8_t> data = {'$', 'T', 'Y', 'A', 'N', '$', 0, 0}; |
| 377 | data.resize(0x6000); |
| 378 | constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = { |
| 379 | 0x01, 0x00, 0x01, 0x02, 0x03, 0x04, 0x00, 0xf5}; |
| 380 | copy(fruHeader.begin(), fruHeader.end(), back_inserter(data)); |
| 381 | |
Zev Weiss | 1525e85 | 2022-03-22 22:27:43 +0000 | [diff] [blame] | 382 | off_t offset = 0; |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 383 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData{}; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 384 | auto getData = [&data](auto o, auto l, auto* b) { |
| 385 | return getDataTempl(data, o, l, b); |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 386 | }; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 387 | FRUReader reader(getData); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 388 | |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 389 | EXPECT_TRUE(findFRUHeader(reader, "error", blockData, offset)); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 390 | EXPECT_EQ(0x6000, offset); |
| 391 | } |