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