1 // 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
15 #include "cable.hpp"
16 #include "commands.hpp"
17 #include "handler_mock.hpp"
18 #include "helper.hpp"
19
20 #include <cstdint>
21 #include <cstring>
22 #include <tuple>
23 #include <vector>
24
25 #include <gtest/gtest.h>
26
27 using ::testing::Return;
28 using ::testing::StrEq;
29
30 namespace google
31 {
32 namespace ipmi
33 {
34
TEST(CableCommandTest,RequestTooSmall)35 TEST(CableCommandTest, RequestTooSmall)
36 {
37 std::vector<std::uint8_t> request = {};
38 HandlerMock hMock;
39
40 EXPECT_EQ(::ipmi::responseReqDataLenInvalid(), cableCheck(request, &hMock));
41 }
42
TEST(CableCommandTest,FailsLengthSanityCheck)43 TEST(CableCommandTest, FailsLengthSanityCheck)
44 {
45 // Minimum is three bytes, but a length of zero for the string is invalid.
46 std::vector<std::uint8_t> request = {0x00, 'a'};
47 HandlerMock hMock;
48
49 EXPECT_EQ(::ipmi::responseReqDataLenInvalid(), cableCheck(request, &hMock));
50 }
51
TEST(CableCommandTest,LengthTooLongForPacket)52 TEST(CableCommandTest, LengthTooLongForPacket)
53 {
54 // The length of a the string, as specified is longer than string provided.
55 std::vector<std::uint8_t> request = {0x02, 'a'};
56 HandlerMock hMock;
57
58 EXPECT_EQ(::ipmi::responseReqDataLenInvalid(), cableCheck(request, &hMock));
59 }
60
TEST(CableCommandTest,ValidRequestValidReturn)61 TEST(CableCommandTest, ValidRequestValidReturn)
62 {
63 std::vector<std::uint8_t> request = {0x01, 'a'};
64
65 HandlerMock hMock;
66
67 EXPECT_CALL(hMock, getRxPackets(StrEq("a"))).WillOnce(Return(0));
68
69 // Check results.
70 struct CableReply expectedReply;
71 expectedReply.value = 0;
72
73 auto reply = cableCheck(request, &hMock);
74 auto result = ValidateReply(reply);
75 auto& data = result.second;
76
77 EXPECT_EQ(sizeof(struct CableReply), data.size());
78 EXPECT_EQ(SysOEMCommands::SysCableCheck, result.first);
79 EXPECT_EQ(expectedReply.value, data[0]);
80 }
81
82 } // namespace ipmi
83 } // namespace google
84