blob: 11a1aba21012c57fb7c4af909291ec7c801161fc [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.
Ed Tanous07d467b2021-02-23 14:48:37 -080016 constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = {
Patrick Venturea62466c2021-02-08 14:55:18 -080017 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
18
Ed Tanous07d467b2021-02-23 14:48:37 -080019 EXPECT_FALSE(validateHeader(fruHeader));
Patrick Venturea62466c2021-02-08 14:55:18 -080020}
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.
Ed Tanous07d467b2021-02-23 14:48:37 -080025 constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = {
Vijay Khemka7a682a32021-04-12 22:15:00 +000026 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
27
Ed Tanous07d467b2021-02-23 14:48:37 -080028 EXPECT_FALSE(validateHeader(fruHeader));
Vijay Khemka7a682a32021-04-12 22:15:00 +000029}
30
31TEST(ValidateHeaderTest, InvalidPaddingReturnsFalse)
32{
33 // Validates the padding byte (7th byte).
Ed Tanous07d467b2021-02-23 14:48:37 -080034 constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = {
Vijay Khemka7a682a32021-04-12 22:15:00 +000035 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00};
36
Ed Tanous07d467b2021-02-23 14:48:37 -080037 EXPECT_FALSE(validateHeader(fruHeader));
Vijay Khemka7a682a32021-04-12 22:15:00 +000038}
39
40TEST(ValidateHeaderTest, InvalidChecksumReturnsFalse)
41{
42 // Validates the checksum, check for incorrect value.
Ed Tanous07d467b2021-02-23 14:48:37 -080043 constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = {
Vijay Khemka7a682a32021-04-12 22:15:00 +000044 0x01, 0x00, 0x01, 0x02, 0x03, 0x04, 0x00, 0x00};
45
Ed Tanous07d467b2021-02-23 14:48:37 -080046 EXPECT_FALSE(validateHeader(fruHeader));
Vijay Khemka7a682a32021-04-12 22:15:00 +000047}
48
49TEST(ValidateHeaderTest, ValidChecksumReturnsTrue)
50{
51 // Validates the checksum, check for correct value.
Ed Tanous07d467b2021-02-23 14:48:37 -080052 constexpr std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> fruHeader = {
Vijay Khemka7a682a32021-04-12 22:15:00 +000053 0x01, 0x00, 0x01, 0x02, 0x03, 0x04, 0x00, 0xf5};
54
Ed Tanous07d467b2021-02-23 14:48:37 -080055 EXPECT_TRUE(validateHeader(fruHeader));
Vijay Khemka7a682a32021-04-12 22:15:00 +000056}
57
Vijay Khemka1694ef62021-02-12 23:14:49 +000058TEST(VerifyOffsetTest, EmptyFruDataReturnsFalse)
59{
60 // Validates the FruData size is checked for non empty.
Ed Tanous07d467b2021-02-23 14:48:37 -080061 std::vector<uint8_t> fruData = {};
Vijay Khemka1694ef62021-02-12 23:14:49 +000062
Ed Tanous07d467b2021-02-23 14:48:37 -080063 EXPECT_FALSE(verifyOffset(fruData, fruAreas::fruAreaChassis, 0));
Vijay Khemka1694ef62021-02-12 23:14:49 +000064}
65
66TEST(VerifyOffsetTest, AreaOutOfRangeReturnsFalse)
67{
68 // Validates the FruArea value, check if it is within range.
Ed Tanous07d467b2021-02-23 14:48:37 -080069 const std::vector<uint8_t> fruData = {0x01, 0x00, 0x00, 0x00, 0x00,
70 0x00, 0x00, 0x00, 0x00};
Vijay Khemka1694ef62021-02-12 23:14:49 +000071
72 unsigned int areaOutOfRange = 8;
73 EXPECT_FALSE(
Ed Tanous07d467b2021-02-23 14:48:37 -080074 verifyOffset(fruData, static_cast<fruAreas>(areaOutOfRange), 0));
Vijay Khemka1694ef62021-02-12 23:14:49 +000075}
76
77TEST(VerifyOffsetTest, OverlapNextAreaReturnsFalse)
78{
79 // Validates the Overlap of offsets with overlapped values.
Ed Tanous07d467b2021-02-23 14:48:37 -080080 const std::vector<uint8_t> fruData = {0x01, 0x00, 0x01, 0x02, 0x03,
81 0x04, 0x00, 0x00, 0x00};
Vijay Khemka1694ef62021-02-12 23:14:49 +000082
Ed Tanous07d467b2021-02-23 14:48:37 -080083 EXPECT_FALSE(verifyOffset(fruData, fruAreas::fruAreaChassis, 2));
Vijay Khemka1694ef62021-02-12 23:14:49 +000084}
85
86TEST(VerifyOffsetTest, OverlapPrevAreaReturnsFalse)
87{
88 // Validates the Overlap of offsets with overlapped values.
Ed Tanous07d467b2021-02-23 14:48:37 -080089 const std::vector<uint8_t> fruData = {0x01, 0x00, 0x01, 0x03, 0x02,
90 0x07, 0x00, 0x00, 0x00};
Vijay Khemka1694ef62021-02-12 23:14:49 +000091
Ed Tanous07d467b2021-02-23 14:48:37 -080092 EXPECT_FALSE(verifyOffset(fruData, fruAreas::fruAreaProduct, 2));
Vijay Khemka1694ef62021-02-12 23:14:49 +000093}
94
95TEST(VerifyOffsetTest, ValidInputDataNoOverlapReturnsTrue)
96{
97 // Validates all inputs with expected value and no overlap.
Ed Tanous07d467b2021-02-23 14:48:37 -080098 const std::vector<uint8_t> fruData = {0x01, 0x00, 0x01, 0x02, 0x03,
99 0x04, 0x00, 0x00, 0x00};
Vijay Khemka1694ef62021-02-12 23:14:49 +0000100
Ed Tanous07d467b2021-02-23 14:48:37 -0800101 EXPECT_TRUE(verifyOffset(fruData, fruAreas::fruAreaChassis, 1));
Vijay Khemka1694ef62021-02-12 23:14:49 +0000102}
Andrew Jeffery8dacf6a2021-08-02 22:17:18 +0930103
104TEST(VerifyChecksumTest, EmptyInput)
105{
106 std::vector<uint8_t> data = {};
107
108 EXPECT_EQ(calculateChecksum(data), 0);
109}
110
111TEST(VerifyChecksumTest, SingleOneInput)
112{
113 std::vector<uint8_t> data(1, 1);
114
115 EXPECT_EQ(calculateChecksum(data), 255);
116}
117
118TEST(VerifyChecksumTest, AllOneInput)
119{
120 std::vector<uint8_t> data(256, 1);
121
122 EXPECT_EQ(calculateChecksum(data), 0);
123}
124
125TEST(VerifyChecksumTest, WrapBoundaryLow)
126{
127 std::vector<uint8_t> data = { 255, 0 };
128
129 EXPECT_EQ(calculateChecksum(data), 1);
130}
131
132TEST(VerifyChecksumTest, WrapBoundaryExact)
133{
134 std::vector<uint8_t> data = { 255, 1 };
135
136 EXPECT_EQ(calculateChecksum(data), 0);
137}
138
139TEST(VerifyChecksumTest, WrapBoundaryHigh)
140{
141 std::vector<uint8_t> data = { 255, 2 };
142
143 EXPECT_EQ(calculateChecksum(data), 255);
144}