blob: f7a0f2c002985f36a6650c047f686f5c86dbb900 [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
22using ::testing::Return;
Patrick Venture6d3a14c2019-05-29 09:24:42 -070023using ::testing::UnorderedElementsAreArray;
Patrick Venturea82f99e2019-05-24 15:44:35 -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 * Like the state verificationStarted, there is a file open in
40 * verificationCompleted. This state is transitioned to after a stat() command
41 * indicates a successful verification.
42 */
43
44class FirmwareHandlerVerificationCompletedTest
45 : public IpmiOnlyFirmwareStaticTest
46{
47 protected:
Patrick Venture0c642fd2019-05-24 16:09:29 -070048 void getToVerificationCompleted(VerifyCheckResponses checkResponse)
Patrick Venturea82f99e2019-05-24 15:44:35 -070049 {
Patrick Venture0c642fd2019-05-24 16:09:29 -070050 /* The hash was not sent up, as it's technically optional. Therefore,
51 * there is no active hash file.
52 */
Patrick Venturea82f99e2019-05-24 15:44:35 -070053 EXPECT_CALL(imageMock, open(staticLayoutBlobId)).WillOnce(Return(true));
54 EXPECT_TRUE(handler->open(session, flags, staticLayoutBlobId));
Patrick Venture6fdd02e2019-05-28 13:02:04 -070055 expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
56
Patrick Venturea82f99e2019-05-24 15:44:35 -070057 EXPECT_CALL(imageMock, close()).WillRepeatedly(Return());
58 handler->close(session);
Patrick Venture6fdd02e2019-05-28 13:02:04 -070059 expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
Patrick Venturea82f99e2019-05-24 15:44:35 -070060
61 EXPECT_TRUE(handler->open(session, flags, verifyBlobId));
62 EXPECT_CALL(*verifyMockPtr, triggerVerification())
63 .WillOnce(Return(true));
64
65 EXPECT_TRUE(handler->commit(session, {}));
Patrick Venture6fdd02e2019-05-28 13:02:04 -070066 expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
Patrick Venturea82f99e2019-05-24 15:44:35 -070067
68 EXPECT_CALL(*verifyMockPtr, checkVerificationState())
Patrick Venture0c642fd2019-05-24 16:09:29 -070069 .WillOnce(Return(checkResponse));
Patrick Venturea82f99e2019-05-24 15:44:35 -070070 blobs::BlobMeta meta;
71 EXPECT_TRUE(handler->stat(session, &meta));
Patrick Venture6fdd02e2019-05-28 13:02:04 -070072 expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
Patrick Venturea82f99e2019-05-24 15:44:35 -070073 }
74
75 std::uint16_t session = 1;
76 std::uint16_t flags =
77 blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi;
78};
79
Patrick Venture6d3a14c2019-05-29 09:24:42 -070080/* TODO: deleteBlob(blob) */
81
82/*
83 * canHandleBlob
Patrick Venturea82f99e2019-05-24 15:44:35 -070084 */
Patrick Venture6d3a14c2019-05-29 09:24:42 -070085TEST_F(FirmwareHandlerVerificationCompletedTest,
86 OnVerificationCompleteSuccessUpdateBlobIdNotPresent)
87{
88 /* the uploadBlobId is only added on close() of the verifyBlobId. This is a
89 * consistent behavior with verifyBlobId only added when closing the image
90 * or hash.
91 */
92 getToVerificationCompleted(VerifyCheckResponses::success);
93 EXPECT_FALSE(handler->canHandleBlob(updateBlobId));
94}
95
96TEST_F(FirmwareHandlerVerificationCompletedTest,
97 OnVerificationCompleteFailureUpdateBlobIdNotPresent)
98{
99 getToVerificationCompleted(VerifyCheckResponses::failed);
100 EXPECT_FALSE(handler->canHandleBlob(updateBlobId));
101}
102
103/*
104 * getBlobIds
105 */
106TEST_F(FirmwareHandlerVerificationCompletedTest, GetBlobIdsReturnsExpectedList)
107{
108 getToVerificationCompleted(VerifyCheckResponses::success);
109 std::vector<std::string> expected = {verifyBlobId, hashBlobId,
110 activeImageBlobId, staticLayoutBlobId};
111 EXPECT_THAT(handler->getBlobIds(), UnorderedElementsAreArray(expected));
112}
Patrick Venturea82f99e2019-05-24 15:44:35 -0700113
114/*
115 * stat(blob)
116 */
Patrick Venture0c642fd2019-05-24 16:09:29 -0700117TEST_F(FirmwareHandlerVerificationCompletedTest,
118 StatOnActiveImageReturnsFailure)
119{
120 getToVerificationCompleted(VerifyCheckResponses::success);
121 ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId));
122
123 blobs::BlobMeta meta;
124 EXPECT_FALSE(handler->stat(activeImageBlobId, &meta));
125}
126
Patrick Venturefbf07ff2019-05-29 08:58:45 -0700127TEST_F(FirmwareHandlerVerificationCompletedTest,
128 VerifyActiveHashIdMissingInThisCase)
129{
130 /* The path taken to get to this state never opened the hash blob Id, which
131 * is fine. But let's verify it behaved as intended.
132 */
133 getToVerificationCompleted(VerifyCheckResponses::success);
134 EXPECT_FALSE(handler->canHandleBlob(activeHashBlobId));
135}
136
137/* TODO: Add sufficient warning that you can get to verificationCompleted
138 * without ever opening the image blob id (or the tarball one).
139 *
140 * Although in this case, it's expected that any verification triggered would
141 * certainly fail. So, although it's possible, it's uninteresting.
142 */
143
Patrick Venture0c642fd2019-05-24 16:09:29 -0700144TEST_F(FirmwareHandlerVerificationCompletedTest, StatOnVerifyBlobReturnsFailure)
145{
146 getToVerificationCompleted(VerifyCheckResponses::success);
147 ASSERT_TRUE(handler->canHandleBlob(verifyBlobId));
148
149 blobs::BlobMeta meta;
150 EXPECT_FALSE(handler->stat(verifyBlobId, &meta));
151}
152
153TEST_F(FirmwareHandlerVerificationCompletedTest,
154 StatOnNormalBlobsReturnsSuccess)
155{
156 getToVerificationCompleted(VerifyCheckResponses::success);
157
158 blobs::BlobMeta expected;
159 expected.blobState = FirmwareBlobHandler::UpdateFlags::ipmi;
160 expected.size = 0;
161
162 std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId};
163 for (const auto& blob : testBlobs)
164 {
165 ASSERT_TRUE(handler->canHandleBlob(blob));
166
167 blobs::BlobMeta meta = {};
168 EXPECT_TRUE(handler->stat(blob, &meta));
169 EXPECT_EQ(expected, meta);
170 }
171}
Patrick Venturea82f99e2019-05-24 15:44:35 -0700172
173/*
174 * stat(session) - the verify blobid is open in this state, so stat on that once
175 * completed should have no effect.
176 *
177 * open(blob) - all open should fail
178 *
179 * close(session) - close on the verify blobid:
180 * 1. if successful adds update blob id, changes state to UpdatePending
181 * 2. if unsuccessful doesn't add update blob id, changes state to?
182 */
183
184/*
185 * writemeta(session) - write meta should fail.
186 * write(session) - write should fail.
187 * read(session) - read returns empty.
188 * commit(session) - ?
189 */
190
191} // namespace
192} // namespace ipmi_flash