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