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 
15 #include "commands.hpp"
16 #include "eth.hpp"
17 #include "handler_mock.hpp"
18 
19 #include <cstdint>
20 #include <cstring>
21 #include <string>
22 #include <tuple>
23 #include <vector>
24 
25 #include <gtest/gtest.h>
26 
27 #define MAX_IPMI_BUFFER 64
28 
29 using ::testing::Return;
30 
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'};
44     const std::uint8_t expectedChannel = 14;
45 
46     HandlerMock hMock;
47     EXPECT_CALL(hMock, getEthDetails(""))
48         .WillOnce(Return(std::make_tuple(
49             expectedChannel,
50             std::string(expectedAnswer,
51                         expectedAnswer + sizeof(expectedAnswer)))));
52 
53     EXPECT_EQ(IPMI_CC_OK,
54               getEthDevice(request.data(), &reply[0], &dataLen, &hMock));
55     struct EthDeviceReply check;
56     std::memcpy(&check, &reply[0], sizeof(check));
57     EXPECT_EQ(check.subcommand, SysOEMCommands::SysGetEthDevice);
58     EXPECT_EQ(check.channel, expectedChannel);
59     EXPECT_EQ(check.ifNameLength, sizeof(expectedAnswer));
60     EXPECT_EQ(0, std::memcmp(expectedAnswer, &reply[sizeof(check)],
61                              sizeof(expectedAnswer)));
62 }
63 
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 }
89 } // namespace ipmi
90 } // namespace google
91