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