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