blob: 1eb8fd8a040af3c851fef69114618c46533d219f [file] [log] [blame]
Willy Tua2056e92021-10-10 13:36:16 -07001// 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
Patrick Venture0e9aae52020-08-13 13:07:09 -070015#include "commands.hpp"
Patrick Venture49f23ad2019-03-16 11:59:55 -070016#include "handler_mock.hpp"
Patrick Venture49f23ad2019-03-16 11:59:55 -070017#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
28using ::testing::Return;
29
30namespace google
31{
32namespace ipmi
33{
34
35TEST(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,
Patrick Venture45fad1b2019-03-18 16:52:14 -070046 pcieSlotCount(request.data(), reply, &dataLen, &hMock));
Patrick Venture49f23ad2019-03-16 11:59:55 -070047 EXPECT_EQ(expectedSize, reply[1]);
48}
49
50TEST(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,
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, 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,
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, 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,
Patrick Venture45fad1b2019-03-18 16:52:14 -070087 pcieSlotI2cBusMapping(request.data(), reply, &dataLen, &hMock));
Patrick Venture49f23ad2019-03-16 11:59:55 -070088}
89
90TEST(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,
Patrick Venture45fad1b2019-03-18 16:52:14 -0700106 pcieSlotI2cBusMapping(request.data(), reply, &dataLen, &hMock));
Patrick Venture49f23ad2019-03-16 11:59:55 -0700107 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