blob: 31c88e9073fae51c4ea8a36fdeeff00bd88f182f [file] [log] [blame]
Patrick Venture0cd945c2019-05-30 13:36:53 -07001/* The goal of these tests is to verify the behavior of all blob commands given
2 * the current state is updatePending. This state is achieved as an exit from
3 * verificationCompleted.
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 Ventureebe456f2019-05-30 16:40:39 -070021using ::testing::IsEmpty;
Patrick Venture0cd945c2019-05-30 13:36:53 -070022using ::testing::Return;
Patrick Venturead933832019-05-30 14:13:29 -070023using ::testing::UnorderedElementsAreArray;
Patrick Venture0cd945c2019-05-30 13:36:53 -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 * Testing canHandleBlob is uninteresting in this state. Getting the BlobIDs
40 * will inform what canHandleBlob will return.
41 */
42
43class FirmwareHandlerUpdatePendingTest : public IpmiOnlyFirmwareStaticTest
44{
Patrick Venture0cd945c2019-05-30 13:36:53 -070045};
46
47/*
48 * There are the following calls (parameters may vary):
49 * canHandleBlob(blob)
50 * getBlobIds
51 */
Patrick Venturead933832019-05-30 14:13:29 -070052TEST_F(FirmwareHandlerUpdatePendingTest, GetBlobsListHasExpectedValues)
53{
54 getToUpdatePending();
55
56 std::vector<std::string> expected = {updateBlobId, activeImageBlobId,
57 hashBlobId, staticLayoutBlobId};
58 EXPECT_THAT(handler->getBlobIds(), UnorderedElementsAreArray(expected));
59}
60
Patrick Venture0cd945c2019-05-30 13:36:53 -070061/*
Patrick Venture19f5d882019-05-30 14:34:55 -070062 * open(blob) - because updatePending is in a fileOpen==false state, one can
63 * then open blobs. However, because we're in a special state, we will restrict
64 * them s.t. they can only open the updateBlobId.
65 */
66TEST_F(FirmwareHandlerUpdatePendingTest,
67 OpenUpdateBlobIdIsSuccessfulAndDoesNotChangeState)
68{
69 getToUpdatePending();
70
71 /* Opening the update blob isn't interesting, except it's required for
72 * commit() which triggers the update process.
73 */
74 EXPECT_TRUE(handler->open(session, flags, updateBlobId));
Patrick Venture6b0aa182019-05-30 14:47:32 -070075 expectedState(FirmwareBlobHandler::UpdateState::updatePending);
Patrick Venture19f5d882019-05-30 14:34:55 -070076}
77
78TEST_F(FirmwareHandlerUpdatePendingTest, OpenAnyBlobOtherThanUpdateFails)
79{
80 getToUpdatePending();
81
82 auto blobs = handler->getBlobIds();
83 for (const auto& blob : blobs)
84 {
85 if (blob == updateBlobId)
86 {
87 continue;
88 }
89 EXPECT_FALSE(handler->open(session, flags, blob));
90 }
91}
92
93/*
Patrick Venture6b0aa182019-05-30 14:47:32 -070094 * close(session) - close from this state is uninteresting.
95 */
96TEST_F(FirmwareHandlerUpdatePendingTest, CloseUpdateBlobDoesNotChangeState)
97{
98 /* Verify nothing changes when one just opens, then closes the updateBlobId.
99 */
100 getToUpdatePending();
101
102 EXPECT_TRUE(handler->open(session, flags, updateBlobId));
103
104 handler->close(session);
105
106 expectedState(FirmwareBlobHandler::UpdateState::updatePending);
107 EXPECT_TRUE(handler->canHandleBlob(updateBlobId));
108}
109
110/*
Patrick Venture94bb9702019-05-30 14:53:22 -0700111 * writemeta(session) - this will return failure.
112 */
113TEST_F(FirmwareHandlerUpdatePendingTest, WriteMetaToUpdateBlobReturnsFailure)
114{
115 getToUpdatePending();
116
117 EXPECT_TRUE(handler->open(session, flags, updateBlobId));
118 EXPECT_FALSE(handler->writeMeta(session, 0, {0x01}));
119}
120
121/*
Patrick Venture4e2fdcd2019-05-30 14:58:57 -0700122 * write(session)
123 */
124TEST_F(FirmwareHandlerUpdatePendingTest, WriteToUpdateBlobReturnsFailure)
125{
126 getToUpdatePending();
127
128 EXPECT_TRUE(handler->open(session, flags, updateBlobId));
129 EXPECT_FALSE(handler->write(session, 0, {0x01}));
130}
131
132/*
Patrick Ventureebe456f2019-05-30 16:40:39 -0700133 * read(session)
134 */
135TEST_F(FirmwareHandlerUpdatePendingTest, ReadFromUpdateBlobIdReturnsEmpty)
136{
137 getToUpdatePending();
138 EXPECT_THAT(handler->read(session, 0, 1), IsEmpty());
139}
140
141/*
Patrick Venture5f562692019-05-30 16:49:46 -0700142 * stat(blob)
Patrick Venture0cd945c2019-05-30 13:36:53 -0700143 */
Patrick Venture5f562692019-05-30 16:49:46 -0700144TEST_F(FirmwareHandlerUpdatePendingTest, StatOnActiveImageReturnsFailure)
145{
146 getToUpdatePending();
147 ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId));
148
149 blobs::BlobMeta meta;
150 EXPECT_FALSE(handler->stat(activeImageBlobId, &meta));
151}
152
153TEST_F(FirmwareHandlerUpdatePendingTest, StatOnUpdateBlobReturnsFailure)
154{
155 getToUpdatePending();
156 ASSERT_TRUE(handler->canHandleBlob(updateBlobId));
157
158 blobs::BlobMeta meta;
159 EXPECT_FALSE(handler->stat(updateBlobId, &meta));
160}
161
162TEST_F(FirmwareHandlerUpdatePendingTest, StatOnNormalBlobsReturnsSuccess)
163{
164 getToUpdatePending();
165
166 blobs::BlobMeta expected;
167 expected.blobState = FirmwareBlobHandler::UpdateFlags::ipmi;
168 expected.size = 0;
169
170 std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId};
171 for (const auto& blob : testBlobs)
172 {
173 ASSERT_TRUE(handler->canHandleBlob(blob));
174
175 blobs::BlobMeta meta = {};
176 EXPECT_TRUE(handler->stat(blob, &meta));
177 EXPECT_EQ(expected, meta);
178 }
179}
Patrick Venture0cd945c2019-05-30 13:36:53 -0700180
181/*
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700182 * stat(session)
183 * In this case, you can open updateBlobId without changing state, therefore,
184 * let's call stat() against a session against this file. This done, ahead of
185 * commit() should report the state as "other."
Patrick Venture0cd945c2019-05-30 13:36:53 -0700186 */
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700187TEST_F(FirmwareHandlerUpdatePendingTest,
188 SessionStatOnUpdateBlobIdReturnsFailure)
189{
190 getToUpdatePending();
191 EXPECT_TRUE(handler->open(session, flags, updateBlobId));
192 expectedState(FirmwareBlobHandler::UpdateState::updatePending);
193
194 blobs::BlobMeta meta, expectedMeta = {};
195 expectedMeta.size = 0;
196 expectedMeta.blobState = flags;
197 expectedMeta.metadata.push_back(
Patrick Ventureda66fd82019-06-03 11:11:24 -0700198 static_cast<std::uint8_t>(ActionStatus::unknown));
Patrick Venture40d7ffc2019-05-30 17:12:06 -0700199
200 EXPECT_TRUE(handler->stat(session, &meta));
201 EXPECT_EQ(expectedMeta, meta);
202 expectedState(FirmwareBlobHandler::UpdateState::updatePending);
203}
Patrick Venture0cd945c2019-05-30 13:36:53 -0700204
205/*
Patrick Venture1a406fe2019-05-31 07:29:56 -0700206 * commit(session)
Patrick Venture0cd945c2019-05-30 13:36:53 -0700207 */
Patrick Venture1a406fe2019-05-31 07:29:56 -0700208TEST_F(FirmwareHandlerUpdatePendingTest,
209 CommitOnUpdateBlobTriggersUpdateAndChangesState)
210{
211 /* Commit triggers the update mechanism (similarly for the verifyBlobId) and
212 * changes state to updateStarted.
213 */
214 getToUpdatePending();
215 EXPECT_TRUE(handler->open(session, flags, updateBlobId));
216 expectedState(FirmwareBlobHandler::UpdateState::updatePending);
217
Patrick Venture1d66fe62019-06-03 14:57:27 -0700218 EXPECT_CALL(*updateMockPtr, trigger()).WillOnce(Return(true));
Patrick Venture1a406fe2019-05-31 07:29:56 -0700219
220 EXPECT_TRUE(handler->commit(session, {}));
221 expectedState(FirmwareBlobHandler::UpdateState::updateStarted);
222}
223
224TEST_F(FirmwareHandlerUpdatePendingTest,
225 CommitOnUpdateBlobTriggersUpdateAndReturnsFailureDoesNotChangeState)
226{
227 getToUpdatePending();
228 EXPECT_TRUE(handler->open(session, flags, updateBlobId));
229 expectedState(FirmwareBlobHandler::UpdateState::updatePending);
230
Patrick Venture1d66fe62019-06-03 14:57:27 -0700231 EXPECT_CALL(*updateMockPtr, trigger()).WillOnce(Return(false));
Patrick Venture1a406fe2019-05-31 07:29:56 -0700232
233 EXPECT_FALSE(handler->commit(session, {}));
234 expectedState(FirmwareBlobHandler::UpdateState::updatePending);
235}
Patrick Venture0cd945c2019-05-30 13:36:53 -0700236
237/*
Patrick Venture1a406fe2019-05-31 07:29:56 -0700238 * TODO: deleteBlob(blob)
Patrick Venture0cd945c2019-05-30 13:36:53 -0700239 */
240
241} // namespace
242} // namespace ipmi_flash