xref: /openbmc/google-ipmi-sys/test/eth_unittest.cpp (revision 8c0094e4eb82029af1d111d2736111cbdda59b63)
1a2056e9cSWilly Tu // Copyright 2021 Google LLC
2a2056e9cSWilly Tu //
3a2056e9cSWilly Tu // Licensed under the Apache License, Version 2.0 (the "License");
4a2056e9cSWilly Tu // you may not use this file except in compliance with the License.
5a2056e9cSWilly Tu // You may obtain a copy of the License at
6a2056e9cSWilly Tu //
7a2056e9cSWilly Tu //      http://www.apache.org/licenses/LICENSE-2.0
8a2056e9cSWilly Tu //
9a2056e9cSWilly Tu // Unless required by applicable law or agreed to in writing, software
10a2056e9cSWilly Tu // distributed under the License is distributed on an "AS IS" BASIS,
11a2056e9cSWilly Tu // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a2056e9cSWilly Tu // See the License for the specific language governing permissions and
13a2056e9cSWilly Tu // limitations under the License.
14a2056e9cSWilly Tu 
150e9aae5dSPatrick Venture #include "commands.hpp"
161ddb94ffSPatrick Venture #include "eth.hpp"
17f085d91dSPatrick Venture #include "handler_mock.hpp"
18ff3cd8e9SWilly Tu #include "helper.hpp"
191ddb94ffSPatrick Venture 
201ddb94ffSPatrick Venture #include <cstdint>
211ddb94ffSPatrick Venture #include <cstring>
22ff3cd8e9SWilly Tu #include <string_view>
23f085d91dSPatrick Venture #include <tuple>
241ddb94ffSPatrick Venture #include <vector>
251ddb94ffSPatrick Venture 
261ddb94ffSPatrick Venture #include <gtest/gtest.h>
271ddb94ffSPatrick Venture 
28f085d91dSPatrick Venture using ::testing::Return;
29f085d91dSPatrick Venture 
301ddb94ffSPatrick Venture namespace google
311ddb94ffSPatrick Venture {
321ddb94ffSPatrick Venture namespace ipmi
331ddb94ffSPatrick Venture {
341ddb94ffSPatrick Venture 
TEST(EthCommandTest,ValidRequestReturnsSuccess)351ddb94ffSPatrick Venture TEST(EthCommandTest, ValidRequestReturnsSuccess)
361ddb94ffSPatrick Venture {
371ddb94ffSPatrick Venture     // This command requests no input, therefore it will just return what it
381ddb94ffSPatrick Venture     // knows.
39ff3cd8e9SWilly Tu     std::vector<std::uint8_t> request = {};
40ff3cd8e9SWilly Tu     const std::string_view expectedAnswer = "eth0";
41b69209b4SWilliam A. Kennington III     const std::uint8_t expectedChannel = 14;
421ddb94ffSPatrick Venture 
43f085d91dSPatrick Venture     HandlerMock hMock;
44b69209b4SWilliam A. Kennington III     EXPECT_CALL(hMock, getEthDetails(""))
45ff3cd8e9SWilly Tu         .WillOnce(
46ff3cd8e9SWilly Tu             Return(std::make_tuple(expectedChannel, expectedAnswer.data())));
47f085d91dSPatrick Venture 
48ff3cd8e9SWilly Tu     auto reply = getEthDevice(request, &hMock);
49ff3cd8e9SWilly Tu     auto result = ValidateReply(reply);
50ff3cd8e9SWilly Tu     auto& data = result.second;
51ff3cd8e9SWilly Tu 
52ff3cd8e9SWilly Tu     EXPECT_EQ(sizeof(EthDeviceReply) + expectedAnswer.size(), data.size());
53ff3cd8e9SWilly Tu     EXPECT_EQ(SysOEMCommands::SysGetEthDevice, result.first);
54ff3cd8e9SWilly Tu     EXPECT_EQ(expectedChannel, data[0]);
55ff3cd8e9SWilly Tu     EXPECT_EQ(expectedAnswer.size(), data[1]);
56*8c0094e4SPatrick Williams     EXPECT_EQ(expectedAnswer.data(),
57*8c0094e4SPatrick Williams               std::string(data.begin() + sizeof(struct EthDeviceReply),
58*8c0094e4SPatrick Williams                           data.end()));
591ddb94ffSPatrick Venture }
601ddb94ffSPatrick Venture 
TEST(EthCommandTest,ValidPopulatedReturnsSuccess)61b69209b4SWilliam A. Kennington III TEST(EthCommandTest, ValidPopulatedReturnsSuccess)
62b69209b4SWilliam A. Kennington III {
63ff3cd8e9SWilly Tu     std::vector<std::uint8_t> request = {'e'};
64ff3cd8e9SWilly Tu     const std::string_view expectedAnswer = "e";
65b69209b4SWilliam A. Kennington III     const std::uint8_t expectedChannel = 11;
66b69209b4SWilliam A. Kennington III 
67b69209b4SWilliam A. Kennington III     HandlerMock hMock;
68b69209b4SWilliam A. Kennington III     EXPECT_CALL(hMock, getEthDetails("e"))
69ff3cd8e9SWilly Tu         .WillOnce(
70ff3cd8e9SWilly Tu             Return(std::make_tuple(expectedChannel, expectedAnswer.data())));
71b69209b4SWilliam A. Kennington III 
72ff3cd8e9SWilly Tu     auto reply = getEthDevice(request, &hMock);
73ff3cd8e9SWilly Tu     auto result = ValidateReply(reply);
74ff3cd8e9SWilly Tu     auto& data = result.second;
75ff3cd8e9SWilly Tu 
76ff3cd8e9SWilly Tu     EXPECT_EQ(sizeof(EthDeviceReply) + expectedAnswer.size(), data.size());
77ff3cd8e9SWilly Tu     EXPECT_EQ(SysOEMCommands::SysGetEthDevice, result.first);
78ff3cd8e9SWilly Tu     EXPECT_EQ(expectedChannel, data[0]);
79ff3cd8e9SWilly Tu     EXPECT_EQ(expectedAnswer.size(), data[1]);
80*8c0094e4SPatrick Williams     EXPECT_EQ(expectedAnswer.data(),
81*8c0094e4SPatrick Williams               std::string(data.begin() + sizeof(struct EthDeviceReply),
82*8c0094e4SPatrick Williams                           data.end()));
83b69209b4SWilliam A. Kennington III }
841ddb94ffSPatrick Venture } // namespace ipmi
851ddb94ffSPatrick Venture } // namespace google
86