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 | |
| 6 | #include <gtest/gtest.h> |
| 7 | |
| 8 | namespace blobs |
| 9 | { |
| 10 | |
| 11 | using ::testing::_; |
| 12 | using ::testing::ElementsAreArray; |
| 13 | using ::testing::Return; |
| 14 | |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 15 | TEST(BlobCommitTest, InvalidCommitDataLengthReturnsFailure) |
| 16 | { |
| 17 | // The commit command supports an optional commit blob. This test verifies |
| 18 | // we sanity check the length of that blob. |
| 19 | |
| 20 | ManagerMock mgr; |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 21 | std::vector<uint8_t> request; |
| 22 | struct BmcBlobCommitTx req; |
| 23 | req.crc = 0; |
| 24 | req.sessionId = 0x54; |
| 25 | req.commitDataLen = |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 26 | 1; // It's one byte, but that's more than the packet size. |
| 27 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 28 | request.resize(sizeof(struct BmcBlobCommitTx)); |
| 29 | std::memcpy(request.data(), &req, sizeof(struct BmcBlobCommitTx)); |
| 30 | EXPECT_EQ(ipmi::responseReqDataLenInvalid(), commitBlob(&mgr, request)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | TEST(BlobCommitTest, ValidCommitNoDataHandlerRejectsReturnsFailure) |
| 34 | { |
| 35 | // The commit packet is valid and the manager's commit call returns failure. |
| 36 | |
| 37 | ManagerMock mgr; |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 38 | std::vector<uint8_t> request; |
| 39 | struct BmcBlobCommitTx req; |
| 40 | req.crc = 0; |
| 41 | req.sessionId = 0x54; |
| 42 | req.commitDataLen = 0; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 43 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 44 | request.resize(sizeof(struct BmcBlobCommitTx)); |
| 45 | std::memcpy(request.data(), &req, sizeof(struct BmcBlobCommitTx)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 46 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 47 | EXPECT_CALL(mgr, commit(req.sessionId, _)).WillOnce(Return(false)); |
| 48 | EXPECT_EQ(ipmi::responseUnspecifiedError(), commitBlob(&mgr, request)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | TEST(BlobCommitTest, ValidCommitNoDataHandlerAcceptsReturnsSuccess) |
| 52 | { |
| 53 | // Commit called with no data and everything returns success. |
| 54 | |
| 55 | ManagerMock mgr; |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 56 | std::vector<uint8_t> request; |
| 57 | struct BmcBlobCommitTx req; |
| 58 | req.crc = 0; |
| 59 | req.sessionId = 0x54; |
| 60 | req.commitDataLen = 0; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 61 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 62 | request.resize(sizeof(struct BmcBlobCommitTx)); |
| 63 | std::memcpy(request.data(), &req, sizeof(struct BmcBlobCommitTx)); |
| 64 | EXPECT_CALL(mgr, commit(req.sessionId, _)).WillOnce(Return(true)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 65 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 66 | EXPECT_EQ(ipmi::responseSuccess(std::vector<uint8_t>{}), |
| 67 | commitBlob(&mgr, request)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | TEST(BlobCommitTest, ValidCommitWithDataHandlerAcceptsReturnsSuccess) |
| 71 | { |
| 72 | // Commit called with extra data and everything returns success. |
| 73 | |
| 74 | ManagerMock mgr; |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 75 | std::vector<uint8_t> request; |
| 76 | std::array<uint8_t, 4> expectedBlob = {0x25, 0x33, 0x45, 0x67}; |
| 77 | struct BmcBlobCommitTx req; |
| 78 | req.crc = 0; |
| 79 | req.sessionId = 0x54; |
| 80 | req.commitDataLen = sizeof(expectedBlob); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 81 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 82 | request.resize(sizeof(struct BmcBlobCommitTx)); |
| 83 | std::memcpy(request.data(), &req, sizeof(struct BmcBlobCommitTx)); |
| 84 | request.insert(request.end(), expectedBlob.begin(), expectedBlob.end()); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 85 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 86 | EXPECT_CALL(mgr, commit(req.sessionId, ElementsAreArray(expectedBlob))) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 87 | .WillOnce(Return(true)); |
| 88 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 89 | EXPECT_EQ(ipmi::responseSuccess(std::vector<uint8_t>{}), |
| 90 | commitBlob(&mgr, request)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 91 | } |
| 92 | } // namespace blobs |