Patrick Venture | a82f99e | 2019-05-24 15:44:35 -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 verificationCompleted. This state is achieved as a out |
| 4 | * of verificationStarted. |
| 5 | */ |
| 6 | #include "firmware_handler.hpp" |
| 7 | #include "firmware_unittest.hpp" |
| 8 | #include "status.hpp" |
| 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; |
Patrick Venture | 6d3a14c | 2019-05-29 09:24:42 -0700 | [diff] [blame^] | 23 | using ::testing::UnorderedElementsAreArray; |
Patrick Venture | a82f99e | 2019-05-24 15:44:35 -0700 | [diff] [blame] | 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) |
| 37 | * commit(session) |
| 38 | * |
| 39 | * Like the state verificationStarted, there is a file open in |
| 40 | * verificationCompleted. This state is transitioned to after a stat() command |
| 41 | * indicates a successful verification. |
| 42 | */ |
| 43 | |
| 44 | class FirmwareHandlerVerificationCompletedTest |
| 45 | : public IpmiOnlyFirmwareStaticTest |
| 46 | { |
| 47 | protected: |
Patrick Venture | 0c642fd | 2019-05-24 16:09:29 -0700 | [diff] [blame] | 48 | void getToVerificationCompleted(VerifyCheckResponses checkResponse) |
Patrick Venture | a82f99e | 2019-05-24 15:44:35 -0700 | [diff] [blame] | 49 | { |
Patrick Venture | 0c642fd | 2019-05-24 16:09:29 -0700 | [diff] [blame] | 50 | /* The hash was not sent up, as it's technically optional. Therefore, |
| 51 | * there is no active hash file. |
| 52 | */ |
Patrick Venture | a82f99e | 2019-05-24 15:44:35 -0700 | [diff] [blame] | 53 | EXPECT_CALL(imageMock, open(staticLayoutBlobId)).WillOnce(Return(true)); |
| 54 | EXPECT_TRUE(handler->open(session, flags, staticLayoutBlobId)); |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 55 | expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress); |
| 56 | |
Patrick Venture | a82f99e | 2019-05-24 15:44:35 -0700 | [diff] [blame] | 57 | EXPECT_CALL(imageMock, close()).WillRepeatedly(Return()); |
| 58 | handler->close(session); |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 59 | expectedState(FirmwareBlobHandler::UpdateState::verificationPending); |
Patrick Venture | a82f99e | 2019-05-24 15:44:35 -0700 | [diff] [blame] | 60 | |
| 61 | EXPECT_TRUE(handler->open(session, flags, verifyBlobId)); |
| 62 | EXPECT_CALL(*verifyMockPtr, triggerVerification()) |
| 63 | .WillOnce(Return(true)); |
| 64 | |
| 65 | EXPECT_TRUE(handler->commit(session, {})); |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 66 | expectedState(FirmwareBlobHandler::UpdateState::verificationStarted); |
Patrick Venture | a82f99e | 2019-05-24 15:44:35 -0700 | [diff] [blame] | 67 | |
| 68 | EXPECT_CALL(*verifyMockPtr, checkVerificationState()) |
Patrick Venture | 0c642fd | 2019-05-24 16:09:29 -0700 | [diff] [blame] | 69 | .WillOnce(Return(checkResponse)); |
Patrick Venture | a82f99e | 2019-05-24 15:44:35 -0700 | [diff] [blame] | 70 | blobs::BlobMeta meta; |
| 71 | EXPECT_TRUE(handler->stat(session, &meta)); |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 72 | expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted); |
Patrick Venture | a82f99e | 2019-05-24 15:44:35 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | std::uint16_t session = 1; |
| 76 | std::uint16_t flags = |
| 77 | blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi; |
| 78 | }; |
| 79 | |
Patrick Venture | 6d3a14c | 2019-05-29 09:24:42 -0700 | [diff] [blame^] | 80 | /* TODO: deleteBlob(blob) */ |
| 81 | |
| 82 | /* |
| 83 | * canHandleBlob |
Patrick Venture | a82f99e | 2019-05-24 15:44:35 -0700 | [diff] [blame] | 84 | */ |
Patrick Venture | 6d3a14c | 2019-05-29 09:24:42 -0700 | [diff] [blame^] | 85 | TEST_F(FirmwareHandlerVerificationCompletedTest, |
| 86 | OnVerificationCompleteSuccessUpdateBlobIdNotPresent) |
| 87 | { |
| 88 | /* the uploadBlobId is only added on close() of the verifyBlobId. This is a |
| 89 | * consistent behavior with verifyBlobId only added when closing the image |
| 90 | * or hash. |
| 91 | */ |
| 92 | getToVerificationCompleted(VerifyCheckResponses::success); |
| 93 | EXPECT_FALSE(handler->canHandleBlob(updateBlobId)); |
| 94 | } |
| 95 | |
| 96 | TEST_F(FirmwareHandlerVerificationCompletedTest, |
| 97 | OnVerificationCompleteFailureUpdateBlobIdNotPresent) |
| 98 | { |
| 99 | getToVerificationCompleted(VerifyCheckResponses::failed); |
| 100 | EXPECT_FALSE(handler->canHandleBlob(updateBlobId)); |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * getBlobIds |
| 105 | */ |
| 106 | TEST_F(FirmwareHandlerVerificationCompletedTest, GetBlobIdsReturnsExpectedList) |
| 107 | { |
| 108 | getToVerificationCompleted(VerifyCheckResponses::success); |
| 109 | std::vector<std::string> expected = {verifyBlobId, hashBlobId, |
| 110 | activeImageBlobId, staticLayoutBlobId}; |
| 111 | EXPECT_THAT(handler->getBlobIds(), UnorderedElementsAreArray(expected)); |
| 112 | } |
Patrick Venture | a82f99e | 2019-05-24 15:44:35 -0700 | [diff] [blame] | 113 | |
| 114 | /* |
| 115 | * stat(blob) |
| 116 | */ |
Patrick Venture | 0c642fd | 2019-05-24 16:09:29 -0700 | [diff] [blame] | 117 | TEST_F(FirmwareHandlerVerificationCompletedTest, |
| 118 | StatOnActiveImageReturnsFailure) |
| 119 | { |
| 120 | getToVerificationCompleted(VerifyCheckResponses::success); |
| 121 | ASSERT_TRUE(handler->canHandleBlob(activeImageBlobId)); |
| 122 | |
| 123 | blobs::BlobMeta meta; |
| 124 | EXPECT_FALSE(handler->stat(activeImageBlobId, &meta)); |
| 125 | } |
| 126 | |
Patrick Venture | fbf07ff | 2019-05-29 08:58:45 -0700 | [diff] [blame] | 127 | TEST_F(FirmwareHandlerVerificationCompletedTest, |
| 128 | VerifyActiveHashIdMissingInThisCase) |
| 129 | { |
| 130 | /* The path taken to get to this state never opened the hash blob Id, which |
| 131 | * is fine. But let's verify it behaved as intended. |
| 132 | */ |
| 133 | getToVerificationCompleted(VerifyCheckResponses::success); |
| 134 | EXPECT_FALSE(handler->canHandleBlob(activeHashBlobId)); |
| 135 | } |
| 136 | |
| 137 | /* TODO: Add sufficient warning that you can get to verificationCompleted |
| 138 | * without ever opening the image blob id (or the tarball one). |
| 139 | * |
| 140 | * Although in this case, it's expected that any verification triggered would |
| 141 | * certainly fail. So, although it's possible, it's uninteresting. |
| 142 | */ |
| 143 | |
Patrick Venture | 0c642fd | 2019-05-24 16:09:29 -0700 | [diff] [blame] | 144 | TEST_F(FirmwareHandlerVerificationCompletedTest, StatOnVerifyBlobReturnsFailure) |
| 145 | { |
| 146 | getToVerificationCompleted(VerifyCheckResponses::success); |
| 147 | ASSERT_TRUE(handler->canHandleBlob(verifyBlobId)); |
| 148 | |
| 149 | blobs::BlobMeta meta; |
| 150 | EXPECT_FALSE(handler->stat(verifyBlobId, &meta)); |
| 151 | } |
| 152 | |
| 153 | TEST_F(FirmwareHandlerVerificationCompletedTest, |
| 154 | StatOnNormalBlobsReturnsSuccess) |
| 155 | { |
| 156 | getToVerificationCompleted(VerifyCheckResponses::success); |
| 157 | |
| 158 | blobs::BlobMeta expected; |
| 159 | expected.blobState = FirmwareBlobHandler::UpdateFlags::ipmi; |
| 160 | expected.size = 0; |
| 161 | |
| 162 | std::vector<std::string> testBlobs = {staticLayoutBlobId, hashBlobId}; |
| 163 | for (const auto& blob : testBlobs) |
| 164 | { |
| 165 | ASSERT_TRUE(handler->canHandleBlob(blob)); |
| 166 | |
| 167 | blobs::BlobMeta meta = {}; |
| 168 | EXPECT_TRUE(handler->stat(blob, &meta)); |
| 169 | EXPECT_EQ(expected, meta); |
| 170 | } |
| 171 | } |
Patrick Venture | a82f99e | 2019-05-24 15:44:35 -0700 | [diff] [blame] | 172 | |
| 173 | /* |
| 174 | * stat(session) - the verify blobid is open in this state, so stat on that once |
| 175 | * completed should have no effect. |
| 176 | * |
| 177 | * open(blob) - all open should fail |
| 178 | * |
| 179 | * close(session) - close on the verify blobid: |
| 180 | * 1. if successful adds update blob id, changes state to UpdatePending |
| 181 | * 2. if unsuccessful doesn't add update blob id, changes state to? |
| 182 | */ |
| 183 | |
| 184 | /* |
| 185 | * writemeta(session) - write meta should fail. |
| 186 | * write(session) - write should fail. |
| 187 | * read(session) - read returns empty. |
| 188 | * commit(session) - ? |
| 189 | */ |
| 190 | |
| 191 | } // namespace |
| 192 | } // namespace ipmi_flash |