blob: 420d7a7ddeb34b44f2757a603aa96e0a4f8dc7b4 [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
66 std::vector<std::string> expected = {updateBlobId, hashBlobId,
67 activeImageBlobId, staticLayoutBlobId};
68 EXPECT_THAT(handler->getBlobIds(), UnorderedElementsAreArray(expected));
69}
Patrick Venture16ab2a12019-05-31 08:52:51 -070070
71/*
72 * TODO: deleteBlob(blob)
73 */
74
75/*
76 * stat(blob)
77 */
Patrick Venturefc34cfa2019-05-31 11:02:51 -070078TEST_F(FirmwareHandlerUpdateStartedTest, StatOnActiveImageReturnsFailure)
79{
80 getToUpdateStarted();
81
82 ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId));
83
84 blobs::BlobMeta meta;
85 EXPECT_FALSE(handler->stat(activeImageBlobId, &meta));
86}
87
88TEST_F(FirmwareHandlerUpdateStartedTest, StatOnUpdateBlobReturnsFailure)
89{
90 getToUpdateStarted();
91
92 ASSERT_TRUE(handler->canHandleBlob(updateBlobId));
93
94 blobs::BlobMeta meta;
95 EXPECT_FALSE(handler->stat(updateBlobId, &meta));
96}
97
98TEST_F(FirmwareHandlerUpdateStartedTest, StatOnNormalBlobsReturnsSuccess)
99{
100 getToUpdateStarted();
101
102 blobs::BlobMeta expected;
103 expected.blobState = FirmwareBlobHandler::UpdateFlags::ipmi;
104 expected.size = 0;
105
106 std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId};
107 for (const auto& blob : testBlobs)
108 {
109 ASSERT_TRUE(handler->canHandleBlob(blob));
110
111 blobs::BlobMeta meta = {};
112 EXPECT_TRUE(handler->stat(blob, &meta));
113 EXPECT_EQ(expected, meta);
114 }
115}
Patrick Venture16ab2a12019-05-31 08:52:51 -0700116
117/*
Patrick Venture16ab2a12019-05-31 08:52:51 -0700118 * writemeta(session)
119 */
Patrick Ventureb6ce52b2019-05-31 11:16:41 -0700120TEST_F(FirmwareHandlerUpdateStartedTest, WriteMetaToUpdateBlobReturnsFailure)
121{
122 getToUpdateStarted();
123 EXPECT_FALSE(handler->writeMeta(session, 0, {0x01}));
124}
Patrick Venture16ab2a12019-05-31 08:52:51 -0700125
126/*
127 * write(session)
128 */
Patrick Venture575118d2019-05-31 11:20:35 -0700129TEST_F(FirmwareHandlerUpdateStartedTest, WriteToUpdateBlobReturnsFailure)
130{
131 getToUpdateStarted();
132 EXPECT_FALSE(handler->write(session, 0, {0x01}));
133}
Patrick Venture16ab2a12019-05-31 08:52:51 -0700134
135/*
136 * read(session)
137 */
Patrick Venture73e2bdf2019-05-31 11:21:52 -0700138TEST_F(FirmwareHandlerUpdateStartedTest, ReadFromUpdateBlobReturnsEmpty)
139{
140 getToUpdateStarted();
141 EXPECT_THAT(handler->read(session, 0, 1), IsEmpty());
142}
Patrick Venture16ab2a12019-05-31 08:52:51 -0700143
144/*
Patrick Venture0079d892019-05-31 11:27:44 -0700145 * commit(session)
146 */
147TEST_F(FirmwareHandlerUpdateStartedTest,
148 CallingCommitShouldReturnTrueAndHaveNoEffect)
149{
150 getToUpdateStarted();
Patrick Venture1d66fe62019-06-03 14:57:27 -0700151 EXPECT_CALL(*updateMockPtr, trigger()).Times(0);
Patrick Venture0079d892019-05-31 11:27:44 -0700152
153 EXPECT_TRUE(handler->commit(session, {}));
154 expectedState(FirmwareBlobHandler::UpdateState::updateStarted);
155}
156
157/*
Patrick Ventureb6ce52b2019-05-31 11:16:41 -0700158 * stat(session) - this will trigger a check, and the state may change.
159 */
Patrick Venturef1f0f652019-06-03 09:10:19 -0700160TEST_F(FirmwareHandlerUpdateStartedTest,
Patrick Ventureda66fd82019-06-03 11:11:24 -0700161 CallStatChecksActionStatusReturnsRunningDoesNotChangeState)
Patrick Venturef1f0f652019-06-03 09:10:19 -0700162{
163 getToUpdateStarted();
164 EXPECT_CALL(*updateMockPtr, status())
Patrick Ventureda66fd82019-06-03 11:11:24 -0700165 .WillOnce(Return(ActionStatus::running));
Patrick Venturef1f0f652019-06-03 09:10:19 -0700166
167 blobs::BlobMeta meta, expectedMeta = {};
168 expectedMeta.size = 0;
169 expectedMeta.blobState = flags | blobs::StateFlags::committing;
170 expectedMeta.metadata.push_back(
Patrick Ventureda66fd82019-06-03 11:11:24 -0700171 static_cast<std::uint8_t>(ActionStatus::running));
Patrick Venturef1f0f652019-06-03 09:10:19 -0700172
173 EXPECT_TRUE(handler->stat(session, &meta));
174 EXPECT_EQ(expectedMeta, meta);
175 expectedState(FirmwareBlobHandler::UpdateState::updateStarted);
176}
177
178TEST_F(FirmwareHandlerUpdateStartedTest,
Patrick Ventureda66fd82019-06-03 11:11:24 -0700179 CallStatChecksActionStatusReturnsFailedChangesStateToCompleted)
Patrick Venturef1f0f652019-06-03 09:10:19 -0700180{
181 getToUpdateStarted();
182 EXPECT_CALL(*updateMockPtr, status())
Patrick Ventureda66fd82019-06-03 11:11:24 -0700183 .WillOnce(Return(ActionStatus::failed));
Patrick Venturef1f0f652019-06-03 09:10:19 -0700184
185 blobs::BlobMeta meta, expectedMeta = {};
186 expectedMeta.size = 0;
187 expectedMeta.blobState = flags | blobs::StateFlags::commit_error;
188 expectedMeta.metadata.push_back(
Patrick Ventureda66fd82019-06-03 11:11:24 -0700189 static_cast<std::uint8_t>(ActionStatus::failed));
Patrick Venturef1f0f652019-06-03 09:10:19 -0700190
191 EXPECT_TRUE(handler->stat(session, &meta));
192 EXPECT_EQ(expectedMeta, meta);
193 expectedState(FirmwareBlobHandler::UpdateState::updateCompleted);
194}
195
196TEST_F(FirmwareHandlerUpdateStartedTest,
Patrick Ventureda66fd82019-06-03 11:11:24 -0700197 CallStatChecksActionStatusReturnsSuccessChangesStateToCompleted)
Patrick Venturef1f0f652019-06-03 09:10:19 -0700198{
199 getToUpdateStarted();
200 EXPECT_CALL(*updateMockPtr, status())
Patrick Ventureda66fd82019-06-03 11:11:24 -0700201 .WillOnce(Return(ActionStatus::success));
Patrick Venturef1f0f652019-06-03 09:10:19 -0700202
203 blobs::BlobMeta meta, expectedMeta = {};
204 expectedMeta.size = 0;
205 expectedMeta.blobState = flags | blobs::StateFlags::committed;
206 expectedMeta.metadata.push_back(
Patrick Ventureda66fd82019-06-03 11:11:24 -0700207 static_cast<std::uint8_t>(ActionStatus::success));
Patrick Venturef1f0f652019-06-03 09:10:19 -0700208
209 EXPECT_TRUE(handler->stat(session, &meta));
210 EXPECT_EQ(expectedMeta, meta);
211 expectedState(FirmwareBlobHandler::UpdateState::updateCompleted);
212}
Patrick Ventureb6ce52b2019-05-31 11:16:41 -0700213
214/*
215 * TODO: close(session) - this will abort.
216 */
217
Patrick Venture16ab2a12019-05-31 08:52:51 -0700218} // namespace
219} // namespace ipmi_flash