Patrick Venture | a82f99e | 2019-05-24 15:44:35 -0700 | [diff] [blame] | 1 | /** |
| 2 | * The goal of these tests is to verify the behavior of all blob commands given |
| 3 | * the current state is verificationCompleted. This state is achieved as a out |
| 4 | * of verificationStarted. |
| 5 | */ |
| 6 | #include "firmware_handler.hpp" |
| 7 | #include "firmware_unittest.hpp" |
| 8 | #include "status.hpp" |
| 9 | #include "util.hpp" |
| 10 | |
| 11 | #include <cstdint> |
| 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
| 15 | #include <gtest/gtest.h> |
| 16 | |
| 17 | namespace ipmi_flash |
| 18 | { |
| 19 | namespace |
| 20 | { |
| 21 | |
Patrick Venture | 65cdcf0 | 2019-05-29 10:18:50 -0700 | [diff] [blame] | 22 | using ::testing::IsEmpty; |
Patrick Venture | 6d3a14c | 2019-05-29 09:24:42 -0700 | [diff] [blame] | 23 | using ::testing::UnorderedElementsAreArray; |
Patrick Venture | a82f99e | 2019-05-24 15:44:35 -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 | * Like the state verificationStarted, there is a file open in |
| 40 | * verificationCompleted. This state is transitioned to after a stat() command |
| 41 | * indicates a successful verification. |
| 42 | */ |
| 43 | |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 44 | class FirmwareHandlerVerificationCompletedTest : |
| 45 | public IpmiOnlyFirmwareStaticTest |
| 46 | {}; |
Patrick Venture | a82f99e | 2019-05-24 15:44:35 -0700 | [diff] [blame] | 47 | |
Patrick Venture | bcc0c77 | 2019-06-17 10:42:06 -0700 | [diff] [blame] | 48 | /* |
| 49 | * deleteBlob(blob) |
| 50 | */ |
| 51 | TEST_F(FirmwareHandlerVerificationCompletedTest, DeleteBlobReturnsFalse) |
| 52 | { |
| 53 | /* Try deleting all blobs, it doesn't really matter which though because you |
| 54 | * cannot close out an open session, therefore you must fail to delete |
| 55 | * anything unless everything is closed. |
| 56 | */ |
| 57 | getToVerificationCompleted(ActionStatus::success); |
| 58 | auto blobs = handler->getBlobIds(); |
| 59 | for (const auto& b : blobs) |
| 60 | { |
| 61 | EXPECT_FALSE(handler->deleteBlob(b)); |
| 62 | } |
| 63 | } |
Patrick Venture | 6d3a14c | 2019-05-29 09:24:42 -0700 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * canHandleBlob |
Patrick Venture | a82f99e | 2019-05-24 15:44:35 -0700 | [diff] [blame] | 67 | */ |
Patrick Venture | 6d3a14c | 2019-05-29 09:24:42 -0700 | [diff] [blame] | 68 | TEST_F(FirmwareHandlerVerificationCompletedTest, |
| 69 | OnVerificationCompleteSuccessUpdateBlobIdNotPresent) |
| 70 | { |
| 71 | /* the uploadBlobId is only added on close() of the verifyBlobId. This is a |
| 72 | * consistent behavior with verifyBlobId only added when closing the image |
| 73 | * or hash. |
| 74 | */ |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 75 | getToVerificationCompleted(ActionStatus::success); |
Patrick Venture | 6d3a14c | 2019-05-29 09:24:42 -0700 | [diff] [blame] | 76 | EXPECT_FALSE(handler->canHandleBlob(updateBlobId)); |
| 77 | } |
| 78 | |
| 79 | TEST_F(FirmwareHandlerVerificationCompletedTest, |
| 80 | OnVerificationCompleteFailureUpdateBlobIdNotPresent) |
| 81 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 82 | getToVerificationCompleted(ActionStatus::failed); |
Patrick Venture | 6d3a14c | 2019-05-29 09:24:42 -0700 | [diff] [blame] | 83 | EXPECT_FALSE(handler->canHandleBlob(updateBlobId)); |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * getBlobIds |
| 88 | */ |
| 89 | TEST_F(FirmwareHandlerVerificationCompletedTest, GetBlobIdsReturnsExpectedList) |
| 90 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 91 | getToVerificationCompleted(ActionStatus::success); |
Patrick Venture | 9a69f73 | 2019-06-17 14:05:13 -0700 | [diff] [blame] | 92 | EXPECT_THAT( |
| 93 | handler->getBlobIds(), |
| 94 | UnorderedElementsAreArray( |
| 95 | {verifyBlobId, hashBlobId, activeImageBlobId, staticLayoutBlobId})); |
Patrick Venture | 6d3a14c | 2019-05-29 09:24:42 -0700 | [diff] [blame] | 96 | } |
Patrick Venture | a82f99e | 2019-05-24 15:44:35 -0700 | [diff] [blame] | 97 | |
| 98 | /* |
| 99 | * stat(blob) |
| 100 | */ |
Patrick Venture | 0c642fd | 2019-05-24 16:09:29 -0700 | [diff] [blame] | 101 | TEST_F(FirmwareHandlerVerificationCompletedTest, |
| 102 | StatOnActiveImageReturnsFailure) |
| 103 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 104 | getToVerificationCompleted(ActionStatus::success); |
Patrick Venture | 0c642fd | 2019-05-24 16:09:29 -0700 | [diff] [blame] | 105 | ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId)); |
| 106 | |
| 107 | blobs::BlobMeta meta; |
| 108 | EXPECT_FALSE(handler->stat(activeImageBlobId, &meta)); |
| 109 | } |
| 110 | |
Patrick Venture | fbf07ff | 2019-05-29 08:58:45 -0700 | [diff] [blame] | 111 | TEST_F(FirmwareHandlerVerificationCompletedTest, |
| 112 | VerifyActiveHashIdMissingInThisCase) |
| 113 | { |
| 114 | /* The path taken to get to this state never opened the hash blob Id, which |
| 115 | * is fine. But let's verify it behaved as intended. |
| 116 | */ |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 117 | getToVerificationCompleted(ActionStatus::success); |
Patrick Venture | fbf07ff | 2019-05-29 08:58:45 -0700 | [diff] [blame] | 118 | EXPECT_FALSE(handler->canHandleBlob(activeHashBlobId)); |
| 119 | } |
| 120 | |
| 121 | /* TODO: Add sufficient warning that you can get to verificationCompleted |
| 122 | * without ever opening the image blob id (or the tarball one). |
| 123 | * |
| 124 | * Although in this case, it's expected that any verification triggered would |
| 125 | * certainly fail. So, although it's possible, it's uninteresting. |
| 126 | */ |
| 127 | |
Patrick Venture | 0c642fd | 2019-05-24 16:09:29 -0700 | [diff] [blame] | 128 | TEST_F(FirmwareHandlerVerificationCompletedTest, StatOnVerifyBlobReturnsFailure) |
| 129 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 130 | getToVerificationCompleted(ActionStatus::success); |
Patrick Venture | 0c642fd | 2019-05-24 16:09:29 -0700 | [diff] [blame] | 131 | ASSERT_TRUE(handler->canHandleBlob(verifyBlobId)); |
| 132 | |
| 133 | blobs::BlobMeta meta; |
| 134 | EXPECT_FALSE(handler->stat(verifyBlobId, &meta)); |
| 135 | } |
| 136 | |
| 137 | TEST_F(FirmwareHandlerVerificationCompletedTest, |
| 138 | StatOnNormalBlobsReturnsSuccess) |
| 139 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 140 | getToVerificationCompleted(ActionStatus::success); |
Patrick Venture | 0c642fd | 2019-05-24 16:09:29 -0700 | [diff] [blame] | 141 | |
Patrick Venture | 0c642fd | 2019-05-24 16:09:29 -0700 | [diff] [blame] | 142 | std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId}; |
| 143 | for (const auto& blob : testBlobs) |
| 144 | { |
| 145 | ASSERT_TRUE(handler->canHandleBlob(blob)); |
| 146 | |
| 147 | blobs::BlobMeta meta = {}; |
| 148 | EXPECT_TRUE(handler->stat(blob, &meta)); |
Benjamin Fair | 1290198 | 2019-11-12 13:55:46 -0800 | [diff] [blame] | 149 | EXPECT_EQ(expectedIdleMeta, meta); |
Patrick Venture | 0c642fd | 2019-05-24 16:09:29 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
Patrick Venture | a82f99e | 2019-05-24 15:44:35 -0700 | [diff] [blame] | 152 | |
| 153 | /* |
| 154 | * stat(session) - the verify blobid is open in this state, so stat on that once |
| 155 | * completed should have no effect. |
Patrick Venture | 65cdcf0 | 2019-05-29 10:18:50 -0700 | [diff] [blame] | 156 | */ |
Patrick Venture | 615c69e | 2019-05-29 10:49:54 -0700 | [diff] [blame] | 157 | TEST_F(FirmwareHandlerVerificationCompletedTest, |
| 158 | SessionStatOnVerifyAfterSuccessDoesNothing) |
| 159 | { |
| 160 | /* Every time you stat() once it's triggered, it checks the state again |
| 161 | * until it's completed. |
| 162 | */ |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 163 | getToVerificationCompleted(ActionStatus::success); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 164 | EXPECT_CALL(*verifyMockPtr, status()).Times(0); |
Patrick Venture | 615c69e | 2019-05-29 10:49:54 -0700 | [diff] [blame] | 165 | |
| 166 | blobs::BlobMeta meta, expectedMeta = {}; |
| 167 | expectedMeta.size = 0; |
| 168 | expectedMeta.blobState = flags | blobs::StateFlags::committed; |
| 169 | expectedMeta.metadata.push_back( |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 170 | static_cast<std::uint8_t>(ActionStatus::success)); |
Patrick Venture | 615c69e | 2019-05-29 10:49:54 -0700 | [diff] [blame] | 171 | |
| 172 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 173 | EXPECT_EQ(expectedMeta, meta); |
| 174 | expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted); |
| 175 | } |
| 176 | |
| 177 | TEST_F(FirmwareHandlerVerificationCompletedTest, |
| 178 | SessionStatOnVerifyAfterFailureDoesNothing) |
| 179 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 180 | getToVerificationCompleted(ActionStatus::failed); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 181 | EXPECT_CALL(*verifyMockPtr, status()).Times(0); |
Patrick Venture | 615c69e | 2019-05-29 10:49:54 -0700 | [diff] [blame] | 182 | |
| 183 | blobs::BlobMeta meta, expectedMeta = {}; |
| 184 | expectedMeta.size = 0; |
| 185 | expectedMeta.blobState = flags | blobs::StateFlags::commit_error; |
| 186 | expectedMeta.metadata.push_back( |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 187 | static_cast<std::uint8_t>(ActionStatus::failed)); |
Patrick Venture | 615c69e | 2019-05-29 10:49:54 -0700 | [diff] [blame] | 188 | |
| 189 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 190 | EXPECT_EQ(expectedMeta, meta); |
| 191 | expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted); |
| 192 | } |
Patrick Venture | 65cdcf0 | 2019-05-29 10:18:50 -0700 | [diff] [blame] | 193 | |
| 194 | /* |
Patrick Venture | a82f99e | 2019-05-24 15:44:35 -0700 | [diff] [blame] | 195 | * open(blob) - all open should fail |
Patrick Venture | a82f99e | 2019-05-24 15:44:35 -0700 | [diff] [blame] | 196 | */ |
Patrick Venture | eee7181 | 2019-05-29 10:41:04 -0700 | [diff] [blame] | 197 | TEST_F(FirmwareHandlerVerificationCompletedTest, |
| 198 | OpeningAnyBlobAvailableFailsAfterSuccess) |
| 199 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 200 | getToVerificationCompleted(ActionStatus::success); |
Patrick Venture | eee7181 | 2019-05-29 10:41:04 -0700 | [diff] [blame] | 201 | |
| 202 | auto blobs = handler->getBlobIds(); |
| 203 | for (const auto& blob : blobs) |
| 204 | { |
| 205 | EXPECT_FALSE(handler->open(session + 1, flags, blob)); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | TEST_F(FirmwareHandlerVerificationCompletedTest, |
| 210 | OpeningAnyBlobAvailableFailsAfterFailure) |
| 211 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 212 | getToVerificationCompleted(ActionStatus::failed); |
Patrick Venture | eee7181 | 2019-05-29 10:41:04 -0700 | [diff] [blame] | 213 | |
| 214 | auto blobs = handler->getBlobIds(); |
| 215 | for (const auto& blob : blobs) |
| 216 | { |
| 217 | EXPECT_FALSE(handler->open(session + 1, flags, blob)); |
| 218 | } |
| 219 | } |
Patrick Venture | a82f99e | 2019-05-24 15:44:35 -0700 | [diff] [blame] | 220 | |
| 221 | /* |
| 222 | * writemeta(session) - write meta should fail. |
Patrick Venture | 4d9b0e1 | 2019-05-29 10:21:40 -0700 | [diff] [blame] | 223 | */ |
Patrick Venture | 2b80137 | 2019-05-29 10:26:01 -0700 | [diff] [blame] | 224 | TEST_F(FirmwareHandlerVerificationCompletedTest, |
| 225 | WriteMetaToVerifyBlobReturnsFailure) |
| 226 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 227 | getToVerificationCompleted(ActionStatus::success); |
Patrick Venture | 2b80137 | 2019-05-29 10:26:01 -0700 | [diff] [blame] | 228 | |
| 229 | std::vector<std::uint8_t> bytes = {0x01, 0x02}; |
| 230 | EXPECT_FALSE(handler->writeMeta(session, 0, bytes)); |
| 231 | } |
Patrick Venture | 4d9b0e1 | 2019-05-29 10:21:40 -0700 | [diff] [blame] | 232 | |
| 233 | /* |
Patrick Venture | a82f99e | 2019-05-24 15:44:35 -0700 | [diff] [blame] | 234 | * write(session) - write should fail. |
Patrick Venture | 65cdcf0 | 2019-05-29 10:18:50 -0700 | [diff] [blame] | 235 | */ |
Patrick Venture | 4d9b0e1 | 2019-05-29 10:21:40 -0700 | [diff] [blame] | 236 | TEST_F(FirmwareHandlerVerificationCompletedTest, |
| 237 | WriteToVerifyBlobReturnsFailure) |
| 238 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 239 | getToVerificationCompleted(ActionStatus::success); |
Patrick Venture | 4d9b0e1 | 2019-05-29 10:21:40 -0700 | [diff] [blame] | 240 | |
| 241 | std::vector<std::uint8_t> bytes = {0x01, 0x02}; |
| 242 | EXPECT_FALSE(handler->write(session, 0, bytes)); |
| 243 | } |
Patrick Venture | 65cdcf0 | 2019-05-29 10:18:50 -0700 | [diff] [blame] | 244 | |
| 245 | /* |
Patrick Venture | a82f99e | 2019-05-24 15:44:35 -0700 | [diff] [blame] | 246 | * read(session) - read returns empty. |
Patrick Venture | 65cdcf0 | 2019-05-29 10:18:50 -0700 | [diff] [blame] | 247 | */ |
| 248 | TEST_F(FirmwareHandlerVerificationCompletedTest, ReadOfVerifyBlobReturnsEmpty) |
| 249 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 250 | getToVerificationCompleted(ActionStatus::success); |
Patrick Venture | 65cdcf0 | 2019-05-29 10:18:50 -0700 | [diff] [blame] | 251 | EXPECT_THAT(handler->read(session, 0, 1), IsEmpty()); |
| 252 | } |
| 253 | |
| 254 | /* |
Patrick Venture | 433cbc3 | 2019-05-30 09:53:10 -0700 | [diff] [blame] | 255 | * commit(session) - returns failure |
Patrick Venture | a82f99e | 2019-05-24 15:44:35 -0700 | [diff] [blame] | 256 | */ |
Patrick Venture | 433cbc3 | 2019-05-30 09:53:10 -0700 | [diff] [blame] | 257 | TEST_F(FirmwareHandlerVerificationCompletedTest, |
| 258 | CommitOnVerifyBlobAfterSuccessReturnsFailure) |
| 259 | { |
| 260 | /* If you've started this'll return success, but if it's finished, it won't |
| 261 | * let you try-again. |
| 262 | */ |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 263 | getToVerificationCompleted(ActionStatus::success); |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 264 | EXPECT_CALL(*verifyMockPtr, trigger()).Times(0); |
Patrick Venture | 433cbc3 | 2019-05-30 09:53:10 -0700 | [diff] [blame] | 265 | |
| 266 | EXPECT_FALSE(handler->commit(session, {})); |
| 267 | } |
| 268 | |
| 269 | TEST_F(FirmwareHandlerVerificationCompletedTest, |
| 270 | CommitOnVerifyBlobAfterFailureReturnsFailure) |
| 271 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 272 | getToVerificationCompleted(ActionStatus::failed); |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 273 | EXPECT_CALL(*verifyMockPtr, trigger()).Times(0); |
Patrick Venture | 433cbc3 | 2019-05-30 09:53:10 -0700 | [diff] [blame] | 274 | |
| 275 | EXPECT_FALSE(handler->commit(session, {})); |
| 276 | } |
Patrick Venture | a82f99e | 2019-05-24 15:44:35 -0700 | [diff] [blame] | 277 | |
Patrick Venture | 65cdcf0 | 2019-05-29 10:18:50 -0700 | [diff] [blame] | 278 | /* |
| 279 | * close(session) - close on the verify blobid: |
| 280 | * 1. if successful adds update blob id, changes state to UpdatePending |
Patrick Venture | 1c6d8f5 | 2019-05-30 10:53:49 -0700 | [diff] [blame] | 281 | */ |
| 282 | TEST_F(FirmwareHandlerVerificationCompletedTest, |
| 283 | CloseAfterSuccessChangesStateAddsUpdateBlob) |
| 284 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 285 | getToVerificationCompleted(ActionStatus::success); |
Patrick Venture | 1c6d8f5 | 2019-05-30 10:53:49 -0700 | [diff] [blame] | 286 | ASSERT_FALSE(handler->canHandleBlob(updateBlobId)); |
| 287 | |
| 288 | handler->close(session); |
| 289 | EXPECT_TRUE(handler->canHandleBlob(updateBlobId)); |
| 290 | expectedState(FirmwareBlobHandler::UpdateState::updatePending); |
| 291 | } |
| 292 | |
| 293 | /* |
| 294 | * close(session) - close on the verify blobid: |
Patrick Venture | 5d9fa02 | 2019-06-17 13:13:30 -0700 | [diff] [blame] | 295 | * 2. if unsuccessful it aborts. |
Patrick Venture | 65cdcf0 | 2019-05-29 10:18:50 -0700 | [diff] [blame] | 296 | */ |
Patrick Venture | 5d9fa02 | 2019-06-17 13:13:30 -0700 | [diff] [blame] | 297 | TEST_F(FirmwareHandlerVerificationCompletedTest, CloseAfterFailureAborts) |
| 298 | { |
| 299 | getToVerificationCompleted(ActionStatus::failed); |
| 300 | ASSERT_FALSE(handler->canHandleBlob(updateBlobId)); |
| 301 | |
| 302 | handler->close(session); |
| 303 | ASSERT_FALSE(handler->canHandleBlob(updateBlobId)); |
| 304 | expectedState(FirmwareBlobHandler::UpdateState::notYetStarted); |
Patrick Venture | 9a69f73 | 2019-06-17 14:05:13 -0700 | [diff] [blame] | 305 | EXPECT_THAT(handler->getBlobIds(), |
| 306 | UnorderedElementsAreArray(startingBlobs)); |
Patrick Venture | 5d9fa02 | 2019-06-17 13:13:30 -0700 | [diff] [blame] | 307 | } |
Patrick Venture | 65cdcf0 | 2019-05-29 10:18:50 -0700 | [diff] [blame] | 308 | |
Patrick Venture | 92f2615 | 2020-05-26 19:47:36 -0700 | [diff] [blame] | 309 | /* |
| 310 | * expire(session) |
| 311 | */ |
| 312 | TEST_F(FirmwareHandlerVerificationCompletedTest, |
| 313 | ExpireAfterVerificationCompletedAborts) |
| 314 | { |
| 315 | getToVerificationCompleted(ActionStatus::failed); |
| 316 | |
| 317 | ASSERT_TRUE(handler->expire(session)); |
| 318 | expectedState(FirmwareBlobHandler::UpdateState::notYetStarted); |
| 319 | EXPECT_THAT(handler->getBlobIds(), |
| 320 | UnorderedElementsAreArray(startingBlobs)); |
| 321 | } |
| 322 | |
Patrick Venture | a82f99e | 2019-05-24 15:44:35 -0700 | [diff] [blame] | 323 | } // namespace |
| 324 | } // namespace ipmi_flash |