blob: 7136223e9dc674940d6adcdff97e6765978abc7a [file] [log] [blame]
Patrick Venture2098ff92019-06-03 10:18:05 -07001/* The goal of these tests is to verify the behavior of all blob commands given
2 * the current state is UpdateCompleted. This state is achieved as an exit from
3 * updateStarted.
4 *
5 * This can be reached with success or failure from an update, and is reached
6 * via a stat() call from updatedStarted.
7 */
Patrick Ventureab1e9622019-06-03 10:45:06 -07008#include "firmware_handler.hpp"
9#include "firmware_unittest.hpp"
10#include "status.hpp"
11#include "util.hpp"
12
13#include <string>
14#include <vector>
15
16#include <gtest/gtest.h>
17
18namespace ipmi_flash
19{
20namespace
21{
22
Patrick Venture7441ab72019-06-05 07:44:38 -070023using ::testing::IsEmpty;
Patrick Venture0c6daf42019-06-05 09:02:50 -070024using ::testing::UnorderedElementsAreArray;
Patrick Venture7441ab72019-06-05 07:44:38 -070025
Patrick Ventureab1e9622019-06-03 10:45:06 -070026/*
27 * There are the following calls (parameters may vary):
28 * canHandleBlob(blob)
29 * getBlobIds
30 * deleteBlob(blob)
31 * stat(blob)
32 * stat(session)
33 * open(blob)
34 * close(session)
35 * writemeta(session)
36 * write(session)
37 * read(session)
38 * commit(session)
39 */
40
41class FirmwareHandlerUpdateCompletedTest : public IpmiOnlyFirmwareStaticTest
Patrick Venture9b37b092020-05-28 20:58:57 -070042{};
Patrick Ventureab1e9622019-06-03 10:45:06 -070043
44/*
45 * open(blob)
46 */
47TEST_F(FirmwareHandlerUpdateCompletedTest,
48 AttemptToOpenFilesReturnsFailureAfterSuccess)
49{
50 /* In state updateCompleted a file is open, which means no others can be. */
Patrick Ventureda66fd82019-06-03 11:11:24 -070051 getToUpdateCompleted(ActionStatus::success);
Patrick Ventureab1e9622019-06-03 10:45:06 -070052
53 auto blobsToOpen = handler->getBlobIds();
54 for (const auto& blob : blobsToOpen)
55 {
56 EXPECT_FALSE(handler->open(session + 1, flags, blob));
57 }
58}
59
60/*
Patrick Venturea2d82392019-06-03 10:55:17 -070061 * stat(session)
62 */
63TEST_F(FirmwareHandlerUpdateCompletedTest,
64 CallingStatSessionAfterCompletedSuccessReturnsStateWithoutRechecking)
65{
Patrick Ventureda66fd82019-06-03 11:11:24 -070066 getToUpdateCompleted(ActionStatus::success);
Patrick Venturea2d82392019-06-03 10:55:17 -070067 EXPECT_CALL(*updateMockPtr, status()).Times(0);
68
69 blobs::BlobMeta meta, expectedMeta = {};
70 expectedMeta.size = 0;
71 expectedMeta.blobState = flags | blobs::StateFlags::committed;
72 expectedMeta.metadata.push_back(
Patrick Ventureda66fd82019-06-03 11:11:24 -070073 static_cast<std::uint8_t>(ActionStatus::success));
Patrick Venturea2d82392019-06-03 10:55:17 -070074
75 EXPECT_TRUE(handler->stat(session, &meta));
76 EXPECT_EQ(expectedMeta, meta);
77 expectedState(FirmwareBlobHandler::UpdateState::updateCompleted);
78}
79
80TEST_F(FirmwareHandlerUpdateCompletedTest,
81 CallingStatSessionAfterCompletedFailureReturnsStateWithoutRechecking)
82{
Patrick Ventureda66fd82019-06-03 11:11:24 -070083 getToUpdateCompleted(ActionStatus::failed);
Patrick Venturea2d82392019-06-03 10:55:17 -070084 EXPECT_CALL(*updateMockPtr, status()).Times(0);
85
86 blobs::BlobMeta meta, expectedMeta = {};
87 expectedMeta.size = 0;
88 expectedMeta.blobState = flags | blobs::StateFlags::commit_error;
89 expectedMeta.metadata.push_back(
Patrick Ventureda66fd82019-06-03 11:11:24 -070090 static_cast<std::uint8_t>(ActionStatus::failed));
Patrick Venturea2d82392019-06-03 10:55:17 -070091
92 EXPECT_TRUE(handler->stat(session, &meta));
93 EXPECT_EQ(expectedMeta, meta);
94 expectedState(FirmwareBlobHandler::UpdateState::updateCompleted);
95}
96
97/*
Patrick Venture25dfaff2019-06-03 13:17:22 -070098 * stat(blob)
99 */
100TEST_F(FirmwareHandlerUpdateCompletedTest, StatOnActiveImageReturnsFailure)
101{
102 getToUpdateCompleted(ActionStatus::success);
103
104 ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId));
105
106 blobs::BlobMeta meta;
107 EXPECT_FALSE(handler->stat(activeImageBlobId, &meta));
108}
109
110TEST_F(FirmwareHandlerUpdateCompletedTest, StatOnUpdateBlobReturnsFailure)
111{
112 getToUpdateCompleted(ActionStatus::success);
113
114 ASSERT_TRUE(handler->canHandleBlob(updateBlobId));
115
116 blobs::BlobMeta meta;
117 EXPECT_FALSE(handler->stat(updateBlobId, &meta));
118}
119
120TEST_F(FirmwareHandlerUpdateCompletedTest, StatOnNormalBlobsReturnsSuccess)
121{
122 getToUpdateCompleted(ActionStatus::success);
123
Patrick Venture25dfaff2019-06-03 13:17:22 -0700124 std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId};
125 for (const auto& blob : testBlobs)
126 {
127 ASSERT_TRUE(handler->canHandleBlob(blob));
128
129 blobs::BlobMeta meta = {};
130 EXPECT_TRUE(handler->stat(blob, &meta));
Benjamin Fair12901982019-11-12 13:55:46 -0800131 EXPECT_EQ(expectedIdleMeta, meta);
Patrick Venture25dfaff2019-06-03 13:17:22 -0700132 }
133}
134
135/*
Patrick Venture58202b22019-06-03 13:38:09 -0700136 * writemeta(session)
137 */
138TEST_F(FirmwareHandlerUpdateCompletedTest, WriteMetaToUpdateBlobReturnsFailure)
139{
140 getToUpdateCompleted(ActionStatus::success);
141
142 EXPECT_FALSE(handler->writeMeta(session, 0, {0x01}));
143}
144
145/*
Patrick Venture254b4cf2019-06-03 13:44:49 -0700146 * write(session)
147 */
148TEST_F(FirmwareHandlerUpdateCompletedTest, WriteToUpdateBlobReturnsFailure)
149{
150 getToUpdateCompleted(ActionStatus::success);
151
152 EXPECT_FALSE(handler->write(session, 0, {0x01}));
153}
154
155/*
Patrick Venturefdeb8642019-06-03 14:30:34 -0700156 * commit(session) - returns failure
157 */
158TEST_F(FirmwareHandlerUpdateCompletedTest,
159 CommitOnUpdateBlobAfterSuccessReturnsFailure)
160{
161 getToUpdateCompleted(ActionStatus::success);
162
Patrick Venture1d66fe62019-06-03 14:57:27 -0700163 EXPECT_CALL(*updateMockPtr, trigger()).Times(0);
Patrick Venturefdeb8642019-06-03 14:30:34 -0700164 EXPECT_FALSE(handler->commit(session, {}));
165}
166
167TEST_F(FirmwareHandlerUpdateCompletedTest,
168 CommitOnUpdateBlobAfterFailureReturnsFailure)
169{
170 getToUpdateCompleted(ActionStatus::failed);
171
Patrick Venture1d66fe62019-06-03 14:57:27 -0700172 EXPECT_CALL(*updateMockPtr, trigger()).Times(0);
Patrick Venturefdeb8642019-06-03 14:30:34 -0700173 EXPECT_FALSE(handler->commit(session, {}));
174}
175
176/*
Patrick Venture7441ab72019-06-05 07:44:38 -0700177 * read(session) - nothing to read here.
178 */
179TEST_F(FirmwareHandlerUpdateCompletedTest, ReadingFromUpdateBlobReturnsNothing)
180{
181 getToUpdateCompleted(ActionStatus::success);
182
183 EXPECT_THAT(handler->read(session, 0, 1), IsEmpty());
184}
185
186/*
Patrick Ventureab1e9622019-06-03 10:45:06 -0700187 * getBlobIds
Patrick Venture0c6daf42019-06-05 09:02:50 -0700188 * canHandleBlob(blob)
189 */
190TEST_F(FirmwareHandlerUpdateCompletedTest, GetBlobListProvidesExpectedBlobs)
191{
192 getToUpdateCompleted(ActionStatus::success);
193
Patrick Venture9a69f732019-06-17 14:05:13 -0700194 EXPECT_THAT(
195 handler->getBlobIds(),
196 UnorderedElementsAreArray(
197 {updateBlobId, hashBlobId, activeImageBlobId, staticLayoutBlobId}));
Patrick Venture0c6daf42019-06-05 09:02:50 -0700198}
199
200/*
Patrick Venture85ae86b2019-06-05 09:18:40 -0700201 * close(session) - closes everything out and returns to state not-yet-started.
202 * It doesn't go and clean out any update artifacts that may be present on the
203 * system. It's up to the update implementation to deal with this before
204 * marking complete.
205 */
206TEST_F(FirmwareHandlerUpdateCompletedTest,
207 ClosingOnUpdateBlobIdAfterSuccessReturnsToNotYetStartedAndCleansBlobList)
208{
209 getToUpdateCompleted(ActionStatus::success);
210
211 handler->close(session);
212 expectedState(FirmwareBlobHandler::UpdateState::notYetStarted);
213
Patrick Venture9a69f732019-06-17 14:05:13 -0700214 EXPECT_THAT(handler->getBlobIds(),
215 UnorderedElementsAreArray(startingBlobs));
Patrick Venture85ae86b2019-06-05 09:18:40 -0700216}
217
218TEST_F(FirmwareHandlerUpdateCompletedTest,
219 ClosingOnUpdateBlobIdAfterFailureReturnsToNotYetStartedAndCleansBlobList)
220{
221 getToUpdateCompleted(ActionStatus::failed);
222
223 handler->close(session);
224 expectedState(FirmwareBlobHandler::UpdateState::notYetStarted);
225
Patrick Venture9a69f732019-06-17 14:05:13 -0700226 EXPECT_THAT(handler->getBlobIds(),
227 UnorderedElementsAreArray(startingBlobs));
Patrick Venture85ae86b2019-06-05 09:18:40 -0700228}
229
230/*
Patrick Venturebcc0c772019-06-17 10:42:06 -0700231 * deleteBlob(blob)
Patrick Ventureab1e9622019-06-03 10:45:06 -0700232 */
Patrick Venturebcc0c772019-06-17 10:42:06 -0700233TEST_F(FirmwareHandlerUpdateCompletedTest, DeleteBlobReturnsFalse)
234{
235 /* Try deleting all blobs, it doesn't really matter which though because you
236 * cannot close out an open session, therefore you must fail to delete
237 * anything unless everything is closed.
238 */
239 getToUpdateCompleted(ActionStatus::success);
240
241 auto blobs = handler->getBlobIds();
242 for (const auto& b : blobs)
243 {
244 EXPECT_FALSE(handler->deleteBlob(b));
245 }
246}
Patrick Ventureab1e9622019-06-03 10:45:06 -0700247
248} // namespace
249} // namespace ipmi_flash