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