1 #include "ipmi.hpp"
2 #include "manager_mock.hpp"
3 
4 #include <cstring>
5 
6 #include <gtest/gtest.h>
7 
8 namespace blobs
9 {
10 
11 using ::testing::_;
12 using ::testing::ElementsAreArray;
13 using ::testing::Return;
14 
TEST(BlobCommitTest,InvalidCommitDataLengthReturnsFailure)15 TEST(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;
21     std::vector<uint8_t> request;
22     struct BmcBlobCommitTx req;
23     req.crc = 0;
24     req.sessionId = 0x54;
25     req.commitDataLen =
26         1; // It's one byte, but that's more than the packet size.
27 
28     request.resize(sizeof(struct BmcBlobCommitTx));
29     std::memcpy(request.data(), &req, sizeof(struct BmcBlobCommitTx));
30     EXPECT_EQ(ipmi::responseReqDataLenInvalid(), commitBlob(&mgr, request));
31 }
32 
TEST(BlobCommitTest,ValidCommitNoDataHandlerRejectsReturnsFailure)33 TEST(BlobCommitTest, ValidCommitNoDataHandlerRejectsReturnsFailure)
34 {
35     // The commit packet is valid and the manager's commit call returns failure.
36 
37     ManagerMock mgr;
38     std::vector<uint8_t> request;
39     struct BmcBlobCommitTx req;
40     req.crc = 0;
41     req.sessionId = 0x54;
42     req.commitDataLen = 0;
43 
44     request.resize(sizeof(struct BmcBlobCommitTx));
45     std::memcpy(request.data(), &req, sizeof(struct BmcBlobCommitTx));
46 
47     EXPECT_CALL(mgr, commit(req.sessionId, _)).WillOnce(Return(false));
48     EXPECT_EQ(ipmi::responseUnspecifiedError(), commitBlob(&mgr, request));
49 }
50 
TEST(BlobCommitTest,ValidCommitNoDataHandlerAcceptsReturnsSuccess)51 TEST(BlobCommitTest, ValidCommitNoDataHandlerAcceptsReturnsSuccess)
52 {
53     // Commit called with no data and everything returns success.
54 
55     ManagerMock mgr;
56     std::vector<uint8_t> request;
57     struct BmcBlobCommitTx req;
58     req.crc = 0;
59     req.sessionId = 0x54;
60     req.commitDataLen = 0;
61 
62     request.resize(sizeof(struct BmcBlobCommitTx));
63     std::memcpy(request.data(), &req, sizeof(struct BmcBlobCommitTx));
64     EXPECT_CALL(mgr, commit(req.sessionId, _)).WillOnce(Return(true));
65 
66     EXPECT_EQ(ipmi::responseSuccess(std::vector<uint8_t>{}),
67               commitBlob(&mgr, request));
68 }
69 
TEST(BlobCommitTest,ValidCommitWithDataHandlerAcceptsReturnsSuccess)70 TEST(BlobCommitTest, ValidCommitWithDataHandlerAcceptsReturnsSuccess)
71 {
72     // Commit called with extra data and everything returns success.
73 
74     ManagerMock mgr;
75     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);
81 
82     request.resize(sizeof(struct BmcBlobCommitTx));
83     std::memcpy(request.data(), &req, sizeof(struct BmcBlobCommitTx));
84     request.insert(request.end(), expectedBlob.begin(), expectedBlob.end());
85 
86     EXPECT_CALL(mgr, commit(req.sessionId, ElementsAreArray(expectedBlob)))
87         .WillOnce(Return(true));
88 
89     EXPECT_EQ(ipmi::responseSuccess(std::vector<uint8_t>{}),
90               commitBlob(&mgr, request));
91 }
92 } // namespace blobs
93