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 | |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 4 | #include <cstring> |
| 5 | #include <string> |
| 6 | |
| 7 | #include <gtest/gtest.h> |
| 8 | |
| 9 | namespace blobs |
| 10 | { |
| 11 | |
| 12 | using ::testing::_; |
| 13 | using ::testing::Invoke; |
| 14 | using ::testing::NotNull; |
| 15 | using ::testing::Return; |
| 16 | using ::testing::StrEq; |
| 17 | |
| 18 | // ipmid.hpp isn't installed where we can grab it and this value is per BMC |
| 19 | // SoC. |
| 20 | #define MAX_IPMI_BUFFER 64 |
| 21 | |
| 22 | TEST(BlobOpenTest, InvalidRequestLengthReturnsFailure) |
| 23 | { |
| 24 | // There is a minimum blobId length of one character, this test verifies |
| 25 | // we check that. |
| 26 | |
| 27 | ManagerMock mgr; |
| 28 | size_t dataLen; |
| 29 | uint8_t request[MAX_IPMI_BUFFER] = {0}; |
| 30 | uint8_t reply[MAX_IPMI_BUFFER] = {0}; |
| 31 | auto req = reinterpret_cast<struct BmcBlobOpenTx*>(request); |
| 32 | std::string blobId = "abc"; |
| 33 | |
Patrick Venture | 00d5f0d | 2019-05-17 19:21:35 -0700 | [diff] [blame] | 34 | req->cmd = static_cast<std::uint8_t>(BlobOEMCommands::bmcBlobOpen); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 35 | req->crc = 0; |
| 36 | req->flags = 0; |
| 37 | // length() doesn't include the nul-terminator. |
| 38 | std::memcpy(req->blobId, blobId.c_str(), blobId.length()); |
| 39 | |
| 40 | dataLen = sizeof(struct BmcBlobOpenTx) + blobId.length(); |
| 41 | |
Patrick Venture | 4125880 | 2018-11-12 10:46:30 -0800 | [diff] [blame] | 42 | EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID, |
| 43 | openBlob(&mgr, request, reply, &dataLen)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | TEST(BlobOpenTest, RequestRejectedReturnsFailure) |
| 47 | { |
| 48 | // The blobId is rejected for any reason. |
| 49 | |
| 50 | ManagerMock mgr; |
| 51 | size_t dataLen; |
| 52 | uint8_t request[MAX_IPMI_BUFFER] = {0}; |
| 53 | uint8_t reply[MAX_IPMI_BUFFER] = {0}; |
| 54 | auto req = reinterpret_cast<struct BmcBlobOpenTx*>(request); |
| 55 | std::string blobId = "a"; |
| 56 | |
Patrick Venture | 00d5f0d | 2019-05-17 19:21:35 -0700 | [diff] [blame] | 57 | req->cmd = static_cast<std::uint8_t>(BlobOEMCommands::bmcBlobOpen); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 58 | req->crc = 0; |
| 59 | req->flags = 0; |
| 60 | // length() doesn't include the nul-terminator, request buff is initialized |
| 61 | // to 0s |
| 62 | std::memcpy(req->blobId, blobId.c_str(), blobId.length()); |
| 63 | |
| 64 | dataLen = sizeof(struct BmcBlobOpenTx) + blobId.length() + 1; |
| 65 | |
| 66 | EXPECT_CALL(mgr, open(req->flags, StrEq(blobId), _)) |
| 67 | .WillOnce(Return(false)); |
| 68 | |
Patrick Venture | 4125880 | 2018-11-12 10:46:30 -0800 | [diff] [blame] | 69 | EXPECT_EQ(IPMI_CC_UNSPECIFIED_ERROR, |
| 70 | openBlob(&mgr, request, reply, &dataLen)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | TEST(BlobOpenTest, BlobOpenReturnsOk) |
| 74 | { |
| 75 | // The boring case where the blobId opens. |
| 76 | |
| 77 | ManagerMock mgr; |
| 78 | size_t dataLen; |
| 79 | uint8_t request[MAX_IPMI_BUFFER] = {0}; |
| 80 | uint8_t reply[MAX_IPMI_BUFFER] = {0}; |
| 81 | auto req = reinterpret_cast<struct BmcBlobOpenTx*>(request); |
| 82 | struct BmcBlobOpenRx rep; |
| 83 | std::string blobId = "a"; |
| 84 | |
Patrick Venture | 00d5f0d | 2019-05-17 19:21:35 -0700 | [diff] [blame] | 85 | req->cmd = static_cast<std::uint8_t>(BlobOEMCommands::bmcBlobOpen); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 86 | req->crc = 0; |
| 87 | req->flags = 0; |
| 88 | // length() doesn't include the nul-terminator, request buff is initialized |
| 89 | // to 0s |
| 90 | std::memcpy(req->blobId, blobId.c_str(), blobId.length()); |
| 91 | |
| 92 | dataLen = sizeof(struct BmcBlobOpenTx) + blobId.length() + 1; |
| 93 | uint16_t returnedSession = 0x54; |
| 94 | |
| 95 | EXPECT_CALL(mgr, open(req->flags, StrEq(blobId), NotNull())) |
| 96 | .WillOnce(Invoke( |
| 97 | [&](uint16_t flags, const std::string& path, uint16_t* session) { |
| 98 | (*session) = returnedSession; |
| 99 | return true; |
| 100 | })); |
| 101 | |
| 102 | EXPECT_EQ(IPMI_CC_OK, openBlob(&mgr, request, reply, &dataLen)); |
| 103 | |
| 104 | rep.crc = 0; |
| 105 | rep.sessionId = returnedSession; |
| 106 | |
| 107 | EXPECT_EQ(sizeof(rep), dataLen); |
| 108 | EXPECT_EQ(0, std::memcmp(reply, &rep, sizeof(rep))); |
| 109 | } |
| 110 | } // namespace blobs |