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