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