1 #include "ipmi.hpp"
2 
3 #include <blobs-ipmid/test/manager_mock.hpp>
4 #include <cstring>
5 #include <string>
6 
7 #include <gtest/gtest.h>
8 
9 namespace blobs
10 {
11 
12 using ::testing::_;
13 using ::testing::Return;
14 using ::testing::StrEq;
15 
16 // ipmid.hpp isn't installed where we can grab it and this value is per BMC
17 // SoC.
18 #define MAX_IPMI_BUFFER 64
19 
20 TEST(BlobDeleteTest, InvalidRequestLengthReturnsFailure)
21 {
22     // There is a minimum blobId length of one character, this test verifies
23     // we check that.
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 BmcBlobDeleteTx*>(request);
30     std::string blobId = "abc";
31 
32     req->cmd = BlobOEMCommands::bmcBlobDelete;
33     req->crc = 0;
34     // length() doesn't include the nul-terminator.
35     std::memcpy(req->blobId, blobId.c_str(), blobId.length());
36 
37     dataLen = sizeof(struct BmcBlobDeleteTx) + blobId.length();
38 
39     EXPECT_EQ(IPMI_CC_INVALID, deleteBlob(&mgr, request, reply, &dataLen));
40 }
41 
42 TEST(BlobDeleteTest, RequestRejectedReturnsFailure)
43 {
44     // The blobId is rejected for any reason.
45 
46     ManagerMock mgr;
47     size_t dataLen;
48     uint8_t request[MAX_IPMI_BUFFER] = {0};
49     uint8_t reply[MAX_IPMI_BUFFER] = {0};
50     auto req = reinterpret_cast<struct BmcBlobDeleteTx*>(request);
51     std::string blobId = "a";
52 
53     req->cmd = BlobOEMCommands::bmcBlobDelete;
54     req->crc = 0;
55     // length() doesn't include the nul-terminator, request buff is initialized
56     // to 0s
57     std::memcpy(req->blobId, blobId.c_str(), blobId.length());
58 
59     dataLen = sizeof(struct BmcBlobDeleteTx) + blobId.length() + 1;
60 
61     EXPECT_CALL(mgr, deleteBlob(StrEq(blobId))).WillOnce(Return(false));
62 
63     EXPECT_EQ(IPMI_CC_INVALID, deleteBlob(&mgr, request, reply, &dataLen));
64 }
65 
66 TEST(BlobDeleteTest, BlobDeleteReturnsOk)
67 {
68     // The boring case where the blobId is deleted.
69 
70     ManagerMock mgr;
71     size_t dataLen;
72     uint8_t request[MAX_IPMI_BUFFER] = {0};
73     uint8_t reply[MAX_IPMI_BUFFER] = {0};
74     auto req = reinterpret_cast<struct BmcBlobDeleteTx*>(request);
75     std::string blobId = "a";
76 
77     req->cmd = BlobOEMCommands::bmcBlobDelete;
78     req->crc = 0;
79     // length() doesn't include the nul-terminator, request buff is initialized
80     // to 0s
81     std::memcpy(req->blobId, blobId.c_str(), blobId.length());
82 
83     dataLen = sizeof(struct BmcBlobDeleteTx) + blobId.length() + 1;
84 
85     EXPECT_CALL(mgr, deleteBlob(StrEq(blobId))).WillOnce(Return(true));
86 
87     EXPECT_EQ(IPMI_CC_OK, deleteBlob(&mgr, request, reply, &dataLen));
88 }
89 } // namespace blobs
90