blob: 26c3725824e8629a72780e26f934e6c8115f5011 [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"
Willy Tuff3cd8e2021-09-14 22:49:55 -070017#include "helper.hpp"
Patrick Venture49f23ad2019-03-16 11:59:55 -070018#include "pcie_i2c.hpp"
19
20#include <cstdint>
21#include <cstring>
22#include <tuple>
23#include <vector>
24
25#include <gtest/gtest.h>
26
Patrick Venture49f23ad2019-03-16 11:59:55 -070027using ::testing::Return;
28
29namespace google
30{
31namespace ipmi
32{
33
34TEST(PcieI2cCommandTest, PcieSlotCountTest)
35{
Willy Tuff3cd8e2021-09-14 22:49:55 -070036 std::vector<std::uint8_t> request = {};
Patrick Venture49f23ad2019-03-16 11:59:55 -070037 size_t expectedSize = 3;
38
39 HandlerMock hMock;
40 EXPECT_CALL(hMock, buildI2cPcieMapping());
41 EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(expectedSize));
Willy Tuff3cd8e2021-09-14 22:49:55 -070042
43 auto reply = pcieSlotCount(request, &hMock);
44 auto result = ValidateReply(reply);
45 auto& data = result.second;
46
47 EXPECT_EQ(sizeof(struct PcieSlotCountReply), data.size());
48 EXPECT_EQ(SysOEMCommands::SysPcieSlotCount, result.first);
49 EXPECT_EQ(expectedSize, data[0]);
Patrick Venture49f23ad2019-03-16 11:59:55 -070050}
51
52TEST(PcieI2cCommandTest, PcieSlotEntryRequestTooShort)
53{
Willy Tuff3cd8e2021-09-14 22:49:55 -070054 std::vector<std::uint8_t> request = {};
Patrick Venture49f23ad2019-03-16 11:59:55 -070055
56 HandlerMock hMock;
Willy Tuff3cd8e2021-09-14 22:49:55 -070057 EXPECT_EQ(::ipmi::responseReqDataLenInvalid(),
58 pcieSlotI2cBusMapping(request, &hMock));
Patrick Venture49f23ad2019-03-16 11:59:55 -070059}
60
61TEST(PcieI2cCommandTest, PcieSlotEntryRequestUnsupportedByPlatform)
62{
63 // If there is no mapping in the device-tree, then the map is of size zero.
Willy Tuff3cd8e2021-09-14 22:49:55 -070064 std::vector<std::uint8_t> request = {0};
Patrick Venture49f23ad2019-03-16 11:59:55 -070065
66 HandlerMock hMock;
67 EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(0));
Willy Tuff3cd8e2021-09-14 22:49:55 -070068 EXPECT_EQ(::ipmi::responseInvalidReservationId(),
69 pcieSlotI2cBusMapping(request, &hMock));
Patrick Venture49f23ad2019-03-16 11:59:55 -070070}
71
72TEST(PcieI2cCommandTest, PcieSlotEntryRequestInvalidIndex)
73{
74 // index of 1 is invalid if length is 1.
Willy Tuff3cd8e2021-09-14 22:49:55 -070075 std::vector<std::uint8_t> request = {1};
Patrick Venture49f23ad2019-03-16 11:59:55 -070076
77 HandlerMock hMock;
78 EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(1));
Willy Tuff3cd8e2021-09-14 22:49:55 -070079 EXPECT_EQ(::ipmi::responseParmOutOfRange(),
80 pcieSlotI2cBusMapping(request, &hMock));
Patrick Venture49f23ad2019-03-16 11:59:55 -070081}
82
83TEST(PcieI2cCommandTest, PcieSlotEntryRequestValidIndex)
84{
85 unsigned int index = 0;
Willy Tuff3cd8e2021-09-14 22:49:55 -070086 std::vector<std::uint8_t> request = {static_cast<std::uint8_t>(index)};
Patrick Venture49f23ad2019-03-16 11:59:55 -070087 std::string slotName = "abcd";
88 std::uint32_t busNum = 5;
89
90 HandlerMock hMock;
91 EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(1));
92 EXPECT_CALL(hMock, getI2cEntry(index))
93 .WillOnce(Return(std::make_tuple(busNum, slotName)));
Willy Tuff3cd8e2021-09-14 22:49:55 -070094
95 auto reply = pcieSlotI2cBusMapping(request, &hMock);
96 auto result = ValidateReply(reply);
97 auto& data = result.second;
98
99 EXPECT_EQ(SysOEMCommands::SysPcieSlotI2cBusMapping, result.first);
100 EXPECT_EQ(busNum, data[0]);
101 EXPECT_EQ(slotName.length(), data[1]);
102 EXPECT_EQ(
103 slotName,
104 std::string(data.begin() + sizeof(struct PcieSlotI2cBusMappingReply),
105 data.end()));
Patrick Venture49f23ad2019-03-16 11:59:55 -0700106}
107
108} // namespace ipmi
109} // namespace google