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