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