blob: 2eeb486b332140818a2117260cabb7f4601b20dc [file] [log] [blame]
Patrick Ventured2037c62019-03-15 10:29:47 -07001#include "cable.hpp"
Patrick Venture0e9aae52020-08-13 13:07:09 -07002#include "commands.hpp"
Patrick Ventured2037c62019-03-15 10:29:47 -07003#include "handler_mock.hpp"
Patrick Ventured2037c62019-03-15 10:29:47 -07004
5#include <cstdint>
6#include <cstring>
7#include <vector>
8
9#include <gtest/gtest.h>
10
11#define MAX_IPMI_BUFFER 64
12
13using ::testing::Return;
14using ::testing::StrEq;
15
16namespace google
17{
18namespace ipmi
19{
20
21TEST(CableCommandTest, RequestTooSmall)
22{
23 std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck};
24 size_t dataLen = request.size();
25 std::uint8_t reply[MAX_IPMI_BUFFER];
26
27 HandlerMock hMock;
28
29 EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
Patrick Venture45fad1b2019-03-18 16:52:14 -070030 cableCheck(request.data(), reply, &dataLen, &hMock));
Patrick Ventured2037c62019-03-15 10:29:47 -070031}
32
33TEST(CableCommandTest, FailsLengthSanityCheck)
34{
35 // Minimum is three bytes, but a length of zero for the string is invalid.
36 std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck, 0x00,
37 'a'};
38
39 size_t dataLen = request.size();
40 std::uint8_t reply[MAX_IPMI_BUFFER];
41
42 HandlerMock hMock;
43
44 EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
Patrick Venture45fad1b2019-03-18 16:52:14 -070045 cableCheck(request.data(), reply, &dataLen, &hMock));
Patrick Ventured2037c62019-03-15 10:29:47 -070046}
47
48TEST(CableCommandTest, LengthTooLongForPacket)
49{
50 // The length of a the string, as specified is longer than string provided.
51 std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck, 0x02,
52 'a'};
53
54 size_t dataLen = request.size();
55 std::uint8_t reply[MAX_IPMI_BUFFER];
56
57 HandlerMock hMock;
58
59 EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
Patrick Venture45fad1b2019-03-18 16:52:14 -070060 cableCheck(request.data(), reply, &dataLen, &hMock));
Patrick Ventured2037c62019-03-15 10:29:47 -070061}
62
63TEST(CableCommandTest, ValidRequestValidReturn)
64{
65 std::vector<std::uint8_t> request = {SysOEMCommands::SysCableCheck, 0x01,
66 'a'};
67
68 size_t dataLen = request.size();
69 std::uint8_t reply[MAX_IPMI_BUFFER];
70
71 HandlerMock hMock;
72
73 EXPECT_CALL(hMock, getRxPackets(StrEq("a"))).WillOnce(Return(0));
Patrick Venture45fad1b2019-03-18 16:52:14 -070074 EXPECT_EQ(IPMI_CC_OK, cableCheck(request.data(), reply, &dataLen, &hMock));
Patrick Ventured2037c62019-03-15 10:29:47 -070075
76 // Check results.
77 struct CableReply expectedReply, actualReply;
78 expectedReply.subcommand = SysOEMCommands::SysCableCheck;
79 expectedReply.value = 0;
80
81 EXPECT_EQ(sizeof(expectedReply), dataLen);
82 std::memcpy(&actualReply, reply, dataLen);
83
84 EXPECT_EQ(expectedReply.subcommand, actualReply.subcommand);
85 EXPECT_EQ(expectedReply.value, actualReply.value);
86}
87
88} // namespace ipmi
89} // namespace google