1 #include "ipmi.hpp"
2 
3 #include <blobs-ipmid/test/manager_mock.hpp>
4 #include <cstring>
5 
6 #include <gtest/gtest.h>
7 
8 namespace blobs
9 {
10 using ::testing::ElementsAreArray;
11 using ::testing::Return;
12 
13 // ipmid.hpp isn't installed where we can grab it and this value is per BMC
14 // SoC.
15 #define MAX_IPMI_BUFFER 64
16 
17 TEST(BlobWriteMetaTest, ManagerReturnsFailureReturnsFailure)
18 {
19     // This verifies a failure from the manager is passed back.
20 
21     ManagerMock mgr;
22     size_t dataLen;
23     uint8_t request[MAX_IPMI_BUFFER] = {0};
24     uint8_t reply[MAX_IPMI_BUFFER] = {0};
25     auto req = reinterpret_cast<struct BmcBlobWriteMetaTx*>(request);
26 
27     req->cmd = BlobOEMCommands::bmcBlobWrite;
28     req->crc = 0;
29     req->sessionId = 0x54;
30     req->offset = 0x100;
31 
32     uint8_t expectedBytes[2] = {0x66, 0x67};
33     std::memcpy(req->data, &expectedBytes[0], sizeof(expectedBytes));
34 
35     dataLen = sizeof(struct BmcBlobWriteMetaTx) + sizeof(expectedBytes);
36 
37     EXPECT_CALL(
38         mgr, writeMeta(req->sessionId, req->offset,
39                        ElementsAreArray(expectedBytes, sizeof(expectedBytes))))
40         .WillOnce(Return(false));
41 
42     EXPECT_EQ(IPMI_CC_INVALID, writeMeta(&mgr, request, reply, &dataLen));
43 }
44 
45 TEST(BlobWriteMetaTest, ManagerReturnsTrueWriteSucceeds)
46 {
47     // The case where everything works.
48 
49     ManagerMock mgr;
50     size_t dataLen;
51     uint8_t request[MAX_IPMI_BUFFER] = {0};
52     uint8_t reply[MAX_IPMI_BUFFER] = {0};
53     auto req = reinterpret_cast<struct BmcBlobWriteMetaTx*>(request);
54 
55     req->cmd = BlobOEMCommands::bmcBlobWrite;
56     req->crc = 0;
57     req->sessionId = 0x54;
58     req->offset = 0x100;
59 
60     uint8_t expectedBytes[2] = {0x66, 0x67};
61     std::memcpy(req->data, &expectedBytes[0], sizeof(expectedBytes));
62 
63     dataLen = sizeof(struct BmcBlobWriteMetaTx) + sizeof(expectedBytes);
64 
65     EXPECT_CALL(
66         mgr, writeMeta(req->sessionId, req->offset,
67                        ElementsAreArray(expectedBytes, sizeof(expectedBytes))))
68         .WillOnce(Return(true));
69 
70     EXPECT_EQ(IPMI_CC_OK, writeMeta(&mgr, request, reply, &dataLen));
71 }
72 } // namespace blobs
73