blob: d4b9c5866d85499af7699e62a6f79f4b50150e83 [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
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(BlobCommitTest, InvalidCommitDataLengthReturnsFailure)
20{
21 // The commit command supports an optional commit blob. This test verifies
22 // we sanity check the length of that blob.
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 BmcBlobCommitTx*>(request);
29
Patrick Venture00d5f0d2019-05-17 19:21:35 -070030 req->cmd = static_cast<std::uint8_t>(BlobOEMCommands::bmcBlobCommit);
Patrick Ventureef3aead2018-09-12 08:53:29 -070031 req->crc = 0;
32 req->sessionId = 0x54;
33 req->commitDataLen =
34 1; // It's one byte, but that's more than the packet size.
35
36 dataLen = sizeof(struct BmcBlobCommitTx);
37
Patrick Venture41258802018-11-12 10:46:30 -080038 EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
39 commitBlob(&mgr, request, reply, &dataLen));
Patrick Ventureef3aead2018-09-12 08:53:29 -070040}
41
42TEST(BlobCommitTest, ValidCommitNoDataHandlerRejectsReturnsFailure)
43{
44 // The commit packet is valid and the manager's commit call returns failure.
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 BmcBlobCommitTx*>(request);
51
Patrick Venture00d5f0d2019-05-17 19:21:35 -070052 req->cmd = static_cast<std::uint8_t>(BlobOEMCommands::bmcBlobCommit);
Patrick Ventureef3aead2018-09-12 08:53:29 -070053 req->crc = 0;
54 req->sessionId = 0x54;
55 req->commitDataLen = 0;
56
57 dataLen = sizeof(struct BmcBlobCommitTx);
58
59 EXPECT_CALL(mgr, commit(req->sessionId, _)).WillOnce(Return(false));
60
Patrick Venture41258802018-11-12 10:46:30 -080061 EXPECT_EQ(IPMI_CC_UNSPECIFIED_ERROR,
62 commitBlob(&mgr, request, reply, &dataLen));
Patrick Ventureef3aead2018-09-12 08:53:29 -070063}
64
65TEST(BlobCommitTest, ValidCommitNoDataHandlerAcceptsReturnsSuccess)
66{
67 // Commit called with no data and everything returns success.
68
69 ManagerMock mgr;
70 size_t dataLen;
71 uint8_t request[MAX_IPMI_BUFFER] = {0};
72 uint8_t reply[MAX_IPMI_BUFFER] = {0};
73 auto req = reinterpret_cast<struct BmcBlobCommitTx*>(request);
74
Patrick Venture00d5f0d2019-05-17 19:21:35 -070075 req->cmd = static_cast<std::uint8_t>(BlobOEMCommands::bmcBlobCommit);
Patrick Ventureef3aead2018-09-12 08:53:29 -070076 req->crc = 0;
77 req->sessionId = 0x54;
78 req->commitDataLen = 0;
79
80 dataLen = sizeof(struct BmcBlobCommitTx);
81
82 EXPECT_CALL(mgr, commit(req->sessionId, _)).WillOnce(Return(true));
83
84 EXPECT_EQ(IPMI_CC_OK, commitBlob(&mgr, request, reply, &dataLen));
85}
86
87TEST(BlobCommitTest, ValidCommitWithDataHandlerAcceptsReturnsSuccess)
88{
89 // Commit called with extra data and everything returns success.
90
91 ManagerMock mgr;
92 size_t dataLen;
93 uint8_t request[MAX_IPMI_BUFFER] = {0};
94 uint8_t reply[MAX_IPMI_BUFFER] = {0};
95 auto req = reinterpret_cast<struct BmcBlobCommitTx*>(request);
96
97 uint8_t expectedBlob[4] = {0x25, 0x33, 0x45, 0x67};
98
Patrick Venture00d5f0d2019-05-17 19:21:35 -070099 req->cmd = static_cast<std::uint8_t>(BlobOEMCommands::bmcBlobCommit);
Patrick Ventureef3aead2018-09-12 08:53:29 -0700100 req->crc = 0;
101 req->sessionId = 0x54;
102 req->commitDataLen = sizeof(expectedBlob);
William A. Kennington III117912d2021-06-15 18:22:44 -0700103 std::memcpy(req + 1, &expectedBlob[0], sizeof(expectedBlob));
Patrick Ventureef3aead2018-09-12 08:53:29 -0700104
105 dataLen = sizeof(struct BmcBlobCommitTx) + sizeof(expectedBlob);
106
107 EXPECT_CALL(mgr,
108 commit(req->sessionId,
109 ElementsAreArray(expectedBlob, sizeof(expectedBlob))))
110 .WillOnce(Return(true));
111
112 EXPECT_EQ(IPMI_CC_OK, commitBlob(&mgr, request, reply, &dataLen));
113}
114} // namespace blobs