| // 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 "cable.hpp" | 
 | #include "commands.hpp" | 
 | #include "handler_mock.hpp" | 
 |  | 
 | #include <cstdint> | 
 | #include <cstring> | 
 | #include <vector> | 
 |  | 
 | #include <gtest/gtest.h> | 
 |  | 
 | #define MAX_IPMI_BUFFER 64 | 
 |  | 
 | using ::testing::Return; | 
 | using ::testing::StrEq; | 
 |  | 
 | namespace google | 
 | { | 
 | namespace ipmi | 
 | { | 
 |  | 
 | TEST(CableCommandTest, RequestTooSmall) | 
 | { | 
 |     std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck}; | 
 |     size_t dataLen = request.size(); | 
 |     std::uint8_t reply[MAX_IPMI_BUFFER]; | 
 |  | 
 |     HandlerMock hMock; | 
 |  | 
 |     EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID, | 
 |               cableCheck(request.data(), reply, &dataLen, &hMock)); | 
 | } | 
 |  | 
 | TEST(CableCommandTest, FailsLengthSanityCheck) | 
 | { | 
 |     // Minimum is three bytes, but a length of zero for the string is invalid. | 
 |     std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck, 0x00, | 
 |                                          'a'}; | 
 |  | 
 |     size_t dataLen = request.size(); | 
 |     std::uint8_t reply[MAX_IPMI_BUFFER]; | 
 |  | 
 |     HandlerMock hMock; | 
 |  | 
 |     EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID, | 
 |               cableCheck(request.data(), reply, &dataLen, &hMock)); | 
 | } | 
 |  | 
 | TEST(CableCommandTest, LengthTooLongForPacket) | 
 | { | 
 |     // The length of a the string, as specified is longer than string provided. | 
 |     std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck, 0x02, | 
 |                                          'a'}; | 
 |  | 
 |     size_t dataLen = request.size(); | 
 |     std::uint8_t reply[MAX_IPMI_BUFFER]; | 
 |  | 
 |     HandlerMock hMock; | 
 |  | 
 |     EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID, | 
 |               cableCheck(request.data(), reply, &dataLen, &hMock)); | 
 | } | 
 |  | 
 | TEST(CableCommandTest, ValidRequestValidReturn) | 
 | { | 
 |     std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck, 0x01, | 
 |                                          'a'}; | 
 |  | 
 |     size_t dataLen = request.size(); | 
 |     std::uint8_t reply[MAX_IPMI_BUFFER]; | 
 |  | 
 |     HandlerMock hMock; | 
 |  | 
 |     EXPECT_CALL(hMock, getRxPackets(StrEq("a"))).WillOnce(Return(0)); | 
 |     EXPECT_EQ(IPMI_CC_OK, cableCheck(request.data(), reply, &dataLen, &hMock)); | 
 |  | 
 |     // Check results. | 
 |     struct CableReply expectedReply, actualReply; | 
 |     expectedReply.subcommand = SysOEMCommands::SysCableCheck; | 
 |     expectedReply.value = 0; | 
 |  | 
 |     EXPECT_EQ(sizeof(expectedReply), dataLen); | 
 |     std::memcpy(&actualReply, reply, dataLen); | 
 |  | 
 |     EXPECT_EQ(expectedReply.subcommand, actualReply.subcommand); | 
 |     EXPECT_EQ(expectedReply.value, actualReply.value); | 
 | } | 
 |  | 
 | } // namespace ipmi | 
 | } // namespace google |