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