blob: 2b5192ac7f6ac6a6cdba37b6fbd0d364354d8d5f [file] [log] [blame]
Willy Tua2056e92021-10-10 13:36:16 -07001// 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 Venture0e9aae52020-08-13 13:07:09 -070015#include "commands.hpp"
Patrick Venture1ddb94f2019-03-13 16:01:35 -070016#include "eth.hpp"
Patrick Venturef085d912019-03-15 08:50:00 -070017#include "handler_mock.hpp"
Patrick Venture1ddb94f2019-03-13 16:01:35 -070018
19#include <cstdint>
20#include <cstring>
Patrick Venturef085d912019-03-15 08:50:00 -070021#include <string>
22#include <tuple>
Patrick Venture1ddb94f2019-03-13 16:01:35 -070023#include <vector>
24
25#include <gtest/gtest.h>
26
27#define MAX_IPMI_BUFFER 64
28
Patrick Venturef085d912019-03-15 08:50:00 -070029using ::testing::Return;
30
Patrick Venture1ddb94f2019-03-13 16:01:35 -070031namespace google
32{
33namespace ipmi
34{
35
36TEST(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 IIIb69209b2021-07-13 13:22:24 -070044 const std::uint8_t expectedChannel = 14;
Patrick Venture1ddb94f2019-03-13 16:01:35 -070045
Patrick Venturef085d912019-03-15 08:50:00 -070046 HandlerMock hMock;
William A. Kennington IIIb69209b2021-07-13 13:22:24 -070047 EXPECT_CALL(hMock, getEthDetails(""))
Patrick Venturef085d912019-03-15 08:50:00 -070048 .WillOnce(Return(std::make_tuple(
49 expectedChannel,
50 std::string(expectedAnswer,
51 expectedAnswer + sizeof(expectedAnswer)))));
52
53 EXPECT_EQ(IPMI_CC_OK,
Patrick Venture45fad1b2019-03-18 16:52:14 -070054 getEthDevice(request.data(), &reply[0], &dataLen, &hMock));
Patrick Venture1ddb94f2019-03-13 16:01:35 -070055 struct EthDeviceReply check;
56 std::memcpy(&check, &reply[0], sizeof(check));
57 EXPECT_EQ(check.subcommand, SysOEMCommands::SysGetEthDevice);
Patrick Venturef085d912019-03-15 08:50:00 -070058 EXPECT_EQ(check.channel, expectedChannel);
Patrick Venture45fad1b2019-03-18 16:52:14 -070059 EXPECT_EQ(check.ifNameLength, sizeof(expectedAnswer));
Patrick Venture1ddb94f2019-03-13 16:01:35 -070060 EXPECT_EQ(0, std::memcmp(expectedAnswer, &reply[sizeof(check)],
61 sizeof(expectedAnswer)));
62}
63
William A. Kennington IIIb69209b2021-07-13 13:22:24 -070064TEST(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 Venture1ddb94f2019-03-13 16:01:35 -070089} // namespace ipmi
90} // namespace google