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