blob: 3b958c1529a9fdf94bf9696d9480cd1782d43575 [file] [log] [blame]
Patrick Venture61d2ed42019-05-23 18:16:31 -07001/**
2 * The goal of these tests is to verify the behavior of all blob commands given
3 * the current state is verificationPending. This state is achieved as a
4 * transition out of uploadInProgress.
5 */
6#include "firmware_handler.hpp"
7#include "firmware_unittest.hpp"
Patrick Venture01a33272019-05-23 19:48:22 -07008#include "status.hpp"
Patrick Venture61d2ed42019-05-23 18:16:31 -07009#include "util.hpp"
10
Patrick Venture930c7b72019-05-24 11:11:08 -070011#include <algorithm>
Patrick Venture61d2ed42019-05-23 18:16:31 -070012#include <cstdint>
13#include <string>
14#include <vector>
15
16#include <gtest/gtest.h>
17
18namespace ipmi_flash
19{
20namespace
21{
22
Patrick Venture2567ebc2019-05-24 10:02:53 -070023using ::testing::IsEmpty;
Patrick Venture61d2ed42019-05-23 18:16:31 -070024using ::testing::Return;
Patrick Ventureefba42d2019-05-24 10:48:16 -070025using ::testing::UnorderedElementsAreArray;
Patrick Venture61d2ed42019-05-23 18:16:31 -070026
27/*
28 * There are the following calls (parameters may vary):
29 * canHandleBlob(blob)
30 * getBlobIds
31 * deleteBlob(blob)
32 * stat(blob)
33 * stat(session)
34 * open(blob)
35 * close(session)
36 * writemeta(session)
37 * write(session)
38 * read(session)
39 * commit(session)
40 *
41 * Testing canHandleBlob is uninteresting in this state. Getting the BlobIDs
42 * will inform what canHandleBlob will return.
43 */
44
45class FirmwareHandlerVerificationPendingTest : public IpmiOnlyFirmwareStaticTest
Patrick Venture9b37b092020-05-28 20:58:57 -070046{};
Patrick Venture61d2ed42019-05-23 18:16:31 -070047
48/*
49 * getBlobIds
Patrick Venture61d2ed42019-05-23 18:16:31 -070050 */
Patrick Venture61d2ed42019-05-23 18:16:31 -070051TEST_F(FirmwareHandlerVerificationPendingTest, VerifyBlobIdAvailableInState)
52{
53 /* Only in the verificationPending state (and later), should the
Patrick Venture0f82ce42019-05-28 14:06:04 -070054 * verifyBlobId be present.
55 */
Patrick Venture930c7b72019-05-24 11:11:08 -070056 EXPECT_FALSE(handler->canHandleBlob(verifyBlobId));
57
Patrick Venture61d2ed42019-05-23 18:16:31 -070058 getToVerificationPending(staticLayoutBlobId);
Patrick Venture930c7b72019-05-24 11:11:08 -070059
Patrick Venture61d2ed42019-05-23 18:16:31 -070060 EXPECT_TRUE(handler->canHandleBlob(verifyBlobId));
Patrick Ventureb386b862019-05-23 18:42:54 -070061 EXPECT_TRUE(handler->canHandleBlob(activeImageBlobId));
Patrick Venture0f82ce42019-05-28 14:06:04 -070062 EXPECT_FALSE(handler->canHandleBlob(updateBlobId));
Patrick Venture61d2ed42019-05-23 18:16:31 -070063}
64
Patrick Ventureb386b862019-05-23 18:42:54 -070065/*
Patrick Venture2ca66522019-06-17 11:58:38 -070066 * delete(blob)
Patrick Ventureb386b862019-05-23 18:42:54 -070067 */
Patrick Venture2ca66522019-06-17 11:58:38 -070068TEST_F(FirmwareHandlerVerificationPendingTest, DeleteVerifyPendingAbortsProcess)
69{
70 /* It doesn't matter what blob id is used to delete in the design, so just
71 * delete the verify blob id
72 */
73 getToVerificationPending(staticLayoutBlobId);
74
75 EXPECT_CALL(*verifyMockPtr, abort()).Times(0);
76
77 ASSERT_TRUE(handler->canHandleBlob(verifyBlobId));
78 EXPECT_TRUE(handler->deleteBlob(verifyBlobId));
79
80 std::vector<std::string> expectedBlobs = {staticLayoutBlobId, hashBlobId};
81 EXPECT_THAT(handler->getBlobIds(),
82 UnorderedElementsAreArray(expectedBlobs));
83 expectedState(FirmwareBlobHandler::UpdateState::notYetStarted);
84}
85
86TEST_F(FirmwareHandlerVerificationPendingTest, DeleteActiveImageAbortsProcess)
87{
88 getToVerificationPending(staticLayoutBlobId);
89
90 EXPECT_CALL(*verifyMockPtr, abort()).Times(0);
91
92 ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId));
93 EXPECT_TRUE(handler->deleteBlob(activeImageBlobId));
94
95 std::vector<std::string> expectedBlobs = {staticLayoutBlobId, hashBlobId};
96 EXPECT_THAT(handler->getBlobIds(),
97 UnorderedElementsAreArray(expectedBlobs));
98 expectedState(FirmwareBlobHandler::UpdateState::notYetStarted);
99}
100
101TEST_F(FirmwareHandlerVerificationPendingTest, DeleteStaticLayoutAbortsProcess)
102{
103 getToVerificationPending(staticLayoutBlobId);
104
105 EXPECT_CALL(*verifyMockPtr, abort()).Times(0);
106
107 ASSERT_TRUE(handler->canHandleBlob(staticLayoutBlobId));
108 EXPECT_TRUE(handler->deleteBlob(staticLayoutBlobId));
109
110 std::vector<std::string> expectedBlobs = {staticLayoutBlobId, hashBlobId};
111 EXPECT_THAT(handler->getBlobIds(),
112 UnorderedElementsAreArray(expectedBlobs));
113 expectedState(FirmwareBlobHandler::UpdateState::notYetStarted);
114}
115
116TEST_F(FirmwareHandlerVerificationPendingTest, DeleteHashAbortsProcess)
117{
118 getToVerificationPending(staticLayoutBlobId);
119
120 EXPECT_CALL(*verifyMockPtr, abort()).Times(0);
121
122 ASSERT_TRUE(handler->canHandleBlob(hashBlobId));
123 EXPECT_TRUE(handler->deleteBlob(hashBlobId));
124
125 std::vector<std::string> expectedBlobs = {staticLayoutBlobId, hashBlobId};
126 EXPECT_THAT(handler->getBlobIds(),
127 UnorderedElementsAreArray(expectedBlobs));
128 expectedState(FirmwareBlobHandler::UpdateState::notYetStarted);
129}
Patrick Ventureb386b862019-05-23 18:42:54 -0700130
131/*
132 * stat(blob)
133 */
134TEST_F(FirmwareHandlerVerificationPendingTest, StatOnActiveImageReturnsFailure)
135{
136 getToVerificationPending(staticLayoutBlobId);
Patrick Venture930c7b72019-05-24 11:11:08 -0700137 ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId));
Patrick Ventureb386b862019-05-23 18:42:54 -0700138
139 blobs::BlobMeta meta;
140 EXPECT_FALSE(handler->stat(activeImageBlobId, &meta));
141}
142
143TEST_F(FirmwareHandlerVerificationPendingTest, StatOnActiveHashReturnsFailure)
144{
145 getToVerificationPending(hashBlobId);
Patrick Venture930c7b72019-05-24 11:11:08 -0700146 ASSERT_TRUE(handler->canHandleBlob(activeHashBlobId));
Patrick Ventureb386b862019-05-23 18:42:54 -0700147
148 blobs::BlobMeta meta;
149 EXPECT_FALSE(handler->stat(activeHashBlobId, &meta));
150}
151
152TEST_F(FirmwareHandlerVerificationPendingTest,
153 StatOnVerificationBlobReturnsFailure)
154{
Patrick Venture1999eef2019-07-01 11:44:09 -0700155 getToVerificationPending(staticLayoutBlobId);
Patrick Venture0f82ce42019-05-28 14:06:04 -0700156 ASSERT_TRUE(handler->canHandleBlob(verifyBlobId));
Patrick Ventureb386b862019-05-23 18:42:54 -0700157
158 blobs::BlobMeta meta;
159 EXPECT_FALSE(handler->stat(verifyBlobId, &meta));
160}
161
Patrick Venture1999eef2019-07-01 11:44:09 -0700162TEST_F(FirmwareHandlerVerificationPendingTest,
163 VerificationBlobNotFoundWithoutStaticDataAsWell)
164{
165 /* If you only ever open the hash blob id, and never the firmware blob id,
166 * the verify blob isn't added.
167 */
168 getToVerificationPending(hashBlobId);
169 EXPECT_FALSE(handler->canHandleBlob(verifyBlobId));
170}
171
Patrick Ventureb386b862019-05-23 18:42:54 -0700172TEST_F(FirmwareHandlerVerificationPendingTest, StatOnNormalBlobsReturnsSuccess)
173{
174 getToVerificationPending(staticLayoutBlobId);
175
Patrick Ventureb386b862019-05-23 18:42:54 -0700176 std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId};
177 for (const auto& blob : testBlobs)
178 {
Patrick Venture930c7b72019-05-24 11:11:08 -0700179 ASSERT_TRUE(handler->canHandleBlob(blob));
Patrick Ventureb386b862019-05-23 18:42:54 -0700180 blobs::BlobMeta meta = {};
181 EXPECT_TRUE(handler->stat(blob, &meta));
Benjamin Fair12901982019-11-12 13:55:46 -0800182 EXPECT_EQ(expectedIdleMeta, meta);
Patrick Ventureb386b862019-05-23 18:42:54 -0700183 }
184}
185
186/*
Patrick Ventureb386b862019-05-23 18:42:54 -0700187 * open(blob)
188 */
Patrick Ventureda8fcd12019-05-23 18:53:50 -0700189TEST_F(FirmwareHandlerVerificationPendingTest, OpenVerifyBlobSucceeds)
190{
191 getToVerificationPending(staticLayoutBlobId);
192
193 /* the session is safe because it was already closed to get to this state.
194 */
195 EXPECT_TRUE(handler->open(session, flags, verifyBlobId));
196}
197
Patrick Venture0f82ce42019-05-28 14:06:04 -0700198TEST_F(FirmwareHandlerVerificationPendingTest, OpenActiveBlobsFail)
Patrick Ventureda8fcd12019-05-23 18:53:50 -0700199{
200 /* Try opening the active blob Id. This test is equivalent to trying to
201 * open the active hash blob id, in that neither are ever allowed.
202 */
203 getToVerificationPending(staticLayoutBlobId);
204 EXPECT_FALSE(handler->open(session, flags, activeImageBlobId));
Patrick Venture0f82ce42019-05-28 14:06:04 -0700205 EXPECT_FALSE(handler->open(session, flags, activeHashBlobId));
Patrick Ventureda8fcd12019-05-23 18:53:50 -0700206}
207
208TEST_F(FirmwareHandlerVerificationPendingTest,
209 OpenImageBlobTransitionsToUploadInProgress)
210{
211 getToVerificationPending(staticLayoutBlobId);
Patrick Ventureefba42d2019-05-24 10:48:16 -0700212
213 /* Verify the active blob for the image is in the list once to start.
214 * Note: This is truly tested under the notYetStarted::open() test.
215 */
216 std::vector<std::string> expectedBlobs = {staticLayoutBlobId, hashBlobId,
217 verifyBlobId, activeImageBlobId};
218
219 EXPECT_THAT(handler->getBlobIds(),
220 UnorderedElementsAreArray(expectedBlobs));
221
Patrick Venture6d7735d2019-06-21 10:03:19 -0700222 /* Verifies it isn't triggered again. */
223 EXPECT_CALL(*prepareMockPtr, trigger()).Times(0);
224
Patrick Ventured4e20de2019-07-18 12:48:05 -0700225 EXPECT_CALL(*imageMock2, open(staticLayoutBlobId)).WillOnce(Return(true));
Patrick Ventureda8fcd12019-05-23 18:53:50 -0700226 EXPECT_TRUE(handler->open(session, flags, staticLayoutBlobId));
Patrick Venture6fdd02e2019-05-28 13:02:04 -0700227 expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
Patrick Ventureefba42d2019-05-24 10:48:16 -0700228
Patrick Venture930c7b72019-05-24 11:11:08 -0700229 expectedBlobs.erase(
Patrick Venture6fdd02e2019-05-28 13:02:04 -0700230 std::remove(expectedBlobs.begin(), expectedBlobs.end(), verifyBlobId),
231 expectedBlobs.end());
Patrick Venture930c7b72019-05-24 11:11:08 -0700232
233 /* Verify the active blob ID was not added to the list twice and
234 * verifyBlobId is removed
235 */
Patrick Ventureefba42d2019-05-24 10:48:16 -0700236 EXPECT_THAT(handler->getBlobIds(),
237 UnorderedElementsAreArray(expectedBlobs));
Patrick Ventureda8fcd12019-05-23 18:53:50 -0700238}
239
240/*
Patrick Venture1e389c92019-05-23 19:15:05 -0700241 * close(session)
242 */
243TEST_F(FirmwareHandlerVerificationPendingTest,
Patrick Venture6fdd02e2019-05-28 13:02:04 -0700244 ClosingVerifyBlobWithoutCommitDoesNotChangeState)
Patrick Venture1e389c92019-05-23 19:15:05 -0700245{
Patrick Venture6fdd02e2019-05-28 13:02:04 -0700246 /* commit() will change the state, closing post-commit is part of
247 * verificationStarted testing.
248 */
Patrick Venture1e389c92019-05-23 19:15:05 -0700249 getToVerificationPending(staticLayoutBlobId);
250 EXPECT_TRUE(handler->open(session, flags, verifyBlobId));
Patrick Venture6fdd02e2019-05-28 13:02:04 -0700251 expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
Patrick Venture1e389c92019-05-23 19:15:05 -0700252
253 handler->close(session);
Patrick Venture6fdd02e2019-05-28 13:02:04 -0700254 expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
Patrick Venture1e389c92019-05-23 19:15:05 -0700255}
256
257/*
Patrick Venture19044e12019-05-23 19:30:28 -0700258 * commit(session)
259 */
260TEST_F(FirmwareHandlerVerificationPendingTest,
261 CommitOnVerifyBlobTriggersVerificationAndStateTransition)
262{
263 getToVerificationPending(staticLayoutBlobId);
264 EXPECT_TRUE(handler->open(session, flags, verifyBlobId));
Patrick Venture1d66fe62019-06-03 14:57:27 -0700265 EXPECT_CALL(*verifyMockPtr, trigger()).WillOnce(Return(true));
Patrick Venture19044e12019-05-23 19:30:28 -0700266
267 EXPECT_TRUE(handler->commit(session, {}));
Patrick Venture6fdd02e2019-05-28 13:02:04 -0700268 expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
Patrick Venture19044e12019-05-23 19:30:28 -0700269}
270
271/*
Patrick Ventureda8fcd12019-05-23 18:53:50 -0700272 * stat(session) - in this state, you can only open(verifyBlobId) without
273 * changing state.
274 */
Patrick Venture01a33272019-05-23 19:48:22 -0700275TEST_F(FirmwareHandlerVerificationPendingTest, StatOnVerifyBlobIdReturnsState)
276{
277 /* If this is called before commit(), it's still verificationPending, so it
278 * just returns the state is other
279 */
280 getToVerificationPending(staticLayoutBlobId);
281 EXPECT_TRUE(handler->open(session, flags, verifyBlobId));
Patrick Venture1d66fe62019-06-03 14:57:27 -0700282 EXPECT_CALL(*verifyMockPtr, trigger()).Times(0);
Patrick Venturef1f0f652019-06-03 09:10:19 -0700283 EXPECT_CALL(*verifyMockPtr, status()).Times(0);
Patrick Venture01a33272019-05-23 19:48:22 -0700284
285 blobs::BlobMeta meta, expectedMeta = {};
286 expectedMeta.size = 0;
287 expectedMeta.blobState = flags;
288 expectedMeta.metadata.push_back(
Patrick Ventureda66fd82019-06-03 11:11:24 -0700289 static_cast<std::uint8_t>(ActionStatus::unknown));
Patrick Venture01a33272019-05-23 19:48:22 -0700290
291 EXPECT_TRUE(handler->stat(session, &meta));
292 EXPECT_EQ(expectedMeta, meta);
293}
294
Patrick Ventureb386b862019-05-23 18:42:54 -0700295/*
Patrick Ventureb386b862019-05-23 18:42:54 -0700296 * writemeta(session)
297 */
Patrick Ventureb611a082019-05-23 20:27:28 -0700298TEST_F(FirmwareHandlerVerificationPendingTest, WriteMetaAgainstVerifyFails)
299{
300 /* The verifyBlobId has no data handler, which means write meta fails. */
301 getToVerificationPending(staticLayoutBlobId);
302
303 EXPECT_TRUE(handler->open(session, flags, verifyBlobId));
304
305 std::vector<std::uint8_t> bytes = {0x01, 0x02};
306 EXPECT_FALSE(handler->writeMeta(session, 0, bytes));
307}
308
Patrick Ventureb386b862019-05-23 18:42:54 -0700309/*
310 * write(session)
311 */
Patrick Ventureab731e92019-05-24 09:58:00 -0700312TEST_F(FirmwareHandlerVerificationPendingTest, WriteAgainstVerifyBlobIdFails)
313{
314 getToVerificationPending(staticLayoutBlobId);
315
316 EXPECT_TRUE(handler->open(session, flags, verifyBlobId));
317
318 std::vector<std::uint8_t> bytes = {0x01, 0x02};
319 EXPECT_FALSE(handler->write(session, 0, bytes));
320}
321
Patrick Ventureb386b862019-05-23 18:42:54 -0700322/*
323 * read(session)
324 */
Patrick Venture2567ebc2019-05-24 10:02:53 -0700325TEST_F(FirmwareHandlerVerificationPendingTest,
326 ReadAgainstVerifyBlobIdReturnsEmpty)
327{
328 getToVerificationPending(staticLayoutBlobId);
329
330 EXPECT_TRUE(handler->open(session, flags, verifyBlobId));
Patrick Venture0f82ce42019-05-28 14:06:04 -0700331 EXPECT_THAT(handler->read(session, 0, 1), IsEmpty());
Patrick Venture2567ebc2019-05-24 10:02:53 -0700332}
Patrick Ventureb386b862019-05-23 18:42:54 -0700333
Patrick Venture61d2ed42019-05-23 18:16:31 -0700334} // namespace
335} // namespace ipmi_flash