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