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 | |
| 158 | uint16_t idx; |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 159 | for (idx = offset; idx < data.size() && idx < offset + length; |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 160 | ++idx, ++outBuf) |
| 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 = {}; |
| 171 | for (size_t i = 0; i < blockSize * 2; i++) |
| 172 | { |
| 173 | data.push_back(i); |
| 174 | } |
| 175 | std::array<uint8_t, blockSize * 2> rdbuf; |
| 176 | auto getData = [&data](auto o, auto l, auto* b) { |
| 177 | return getDataTempl(data, o, l, b); |
| 178 | }; |
| 179 | FRUReader reader(getData); |
| 180 | |
| 181 | EXPECT_EQ(reader.read(0, data.size(), rdbuf.data()), |
| 182 | static_cast<ssize_t>(data.size())); |
| 183 | EXPECT_TRUE(std::equal(rdbuf.begin(), rdbuf.end(), data.begin())); |
| 184 | for (size_t i = 0; i < blockSize * 2; i++) |
| 185 | { |
| 186 | EXPECT_EQ(reader.read(i, 1, rdbuf.data()), 1); |
| 187 | EXPECT_EQ(rdbuf[i], i); |
| 188 | } |
| 189 | EXPECT_EQ(reader.read(blockSize - 1, 2, rdbuf.data()), 2); |
| 190 | EXPECT_EQ(rdbuf[0], blockSize - 1); |
| 191 | EXPECT_EQ(rdbuf[1], blockSize); |
| 192 | } |
| 193 | |
| 194 | TEST(FRUReaderTest, StartPastUnknownEOF) |
| 195 | { |
| 196 | const std::vector<uint8_t> data = {}; |
| 197 | auto getData = [&data](auto o, auto l, auto* b) { |
| 198 | return getDataTempl(data, o, l, b); |
| 199 | }; |
| 200 | FRUReader reader(getData); |
| 201 | |
| 202 | EXPECT_EQ(reader.read(1, 1, nullptr), 0); |
| 203 | } |
| 204 | |
| 205 | TEST(FRUReaderTest, StartPastKnownEOF) |
| 206 | { |
| 207 | std::vector<uint8_t> data = {}; |
| 208 | data.resize(blockSize / 2); |
| 209 | std::array<uint8_t, blockSize> blockData; |
| 210 | auto getData = [&data](auto o, auto l, auto* b) { |
| 211 | return getDataTempl(data, o, l, b); |
| 212 | }; |
| 213 | FRUReader reader(getData); |
| 214 | |
| 215 | EXPECT_EQ(reader.read(0, blockSize, blockData.data()), |
| 216 | static_cast<ssize_t>(data.size())); |
| 217 | EXPECT_EQ(reader.read(data.size(), 1, nullptr), 0); |
| 218 | EXPECT_EQ(reader.read(data.size() + 1, 1, nullptr), 0); |
| 219 | EXPECT_EQ(reader.read(blockSize, 1, nullptr), 0); |
| 220 | EXPECT_EQ(reader.read(blockSize + 1, 1, nullptr), 0); |
| 221 | } |
| 222 | |
| 223 | TEST(FRUReaderTest, DecreasingEOF) |
| 224 | { |
| 225 | const std::vector<uint8_t> data = {}; |
| 226 | auto getData = [&data](auto o, auto l, auto* b) { |
| 227 | return getDataTempl(data, o, l, b); |
| 228 | }; |
| 229 | FRUReader reader(getData); |
| 230 | |
| 231 | EXPECT_EQ(reader.read(blockSize * 2, 1, nullptr), 0); |
| 232 | EXPECT_EQ(reader.read(blockSize + (blockSize / 2), 1, nullptr), 0); |
| 233 | EXPECT_EQ(reader.read(blockSize, 1, nullptr), 0); |
| 234 | EXPECT_EQ(reader.read(blockSize / 2, 1, nullptr), 0); |
| 235 | EXPECT_EQ(reader.read(0, 1, nullptr), 0); |
| 236 | } |
| 237 | |
| 238 | TEST(FRUReaderTest, CacheHit) |
| 239 | { |
| 240 | std::vector<uint8_t> data = {'X'}; |
| 241 | std::array<uint8_t, blockSize> read1, read2; |
| 242 | auto getData = [&data](auto o, auto l, auto* b) { |
| 243 | return getDataTempl(data, o, l, b); |
| 244 | }; |
| 245 | FRUReader reader(getData); |
| 246 | |
| 247 | // cache hit should return the same data for the second read even if we |
| 248 | // change it behind the FRUReader's back after the first |
| 249 | EXPECT_EQ(reader.read(0, blockSize, read1.data()), 1); |
| 250 | data[0] = 'Y'; |
| 251 | EXPECT_EQ(reader.read(0, blockSize, read2.data()), 1); |
| 252 | EXPECT_EQ(read1[0], read2[0]); |
| 253 | } |
| 254 | |
| 255 | TEST(FRUReaderTest, ReadPastKnownEnd) |
| 256 | { |
| 257 | const std::vector<uint8_t> data = {'X', 'Y'}; |
| 258 | std::array<uint8_t, blockSize> rdbuf; |
| 259 | auto getData = [&data](auto o, auto l, auto* b) { |
| 260 | return getDataTempl(data, o, l, b); |
| 261 | }; |
| 262 | FRUReader reader(getData); |
| 263 | |
| 264 | EXPECT_EQ(reader.read(0, data.size(), rdbuf.data()), |
| 265 | static_cast<ssize_t>(data.size())); |
| 266 | EXPECT_EQ(rdbuf[0], 'X'); |
| 267 | EXPECT_EQ(rdbuf[1], 'Y'); |
| 268 | EXPECT_EQ(reader.read(1, data.size(), rdbuf.data()), |
| 269 | static_cast<ssize_t>(data.size() - 1)); |
| 270 | EXPECT_EQ(rdbuf[0], 'Y'); |
| 271 | } |
| 272 | |
| 273 | TEST(FRUReaderTest, MultiBlockRead) |
| 274 | { |
| 275 | std::vector<uint8_t> data = {}; |
| 276 | data.resize(blockSize, 'X'); |
| 277 | data.resize(2 * blockSize, 'Y'); |
| 278 | std::array<uint8_t, 2 * blockSize> rdbuf; |
| 279 | auto getData = [&data](auto o, auto l, auto* b) { |
| 280 | return getDataTempl(data, o, l, b); |
| 281 | }; |
| 282 | FRUReader reader(getData); |
| 283 | |
| 284 | EXPECT_EQ(reader.read(0, 2 * blockSize, rdbuf.data()), |
| 285 | static_cast<ssize_t>(2 * blockSize)); |
| 286 | EXPECT_TRUE(std::equal(rdbuf.begin(), rdbuf.end(), data.begin())); |
| 287 | } |
| 288 | |
| 289 | TEST(FRUReaderTest, ShrinkingEEPROM) |
| 290 | { |
| 291 | std::vector<uint8_t> data = {}; |
| 292 | data.resize(3 * blockSize, 'X'); |
| 293 | std::array<uint8_t, blockSize> rdbuf; |
| 294 | auto getData = [&data](auto o, auto l, auto* b) { |
| 295 | return getDataTempl(data, o, l, b); |
| 296 | }; |
| 297 | FRUReader reader(getData); |
| 298 | |
| 299 | EXPECT_EQ(reader.read(data.size() - 1, 2, rdbuf.data()), 1); |
| 300 | data.resize(blockSize); |
| 301 | EXPECT_EQ(reader.read(data.size() - 1, 2, rdbuf.data()), 1); |
| 302 | } |
| 303 | |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 304 | TEST(FindFRUHeaderTest, InvalidHeader) |
| 305 | { |
| 306 | const std::vector<uint8_t> data = {255, 16}; |
Zev Weiss | 1525e85 | 2022-03-22 22:27:43 +0000 | [diff] [blame] | 307 | off_t offset = 0; |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 308 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame^] | 309 | auto getData = [&data](auto o, auto l, auto* b) { |
| 310 | return getDataTempl(data, o, l, b); |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 311 | }; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame^] | 312 | FRUReader reader(getData); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 313 | |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame^] | 314 | EXPECT_FALSE(findFRUHeader(reader, "error", blockData, offset)); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | TEST(FindFRUHeaderTest, NoData) |
| 318 | { |
| 319 | const std::vector<uint8_t> data = {}; |
Zev Weiss | 1525e85 | 2022-03-22 22:27:43 +0000 | [diff] [blame] | 320 | off_t offset = 0; |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 321 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame^] | 322 | auto getData = [&data](auto o, auto l, auto* b) { |
| 323 | return getDataTempl(data, o, l, b); |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 324 | }; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame^] | 325 | FRUReader reader(getData); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 326 | |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame^] | 327 | EXPECT_FALSE(findFRUHeader(reader, "error", blockData, offset)); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | TEST(FindFRUHeaderTest, ValidHeader) |
| 331 | { |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 332 | const std::vector<uint8_t> data = {0x01, 0x00, 0x01, 0x02, |
| 333 | 0x03, 0x04, 0x00, 0xf5}; |
Zev Weiss | 1525e85 | 2022-03-22 22:27:43 +0000 | [diff] [blame] | 334 | off_t offset = 0; |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 335 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame^] | 336 | auto getData = [&data](auto o, auto l, auto* b) { |
| 337 | return getDataTempl(data, o, l, b); |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 338 | }; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame^] | 339 | FRUReader reader(getData); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 340 | |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame^] | 341 | EXPECT_TRUE(findFRUHeader(reader, "error", blockData, offset)); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 342 | EXPECT_EQ(0, offset); |
| 343 | } |
| 344 | |
| 345 | TEST(FindFRUHeaderTest, TyanInvalidHeader) |
| 346 | { |
| 347 | std::vector<uint8_t> data = {'$', 'T', 'Y', 'A', 'N', '$', 0, 0}; |
| 348 | data.resize(0x6000 + I2C_SMBUS_BLOCK_MAX); |
Zev Weiss | 1525e85 | 2022-03-22 22:27:43 +0000 | [diff] [blame] | 349 | off_t offset = 0; |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 350 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame^] | 351 | auto getData = [&data](auto o, auto l, auto* b) { |
| 352 | return getDataTempl(data, o, l, b); |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 353 | }; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame^] | 354 | FRUReader reader(getData); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 355 | |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame^] | 356 | EXPECT_FALSE(findFRUHeader(reader, "error", blockData, offset)); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | TEST(FindFRUHeaderTest, TyanNoData) |
| 360 | { |
| 361 | const std::vector<uint8_t> data = {'$', 'T', 'Y', 'A', 'N', '$', 0, 0}; |
Zev Weiss | 1525e85 | 2022-03-22 22:27:43 +0000 | [diff] [blame] | 362 | off_t offset = 0; |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 363 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame^] | 364 | auto getData = [&data](auto o, auto l, auto* b) { |
| 365 | return getDataTempl(data, o, l, b); |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 366 | }; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame^] | 367 | FRUReader reader(getData); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 368 | |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame^] | 369 | EXPECT_FALSE(findFRUHeader(reader, "error", blockData, offset)); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | TEST(FindFRUHeaderTest, TyanValidHeader) |
| 373 | { |
| 374 | std::vector<uint8_t> data = {'$', 'T', 'Y', 'A', 'N', '$', 0, 0}; |
| 375 | data.resize(0x6000); |
| 376 | constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = { |
| 377 | 0x01, 0x00, 0x01, 0x02, 0x03, 0x04, 0x00, 0xf5}; |
| 378 | copy(fruHeader.begin(), fruHeader.end(), back_inserter(data)); |
| 379 | |
Zev Weiss | 1525e85 | 2022-03-22 22:27:43 +0000 | [diff] [blame] | 380 | off_t offset = 0; |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 381 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame^] | 382 | auto getData = [&data](auto o, auto l, auto* b) { |
| 383 | return getDataTempl(data, o, l, b); |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 384 | }; |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame^] | 385 | FRUReader reader(getData); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 386 | |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame^] | 387 | EXPECT_TRUE(findFRUHeader(reader, "error", blockData, offset)); |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 388 | EXPECT_EQ(0x6000, offset); |
| 389 | } |