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