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