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 "cable.hpp" 16 #include "commands.hpp" 17 #include "handler_mock.hpp" 18 19 #include <cstdint> 20 #include <cstring> 21 #include <vector> 22 23 #include <gtest/gtest.h> 24 25 #define MAX_IPMI_BUFFER 64 26 27 using ::testing::Return; 28 using ::testing::StrEq; 29 30 namespace google 31 { 32 namespace ipmi 33 { 34 35 TEST(CableCommandTest, RequestTooSmall) 36 { 37 std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck}; 38 size_t dataLen = request.size(); 39 std::uint8_t reply[MAX_IPMI_BUFFER]; 40 41 HandlerMock hMock; 42 43 EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID, 44 cableCheck(request.data(), reply, &dataLen, &hMock)); 45 } 46 47 TEST(CableCommandTest, FailsLengthSanityCheck) 48 { 49 // Minimum is three bytes, but a length of zero for the string is invalid. 50 std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck, 0x00, 51 'a'}; 52 53 size_t dataLen = request.size(); 54 std::uint8_t reply[MAX_IPMI_BUFFER]; 55 56 HandlerMock hMock; 57 58 EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID, 59 cableCheck(request.data(), reply, &dataLen, &hMock)); 60 } 61 62 TEST(CableCommandTest, LengthTooLongForPacket) 63 { 64 // The length of a the string, as specified is longer than string provided. 65 std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck, 0x02, 66 'a'}; 67 68 size_t dataLen = request.size(); 69 std::uint8_t reply[MAX_IPMI_BUFFER]; 70 71 HandlerMock hMock; 72 73 EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID, 74 cableCheck(request.data(), reply, &dataLen, &hMock)); 75 } 76 77 TEST(CableCommandTest, ValidRequestValidReturn) 78 { 79 std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck, 0x01, 80 'a'}; 81 82 size_t dataLen = request.size(); 83 std::uint8_t reply[MAX_IPMI_BUFFER]; 84 85 HandlerMock hMock; 86 87 EXPECT_CALL(hMock, getRxPackets(StrEq("a"))).WillOnce(Return(0)); 88 EXPECT_EQ(IPMI_CC_OK, cableCheck(request.data(), reply, &dataLen, &hMock)); 89 90 // Check results. 91 struct CableReply expectedReply, actualReply; 92 expectedReply.subcommand = SysOEMCommands::SysCableCheck; 93 expectedReply.value = 0; 94 95 EXPECT_EQ(sizeof(expectedReply), dataLen); 96 std::memcpy(&actualReply, reply, dataLen); 97 98 EXPECT_EQ(expectedReply.subcommand, actualReply.subcommand); 99 EXPECT_EQ(expectedReply.value, actualReply.value); 100 } 101 102 } // namespace ipmi 103 } // namespace google 104