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 | |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 12 | using ::testing::Return; |
| 13 | using ::testing::StrEq; |
| 14 | |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 15 | TEST(BlobDeleteTest, InvalidRequestLengthReturnsFailure) |
| 16 | { |
| 17 | // There is a minimum blobId length of one character, this test verifies |
| 18 | // we check that. |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 19 | ManagerMock mgr; |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 20 | std::vector<uint8_t> request; |
| 21 | struct BmcBlobDeleteTx req; |
| 22 | req.crc = 0; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 23 | std::string blobId = "abc"; |
| 24 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 25 | request.resize(sizeof(struct BmcBlobDeleteTx)); |
| 26 | std::memcpy(request.data(), &req, sizeof(struct BmcBlobDeleteTx)); |
| 27 | request.insert(request.end(), blobId.begin(), blobId.end()); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 28 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 29 | EXPECT_EQ(ipmi::responseReqDataLenInvalid(), deleteBlob(&mgr, request)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | TEST(BlobDeleteTest, RequestRejectedReturnsFailure) |
| 33 | { |
| 34 | // The blobId is rejected for any reason. |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 35 | ManagerMock mgr; |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 36 | std::vector<uint8_t> request; |
| 37 | struct BmcBlobDeleteTx req; |
| 38 | req.crc = 0; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 39 | std::string blobId = "a"; |
| 40 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 41 | request.resize(sizeof(struct BmcBlobDeleteTx)); |
| 42 | std::memcpy(request.data(), &req, sizeof(struct BmcBlobDeleteTx)); |
| 43 | request.insert(request.end(), blobId.begin(), blobId.end()); |
| 44 | request.emplace_back('\0'); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 45 | |
| 46 | EXPECT_CALL(mgr, deleteBlob(StrEq(blobId))).WillOnce(Return(false)); |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 47 | EXPECT_EQ(ipmi::responseUnspecifiedError(), deleteBlob(&mgr, request)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | TEST(BlobDeleteTest, BlobDeleteReturnsOk) |
| 51 | { |
| 52 | // The boring case where the blobId is deleted. |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 53 | ManagerMock mgr; |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 54 | std::vector<uint8_t> request; |
| 55 | struct BmcBlobDeleteTx req; |
| 56 | req.crc = 0; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 57 | std::string blobId = "a"; |
| 58 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 59 | request.resize(sizeof(struct BmcBlobDeleteTx)); |
| 60 | std::memcpy(request.data(), &req, sizeof(struct BmcBlobDeleteTx)); |
| 61 | request.insert(request.end(), blobId.begin(), blobId.end()); |
| 62 | request.emplace_back('\0'); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 63 | |
| 64 | EXPECT_CALL(mgr, deleteBlob(StrEq(blobId))).WillOnce(Return(true)); |
| 65 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 66 | EXPECT_EQ(ipmi::responseSuccess(std::vector<uint8_t>{}), |
| 67 | deleteBlob(&mgr, request)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 68 | } |
| 69 | } // namespace blobs |