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()); |
| 35 | blobs.push_back(std::move(HandlerPack(hashBlobId, std::move(image)))); |
| 36 | |
| 37 | image = std::make_unique<ImageHandlerMock>(); |
| 38 | imageMock2 = reinterpret_cast<ImageHandlerMock*>(image.get()); |
| 39 | blobs.push_back( |
| 40 | std::move(HandlerPack(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 | 8a4f2aa | 2019-05-23 08:40:21 -0700 | [diff] [blame] | 62 | handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 63 | std::move(blobs), data, std::move(packs)); |
Patrick Venture | 8a4f2aa | 2019-05-23 08:40:21 -0700 | [diff] [blame] | 64 | } |
Patrick Venture | 19044e1 | 2019-05-23 19:30:28 -0700 | [diff] [blame] | 65 | |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 66 | void expectedState(FirmwareBlobHandler::UpdateState state) |
| 67 | { |
| 68 | auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get()); |
| 69 | EXPECT_EQ(state, realHandler->getCurrentState()); |
| 70 | } |
| 71 | |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 72 | void openToInProgress(const std::string& blobId) |
| 73 | { |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 74 | if (blobId == hashBlobId) |
| 75 | { |
| 76 | EXPECT_CALL(*hashImageMock, open(blobId)).WillOnce(Return(true)); |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | EXPECT_CALL(*imageMock2, open(blobId)).WillOnce(Return(true)); |
| 81 | } |
| 82 | |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 83 | if (blobId != hashBlobId) |
| 84 | { |
| 85 | EXPECT_CALL(*prepareMockPtr, trigger()).WillOnce(Return(true)); |
| 86 | } |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 87 | EXPECT_TRUE(handler->open(session, flags, blobId)); |
| 88 | expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress); |
| 89 | } |
| 90 | |
| 91 | void getToVerificationPending(const std::string& blobId) |
| 92 | { |
| 93 | openToInProgress(blobId); |
| 94 | |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 95 | if (blobId == hashBlobId) |
| 96 | { |
| 97 | EXPECT_CALL(*hashImageMock, close()).WillRepeatedly(Return()); |
| 98 | } |
| 99 | else |
| 100 | { |
| 101 | EXPECT_CALL(*imageMock2, close()).WillRepeatedly(Return()); |
| 102 | } |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 103 | handler->close(session); |
| 104 | expectedState(FirmwareBlobHandler::UpdateState::verificationPending); |
| 105 | } |
| 106 | |
| 107 | void getToVerificationStarted(const std::string& blobId) |
| 108 | { |
| 109 | getToVerificationPending(blobId); |
| 110 | |
| 111 | EXPECT_TRUE(handler->open(session, flags, verifyBlobId)); |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 112 | EXPECT_CALL(*verifyMockPtr, trigger()).WillOnce(Return(true)); |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 113 | |
| 114 | EXPECT_TRUE(handler->commit(session, {})); |
| 115 | expectedState(FirmwareBlobHandler::UpdateState::verificationStarted); |
| 116 | } |
| 117 | |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 118 | void getToVerificationStartedWitHashBlob() |
| 119 | { |
| 120 | /* Open both static and hash to check for activeHashBlobId. */ |
| 121 | getToVerificationPending(staticLayoutBlobId); |
| 122 | |
| 123 | openToInProgress(hashBlobId); |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 124 | EXPECT_CALL(*hashImageMock, close()).WillRepeatedly(Return()); |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 125 | handler->close(session); |
| 126 | expectedState(FirmwareBlobHandler::UpdateState::verificationPending); |
| 127 | |
| 128 | /* Now the hash is active AND the static image is active. */ |
| 129 | EXPECT_TRUE(handler->open(session, flags, verifyBlobId)); |
| 130 | EXPECT_CALL(*verifyMockPtr, trigger()).WillOnce(Return(true)); |
| 131 | |
| 132 | EXPECT_TRUE(handler->commit(session, {})); |
| 133 | expectedState(FirmwareBlobHandler::UpdateState::verificationStarted); |
| 134 | } |
| 135 | |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 136 | void getToVerificationCompleted(ActionStatus checkResponse) |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 137 | { |
| 138 | getToVerificationStarted(staticLayoutBlobId); |
| 139 | |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 140 | EXPECT_CALL(*verifyMockPtr, status()).WillOnce(Return(checkResponse)); |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 141 | blobs::BlobMeta meta; |
| 142 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 143 | expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted); |
| 144 | } |
| 145 | |
| 146 | void getToUpdatePending() |
| 147 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 148 | getToVerificationCompleted(ActionStatus::success); |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 149 | |
| 150 | handler->close(session); |
| 151 | expectedState(FirmwareBlobHandler::UpdateState::updatePending); |
| 152 | } |
| 153 | |
| 154 | void getToUpdateStarted() |
| 155 | { |
| 156 | getToUpdatePending(); |
| 157 | EXPECT_TRUE(handler->open(session, flags, updateBlobId)); |
| 158 | |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 159 | EXPECT_CALL(*updateMockPtr, trigger()).WillOnce(Return(true)); |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 160 | EXPECT_TRUE(handler->commit(session, {})); |
| 161 | expectedState(FirmwareBlobHandler::UpdateState::updateStarted); |
| 162 | } |
| 163 | |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 164 | void getToUpdateCompleted(ActionStatus result) |
Patrick Venture | ab1e962 | 2019-06-03 10:45:06 -0700 | [diff] [blame] | 165 | { |
| 166 | getToUpdateStarted(); |
| 167 | EXPECT_CALL(*updateMockPtr, status()).WillOnce(Return(result)); |
| 168 | |
| 169 | blobs::BlobMeta meta; |
| 170 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 171 | expectedState(FirmwareBlobHandler::UpdateState::updateCompleted); |
| 172 | } |
| 173 | |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 174 | ImageHandlerMock *hashImageMock, *imageMock2; |
| 175 | |
Patrick Venture | 19044e1 | 2019-05-23 19:30:28 -0700 | [diff] [blame] | 176 | std::vector<HandlerPack> blobs; |
| 177 | std::vector<DataHandlerPack> data = { |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 178 | {FirmwareFlags::UpdateFlags::ipmi, nullptr}}; |
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; |
| 200 | std::vector<DataHandlerPack> data = { |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 201 | {FirmwareFlags::UpdateFlags::ipmi, nullptr}}; |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 202 | std::unique_ptr<blobs::GenericBlobInterface> handler; |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 203 | |
| 204 | void SetUp() override |
| 205 | { |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 206 | std::unique_ptr<ImageHandlerInterface> image = |
| 207 | std::make_unique<ImageHandlerMock>(); |
| 208 | hashImageMock = reinterpret_cast<ImageHandlerMock*>(image.get()); |
| 209 | blobs.push_back(std::move(HandlerPack(hashBlobId, std::move(image)))); |
| 210 | |
| 211 | image = std::make_unique<ImageHandlerMock>(); |
| 212 | imageMock = reinterpret_cast<ImageHandlerMock*>(image.get()); |
| 213 | blobs.push_back(std::move(HandlerPack("asdf", std::move(image)))); |
| 214 | |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 215 | handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 216 | std::move(blobs), data, std::move(CreateActionMap("asdf"))); |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 217 | } |
| 218 | }; |
| 219 | |
| 220 | class FakeLpcFirmwareTest : public ::testing::Test |
| 221 | { |
| 222 | protected: |
| 223 | DataHandlerMock dataMock; |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 224 | ImageHandlerMock *hashImageMock, *imageMock; |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 225 | std::vector<HandlerPack> blobs; |
| 226 | std::vector<DataHandlerPack> data; |
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()); |
| 234 | blobs.push_back(std::move(HandlerPack(hashBlobId, std::move(image)))); |
| 235 | |
| 236 | image = std::make_unique<ImageHandlerMock>(); |
| 237 | imageMock = reinterpret_cast<ImageHandlerMock*>(image.get()); |
| 238 | blobs.push_back(std::move(HandlerPack("asdf", std::move(image)))); |
| 239 | |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 240 | data = { |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 241 | {FirmwareFlags::UpdateFlags::ipmi, nullptr}, |
| 242 | {FirmwareFlags::UpdateFlags::lpc, &dataMock}, |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 243 | }; |
| 244 | handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 245 | std::move(blobs), data, std::move(CreateActionMap("asdf"))); |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 246 | } |
| 247 | }; |
| 248 | |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 249 | } // namespace |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 250 | } // namespace ipmi_flash |