blob: 824cec92f3c8bad22db4613e856c31cfd82a1a1f [file] [log] [blame]
Patrick Ventureaa32a362018-12-13 10:52:33 -08001#include "data_interface_mock.hpp"
Patrick Venture84778b82019-06-26 20:11:09 -07002#include "flags.hpp"
Patrick Venture1f09d412019-06-19 16:01:06 -07003#include "status.hpp"
Patrick Venture5f2fcc42019-06-20 07:21:05 -07004#include "tool_errors.hpp"
Patrick Ventureaa32a362018-12-13 10:52:33 -08005#include "updater.hpp"
Patrick Venture55646de2019-05-16 10:06:26 -07006#include "updater_mock.hpp"
Patrick Venture7dad86f2019-05-17 08:52:20 -07007#include "util.hpp"
Patrick Ventureaa32a362018-12-13 10:52:33 -08008
Jie Yang328f5202021-03-16 00:52:07 -07009#include <blobs-ipmid/blobs.hpp>
Patrick Venture8cdf9642020-09-30 09:41:51 -070010#include <ipmiblob/blob_errors.hpp>
Patrick Venture664c5bc2019-03-07 08:09:45 -080011#include <ipmiblob/test/blob_interface_mock.hpp>
Patrick Venture9b37b092020-05-28 20:58:57 -070012
Patrick Ventureaa32a362018-12-13 10:52:33 -080013#include <string>
Patrick Venture8cdf9642020-09-30 09:41:51 -070014#include <vector>
Patrick Ventureaa32a362018-12-13 10:52:33 -080015
Patrick Venture8cdf9642020-09-30 09:41:51 -070016#include <gmock/gmock.h>
Patrick Ventureaa32a362018-12-13 10:52:33 -080017#include <gtest/gtest.h>
18
Patrick Venture9b534f02018-12-13 16:10:02 -080019namespace host_tool
20{
21
Patrick Venture7dcca5d2019-05-15 12:32:33 -070022using ::testing::_;
Patrick Ventureaa32a362018-12-13 10:52:33 -080023using ::testing::Eq;
Jie Yang328f5202021-03-16 00:52:07 -070024using ::testing::IsEmpty;
Patrick Ventureaa32a362018-12-13 10:52:33 -080025using ::testing::Return;
Patrick Venture8cdf9642020-09-30 09:41:51 -070026using ::testing::Throw;
Patrick Ventureb58f5612019-05-07 09:22:07 -070027using ::testing::TypedEq;
Patrick Ventureaa32a362018-12-13 10:52:33 -080028
Patrick Venture1f09d412019-06-19 16:01:06 -070029class UpdateHandlerTest : public ::testing::Test
Patrick Venture55646de2019-05-16 10:06:26 -070030{
Patrick Venture1f09d412019-06-19 16:01:06 -070031 protected:
32 const std::uint16_t session = 0xbeef;
33
Patrick Venture55646de2019-05-16 10:06:26 -070034 DataInterfaceMock handlerMock;
35 ipmiblob::BlobInterfaceMock blobMock;
Patrick Venture1f09d412019-06-19 16:01:06 -070036 UpdateHandler updater{&blobMock, &handlerMock};
37};
Patrick Venture55646de2019-05-16 10:06:26 -070038
Patrick Venture1f09d412019-06-19 16:01:06 -070039TEST_F(UpdateHandlerTest, CheckAvailableSuccess)
40{
Patrick Venture55646de2019-05-16 10:06:26 -070041 EXPECT_CALL(blobMock, getBlobList())
Patrick Venture7dad86f2019-05-17 08:52:20 -070042 .WillOnce(
Patrick Venture1d5a31c2019-05-20 11:38:22 -070043 Return(std::vector<std::string>({ipmi_flash::staticLayoutBlobId})));
Patrick Venture55646de2019-05-16 10:06:26 -070044
Patrick Venture1d5a31c2019-05-20 11:38:22 -070045 EXPECT_TRUE(updater.checkAvailable(ipmi_flash::staticLayoutBlobId));
Patrick Venture55646de2019-05-16 10:06:26 -070046}
47
Patrick Venture8cdf9642020-09-30 09:41:51 -070048TEST_F(UpdateHandlerTest, CheckAvailableFailure)
49{
50 EXPECT_CALL(blobMock, getBlobList())
51 .WillOnce(Return(std::vector<std::string>()));
52
53 EXPECT_FALSE(updater.checkAvailable(ipmi_flash::staticLayoutBlobId));
54}
55
Patrick Venture1f09d412019-06-19 16:01:06 -070056TEST_F(UpdateHandlerTest, SendFileSuccess)
Patrick Venture55646de2019-05-16 10:06:26 -070057{
58 /* Call sendFile to verify it does what we expect. */
Patrick Venture55646de2019-05-16 10:06:26 -070059 std::string firmwareImage = "image.bin";
60
61 std::uint16_t supported =
62 static_cast<std::uint16_t>(
Patrick Venture84778b82019-06-26 20:11:09 -070063 ipmi_flash::FirmwareFlags::UpdateFlags::lpc) |
64 static_cast<std::uint16_t>(
65 ipmi_flash::FirmwareFlags::UpdateFlags::openWrite);
Patrick Venture55646de2019-05-16 10:06:26 -070066
67 EXPECT_CALL(handlerMock, supportedType())
Patrick Venture84778b82019-06-26 20:11:09 -070068 .WillOnce(Return(ipmi_flash::FirmwareFlags::UpdateFlags::lpc));
Patrick Venture55646de2019-05-16 10:06:26 -070069
Patrick Venture1f09d412019-06-19 16:01:06 -070070 EXPECT_CALL(blobMock, openBlob(ipmi_flash::staticLayoutBlobId, supported))
Patrick Venture55646de2019-05-16 10:06:26 -070071 .WillOnce(Return(session));
72
Patrick Venture1f09d412019-06-19 16:01:06 -070073 EXPECT_CALL(handlerMock, sendContents(firmwareImage, session))
Patrick Venture55646de2019-05-16 10:06:26 -070074 .WillOnce(Return(true));
75
76 EXPECT_CALL(blobMock, closeBlob(session)).Times(1);
77
Patrick Venture1d5a31c2019-05-20 11:38:22 -070078 updater.sendFile(ipmi_flash::staticLayoutBlobId, firmwareImage);
Patrick Venture55646de2019-05-16 10:06:26 -070079}
80
Patrick Venture8cdf9642020-09-30 09:41:51 -070081TEST_F(UpdateHandlerTest, SendFileExceptsOnBlobOpening)
82{
83 std::string firmwareImage = "image.bin";
84
85 std::uint16_t supported =
86 static_cast<std::uint16_t>(
87 ipmi_flash::FirmwareFlags::UpdateFlags::lpc) |
88 static_cast<std::uint16_t>(
89 ipmi_flash::FirmwareFlags::UpdateFlags::openWrite);
90
91 EXPECT_CALL(handlerMock, supportedType())
92 .WillOnce(Return(ipmi_flash::FirmwareFlags::UpdateFlags::lpc));
93
94 EXPECT_CALL(blobMock, openBlob(ipmi_flash::staticLayoutBlobId, supported))
95 .WillOnce(Throw(ipmiblob::BlobException("asdf")));
96
97 EXPECT_THROW(
98 updater.sendFile(ipmi_flash::staticLayoutBlobId, firmwareImage),
99 ToolException);
100}
101
102TEST_F(UpdateHandlerTest, SendFileHandlerFailureCausesException)
103{
104 std::string firmwareImage = "image.bin";
105
106 std::uint16_t supported =
107 static_cast<std::uint16_t>(
108 ipmi_flash::FirmwareFlags::UpdateFlags::lpc) |
109 static_cast<std::uint16_t>(
110 ipmi_flash::FirmwareFlags::UpdateFlags::openWrite);
111
112 EXPECT_CALL(handlerMock, supportedType())
113 .WillOnce(Return(ipmi_flash::FirmwareFlags::UpdateFlags::lpc));
114
115 EXPECT_CALL(blobMock, openBlob(ipmi_flash::staticLayoutBlobId, supported))
116 .WillOnce(Return(session));
117
118 EXPECT_CALL(handlerMock, sendContents(firmwareImage, session))
119 .WillOnce(Return(false));
120
121 EXPECT_CALL(blobMock, closeBlob(session)).Times(1);
122
123 EXPECT_THROW(
124 updater.sendFile(ipmi_flash::staticLayoutBlobId, firmwareImage),
125 ToolException);
126}
127
Patrick Venture1f09d412019-06-19 16:01:06 -0700128TEST_F(UpdateHandlerTest, VerifyFileHandleReturnsTrueOnSuccess)
Patrick Ventureaa32a362018-12-13 10:52:33 -0800129{
Patrick Venture1f09d412019-06-19 16:01:06 -0700130 EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _))
Patrick Ventureaa32a362018-12-13 10:52:33 -0800131 .WillOnce(Return(session));
Patrick Venture55646de2019-05-16 10:06:26 -0700132 EXPECT_CALL(blobMock, commit(session, _)).WillOnce(Return());
Patrick Venture1f09d412019-06-19 16:01:06 -0700133 ipmiblob::StatResponse verificationResponse = {};
134 /* the other details of the response are ignored, and should be. */
135 verificationResponse.metadata.push_back(
136 static_cast<std::uint8_t>(ipmi_flash::ActionStatus::success));
Patrick Venture7dcca5d2019-05-15 12:32:33 -0700137
Patrick Venture1f09d412019-06-19 16:01:06 -0700138 EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
139 .WillOnce(Return(verificationResponse));
140 EXPECT_CALL(blobMock, closeBlob(session)).WillOnce(Return());
141
Brandon Kim6749ba12019-09-19 13:31:37 -0700142 EXPECT_TRUE(updater.verifyFile(ipmi_flash::verifyBlobId, false));
Patrick Venture1f09d412019-06-19 16:01:06 -0700143}
144
Patrick Venture8cdf9642020-09-30 09:41:51 -0700145TEST_F(UpdateHandlerTest, VerifyFileHandleSkipsPollingIfIgnoreStatus)
146{
147 /* if ignoreStatus, it'll skip polling for a verification result. */
148 EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _))
149 .WillOnce(Return(session));
150 EXPECT_CALL(blobMock, commit(session, _)).WillOnce(Return());
151
152 EXPECT_CALL(blobMock, closeBlob(session)).WillOnce(Return());
153
154 EXPECT_TRUE(updater.verifyFile(ipmi_flash::verifyBlobId, true));
155}
156
157TEST_F(UpdateHandlerTest, VerifyFileConvertsOpenBlobExceptionToToolException)
158{
159 /* On open, it can except and this is converted to a ToolException. */
160 EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _))
161 .WillOnce(Throw(ipmiblob::BlobException("asdf")));
162 EXPECT_THROW(updater.verifyFile(ipmi_flash::verifyBlobId, false),
163 ToolException);
164}
165
166TEST_F(UpdateHandlerTest, VerifyFileCommitExceptionForwards)
167{
168 /* On commit, it can except. */
169 EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _))
170 .WillOnce(Return(session));
171 EXPECT_CALL(blobMock, commit(session, _))
172 .WillOnce(Throw(ipmiblob::BlobException("asdf")));
173 EXPECT_THROW(updater.verifyFile(ipmi_flash::verifyBlobId, false),
174 ToolException);
175}
176
Jie Yang328f5202021-03-16 00:52:07 -0700177TEST_F(UpdateHandlerTest, ReadVerisonReturnExpected)
178{
179 /* It can return as expected, when polling and readBytes succeeds. */
180 EXPECT_CALL(blobMock, openBlob(ipmi_flash::biosVersionBlobId, _))
181 .WillOnce(Return(session));
182 ipmiblob::StatResponse readVersionResponse = {};
183 readVersionResponse.blob_state =
184 blobs::StateFlags::open_read | blobs::StateFlags::committed;
185 readVersionResponse.size = 10;
186 EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
187 .WillOnce(Return(readVersionResponse));
188 std::vector<uint8_t> resp = {0x2d, 0xfe};
189 EXPECT_CALL(blobMock, readBytes(session, 0, _)).WillOnce(Return(resp));
190
191 EXPECT_CALL(blobMock, closeBlob(session)).WillOnce(Return());
192 EXPECT_EQ(resp, updater.readVersion(ipmi_flash::biosVersionBlobId));
193}
194
195TEST_F(UpdateHandlerTest, ReadVersionExceptionWhenPollingSucceedsReadBytesFails)
196{
197 /* On readBytes, it can except. */
198 EXPECT_CALL(blobMock, openBlob(ipmi_flash::biosVersionBlobId, _))
199 .WillOnce(Return(session));
200 ipmiblob::StatResponse readVersionResponse = {};
201 readVersionResponse.blob_state =
202 blobs::StateFlags::open_read | blobs::StateFlags::committed;
203 readVersionResponse.size = 10;
204 EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
205 .WillOnce(Return(readVersionResponse));
206 EXPECT_CALL(blobMock, readBytes(session, 0, _))
207 .WillOnce(Throw(ipmiblob::BlobException("asdf")));
208 EXPECT_CALL(blobMock, closeBlob(session)).WillOnce(Return());
209 EXPECT_THROW(updater.readVersion(ipmi_flash::biosVersionBlobId),
210 ToolException);
211}
212
213TEST_F(UpdateHandlerTest, ReadVersionReturnsEmptyIfPollingFails)
214{
215 /* It can return an empty result, when polling fails. */
216 EXPECT_CALL(blobMock, openBlob(ipmi_flash::biosVersionBlobId, _))
217 .WillOnce(Return(session));
218 ipmiblob::StatResponse readVersionResponse = {};
219 readVersionResponse.blob_state = blobs::StateFlags::commit_error;
220 EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
221 .WillOnce(Return(readVersionResponse));
222 EXPECT_CALL(blobMock, closeBlob(session)).WillOnce(Return());
223 EXPECT_THAT(updater.readVersion(ipmi_flash::biosVersionBlobId), IsEmpty());
224}
225
226TEST_F(UpdateHandlerTest, ReadVersionCovertsOpenBlobExceptionToToolException)
227{
228 /* On open, it can except and this is converted to a ToolException. */
229 EXPECT_CALL(blobMock, openBlob(ipmi_flash::biosVersionBlobId, _))
230 .WillOnce(Throw(ipmiblob::BlobException("asdf")));
231 EXPECT_THROW(updater.readVersion(ipmi_flash::biosVersionBlobId),
232 ToolException);
233}
234
Patrick Venture8cdf9642020-09-30 09:41:51 -0700235TEST_F(UpdateHandlerTest, CleanArtifactsSkipsCleanupIfUnableToOpen)
236{
237 /* It only tries to commit if it's able to open the blob. However, if
238 * committing fails, this error is ignored.
239 */
240 EXPECT_CALL(blobMock, openBlob(ipmi_flash::cleanupBlobId, _))
241 .WillOnce(Throw(ipmiblob::BlobException("asdf")));
242 EXPECT_CALL(blobMock, commit(_, _)).Times(0);
243 EXPECT_CALL(blobMock, closeBlob(_)).Times(0);
244
245 updater.cleanArtifacts();
246}
247
248TEST_F(UpdateHandlerTest, CleanArtifactsIfOpenDoesClose)
249{
250 /* The closeBlob call is called even if commit excepts. */
251 std::uint16_t session = 0xa5eb;
252 EXPECT_CALL(blobMock, openBlob(ipmi_flash::cleanupBlobId, _))
253 .WillOnce(Return(session));
254 EXPECT_CALL(blobMock, commit(session, _))
255 .WillOnce(Throw(ipmiblob::BlobException("asdf")));
256 EXPECT_CALL(blobMock, closeBlob(session));
257
258 updater.cleanArtifacts();
259}
260
261TEST_F(UpdateHandlerTest, CleanArtifactsSuccessPath)
262{
263 std::uint16_t session = 0xa5eb;
264 EXPECT_CALL(blobMock, openBlob(ipmi_flash::cleanupBlobId, _))
265 .WillOnce(Return(session));
266 EXPECT_CALL(blobMock, commit(session, _));
267 EXPECT_CALL(blobMock, closeBlob(session));
268
269 updater.cleanArtifacts();
270}
271
Patrick Venture1f09d412019-06-19 16:01:06 -0700272class UpdaterTest : public ::testing::Test
273{
274 protected:
Patrick Venture8cdf9642020-09-30 09:41:51 -0700275 static constexpr char image[] = "image.bin";
276 static constexpr char signature[] = "signature.bin";
277 static constexpr char layout[] = "static";
278 static constexpr char path[] = "/flash/static";
279
Patrick Venture1f09d412019-06-19 16:01:06 -0700280 ipmiblob::BlobInterfaceMock blobMock;
281 std::uint16_t session = 0xbeef;
Brandon Kim6749ba12019-09-19 13:31:37 -0700282 bool defaultIgnore = false;
Patrick Venture1f09d412019-06-19 16:01:06 -0700283};
284
Patrick Venture1f09d412019-06-19 16:01:06 -0700285TEST_F(UpdaterTest, UpdateMainReturnsSuccessIfAllSuccess)
286{
Patrick Venture1f09d412019-06-19 16:01:06 -0700287 UpdateHandlerMock handler;
288
Patrick Venture8cdf9642020-09-30 09:41:51 -0700289 EXPECT_CALL(handler, checkAvailable(path)).WillOnce(Return(true));
290 EXPECT_CALL(handler, sendFile(path, image)).WillOnce(Return());
291 EXPECT_CALL(handler, sendFile(ipmi_flash::hashBlobId, signature))
292 .WillOnce(Return());
Brandon Kim6749ba12019-09-19 13:31:37 -0700293 EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore))
Patrick Venture1f09d412019-06-19 16:01:06 -0700294 .WillOnce(Return(true));
Brandon Kim6749ba12019-09-19 13:31:37 -0700295 EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId, defaultIgnore))
Patrick Venture1f09d412019-06-19 16:01:06 -0700296 .WillOnce(Return(true));
297
Patrick Venture8cdf9642020-09-30 09:41:51 -0700298 updaterMain(&handler, image, signature, layout, defaultIgnore);
Brandon Kim6749ba12019-09-19 13:31:37 -0700299}
300
301TEST_F(UpdaterTest, UpdateMainReturnsSuccessWithIgnoreUpdate)
302{
Brandon Kim6749ba12019-09-19 13:31:37 -0700303 UpdateHandlerMock handler;
304 bool updateIgnore = true;
305
Patrick Venture8cdf9642020-09-30 09:41:51 -0700306 EXPECT_CALL(handler, checkAvailable(path)).WillOnce(Return(true));
307 EXPECT_CALL(handler, sendFile(path, image)).WillOnce(Return());
308 EXPECT_CALL(handler, sendFile(ipmi_flash::hashBlobId, signature))
309 .WillOnce(Return());
Brandon Kim6749ba12019-09-19 13:31:37 -0700310 EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore))
311 .WillOnce(Return(true));
312 EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId, updateIgnore))
313 .WillOnce(Return(true));
314
Patrick Venture8cdf9642020-09-30 09:41:51 -0700315 updaterMain(&handler, image, signature, layout, updateIgnore);
Patrick Venture1f09d412019-06-19 16:01:06 -0700316}
Patrick Venture9b534f02018-12-13 16:10:02 -0800317
Patrick Venture5f2fcc42019-06-20 07:21:05 -0700318TEST_F(UpdaterTest, UpdateMainCleansUpOnFailure)
319{
Patrick Venture5f2fcc42019-06-20 07:21:05 -0700320 UpdateHandlerMock handler;
321
Patrick Venture8cdf9642020-09-30 09:41:51 -0700322 EXPECT_CALL(handler, checkAvailable(path)).WillOnce(Return(true));
323 EXPECT_CALL(handler, sendFile(path, image)).WillOnce(Return());
324 EXPECT_CALL(handler, sendFile(ipmi_flash::hashBlobId, signature))
325 .WillOnce(Return());
Brandon Kim6749ba12019-09-19 13:31:37 -0700326 EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore))
Patrick Venture5f2fcc42019-06-20 07:21:05 -0700327 .WillOnce(Return(false));
328 EXPECT_CALL(handler, cleanArtifacts()).WillOnce(Return());
329
Patrick Venture8cdf9642020-09-30 09:41:51 -0700330 EXPECT_THROW(updaterMain(&handler, image, signature, layout, defaultIgnore),
331 ToolException);
332}
333
334TEST_F(UpdaterTest, UpdateMainExceptsOnUpdateBlobFailure)
335{
336 UpdateHandlerMock handler;
337
338 EXPECT_CALL(handler, checkAvailable(path)).WillOnce(Return(true));
339 EXPECT_CALL(handler, sendFile(path, image)).WillOnce(Return());
340 EXPECT_CALL(handler, sendFile(ipmi_flash::hashBlobId, signature))
341 .WillOnce(Return());
342 EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore))
343 .WillOnce(Return(true));
344 EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId, defaultIgnore))
345 .WillOnce(Return(false));
346 EXPECT_CALL(handler, cleanArtifacts()).WillOnce(Return());
347
348 EXPECT_THROW(updaterMain(&handler, image, signature, layout, defaultIgnore),
349 ToolException);
350}
351
352TEST_F(UpdaterTest, UpdateMainExceptsIfAvailableNotFound)
353{
354 UpdateHandlerMock handler;
355
356 EXPECT_CALL(handler, checkAvailable(path)).WillOnce(Return(false));
357
358 EXPECT_THROW(updaterMain(&handler, image, signature, layout, defaultIgnore),
359 ToolException);
Patrick Venture5f2fcc42019-06-20 07:21:05 -0700360}
361
Patrick Venture9b534f02018-12-13 16:10:02 -0800362} // namespace host_tool