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