Patrick Venture | a62466c | 2021-02-08 14:55:18 -0800 | [diff] [blame] | 1 | #include "FruUtils.hpp" |
| 2 | |
| 3 | #include <array> |
| 4 | |
| 5 | #include "gtest/gtest.h" |
| 6 | |
| 7 | extern "C" |
| 8 | { |
| 9 | // Include for I2C_SMBUS_BLOCK_MAX |
| 10 | #include <linux/i2c.h> |
| 11 | } |
| 12 | |
| 13 | TEST(ValidateHeaderTest, InvalidFruVersionReturnsFalse) |
| 14 | { |
| 15 | // Validates the FruVersion is checked for the only legal value. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame^] | 16 | constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = { |
Patrick Venture | a62466c | 2021-02-08 14:55:18 -0800 | [diff] [blame] | 17 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 18 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame^] | 19 | EXPECT_FALSE(validateHeader(fruHeader)); |
Patrick Venture | a62466c | 2021-02-08 14:55:18 -0800 | [diff] [blame] | 20 | } |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 21 | |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 22 | TEST(ValidateHeaderTest, InvalidReservedReturnsFalse) |
| 23 | { |
| 24 | // Validates the reserved bit(7:4) of first bytes. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame^] | 25 | constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = { |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 26 | 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 27 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame^] | 28 | EXPECT_FALSE(validateHeader(fruHeader)); |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | TEST(ValidateHeaderTest, InvalidPaddingReturnsFalse) |
| 32 | { |
| 33 | // Validates the padding byte (7th byte). |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame^] | 34 | constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = { |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 35 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00}; |
| 36 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame^] | 37 | EXPECT_FALSE(validateHeader(fruHeader)); |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | TEST(ValidateHeaderTest, InvalidChecksumReturnsFalse) |
| 41 | { |
| 42 | // Validates the checksum, check for incorrect value. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame^] | 43 | constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = { |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 44 | 0x01, 0x00, 0x01, 0x02, 0x03, 0x04, 0x00, 0x00}; |
| 45 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame^] | 46 | EXPECT_FALSE(validateHeader(fruHeader)); |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | TEST(ValidateHeaderTest, ValidChecksumReturnsTrue) |
| 50 | { |
| 51 | // Validates the checksum, check for correct value. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame^] | 52 | constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = { |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 53 | 0x01, 0x00, 0x01, 0x02, 0x03, 0x04, 0x00, 0xf5}; |
| 54 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame^] | 55 | EXPECT_TRUE(validateHeader(fruHeader)); |
Vijay Khemka | 7a682a3 | 2021-04-12 22:15:00 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 58 | TEST(VerifyOffsetTest, EmptyFruDataReturnsFalse) |
| 59 | { |
| 60 | // Validates the FruData size is checked for non empty. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame^] | 61 | std::vector<uint8_t> fruData = {}; |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 62 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame^] | 63 | EXPECT_FALSE(verifyOffset(fruData, fruAreas::fruAreaChassis, 0)); |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | TEST(VerifyOffsetTest, AreaOutOfRangeReturnsFalse) |
| 67 | { |
| 68 | // Validates the FruArea value, check if it is within range. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame^] | 69 | const std::vector<uint8_t> fruData = {0x01, 0x00, 0x00, 0x00, 0x00, |
| 70 | 0x00, 0x00, 0x00, 0x00}; |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 71 | |
| 72 | unsigned int areaOutOfRange = 8; |
| 73 | EXPECT_FALSE( |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame^] | 74 | verifyOffset(fruData, static_cast<fruAreas>(areaOutOfRange), 0)); |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | TEST(VerifyOffsetTest, OverlapNextAreaReturnsFalse) |
| 78 | { |
| 79 | // Validates the Overlap of offsets with overlapped values. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame^] | 80 | const std::vector<uint8_t> fruData = {0x01, 0x00, 0x01, 0x02, 0x03, |
| 81 | 0x04, 0x00, 0x00, 0x00}; |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 82 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame^] | 83 | EXPECT_FALSE(verifyOffset(fruData, fruAreas::fruAreaChassis, 2)); |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | TEST(VerifyOffsetTest, OverlapPrevAreaReturnsFalse) |
| 87 | { |
| 88 | // Validates the Overlap of offsets with overlapped values. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame^] | 89 | const std::vector<uint8_t> fruData = {0x01, 0x00, 0x01, 0x03, 0x02, |
| 90 | 0x07, 0x00, 0x00, 0x00}; |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 91 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame^] | 92 | EXPECT_FALSE(verifyOffset(fruData, fruAreas::fruAreaProduct, 2)); |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | TEST(VerifyOffsetTest, ValidInputDataNoOverlapReturnsTrue) |
| 96 | { |
| 97 | // Validates all inputs with expected value and no overlap. |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame^] | 98 | const std::vector<uint8_t> fruData = {0x01, 0x00, 0x01, 0x02, 0x03, |
| 99 | 0x04, 0x00, 0x00, 0x00}; |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 100 | |
Ed Tanous | 07d467b | 2021-02-23 14:48:37 -0800 | [diff] [blame^] | 101 | EXPECT_TRUE(verifyOffset(fruData, fruAreas::fruAreaChassis, 1)); |
Vijay Khemka | 1694ef6 | 2021-02-12 23:14:49 +0000 | [diff] [blame] | 102 | } |