1 #include "commands.hpp"
2 #include "handler_mock.hpp"
3 #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 
14 using ::testing::Return;
15 
16 namespace google
17 {
18 namespace ipmi
19 {
20 
21 TEST(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,
32               pcieSlotCount(request.data(), reply, &dataLen, &hMock));
33     EXPECT_EQ(expectedSize, reply[1]);
34 }
35 
36 TEST(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,
45               pcieSlotI2cBusMapping(request.data(), reply, &dataLen, &hMock));
46 }
47 
48 TEST(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,
59               pcieSlotI2cBusMapping(request.data(), reply, &dataLen, &hMock));
60 }
61 
62 TEST(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,
73               pcieSlotI2cBusMapping(request.data(), reply, &dataLen, &hMock));
74 }
75 
76 TEST(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,
92               pcieSlotI2cBusMapping(request.data(), reply, &dataLen, &hMock));
93     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
100