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