blob: cbbb15189bd31d900c750d6620f0991fbe489306 [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,
Patrick Venture86ef75d2018-12-19 15:59:03 -080044 'a', 'b', 'c', 'd', 0x00};
Patrick Venturec79faa12018-12-12 13:12:21 -080045
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,
Patrick Venture86ef75d2018-12-19 15:59:03 -080089 'a', 'b', 'c', 'd', 0x00};
Patrick Venturec79faa12018-12-12 13:12:21 -080090
91 EXPECT_CALL(ipmiMock, sendPacket(Eq(request2))).WillOnce(Return(resp2));
92
Patrick Venture86ef75d2018-12-19 15:59:03 -080093 /* A std::string is not nul-terminated by default. */
94 std::vector<std::string> expectedList = {std::string{"abcd"}};
Patrick Venturec79faa12018-12-12 13:12:21 -080095
96 EXPECT_EQ(expectedList, blob.getBlobList());
97}
Patrick Venture0bf8bf02018-12-12 20:43:25 -080098
99TEST(BlobHandler, getStatWithMetadata)
100{
101 /* Stat received metadata. */
102 IpmiInterfaceMock ipmiMock;
103 BlobHandler blob(&ipmiMock);
104 std::vector<std::uint8_t> request = {
105 0xcf, 0xc2, 0x00, BlobHandler::BlobOEMCommands::bmcBlobStat,
106 0x00, 0x00, 'a', 'b',
Patrick Venture0d88a122018-12-17 07:52:04 -0800107 'c', 'd', 0x00};
Patrick Venture0bf8bf02018-12-12 20:43:25 -0800108
109 /* return blob_state: 0xffff, size: 0x00, metadata 0x3445 */
110 std::vector<std::uint8_t> resp = {0xcf, 0xc2, 0x00, 0x00, 0x00, 0xff, 0xff,
111 0x00, 0x00, 0x00, 0x00, 0x02, 0x34, 0x45};
112
113 EXPECT_CALL(ipmiMock, sendPacket(Eq(request))).WillOnce(Return(resp));
114
115 auto meta = blob.getStat("abcd");
116 EXPECT_EQ(meta.blob_state, 0xffff);
117 EXPECT_EQ(meta.size, 0x00);
118 std::vector<std::uint8_t> metadata = {0x34, 0x45};
119 EXPECT_EQ(metadata, meta.metadata);
120}
121
122TEST(BlobHandler, getStatNoMetadata)
123{
124 /* Stat received no metadata. */
125 IpmiInterfaceMock ipmiMock;
126 BlobHandler blob(&ipmiMock);
127 std::vector<std::uint8_t> request = {
128 0xcf, 0xc2, 0x00, BlobHandler::BlobOEMCommands::bmcBlobStat,
129 0x00, 0x00, 'a', 'b',
Patrick Venture0d88a122018-12-17 07:52:04 -0800130 'c', 'd', 0x00};
Patrick Venture0bf8bf02018-12-12 20:43:25 -0800131
132 /* return blob_state: 0xffff, size: 0x00, metadata 0x3445 */
133 std::vector<std::uint8_t> resp = {0xcf, 0xc2, 0x00, 0x00, 0x00, 0xff,
134 0xff, 0x00, 0x00, 0x00, 0x00, 0x00};
135
136 EXPECT_CALL(ipmiMock, sendPacket(Eq(request))).WillOnce(Return(resp));
137
138 auto meta = blob.getStat("abcd");
139 EXPECT_EQ(meta.blob_state, 0xffff);
140 EXPECT_EQ(meta.size, 0x00);
141 std::vector<std::uint8_t> metadata = {};
142 EXPECT_EQ(metadata, meta.metadata);
143}
Patrick Venture0533d0b2018-12-13 08:48:24 -0800144
145TEST(BlobHandler, openBlobSucceeds)
146{
147 /* The open blob succeeds. */
148 IpmiInterfaceMock ipmiMock;
149 BlobHandler blob(&ipmiMock);
150
151 std::vector<std::uint8_t> request = {
152 0xcf, 0xc2, 0x00, BlobHandler::BlobOEMCommands::bmcBlobOpen,
153 0x00, 0x00, 0x02, 0x04,
Patrick Venture0d88a122018-12-17 07:52:04 -0800154 'a', 'b', 'c', 'd',
155 0x00};
Patrick Venture0533d0b2018-12-13 08:48:24 -0800156
157 std::vector<std::uint8_t> resp = {0xcf, 0xc2, 0x00, 0x00, 0x00, 0xfe, 0xed};
158
159 EXPECT_CALL(ipmiMock, sendPacket(Eq(request))).WillOnce(Return(resp));
160
161 auto session =
162 blob.openBlob("abcd", blobs::FirmwareBlobHandler::UpdateFlags::lpc);
163 EXPECT_EQ(0xedfe, session);
164}