blob: d9b4e7070e15576b577832babeaa9a7bc10a0cb1 [file] [log] [blame]
Patrick Venturea82f99e2019-05-24 15:44:35 -07001/**
2 * The goal of these tests is to verify the behavior of all blob commands given
3 * the current state is verificationCompleted. This state is achieved as a out
4 * of verificationStarted.
5 */
6#include "firmware_handler.hpp"
7#include "firmware_unittest.hpp"
8#include "status.hpp"
9#include "util.hpp"
10
11#include <cstdint>
12#include <string>
13#include <vector>
14
15#include <gtest/gtest.h>
16
17namespace ipmi_flash
18{
19namespace
20{
21
Patrick Venture65cdcf02019-05-29 10:18:50 -070022using ::testing::IsEmpty;
Patrick Venturea82f99e2019-05-24 15:44:35 -070023using ::testing::Return;
Patrick Venture6d3a14c2019-05-29 09:24:42 -070024using ::testing::UnorderedElementsAreArray;
Patrick Venturea82f99e2019-05-24 15:44:35 -070025
26/*
27 * There are the following calls (parameters may vary):
28 * canHandleBlob(blob)
29 * getBlobIds
30 * deleteBlob(blob)
31 * stat(blob)
32 * stat(session)
33 * open(blob)
34 * close(session)
35 * writemeta(session)
36 * write(session)
37 * read(session)
38 * commit(session)
39 *
40 * Like the state verificationStarted, there is a file open in
41 * verificationCompleted. This state is transitioned to after a stat() command
42 * indicates a successful verification.
43 */
44
45class FirmwareHandlerVerificationCompletedTest
46 : public IpmiOnlyFirmwareStaticTest
47{
Patrick Venturea82f99e2019-05-24 15:44:35 -070048};
49
Patrick Venture6d3a14c2019-05-29 09:24:42 -070050/* TODO: deleteBlob(blob) */
51
52/*
53 * canHandleBlob
Patrick Venturea82f99e2019-05-24 15:44:35 -070054 */
Patrick Venture6d3a14c2019-05-29 09:24:42 -070055TEST_F(FirmwareHandlerVerificationCompletedTest,
56 OnVerificationCompleteSuccessUpdateBlobIdNotPresent)
57{
58 /* the uploadBlobId is only added on close() of the verifyBlobId. This is a
59 * consistent behavior with verifyBlobId only added when closing the image
60 * or hash.
61 */
Patrick Ventureda66fd82019-06-03 11:11:24 -070062 getToVerificationCompleted(ActionStatus::success);
Patrick Venture6d3a14c2019-05-29 09:24:42 -070063 EXPECT_FALSE(handler->canHandleBlob(updateBlobId));
64}
65
66TEST_F(FirmwareHandlerVerificationCompletedTest,
67 OnVerificationCompleteFailureUpdateBlobIdNotPresent)
68{
Patrick Ventureda66fd82019-06-03 11:11:24 -070069 getToVerificationCompleted(ActionStatus::failed);
Patrick Venture6d3a14c2019-05-29 09:24:42 -070070 EXPECT_FALSE(handler->canHandleBlob(updateBlobId));
71}
72
73/*
74 * getBlobIds
75 */
76TEST_F(FirmwareHandlerVerificationCompletedTest, GetBlobIdsReturnsExpectedList)
77{
Patrick Ventureda66fd82019-06-03 11:11:24 -070078 getToVerificationCompleted(ActionStatus::success);
Patrick Venture6d3a14c2019-05-29 09:24:42 -070079 std::vector<std::string> expected = {verifyBlobId, hashBlobId,
80 activeImageBlobId, staticLayoutBlobId};
81 EXPECT_THAT(handler->getBlobIds(), UnorderedElementsAreArray(expected));
82}
Patrick Venturea82f99e2019-05-24 15:44:35 -070083
84/*
85 * stat(blob)
86 */
Patrick Venture0c642fd2019-05-24 16:09:29 -070087TEST_F(FirmwareHandlerVerificationCompletedTest,
88 StatOnActiveImageReturnsFailure)
89{
Patrick Ventureda66fd82019-06-03 11:11:24 -070090 getToVerificationCompleted(ActionStatus::success);
Patrick Venture0c642fd2019-05-24 16:09:29 -070091 ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId));
92
93 blobs::BlobMeta meta;
94 EXPECT_FALSE(handler->stat(activeImageBlobId, &meta));
95}
96
Patrick Venturefbf07ff2019-05-29 08:58:45 -070097TEST_F(FirmwareHandlerVerificationCompletedTest,
98 VerifyActiveHashIdMissingInThisCase)
99{
100 /* The path taken to get to this state never opened the hash blob Id, which
101 * is fine. But let's verify it behaved as intended.
102 */
Patrick Ventureda66fd82019-06-03 11:11:24 -0700103 getToVerificationCompleted(ActionStatus::success);
Patrick Venturefbf07ff2019-05-29 08:58:45 -0700104 EXPECT_FALSE(handler->canHandleBlob(activeHashBlobId));
105}
106
107/* TODO: Add sufficient warning that you can get to verificationCompleted
108 * without ever opening the image blob id (or the tarball one).
109 *
110 * Although in this case, it's expected that any verification triggered would
111 * certainly fail. So, although it's possible, it's uninteresting.
112 */
113
Patrick Venture0c642fd2019-05-24 16:09:29 -0700114TEST_F(FirmwareHandlerVerificationCompletedTest, StatOnVerifyBlobReturnsFailure)
115{
Patrick Ventureda66fd82019-06-03 11:11:24 -0700116 getToVerificationCompleted(ActionStatus::success);
Patrick Venture0c642fd2019-05-24 16:09:29 -0700117 ASSERT_TRUE(handler->canHandleBlob(verifyBlobId));
118
119 blobs::BlobMeta meta;
120 EXPECT_FALSE(handler->stat(verifyBlobId, &meta));
121}
122
123TEST_F(FirmwareHandlerVerificationCompletedTest,
124 StatOnNormalBlobsReturnsSuccess)
125{
Patrick Ventureda66fd82019-06-03 11:11:24 -0700126 getToVerificationCompleted(ActionStatus::success);
Patrick Venture0c642fd2019-05-24 16:09:29 -0700127
128 blobs::BlobMeta expected;
129 expected.blobState = FirmwareBlobHandler::UpdateFlags::ipmi;
130 expected.size = 0;
131
132 std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId};
133 for (const auto& blob : testBlobs)
134 {
135 ASSERT_TRUE(handler->canHandleBlob(blob));
136
137 blobs::BlobMeta meta = {};
138 EXPECT_TRUE(handler->stat(blob, &meta));
139 EXPECT_EQ(expected, meta);
140 }
141}
Patrick Venturea82f99e2019-05-24 15:44:35 -0700142
143/*
144 * stat(session) - the verify blobid is open in this state, so stat on that once
145 * completed should have no effect.
Patrick Venture65cdcf02019-05-29 10:18:50 -0700146 */
Patrick Venture615c69e2019-05-29 10:49:54 -0700147TEST_F(FirmwareHandlerVerificationCompletedTest,
148 SessionStatOnVerifyAfterSuccessDoesNothing)
149{
150 /* Every time you stat() once it's triggered, it checks the state again
151 * until it's completed.
152 */
Patrick Ventureda66fd82019-06-03 11:11:24 -0700153 getToVerificationCompleted(ActionStatus::success);
Patrick Venturef1f0f652019-06-03 09:10:19 -0700154 EXPECT_CALL(*verifyMockPtr, status()).Times(0);
Patrick Venture615c69e2019-05-29 10:49:54 -0700155
156 blobs::BlobMeta meta, expectedMeta = {};
157 expectedMeta.size = 0;
158 expectedMeta.blobState = flags | blobs::StateFlags::committed;
159 expectedMeta.metadata.push_back(
Patrick Ventureda66fd82019-06-03 11:11:24 -0700160 static_cast<std::uint8_t>(ActionStatus::success));
Patrick Venture615c69e2019-05-29 10:49:54 -0700161
162 EXPECT_TRUE(handler->stat(session, &meta));
163 EXPECT_EQ(expectedMeta, meta);
164 expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
165}
166
167TEST_F(FirmwareHandlerVerificationCompletedTest,
168 SessionStatOnVerifyAfterFailureDoesNothing)
169{
Patrick Ventureda66fd82019-06-03 11:11:24 -0700170 getToVerificationCompleted(ActionStatus::failed);
Patrick Venturef1f0f652019-06-03 09:10:19 -0700171 EXPECT_CALL(*verifyMockPtr, status()).Times(0);
Patrick Venture615c69e2019-05-29 10:49:54 -0700172
173 blobs::BlobMeta meta, expectedMeta = {};
174 expectedMeta.size = 0;
175 expectedMeta.blobState = flags | blobs::StateFlags::commit_error;
176 expectedMeta.metadata.push_back(
Patrick Ventureda66fd82019-06-03 11:11:24 -0700177 static_cast<std::uint8_t>(ActionStatus::failed));
Patrick Venture615c69e2019-05-29 10:49:54 -0700178
179 EXPECT_TRUE(handler->stat(session, &meta));
180 EXPECT_EQ(expectedMeta, meta);
181 expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
182}
Patrick Venture65cdcf02019-05-29 10:18:50 -0700183
184/*
Patrick Venturea82f99e2019-05-24 15:44:35 -0700185 * open(blob) - all open should fail
Patrick Venturea82f99e2019-05-24 15:44:35 -0700186 */
Patrick Ventureeee71812019-05-29 10:41:04 -0700187TEST_F(FirmwareHandlerVerificationCompletedTest,
188 OpeningAnyBlobAvailableFailsAfterSuccess)
189{
Patrick Ventureda66fd82019-06-03 11:11:24 -0700190 getToVerificationCompleted(ActionStatus::success);
Patrick Ventureeee71812019-05-29 10:41:04 -0700191
192 auto blobs = handler->getBlobIds();
193 for (const auto& blob : blobs)
194 {
195 EXPECT_FALSE(handler->open(session + 1, flags, blob));
196 }
197}
198
199TEST_F(FirmwareHandlerVerificationCompletedTest,
200 OpeningAnyBlobAvailableFailsAfterFailure)
201{
Patrick Ventureda66fd82019-06-03 11:11:24 -0700202 getToVerificationCompleted(ActionStatus::failed);
Patrick Ventureeee71812019-05-29 10:41:04 -0700203
204 auto blobs = handler->getBlobIds();
205 for (const auto& blob : blobs)
206 {
207 EXPECT_FALSE(handler->open(session + 1, flags, blob));
208 }
209}
Patrick Venturea82f99e2019-05-24 15:44:35 -0700210
211/*
212 * writemeta(session) - write meta should fail.
Patrick Venture4d9b0e12019-05-29 10:21:40 -0700213 */
Patrick Venture2b801372019-05-29 10:26:01 -0700214TEST_F(FirmwareHandlerVerificationCompletedTest,
215 WriteMetaToVerifyBlobReturnsFailure)
216{
Patrick Ventureda66fd82019-06-03 11:11:24 -0700217 getToVerificationCompleted(ActionStatus::success);
Patrick Venture2b801372019-05-29 10:26:01 -0700218
219 std::vector<std::uint8_t> bytes = {0x01, 0x02};
220 EXPECT_FALSE(handler->writeMeta(session, 0, bytes));
221}
Patrick Venture4d9b0e12019-05-29 10:21:40 -0700222
223/*
Patrick Venturea82f99e2019-05-24 15:44:35 -0700224 * write(session) - write should fail.
Patrick Venture65cdcf02019-05-29 10:18:50 -0700225 */
Patrick Venture4d9b0e12019-05-29 10:21:40 -0700226TEST_F(FirmwareHandlerVerificationCompletedTest,
227 WriteToVerifyBlobReturnsFailure)
228{
Patrick Ventureda66fd82019-06-03 11:11:24 -0700229 getToVerificationCompleted(ActionStatus::success);
Patrick Venture4d9b0e12019-05-29 10:21:40 -0700230
231 std::vector<std::uint8_t> bytes = {0x01, 0x02};
232 EXPECT_FALSE(handler->write(session, 0, bytes));
233}
Patrick Venture65cdcf02019-05-29 10:18:50 -0700234
235/*
Patrick Venturea82f99e2019-05-24 15:44:35 -0700236 * read(session) - read returns empty.
Patrick Venture65cdcf02019-05-29 10:18:50 -0700237 */
238TEST_F(FirmwareHandlerVerificationCompletedTest, ReadOfVerifyBlobReturnsEmpty)
239{
Patrick Ventureda66fd82019-06-03 11:11:24 -0700240 getToVerificationCompleted(ActionStatus::success);
Patrick Venture65cdcf02019-05-29 10:18:50 -0700241 EXPECT_THAT(handler->read(session, 0, 1), IsEmpty());
242}
243
244/*
Patrick Venture433cbc32019-05-30 09:53:10 -0700245 * commit(session) - returns failure
Patrick Venturea82f99e2019-05-24 15:44:35 -0700246 */
Patrick Venture433cbc32019-05-30 09:53:10 -0700247TEST_F(FirmwareHandlerVerificationCompletedTest,
248 CommitOnVerifyBlobAfterSuccessReturnsFailure)
249{
250 /* If you've started this'll return success, but if it's finished, it won't
251 * let you try-again.
252 */
Patrick Ventureda66fd82019-06-03 11:11:24 -0700253 getToVerificationCompleted(ActionStatus::success);
Patrick Venture1d66fe62019-06-03 14:57:27 -0700254 EXPECT_CALL(*verifyMockPtr, trigger()).Times(0);
Patrick Venture433cbc32019-05-30 09:53:10 -0700255
256 EXPECT_FALSE(handler->commit(session, {}));
257}
258
259TEST_F(FirmwareHandlerVerificationCompletedTest,
260 CommitOnVerifyBlobAfterFailureReturnsFailure)
261{
Patrick Ventureda66fd82019-06-03 11:11:24 -0700262 getToVerificationCompleted(ActionStatus::failed);
Patrick Venture1d66fe62019-06-03 14:57:27 -0700263 EXPECT_CALL(*verifyMockPtr, trigger()).Times(0);
Patrick Venture433cbc32019-05-30 09:53:10 -0700264
265 EXPECT_FALSE(handler->commit(session, {}));
266}
Patrick Venturea82f99e2019-05-24 15:44:35 -0700267
Patrick Venture65cdcf02019-05-29 10:18:50 -0700268/*
269 * close(session) - close on the verify blobid:
270 * 1. if successful adds update blob id, changes state to UpdatePending
Patrick Venture1c6d8f52019-05-30 10:53:49 -0700271 */
272TEST_F(FirmwareHandlerVerificationCompletedTest,
273 CloseAfterSuccessChangesStateAddsUpdateBlob)
274{
Patrick Ventureda66fd82019-06-03 11:11:24 -0700275 getToVerificationCompleted(ActionStatus::success);
Patrick Venture1c6d8f52019-05-30 10:53:49 -0700276 ASSERT_FALSE(handler->canHandleBlob(updateBlobId));
277
278 handler->close(session);
279 EXPECT_TRUE(handler->canHandleBlob(updateBlobId));
280 expectedState(FirmwareBlobHandler::UpdateState::updatePending);
281}
282
283/*
284 * close(session) - close on the verify blobid:
285 * TODO: 2. if unsuccessful doesn't add update blob id, changes state to?
Patrick Venture65cdcf02019-05-29 10:18:50 -0700286 */
287
Patrick Venturea82f99e2019-05-24 15:44:35 -0700288} // namespace
289} // namespace ipmi_flash