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