xref: /openbmc/google-ipmi-sys/test/eth_unittest.cpp (revision 8c0094e4eb82029af1d111d2736111cbdda59b63)
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 #include "helper.hpp"
19 
20 #include <cstdint>
21 #include <cstring>
22 #include <string_view>
23 #include <tuple>
24 #include <vector>
25 
26 #include <gtest/gtest.h>
27 
28 using ::testing::Return;
29 
30 namespace google
31 {
32 namespace ipmi
33 {
34 
TEST(EthCommandTest,ValidRequestReturnsSuccess)35 TEST(EthCommandTest, ValidRequestReturnsSuccess)
36 {
37     // This command requests no input, therefore it will just return what it
38     // knows.
39     std::vector<std::uint8_t> request = {};
40     const std::string_view expectedAnswer = "eth0";
41     const std::uint8_t expectedChannel = 14;
42 
43     HandlerMock hMock;
44     EXPECT_CALL(hMock, getEthDetails(""))
45         .WillOnce(
46             Return(std::make_tuple(expectedChannel, expectedAnswer.data())));
47 
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]);
56     EXPECT_EQ(expectedAnswer.data(),
57               std::string(data.begin() + sizeof(struct EthDeviceReply),
58                           data.end()));
59 }
60 
TEST(EthCommandTest,ValidPopulatedReturnsSuccess)61 TEST(EthCommandTest, ValidPopulatedReturnsSuccess)
62 {
63     std::vector<std::uint8_t> request = {'e'};
64     const std::string_view expectedAnswer = "e";
65     const std::uint8_t expectedChannel = 11;
66 
67     HandlerMock hMock;
68     EXPECT_CALL(hMock, getEthDetails("e"))
69         .WillOnce(
70             Return(std::make_tuple(expectedChannel, expectedAnswer.data())));
71 
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]);
80     EXPECT_EQ(expectedAnswer.data(),
81               std::string(data.begin() + sizeof(struct EthDeviceReply),
82                           data.end()));
83 }
84 } // namespace ipmi
85 } // namespace google
86