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, |
| 109 | StatOnVerifyBlobIdAfterCommitCheckStateAndReturnsFailed) |
| 110 | { |
| 111 | /* If the returned state from the verification handler is failed, it sets |
| 112 | * commit_error and transitions to verificationCompleted. |
| 113 | */ |
| 114 | getToVerificationStarted(staticLayoutBlobId); |
| 115 | EXPECT_CALL(*verifyMockPtr, checkVerificationState()) |
| 116 | .WillOnce(Return(VerifyCheckResponses::failed)); |
| 117 | |
| 118 | blobs::BlobMeta meta, expectedMeta = {}; |
| 119 | expectedMeta.size = 0; |
| 120 | expectedMeta.blobState = flags | blobs::StateFlags::commit_error; |
| 121 | expectedMeta.metadata.push_back( |
| 122 | static_cast<std::uint8_t>(VerifyCheckResponses::failed)); |
| 123 | |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 124 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 125 | EXPECT_EQ(expectedMeta, meta); |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 126 | expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted); |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | TEST_F(FirmwareHandlerVerificationStartedTest, |
| 130 | StatOnVerifyBlobIdAfterCommitCheckStateAndReturnsSuccess) |
| 131 | { |
| 132 | /* If the returned state from the verification handler is success, it sets |
| 133 | * committed and transitions to verificationCompleted. |
| 134 | */ |
| 135 | getToVerificationStarted(staticLayoutBlobId); |
| 136 | EXPECT_CALL(*verifyMockPtr, checkVerificationState()) |
| 137 | .WillOnce(Return(VerifyCheckResponses::success)); |
| 138 | |
| 139 | blobs::BlobMeta meta, expectedMeta = {}; |
| 140 | expectedMeta.size = 0; |
| 141 | expectedMeta.blobState = flags | blobs::StateFlags::committed; |
| 142 | expectedMeta.metadata.push_back( |
| 143 | static_cast<std::uint8_t>(VerifyCheckResponses::success)); |
| 144 | |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 145 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 146 | EXPECT_EQ(expectedMeta, meta); |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 147 | expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted); |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /* TODO: Once verificationCompleted is the state, canHandleBlob should accept |
| 151 | * updateBlobId. |
| 152 | */ |
| 153 | |
Patrick Venture | 2c01415 | 2019-05-28 18:16:05 -0700 | [diff] [blame] | 154 | /* TODO: deleteBlob(blob) */ |
Patrick Venture | a04997b | 2019-05-24 10:15:48 -0700 | [diff] [blame] | 155 | |
| 156 | /* |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 157 | * stat(blob) |
Patrick Venture | a04997b | 2019-05-24 10:15:48 -0700 | [diff] [blame] | 158 | */ |
| 159 | TEST_F(FirmwareHandlerVerificationStartedTest, StatOnActiveImageReturnsFailure) |
| 160 | { |
| 161 | getToVerificationStarted(staticLayoutBlobId); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 162 | ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId)); |
Patrick Venture | a04997b | 2019-05-24 10:15:48 -0700 | [diff] [blame] | 163 | |
| 164 | blobs::BlobMeta meta; |
| 165 | EXPECT_FALSE(handler->stat(activeImageBlobId, &meta)); |
| 166 | } |
| 167 | |
| 168 | TEST_F(FirmwareHandlerVerificationStartedTest, StatOnActiveHashReturnsFailure) |
| 169 | { |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 170 | getToVerificationStarted(hashBlobId); |
| 171 | ASSERT_TRUE(handler->canHandleBlob(activeHashBlobId)); |
Patrick Venture | a04997b | 2019-05-24 10:15:48 -0700 | [diff] [blame] | 172 | |
| 173 | blobs::BlobMeta meta; |
| 174 | EXPECT_FALSE(handler->stat(activeHashBlobId, &meta)); |
| 175 | } |
| 176 | |
| 177 | TEST_F(FirmwareHandlerVerificationStartedTest, StatOnVerifyBlobReturnsFailure) |
| 178 | { |
| 179 | /* the verifyBlobId is available starting at verificationPending. */ |
| 180 | getToVerificationStarted(staticLayoutBlobId); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 181 | ASSERT_TRUE(handler->canHandleBlob(verifyBlobId)); |
Patrick Venture | a04997b | 2019-05-24 10:15:48 -0700 | [diff] [blame] | 182 | |
| 183 | blobs::BlobMeta meta; |
| 184 | EXPECT_FALSE(handler->stat(verifyBlobId, &meta)); |
| 185 | } |
| 186 | |
| 187 | TEST_F(FirmwareHandlerVerificationStartedTest, StatOnNormalBlobsReturnsSuccess) |
| 188 | { |
| 189 | getToVerificationStarted(staticLayoutBlobId); |
| 190 | |
| 191 | blobs::BlobMeta expected; |
| 192 | expected.blobState = FirmwareBlobHandler::UpdateFlags::ipmi; |
| 193 | expected.size = 0; |
| 194 | |
| 195 | std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId}; |
| 196 | for (const auto& blob : testBlobs) |
| 197 | { |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 198 | ASSERT_TRUE(handler->canHandleBlob(blob)); |
| 199 | |
Patrick Venture | a04997b | 2019-05-24 10:15:48 -0700 | [diff] [blame] | 200 | blobs::BlobMeta meta = {}; |
| 201 | EXPECT_TRUE(handler->stat(blob, &meta)); |
| 202 | EXPECT_EQ(expected, meta); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /* |
Patrick Venture | fb74ad5 | 2019-05-24 15:03:35 -0700 | [diff] [blame] | 207 | * writemeta(session) |
| 208 | */ |
| 209 | TEST_F(FirmwareHandlerVerificationStartedTest, |
| 210 | WriteMetaOnVerifySessionReturnsFailure) |
| 211 | { |
| 212 | getToVerificationStarted(staticLayoutBlobId); |
| 213 | |
| 214 | std::vector<std::uint8_t> bytes = {0x01, 0x02}; |
| 215 | EXPECT_FALSE(handler->writeMeta(session, 0, bytes)); |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * write(session) |
| 220 | */ |
Patrick Venture | 9c6de5a | 2019-05-24 15:12:26 -0700 | [diff] [blame] | 221 | TEST_F(FirmwareHandlerVerificationStartedTest, |
| 222 | WriteOnVerifySessionReturnsFailure) |
| 223 | { |
| 224 | getToVerificationStarted(staticLayoutBlobId); |
| 225 | |
| 226 | std::vector<std::uint8_t> bytes = {0x01, 0x02}; |
| 227 | EXPECT_FALSE(handler->write(session, 0, bytes)); |
| 228 | } |
Patrick Venture | fb74ad5 | 2019-05-24 15:03:35 -0700 | [diff] [blame] | 229 | |
| 230 | /* |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 231 | * 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] | 232 | */ |
| 233 | TEST_F(FirmwareHandlerVerificationStartedTest, |
| 234 | AttemptToOpenImageFileReturnsFailure) |
| 235 | { |
| 236 | /* Attempt to open a file one normally can open, however, as there is |
| 237 | * already a file open, this will fail. |
| 238 | */ |
| 239 | getToVerificationStarted(staticLayoutBlobId); |
| 240 | |
Patrick Venture | 2c01415 | 2019-05-28 18:16:05 -0700 | [diff] [blame] | 241 | auto blobsToOpen = handler->getBlobIds(); |
| 242 | for (const auto& blob : blobsToOpen) |
| 243 | { |
| 244 | EXPECT_FALSE(handler->open(session + 1, flags, blob)); |
| 245 | } |
Patrick Venture | d450910 | 2019-05-24 15:22:04 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /* |
Patrick Venture | 8facb38 | 2019-05-24 15:27:36 -0700 | [diff] [blame] | 249 | * read(session) |
| 250 | */ |
| 251 | TEST_F(FirmwareHandlerVerificationStartedTest, ReadOfVerifyBlobReturnsEmpty) |
| 252 | { |
| 253 | getToVerificationStarted(staticLayoutBlobId); |
Patrick Venture | 2c01415 | 2019-05-28 18:16:05 -0700 | [diff] [blame] | 254 | EXPECT_THAT(handler->read(session, 0, 1), IsEmpty()); |
Patrick Venture | 8facb38 | 2019-05-24 15:27:36 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /* |
Patrick Venture | ddc3507 | 2019-05-24 15:30:57 -0700 | [diff] [blame] | 258 | * commit(session) |
| 259 | */ |
| 260 | TEST_F(FirmwareHandlerVerificationStartedTest, |
| 261 | CommitOnVerifyDuringVerificationHasNoImpact) |
| 262 | { |
| 263 | getToVerificationStarted(staticLayoutBlobId); |
| 264 | EXPECT_TRUE(handler->commit(session, {})); |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 265 | expectedState(FirmwareBlobHandler::UpdateState::verificationStarted); |
Patrick Venture | ddc3507 | 2019-05-24 15:30:57 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* |
Patrick Venture | a04997b | 2019-05-24 10:15:48 -0700 | [diff] [blame] | 269 | * close(session) - close while state if verificationStarted without calling |
| 270 | * stat first will abort. |
Patrick Venture | 35b3fc9 | 2019-05-24 15:36:01 -0700 | [diff] [blame] | 271 | * TODO: implement this test when we implement abort. |
Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame] | 272 | */ |
| 273 | |
| 274 | } // namespace |
| 275 | } // namespace ipmi_flash |