blob: 237e0e97047503b0342d7a133641488ad1487e7a [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
116 blobs::BlobMeta expected;
117 expected.blobState = FirmwareBlobHandler::UpdateFlags::ipmi;
118 expected.size = 0;
119
120 std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId};
121 for (const auto& blob : testBlobs)
122 {
123 ASSERT_TRUE(handler->canHandleBlob(blob));
124
125 blobs::BlobMeta meta = {};
126 EXPECT_TRUE(handler->stat(blob, &meta));
127 EXPECT_EQ(expected, meta);
128 }
129}
Patrick Venture16ab2a12019-05-31 08:52:51 -0700130
131/*
Patrick Venture16ab2a12019-05-31 08:52:51 -0700132 * writemeta(session)
133 */
Patrick Ventureb6ce52b2019-05-31 11:16:41 -0700134TEST_F(FirmwareHandlerUpdateStartedTest, WriteMetaToUpdateBlobReturnsFailure)
135{
136 getToUpdateStarted();
137 EXPECT_FALSE(handler->writeMeta(session, 0, {0x01}));
138}
Patrick Venture16ab2a12019-05-31 08:52:51 -0700139
140/*
141 * write(session)
142 */
Patrick Venture575118d2019-05-31 11:20:35 -0700143TEST_F(FirmwareHandlerUpdateStartedTest, WriteToUpdateBlobReturnsFailure)
144{
145 getToUpdateStarted();
146 EXPECT_FALSE(handler->write(session, 0, {0x01}));
147}
Patrick Venture16ab2a12019-05-31 08:52:51 -0700148
149/*
150 * read(session)
151 */
Patrick Venture73e2bdf2019-05-31 11:21:52 -0700152TEST_F(FirmwareHandlerUpdateStartedTest, ReadFromUpdateBlobReturnsEmpty)
153{
154 getToUpdateStarted();
155 EXPECT_THAT(handler->read(session, 0, 1), IsEmpty());
156}
Patrick Venture16ab2a12019-05-31 08:52:51 -0700157
158/*
Patrick Venture0079d892019-05-31 11:27:44 -0700159 * commit(session)
160 */
161TEST_F(FirmwareHandlerUpdateStartedTest,
162 CallingCommitShouldReturnTrueAndHaveNoEffect)
163{
164 getToUpdateStarted();
Patrick Venture1d66fe62019-06-03 14:57:27 -0700165 EXPECT_CALL(*updateMockPtr, trigger()).Times(0);
Patrick Venture0079d892019-05-31 11:27:44 -0700166
167 EXPECT_TRUE(handler->commit(session, {}));
168 expectedState(FirmwareBlobHandler::UpdateState::updateStarted);
169}
170
171/*
Patrick Ventureb6ce52b2019-05-31 11:16:41 -0700172 * stat(session) - this will trigger a check, and the state may change.
173 */
Patrick Venturef1f0f652019-06-03 09:10:19 -0700174TEST_F(FirmwareHandlerUpdateStartedTest,
Patrick Ventureda66fd82019-06-03 11:11:24 -0700175 CallStatChecksActionStatusReturnsRunningDoesNotChangeState)
Patrick Venturef1f0f652019-06-03 09:10:19 -0700176{
177 getToUpdateStarted();
178 EXPECT_CALL(*updateMockPtr, status())
Patrick Ventureda66fd82019-06-03 11:11:24 -0700179 .WillOnce(Return(ActionStatus::running));
Patrick Venturef1f0f652019-06-03 09:10:19 -0700180
181 blobs::BlobMeta meta, expectedMeta = {};
182 expectedMeta.size = 0;
183 expectedMeta.blobState = flags | blobs::StateFlags::committing;
184 expectedMeta.metadata.push_back(
Patrick Ventureda66fd82019-06-03 11:11:24 -0700185 static_cast<std::uint8_t>(ActionStatus::running));
Patrick Venturef1f0f652019-06-03 09:10:19 -0700186
187 EXPECT_TRUE(handler->stat(session, &meta));
188 EXPECT_EQ(expectedMeta, meta);
189 expectedState(FirmwareBlobHandler::UpdateState::updateStarted);
190}
191
192TEST_F(FirmwareHandlerUpdateStartedTest,
Patrick Ventureda66fd82019-06-03 11:11:24 -0700193 CallStatChecksActionStatusReturnsFailedChangesStateToCompleted)
Patrick Venturef1f0f652019-06-03 09:10:19 -0700194{
195 getToUpdateStarted();
196 EXPECT_CALL(*updateMockPtr, status())
Patrick Ventureda66fd82019-06-03 11:11:24 -0700197 .WillOnce(Return(ActionStatus::failed));
Patrick Venturef1f0f652019-06-03 09:10:19 -0700198
199 blobs::BlobMeta meta, expectedMeta = {};
200 expectedMeta.size = 0;
201 expectedMeta.blobState = flags | blobs::StateFlags::commit_error;
202 expectedMeta.metadata.push_back(
Patrick Ventureda66fd82019-06-03 11:11:24 -0700203 static_cast<std::uint8_t>(ActionStatus::failed));
Patrick Venturef1f0f652019-06-03 09:10:19 -0700204
205 EXPECT_TRUE(handler->stat(session, &meta));
206 EXPECT_EQ(expectedMeta, meta);
207 expectedState(FirmwareBlobHandler::UpdateState::updateCompleted);
208}
209
210TEST_F(FirmwareHandlerUpdateStartedTest,
Patrick Ventureda66fd82019-06-03 11:11:24 -0700211 CallStatChecksActionStatusReturnsSuccessChangesStateToCompleted)
Patrick Venturef1f0f652019-06-03 09:10:19 -0700212{
213 getToUpdateStarted();
214 EXPECT_CALL(*updateMockPtr, status())
Patrick Ventureda66fd82019-06-03 11:11:24 -0700215 .WillOnce(Return(ActionStatus::success));
Patrick Venturef1f0f652019-06-03 09:10:19 -0700216
217 blobs::BlobMeta meta, expectedMeta = {};
218 expectedMeta.size = 0;
219 expectedMeta.blobState = flags | blobs::StateFlags::committed;
220 expectedMeta.metadata.push_back(
Patrick Ventureda66fd82019-06-03 11:11:24 -0700221 static_cast<std::uint8_t>(ActionStatus::success));
Patrick Venturef1f0f652019-06-03 09:10:19 -0700222
223 EXPECT_TRUE(handler->stat(session, &meta));
224 EXPECT_EQ(expectedMeta, meta);
225 expectedState(FirmwareBlobHandler::UpdateState::updateCompleted);
226}
Patrick Ventureb6ce52b2019-05-31 11:16:41 -0700227
228/*
Patrick Venture49731712019-06-17 10:04:02 -0700229 * close(session) - this will abort.
Patrick Ventureb6ce52b2019-05-31 11:16:41 -0700230 */
Patrick Venture49731712019-06-17 10:04:02 -0700231TEST_F(FirmwareHandlerUpdateStartedTest, CloseOnUpdateDuringUpdateAbortsProcess)
232{
233 getToUpdateStarted();
234 EXPECT_CALL(*updateMockPtr, abort()).Times(1);
235
236 EXPECT_TRUE(handler->close(session));
237
Patrick Venture49731712019-06-17 10:04:02 -0700238 EXPECT_THAT(handler->getBlobIds(),
Patrick Venture9a69f732019-06-17 14:05:13 -0700239 UnorderedElementsAreArray(startingBlobs));
Patrick Venture49731712019-06-17 10:04:02 -0700240
241 expectedState(FirmwareBlobHandler::UpdateState::notYetStarted);
242}
Patrick Ventureb6ce52b2019-05-31 11:16:41 -0700243
Patrick Venture16ab2a12019-05-31 08:52:51 -0700244} // namespace
245} // namespace ipmi_flash