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 
TEST(BlobDeleteTest,InvalidRequestLengthReturnsFailure)15 TEST(BlobDeleteTest, InvalidRequestLengthReturnsFailure)
16 {
17     // There is a minimum blobId length of one character, this test verifies
18     // we check that.
19     ManagerMock mgr;
20     std::vector<uint8_t> request;
21     struct BmcBlobDeleteTx req;
22     req.crc = 0;
23     std::string blobId = "abc";
24 
25     request.resize(sizeof(struct BmcBlobDeleteTx));
26     std::memcpy(request.data(), &req, sizeof(struct BmcBlobDeleteTx));
27     request.insert(request.end(), blobId.begin(), blobId.end());
28 
29     EXPECT_EQ(ipmi::responseReqDataLenInvalid(), deleteBlob(&mgr, request));
30 }
31 
TEST(BlobDeleteTest,RequestRejectedReturnsFailure)32 TEST(BlobDeleteTest, RequestRejectedReturnsFailure)
33 {
34     // The blobId is rejected for any reason.
35     ManagerMock mgr;
36     std::vector<uint8_t> request;
37     struct BmcBlobDeleteTx req;
38     req.crc = 0;
39     std::string blobId = "a";
40 
41     request.resize(sizeof(struct BmcBlobDeleteTx));
42     std::memcpy(request.data(), &req, sizeof(struct BmcBlobDeleteTx));
43     request.insert(request.end(), blobId.begin(), blobId.end());
44     request.emplace_back('\0');
45 
46     EXPECT_CALL(mgr, deleteBlob(StrEq(blobId))).WillOnce(Return(false));
47     EXPECT_EQ(ipmi::responseUnspecifiedError(), deleteBlob(&mgr, request));
48 }
49 
TEST(BlobDeleteTest,BlobDeleteReturnsOk)50 TEST(BlobDeleteTest, BlobDeleteReturnsOk)
51 {
52     // The boring case where the blobId is deleted.
53     ManagerMock mgr;
54     std::vector<uint8_t> request;
55     struct BmcBlobDeleteTx req;
56     req.crc = 0;
57     std::string blobId = "a";
58 
59     request.resize(sizeof(struct BmcBlobDeleteTx));
60     std::memcpy(request.data(), &req, sizeof(struct BmcBlobDeleteTx));
61     request.insert(request.end(), blobId.begin(), blobId.end());
62     request.emplace_back('\0');
63 
64     EXPECT_CALL(mgr, deleteBlob(StrEq(blobId))).WillOnce(Return(true));
65 
66     EXPECT_EQ(ipmi::responseSuccess(std::vector<uint8_t>{}),
67               deleteBlob(&mgr, request));
68 }
69 } // namespace blobs
70