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 #include <string>
7ef3aeadcSPatrick Venture
8ef3aeadcSPatrick Venture #include <gtest/gtest.h>
9ef3aeadcSPatrick Venture
10ef3aeadcSPatrick Venture namespace blobs
11ef3aeadcSPatrick Venture {
12ef3aeadcSPatrick Venture
13ef3aeadcSPatrick Venture using ::testing::_;
14ef3aeadcSPatrick Venture using ::testing::Invoke;
15ef3aeadcSPatrick Venture using ::testing::NotNull;
16ef3aeadcSPatrick Venture using ::testing::Return;
17ef3aeadcSPatrick Venture using ::testing::StrEq;
18ef3aeadcSPatrick Venture
TEST(BlobOpenTest,InvalidRequestLengthReturnsFailure)19ef3aeadcSPatrick Venture TEST(BlobOpenTest, InvalidRequestLengthReturnsFailure)
20ef3aeadcSPatrick Venture {
21ef3aeadcSPatrick Venture // There is a minimum blobId length of one character, this test verifies
22ef3aeadcSPatrick Venture // we check that.
23ef3aeadcSPatrick Venture ManagerMock mgr;
24*067ece15SWilly Tu std::vector<uint8_t> request;
25*067ece15SWilly Tu BmcBlobOpenTx req;
26ef3aeadcSPatrick Venture std::string blobId = "abc";
27ef3aeadcSPatrick Venture
28*067ece15SWilly Tu req.crc = 0;
29*067ece15SWilly Tu req.flags = 0;
30ef3aeadcSPatrick Venture
31*067ece15SWilly Tu // Missintg the nul-terminator.
32*067ece15SWilly Tu request.resize(sizeof(struct BmcBlobOpenTx));
33*067ece15SWilly Tu std::memcpy(request.data(), &req, sizeof(struct BmcBlobOpenTx));
34*067ece15SWilly Tu request.insert(request.end(), blobId.begin(), blobId.end());
35ef3aeadcSPatrick Venture
36*067ece15SWilly Tu EXPECT_EQ(ipmi::responseReqDataLenInvalid(), openBlob(&mgr, request));
37ef3aeadcSPatrick Venture }
38ef3aeadcSPatrick Venture
TEST(BlobOpenTest,RequestRejectedReturnsFailure)39ef3aeadcSPatrick Venture TEST(BlobOpenTest, RequestRejectedReturnsFailure)
40ef3aeadcSPatrick Venture {
41ef3aeadcSPatrick Venture // The blobId is rejected for any reason.
42ef3aeadcSPatrick Venture ManagerMock mgr;
43*067ece15SWilly Tu std::vector<uint8_t> request;
44*067ece15SWilly Tu BmcBlobOpenTx req;
45ef3aeadcSPatrick Venture std::string blobId = "a";
46ef3aeadcSPatrick Venture
47*067ece15SWilly Tu req.crc = 0;
48*067ece15SWilly Tu req.flags = 0;
49*067ece15SWilly Tu request.resize(sizeof(struct BmcBlobOpenTx));
50*067ece15SWilly Tu std::memcpy(request.data(), &req, sizeof(struct BmcBlobOpenTx));
51*067ece15SWilly Tu request.insert(request.end(), blobId.begin(), blobId.end());
52*067ece15SWilly Tu request.emplace_back('\0');
53ef3aeadcSPatrick Venture
54*067ece15SWilly Tu EXPECT_CALL(mgr, open(req.flags, StrEq(blobId), _)).WillOnce(Return(false));
55ef3aeadcSPatrick Venture
56*067ece15SWilly Tu EXPECT_EQ(ipmi::responseUnspecifiedError(), openBlob(&mgr, request));
57ef3aeadcSPatrick Venture }
58ef3aeadcSPatrick Venture
TEST(BlobOpenTest,BlobOpenReturnsOk)59ef3aeadcSPatrick Venture TEST(BlobOpenTest, BlobOpenReturnsOk)
60ef3aeadcSPatrick Venture {
61ef3aeadcSPatrick Venture // The boring case where the blobId opens.
62ef3aeadcSPatrick Venture
63ef3aeadcSPatrick Venture ManagerMock mgr;
64*067ece15SWilly Tu std::vector<uint8_t> request;
65*067ece15SWilly Tu BmcBlobOpenTx req;
66ef3aeadcSPatrick Venture struct BmcBlobOpenRx rep;
67ef3aeadcSPatrick Venture std::string blobId = "a";
68ef3aeadcSPatrick Venture
69*067ece15SWilly Tu req.crc = 0;
70*067ece15SWilly Tu req.flags = 0;
71*067ece15SWilly Tu request.resize(sizeof(struct BmcBlobOpenTx));
72*067ece15SWilly Tu std::memcpy(request.data(), &req, sizeof(struct BmcBlobOpenTx));
73*067ece15SWilly Tu request.insert(request.end(), blobId.begin(), blobId.end());
74*067ece15SWilly Tu request.emplace_back('\0');
75ef3aeadcSPatrick Venture
76ef3aeadcSPatrick Venture uint16_t returnedSession = 0x54;
77ef3aeadcSPatrick Venture
78*067ece15SWilly Tu EXPECT_CALL(mgr, open(req.flags, StrEq(blobId), NotNull()))
79993f5419SWilliam A. Kennington III .WillOnce(Invoke([&](uint16_t, const std::string&, uint16_t* session) {
80ef3aeadcSPatrick Venture (*session) = returnedSession;
81ef3aeadcSPatrick Venture return true;
82ef3aeadcSPatrick Venture }));
83ef3aeadcSPatrick Venture
84*067ece15SWilly Tu auto result = validateReply(openBlob(&mgr, request));
85ef3aeadcSPatrick Venture
86ef3aeadcSPatrick Venture rep.crc = 0;
87ef3aeadcSPatrick Venture rep.sessionId = returnedSession;
88ef3aeadcSPatrick Venture
89*067ece15SWilly Tu EXPECT_EQ(sizeof(rep), result.size());
90*067ece15SWilly Tu EXPECT_EQ(0, std::memcmp(result.data(), &rep, sizeof(rep)));
91ef3aeadcSPatrick Venture }
92ef3aeadcSPatrick Venture } // namespace blobs
93