blob: 3aa97edc78527259e65021d4cbdc794a5e851c04 [file] [log] [blame]
Patrick Venture16ab2a12019-05-31 08:52:51 -07001/* The goal of these tests is to verify the behavior of all blob commands given
2 * the current state is updateStarted. This state is achieved as an exit from
3 * updatePending.
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 Venture73e2bdf2019-05-31 11:21:52 -070021using ::testing::IsEmpty;
Patrick Venture16ab2a12019-05-31 08:52:51 -070022using ::testing::Return;
Patrick Ventured9a95782019-05-31 10:46:56 -070023using ::testing::UnorderedElementsAreArray;
Patrick Venture16ab2a12019-05-31 08:52:51 -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
40class FirmwareHandlerUpdateStartedTest : public IpmiOnlyFirmwareStaticTest
Patrick Venture9b37b092020-05-28 20:58:57 -070041{};
Patrick Venture16ab2a12019-05-31 08:52:51 -070042
Patrick Venturef515b9c2019-05-31 10:38:16 -070043/*
44 * open(blob)
45 */
46TEST_F(FirmwareHandlerUpdateStartedTest, AttemptToOpenFilesReturnsFailure)
47{
48 /* In state updateStarted a file is open, which means no others can be. */
49 getToUpdateStarted();
50
51 auto blobsToOpen = handler->getBlobIds();
52 for (const auto& blob : blobsToOpen)
53 {
54 EXPECT_FALSE(handler->open(session + 1, flags, blob));
55 }
56}
57
Patrick Venture16ab2a12019-05-31 08:52:51 -070058/* canHandleBlob(blob)
59 * getBlobIds
60 */
Patrick Ventured9a95782019-05-31 10:46:56 -070061TEST_F(FirmwareHandlerUpdateStartedTest, VerifyListOfBlobs)
62{
63 getToUpdateStarted();
64
Patrick Venture9a69f732019-06-17 14:05:13 -070065 EXPECT_THAT(
66 handler->getBlobIds(),
67 UnorderedElementsAreArray(
68 {updateBlobId, hashBlobId, activeImageBlobId, staticLayoutBlobId}));
Patrick Ventured9a95782019-05-31 10:46:56 -070069}
Patrick Venture16ab2a12019-05-31 08:52:51 -070070
71/*
Patrick Venturebcc0c772019-06-17 10:42:06 -070072 * deleteBlob(blob)
Patrick Venture16ab2a12019-05-31 08:52:51 -070073 */
Patrick Venturebcc0c772019-06-17 10:42:06 -070074TEST_F(FirmwareHandlerUpdateStartedTest, DeleteBlobReturnsFalse)
75{
76 /* Try deleting all blobs, it doesn't really matter which though because you
77 * cannot close out an open session, therefore you must fail to delete
78 * anything unless everything is closed.
79 */
80 getToUpdateStarted();
81 auto blobs = handler->getBlobIds();
82 for (const auto& b : blobs)
83 {
84 EXPECT_FALSE(handler->deleteBlob(b));
85 }
86}
Patrick Venture16ab2a12019-05-31 08:52:51 -070087
88/*
89 * stat(blob)
90 */
Patrick Venturefc34cfa2019-05-31 11:02:51 -070091TEST_F(FirmwareHandlerUpdateStartedTest, StatOnActiveImageReturnsFailure)
92{
93 getToUpdateStarted();
94
95 ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId));
96
97 blobs::BlobMeta meta;
98 EXPECT_FALSE(handler->stat(activeImageBlobId, &meta));
99}
100
101TEST_F(FirmwareHandlerUpdateStartedTest, StatOnUpdateBlobReturnsFailure)
102{
103 getToUpdateStarted();
104
105 ASSERT_TRUE(handler->canHandleBlob(updateBlobId));
106
107 blobs::BlobMeta meta;
108 EXPECT_FALSE(handler->stat(updateBlobId, &meta));
109}
110
111TEST_F(FirmwareHandlerUpdateStartedTest, StatOnNormalBlobsReturnsSuccess)
112{
113 getToUpdateStarted();
114
Patrick Venturefc34cfa2019-05-31 11:02:51 -0700115 std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId};
116 for (const auto& blob : testBlobs)
117 {
118 ASSERT_TRUE(handler->canHandleBlob(blob));
119
120 blobs::BlobMeta meta = {};
121 EXPECT_TRUE(handler->stat(blob, &meta));
Benjamin Fair12901982019-11-12 13:55:46 -0800122 EXPECT_EQ(expectedIdleMeta, meta);
Patrick Venturefc34cfa2019-05-31 11:02:51 -0700123 }
124}
Patrick Venture16ab2a12019-05-31 08:52:51 -0700125
126/*
Patrick Venture16ab2a12019-05-31 08:52:51 -0700127 * writemeta(session)
128 */
Patrick Ventureb6ce52b2019-05-31 11:16:41 -0700129TEST_F(FirmwareHandlerUpdateStartedTest, WriteMetaToUpdateBlobReturnsFailure)
130{
131 getToUpdateStarted();
132 EXPECT_FALSE(handler->writeMeta(session, 0, {0x01}));
133}
Patrick Venture16ab2a12019-05-31 08:52:51 -0700134
135/*
136 * write(session)
137 */
Patrick Venture575118d2019-05-31 11:20:35 -0700138TEST_F(FirmwareHandlerUpdateStartedTest, WriteToUpdateBlobReturnsFailure)
139{
140 getToUpdateStarted();
141 EXPECT_FALSE(handler->write(session, 0, {0x01}));
142}
Patrick Venture16ab2a12019-05-31 08:52:51 -0700143
144/*
145 * read(session)
146 */
Patrick Venture73e2bdf2019-05-31 11:21:52 -0700147TEST_F(FirmwareHandlerUpdateStartedTest, ReadFromUpdateBlobReturnsEmpty)
148{
149 getToUpdateStarted();
150 EXPECT_THAT(handler->read(session, 0, 1), IsEmpty());
151}
Patrick Venture16ab2a12019-05-31 08:52:51 -0700152
153/*
Patrick Venture0079d892019-05-31 11:27:44 -0700154 * commit(session)
155 */
156TEST_F(FirmwareHandlerUpdateStartedTest,
157 CallingCommitShouldReturnTrueAndHaveNoEffect)
158{
159 getToUpdateStarted();
Patrick Venture1d66fe62019-06-03 14:57:27 -0700160 EXPECT_CALL(*updateMockPtr, trigger()).Times(0);
Patrick Venture0079d892019-05-31 11:27:44 -0700161
162 EXPECT_TRUE(handler->commit(session, {}));
163 expectedState(FirmwareBlobHandler::UpdateState::updateStarted);
164}
165
166/*
Patrick Ventureb6ce52b2019-05-31 11:16:41 -0700167 * stat(session) - this will trigger a check, and the state may change.
168 */
Patrick Venturef1f0f652019-06-03 09:10:19 -0700169TEST_F(FirmwareHandlerUpdateStartedTest,
Patrick Ventureda66fd82019-06-03 11:11:24 -0700170 CallStatChecksActionStatusReturnsRunningDoesNotChangeState)
Patrick Venturef1f0f652019-06-03 09:10:19 -0700171{
172 getToUpdateStarted();
173 EXPECT_CALL(*updateMockPtr, status())
Patrick Ventureda66fd82019-06-03 11:11:24 -0700174 .WillOnce(Return(ActionStatus::running));
Patrick Venturef1f0f652019-06-03 09:10:19 -0700175
176 blobs::BlobMeta meta, expectedMeta = {};
177 expectedMeta.size = 0;
178 expectedMeta.blobState = flags | blobs::StateFlags::committing;
179 expectedMeta.metadata.push_back(
Patrick Ventureda66fd82019-06-03 11:11:24 -0700180 static_cast<std::uint8_t>(ActionStatus::running));
Patrick Venturef1f0f652019-06-03 09:10:19 -0700181
182 EXPECT_TRUE(handler->stat(session, &meta));
183 EXPECT_EQ(expectedMeta, meta);
184 expectedState(FirmwareBlobHandler::UpdateState::updateStarted);
185}
186
187TEST_F(FirmwareHandlerUpdateStartedTest,
Patrick Ventureda66fd82019-06-03 11:11:24 -0700188 CallStatChecksActionStatusReturnsFailedChangesStateToCompleted)
Patrick Venturef1f0f652019-06-03 09:10:19 -0700189{
190 getToUpdateStarted();
191 EXPECT_CALL(*updateMockPtr, status())
Patrick Ventureda66fd82019-06-03 11:11:24 -0700192 .WillOnce(Return(ActionStatus::failed));
Patrick Venturef1f0f652019-06-03 09:10:19 -0700193
194 blobs::BlobMeta meta, expectedMeta = {};
195 expectedMeta.size = 0;
196 expectedMeta.blobState = flags | blobs::StateFlags::commit_error;
197 expectedMeta.metadata.push_back(
Patrick Ventureda66fd82019-06-03 11:11:24 -0700198 static_cast<std::uint8_t>(ActionStatus::failed));
Patrick Venturef1f0f652019-06-03 09:10:19 -0700199
200 EXPECT_TRUE(handler->stat(session, &meta));
201 EXPECT_EQ(expectedMeta, meta);
202 expectedState(FirmwareBlobHandler::UpdateState::updateCompleted);
203}
204
205TEST_F(FirmwareHandlerUpdateStartedTest,
Patrick Ventureda66fd82019-06-03 11:11:24 -0700206 CallStatChecksActionStatusReturnsSuccessChangesStateToCompleted)
Patrick Venturef1f0f652019-06-03 09:10:19 -0700207{
208 getToUpdateStarted();
209 EXPECT_CALL(*updateMockPtr, status())
Patrick Ventureda66fd82019-06-03 11:11:24 -0700210 .WillOnce(Return(ActionStatus::success));
Patrick Venturef1f0f652019-06-03 09:10:19 -0700211
212 blobs::BlobMeta meta, expectedMeta = {};
213 expectedMeta.size = 0;
214 expectedMeta.blobState = flags | blobs::StateFlags::committed;
215 expectedMeta.metadata.push_back(
Patrick Ventureda66fd82019-06-03 11:11:24 -0700216 static_cast<std::uint8_t>(ActionStatus::success));
Patrick Venturef1f0f652019-06-03 09:10:19 -0700217
218 EXPECT_TRUE(handler->stat(session, &meta));
219 EXPECT_EQ(expectedMeta, meta);
220 expectedState(FirmwareBlobHandler::UpdateState::updateCompleted);
221}
Patrick Ventureb6ce52b2019-05-31 11:16:41 -0700222
223/*
Patrick Venture49731712019-06-17 10:04:02 -0700224 * close(session) - this will abort.
Patrick Ventureb6ce52b2019-05-31 11:16:41 -0700225 */
Patrick Venture49731712019-06-17 10:04:02 -0700226TEST_F(FirmwareHandlerUpdateStartedTest, CloseOnUpdateDuringUpdateAbortsProcess)
227{
228 getToUpdateStarted();
229 EXPECT_CALL(*updateMockPtr, abort()).Times(1);
230
231 EXPECT_TRUE(handler->close(session));
232
Patrick Venture49731712019-06-17 10:04:02 -0700233 EXPECT_THAT(handler->getBlobIds(),
Patrick Venture9a69f732019-06-17 14:05:13 -0700234 UnorderedElementsAreArray(startingBlobs));
Patrick Venture49731712019-06-17 10:04:02 -0700235
236 expectedState(FirmwareBlobHandler::UpdateState::notYetStarted);
237}
Patrick Ventureb6ce52b2019-05-31 11:16:41 -0700238
Patrick Venture16ab2a12019-05-31 08:52:51 -0700239} // namespace
240} // namespace ipmi_flash