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 | 19044e1 | 2019-05-23 19:30:28 -0700 | [diff] [blame] | 106 | ImageHandlerMock imageMock; |
| 107 | std::vector<HandlerPack> blobs; |
| 108 | std::vector<DataHandlerPack> data = { |
| 109 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}}; |
| 110 | std::unique_ptr<blobs::GenericBlobInterface> handler; |
| 111 | VerificationMock* verifyMockPtr; |
Patrick Venture | 1a406fe | 2019-05-31 07:29:56 -0700 | [diff] [blame] | 112 | UpdateMock* updateMockPtr; |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 113 | |
| 114 | std::uint16_t session = 1; |
| 115 | std::uint16_t flags = |
| 116 | blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi; |
Patrick Venture | 8a4f2aa | 2019-05-23 08:40:21 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 119 | class IpmiOnlyFirmwareTest : public ::testing::Test |
| 120 | { |
| 121 | protected: |
| 122 | ImageHandlerMock imageMock; |
| 123 | std::vector<HandlerPack> blobs; |
| 124 | std::vector<DataHandlerPack> data = { |
| 125 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}}; |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 126 | std::unique_ptr<blobs::GenericBlobInterface> handler; |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 127 | |
| 128 | void SetUp() override |
| 129 | { |
| 130 | blobs = { |
| 131 | {hashBlobId, &imageMock}, |
| 132 | {"asdf", &imageMock}, |
| 133 | }; |
| 134 | handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | 27ac582 | 2019-05-20 17:39:31 -0700 | [diff] [blame] | 135 | blobs, data, CreateVerifyMock(), CreateUpdateMock()); |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 136 | } |
| 137 | }; |
| 138 | |
| 139 | class FakeLpcFirmwareTest : public ::testing::Test |
| 140 | { |
| 141 | protected: |
| 142 | DataHandlerMock dataMock; |
| 143 | ImageHandlerMock imageMock; |
| 144 | std::vector<HandlerPack> blobs; |
| 145 | std::vector<DataHandlerPack> data; |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 146 | std::unique_ptr<blobs::GenericBlobInterface> handler; |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 147 | |
| 148 | void SetUp() override |
| 149 | { |
| 150 | blobs = { |
| 151 | {hashBlobId, &imageMock}, |
| 152 | {"asdf", &imageMock}, |
| 153 | }; |
| 154 | data = { |
| 155 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
| 156 | {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock}, |
| 157 | }; |
| 158 | handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | 27ac582 | 2019-05-20 17:39:31 -0700 | [diff] [blame] | 159 | blobs, data, CreateVerifyMock(), CreateUpdateMock()); |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 160 | } |
| 161 | }; |
| 162 | |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 163 | } // namespace |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 164 | } // namespace ipmi_flash |