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