blob: e5f57e5879b7e4d680ecb5c71b3722e8c2c8bb24 [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 {
Patrick Venture237e2c62019-05-23 20:35:33 -070047 EXPECT_CALL(imageMock, open(blobId)).WillOnce(Return(true));
48 EXPECT_TRUE(handler->open(session, flags, blobId));
Patrick Venture6fdd02e2019-05-28 13:02:04 -070049 expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
50
Patrick Venture237e2c62019-05-23 20:35:33 -070051 EXPECT_CALL(imageMock, close()).WillRepeatedly(Return());
52 handler->close(session);
Patrick Venture6fdd02e2019-05-28 13:02:04 -070053 expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
Patrick Venture237e2c62019-05-23 20:35:33 -070054
55 EXPECT_TRUE(handler->open(session, flags, verifyBlobId));
56 EXPECT_CALL(*verifyMockPtr, triggerVerification())
57 .WillOnce(Return(true));
58
59 EXPECT_TRUE(handler->commit(session, {}));
Patrick Venture6fdd02e2019-05-28 13:02:04 -070060 expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
Patrick Venture237e2c62019-05-23 20:35:33 -070061 }
62
63 std::uint16_t session = 1;
64 std::uint16_t flags =
65 blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi;
66};
67
68/*
69 * stat(session)
70 */
71TEST_F(FirmwareHandlerVerificationStartedTest,
72 StatOnVerifyBlobIdAfterCommitChecksStateAndReturnsRunning)
73{
74 getToVerificationStarted(staticLayoutBlobId);
75 EXPECT_CALL(*verifyMockPtr, checkVerificationState())
76 .WillOnce(Return(VerifyCheckResponses::running));
77
78 blobs::BlobMeta meta, expectedMeta = {};
79 expectedMeta.size = 0;
80 expectedMeta.blobState = flags | blobs::StateFlags::committing;
81 expectedMeta.metadata.push_back(
82 static_cast<std::uint8_t>(VerifyCheckResponses::running));
83
84 EXPECT_TRUE(handler->stat(session, &meta));
85 EXPECT_EQ(expectedMeta, meta);
86}
87
88TEST_F(FirmwareHandlerVerificationStartedTest,
89 StatOnVerifyBlobIdAfterCommitCheckStateAndReturnsFailed)
90{
91 /* If the returned state from the verification handler is failed, it sets
92 * commit_error and transitions to verificationCompleted.
93 */
94 getToVerificationStarted(staticLayoutBlobId);
95 EXPECT_CALL(*verifyMockPtr, checkVerificationState())
96 .WillOnce(Return(VerifyCheckResponses::failed));
97
98 blobs::BlobMeta meta, expectedMeta = {};
99 expectedMeta.size = 0;
100 expectedMeta.blobState = flags | blobs::StateFlags::commit_error;
101 expectedMeta.metadata.push_back(
102 static_cast<std::uint8_t>(VerifyCheckResponses::failed));
103
Patrick Venture237e2c62019-05-23 20:35:33 -0700104 EXPECT_TRUE(handler->stat(session, &meta));
105 EXPECT_EQ(expectedMeta, meta);
Patrick Venture6fdd02e2019-05-28 13:02:04 -0700106 expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
Patrick Venture237e2c62019-05-23 20:35:33 -0700107}
108
109TEST_F(FirmwareHandlerVerificationStartedTest,
110 StatOnVerifyBlobIdAfterCommitCheckStateAndReturnsSuccess)
111{
112 /* If the returned state from the verification handler is success, it sets
113 * committed and transitions to verificationCompleted.
114 */
115 getToVerificationStarted(staticLayoutBlobId);
116 EXPECT_CALL(*verifyMockPtr, checkVerificationState())
117 .WillOnce(Return(VerifyCheckResponses::success));
118
119 blobs::BlobMeta meta, expectedMeta = {};
120 expectedMeta.size = 0;
121 expectedMeta.blobState = flags | blobs::StateFlags::committed;
122 expectedMeta.metadata.push_back(
123 static_cast<std::uint8_t>(VerifyCheckResponses::success));
124
Patrick Venture237e2c62019-05-23 20:35:33 -0700125 EXPECT_TRUE(handler->stat(session, &meta));
126 EXPECT_EQ(expectedMeta, meta);
Patrick Venture6fdd02e2019-05-28 13:02:04 -0700127 expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
Patrick Venture237e2c62019-05-23 20:35:33 -0700128}
129
130/* TODO: Once verificationCompleted is the state, canHandleBlob should accept
131 * updateBlobId.
132 */
133
134/* TODO:
135 * canHandleBlob(blob)
Patrick Venturea04997b2019-05-24 10:15:48 -0700136 *
Patrick Venture237e2c62019-05-23 20:35:33 -0700137 * getBlobIds
Patrick Venturea04997b2019-05-24 10:15:48 -0700138 *
Patrick Venture237e2c62019-05-23 20:35:33 -0700139 * deleteBlob(blob)
Patrick Venturea04997b2019-05-24 10:15:48 -0700140 */
141
142/*
Patrick Venture237e2c62019-05-23 20:35:33 -0700143 * stat(blob)
Patrick Venturea04997b2019-05-24 10:15:48 -0700144 */
145TEST_F(FirmwareHandlerVerificationStartedTest, StatOnActiveImageReturnsFailure)
146{
147 getToVerificationStarted(staticLayoutBlobId);
Patrick Venture930c7b72019-05-24 11:11:08 -0700148 ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId));
Patrick Venturea04997b2019-05-24 10:15:48 -0700149
150 blobs::BlobMeta meta;
151 EXPECT_FALSE(handler->stat(activeImageBlobId, &meta));
152}
153
154TEST_F(FirmwareHandlerVerificationStartedTest, StatOnActiveHashReturnsFailure)
155{
Patrick Venture930c7b72019-05-24 11:11:08 -0700156 getToVerificationStarted(hashBlobId);
157 ASSERT_TRUE(handler->canHandleBlob(activeHashBlobId));
Patrick Venturea04997b2019-05-24 10:15:48 -0700158
159 blobs::BlobMeta meta;
160 EXPECT_FALSE(handler->stat(activeHashBlobId, &meta));
161}
162
163TEST_F(FirmwareHandlerVerificationStartedTest, StatOnVerifyBlobReturnsFailure)
164{
165 /* the verifyBlobId is available starting at verificationPending. */
166 getToVerificationStarted(staticLayoutBlobId);
Patrick Venture930c7b72019-05-24 11:11:08 -0700167 ASSERT_TRUE(handler->canHandleBlob(verifyBlobId));
Patrick Venturea04997b2019-05-24 10:15:48 -0700168
169 blobs::BlobMeta meta;
170 EXPECT_FALSE(handler->stat(verifyBlobId, &meta));
171}
172
173TEST_F(FirmwareHandlerVerificationStartedTest, StatOnNormalBlobsReturnsSuccess)
174{
175 getToVerificationStarted(staticLayoutBlobId);
176
177 blobs::BlobMeta expected;
178 expected.blobState = FirmwareBlobHandler::UpdateFlags::ipmi;
179 expected.size = 0;
180
181 std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId};
182 for (const auto& blob : testBlobs)
183 {
Patrick Venture930c7b72019-05-24 11:11:08 -0700184 ASSERT_TRUE(handler->canHandleBlob(blob));
185
Patrick Venturea04997b2019-05-24 10:15:48 -0700186 blobs::BlobMeta meta = {};
187 EXPECT_TRUE(handler->stat(blob, &meta));
188 EXPECT_EQ(expected, meta);
189 }
190}
191
192/*
Patrick Venturefb74ad52019-05-24 15:03:35 -0700193 * writemeta(session)
194 */
195TEST_F(FirmwareHandlerVerificationStartedTest,
196 WriteMetaOnVerifySessionReturnsFailure)
197{
198 getToVerificationStarted(staticLayoutBlobId);
199
200 std::vector<std::uint8_t> bytes = {0x01, 0x02};
201 EXPECT_FALSE(handler->writeMeta(session, 0, bytes));
202}
203
204/*
205 * write(session)
206 */
Patrick Venture9c6de5a2019-05-24 15:12:26 -0700207TEST_F(FirmwareHandlerVerificationStartedTest,
208 WriteOnVerifySessionReturnsFailure)
209{
210 getToVerificationStarted(staticLayoutBlobId);
211
212 std::vector<std::uint8_t> bytes = {0x01, 0x02};
213 EXPECT_FALSE(handler->write(session, 0, bytes));
214}
Patrick Venturefb74ad52019-05-24 15:03:35 -0700215
216/*
Patrick Venture237e2c62019-05-23 20:35:33 -0700217 * open(blob) - there is nothing you can open, this state has an open file.
Patrick Ventured4509102019-05-24 15:22:04 -0700218 */
219TEST_F(FirmwareHandlerVerificationStartedTest,
220 AttemptToOpenImageFileReturnsFailure)
221{
222 /* Attempt to open a file one normally can open, however, as there is
223 * already a file open, this will fail.
224 */
225 getToVerificationStarted(staticLayoutBlobId);
226
227 EXPECT_FALSE(handler->open(session + 1, flags, staticLayoutBlobId));
228}
229
230/*
Patrick Venture8facb382019-05-24 15:27:36 -0700231 * read(session)
232 */
233TEST_F(FirmwareHandlerVerificationStartedTest, ReadOfVerifyBlobReturnsEmpty)
234{
235 getToVerificationStarted(staticLayoutBlobId);
236 EXPECT_THAT(handler->read(session, 0, 32), IsEmpty());
237}
238
239/*
Patrick Ventureddc35072019-05-24 15:30:57 -0700240 * commit(session)
241 */
242TEST_F(FirmwareHandlerVerificationStartedTest,
243 CommitOnVerifyDuringVerificationHasNoImpact)
244{
245 getToVerificationStarted(staticLayoutBlobId);
246 EXPECT_TRUE(handler->commit(session, {}));
Patrick Venture6fdd02e2019-05-28 13:02:04 -0700247 expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
Patrick Ventureddc35072019-05-24 15:30:57 -0700248}
249
250/*
Patrick Venturea04997b2019-05-24 10:15:48 -0700251 * close(session) - close while state if verificationStarted without calling
252 * stat first will abort.
Patrick Venture35b3fc92019-05-24 15:36:01 -0700253 * TODO: implement this test when we implement abort.
Patrick Venture237e2c62019-05-23 20:35:33 -0700254 */
255
256} // namespace
257} // namespace ipmi_flash