blob: b938cbe7b00043b512ce64262e21b237c49d1329 [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::NotNull;
16using ::testing::Return;
17using ::testing::StrEq;
18
Patrick Ventureef3aead2018-09-12 08:53:29 -070019TEST(BlobOpenTest, InvalidRequestLengthReturnsFailure)
20{
21 // There is a minimum blobId length of one character, this test verifies
22 // we check that.
Patrick Ventureef3aead2018-09-12 08:53:29 -070023 ManagerMock mgr;
Willy Tu067ece12022-06-16 02:07:06 -070024 std::vector<uint8_t> request;
25 BmcBlobOpenTx req;
Patrick Ventureef3aead2018-09-12 08:53:29 -070026 std::string blobId = "abc";
27
Willy Tu067ece12022-06-16 02:07:06 -070028 req.crc = 0;
29 req.flags = 0;
Patrick Ventureef3aead2018-09-12 08:53:29 -070030
Willy Tu067ece12022-06-16 02:07:06 -070031 // Missintg the nul-terminator.
32 request.resize(sizeof(struct BmcBlobOpenTx));
33 std::memcpy(request.data(), &req, sizeof(struct BmcBlobOpenTx));
34 request.insert(request.end(), blobId.begin(), blobId.end());
Patrick Ventureef3aead2018-09-12 08:53:29 -070035
Willy Tu067ece12022-06-16 02:07:06 -070036 EXPECT_EQ(ipmi::responseReqDataLenInvalid(), openBlob(&mgr, request));
Patrick Ventureef3aead2018-09-12 08:53:29 -070037}
38
39TEST(BlobOpenTest, RequestRejectedReturnsFailure)
40{
41 // The blobId is rejected for any reason.
Patrick Ventureef3aead2018-09-12 08:53:29 -070042 ManagerMock mgr;
Willy Tu067ece12022-06-16 02:07:06 -070043 std::vector<uint8_t> request;
44 BmcBlobOpenTx req;
Patrick Ventureef3aead2018-09-12 08:53:29 -070045 std::string blobId = "a";
46
Willy Tu067ece12022-06-16 02:07:06 -070047 req.crc = 0;
48 req.flags = 0;
49 request.resize(sizeof(struct BmcBlobOpenTx));
50 std::memcpy(request.data(), &req, sizeof(struct BmcBlobOpenTx));
51 request.insert(request.end(), blobId.begin(), blobId.end());
52 request.emplace_back('\0');
Patrick Ventureef3aead2018-09-12 08:53:29 -070053
Willy Tu067ece12022-06-16 02:07:06 -070054 EXPECT_CALL(mgr, open(req.flags, StrEq(blobId), _)).WillOnce(Return(false));
Patrick Ventureef3aead2018-09-12 08:53:29 -070055
Willy Tu067ece12022-06-16 02:07:06 -070056 EXPECT_EQ(ipmi::responseUnspecifiedError(), openBlob(&mgr, request));
Patrick Ventureef3aead2018-09-12 08:53:29 -070057}
58
59TEST(BlobOpenTest, BlobOpenReturnsOk)
60{
61 // The boring case where the blobId opens.
62
63 ManagerMock mgr;
Willy Tu067ece12022-06-16 02:07:06 -070064 std::vector<uint8_t> request;
65 BmcBlobOpenTx req;
Patrick Ventureef3aead2018-09-12 08:53:29 -070066 struct BmcBlobOpenRx rep;
67 std::string blobId = "a";
68
Willy Tu067ece12022-06-16 02:07:06 -070069 req.crc = 0;
70 req.flags = 0;
71 request.resize(sizeof(struct BmcBlobOpenTx));
72 std::memcpy(request.data(), &req, sizeof(struct BmcBlobOpenTx));
73 request.insert(request.end(), blobId.begin(), blobId.end());
74 request.emplace_back('\0');
Patrick Ventureef3aead2018-09-12 08:53:29 -070075
Patrick Ventureef3aead2018-09-12 08:53:29 -070076 uint16_t returnedSession = 0x54;
77
Willy Tu067ece12022-06-16 02:07:06 -070078 EXPECT_CALL(mgr, open(req.flags, StrEq(blobId), NotNull()))
William A. Kennington III993f5412021-06-15 18:19:18 -070079 .WillOnce(Invoke([&](uint16_t, const std::string&, uint16_t* session) {
Patrick Williams97e69ca2024-08-16 15:21:49 -040080 (*session) = returnedSession;
81 return true;
82 }));
Patrick Ventureef3aead2018-09-12 08:53:29 -070083
Willy Tu067ece12022-06-16 02:07:06 -070084 auto result = validateReply(openBlob(&mgr, request));
Patrick Ventureef3aead2018-09-12 08:53:29 -070085
86 rep.crc = 0;
87 rep.sessionId = returnedSession;
88
Willy Tu067ece12022-06-16 02:07:06 -070089 EXPECT_EQ(sizeof(rep), result.size());
90 EXPECT_EQ(0, std::memcmp(result.data(), &rep, sizeof(rep)));
Patrick Ventureef3aead2018-09-12 08:53:29 -070091}
92} // namespace blobs