Patrick Venture | 237e2c6 | 2019-05-23 20:35:33 -0700 | [diff] [blame^] | 1 | /* The goal of these tests is to verify the behavior of all blob commands given |
| 2 | * the current state is verificationStarted. This state is achieved as a out of |
| 3 | * verificationPending. |
| 4 | */ |
| 5 | #include "firmware_handler.hpp" |
| 6 | #include "firmware_unittest.hpp" |
| 7 | #include "status.hpp" |
| 8 | #include "util.hpp" |
| 9 | |
| 10 | #include <cstdint> |
| 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include <gtest/gtest.h> |
| 15 | |
| 16 | namespace ipmi_flash |
| 17 | { |
| 18 | namespace |
| 19 | { |
| 20 | |
| 21 | using ::testing::Return; |
| 22 | |
| 23 | /* |
| 24 | * There are the following calls (parameters may vary): |
| 25 | * canHandleBlob(blob) |
| 26 | * getBlobIds |
| 27 | * deleteBlob(blob) |
| 28 | * stat(blob) |
| 29 | * stat(session) |
| 30 | * open(blob) |
| 31 | * close(session) |
| 32 | * writemeta(session) |
| 33 | * write(session) |
| 34 | * read(session) |
| 35 | * commit(session) |
| 36 | * |
| 37 | * Testing canHandleBlob is uninteresting in this state. Getting the BlobIDs |
| 38 | * will inform what canHandleBlob will return. |
| 39 | */ |
| 40 | |
| 41 | class FirmwareHandlerVerificationStartedTest : public IpmiOnlyFirmwareStaticTest |
| 42 | { |
| 43 | protected: |
| 44 | void getToVerificationStarted(const std::string& blobId) |
| 45 | { |
| 46 | auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get()); |
| 47 | EXPECT_CALL(imageMock, open(blobId)).WillOnce(Return(true)); |
| 48 | EXPECT_TRUE(handler->open(session, flags, blobId)); |
| 49 | EXPECT_EQ(FirmwareBlobHandler::UpdateState::uploadInProgress, |
| 50 | realHandler->getCurrentState()); |
| 51 | EXPECT_CALL(imageMock, close()).WillRepeatedly(Return()); |
| 52 | handler->close(session); |
| 53 | EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationPending, |
| 54 | realHandler->getCurrentState()); |
| 55 | |
| 56 | EXPECT_TRUE(handler->open(session, flags, verifyBlobId)); |
| 57 | EXPECT_CALL(*verifyMockPtr, triggerVerification()) |
| 58 | .WillOnce(Return(true)); |
| 59 | |
| 60 | EXPECT_TRUE(handler->commit(session, {})); |
| 61 | EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationStarted, |
| 62 | realHandler->getCurrentState()); |
| 63 | } |
| 64 | |
| 65 | std::uint16_t session = 1; |
| 66 | std::uint16_t flags = |
| 67 | blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi; |
| 68 | }; |
| 69 | |
| 70 | /* |
| 71 | * stat(session) |
| 72 | */ |
| 73 | TEST_F(FirmwareHandlerVerificationStartedTest, |
| 74 | StatOnVerifyBlobIdAfterCommitChecksStateAndReturnsRunning) |
| 75 | { |
| 76 | getToVerificationStarted(staticLayoutBlobId); |
| 77 | EXPECT_CALL(*verifyMockPtr, checkVerificationState()) |
| 78 | .WillOnce(Return(VerifyCheckResponses::running)); |
| 79 | |
| 80 | blobs::BlobMeta meta, expectedMeta = {}; |
| 81 | expectedMeta.size = 0; |
| 82 | expectedMeta.blobState = flags | blobs::StateFlags::committing; |
| 83 | expectedMeta.metadata.push_back( |
| 84 | static_cast<std::uint8_t>(VerifyCheckResponses::running)); |
| 85 | |
| 86 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 87 | EXPECT_EQ(expectedMeta, meta); |
| 88 | } |
| 89 | |
| 90 | TEST_F(FirmwareHandlerVerificationStartedTest, |
| 91 | StatOnVerifyBlobIdAfterCommitCheckStateAndReturnsFailed) |
| 92 | { |
| 93 | /* If the returned state from the verification handler is failed, it sets |
| 94 | * commit_error and transitions to verificationCompleted. |
| 95 | */ |
| 96 | getToVerificationStarted(staticLayoutBlobId); |
| 97 | EXPECT_CALL(*verifyMockPtr, checkVerificationState()) |
| 98 | .WillOnce(Return(VerifyCheckResponses::failed)); |
| 99 | |
| 100 | blobs::BlobMeta meta, expectedMeta = {}; |
| 101 | expectedMeta.size = 0; |
| 102 | expectedMeta.blobState = flags | blobs::StateFlags::commit_error; |
| 103 | expectedMeta.metadata.push_back( |
| 104 | static_cast<std::uint8_t>(VerifyCheckResponses::failed)); |
| 105 | |
| 106 | auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get()); |
| 107 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 108 | EXPECT_EQ(expectedMeta, meta); |
| 109 | |
| 110 | EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationCompleted, |
| 111 | realHandler->getCurrentState()); |
| 112 | } |
| 113 | |
| 114 | TEST_F(FirmwareHandlerVerificationStartedTest, |
| 115 | StatOnVerifyBlobIdAfterCommitCheckStateAndReturnsSuccess) |
| 116 | { |
| 117 | /* If the returned state from the verification handler is success, it sets |
| 118 | * committed and transitions to verificationCompleted. |
| 119 | */ |
| 120 | getToVerificationStarted(staticLayoutBlobId); |
| 121 | EXPECT_CALL(*verifyMockPtr, checkVerificationState()) |
| 122 | .WillOnce(Return(VerifyCheckResponses::success)); |
| 123 | |
| 124 | blobs::BlobMeta meta, expectedMeta = {}; |
| 125 | expectedMeta.size = 0; |
| 126 | expectedMeta.blobState = flags | blobs::StateFlags::committed; |
| 127 | expectedMeta.metadata.push_back( |
| 128 | static_cast<std::uint8_t>(VerifyCheckResponses::success)); |
| 129 | |
| 130 | auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get()); |
| 131 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 132 | EXPECT_EQ(expectedMeta, meta); |
| 133 | |
| 134 | EXPECT_EQ(FirmwareBlobHandler::UpdateState::verificationCompleted, |
| 135 | realHandler->getCurrentState()); |
| 136 | } |
| 137 | |
| 138 | /* TODO: Once verificationCompleted is the state, canHandleBlob should accept |
| 139 | * updateBlobId. |
| 140 | */ |
| 141 | |
| 142 | /* TODO: |
| 143 | * canHandleBlob(blob) |
| 144 | * getBlobIds |
| 145 | * deleteBlob(blob) |
| 146 | * stat(blob) |
| 147 | * open(blob) - there is nothing you can open, this state has an open file. |
| 148 | * close(session) |
| 149 | * writemeta(session) |
| 150 | * write(session) |
| 151 | * read(session) |
| 152 | * commit(session) |
| 153 | */ |
| 154 | |
| 155 | } // namespace |
| 156 | } // namespace ipmi_flash |