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