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