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 <vector> |
| 6 | |
| 7 | #include <gtest/gtest.h> |
| 8 | |
| 9 | namespace blobs |
| 10 | { |
| 11 | |
| 12 | using ::testing::Return; |
| 13 | |
| 14 | // ipmid.hpp isn't installed where we can grab it and this value is per BMC |
| 15 | // SoC. |
| 16 | #define MAX_IPMI_BUFFER 64 |
| 17 | |
| 18 | TEST(BlobReadTest, ManagerReturnsNoData) |
| 19 | { |
| 20 | // Verify that if no data is returned the IPMI command reply has no |
| 21 | // payload. The manager, in all failures, will just return 0 bytes. |
| 22 | |
| 23 | ManagerMock mgr; |
| 24 | size_t dataLen; |
| 25 | uint8_t request[MAX_IPMI_BUFFER] = {0}; |
| 26 | uint8_t reply[MAX_IPMI_BUFFER] = {0}; |
| 27 | auto req = reinterpret_cast<struct BmcBlobReadTx*>(request); |
| 28 | |
Patrick Venture | 00d5f0d | 2019-05-17 19:21:35 -0700 | [diff] [blame] | 29 | req->cmd = static_cast<std::uint8_t>(BlobOEMCommands::bmcBlobRead); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 30 | req->crc = 0; |
| 31 | req->sessionId = 0x54; |
| 32 | req->offset = 0x100; |
| 33 | req->requestedSize = 0x10; |
| 34 | |
| 35 | dataLen = sizeof(struct BmcBlobReadTx); |
| 36 | |
| 37 | std::vector<uint8_t> data; |
| 38 | |
| 39 | EXPECT_CALL(mgr, read(req->sessionId, req->offset, req->requestedSize)) |
| 40 | .WillOnce(Return(data)); |
| 41 | |
| 42 | EXPECT_EQ(IPMI_CC_OK, readBlob(&mgr, request, reply, &dataLen)); |
| 43 | EXPECT_EQ(sizeof(struct BmcBlobReadRx), dataLen); |
| 44 | } |
| 45 | |
| 46 | TEST(BlobReadTest, ManagerReturnsData) |
| 47 | { |
| 48 | // Verify that if data is returned, it's placed in the expected location. |
| 49 | |
| 50 | ManagerMock mgr; |
| 51 | size_t dataLen; |
| 52 | uint8_t request[MAX_IPMI_BUFFER] = {0}; |
| 53 | uint8_t reply[MAX_IPMI_BUFFER] = {0}; |
| 54 | auto req = reinterpret_cast<struct BmcBlobReadTx*>(request); |
| 55 | |
Patrick Venture | 00d5f0d | 2019-05-17 19:21:35 -0700 | [diff] [blame] | 56 | req->cmd = static_cast<std::uint8_t>(BlobOEMCommands::bmcBlobRead); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 57 | req->crc = 0; |
| 58 | req->sessionId = 0x54; |
| 59 | req->offset = 0x100; |
| 60 | req->requestedSize = 0x10; |
| 61 | |
| 62 | dataLen = sizeof(struct BmcBlobReadTx); |
| 63 | |
| 64 | std::vector<uint8_t> data = {0x02, 0x03, 0x05, 0x06}; |
| 65 | |
| 66 | EXPECT_CALL(mgr, read(req->sessionId, req->offset, req->requestedSize)) |
| 67 | .WillOnce(Return(data)); |
| 68 | |
| 69 | EXPECT_EQ(IPMI_CC_OK, readBlob(&mgr, request, reply, &dataLen)); |
| 70 | EXPECT_EQ(sizeof(struct BmcBlobReadRx) + data.size(), dataLen); |
| 71 | EXPECT_EQ(0, std::memcmp(&reply[sizeof(struct BmcBlobReadRx)], data.data(), |
| 72 | data.size())); |
| 73 | } |
| 74 | |
| 75 | /* TODO(venture): We need a test that handles other checks such as if the size |
| 76 | * requested won't fit into a packet response. |
| 77 | */ |
| 78 | } // namespace blobs |