blob: 3278f255cb1bfa0ec5b59d7ca8c6b3025d37d282 [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
7#include <gtest/gtest.h>
8
9namespace blobs
10{
11
12using ::testing::_;
13using ::testing::Invoke;
14using ::testing::Matcher;
15using ::testing::NotNull;
16using ::testing::Return;
17
18// ipmid.hpp isn't installed where we can grab it and this value is per BMC
19// SoC.
20#define MAX_IPMI_BUFFER 64
21
22TEST(BlobSessionStatTest, RequestRejectedByManagerReturnsFailure)
23{
24 // If the session ID is invalid, the request must fail.
25
26 ManagerMock mgr;
Willy Tu067ece12022-06-16 02:07:06 -070027 std::vector<uint8_t> request;
28 struct BmcBlobSessionStatTx req;
29 req.crc = 0;
30 req.sessionId = 0x54;
Patrick Ventureef3aead2018-09-12 08:53:29 -070031
Willy Tu067ece12022-06-16 02:07:06 -070032 request.resize(sizeof(struct BmcBlobSessionStatTx));
33 std::memcpy(request.data(), &req, sizeof(struct BmcBlobSessionStatTx));
Patrick Ventureef3aead2018-09-12 08:53:29 -070034
Patrick Venture8bc11772019-06-04 07:20:24 -070035 EXPECT_CALL(mgr,
Willy Tu067ece12022-06-16 02:07:06 -070036 stat(Matcher<uint16_t>(req.sessionId), Matcher<BlobMeta*>(_)))
Patrick Ventureef3aead2018-09-12 08:53:29 -070037 .WillOnce(Return(false));
38
Willy Tu067ece12022-06-16 02:07:06 -070039 EXPECT_EQ(ipmi::responseUnspecifiedError(), sessionStatBlob(&mgr, request));
Patrick Ventureef3aead2018-09-12 08:53:29 -070040}
41
42TEST(BlobSessionStatTest, RequestSucceedsNoMetadata)
43{
44 // Stat request succeeeds but there were no metadata bytes.
45
46 ManagerMock mgr;
Willy Tu067ece12022-06-16 02:07:06 -070047 std::vector<uint8_t> request;
48 struct BmcBlobSessionStatTx req;
49 req.crc = 0;
50 req.sessionId = 0x54;
Patrick Ventureef3aead2018-09-12 08:53:29 -070051
Willy Tu067ece12022-06-16 02:07:06 -070052 request.resize(sizeof(struct BmcBlobSessionStatTx));
53 std::memcpy(request.data(), &req, sizeof(struct BmcBlobSessionStatTx));
Patrick Ventureef3aead2018-09-12 08:53:29 -070054
55 struct BmcBlobStatRx rep;
56 rep.crc = 0x00;
57 rep.blobState = 0x01;
58 rep.size = 0x100;
59 rep.metadataLen = 0x00;
60
Patrick Ventureb6ed5622020-09-28 13:17:14 -070061 uint16_t blobState = rep.blobState;
62 uint32_t size = rep.size;
63
Willy Tu067ece12022-06-16 02:07:06 -070064 EXPECT_CALL(mgr, stat(Matcher<uint16_t>(req.sessionId),
Patrick Venture8bc11772019-06-04 07:20:24 -070065 Matcher<BlobMeta*>(NotNull())))
William A. Kennington III993f5412021-06-15 18:19:18 -070066 .WillOnce(Invoke([&](uint16_t, BlobMeta* meta) {
Patrick Ventureb6ed5622020-09-28 13:17:14 -070067 meta->blobState = blobState;
68 meta->size = size;
Patrick Ventureef3aead2018-09-12 08:53:29 -070069 return true;
70 }));
71
Willy Tu067ece12022-06-16 02:07:06 -070072 auto result = validateReply(sessionStatBlob(&mgr, request));
Patrick Ventureef3aead2018-09-12 08:53:29 -070073
Willy Tu067ece12022-06-16 02:07:06 -070074 EXPECT_EQ(sizeof(rep), result.size());
75 EXPECT_EQ(0, std::memcmp(result.data(), &rep, sizeof(rep)));
Patrick Ventureef3aead2018-09-12 08:53:29 -070076}
77
78TEST(BlobSessionStatTest, RequestSucceedsWithMetadata)
79{
80 // Stat request succeeds and there were metadata bytes.
81
82 ManagerMock mgr;
Willy Tu067ece12022-06-16 02:07:06 -070083 std::vector<uint8_t> request;
84 struct BmcBlobSessionStatTx req;
85 req.crc = 0;
86 req.sessionId = 0x54;
Patrick Ventureef3aead2018-09-12 08:53:29 -070087
Willy Tu067ece12022-06-16 02:07:06 -070088 request.resize(sizeof(struct BmcBlobSessionStatTx));
89 std::memcpy(request.data(), &req, sizeof(struct BmcBlobSessionStatTx));
Patrick Ventureef3aead2018-09-12 08:53:29 -070090
Patrick Venture8bc11772019-06-04 07:20:24 -070091 BlobMeta lmeta;
Patrick Ventureef3aead2018-09-12 08:53:29 -070092 lmeta.blobState = 0x01;
93 lmeta.size = 0x100;
94 lmeta.metadata.push_back(0x01);
95 lmeta.metadata.push_back(0x02);
96 lmeta.metadata.push_back(0x03);
97 lmeta.metadata.push_back(0x04);
98
99 struct BmcBlobStatRx rep;
100 rep.crc = 0x00;
101 rep.blobState = lmeta.blobState;
102 rep.size = lmeta.size;
103 rep.metadataLen = lmeta.metadata.size();
104
Willy Tu067ece12022-06-16 02:07:06 -0700105 EXPECT_CALL(mgr, stat(Matcher<uint16_t>(req.sessionId),
Patrick Venture8bc11772019-06-04 07:20:24 -0700106 Matcher<BlobMeta*>(NotNull())))
William A. Kennington III993f5412021-06-15 18:19:18 -0700107 .WillOnce(Invoke([&](uint16_t, BlobMeta* meta) {
Patrick Ventureef3aead2018-09-12 08:53:29 -0700108 (*meta) = lmeta;
109 return true;
110 }));
111
Willy Tu067ece12022-06-16 02:07:06 -0700112 auto result = validateReply(sessionStatBlob(&mgr, request));
Patrick Ventureef3aead2018-09-12 08:53:29 -0700113
Willy Tu067ece12022-06-16 02:07:06 -0700114 EXPECT_EQ(sizeof(rep) + lmeta.metadata.size(), result.size());
115 EXPECT_EQ(0, std::memcmp(result.data(), &rep, sizeof(rep)));
116 EXPECT_EQ(0, std::memcmp(result.data() + sizeof(rep), lmeta.metadata.data(),
Patrick Ventureef3aead2018-09-12 08:53:29 -0700117 lmeta.metadata.size()));
118}
119} // namespace blobs