Willy Tu | a2056e9 | 2021-10-10 13:36:16 -0700 | [diff] [blame] | 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 | |
Patrick Venture | 0e9aae5 | 2020-08-13 13:07:09 -0700 | [diff] [blame] | 15 | #include "commands.hpp" |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame] | 16 | #include "handler_mock.hpp" |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 17 | #include "helper.hpp" |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame] | 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 | |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame] | 27 | using ::testing::Return; |
| 28 | |
| 29 | namespace google |
| 30 | { |
| 31 | namespace ipmi |
| 32 | { |
| 33 | |
| 34 | TEST(PcieI2cCommandTest, PcieSlotCountTest) |
| 35 | { |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 36 | std::vector<std::uint8_t> request = {}; |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame] | 37 | size_t expectedSize = 3; |
| 38 | |
| 39 | HandlerMock hMock; |
| 40 | EXPECT_CALL(hMock, buildI2cPcieMapping()); |
| 41 | EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(expectedSize)); |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 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]); |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | TEST(PcieI2cCommandTest, PcieSlotEntryRequestTooShort) |
| 53 | { |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 54 | std::vector<std::uint8_t> request = {}; |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame] | 55 | |
| 56 | HandlerMock hMock; |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 57 | EXPECT_EQ(::ipmi::responseReqDataLenInvalid(), |
| 58 | pcieSlotI2cBusMapping(request, &hMock)); |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | TEST(PcieI2cCommandTest, PcieSlotEntryRequestUnsupportedByPlatform) |
| 62 | { |
| 63 | // If there is no mapping in the device-tree, then the map is of size zero. |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 64 | std::vector<std::uint8_t> request = {0}; |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame] | 65 | |
| 66 | HandlerMock hMock; |
| 67 | EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(0)); |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 68 | EXPECT_EQ(::ipmi::responseInvalidReservationId(), |
| 69 | pcieSlotI2cBusMapping(request, &hMock)); |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | TEST(PcieI2cCommandTest, PcieSlotEntryRequestInvalidIndex) |
| 73 | { |
| 74 | // index of 1 is invalid if length is 1. |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 75 | std::vector<std::uint8_t> request = {1}; |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame] | 76 | |
| 77 | HandlerMock hMock; |
| 78 | EXPECT_CALL(hMock, getI2cPcieMappingSize()).WillOnce(Return(1)); |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 79 | EXPECT_EQ(::ipmi::responseParmOutOfRange(), |
| 80 | pcieSlotI2cBusMapping(request, &hMock)); |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | TEST(PcieI2cCommandTest, PcieSlotEntryRequestValidIndex) |
| 84 | { |
| 85 | unsigned int index = 0; |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 86 | std::vector<std::uint8_t> request = {static_cast<std::uint8_t>(index)}; |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame] | 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))); |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 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())); |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | } // namespace ipmi |
| 109 | } // namespace google |