Patrick Venture | 0e9aae5 | 2020-08-13 13:07:09 -0700 | [diff] [blame] | 1 | #include "commands.hpp" |
Patrick Venture | 1ddb94f | 2019-03-13 16:01:35 -0700 | [diff] [blame] | 2 | #include "eth.hpp" |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 3 | #include "handler_mock.hpp" |
Patrick Venture | 1ddb94f | 2019-03-13 16:01:35 -0700 | [diff] [blame] | 4 | |
| 5 | #include <cstdint> |
| 6 | #include <cstring> |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 7 | #include <string> |
| 8 | #include <tuple> |
Patrick Venture | 1ddb94f | 2019-03-13 16:01:35 -0700 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
| 11 | #include <gtest/gtest.h> |
| 12 | |
| 13 | #define MAX_IPMI_BUFFER 64 |
| 14 | |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 15 | using ::testing::Return; |
| 16 | |
Patrick Venture | 1ddb94f | 2019-03-13 16:01:35 -0700 | [diff] [blame] | 17 | namespace google |
| 18 | { |
| 19 | namespace ipmi |
| 20 | { |
| 21 | |
| 22 | TEST(EthCommandTest, ValidRequestReturnsSuccess) |
| 23 | { |
| 24 | // This command requests no input, therefore it will just return what it |
| 25 | // knows. |
| 26 | std::vector<std::uint8_t> request = {SysOEMCommands::SysGetEthDevice}; |
| 27 | size_t dataLen = request.size(); |
| 28 | std::uint8_t reply[MAX_IPMI_BUFFER]; |
| 29 | const std::uint8_t expectedAnswer[4] = {'e', 't', 'h', '0'}; |
William A. Kennington III | b69209b | 2021-07-13 13:22:24 -0700 | [diff] [blame^] | 30 | const std::uint8_t expectedChannel = 14; |
Patrick Venture | 1ddb94f | 2019-03-13 16:01:35 -0700 | [diff] [blame] | 31 | |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 32 | HandlerMock hMock; |
William A. Kennington III | b69209b | 2021-07-13 13:22:24 -0700 | [diff] [blame^] | 33 | EXPECT_CALL(hMock, getEthDetails("")) |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 34 | .WillOnce(Return(std::make_tuple( |
| 35 | expectedChannel, |
| 36 | std::string(expectedAnswer, |
| 37 | expectedAnswer + sizeof(expectedAnswer))))); |
| 38 | |
| 39 | EXPECT_EQ(IPMI_CC_OK, |
Patrick Venture | 45fad1b | 2019-03-18 16:52:14 -0700 | [diff] [blame] | 40 | getEthDevice(request.data(), &reply[0], &dataLen, &hMock)); |
Patrick Venture | 1ddb94f | 2019-03-13 16:01:35 -0700 | [diff] [blame] | 41 | struct EthDeviceReply check; |
| 42 | std::memcpy(&check, &reply[0], sizeof(check)); |
| 43 | EXPECT_EQ(check.subcommand, SysOEMCommands::SysGetEthDevice); |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 44 | EXPECT_EQ(check.channel, expectedChannel); |
Patrick Venture | 45fad1b | 2019-03-18 16:52:14 -0700 | [diff] [blame] | 45 | EXPECT_EQ(check.ifNameLength, sizeof(expectedAnswer)); |
Patrick Venture | 1ddb94f | 2019-03-13 16:01:35 -0700 | [diff] [blame] | 46 | EXPECT_EQ(0, std::memcmp(expectedAnswer, &reply[sizeof(check)], |
| 47 | sizeof(expectedAnswer))); |
| 48 | } |
| 49 | |
William A. Kennington III | b69209b | 2021-07-13 13:22:24 -0700 | [diff] [blame^] | 50 | TEST(EthCommandTest, ValidPopulatedReturnsSuccess) |
| 51 | { |
| 52 | std::vector<std::uint8_t> request = {SysOEMCommands::SysGetEthDevice, 'e'}; |
| 53 | size_t dataLen = request.size(); |
| 54 | std::uint8_t reply[MAX_IPMI_BUFFER]; |
| 55 | const std::uint8_t expectedAnswer[1] = {'e'}; |
| 56 | const std::uint8_t expectedChannel = 11; |
| 57 | |
| 58 | HandlerMock hMock; |
| 59 | EXPECT_CALL(hMock, getEthDetails("e")) |
| 60 | .WillOnce(Return(std::make_tuple( |
| 61 | expectedChannel, |
| 62 | std::string(expectedAnswer, |
| 63 | expectedAnswer + sizeof(expectedAnswer))))); |
| 64 | |
| 65 | EXPECT_EQ(IPMI_CC_OK, |
| 66 | getEthDevice(request.data(), &reply[0], &dataLen, &hMock)); |
| 67 | struct EthDeviceReply check; |
| 68 | std::memcpy(&check, &reply[0], sizeof(check)); |
| 69 | EXPECT_EQ(check.subcommand, SysOEMCommands::SysGetEthDevice); |
| 70 | EXPECT_EQ(check.channel, expectedChannel); |
| 71 | EXPECT_EQ(check.ifNameLength, sizeof(expectedAnswer)); |
| 72 | EXPECT_EQ(0, std::memcmp(expectedAnswer, &reply[sizeof(check)], |
| 73 | sizeof(expectedAnswer))); |
| 74 | } |
Patrick Venture | 1ddb94f | 2019-03-13 16:01:35 -0700 | [diff] [blame] | 75 | } // namespace ipmi |
| 76 | } // namespace google |