blob: 094e3f9e7247ebf9cbec8ee7e22ea6ba05dfec02 [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}