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 |
| 42 | { |
| 43 | }; |
| 44 | |
| 45 | /* |
| 46 | * open(blob) |
| 47 | */ |
| 48 | TEST_F(FirmwareHandlerUpdateCompletedTest, |
| 49 | AttemptToOpenFilesReturnsFailureAfterSuccess) |
| 50 | { |
| 51 | /* 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] | 52 | getToUpdateCompleted(ActionStatus::success); |
Patrick Venture | ab1e962 | 2019-06-03 10:45:06 -0700 | [diff] [blame] | 53 | |
| 54 | auto blobsToOpen = handler->getBlobIds(); |
| 55 | for (const auto& blob : blobsToOpen) |
| 56 | { |
| 57 | EXPECT_FALSE(handler->open(session + 1, flags, blob)); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /* |
Patrick Venture | a2d8239 | 2019-06-03 10:55:17 -0700 | [diff] [blame] | 62 | * stat(session) |
| 63 | */ |
| 64 | TEST_F(FirmwareHandlerUpdateCompletedTest, |
| 65 | CallingStatSessionAfterCompletedSuccessReturnsStateWithoutRechecking) |
| 66 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 67 | getToUpdateCompleted(ActionStatus::success); |
Patrick Venture | a2d8239 | 2019-06-03 10:55:17 -0700 | [diff] [blame] | 68 | EXPECT_CALL(*updateMockPtr, status()).Times(0); |
| 69 | |
| 70 | blobs::BlobMeta meta, expectedMeta = {}; |
| 71 | expectedMeta.size = 0; |
| 72 | expectedMeta.blobState = flags | blobs::StateFlags::committed; |
| 73 | expectedMeta.metadata.push_back( |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 74 | static_cast<std::uint8_t>(ActionStatus::success)); |
Patrick Venture | a2d8239 | 2019-06-03 10:55:17 -0700 | [diff] [blame] | 75 | |
| 76 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 77 | EXPECT_EQ(expectedMeta, meta); |
| 78 | expectedState(FirmwareBlobHandler::UpdateState::updateCompleted); |
| 79 | } |
| 80 | |
| 81 | TEST_F(FirmwareHandlerUpdateCompletedTest, |
| 82 | CallingStatSessionAfterCompletedFailureReturnsStateWithoutRechecking) |
| 83 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 84 | getToUpdateCompleted(ActionStatus::failed); |
Patrick Venture | a2d8239 | 2019-06-03 10:55:17 -0700 | [diff] [blame] | 85 | EXPECT_CALL(*updateMockPtr, status()).Times(0); |
| 86 | |
| 87 | blobs::BlobMeta meta, expectedMeta = {}; |
| 88 | expectedMeta.size = 0; |
| 89 | expectedMeta.blobState = flags | blobs::StateFlags::commit_error; |
| 90 | expectedMeta.metadata.push_back( |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 91 | static_cast<std::uint8_t>(ActionStatus::failed)); |
Patrick Venture | a2d8239 | 2019-06-03 10:55:17 -0700 | [diff] [blame] | 92 | |
| 93 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 94 | EXPECT_EQ(expectedMeta, meta); |
| 95 | expectedState(FirmwareBlobHandler::UpdateState::updateCompleted); |
| 96 | } |
| 97 | |
| 98 | /* |
Patrick Venture | 25dfaff | 2019-06-03 13:17:22 -0700 | [diff] [blame] | 99 | * stat(blob) |
| 100 | */ |
| 101 | TEST_F(FirmwareHandlerUpdateCompletedTest, StatOnActiveImageReturnsFailure) |
| 102 | { |
| 103 | getToUpdateCompleted(ActionStatus::success); |
| 104 | |
| 105 | ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId)); |
| 106 | |
| 107 | blobs::BlobMeta meta; |
| 108 | EXPECT_FALSE(handler->stat(activeImageBlobId, &meta)); |
| 109 | } |
| 110 | |
| 111 | TEST_F(FirmwareHandlerUpdateCompletedTest, StatOnUpdateBlobReturnsFailure) |
| 112 | { |
| 113 | getToUpdateCompleted(ActionStatus::success); |
| 114 | |
| 115 | ASSERT_TRUE(handler->canHandleBlob(updateBlobId)); |
| 116 | |
| 117 | blobs::BlobMeta meta; |
| 118 | EXPECT_FALSE(handler->stat(updateBlobId, &meta)); |
| 119 | } |
| 120 | |
| 121 | TEST_F(FirmwareHandlerUpdateCompletedTest, StatOnNormalBlobsReturnsSuccess) |
| 122 | { |
| 123 | getToUpdateCompleted(ActionStatus::success); |
| 124 | |
Patrick Venture | 25dfaff | 2019-06-03 13:17:22 -0700 | [diff] [blame] | 125 | std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId}; |
| 126 | for (const auto& blob : testBlobs) |
| 127 | { |
| 128 | ASSERT_TRUE(handler->canHandleBlob(blob)); |
| 129 | |
| 130 | blobs::BlobMeta meta = {}; |
| 131 | EXPECT_TRUE(handler->stat(blob, &meta)); |
Benjamin Fair | 1290198 | 2019-11-12 13:55:46 -0800 | [diff] [blame^] | 132 | EXPECT_EQ(expectedIdleMeta, meta); |
Patrick Venture | 25dfaff | 2019-06-03 13:17:22 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | /* |
Patrick Venture | 58202b2 | 2019-06-03 13:38:09 -0700 | [diff] [blame] | 137 | * writemeta(session) |
| 138 | */ |
| 139 | TEST_F(FirmwareHandlerUpdateCompletedTest, WriteMetaToUpdateBlobReturnsFailure) |
| 140 | { |
| 141 | getToUpdateCompleted(ActionStatus::success); |
| 142 | |
| 143 | EXPECT_FALSE(handler->writeMeta(session, 0, {0x01})); |
| 144 | } |
| 145 | |
| 146 | /* |
Patrick Venture | 254b4cf | 2019-06-03 13:44:49 -0700 | [diff] [blame] | 147 | * write(session) |
| 148 | */ |
| 149 | TEST_F(FirmwareHandlerUpdateCompletedTest, WriteToUpdateBlobReturnsFailure) |
| 150 | { |
| 151 | getToUpdateCompleted(ActionStatus::success); |
| 152 | |
| 153 | EXPECT_FALSE(handler->write(session, 0, {0x01})); |
| 154 | } |
| 155 | |
| 156 | /* |
Patrick Venture | fdeb864 | 2019-06-03 14:30:34 -0700 | [diff] [blame] | 157 | * commit(session) - returns failure |
| 158 | */ |
| 159 | TEST_F(FirmwareHandlerUpdateCompletedTest, |
| 160 | CommitOnUpdateBlobAfterSuccessReturnsFailure) |
| 161 | { |
| 162 | getToUpdateCompleted(ActionStatus::success); |
| 163 | |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 164 | EXPECT_CALL(*updateMockPtr, trigger()).Times(0); |
Patrick Venture | fdeb864 | 2019-06-03 14:30:34 -0700 | [diff] [blame] | 165 | EXPECT_FALSE(handler->commit(session, {})); |
| 166 | } |
| 167 | |
| 168 | TEST_F(FirmwareHandlerUpdateCompletedTest, |
| 169 | CommitOnUpdateBlobAfterFailureReturnsFailure) |
| 170 | { |
| 171 | getToUpdateCompleted(ActionStatus::failed); |
| 172 | |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 173 | EXPECT_CALL(*updateMockPtr, trigger()).Times(0); |
Patrick Venture | fdeb864 | 2019-06-03 14:30:34 -0700 | [diff] [blame] | 174 | EXPECT_FALSE(handler->commit(session, {})); |
| 175 | } |
| 176 | |
| 177 | /* |
Patrick Venture | 7441ab7 | 2019-06-05 07:44:38 -0700 | [diff] [blame] | 178 | * read(session) - nothing to read here. |
| 179 | */ |
| 180 | TEST_F(FirmwareHandlerUpdateCompletedTest, ReadingFromUpdateBlobReturnsNothing) |
| 181 | { |
| 182 | getToUpdateCompleted(ActionStatus::success); |
| 183 | |
| 184 | EXPECT_THAT(handler->read(session, 0, 1), IsEmpty()); |
| 185 | } |
| 186 | |
| 187 | /* |
Patrick Venture | ab1e962 | 2019-06-03 10:45:06 -0700 | [diff] [blame] | 188 | * getBlobIds |
Patrick Venture | 0c6daf4 | 2019-06-05 09:02:50 -0700 | [diff] [blame] | 189 | * canHandleBlob(blob) |
| 190 | */ |
| 191 | TEST_F(FirmwareHandlerUpdateCompletedTest, GetBlobListProvidesExpectedBlobs) |
| 192 | { |
| 193 | getToUpdateCompleted(ActionStatus::success); |
| 194 | |
Patrick Venture | 9a69f73 | 2019-06-17 14:05:13 -0700 | [diff] [blame] | 195 | EXPECT_THAT( |
| 196 | handler->getBlobIds(), |
| 197 | UnorderedElementsAreArray( |
| 198 | {updateBlobId, hashBlobId, activeImageBlobId, staticLayoutBlobId})); |
Patrick Venture | 0c6daf4 | 2019-06-05 09:02:50 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /* |
Patrick Venture | 85ae86b | 2019-06-05 09:18:40 -0700 | [diff] [blame] | 202 | * close(session) - closes everything out and returns to state not-yet-started. |
| 203 | * It doesn't go and clean out any update artifacts that may be present on the |
| 204 | * system. It's up to the update implementation to deal with this before |
| 205 | * marking complete. |
| 206 | */ |
| 207 | TEST_F(FirmwareHandlerUpdateCompletedTest, |
| 208 | ClosingOnUpdateBlobIdAfterSuccessReturnsToNotYetStartedAndCleansBlobList) |
| 209 | { |
| 210 | getToUpdateCompleted(ActionStatus::success); |
| 211 | |
| 212 | handler->close(session); |
| 213 | expectedState(FirmwareBlobHandler::UpdateState::notYetStarted); |
| 214 | |
Patrick Venture | 9a69f73 | 2019-06-17 14:05:13 -0700 | [diff] [blame] | 215 | EXPECT_THAT(handler->getBlobIds(), |
| 216 | UnorderedElementsAreArray(startingBlobs)); |
Patrick Venture | 85ae86b | 2019-06-05 09:18:40 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | TEST_F(FirmwareHandlerUpdateCompletedTest, |
| 220 | ClosingOnUpdateBlobIdAfterFailureReturnsToNotYetStartedAndCleansBlobList) |
| 221 | { |
| 222 | getToUpdateCompleted(ActionStatus::failed); |
| 223 | |
| 224 | handler->close(session); |
| 225 | expectedState(FirmwareBlobHandler::UpdateState::notYetStarted); |
| 226 | |
Patrick Venture | 9a69f73 | 2019-06-17 14:05:13 -0700 | [diff] [blame] | 227 | EXPECT_THAT(handler->getBlobIds(), |
| 228 | UnorderedElementsAreArray(startingBlobs)); |
Patrick Venture | 85ae86b | 2019-06-05 09:18:40 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /* |
Patrick Venture | bcc0c77 | 2019-06-17 10:42:06 -0700 | [diff] [blame] | 232 | * deleteBlob(blob) |
Patrick Venture | ab1e962 | 2019-06-03 10:45:06 -0700 | [diff] [blame] | 233 | */ |
Patrick Venture | bcc0c77 | 2019-06-17 10:42:06 -0700 | [diff] [blame] | 234 | TEST_F(FirmwareHandlerUpdateCompletedTest, DeleteBlobReturnsFalse) |
| 235 | { |
| 236 | /* Try deleting all blobs, it doesn't really matter which though because you |
| 237 | * cannot close out an open session, therefore you must fail to delete |
| 238 | * anything unless everything is closed. |
| 239 | */ |
| 240 | getToUpdateCompleted(ActionStatus::success); |
| 241 | |
| 242 | auto blobs = handler->getBlobIds(); |
| 243 | for (const auto& b : blobs) |
| 244 | { |
| 245 | EXPECT_FALSE(handler->deleteBlob(b)); |
| 246 | } |
| 247 | } |
Patrick Venture | ab1e962 | 2019-06-03 10:45:06 -0700 | [diff] [blame] | 248 | |
| 249 | } // namespace |
| 250 | } // namespace ipmi_flash |