blob: 6985eb1c37d59a0d6f6e78e3bc7098cad72df7a4 [file] [log] [blame]
Patrick Ventureef3aead2018-09-12 08:53:29 -07001#include "ipmi.hpp"
Patrick Ventureef3aead2018-09-12 08:53:29 -07002
Patrick Venture7210b312018-10-03 14:01:35 -07003#include <blobs-ipmid/test/manager_mock.hpp>
Patrick Ventureef3aead2018-09-12 08:53:29 -07004#include <cstring>
5#include <string>
6
7#include <gtest/gtest.h>
8
9namespace blobs
10{
11
12using ::testing::_;
13using ::testing::Return;
14using ::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
20TEST(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
Patrick Venture41258802018-11-12 10:46:30 -080039 EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
40 deleteBlob(&mgr, request, reply, &dataLen));
Patrick Ventureef3aead2018-09-12 08:53:29 -070041}
42
43TEST(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 = 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
Patrick Venture41258802018-11-12 10:46:30 -080064 EXPECT_EQ(IPMI_CC_UNSPECIFIED_ERROR,
65 deleteBlob(&mgr, request, reply, &dataLen));
Patrick Ventureef3aead2018-09-12 08:53:29 -070066}
67
68TEST(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 = 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