Patrick Venture | 2098ff9 | 2019-06-03 10:18:05 -0700 | [diff] [blame] | 1 | /* 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 Venture | ab1e962 | 2019-06-03 10:45:06 -0700 | [diff] [blame] | 8 | #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 | |
| 18 | namespace ipmi_flash |
| 19 | { |
| 20 | namespace |
| 21 | { |
| 22 | |
Patrick Venture | 7441ab7 | 2019-06-05 07:44:38 -0700 | [diff] [blame] | 23 | using ::testing::IsEmpty; |
Patrick Venture | 0c6daf4 | 2019-06-05 09:02:50 -0700 | [diff] [blame] | 24 | using ::testing::UnorderedElementsAreArray; |
Patrick Venture | 7441ab7 | 2019-06-05 07:44:38 -0700 | [diff] [blame] | 25 | |
Patrick Venture | ab1e962 | 2019-06-03 10:45:06 -0700 | [diff] [blame] | 26 | /* |
| 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 | |
| 41 | class FirmwareHandlerUpdateCompletedTest : public IpmiOnlyFirmwareStaticTest |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 42 | {}; |
Patrick Venture | ab1e962 | 2019-06-03 10:45:06 -0700 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | * open(blob) |
| 46 | */ |
| 47 | TEST_F(FirmwareHandlerUpdateCompletedTest, |
| 48 | AttemptToOpenFilesReturnsFailureAfterSuccess) |
| 49 | { |
| 50 | /* In state updateCompleted a file is open, which means no others can be. */ |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 51 | getToUpdateCompleted(ActionStatus::success); |
Patrick Venture | ab1e962 | 2019-06-03 10:45:06 -0700 | [diff] [blame] | 52 | |
| 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 Venture | a2d8239 | 2019-06-03 10:55:17 -0700 | [diff] [blame] | 61 | * stat(session) |
| 62 | */ |
| 63 | TEST_F(FirmwareHandlerUpdateCompletedTest, |
| 64 | CallingStatSessionAfterCompletedSuccessReturnsStateWithoutRechecking) |
| 65 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 66 | getToUpdateCompleted(ActionStatus::success); |
Patrick Venture | a2d8239 | 2019-06-03 10:55:17 -0700 | [diff] [blame] | 67 | 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 Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 73 | static_cast<std::uint8_t>(ActionStatus::success)); |
Patrick Venture | a2d8239 | 2019-06-03 10:55:17 -0700 | [diff] [blame] | 74 | |
| 75 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 76 | EXPECT_EQ(expectedMeta, meta); |
| 77 | expectedState(FirmwareBlobHandler::UpdateState::updateCompleted); |
| 78 | } |
| 79 | |
| 80 | TEST_F(FirmwareHandlerUpdateCompletedTest, |
| 81 | CallingStatSessionAfterCompletedFailureReturnsStateWithoutRechecking) |
| 82 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 83 | getToUpdateCompleted(ActionStatus::failed); |
Patrick Venture | a2d8239 | 2019-06-03 10:55:17 -0700 | [diff] [blame] | 84 | 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 Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 90 | static_cast<std::uint8_t>(ActionStatus::failed)); |
Patrick Venture | a2d8239 | 2019-06-03 10:55:17 -0700 | [diff] [blame] | 91 | |
| 92 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 93 | EXPECT_EQ(expectedMeta, meta); |
| 94 | expectedState(FirmwareBlobHandler::UpdateState::updateCompleted); |
| 95 | } |
| 96 | |
| 97 | /* |
Patrick Venture | 25dfaff | 2019-06-03 13:17:22 -0700 | [diff] [blame] | 98 | * stat(blob) |
| 99 | */ |
| 100 | TEST_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 | |
| 110 | TEST_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 | |
| 120 | TEST_F(FirmwareHandlerUpdateCompletedTest, StatOnNormalBlobsReturnsSuccess) |
| 121 | { |
| 122 | getToUpdateCompleted(ActionStatus::success); |
| 123 | |
Patrick Venture | 25dfaff | 2019-06-03 13:17:22 -0700 | [diff] [blame] | 124 | 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 Fair | 1290198 | 2019-11-12 13:55:46 -0800 | [diff] [blame] | 131 | EXPECT_EQ(expectedIdleMeta, meta); |
Patrick Venture | 25dfaff | 2019-06-03 13:17:22 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
| 135 | /* |
Patrick Venture | 58202b2 | 2019-06-03 13:38:09 -0700 | [diff] [blame] | 136 | * writemeta(session) |
| 137 | */ |
| 138 | TEST_F(FirmwareHandlerUpdateCompletedTest, WriteMetaToUpdateBlobReturnsFailure) |
| 139 | { |
| 140 | getToUpdateCompleted(ActionStatus::success); |
| 141 | |
| 142 | EXPECT_FALSE(handler->writeMeta(session, 0, {0x01})); |
| 143 | } |
| 144 | |
| 145 | /* |
Patrick Venture | 254b4cf | 2019-06-03 13:44:49 -0700 | [diff] [blame] | 146 | * write(session) |
| 147 | */ |
| 148 | TEST_F(FirmwareHandlerUpdateCompletedTest, WriteToUpdateBlobReturnsFailure) |
| 149 | { |
| 150 | getToUpdateCompleted(ActionStatus::success); |
| 151 | |
| 152 | EXPECT_FALSE(handler->write(session, 0, {0x01})); |
| 153 | } |
| 154 | |
| 155 | /* |
Patrick Venture | fdeb864 | 2019-06-03 14:30:34 -0700 | [diff] [blame] | 156 | * commit(session) - returns failure |
| 157 | */ |
| 158 | TEST_F(FirmwareHandlerUpdateCompletedTest, |
| 159 | CommitOnUpdateBlobAfterSuccessReturnsFailure) |
| 160 | { |
| 161 | getToUpdateCompleted(ActionStatus::success); |
| 162 | |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 163 | EXPECT_CALL(*updateMockPtr, trigger()).Times(0); |
Patrick Venture | fdeb864 | 2019-06-03 14:30:34 -0700 | [diff] [blame] | 164 | EXPECT_FALSE(handler->commit(session, {})); |
| 165 | } |
| 166 | |
| 167 | TEST_F(FirmwareHandlerUpdateCompletedTest, |
| 168 | CommitOnUpdateBlobAfterFailureReturnsFailure) |
| 169 | { |
| 170 | getToUpdateCompleted(ActionStatus::failed); |
| 171 | |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 172 | EXPECT_CALL(*updateMockPtr, trigger()).Times(0); |
Patrick Venture | fdeb864 | 2019-06-03 14:30:34 -0700 | [diff] [blame] | 173 | EXPECT_FALSE(handler->commit(session, {})); |
| 174 | } |
| 175 | |
| 176 | /* |
Patrick Venture | 7441ab7 | 2019-06-05 07:44:38 -0700 | [diff] [blame] | 177 | * read(session) - nothing to read here. |
| 178 | */ |
| 179 | TEST_F(FirmwareHandlerUpdateCompletedTest, ReadingFromUpdateBlobReturnsNothing) |
| 180 | { |
| 181 | getToUpdateCompleted(ActionStatus::success); |
| 182 | |
| 183 | EXPECT_THAT(handler->read(session, 0, 1), IsEmpty()); |
| 184 | } |
| 185 | |
| 186 | /* |
Patrick Venture | ab1e962 | 2019-06-03 10:45:06 -0700 | [diff] [blame] | 187 | * getBlobIds |
Patrick Venture | 0c6daf4 | 2019-06-05 09:02:50 -0700 | [diff] [blame] | 188 | * canHandleBlob(blob) |
| 189 | */ |
| 190 | TEST_F(FirmwareHandlerUpdateCompletedTest, GetBlobListProvidesExpectedBlobs) |
| 191 | { |
| 192 | getToUpdateCompleted(ActionStatus::success); |
| 193 | |
Patrick Venture | 9a69f73 | 2019-06-17 14:05:13 -0700 | [diff] [blame] | 194 | EXPECT_THAT( |
| 195 | handler->getBlobIds(), |
| 196 | UnorderedElementsAreArray( |
| 197 | {updateBlobId, hashBlobId, activeImageBlobId, staticLayoutBlobId})); |
Patrick Venture | 0c6daf4 | 2019-06-05 09:02:50 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /* |
Patrick Venture | 85ae86b | 2019-06-05 09:18:40 -0700 | [diff] [blame] | 201 | * 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 | */ |
| 206 | TEST_F(FirmwareHandlerUpdateCompletedTest, |
| 207 | ClosingOnUpdateBlobIdAfterSuccessReturnsToNotYetStartedAndCleansBlobList) |
| 208 | { |
| 209 | getToUpdateCompleted(ActionStatus::success); |
| 210 | |
| 211 | handler->close(session); |
| 212 | expectedState(FirmwareBlobHandler::UpdateState::notYetStarted); |
| 213 | |
Patrick Venture | 9a69f73 | 2019-06-17 14:05:13 -0700 | [diff] [blame] | 214 | EXPECT_THAT(handler->getBlobIds(), |
| 215 | UnorderedElementsAreArray(startingBlobs)); |
Patrick Venture | 85ae86b | 2019-06-05 09:18:40 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | TEST_F(FirmwareHandlerUpdateCompletedTest, |
| 219 | ClosingOnUpdateBlobIdAfterFailureReturnsToNotYetStartedAndCleansBlobList) |
| 220 | { |
| 221 | getToUpdateCompleted(ActionStatus::failed); |
| 222 | |
| 223 | handler->close(session); |
| 224 | expectedState(FirmwareBlobHandler::UpdateState::notYetStarted); |
| 225 | |
Patrick Venture | 9a69f73 | 2019-06-17 14:05:13 -0700 | [diff] [blame] | 226 | EXPECT_THAT(handler->getBlobIds(), |
| 227 | UnorderedElementsAreArray(startingBlobs)); |
Patrick Venture | 85ae86b | 2019-06-05 09:18:40 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /* |
Patrick Venture | bcc0c77 | 2019-06-17 10:42:06 -0700 | [diff] [blame] | 231 | * deleteBlob(blob) |
Patrick Venture | ab1e962 | 2019-06-03 10:45:06 -0700 | [diff] [blame] | 232 | */ |
Patrick Venture | bcc0c77 | 2019-06-17 10:42:06 -0700 | [diff] [blame] | 233 | TEST_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 Venture | ab1e962 | 2019-06-03 10:45:06 -0700 | [diff] [blame] | 247 | |
Patrick Venture | 92f2615 | 2020-05-26 19:47:36 -0700 | [diff] [blame] | 248 | /* |
| 249 | * expire(session) |
| 250 | */ |
| 251 | TEST_F(FirmwareHandlerUpdateCompletedTest, ExpireOnUpdateCompletedAbortsProcess) |
| 252 | { |
| 253 | getToUpdateCompleted(ActionStatus::success); |
| 254 | |
| 255 | ASSERT_TRUE(handler->expire(session)); |
| 256 | EXPECT_THAT(handler->getBlobIds(), |
| 257 | UnorderedElementsAreArray(startingBlobs)); |
| 258 | |
| 259 | expectedState(FirmwareBlobHandler::UpdateState::notYetStarted); |
| 260 | } |
| 261 | |
Patrick Venture | ab1e962 | 2019-06-03 10:45:06 -0700 | [diff] [blame] | 262 | } // namespace |
| 263 | } // namespace ipmi_flash |