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