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