blob: e9db343dfc2905be1e72c870edb255cbee34a567 [file] [log] [blame]
Patrick Venture1361a122019-05-20 07:36:05 -07001#pragma once
2
3#include "data_mock.hpp"
4#include "firmware_handler.hpp"
Patrick Venture84778b82019-06-26 20:11:09 -07005#include "flags.hpp"
Patrick Venture1361a122019-05-20 07:36:05 -07006#include "image_mock.hpp"
Patrick Venture1d66fe62019-06-03 14:57:27 -07007#include "triggerable_mock.hpp"
Patrick Venture4b1b0452020-09-30 13:40:53 -07008#include "util.hpp"
Patrick Venture1361a122019-05-20 07:36:05 -07009
10#include <memory>
Patrick Venture9a69f732019-06-17 14:05:13 -070011#include <string>
Patrick Venturefa06a5f2019-07-01 09:22:38 -070012#include <unordered_map>
Patrick Venture1361a122019-05-20 07:36:05 -070013#include <vector>
14
15#include <gmock/gmock.h>
16#include <gtest/gtest.h>
17
Patrick Venture1d5a31c2019-05-20 11:38:22 -070018namespace ipmi_flash
Patrick Venture1361a122019-05-20 07:36:05 -070019{
Patrick Venture16ab2a12019-05-31 08:52:51 -070020namespace
21{
22
23using ::testing::Return;
Patrick Venture1361a122019-05-20 07:36:05 -070024
Patrick Venture8a4f2aa2019-05-23 08:40:21 -070025class IpmiOnlyFirmwareStaticTest : public ::testing::Test
26{
27 protected:
Patrick Venture8a4f2aa2019-05-23 08:40:21 -070028 void SetUp() override
29 {
Patrick Ventured4e20de2019-07-18 12:48:05 -070030 /* 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 Venturec6ba8ff2020-09-23 12:42:57 -070036 blobs.emplace_back(hashBlobId, std::move(image));
Patrick Ventured4e20de2019-07-18 12:48:05 -070037
38 image = std::make_unique<ImageHandlerMock>();
39 imageMock2 = reinterpret_cast<ImageHandlerMock*>(image.get());
Patrick Venturec6ba8ff2020-09-23 12:42:57 -070040 blobs.emplace_back(staticLayoutBlobId, std::move(image));
Patrick Venture19044e12019-05-23 19:30:28 -070041
Patrick Venture6d7735d2019-06-21 10:03:19 -070042 std::unique_ptr<TriggerableActionInterface> prepareMock =
43 std::make_unique<TriggerMock>();
44 prepareMockPtr = reinterpret_cast<TriggerMock*>(prepareMock.get());
45
Patrick Venture1d66fe62019-06-03 14:57:27 -070046 std::unique_ptr<TriggerableActionInterface> verifyMock =
47 std::make_unique<TriggerMock>();
48 verifyMockPtr = reinterpret_cast<TriggerMock*>(verifyMock.get());
Patrick Venture19044e12019-05-23 19:30:28 -070049
Patrick Venture1d66fe62019-06-03 14:57:27 -070050 std::unique_ptr<TriggerableActionInterface> updateMock =
51 std::make_unique<TriggerMock>();
52 updateMockPtr = reinterpret_cast<TriggerMock*>(updateMock.get());
Patrick Venture1a406fe2019-05-31 07:29:56 -070053
Patrick Venturefa06a5f2019-07-01 09:22:38 -070054 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 Venture4934daa2020-09-22 16:37:44 -070062 std::vector<DataHandlerPack> data;
63 data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr);
64
Patrick Venture8a4f2aa2019-05-23 08:40:21 -070065 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture7b783432020-09-22 15:55:08 -070066 std::move(blobs), std::move(data), std::move(packs));
Patrick Venture8a4f2aa2019-05-23 08:40:21 -070067 }
Patrick Venture19044e12019-05-23 19:30:28 -070068
Patrick Venture6fdd02e2019-05-28 13:02:04 -070069 void expectedState(FirmwareBlobHandler::UpdateState state)
70 {
71 auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
72 EXPECT_EQ(state, realHandler->getCurrentState());
73 }
74
Patrick Venture16ab2a12019-05-31 08:52:51 -070075 void openToInProgress(const std::string& blobId)
76 {
Patrick Ventured4e20de2019-07-18 12:48:05 -070077 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 Venturefa06a5f2019-07-01 09:22:38 -070086 if (blobId != hashBlobId)
87 {
88 EXPECT_CALL(*prepareMockPtr, trigger()).WillOnce(Return(true));
89 }
Patrick Venture16ab2a12019-05-31 08:52:51 -070090 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 Ventured4e20de2019-07-18 12:48:05 -070098 if (blobId == hashBlobId)
99 {
100 EXPECT_CALL(*hashImageMock, close()).WillRepeatedly(Return());
101 }
102 else
103 {
104 EXPECT_CALL(*imageMock2, close()).WillRepeatedly(Return());
105 }
Patrick Venture16ab2a12019-05-31 08:52:51 -0700106 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 Venture1d66fe62019-06-03 14:57:27 -0700115 EXPECT_CALL(*verifyMockPtr, trigger()).WillOnce(Return(true));
Patrick Venture16ab2a12019-05-31 08:52:51 -0700116
117 EXPECT_TRUE(handler->commit(session, {}));
118 expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
119 }
120
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700121 void getToVerificationStartedWitHashBlob()
122 {
123 /* Open both static and hash to check for activeHashBlobId. */
124 getToVerificationPending(staticLayoutBlobId);
125
126 openToInProgress(hashBlobId);
Patrick Ventured4e20de2019-07-18 12:48:05 -0700127 EXPECT_CALL(*hashImageMock, close()).WillRepeatedly(Return());
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700128 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 Ventureda66fd82019-06-03 11:11:24 -0700139 void getToVerificationCompleted(ActionStatus checkResponse)
Patrick Venture16ab2a12019-05-31 08:52:51 -0700140 {
141 getToVerificationStarted(staticLayoutBlobId);
142
Patrick Venturef1f0f652019-06-03 09:10:19 -0700143 EXPECT_CALL(*verifyMockPtr, status()).WillOnce(Return(checkResponse));
Patrick Venture16ab2a12019-05-31 08:52:51 -0700144 blobs::BlobMeta meta;
145 EXPECT_TRUE(handler->stat(session, &meta));
146 expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
147 }
148
149 void getToUpdatePending()
150 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700151 getToVerificationCompleted(ActionStatus::success);
Patrick Venture16ab2a12019-05-31 08:52:51 -0700152
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 Venture1d66fe62019-06-03 14:57:27 -0700162 EXPECT_CALL(*updateMockPtr, trigger()).WillOnce(Return(true));
Patrick Venture16ab2a12019-05-31 08:52:51 -0700163 EXPECT_TRUE(handler->commit(session, {}));
164 expectedState(FirmwareBlobHandler::UpdateState::updateStarted);
165 }
166
Patrick Ventureda66fd82019-06-03 11:11:24 -0700167 void getToUpdateCompleted(ActionStatus result)
Patrick Ventureab1e9622019-06-03 10:45:06 -0700168 {
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 Ventured4e20de2019-07-18 12:48:05 -0700177 ImageHandlerMock *hashImageMock, *imageMock2;
178
Patrick Venture19044e12019-05-23 19:30:28 -0700179 std::vector<HandlerPack> blobs;
Patrick Ventured4e20de2019-07-18 12:48:05 -0700180
Patrick Venture19044e12019-05-23 19:30:28 -0700181 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Ventured4e20de2019-07-18 12:48:05 -0700182
Patrick Venture6d7735d2019-06-21 10:03:19 -0700183 TriggerMock* prepareMockPtr;
Patrick Venture1d66fe62019-06-03 14:57:27 -0700184 TriggerMock* verifyMockPtr;
185 TriggerMock* updateMockPtr;
Patrick Venture16ab2a12019-05-31 08:52:51 -0700186
187 std::uint16_t session = 1;
188 std::uint16_t flags =
Patrick Venture84778b82019-06-26 20:11:09 -0700189 blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::ipmi;
Patrick Venture9a69f732019-06-17 14:05:13 -0700190
Benjamin Fair12901982019-11-12 13:55:46 -0800191 blobs::BlobMeta expectedIdleMeta = {0xff00, 0, {}};
192
Patrick Venture9a69f732019-06-17 14:05:13 -0700193 std::vector<std::string> startingBlobs = {staticLayoutBlobId, hashBlobId};
Patrick Venture8a4f2aa2019-05-23 08:40:21 -0700194};
195
Patrick Venture1361a122019-05-20 07:36:05 -0700196class IpmiOnlyFirmwareTest : public ::testing::Test
197{
198 protected:
Patrick Ventured4e20de2019-07-18 12:48:05 -0700199 ImageHandlerMock *hashImageMock, *imageMock;
Patrick Venture1361a122019-05-20 07:36:05 -0700200 std::vector<HandlerPack> blobs;
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700201 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Venture1361a122019-05-20 07:36:05 -0700202
203 void SetUp() override
204 {
Patrick Ventured4e20de2019-07-18 12:48:05 -0700205 std::unique_ptr<ImageHandlerInterface> image =
206 std::make_unique<ImageHandlerMock>();
207 hashImageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
Patrick Venturec6ba8ff2020-09-23 12:42:57 -0700208 blobs.emplace_back(hashBlobId, std::move(image));
Patrick Ventured4e20de2019-07-18 12:48:05 -0700209
210 image = std::make_unique<ImageHandlerMock>();
211 imageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
Patrick Venturec6ba8ff2020-09-23 12:42:57 -0700212 blobs.emplace_back("asdf", std::move(image));
Patrick Ventured4e20de2019-07-18 12:48:05 -0700213
Patrick Venture4934daa2020-09-22 16:37:44 -0700214 std::vector<DataHandlerPack> data;
215 data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr);
216
Patrick Venture1361a122019-05-20 07:36:05 -0700217 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Ventureda4d4a42020-09-28 11:41:05 -0700218 std::move(blobs), std::move(data), CreateActionMap("asdf"));
Patrick Venture1361a122019-05-20 07:36:05 -0700219 }
220};
221
222class FakeLpcFirmwareTest : public ::testing::Test
223{
224 protected:
Patrick Venture4934daa2020-09-22 16:37:44 -0700225 DataHandlerMock* dataMock;
Patrick Ventured4e20de2019-07-18 12:48:05 -0700226 ImageHandlerMock *hashImageMock, *imageMock;
Patrick Venture1361a122019-05-20 07:36:05 -0700227 std::vector<HandlerPack> blobs;
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700228 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Venture1361a122019-05-20 07:36:05 -0700229
230 void SetUp() override
231 {
Patrick Ventured4e20de2019-07-18 12:48:05 -0700232 std::unique_ptr<ImageHandlerInterface> image =
233 std::make_unique<ImageHandlerMock>();
234 hashImageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
Patrick Venturec6ba8ff2020-09-23 12:42:57 -0700235 blobs.emplace_back(hashBlobId, std::move(image));
Patrick Ventured4e20de2019-07-18 12:48:05 -0700236
237 image = std::make_unique<ImageHandlerMock>();
238 imageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
Patrick Venturec6ba8ff2020-09-23 12:42:57 -0700239 blobs.emplace_back("asdf", std::move(image));
Patrick Ventured4e20de2019-07-18 12:48:05 -0700240
Patrick Venture4934daa2020-09-22 16:37:44 -0700241 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 Venture1361a122019-05-20 07:36:05 -0700248 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Ventureda4d4a42020-09-28 11:41:05 -0700249 std::move(blobs), std::move(data), CreateActionMap("asdf"));
Patrick Venture1361a122019-05-20 07:36:05 -0700250 }
251};
252
Patrick Venture16ab2a12019-05-31 08:52:51 -0700253} // namespace
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700254} // namespace ipmi_flash