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 | |
| 15 | TEST(ValidateHeaderTest, InvalidFruVersionReturnsFalse) |
| 16 | { |
| 17 | // Validates the FruVersion is checked for the only legal value. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 18 | constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = { |
Patrick Venture | a62466c | 2021-02-08 14:55:18 -0800 | [diff] [blame] | 19 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 20 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 21 | EXPECT_FALSE(validateHeader(fruHeader)); |
Patrick Venture | a62466c | 2021-02-08 14:55:18 -0800 | [diff] [blame] | 22 | } |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 23 | |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 24 | TEST(ValidateHeaderTest, InvalidReservedReturnsFalse) |
| 25 | { |
| 26 | // Validates the reserved bit(7:4) of first bytes. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 27 | constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = { |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 28 | 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 29 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 30 | EXPECT_FALSE(validateHeader(fruHeader)); |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | TEST(ValidateHeaderTest, InvalidPaddingReturnsFalse) |
| 34 | { |
| 35 | // Validates the padding byte (7th byte). |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 36 | constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = { |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 37 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00}; |
| 38 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 39 | EXPECT_FALSE(validateHeader(fruHeader)); |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | TEST(ValidateHeaderTest, InvalidChecksumReturnsFalse) |
| 43 | { |
| 44 | // Validates the checksum, check for incorrect value. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 45 | constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = { |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 46 | 0x01, 0x00, 0x01, 0x02, 0x03, 0x04, 0x00, 0x00}; |
| 47 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 48 | EXPECT_FALSE(validateHeader(fruHeader)); |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | TEST(ValidateHeaderTest, ValidChecksumReturnsTrue) |
| 52 | { |
| 53 | // Validates the checksum, check for correct value. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 54 | constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = { |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 55 | 0x01, 0x00, 0x01, 0x02, 0x03, 0x04, 0x00, 0xf5}; |
| 56 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 57 | EXPECT_TRUE(validateHeader(fruHeader)); |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 60 | TEST(VerifyOffsetTest, EmptyFruDataReturnsFalse) |
| 61 | { |
| 62 | // Validates the FruData size is checked for non empty. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 63 | std::vector<uint8_t> fruData = {}; |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 64 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 65 | EXPECT_FALSE(verifyOffset(fruData, fruAreas::fruAreaChassis, 0)); |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | TEST(VerifyOffsetTest, AreaOutOfRangeReturnsFalse) |
| 69 | { |
| 70 | // Validates the FruArea value, check if it is within range. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 71 | const std::vector<uint8_t> fruData = {0x01, 0x00, 0x00, 0x00, 0x00, |
| 72 | 0x00, 0x00, 0x00, 0x00}; |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 73 | |
| 74 | unsigned int areaOutOfRange = 8; |
| 75 | EXPECT_FALSE( |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 76 | verifyOffset(fruData, static_cast<fruAreas>(areaOutOfRange), 0)); |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | TEST(VerifyOffsetTest, OverlapNextAreaReturnsFalse) |
| 80 | { |
| 81 | // Validates the Overlap of offsets with overlapped values. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 82 | const std::vector<uint8_t> fruData = {0x01, 0x00, 0x01, 0x02, 0x03, |
| 83 | 0x04, 0x00, 0x00, 0x00}; |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 84 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 85 | EXPECT_FALSE(verifyOffset(fruData, fruAreas::fruAreaChassis, 2)); |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | TEST(VerifyOffsetTest, OverlapPrevAreaReturnsFalse) |
| 89 | { |
| 90 | // Validates the Overlap of offsets with overlapped values. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 91 | const std::vector<uint8_t> fruData = {0x01, 0x00, 0x01, 0x03, 0x02, |
| 92 | 0x07, 0x00, 0x00, 0x00}; |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 93 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 94 | EXPECT_FALSE(verifyOffset(fruData, fruAreas::fruAreaProduct, 2)); |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | TEST(VerifyOffsetTest, ValidInputDataNoOverlapReturnsTrue) |
| 98 | { |
| 99 | // Validates all inputs with expected value and no overlap. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 100 | const std::vector<uint8_t> fruData = {0x01, 0x00, 0x01, 0x02, 0x03, |
| 101 | 0x04, 0x00, 0x00, 0x00}; |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 102 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame] | 103 | EXPECT_TRUE(verifyOffset(fruData, fruAreas::fruAreaChassis, 1)); |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 104 | } |
Andrew Jeffery | 8dacf6a | 2021-08-02 22:17:18 +0930 | [diff] [blame] | 105 | |
| 106 | TEST(VerifyChecksumTest, EmptyInput) |
| 107 | { |
| 108 | std::vector<uint8_t> data = {}; |
| 109 | |
| 110 | EXPECT_EQ(calculateChecksum(data), 0); |
| 111 | } |
| 112 | |
| 113 | TEST(VerifyChecksumTest, SingleOneInput) |
| 114 | { |
| 115 | std::vector<uint8_t> data(1, 1); |
| 116 | |
| 117 | EXPECT_EQ(calculateChecksum(data), 255); |
| 118 | } |
| 119 | |
| 120 | TEST(VerifyChecksumTest, AllOneInput) |
| 121 | { |
| 122 | std::vector<uint8_t> data(256, 1); |
| 123 | |
| 124 | EXPECT_EQ(calculateChecksum(data), 0); |
| 125 | } |
| 126 | |
| 127 | TEST(VerifyChecksumTest, WrapBoundaryLow) |
| 128 | { |
Ed Tanous | fbce8e2 | 2021-09-02 15:29:12 -0700 | [diff] [blame] | 129 | std::vector<uint8_t> data = {255, 0}; |
Andrew Jeffery | 8dacf6a | 2021-08-02 22:17:18 +0930 | [diff] [blame] | 130 | |
| 131 | EXPECT_EQ(calculateChecksum(data), 1); |
| 132 | } |
| 133 | |
| 134 | TEST(VerifyChecksumTest, WrapBoundaryExact) |
| 135 | { |
Ed Tanous | fbce8e2 | 2021-09-02 15:29:12 -0700 | [diff] [blame] | 136 | std::vector<uint8_t> data = {255, 1}; |
Andrew Jeffery | 8dacf6a | 2021-08-02 22:17:18 +0930 | [diff] [blame] | 137 | |
| 138 | EXPECT_EQ(calculateChecksum(data), 0); |
| 139 | } |
| 140 | |
| 141 | TEST(VerifyChecksumTest, WrapBoundaryHigh) |
| 142 | { |
Ed Tanous | fbce8e2 | 2021-09-02 15:29:12 -0700 | [diff] [blame] | 143 | std::vector<uint8_t> data = {255, 2}; |
Andrew Jeffery | 8dacf6a | 2021-08-02 22:17:18 +0930 | [diff] [blame] | 144 | |
| 145 | EXPECT_EQ(calculateChecksum(data), 255); |
| 146 | } |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 147 | |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 148 | int64_t getDataTempl(const std::vector<uint8_t>& data, |
| 149 | [[maybe_unused]] int flag, [[maybe_unused]] int file, |
Zev Weiss | 1525e85 | 2022-03-22 22:27:43 +0000 | [diff] [blame] | 150 | [[maybe_unused]] uint16_t address, off_t offset, |
| 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 | |
| 168 | TEST(FindFRUHeaderTest, InvalidHeader) |
| 169 | { |
| 170 | const std::vector<uint8_t> data = {255, 16}; |
Zev Weiss | 1525e85 | 2022-03-22 22:27:43 +0000 | [diff] [blame] | 171 | off_t offset = 0; |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 172 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData; |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 173 | auto getData = [&data](auto fl, auto fi, auto a, auto o, auto l, auto* b) { |
| 174 | return getDataTempl(data, fl, fi, a, o, l, b); |
| 175 | }; |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 176 | |
| 177 | EXPECT_FALSE(findFRUHeader(0, 0, 0, getData, "error", blockData, offset)); |
| 178 | } |
| 179 | |
| 180 | TEST(FindFRUHeaderTest, NoData) |
| 181 | { |
| 182 | const std::vector<uint8_t> data = {}; |
Zev Weiss | 1525e85 | 2022-03-22 22:27:43 +0000 | [diff] [blame] | 183 | off_t offset = 0; |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 184 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData; |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 185 | auto getData = [&data](auto fl, auto fi, auto a, auto o, auto l, auto* b) { |
| 186 | return getDataTempl(data, fl, fi, a, o, l, b); |
| 187 | }; |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 188 | |
| 189 | EXPECT_FALSE(findFRUHeader(0, 0, 0, getData, "error", blockData, offset)); |
| 190 | } |
| 191 | |
| 192 | TEST(FindFRUHeaderTest, ValidHeader) |
| 193 | { |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 194 | const std::vector<uint8_t> data = {0x01, 0x00, 0x01, 0x02, |
| 195 | 0x03, 0x04, 0x00, 0xf5}; |
Zev Weiss | 1525e85 | 2022-03-22 22:27:43 +0000 | [diff] [blame] | 196 | off_t offset = 0; |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 197 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData; |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 198 | auto getData = [&data](auto fl, auto fi, auto a, auto o, auto l, auto* b) { |
| 199 | return getDataTempl(data, fl, fi, a, o, l, b); |
| 200 | }; |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 201 | |
| 202 | EXPECT_TRUE(findFRUHeader(0, 0, 0, getData, "error", blockData, offset)); |
| 203 | EXPECT_EQ(0, offset); |
| 204 | } |
| 205 | |
| 206 | TEST(FindFRUHeaderTest, TyanInvalidHeader) |
| 207 | { |
| 208 | std::vector<uint8_t> data = {'$', 'T', 'Y', 'A', 'N', '$', 0, 0}; |
| 209 | data.resize(0x6000 + I2C_SMBUS_BLOCK_MAX); |
Zev Weiss | 1525e85 | 2022-03-22 22:27:43 +0000 | [diff] [blame] | 210 | off_t offset = 0; |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 211 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData; |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 212 | auto getData = [&data](auto fl, auto fi, auto a, auto o, auto l, auto* b) { |
| 213 | return getDataTempl(data, fl, fi, a, o, l, b); |
| 214 | }; |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 215 | |
| 216 | EXPECT_FALSE(findFRUHeader(0, 0, 0, getData, "error", blockData, offset)); |
| 217 | } |
| 218 | |
| 219 | TEST(FindFRUHeaderTest, TyanNoData) |
| 220 | { |
| 221 | const std::vector<uint8_t> data = {'$', 'T', 'Y', 'A', 'N', '$', 0, 0}; |
Zev Weiss | 1525e85 | 2022-03-22 22:27:43 +0000 | [diff] [blame] | 222 | off_t offset = 0; |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 223 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData; |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 224 | auto getData = [&data](auto fl, auto fi, auto a, auto o, auto l, auto* b) { |
| 225 | return getDataTempl(data, fl, fi, a, o, l, b); |
| 226 | }; |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 227 | |
| 228 | EXPECT_FALSE(findFRUHeader(0, 0, 0, getData, "error", blockData, offset)); |
| 229 | } |
| 230 | |
| 231 | TEST(FindFRUHeaderTest, TyanValidHeader) |
| 232 | { |
| 233 | std::vector<uint8_t> data = {'$', 'T', 'Y', 'A', 'N', '$', 0, 0}; |
| 234 | data.resize(0x6000); |
| 235 | constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = { |
| 236 | 0x01, 0x00, 0x01, 0x02, 0x03, 0x04, 0x00, 0xf5}; |
| 237 | copy(fruHeader.begin(), fruHeader.end(), back_inserter(data)); |
| 238 | |
Zev Weiss | 1525e85 | 2022-03-22 22:27:43 +0000 | [diff] [blame] | 239 | off_t offset = 0; |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 240 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData; |
Andrew Jeffery | f8ae2ba | 2022-03-25 15:13:55 +1030 | [diff] [blame] | 241 | auto getData = [&data](auto fl, auto fi, auto a, auto o, auto l, auto* b) { |
| 242 | return getDataTempl(data, fl, fi, a, o, l, b); |
| 243 | }; |
Oskar Senft | bd4075f | 2021-10-05 23:42:43 -0400 | [diff] [blame] | 244 | |
| 245 | EXPECT_TRUE(findFRUHeader(0, 0, 0, getData, "error", blockData, offset)); |
| 246 | EXPECT_EQ(0x6000, offset); |
| 247 | } |