Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 1 | /* 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 | |
| 16 | namespace ipmi_flash |
| 17 | { |
| 18 | namespace |
| 19 | { |
| 20 | |
Patrick Venture | 73e2bdf | 2019-05-31 11:21:52 -0700 | [diff] [blame] | 21 | using ::testing::IsEmpty; |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 22 | using ::testing::Return; |
Patrick Venture | d9a9578 | 2019-05-31 10:46:56 -0700 | [diff] [blame] | 23 | using ::testing::UnorderedElementsAreArray; |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 24 | |
| 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 | |
| 40 | class FirmwareHandlerUpdateStartedTest : public IpmiOnlyFirmwareStaticTest |
| 41 | { |
| 42 | }; |
| 43 | |
Patrick Venture | f515b9c | 2019-05-31 10:38:16 -0700 | [diff] [blame] | 44 | /* |
| 45 | * open(blob) |
| 46 | */ |
| 47 | TEST_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 Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 59 | /* canHandleBlob(blob) |
| 60 | * getBlobIds |
| 61 | */ |
Patrick Venture | d9a9578 | 2019-05-31 10:46:56 -0700 | [diff] [blame] | 62 | TEST_F(FirmwareHandlerUpdateStartedTest, VerifyListOfBlobs) |
| 63 | { |
| 64 | getToUpdateStarted(); |
| 65 | |
Patrick Venture | 9a69f73 | 2019-06-17 14:05:13 -0700 | [diff] [blame^] | 66 | EXPECT_THAT( |
| 67 | handler->getBlobIds(), |
| 68 | UnorderedElementsAreArray( |
| 69 | {updateBlobId, hashBlobId, activeImageBlobId, staticLayoutBlobId})); |
Patrick Venture | d9a9578 | 2019-05-31 10:46:56 -0700 | [diff] [blame] | 70 | } |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 71 | |
| 72 | /* |
Patrick Venture | bcc0c77 | 2019-06-17 10:42:06 -0700 | [diff] [blame] | 73 | * deleteBlob(blob) |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 74 | */ |
Patrick Venture | bcc0c77 | 2019-06-17 10:42:06 -0700 | [diff] [blame] | 75 | TEST_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 Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 88 | |
| 89 | /* |
| 90 | * stat(blob) |
| 91 | */ |
Patrick Venture | fc34cfa | 2019-05-31 11:02:51 -0700 | [diff] [blame] | 92 | TEST_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 | |
| 102 | TEST_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 | |
| 112 | TEST_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 Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 130 | |
| 131 | /* |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 132 | * writemeta(session) |
| 133 | */ |
Patrick Venture | b6ce52b | 2019-05-31 11:16:41 -0700 | [diff] [blame] | 134 | TEST_F(FirmwareHandlerUpdateStartedTest, WriteMetaToUpdateBlobReturnsFailure) |
| 135 | { |
| 136 | getToUpdateStarted(); |
| 137 | EXPECT_FALSE(handler->writeMeta(session, 0, {0x01})); |
| 138 | } |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * write(session) |
| 142 | */ |
Patrick Venture | 575118d | 2019-05-31 11:20:35 -0700 | [diff] [blame] | 143 | TEST_F(FirmwareHandlerUpdateStartedTest, WriteToUpdateBlobReturnsFailure) |
| 144 | { |
| 145 | getToUpdateStarted(); |
| 146 | EXPECT_FALSE(handler->write(session, 0, {0x01})); |
| 147 | } |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 148 | |
| 149 | /* |
| 150 | * read(session) |
| 151 | */ |
Patrick Venture | 73e2bdf | 2019-05-31 11:21:52 -0700 | [diff] [blame] | 152 | TEST_F(FirmwareHandlerUpdateStartedTest, ReadFromUpdateBlobReturnsEmpty) |
| 153 | { |
| 154 | getToUpdateStarted(); |
| 155 | EXPECT_THAT(handler->read(session, 0, 1), IsEmpty()); |
| 156 | } |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 157 | |
| 158 | /* |
Patrick Venture | 0079d89 | 2019-05-31 11:27:44 -0700 | [diff] [blame] | 159 | * commit(session) |
| 160 | */ |
| 161 | TEST_F(FirmwareHandlerUpdateStartedTest, |
| 162 | CallingCommitShouldReturnTrueAndHaveNoEffect) |
| 163 | { |
| 164 | getToUpdateStarted(); |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 165 | EXPECT_CALL(*updateMockPtr, trigger()).Times(0); |
Patrick Venture | 0079d89 | 2019-05-31 11:27:44 -0700 | [diff] [blame] | 166 | |
| 167 | EXPECT_TRUE(handler->commit(session, {})); |
| 168 | expectedState(FirmwareBlobHandler::UpdateState::updateStarted); |
| 169 | } |
| 170 | |
| 171 | /* |
Patrick Venture | b6ce52b | 2019-05-31 11:16:41 -0700 | [diff] [blame] | 172 | * stat(session) - this will trigger a check, and the state may change. |
| 173 | */ |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 174 | TEST_F(FirmwareHandlerUpdateStartedTest, |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 175 | CallStatChecksActionStatusReturnsRunningDoesNotChangeState) |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 176 | { |
| 177 | getToUpdateStarted(); |
| 178 | EXPECT_CALL(*updateMockPtr, status()) |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 179 | .WillOnce(Return(ActionStatus::running)); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 180 | |
| 181 | blobs::BlobMeta meta, expectedMeta = {}; |
| 182 | expectedMeta.size = 0; |
| 183 | expectedMeta.blobState = flags | blobs::StateFlags::committing; |
| 184 | expectedMeta.metadata.push_back( |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 185 | static_cast<std::uint8_t>(ActionStatus::running)); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 186 | |
| 187 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 188 | EXPECT_EQ(expectedMeta, meta); |
| 189 | expectedState(FirmwareBlobHandler::UpdateState::updateStarted); |
| 190 | } |
| 191 | |
| 192 | TEST_F(FirmwareHandlerUpdateStartedTest, |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 193 | CallStatChecksActionStatusReturnsFailedChangesStateToCompleted) |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 194 | { |
| 195 | getToUpdateStarted(); |
| 196 | EXPECT_CALL(*updateMockPtr, status()) |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 197 | .WillOnce(Return(ActionStatus::failed)); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 198 | |
| 199 | blobs::BlobMeta meta, expectedMeta = {}; |
| 200 | expectedMeta.size = 0; |
| 201 | expectedMeta.blobState = flags | blobs::StateFlags::commit_error; |
| 202 | expectedMeta.metadata.push_back( |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 203 | static_cast<std::uint8_t>(ActionStatus::failed)); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 204 | |
| 205 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 206 | EXPECT_EQ(expectedMeta, meta); |
| 207 | expectedState(FirmwareBlobHandler::UpdateState::updateCompleted); |
| 208 | } |
| 209 | |
| 210 | TEST_F(FirmwareHandlerUpdateStartedTest, |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 211 | CallStatChecksActionStatusReturnsSuccessChangesStateToCompleted) |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 212 | { |
| 213 | getToUpdateStarted(); |
| 214 | EXPECT_CALL(*updateMockPtr, status()) |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 215 | .WillOnce(Return(ActionStatus::success)); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 216 | |
| 217 | blobs::BlobMeta meta, expectedMeta = {}; |
| 218 | expectedMeta.size = 0; |
| 219 | expectedMeta.blobState = flags | blobs::StateFlags::committed; |
| 220 | expectedMeta.metadata.push_back( |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 221 | static_cast<std::uint8_t>(ActionStatus::success)); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 222 | |
| 223 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 224 | EXPECT_EQ(expectedMeta, meta); |
| 225 | expectedState(FirmwareBlobHandler::UpdateState::updateCompleted); |
| 226 | } |
Patrick Venture | b6ce52b | 2019-05-31 11:16:41 -0700 | [diff] [blame] | 227 | |
| 228 | /* |
Patrick Venture | 4973171 | 2019-06-17 10:04:02 -0700 | [diff] [blame] | 229 | * close(session) - this will abort. |
Patrick Venture | b6ce52b | 2019-05-31 11:16:41 -0700 | [diff] [blame] | 230 | */ |
Patrick Venture | 4973171 | 2019-06-17 10:04:02 -0700 | [diff] [blame] | 231 | TEST_F(FirmwareHandlerUpdateStartedTest, CloseOnUpdateDuringUpdateAbortsProcess) |
| 232 | { |
| 233 | getToUpdateStarted(); |
| 234 | EXPECT_CALL(*updateMockPtr, abort()).Times(1); |
| 235 | |
| 236 | EXPECT_TRUE(handler->close(session)); |
| 237 | |
Patrick Venture | 4973171 | 2019-06-17 10:04:02 -0700 | [diff] [blame] | 238 | EXPECT_THAT(handler->getBlobIds(), |
Patrick Venture | 9a69f73 | 2019-06-17 14:05:13 -0700 | [diff] [blame^] | 239 | UnorderedElementsAreArray(startingBlobs)); |
Patrick Venture | 4973171 | 2019-06-17 10:04:02 -0700 | [diff] [blame] | 240 | |
| 241 | expectedState(FirmwareBlobHandler::UpdateState::notYetStarted); |
| 242 | } |
Patrick Venture | b6ce52b | 2019-05-31 11:16:41 -0700 | [diff] [blame] | 243 | |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 244 | } // namespace |
| 245 | } // namespace ipmi_flash |