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