blob: 27124ad6d6dcd7f4dbd21a3f7b7ae4ddb88779bc [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 Ventured2037c62019-03-15 10:29:47 -070015#include "cable.hpp"
Patrick Venture0e9aae52020-08-13 13:07:09 -070016#include "commands.hpp"
Patrick Ventured2037c62019-03-15 10:29:47 -070017#include "handler_mock.hpp"
Willy Tuff3cd8e2021-09-14 22:49:55 -070018#include "helper.hpp"
Patrick Ventured2037c62019-03-15 10:29:47 -070019
20#include <cstdint>
21#include <cstring>
Willy Tuff3cd8e2021-09-14 22:49:55 -070022#include <tuple>
Patrick Ventured2037c62019-03-15 10:29:47 -070023#include <vector>
24
25#include <gtest/gtest.h>
26
Patrick Ventured2037c62019-03-15 10:29:47 -070027using ::testing::Return;
28using ::testing::StrEq;
29
30namespace google
31{
32namespace ipmi
33{
34
35TEST(CableCommandTest, RequestTooSmall)
36{
Willy Tuff3cd8e2021-09-14 22:49:55 -070037 std::vector<std::uint8_t> request = {};
Patrick Ventured2037c62019-03-15 10:29:47 -070038 HandlerMock hMock;
39
Willy Tuff3cd8e2021-09-14 22:49:55 -070040 EXPECT_EQ(::ipmi::responseReqDataLenInvalid(), cableCheck(request, &hMock));
Patrick Ventured2037c62019-03-15 10:29:47 -070041}
42
43TEST(CableCommandTest, FailsLengthSanityCheck)
44{
45 // Minimum is three bytes, but a length of zero for the string is invalid.
Willy Tuff3cd8e2021-09-14 22:49:55 -070046 std::vector<std::uint8_t> request = {0x00, 'a'};
Patrick Ventured2037c62019-03-15 10:29:47 -070047 HandlerMock hMock;
48
Willy Tuff3cd8e2021-09-14 22:49:55 -070049 EXPECT_EQ(::ipmi::responseReqDataLenInvalid(), cableCheck(request, &hMock));
Patrick Ventured2037c62019-03-15 10:29:47 -070050}
51
52TEST(CableCommandTest, LengthTooLongForPacket)
53{
54 // The length of a the string, as specified is longer than string provided.
Willy Tuff3cd8e2021-09-14 22:49:55 -070055 std::vector<std::uint8_t> request = {0x02, 'a'};
Patrick Ventured2037c62019-03-15 10:29:47 -070056 HandlerMock hMock;
57
Willy Tuff3cd8e2021-09-14 22:49:55 -070058 EXPECT_EQ(::ipmi::responseReqDataLenInvalid(), cableCheck(request, &hMock));
Patrick Ventured2037c62019-03-15 10:29:47 -070059}
60
61TEST(CableCommandTest, ValidRequestValidReturn)
62{
Willy Tuff3cd8e2021-09-14 22:49:55 -070063 std::vector<std::uint8_t> request = {0x01, 'a'};
Patrick Ventured2037c62019-03-15 10:29:47 -070064
65 HandlerMock hMock;
66
67 EXPECT_CALL(hMock, getRxPackets(StrEq("a"))).WillOnce(Return(0));
Patrick Ventured2037c62019-03-15 10:29:47 -070068
69 // Check results.
Willy Tuff3cd8e2021-09-14 22:49:55 -070070 struct CableReply expectedReply;
Patrick Ventured2037c62019-03-15 10:29:47 -070071 expectedReply.value = 0;
72
Willy Tuff3cd8e2021-09-14 22:49:55 -070073 auto reply = cableCheck(request, &hMock);
74 auto result = ValidateReply(reply);
75 auto& data = result.second;
Patrick Ventured2037c62019-03-15 10:29:47 -070076
Willy Tuff3cd8e2021-09-14 22:49:55 -070077 EXPECT_EQ(sizeof(struct CableReply), data.size());
78 EXPECT_EQ(SysOEMCommands::SysCableCheck, result.first);
79 EXPECT_EQ(expectedReply.value, data[0]);
Patrick Ventured2037c62019-03-15 10:29:47 -070080}
81
82} // namespace ipmi
83} // namespace google