blob: c56d5d5f6fc458f1b74c5acb81a33ec88dcb5005 [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"
4
Patrick Venture4eb55952018-11-16 15:36:24 -08005#include <sdbusplus/test/sdbus_mock.hpp>
Patrick Venture72388d72018-11-07 15:06:36 -08006#include <vector>
7
8#include <gmock/gmock.h>
9#include <gtest/gtest.h>
10
11namespace blobs
12{
Patrick Venture21c62c12018-11-09 17:46:58 -080013using ::testing::Eq;
Patrick Venture72388d72018-11-07 15:06:36 -080014using ::testing::Return;
Patrick Venture991910a2018-11-09 19:43:48 -080015using ::testing::StrEq;
Patrick Venture72388d72018-11-07 15:06:36 -080016
17TEST(FirmwareHandlerOpenTest, OpenWithEverythingValid)
18{
19 /* The client passes write set, and a transport that's supported, and a
20 * firmware image blob_id that's supported. */
21
22 ImageHandlerMock imageMock;
23
24 std::vector<HandlerPack> blobs = {
Patrick Venture18235e62018-11-08 10:21:09 -080025 {FirmwareBlobHandler::hashBlobID, &imageMock},
Patrick Venture72388d72018-11-07 15:06:36 -080026 {"asdf", &imageMock},
27 };
28 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -080029 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -080030 };
31
Patrick Venture4eb55952018-11-16 15:36:24 -080032 sdbusplus::SdBusMock sdbus_mock;
33 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
34
35 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
36 std::move(bus_mock), blobs, data);
Patrick Venture72388d72018-11-07 15:06:36 -080037
38 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
39
40 EXPECT_TRUE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -080041 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
Patrick Venture21c62c12018-11-09 17:46:58 -080042
43 /* The active image blob_id was added. */
44 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -080045 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture21c62c12018-11-09 17:46:58 -080046 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
47 FirmwareBlobHandler::activeImageBlobID));
48}
49
50TEST(FirmwareHandlerOpenTest, OpenWithEverythingValidHashFile)
51{
52 /* Open the hash file by blob_id. */
53 ImageHandlerMock imageMock;
54
55 std::vector<HandlerPack> blobs = {
56 {FirmwareBlobHandler::hashBlobID, &imageMock},
57 {"asdf", &imageMock},
58 };
59 std::vector<DataHandlerPack> data = {
60 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
61 };
62
Patrick Venture4eb55952018-11-16 15:36:24 -080063 sdbusplus::SdBusMock sdbus_mock;
64 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
65
66 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
67 std::move(bus_mock), blobs, data);
Patrick Venture21c62c12018-11-09 17:46:58 -080068
69 EXPECT_CALL(imageMock, open(Eq(FirmwareBlobHandler::hashBlobID)))
70 .WillOnce(Return(true));
71
72 EXPECT_TRUE(handler->open(
73 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
74 FirmwareBlobHandler::hashBlobID));
75
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(),
80 FirmwareBlobHandler::activeHashBlobID));
81}
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 = {
92 {FirmwareBlobHandler::hashBlobID, &imageMock},
93 {"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(
104 std::move(bus_mock), blobs, data);
Patrick Venture6e307a72018-11-09 18:21:17 -0800105
106 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
107 EXPECT_CALL(imageMock, open(Eq(FirmwareBlobHandler::hashBlobID)))
108 .WillOnce(Return(true));
109
110 EXPECT_TRUE(handler->open(
111 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc,
112 FirmwareBlobHandler::hashBlobID));
113
114 /* The active hash blob_id was added. */
115 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800116 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture6e307a72018-11-09 18:21:17 -0800117 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
118 FirmwareBlobHandler::activeHashBlobID));
119}
120
121TEST(FirmwareHandlerOpenTest, OpenWithDataHandlerReturnsFailure)
122{
123 /* The data handler call returns failure on open, therefore open fails. */
124 DataHandlerMock dataMock;
125 ImageHandlerMock imageMock;
126
127 std::vector<HandlerPack> blobs = {
128 {FirmwareBlobHandler::hashBlobID, &imageMock},
129 {"asdf", &imageMock},
130 };
131 std::vector<DataHandlerPack> data = {
132 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
133 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
134 };
135
Patrick Venture4eb55952018-11-16 15:36:24 -0800136 sdbusplus::SdBusMock sdbus_mock;
137 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
138
139 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
140 std::move(bus_mock), blobs, data);
Patrick Venture6e307a72018-11-09 18:21:17 -0800141
142 EXPECT_CALL(dataMock, open()).WillOnce(Return(false));
143
144 EXPECT_FALSE(handler->open(
145 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc,
146 FirmwareBlobHandler::hashBlobID));
147
148 /* The active hash blob_id was added. */
149 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800150 EXPECT_EQ(3, currentBlobs.size());
Patrick Venture6e307a72018-11-09 18:21:17 -0800151}
152
Patrick Venture991910a2018-11-09 19:43:48 -0800153TEST(FirmwareHandlerOpenTest, OpenEverythingSucceedsVerifyOpenFileCheck)
154{
155 /* Verify only one file can be open at a time by opening a file, trying
156 * again, then closing, and trying again.
157 */
158 ImageHandlerMock imageMock1, imageMock2;
159
160 std::vector<HandlerPack> blobs = {
161 {FirmwareBlobHandler::hashBlobID, &imageMock1},
162 {"asdf", &imageMock2},
163 };
164 std::vector<DataHandlerPack> data = {
165 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
166 };
167
Patrick Venture4eb55952018-11-16 15:36:24 -0800168 sdbusplus::SdBusMock sdbus_mock;
169 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
170
171 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
172 std::move(bus_mock), blobs, data);
Patrick Venture991910a2018-11-09 19:43:48 -0800173
174 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
175
176 EXPECT_TRUE(handler->open(
177 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
178
179 /* The active image blob_id was added. */
180 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800181 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture991910a2018-11-09 19:43:48 -0800182 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
183 FirmwareBlobHandler::activeImageBlobID));
184
185 /* Open the hash file (since we opened an image file). */
186 EXPECT_FALSE(handler->open(
187 1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
188 FirmwareBlobHandler::hashBlobID));
189
Patrick Venture68bb1432018-11-09 20:08:48 -0800190 EXPECT_TRUE(handler->close(0));
Patrick Venture991910a2018-11-09 19:43:48 -0800191
192 EXPECT_CALL(imageMock1, open(StrEq(FirmwareBlobHandler::hashBlobID)))
193 .WillOnce(Return(true));
194
195 EXPECT_TRUE(handler->open(
196 1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
197 FirmwareBlobHandler::hashBlobID));
198}
199
Patrick Venture21c62c12018-11-09 17:46:58 -0800200TEST(FirmwareHandlerOpenTest, OpenEverythingSucceedsOpenActiveFails)
201{
202 /* Attempting to open the active image blob, when it's present will fail.
203 *
204 * TODO: We'll need another test that closes first because you can only have
205 * one file open at a time.
206 */
207 ImageHandlerMock imageMock;
208
209 std::vector<HandlerPack> blobs = {
210 {FirmwareBlobHandler::hashBlobID, &imageMock},
211 {"asdf", &imageMock},
212 };
213 std::vector<DataHandlerPack> data = {
214 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
215 };
216
Patrick Venture4eb55952018-11-16 15:36:24 -0800217 sdbusplus::SdBusMock sdbus_mock;
218 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
219
220 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
221 std::move(bus_mock), blobs, data);
Patrick Venture21c62c12018-11-09 17:46:58 -0800222
223 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
224
225 EXPECT_TRUE(handler->open(
226 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
227
228 /* The active image blob_id was added. */
229 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800230 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture21c62c12018-11-09 17:46:58 -0800231 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
232 FirmwareBlobHandler::activeImageBlobID));
233
Patrick Venture991910a2018-11-09 19:43:48 -0800234 /* Close only active session, to verify it's failing on attempt to open a
235 * specific blob_id.
236 */
Patrick Venture68bb1432018-11-09 20:08:48 -0800237 EXPECT_TRUE(handler->close(0));
Patrick Venture991910a2018-11-09 19:43:48 -0800238
Patrick Venture21c62c12018-11-09 17:46:58 -0800239 EXPECT_FALSE(handler->open(
240 1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
241 FirmwareBlobHandler::activeImageBlobID));
Patrick Venture72388d72018-11-07 15:06:36 -0800242}
243
244TEST(FirmwareHandlerOpenTest, OpenWithEverythingValidImageHandlerFails)
245{
246 /* The image handler for a specific type of image is allowed to return
247 * failure on open. let's simulate that. */
248
249 ImageHandlerMock imageMock;
250
251 std::vector<HandlerPack> blobs = {
Patrick Venture18235e62018-11-08 10:21:09 -0800252 {FirmwareBlobHandler::hashBlobID, &imageMock},
Patrick Venture72388d72018-11-07 15:06:36 -0800253 {"asdf", &imageMock},
254 };
255 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -0800256 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -0800257 };
258
Patrick Venture4eb55952018-11-16 15:36:24 -0800259 sdbusplus::SdBusMock sdbus_mock;
260 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
261
262 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
263 std::move(bus_mock), blobs, data);
Patrick Venture72388d72018-11-07 15:06:36 -0800264
265 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(false));
266
267 EXPECT_FALSE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -0800268 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
Patrick Venturedb253bf2018-11-09 19:36:03 -0800269
270 /* Verify blob_id list doesn't grow. */
271 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800272 EXPECT_EQ(3, currentBlobs.size());
Patrick Venture72388d72018-11-07 15:06:36 -0800273}
274
275TEST(FirmwareHandlerOpenTest, OpenWithoutWriteFails)
276{
277 /* The client must set the file write bit. */
278
279 ImageHandlerMock imageMock;
280
281 std::vector<HandlerPack> blobs = {
Patrick Venture18235e62018-11-08 10:21:09 -0800282 {FirmwareBlobHandler::hashBlobID, &imageMock},
Patrick Venture72388d72018-11-07 15:06:36 -0800283 {"asdf", &imageMock},
284 };
285 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -0800286 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -0800287 };
288
Patrick Venture4eb55952018-11-16 15:36:24 -0800289 sdbusplus::SdBusMock sdbus_mock;
290 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
291
292 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
293 std::move(bus_mock), blobs, data);
Patrick Venture72388d72018-11-07 15:06:36 -0800294
Patrick Venture05abf7e2018-11-09 11:02:56 -0800295 EXPECT_FALSE(
296 handler->open(0, FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
Patrick Venture72388d72018-11-07 15:06:36 -0800297}
298
299TEST(FirmwareHandlerOpenTest, OpenWithInvalidTransportBit)
300{
301 /* The client sends a request with a transport mechanism not supported. */
302
303 ImageHandlerMock imageMock;
304
305 std::vector<HandlerPack> blobs = {
Patrick Venture18235e62018-11-08 10:21:09 -0800306 {FirmwareBlobHandler::hashBlobID, &imageMock},
Patrick Venture72388d72018-11-07 15:06:36 -0800307 {"asdf", &imageMock},
308 };
309 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -0800310 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -0800311 };
312
Patrick Venture4eb55952018-11-16 15:36:24 -0800313 sdbusplus::SdBusMock sdbus_mock;
314 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
315
316 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
317 std::move(bus_mock), blobs, data);
Patrick Venture72388d72018-11-07 15:06:36 -0800318
Patrick Venture05abf7e2018-11-09 11:02:56 -0800319 EXPECT_FALSE(
320 handler->open(0, FirmwareBlobHandler::UpdateFlags::lpc, "asdf"));
Patrick Venture72388d72018-11-07 15:06:36 -0800321}
322
323TEST(FirmwareHandlerOpenTest, OpenWithInvalidImageBlobId)
324{
325 /* The client sends a request with an invalid image blob_id. */
326
327 ImageHandlerMock imageMock;
328
329 std::vector<HandlerPack> blobs = {
Patrick Venture18235e62018-11-08 10:21:09 -0800330 {FirmwareBlobHandler::hashBlobID, &imageMock},
Patrick Venture72388d72018-11-07 15:06:36 -0800331 {"asdf", &imageMock},
332 };
333 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -0800334 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -0800335 };
336
Patrick Venture4eb55952018-11-16 15:36:24 -0800337 sdbusplus::SdBusMock sdbus_mock;
338 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
339
340 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
341 std::move(bus_mock), blobs, data);
Patrick Venture72388d72018-11-07 15:06:36 -0800342
343 EXPECT_FALSE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -0800344 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "bcdf"));
Patrick Venture72388d72018-11-07 15:06:36 -0800345}
346
347/* TODO: The client sends a request during verification. */
348/* TODO: The client sends a second request to open when there is already an open
349 * file.
350 */
351/* TODO: The client sends a request to open active image. */
352/* TODO: The client sends a request to open active hash. */
353
354} // namespace blobs