blob: 86b10f57d9c53a63892e9c34b81aa0101e4260d6 [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,
109 StatOnVerifyBlobIdAfterCommitCheckStateAndReturnsFailed)
110{
111 /* If the returned state from the verification handler is failed, it sets
112 * commit_error and transitions to verificationCompleted.
113 */
114 getToVerificationStarted(staticLayoutBlobId);
115 EXPECT_CALL(*verifyMockPtr, checkVerificationState())
116 .WillOnce(Return(VerifyCheckResponses::failed));
117
118 blobs::BlobMeta meta, expectedMeta = {};
119 expectedMeta.size = 0;
120 expectedMeta.blobState = flags | blobs::StateFlags::commit_error;
121 expectedMeta.metadata.push_back(
122 static_cast<std::uint8_t>(VerifyCheckResponses::failed));
123
Patrick Venture237e2c62019-05-23 20:35:33 -0700124 EXPECT_TRUE(handler->stat(session, &meta));
125 EXPECT_EQ(expectedMeta, meta);
Patrick Venture6fdd02e2019-05-28 13:02:04 -0700126 expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
Patrick Venture237e2c62019-05-23 20:35:33 -0700127}
128
129TEST_F(FirmwareHandlerVerificationStartedTest,
130 StatOnVerifyBlobIdAfterCommitCheckStateAndReturnsSuccess)
131{
132 /* If the returned state from the verification handler is success, it sets
133 * committed and transitions to verificationCompleted.
134 */
135 getToVerificationStarted(staticLayoutBlobId);
136 EXPECT_CALL(*verifyMockPtr, checkVerificationState())
137 .WillOnce(Return(VerifyCheckResponses::success));
138
139 blobs::BlobMeta meta, expectedMeta = {};
140 expectedMeta.size = 0;
141 expectedMeta.blobState = flags | blobs::StateFlags::committed;
142 expectedMeta.metadata.push_back(
143 static_cast<std::uint8_t>(VerifyCheckResponses::success));
144
Patrick Venture237e2c62019-05-23 20:35:33 -0700145 EXPECT_TRUE(handler->stat(session, &meta));
146 EXPECT_EQ(expectedMeta, meta);
Patrick Venture6fdd02e2019-05-28 13:02:04 -0700147 expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
Patrick Venture237e2c62019-05-23 20:35:33 -0700148}
149
150/* TODO: Once verificationCompleted is the state, canHandleBlob should accept
151 * updateBlobId.
152 */
153
Patrick Venture2c014152019-05-28 18:16:05 -0700154/* TODO: deleteBlob(blob) */
Patrick Venturea04997b2019-05-24 10:15:48 -0700155
156/*
Patrick Venture237e2c62019-05-23 20:35:33 -0700157 * stat(blob)
Patrick Venturea04997b2019-05-24 10:15:48 -0700158 */
159TEST_F(FirmwareHandlerVerificationStartedTest, StatOnActiveImageReturnsFailure)
160{
161 getToVerificationStarted(staticLayoutBlobId);
Patrick Venture930c7b72019-05-24 11:11:08 -0700162 ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId));
Patrick Venturea04997b2019-05-24 10:15:48 -0700163
164 blobs::BlobMeta meta;
165 EXPECT_FALSE(handler->stat(activeImageBlobId, &meta));
166}
167
168TEST_F(FirmwareHandlerVerificationStartedTest, StatOnActiveHashReturnsFailure)
169{
Patrick Venture930c7b72019-05-24 11:11:08 -0700170 getToVerificationStarted(hashBlobId);
171 ASSERT_TRUE(handler->canHandleBlob(activeHashBlobId));
Patrick Venturea04997b2019-05-24 10:15:48 -0700172
173 blobs::BlobMeta meta;
174 EXPECT_FALSE(handler->stat(activeHashBlobId, &meta));
175}
176
177TEST_F(FirmwareHandlerVerificationStartedTest, StatOnVerifyBlobReturnsFailure)
178{
179 /* the verifyBlobId is available starting at verificationPending. */
180 getToVerificationStarted(staticLayoutBlobId);
Patrick Venture930c7b72019-05-24 11:11:08 -0700181 ASSERT_TRUE(handler->canHandleBlob(verifyBlobId));
Patrick Venturea04997b2019-05-24 10:15:48 -0700182
183 blobs::BlobMeta meta;
184 EXPECT_FALSE(handler->stat(verifyBlobId, &meta));
185}
186
187TEST_F(FirmwareHandlerVerificationStartedTest, StatOnNormalBlobsReturnsSuccess)
188{
189 getToVerificationStarted(staticLayoutBlobId);
190
191 blobs::BlobMeta expected;
192 expected.blobState = FirmwareBlobHandler::UpdateFlags::ipmi;
193 expected.size = 0;
194
195 std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId};
196 for (const auto& blob : testBlobs)
197 {
Patrick Venture930c7b72019-05-24 11:11:08 -0700198 ASSERT_TRUE(handler->canHandleBlob(blob));
199
Patrick Venturea04997b2019-05-24 10:15:48 -0700200 blobs::BlobMeta meta = {};
201 EXPECT_TRUE(handler->stat(blob, &meta));
202 EXPECT_EQ(expected, meta);
203 }
204}
205
206/*
Patrick Venturefb74ad52019-05-24 15:03:35 -0700207 * writemeta(session)
208 */
209TEST_F(FirmwareHandlerVerificationStartedTest,
210 WriteMetaOnVerifySessionReturnsFailure)
211{
212 getToVerificationStarted(staticLayoutBlobId);
213
214 std::vector<std::uint8_t> bytes = {0x01, 0x02};
215 EXPECT_FALSE(handler->writeMeta(session, 0, bytes));
216}
217
218/*
219 * write(session)
220 */
Patrick Venture9c6de5a2019-05-24 15:12:26 -0700221TEST_F(FirmwareHandlerVerificationStartedTest,
222 WriteOnVerifySessionReturnsFailure)
223{
224 getToVerificationStarted(staticLayoutBlobId);
225
226 std::vector<std::uint8_t> bytes = {0x01, 0x02};
227 EXPECT_FALSE(handler->write(session, 0, bytes));
228}
Patrick Venturefb74ad52019-05-24 15:03:35 -0700229
230/*
Patrick Venture237e2c62019-05-23 20:35:33 -0700231 * open(blob) - there is nothing you can open, this state has an open file.
Patrick Ventured4509102019-05-24 15:22:04 -0700232 */
233TEST_F(FirmwareHandlerVerificationStartedTest,
234 AttemptToOpenImageFileReturnsFailure)
235{
236 /* Attempt to open a file one normally can open, however, as there is
237 * already a file open, this will fail.
238 */
239 getToVerificationStarted(staticLayoutBlobId);
240
Patrick Venture2c014152019-05-28 18:16:05 -0700241 auto blobsToOpen = handler->getBlobIds();
242 for (const auto& blob : blobsToOpen)
243 {
244 EXPECT_FALSE(handler->open(session + 1, flags, blob));
245 }
Patrick Ventured4509102019-05-24 15:22:04 -0700246}
247
248/*
Patrick Venture8facb382019-05-24 15:27:36 -0700249 * read(session)
250 */
251TEST_F(FirmwareHandlerVerificationStartedTest, ReadOfVerifyBlobReturnsEmpty)
252{
253 getToVerificationStarted(staticLayoutBlobId);
Patrick Venture2c014152019-05-28 18:16:05 -0700254 EXPECT_THAT(handler->read(session, 0, 1), IsEmpty());
Patrick Venture8facb382019-05-24 15:27:36 -0700255}
256
257/*
Patrick Ventureddc35072019-05-24 15:30:57 -0700258 * commit(session)
259 */
260TEST_F(FirmwareHandlerVerificationStartedTest,
261 CommitOnVerifyDuringVerificationHasNoImpact)
262{
263 getToVerificationStarted(staticLayoutBlobId);
264 EXPECT_TRUE(handler->commit(session, {}));
Patrick Venture6fdd02e2019-05-28 13:02:04 -0700265 expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
Patrick Ventureddc35072019-05-24 15:30:57 -0700266}
267
268/*
Patrick Venturea04997b2019-05-24 10:15:48 -0700269 * close(session) - close while state if verificationStarted without calling
270 * stat first will abort.
Patrick Venture35b3fc92019-05-24 15:36:01 -0700271 * TODO: implement this test when we implement abort.
Patrick Venture237e2c62019-05-23 20:35:33 -0700272 */
273
274} // namespace
275} // namespace ipmi_flash