Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 1 | #include "helper.hpp" |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 2 | #include "ipmi.hpp" |
Patrick Venture | cd8dab4 | 2019-01-15 19:57:38 -0800 | [diff] [blame] | 3 | #include "manager_mock.hpp" |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 4 | |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 5 | #include <cstring> |
| 6 | #include <string> |
| 7 | |
| 8 | #include <gtest/gtest.h> |
| 9 | |
| 10 | namespace blobs |
| 11 | { |
| 12 | |
| 13 | using ::testing::_; |
| 14 | using ::testing::Invoke; |
| 15 | using ::testing::NotNull; |
| 16 | using ::testing::Return; |
| 17 | using ::testing::StrEq; |
| 18 | |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 19 | TEST(BlobOpenTest, InvalidRequestLengthReturnsFailure) |
| 20 | { |
| 21 | // There is a minimum blobId length of one character, this test verifies |
| 22 | // we check that. |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 23 | ManagerMock mgr; |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 24 | std::vector<uint8_t> request; |
| 25 | BmcBlobOpenTx req; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 26 | std::string blobId = "abc"; |
| 27 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 28 | req.crc = 0; |
| 29 | req.flags = 0; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 30 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 31 | // Missintg the nul-terminator. |
| 32 | request.resize(sizeof(struct BmcBlobOpenTx)); |
| 33 | std::memcpy(request.data(), &req, sizeof(struct BmcBlobOpenTx)); |
| 34 | request.insert(request.end(), blobId.begin(), blobId.end()); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 35 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 36 | EXPECT_EQ(ipmi::responseReqDataLenInvalid(), openBlob(&mgr, request)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | TEST(BlobOpenTest, RequestRejectedReturnsFailure) |
| 40 | { |
| 41 | // The blobId is rejected for any reason. |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 42 | ManagerMock mgr; |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 43 | std::vector<uint8_t> request; |
| 44 | BmcBlobOpenTx req; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 45 | std::string blobId = "a"; |
| 46 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 47 | req.crc = 0; |
| 48 | req.flags = 0; |
| 49 | request.resize(sizeof(struct BmcBlobOpenTx)); |
| 50 | std::memcpy(request.data(), &req, sizeof(struct BmcBlobOpenTx)); |
| 51 | request.insert(request.end(), blobId.begin(), blobId.end()); |
| 52 | request.emplace_back('\0'); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 53 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 54 | EXPECT_CALL(mgr, open(req.flags, StrEq(blobId), _)).WillOnce(Return(false)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 55 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 56 | EXPECT_EQ(ipmi::responseUnspecifiedError(), openBlob(&mgr, request)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | TEST(BlobOpenTest, BlobOpenReturnsOk) |
| 60 | { |
| 61 | // The boring case where the blobId opens. |
| 62 | |
| 63 | ManagerMock mgr; |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 64 | std::vector<uint8_t> request; |
| 65 | BmcBlobOpenTx req; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 66 | struct BmcBlobOpenRx rep; |
| 67 | std::string blobId = "a"; |
| 68 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 69 | req.crc = 0; |
| 70 | req.flags = 0; |
| 71 | request.resize(sizeof(struct BmcBlobOpenTx)); |
| 72 | std::memcpy(request.data(), &req, sizeof(struct BmcBlobOpenTx)); |
| 73 | request.insert(request.end(), blobId.begin(), blobId.end()); |
| 74 | request.emplace_back('\0'); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 75 | |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 76 | uint16_t returnedSession = 0x54; |
| 77 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 78 | EXPECT_CALL(mgr, open(req.flags, StrEq(blobId), NotNull())) |
William A. Kennington III | 993f541 | 2021-06-15 18:19:18 -0700 | [diff] [blame] | 79 | .WillOnce(Invoke([&](uint16_t, const std::string&, uint16_t* session) { |
Patrick Williams | 97e69ca | 2024-08-16 15:21:49 -0400 | [diff] [blame^] | 80 | (*session) = returnedSession; |
| 81 | return true; |
| 82 | })); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 83 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 84 | auto result = validateReply(openBlob(&mgr, request)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 85 | |
| 86 | rep.crc = 0; |
| 87 | rep.sessionId = returnedSession; |
| 88 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 89 | EXPECT_EQ(sizeof(rep), result.size()); |
| 90 | EXPECT_EQ(0, std::memcmp(result.data(), &rep, sizeof(rep))); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 91 | } |
| 92 | } // namespace blobs |