blob: 6cc8223afc50aa44316d05f0c9eed879387c1027 [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
6#include <gtest/gtest.h>
7
8namespace blobs
9{
10
11using ::testing::_;
12using ::testing::ElementsAreArray;
13using ::testing::Return;
14
Patrick Ventureef3aead2018-09-12 08:53:29 -070015TEST(BlobCommitTest, InvalidCommitDataLengthReturnsFailure)
16{
17 // The commit command supports an optional commit blob. This test verifies
18 // we sanity check the length of that blob.
19
20 ManagerMock mgr;
Willy Tu067ece12022-06-16 02:07:06 -070021 std::vector<uint8_t> request;
22 struct BmcBlobCommitTx req;
23 req.crc = 0;
24 req.sessionId = 0x54;
25 req.commitDataLen =
Patrick Ventureef3aead2018-09-12 08:53:29 -070026 1; // It's one byte, but that's more than the packet size.
27
Willy Tu067ece12022-06-16 02:07:06 -070028 request.resize(sizeof(struct BmcBlobCommitTx));
29 std::memcpy(request.data(), &req, sizeof(struct BmcBlobCommitTx));
30 EXPECT_EQ(ipmi::responseReqDataLenInvalid(), commitBlob(&mgr, request));
Patrick Ventureef3aead2018-09-12 08:53:29 -070031}
32
33TEST(BlobCommitTest, ValidCommitNoDataHandlerRejectsReturnsFailure)
34{
35 // The commit packet is valid and the manager's commit call returns failure.
36
37 ManagerMock mgr;
Willy Tu067ece12022-06-16 02:07:06 -070038 std::vector<uint8_t> request;
39 struct BmcBlobCommitTx req;
40 req.crc = 0;
41 req.sessionId = 0x54;
42 req.commitDataLen = 0;
Patrick Ventureef3aead2018-09-12 08:53:29 -070043
Willy Tu067ece12022-06-16 02:07:06 -070044 request.resize(sizeof(struct BmcBlobCommitTx));
45 std::memcpy(request.data(), &req, sizeof(struct BmcBlobCommitTx));
Patrick Ventureef3aead2018-09-12 08:53:29 -070046
Willy Tu067ece12022-06-16 02:07:06 -070047 EXPECT_CALL(mgr, commit(req.sessionId, _)).WillOnce(Return(false));
48 EXPECT_EQ(ipmi::responseUnspecifiedError(), commitBlob(&mgr, request));
Patrick Ventureef3aead2018-09-12 08:53:29 -070049}
50
51TEST(BlobCommitTest, ValidCommitNoDataHandlerAcceptsReturnsSuccess)
52{
53 // Commit called with no data and everything returns success.
54
55 ManagerMock mgr;
Willy Tu067ece12022-06-16 02:07:06 -070056 std::vector<uint8_t> request;
57 struct BmcBlobCommitTx req;
58 req.crc = 0;
59 req.sessionId = 0x54;
60 req.commitDataLen = 0;
Patrick Ventureef3aead2018-09-12 08:53:29 -070061
Willy Tu067ece12022-06-16 02:07:06 -070062 request.resize(sizeof(struct BmcBlobCommitTx));
63 std::memcpy(request.data(), &req, sizeof(struct BmcBlobCommitTx));
64 EXPECT_CALL(mgr, commit(req.sessionId, _)).WillOnce(Return(true));
Patrick Ventureef3aead2018-09-12 08:53:29 -070065
Willy Tu067ece12022-06-16 02:07:06 -070066 EXPECT_EQ(ipmi::responseSuccess(std::vector<uint8_t>{}),
67 commitBlob(&mgr, request));
Patrick Ventureef3aead2018-09-12 08:53:29 -070068}
69
70TEST(BlobCommitTest, ValidCommitWithDataHandlerAcceptsReturnsSuccess)
71{
72 // Commit called with extra data and everything returns success.
73
74 ManagerMock mgr;
Willy Tu067ece12022-06-16 02:07:06 -070075 std::vector<uint8_t> request;
76 std::array<uint8_t, 4> expectedBlob = {0x25, 0x33, 0x45, 0x67};
77 struct BmcBlobCommitTx req;
78 req.crc = 0;
79 req.sessionId = 0x54;
80 req.commitDataLen = sizeof(expectedBlob);
Patrick Ventureef3aead2018-09-12 08:53:29 -070081
Willy Tu067ece12022-06-16 02:07:06 -070082 request.resize(sizeof(struct BmcBlobCommitTx));
83 std::memcpy(request.data(), &req, sizeof(struct BmcBlobCommitTx));
84 request.insert(request.end(), expectedBlob.begin(), expectedBlob.end());
Patrick Ventureef3aead2018-09-12 08:53:29 -070085
Willy Tu067ece12022-06-16 02:07:06 -070086 EXPECT_CALL(mgr, commit(req.sessionId, ElementsAreArray(expectedBlob)))
Patrick Ventureef3aead2018-09-12 08:53:29 -070087 .WillOnce(Return(true));
88
Willy Tu067ece12022-06-16 02:07:06 -070089 EXPECT_EQ(ipmi::responseSuccess(std::vector<uint8_t>{}),
90 commitBlob(&mgr, request));
Patrick Ventureef3aead2018-09-12 08:53:29 -070091}
92} // namespace blobs