blob: 197574068fee75728a3d16a0575cab5f7315d0fb [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 Venture72388d72018-11-07 15:06:36 -08005#include <vector>
6
7#include <gmock/gmock.h>
8#include <gtest/gtest.h>
9
10namespace blobs
11{
Patrick Venture21c62c12018-11-09 17:46:58 -080012using ::testing::Eq;
Patrick Venture72388d72018-11-07 15:06:36 -080013using ::testing::Return;
Patrick Venture991910a2018-11-09 19:43:48 -080014using ::testing::StrEq;
Patrick Venture72388d72018-11-07 15:06:36 -080015
16TEST(FirmwareHandlerOpenTest, OpenWithEverythingValid)
17{
18 /* The client passes write set, and a transport that's supported, and a
19 * firmware image blob_id that's supported. */
20
21 ImageHandlerMock imageMock;
22
23 std::vector<HandlerPack> blobs = {
Patrick Venture18235e62018-11-08 10:21:09 -080024 {FirmwareBlobHandler::hashBlobID, &imageMock},
Patrick Venture72388d72018-11-07 15:06:36 -080025 {"asdf", &imageMock},
26 };
27 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -080028 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -080029 };
30
31 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
32
33 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
34
35 EXPECT_TRUE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -080036 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
Patrick Venture21c62c12018-11-09 17:46:58 -080037
38 /* The active image blob_id was added. */
39 auto currentBlobs = handler->getBlobIds();
40 EXPECT_EQ(3, currentBlobs.size());
41 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
42 FirmwareBlobHandler::activeImageBlobID));
43}
44
45TEST(FirmwareHandlerOpenTest, OpenWithEverythingValidHashFile)
46{
47 /* Open the hash file by blob_id. */
48 ImageHandlerMock imageMock;
49
50 std::vector<HandlerPack> blobs = {
51 {FirmwareBlobHandler::hashBlobID, &imageMock},
52 {"asdf", &imageMock},
53 };
54 std::vector<DataHandlerPack> data = {
55 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
56 };
57
58 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
59
60 EXPECT_CALL(imageMock, open(Eq(FirmwareBlobHandler::hashBlobID)))
61 .WillOnce(Return(true));
62
63 EXPECT_TRUE(handler->open(
64 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
65 FirmwareBlobHandler::hashBlobID));
66
67 /* The active hash blob_id was added. */
68 auto currentBlobs = handler->getBlobIds();
69 EXPECT_EQ(3, currentBlobs.size());
70 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
71 FirmwareBlobHandler::activeHashBlobID));
72}
73
Patrick Venture6e307a72018-11-09 18:21:17 -080074TEST(FirmwareHandlerOpenTest, OpenWithDataHandlerAllSucceeds)
75{
76 /* Attempting to open a file that has an active handler, and use that active
77 * handler method.
78 */
79 DataHandlerMock dataMock;
80 ImageHandlerMock imageMock;
81
82 std::vector<HandlerPack> blobs = {
83 {FirmwareBlobHandler::hashBlobID, &imageMock},
84 {"asdf", &imageMock},
85 };
86 std::vector<DataHandlerPack> data = {
87 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
88 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
89 };
90
91 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
92
93 EXPECT_CALL(dataMock, open()).WillOnce(Return(true));
94 EXPECT_CALL(imageMock, open(Eq(FirmwareBlobHandler::hashBlobID)))
95 .WillOnce(Return(true));
96
97 EXPECT_TRUE(handler->open(
98 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc,
99 FirmwareBlobHandler::hashBlobID));
100
101 /* The active hash blob_id was added. */
102 auto currentBlobs = handler->getBlobIds();
103 EXPECT_EQ(3, currentBlobs.size());
104 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
105 FirmwareBlobHandler::activeHashBlobID));
106}
107
108TEST(FirmwareHandlerOpenTest, OpenWithDataHandlerReturnsFailure)
109{
110 /* The data handler call returns failure on open, therefore open fails. */
111 DataHandlerMock dataMock;
112 ImageHandlerMock imageMock;
113
114 std::vector<HandlerPack> blobs = {
115 {FirmwareBlobHandler::hashBlobID, &imageMock},
116 {"asdf", &imageMock},
117 };
118 std::vector<DataHandlerPack> data = {
119 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
120 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
121 };
122
123 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
124
125 EXPECT_CALL(dataMock, open()).WillOnce(Return(false));
126
127 EXPECT_FALSE(handler->open(
128 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::lpc,
129 FirmwareBlobHandler::hashBlobID));
130
131 /* The active hash blob_id was added. */
132 auto currentBlobs = handler->getBlobIds();
133 EXPECT_EQ(2, currentBlobs.size());
134}
135
Patrick Venture991910a2018-11-09 19:43:48 -0800136TEST(FirmwareHandlerOpenTest, OpenEverythingSucceedsVerifyOpenFileCheck)
137{
138 /* Verify only one file can be open at a time by opening a file, trying
139 * again, then closing, and trying again.
140 */
141 ImageHandlerMock imageMock1, imageMock2;
142
143 std::vector<HandlerPack> blobs = {
144 {FirmwareBlobHandler::hashBlobID, &imageMock1},
145 {"asdf", &imageMock2},
146 };
147 std::vector<DataHandlerPack> data = {
148 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
149 };
150
151 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
152
153 EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true));
154
155 EXPECT_TRUE(handler->open(
156 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
157
158 /* The active image blob_id was added. */
159 auto currentBlobs = handler->getBlobIds();
160 EXPECT_EQ(3, currentBlobs.size());
161 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
162 FirmwareBlobHandler::activeImageBlobID));
163
164 /* Open the hash file (since we opened an image file). */
165 EXPECT_FALSE(handler->open(
166 1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
167 FirmwareBlobHandler::hashBlobID));
168
Patrick Venture68bb1432018-11-09 20:08:48 -0800169 EXPECT_TRUE(handler->close(0));
Patrick Venture991910a2018-11-09 19:43:48 -0800170
171 EXPECT_CALL(imageMock1, open(StrEq(FirmwareBlobHandler::hashBlobID)))
172 .WillOnce(Return(true));
173
174 EXPECT_TRUE(handler->open(
175 1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
176 FirmwareBlobHandler::hashBlobID));
177}
178
Patrick Venture21c62c12018-11-09 17:46:58 -0800179TEST(FirmwareHandlerOpenTest, OpenEverythingSucceedsOpenActiveFails)
180{
181 /* Attempting to open the active image blob, when it's present will fail.
182 *
183 * TODO: We'll need another test that closes first because you can only have
184 * one file open at a time.
185 */
186 ImageHandlerMock imageMock;
187
188 std::vector<HandlerPack> blobs = {
189 {FirmwareBlobHandler::hashBlobID, &imageMock},
190 {"asdf", &imageMock},
191 };
192 std::vector<DataHandlerPack> data = {
193 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
194 };
195
196 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
197
198 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(true));
199
200 EXPECT_TRUE(handler->open(
201 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
202
203 /* The active image blob_id was added. */
204 auto currentBlobs = handler->getBlobIds();
205 EXPECT_EQ(3, currentBlobs.size());
206 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
207 FirmwareBlobHandler::activeImageBlobID));
208
Patrick Venture991910a2018-11-09 19:43:48 -0800209 /* Close only active session, to verify it's failing on attempt to open a
210 * specific blob_id.
211 */
Patrick Venture68bb1432018-11-09 20:08:48 -0800212 EXPECT_TRUE(handler->close(0));
Patrick Venture991910a2018-11-09 19:43:48 -0800213
Patrick Venture21c62c12018-11-09 17:46:58 -0800214 EXPECT_FALSE(handler->open(
215 1, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
216 FirmwareBlobHandler::activeImageBlobID));
Patrick Venture72388d72018-11-07 15:06:36 -0800217}
218
219TEST(FirmwareHandlerOpenTest, OpenWithEverythingValidImageHandlerFails)
220{
221 /* The image handler for a specific type of image is allowed to return
222 * failure on open. let's simulate that. */
223
224 ImageHandlerMock imageMock;
225
226 std::vector<HandlerPack> blobs = {
Patrick Venture18235e62018-11-08 10:21:09 -0800227 {FirmwareBlobHandler::hashBlobID, &imageMock},
Patrick Venture72388d72018-11-07 15:06:36 -0800228 {"asdf", &imageMock},
229 };
230 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -0800231 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -0800232 };
233
234 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
235
236 EXPECT_CALL(imageMock, open("asdf")).WillOnce(Return(false));
237
238 EXPECT_FALSE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -0800239 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
Patrick Venturedb253bf2018-11-09 19:36:03 -0800240
241 /* Verify blob_id list doesn't grow. */
242 auto currentBlobs = handler->getBlobIds();
243 EXPECT_EQ(2, currentBlobs.size());
Patrick Venture72388d72018-11-07 15:06:36 -0800244}
245
246TEST(FirmwareHandlerOpenTest, OpenWithoutWriteFails)
247{
248 /* The client must set the file write bit. */
249
250 ImageHandlerMock imageMock;
251
252 std::vector<HandlerPack> blobs = {
Patrick Venture18235e62018-11-08 10:21:09 -0800253 {FirmwareBlobHandler::hashBlobID, &imageMock},
Patrick Venture72388d72018-11-07 15:06:36 -0800254 {"asdf", &imageMock},
255 };
256 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -0800257 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -0800258 };
259
260 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
261
Patrick Venture05abf7e2018-11-09 11:02:56 -0800262 EXPECT_FALSE(
263 handler->open(0, FirmwareBlobHandler::UpdateFlags::ipmi, "asdf"));
Patrick Venture72388d72018-11-07 15:06:36 -0800264}
265
266TEST(FirmwareHandlerOpenTest, OpenWithInvalidTransportBit)
267{
268 /* The client sends a request with a transport mechanism not supported. */
269
270 ImageHandlerMock imageMock;
271
272 std::vector<HandlerPack> blobs = {
Patrick Venture18235e62018-11-08 10:21:09 -0800273 {FirmwareBlobHandler::hashBlobID, &imageMock},
Patrick Venture72388d72018-11-07 15:06:36 -0800274 {"asdf", &imageMock},
275 };
276 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -0800277 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -0800278 };
279
280 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
281
Patrick Venture05abf7e2018-11-09 11:02:56 -0800282 EXPECT_FALSE(
283 handler->open(0, FirmwareBlobHandler::UpdateFlags::lpc, "asdf"));
Patrick Venture72388d72018-11-07 15:06:36 -0800284}
285
286TEST(FirmwareHandlerOpenTest, OpenWithInvalidImageBlobId)
287{
288 /* The client sends a request with an invalid image blob_id. */
289
290 ImageHandlerMock imageMock;
291
292 std::vector<HandlerPack> blobs = {
Patrick Venture18235e62018-11-08 10:21:09 -0800293 {FirmwareBlobHandler::hashBlobID, &imageMock},
Patrick Venture72388d72018-11-07 15:06:36 -0800294 {"asdf", &imageMock},
295 };
296 std::vector<DataHandlerPack> data = {
Patrick Venture05abf7e2018-11-09 11:02:56 -0800297 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
Patrick Venture72388d72018-11-07 15:06:36 -0800298 };
299
300 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
301
302 EXPECT_FALSE(handler->open(
Patrick Venture05abf7e2018-11-09 11:02:56 -0800303 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "bcdf"));
Patrick Venture72388d72018-11-07 15:06:36 -0800304}
305
306/* TODO: The client sends a request during verification. */
307/* TODO: The client sends a second request to open when there is already an open
308 * file.
309 */
310/* TODO: The client sends a request to open active image. */
311/* TODO: The client sends a request to open active hash. */
312
313} // namespace blobs