blob: 5b6d690fc1bfe5c671204c70ff095ef642f6eea5 [file] [log] [blame]
Patrick Venture72388d72018-11-07 15:06:36 -08001#include "data_mock.hpp"
2#include "firmware_handler.hpp"
3#include "image_mock.hpp"
Patrick Venture7dad86f2019-05-17 08:52:20 -07004#include "util.hpp"
Patrick Venture72388d72018-11-07 15:06:36 -08005
Patrick Venture4eb55952018-11-16 15:36:24 -08006#include <sdbusplus/test/sdbus_mock.hpp>
Patrick Venture72388d72018-11-07 15:06:36 -08007#include <vector>
8
9#include <gmock/gmock.h>
10#include <gtest/gtest.h>
11
12namespace blobs
13{
Patrick Venture21c62c12018-11-09 17:46:58 -080014using ::testing::Eq;
Patrick Venture72388d72018-11-07 15:06:36 -080015using ::testing::Return;
Patrick Venture991910a2018-11-09 19:43:48 -080016using ::testing::StrEq;
Patrick Venture72388d72018-11-07 15:06:36 -080017
18TEST(FirmwareHandlerOpenTest, OpenWithEverythingValid)
19{
20 /* The client passes write set, and a transport that's supported, and a
21 * firmware image blob_id that's supported. */
22
23 ImageHandlerMock imageMock;
24
25 std::vector<HandlerPack> blobs = {
Patrick Venture7dad86f2019-05-17 08:52:20 -070026 {hashBlobId, &imageMock},
Patrick Venture72388d72018-11-07 15:06:36 -080027 {"asdf", &imageMock},
28 };
29 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -080030 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -080031 };
32
Patrick Venture4eb55952018-11-16 15:36:24 -080033 sdbusplus::SdBusMock sdbus_mock;
34 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
35
36 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture74059d62019-05-17 10:40:26 -070037 std::move(bus_mock), blobs, data, "");
Patrick Venture72388d72018-11-07 15:06:36 -080038
39 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
40
41 EXPECT_TRUE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -080042 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
Patrick Venture21c62c12018-11-09 17:46:58 -080043
44 /* The active image blob_id was added. */
45 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -080046 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture21c62c12018-11-09 17:46:58 -080047 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -070048 activeImageBlobId));
Patrick Venture21c62c12018-11-09 17:46:58 -080049}
50
51TEST(FirmwareHandlerOpenTest, OpenWithEverythingValidHashFile)
52{
53 /* Open the hash file by blob_id. */
54 ImageHandlerMock imageMock;
55
56 std::vector<HandlerPack> blobs = {
Patrick Venture7dad86f2019-05-17 08:52:20 -070057 {hashBlobId, &imageMock},
Patrick Venture21c62c12018-11-09 17:46:58 -080058 {"asdf", &imageMock},
59 };
60 std::vector<DataHandlerPack> data = {
61 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
62 };
63
Patrick Venture4eb55952018-11-16 15:36:24 -080064 sdbusplus::SdBusMock sdbus_mock;
65 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
66
67 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture74059d62019-05-17 10:40:26 -070068 std::move(bus_mock), blobs, data, "");
Patrick Venture21c62c12018-11-09 17:46:58 -080069
Patrick Venture7dad86f2019-05-17 08:52:20 -070070 EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
Patrick Venture21c62c12018-11-09 17:46:58 -080071
72 EXPECT_TRUE(handler->open(
73 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -070074 hashBlobId));
Patrick Venture21c62c12018-11-09 17:46:58 -080075
76 /* The active hash blob_id was added. */
77 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -080078 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture21c62c12018-11-09 17:46:58 -080079 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -070080 activeHashBlobId));
Patrick Venture21c62c12018-11-09 17:46:58 -080081}
82
Patrick Venture6e307a72018-11-09 18:21:17 -080083TEST(FirmwareHandlerOpenTest, OpenWithDataHandlerAllSucceeds)
84{
85 /* Attempting to open a file that has an active handler, and use that active
86 * handler method.
87 */
88 DataHandlerMock dataMock;
89 ImageHandlerMock imageMock;
90
91 std::vector<HandlerPack> blobs = {
Patrick Venture7dad86f2019-05-17 08:52:20 -070092 {hashBlobId, &imageMock},
Patrick Venture6e307a72018-11-09 18:21:17 -080093 {"asdf", &imageMock},
94 };
95 std::vector<DataHandlerPack> data = {
96 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
97 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
98 };
99
Patrick Venture4eb55952018-11-16 15:36:24 -0800100 sdbusplus::SdBusMock sdbus_mock;
101 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
102
103 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture74059d62019-05-17 10:40:26 -0700104 std::move(bus_mock), blobs, data, "");
Patrick Venture6e307a72018-11-09 18:21:17 -0800105
106 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
Patrick Venture7dad86f2019-05-17 08:52:20 -0700107 EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
Patrick Venture6e307a72018-11-09 18:21:17 -0800108
109 EXPECT_TRUE(handler->open(
110 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc,
Patrick Venture7dad86f2019-05-17 08:52:20 -0700111 hashBlobId));
Patrick Venture6e307a72018-11-09 18:21:17 -0800112
113 /* The active hash blob_id was added. */
114 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800115 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture6e307a72018-11-09 18:21:17 -0800116 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -0700117 activeHashBlobId));
Patrick Venture6e307a72018-11-09 18:21:17 -0800118}
119
120TEST(FirmwareHandlerOpenTest, OpenWithDataHandlerReturnsFailure)
121{
122 /* The data handler call returns failure on open, therefore open fails. */
123 DataHandlerMock dataMock;
124 ImageHandlerMock imageMock;
125
126 std::vector<HandlerPack> blobs = {
Patrick Venture7dad86f2019-05-17 08:52:20 -0700127 {hashBlobId, &imageMock},
Patrick Venture6e307a72018-11-09 18:21:17 -0800128 {"asdf", &imageMock},
129 };
130 std::vector<DataHandlerPack> data = {
131 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
132 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
133 };
134
Patrick Venture4eb55952018-11-16 15:36:24 -0800135 sdbusplus::SdBusMock sdbus_mock;
136 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
137
138 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture74059d62019-05-17 10:40:26 -0700139 std::move(bus_mock), blobs, data, "");
Patrick Venture6e307a72018-11-09 18:21:17 -0800140
141 EXPECT_CALL(dataMock, open()).WillOnce(Return(false));
142
143 EXPECT_FALSE(handler->open(
144 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc,
Patrick Venture7dad86f2019-05-17 08:52:20 -0700145 hashBlobId));
Patrick Venture6e307a72018-11-09 18:21:17 -0800146
147 /* The active hash blob_id was added. */
148 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800149 EXPECT_EQ(3, currentBlobs.size());
Patrick Venture6e307a72018-11-09 18:21:17 -0800150}
151
Patrick Venture991910a2018-11-09 19:43:48 -0800152TEST(FirmwareHandlerOpenTest, OpenEverythingSucceedsVerifyOpenFileCheck)
153{
154 /* Verify only one file can be open at a time by opening a file, trying
155 * again, then closing, and trying again.
156 */
157 ImageHandlerMock imageMock1, imageMock2;
158
159 std::vector<HandlerPack> blobs = {
Patrick Venture7dad86f2019-05-17 08:52:20 -0700160 {hashBlobId, &imageMock1},
Patrick Venture991910a2018-11-09 19:43:48 -0800161 {"asdf", &imageMock2},
162 };
163 std::vector<DataHandlerPack> data = {
164 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
165 };
166
Patrick Venture4eb55952018-11-16 15:36:24 -0800167 sdbusplus::SdBusMock sdbus_mock;
168 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
169
170 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture74059d62019-05-17 10:40:26 -0700171 std::move(bus_mock), blobs, data, "");
Patrick Venture991910a2018-11-09 19:43:48 -0800172
173 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
174
175 EXPECT_TRUE(handler->open(
176 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
177
178 /* The active image blob_id was added. */
179 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800180 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture991910a2018-11-09 19:43:48 -0800181 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -0700182 activeImageBlobId));
Patrick Venture991910a2018-11-09 19:43:48 -0800183
184 /* Open the hash file (since we opened an image file). */
185 EXPECT_FALSE(handler->open(
186 1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -0700187 hashBlobId));
Patrick Venture991910a2018-11-09 19:43:48 -0800188
Patrick Venture68bb1432018-11-09 20:08:48 -0800189 EXPECT_TRUE(handler->close(0));
Patrick Venture991910a2018-11-09 19:43:48 -0800190
Patrick Venture7dad86f2019-05-17 08:52:20 -0700191 EXPECT_CALL(imageMock1, open(StrEq(hashBlobId))).WillOnce(Return(true));
Patrick Venture991910a2018-11-09 19:43:48 -0800192
193 EXPECT_TRUE(handler->open(
194 1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -0700195 hashBlobId));
Patrick Venture991910a2018-11-09 19:43:48 -0800196}
197
Patrick Venture21c62c12018-11-09 17:46:58 -0800198TEST(FirmwareHandlerOpenTest, OpenEverythingSucceedsOpenActiveFails)
199{
200 /* Attempting to open the active image blob, when it's present will fail.
201 *
202 * TODO: We'll need another test that closes first because you can only have
203 * one file open at a time.
204 */
205 ImageHandlerMock imageMock;
206
207 std::vector<HandlerPack> blobs = {
Patrick Venture7dad86f2019-05-17 08:52:20 -0700208 {hashBlobId, &imageMock},
Patrick Venture21c62c12018-11-09 17:46:58 -0800209 {"asdf", &imageMock},
210 };
211 std::vector<DataHandlerPack> data = {
212 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
213 };
214
Patrick Venture4eb55952018-11-16 15:36:24 -0800215 sdbusplus::SdBusMock sdbus_mock;
216 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
217
218 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture74059d62019-05-17 10:40:26 -0700219 std::move(bus_mock), blobs, data, "");
Patrick Venture21c62c12018-11-09 17:46:58 -0800220
221 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
222
223 EXPECT_TRUE(handler->open(
224 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
225
226 /* The active image blob_id was added. */
227 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800228 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture21c62c12018-11-09 17:46:58 -0800229 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -0700230 activeImageBlobId));
Patrick Venture21c62c12018-11-09 17:46:58 -0800231
Patrick Venture991910a2018-11-09 19:43:48 -0800232 /* Close only active session, to verify it's failing on attempt to open a
233 * specific blob_id.
234 */
Patrick Venture68bb1432018-11-09 20:08:48 -0800235 EXPECT_TRUE(handler->close(0));
Patrick Venture991910a2018-11-09 19:43:48 -0800236
Patrick Venture21c62c12018-11-09 17:46:58 -0800237 EXPECT_FALSE(handler->open(
238 1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -0700239 activeImageBlobId));
Patrick Venture72388d72018-11-07 15:06:36 -0800240}
241
242TEST(FirmwareHandlerOpenTest, OpenWithEverythingValidImageHandlerFails)
243{
244 /* The image handler for a specific type of image is allowed to return
245 * failure on open. let's simulate that. */
246
247 ImageHandlerMock imageMock;
248
249 std::vector<HandlerPack> blobs = {
Patrick Venture7dad86f2019-05-17 08:52:20 -0700250 {hashBlobId, &imageMock},
Patrick Venture72388d72018-11-07 15:06:36 -0800251 {"asdf", &imageMock},
252 };
253 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -0800254 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -0800255 };
256
Patrick Venture4eb55952018-11-16 15:36:24 -0800257 sdbusplus::SdBusMock sdbus_mock;
258 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
259
260 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture74059d62019-05-17 10:40:26 -0700261 std::move(bus_mock), blobs, data, "");
Patrick Venture72388d72018-11-07 15:06:36 -0800262
263 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(false));
264
265 EXPECT_FALSE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -0800266 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
Patrick Venturedb253bf2018-11-09 19:36:03 -0800267
268 /* Verify blob_id list doesn't grow. */
269 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800270 EXPECT_EQ(3, currentBlobs.size());
Patrick Venture72388d72018-11-07 15:06:36 -0800271}
272
273TEST(FirmwareHandlerOpenTest, OpenWithoutWriteFails)
274{
275 /* The client must set the file write bit. */
276
277 ImageHandlerMock imageMock;
278
279 std::vector<HandlerPack> blobs = {
Patrick Venture7dad86f2019-05-17 08:52:20 -0700280 {hashBlobId, &imageMock},
Patrick Venture72388d72018-11-07 15:06:36 -0800281 {"asdf", &imageMock},
282 };
283 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -0800284 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -0800285 };
286
Patrick Venture4eb55952018-11-16 15:36:24 -0800287 sdbusplus::SdBusMock sdbus_mock;
288 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
289
290 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture74059d62019-05-17 10:40:26 -0700291 std::move(bus_mock), blobs, data, "");
Patrick Venture72388d72018-11-07 15:06:36 -0800292
Patrick Venture05abf7e2018-11-09 11:02:56 -0800293 EXPECT_FALSE(
294 handler->open(0, FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
Patrick Venture72388d72018-11-07 15:06:36 -0800295}
296
297TEST(FirmwareHandlerOpenTest, OpenWithInvalidTransportBit)
298{
299 /* The client sends a request with a transport mechanism not supported. */
300
301 ImageHandlerMock imageMock;
302
303 std::vector<HandlerPack> blobs = {
Patrick Venture7dad86f2019-05-17 08:52:20 -0700304 {hashBlobId, &imageMock},
Patrick Venture72388d72018-11-07 15:06:36 -0800305 {"asdf", &imageMock},
306 };
307 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -0800308 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -0800309 };
310
Patrick Venture4eb55952018-11-16 15:36:24 -0800311 sdbusplus::SdBusMock sdbus_mock;
312 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
313
314 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture74059d62019-05-17 10:40:26 -0700315 std::move(bus_mock), blobs, data, "");
Patrick Venture72388d72018-11-07 15:06:36 -0800316
Patrick Venture05abf7e2018-11-09 11:02:56 -0800317 EXPECT_FALSE(
318 handler->open(0, FirmwareBlobHandler::UpdateFlags::lpc, "asdf"));
Patrick Venture72388d72018-11-07 15:06:36 -0800319}
320
321TEST(FirmwareHandlerOpenTest, OpenWithInvalidImageBlobId)
322{
323 /* The client sends a request with an invalid image blob_id. */
324
325 ImageHandlerMock imageMock;
326
327 std::vector<HandlerPack> blobs = {
Patrick Venture7dad86f2019-05-17 08:52:20 -0700328 {hashBlobId, &imageMock},
Patrick Venture72388d72018-11-07 15:06:36 -0800329 {"asdf", &imageMock},
330 };
331 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -0800332 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -0800333 };
334
Patrick Venture4eb55952018-11-16 15:36:24 -0800335 sdbusplus::SdBusMock sdbus_mock;
336 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
337
338 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture74059d62019-05-17 10:40:26 -0700339 std::move(bus_mock), blobs, data, "");
Patrick Venture72388d72018-11-07 15:06:36 -0800340
341 EXPECT_FALSE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -0800342 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "bcdf"));
Patrick Venture72388d72018-11-07 15:06:36 -0800343}
344
345/* TODO: The client sends a request during verification. */
346/* TODO: The client sends a second request to open when there is already an open
347 * file.
348 */
349/* TODO: The client sends a request to open active image. */
350/* TODO: The client sends a request to open active hash. */
351
352} // namespace blobs