Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 1 | #include "helper.hpp" |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 2 | #include "ipmi.hpp" |
Patrick Venture | cd8dab4 | 2019-01-15 19:57:38 -0800 | [diff] [blame] | 3 | #include "manager_mock.hpp" |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 4 | #include "process.hpp" |
| 5 | |
Patrick Venture | de8a16e | 2019-03-07 12:48:32 -0800 | [diff] [blame] | 6 | #include <ipmiblob/test/crc_mock.hpp> |
Patrick Williams | 5250957 | 2023-05-10 07:51:18 -0500 | [diff] [blame] | 7 | |
| 8 | #include <cstring> |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 9 | #include <span> |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 10 | |
| 11 | #include <gtest/gtest.h> |
| 12 | |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 13 | // ipmid.hpp isn't installed where we can grab it and this value is per BMC |
| 14 | // SoC. |
| 15 | #define MAX_IPMI_BUFFER 64 |
| 16 | |
Patrick Venture | de8a16e | 2019-03-07 12:48:32 -0800 | [diff] [blame] | 17 | using ::testing::_; |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 18 | using ::testing::ElementsAre; |
Patrick Venture | de8a16e | 2019-03-07 12:48:32 -0800 | [diff] [blame] | 19 | using ::testing::Eq; |
Patrick Venture | de8a16e | 2019-03-07 12:48:32 -0800 | [diff] [blame] | 20 | using ::testing::Return; |
| 21 | using ::testing::StrictMock; |
| 22 | |
| 23 | namespace ipmiblob |
| 24 | { |
| 25 | CrcInterface* crcIntf = nullptr; |
| 26 | |
| 27 | std::uint16_t generateCrc(const std::vector<std::uint8_t>& data) |
| 28 | { |
| 29 | return (crcIntf) ? crcIntf->generateCrc(data) : 0x00; |
| 30 | } |
| 31 | } // namespace ipmiblob |
| 32 | |
| 33 | namespace blobs |
| 34 | { |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 35 | namespace |
| 36 | { |
| 37 | |
| 38 | void EqualFunctions(IpmiBlobHandler lhs, IpmiBlobHandler rhs) |
| 39 | { |
| 40 | EXPECT_FALSE(lhs == nullptr); |
| 41 | EXPECT_FALSE(rhs == nullptr); |
| 42 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 43 | Resp (*const* lPtr)(ManagerInterface*, std::span<const uint8_t>) = |
| 44 | lhs.target<Resp (*)(ManagerInterface*, std::span<const uint8_t>)>(); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 45 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 46 | Resp (*const* rPtr)(ManagerInterface*, std::span<const uint8_t>) = |
| 47 | rhs.target<Resp (*)(ManagerInterface*, std::span<const uint8_t>)>(); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 48 | |
| 49 | EXPECT_TRUE(lPtr); |
| 50 | EXPECT_TRUE(rPtr); |
| 51 | EXPECT_EQ(*lPtr, *rPtr); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | } // namespace |
| 55 | |
Patrick Venture | de8a16e | 2019-03-07 12:48:32 -0800 | [diff] [blame] | 56 | class ValidateBlobCommandTest : public ::testing::Test |
| 57 | { |
| 58 | protected: |
| 59 | void SetUp() override |
| 60 | { |
| 61 | ipmiblob::crcIntf = &crcMock; |
| 62 | } |
| 63 | |
| 64 | ipmiblob::CrcMock crcMock; |
| 65 | }; |
| 66 | |
| 67 | TEST_F(ValidateBlobCommandTest, InvalidCommandReturnsFailure) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 68 | { |
| 69 | // Verify we handle an invalid command. |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 70 | std::vector<uint8_t> request(MAX_IPMI_BUFFER - 1); |
| 71 | // There is no command 0xff. |
| 72 | IpmiBlobHandler handler = validateBlobCommand(0xff, request); |
| 73 | EXPECT_EQ(ipmi::responseInvalidFieldRequest(), handler(nullptr, {})); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Patrick Venture | de8a16e | 2019-03-07 12:48:32 -0800 | [diff] [blame] | 76 | TEST_F(ValidateBlobCommandTest, ValidCommandWithoutPayload) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 77 | { |
| 78 | // Verify we handle a valid command that doesn't have a payload. |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 79 | std::vector<uint8_t> request(MAX_IPMI_BUFFER - 1); |
| 80 | IpmiBlobHandler handler = validateBlobCommand( |
| 81 | static_cast<std::uint8_t>(BlobOEMCommands::bmcBlobGetCount), request); |
| 82 | EXPECT_FALSE(handler == nullptr); |
| 83 | EqualFunctions(getBlobCount, handler); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Patrick Venture | de8a16e | 2019-03-07 12:48:32 -0800 | [diff] [blame] | 86 | TEST_F(ValidateBlobCommandTest, WithPayloadMinimumLengthIs3VerifyChecks) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 87 | { |
| 88 | // Verify that if there's a payload, it's at least one command byte and |
| 89 | // two bytes for the crc16 and then one data byte. |
| 90 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 91 | std::vector<uint8_t> request(sizeof(uint16_t)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 92 | // There is a payload, but there are insufficient bytes. |
| 93 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 94 | IpmiBlobHandler handler = validateBlobCommand( |
| 95 | static_cast<std::uint8_t>(BlobOEMCommands::bmcBlobGetCount), request); |
| 96 | EXPECT_EQ(ipmi::responseReqDataLenInvalid(), handler(nullptr, {})); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Patrick Venture | de8a16e | 2019-03-07 12:48:32 -0800 | [diff] [blame] | 99 | TEST_F(ValidateBlobCommandTest, WithPayloadAndInvalidCrc) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 100 | { |
| 101 | // Verify that the CRC is checked, and failure is reported. |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 102 | std::vector<uint8_t> request; |
| 103 | BmcBlobWriteTx req; |
| 104 | req.crc = 0x34; |
| 105 | req.sessionId = 0x54; |
| 106 | req.offset = 0x100; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 107 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 108 | std::array<uint8_t, 2> expectedBytes = {0x66, 0x67}; |
| 109 | request.resize(sizeof(struct BmcBlobWriteTx)); |
| 110 | std::memcpy(request.data(), &req, sizeof(struct BmcBlobWriteTx)); |
| 111 | request.insert(request.end(), expectedBytes.begin(), expectedBytes.end()); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 112 | |
| 113 | // skip over cmd and crc. |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 114 | std::vector<uint8_t> bytes(request.begin() + sizeof(req.crc), |
| 115 | request.end()); |
Patrick Venture | de8a16e | 2019-03-07 12:48:32 -0800 | [diff] [blame] | 116 | EXPECT_CALL(crcMock, generateCrc(Eq(bytes))).WillOnce(Return(0x1234)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 117 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 118 | IpmiBlobHandler handler = validateBlobCommand( |
| 119 | static_cast<std::uint8_t>(BlobOEMCommands::bmcBlobWrite), request); |
| 120 | EXPECT_EQ(ipmi::responseUnspecifiedError(), handler(nullptr, {})); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Patrick Venture | de8a16e | 2019-03-07 12:48:32 -0800 | [diff] [blame] | 123 | TEST_F(ValidateBlobCommandTest, WithPayloadAndValidCrc) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 124 | { |
| 125 | // Verify the CRC is checked and if it matches, return the handler. |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 126 | std::vector<uint8_t> request; |
| 127 | BmcBlobWriteTx req; |
| 128 | req.crc = 0x3412; |
| 129 | req.sessionId = 0x54; |
| 130 | req.offset = 0x100; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 131 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 132 | std::array<uint8_t, 2> expectedBytes = {0x66, 0x67}; |
| 133 | request.resize(sizeof(struct BmcBlobWriteTx)); |
| 134 | std::memcpy(request.data(), &req, sizeof(struct BmcBlobWriteTx)); |
| 135 | request.insert(request.end(), expectedBytes.begin(), expectedBytes.end()); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 136 | |
| 137 | // skip over cmd and crc. |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 138 | std::vector<uint8_t> bytes(request.begin() + sizeof(req.crc), |
| 139 | request.end()); |
Patrick Venture | de8a16e | 2019-03-07 12:48:32 -0800 | [diff] [blame] | 140 | EXPECT_CALL(crcMock, generateCrc(Eq(bytes))).WillOnce(Return(0x3412)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 141 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 142 | IpmiBlobHandler handler = validateBlobCommand( |
| 143 | static_cast<std::uint8_t>(BlobOEMCommands::bmcBlobWrite), request); |
| 144 | EXPECT_FALSE(handler == nullptr); |
| 145 | EqualFunctions(writeBlob, handler); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Patrick Venture | de8a16e | 2019-03-07 12:48:32 -0800 | [diff] [blame] | 148 | class ProcessBlobCommandTest : public ::testing::Test |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 149 | { |
Patrick Venture | de8a16e | 2019-03-07 12:48:32 -0800 | [diff] [blame] | 150 | protected: |
| 151 | void SetUp() override |
| 152 | { |
| 153 | ipmiblob::crcIntf = &crcMock; |
| 154 | } |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 155 | |
Patrick Venture | de8a16e | 2019-03-07 12:48:32 -0800 | [diff] [blame] | 156 | ipmiblob::CrcMock crcMock; |
| 157 | }; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 158 | |
Patrick Venture | de8a16e | 2019-03-07 12:48:32 -0800 | [diff] [blame] | 159 | TEST_F(ProcessBlobCommandTest, CommandReturnsNotOk) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 160 | { |
| 161 | // Verify that if the IPMI command handler returns not OK that this is |
| 162 | // noticed and returned. |
| 163 | |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 164 | StrictMock<ManagerMock> manager; |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 165 | std::vector<uint8_t> request(MAX_IPMI_BUFFER - 1); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 166 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 167 | IpmiBlobHandler h = [](ManagerInterface*, std::span<const uint8_t>) { |
| 168 | return ipmi::responseInvalidCommand(); |
| 169 | }; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 170 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 171 | EXPECT_EQ(ipmi::responseInvalidCommand(), |
Willy Tu | 83f9992 | 2022-06-22 14:59:07 -0700 | [diff] [blame] | 172 | processBlobCommand(h, &manager, request, MAX_IPMI_BUFFER)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Patrick Venture | de8a16e | 2019-03-07 12:48:32 -0800 | [diff] [blame] | 175 | TEST_F(ProcessBlobCommandTest, CommandReturnsOkWithNoPayload) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 176 | { |
| 177 | // Verify that if the IPMI command handler returns OK but without a payload |
| 178 | // it doesn't try to compute a CRC. |
| 179 | |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 180 | StrictMock<ManagerMock> manager; |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 181 | std::vector<uint8_t> request(MAX_IPMI_BUFFER - 1); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 182 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 183 | IpmiBlobHandler h = [](ManagerInterface*, std::span<const uint8_t>) { |
| 184 | return ipmi::responseSuccess(std::vector<uint8_t>()); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 185 | }; |
| 186 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 187 | EXPECT_EQ(ipmi::responseSuccess(std::vector<uint8_t>()), |
Willy Tu | 83f9992 | 2022-06-22 14:59:07 -0700 | [diff] [blame] | 188 | processBlobCommand(h, &manager, request, MAX_IPMI_BUFFER)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Patrick Venture | de8a16e | 2019-03-07 12:48:32 -0800 | [diff] [blame] | 191 | TEST_F(ProcessBlobCommandTest, CommandReturnsOkWithInvalidPayloadLength) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 192 | { |
Patrick Venture | d1c3e86 | 2019-01-10 13:12:20 -0800 | [diff] [blame] | 193 | // There is a minimum payload length of 2 bytes (the CRC only, no data, for |
| 194 | // read), this returns 1. |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 195 | |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 196 | StrictMock<ManagerMock> manager; |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 197 | std::vector<uint8_t> request(MAX_IPMI_BUFFER - 1); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 198 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 199 | IpmiBlobHandler h = [](ManagerInterface*, std::span<const uint8_t>) { |
| 200 | return ipmi::responseSuccess(std::vector<uint8_t>(1)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 201 | }; |
| 202 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 203 | EXPECT_EQ(ipmi::responseUnspecifiedError(), |
Willy Tu | 83f9992 | 2022-06-22 14:59:07 -0700 | [diff] [blame] | 204 | processBlobCommand(h, &manager, request, MAX_IPMI_BUFFER)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Patrick Venture | de8a16e | 2019-03-07 12:48:32 -0800 | [diff] [blame] | 207 | TEST_F(ProcessBlobCommandTest, CommandReturnsOkWithValidPayloadLength) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 208 | { |
| 209 | // There is a minimum payload length of 3 bytes, this command returns a |
| 210 | // payload of 3 bytes and the crc code is called to process the payload. |
| 211 | |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 212 | StrictMock<ManagerMock> manager; |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 213 | std::vector<uint8_t> request(MAX_IPMI_BUFFER - 1); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 214 | uint32_t payloadLen = sizeof(uint16_t) + sizeof(uint8_t); |
| 215 | |
Patrick Williams | 97e69ca | 2024-08-16 15:21:49 -0400 | [diff] [blame^] | 216 | IpmiBlobHandler h = |
| 217 | [payloadLen](ManagerInterface*, std::span<const uint8_t>) { |
| 218 | std::vector<uint8_t> output(payloadLen, 0); |
| 219 | output[2] = 0x56; |
| 220 | return ipmi::responseSuccess(output); |
| 221 | }; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 222 | |
Patrick Venture | de8a16e | 2019-03-07 12:48:32 -0800 | [diff] [blame] | 223 | EXPECT_CALL(crcMock, generateCrc(_)).WillOnce(Return(0x3412)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 224 | |
Willy Tu | 83f9992 | 2022-06-22 14:59:07 -0700 | [diff] [blame] | 225 | auto result = validateReply( |
| 226 | processBlobCommand(h, &manager, request, MAX_IPMI_BUFFER)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 227 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 228 | EXPECT_EQ(result.size(), payloadLen); |
| 229 | EXPECT_THAT(result, ElementsAre(0x12, 0x34, 0x56)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 230 | } |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 231 | |
Willy Tu | 83f9992 | 2022-06-22 14:59:07 -0700 | [diff] [blame] | 232 | TEST_F(ProcessBlobCommandTest, |
| 233 | CommandReturnsErrorWithReplyExceededMaxTransferSize) |
| 234 | { |
| 235 | // There is a minimum payload length of 3 bytes, this command returns a |
| 236 | // payload of 3 bytes and the crc code is called to process the payload. |
| 237 | |
| 238 | StrictMock<ManagerMock> manager; |
| 239 | std::vector<uint8_t> request(MAX_IPMI_BUFFER - 1); |
| 240 | uint32_t payloadLen = sizeof(uint16_t) + sizeof(uint8_t); |
| 241 | |
Patrick Williams | 97e69ca | 2024-08-16 15:21:49 -0400 | [diff] [blame^] | 242 | IpmiBlobHandler h = |
| 243 | [payloadLen](ManagerInterface*, std::span<const uint8_t>) { |
| 244 | std::vector<uint8_t> output(payloadLen, 0); |
| 245 | output[2] = 0x56; |
| 246 | return ipmi::responseSuccess(output); |
| 247 | }; |
Willy Tu | 83f9992 | 2022-06-22 14:59:07 -0700 | [diff] [blame] | 248 | |
| 249 | EXPECT_EQ(ipmi::responseResponseError(), |
| 250 | processBlobCommand(h, &manager, request, 0)); |
| 251 | } |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 252 | } // namespace blobs |