blob: e7e0ed6b4b112ddba77b3ee27db3a6ed4ad42f17 [file] [log] [blame]
Patrick Venturec79faa12018-12-12 13:12:21 -08001#include "blob_handler.hpp"
2#include "ipmi_interface_mock.hpp"
3
4#include <gtest/gtest.h>
5
6std::uint16_t expectedCrc = 0;
7
8std::uint16_t generateCrc(const std::vector<std::uint8_t>& data)
9{
10 return expectedCrc;
11}
12
13using ::testing::Eq;
14using ::testing::Return;
15
16TEST(BlobHandler, getCountIpmiHappy)
17{
18 /* Verify returns the value specified by the IPMI response. */
19 IpmiInterfaceMock ipmiMock;
20 BlobHandler blob(&ipmiMock);
21 std::vector<std::uint8_t> request = {
22 0xcf, 0xc2, 0x00, BlobHandler::BlobOEMCommands::bmcBlobGetCount};
23
24 /* return 1 blob count. */
25 std::vector<std::uint8_t> resp = {0xcf, 0xc2, 0x00, 0x00, 0x00,
26 0x01, 0x00, 0x00, 0x00};
27
28 EXPECT_CALL(ipmiMock, sendPacket(Eq(request))).WillOnce(Return(resp));
29 EXPECT_EQ(1, blob.getBlobCount());
30}
31
32TEST(BlobHandler, enumerateBlobIpmiHappy)
33{
34 /* Verify returns the name specified by the IPMI response. */
35 IpmiInterfaceMock ipmiMock;
36 BlobHandler blob(&ipmiMock);
37 std::vector<std::uint8_t> request = {
38 0xcf, 0xc2, 0x00, BlobHandler::BlobOEMCommands::bmcBlobEnumerate,
39 0x00, 0x00, 0x01, 0x00,
40 0x00, 0x00};
41
42 /* return value. */
43 std::vector<std::uint8_t> resp = {0xcf, 0xc2, 0x00, 0x00, 0x00,
44 'a', 'b', 'c', 'd'};
45
46 EXPECT_CALL(ipmiMock, sendPacket(Eq(request))).WillOnce(Return(resp));
47 EXPECT_STREQ("abcd", blob.enumerateBlob(1).c_str());
48}
49
50TEST(BlobHandler, enumerateBlobIpmiNoBytes)
51{
52 /* Simulate a case where the IPMI command returns no data. */
53 IpmiInterfaceMock ipmiMock;
54 BlobHandler blob(&ipmiMock);
55 std::vector<std::uint8_t> request = {
56 0xcf, 0xc2, 0x00, BlobHandler::BlobOEMCommands::bmcBlobEnumerate,
57 0x00, 0x00, 0x01, 0x00,
58 0x00, 0x00};
59
60 /* return value. */
61 std::vector<std::uint8_t> resp = {};
62
63 EXPECT_CALL(ipmiMock, sendPacket(Eq(request))).WillOnce(Return(resp));
64 EXPECT_STREQ("", blob.enumerateBlob(1).c_str());
65}
66
67TEST(BlobHandler, getBlobListIpmiHappy)
68{
69 /* Verify returns the list built via the above two commands. */
70 IpmiInterfaceMock ipmiMock;
71 BlobHandler blob(&ipmiMock);
72
73 std::vector<std::uint8_t> request1 = {
74 0xcf, 0xc2, 0x00, BlobHandler::BlobOEMCommands::bmcBlobGetCount};
75
76 /* return 1 blob count. */
77 std::vector<std::uint8_t> resp1 = {0xcf, 0xc2, 0x00, 0x00, 0x00,
78 0x01, 0x00, 0x00, 0x00};
79
80 EXPECT_CALL(ipmiMock, sendPacket(Eq(request1))).WillOnce(Return(resp1));
81
82 std::vector<std::uint8_t> request2 = {
83 0xcf, 0xc2, 0x00, BlobHandler::BlobOEMCommands::bmcBlobEnumerate,
84 0x00, 0x00, 0x00, 0x00,
85 0x00, 0x00};
86
87 /* return value. */
88 std::vector<std::uint8_t> resp2 = {0xcf, 0xc2, 0x00, 0x00, 0x00,
89 'a', 'b', 'c', 'd'};
90
91 EXPECT_CALL(ipmiMock, sendPacket(Eq(request2))).WillOnce(Return(resp2));
92
93 std::vector<std::string> expectedList = {"abcd"};
94
95 EXPECT_EQ(expectedList, blob.getBlobList());
96}
Patrick Venture0bf8bf02018-12-12 20:43:25 -080097
98TEST(BlobHandler, getStatWithMetadata)
99{
100 /* Stat received metadata. */
101 IpmiInterfaceMock ipmiMock;
102 BlobHandler blob(&ipmiMock);
103 std::vector<std::uint8_t> request = {
104 0xcf, 0xc2, 0x00, BlobHandler::BlobOEMCommands::bmcBlobStat,
105 0x00, 0x00, 'a', 'b',
106 'c', 'd'};
107
108 /* return blob_state: 0xffff, size: 0x00, metadata 0x3445 */
109 std::vector<std::uint8_t> resp = {0xcf, 0xc2, 0x00, 0x00, 0x00, 0xff, 0xff,
110 0x00, 0x00, 0x00, 0x00, 0x02, 0x34, 0x45};
111
112 EXPECT_CALL(ipmiMock, sendPacket(Eq(request))).WillOnce(Return(resp));
113
114 auto meta = blob.getStat("abcd");
115 EXPECT_EQ(meta.blob_state, 0xffff);
116 EXPECT_EQ(meta.size, 0x00);
117 std::vector<std::uint8_t> metadata = {0x34, 0x45};
118 EXPECT_EQ(metadata, meta.metadata);
119}
120
121TEST(BlobHandler, getStatNoMetadata)
122{
123 /* Stat received no metadata. */
124 IpmiInterfaceMock ipmiMock;
125 BlobHandler blob(&ipmiMock);
126 std::vector<std::uint8_t> request = {
127 0xcf, 0xc2, 0x00, BlobHandler::BlobOEMCommands::bmcBlobStat,
128 0x00, 0x00, 'a', 'b',
129 'c', 'd'};
130
131 /* return blob_state: 0xffff, size: 0x00, metadata 0x3445 */
132 std::vector<std::uint8_t> resp = {0xcf, 0xc2, 0x00, 0x00, 0x00, 0xff,
133 0xff, 0x00, 0x00, 0x00, 0x00, 0x00};
134
135 EXPECT_CALL(ipmiMock, sendPacket(Eq(request))).WillOnce(Return(resp));
136
137 auto meta = blob.getStat("abcd");
138 EXPECT_EQ(meta.blob_state, 0xffff);
139 EXPECT_EQ(meta.size, 0x00);
140 std::vector<std::uint8_t> metadata = {};
141 EXPECT_EQ(metadata, meta.metadata);
142}
Patrick Venture0533d0b2018-12-13 08:48:24 -0800143
144TEST(BlobHandler, openBlobSucceeds)
145{
146 /* The open blob succeeds. */
147 IpmiInterfaceMock ipmiMock;
148 BlobHandler blob(&ipmiMock);
149
150 std::vector<std::uint8_t> request = {
151 0xcf, 0xc2, 0x00, BlobHandler::BlobOEMCommands::bmcBlobOpen,
152 0x00, 0x00, 0x02, 0x04,
153 'a', 'b', 'c', 'd'};
154
155 std::vector<std::uint8_t> resp = {0xcf, 0xc2, 0x00, 0x00, 0x00, 0xfe, 0xed};
156
157 EXPECT_CALL(ipmiMock, sendPacket(Eq(request))).WillOnce(Return(resp));
158
159 auto session =
160 blob.openBlob("abcd", blobs::FirmwareBlobHandler::UpdateFlags::lpc);
161 EXPECT_EQ(0xedfe, session);
162}