blob: 086dcfa7ca5fc8e3944b2beec4fadd4794964925 [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
41{
42};
43
Patrick Venturef515b9c2019-05-31 10:38:16 -070044/*
45 * open(blob)
46 */
47TEST_F(FirmwareHandlerUpdateStartedTest, AttemptToOpenFilesReturnsFailure)
48{
49 /* In state updateStarted a file is open, which means no others can be. */
50 getToUpdateStarted();
51
52 auto blobsToOpen = handler->getBlobIds();
53 for (const auto& blob : blobsToOpen)
54 {
55 EXPECT_FALSE(handler->open(session + 1, flags, blob));
56 }
57}
58
Patrick Venture16ab2a12019-05-31 08:52:51 -070059/* canHandleBlob(blob)
60 * getBlobIds
61 */
Patrick Ventured9a95782019-05-31 10:46:56 -070062TEST_F(FirmwareHandlerUpdateStartedTest, VerifyListOfBlobs)
63{
64 getToUpdateStarted();
65
Patrick Venture9a69f732019-06-17 14:05:13 -070066 EXPECT_THAT(
67 handler->getBlobIds(),
68 UnorderedElementsAreArray(
69 {updateBlobId, hashBlobId, activeImageBlobId, staticLayoutBlobId}));
Patrick Ventured9a95782019-05-31 10:46:56 -070070}
Patrick Venture16ab2a12019-05-31 08:52:51 -070071
72/*
Patrick Venturebcc0c772019-06-17 10:42:06 -070073 * deleteBlob(blob)
Patrick Venture16ab2a12019-05-31 08:52:51 -070074 */
Patrick Venturebcc0c772019-06-17 10:42:06 -070075TEST_F(FirmwareHandlerUpdateStartedTest, DeleteBlobReturnsFalse)
76{
77 /* Try deleting all blobs, it doesn't really matter which though because you
78 * cannot close out an open session, therefore you must fail to delete
79 * anything unless everything is closed.
80 */
81 getToUpdateStarted();
82 auto blobs = handler->getBlobIds();
83 for (const auto& b : blobs)
84 {
85 EXPECT_FALSE(handler->deleteBlob(b));
86 }
87}
Patrick Venture16ab2a12019-05-31 08:52:51 -070088
89/*
90 * stat(blob)
91 */
Patrick Venturefc34cfa2019-05-31 11:02:51 -070092TEST_F(FirmwareHandlerUpdateStartedTest, StatOnActiveImageReturnsFailure)
93{
94 getToUpdateStarted();
95
96 ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId));
97
98 blobs::BlobMeta meta;
99 EXPECT_FALSE(handler->stat(activeImageBlobId, &meta));
100}
101
102TEST_F(FirmwareHandlerUpdateStartedTest, StatOnUpdateBlobReturnsFailure)
103{
104 getToUpdateStarted();
105
106 ASSERT_TRUE(handler->canHandleBlob(updateBlobId));
107
108 blobs::BlobMeta meta;
109 EXPECT_FALSE(handler->stat(updateBlobId, &meta));
110}
111
112TEST_F(FirmwareHandlerUpdateStartedTest, StatOnNormalBlobsReturnsSuccess)
113{
114 getToUpdateStarted();
115
Patrick Venturefc34cfa2019-05-31 11:02:51 -0700116 std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId};
117 for (const auto& blob : testBlobs)
118 {
119 ASSERT_TRUE(handler->canHandleBlob(blob));
120
121 blobs::BlobMeta meta = {};
122 EXPECT_TRUE(handler->stat(blob, &meta));
Benjamin Fair12901982019-11-12 13:55:46 -0800123 EXPECT_EQ(expectedIdleMeta, meta);
Patrick Venturefc34cfa2019-05-31 11:02:51 -0700124 }
125}
Patrick Venture16ab2a12019-05-31 08:52:51 -0700126
127/*
Patrick Venture16ab2a12019-05-31 08:52:51 -0700128 * writemeta(session)
129 */
Patrick Ventureb6ce52b2019-05-31 11:16:41 -0700130TEST_F(FirmwareHandlerUpdateStartedTest, WriteMetaToUpdateBlobReturnsFailure)
131{
132 getToUpdateStarted();
133 EXPECT_FALSE(handler->writeMeta(session, 0, {0x01}));
134}
Patrick Venture16ab2a12019-05-31 08:52:51 -0700135
136/*
137 * write(session)
138 */
Patrick Venture575118d2019-05-31 11:20:35 -0700139TEST_F(FirmwareHandlerUpdateStartedTest, WriteToUpdateBlobReturnsFailure)
140{
141 getToUpdateStarted();
142 EXPECT_FALSE(handler->write(session, 0, {0x01}));
143}
Patrick Venture16ab2a12019-05-31 08:52:51 -0700144
145/*
146 * read(session)
147 */
Patrick Venture73e2bdf2019-05-31 11:21:52 -0700148TEST_F(FirmwareHandlerUpdateStartedTest, ReadFromUpdateBlobReturnsEmpty)
149{
150 getToUpdateStarted();
151 EXPECT_THAT(handler->read(session, 0, 1), IsEmpty());
152}
Patrick Venture16ab2a12019-05-31 08:52:51 -0700153
154/*
Patrick Venture0079d892019-05-31 11:27:44 -0700155 * commit(session)
156 */
157TEST_F(FirmwareHandlerUpdateStartedTest,
158 CallingCommitShouldReturnTrueAndHaveNoEffect)
159{
160 getToUpdateStarted();
Patrick Venture1d66fe62019-06-03 14:57:27 -0700161 EXPECT_CALL(*updateMockPtr, trigger()).Times(0);
Patrick Venture0079d892019-05-31 11:27:44 -0700162
163 EXPECT_TRUE(handler->commit(session, {}));
164 expectedState(FirmwareBlobHandler::UpdateState::updateStarted);
165}
166
167/*
Patrick Ventureb6ce52b2019-05-31 11:16:41 -0700168 * stat(session) - this will trigger a check, and the state may change.
169 */
Patrick Venturef1f0f652019-06-03 09:10:19 -0700170TEST_F(FirmwareHandlerUpdateStartedTest,
Patrick Ventureda66fd82019-06-03 11:11:24 -0700171 CallStatChecksActionStatusReturnsRunningDoesNotChangeState)
Patrick Venturef1f0f652019-06-03 09:10:19 -0700172{
173 getToUpdateStarted();
174 EXPECT_CALL(*updateMockPtr, status())
Patrick Ventureda66fd82019-06-03 11:11:24 -0700175 .WillOnce(Return(ActionStatus::running));
Patrick Venturef1f0f652019-06-03 09:10:19 -0700176
177 blobs::BlobMeta meta, expectedMeta = {};
178 expectedMeta.size = 0;
179 expectedMeta.blobState = flags | blobs::StateFlags::committing;
180 expectedMeta.metadata.push_back(
Patrick Ventureda66fd82019-06-03 11:11:24 -0700181 static_cast<std::uint8_t>(ActionStatus::running));
Patrick Venturef1f0f652019-06-03 09:10:19 -0700182
183 EXPECT_TRUE(handler->stat(session, &meta));
184 EXPECT_EQ(expectedMeta, meta);
185 expectedState(FirmwareBlobHandler::UpdateState::updateStarted);
186}
187
188TEST_F(FirmwareHandlerUpdateStartedTest,
Patrick Ventureda66fd82019-06-03 11:11:24 -0700189 CallStatChecksActionStatusReturnsFailedChangesStateToCompleted)
Patrick Venturef1f0f652019-06-03 09:10:19 -0700190{
191 getToUpdateStarted();
192 EXPECT_CALL(*updateMockPtr, status())
Patrick Ventureda66fd82019-06-03 11:11:24 -0700193 .WillOnce(Return(ActionStatus::failed));
Patrick Venturef1f0f652019-06-03 09:10:19 -0700194
195 blobs::BlobMeta meta, expectedMeta = {};
196 expectedMeta.size = 0;
197 expectedMeta.blobState = flags | blobs::StateFlags::commit_error;
198 expectedMeta.metadata.push_back(
Patrick Ventureda66fd82019-06-03 11:11:24 -0700199 static_cast<std::uint8_t>(ActionStatus::failed));
Patrick Venturef1f0f652019-06-03 09:10:19 -0700200
201 EXPECT_TRUE(handler->stat(session, &meta));
202 EXPECT_EQ(expectedMeta, meta);
203 expectedState(FirmwareBlobHandler::UpdateState::updateCompleted);
204}
205
206TEST_F(FirmwareHandlerUpdateStartedTest,
Patrick Ventureda66fd82019-06-03 11:11:24 -0700207 CallStatChecksActionStatusReturnsSuccessChangesStateToCompleted)
Patrick Venturef1f0f652019-06-03 09:10:19 -0700208{
209 getToUpdateStarted();
210 EXPECT_CALL(*updateMockPtr, status())
Patrick Ventureda66fd82019-06-03 11:11:24 -0700211 .WillOnce(Return(ActionStatus::success));
Patrick Venturef1f0f652019-06-03 09:10:19 -0700212
213 blobs::BlobMeta meta, expectedMeta = {};
214 expectedMeta.size = 0;
215 expectedMeta.blobState = flags | blobs::StateFlags::committed;
216 expectedMeta.metadata.push_back(
Patrick Ventureda66fd82019-06-03 11:11:24 -0700217 static_cast<std::uint8_t>(ActionStatus::success));
Patrick Venturef1f0f652019-06-03 09:10:19 -0700218
219 EXPECT_TRUE(handler->stat(session, &meta));
220 EXPECT_EQ(expectedMeta, meta);
221 expectedState(FirmwareBlobHandler::UpdateState::updateCompleted);
222}
Patrick Ventureb6ce52b2019-05-31 11:16:41 -0700223
224/*
Patrick Venture49731712019-06-17 10:04:02 -0700225 * close(session) - this will abort.
Patrick Ventureb6ce52b2019-05-31 11:16:41 -0700226 */
Patrick Venture49731712019-06-17 10:04:02 -0700227TEST_F(FirmwareHandlerUpdateStartedTest, CloseOnUpdateDuringUpdateAbortsProcess)
228{
229 getToUpdateStarted();
230 EXPECT_CALL(*updateMockPtr, abort()).Times(1);
231
232 EXPECT_TRUE(handler->close(session));
233
Patrick Venture49731712019-06-17 10:04:02 -0700234 EXPECT_THAT(handler->getBlobIds(),
Patrick Venture9a69f732019-06-17 14:05:13 -0700235 UnorderedElementsAreArray(startingBlobs));
Patrick Venture49731712019-06-17 10:04:02 -0700236
237 expectedState(FirmwareBlobHandler::UpdateState::notYetStarted);
238}
Patrick Ventureb6ce52b2019-05-31 11:16:41 -0700239
Patrick Venture16ab2a12019-05-31 08:52:51 -0700240} // namespace
241} // namespace ipmi_flash