blob: 704c1447f72a1abc0cc88bc79a71be74b641aa72 [file] [log] [blame]
Patrick Ventureef3aead2018-09-12 08:53:29 -07001#include "ipmi.hpp"
Patrick Ventureef3aead2018-09-12 08:53:29 -07002
Patrick Venture7210b312018-10-03 14:01:35 -07003#include <blobs-ipmid/test/manager_mock.hpp>
Patrick Ventureef3aead2018-09-12 08:53:29 -07004#include <cstring>
5#include <string>
6
7#include <gtest/gtest.h>
8
9namespace blobs
10{
11
12using ::testing::_;
13using ::testing::Invoke;
14using ::testing::NotNull;
15using ::testing::Return;
16using ::testing::StrEq;
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(BlobOpenTest, InvalidRequestLengthReturnsFailure)
23{
24 // There is a minimum blobId length of one character, this test verifies
25 // we check that.
26
27 ManagerMock mgr;
28 size_t dataLen;
29 uint8_t request[MAX_IPMI_BUFFER] = {0};
30 uint8_t reply[MAX_IPMI_BUFFER] = {0};
31 auto req = reinterpret_cast<struct BmcBlobOpenTx*>(request);
32 std::string blobId = "abc";
33
34 req->cmd = BlobOEMCommands::bmcBlobOpen;
35 req->crc = 0;
36 req->flags = 0;
37 // length() doesn't include the nul-terminator.
38 std::memcpy(req->blobId, blobId.c_str(), blobId.length());
39
40 dataLen = sizeof(struct BmcBlobOpenTx) + blobId.length();
41
Patrick Venture41258802018-11-12 10:46:30 -080042 EXPECT_EQ(IPMI_CC_REQ_DATA_LEN_INVALID,
43 openBlob(&mgr, request, reply, &dataLen));
Patrick Ventureef3aead2018-09-12 08:53:29 -070044}
45
46TEST(BlobOpenTest, RequestRejectedReturnsFailure)
47{
48 // The blobId is rejected for any reason.
49
50 ManagerMock mgr;
51 size_t dataLen;
52 uint8_t request[MAX_IPMI_BUFFER] = {0};
53 uint8_t reply[MAX_IPMI_BUFFER] = {0};
54 auto req = reinterpret_cast<struct BmcBlobOpenTx*>(request);
55 std::string blobId = "a";
56
57 req->cmd = BlobOEMCommands::bmcBlobOpen;
58 req->crc = 0;
59 req->flags = 0;
60 // length() doesn't include the nul-terminator, request buff is initialized
61 // to 0s
62 std::memcpy(req->blobId, blobId.c_str(), blobId.length());
63
64 dataLen = sizeof(struct BmcBlobOpenTx) + blobId.length() + 1;
65
66 EXPECT_CALL(mgr, open(req->flags, StrEq(blobId), _))
67 .WillOnce(Return(false));
68
Patrick Venture41258802018-11-12 10:46:30 -080069 EXPECT_EQ(IPMI_CC_UNSPECIFIED_ERROR,
70 openBlob(&mgr, request, reply, &dataLen));
Patrick Ventureef3aead2018-09-12 08:53:29 -070071}
72
73TEST(BlobOpenTest, BlobOpenReturnsOk)
74{
75 // The boring case where the blobId opens.
76
77 ManagerMock mgr;
78 size_t dataLen;
79 uint8_t request[MAX_IPMI_BUFFER] = {0};
80 uint8_t reply[MAX_IPMI_BUFFER] = {0};
81 auto req = reinterpret_cast<struct BmcBlobOpenTx*>(request);
82 struct BmcBlobOpenRx rep;
83 std::string blobId = "a";
84
85 req->cmd = BlobOEMCommands::bmcBlobOpen;
86 req->crc = 0;
87 req->flags = 0;
88 // length() doesn't include the nul-terminator, request buff is initialized
89 // to 0s
90 std::memcpy(req->blobId, blobId.c_str(), blobId.length());
91
92 dataLen = sizeof(struct BmcBlobOpenTx) + blobId.length() + 1;
93 uint16_t returnedSession = 0x54;
94
95 EXPECT_CALL(mgr, open(req->flags, StrEq(blobId), NotNull()))
96 .WillOnce(Invoke(
97 [&](uint16_t flags, const std::string& path, uint16_t* session) {
98 (*session) = returnedSession;
99 return true;
100 }));
101
102 EXPECT_EQ(IPMI_CC_OK, openBlob(&mgr, request, reply, &dataLen));
103
104 rep.crc = 0;
105 rep.sessionId = returnedSession;
106
107 EXPECT_EQ(sizeof(rep), dataLen);
108 EXPECT_EQ(0, std::memcmp(reply, &rep, sizeof(rep)));
109}
110} // namespace blobs