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 | { |
Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | /* |
| 50 | * getBlobIds |
Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -0700 | [diff] [blame] | 51 | */ |
Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -0700 | [diff] [blame] | 52 | TEST_F(FirmwareHandlerVerificationPendingTest, VerifyBlobIdAvailableInState) |
| 53 | { |
| 54 | /* Only in the verificationPending state (and later), should the |
Patrick Venture | 0f82ce4 | 2019-05-28 14:06:04 -0700 | [diff] [blame] | 55 | * verifyBlobId be present. |
| 56 | */ |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 57 | EXPECT_FALSE(handler->canHandleBlob(verifyBlobId)); |
| 58 | |
Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -0700 | [diff] [blame] | 59 | getToVerificationPending(staticLayoutBlobId); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 60 | |
Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -0700 | [diff] [blame] | 61 | EXPECT_TRUE(handler->canHandleBlob(verifyBlobId)); |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 62 | EXPECT_TRUE(handler->canHandleBlob(activeImageBlobId)); |
Patrick Venture | 0f82ce4 | 2019-05-28 14:06:04 -0700 | [diff] [blame] | 63 | EXPECT_FALSE(handler->canHandleBlob(updateBlobId)); |
Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 66 | /* |
| 67 | * delete(blob) TODO: Implement this. |
| 68 | */ |
| 69 | |
| 70 | /* |
| 71 | * stat(blob) |
| 72 | */ |
| 73 | TEST_F(FirmwareHandlerVerificationPendingTest, StatOnActiveImageReturnsFailure) |
| 74 | { |
| 75 | getToVerificationPending(staticLayoutBlobId); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 76 | ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId)); |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 77 | |
| 78 | blobs::BlobMeta meta; |
| 79 | EXPECT_FALSE(handler->stat(activeImageBlobId, &meta)); |
| 80 | } |
| 81 | |
| 82 | TEST_F(FirmwareHandlerVerificationPendingTest, StatOnActiveHashReturnsFailure) |
| 83 | { |
| 84 | getToVerificationPending(hashBlobId); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 85 | ASSERT_TRUE(handler->canHandleBlob(activeHashBlobId)); |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 86 | |
| 87 | blobs::BlobMeta meta; |
| 88 | EXPECT_FALSE(handler->stat(activeHashBlobId, &meta)); |
| 89 | } |
| 90 | |
| 91 | TEST_F(FirmwareHandlerVerificationPendingTest, |
| 92 | StatOnVerificationBlobReturnsFailure) |
| 93 | { |
| 94 | getToVerificationPending(hashBlobId); |
Patrick Venture | 0f82ce4 | 2019-05-28 14:06:04 -0700 | [diff] [blame] | 95 | ASSERT_TRUE(handler->canHandleBlob(verifyBlobId)); |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 96 | |
| 97 | blobs::BlobMeta meta; |
| 98 | EXPECT_FALSE(handler->stat(verifyBlobId, &meta)); |
| 99 | } |
| 100 | |
| 101 | TEST_F(FirmwareHandlerVerificationPendingTest, StatOnNormalBlobsReturnsSuccess) |
| 102 | { |
| 103 | getToVerificationPending(staticLayoutBlobId); |
| 104 | |
| 105 | blobs::BlobMeta expected; |
| 106 | expected.blobState = FirmwareBlobHandler::UpdateFlags::ipmi; |
| 107 | expected.size = 0; |
| 108 | |
| 109 | std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId}; |
| 110 | for (const auto& blob : testBlobs) |
| 111 | { |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 112 | ASSERT_TRUE(handler->canHandleBlob(blob)); |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 113 | blobs::BlobMeta meta = {}; |
| 114 | EXPECT_TRUE(handler->stat(blob, &meta)); |
| 115 | EXPECT_EQ(expected, meta); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /* |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 120 | * open(blob) |
| 121 | */ |
Patrick Venture | da8fcd1 | 2019-05-23 18:53:50 -0700 | [diff] [blame] | 122 | TEST_F(FirmwareHandlerVerificationPendingTest, OpenVerifyBlobSucceeds) |
| 123 | { |
| 124 | getToVerificationPending(staticLayoutBlobId); |
| 125 | |
| 126 | /* the session is safe because it was already closed to get to this state. |
| 127 | */ |
| 128 | EXPECT_TRUE(handler->open(session, flags, verifyBlobId)); |
| 129 | } |
| 130 | |
Patrick Venture | 0f82ce4 | 2019-05-28 14:06:04 -0700 | [diff] [blame] | 131 | TEST_F(FirmwareHandlerVerificationPendingTest, OpenActiveBlobsFail) |
Patrick Venture | da8fcd1 | 2019-05-23 18:53:50 -0700 | [diff] [blame] | 132 | { |
| 133 | /* Try opening the active blob Id. This test is equivalent to trying to |
| 134 | * open the active hash blob id, in that neither are ever allowed. |
| 135 | */ |
| 136 | getToVerificationPending(staticLayoutBlobId); |
| 137 | EXPECT_FALSE(handler->open(session, flags, activeImageBlobId)); |
Patrick Venture | 0f82ce4 | 2019-05-28 14:06:04 -0700 | [diff] [blame] | 138 | EXPECT_FALSE(handler->open(session, flags, activeHashBlobId)); |
Patrick Venture | da8fcd1 | 2019-05-23 18:53:50 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | TEST_F(FirmwareHandlerVerificationPendingTest, |
| 142 | OpenImageBlobTransitionsToUploadInProgress) |
| 143 | { |
| 144 | getToVerificationPending(staticLayoutBlobId); |
Patrick Venture | efba42d | 2019-05-24 10:48:16 -0700 | [diff] [blame] | 145 | |
| 146 | /* Verify the active blob for the image is in the list once to start. |
| 147 | * Note: This is truly tested under the notYetStarted::open() test. |
| 148 | */ |
| 149 | std::vector<std::string> expectedBlobs = {staticLayoutBlobId, hashBlobId, |
| 150 | verifyBlobId, activeImageBlobId}; |
| 151 | |
| 152 | EXPECT_THAT(handler->getBlobIds(), |
| 153 | UnorderedElementsAreArray(expectedBlobs)); |
| 154 | |
Patrick Venture | da8fcd1 | 2019-05-23 18:53:50 -0700 | [diff] [blame] | 155 | EXPECT_CALL(imageMock, open(staticLayoutBlobId)).WillOnce(Return(true)); |
| 156 | EXPECT_TRUE(handler->open(session, flags, staticLayoutBlobId)); |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 157 | expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress); |
Patrick Venture | efba42d | 2019-05-24 10:48:16 -0700 | [diff] [blame] | 158 | |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 159 | expectedBlobs.erase( |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 160 | std::remove(expectedBlobs.begin(), expectedBlobs.end(), verifyBlobId), |
| 161 | expectedBlobs.end()); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame] | 162 | |
| 163 | /* Verify the active blob ID was not added to the list twice and |
| 164 | * verifyBlobId is removed |
| 165 | */ |
Patrick Venture | efba42d | 2019-05-24 10:48:16 -0700 | [diff] [blame] | 166 | EXPECT_THAT(handler->getBlobIds(), |
| 167 | UnorderedElementsAreArray(expectedBlobs)); |
Patrick Venture | da8fcd1 | 2019-05-23 18:53:50 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /* |
Patrick Venture | 1e389c9 | 2019-05-23 19:15:05 -0700 | [diff] [blame] | 171 | * close(session) |
| 172 | */ |
| 173 | TEST_F(FirmwareHandlerVerificationPendingTest, |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 174 | ClosingVerifyBlobWithoutCommitDoesNotChangeState) |
Patrick Venture | 1e389c9 | 2019-05-23 19:15:05 -0700 | [diff] [blame] | 175 | { |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 176 | /* commit() will change the state, closing post-commit is part of |
| 177 | * verificationStarted testing. |
| 178 | */ |
Patrick Venture | 1e389c9 | 2019-05-23 19:15:05 -0700 | [diff] [blame] | 179 | getToVerificationPending(staticLayoutBlobId); |
| 180 | EXPECT_TRUE(handler->open(session, flags, verifyBlobId)); |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 181 | expectedState(FirmwareBlobHandler::UpdateState::verificationPending); |
Patrick Venture | 1e389c9 | 2019-05-23 19:15:05 -0700 | [diff] [blame] | 182 | |
| 183 | handler->close(session); |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 184 | expectedState(FirmwareBlobHandler::UpdateState::verificationPending); |
Patrick Venture | 1e389c9 | 2019-05-23 19:15:05 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /* |
Patrick Venture | 19044e1 | 2019-05-23 19:30:28 -0700 | [diff] [blame] | 188 | * commit(session) |
| 189 | */ |
| 190 | TEST_F(FirmwareHandlerVerificationPendingTest, |
| 191 | CommitOnVerifyBlobTriggersVerificationAndStateTransition) |
| 192 | { |
| 193 | getToVerificationPending(staticLayoutBlobId); |
| 194 | EXPECT_TRUE(handler->open(session, flags, verifyBlobId)); |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame^] | 195 | EXPECT_CALL(*verifyMockPtr, trigger()).WillOnce(Return(true)); |
Patrick Venture | 19044e1 | 2019-05-23 19:30:28 -0700 | [diff] [blame] | 196 | |
| 197 | EXPECT_TRUE(handler->commit(session, {})); |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 198 | expectedState(FirmwareBlobHandler::UpdateState::verificationStarted); |
Patrick Venture | 19044e1 | 2019-05-23 19:30:28 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /* |
Patrick Venture | da8fcd1 | 2019-05-23 18:53:50 -0700 | [diff] [blame] | 202 | * stat(session) - in this state, you can only open(verifyBlobId) without |
| 203 | * changing state. |
| 204 | */ |
Patrick Venture | 01a3327 | 2019-05-23 19:48:22 -0700 | [diff] [blame] | 205 | TEST_F(FirmwareHandlerVerificationPendingTest, StatOnVerifyBlobIdReturnsState) |
| 206 | { |
| 207 | /* If this is called before commit(), it's still verificationPending, so it |
| 208 | * just returns the state is other |
| 209 | */ |
| 210 | getToVerificationPending(staticLayoutBlobId); |
| 211 | EXPECT_TRUE(handler->open(session, flags, verifyBlobId)); |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame^] | 212 | EXPECT_CALL(*verifyMockPtr, trigger()).Times(0); |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 213 | EXPECT_CALL(*verifyMockPtr, status()).Times(0); |
Patrick Venture | 01a3327 | 2019-05-23 19:48:22 -0700 | [diff] [blame] | 214 | |
| 215 | blobs::BlobMeta meta, expectedMeta = {}; |
| 216 | expectedMeta.size = 0; |
| 217 | expectedMeta.blobState = flags; |
| 218 | expectedMeta.metadata.push_back( |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 219 | static_cast<std::uint8_t>(ActionStatus::unknown)); |
Patrick Venture | 01a3327 | 2019-05-23 19:48:22 -0700 | [diff] [blame] | 220 | |
| 221 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 222 | EXPECT_EQ(expectedMeta, meta); |
| 223 | } |
| 224 | |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 225 | /* |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 226 | * writemeta(session) |
| 227 | */ |
Patrick Venture | b611a08 | 2019-05-23 20:27:28 -0700 | [diff] [blame] | 228 | TEST_F(FirmwareHandlerVerificationPendingTest, WriteMetaAgainstVerifyFails) |
| 229 | { |
| 230 | /* The verifyBlobId has no data handler, which means write meta fails. */ |
| 231 | getToVerificationPending(staticLayoutBlobId); |
| 232 | |
| 233 | EXPECT_TRUE(handler->open(session, flags, verifyBlobId)); |
| 234 | |
| 235 | std::vector<std::uint8_t> bytes = {0x01, 0x02}; |
| 236 | EXPECT_FALSE(handler->writeMeta(session, 0, bytes)); |
| 237 | } |
| 238 | |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 239 | /* |
| 240 | * write(session) |
| 241 | */ |
Patrick Venture | ab731e9 | 2019-05-24 09:58:00 -0700 | [diff] [blame] | 242 | TEST_F(FirmwareHandlerVerificationPendingTest, WriteAgainstVerifyBlobIdFails) |
| 243 | { |
| 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->write(session, 0, bytes)); |
| 250 | } |
| 251 | |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 252 | /* |
| 253 | * read(session) |
| 254 | */ |
Patrick Venture | 2567ebc | 2019-05-24 10:02:53 -0700 | [diff] [blame] | 255 | TEST_F(FirmwareHandlerVerificationPendingTest, |
| 256 | ReadAgainstVerifyBlobIdReturnsEmpty) |
| 257 | { |
| 258 | getToVerificationPending(staticLayoutBlobId); |
| 259 | |
| 260 | EXPECT_TRUE(handler->open(session, flags, verifyBlobId)); |
Patrick Venture | 0f82ce4 | 2019-05-28 14:06:04 -0700 | [diff] [blame] | 261 | EXPECT_THAT(handler->read(session, 0, 1), IsEmpty()); |
Patrick Venture | 2567ebc | 2019-05-24 10:02:53 -0700 | [diff] [blame] | 262 | } |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 263 | |
Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -0700 | [diff] [blame] | 264 | } // namespace |
| 265 | } // namespace ipmi_flash |