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