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::Invoke; |
| 13 | using ::testing::Matcher; |
| 14 | using ::testing::NotNull; |
| 15 | using ::testing::Return; |
| 16 | |
| 17 | // ipmid.hpp isn't installed where we can grab it and this value is per BMC |
| 18 | // SoC. |
| 19 | #define MAX_IPMI_BUFFER 64 |
| 20 | |
| 21 | TEST(BlobSessionStatTest, RequestRejectedByManagerReturnsFailure) |
| 22 | { |
| 23 | // If the session ID is invalid, the request must fail. |
| 24 | |
| 25 | ManagerMock mgr; |
| 26 | size_t dataLen; |
| 27 | uint8_t request[MAX_IPMI_BUFFER] = {0}; |
| 28 | uint8_t reply[MAX_IPMI_BUFFER] = {0}; |
| 29 | auto req = reinterpret_cast<struct BmcBlobSessionStatTx*>(request); |
Patrick Venture | 00d5f0d | 2019-05-17 19:21:35 -0700 | [diff] [blame] | 30 | req->cmd = static_cast<std::uint8_t>(BlobOEMCommands::bmcBlobSessionStat); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 31 | req->crc = 0; |
| 32 | req->sessionId = 0x54; |
| 33 | |
| 34 | dataLen = sizeof(struct BmcBlobSessionStatTx); |
| 35 | |
Patrick Venture | 8bc1177 | 2019-06-04 07:20:24 -0700 | [diff] [blame] | 36 | EXPECT_CALL(mgr, |
| 37 | stat(Matcher<uint16_t>(req->sessionId), Matcher<BlobMeta*>(_))) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 38 | .WillOnce(Return(false)); |
| 39 | |
Patrick Venture | 4125880 | 2018-11-12 10:46:30 -0800 | [diff] [blame] | 40 | EXPECT_EQ(IPMI_CC_UNSPECIFIED_ERROR, |
| 41 | sessionStatBlob(&mgr, request, reply, &dataLen)); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | TEST(BlobSessionStatTest, RequestSucceedsNoMetadata) |
| 45 | { |
| 46 | // Stat request succeeeds but there were no metadata bytes. |
| 47 | |
| 48 | ManagerMock mgr; |
| 49 | size_t dataLen; |
| 50 | uint8_t request[MAX_IPMI_BUFFER] = {0}; |
| 51 | uint8_t reply[MAX_IPMI_BUFFER] = {0}; |
| 52 | auto req = reinterpret_cast<struct BmcBlobSessionStatTx*>(request); |
Patrick Venture | 00d5f0d | 2019-05-17 19:21:35 -0700 | [diff] [blame] | 53 | req->cmd = static_cast<std::uint8_t>(BlobOEMCommands::bmcBlobSessionStat); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 54 | req->crc = 0; |
| 55 | req->sessionId = 0x54; |
| 56 | |
| 57 | dataLen = sizeof(struct BmcBlobSessionStatTx); |
| 58 | |
| 59 | struct BmcBlobStatRx rep; |
| 60 | rep.crc = 0x00; |
| 61 | rep.blobState = 0x01; |
| 62 | rep.size = 0x100; |
| 63 | rep.metadataLen = 0x00; |
| 64 | |
Patrick Venture | b6ed562 | 2020-09-28 13:17:14 -0700 | [diff] [blame] | 65 | uint16_t blobState = rep.blobState; |
| 66 | uint32_t size = rep.size; |
| 67 | |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 68 | EXPECT_CALL(mgr, stat(Matcher<uint16_t>(req->sessionId), |
Patrick Venture | 8bc1177 | 2019-06-04 07:20:24 -0700 | [diff] [blame] | 69 | Matcher<BlobMeta*>(NotNull()))) |
William A. Kennington III | 993f541 | 2021-06-15 18:19:18 -0700 | [diff] [blame] | 70 | .WillOnce(Invoke([&](uint16_t, BlobMeta* meta) { |
Patrick Venture | b6ed562 | 2020-09-28 13:17:14 -0700 | [diff] [blame] | 71 | meta->blobState = blobState; |
| 72 | meta->size = size; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 73 | return true; |
| 74 | })); |
| 75 | |
| 76 | EXPECT_EQ(IPMI_CC_OK, sessionStatBlob(&mgr, request, reply, &dataLen)); |
| 77 | |
| 78 | EXPECT_EQ(sizeof(rep), dataLen); |
| 79 | EXPECT_EQ(0, std::memcmp(reply, &rep, sizeof(rep))); |
| 80 | } |
| 81 | |
| 82 | TEST(BlobSessionStatTest, RequestSucceedsWithMetadata) |
| 83 | { |
| 84 | // Stat request succeeds and there were metadata bytes. |
| 85 | |
| 86 | ManagerMock mgr; |
| 87 | size_t dataLen; |
| 88 | uint8_t request[MAX_IPMI_BUFFER] = {0}; |
| 89 | uint8_t reply[MAX_IPMI_BUFFER] = {0}; |
| 90 | auto req = reinterpret_cast<struct BmcBlobSessionStatTx*>(request); |
Patrick Venture | 00d5f0d | 2019-05-17 19:21:35 -0700 | [diff] [blame] | 91 | req->cmd = static_cast<std::uint8_t>(BlobOEMCommands::bmcBlobSessionStat); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 92 | req->crc = 0; |
| 93 | req->sessionId = 0x54; |
| 94 | |
| 95 | dataLen = sizeof(struct BmcBlobSessionStatTx); |
| 96 | |
Patrick Venture | 8bc1177 | 2019-06-04 07:20:24 -0700 | [diff] [blame] | 97 | BlobMeta lmeta; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 98 | lmeta.blobState = 0x01; |
| 99 | lmeta.size = 0x100; |
| 100 | lmeta.metadata.push_back(0x01); |
| 101 | lmeta.metadata.push_back(0x02); |
| 102 | lmeta.metadata.push_back(0x03); |
| 103 | lmeta.metadata.push_back(0x04); |
| 104 | |
| 105 | struct BmcBlobStatRx rep; |
| 106 | rep.crc = 0x00; |
| 107 | rep.blobState = lmeta.blobState; |
| 108 | rep.size = lmeta.size; |
| 109 | rep.metadataLen = lmeta.metadata.size(); |
| 110 | |
| 111 | EXPECT_CALL(mgr, stat(Matcher<uint16_t>(req->sessionId), |
Patrick Venture | 8bc1177 | 2019-06-04 07:20:24 -0700 | [diff] [blame] | 112 | Matcher<BlobMeta*>(NotNull()))) |
William A. Kennington III | 993f541 | 2021-06-15 18:19:18 -0700 | [diff] [blame] | 113 | .WillOnce(Invoke([&](uint16_t, BlobMeta* meta) { |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 114 | (*meta) = lmeta; |
| 115 | return true; |
| 116 | })); |
| 117 | |
| 118 | EXPECT_EQ(IPMI_CC_OK, sessionStatBlob(&mgr, request, reply, &dataLen)); |
| 119 | |
| 120 | EXPECT_EQ(sizeof(rep) + lmeta.metadata.size(), dataLen); |
| 121 | EXPECT_EQ(0, std::memcmp(reply, &rep, sizeof(rep))); |
| 122 | EXPECT_EQ(0, std::memcmp(reply + sizeof(rep), lmeta.metadata.data(), |
| 123 | lmeta.metadata.size())); |
| 124 | } |
| 125 | } // namespace blobs |