1 // Copyright 2021 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "commands.hpp" 16 #include "handler_mock.hpp" 17 #include "pcie_i2c.hpp" 18 19 #include <cstdint> 20 #include <cstring> 21 #include <tuple> 22 #include <vector> 23 24 #include <gtest/gtest.h> 25 26 #define MAX_IPMI_BUFFER 64 27 28 using ::testing::Return; 29 30 namespace google 31 { 32 namespace ipmi 33 { 34 35 TEST(PcieI2cCommandTest, PcieSlotCountTest) 36 { 37 std::vector<std::uint8_t> request = {SysOEMCommands::SysPcieSlotCount}; 38 size_t dataLen = request.size(); 39 std::uint8_t reply[MAX_IPMI_BUFFER]; 40 size_t expectedSize = 3; 41 42 HandlerMock hMock; 43 EXPECT_CALL(hMock, buildI2cPcieMapping()); 44 EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(expectedSize)); 45 EXPECT_EQ(IPMI_CC_OK, 46 pcieSlotCount(request.data(), reply, &dataLen, &hMock)); 47 EXPECT_EQ(expectedSize, reply[1]); 48 } 49 50 TEST(PcieI2cCommandTest, PcieSlotEntryRequestTooShort) 51 { 52 std::vector<std::uint8_t> request = { 53 SysOEMCommands::SysPcieSlotI2cBusMapping}; 54 size_t dataLen = request.size(); 55 std::uint8_t reply[MAX_IPMI_BUFFER]; 56 57 HandlerMock hMock; 58 EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID, 59 pcieSlotI2cBusMapping(request.data(), reply, &dataLen, &hMock)); 60 } 61 62 TEST(PcieI2cCommandTest, PcieSlotEntryRequestUnsupportedByPlatform) 63 { 64 // If there is no mapping in the device-tree, then the map is of size zero. 65 std::vector<std::uint8_t> request = { 66 SysOEMCommands::SysPcieSlotI2cBusMapping, 0}; 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(0)); 72 EXPECT_EQ(IPMI_CC_INVALID_RESERVATION_ID, 73 pcieSlotI2cBusMapping(request.data(), reply, &dataLen, &hMock)); 74 } 75 76 TEST(PcieI2cCommandTest, PcieSlotEntryRequestInvalidIndex) 77 { 78 // index of 1 is invalid if length is 1. 79 std::vector<std::uint8_t> request = { 80 SysOEMCommands::SysPcieSlotI2cBusMapping, 1}; 81 size_t dataLen = request.size(); 82 std::uint8_t reply[MAX_IPMI_BUFFER]; 83 84 HandlerMock hMock; 85 EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(1)); 86 EXPECT_EQ(IPMI_CC_PARM_OUT_OF_RANGE, 87 pcieSlotI2cBusMapping(request.data(), reply, &dataLen, &hMock)); 88 } 89 90 TEST(PcieI2cCommandTest, PcieSlotEntryRequestValidIndex) 91 { 92 unsigned int index = 0; 93 std::vector<std::uint8_t> request = { 94 SysOEMCommands::SysPcieSlotI2cBusMapping, 95 static_cast<std::uint8_t>(index)}; 96 size_t dataLen = request.size(); 97 std::uint8_t reply[MAX_IPMI_BUFFER]; 98 std::string slotName = "abcd"; 99 std::uint32_t busNum = 5; 100 101 HandlerMock hMock; 102 EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(1)); 103 EXPECT_CALL(hMock, getI2cEntry(index)) 104 .WillOnce(Return(std::make_tuple(busNum, slotName))); 105 EXPECT_EQ(IPMI_CC_OK, 106 pcieSlotI2cBusMapping(request.data(), reply, &dataLen, &hMock)); 107 EXPECT_EQ(busNum, reply[1]); 108 EXPECT_EQ(slotName.length(), reply[2]); 109 EXPECT_EQ(0, std::memcmp(slotName.c_str(), &reply[3], reply[2])); 110 } 111 112 } // namespace ipmi 113 } // namespace google 114