blob: 4b455c66f31f01744cb2e8356748811ab1825f42 [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;
23
24/*
25 * There are the following calls (parameters may vary):
26 * canHandleBlob(blob)
27 * getBlobIds
28 * deleteBlob(blob)
29 * stat(blob)
30 * stat(session)
31 * open(blob)
32 * close(session)
33 * writemeta(session)
34 * write(session)
35 * read(session)
36 * commit(session)
37 *
38 * Testing canHandleBlob is uninteresting in this state. Getting the BlobIDs
39 * will inform what canHandleBlob will return.
40 */
41
42class FirmwareHandlerVerificationStartedTest : public IpmiOnlyFirmwareStaticTest
43{
44 protected:
45 void getToVerificationStarted(const std::string& blobId)
46 {
47 auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
48 EXPECT_CALL(imageMock, open(blobId)).WillOnce(Return(true));
49 EXPECT_TRUE(handler->open(session, flags, blobId));
50 EXPECT_EQ(FirmwareBlobHandler::UpdateState::uploadInProgress,
51 realHandler->getCurrentState());
52 EXPECT_CALL(imageMock, close()).WillRepeatedly(Return());
53 handler->close(session);
54 EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationPending,
55 realHandler->getCurrentState());
56
57 EXPECT_TRUE(handler->open(session, flags, verifyBlobId));
58 EXPECT_CALL(*verifyMockPtr, triggerVerification())
59 .WillOnce(Return(true));
60
61 EXPECT_TRUE(handler->commit(session, {}));
62 EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationStarted,
63 realHandler->getCurrentState());
64 }
65
66 std::uint16_t session = 1;
67 std::uint16_t flags =
68 blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi;
69};
70
71/*
72 * stat(session)
73 */
74TEST_F(FirmwareHandlerVerificationStartedTest,
75 StatOnVerifyBlobIdAfterCommitChecksStateAndReturnsRunning)
76{
77 getToVerificationStarted(staticLayoutBlobId);
78 EXPECT_CALL(*verifyMockPtr, checkVerificationState())
79 .WillOnce(Return(VerifyCheckResponses::running));
80
81 blobs::BlobMeta meta, expectedMeta = {};
82 expectedMeta.size = 0;
83 expectedMeta.blobState = flags | blobs::StateFlags::committing;
84 expectedMeta.metadata.push_back(
85 static_cast<std::uint8_t>(VerifyCheckResponses::running));
86
87 EXPECT_TRUE(handler->stat(session, &meta));
88 EXPECT_EQ(expectedMeta, meta);
89}
90
91TEST_F(FirmwareHandlerVerificationStartedTest,
92 StatOnVerifyBlobIdAfterCommitCheckStateAndReturnsFailed)
93{
94 /* If the returned state from the verification handler is failed, it sets
95 * commit_error and transitions to verificationCompleted.
96 */
97 getToVerificationStarted(staticLayoutBlobId);
98 EXPECT_CALL(*verifyMockPtr, checkVerificationState())
99 .WillOnce(Return(VerifyCheckResponses::failed));
100
101 blobs::BlobMeta meta, expectedMeta = {};
102 expectedMeta.size = 0;
103 expectedMeta.blobState = flags | blobs::StateFlags::commit_error;
104 expectedMeta.metadata.push_back(
105 static_cast<std::uint8_t>(VerifyCheckResponses::failed));
106
107 auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
108 EXPECT_TRUE(handler->stat(session, &meta));
109 EXPECT_EQ(expectedMeta, meta);
110
111 EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationCompleted,
112 realHandler->getCurrentState());
113}
114
115TEST_F(FirmwareHandlerVerificationStartedTest,
116 StatOnVerifyBlobIdAfterCommitCheckStateAndReturnsSuccess)
117{
118 /* If the returned state from the verification handler is success, it sets
119 * committed and transitions to verificationCompleted.
120 */
121 getToVerificationStarted(staticLayoutBlobId);
122 EXPECT_CALL(*verifyMockPtr, checkVerificationState())
123 .WillOnce(Return(VerifyCheckResponses::success));
124
125 blobs::BlobMeta meta, expectedMeta = {};
126 expectedMeta.size = 0;
127 expectedMeta.blobState = flags | blobs::StateFlags::committed;
128 expectedMeta.metadata.push_back(
129 static_cast<std::uint8_t>(VerifyCheckResponses::success));
130
131 auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
132 EXPECT_TRUE(handler->stat(session, &meta));
133 EXPECT_EQ(expectedMeta, meta);
134
135 EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationCompleted,
136 realHandler->getCurrentState());
137}
138
139/* TODO: Once verificationCompleted is the state, canHandleBlob should accept
140 * updateBlobId.
141 */
142
143/* TODO:
144 * canHandleBlob(blob)
Patrick Venturea04997b2019-05-24 10:15:48 -0700145 *
Patrick Venture237e2c62019-05-23 20:35:33 -0700146 * getBlobIds
Patrick Venturea04997b2019-05-24 10:15:48 -0700147 *
Patrick Venture237e2c62019-05-23 20:35:33 -0700148 * deleteBlob(blob)
Patrick Venturea04997b2019-05-24 10:15:48 -0700149 */
150
151/*
Patrick Venture237e2c62019-05-23 20:35:33 -0700152 * stat(blob)
Patrick Venturea04997b2019-05-24 10:15:48 -0700153 */
154TEST_F(FirmwareHandlerVerificationStartedTest, StatOnActiveImageReturnsFailure)
155{
156 getToVerificationStarted(staticLayoutBlobId);
Patrick Venture930c7b72019-05-24 11:11:08 -0700157 ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId));
Patrick Venturea04997b2019-05-24 10:15:48 -0700158
159 blobs::BlobMeta meta;
160 EXPECT_FALSE(handler->stat(activeImageBlobId, &meta));
161}
162
163TEST_F(FirmwareHandlerVerificationStartedTest, StatOnActiveHashReturnsFailure)
164{
Patrick Venture930c7b72019-05-24 11:11:08 -0700165 getToVerificationStarted(hashBlobId);
166 ASSERT_TRUE(handler->canHandleBlob(activeHashBlobId));
Patrick Venturea04997b2019-05-24 10:15:48 -0700167
168 blobs::BlobMeta meta;
169 EXPECT_FALSE(handler->stat(activeHashBlobId, &meta));
170}
171
172TEST_F(FirmwareHandlerVerificationStartedTest, StatOnVerifyBlobReturnsFailure)
173{
174 /* the verifyBlobId is available starting at verificationPending. */
175 getToVerificationStarted(staticLayoutBlobId);
Patrick Venture930c7b72019-05-24 11:11:08 -0700176 ASSERT_TRUE(handler->canHandleBlob(verifyBlobId));
Patrick Venturea04997b2019-05-24 10:15:48 -0700177
178 blobs::BlobMeta meta;
179 EXPECT_FALSE(handler->stat(verifyBlobId, &meta));
180}
181
182TEST_F(FirmwareHandlerVerificationStartedTest, StatOnNormalBlobsReturnsSuccess)
183{
184 getToVerificationStarted(staticLayoutBlobId);
185
186 blobs::BlobMeta expected;
187 expected.blobState = FirmwareBlobHandler::UpdateFlags::ipmi;
188 expected.size = 0;
189
190 std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId};
191 for (const auto& blob : testBlobs)
192 {
Patrick Venture930c7b72019-05-24 11:11:08 -0700193 ASSERT_TRUE(handler->canHandleBlob(blob));
194
Patrick Venturea04997b2019-05-24 10:15:48 -0700195 blobs::BlobMeta meta = {};
196 EXPECT_TRUE(handler->stat(blob, &meta));
197 EXPECT_EQ(expected, meta);
198 }
199}
200
201/*
Patrick Venturefb74ad52019-05-24 15:03:35 -0700202 * writemeta(session)
203 */
204TEST_F(FirmwareHandlerVerificationStartedTest,
205 WriteMetaOnVerifySessionReturnsFailure)
206{
207 getToVerificationStarted(staticLayoutBlobId);
208
209 std::vector<std::uint8_t> bytes = {0x01, 0x02};
210 EXPECT_FALSE(handler->writeMeta(session, 0, bytes));
211}
212
213/*
214 * write(session)
215 */
Patrick Venture9c6de5a2019-05-24 15:12:26 -0700216TEST_F(FirmwareHandlerVerificationStartedTest,
217 WriteOnVerifySessionReturnsFailure)
218{
219 getToVerificationStarted(staticLayoutBlobId);
220
221 std::vector<std::uint8_t> bytes = {0x01, 0x02};
222 EXPECT_FALSE(handler->write(session, 0, bytes));
223}
Patrick Venturefb74ad52019-05-24 15:03:35 -0700224
225/*
Patrick Venture237e2c62019-05-23 20:35:33 -0700226 * open(blob) - there is nothing you can open, this state has an open file.
Patrick Ventured4509102019-05-24 15:22:04 -0700227 */
228TEST_F(FirmwareHandlerVerificationStartedTest,
229 AttemptToOpenImageFileReturnsFailure)
230{
231 /* Attempt to open a file one normally can open, however, as there is
232 * already a file open, this will fail.
233 */
234 getToVerificationStarted(staticLayoutBlobId);
235
236 EXPECT_FALSE(handler->open(session + 1, flags, staticLayoutBlobId));
237}
238
239/*
Patrick Venture8facb382019-05-24 15:27:36 -0700240 * read(session)
241 */
242TEST_F(FirmwareHandlerVerificationStartedTest, ReadOfVerifyBlobReturnsEmpty)
243{
244 getToVerificationStarted(staticLayoutBlobId);
245 EXPECT_THAT(handler->read(session, 0, 32), IsEmpty());
246}
247
248/*
Patrick Ventureddc35072019-05-24 15:30:57 -0700249 * commit(session)
250 */
251TEST_F(FirmwareHandlerVerificationStartedTest,
252 CommitOnVerifyDuringVerificationHasNoImpact)
253{
254 getToVerificationStarted(staticLayoutBlobId);
255 EXPECT_TRUE(handler->commit(session, {}));
256 auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
257 EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationStarted,
258 realHandler->getCurrentState());
259}
260
261/*
Patrick Venturea04997b2019-05-24 10:15:48 -0700262 * close(session) - close while state if verificationStarted without calling
263 * stat first will abort.
Patrick Venture237e2c62019-05-23 20:35:33 -0700264 */
265
266} // namespace
267} // namespace ipmi_flash