blob: 50c9ed7dd316090354dcc01e44dbfbbbf9baac61 [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"
Willy Tuff3cd8e2021-09-14 22:49:55 -070018#include "helper.hpp"
Patrick Venture1ddb94f2019-03-13 16:01:35 -070019
20#include <cstdint>
21#include <cstring>
Willy Tuff3cd8e2021-09-14 22:49:55 -070022#include <string_view>
Patrick Venturef085d912019-03-15 08:50:00 -070023#include <tuple>
Patrick Venture1ddb94f2019-03-13 16:01:35 -070024#include <vector>
25
26#include <gtest/gtest.h>
27
Patrick Venturef085d912019-03-15 08:50:00 -070028using ::testing::Return;
29
Patrick Venture1ddb94f2019-03-13 16:01:35 -070030namespace google
31{
32namespace ipmi
33{
34
35TEST(EthCommandTest, ValidRequestReturnsSuccess)
36{
37 // This command requests no input, therefore it will just return what it
38 // knows.
Willy Tuff3cd8e2021-09-14 22:49:55 -070039 std::vector<std::uint8_t> request = {};
40 const std::string_view expectedAnswer = "eth0";
William A. Kennington IIIb69209b2021-07-13 13:22:24 -070041 const std::uint8_t expectedChannel = 14;
Patrick Venture1ddb94f2019-03-13 16:01:35 -070042
Patrick Venturef085d912019-03-15 08:50:00 -070043 HandlerMock hMock;
William A. Kennington IIIb69209b2021-07-13 13:22:24 -070044 EXPECT_CALL(hMock, getEthDetails(""))
Willy Tuff3cd8e2021-09-14 22:49:55 -070045 .WillOnce(
46 Return(std::make_tuple(expectedChannel, expectedAnswer.data())));
Patrick Venturef085d912019-03-15 08:50:00 -070047
Willy Tuff3cd8e2021-09-14 22:49:55 -070048 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 Williams8c0094e2024-08-16 15:22:37 -040056 EXPECT_EQ(expectedAnswer.data(),
57 std::string(data.begin() + sizeof(struct EthDeviceReply),
58 data.end()));
Patrick Venture1ddb94f2019-03-13 16:01:35 -070059}
60
William A. Kennington IIIb69209b2021-07-13 13:22:24 -070061TEST(EthCommandTest, ValidPopulatedReturnsSuccess)
62{
Willy Tuff3cd8e2021-09-14 22:49:55 -070063 std::vector<std::uint8_t> request = {'e'};
64 const std::string_view expectedAnswer = "e";
William A. Kennington IIIb69209b2021-07-13 13:22:24 -070065 const std::uint8_t expectedChannel = 11;
66
67 HandlerMock hMock;
68 EXPECT_CALL(hMock, getEthDetails("e"))
Willy Tuff3cd8e2021-09-14 22:49:55 -070069 .WillOnce(
70 Return(std::make_tuple(expectedChannel, expectedAnswer.data())));
William A. Kennington IIIb69209b2021-07-13 13:22:24 -070071
Willy Tuff3cd8e2021-09-14 22:49:55 -070072 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 Williams8c0094e2024-08-16 15:22:37 -040080 EXPECT_EQ(expectedAnswer.data(),
81 std::string(data.begin() + sizeof(struct EthDeviceReply),
82 data.end()));
William A. Kennington IIIb69209b2021-07-13 13:22:24 -070083}
Patrick Venture1ddb94f2019-03-13 16:01:35 -070084} // namespace ipmi
85} // namespace google