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