blob: 3336b2d3e30f9868bb39b55dd53fba6ab0c984bd [file] [log] [blame]
Patrick Ventureef3aead2018-09-12 08:53:29 -07001#include "ipmi.hpp"
Patrick Venturecd8dab42019-01-15 19:57:38 -08002#include "manager_mock.hpp"
Patrick Ventureef3aead2018-09-12 08:53:29 -07003
Patrick Ventureef3aead2018-09-12 08:53:29 -07004#include <cstring>
5#include <string>
6
7#include <gtest/gtest.h>
8
9namespace blobs
10{
11
Patrick Ventureef3aead2018-09-12 08:53:29 -070012using ::testing::Return;
13using ::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
19TEST(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
Patrick Venture00d5f0d2019-05-17 19:21:35 -070031 req->cmd = static_cast<std::uint8_t>(BlobOEMCommands::bmcBlobDelete);
Patrick Ventureef3aead2018-09-12 08:53:29 -070032 req->crc = 0;
33 // length() doesn't include the nul-terminator.
William A. Kennington III117912d2021-06-15 18:22:44 -070034 std::memcpy(req + 1, blobId.c_str(), blobId.length());
Patrick Ventureef3aead2018-09-12 08:53:29 -070035
36 dataLen = sizeof(struct BmcBlobDeleteTx) + blobId.length();
37
Patrick Venture41258802018-11-12 10:46:30 -080038 EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
39 deleteBlob(&mgr, request, reply, &dataLen));
Patrick Ventureef3aead2018-09-12 08:53:29 -070040}
41
42TEST(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
Patrick Venture00d5f0d2019-05-17 19:21:35 -070053 req->cmd = static_cast<std::uint8_t>(BlobOEMCommands::bmcBlobDelete);
Patrick Ventureef3aead2018-09-12 08:53:29 -070054 req->crc = 0;
55 // length() doesn't include the nul-terminator, request buff is initialized
56 // to 0s
William A. Kennington III117912d2021-06-15 18:22:44 -070057 std::memcpy(req + 1, blobId.c_str(), blobId.length());
Patrick Ventureef3aead2018-09-12 08:53:29 -070058
59 dataLen = sizeof(struct BmcBlobDeleteTx) + blobId.length() + 1;
60
61 EXPECT_CALL(mgr, deleteBlob(StrEq(blobId))).WillOnce(Return(false));
62
Patrick Venture41258802018-11-12 10:46:30 -080063 EXPECT_EQ(IPMI_CC_UNSPECIFIED_ERROR,
64 deleteBlob(&mgr, request, reply, &dataLen));
Patrick Ventureef3aead2018-09-12 08:53:29 -070065}
66
67TEST(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
Patrick Venture00d5f0d2019-05-17 19:21:35 -070078 req->cmd = static_cast<std::uint8_t>(BlobOEMCommands::bmcBlobDelete);
Patrick Ventureef3aead2018-09-12 08:53:29 -070079 req->crc = 0;
80 // length() doesn't include the nul-terminator, request buff is initialized
81 // to 0s
William A. Kennington III117912d2021-06-15 18:22:44 -070082 std::memcpy(req + 1, blobId.c_str(), blobId.length());
Patrick Ventureef3aead2018-09-12 08:53:29 -070083
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