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