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::_;
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 = static_cast<std::uint8_t>(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_REQ_DATA_LEN_INVALID,
40               deleteBlob(&mgr, request, reply, &dataLen));
41 }
42 
43 TEST(BlobDeleteTest, RequestRejectedReturnsFailure)
44 {
45     // The blobId is rejected for any reason.
46 
47     ManagerMock mgr;
48     size_t dataLen;
49     uint8_t request[MAX_IPMI_BUFFER] = {0};
50     uint8_t reply[MAX_IPMI_BUFFER] = {0};
51     auto req = reinterpret_cast<struct BmcBlobDeleteTx*>(request);
52     std::string blobId = "a";
53 
54     req->cmd = static_cast<std::uint8_t>(BlobOEMCommands::bmcBlobDelete);
55     req->crc = 0;
56     // length() doesn't include the nul-terminator, request buff is initialized
57     // to 0s
58     std::memcpy(req->blobId, blobId.c_str(), blobId.length());
59 
60     dataLen = sizeof(struct BmcBlobDeleteTx) + blobId.length() + 1;
61 
62     EXPECT_CALL(mgr, deleteBlob(StrEq(blobId))).WillOnce(Return(false));
63 
64     EXPECT_EQ(IPMI_CC_UNSPECIFIED_ERROR,
65               deleteBlob(&mgr, request, reply, &dataLen));
66 }
67 
68 TEST(BlobDeleteTest, BlobDeleteReturnsOk)
69 {
70     // The boring case where the blobId is deleted.
71 
72     ManagerMock mgr;
73     size_t dataLen;
74     uint8_t request[MAX_IPMI_BUFFER] = {0};
75     uint8_t reply[MAX_IPMI_BUFFER] = {0};
76     auto req = reinterpret_cast<struct BmcBlobDeleteTx*>(request);
77     std::string blobId = "a";
78 
79     req->cmd = static_cast<std::uint8_t>(BlobOEMCommands::bmcBlobDelete);
80     req->crc = 0;
81     // length() doesn't include the nul-terminator, request buff is initialized
82     // to 0s
83     std::memcpy(req->blobId, blobId.c_str(), blobId.length());
84 
85     dataLen = sizeof(struct BmcBlobDeleteTx) + blobId.length() + 1;
86 
87     EXPECT_CALL(mgr, deleteBlob(StrEq(blobId))).WillOnce(Return(true));
88 
89     EXPECT_EQ(IPMI_CC_OK, deleteBlob(&mgr, request, reply, &dataLen));
90 }
91 } // namespace blobs
92