blob: df2c422310728e250aa1ac2144ee469269d32427 [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
Patrick Venture9b37b092020-05-28 20:58:57 -070044{};
Patrick Venture237e2c62019-05-23 20:35:33 -070045
46/*
Patrick Venture2c014152019-05-28 18:16:05 -070047 * canHandleBlob(blob)
48 * getBlobIds()
49 */
50TEST_F(FirmwareHandlerVerificationStartedTest, GetBlobIdsReturnsExpectedList)
51{
52 getToVerificationStarted(staticLayoutBlobId);
53
Patrick Venture9a69f732019-06-17 14:05:13 -070054 auto blobs = handler->getBlobIds();
55 EXPECT_THAT(
56 blobs, UnorderedElementsAreArray({activeImageBlobId, staticLayoutBlobId,
57 hashBlobId, verifyBlobId}));
Patrick Venture2c014152019-05-28 18:16:05 -070058
Patrick Venture9a69f732019-06-17 14:05:13 -070059 for (const auto& blob : blobs)
Patrick Venture2c014152019-05-28 18:16:05 -070060 {
61 EXPECT_TRUE(handler->canHandleBlob(blob));
62 }
63}
64
65/*
Patrick Venture237e2c62019-05-23 20:35:33 -070066 * stat(session)
67 */
68TEST_F(FirmwareHandlerVerificationStartedTest,
69 StatOnVerifyBlobIdAfterCommitChecksStateAndReturnsRunning)
70{
71 getToVerificationStarted(staticLayoutBlobId);
Patrick Venturef1f0f652019-06-03 09:10:19 -070072 EXPECT_CALL(*verifyMockPtr, status())
Patrick Ventureda66fd82019-06-03 11:11:24 -070073 .WillOnce(Return(ActionStatus::running));
Patrick Venture237e2c62019-05-23 20:35:33 -070074
75 blobs::BlobMeta meta, expectedMeta = {};
76 expectedMeta.size = 0;
77 expectedMeta.blobState = flags | blobs::StateFlags::committing;
78 expectedMeta.metadata.push_back(
Patrick Ventureda66fd82019-06-03 11:11:24 -070079 static_cast<std::uint8_t>(ActionStatus::running));
Patrick Venture237e2c62019-05-23 20:35:33 -070080
81 EXPECT_TRUE(handler->stat(session, &meta));
82 EXPECT_EQ(expectedMeta, meta);
83}
84
85TEST_F(FirmwareHandlerVerificationStartedTest,
Patrick Ventured52c2c32019-05-30 09:54:35 -070086 StatOnVerifyBlobIdAfterCommitChecksStateAndReturnsOther)
87{
88 getToVerificationStarted(staticLayoutBlobId);
Patrick Venturef1f0f652019-06-03 09:10:19 -070089 EXPECT_CALL(*verifyMockPtr, status())
Patrick Ventureda66fd82019-06-03 11:11:24 -070090 .WillOnce(Return(ActionStatus::unknown));
Patrick Ventured52c2c32019-05-30 09:54:35 -070091
92 blobs::BlobMeta meta, expectedMeta = {};
93 expectedMeta.size = 0;
94 expectedMeta.blobState = flags | blobs::StateFlags::committing;
95 expectedMeta.metadata.push_back(
Patrick Ventureda66fd82019-06-03 11:11:24 -070096 static_cast<std::uint8_t>(ActionStatus::unknown));
Patrick Ventured52c2c32019-05-30 09:54:35 -070097
98 EXPECT_TRUE(handler->stat(session, &meta));
99 EXPECT_EQ(expectedMeta, meta);
100}
101
102TEST_F(FirmwareHandlerVerificationStartedTest,
Patrick Venture237e2c62019-05-23 20:35:33 -0700103 StatOnVerifyBlobIdAfterCommitCheckStateAndReturnsFailed)
104{
105 /* If the returned state from the verification handler is failed, it sets
106 * commit_error and transitions to verificationCompleted.
107 */
108 getToVerificationStarted(staticLayoutBlobId);
Patrick Venturef1f0f652019-06-03 09:10:19 -0700109 EXPECT_CALL(*verifyMockPtr, status())
Patrick Ventureda66fd82019-06-03 11:11:24 -0700110 .WillOnce(Return(ActionStatus::failed));
Patrick Venture237e2c62019-05-23 20:35:33 -0700111
112 blobs::BlobMeta meta, expectedMeta = {};
113 expectedMeta.size = 0;
114 expectedMeta.blobState = flags | blobs::StateFlags::commit_error;
115 expectedMeta.metadata.push_back(
Patrick Ventureda66fd82019-06-03 11:11:24 -0700116 static_cast<std::uint8_t>(ActionStatus::failed));
Patrick Venture237e2c62019-05-23 20:35:33 -0700117
Patrick Venture237e2c62019-05-23 20:35:33 -0700118 EXPECT_TRUE(handler->stat(session, &meta));
119 EXPECT_EQ(expectedMeta, meta);
Patrick Venture6fdd02e2019-05-28 13:02:04 -0700120 expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
Patrick Venture237e2c62019-05-23 20:35:33 -0700121}
122
123TEST_F(FirmwareHandlerVerificationStartedTest,
124 StatOnVerifyBlobIdAfterCommitCheckStateAndReturnsSuccess)
125{
126 /* If the returned state from the verification handler is success, it sets
127 * committed and transitions to verificationCompleted.
128 */
129 getToVerificationStarted(staticLayoutBlobId);
Patrick Venturef1f0f652019-06-03 09:10:19 -0700130 EXPECT_CALL(*verifyMockPtr, status())
Patrick Ventureda66fd82019-06-03 11:11:24 -0700131 .WillOnce(Return(ActionStatus::success));
Patrick Venture237e2c62019-05-23 20:35:33 -0700132
133 blobs::BlobMeta meta, expectedMeta = {};
134 expectedMeta.size = 0;
135 expectedMeta.blobState = flags | blobs::StateFlags::committed;
136 expectedMeta.metadata.push_back(
Patrick Ventureda66fd82019-06-03 11:11:24 -0700137 static_cast<std::uint8_t>(ActionStatus::success));
Patrick Venture237e2c62019-05-23 20:35:33 -0700138
Patrick Venture237e2c62019-05-23 20:35:33 -0700139 EXPECT_TRUE(handler->stat(session, &meta));
140 EXPECT_EQ(expectedMeta, meta);
Patrick Venture6fdd02e2019-05-28 13:02:04 -0700141 expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
Patrick Venture237e2c62019-05-23 20:35:33 -0700142}
143
Patrick Venturebcc0c772019-06-17 10:42:06 -0700144/*
145 * deleteBlob(blob)
146 */
147TEST_F(FirmwareHandlerVerificationStartedTest, DeleteBlobReturnsFalse)
148{
149 /* Try deleting all blobs, it doesn't really matter which though because you
150 * cannot close out an open session, therefore you must fail to delete
151 * anything unless everything is closed.
152 */
153 getToVerificationStarted(staticLayoutBlobId);
154 auto blobs = handler->getBlobIds();
155 for (const auto& b : blobs)
156 {
157 EXPECT_FALSE(handler->deleteBlob(b));
158 }
159}
Patrick Venturea04997b2019-05-24 10:15:48 -0700160
161/*
Patrick Venture237e2c62019-05-23 20:35:33 -0700162 * stat(blob)
Patrick Venturea04997b2019-05-24 10:15:48 -0700163 */
164TEST_F(FirmwareHandlerVerificationStartedTest, StatOnActiveImageReturnsFailure)
165{
166 getToVerificationStarted(staticLayoutBlobId);
Patrick Venture930c7b72019-05-24 11:11:08 -0700167 ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId));
Patrick Venturea04997b2019-05-24 10:15:48 -0700168
169 blobs::BlobMeta meta;
170 EXPECT_FALSE(handler->stat(activeImageBlobId, &meta));
171}
172
173TEST_F(FirmwareHandlerVerificationStartedTest, StatOnActiveHashReturnsFailure)
174{
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700175 getToVerificationStartedWitHashBlob();
Patrick Venture930c7b72019-05-24 11:11:08 -0700176 ASSERT_TRUE(handler->canHandleBlob(activeHashBlobId));
Patrick Venturea04997b2019-05-24 10:15:48 -0700177
178 blobs::BlobMeta meta;
179 EXPECT_FALSE(handler->stat(activeHashBlobId, &meta));
180}
181
182TEST_F(FirmwareHandlerVerificationStartedTest, StatOnVerifyBlobReturnsFailure)
183{
184 /* the verifyBlobId is available starting at verificationPending. */
185 getToVerificationStarted(staticLayoutBlobId);
Patrick Venture930c7b72019-05-24 11:11:08 -0700186 ASSERT_TRUE(handler->canHandleBlob(verifyBlobId));
Patrick Venturea04997b2019-05-24 10:15:48 -0700187
188 blobs::BlobMeta meta;
189 EXPECT_FALSE(handler->stat(verifyBlobId, &meta));
190}
191
192TEST_F(FirmwareHandlerVerificationStartedTest, StatOnNormalBlobsReturnsSuccess)
193{
194 getToVerificationStarted(staticLayoutBlobId);
195
Patrick Venturea04997b2019-05-24 10:15:48 -0700196 std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId};
197 for (const auto& blob : testBlobs)
198 {
Patrick Venture930c7b72019-05-24 11:11:08 -0700199 ASSERT_TRUE(handler->canHandleBlob(blob));
200
Patrick Venturea04997b2019-05-24 10:15:48 -0700201 blobs::BlobMeta meta = {};
202 EXPECT_TRUE(handler->stat(blob, &meta));
Benjamin Fair12901982019-11-12 13:55:46 -0800203 EXPECT_EQ(expectedIdleMeta, meta);
Patrick Venturea04997b2019-05-24 10:15:48 -0700204 }
205}
206
207/*
Patrick Venturefb74ad52019-05-24 15:03:35 -0700208 * writemeta(session)
209 */
210TEST_F(FirmwareHandlerVerificationStartedTest,
211 WriteMetaOnVerifySessionReturnsFailure)
212{
213 getToVerificationStarted(staticLayoutBlobId);
214
215 std::vector<std::uint8_t> bytes = {0x01, 0x02};
216 EXPECT_FALSE(handler->writeMeta(session, 0, bytes));
217}
218
219/*
220 * write(session)
221 */
Patrick Venture9c6de5a2019-05-24 15:12:26 -0700222TEST_F(FirmwareHandlerVerificationStartedTest,
223 WriteOnVerifySessionReturnsFailure)
224{
225 getToVerificationStarted(staticLayoutBlobId);
226
227 std::vector<std::uint8_t> bytes = {0x01, 0x02};
228 EXPECT_FALSE(handler->write(session, 0, bytes));
229}
Patrick Venturefb74ad52019-05-24 15:03:35 -0700230
231/*
Patrick Venture237e2c62019-05-23 20:35:33 -0700232 * open(blob) - there is nothing you can open, this state has an open file.
Patrick Ventured4509102019-05-24 15:22:04 -0700233 */
234TEST_F(FirmwareHandlerVerificationStartedTest,
235 AttemptToOpenImageFileReturnsFailure)
236{
237 /* Attempt to open a file one normally can open, however, as there is
238 * already a file open, this will fail.
239 */
240 getToVerificationStarted(staticLayoutBlobId);
241
Patrick Venture2c014152019-05-28 18:16:05 -0700242 auto blobsToOpen = handler->getBlobIds();
243 for (const auto& blob : blobsToOpen)
244 {
245 EXPECT_FALSE(handler->open(session + 1, flags, blob));
246 }
Patrick Ventured4509102019-05-24 15:22:04 -0700247}
248
249/*
Patrick Venture8facb382019-05-24 15:27:36 -0700250 * read(session)
251 */
252TEST_F(FirmwareHandlerVerificationStartedTest, ReadOfVerifyBlobReturnsEmpty)
253{
254 getToVerificationStarted(staticLayoutBlobId);
Patrick Venture2c014152019-05-28 18:16:05 -0700255 EXPECT_THAT(handler->read(session, 0, 1), IsEmpty());
Patrick Venture8facb382019-05-24 15:27:36 -0700256}
257
258/*
Patrick Ventureddc35072019-05-24 15:30:57 -0700259 * commit(session)
260 */
261TEST_F(FirmwareHandlerVerificationStartedTest,
262 CommitOnVerifyDuringVerificationHasNoImpact)
263{
264 getToVerificationStarted(staticLayoutBlobId);
265 EXPECT_TRUE(handler->commit(session, {}));
Patrick Venture6fdd02e2019-05-28 13:02:04 -0700266 expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
Patrick Ventureddc35072019-05-24 15:30:57 -0700267}
268
269/*
Patrick Venturea04997b2019-05-24 10:15:48 -0700270 * close(session) - close while state if verificationStarted without calling
271 * stat first will abort.
Patrick Venture237e2c62019-05-23 20:35:33 -0700272 */
Patrick Ventured5741022019-06-17 09:08:35 -0700273TEST_F(FirmwareHandlerVerificationStartedTest,
274 CloseOnVerifyDuringVerificationAbortsProcess)
275{
276 getToVerificationStarted(staticLayoutBlobId);
277 EXPECT_CALL(*verifyMockPtr, abort()).Times(1);
278
279 EXPECT_TRUE(handler->close(session));
280
Patrick Ventured5741022019-06-17 09:08:35 -0700281 EXPECT_THAT(handler->getBlobIds(),
Patrick Venture9a69f732019-06-17 14:05:13 -0700282 UnorderedElementsAreArray(startingBlobs));
Patrick Ventured5741022019-06-17 09:08:35 -0700283
284 expectedState(FirmwareBlobHandler::UpdateState::notYetStarted);
285}
Patrick Venture237e2c62019-05-23 20:35:33 -0700286
287} // namespace
288} // namespace ipmi_flash