Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | 27ac582 | 2019-05-20 17:39:31 -0700 | [diff] [blame] | 3 | #include "bmc_update_mock.hpp" |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 4 | #include "data_mock.hpp" |
| 5 | #include "firmware_handler.hpp" |
| 6 | #include "image_mock.hpp" |
| 7 | #include "verification_mock.hpp" |
| 8 | |
| 9 | #include <memory> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include <gmock/gmock.h> |
| 13 | #include <gtest/gtest.h> |
| 14 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 15 | namespace ipmi_flash |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 16 | { |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 17 | namespace |
| 18 | { |
| 19 | |
| 20 | using ::testing::Return; |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 21 | |
Patrick Venture | 8a4f2aa | 2019-05-23 08:40:21 -0700 | [diff] [blame] | 22 | class IpmiOnlyFirmwareStaticTest : public ::testing::Test |
| 23 | { |
| 24 | protected: |
Patrick Venture | 8a4f2aa | 2019-05-23 08:40:21 -0700 | [diff] [blame] | 25 | void SetUp() override |
| 26 | { |
| 27 | blobs = { |
| 28 | {hashBlobId, &imageMock}, |
| 29 | {staticLayoutBlobId, &imageMock}, |
| 30 | }; |
Patrick Venture | 19044e1 | 2019-05-23 19:30:28 -0700 | [diff] [blame] | 31 | |
| 32 | std::unique_ptr<VerificationInterface> verifyMock = |
| 33 | std::make_unique<VerificationMock>(); |
| 34 | verifyMockPtr = reinterpret_cast<VerificationMock*>(verifyMock.get()); |
| 35 | |
Patrick Venture | 1a406fe | 2019-05-31 07:29:56 -0700 | [diff] [blame] | 36 | std::unique_ptr<UpdateInterface> updateMock = |
| 37 | std::make_unique<UpdateMock>(); |
| 38 | updateMockPtr = reinterpret_cast<UpdateMock*>(updateMock.get()); |
| 39 | |
Patrick Venture | 8a4f2aa | 2019-05-23 08:40:21 -0700 | [diff] [blame] | 40 | handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | 1a406fe | 2019-05-31 07:29:56 -0700 | [diff] [blame] | 41 | blobs, data, std::move(verifyMock), std::move(updateMock)); |
Patrick Venture | 8a4f2aa | 2019-05-23 08:40:21 -0700 | [diff] [blame] | 42 | } |
Patrick Venture | 19044e1 | 2019-05-23 19:30:28 -0700 | [diff] [blame] | 43 | |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 44 | void expectedState(FirmwareBlobHandler::UpdateState state) |
| 45 | { |
| 46 | auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get()); |
| 47 | EXPECT_EQ(state, realHandler->getCurrentState()); |
| 48 | } |
| 49 | |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 50 | void openToInProgress(const std::string& blobId) |
| 51 | { |
| 52 | EXPECT_CALL(imageMock, open(blobId)).WillOnce(Return(true)); |
| 53 | EXPECT_TRUE(handler->open(session, flags, blobId)); |
| 54 | expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress); |
| 55 | } |
| 56 | |
| 57 | void getToVerificationPending(const std::string& blobId) |
| 58 | { |
| 59 | openToInProgress(blobId); |
| 60 | |
| 61 | EXPECT_CALL(imageMock, close()).WillRepeatedly(Return()); |
| 62 | handler->close(session); |
| 63 | expectedState(FirmwareBlobHandler::UpdateState::verificationPending); |
| 64 | } |
| 65 | |
| 66 | void getToVerificationStarted(const std::string& blobId) |
| 67 | { |
| 68 | getToVerificationPending(blobId); |
| 69 | |
| 70 | EXPECT_TRUE(handler->open(session, flags, verifyBlobId)); |
| 71 | EXPECT_CALL(*verifyMockPtr, triggerVerification()) |
| 72 | .WillOnce(Return(true)); |
| 73 | |
| 74 | EXPECT_TRUE(handler->commit(session, {})); |
| 75 | expectedState(FirmwareBlobHandler::UpdateState::verificationStarted); |
| 76 | } |
| 77 | |
| 78 | void getToVerificationCompleted(VerifyCheckResponses checkResponse) |
| 79 | { |
| 80 | getToVerificationStarted(staticLayoutBlobId); |
| 81 | |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 82 | EXPECT_CALL(*verifyMockPtr, status()).WillOnce(Return(checkResponse)); |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 83 | blobs::BlobMeta meta; |
| 84 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 85 | expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted); |
| 86 | } |
| 87 | |
| 88 | void getToUpdatePending() |
| 89 | { |
| 90 | getToVerificationCompleted(VerifyCheckResponses::success); |
| 91 | |
| 92 | handler->close(session); |
| 93 | expectedState(FirmwareBlobHandler::UpdateState::updatePending); |
| 94 | } |
| 95 | |
| 96 | void getToUpdateStarted() |
| 97 | { |
| 98 | getToUpdatePending(); |
| 99 | EXPECT_TRUE(handler->open(session, flags, updateBlobId)); |
| 100 | |
| 101 | EXPECT_CALL(*updateMockPtr, triggerUpdate()).WillOnce(Return(true)); |
| 102 | EXPECT_TRUE(handler->commit(session, {})); |
| 103 | expectedState(FirmwareBlobHandler::UpdateState::updateStarted); |
| 104 | } |
| 105 | |
Patrick Venture | ab1e962 | 2019-06-03 10:45:06 -0700 | [diff] [blame^] | 106 | void getToUpdateCompleted(UpdateStatus result) |
| 107 | { |
| 108 | getToUpdateStarted(); |
| 109 | EXPECT_CALL(*updateMockPtr, status()).WillOnce(Return(result)); |
| 110 | |
| 111 | blobs::BlobMeta meta; |
| 112 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 113 | expectedState(FirmwareBlobHandler::UpdateState::updateCompleted); |
| 114 | } |
| 115 | |
Patrick Venture | 19044e1 | 2019-05-23 19:30:28 -0700 | [diff] [blame] | 116 | ImageHandlerMock imageMock; |
| 117 | std::vector<HandlerPack> blobs; |
| 118 | std::vector<DataHandlerPack> data = { |
| 119 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}}; |
| 120 | std::unique_ptr<blobs::GenericBlobInterface> handler; |
| 121 | VerificationMock* verifyMockPtr; |
Patrick Venture | 1a406fe | 2019-05-31 07:29:56 -0700 | [diff] [blame] | 122 | UpdateMock* updateMockPtr; |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 123 | |
| 124 | std::uint16_t session = 1; |
| 125 | std::uint16_t flags = |
| 126 | blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi; |
Patrick Venture | 8a4f2aa | 2019-05-23 08:40:21 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 129 | class IpmiOnlyFirmwareTest : public ::testing::Test |
| 130 | { |
| 131 | protected: |
| 132 | ImageHandlerMock imageMock; |
| 133 | std::vector<HandlerPack> blobs; |
| 134 | std::vector<DataHandlerPack> data = { |
| 135 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}}; |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 136 | std::unique_ptr<blobs::GenericBlobInterface> handler; |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 137 | |
| 138 | void SetUp() override |
| 139 | { |
| 140 | blobs = { |
| 141 | {hashBlobId, &imageMock}, |
| 142 | {"asdf", &imageMock}, |
| 143 | }; |
| 144 | handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | 27ac582 | 2019-05-20 17:39:31 -0700 | [diff] [blame] | 145 | blobs, data, CreateVerifyMock(), CreateUpdateMock()); |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 146 | } |
| 147 | }; |
| 148 | |
| 149 | class FakeLpcFirmwareTest : public ::testing::Test |
| 150 | { |
| 151 | protected: |
| 152 | DataHandlerMock dataMock; |
| 153 | ImageHandlerMock imageMock; |
| 154 | std::vector<HandlerPack> blobs; |
| 155 | std::vector<DataHandlerPack> data; |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 156 | std::unique_ptr<blobs::GenericBlobInterface> handler; |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 157 | |
| 158 | void SetUp() override |
| 159 | { |
| 160 | blobs = { |
| 161 | {hashBlobId, &imageMock}, |
| 162 | {"asdf", &imageMock}, |
| 163 | }; |
| 164 | data = { |
| 165 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
| 166 | {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock}, |
| 167 | }; |
| 168 | handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | 27ac582 | 2019-05-20 17:39:31 -0700 | [diff] [blame] | 169 | blobs, data, CreateVerifyMock(), CreateUpdateMock()); |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 170 | } |
| 171 | }; |
| 172 | |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 173 | } // namespace |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 174 | } // namespace ipmi_flash |