blob: 06b4706e7fea8c0913dac38866fd8111ae500747 [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"
Patrick Ventured2037c62019-03-15 10:29:47 -070018
19#include <cstdint>
20#include <cstring>
21#include <vector>
22
23#include <gtest/gtest.h>
24
25#define MAX_IPMI_BUFFER 64
26
27using ::testing::Return;
28using ::testing::StrEq;
29
30namespace google
31{
32namespace ipmi
33{
34
35TEST(CableCommandTest, RequestTooSmall)
36{
37 std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck};
38 size_t dataLen = request.size();
39 std::uint8_t reply[MAX_IPMI_BUFFER];
40
41 HandlerMock hMock;
42
43 EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
Patrick Venture45fad1b2019-03-18 16:52:14 -070044 cableCheck(request.data(), reply, &dataLen, &hMock));
Patrick Ventured2037c62019-03-15 10:29:47 -070045}
46
47TEST(CableCommandTest, FailsLengthSanityCheck)
48{
49 // Minimum is three bytes, but a length of zero for the string is invalid.
50 std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck, 0x00,
51 'a'};
52
53 size_t dataLen = request.size();
54 std::uint8_t reply[MAX_IPMI_BUFFER];
55
56 HandlerMock hMock;
57
58 EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
Patrick Venture45fad1b2019-03-18 16:52:14 -070059 cableCheck(request.data(), reply, &dataLen, &hMock));
Patrick Ventured2037c62019-03-15 10:29:47 -070060}
61
62TEST(CableCommandTest, LengthTooLongForPacket)
63{
64 // The length of a the string, as specified is longer than string provided.
65 std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck, 0x02,
66 'a'};
67
68 size_t dataLen = request.size();
69 std::uint8_t reply[MAX_IPMI_BUFFER];
70
71 HandlerMock hMock;
72
73 EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
Patrick Venture45fad1b2019-03-18 16:52:14 -070074 cableCheck(request.data(), reply, &dataLen, &hMock));
Patrick Ventured2037c62019-03-15 10:29:47 -070075}
76
77TEST(CableCommandTest, ValidRequestValidReturn)
78{
79 std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck, 0x01,
80 'a'};
81
82 size_t dataLen = request.size();
83 std::uint8_t reply[MAX_IPMI_BUFFER];
84
85 HandlerMock hMock;
86
87 EXPECT_CALL(hMock, getRxPackets(StrEq("a"))).WillOnce(Return(0));
Patrick Venture45fad1b2019-03-18 16:52:14 -070088 EXPECT_EQ(IPMI_CC_OK, cableCheck(request.data(), reply, &dataLen, &hMock));
Patrick Ventured2037c62019-03-15 10:29:47 -070089
90 // Check results.
91 struct CableReply expectedReply, actualReply;
92 expectedReply.subcommand = SysOEMCommands::SysCableCheck;
93 expectedReply.value = 0;
94
95 EXPECT_EQ(sizeof(expectedReply), dataLen);
96 std::memcpy(&actualReply, reply, dataLen);
97
98 EXPECT_EQ(expectedReply.subcommand, actualReply.subcommand);
99 EXPECT_EQ(expectedReply.value, actualReply.value);
100}
101
102} // namespace ipmi
103} // namespace google