Patrick Venture | ebcc522 | 2019-05-23 10:36:40 -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 uploadInProgress. This state is achieved when an image |
| 4 | * or hash blob is opened and the handler is expected to receive bytes. |
| 5 | */ |
| 6 | #include "firmware_handler.hpp" |
| 7 | #include "firmware_unittest.hpp" |
| 8 | |
Patrick Venture | 615123a | 2019-05-23 17:20:07 -0700 | [diff] [blame] | 9 | #include <cstdint> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
Patrick Venture | ebcc522 | 2019-05-23 10:36:40 -0700 | [diff] [blame] | 13 | #include <gtest/gtest.h> |
| 14 | |
| 15 | namespace ipmi_flash |
| 16 | { |
| 17 | namespace |
| 18 | { |
| 19 | |
Patrick Venture | 615123a | 2019-05-23 17:20:07 -0700 | [diff] [blame] | 20 | using ::testing::ContainerEq; |
Patrick Venture | 8326d07 | 2019-05-23 17:45:42 -0700 | [diff] [blame] | 21 | using ::testing::IsEmpty; |
Patrick Venture | ebcc522 | 2019-05-23 10:36:40 -0700 | [diff] [blame] | 22 | using ::testing::Return; |
| 23 | using ::testing::UnorderedElementsAreArray; |
| 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) |
Patrick Venture | 547fa3a | 2019-05-23 16:06:37 -0700 | [diff] [blame] | 37 | * commit(session) |
Patrick Venture | ebcc522 | 2019-05-23 10:36:40 -0700 | [diff] [blame] | 38 | * |
| 39 | * Testing canHandleBlob is uninteresting in this state. Getting the BlobIDs |
| 40 | * will inform what canHandleBlob will return. |
| 41 | */ |
| 42 | class FirmwareHandlerUploadInProgressTest : public IpmiOnlyFirmwareStaticTest |
| 43 | { |
Patrick Venture | be0fb5e | 2019-05-23 16:14:20 -0700 | [diff] [blame] | 44 | protected: |
| 45 | void openToInProgress(const std::string& blobId) |
| 46 | { |
| 47 | auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get()); |
| 48 | EXPECT_CALL(imageMock, open(blobId)).WillOnce(Return(true)); |
| 49 | EXPECT_TRUE(handler->open(session, flags, blobId)); |
| 50 | EXPECT_EQ(FirmwareBlobHandler::UpdateState::uploadInProgress, |
| 51 | realHandler->getCurrentState()); |
| 52 | } |
| 53 | |
| 54 | std::uint16_t session = 1; |
| 55 | std::uint16_t flags = |
| 56 | blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi; |
Patrick Venture | ebcc522 | 2019-05-23 10:36:40 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | TEST_F(FirmwareHandlerUploadInProgressTest, GetBlobIdsVerifyOutputActiveImage) |
| 60 | { |
| 61 | /* Opening the image file will add the active image blob id */ |
Patrick Venture | be0fb5e | 2019-05-23 16:14:20 -0700 | [diff] [blame] | 62 | openToInProgress(staticLayoutBlobId); |
Patrick Venture | ebcc522 | 2019-05-23 10:36:40 -0700 | [diff] [blame] | 63 | |
| 64 | std::vector<std::string> expectedAfterImage = { |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame^] | 65 | staticLayoutBlobId, hashBlobId, activeImageBlobId}; |
Patrick Venture | ebcc522 | 2019-05-23 10:36:40 -0700 | [diff] [blame] | 66 | EXPECT_THAT(handler->getBlobIds(), |
| 67 | UnorderedElementsAreArray(expectedAfterImage)); |
| 68 | } |
| 69 | |
| 70 | TEST_F(FirmwareHandlerUploadInProgressTest, GetBlobIdsVerifyOutputActiveHash) |
| 71 | { |
| 72 | /* Opening the image file will add the active image blob id */ |
Patrick Venture | be0fb5e | 2019-05-23 16:14:20 -0700 | [diff] [blame] | 73 | openToInProgress(hashBlobId); |
Patrick Venture | ebcc522 | 2019-05-23 10:36:40 -0700 | [diff] [blame] | 74 | |
| 75 | std::vector<std::string> expectedAfterImage = { |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame^] | 76 | staticLayoutBlobId, hashBlobId, activeHashBlobId}; |
Patrick Venture | ebcc522 | 2019-05-23 10:36:40 -0700 | [diff] [blame] | 77 | EXPECT_THAT(handler->getBlobIds(), |
| 78 | UnorderedElementsAreArray(expectedAfterImage)); |
| 79 | } |
| 80 | |
Patrick Venture | 6f72978 | 2019-05-23 11:16:13 -0700 | [diff] [blame] | 81 | /* TODO: Try deleting some blobs -- in this state it will depend on what the |
| 82 | * blob id is, but it's not yet implemented |
| 83 | */ |
| 84 | |
Patrick Venture | 41205cc | 2019-05-23 11:43:43 -0700 | [diff] [blame] | 85 | /* |
| 86 | * stat(blob) |
| 87 | */ |
| 88 | TEST_F(FirmwareHandlerUploadInProgressTest, StatOnActiveImageReturnsFailure) |
| 89 | { |
| 90 | /* you cannot call stat() on the active image or the active hash or the |
| 91 | * verify blob. |
| 92 | */ |
Patrick Venture | be0fb5e | 2019-05-23 16:14:20 -0700 | [diff] [blame] | 93 | openToInProgress(staticLayoutBlobId); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame^] | 94 | ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId)); |
Patrick Venture | 41205cc | 2019-05-23 11:43:43 -0700 | [diff] [blame] | 95 | |
Patrick Venture | 41205cc | 2019-05-23 11:43:43 -0700 | [diff] [blame] | 96 | blobs::BlobMeta meta; |
| 97 | EXPECT_FALSE(handler->stat(activeImageBlobId, &meta)); |
| 98 | } |
| 99 | |
| 100 | TEST_F(FirmwareHandlerUploadInProgressTest, StatOnActiveHashReturnsFailure) |
| 101 | { |
| 102 | /* this test is separate from the active image one so that the state doesn't |
| 103 | * change from close. |
| 104 | */ |
Patrick Venture | be0fb5e | 2019-05-23 16:14:20 -0700 | [diff] [blame] | 105 | openToInProgress(hashBlobId); |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame^] | 106 | ASSERT_TRUE(handler->canHandleBlob(activeHashBlobId)); |
Patrick Venture | 41205cc | 2019-05-23 11:43:43 -0700 | [diff] [blame] | 107 | |
Patrick Venture | 41205cc | 2019-05-23 11:43:43 -0700 | [diff] [blame] | 108 | blobs::BlobMeta meta; |
| 109 | EXPECT_FALSE(handler->stat(activeHashBlobId, &meta)); |
| 110 | } |
| 111 | |
| 112 | TEST_F(FirmwareHandlerUploadInProgressTest, StatOnNormalBlobsReturnsSuccess) |
| 113 | { |
| 114 | /* Calling stat() on the normal blobs (not the active) ones will work and |
| 115 | * return the same information as in the notYetStarted state. |
| 116 | */ |
| 117 | blobs::BlobMeta expected; |
| 118 | expected.blobState = FirmwareBlobHandler::UpdateFlags::ipmi; |
| 119 | expected.size = 0; |
| 120 | |
Patrick Venture | be0fb5e | 2019-05-23 16:14:20 -0700 | [diff] [blame] | 121 | openToInProgress(staticLayoutBlobId); |
Patrick Venture | 41205cc | 2019-05-23 11:43:43 -0700 | [diff] [blame] | 122 | |
| 123 | std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId}; |
| 124 | for (const auto& blob : testBlobs) |
| 125 | { |
| 126 | blobs::BlobMeta meta = {}; |
| 127 | EXPECT_TRUE(handler->stat(blob, &meta)); |
| 128 | EXPECT_EQ(expected, meta); |
| 129 | } |
| 130 | } |
| 131 | |
Patrick Venture | 41205cc | 2019-05-23 11:43:43 -0700 | [diff] [blame] | 132 | /* |
| 133 | * stat(session) |
Patrick Venture | efc366e | 2019-05-23 12:00:21 -0700 | [diff] [blame] | 134 | */ |
| 135 | TEST_F(FirmwareHandlerUploadInProgressTest, |
| 136 | CallingStatOnActiveImageOrHashSessionReturnsDetails) |
| 137 | { |
| 138 | /* This test will verify that the underlying image handler is called with |
| 139 | * this stat, in addition to the normal information. |
| 140 | */ |
Patrick Venture | be0fb5e | 2019-05-23 16:14:20 -0700 | [diff] [blame] | 141 | openToInProgress(staticLayoutBlobId); |
Patrick Venture | efc366e | 2019-05-23 12:00:21 -0700 | [diff] [blame] | 142 | |
| 143 | EXPECT_CALL(imageMock, getSize()).WillOnce(Return(32)); |
| 144 | |
| 145 | blobs::BlobMeta meta, expectedMeta = {}; |
| 146 | expectedMeta.size = 32; |
| 147 | expectedMeta.blobState = |
| 148 | blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi; |
| 149 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 150 | EXPECT_EQ(expectedMeta, meta); |
| 151 | } |
| 152 | |
| 153 | /* |
Patrick Venture | 5788dbb | 2019-05-23 12:25:42 -0700 | [diff] [blame] | 154 | * open(blob) - While any blob is open, all other fail. |
| 155 | */ |
| 156 | TEST_F(FirmwareHandlerUploadInProgressTest, OpeningHashFileWhileImageOpenFails) |
| 157 | { |
| 158 | /* To be in this state, something must be open (and specifically either an |
| 159 | * active image (or tarball) or the hash file. Also verifies you can't just |
| 160 | * re-open the currently open file. |
| 161 | */ |
Patrick Venture | be0fb5e | 2019-05-23 16:14:20 -0700 | [diff] [blame] | 162 | openToInProgress(staticLayoutBlobId); |
Patrick Venture | 5788dbb | 2019-05-23 12:25:42 -0700 | [diff] [blame] | 163 | |
| 164 | std::vector<std::string> blobsToTry = { |
| 165 | hashBlobId, activeImageBlobId, activeHashBlobId, staticLayoutBlobId}; |
| 166 | for (const auto& blob : blobsToTry) |
| 167 | { |
| 168 | EXPECT_FALSE(handler->open(2, flags, blob)); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | TEST_F(FirmwareHandlerUploadInProgressTest, OpeningImageFileWhileHashOpenFails) |
| 173 | { |
Patrick Venture | be0fb5e | 2019-05-23 16:14:20 -0700 | [diff] [blame] | 174 | openToInProgress(hashBlobId); |
Patrick Venture | 5788dbb | 2019-05-23 12:25:42 -0700 | [diff] [blame] | 175 | |
| 176 | std::vector<std::string> blobsToTry = { |
| 177 | hashBlobId, activeImageBlobId, activeHashBlobId, staticLayoutBlobId}; |
| 178 | for (const auto& blob : blobsToTry) |
| 179 | { |
| 180 | EXPECT_FALSE(handler->open(2, flags, blob)); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /* |
Patrick Venture | 79b4474 | 2019-05-23 12:36:11 -0700 | [diff] [blame] | 185 | * close(session) - closing the hash or image will trigger a state transition to |
| 186 | * verificationPending. |
Patrick Venture | 79b4474 | 2019-05-23 12:36:11 -0700 | [diff] [blame] | 187 | * |
| 188 | * NOTE: Re-opening /flash/image will transition back to uploadInProgress, but |
| 189 | * that is verified in the verificationPending::open tests. |
| 190 | */ |
| 191 | TEST_F(FirmwareHandlerUploadInProgressTest, |
| 192 | ClosingImageFileTransitionsToVerificationPending) |
| 193 | { |
Patrick Venture | 79b4474 | 2019-05-23 12:36:11 -0700 | [diff] [blame] | 194 | auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get()); |
| 195 | |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame^] | 196 | EXPECT_FALSE(handler->canHandleBlob(verifyBlobId)); |
Patrick Venture | 79b4474 | 2019-05-23 12:36:11 -0700 | [diff] [blame] | 197 | |
| 198 | EXPECT_CALL(imageMock, open(staticLayoutBlobId)).WillOnce(Return(true)); |
| 199 | |
Patrick Venture | be0fb5e | 2019-05-23 16:14:20 -0700 | [diff] [blame] | 200 | EXPECT_TRUE(handler->open(session, flags, staticLayoutBlobId)); |
Patrick Venture | 79b4474 | 2019-05-23 12:36:11 -0700 | [diff] [blame] | 201 | EXPECT_EQ(FirmwareBlobHandler::UpdateState::uploadInProgress, |
| 202 | realHandler->getCurrentState()); |
| 203 | |
| 204 | handler->close(1); |
| 205 | EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationPending, |
| 206 | realHandler->getCurrentState()); |
| 207 | |
| 208 | EXPECT_TRUE(handler->canHandleBlob(verifyBlobId)); |
| 209 | } |
| 210 | |
| 211 | TEST_F(FirmwareHandlerUploadInProgressTest, |
| 212 | ClosingHashFileTransitionsToVerificationPending) |
| 213 | { |
Patrick Venture | 79b4474 | 2019-05-23 12:36:11 -0700 | [diff] [blame] | 214 | auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get()); |
| 215 | |
Patrick Venture | 930c7b7 | 2019-05-24 11:11:08 -0700 | [diff] [blame^] | 216 | EXPECT_FALSE(handler->canHandleBlob(verifyBlobId)); |
Patrick Venture | 79b4474 | 2019-05-23 12:36:11 -0700 | [diff] [blame] | 217 | |
| 218 | EXPECT_CALL(imageMock, open(hashBlobId)).WillOnce(Return(true)); |
| 219 | |
Patrick Venture | be0fb5e | 2019-05-23 16:14:20 -0700 | [diff] [blame] | 220 | EXPECT_TRUE(handler->open(session, flags, hashBlobId)); |
Patrick Venture | 79b4474 | 2019-05-23 12:36:11 -0700 | [diff] [blame] | 221 | EXPECT_EQ(FirmwareBlobHandler::UpdateState::uploadInProgress, |
| 222 | realHandler->getCurrentState()); |
| 223 | |
| 224 | handler->close(1); |
| 225 | EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationPending, |
| 226 | realHandler->getCurrentState()); |
| 227 | |
| 228 | EXPECT_TRUE(handler->canHandleBlob(verifyBlobId)); |
| 229 | } |
| 230 | |
| 231 | /* |
Patrick Venture | 41205cc | 2019-05-23 11:43:43 -0700 | [diff] [blame] | 232 | * writemeta(session) |
Patrick Venture | 7cf4440 | 2019-05-23 13:01:47 -0700 | [diff] [blame] | 233 | */ |
Patrick Venture | 31eefd4 | 2019-05-23 20:27:41 -0700 | [diff] [blame] | 234 | TEST_F(FirmwareHandlerUploadInProgressTest, |
| 235 | WriteMetaAgainstImageReturnsFailureIfNoDataHandler) |
Patrick Venture | 7cf4440 | 2019-05-23 13:01:47 -0700 | [diff] [blame] | 236 | { |
| 237 | /* Calling write/read/writeMeta are uninteresting against the open blob in |
| 238 | * this case because the blob will just pass the call along. Whereas |
| 239 | * calling against the verify or update blob may be more interesting. |
| 240 | */ |
Patrick Venture | be0fb5e | 2019-05-23 16:14:20 -0700 | [diff] [blame] | 241 | openToInProgress(staticLayoutBlobId); |
Patrick Venture | 7cf4440 | 2019-05-23 13:01:47 -0700 | [diff] [blame] | 242 | |
Patrick Venture | 31eefd4 | 2019-05-23 20:27:41 -0700 | [diff] [blame] | 243 | /* TODO: Consider adding a test that has a data handler, but that test |
| 244 | * already exists under the general writeMeta test suite. |
| 245 | */ |
Patrick Venture | 7cf4440 | 2019-05-23 13:01:47 -0700 | [diff] [blame] | 246 | /* Note: with IPMI as the transport there's no data handler, so this should |
| 247 | * fail nicely. */ |
| 248 | std::vector<std::uint8_t> bytes = {0x01, 0x02}; |
Patrick Venture | be0fb5e | 2019-05-23 16:14:20 -0700 | [diff] [blame] | 249 | EXPECT_FALSE(handler->writeMeta(session, 0, bytes)); |
Patrick Venture | 7cf4440 | 2019-05-23 13:01:47 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /* |
Patrick Venture | 41205cc | 2019-05-23 11:43:43 -0700 | [diff] [blame] | 253 | * write(session) |
Patrick Venture | 615123a | 2019-05-23 17:20:07 -0700 | [diff] [blame] | 254 | */ |
| 255 | TEST_F(FirmwareHandlerUploadInProgressTest, WriteToImageReturnsSuccess) |
| 256 | { |
| 257 | openToInProgress(staticLayoutBlobId); |
| 258 | std::vector<std::uint8_t> bytes = {0x01, 0x02}; |
| 259 | EXPECT_CALL(imageMock, write(0, ContainerEq(bytes))).WillOnce(Return(true)); |
| 260 | EXPECT_TRUE(handler->write(session, 0, bytes)); |
| 261 | } |
| 262 | |
| 263 | TEST_F(FirmwareHandlerUploadInProgressTest, WriteToHashReturnsSuccess) |
| 264 | { |
| 265 | openToInProgress(hashBlobId); |
| 266 | std::vector<std::uint8_t> bytes = {0x01, 0x02}; |
| 267 | EXPECT_CALL(imageMock, write(0, ContainerEq(bytes))).WillOnce(Return(true)); |
| 268 | EXPECT_TRUE(handler->write(session, 0, bytes)); |
| 269 | } |
| 270 | |
| 271 | /* |
Patrick Venture | 41205cc | 2019-05-23 11:43:43 -0700 | [diff] [blame] | 272 | * read(session) |
Patrick Venture | 615123a | 2019-05-23 17:20:07 -0700 | [diff] [blame] | 273 | */ |
Patrick Venture | 8326d07 | 2019-05-23 17:45:42 -0700 | [diff] [blame] | 274 | TEST_F(FirmwareHandlerUploadInProgressTest, ReadImageFileReturnsFailure) |
| 275 | { |
| 276 | /* Read is not supported. */ |
| 277 | openToInProgress(staticLayoutBlobId); |
| 278 | EXPECT_THAT(handler->read(session, 0, 32), IsEmpty()); |
| 279 | } |
Patrick Venture | 615123a | 2019-05-23 17:20:07 -0700 | [diff] [blame] | 280 | |
| 281 | /* |
Patrick Venture | be0fb5e | 2019-05-23 16:14:20 -0700 | [diff] [blame] | 282 | * commit(session) |
Patrick Venture | 41205cc | 2019-05-23 11:43:43 -0700 | [diff] [blame] | 283 | */ |
Patrick Venture | 1fca190 | 2019-05-23 17:54:18 -0700 | [diff] [blame] | 284 | TEST_F(FirmwareHandlerUploadInProgressTest, |
| 285 | CommitAgainstImageFileReturnsFailure) |
| 286 | { |
| 287 | /* Commit is only valid against specific blobs. */ |
| 288 | openToInProgress(staticLayoutBlobId); |
| 289 | EXPECT_FALSE(handler->commit(session, {})); |
| 290 | } |
| 291 | |
| 292 | TEST_F(FirmwareHandlerUploadInProgressTest, CommitAgainstHashFileReturnsFailure) |
| 293 | { |
| 294 | openToInProgress(hashBlobId); |
| 295 | EXPECT_FALSE(handler->commit(session, {})); |
| 296 | } |
Patrick Venture | 41205cc | 2019-05-23 11:43:43 -0700 | [diff] [blame] | 297 | |
Patrick Venture | ebcc522 | 2019-05-23 10:36:40 -0700 | [diff] [blame] | 298 | } // namespace |
| 299 | } // namespace ipmi_flash |