blob: 0d18bda53173839b953f6c70319b958765c6dfcd [file] [log] [blame]
Patrick Venture1361a122019-05-20 07:36:05 -07001#pragma once
2
Jason Ling84bff8b2020-11-06 13:45:09 -08003#include "create_action_map.hpp"
Patrick Venture1361a122019-05-20 07:36:05 -07004#include "data_mock.hpp"
5#include "firmware_handler.hpp"
Patrick Venture84778b82019-06-26 20:11:09 -07006#include "flags.hpp"
Patrick Venture1361a122019-05-20 07:36:05 -07007#include "image_mock.hpp"
Patrick Venture1d66fe62019-06-03 14:57:27 -07008#include "triggerable_mock.hpp"
Patrick Venture4b1b0452020-09-30 13:40:53 -07009#include "util.hpp"
Patrick Venture1361a122019-05-20 07:36:05 -070010
11#include <memory>
Patrick Venture9a69f732019-06-17 14:05:13 -070012#include <string>
Patrick Venturefa06a5f2019-07-01 09:22:38 -070013#include <unordered_map>
Patrick Venture1361a122019-05-20 07:36:05 -070014#include <vector>
15
16#include <gmock/gmock.h>
17#include <gtest/gtest.h>
18
Patrick Venture1d5a31c2019-05-20 11:38:22 -070019namespace ipmi_flash
Patrick Venture1361a122019-05-20 07:36:05 -070020{
Patrick Venture16ab2a12019-05-31 08:52:51 -070021namespace
22{
23
24using ::testing::Return;
Patrick Venture1361a122019-05-20 07:36:05 -070025
Patrick Venture8a4f2aa2019-05-23 08:40:21 -070026class IpmiOnlyFirmwareStaticTest : public ::testing::Test
27{
28 protected:
Patrick Venture8a4f2aa2019-05-23 08:40:21 -070029 void SetUp() override
30 {
Patrick Ventured4e20de2019-07-18 12:48:05 -070031 /* 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 Venturec6ba8ff2020-09-23 12:42:57 -070037 blobs.emplace_back(hashBlobId, std::move(image));
Patrick Ventured4e20de2019-07-18 12:48:05 -070038
39 image = std::make_unique<ImageHandlerMock>();
40 imageMock2 = reinterpret_cast<ImageHandlerMock*>(image.get());
Patrick Venturec6ba8ff2020-09-23 12:42:57 -070041 blobs.emplace_back(staticLayoutBlobId, std::move(image));
Patrick Venture19044e12019-05-23 19:30:28 -070042
Patrick Venture6d7735d2019-06-21 10:03:19 -070043 std::unique_ptr<TriggerableActionInterface> prepareMock =
44 std::make_unique<TriggerMock>();
45 prepareMockPtr = reinterpret_cast<TriggerMock*>(prepareMock.get());
46
Patrick Venture1d66fe62019-06-03 14:57:27 -070047 std::unique_ptr<TriggerableActionInterface> verifyMock =
48 std::make_unique<TriggerMock>();
49 verifyMockPtr = reinterpret_cast<TriggerMock*>(verifyMock.get());
Patrick Venture19044e12019-05-23 19:30:28 -070050
Patrick Venture1d66fe62019-06-03 14:57:27 -070051 std::unique_ptr<TriggerableActionInterface> updateMock =
52 std::make_unique<TriggerMock>();
53 updateMockPtr = reinterpret_cast<TriggerMock*>(updateMock.get());
Patrick Venture1a406fe2019-05-31 07:29:56 -070054
Patrick Venturefa06a5f2019-07-01 09:22:38 -070055 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 Venture4934daa2020-09-22 16:37:44 -070063 std::vector<DataHandlerPack> data;
64 data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr);
65
Patrick Venture8a4f2aa2019-05-23 08:40:21 -070066 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture7b783432020-09-22 15:55:08 -070067 std::move(blobs), std::move(data), std::move(packs));
Patrick Venture8a4f2aa2019-05-23 08:40:21 -070068 }
Patrick Venture19044e12019-05-23 19:30:28 -070069
Patrick Venture6fdd02e2019-05-28 13:02:04 -070070 void expectedState(FirmwareBlobHandler::UpdateState state)
71 {
72 auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
73 EXPECT_EQ(state, realHandler->getCurrentState());
74 }
75
Patrick Venture16ab2a12019-05-31 08:52:51 -070076 void openToInProgress(const std::string& blobId)
77 {
Patrick Ventured4e20de2019-07-18 12:48:05 -070078 if (blobId == hashBlobId)
79 {
Jason Ling56a22732020-10-23 19:53:17 -070080 EXPECT_CALL(*hashImageMock, open(blobId, std::ios::out))
81 .WillOnce(Return(true));
Patrick Ventured4e20de2019-07-18 12:48:05 -070082 }
83 else
84 {
Jason Ling56a22732020-10-23 19:53:17 -070085 EXPECT_CALL(*imageMock2, open(blobId, std::ios::out))
86 .WillOnce(Return(true));
Patrick Ventured4e20de2019-07-18 12:48:05 -070087 }
88
Patrick Venturefa06a5f2019-07-01 09:22:38 -070089 if (blobId != hashBlobId)
90 {
91 EXPECT_CALL(*prepareMockPtr, trigger()).WillOnce(Return(true));
92 }
Patrick Venture16ab2a12019-05-31 08:52:51 -070093 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 Ventured4e20de2019-07-18 12:48:05 -0700101 if (blobId == hashBlobId)
102 {
103 EXPECT_CALL(*hashImageMock, close()).WillRepeatedly(Return());
104 }
105 else
106 {
107 EXPECT_CALL(*imageMock2, close()).WillRepeatedly(Return());
108 }
Patrick Venture16ab2a12019-05-31 08:52:51 -0700109 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 Venture1d66fe62019-06-03 14:57:27 -0700118 EXPECT_CALL(*verifyMockPtr, trigger()).WillOnce(Return(true));
Patrick Venture16ab2a12019-05-31 08:52:51 -0700119
120 EXPECT_TRUE(handler->commit(session, {}));
121 expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
122 }
123
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700124 void getToVerificationStartedWitHashBlob()
125 {
126 /* Open both static and hash to check for activeHashBlobId. */
127 getToVerificationPending(staticLayoutBlobId);
128
129 openToInProgress(hashBlobId);
Patrick Ventured4e20de2019-07-18 12:48:05 -0700130 EXPECT_CALL(*hashImageMock, close()).WillRepeatedly(Return());
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700131 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 Ventureda66fd82019-06-03 11:11:24 -0700142 void getToVerificationCompleted(ActionStatus checkResponse)
Patrick Venture16ab2a12019-05-31 08:52:51 -0700143 {
144 getToVerificationStarted(staticLayoutBlobId);
145
Patrick Venturef1f0f652019-06-03 09:10:19 -0700146 EXPECT_CALL(*verifyMockPtr, status()).WillOnce(Return(checkResponse));
Patrick Venture16ab2a12019-05-31 08:52:51 -0700147 blobs::BlobMeta meta;
148 EXPECT_TRUE(handler->stat(session, &meta));
149 expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
150 }
151
152 void getToUpdatePending()
153 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700154 getToVerificationCompleted(ActionStatus::success);
Patrick Venture16ab2a12019-05-31 08:52:51 -0700155
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 Venture1d66fe62019-06-03 14:57:27 -0700165 EXPECT_CALL(*updateMockPtr, trigger()).WillOnce(Return(true));
Patrick Venture16ab2a12019-05-31 08:52:51 -0700166 EXPECT_TRUE(handler->commit(session, {}));
167 expectedState(FirmwareBlobHandler::UpdateState::updateStarted);
168 }
169
Patrick Ventureda66fd82019-06-03 11:11:24 -0700170 void getToUpdateCompleted(ActionStatus result)
Patrick Ventureab1e9622019-06-03 10:45:06 -0700171 {
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 Ventured4e20de2019-07-18 12:48:05 -0700180 ImageHandlerMock *hashImageMock, *imageMock2;
181
Patrick Venture19044e12019-05-23 19:30:28 -0700182 std::vector<HandlerPack> blobs;
Patrick Ventured4e20de2019-07-18 12:48:05 -0700183
Patrick Venture19044e12019-05-23 19:30:28 -0700184 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Ventured4e20de2019-07-18 12:48:05 -0700185
Patrick Venture6d7735d2019-06-21 10:03:19 -0700186 TriggerMock* prepareMockPtr;
Patrick Venture1d66fe62019-06-03 14:57:27 -0700187 TriggerMock* verifyMockPtr;
188 TriggerMock* updateMockPtr;
Patrick Venture16ab2a12019-05-31 08:52:51 -0700189
190 std::uint16_t session = 1;
191 std::uint16_t flags =
Patrick Venture84778b82019-06-26 20:11:09 -0700192 blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::ipmi;
Patrick Venture9a69f732019-06-17 14:05:13 -0700193
Benjamin Fair12901982019-11-12 13:55:46 -0800194 blobs::BlobMeta expectedIdleMeta = {0xff00, 0, {}};
195
Patrick Venture9a69f732019-06-17 14:05:13 -0700196 std::vector<std::string> startingBlobs = {staticLayoutBlobId, hashBlobId};
Patrick Venture8a4f2aa2019-05-23 08:40:21 -0700197};
198
Patrick Venture1361a122019-05-20 07:36:05 -0700199class IpmiOnlyFirmwareTest : public ::testing::Test
200{
201 protected:
Patrick Ventured4e20de2019-07-18 12:48:05 -0700202 ImageHandlerMock *hashImageMock, *imageMock;
Patrick Venture1361a122019-05-20 07:36:05 -0700203 std::vector<HandlerPack> blobs;
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700204 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Venture1361a122019-05-20 07:36:05 -0700205
206 void SetUp() override
207 {
Patrick Ventured4e20de2019-07-18 12:48:05 -0700208 std::unique_ptr<ImageHandlerInterface> image =
209 std::make_unique<ImageHandlerMock>();
210 hashImageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
Patrick Venturec6ba8ff2020-09-23 12:42:57 -0700211 blobs.emplace_back(hashBlobId, std::move(image));
Patrick Ventured4e20de2019-07-18 12:48:05 -0700212
213 image = std::make_unique<ImageHandlerMock>();
214 imageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
Patrick Venturec6ba8ff2020-09-23 12:42:57 -0700215 blobs.emplace_back("asdf", std::move(image));
Patrick Ventured4e20de2019-07-18 12:48:05 -0700216
Patrick Venture4934daa2020-09-22 16:37:44 -0700217 std::vector<DataHandlerPack> data;
218 data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr);
219
Patrick Venture1361a122019-05-20 07:36:05 -0700220 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Ventureda4d4a42020-09-28 11:41:05 -0700221 std::move(blobs), std::move(data), CreateActionMap("asdf"));
Patrick Venture1361a122019-05-20 07:36:05 -0700222 }
223};
224
225class FakeLpcFirmwareTest : public ::testing::Test
226{
227 protected:
Patrick Venture4934daa2020-09-22 16:37:44 -0700228 DataHandlerMock* dataMock;
Patrick Ventured4e20de2019-07-18 12:48:05 -0700229 ImageHandlerMock *hashImageMock, *imageMock;
Patrick Venture1361a122019-05-20 07:36:05 -0700230 std::vector<HandlerPack> blobs;
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700231 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Venture1361a122019-05-20 07:36:05 -0700232
233 void SetUp() override
234 {
Patrick Ventured4e20de2019-07-18 12:48:05 -0700235 std::unique_ptr<ImageHandlerInterface> image =
236 std::make_unique<ImageHandlerMock>();
237 hashImageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
Patrick Venturec6ba8ff2020-09-23 12:42:57 -0700238 blobs.emplace_back(hashBlobId, std::move(image));
Patrick Ventured4e20de2019-07-18 12:48:05 -0700239
240 image = std::make_unique<ImageHandlerMock>();
241 imageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
Patrick Venturec6ba8ff2020-09-23 12:42:57 -0700242 blobs.emplace_back("asdf", std::move(image));
Patrick Ventured4e20de2019-07-18 12:48:05 -0700243
Patrick Venture4934daa2020-09-22 16:37:44 -0700244 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 Venture1361a122019-05-20 07:36:05 -0700251 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Ventureda4d4a42020-09-28 11:41:05 -0700252 std::move(blobs), std::move(data), CreateActionMap("asdf"));
Patrick Venture1361a122019-05-20 07:36:05 -0700253 }
254};
255
Patrick Venture16ab2a12019-05-31 08:52:51 -0700256} // namespace
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700257} // namespace ipmi_flash