blob: 1e73c987c95d7439083bd175f86f0671edbd9998 [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{
45 protected:
46 void getToVerificationStarted(const std::string& blobId)
47 {
Patrick Venture237e2c62019-05-23 20:35:33 -070048 EXPECT_CALL(imageMock, open(blobId)).WillOnce(Return(true));
49 EXPECT_TRUE(handler->open(session, flags, blobId));
Patrick Venture6fdd02e2019-05-28 13:02:04 -070050 expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
51
Patrick Venture237e2c62019-05-23 20:35:33 -070052 EXPECT_CALL(imageMock, close()).WillRepeatedly(Return());
53 handler->close(session);
Patrick Venture6fdd02e2019-05-28 13:02:04 -070054 expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
Patrick Venture237e2c62019-05-23 20:35:33 -070055
56 EXPECT_TRUE(handler->open(session, flags, verifyBlobId));
57 EXPECT_CALL(*verifyMockPtr, triggerVerification())
58 .WillOnce(Return(true));
59
60 EXPECT_TRUE(handler->commit(session, {}));
Patrick Venture6fdd02e2019-05-28 13:02:04 -070061 expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
Patrick Venture237e2c62019-05-23 20:35:33 -070062 }
63
64 std::uint16_t session = 1;
65 std::uint16_t flags =
66 blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi;
67};
68
69/*
Patrick Venture2c014152019-05-28 18:16:05 -070070 * canHandleBlob(blob)
71 * getBlobIds()
72 */
73TEST_F(FirmwareHandlerVerificationStartedTest, GetBlobIdsReturnsExpectedList)
74{
75 getToVerificationStarted(staticLayoutBlobId);
76
77 std::vector<std::string> expectedList = {
78 activeImageBlobId, staticLayoutBlobId, hashBlobId, verifyBlobId};
79
80 EXPECT_THAT(handler->getBlobIds(), UnorderedElementsAreArray(expectedList));
81
82 for (const auto& blob : expectedList)
83 {
84 EXPECT_TRUE(handler->canHandleBlob(blob));
85 }
86}
87
88/*
Patrick Venture237e2c62019-05-23 20:35:33 -070089 * stat(session)
90 */
91TEST_F(FirmwareHandlerVerificationStartedTest,
92 StatOnVerifyBlobIdAfterCommitChecksStateAndReturnsRunning)
93{
94 getToVerificationStarted(staticLayoutBlobId);
95 EXPECT_CALL(*verifyMockPtr, checkVerificationState())
96 .WillOnce(Return(VerifyCheckResponses::running));
97
98 blobs::BlobMeta meta, expectedMeta = {};
99 expectedMeta.size = 0;
100 expectedMeta.blobState = flags | blobs::StateFlags::committing;
101 expectedMeta.metadata.push_back(
102 static_cast<std::uint8_t>(VerifyCheckResponses::running));
103
104 EXPECT_TRUE(handler->stat(session, &meta));
105 EXPECT_EQ(expectedMeta, meta);
106}
107
108TEST_F(FirmwareHandlerVerificationStartedTest,
Patrick Ventured52c2c32019-05-30 09:54:35 -0700109 StatOnVerifyBlobIdAfterCommitChecksStateAndReturnsOther)
110{
111 getToVerificationStarted(staticLayoutBlobId);
112 EXPECT_CALL(*verifyMockPtr, checkVerificationState())
113 .WillOnce(Return(VerifyCheckResponses::other));
114
115 blobs::BlobMeta meta, expectedMeta = {};
116 expectedMeta.size = 0;
117 expectedMeta.blobState = flags | blobs::StateFlags::committing;
118 expectedMeta.metadata.push_back(
119 static_cast<std::uint8_t>(VerifyCheckResponses::other));
120
121 EXPECT_TRUE(handler->stat(session, &meta));
122 EXPECT_EQ(expectedMeta, meta);
123}
124
125TEST_F(FirmwareHandlerVerificationStartedTest,
Patrick Venture237e2c62019-05-23 20:35:33 -0700126 StatOnVerifyBlobIdAfterCommitCheckStateAndReturnsFailed)
127{
128 /* If the returned state from the verification handler is failed, it sets
129 * commit_error and transitions to verificationCompleted.
130 */
131 getToVerificationStarted(staticLayoutBlobId);
132 EXPECT_CALL(*verifyMockPtr, checkVerificationState())
133 .WillOnce(Return(VerifyCheckResponses::failed));
134
135 blobs::BlobMeta meta, expectedMeta = {};
136 expectedMeta.size = 0;
137 expectedMeta.blobState = flags | blobs::StateFlags::commit_error;
138 expectedMeta.metadata.push_back(
139 static_cast<std::uint8_t>(VerifyCheckResponses::failed));
140
Patrick Venture237e2c62019-05-23 20:35:33 -0700141 EXPECT_TRUE(handler->stat(session, &meta));
142 EXPECT_EQ(expectedMeta, meta);
Patrick Venture6fdd02e2019-05-28 13:02:04 -0700143 expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
Patrick Venture237e2c62019-05-23 20:35:33 -0700144}
145
146TEST_F(FirmwareHandlerVerificationStartedTest,
147 StatOnVerifyBlobIdAfterCommitCheckStateAndReturnsSuccess)
148{
149 /* If the returned state from the verification handler is success, it sets
150 * committed and transitions to verificationCompleted.
151 */
152 getToVerificationStarted(staticLayoutBlobId);
153 EXPECT_CALL(*verifyMockPtr, checkVerificationState())
154 .WillOnce(Return(VerifyCheckResponses::success));
155
156 blobs::BlobMeta meta, expectedMeta = {};
157 expectedMeta.size = 0;
158 expectedMeta.blobState = flags | blobs::StateFlags::committed;
159 expectedMeta.metadata.push_back(
160 static_cast<std::uint8_t>(VerifyCheckResponses::success));
161
Patrick Venture237e2c62019-05-23 20:35:33 -0700162 EXPECT_TRUE(handler->stat(session, &meta));
163 EXPECT_EQ(expectedMeta, meta);
Patrick Venture6fdd02e2019-05-28 13:02:04 -0700164 expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
Patrick Venture237e2c62019-05-23 20:35:33 -0700165}
166
167/* TODO: Once verificationCompleted is the state, canHandleBlob should accept
168 * updateBlobId.
169 */
170
Patrick Venture2c014152019-05-28 18:16:05 -0700171/* TODO: deleteBlob(blob) */
Patrick Venturea04997b2019-05-24 10:15:48 -0700172
173/*
Patrick Venture237e2c62019-05-23 20:35:33 -0700174 * stat(blob)
Patrick Venturea04997b2019-05-24 10:15:48 -0700175 */
176TEST_F(FirmwareHandlerVerificationStartedTest, StatOnActiveImageReturnsFailure)
177{
178 getToVerificationStarted(staticLayoutBlobId);
Patrick Venture930c7b72019-05-24 11:11:08 -0700179 ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId));
Patrick Venturea04997b2019-05-24 10:15:48 -0700180
181 blobs::BlobMeta meta;
182 EXPECT_FALSE(handler->stat(activeImageBlobId, &meta));
183}
184
185TEST_F(FirmwareHandlerVerificationStartedTest, StatOnActiveHashReturnsFailure)
186{
Patrick Venture930c7b72019-05-24 11:11:08 -0700187 getToVerificationStarted(hashBlobId);
188 ASSERT_TRUE(handler->canHandleBlob(activeHashBlobId));
Patrick Venturea04997b2019-05-24 10:15:48 -0700189
190 blobs::BlobMeta meta;
191 EXPECT_FALSE(handler->stat(activeHashBlobId, &meta));
192}
193
194TEST_F(FirmwareHandlerVerificationStartedTest, StatOnVerifyBlobReturnsFailure)
195{
196 /* the verifyBlobId is available starting at verificationPending. */
197 getToVerificationStarted(staticLayoutBlobId);
Patrick Venture930c7b72019-05-24 11:11:08 -0700198 ASSERT_TRUE(handler->canHandleBlob(verifyBlobId));
Patrick Venturea04997b2019-05-24 10:15:48 -0700199
200 blobs::BlobMeta meta;
201 EXPECT_FALSE(handler->stat(verifyBlobId, &meta));
202}
203
204TEST_F(FirmwareHandlerVerificationStartedTest, StatOnNormalBlobsReturnsSuccess)
205{
206 getToVerificationStarted(staticLayoutBlobId);
207
208 blobs::BlobMeta expected;
209 expected.blobState = FirmwareBlobHandler::UpdateFlags::ipmi;
210 expected.size = 0;
211
212 std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId};
213 for (const auto& blob : testBlobs)
214 {
Patrick Venture930c7b72019-05-24 11:11:08 -0700215 ASSERT_TRUE(handler->canHandleBlob(blob));
216
Patrick Venturea04997b2019-05-24 10:15:48 -0700217 blobs::BlobMeta meta = {};
218 EXPECT_TRUE(handler->stat(blob, &meta));
219 EXPECT_EQ(expected, meta);
220 }
221}
222
223/*
Patrick Venturefb74ad52019-05-24 15:03:35 -0700224 * writemeta(session)
225 */
226TEST_F(FirmwareHandlerVerificationStartedTest,
227 WriteMetaOnVerifySessionReturnsFailure)
228{
229 getToVerificationStarted(staticLayoutBlobId);
230
231 std::vector<std::uint8_t> bytes = {0x01, 0x02};
232 EXPECT_FALSE(handler->writeMeta(session, 0, bytes));
233}
234
235/*
236 * write(session)
237 */
Patrick Venture9c6de5a2019-05-24 15:12:26 -0700238TEST_F(FirmwareHandlerVerificationStartedTest,
239 WriteOnVerifySessionReturnsFailure)
240{
241 getToVerificationStarted(staticLayoutBlobId);
242
243 std::vector<std::uint8_t> bytes = {0x01, 0x02};
244 EXPECT_FALSE(handler->write(session, 0, bytes));
245}
Patrick Venturefb74ad52019-05-24 15:03:35 -0700246
247/*
Patrick Venture237e2c62019-05-23 20:35:33 -0700248 * open(blob) - there is nothing you can open, this state has an open file.
Patrick Ventured4509102019-05-24 15:22:04 -0700249 */
250TEST_F(FirmwareHandlerVerificationStartedTest,
251 AttemptToOpenImageFileReturnsFailure)
252{
253 /* Attempt to open a file one normally can open, however, as there is
254 * already a file open, this will fail.
255 */
256 getToVerificationStarted(staticLayoutBlobId);
257
Patrick Venture2c014152019-05-28 18:16:05 -0700258 auto blobsToOpen = handler->getBlobIds();
259 for (const auto& blob : blobsToOpen)
260 {
261 EXPECT_FALSE(handler->open(session + 1, flags, blob));
262 }
Patrick Ventured4509102019-05-24 15:22:04 -0700263}
264
265/*
Patrick Venture8facb382019-05-24 15:27:36 -0700266 * read(session)
267 */
268TEST_F(FirmwareHandlerVerificationStartedTest, ReadOfVerifyBlobReturnsEmpty)
269{
270 getToVerificationStarted(staticLayoutBlobId);
Patrick Venture2c014152019-05-28 18:16:05 -0700271 EXPECT_THAT(handler->read(session, 0, 1), IsEmpty());
Patrick Venture8facb382019-05-24 15:27:36 -0700272}
273
274/*
Patrick Ventureddc35072019-05-24 15:30:57 -0700275 * commit(session)
276 */
277TEST_F(FirmwareHandlerVerificationStartedTest,
278 CommitOnVerifyDuringVerificationHasNoImpact)
279{
280 getToVerificationStarted(staticLayoutBlobId);
281 EXPECT_TRUE(handler->commit(session, {}));
Patrick Venture6fdd02e2019-05-28 13:02:04 -0700282 expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
Patrick Ventureddc35072019-05-24 15:30:57 -0700283}
284
285/*
Patrick Venturea04997b2019-05-24 10:15:48 -0700286 * close(session) - close while state if verificationStarted without calling
287 * stat first will abort.
Patrick Venture35b3fc92019-05-24 15:36:01 -0700288 * TODO: implement this test when we implement abort.
Patrick Venture237e2c62019-05-23 20:35:33 -0700289 */
290
291} // namespace
292} // namespace ipmi_flash