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 "helper.hpp"
18 #include "pcie_i2c.hpp"
19
20 #include <cstdint>
21 #include <cstring>
22 #include <tuple>
23 #include <vector>
24
25 #include <gtest/gtest.h>
26
27 using ::testing::Return;
28
29 namespace google
30 {
31 namespace ipmi
32 {
33
TEST(PcieI2cCommandTest,PcieSlotCountTest)34 TEST(PcieI2cCommandTest, PcieSlotCountTest)
35 {
36 std::vector<std::uint8_t> request = {};
37 size_t expectedSize = 3;
38
39 HandlerMock hMock;
40 EXPECT_CALL(hMock, buildI2cPcieMapping());
41 EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(expectedSize));
42
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]);
50 }
51
TEST(PcieI2cCommandTest,PcieSlotEntryRequestTooShort)52 TEST(PcieI2cCommandTest, PcieSlotEntryRequestTooShort)
53 {
54 std::vector<std::uint8_t> request = {};
55
56 HandlerMock hMock;
57 EXPECT_EQ(::ipmi::responseReqDataLenInvalid(),
58 pcieSlotI2cBusMapping(request, &hMock));
59 }
60
TEST(PcieI2cCommandTest,PcieSlotEntryRequestUnsupportedByPlatform)61 TEST(PcieI2cCommandTest, PcieSlotEntryRequestUnsupportedByPlatform)
62 {
63 // If there is no mapping in the device-tree, then the map is of size zero.
64 std::vector<std::uint8_t> request = {0};
65
66 HandlerMock hMock;
67 EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(0));
68 EXPECT_EQ(::ipmi::responseInvalidReservationId(),
69 pcieSlotI2cBusMapping(request, &hMock));
70 }
71
TEST(PcieI2cCommandTest,PcieSlotEntryRequestInvalidIndex)72 TEST(PcieI2cCommandTest, PcieSlotEntryRequestInvalidIndex)
73 {
74 // index of 1 is invalid if length is 1.
75 std::vector<std::uint8_t> request = {1};
76
77 HandlerMock hMock;
78 EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(1));
79 EXPECT_EQ(::ipmi::responseParmOutOfRange(),
80 pcieSlotI2cBusMapping(request, &hMock));
81 }
82
TEST(PcieI2cCommandTest,PcieSlotEntryRequestValidIndex)83 TEST(PcieI2cCommandTest, PcieSlotEntryRequestValidIndex)
84 {
85 unsigned int index = 0;
86 std::vector<std::uint8_t> request = {static_cast<std::uint8_t>(index)};
87 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)));
94
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()));
106 }
107
108 } // namespace ipmi
109 } // namespace google
110