1*067ece15SWilly Tu #include "helper.hpp"
2ef3aeadcSPatrick Venture #include "ipmi.hpp"
3cd8dab49SPatrick Venture #include "manager_mock.hpp"
4ef3aeadcSPatrick Venture
5ef3aeadcSPatrick Venture #include <cstring>
6ef3aeadcSPatrick Venture
7ef3aeadcSPatrick Venture #include <gtest/gtest.h>
8ef3aeadcSPatrick Venture
9ef3aeadcSPatrick Venture namespace blobs
10ef3aeadcSPatrick Venture {
11ef3aeadcSPatrick Venture
12ef3aeadcSPatrick Venture using ::testing::_;
13ef3aeadcSPatrick Venture using ::testing::Invoke;
14ef3aeadcSPatrick Venture using ::testing::Matcher;
15ef3aeadcSPatrick Venture using ::testing::NotNull;
16ef3aeadcSPatrick Venture using ::testing::Return;
17ef3aeadcSPatrick Venture
18ef3aeadcSPatrick Venture // ipmid.hpp isn't installed where we can grab it and this value is per BMC
19ef3aeadcSPatrick Venture // SoC.
20ef3aeadcSPatrick Venture #define MAX_IPMI_BUFFER 64
21ef3aeadcSPatrick Venture
TEST(BlobSessionStatTest,RequestRejectedByManagerReturnsFailure)22ef3aeadcSPatrick Venture TEST(BlobSessionStatTest, RequestRejectedByManagerReturnsFailure)
23ef3aeadcSPatrick Venture {
24ef3aeadcSPatrick Venture // If the session ID is invalid, the request must fail.
25ef3aeadcSPatrick Venture
26ef3aeadcSPatrick Venture ManagerMock mgr;
27*067ece15SWilly Tu std::vector<uint8_t> request;
28*067ece15SWilly Tu struct BmcBlobSessionStatTx req;
29*067ece15SWilly Tu req.crc = 0;
30*067ece15SWilly Tu req.sessionId = 0x54;
31ef3aeadcSPatrick Venture
32*067ece15SWilly Tu request.resize(sizeof(struct BmcBlobSessionStatTx));
33*067ece15SWilly Tu std::memcpy(request.data(), &req, sizeof(struct BmcBlobSessionStatTx));
34ef3aeadcSPatrick Venture
358bc11779SPatrick Venture EXPECT_CALL(mgr,
36*067ece15SWilly Tu stat(Matcher<uint16_t>(req.sessionId), Matcher<BlobMeta*>(_)))
37ef3aeadcSPatrick Venture .WillOnce(Return(false));
38ef3aeadcSPatrick Venture
39*067ece15SWilly Tu EXPECT_EQ(ipmi::responseUnspecifiedError(), sessionStatBlob(&mgr, request));
40ef3aeadcSPatrick Venture }
41ef3aeadcSPatrick Venture
TEST(BlobSessionStatTest,RequestSucceedsNoMetadata)42ef3aeadcSPatrick Venture TEST(BlobSessionStatTest, RequestSucceedsNoMetadata)
43ef3aeadcSPatrick Venture {
44ef3aeadcSPatrick Venture // Stat request succeeeds but there were no metadata bytes.
45ef3aeadcSPatrick Venture
46ef3aeadcSPatrick Venture ManagerMock mgr;
47*067ece15SWilly Tu std::vector<uint8_t> request;
48*067ece15SWilly Tu struct BmcBlobSessionStatTx req;
49*067ece15SWilly Tu req.crc = 0;
50*067ece15SWilly Tu req.sessionId = 0x54;
51ef3aeadcSPatrick Venture
52*067ece15SWilly Tu request.resize(sizeof(struct BmcBlobSessionStatTx));
53*067ece15SWilly Tu std::memcpy(request.data(), &req, sizeof(struct BmcBlobSessionStatTx));
54ef3aeadcSPatrick Venture
55ef3aeadcSPatrick Venture struct BmcBlobStatRx rep;
56ef3aeadcSPatrick Venture rep.crc = 0x00;
57ef3aeadcSPatrick Venture rep.blobState = 0x01;
58ef3aeadcSPatrick Venture rep.size = 0x100;
59ef3aeadcSPatrick Venture rep.metadataLen = 0x00;
60ef3aeadcSPatrick Venture
61b6ed5626SPatrick Venture uint16_t blobState = rep.blobState;
62b6ed5626SPatrick Venture uint32_t size = rep.size;
63b6ed5626SPatrick Venture
64*067ece15SWilly Tu EXPECT_CALL(mgr, stat(Matcher<uint16_t>(req.sessionId),
658bc11779SPatrick Venture Matcher<BlobMeta*>(NotNull())))
66993f5419SWilliam A. Kennington III .WillOnce(Invoke([&](uint16_t, BlobMeta* meta) {
67b6ed5626SPatrick Venture meta->blobState = blobState;
68b6ed5626SPatrick Venture meta->size = size;
69ef3aeadcSPatrick Venture return true;
70ef3aeadcSPatrick Venture }));
71ef3aeadcSPatrick Venture
72*067ece15SWilly Tu auto result = validateReply(sessionStatBlob(&mgr, request));
73ef3aeadcSPatrick Venture
74*067ece15SWilly Tu EXPECT_EQ(sizeof(rep), result.size());
75*067ece15SWilly Tu EXPECT_EQ(0, std::memcmp(result.data(), &rep, sizeof(rep)));
76ef3aeadcSPatrick Venture }
77ef3aeadcSPatrick Venture
TEST(BlobSessionStatTest,RequestSucceedsWithMetadata)78ef3aeadcSPatrick Venture TEST(BlobSessionStatTest, RequestSucceedsWithMetadata)
79ef3aeadcSPatrick Venture {
80ef3aeadcSPatrick Venture // Stat request succeeds and there were metadata bytes.
81ef3aeadcSPatrick Venture
82ef3aeadcSPatrick Venture ManagerMock mgr;
83*067ece15SWilly Tu std::vector<uint8_t> request;
84*067ece15SWilly Tu struct BmcBlobSessionStatTx req;
85*067ece15SWilly Tu req.crc = 0;
86*067ece15SWilly Tu req.sessionId = 0x54;
87ef3aeadcSPatrick Venture
88*067ece15SWilly Tu request.resize(sizeof(struct BmcBlobSessionStatTx));
89*067ece15SWilly Tu std::memcpy(request.data(), &req, sizeof(struct BmcBlobSessionStatTx));
90ef3aeadcSPatrick Venture
918bc11779SPatrick Venture BlobMeta lmeta;
92ef3aeadcSPatrick Venture lmeta.blobState = 0x01;
93ef3aeadcSPatrick Venture lmeta.size = 0x100;
94ef3aeadcSPatrick Venture lmeta.metadata.push_back(0x01);
95ef3aeadcSPatrick Venture lmeta.metadata.push_back(0x02);
96ef3aeadcSPatrick Venture lmeta.metadata.push_back(0x03);
97ef3aeadcSPatrick Venture lmeta.metadata.push_back(0x04);
98ef3aeadcSPatrick Venture
99ef3aeadcSPatrick Venture struct BmcBlobStatRx rep;
100ef3aeadcSPatrick Venture rep.crc = 0x00;
101ef3aeadcSPatrick Venture rep.blobState = lmeta.blobState;
102ef3aeadcSPatrick Venture rep.size = lmeta.size;
103ef3aeadcSPatrick Venture rep.metadataLen = lmeta.metadata.size();
104ef3aeadcSPatrick Venture
105*067ece15SWilly Tu EXPECT_CALL(mgr, stat(Matcher<uint16_t>(req.sessionId),
1068bc11779SPatrick Venture Matcher<BlobMeta*>(NotNull())))
107993f5419SWilliam A. Kennington III .WillOnce(Invoke([&](uint16_t, BlobMeta* meta) {
108ef3aeadcSPatrick Venture (*meta) = lmeta;
109ef3aeadcSPatrick Venture return true;
110ef3aeadcSPatrick Venture }));
111ef3aeadcSPatrick Venture
112*067ece15SWilly Tu auto result = validateReply(sessionStatBlob(&mgr, request));
113ef3aeadcSPatrick Venture
114*067ece15SWilly Tu EXPECT_EQ(sizeof(rep) + lmeta.metadata.size(), result.size());
115*067ece15SWilly Tu EXPECT_EQ(0, std::memcmp(result.data(), &rep, sizeof(rep)));
116*067ece15SWilly Tu EXPECT_EQ(0, std::memcmp(result.data() + sizeof(rep), lmeta.metadata.data(),
117ef3aeadcSPatrick Venture lmeta.metadata.size()));
118ef3aeadcSPatrick Venture }
119ef3aeadcSPatrick Venture } // namespace blobs
120