blob: 5148a441abfe6a0b36ca804274a7f42334491a6d [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 Venture3ecb3502019-05-17 11:03:51 -07005#include "verification_mock.hpp"
Patrick Venture72388d72018-11-07 15:06:36 -08006
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 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -070034 blobs, data, CreateVerifyMock());
Patrick Venture72388d72018-11-07 15:06:36 -080035
36 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
37
38 EXPECT_TRUE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -080039 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
Patrick Venture21c62c12018-11-09 17:46:58 -080040
41 /* The active image blob_id was added. */
42 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -080043 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture21c62c12018-11-09 17:46:58 -080044 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -070045 activeImageBlobId));
Patrick Venture21c62c12018-11-09 17:46:58 -080046}
47
48TEST(FirmwareHandlerOpenTest, OpenWithEverythingValidHashFile)
49{
50 /* Open the hash file by blob_id. */
51 ImageHandlerMock imageMock;
52
53 std::vector<HandlerPack> blobs = {
Patrick Venture7dad86f2019-05-17 08:52:20 -070054 {hashBlobId, &imageMock},
Patrick Venture21c62c12018-11-09 17:46:58 -080055 {"asdf", &imageMock},
56 };
57 std::vector<DataHandlerPack> data = {
58 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
59 };
60
Patrick Venture4eb55952018-11-16 15:36:24 -080061 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -070062 blobs, data, CreateVerifyMock());
Patrick Venture21c62c12018-11-09 17:46:58 -080063
Patrick Venture7dad86f2019-05-17 08:52:20 -070064 EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
Patrick Venture21c62c12018-11-09 17:46:58 -080065
66 EXPECT_TRUE(handler->open(
67 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -070068 hashBlobId));
Patrick Venture21c62c12018-11-09 17:46:58 -080069
70 /* The active hash blob_id was added. */
71 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -080072 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture21c62c12018-11-09 17:46:58 -080073 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -070074 activeHashBlobId));
Patrick Venture21c62c12018-11-09 17:46:58 -080075}
76
Patrick Venture6e307a72018-11-09 18:21:17 -080077TEST(FirmwareHandlerOpenTest, OpenWithDataHandlerAllSucceeds)
78{
79 /* Attempting to open a file that has an active handler, and use that active
80 * handler method.
81 */
82 DataHandlerMock dataMock;
83 ImageHandlerMock imageMock;
84
85 std::vector<HandlerPack> blobs = {
Patrick Venture7dad86f2019-05-17 08:52:20 -070086 {hashBlobId, &imageMock},
Patrick Venture6e307a72018-11-09 18:21:17 -080087 {"asdf", &imageMock},
88 };
89 std::vector<DataHandlerPack> data = {
90 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
91 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
92 };
93
Patrick Venture4eb55952018-11-16 15:36:24 -080094 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -070095 blobs, data, CreateVerifyMock());
Patrick Venture6e307a72018-11-09 18:21:17 -080096
97 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
Patrick Venture7dad86f2019-05-17 08:52:20 -070098 EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
Patrick Venture6e307a72018-11-09 18:21:17 -080099
100 EXPECT_TRUE(handler->open(
101 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc,
Patrick Venture7dad86f2019-05-17 08:52:20 -0700102 hashBlobId));
Patrick Venture6e307a72018-11-09 18:21:17 -0800103
104 /* The active hash blob_id was added. */
105 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800106 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture6e307a72018-11-09 18:21:17 -0800107 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -0700108 activeHashBlobId));
Patrick Venture6e307a72018-11-09 18:21:17 -0800109}
110
111TEST(FirmwareHandlerOpenTest, OpenWithDataHandlerReturnsFailure)
112{
113 /* The data handler call returns failure on open, therefore open fails. */
114 DataHandlerMock dataMock;
115 ImageHandlerMock imageMock;
116
117 std::vector<HandlerPack> blobs = {
Patrick Venture7dad86f2019-05-17 08:52:20 -0700118 {hashBlobId, &imageMock},
Patrick Venture6e307a72018-11-09 18:21:17 -0800119 {"asdf", &imageMock},
120 };
121 std::vector<DataHandlerPack> data = {
122 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
123 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
124 };
125
Patrick Venture4eb55952018-11-16 15:36:24 -0800126 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -0700127 blobs, data, CreateVerifyMock());
Patrick Venture6e307a72018-11-09 18:21:17 -0800128
129 EXPECT_CALL(dataMock, open()).WillOnce(Return(false));
130
131 EXPECT_FALSE(handler->open(
132 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc,
Patrick Venture7dad86f2019-05-17 08:52:20 -0700133 hashBlobId));
Patrick Venture6e307a72018-11-09 18:21:17 -0800134
135 /* The active hash blob_id was added. */
136 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800137 EXPECT_EQ(3, currentBlobs.size());
Patrick Venture6e307a72018-11-09 18:21:17 -0800138}
139
Patrick Venture991910a2018-11-09 19:43:48 -0800140TEST(FirmwareHandlerOpenTest, OpenEverythingSucceedsVerifyOpenFileCheck)
141{
142 /* Verify only one file can be open at a time by opening a file, trying
143 * again, then closing, and trying again.
144 */
145 ImageHandlerMock imageMock1, imageMock2;
146
147 std::vector<HandlerPack> blobs = {
Patrick Venture7dad86f2019-05-17 08:52:20 -0700148 {hashBlobId, &imageMock1},
Patrick Venture991910a2018-11-09 19:43:48 -0800149 {"asdf", &imageMock2},
150 };
151 std::vector<DataHandlerPack> data = {
152 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
153 };
154
Patrick Venture4eb55952018-11-16 15:36:24 -0800155 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -0700156 blobs, data, CreateVerifyMock());
Patrick Venture991910a2018-11-09 19:43:48 -0800157
158 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
159
160 EXPECT_TRUE(handler->open(
161 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
162
163 /* The active image blob_id was added. */
164 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800165 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture991910a2018-11-09 19:43:48 -0800166 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -0700167 activeImageBlobId));
Patrick Venture991910a2018-11-09 19:43:48 -0800168
169 /* Open the hash file (since we opened an image file). */
170 EXPECT_FALSE(handler->open(
171 1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -0700172 hashBlobId));
Patrick Venture991910a2018-11-09 19:43:48 -0800173
Patrick Venture68bb1432018-11-09 20:08:48 -0800174 EXPECT_TRUE(handler->close(0));
Patrick Venture991910a2018-11-09 19:43:48 -0800175
Patrick Venture7dad86f2019-05-17 08:52:20 -0700176 EXPECT_CALL(imageMock1, open(StrEq(hashBlobId))).WillOnce(Return(true));
Patrick Venture991910a2018-11-09 19:43:48 -0800177
178 EXPECT_TRUE(handler->open(
179 1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -0700180 hashBlobId));
Patrick Venture991910a2018-11-09 19:43:48 -0800181}
182
Patrick Venture21c62c12018-11-09 17:46:58 -0800183TEST(FirmwareHandlerOpenTest, OpenEverythingSucceedsOpenActiveFails)
184{
185 /* Attempting to open the active image blob, when it's present will fail.
186 *
187 * TODO: We'll need another test that closes first because you can only have
188 * one file open at a time.
189 */
190 ImageHandlerMock imageMock;
191
192 std::vector<HandlerPack> blobs = {
Patrick Venture7dad86f2019-05-17 08:52:20 -0700193 {hashBlobId, &imageMock},
Patrick Venture21c62c12018-11-09 17:46:58 -0800194 {"asdf", &imageMock},
195 };
196 std::vector<DataHandlerPack> data = {
197 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
198 };
199
Patrick Venture4eb55952018-11-16 15:36:24 -0800200 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -0700201 blobs, data, CreateVerifyMock());
Patrick Venture21c62c12018-11-09 17:46:58 -0800202
203 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
204
205 EXPECT_TRUE(handler->open(
206 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
207
208 /* The active image blob_id was added. */
209 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800210 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture21c62c12018-11-09 17:46:58 -0800211 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -0700212 activeImageBlobId));
Patrick Venture21c62c12018-11-09 17:46:58 -0800213
Patrick Venture991910a2018-11-09 19:43:48 -0800214 /* Close only active session, to verify it's failing on attempt to open a
215 * specific blob_id.
216 */
Patrick Venture68bb1432018-11-09 20:08:48 -0800217 EXPECT_TRUE(handler->close(0));
Patrick Venture991910a2018-11-09 19:43:48 -0800218
Patrick Venture21c62c12018-11-09 17:46:58 -0800219 EXPECT_FALSE(handler->open(
220 1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -0700221 activeImageBlobId));
Patrick Venture72388d72018-11-07 15:06:36 -0800222}
223
224TEST(FirmwareHandlerOpenTest, OpenWithEverythingValidImageHandlerFails)
225{
226 /* The image handler for a specific type of image is allowed to return
227 * failure on open. let's simulate that. */
228
229 ImageHandlerMock imageMock;
230
231 std::vector<HandlerPack> blobs = {
Patrick Venture7dad86f2019-05-17 08:52:20 -0700232 {hashBlobId, &imageMock},
Patrick Venture72388d72018-11-07 15:06:36 -0800233 {"asdf", &imageMock},
234 };
235 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -0800236 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -0800237 };
238
Patrick Venture4eb55952018-11-16 15:36:24 -0800239 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -0700240 blobs, data, CreateVerifyMock());
Patrick Venture72388d72018-11-07 15:06:36 -0800241
242 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(false));
243
244 EXPECT_FALSE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -0800245 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
Patrick Venturedb253bf2018-11-09 19:36:03 -0800246
247 /* Verify blob_id list doesn't grow. */
248 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -0800249 EXPECT_EQ(3, currentBlobs.size());
Patrick Venture72388d72018-11-07 15:06:36 -0800250}
251
252TEST(FirmwareHandlerOpenTest, OpenWithoutWriteFails)
253{
254 /* The client must set the file write bit. */
255
256 ImageHandlerMock imageMock;
257
258 std::vector<HandlerPack> blobs = {
Patrick Venture7dad86f2019-05-17 08:52:20 -0700259 {hashBlobId, &imageMock},
Patrick Venture72388d72018-11-07 15:06:36 -0800260 {"asdf", &imageMock},
261 };
262 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -0800263 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -0800264 };
265
Patrick Venture4eb55952018-11-16 15:36:24 -0800266 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -0700267 blobs, data, CreateVerifyMock());
Patrick Venture72388d72018-11-07 15:06:36 -0800268
Patrick Venture05abf7e2018-11-09 11:02:56 -0800269 EXPECT_FALSE(
270 handler->open(0, FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
Patrick Venture72388d72018-11-07 15:06:36 -0800271}
272
273TEST(FirmwareHandlerOpenTest, OpenWithInvalidTransportBit)
274{
275 /* The client sends a request with a transport mechanism not supported. */
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 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -0700288 blobs, data, CreateVerifyMock());
Patrick Venture72388d72018-11-07 15:06:36 -0800289
Patrick Venture05abf7e2018-11-09 11:02:56 -0800290 EXPECT_FALSE(
291 handler->open(0, FirmwareBlobHandler::UpdateFlags::lpc, "asdf"));
Patrick Venture72388d72018-11-07 15:06:36 -0800292}
293
294TEST(FirmwareHandlerOpenTest, OpenWithInvalidImageBlobId)
295{
296 /* The client sends a request with an invalid image blob_id. */
297
298 ImageHandlerMock imageMock;
299
300 std::vector<HandlerPack> blobs = {
Patrick Venture7dad86f2019-05-17 08:52:20 -0700301 {hashBlobId, &imageMock},
Patrick Venture72388d72018-11-07 15:06:36 -0800302 {"asdf", &imageMock},
303 };
304 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -0800305 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -0800306 };
307
Patrick Venture4eb55952018-11-16 15:36:24 -0800308 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -0700309 blobs, data, CreateVerifyMock());
Patrick Venture72388d72018-11-07 15:06:36 -0800310
311 EXPECT_FALSE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -0800312 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "bcdf"));
Patrick Venture72388d72018-11-07 15:06:36 -0800313}
314
315/* TODO: The client sends a request during verification. */
316/* TODO: The client sends a second request to open when there is already an open
317 * file.
318 */
319/* TODO: The client sends a request to open active image. */
320/* TODO: The client sends a request to open active hash. */
321
322} // namespace blobs