blob: ab091adf16ca0a23e1a8208a65cdd1fb0dd1653d [file] [log] [blame]
Willy Tu067ece12022-06-16 02:07:06 -07001#include "helper.hpp"
Patrick Ventureef3aead2018-09-12 08:53:29 -07002#include "ipmi.hpp"
Patrick Venturecd8dab42019-01-15 19:57:38 -08003#include "manager_mock.hpp"
Patrick Ventureef3aead2018-09-12 08:53:29 -07004
Patrick Ventureef3aead2018-09-12 08:53:29 -07005#include <cstring>
6#include <string>
7
8#include <gtest/gtest.h>
9
10namespace blobs
11{
12
13using ::testing::_;
14using ::testing::Invoke;
15using ::testing::Matcher;
16using ::testing::NotNull;
17using ::testing::Return;
18using ::testing::StrEq;
19
20// ipmid.hpp isn't installed where we can grab it and this value is per BMC
21// SoC.
22#define MAX_IPMI_BUFFER 64
23
24TEST(BlobStatTest, InvalidRequestLengthReturnsFailure)
25{
26 // There is a minimum blobId length of one character, this test verifies
27 // we check that.
Patrick Ventureef3aead2018-09-12 08:53:29 -070028 ManagerMock mgr;
Willy Tu067ece12022-06-16 02:07:06 -070029 std::vector<uint8_t> request;
30 struct BmcBlobStatTx req;
Patrick Ventureef3aead2018-09-12 08:53:29 -070031 std::string blobId = "abc";
32
Willy Tu067ece12022-06-16 02:07:06 -070033 req.crc = 0;
34 request.resize(sizeof(struct BmcBlobStatTx));
35 std::memcpy(request.data(), &req, sizeof(struct BmcBlobStatTx));
36 // Do not include the nul-terminator
37 request.insert(request.end(), blobId.begin(), blobId.end());
Patrick Ventureef3aead2018-09-12 08:53:29 -070038
Willy Tu067ece12022-06-16 02:07:06 -070039 EXPECT_EQ(ipmi::responseReqDataLenInvalid(), statBlob(&mgr, request));
Patrick Ventureef3aead2018-09-12 08:53:29 -070040}
41
42TEST(BlobStatTest, RequestRejectedReturnsFailure)
43{
44 // The blobId is rejected for any reason.
45
46 ManagerMock mgr;
Willy Tu067ece12022-06-16 02:07:06 -070047 std::vector<uint8_t> request;
48 struct BmcBlobStatTx req;
Patrick Ventureef3aead2018-09-12 08:53:29 -070049 std::string blobId = "a";
50
Willy Tu067ece12022-06-16 02:07:06 -070051 req.crc = 0;
52 request.resize(sizeof(struct BmcBlobStatTx));
53 std::memcpy(request.data(), &req, sizeof(struct BmcBlobStatTx));
54 request.insert(request.end(), blobId.begin(), blobId.end());
55 request.emplace_back('\0');
Patrick Ventureef3aead2018-09-12 08:53:29 -070056
57 EXPECT_CALL(mgr, stat(Matcher<const std::string&>(StrEq(blobId)),
Patrick Venture8bc11772019-06-04 07:20:24 -070058 Matcher<BlobMeta*>(_)))
Patrick Ventureef3aead2018-09-12 08:53:29 -070059 .WillOnce(Return(false));
60
Willy Tu067ece12022-06-16 02:07:06 -070061 EXPECT_EQ(ipmi::responseUnspecifiedError(), statBlob(&mgr, request));
Patrick Ventureef3aead2018-09-12 08:53:29 -070062}
63
64TEST(BlobStatTest, RequestSucceedsNoMetadata)
65{
66 // Stat request succeeeds but there were no metadata bytes.
67
68 ManagerMock mgr;
Willy Tu067ece12022-06-16 02:07:06 -070069 std::vector<uint8_t> request;
70 struct BmcBlobStatTx req;
Patrick Ventureef3aead2018-09-12 08:53:29 -070071 std::string blobId = "a";
72
Willy Tu067ece12022-06-16 02:07:06 -070073 req.crc = 0;
74 request.resize(sizeof(struct BmcBlobStatTx));
75 std::memcpy(request.data(), &req, sizeof(struct BmcBlobStatTx));
76 request.insert(request.end(), blobId.begin(), blobId.end());
77 request.emplace_back('\0');
Patrick Ventureef3aead2018-09-12 08:53:29 -070078
79 struct BmcBlobStatRx rep;
80 rep.crc = 0x00;
81 rep.blobState = 0x01;
82 rep.size = 0x100;
83 rep.metadataLen = 0x00;
84
Patrick Ventureb6ed5622020-09-28 13:17:14 -070085 uint16_t blobState = rep.blobState;
86 uint32_t size = rep.size;
87
Patrick Ventureef3aead2018-09-12 08:53:29 -070088 EXPECT_CALL(mgr, stat(Matcher<const std::string&>(StrEq(blobId)),
Patrick Venture8bc11772019-06-04 07:20:24 -070089 Matcher<BlobMeta*>(NotNull())))
William A. Kennington III993f5412021-06-15 18:19:18 -070090 .WillOnce(Invoke([&](const std::string&, BlobMeta* meta) {
Patrick Ventureb6ed5622020-09-28 13:17:14 -070091 meta->blobState = blobState;
92 meta->size = size;
Patrick Ventureef3aead2018-09-12 08:53:29 -070093 return true;
94 }));
95
Willy Tu067ece12022-06-16 02:07:06 -070096 auto result = validateReply(statBlob(&mgr, request));
Patrick Ventureef3aead2018-09-12 08:53:29 -070097
Willy Tu067ece12022-06-16 02:07:06 -070098 EXPECT_EQ(sizeof(rep), result.size());
99 EXPECT_EQ(0, std::memcmp(result.data(), &rep, sizeof(rep)));
Patrick Ventureef3aead2018-09-12 08:53:29 -0700100}
101
102TEST(BlobStatTest, RequestSucceedsWithMetadata)
103{
104 // Stat request succeeds and there were metadata bytes.
105
106 ManagerMock mgr;
Willy Tu067ece12022-06-16 02:07:06 -0700107 std::vector<uint8_t> request;
108 struct BmcBlobStatTx req;
Patrick Ventureef3aead2018-09-12 08:53:29 -0700109 std::string blobId = "a";
110
Willy Tu067ece12022-06-16 02:07:06 -0700111 req.crc = 0;
112 request.resize(sizeof(struct BmcBlobStatTx));
113 std::memcpy(request.data(), &req, sizeof(struct BmcBlobStatTx));
114 request.insert(request.end(), blobId.begin(), blobId.end());
115 request.emplace_back('\0');
Patrick Ventureef3aead2018-09-12 08:53:29 -0700116
Patrick Venture8bc11772019-06-04 07:20:24 -0700117 BlobMeta lmeta;
Patrick Ventureef3aead2018-09-12 08:53:29 -0700118 lmeta.blobState = 0x01;
119 lmeta.size = 0x100;
120 lmeta.metadata.push_back(0x01);
121 lmeta.metadata.push_back(0x02);
122 lmeta.metadata.push_back(0x03);
123 lmeta.metadata.push_back(0x04);
124
125 struct BmcBlobStatRx rep;
126 rep.crc = 0x00;
127 rep.blobState = lmeta.blobState;
128 rep.size = lmeta.size;
129 rep.metadataLen = lmeta.metadata.size();
130
131 EXPECT_CALL(mgr, stat(Matcher<const std::string&>(StrEq(blobId)),
Patrick Venture8bc11772019-06-04 07:20:24 -0700132 Matcher<BlobMeta*>(NotNull())))
William A. Kennington III993f5412021-06-15 18:19:18 -0700133 .WillOnce(Invoke([&](const std::string&, BlobMeta* meta) {
Patrick Ventureef3aead2018-09-12 08:53:29 -0700134 (*meta) = lmeta;
135 return true;
136 }));
137
Willy Tu067ece12022-06-16 02:07:06 -0700138 auto result = validateReply(statBlob(&mgr, request));
Patrick Ventureef3aead2018-09-12 08:53:29 -0700139
Willy Tu067ece12022-06-16 02:07:06 -0700140 EXPECT_EQ(sizeof(rep) + lmeta.metadata.size(), result.size());
141 EXPECT_EQ(0, std::memcmp(result.data(), &rep, sizeof(rep)));
142 EXPECT_EQ(0, std::memcmp(result.data() + sizeof(rep), lmeta.metadata.data(),
Patrick Ventureef3aead2018-09-12 08:53:29 -0700143 lmeta.metadata.size()));
144}
145} // namespace blobs