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