blob: 74e24b27fda8190d3873d5c025ca86777c5d1c77 [file] [log] [blame]
Patrick Venture237e2c62019-05-23 20:35:33 -07001/* The goal of these tests is to verify the behavior of all blob commands given
2 * the current state is verificationStarted. This state is achieved as a out of
3 * verificationPending.
4 */
5#include "firmware_handler.hpp"
6#include "firmware_unittest.hpp"
7#include "status.hpp"
8#include "util.hpp"
9
10#include <cstdint>
11#include <string>
12#include <vector>
13
14#include <gtest/gtest.h>
15
16namespace ipmi_flash
17{
18namespace
19{
20
Patrick Venture8facb382019-05-24 15:27:36 -070021using ::testing::IsEmpty;
Patrick Venture237e2c62019-05-23 20:35:33 -070022using ::testing::Return;
Patrick Venture2c014152019-05-28 18:16:05 -070023using ::testing::UnorderedElementsAreArray;
Patrick Venture237e2c62019-05-23 20:35:33 -070024
25/*
26 * There are the following calls (parameters may vary):
27 * canHandleBlob(blob)
28 * getBlobIds
29 * deleteBlob(blob)
30 * stat(blob)
31 * stat(session)
32 * open(blob)
33 * close(session)
34 * writemeta(session)
35 * write(session)
36 * read(session)
37 * commit(session)
38 *
39 * Testing canHandleBlob is uninteresting in this state. Getting the BlobIDs
40 * will inform what canHandleBlob will return.
41 */
42
43class FirmwareHandlerVerificationStartedTest : public IpmiOnlyFirmwareStaticTest
44{
Patrick Venture237e2c62019-05-23 20:35:33 -070045};
46
47/*
Patrick Venture2c014152019-05-28 18:16:05 -070048 * canHandleBlob(blob)
49 * getBlobIds()
50 */
51TEST_F(FirmwareHandlerVerificationStartedTest, GetBlobIdsReturnsExpectedList)
52{
53 getToVerificationStarted(staticLayoutBlobId);
54
Patrick Venture9a69f732019-06-17 14:05:13 -070055 auto blobs = handler->getBlobIds();
56 EXPECT_THAT(
57 blobs, UnorderedElementsAreArray({activeImageBlobId, staticLayoutBlobId,
58 hashBlobId, verifyBlobId}));
Patrick Venture2c014152019-05-28 18:16:05 -070059
Patrick Venture9a69f732019-06-17 14:05:13 -070060 for (const auto& blob : blobs)
Patrick Venture2c014152019-05-28 18:16:05 -070061 {
62 EXPECT_TRUE(handler->canHandleBlob(blob));
63 }
64}
65
66/*
Patrick Venture237e2c62019-05-23 20:35:33 -070067 * stat(session)
68 */
69TEST_F(FirmwareHandlerVerificationStartedTest,
70 StatOnVerifyBlobIdAfterCommitChecksStateAndReturnsRunning)
71{
72 getToVerificationStarted(staticLayoutBlobId);
Patrick Venturef1f0f652019-06-03 09:10:19 -070073 EXPECT_CALL(*verifyMockPtr, status())
Patrick Ventureda66fd82019-06-03 11:11:24 -070074 .WillOnce(Return(ActionStatus::running));
Patrick Venture237e2c62019-05-23 20:35:33 -070075
76 blobs::BlobMeta meta, expectedMeta = {};
77 expectedMeta.size = 0;
78 expectedMeta.blobState = flags | blobs::StateFlags::committing;
79 expectedMeta.metadata.push_back(
Patrick Ventureda66fd82019-06-03 11:11:24 -070080 static_cast<std::uint8_t>(ActionStatus::running));
Patrick Venture237e2c62019-05-23 20:35:33 -070081
82 EXPECT_TRUE(handler->stat(session, &meta));
83 EXPECT_EQ(expectedMeta, meta);
84}
85
86TEST_F(FirmwareHandlerVerificationStartedTest,
Patrick Ventured52c2c32019-05-30 09:54:35 -070087 StatOnVerifyBlobIdAfterCommitChecksStateAndReturnsOther)
88{
89 getToVerificationStarted(staticLayoutBlobId);
Patrick Venturef1f0f652019-06-03 09:10:19 -070090 EXPECT_CALL(*verifyMockPtr, status())
Patrick Ventureda66fd82019-06-03 11:11:24 -070091 .WillOnce(Return(ActionStatus::unknown));
Patrick Ventured52c2c32019-05-30 09:54:35 -070092
93 blobs::BlobMeta meta, expectedMeta = {};
94 expectedMeta.size = 0;
95 expectedMeta.blobState = flags | blobs::StateFlags::committing;
96 expectedMeta.metadata.push_back(
Patrick Ventureda66fd82019-06-03 11:11:24 -070097 static_cast<std::uint8_t>(ActionStatus::unknown));
Patrick Ventured52c2c32019-05-30 09:54:35 -070098
99 EXPECT_TRUE(handler->stat(session, &meta));
100 EXPECT_EQ(expectedMeta, meta);
101}
102
103TEST_F(FirmwareHandlerVerificationStartedTest,
Patrick Venture237e2c62019-05-23 20:35:33 -0700104 StatOnVerifyBlobIdAfterCommitCheckStateAndReturnsFailed)
105{
106 /* If the returned state from the verification handler is failed, it sets
107 * commit_error and transitions to verificationCompleted.
108 */
109 getToVerificationStarted(staticLayoutBlobId);
Patrick Venturef1f0f652019-06-03 09:10:19 -0700110 EXPECT_CALL(*verifyMockPtr, status())
Patrick Ventureda66fd82019-06-03 11:11:24 -0700111 .WillOnce(Return(ActionStatus::failed));
Patrick Venture237e2c62019-05-23 20:35:33 -0700112
113 blobs::BlobMeta meta, expectedMeta = {};
114 expectedMeta.size = 0;
115 expectedMeta.blobState = flags | blobs::StateFlags::commit_error;
116 expectedMeta.metadata.push_back(
Patrick Ventureda66fd82019-06-03 11:11:24 -0700117 static_cast<std::uint8_t>(ActionStatus::failed));
Patrick Venture237e2c62019-05-23 20:35:33 -0700118
Patrick Venture237e2c62019-05-23 20:35:33 -0700119 EXPECT_TRUE(handler->stat(session, &meta));
120 EXPECT_EQ(expectedMeta, meta);
Patrick Venture6fdd02e2019-05-28 13:02:04 -0700121 expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
Patrick Venture237e2c62019-05-23 20:35:33 -0700122}
123
124TEST_F(FirmwareHandlerVerificationStartedTest,
125 StatOnVerifyBlobIdAfterCommitCheckStateAndReturnsSuccess)
126{
127 /* If the returned state from the verification handler is success, it sets
128 * committed and transitions to verificationCompleted.
129 */
130 getToVerificationStarted(staticLayoutBlobId);
Patrick Venturef1f0f652019-06-03 09:10:19 -0700131 EXPECT_CALL(*verifyMockPtr, status())
Patrick Ventureda66fd82019-06-03 11:11:24 -0700132 .WillOnce(Return(ActionStatus::success));
Patrick Venture237e2c62019-05-23 20:35:33 -0700133
134 blobs::BlobMeta meta, expectedMeta = {};
135 expectedMeta.size = 0;
136 expectedMeta.blobState = flags | blobs::StateFlags::committed;
137 expectedMeta.metadata.push_back(
Patrick Ventureda66fd82019-06-03 11:11:24 -0700138 static_cast<std::uint8_t>(ActionStatus::success));
Patrick Venture237e2c62019-05-23 20:35:33 -0700139
Patrick Venture237e2c62019-05-23 20:35:33 -0700140 EXPECT_TRUE(handler->stat(session, &meta));
141 EXPECT_EQ(expectedMeta, meta);
Patrick Venture6fdd02e2019-05-28 13:02:04 -0700142 expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
Patrick Venture237e2c62019-05-23 20:35:33 -0700143}
144
Patrick Venturebcc0c772019-06-17 10:42:06 -0700145/*
146 * deleteBlob(blob)
147 */
148TEST_F(FirmwareHandlerVerificationStartedTest, DeleteBlobReturnsFalse)
149{
150 /* Try deleting all blobs, it doesn't really matter which though because you
151 * cannot close out an open session, therefore you must fail to delete
152 * anything unless everything is closed.
153 */
154 getToVerificationStarted(staticLayoutBlobId);
155 auto blobs = handler->getBlobIds();
156 for (const auto& b : blobs)
157 {
158 EXPECT_FALSE(handler->deleteBlob(b));
159 }
160}
Patrick Venturea04997b2019-05-24 10:15:48 -0700161
162/*
Patrick Venture237e2c62019-05-23 20:35:33 -0700163 * stat(blob)
Patrick Venturea04997b2019-05-24 10:15:48 -0700164 */
165TEST_F(FirmwareHandlerVerificationStartedTest, StatOnActiveImageReturnsFailure)
166{
167 getToVerificationStarted(staticLayoutBlobId);
Patrick Venture930c7b72019-05-24 11:11:08 -0700168 ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId));
Patrick Venturea04997b2019-05-24 10:15:48 -0700169
170 blobs::BlobMeta meta;
171 EXPECT_FALSE(handler->stat(activeImageBlobId, &meta));
172}
173
174TEST_F(FirmwareHandlerVerificationStartedTest, StatOnActiveHashReturnsFailure)
175{
Patrick Venture930c7b72019-05-24 11:11:08 -0700176 getToVerificationStarted(hashBlobId);
177 ASSERT_TRUE(handler->canHandleBlob(activeHashBlobId));
Patrick Venturea04997b2019-05-24 10:15:48 -0700178
179 blobs::BlobMeta meta;
180 EXPECT_FALSE(handler->stat(activeHashBlobId, &meta));
181}
182
183TEST_F(FirmwareHandlerVerificationStartedTest, StatOnVerifyBlobReturnsFailure)
184{
185 /* the verifyBlobId is available starting at verificationPending. */
186 getToVerificationStarted(staticLayoutBlobId);
Patrick Venture930c7b72019-05-24 11:11:08 -0700187 ASSERT_TRUE(handler->canHandleBlob(verifyBlobId));
Patrick Venturea04997b2019-05-24 10:15:48 -0700188
189 blobs::BlobMeta meta;
190 EXPECT_FALSE(handler->stat(verifyBlobId, &meta));
191}
192
193TEST_F(FirmwareHandlerVerificationStartedTest, StatOnNormalBlobsReturnsSuccess)
194{
195 getToVerificationStarted(staticLayoutBlobId);
196
197 blobs::BlobMeta expected;
198 expected.blobState = FirmwareBlobHandler::UpdateFlags::ipmi;
199 expected.size = 0;
200
201 std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId};
202 for (const auto& blob : testBlobs)
203 {
Patrick Venture930c7b72019-05-24 11:11:08 -0700204 ASSERT_TRUE(handler->canHandleBlob(blob));
205
Patrick Venturea04997b2019-05-24 10:15:48 -0700206 blobs::BlobMeta meta = {};
207 EXPECT_TRUE(handler->stat(blob, &meta));
208 EXPECT_EQ(expected, meta);
209 }
210}
211
212/*
Patrick Venturefb74ad52019-05-24 15:03:35 -0700213 * writemeta(session)
214 */
215TEST_F(FirmwareHandlerVerificationStartedTest,
216 WriteMetaOnVerifySessionReturnsFailure)
217{
218 getToVerificationStarted(staticLayoutBlobId);
219
220 std::vector<std::uint8_t> bytes = {0x01, 0x02};
221 EXPECT_FALSE(handler->writeMeta(session, 0, bytes));
222}
223
224/*
225 * write(session)
226 */
Patrick Venture9c6de5a2019-05-24 15:12:26 -0700227TEST_F(FirmwareHandlerVerificationStartedTest,
228 WriteOnVerifySessionReturnsFailure)
229{
230 getToVerificationStarted(staticLayoutBlobId);
231
232 std::vector<std::uint8_t> bytes = {0x01, 0x02};
233 EXPECT_FALSE(handler->write(session, 0, bytes));
234}
Patrick Venturefb74ad52019-05-24 15:03:35 -0700235
236/*
Patrick Venture237e2c62019-05-23 20:35:33 -0700237 * open(blob) - there is nothing you can open, this state has an open file.
Patrick Ventured4509102019-05-24 15:22:04 -0700238 */
239TEST_F(FirmwareHandlerVerificationStartedTest,
240 AttemptToOpenImageFileReturnsFailure)
241{
242 /* Attempt to open a file one normally can open, however, as there is
243 * already a file open, this will fail.
244 */
245 getToVerificationStarted(staticLayoutBlobId);
246
Patrick Venture2c014152019-05-28 18:16:05 -0700247 auto blobsToOpen = handler->getBlobIds();
248 for (const auto& blob : blobsToOpen)
249 {
250 EXPECT_FALSE(handler->open(session + 1, flags, blob));
251 }
Patrick Ventured4509102019-05-24 15:22:04 -0700252}
253
254/*
Patrick Venture8facb382019-05-24 15:27:36 -0700255 * read(session)
256 */
257TEST_F(FirmwareHandlerVerificationStartedTest, ReadOfVerifyBlobReturnsEmpty)
258{
259 getToVerificationStarted(staticLayoutBlobId);
Patrick Venture2c014152019-05-28 18:16:05 -0700260 EXPECT_THAT(handler->read(session, 0, 1), IsEmpty());
Patrick Venture8facb382019-05-24 15:27:36 -0700261}
262
263/*
Patrick Ventureddc35072019-05-24 15:30:57 -0700264 * commit(session)
265 */
266TEST_F(FirmwareHandlerVerificationStartedTest,
267 CommitOnVerifyDuringVerificationHasNoImpact)
268{
269 getToVerificationStarted(staticLayoutBlobId);
270 EXPECT_TRUE(handler->commit(session, {}));
Patrick Venture6fdd02e2019-05-28 13:02:04 -0700271 expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
Patrick Ventureddc35072019-05-24 15:30:57 -0700272}
273
274/*
Patrick Venturea04997b2019-05-24 10:15:48 -0700275 * close(session) - close while state if verificationStarted without calling
276 * stat first will abort.
Patrick Venture237e2c62019-05-23 20:35:33 -0700277 */
Patrick Ventured5741022019-06-17 09:08:35 -0700278TEST_F(FirmwareHandlerVerificationStartedTest,
279 CloseOnVerifyDuringVerificationAbortsProcess)
280{
281 getToVerificationStarted(staticLayoutBlobId);
282 EXPECT_CALL(*verifyMockPtr, abort()).Times(1);
283
284 EXPECT_TRUE(handler->close(session));
285
Patrick Ventured5741022019-06-17 09:08:35 -0700286 EXPECT_THAT(handler->getBlobIds(),
Patrick Venture9a69f732019-06-17 14:05:13 -0700287 UnorderedElementsAreArray(startingBlobs));
Patrick Ventured5741022019-06-17 09:08:35 -0700288
289 expectedState(FirmwareBlobHandler::UpdateState::notYetStarted);
290}
Patrick Venture237e2c62019-05-23 20:35:33 -0700291
292} // namespace
293} // namespace ipmi_flash