blob: 796d97aee43144e8d15da39f537da03ee705b227 [file] [log] [blame]
Patrick Venture49f23ad2019-03-16 11:59:55 -07001#include "handler_mock.hpp"
Patrick Ventureeff1f2e2020-08-05 08:27:36 -07002#include "ipmi.hpp"
Patrick Venture49f23ad2019-03-16 11:59:55 -07003#include "pcie_i2c.hpp"
4
5#include <cstdint>
6#include <cstring>
7#include <tuple>
8#include <vector>
9
10#include <gtest/gtest.h>
11
12#define MAX_IPMI_BUFFER 64
13
14using ::testing::Return;
15
16namespace google
17{
18namespace ipmi
19{
20
21TEST(PcieI2cCommandTest, PcieSlotCountTest)
22{
23 std::vector<std::uint8_t> request = {SysOEMCommands::SysPcieSlotCount};
24 size_t dataLen = request.size();
25 std::uint8_t reply[MAX_IPMI_BUFFER];
26 size_t expectedSize = 3;
27
28 HandlerMock hMock;
29 EXPECT_CALL(hMock, buildI2cPcieMapping());
30 EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(expectedSize));
31 EXPECT_EQ(IPMI_CC_OK,
Patrick Venture45fad1b2019-03-18 16:52:14 -070032 pcieSlotCount(request.data(), reply, &dataLen, &hMock));
Patrick Venture49f23ad2019-03-16 11:59:55 -070033 EXPECT_EQ(expectedSize, reply[1]);
34}
35
36TEST(PcieI2cCommandTest, PcieSlotEntryRequestTooShort)
37{
38 std::vector<std::uint8_t> request = {
39 SysOEMCommands::SysPcieSlotI2cBusMapping};
40 size_t dataLen = request.size();
41 std::uint8_t reply[MAX_IPMI_BUFFER];
42
43 HandlerMock hMock;
44 EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
Patrick Venture45fad1b2019-03-18 16:52:14 -070045 pcieSlotI2cBusMapping(request.data(), reply, &dataLen, &hMock));
Patrick Venture49f23ad2019-03-16 11:59:55 -070046}
47
48TEST(PcieI2cCommandTest, PcieSlotEntryRequestUnsupportedByPlatform)
49{
50 // If there is no mapping in the device-tree, then the map is of size zero.
51 std::vector<std::uint8_t> request = {
52 SysOEMCommands::SysPcieSlotI2cBusMapping, 0};
53 size_t dataLen = request.size();
54 std::uint8_t reply[MAX_IPMI_BUFFER];
55
56 HandlerMock hMock;
57 EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(0));
58 EXPECT_EQ(IPMI_CC_INVALID_RESERVATION_ID,
Patrick Venture45fad1b2019-03-18 16:52:14 -070059 pcieSlotI2cBusMapping(request.data(), reply, &dataLen, &hMock));
Patrick Venture49f23ad2019-03-16 11:59:55 -070060}
61
62TEST(PcieI2cCommandTest, PcieSlotEntryRequestInvalidIndex)
63{
64 // index of 1 is invalid if length is 1.
65 std::vector<std::uint8_t> request = {
66 SysOEMCommands::SysPcieSlotI2cBusMapping, 1};
67 size_t dataLen = request.size();
68 std::uint8_t reply[MAX_IPMI_BUFFER];
69
70 HandlerMock hMock;
71 EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(1));
72 EXPECT_EQ(IPMI_CC_PARM_OUT_OF_RANGE,
Patrick Venture45fad1b2019-03-18 16:52:14 -070073 pcieSlotI2cBusMapping(request.data(), reply, &dataLen, &hMock));
Patrick Venture49f23ad2019-03-16 11:59:55 -070074}
75
76TEST(PcieI2cCommandTest, PcieSlotEntryRequestValidIndex)
77{
78 unsigned int index = 0;
79 std::vector<std::uint8_t> request = {
80 SysOEMCommands::SysPcieSlotI2cBusMapping,
81 static_cast<std::uint8_t>(index)};
82 size_t dataLen = request.size();
83 std::uint8_t reply[MAX_IPMI_BUFFER];
84 std::string slotName = "abcd";
85 std::uint32_t busNum = 5;
86
87 HandlerMock hMock;
88 EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(1));
89 EXPECT_CALL(hMock, getI2cEntry(index))
90 .WillOnce(Return(std::make_tuple(busNum, slotName)));
91 EXPECT_EQ(IPMI_CC_OK,
Patrick Venture45fad1b2019-03-18 16:52:14 -070092 pcieSlotI2cBusMapping(request.data(), reply, &dataLen, &hMock));
Patrick Venture49f23ad2019-03-16 11:59:55 -070093 EXPECT_EQ(busNum, reply[1]);
94 EXPECT_EQ(slotName.length(), reply[2]);
95 EXPECT_EQ(0, std::memcmp(slotName.c_str(), &reply[3], reply[2]));
96}
97
98} // namespace ipmi
99} // namespace google