1 #include "ipmi.hpp"
2 #include "manager_mock.hpp"
3 
4 #include <cstring>
5 
6 #include <gtest/gtest.h>
7 
8 namespace blobs
9 {
10 
11 using ::testing::_;
12 using ::testing::Invoke;
13 using ::testing::Matcher;
14 using ::testing::NotNull;
15 using ::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 
21 TEST(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 
40     EXPECT_EQ(IPMI_CC_INVALID, sessionStatBlob(&mgr, request, reply, &dataLen));
41 }
42 
43 TEST(BlobSessionStatTest, RequestSucceedsNoMetadata)
44 {
45     // Stat request succeeeds but there were no metadata bytes.
46 
47     ManagerMock mgr;
48     size_t dataLen;
49     uint8_t request[MAX_IPMI_BUFFER] = {0};
50     uint8_t reply[MAX_IPMI_BUFFER] = {0};
51     auto req = reinterpret_cast<struct BmcBlobSessionStatTx*>(request);
52     req->cmd = BlobOEMCommands::bmcBlobSessionStat;
53     req->crc = 0;
54     req->sessionId = 0x54;
55 
56     dataLen = sizeof(struct BmcBlobSessionStatTx);
57 
58     struct BmcBlobStatRx rep;
59     rep.crc = 0x00;
60     rep.blobState = 0x01;
61     rep.size = 0x100;
62     rep.metadataLen = 0x00;
63 
64     EXPECT_CALL(mgr, stat(Matcher<uint16_t>(req->sessionId),
65                           Matcher<struct BlobMeta*>(NotNull())))
66         .WillOnce(Invoke([&](uint16_t session, struct BlobMeta* meta) {
67             meta->blobState = rep.blobState;
68             meta->size = rep.size;
69             return true;
70         }));
71 
72     EXPECT_EQ(IPMI_CC_OK, sessionStatBlob(&mgr, request, reply, &dataLen));
73 
74     EXPECT_EQ(sizeof(rep), dataLen);
75     EXPECT_EQ(0, std::memcmp(reply, &rep, sizeof(rep)));
76 }
77 
78 TEST(BlobSessionStatTest, RequestSucceedsWithMetadata)
79 {
80     // Stat request succeeds and there were metadata bytes.
81 
82     ManagerMock mgr;
83     size_t dataLen;
84     uint8_t request[MAX_IPMI_BUFFER] = {0};
85     uint8_t reply[MAX_IPMI_BUFFER] = {0};
86     auto req = reinterpret_cast<struct BmcBlobSessionStatTx*>(request);
87     req->cmd = BlobOEMCommands::bmcBlobSessionStat;
88     req->crc = 0;
89     req->sessionId = 0x54;
90 
91     dataLen = sizeof(struct BmcBlobSessionStatTx);
92 
93     struct BlobMeta lmeta;
94     lmeta.blobState = 0x01;
95     lmeta.size = 0x100;
96     lmeta.metadata.push_back(0x01);
97     lmeta.metadata.push_back(0x02);
98     lmeta.metadata.push_back(0x03);
99     lmeta.metadata.push_back(0x04);
100 
101     struct BmcBlobStatRx rep;
102     rep.crc = 0x00;
103     rep.blobState = lmeta.blobState;
104     rep.size = lmeta.size;
105     rep.metadataLen = lmeta.metadata.size();
106 
107     EXPECT_CALL(mgr, stat(Matcher<uint16_t>(req->sessionId),
108                           Matcher<struct BlobMeta*>(NotNull())))
109         .WillOnce(Invoke([&](uint16_t session, struct BlobMeta* meta) {
110             (*meta) = lmeta;
111             return true;
112         }));
113 
114     EXPECT_EQ(IPMI_CC_OK, sessionStatBlob(&mgr, request, reply, &dataLen));
115 
116     EXPECT_EQ(sizeof(rep) + lmeta.metadata.size(), dataLen);
117     EXPECT_EQ(0, std::memcmp(reply, &rep, sizeof(rep)));
118     EXPECT_EQ(0, std::memcmp(reply + sizeof(rep), lmeta.metadata.data(),
119                              lmeta.metadata.size()));
120 }
121 } // namespace blobs
122