Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -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 verificationPending. This state is achieved as a |
| 4 | * transition out of uploadInProgress. |
| 5 | */ |
| 6 | #include "firmware_handler.hpp" |
| 7 | #include "firmware_unittest.hpp" |
Patrick Venture | 01a3327 | 2019-05-23 19:48:22 -0700 | [diff] [blame] | 8 | #include "status.hpp" |
Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -0700 | [diff] [blame] | 9 | #include "util.hpp" |
| 10 | |
| 11 | #include <cstdint> |
| 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
| 15 | #include <gtest/gtest.h> |
| 16 | |
| 17 | namespace ipmi_flash |
| 18 | { |
| 19 | namespace |
| 20 | { |
| 21 | |
| 22 | using ::testing::Return; |
| 23 | |
| 24 | /* |
| 25 | * There are the following calls (parameters may vary): |
| 26 | * canHandleBlob(blob) |
| 27 | * getBlobIds |
| 28 | * deleteBlob(blob) |
| 29 | * stat(blob) |
| 30 | * stat(session) |
| 31 | * open(blob) |
| 32 | * close(session) |
| 33 | * writemeta(session) |
| 34 | * write(session) |
| 35 | * read(session) |
| 36 | * commit(session) |
| 37 | * |
| 38 | * Testing canHandleBlob is uninteresting in this state. Getting the BlobIDs |
| 39 | * will inform what canHandleBlob will return. |
| 40 | */ |
| 41 | |
| 42 | class FirmwareHandlerVerificationPendingTest : public IpmiOnlyFirmwareStaticTest |
| 43 | { |
| 44 | protected: |
| 45 | void getToVerificationPending(const std::string& blobId) |
| 46 | { |
| 47 | auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get()); |
| 48 | EXPECT_CALL(imageMock, open(blobId)).WillOnce(Return(true)); |
| 49 | EXPECT_TRUE(handler->open(session, flags, blobId)); |
| 50 | EXPECT_EQ(FirmwareBlobHandler::UpdateState::uploadInProgress, |
| 51 | realHandler->getCurrentState()); |
| 52 | EXPECT_CALL(imageMock, close()).WillRepeatedly(Return()); |
| 53 | handler->close(session); |
| 54 | EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationPending, |
| 55 | realHandler->getCurrentState()); |
| 56 | } |
| 57 | |
| 58 | std::uint16_t session = 1; |
| 59 | std::uint16_t flags = |
| 60 | blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi; |
| 61 | }; |
| 62 | |
| 63 | /* |
| 64 | * getBlobIds |
Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -0700 | [diff] [blame] | 65 | */ |
Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -0700 | [diff] [blame] | 66 | TEST_F(FirmwareHandlerVerificationPendingTest, VerifyBlobIdAvailableInState) |
| 67 | { |
| 68 | /* Only in the verificationPending state (and later), should the |
| 69 | * verifyBlobId be present. */ |
| 70 | |
| 71 | /* TODO: Add this test in when the change is made. */ |
| 72 | // EXPECT_FALSE(handler->canHandleBlob(verifyBlobId)); |
| 73 | getToVerificationPending(staticLayoutBlobId); |
| 74 | EXPECT_TRUE(handler->canHandleBlob(verifyBlobId)); |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 75 | EXPECT_TRUE(handler->canHandleBlob(activeImageBlobId)); |
Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 78 | /* |
| 79 | * delete(blob) TODO: Implement this. |
| 80 | */ |
| 81 | |
| 82 | /* |
| 83 | * stat(blob) |
| 84 | */ |
| 85 | TEST_F(FirmwareHandlerVerificationPendingTest, StatOnActiveImageReturnsFailure) |
| 86 | { |
| 87 | getToVerificationPending(staticLayoutBlobId); |
| 88 | |
| 89 | blobs::BlobMeta meta; |
| 90 | EXPECT_FALSE(handler->stat(activeImageBlobId, &meta)); |
| 91 | } |
| 92 | |
| 93 | TEST_F(FirmwareHandlerVerificationPendingTest, StatOnActiveHashReturnsFailure) |
| 94 | { |
| 95 | getToVerificationPending(hashBlobId); |
| 96 | |
| 97 | blobs::BlobMeta meta; |
| 98 | EXPECT_FALSE(handler->stat(activeHashBlobId, &meta)); |
| 99 | } |
| 100 | |
| 101 | TEST_F(FirmwareHandlerVerificationPendingTest, |
| 102 | StatOnVerificationBlobReturnsFailure) |
| 103 | { |
| 104 | getToVerificationPending(hashBlobId); |
| 105 | |
| 106 | blobs::BlobMeta meta; |
| 107 | EXPECT_FALSE(handler->stat(verifyBlobId, &meta)); |
| 108 | } |
| 109 | |
| 110 | TEST_F(FirmwareHandlerVerificationPendingTest, StatOnNormalBlobsReturnsSuccess) |
| 111 | { |
| 112 | getToVerificationPending(staticLayoutBlobId); |
| 113 | |
| 114 | blobs::BlobMeta expected; |
| 115 | expected.blobState = FirmwareBlobHandler::UpdateFlags::ipmi; |
| 116 | expected.size = 0; |
| 117 | |
| 118 | std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId}; |
| 119 | for (const auto& blob : testBlobs) |
| 120 | { |
| 121 | blobs::BlobMeta meta = {}; |
| 122 | EXPECT_TRUE(handler->stat(blob, &meta)); |
| 123 | EXPECT_EQ(expected, meta); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /* |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 128 | * open(blob) |
| 129 | */ |
Patrick Venture | da8fcd1 | 2019-05-23 18:53:50 -0700 | [diff] [blame] | 130 | TEST_F(FirmwareHandlerVerificationPendingTest, OpenVerifyBlobSucceeds) |
| 131 | { |
| 132 | getToVerificationPending(staticLayoutBlobId); |
| 133 | |
| 134 | /* the session is safe because it was already closed to get to this state. |
| 135 | */ |
| 136 | EXPECT_TRUE(handler->open(session, flags, verifyBlobId)); |
| 137 | } |
| 138 | |
| 139 | TEST_F(FirmwareHandlerVerificationPendingTest, OpenActiveImageBlobFails) |
| 140 | { |
| 141 | /* Try opening the active blob Id. This test is equivalent to trying to |
| 142 | * open the active hash blob id, in that neither are ever allowed. |
| 143 | */ |
| 144 | getToVerificationPending(staticLayoutBlobId); |
| 145 | EXPECT_FALSE(handler->open(session, flags, activeImageBlobId)); |
| 146 | } |
| 147 | |
| 148 | TEST_F(FirmwareHandlerVerificationPendingTest, |
| 149 | OpenImageBlobTransitionsToUploadInProgress) |
| 150 | { |
| 151 | getToVerificationPending(staticLayoutBlobId); |
| 152 | EXPECT_CALL(imageMock, open(staticLayoutBlobId)).WillOnce(Return(true)); |
| 153 | EXPECT_TRUE(handler->open(session, flags, staticLayoutBlobId)); |
| 154 | |
| 155 | auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get()); |
| 156 | EXPECT_EQ(FirmwareBlobHandler::UpdateState::uploadInProgress, |
| 157 | realHandler->getCurrentState()); |
| 158 | } |
| 159 | |
| 160 | /* |
Patrick Venture | 1e389c9 | 2019-05-23 19:15:05 -0700 | [diff] [blame] | 161 | * close(session) |
| 162 | */ |
| 163 | TEST_F(FirmwareHandlerVerificationPendingTest, |
| 164 | ClosingVerifyBlobDoesNotChangeState) |
| 165 | { |
| 166 | getToVerificationPending(staticLayoutBlobId); |
| 167 | EXPECT_TRUE(handler->open(session, flags, verifyBlobId)); |
| 168 | |
| 169 | auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get()); |
| 170 | EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationPending, |
| 171 | realHandler->getCurrentState()); |
| 172 | |
| 173 | handler->close(session); |
| 174 | |
| 175 | EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationPending, |
| 176 | realHandler->getCurrentState()); |
| 177 | } |
| 178 | |
| 179 | /* |
Patrick Venture | 19044e1 | 2019-05-23 19:30:28 -0700 | [diff] [blame] | 180 | * commit(session) |
| 181 | */ |
| 182 | TEST_F(FirmwareHandlerVerificationPendingTest, |
| 183 | CommitOnVerifyBlobTriggersVerificationAndStateTransition) |
| 184 | { |
| 185 | getToVerificationPending(staticLayoutBlobId); |
| 186 | EXPECT_TRUE(handler->open(session, flags, verifyBlobId)); |
| 187 | EXPECT_CALL(*verifyMockPtr, triggerVerification()).WillOnce(Return(true)); |
| 188 | |
| 189 | EXPECT_TRUE(handler->commit(session, {})); |
| 190 | |
| 191 | auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get()); |
| 192 | EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationStarted, |
| 193 | realHandler->getCurrentState()); |
| 194 | } |
| 195 | |
| 196 | /* |
Patrick Venture | da8fcd1 | 2019-05-23 18:53:50 -0700 | [diff] [blame] | 197 | * stat(session) - in this state, you can only open(verifyBlobId) without |
| 198 | * changing state. |
| 199 | */ |
Patrick Venture | 01a3327 | 2019-05-23 19:48:22 -0700 | [diff] [blame] | 200 | TEST_F(FirmwareHandlerVerificationPendingTest, StatOnVerifyBlobIdReturnsState) |
| 201 | { |
| 202 | /* If this is called before commit(), it's still verificationPending, so it |
| 203 | * just returns the state is other |
| 204 | */ |
| 205 | getToVerificationPending(staticLayoutBlobId); |
| 206 | EXPECT_TRUE(handler->open(session, flags, verifyBlobId)); |
| 207 | EXPECT_CALL(*verifyMockPtr, triggerVerification()).Times(0); |
| 208 | EXPECT_CALL(*verifyMockPtr, checkVerificationState()).Times(0); |
| 209 | |
| 210 | blobs::BlobMeta meta, expectedMeta = {}; |
| 211 | expectedMeta.size = 0; |
| 212 | expectedMeta.blobState = flags; |
| 213 | expectedMeta.metadata.push_back( |
| 214 | static_cast<std::uint8_t>(VerifyCheckResponses::other)); |
| 215 | |
| 216 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 217 | EXPECT_EQ(expectedMeta, meta); |
| 218 | } |
| 219 | |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 220 | /* |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 221 | * writemeta(session) |
| 222 | */ |
Patrick Venture | b611a08 | 2019-05-23 20:27:28 -0700 | [diff] [blame] | 223 | TEST_F(FirmwareHandlerVerificationPendingTest, WriteMetaAgainstVerifyFails) |
| 224 | { |
| 225 | /* The verifyBlobId has no data handler, which means write meta fails. */ |
| 226 | getToVerificationPending(staticLayoutBlobId); |
| 227 | |
| 228 | EXPECT_TRUE(handler->open(session, flags, verifyBlobId)); |
| 229 | |
| 230 | std::vector<std::uint8_t> bytes = {0x01, 0x02}; |
| 231 | EXPECT_FALSE(handler->writeMeta(session, 0, bytes)); |
| 232 | } |
| 233 | |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 234 | /* |
| 235 | * write(session) |
| 236 | */ |
| 237 | /* |
| 238 | * read(session) |
| 239 | */ |
Patrick Venture | b386b86 | 2019-05-23 18:42:54 -0700 | [diff] [blame] | 240 | |
Patrick Venture | 61d2ed4 | 2019-05-23 18:16:31 -0700 | [diff] [blame] | 241 | } // namespace |
| 242 | } // namespace ipmi_flash |