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 = { |
| 65 | staticLayoutBlobId, hashBlobId, verifyBlobId, activeImageBlobId}; |
| 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 = { |
| 76 | staticLayoutBlobId, hashBlobId, verifyBlobId, activeHashBlobId}; |
| 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 | 41205cc | 2019-05-23 11:43:43 -0700 | [diff] [blame] | 94 | |
Patrick Venture | 41205cc | 2019-05-23 11:43:43 -0700 | [diff] [blame] | 95 | blobs::BlobMeta meta; |
| 96 | EXPECT_FALSE(handler->stat(activeImageBlobId, &meta)); |
| 97 | } |
| 98 | |
| 99 | TEST_F(FirmwareHandlerUploadInProgressTest, StatOnActiveHashReturnsFailure) |
| 100 | { |
| 101 | /* this test is separate from the active image one so that the state doesn't |
| 102 | * change from close. |
| 103 | */ |
Patrick Venture | be0fb5e | 2019-05-23 16:14:20 -0700 | [diff] [blame] | 104 | openToInProgress(hashBlobId); |
Patrick Venture | 41205cc | 2019-05-23 11:43:43 -0700 | [diff] [blame] | 105 | |
Patrick Venture | 41205cc | 2019-05-23 11:43:43 -0700 | [diff] [blame] | 106 | blobs::BlobMeta meta; |
| 107 | EXPECT_FALSE(handler->stat(activeHashBlobId, &meta)); |
| 108 | } |
| 109 | |
| 110 | TEST_F(FirmwareHandlerUploadInProgressTest, StatOnNormalBlobsReturnsSuccess) |
| 111 | { |
| 112 | /* Calling stat() on the normal blobs (not the active) ones will work and |
| 113 | * return the same information as in the notYetStarted state. |
| 114 | */ |
| 115 | blobs::BlobMeta expected; |
| 116 | expected.blobState = FirmwareBlobHandler::UpdateFlags::ipmi; |
| 117 | expected.size = 0; |
| 118 | |
Patrick Venture | be0fb5e | 2019-05-23 16:14:20 -0700 | [diff] [blame] | 119 | openToInProgress(staticLayoutBlobId); |
Patrick Venture | 41205cc | 2019-05-23 11:43:43 -0700 | [diff] [blame] | 120 | |
| 121 | std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId}; |
| 122 | for (const auto& blob : testBlobs) |
| 123 | { |
| 124 | blobs::BlobMeta meta = {}; |
| 125 | EXPECT_TRUE(handler->stat(blob, &meta)); |
| 126 | EXPECT_EQ(expected, meta); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /* TODO: stat(verifyblobid) only should exist once both /image and /hash have |
| 131 | * closed, so add this test when this is the case. NOTE: /image or /tarball |
| 132 | * should have the same effect. |
| 133 | */ |
| 134 | |
| 135 | /* |
| 136 | * stat(session) |
Patrick Venture | efc366e | 2019-05-23 12:00:21 -0700 | [diff] [blame] | 137 | */ |
| 138 | TEST_F(FirmwareHandlerUploadInProgressTest, |
| 139 | CallingStatOnActiveImageOrHashSessionReturnsDetails) |
| 140 | { |
| 141 | /* This test will verify that the underlying image handler is called with |
| 142 | * this stat, in addition to the normal information. |
| 143 | */ |
Patrick Venture | be0fb5e | 2019-05-23 16:14:20 -0700 | [diff] [blame] | 144 | openToInProgress(staticLayoutBlobId); |
Patrick Venture | efc366e | 2019-05-23 12:00:21 -0700 | [diff] [blame] | 145 | |
| 146 | EXPECT_CALL(imageMock, getSize()).WillOnce(Return(32)); |
| 147 | |
| 148 | blobs::BlobMeta meta, expectedMeta = {}; |
| 149 | expectedMeta.size = 32; |
| 150 | expectedMeta.blobState = |
| 151 | blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi; |
| 152 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 153 | EXPECT_EQ(expectedMeta, meta); |
| 154 | } |
| 155 | |
| 156 | /* |
Patrick Venture | 5788dbb | 2019-05-23 12:25:42 -0700 | [diff] [blame] | 157 | * open(blob) - While any blob is open, all other fail. |
| 158 | */ |
| 159 | TEST_F(FirmwareHandlerUploadInProgressTest, OpeningHashFileWhileImageOpenFails) |
| 160 | { |
| 161 | /* To be in this state, something must be open (and specifically either an |
| 162 | * active image (or tarball) or the hash file. Also verifies you can't just |
| 163 | * re-open the currently open file. |
| 164 | */ |
Patrick Venture | be0fb5e | 2019-05-23 16:14:20 -0700 | [diff] [blame] | 165 | openToInProgress(staticLayoutBlobId); |
Patrick Venture | 5788dbb | 2019-05-23 12:25:42 -0700 | [diff] [blame] | 166 | |
| 167 | std::vector<std::string> blobsToTry = { |
| 168 | hashBlobId, activeImageBlobId, activeHashBlobId, staticLayoutBlobId}; |
| 169 | for (const auto& blob : blobsToTry) |
| 170 | { |
| 171 | EXPECT_FALSE(handler->open(2, flags, blob)); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | TEST_F(FirmwareHandlerUploadInProgressTest, OpeningImageFileWhileHashOpenFails) |
| 176 | { |
Patrick Venture | be0fb5e | 2019-05-23 16:14:20 -0700 | [diff] [blame] | 177 | openToInProgress(hashBlobId); |
Patrick Venture | 5788dbb | 2019-05-23 12:25:42 -0700 | [diff] [blame] | 178 | |
| 179 | std::vector<std::string> blobsToTry = { |
| 180 | hashBlobId, activeImageBlobId, activeHashBlobId, staticLayoutBlobId}; |
| 181 | for (const auto& blob : blobsToTry) |
| 182 | { |
| 183 | EXPECT_FALSE(handler->open(2, flags, blob)); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /* |
Patrick Venture | 79b4474 | 2019-05-23 12:36:11 -0700 | [diff] [blame] | 188 | * close(session) - closing the hash or image will trigger a state transition to |
| 189 | * verificationPending. |
| 190 | * TODO: This state transition should add the verifyBlobId. This will test that |
| 191 | * it's there, but this test doesn't verify that it only just now appeared. |
| 192 | * |
| 193 | * NOTE: Re-opening /flash/image will transition back to uploadInProgress, but |
| 194 | * that is verified in the verificationPending::open tests. |
| 195 | */ |
| 196 | TEST_F(FirmwareHandlerUploadInProgressTest, |
| 197 | ClosingImageFileTransitionsToVerificationPending) |
| 198 | { |
Patrick Venture | 79b4474 | 2019-05-23 12:36:11 -0700 | [diff] [blame] | 199 | auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get()); |
| 200 | |
| 201 | /* TODO: uncomment this when verify is properly added. */ |
| 202 | // EXPECT_FALSE(handler->canHandleBlob(verifyBlobId)); |
| 203 | |
| 204 | EXPECT_CALL(imageMock, open(staticLayoutBlobId)).WillOnce(Return(true)); |
| 205 | |
Patrick Venture | be0fb5e | 2019-05-23 16:14:20 -0700 | [diff] [blame] | 206 | EXPECT_TRUE(handler->open(session, flags, staticLayoutBlobId)); |
Patrick Venture | 79b4474 | 2019-05-23 12:36:11 -0700 | [diff] [blame] | 207 | EXPECT_EQ(FirmwareBlobHandler::UpdateState::uploadInProgress, |
| 208 | realHandler->getCurrentState()); |
| 209 | |
| 210 | handler->close(1); |
| 211 | EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationPending, |
| 212 | realHandler->getCurrentState()); |
| 213 | |
| 214 | EXPECT_TRUE(handler->canHandleBlob(verifyBlobId)); |
| 215 | } |
| 216 | |
| 217 | TEST_F(FirmwareHandlerUploadInProgressTest, |
| 218 | ClosingHashFileTransitionsToVerificationPending) |
| 219 | { |
Patrick Venture | 79b4474 | 2019-05-23 12:36:11 -0700 | [diff] [blame] | 220 | auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get()); |
| 221 | |
| 222 | /* TODO: uncomment this when verify is properly added. */ |
| 223 | // EXPECT_FALSE(handler->canHandleBlob(verifyBlobId)); |
| 224 | |
| 225 | EXPECT_CALL(imageMock, open(hashBlobId)).WillOnce(Return(true)); |
| 226 | |
Patrick Venture | be0fb5e | 2019-05-23 16:14:20 -0700 | [diff] [blame] | 227 | EXPECT_TRUE(handler->open(session, flags, hashBlobId)); |
Patrick Venture | 79b4474 | 2019-05-23 12:36:11 -0700 | [diff] [blame] | 228 | EXPECT_EQ(FirmwareBlobHandler::UpdateState::uploadInProgress, |
| 229 | realHandler->getCurrentState()); |
| 230 | |
| 231 | handler->close(1); |
| 232 | EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationPending, |
| 233 | realHandler->getCurrentState()); |
| 234 | |
| 235 | EXPECT_TRUE(handler->canHandleBlob(verifyBlobId)); |
| 236 | } |
| 237 | |
| 238 | /* |
Patrick Venture | 41205cc | 2019-05-23 11:43:43 -0700 | [diff] [blame] | 239 | * writemeta(session) |
Patrick Venture | 7cf4440 | 2019-05-23 13:01:47 -0700 | [diff] [blame] | 240 | */ |
Patrick Venture | 31eefd4 | 2019-05-23 20:27:41 -0700 | [diff] [blame] | 241 | TEST_F(FirmwareHandlerUploadInProgressTest, |
| 242 | WriteMetaAgainstImageReturnsFailureIfNoDataHandler) |
Patrick Venture | 7cf4440 | 2019-05-23 13:01:47 -0700 | [diff] [blame] | 243 | { |
| 244 | /* Calling write/read/writeMeta are uninteresting against the open blob in |
| 245 | * this case because the blob will just pass the call along. Whereas |
| 246 | * calling against the verify or update blob may be more interesting. |
| 247 | */ |
Patrick Venture | be0fb5e | 2019-05-23 16:14:20 -0700 | [diff] [blame] | 248 | openToInProgress(staticLayoutBlobId); |
Patrick Venture | 7cf4440 | 2019-05-23 13:01:47 -0700 | [diff] [blame] | 249 | |
Patrick Venture | 31eefd4 | 2019-05-23 20:27:41 -0700 | [diff] [blame] | 250 | /* TODO: Consider adding a test that has a data handler, but that test |
| 251 | * already exists under the general writeMeta test suite. |
| 252 | */ |
Patrick Venture | 7cf4440 | 2019-05-23 13:01:47 -0700 | [diff] [blame] | 253 | /* Note: with IPMI as the transport there's no data handler, so this should |
| 254 | * fail nicely. */ |
| 255 | std::vector<std::uint8_t> bytes = {0x01, 0x02}; |
Patrick Venture | be0fb5e | 2019-05-23 16:14:20 -0700 | [diff] [blame] | 256 | EXPECT_FALSE(handler->writeMeta(session, 0, bytes)); |
Patrick Venture | 7cf4440 | 2019-05-23 13:01:47 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* |
Patrick Venture | 41205cc | 2019-05-23 11:43:43 -0700 | [diff] [blame] | 260 | * write(session) |
Patrick Venture | 615123a | 2019-05-23 17:20:07 -0700 | [diff] [blame] | 261 | */ |
| 262 | TEST_F(FirmwareHandlerUploadInProgressTest, WriteToImageReturnsSuccess) |
| 263 | { |
| 264 | openToInProgress(staticLayoutBlobId); |
| 265 | std::vector<std::uint8_t> bytes = {0x01, 0x02}; |
| 266 | EXPECT_CALL(imageMock, write(0, ContainerEq(bytes))).WillOnce(Return(true)); |
| 267 | EXPECT_TRUE(handler->write(session, 0, bytes)); |
| 268 | } |
| 269 | |
| 270 | TEST_F(FirmwareHandlerUploadInProgressTest, WriteToHashReturnsSuccess) |
| 271 | { |
| 272 | openToInProgress(hashBlobId); |
| 273 | std::vector<std::uint8_t> bytes = {0x01, 0x02}; |
| 274 | EXPECT_CALL(imageMock, write(0, ContainerEq(bytes))).WillOnce(Return(true)); |
| 275 | EXPECT_TRUE(handler->write(session, 0, bytes)); |
| 276 | } |
| 277 | |
| 278 | /* |
Patrick Venture | 41205cc | 2019-05-23 11:43:43 -0700 | [diff] [blame] | 279 | * read(session) |
Patrick Venture | 615123a | 2019-05-23 17:20:07 -0700 | [diff] [blame] | 280 | */ |
Patrick Venture | 8326d07 | 2019-05-23 17:45:42 -0700 | [diff] [blame] | 281 | TEST_F(FirmwareHandlerUploadInProgressTest, ReadImageFileReturnsFailure) |
| 282 | { |
| 283 | /* Read is not supported. */ |
| 284 | openToInProgress(staticLayoutBlobId); |
| 285 | EXPECT_THAT(handler->read(session, 0, 32), IsEmpty()); |
| 286 | } |
Patrick Venture | 615123a | 2019-05-23 17:20:07 -0700 | [diff] [blame] | 287 | |
| 288 | /* |
Patrick Venture | be0fb5e | 2019-05-23 16:14:20 -0700 | [diff] [blame] | 289 | * commit(session) |
Patrick Venture | 41205cc | 2019-05-23 11:43:43 -0700 | [diff] [blame] | 290 | */ |
Patrick Venture | 1fca190 | 2019-05-23 17:54:18 -0700 | [diff] [blame] | 291 | TEST_F(FirmwareHandlerUploadInProgressTest, |
| 292 | CommitAgainstImageFileReturnsFailure) |
| 293 | { |
| 294 | /* Commit is only valid against specific blobs. */ |
| 295 | openToInProgress(staticLayoutBlobId); |
| 296 | EXPECT_FALSE(handler->commit(session, {})); |
| 297 | } |
| 298 | |
| 299 | TEST_F(FirmwareHandlerUploadInProgressTest, CommitAgainstHashFileReturnsFailure) |
| 300 | { |
| 301 | openToInProgress(hashBlobId); |
| 302 | EXPECT_FALSE(handler->commit(session, {})); |
| 303 | } |
Patrick Venture | 41205cc | 2019-05-23 11:43:43 -0700 | [diff] [blame] | 304 | |
Patrick Venture | ebcc522 | 2019-05-23 10:36:40 -0700 | [diff] [blame] | 305 | } // namespace |
| 306 | } // namespace ipmi_flash |