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