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 | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 176 | getToVerificationStarted(hashBlobId); |
| 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 | |
| 197 | blobs::BlobMeta expected; |
| 198 | expected.blobState = FirmwareBlobHandler::UpdateFlags::ipmi; |
| 199 | expected.size = 0; |
| 200 | |
| 201 | std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId}; |
| 202 | for (const auto& blob : testBlobs) |
| 203 | { |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 204 | ASSERT_TRUE(handler->canHandleBlob(blob)); |
| 205 | |
Patrick Venture | a04997b | 2019-05-24 10:15:48 -0700 | [diff] [blame] | 206 | blobs::BlobMeta meta = {}; |
| 207 | EXPECT_TRUE(handler->stat(blob, &meta)); |
| 208 | EXPECT_EQ(expected, meta); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /* |
Patrick Venture | fb74ad5 | 2019-05-24 15:03:35 -0700 | [diff] [blame] | 213 | * writemeta(session) |
| 214 | */ |
| 215 | TEST_F(FirmwareHandlerVerificationStartedTest, |
| 216 | WriteMetaOnVerifySessionReturnsFailure) |
| 217 | { |
| 218 | getToVerificationStarted(staticLayoutBlobId); |
| 219 | |
| 220 | std::vector<std::uint8_t> bytes = {0x01, 0x02}; |
| 221 | EXPECT_FALSE(handler->writeMeta(session, 0, bytes)); |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * write(session) |
| 226 | */ |
Patrick Venture | 9c6de5a | 2019-05-24 15:12:26 -0700 | [diff] [blame] | 227 | TEST_F(FirmwareHandlerVerificationStartedTest, |
| 228 | WriteOnVerifySessionReturnsFailure) |
| 229 | { |
| 230 | getToVerificationStarted(staticLayoutBlobId); |
| 231 | |
| 232 | std::vector<std::uint8_t> bytes = {0x01, 0x02}; |
| 233 | EXPECT_FALSE(handler->write(session, 0, bytes)); |
| 234 | } |
Patrick Venture | fb74ad5 | 2019-05-24 15:03:35 -0700 | [diff] [blame] | 235 | |
| 236 | /* |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 237 | * 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] | 238 | */ |
| 239 | TEST_F(FirmwareHandlerVerificationStartedTest, |
| 240 | AttemptToOpenImageFileReturnsFailure) |
| 241 | { |
| 242 | /* Attempt to open a file one normally can open, however, as there is |
| 243 | * already a file open, this will fail. |
| 244 | */ |
| 245 | getToVerificationStarted(staticLayoutBlobId); |
| 246 | |
Patrick Venture | 2c01415 | 2019-05-28 18:16:05 -0700 | [diff] [blame] | 247 | auto blobsToOpen = handler->getBlobIds(); |
| 248 | for (const auto& blob : blobsToOpen) |
| 249 | { |
| 250 | EXPECT_FALSE(handler->open(session + 1, flags, blob)); |
| 251 | } |
Patrick Venture | d450910 | 2019-05-24 15:22:04 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /* |
Patrick Venture | 8facb38 | 2019-05-24 15:27:36 -0700 | [diff] [blame] | 255 | * read(session) |
| 256 | */ |
| 257 | TEST_F(FirmwareHandlerVerificationStartedTest, ReadOfVerifyBlobReturnsEmpty) |
| 258 | { |
| 259 | getToVerificationStarted(staticLayoutBlobId); |
Patrick Venture | 2c01415 | 2019-05-28 18:16:05 -0700 | [diff] [blame] | 260 | EXPECT_THAT(handler->read(session, 0, 1), IsEmpty()); |
Patrick Venture | 8facb38 | 2019-05-24 15:27:36 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /* |
Patrick Venture | ddc3507 | 2019-05-24 15:30:57 -0700 | [diff] [blame] | 264 | * commit(session) |
| 265 | */ |
| 266 | TEST_F(FirmwareHandlerVerificationStartedTest, |
| 267 | CommitOnVerifyDuringVerificationHasNoImpact) |
| 268 | { |
| 269 | getToVerificationStarted(staticLayoutBlobId); |
| 270 | EXPECT_TRUE(handler->commit(session, {})); |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 271 | expectedState(FirmwareBlobHandler::UpdateState::verificationStarted); |
Patrick Venture | ddc3507 | 2019-05-24 15:30:57 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /* |
Patrick Venture | a04997b | 2019-05-24 10:15:48 -0700 | [diff] [blame] | 275 | * close(session) - close while state if verificationStarted without calling |
| 276 | * stat first will abort. |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 277 | */ |
Patrick Venture | d574102 | 2019-06-17 09:08:35 -0700 | [diff] [blame] | 278 | TEST_F(FirmwareHandlerVerificationStartedTest, |
| 279 | CloseOnVerifyDuringVerificationAbortsProcess) |
| 280 | { |
| 281 | getToVerificationStarted(staticLayoutBlobId); |
| 282 | EXPECT_CALL(*verifyMockPtr, abort()).Times(1); |
| 283 | |
| 284 | EXPECT_TRUE(handler->close(session)); |
| 285 | |
Patrick Venture | d574102 | 2019-06-17 09:08:35 -0700 | [diff] [blame] | 286 | EXPECT_THAT(handler->getBlobIds(), |
Patrick Venture | 9a69f73 | 2019-06-17 14:05:13 -0700 | [diff] [blame^] | 287 | UnorderedElementsAreArray(startingBlobs)); |
Patrick Venture | d574102 | 2019-06-17 09:08:35 -0700 | [diff] [blame] | 288 | |
| 289 | expectedState(FirmwareBlobHandler::UpdateState::notYetStarted); |
| 290 | } |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 291 | |
| 292 | } // namespace |
| 293 | } // namespace ipmi_flash |