Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -0700 | [diff] [blame] | 1 | /** |
| 2 | * The goal of these tests is to verify the behavior of all blob commands given |
| 3 | * the current state is verificationPending. This state is achieved as a |
| 4 | * transition out of uploadInProgress. |
| 5 | */ |
| 6 | #include "firmware_handler.hpp" |
| 7 | #include "firmware_unittest.hpp" |
Patrick Venture | 01a3327 | 2019-05-23 19:48:22 -0700 | [diff] [blame] | 8 | #include "status.hpp" |
Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -0700 | [diff] [blame] | 9 | #include "util.hpp" |
| 10 | |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 11 | #include <algorithm> |
Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -0700 | [diff] [blame] | 12 | #include <cstdint> |
| 13 | #include <string> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include <gtest/gtest.h> |
| 17 | |
| 18 | namespace ipmi_flash |
| 19 | { |
| 20 | namespace |
| 21 | { |
| 22 | |
Patrick Venture | 2567ebc | 2019-05-24 10:02:53 -0700 | [diff] [blame] | 23 | using ::testing::IsEmpty; |
Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -0700 | [diff] [blame] | 24 | using ::testing::Return; |
Patrick Venture | efba42d | 2019-05-24 10:48:16 -0700 | [diff] [blame] | 25 | using ::testing::UnorderedElementsAreArray; |
Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -0700 | [diff] [blame] | 26 | |
| 27 | /* |
| 28 | * There are the following calls (parameters may vary): |
| 29 | * canHandleBlob(blob) |
| 30 | * getBlobIds |
| 31 | * deleteBlob(blob) |
| 32 | * stat(blob) |
| 33 | * stat(session) |
| 34 | * open(blob) |
| 35 | * close(session) |
| 36 | * writemeta(session) |
| 37 | * write(session) |
| 38 | * read(session) |
| 39 | * commit(session) |
| 40 | * |
| 41 | * Testing canHandleBlob is uninteresting in this state. Getting the BlobIDs |
| 42 | * will inform what canHandleBlob will return. |
| 43 | */ |
| 44 | |
| 45 | class FirmwareHandlerVerificationPendingTest : public IpmiOnlyFirmwareStaticTest |
| 46 | { |
| 47 | protected: |
| 48 | void getToVerificationPending(const std::string& blobId) |
| 49 | { |
Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -0700 | [diff] [blame] | 50 | EXPECT_CALL(imageMock, open(blobId)).WillOnce(Return(true)); |
| 51 | EXPECT_TRUE(handler->open(session, flags, blobId)); |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 52 | expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress); |
| 53 | |
Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -0700 | [diff] [blame] | 54 | EXPECT_CALL(imageMock, close()).WillRepeatedly(Return()); |
| 55 | handler->close(session); |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 56 | expectedState(FirmwareBlobHandler::UpdateState::verificationPending); |
Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | std::uint16_t session = 1; |
| 60 | std::uint16_t flags = |
| 61 | blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi; |
| 62 | }; |
| 63 | |
| 64 | /* |
| 65 | * getBlobIds |
Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -0700 | [diff] [blame] | 66 | */ |
Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -0700 | [diff] [blame] | 67 | TEST_F(FirmwareHandlerVerificationPendingTest, VerifyBlobIdAvailableInState) |
| 68 | { |
| 69 | /* Only in the verificationPending state (and later), should the |
| 70 | * verifyBlobId be present. */ |
| 71 | |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 72 | EXPECT_FALSE(handler->canHandleBlob(verifyBlobId)); |
| 73 | |
Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -0700 | [diff] [blame] | 74 | getToVerificationPending(staticLayoutBlobId); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 75 | |
Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -0700 | [diff] [blame] | 76 | EXPECT_TRUE(handler->canHandleBlob(verifyBlobId)); |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 77 | EXPECT_TRUE(handler->canHandleBlob(activeImageBlobId)); |
Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 80 | /* |
| 81 | * delete(blob) TODO: Implement this. |
| 82 | */ |
| 83 | |
| 84 | /* |
| 85 | * stat(blob) |
| 86 | */ |
| 87 | TEST_F(FirmwareHandlerVerificationPendingTest, StatOnActiveImageReturnsFailure) |
| 88 | { |
| 89 | getToVerificationPending(staticLayoutBlobId); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 90 | ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId)); |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 91 | |
| 92 | blobs::BlobMeta meta; |
| 93 | EXPECT_FALSE(handler->stat(activeImageBlobId, &meta)); |
| 94 | } |
| 95 | |
| 96 | TEST_F(FirmwareHandlerVerificationPendingTest, StatOnActiveHashReturnsFailure) |
| 97 | { |
| 98 | getToVerificationPending(hashBlobId); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 99 | ASSERT_TRUE(handler->canHandleBlob(activeHashBlobId)); |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 100 | |
| 101 | blobs::BlobMeta meta; |
| 102 | EXPECT_FALSE(handler->stat(activeHashBlobId, &meta)); |
| 103 | } |
| 104 | |
| 105 | TEST_F(FirmwareHandlerVerificationPendingTest, |
| 106 | StatOnVerificationBlobReturnsFailure) |
| 107 | { |
| 108 | getToVerificationPending(hashBlobId); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 109 | ASSERT_TRUE(handler->canHandleBlob(activeHashBlobId)); |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 110 | |
| 111 | blobs::BlobMeta meta; |
| 112 | EXPECT_FALSE(handler->stat(verifyBlobId, &meta)); |
| 113 | } |
| 114 | |
| 115 | TEST_F(FirmwareHandlerVerificationPendingTest, StatOnNormalBlobsReturnsSuccess) |
| 116 | { |
| 117 | getToVerificationPending(staticLayoutBlobId); |
| 118 | |
| 119 | blobs::BlobMeta expected; |
| 120 | expected.blobState = FirmwareBlobHandler::UpdateFlags::ipmi; |
| 121 | expected.size = 0; |
| 122 | |
| 123 | std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId}; |
| 124 | for (const auto& blob : testBlobs) |
| 125 | { |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 126 | ASSERT_TRUE(handler->canHandleBlob(blob)); |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 127 | blobs::BlobMeta meta = {}; |
| 128 | EXPECT_TRUE(handler->stat(blob, &meta)); |
| 129 | EXPECT_EQ(expected, meta); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /* |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 134 | * open(blob) |
| 135 | */ |
Patrick Venture | da8fcd1 | 2019-05-23 18:53:50 -0700 | [diff] [blame] | 136 | TEST_F(FirmwareHandlerVerificationPendingTest, OpenVerifyBlobSucceeds) |
| 137 | { |
| 138 | getToVerificationPending(staticLayoutBlobId); |
| 139 | |
| 140 | /* the session is safe because it was already closed to get to this state. |
| 141 | */ |
| 142 | EXPECT_TRUE(handler->open(session, flags, verifyBlobId)); |
| 143 | } |
| 144 | |
| 145 | TEST_F(FirmwareHandlerVerificationPendingTest, OpenActiveImageBlobFails) |
| 146 | { |
| 147 | /* Try opening the active blob Id. This test is equivalent to trying to |
| 148 | * open the active hash blob id, in that neither are ever allowed. |
| 149 | */ |
| 150 | getToVerificationPending(staticLayoutBlobId); |
| 151 | EXPECT_FALSE(handler->open(session, flags, activeImageBlobId)); |
| 152 | } |
| 153 | |
| 154 | TEST_F(FirmwareHandlerVerificationPendingTest, |
| 155 | OpenImageBlobTransitionsToUploadInProgress) |
| 156 | { |
| 157 | getToVerificationPending(staticLayoutBlobId); |
Patrick Venture | efba42d | 2019-05-24 10:48:16 -0700 | [diff] [blame] | 158 | |
| 159 | /* Verify the active blob for the image is in the list once to start. |
| 160 | * Note: This is truly tested under the notYetStarted::open() test. |
| 161 | */ |
| 162 | std::vector<std::string> expectedBlobs = {staticLayoutBlobId, hashBlobId, |
| 163 | verifyBlobId, activeImageBlobId}; |
| 164 | |
| 165 | EXPECT_THAT(handler->getBlobIds(), |
| 166 | UnorderedElementsAreArray(expectedBlobs)); |
| 167 | |
Patrick Venture | da8fcd1 | 2019-05-23 18:53:50 -0700 | [diff] [blame] | 168 | EXPECT_CALL(imageMock, open(staticLayoutBlobId)).WillOnce(Return(true)); |
| 169 | EXPECT_TRUE(handler->open(session, flags, staticLayoutBlobId)); |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 170 | expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress); |
Patrick Venture | efba42d | 2019-05-24 10:48:16 -0700 | [diff] [blame] | 171 | |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 172 | expectedBlobs.erase( |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 173 | std::remove(expectedBlobs.begin(), expectedBlobs.end(), verifyBlobId), |
| 174 | expectedBlobs.end()); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 175 | |
| 176 | /* Verify the active blob ID was not added to the list twice and |
| 177 | * verifyBlobId is removed |
| 178 | */ |
Patrick Venture | efba42d | 2019-05-24 10:48:16 -0700 | [diff] [blame] | 179 | EXPECT_THAT(handler->getBlobIds(), |
| 180 | UnorderedElementsAreArray(expectedBlobs)); |
Patrick Venture | da8fcd1 | 2019-05-23 18:53:50 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /* |
Patrick Venture | 1e389c9 | 2019-05-23 19:15:05 -0700 | [diff] [blame] | 184 | * close(session) |
| 185 | */ |
| 186 | TEST_F(FirmwareHandlerVerificationPendingTest, |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 187 | ClosingVerifyBlobWithoutCommitDoesNotChangeState) |
Patrick Venture | 1e389c9 | 2019-05-23 19:15:05 -0700 | [diff] [blame] | 188 | { |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 189 | /* commit() will change the state, closing post-commit is part of |
| 190 | * verificationStarted testing. |
| 191 | */ |
Patrick Venture | 1e389c9 | 2019-05-23 19:15:05 -0700 | [diff] [blame] | 192 | getToVerificationPending(staticLayoutBlobId); |
| 193 | EXPECT_TRUE(handler->open(session, flags, verifyBlobId)); |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 194 | expectedState(FirmwareBlobHandler::UpdateState::verificationPending); |
Patrick Venture | 1e389c9 | 2019-05-23 19:15:05 -0700 | [diff] [blame] | 195 | |
| 196 | handler->close(session); |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 197 | expectedState(FirmwareBlobHandler::UpdateState::verificationPending); |
Patrick Venture | 1e389c9 | 2019-05-23 19:15:05 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /* |
Patrick Venture | 19044e1 | 2019-05-23 19:30:28 -0700 | [diff] [blame] | 201 | * commit(session) |
| 202 | */ |
| 203 | TEST_F(FirmwareHandlerVerificationPendingTest, |
| 204 | CommitOnVerifyBlobTriggersVerificationAndStateTransition) |
| 205 | { |
| 206 | getToVerificationPending(staticLayoutBlobId); |
| 207 | EXPECT_TRUE(handler->open(session, flags, verifyBlobId)); |
| 208 | EXPECT_CALL(*verifyMockPtr, triggerVerification()).WillOnce(Return(true)); |
| 209 | |
| 210 | EXPECT_TRUE(handler->commit(session, {})); |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 211 | expectedState(FirmwareBlobHandler::UpdateState::verificationStarted); |
Patrick Venture | 19044e1 | 2019-05-23 19:30:28 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* |
Patrick Venture | da8fcd1 | 2019-05-23 18:53:50 -0700 | [diff] [blame] | 215 | * stat(session) - in this state, you can only open(verifyBlobId) without |
| 216 | * changing state. |
| 217 | */ |
Patrick Venture | 01a3327 | 2019-05-23 19:48:22 -0700 | [diff] [blame] | 218 | TEST_F(FirmwareHandlerVerificationPendingTest, StatOnVerifyBlobIdReturnsState) |
| 219 | { |
| 220 | /* If this is called before commit(), it's still verificationPending, so it |
| 221 | * just returns the state is other |
| 222 | */ |
| 223 | getToVerificationPending(staticLayoutBlobId); |
| 224 | EXPECT_TRUE(handler->open(session, flags, verifyBlobId)); |
| 225 | EXPECT_CALL(*verifyMockPtr, triggerVerification()).Times(0); |
| 226 | EXPECT_CALL(*verifyMockPtr, checkVerificationState()).Times(0); |
| 227 | |
| 228 | blobs::BlobMeta meta, expectedMeta = {}; |
| 229 | expectedMeta.size = 0; |
| 230 | expectedMeta.blobState = flags; |
| 231 | expectedMeta.metadata.push_back( |
| 232 | static_cast<std::uint8_t>(VerifyCheckResponses::other)); |
| 233 | |
| 234 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 235 | EXPECT_EQ(expectedMeta, meta); |
| 236 | } |
| 237 | |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 238 | /* |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 239 | * writemeta(session) |
| 240 | */ |
Patrick Venture | b611a08 | 2019-05-23 20:27:28 -0700 | [diff] [blame] | 241 | TEST_F(FirmwareHandlerVerificationPendingTest, WriteMetaAgainstVerifyFails) |
| 242 | { |
| 243 | /* The verifyBlobId has no data handler, which means write meta fails. */ |
| 244 | getToVerificationPending(staticLayoutBlobId); |
| 245 | |
| 246 | EXPECT_TRUE(handler->open(session, flags, verifyBlobId)); |
| 247 | |
| 248 | std::vector<std::uint8_t> bytes = {0x01, 0x02}; |
| 249 | EXPECT_FALSE(handler->writeMeta(session, 0, bytes)); |
| 250 | } |
| 251 | |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 252 | /* |
| 253 | * write(session) |
| 254 | */ |
Patrick Venture | ab731e9 | 2019-05-24 09:58:00 -0700 | [diff] [blame] | 255 | TEST_F(FirmwareHandlerVerificationPendingTest, WriteAgainstVerifyBlobIdFails) |
| 256 | { |
| 257 | getToVerificationPending(staticLayoutBlobId); |
| 258 | |
| 259 | EXPECT_TRUE(handler->open(session, flags, verifyBlobId)); |
| 260 | |
| 261 | std::vector<std::uint8_t> bytes = {0x01, 0x02}; |
| 262 | EXPECT_FALSE(handler->write(session, 0, bytes)); |
| 263 | } |
| 264 | |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 265 | /* |
| 266 | * read(session) |
| 267 | */ |
Patrick Venture | 2567ebc | 2019-05-24 10:02:53 -0700 | [diff] [blame] | 268 | TEST_F(FirmwareHandlerVerificationPendingTest, |
| 269 | ReadAgainstVerifyBlobIdReturnsEmpty) |
| 270 | { |
| 271 | getToVerificationPending(staticLayoutBlobId); |
| 272 | |
| 273 | EXPECT_TRUE(handler->open(session, flags, verifyBlobId)); |
| 274 | EXPECT_THAT(handler->read(session, 0, 32), IsEmpty()); |
| 275 | } |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 276 | |
Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -0700 | [diff] [blame] | 277 | } // namespace |
| 278 | } // namespace ipmi_flash |