Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 1 | /* The goal of these tests is to verify the behavior of all blob commands given |
| 2 | * the current state is verificationStarted. This state is achieved as a out of |
| 3 | * verificationPending. |
| 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 | 8facb38 | 2019-05-24 15:27:36 -0700 | [diff] [blame] | 21 | using ::testing::IsEmpty; |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 22 | using ::testing::Return; |
Patrick Venture | 2c01415 | 2019-05-28 18:16:05 -0700 | [diff] [blame] | 23 | using ::testing::UnorderedElementsAreArray; |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -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 | * Testing canHandleBlob is uninteresting in this state. Getting the BlobIDs |
| 40 | * will inform what canHandleBlob will return. |
| 41 | */ |
| 42 | |
| 43 | class FirmwareHandlerVerificationStartedTest : public IpmiOnlyFirmwareStaticTest |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame^] | 44 | {}; |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 45 | |
| 46 | /* |
Patrick Venture | 2c01415 | 2019-05-28 18:16:05 -0700 | [diff] [blame] | 47 | * canHandleBlob(blob) |
| 48 | * getBlobIds() |
| 49 | */ |
| 50 | TEST_F(FirmwareHandlerVerificationStartedTest, GetBlobIdsReturnsExpectedList) |
| 51 | { |
| 52 | getToVerificationStarted(staticLayoutBlobId); |
| 53 | |
Patrick Venture | 9a69f73 | 2019-06-17 14:05:13 -0700 | [diff] [blame] | 54 | auto blobs = handler->getBlobIds(); |
| 55 | EXPECT_THAT( |
| 56 | blobs, UnorderedElementsAreArray({activeImageBlobId, staticLayoutBlobId, |
| 57 | hashBlobId, verifyBlobId})); |
Patrick Venture | 2c01415 | 2019-05-28 18:16:05 -0700 | [diff] [blame] | 58 | |
Patrick Venture | 9a69f73 | 2019-06-17 14:05:13 -0700 | [diff] [blame] | 59 | for (const auto& blob : blobs) |
Patrick Venture | 2c01415 | 2019-05-28 18:16:05 -0700 | [diff] [blame] | 60 | { |
| 61 | EXPECT_TRUE(handler->canHandleBlob(blob)); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /* |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 66 | * stat(session) |
| 67 | */ |
| 68 | TEST_F(FirmwareHandlerVerificationStartedTest, |
| 69 | StatOnVerifyBlobIdAfterCommitChecksStateAndReturnsRunning) |
| 70 | { |
| 71 | getToVerificationStarted(staticLayoutBlobId); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 72 | EXPECT_CALL(*verifyMockPtr, status()) |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 73 | .WillOnce(Return(ActionStatus::running)); |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 74 | |
| 75 | blobs::BlobMeta meta, expectedMeta = {}; |
| 76 | expectedMeta.size = 0; |
| 77 | expectedMeta.blobState = flags | blobs::StateFlags::committing; |
| 78 | expectedMeta.metadata.push_back( |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 79 | static_cast<std::uint8_t>(ActionStatus::running)); |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 80 | |
| 81 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 82 | EXPECT_EQ(expectedMeta, meta); |
| 83 | } |
| 84 | |
| 85 | TEST_F(FirmwareHandlerVerificationStartedTest, |
Patrick Venture | d52c2c3 | 2019-05-30 09:54:35 -0700 | [diff] [blame] | 86 | StatOnVerifyBlobIdAfterCommitChecksStateAndReturnsOther) |
| 87 | { |
| 88 | getToVerificationStarted(staticLayoutBlobId); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 89 | EXPECT_CALL(*verifyMockPtr, status()) |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 90 | .WillOnce(Return(ActionStatus::unknown)); |
Patrick Venture | d52c2c3 | 2019-05-30 09:54:35 -0700 | [diff] [blame] | 91 | |
| 92 | blobs::BlobMeta meta, expectedMeta = {}; |
| 93 | expectedMeta.size = 0; |
| 94 | expectedMeta.blobState = flags | blobs::StateFlags::committing; |
| 95 | expectedMeta.metadata.push_back( |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 96 | static_cast<std::uint8_t>(ActionStatus::unknown)); |
Patrick Venture | d52c2c3 | 2019-05-30 09:54:35 -0700 | [diff] [blame] | 97 | |
| 98 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 99 | EXPECT_EQ(expectedMeta, meta); |
| 100 | } |
| 101 | |
| 102 | TEST_F(FirmwareHandlerVerificationStartedTest, |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 103 | StatOnVerifyBlobIdAfterCommitCheckStateAndReturnsFailed) |
| 104 | { |
| 105 | /* If the returned state from the verification handler is failed, it sets |
| 106 | * commit_error and transitions to verificationCompleted. |
| 107 | */ |
| 108 | getToVerificationStarted(staticLayoutBlobId); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 109 | EXPECT_CALL(*verifyMockPtr, status()) |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 110 | .WillOnce(Return(ActionStatus::failed)); |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 111 | |
| 112 | blobs::BlobMeta meta, expectedMeta = {}; |
| 113 | expectedMeta.size = 0; |
| 114 | expectedMeta.blobState = flags | blobs::StateFlags::commit_error; |
| 115 | expectedMeta.metadata.push_back( |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 116 | static_cast<std::uint8_t>(ActionStatus::failed)); |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 117 | |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 118 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 119 | EXPECT_EQ(expectedMeta, meta); |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 120 | expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted); |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | TEST_F(FirmwareHandlerVerificationStartedTest, |
| 124 | StatOnVerifyBlobIdAfterCommitCheckStateAndReturnsSuccess) |
| 125 | { |
| 126 | /* If the returned state from the verification handler is success, it sets |
| 127 | * committed and transitions to verificationCompleted. |
| 128 | */ |
| 129 | getToVerificationStarted(staticLayoutBlobId); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 130 | EXPECT_CALL(*verifyMockPtr, status()) |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 131 | .WillOnce(Return(ActionStatus::success)); |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 132 | |
| 133 | blobs::BlobMeta meta, expectedMeta = {}; |
| 134 | expectedMeta.size = 0; |
| 135 | expectedMeta.blobState = flags | blobs::StateFlags::committed; |
| 136 | expectedMeta.metadata.push_back( |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 137 | static_cast<std::uint8_t>(ActionStatus::success)); |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 138 | |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 139 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 140 | EXPECT_EQ(expectedMeta, meta); |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 141 | expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted); |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Patrick Venture | bcc0c77 | 2019-06-17 10:42:06 -0700 | [diff] [blame] | 144 | /* |
| 145 | * deleteBlob(blob) |
| 146 | */ |
| 147 | TEST_F(FirmwareHandlerVerificationStartedTest, DeleteBlobReturnsFalse) |
| 148 | { |
| 149 | /* Try deleting all blobs, it doesn't really matter which though because you |
| 150 | * cannot close out an open session, therefore you must fail to delete |
| 151 | * anything unless everything is closed. |
| 152 | */ |
| 153 | getToVerificationStarted(staticLayoutBlobId); |
| 154 | auto blobs = handler->getBlobIds(); |
| 155 | for (const auto& b : blobs) |
| 156 | { |
| 157 | EXPECT_FALSE(handler->deleteBlob(b)); |
| 158 | } |
| 159 | } |
Patrick Venture | a04997b | 2019-05-24 10:15:48 -0700 | [diff] [blame] | 160 | |
| 161 | /* |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 162 | * stat(blob) |
Patrick Venture | a04997b | 2019-05-24 10:15:48 -0700 | [diff] [blame] | 163 | */ |
| 164 | TEST_F(FirmwareHandlerVerificationStartedTest, StatOnActiveImageReturnsFailure) |
| 165 | { |
| 166 | getToVerificationStarted(staticLayoutBlobId); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 167 | ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId)); |
Patrick Venture | a04997b | 2019-05-24 10:15:48 -0700 | [diff] [blame] | 168 | |
| 169 | blobs::BlobMeta meta; |
| 170 | EXPECT_FALSE(handler->stat(activeImageBlobId, &meta)); |
| 171 | } |
| 172 | |
| 173 | TEST_F(FirmwareHandlerVerificationStartedTest, StatOnActiveHashReturnsFailure) |
| 174 | { |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 175 | getToVerificationStartedWitHashBlob(); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 176 | ASSERT_TRUE(handler->canHandleBlob(activeHashBlobId)); |
Patrick Venture | a04997b | 2019-05-24 10:15:48 -0700 | [diff] [blame] | 177 | |
| 178 | blobs::BlobMeta meta; |
| 179 | EXPECT_FALSE(handler->stat(activeHashBlobId, &meta)); |
| 180 | } |
| 181 | |
| 182 | TEST_F(FirmwareHandlerVerificationStartedTest, StatOnVerifyBlobReturnsFailure) |
| 183 | { |
| 184 | /* the verifyBlobId is available starting at verificationPending. */ |
| 185 | getToVerificationStarted(staticLayoutBlobId); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 186 | ASSERT_TRUE(handler->canHandleBlob(verifyBlobId)); |
Patrick Venture | a04997b | 2019-05-24 10:15:48 -0700 | [diff] [blame] | 187 | |
| 188 | blobs::BlobMeta meta; |
| 189 | EXPECT_FALSE(handler->stat(verifyBlobId, &meta)); |
| 190 | } |
| 191 | |
| 192 | TEST_F(FirmwareHandlerVerificationStartedTest, StatOnNormalBlobsReturnsSuccess) |
| 193 | { |
| 194 | getToVerificationStarted(staticLayoutBlobId); |
| 195 | |
Patrick Venture | a04997b | 2019-05-24 10:15:48 -0700 | [diff] [blame] | 196 | std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId}; |
| 197 | for (const auto& blob : testBlobs) |
| 198 | { |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 199 | ASSERT_TRUE(handler->canHandleBlob(blob)); |
| 200 | |
Patrick Venture | a04997b | 2019-05-24 10:15:48 -0700 | [diff] [blame] | 201 | blobs::BlobMeta meta = {}; |
| 202 | EXPECT_TRUE(handler->stat(blob, &meta)); |
Benjamin Fair | 1290198 | 2019-11-12 13:55:46 -0800 | [diff] [blame] | 203 | EXPECT_EQ(expectedIdleMeta, meta); |
Patrick Venture | a04997b | 2019-05-24 10:15:48 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | /* |
Patrick Venture | fb74ad5 | 2019-05-24 15:03:35 -0700 | [diff] [blame] | 208 | * writemeta(session) |
| 209 | */ |
| 210 | TEST_F(FirmwareHandlerVerificationStartedTest, |
| 211 | WriteMetaOnVerifySessionReturnsFailure) |
| 212 | { |
| 213 | getToVerificationStarted(staticLayoutBlobId); |
| 214 | |
| 215 | std::vector<std::uint8_t> bytes = {0x01, 0x02}; |
| 216 | EXPECT_FALSE(handler->writeMeta(session, 0, bytes)); |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * write(session) |
| 221 | */ |
Patrick Venture | 9c6de5a | 2019-05-24 15:12:26 -0700 | [diff] [blame] | 222 | TEST_F(FirmwareHandlerVerificationStartedTest, |
| 223 | WriteOnVerifySessionReturnsFailure) |
| 224 | { |
| 225 | getToVerificationStarted(staticLayoutBlobId); |
| 226 | |
| 227 | std::vector<std::uint8_t> bytes = {0x01, 0x02}; |
| 228 | EXPECT_FALSE(handler->write(session, 0, bytes)); |
| 229 | } |
Patrick Venture | fb74ad5 | 2019-05-24 15:03:35 -0700 | [diff] [blame] | 230 | |
| 231 | /* |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 232 | * open(blob) - there is nothing you can open, this state has an open file. |
Patrick Venture | d450910 | 2019-05-24 15:22:04 -0700 | [diff] [blame] | 233 | */ |
| 234 | TEST_F(FirmwareHandlerVerificationStartedTest, |
| 235 | AttemptToOpenImageFileReturnsFailure) |
| 236 | { |
| 237 | /* Attempt to open a file one normally can open, however, as there is |
| 238 | * already a file open, this will fail. |
| 239 | */ |
| 240 | getToVerificationStarted(staticLayoutBlobId); |
| 241 | |
Patrick Venture | 2c01415 | 2019-05-28 18:16:05 -0700 | [diff] [blame] | 242 | auto blobsToOpen = handler->getBlobIds(); |
| 243 | for (const auto& blob : blobsToOpen) |
| 244 | { |
| 245 | EXPECT_FALSE(handler->open(session + 1, flags, blob)); |
| 246 | } |
Patrick Venture | d450910 | 2019-05-24 15:22:04 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /* |
Patrick Venture | 8facb38 | 2019-05-24 15:27:36 -0700 | [diff] [blame] | 250 | * read(session) |
| 251 | */ |
| 252 | TEST_F(FirmwareHandlerVerificationStartedTest, ReadOfVerifyBlobReturnsEmpty) |
| 253 | { |
| 254 | getToVerificationStarted(staticLayoutBlobId); |
Patrick Venture | 2c01415 | 2019-05-28 18:16:05 -0700 | [diff] [blame] | 255 | EXPECT_THAT(handler->read(session, 0, 1), IsEmpty()); |
Patrick Venture | 8facb38 | 2019-05-24 15:27:36 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /* |
Patrick Venture | ddc3507 | 2019-05-24 15:30:57 -0700 | [diff] [blame] | 259 | * commit(session) |
| 260 | */ |
| 261 | TEST_F(FirmwareHandlerVerificationStartedTest, |
| 262 | CommitOnVerifyDuringVerificationHasNoImpact) |
| 263 | { |
| 264 | getToVerificationStarted(staticLayoutBlobId); |
| 265 | EXPECT_TRUE(handler->commit(session, {})); |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 266 | expectedState(FirmwareBlobHandler::UpdateState::verificationStarted); |
Patrick Venture | ddc3507 | 2019-05-24 15:30:57 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /* |
Patrick Venture | a04997b | 2019-05-24 10:15:48 -0700 | [diff] [blame] | 270 | * close(session) - close while state if verificationStarted without calling |
| 271 | * stat first will abort. |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 272 | */ |
Patrick Venture | d574102 | 2019-06-17 09:08:35 -0700 | [diff] [blame] | 273 | TEST_F(FirmwareHandlerVerificationStartedTest, |
| 274 | CloseOnVerifyDuringVerificationAbortsProcess) |
| 275 | { |
| 276 | getToVerificationStarted(staticLayoutBlobId); |
| 277 | EXPECT_CALL(*verifyMockPtr, abort()).Times(1); |
| 278 | |
| 279 | EXPECT_TRUE(handler->close(session)); |
| 280 | |
Patrick Venture | d574102 | 2019-06-17 09:08:35 -0700 | [diff] [blame] | 281 | EXPECT_THAT(handler->getBlobIds(), |
Patrick Venture | 9a69f73 | 2019-06-17 14:05:13 -0700 | [diff] [blame] | 282 | UnorderedElementsAreArray(startingBlobs)); |
Patrick Venture | d574102 | 2019-06-17 09:08:35 -0700 | [diff] [blame] | 283 | |
| 284 | expectedState(FirmwareBlobHandler::UpdateState::notYetStarted); |
| 285 | } |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 286 | |
| 287 | } // namespace |
| 288 | } // namespace ipmi_flash |