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