blob: a974c2b00c1d9c34469b7f5d70be5b342f044eb6 [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 Venturefa06a5f2019-07-01 09:22:38 -0700176 getToVerificationStartedWitHashBlob();
Patrick Venture930c7b72019-05-24 11:11:08 -0700177 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
Patrick Venturea04997b2019-05-24 10:15:48 -0700197 std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId};
198 for (const auto& blob : testBlobs)
199 {
Patrick Venture930c7b72019-05-24 11:11:08 -0700200 ASSERT_TRUE(handler->canHandleBlob(blob));
201
Patrick Venturea04997b2019-05-24 10:15:48 -0700202 blobs::BlobMeta meta = {};
203 EXPECT_TRUE(handler->stat(blob, &meta));
Benjamin Fair12901982019-11-12 13:55:46 -0800204 EXPECT_EQ(expectedIdleMeta, meta);
Patrick Venturea04997b2019-05-24 10:15:48 -0700205 }
206}
207
208/*
Patrick Venturefb74ad52019-05-24 15:03:35 -0700209 * writemeta(session)
210 */
211TEST_F(FirmwareHandlerVerificationStartedTest,
212 WriteMetaOnVerifySessionReturnsFailure)
213{
214 getToVerificationStarted(staticLayoutBlobId);
215
216 std::vector<std::uint8_t> bytes = {0x01, 0x02};
217 EXPECT_FALSE(handler->writeMeta(session, 0, bytes));
218}
219
220/*
221 * write(session)
222 */
Patrick Venture9c6de5a2019-05-24 15:12:26 -0700223TEST_F(FirmwareHandlerVerificationStartedTest,
224 WriteOnVerifySessionReturnsFailure)
225{
226 getToVerificationStarted(staticLayoutBlobId);
227
228 std::vector<std::uint8_t> bytes = {0x01, 0x02};
229 EXPECT_FALSE(handler->write(session, 0, bytes));
230}
Patrick Venturefb74ad52019-05-24 15:03:35 -0700231
232/*
Patrick Venture237e2c62019-05-23 20:35:33 -0700233 * open(blob) - there is nothing you can open, this state has an open file.
Patrick Ventured4509102019-05-24 15:22:04 -0700234 */
235TEST_F(FirmwareHandlerVerificationStartedTest,
236 AttemptToOpenImageFileReturnsFailure)
237{
238 /* Attempt to open a file one normally can open, however, as there is
239 * already a file open, this will fail.
240 */
241 getToVerificationStarted(staticLayoutBlobId);
242
Patrick Venture2c014152019-05-28 18:16:05 -0700243 auto blobsToOpen = handler->getBlobIds();
244 for (const auto& blob : blobsToOpen)
245 {
246 EXPECT_FALSE(handler->open(session + 1, flags, blob));
247 }
Patrick Ventured4509102019-05-24 15:22:04 -0700248}
249
250/*
Patrick Venture8facb382019-05-24 15:27:36 -0700251 * read(session)
252 */
253TEST_F(FirmwareHandlerVerificationStartedTest, ReadOfVerifyBlobReturnsEmpty)
254{
255 getToVerificationStarted(staticLayoutBlobId);
Patrick Venture2c014152019-05-28 18:16:05 -0700256 EXPECT_THAT(handler->read(session, 0, 1), IsEmpty());
Patrick Venture8facb382019-05-24 15:27:36 -0700257}
258
259/*
Patrick Ventureddc35072019-05-24 15:30:57 -0700260 * commit(session)
261 */
262TEST_F(FirmwareHandlerVerificationStartedTest,
263 CommitOnVerifyDuringVerificationHasNoImpact)
264{
265 getToVerificationStarted(staticLayoutBlobId);
266 EXPECT_TRUE(handler->commit(session, {}));
Patrick Venture6fdd02e2019-05-28 13:02:04 -0700267 expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
Patrick Ventureddc35072019-05-24 15:30:57 -0700268}
269
270/*
Patrick Venturea04997b2019-05-24 10:15:48 -0700271 * close(session) - close while state if verificationStarted without calling
272 * stat first will abort.
Patrick Venture237e2c62019-05-23 20:35:33 -0700273 */
Patrick Ventured5741022019-06-17 09:08:35 -0700274TEST_F(FirmwareHandlerVerificationStartedTest,
275 CloseOnVerifyDuringVerificationAbortsProcess)
276{
277 getToVerificationStarted(staticLayoutBlobId);
278 EXPECT_CALL(*verifyMockPtr, abort()).Times(1);
279
280 EXPECT_TRUE(handler->close(session));
281
Patrick Ventured5741022019-06-17 09:08:35 -0700282 EXPECT_THAT(handler->getBlobIds(),
Patrick Venture9a69f732019-06-17 14:05:13 -0700283 UnorderedElementsAreArray(startingBlobs));
Patrick Ventured5741022019-06-17 09:08:35 -0700284
285 expectedState(FirmwareBlobHandler::UpdateState::notYetStarted);
286}
Patrick Venture237e2c62019-05-23 20:35:33 -0700287
288} // namespace
289} // namespace ipmi_flash