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 | |
| 7 | #include <gtest/gtest.h> |
| 8 | |
| 9 | namespace blobs |
| 10 | { |
| 11 | |
| 12 | using ::testing::_; |
| 13 | using ::testing::Invoke; |
| 14 | using ::testing::Matcher; |
| 15 | using ::testing::NotNull; |
| 16 | using ::testing::Return; |
| 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(BlobSessionStatTest, RequestRejectedByManagerReturnsFailure) |
| 23 | { |
| 24 | // If the session ID is invalid, the request must fail. |
| 25 | |
| 26 | ManagerMock mgr; |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 27 | std::vector<uint8_t> request; |
| 28 | struct BmcBlobSessionStatTx req; |
| 29 | req.crc = 0; |
| 30 | req.sessionId = 0x54; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 31 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 32 | request.resize(sizeof(struct BmcBlobSessionStatTx)); |
| 33 | std::memcpy(request.data(), &req, sizeof(struct BmcBlobSessionStatTx)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 34 | |
Patrick Venture | 8bc1177 | 2019-06-04 07:20:24 -0700 | [diff] [blame] | 35 | EXPECT_CALL(mgr, |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 36 | stat(Matcher<uint16_t>(req.sessionId), Matcher<BlobMeta*>(_))) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 37 | .WillOnce(Return(false)); |
| 38 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 39 | EXPECT_EQ(ipmi::responseUnspecifiedError(), sessionStatBlob(&mgr, request)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | TEST(BlobSessionStatTest, RequestSucceedsNoMetadata) |
| 43 | { |
| 44 | // Stat request succeeeds but there were no metadata bytes. |
| 45 | |
| 46 | ManagerMock mgr; |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 47 | std::vector<uint8_t> request; |
| 48 | struct BmcBlobSessionStatTx req; |
| 49 | req.crc = 0; |
| 50 | req.sessionId = 0x54; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 51 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 52 | request.resize(sizeof(struct BmcBlobSessionStatTx)); |
| 53 | std::memcpy(request.data(), &req, sizeof(struct BmcBlobSessionStatTx)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 54 | |
| 55 | struct BmcBlobStatRx rep; |
| 56 | rep.crc = 0x00; |
| 57 | rep.blobState = 0x01; |
| 58 | rep.size = 0x100; |
| 59 | rep.metadataLen = 0x00; |
| 60 | |
Patrick Venture | b6ed562 | 2020-09-28 13:17:14 -0700 | [diff] [blame] | 61 | uint16_t blobState = rep.blobState; |
| 62 | uint32_t size = rep.size; |
| 63 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 64 | EXPECT_CALL(mgr, stat(Matcher<uint16_t>(req.sessionId), |
Patrick Venture | 8bc1177 | 2019-06-04 07:20:24 -0700 | [diff] [blame] | 65 | Matcher<BlobMeta*>(NotNull()))) |
William A. Kennington III | 993f541 | 2021-06-15 18:19:18 -0700 | [diff] [blame] | 66 | .WillOnce(Invoke([&](uint16_t, BlobMeta* meta) { |
Patrick Venture | b6ed562 | 2020-09-28 13:17:14 -0700 | [diff] [blame] | 67 | meta->blobState = blobState; |
| 68 | meta->size = size; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 69 | return true; |
| 70 | })); |
| 71 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 72 | auto result = validateReply(sessionStatBlob(&mgr, request)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 73 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 74 | EXPECT_EQ(sizeof(rep), result.size()); |
| 75 | EXPECT_EQ(0, std::memcmp(result.data(), &rep, sizeof(rep))); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | TEST(BlobSessionStatTest, RequestSucceedsWithMetadata) |
| 79 | { |
| 80 | // Stat request succeeds and there were metadata bytes. |
| 81 | |
| 82 | ManagerMock mgr; |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 83 | std::vector<uint8_t> request; |
| 84 | struct BmcBlobSessionStatTx req; |
| 85 | req.crc = 0; |
| 86 | req.sessionId = 0x54; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 87 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 88 | request.resize(sizeof(struct BmcBlobSessionStatTx)); |
| 89 | std::memcpy(request.data(), &req, sizeof(struct BmcBlobSessionStatTx)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 90 | |
Patrick Venture | 8bc1177 | 2019-06-04 07:20:24 -0700 | [diff] [blame] | 91 | BlobMeta lmeta; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 92 | lmeta.blobState = 0x01; |
| 93 | lmeta.size = 0x100; |
| 94 | lmeta.metadata.push_back(0x01); |
| 95 | lmeta.metadata.push_back(0x02); |
| 96 | lmeta.metadata.push_back(0x03); |
| 97 | lmeta.metadata.push_back(0x04); |
| 98 | |
| 99 | struct BmcBlobStatRx rep; |
| 100 | rep.crc = 0x00; |
| 101 | rep.blobState = lmeta.blobState; |
| 102 | rep.size = lmeta.size; |
| 103 | rep.metadataLen = lmeta.metadata.size(); |
| 104 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 105 | EXPECT_CALL(mgr, stat(Matcher<uint16_t>(req.sessionId), |
Patrick Venture | 8bc1177 | 2019-06-04 07:20:24 -0700 | [diff] [blame] | 106 | Matcher<BlobMeta*>(NotNull()))) |
William A. Kennington III | 993f541 | 2021-06-15 18:19:18 -0700 | [diff] [blame] | 107 | .WillOnce(Invoke([&](uint16_t, BlobMeta* meta) { |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 108 | (*meta) = lmeta; |
| 109 | return true; |
| 110 | })); |
| 111 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 112 | auto result = validateReply(sessionStatBlob(&mgr, request)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 113 | |
Willy Tu | 067ece1 | 2022-06-16 02:07:06 -0700 | [diff] [blame] | 114 | EXPECT_EQ(sizeof(rep) + lmeta.metadata.size(), result.size()); |
| 115 | EXPECT_EQ(0, std::memcmp(result.data(), &rep, sizeof(rep))); |
| 116 | EXPECT_EQ(0, std::memcmp(result.data() + sizeof(rep), lmeta.metadata.data(), |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 117 | lmeta.metadata.size())); |
| 118 | } |
| 119 | } // namespace blobs |