1 #include "ipmi.hpp"
2 #include "manager_mock.hpp"
3 
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 = static_cast<std::uint8_t>(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 + 1, &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_UNSPECIFIED_ERROR,
43               writeMeta(&mgr, request, reply, &dataLen));
44 }
45 
46 TEST(BlobWriteMetaTest, ManagerReturnsTrueWriteSucceeds)
47 {
48     // The case where everything works.
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 BmcBlobWriteMetaTx*>(request);
55 
56     req->cmd = static_cast<std::uint8_t>(BlobOEMCommands::bmcBlobWrite);
57     req->crc = 0;
58     req->sessionId = 0x54;
59     req->offset = 0x100;
60 
61     uint8_t expectedBytes[2] = {0x66, 0x67};
62     std::memcpy(req + 1, &expectedBytes[0], sizeof(expectedBytes));
63 
64     dataLen = sizeof(struct BmcBlobWriteMetaTx) + sizeof(expectedBytes);
65 
66     EXPECT_CALL(
67         mgr, writeMeta(req->sessionId, req->offset,
68                        ElementsAreArray(expectedBytes, sizeof(expectedBytes))))
69         .WillOnce(Return(true));
70 
71     EXPECT_EQ(IPMI_CC_OK, writeMeta(&mgr, request, reply, &dataLen));
72 }
73 } // namespace blobs
74