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