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::Matcher; |
| 16 | using ::testing::NotNull; |
| 17 | using ::testing::Return; |
| 18 | using ::testing::StrEq; |
| 19 | |
| 20 | // ipmid.hpp isn't installed where we can grab it and this value is per BMC |
| 21 | // SoC. |
| 22 | #define MAX_IPMI_BUFFER 64 |
| 23 | |
| 24 | TEST(BlobStatTest, InvalidRequestLengthReturnsFailure) |
| 25 | { |
| 26 | // There is a minimum blobId length of one character, this test verifies |
| 27 | // we check that. |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 28 | ManagerMock mgr; |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 29 | std::vector<uint8_t> request; |
| 30 | struct BmcBlobStatTx req; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 31 | std::string blobId = "abc"; |
| 32 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 33 | req.crc = 0; |
| 34 | request.resize(sizeof(struct BmcBlobStatTx)); |
| 35 | std::memcpy(request.data(), &req, sizeof(struct BmcBlobStatTx)); |
| 36 | // Do not include the nul-terminator |
| 37 | request.insert(request.end(), blobId.begin(), blobId.end()); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 38 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 39 | EXPECT_EQ(ipmi::responseReqDataLenInvalid(), statBlob(&mgr, request)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | TEST(BlobStatTest, RequestRejectedReturnsFailure) |
| 43 | { |
| 44 | // The blobId is rejected for any reason. |
| 45 | |
| 46 | ManagerMock mgr; |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 47 | std::vector<uint8_t> request; |
| 48 | struct BmcBlobStatTx req; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 49 | std::string blobId = "a"; |
| 50 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 51 | req.crc = 0; |
| 52 | request.resize(sizeof(struct BmcBlobStatTx)); |
| 53 | std::memcpy(request.data(), &req, sizeof(struct BmcBlobStatTx)); |
| 54 | request.insert(request.end(), blobId.begin(), blobId.end()); |
| 55 | request.emplace_back('\0'); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 56 | |
| 57 | EXPECT_CALL(mgr, stat(Matcher<const std::string&>(StrEq(blobId)), |
Patrick Venture | 8bc1177 | 2019-06-04 07:20:24 -0700 | [diff] [blame] | 58 | Matcher<BlobMeta*>(_))) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 59 | .WillOnce(Return(false)); |
| 60 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 61 | EXPECT_EQ(ipmi::responseUnspecifiedError(), statBlob(&mgr, request)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | TEST(BlobStatTest, RequestSucceedsNoMetadata) |
| 65 | { |
| 66 | // Stat request succeeeds but there were no metadata bytes. |
| 67 | |
| 68 | ManagerMock mgr; |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 69 | std::vector<uint8_t> request; |
| 70 | struct BmcBlobStatTx req; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 71 | std::string blobId = "a"; |
| 72 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 73 | req.crc = 0; |
| 74 | request.resize(sizeof(struct BmcBlobStatTx)); |
| 75 | std::memcpy(request.data(), &req, sizeof(struct BmcBlobStatTx)); |
| 76 | request.insert(request.end(), blobId.begin(), blobId.end()); |
| 77 | request.emplace_back('\0'); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 78 | |
| 79 | struct BmcBlobStatRx rep; |
| 80 | rep.crc = 0x00; |
| 81 | rep.blobState = 0x01; |
| 82 | rep.size = 0x100; |
| 83 | rep.metadataLen = 0x00; |
| 84 | |
Patrick Venture | b6ed562 | 2020-09-28 13:17:14 -0700 | [diff] [blame] | 85 | uint16_t blobState = rep.blobState; |
| 86 | uint32_t size = rep.size; |
| 87 | |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 88 | EXPECT_CALL(mgr, stat(Matcher<const std::string&>(StrEq(blobId)), |
Patrick Venture | 8bc1177 | 2019-06-04 07:20:24 -0700 | [diff] [blame] | 89 | Matcher<BlobMeta*>(NotNull()))) |
William A. Kennington III | 993f541 | 2021-06-15 18:19:18 -0700 | [diff] [blame] | 90 | .WillOnce(Invoke([&](const std::string&, BlobMeta* meta) { |
Patrick Venture | b6ed562 | 2020-09-28 13:17:14 -0700 | [diff] [blame] | 91 | meta->blobState = blobState; |
| 92 | meta->size = size; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 93 | return true; |
| 94 | })); |
| 95 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 96 | auto result = validateReply(statBlob(&mgr, request)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 97 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 98 | EXPECT_EQ(sizeof(rep), result.size()); |
| 99 | EXPECT_EQ(0, std::memcmp(result.data(), &rep, sizeof(rep))); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | TEST(BlobStatTest, RequestSucceedsWithMetadata) |
| 103 | { |
| 104 | // Stat request succeeds and there were metadata bytes. |
| 105 | |
| 106 | ManagerMock mgr; |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 107 | std::vector<uint8_t> request; |
| 108 | struct BmcBlobStatTx req; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 109 | std::string blobId = "a"; |
| 110 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 111 | req.crc = 0; |
| 112 | request.resize(sizeof(struct BmcBlobStatTx)); |
| 113 | std::memcpy(request.data(), &req, sizeof(struct BmcBlobStatTx)); |
| 114 | request.insert(request.end(), blobId.begin(), blobId.end()); |
| 115 | request.emplace_back('\0'); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 116 | |
Patrick Venture | 8bc1177 | 2019-06-04 07:20:24 -0700 | [diff] [blame] | 117 | BlobMeta lmeta; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 118 | lmeta.blobState = 0x01; |
| 119 | lmeta.size = 0x100; |
| 120 | lmeta.metadata.push_back(0x01); |
| 121 | lmeta.metadata.push_back(0x02); |
| 122 | lmeta.metadata.push_back(0x03); |
| 123 | lmeta.metadata.push_back(0x04); |
| 124 | |
| 125 | struct BmcBlobStatRx rep; |
| 126 | rep.crc = 0x00; |
| 127 | rep.blobState = lmeta.blobState; |
| 128 | rep.size = lmeta.size; |
| 129 | rep.metadataLen = lmeta.metadata.size(); |
| 130 | |
| 131 | EXPECT_CALL(mgr, stat(Matcher<const std::string&>(StrEq(blobId)), |
Patrick Venture | 8bc1177 | 2019-06-04 07:20:24 -0700 | [diff] [blame] | 132 | Matcher<BlobMeta*>(NotNull()))) |
William A. Kennington III | 993f541 | 2021-06-15 18:19:18 -0700 | [diff] [blame] | 133 | .WillOnce(Invoke([&](const std::string&, BlobMeta* meta) { |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 134 | (*meta) = lmeta; |
| 135 | return true; |
| 136 | })); |
| 137 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 138 | auto result = validateReply(statBlob(&mgr, request)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 139 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 140 | EXPECT_EQ(sizeof(rep) + lmeta.metadata.size(), result.size()); |
| 141 | EXPECT_EQ(0, std::memcmp(result.data(), &rep, sizeof(rep))); |
| 142 | EXPECT_EQ(0, std::memcmp(result.data() + sizeof(rep), lmeta.metadata.data(), |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 143 | lmeta.metadata.size())); |
| 144 | } |
| 145 | } // namespace blobs |