blob: a71252c532dbd57b6915d432c6f0fc6647e7dcbd [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
6#include <gtest/gtest.h>
7
8namespace blobs
9{
10
11using ::testing::_;
12using ::testing::Invoke;
13using ::testing::Matcher;
14using ::testing::NotNull;
15using ::testing::Return;
16
17// ipmid.hpp isn't installed where we can grab it and this value is per BMC
18// SoC.
19#define MAX_IPMI_BUFFER 64
20
21TEST(BlobSessionStatTest, RequestRejectedByManagerReturnsFailure)
22{
23 // If the session ID is invalid, the request must fail.
24
25 ManagerMock mgr;
26 size_t dataLen;
27 uint8_t request[MAX_IPMI_BUFFER] = {0};
28 uint8_t reply[MAX_IPMI_BUFFER] = {0};
29 auto req = reinterpret_cast<struct BmcBlobSessionStatTx*>(request);
30 req->cmd = BlobOEMCommands::bmcBlobSessionStat;
31 req->crc = 0;
32 req->sessionId = 0x54;
33
34 dataLen = sizeof(struct BmcBlobSessionStatTx);
35
36 EXPECT_CALL(mgr, stat(Matcher<uint16_t>(req->sessionId),
37 Matcher<struct BlobMeta*>(_)))
38 .WillOnce(Return(false));
39
Patrick Venture41258802018-11-12 10:46:30 -080040 EXPECT_EQ(IPMI_CC_UNSPECIFIED_ERROR,
41 sessionStatBlob(&mgr, request, reply, &dataLen));
Patrick Ventureef3aead2018-09-12 08:53:29 -070042}
43
44TEST(BlobSessionStatTest, RequestSucceedsNoMetadata)
45{
46 // Stat request succeeeds but there were no metadata bytes.
47
48 ManagerMock mgr;
49 size_t dataLen;
50 uint8_t request[MAX_IPMI_BUFFER] = {0};
51 uint8_t reply[MAX_IPMI_BUFFER] = {0};
52 auto req = reinterpret_cast<struct BmcBlobSessionStatTx*>(request);
53 req->cmd = BlobOEMCommands::bmcBlobSessionStat;
54 req->crc = 0;
55 req->sessionId = 0x54;
56
57 dataLen = sizeof(struct BmcBlobSessionStatTx);
58
59 struct BmcBlobStatRx rep;
60 rep.crc = 0x00;
61 rep.blobState = 0x01;
62 rep.size = 0x100;
63 rep.metadataLen = 0x00;
64
65 EXPECT_CALL(mgr, stat(Matcher<uint16_t>(req->sessionId),
66 Matcher<struct BlobMeta*>(NotNull())))
67 .WillOnce(Invoke([&](uint16_t session, struct BlobMeta* meta) {
68 meta->blobState = rep.blobState;
69 meta->size = rep.size;
70 return true;
71 }));
72
73 EXPECT_EQ(IPMI_CC_OK, sessionStatBlob(&mgr, request, reply, &dataLen));
74
75 EXPECT_EQ(sizeof(rep), dataLen);
76 EXPECT_EQ(0, std::memcmp(reply, &rep, sizeof(rep)));
77}
78
79TEST(BlobSessionStatTest, RequestSucceedsWithMetadata)
80{
81 // Stat request succeeds and there were metadata bytes.
82
83 ManagerMock mgr;
84 size_t dataLen;
85 uint8_t request[MAX_IPMI_BUFFER] = {0};
86 uint8_t reply[MAX_IPMI_BUFFER] = {0};
87 auto req = reinterpret_cast<struct BmcBlobSessionStatTx*>(request);
88 req->cmd = BlobOEMCommands::bmcBlobSessionStat;
89 req->crc = 0;
90 req->sessionId = 0x54;
91
92 dataLen = sizeof(struct BmcBlobSessionStatTx);
93
94 struct BlobMeta lmeta;
95 lmeta.blobState = 0x01;
96 lmeta.size = 0x100;
97 lmeta.metadata.push_back(0x01);
98 lmeta.metadata.push_back(0x02);
99 lmeta.metadata.push_back(0x03);
100 lmeta.metadata.push_back(0x04);
101
102 struct BmcBlobStatRx rep;
103 rep.crc = 0x00;
104 rep.blobState = lmeta.blobState;
105 rep.size = lmeta.size;
106 rep.metadataLen = lmeta.metadata.size();
107
108 EXPECT_CALL(mgr, stat(Matcher<uint16_t>(req->sessionId),
109 Matcher<struct BlobMeta*>(NotNull())))
110 .WillOnce(Invoke([&](uint16_t session, struct BlobMeta* meta) {
111 (*meta) = lmeta;
112 return true;
113 }));
114
115 EXPECT_EQ(IPMI_CC_OK, sessionStatBlob(&mgr, request, reply, &dataLen));
116
117 EXPECT_EQ(sizeof(rep) + lmeta.metadata.size(), dataLen);
118 EXPECT_EQ(0, std::memcmp(reply, &rep, sizeof(rep)));
119 EXPECT_EQ(0, std::memcmp(reply + sizeof(rep), lmeta.metadata.data(),
120 lmeta.metadata.size()));
121}
122} // namespace blobs