Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 1 | #include "cable.hpp" |
Patrick Venture | 0e9aae5 | 2020-08-13 13:07:09 -0700 | [diff] [blame^] | 2 | #include "commands.hpp" |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 3 | #include "handler_mock.hpp" |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 4 | |
| 5 | #include <cstdint> |
| 6 | #include <cstring> |
| 7 | #include <vector> |
| 8 | |
| 9 | #include <gtest/gtest.h> |
| 10 | |
| 11 | #define MAX_IPMI_BUFFER 64 |
| 12 | |
| 13 | using ::testing::Return; |
| 14 | using ::testing::StrEq; |
| 15 | |
| 16 | namespace google |
| 17 | { |
| 18 | namespace ipmi |
| 19 | { |
| 20 | |
| 21 | TEST(CableCommandTest, RequestTooSmall) |
| 22 | { |
| 23 | std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck}; |
| 24 | size_t dataLen = request.size(); |
| 25 | std::uint8_t reply[MAX_IPMI_BUFFER]; |
| 26 | |
| 27 | HandlerMock hMock; |
| 28 | |
| 29 | EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID, |
Patrick Venture | 45fad1b | 2019-03-18 16:52:14 -0700 | [diff] [blame] | 30 | cableCheck(request.data(), reply, &dataLen, &hMock)); |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | TEST(CableCommandTest, FailsLengthSanityCheck) |
| 34 | { |
| 35 | // Minimum is three bytes, but a length of zero for the string is invalid. |
| 36 | std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck, 0x00, |
| 37 | 'a'}; |
| 38 | |
| 39 | size_t dataLen = request.size(); |
| 40 | std::uint8_t reply[MAX_IPMI_BUFFER]; |
| 41 | |
| 42 | HandlerMock hMock; |
| 43 | |
| 44 | EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID, |
Patrick Venture | 45fad1b | 2019-03-18 16:52:14 -0700 | [diff] [blame] | 45 | cableCheck(request.data(), reply, &dataLen, &hMock)); |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | TEST(CableCommandTest, LengthTooLongForPacket) |
| 49 | { |
| 50 | // The length of a the string, as specified is longer than string provided. |
| 51 | std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck, 0x02, |
| 52 | 'a'}; |
| 53 | |
| 54 | size_t dataLen = request.size(); |
| 55 | std::uint8_t reply[MAX_IPMI_BUFFER]; |
| 56 | |
| 57 | HandlerMock hMock; |
| 58 | |
| 59 | EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID, |
Patrick Venture | 45fad1b | 2019-03-18 16:52:14 -0700 | [diff] [blame] | 60 | cableCheck(request.data(), reply, &dataLen, &hMock)); |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | TEST(CableCommandTest, ValidRequestValidReturn) |
| 64 | { |
| 65 | std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck, 0x01, |
| 66 | 'a'}; |
| 67 | |
| 68 | size_t dataLen = request.size(); |
| 69 | std::uint8_t reply[MAX_IPMI_BUFFER]; |
| 70 | |
| 71 | HandlerMock hMock; |
| 72 | |
| 73 | EXPECT_CALL(hMock, getRxPackets(StrEq("a"))).WillOnce(Return(0)); |
Patrick Venture | 45fad1b | 2019-03-18 16:52:14 -0700 | [diff] [blame] | 74 | EXPECT_EQ(IPMI_CC_OK, cableCheck(request.data(), reply, &dataLen, &hMock)); |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 75 | |
| 76 | // Check results. |
| 77 | struct CableReply expectedReply, actualReply; |
| 78 | expectedReply.subcommand = SysOEMCommands::SysCableCheck; |
| 79 | expectedReply.value = 0; |
| 80 | |
| 81 | EXPECT_EQ(sizeof(expectedReply), dataLen); |
| 82 | std::memcpy(&actualReply, reply, dataLen); |
| 83 | |
| 84 | EXPECT_EQ(expectedReply.subcommand, actualReply.subcommand); |
| 85 | EXPECT_EQ(expectedReply.value, actualReply.value); |
| 86 | } |
| 87 | |
| 88 | } // namespace ipmi |
| 89 | } // namespace google |