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.
16     constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = {
17         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
18 
19     EXPECT_FALSE(validateHeader(fruHeader));
20 }
21 
22 TEST(ValidateHeaderTest, InvalidReservedReturnsFalse)
23 {
24     // Validates the reserved bit(7:4) of first bytes.
25     constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = {
26         0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
27 
28     EXPECT_FALSE(validateHeader(fruHeader));
29 }
30 
31 TEST(ValidateHeaderTest, InvalidPaddingReturnsFalse)
32 {
33     // Validates the padding byte (7th byte).
34     constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = {
35         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00};
36 
37     EXPECT_FALSE(validateHeader(fruHeader));
38 }
39 
40 TEST(ValidateHeaderTest, InvalidChecksumReturnsFalse)
41 {
42     // Validates the checksum, check for incorrect value.
43     constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = {
44         0x01, 0x00, 0x01, 0x02, 0x03, 0x04, 0x00, 0x00};
45 
46     EXPECT_FALSE(validateHeader(fruHeader));
47 }
48 
49 TEST(ValidateHeaderTest, ValidChecksumReturnsTrue)
50 {
51     // Validates the checksum, check for correct value.
52     constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = {
53         0x01, 0x00, 0x01, 0x02, 0x03, 0x04, 0x00, 0xf5};
54 
55     EXPECT_TRUE(validateHeader(fruHeader));
56 }
57 
58 TEST(VerifyOffsetTest, EmptyFruDataReturnsFalse)
59 {
60     // Validates the FruData size is checked for non empty.
61     std::vector<uint8_t> fruData = {};
62 
63     EXPECT_FALSE(verifyOffset(fruData, fruAreas::fruAreaChassis, 0));
64 }
65 
66 TEST(VerifyOffsetTest, AreaOutOfRangeReturnsFalse)
67 {
68     // Validates the FruArea value, check if it is within range.
69     const std::vector<uint8_t> fruData = {0x01, 0x00, 0x00, 0x00, 0x00,
70                                           0x00, 0x00, 0x00, 0x00};
71 
72     unsigned int areaOutOfRange = 8;
73     EXPECT_FALSE(
74         verifyOffset(fruData, static_cast<fruAreas>(areaOutOfRange), 0));
75 }
76 
77 TEST(VerifyOffsetTest, OverlapNextAreaReturnsFalse)
78 {
79     // Validates the Overlap of offsets with overlapped values.
80     const std::vector<uint8_t> fruData = {0x01, 0x00, 0x01, 0x02, 0x03,
81                                           0x04, 0x00, 0x00, 0x00};
82 
83     EXPECT_FALSE(verifyOffset(fruData, fruAreas::fruAreaChassis, 2));
84 }
85 
86 TEST(VerifyOffsetTest, OverlapPrevAreaReturnsFalse)
87 {
88     // Validates the Overlap of offsets with overlapped values.
89     const std::vector<uint8_t> fruData = {0x01, 0x00, 0x01, 0x03, 0x02,
90                                           0x07, 0x00, 0x00, 0x00};
91 
92     EXPECT_FALSE(verifyOffset(fruData, fruAreas::fruAreaProduct, 2));
93 }
94 
95 TEST(VerifyOffsetTest, ValidInputDataNoOverlapReturnsTrue)
96 {
97     // Validates all inputs with expected value and no overlap.
98     const std::vector<uint8_t> fruData = {0x01, 0x00, 0x01, 0x02, 0x03,
99                                           0x04, 0x00, 0x00, 0x00};
100 
101     EXPECT_TRUE(verifyOffset(fruData, fruAreas::fruAreaChassis, 1));
102 }
103 
104 TEST(VerifyChecksumTest, EmptyInput)
105 {
106     std::vector<uint8_t> data = {};
107 
108     EXPECT_EQ(calculateChecksum(data), 0);
109 }
110 
111 TEST(VerifyChecksumTest, SingleOneInput)
112 {
113     std::vector<uint8_t> data(1, 1);
114 
115     EXPECT_EQ(calculateChecksum(data), 255);
116 }
117 
118 TEST(VerifyChecksumTest, AllOneInput)
119 {
120     std::vector<uint8_t> data(256, 1);
121 
122     EXPECT_EQ(calculateChecksum(data), 0);
123 }
124 
125 TEST(VerifyChecksumTest, WrapBoundaryLow)
126 {
127     std::vector<uint8_t> data = {255, 0};
128 
129     EXPECT_EQ(calculateChecksum(data), 1);
130 }
131 
132 TEST(VerifyChecksumTest, WrapBoundaryExact)
133 {
134     std::vector<uint8_t> data = {255, 1};
135 
136     EXPECT_EQ(calculateChecksum(data), 0);
137 }
138 
139 TEST(VerifyChecksumTest, WrapBoundaryHigh)
140 {
141     std::vector<uint8_t> data = {255, 2};
142 
143     EXPECT_EQ(calculateChecksum(data), 255);
144 }
145