xref: /openbmc/phosphor-ipmi-blobs/test/ipmi_sessionstat_unittest.cpp (revision 97e69ca106fc2415f89370eea36fb674435b5bdb)
1 #include "helper.hpp"
2 #include "ipmi.hpp"
3 #include "manager_mock.hpp"
4 
5 #include <cstring>
6 
7 #include <gtest/gtest.h>
8 
9 namespace blobs
10 {
11 
12 using ::testing::_;
13 using ::testing::Invoke;
14 using ::testing::Matcher;
15 using ::testing::NotNull;
16 using ::testing::Return;
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 
TEST(BlobSessionStatTest,RequestRejectedByManagerReturnsFailure)22 TEST(BlobSessionStatTest, RequestRejectedByManagerReturnsFailure)
23 {
24     // If the session ID is invalid, the request must fail.
25 
26     ManagerMock mgr;
27     std::vector<uint8_t> request;
28     struct BmcBlobSessionStatTx req;
29     req.crc = 0;
30     req.sessionId = 0x54;
31 
32     request.resize(sizeof(struct BmcBlobSessionStatTx));
33     std::memcpy(request.data(), &req, sizeof(struct BmcBlobSessionStatTx));
34 
35     EXPECT_CALL(mgr,
36                 stat(Matcher<uint16_t>(req.sessionId), Matcher<BlobMeta*>(_)))
37         .WillOnce(Return(false));
38 
39     EXPECT_EQ(ipmi::responseUnspecifiedError(), sessionStatBlob(&mgr, request));
40 }
41 
TEST(BlobSessionStatTest,RequestSucceedsNoMetadata)42 TEST(BlobSessionStatTest, RequestSucceedsNoMetadata)
43 {
44     // Stat request succeeeds but there were no metadata bytes.
45 
46     ManagerMock mgr;
47     std::vector<uint8_t> request;
48     struct BmcBlobSessionStatTx req;
49     req.crc = 0;
50     req.sessionId = 0x54;
51 
52     request.resize(sizeof(struct BmcBlobSessionStatTx));
53     std::memcpy(request.data(), &req, sizeof(struct BmcBlobSessionStatTx));
54 
55     struct BmcBlobStatRx rep;
56     rep.crc = 0x00;
57     rep.blobState = 0x01;
58     rep.size = 0x100;
59     rep.metadataLen = 0x00;
60 
61     uint16_t blobState = rep.blobState;
62     uint32_t size = rep.size;
63 
64     EXPECT_CALL(mgr, stat(Matcher<uint16_t>(req.sessionId),
65                           Matcher<BlobMeta*>(NotNull())))
66         .WillOnce(Invoke([&](uint16_t, BlobMeta* meta) {
67             meta->blobState = blobState;
68             meta->size = size;
69             return true;
70         }));
71 
72     auto result = validateReply(sessionStatBlob(&mgr, request));
73 
74     EXPECT_EQ(sizeof(rep), result.size());
75     EXPECT_EQ(0, std::memcmp(result.data(), &rep, sizeof(rep)));
76 }
77 
TEST(BlobSessionStatTest,RequestSucceedsWithMetadata)78 TEST(BlobSessionStatTest, RequestSucceedsWithMetadata)
79 {
80     // Stat request succeeds and there were metadata bytes.
81 
82     ManagerMock mgr;
83     std::vector<uint8_t> request;
84     struct BmcBlobSessionStatTx req;
85     req.crc = 0;
86     req.sessionId = 0x54;
87 
88     request.resize(sizeof(struct BmcBlobSessionStatTx));
89     std::memcpy(request.data(), &req, sizeof(struct BmcBlobSessionStatTx));
90 
91     BlobMeta lmeta;
92     lmeta.blobState = 0x01;
93     lmeta.size = 0x100;
94     lmeta.metadata.push_back(0x01);
95     lmeta.metadata.push_back(0x02);
96     lmeta.metadata.push_back(0x03);
97     lmeta.metadata.push_back(0x04);
98 
99     struct BmcBlobStatRx rep;
100     rep.crc = 0x00;
101     rep.blobState = lmeta.blobState;
102     rep.size = lmeta.size;
103     rep.metadataLen = lmeta.metadata.size();
104 
105     EXPECT_CALL(mgr, stat(Matcher<uint16_t>(req.sessionId),
106                           Matcher<BlobMeta*>(NotNull())))
107         .WillOnce(Invoke([&](uint16_t, BlobMeta* meta) {
108             (*meta) = lmeta;
109             return true;
110         }));
111 
112     auto result = validateReply(sessionStatBlob(&mgr, request));
113 
114     EXPECT_EQ(sizeof(rep) + lmeta.metadata.size(), result.size());
115     EXPECT_EQ(0, std::memcmp(result.data(), &rep, sizeof(rep)));
116     EXPECT_EQ(0, std::memcmp(result.data() + sizeof(rep), lmeta.metadata.data(),
117                              lmeta.metadata.size()));
118 }
119 } // namespace blobs
120