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" |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 5 | #include "flags.hpp" |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 6 | #include "image_mock.hpp" |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 7 | #include "triggerable_mock.hpp" |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 8 | |
| 9 | #include <memory> |
Patrick Venture | 9a69f73 | 2019-06-17 14:05:13 -0700 | [diff] [blame] | 10 | #include <string> |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 11 | #include <unordered_map> |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 12 | #include <vector> |
| 13 | |
| 14 | #include <gmock/gmock.h> |
| 15 | #include <gtest/gtest.h> |
| 16 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 17 | namespace ipmi_flash |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 18 | { |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 19 | namespace |
| 20 | { |
| 21 | |
| 22 | using ::testing::Return; |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 23 | |
Patrick Venture | 8a4f2aa | 2019-05-23 08:40:21 -0700 | [diff] [blame] | 24 | class IpmiOnlyFirmwareStaticTest : public ::testing::Test |
| 25 | { |
| 26 | protected: |
Patrick Venture | 8a4f2aa | 2019-05-23 08:40:21 -0700 | [diff] [blame] | 27 | void SetUp() override |
| 28 | { |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 29 | /* Unfortunately, since the FirmwareHandler object ends up owning the |
| 30 | * handlers, we can't just share handlers. |
| 31 | */ |
| 32 | std::unique_ptr<ImageHandlerInterface> image = |
| 33 | std::make_unique<ImageHandlerMock>(); |
| 34 | hashImageMock = reinterpret_cast<ImageHandlerMock*>(image.get()); |
Patrick Venture | c6ba8ff | 2020-09-23 12:42:57 -0700 | [diff] [blame] | 35 | blobs.emplace_back(hashBlobId, std::move(image)); |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 36 | |
| 37 | image = std::make_unique<ImageHandlerMock>(); |
| 38 | imageMock2 = reinterpret_cast<ImageHandlerMock*>(image.get()); |
Patrick Venture | c6ba8ff | 2020-09-23 12:42:57 -0700 | [diff] [blame] | 39 | blobs.emplace_back(staticLayoutBlobId, std::move(image)); |
Patrick Venture | 19044e1 | 2019-05-23 19:30:28 -0700 | [diff] [blame] | 40 | |
Patrick Venture | 6d7735d | 2019-06-21 10:03:19 -0700 | [diff] [blame] | 41 | std::unique_ptr<TriggerableActionInterface> prepareMock = |
| 42 | std::make_unique<TriggerMock>(); |
| 43 | prepareMockPtr = reinterpret_cast<TriggerMock*>(prepareMock.get()); |
| 44 | |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 45 | std::unique_ptr<TriggerableActionInterface> verifyMock = |
| 46 | std::make_unique<TriggerMock>(); |
| 47 | verifyMockPtr = reinterpret_cast<TriggerMock*>(verifyMock.get()); |
Patrick Venture | 19044e1 | 2019-05-23 19:30:28 -0700 | [diff] [blame] | 48 | |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 49 | std::unique_ptr<TriggerableActionInterface> updateMock = |
| 50 | std::make_unique<TriggerMock>(); |
| 51 | updateMockPtr = reinterpret_cast<TriggerMock*>(updateMock.get()); |
Patrick Venture | 1a406fe | 2019-05-31 07:29:56 -0700 | [diff] [blame] | 52 | |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 53 | std::unique_ptr<ActionPack> actionPack = std::make_unique<ActionPack>(); |
| 54 | actionPack->preparation = std::move(prepareMock); |
| 55 | actionPack->verification = std::move(verifyMock); |
| 56 | actionPack->update = std::move(updateMock); |
| 57 | |
| 58 | ActionMap packs; |
| 59 | packs[staticLayoutBlobId] = std::move(actionPack); |
| 60 | |
Patrick Venture | 4934daa | 2020-09-22 16:37:44 -0700 | [diff] [blame] | 61 | std::vector<DataHandlerPack> data; |
| 62 | data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr); |
| 63 | |
Patrick Venture | 8a4f2aa | 2019-05-23 08:40:21 -0700 | [diff] [blame] | 64 | handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | 7b78343 | 2020-09-22 15:55:08 -0700 | [diff] [blame] | 65 | std::move(blobs), std::move(data), std::move(packs)); |
Patrick Venture | 8a4f2aa | 2019-05-23 08:40:21 -0700 | [diff] [blame] | 66 | } |
Patrick Venture | 19044e1 | 2019-05-23 19:30:28 -0700 | [diff] [blame] | 67 | |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 68 | void expectedState(FirmwareBlobHandler::UpdateState state) |
| 69 | { |
| 70 | auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get()); |
| 71 | EXPECT_EQ(state, realHandler->getCurrentState()); |
| 72 | } |
| 73 | |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 74 | void openToInProgress(const std::string& blobId) |
| 75 | { |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 76 | if (blobId == hashBlobId) |
| 77 | { |
| 78 | EXPECT_CALL(*hashImageMock, open(blobId)).WillOnce(Return(true)); |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | EXPECT_CALL(*imageMock2, open(blobId)).WillOnce(Return(true)); |
| 83 | } |
| 84 | |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 85 | if (blobId != hashBlobId) |
| 86 | { |
| 87 | EXPECT_CALL(*prepareMockPtr, trigger()).WillOnce(Return(true)); |
| 88 | } |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 89 | EXPECT_TRUE(handler->open(session, flags, blobId)); |
| 90 | expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress); |
| 91 | } |
| 92 | |
| 93 | void getToVerificationPending(const std::string& blobId) |
| 94 | { |
| 95 | openToInProgress(blobId); |
| 96 | |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 97 | if (blobId == hashBlobId) |
| 98 | { |
| 99 | EXPECT_CALL(*hashImageMock, close()).WillRepeatedly(Return()); |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | EXPECT_CALL(*imageMock2, close()).WillRepeatedly(Return()); |
| 104 | } |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 105 | handler->close(session); |
| 106 | expectedState(FirmwareBlobHandler::UpdateState::verificationPending); |
| 107 | } |
| 108 | |
| 109 | void getToVerificationStarted(const std::string& blobId) |
| 110 | { |
| 111 | getToVerificationPending(blobId); |
| 112 | |
| 113 | EXPECT_TRUE(handler->open(session, flags, verifyBlobId)); |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 114 | EXPECT_CALL(*verifyMockPtr, trigger()).WillOnce(Return(true)); |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 115 | |
| 116 | EXPECT_TRUE(handler->commit(session, {})); |
| 117 | expectedState(FirmwareBlobHandler::UpdateState::verificationStarted); |
| 118 | } |
| 119 | |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 120 | void getToVerificationStartedWitHashBlob() |
| 121 | { |
| 122 | /* Open both static and hash to check for activeHashBlobId. */ |
| 123 | getToVerificationPending(staticLayoutBlobId); |
| 124 | |
| 125 | openToInProgress(hashBlobId); |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 126 | EXPECT_CALL(*hashImageMock, close()).WillRepeatedly(Return()); |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 127 | handler->close(session); |
| 128 | expectedState(FirmwareBlobHandler::UpdateState::verificationPending); |
| 129 | |
| 130 | /* Now the hash is active AND the static image is active. */ |
| 131 | EXPECT_TRUE(handler->open(session, flags, verifyBlobId)); |
| 132 | EXPECT_CALL(*verifyMockPtr, trigger()).WillOnce(Return(true)); |
| 133 | |
| 134 | EXPECT_TRUE(handler->commit(session, {})); |
| 135 | expectedState(FirmwareBlobHandler::UpdateState::verificationStarted); |
| 136 | } |
| 137 | |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 138 | void getToVerificationCompleted(ActionStatus checkResponse) |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 139 | { |
| 140 | getToVerificationStarted(staticLayoutBlobId); |
| 141 | |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 142 | EXPECT_CALL(*verifyMockPtr, status()).WillOnce(Return(checkResponse)); |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 143 | blobs::BlobMeta meta; |
| 144 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 145 | expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted); |
| 146 | } |
| 147 | |
| 148 | void getToUpdatePending() |
| 149 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 150 | getToVerificationCompleted(ActionStatus::success); |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 151 | |
| 152 | handler->close(session); |
| 153 | expectedState(FirmwareBlobHandler::UpdateState::updatePending); |
| 154 | } |
| 155 | |
| 156 | void getToUpdateStarted() |
| 157 | { |
| 158 | getToUpdatePending(); |
| 159 | EXPECT_TRUE(handler->open(session, flags, updateBlobId)); |
| 160 | |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 161 | EXPECT_CALL(*updateMockPtr, trigger()).WillOnce(Return(true)); |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 162 | EXPECT_TRUE(handler->commit(session, {})); |
| 163 | expectedState(FirmwareBlobHandler::UpdateState::updateStarted); |
| 164 | } |
| 165 | |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 166 | void getToUpdateCompleted(ActionStatus result) |
Patrick Venture | ab1e962 | 2019-06-03 10:45:06 -0700 | [diff] [blame] | 167 | { |
| 168 | getToUpdateStarted(); |
| 169 | EXPECT_CALL(*updateMockPtr, status()).WillOnce(Return(result)); |
| 170 | |
| 171 | blobs::BlobMeta meta; |
| 172 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 173 | expectedState(FirmwareBlobHandler::UpdateState::updateCompleted); |
| 174 | } |
| 175 | |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 176 | ImageHandlerMock *hashImageMock, *imageMock2; |
| 177 | |
Patrick Venture | 19044e1 | 2019-05-23 19:30:28 -0700 | [diff] [blame] | 178 | std::vector<HandlerPack> blobs; |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 179 | |
Patrick Venture | 19044e1 | 2019-05-23 19:30:28 -0700 | [diff] [blame] | 180 | std::unique_ptr<blobs::GenericBlobInterface> handler; |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 181 | |
Patrick Venture | 6d7735d | 2019-06-21 10:03:19 -0700 | [diff] [blame] | 182 | TriggerMock* prepareMockPtr; |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 183 | TriggerMock* verifyMockPtr; |
| 184 | TriggerMock* updateMockPtr; |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 185 | |
| 186 | std::uint16_t session = 1; |
| 187 | std::uint16_t flags = |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 188 | blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::ipmi; |
Patrick Venture | 9a69f73 | 2019-06-17 14:05:13 -0700 | [diff] [blame] | 189 | |
Benjamin Fair | 1290198 | 2019-11-12 13:55:46 -0800 | [diff] [blame] | 190 | blobs::BlobMeta expectedIdleMeta = {0xff00, 0, {}}; |
| 191 | |
Patrick Venture | 9a69f73 | 2019-06-17 14:05:13 -0700 | [diff] [blame] | 192 | std::vector<std::string> startingBlobs = {staticLayoutBlobId, hashBlobId}; |
Patrick Venture | 8a4f2aa | 2019-05-23 08:40:21 -0700 | [diff] [blame] | 193 | }; |
| 194 | |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 195 | class IpmiOnlyFirmwareTest : public ::testing::Test |
| 196 | { |
| 197 | protected: |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 198 | ImageHandlerMock *hashImageMock, *imageMock; |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 199 | std::vector<HandlerPack> blobs; |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 200 | std::unique_ptr<blobs::GenericBlobInterface> handler; |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 201 | |
| 202 | void SetUp() override |
| 203 | { |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 204 | std::unique_ptr<ImageHandlerInterface> image = |
| 205 | std::make_unique<ImageHandlerMock>(); |
| 206 | hashImageMock = reinterpret_cast<ImageHandlerMock*>(image.get()); |
Patrick Venture | c6ba8ff | 2020-09-23 12:42:57 -0700 | [diff] [blame] | 207 | blobs.emplace_back(hashBlobId, std::move(image)); |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 208 | |
| 209 | image = std::make_unique<ImageHandlerMock>(); |
| 210 | imageMock = reinterpret_cast<ImageHandlerMock*>(image.get()); |
Patrick Venture | c6ba8ff | 2020-09-23 12:42:57 -0700 | [diff] [blame] | 211 | blobs.emplace_back("asdf", std::move(image)); |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 212 | |
Patrick Venture | 4934daa | 2020-09-22 16:37:44 -0700 | [diff] [blame] | 213 | std::vector<DataHandlerPack> data; |
| 214 | data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr); |
| 215 | |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 216 | handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | da4d4a4 | 2020-09-28 11:41:05 -0700 | [diff] [blame^] | 217 | std::move(blobs), std::move(data), CreateActionMap("asdf")); |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 218 | } |
| 219 | }; |
| 220 | |
| 221 | class FakeLpcFirmwareTest : public ::testing::Test |
| 222 | { |
| 223 | protected: |
Patrick Venture | 4934daa | 2020-09-22 16:37:44 -0700 | [diff] [blame] | 224 | DataHandlerMock* dataMock; |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 225 | ImageHandlerMock *hashImageMock, *imageMock; |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 226 | std::vector<HandlerPack> blobs; |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 227 | std::unique_ptr<blobs::GenericBlobInterface> handler; |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 228 | |
| 229 | void SetUp() override |
| 230 | { |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 231 | std::unique_ptr<ImageHandlerInterface> image = |
| 232 | std::make_unique<ImageHandlerMock>(); |
| 233 | hashImageMock = reinterpret_cast<ImageHandlerMock*>(image.get()); |
Patrick Venture | c6ba8ff | 2020-09-23 12:42:57 -0700 | [diff] [blame] | 234 | blobs.emplace_back(hashBlobId, std::move(image)); |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 235 | |
| 236 | image = std::make_unique<ImageHandlerMock>(); |
| 237 | imageMock = reinterpret_cast<ImageHandlerMock*>(image.get()); |
Patrick Venture | c6ba8ff | 2020-09-23 12:42:57 -0700 | [diff] [blame] | 238 | blobs.emplace_back("asdf", std::move(image)); |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 239 | |
Patrick Venture | 4934daa | 2020-09-22 16:37:44 -0700 | [diff] [blame] | 240 | auto dataMockInstance = std::make_unique<DataHandlerMock>(); |
| 241 | dataMock = dataMockInstance.get(); |
| 242 | |
| 243 | std::vector<DataHandlerPack> data; |
| 244 | data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr); |
| 245 | data.emplace_back(FirmwareFlags::UpdateFlags::lpc, |
| 246 | std::move(dataMockInstance)); |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 247 | handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | da4d4a4 | 2020-09-28 11:41:05 -0700 | [diff] [blame^] | 248 | std::move(blobs), std::move(data), CreateActionMap("asdf")); |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 249 | } |
| 250 | }; |
| 251 | |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 252 | } // namespace |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 253 | } // namespace ipmi_flash |