blob: 1eb8fd8a040af3c851fef69114618c46533d219f [file] [log] [blame]
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "commands.hpp"
#include "handler_mock.hpp"
#include "pcie_i2c.hpp"
#include <cstdint>
#include <cstring>
#include <tuple>
#include <vector>
#include <gtest/gtest.h>
#define MAX_IPMI_BUFFER 64
using ::testing::Return;
namespace google
{
namespace ipmi
{
TEST(PcieI2cCommandTest, PcieSlotCountTest)
{
std::vector<std::uint8_t> request = {SysOEMCommands::SysPcieSlotCount};
size_t dataLen = request.size();
std::uint8_t reply[MAX_IPMI_BUFFER];
size_t expectedSize = 3;
HandlerMock hMock;
EXPECT_CALL(hMock, buildI2cPcieMapping());
EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(expectedSize));
EXPECT_EQ(IPMI_CC_OK,
pcieSlotCount(request.data(), reply, &dataLen, &hMock));
EXPECT_EQ(expectedSize, reply[1]);
}
TEST(PcieI2cCommandTest, PcieSlotEntryRequestTooShort)
{
std::vector<std::uint8_t> request = {
SysOEMCommands::SysPcieSlotI2cBusMapping};
size_t dataLen = request.size();
std::uint8_t reply[MAX_IPMI_BUFFER];
HandlerMock hMock;
EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
pcieSlotI2cBusMapping(request.data(), reply, &dataLen, &hMock));
}
TEST(PcieI2cCommandTest, PcieSlotEntryRequestUnsupportedByPlatform)
{
// If there is no mapping in the device-tree, then the map is of size zero.
std::vector<std::uint8_t> request = {
SysOEMCommands::SysPcieSlotI2cBusMapping, 0};
size_t dataLen = request.size();
std::uint8_t reply[MAX_IPMI_BUFFER];
HandlerMock hMock;
EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(0));
EXPECT_EQ(IPMI_CC_INVALID_RESERVATION_ID,
pcieSlotI2cBusMapping(request.data(), reply, &dataLen, &hMock));
}
TEST(PcieI2cCommandTest, PcieSlotEntryRequestInvalidIndex)
{
// index of 1 is invalid if length is 1.
std::vector<std::uint8_t> request = {
SysOEMCommands::SysPcieSlotI2cBusMapping, 1};
size_t dataLen = request.size();
std::uint8_t reply[MAX_IPMI_BUFFER];
HandlerMock hMock;
EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(1));
EXPECT_EQ(IPMI_CC_PARM_OUT_OF_RANGE,
pcieSlotI2cBusMapping(request.data(), reply, &dataLen, &hMock));
}
TEST(PcieI2cCommandTest, PcieSlotEntryRequestValidIndex)
{
unsigned int index = 0;
std::vector<std::uint8_t> request = {
SysOEMCommands::SysPcieSlotI2cBusMapping,
static_cast<std::uint8_t>(index)};
size_t dataLen = request.size();
std::uint8_t reply[MAX_IPMI_BUFFER];
std::string slotName = "abcd";
std::uint32_t busNum = 5;
HandlerMock hMock;
EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(1));
EXPECT_CALL(hMock, getI2cEntry(index))
.WillOnce(Return(std::make_tuple(busNum, slotName)));
EXPECT_EQ(IPMI_CC_OK,
pcieSlotI2cBusMapping(request.data(), reply, &dataLen, &hMock));
EXPECT_EQ(busNum, reply[1]);
EXPECT_EQ(slotName.length(), reply[2]);
EXPECT_EQ(0, std::memcmp(slotName.c_str(), &reply[3], reply[2]));
}
} // namespace ipmi
} // namespace google