blob: 5002e6966e8bdf9257559ee0891b2ce42ced8de1 [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
12using ::testing::_;
13using ::testing::Invoke;
14using ::testing::Matcher;
15using ::testing::NotNull;
16using ::testing::Return;
17using ::testing::StrEq;
18
19// ipmid.hpp isn't installed where we can grab it and this value is per BMC
20// SoC.
21#define MAX_IPMI_BUFFER 64
22
23TEST(BlobStatTest, InvalidRequestLengthReturnsFailure)
24{
25 // There is a minimum blobId length of one character, this test verifies
26 // we check that.
27
28 ManagerMock mgr;
29 size_t dataLen;
30 uint8_t request[MAX_IPMI_BUFFER] = {0};
31 uint8_t reply[MAX_IPMI_BUFFER] = {0};
32 auto req = reinterpret_cast<struct BmcBlobStatTx*>(request);
33 std::string blobId = "abc";
34
Patrick Venture00d5f0d2019-05-17 19:21:35 -070035 req->cmd = static_cast<std::uint8_t>(BlobOEMCommands::bmcBlobStat);
Patrick Ventureef3aead2018-09-12 08:53:29 -070036 req->crc = 0;
37 // length() doesn't include the nul-terminator.
38 std::memcpy(req->blobId, blobId.c_str(), blobId.length());
39
40 dataLen = sizeof(struct BmcBlobStatTx) + blobId.length();
41
Patrick Venture41258802018-11-12 10:46:30 -080042 EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
43 statBlob(&mgr, request, reply, &dataLen));
Patrick Ventureef3aead2018-09-12 08:53:29 -070044}
45
46TEST(BlobStatTest, RequestRejectedReturnsFailure)
47{
48 // The blobId is rejected for any reason.
49
50 ManagerMock mgr;
51 size_t dataLen;
52 uint8_t request[MAX_IPMI_BUFFER] = {0};
53 uint8_t reply[MAX_IPMI_BUFFER] = {0};
54 auto req = reinterpret_cast<struct BmcBlobStatTx*>(request);
55 std::string blobId = "a";
56
Patrick Venture00d5f0d2019-05-17 19:21:35 -070057 req->cmd = static_cast<std::uint8_t>(BlobOEMCommands::bmcBlobStat);
Patrick Ventureef3aead2018-09-12 08:53:29 -070058 req->crc = 0;
59 // length() doesn't include the nul-terminator, request buff is initialized
60 // to 0s
61 std::memcpy(req->blobId, blobId.c_str(), blobId.length());
62
63 dataLen = sizeof(struct BmcBlobStatTx) + blobId.length() + 1;
64
65 EXPECT_CALL(mgr, stat(Matcher<const std::string&>(StrEq(blobId)),
Patrick Venture8bc11772019-06-04 07:20:24 -070066 Matcher<BlobMeta*>(_)))
Patrick Ventureef3aead2018-09-12 08:53:29 -070067 .WillOnce(Return(false));
68
Patrick Venture41258802018-11-12 10:46:30 -080069 EXPECT_EQ(IPMI_CC_UNSPECIFIED_ERROR,
70 statBlob(&mgr, request, reply, &dataLen));
Patrick Ventureef3aead2018-09-12 08:53:29 -070071}
72
73TEST(BlobStatTest, RequestSucceedsNoMetadata)
74{
75 // Stat request succeeeds but there were no metadata bytes.
76
77 ManagerMock mgr;
78 size_t dataLen;
79 uint8_t request[MAX_IPMI_BUFFER] = {0};
80 uint8_t reply[MAX_IPMI_BUFFER] = {0};
81 auto req = reinterpret_cast<struct BmcBlobStatTx*>(request);
82 std::string blobId = "a";
83
Patrick Venture00d5f0d2019-05-17 19:21:35 -070084 req->cmd = static_cast<std::uint8_t>(BlobOEMCommands::bmcBlobStat);
Patrick Ventureef3aead2018-09-12 08:53:29 -070085 req->crc = 0;
86 // length() doesn't include the nul-terminator, request buff is initialized
87 // to 0s
88 std::memcpy(req->blobId, blobId.c_str(), blobId.length());
89
90 dataLen = sizeof(struct BmcBlobStatTx) + blobId.length() + 1;
91
92 struct BmcBlobStatRx rep;
93 rep.crc = 0x00;
94 rep.blobState = 0x01;
95 rep.size = 0x100;
96 rep.metadataLen = 0x00;
97
98 EXPECT_CALL(mgr, stat(Matcher<const std::string&>(StrEq(blobId)),
Patrick Venture8bc11772019-06-04 07:20:24 -070099 Matcher<BlobMeta*>(NotNull())))
100 .WillOnce(Invoke([&](const std::string& path, BlobMeta* meta) {
Patrick Ventureef3aead2018-09-12 08:53:29 -0700101 meta->blobState = rep.blobState;
102 meta->size = rep.size;
103 return true;
104 }));
105
106 EXPECT_EQ(IPMI_CC_OK, statBlob(&mgr, request, reply, &dataLen));
107
108 EXPECT_EQ(sizeof(rep), dataLen);
109 EXPECT_EQ(0, std::memcmp(reply, &rep, sizeof(rep)));
110}
111
112TEST(BlobStatTest, RequestSucceedsWithMetadata)
113{
114 // Stat request succeeds and there were metadata bytes.
115
116 ManagerMock mgr;
117 size_t dataLen;
118 uint8_t request[MAX_IPMI_BUFFER] = {0};
119 uint8_t reply[MAX_IPMI_BUFFER] = {0};
120 auto req = reinterpret_cast<struct BmcBlobStatTx*>(request);
121 std::string blobId = "a";
122
Patrick Venture00d5f0d2019-05-17 19:21:35 -0700123 req->cmd = static_cast<std::uint8_t>(BlobOEMCommands::bmcBlobStat);
Patrick Ventureef3aead2018-09-12 08:53:29 -0700124 req->crc = 0;
125 // length() doesn't include the nul-terminator, request buff is initialized
126 // to 0s
127 std::memcpy(req->blobId, blobId.c_str(), blobId.length());
128
129 dataLen = sizeof(struct BmcBlobStatTx) + blobId.length() + 1;
130
Patrick Venture8bc11772019-06-04 07:20:24 -0700131 BlobMeta lmeta;
Patrick Ventureef3aead2018-09-12 08:53:29 -0700132 lmeta.blobState = 0x01;
133 lmeta.size = 0x100;
134 lmeta.metadata.push_back(0x01);
135 lmeta.metadata.push_back(0x02);
136 lmeta.metadata.push_back(0x03);
137 lmeta.metadata.push_back(0x04);
138
139 struct BmcBlobStatRx rep;
140 rep.crc = 0x00;
141 rep.blobState = lmeta.blobState;
142 rep.size = lmeta.size;
143 rep.metadataLen = lmeta.metadata.size();
144
145 EXPECT_CALL(mgr, stat(Matcher<const std::string&>(StrEq(blobId)),
Patrick Venture8bc11772019-06-04 07:20:24 -0700146 Matcher<BlobMeta*>(NotNull())))
147 .WillOnce(Invoke([&](const std::string& path, BlobMeta* meta) {
Patrick Ventureef3aead2018-09-12 08:53:29 -0700148 (*meta) = lmeta;
149 return true;
150 }));
151
152 EXPECT_EQ(IPMI_CC_OK, statBlob(&mgr, request, reply, &dataLen));
153
154 EXPECT_EQ(sizeof(rep) + lmeta.metadata.size(), dataLen);
155 EXPECT_EQ(0, std::memcmp(reply, &rep, sizeof(rep)));
156 EXPECT_EQ(0, std::memcmp(reply + sizeof(rep), lmeta.metadata.data(),
157 lmeta.metadata.size()));
158}
159} // namespace blobs