blob: da2e8f79e9dd7052a2e42ed5a77ad2614a80a399 [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 {
Jason Ling56a22732020-10-23 19:53:17 -070079 EXPECT_CALL(*hashImageMock, open(blobId, std::ios::out))
80 .WillOnce(Return(true));
Patrick Ventured4e20de2019-07-18 12:48:05 -070081 }
82 else
83 {
Jason Ling56a22732020-10-23 19:53:17 -070084 EXPECT_CALL(*imageMock2, open(blobId, std::ios::out))
85 .WillOnce(Return(true));
Patrick Ventured4e20de2019-07-18 12:48:05 -070086 }
87
Patrick Venturefa06a5f2019-07-01 09:22:38 -070088 if (blobId != hashBlobId)
89 {
90 EXPECT_CALL(*prepareMockPtr, trigger()).WillOnce(Return(true));
91 }
Patrick Venture16ab2a12019-05-31 08:52:51 -070092 EXPECT_TRUE(handler->open(session, flags, blobId));
93 expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
94 }
95
96 void getToVerificationPending(const std::string& blobId)
97 {
98 openToInProgress(blobId);
99
Patrick Ventured4e20de2019-07-18 12:48:05 -0700100 if (blobId == hashBlobId)
101 {
102 EXPECT_CALL(*hashImageMock, close()).WillRepeatedly(Return());
103 }
104 else
105 {
106 EXPECT_CALL(*imageMock2, close()).WillRepeatedly(Return());
107 }
Patrick Venture16ab2a12019-05-31 08:52:51 -0700108 handler->close(session);
109 expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
110 }
111
112 void getToVerificationStarted(const std::string& blobId)
113 {
114 getToVerificationPending(blobId);
115
116 EXPECT_TRUE(handler->open(session, flags, verifyBlobId));
Patrick Venture1d66fe62019-06-03 14:57:27 -0700117 EXPECT_CALL(*verifyMockPtr, trigger()).WillOnce(Return(true));
Patrick Venture16ab2a12019-05-31 08:52:51 -0700118
119 EXPECT_TRUE(handler->commit(session, {}));
120 expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
121 }
122
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700123 void getToVerificationStartedWitHashBlob()
124 {
125 /* Open both static and hash to check for activeHashBlobId. */
126 getToVerificationPending(staticLayoutBlobId);
127
128 openToInProgress(hashBlobId);
Patrick Ventured4e20de2019-07-18 12:48:05 -0700129 EXPECT_CALL(*hashImageMock, close()).WillRepeatedly(Return());
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700130 handler->close(session);
131 expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
132
133 /* Now the hash is active AND the static image is active. */
134 EXPECT_TRUE(handler->open(session, flags, verifyBlobId));
135 EXPECT_CALL(*verifyMockPtr, trigger()).WillOnce(Return(true));
136
137 EXPECT_TRUE(handler->commit(session, {}));
138 expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
139 }
140
Patrick Ventureda66fd82019-06-03 11:11:24 -0700141 void getToVerificationCompleted(ActionStatus checkResponse)
Patrick Venture16ab2a12019-05-31 08:52:51 -0700142 {
143 getToVerificationStarted(staticLayoutBlobId);
144
Patrick Venturef1f0f652019-06-03 09:10:19 -0700145 EXPECT_CALL(*verifyMockPtr, status()).WillOnce(Return(checkResponse));
Patrick Venture16ab2a12019-05-31 08:52:51 -0700146 blobs::BlobMeta meta;
147 EXPECT_TRUE(handler->stat(session, &meta));
148 expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
149 }
150
151 void getToUpdatePending()
152 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700153 getToVerificationCompleted(ActionStatus::success);
Patrick Venture16ab2a12019-05-31 08:52:51 -0700154
155 handler->close(session);
156 expectedState(FirmwareBlobHandler::UpdateState::updatePending);
157 }
158
159 void getToUpdateStarted()
160 {
161 getToUpdatePending();
162 EXPECT_TRUE(handler->open(session, flags, updateBlobId));
163
Patrick Venture1d66fe62019-06-03 14:57:27 -0700164 EXPECT_CALL(*updateMockPtr, trigger()).WillOnce(Return(true));
Patrick Venture16ab2a12019-05-31 08:52:51 -0700165 EXPECT_TRUE(handler->commit(session, {}));
166 expectedState(FirmwareBlobHandler::UpdateState::updateStarted);
167 }
168
Patrick Ventureda66fd82019-06-03 11:11:24 -0700169 void getToUpdateCompleted(ActionStatus result)
Patrick Ventureab1e9622019-06-03 10:45:06 -0700170 {
171 getToUpdateStarted();
172 EXPECT_CALL(*updateMockPtr, status()).WillOnce(Return(result));
173
174 blobs::BlobMeta meta;
175 EXPECT_TRUE(handler->stat(session, &meta));
176 expectedState(FirmwareBlobHandler::UpdateState::updateCompleted);
177 }
178
Patrick Ventured4e20de2019-07-18 12:48:05 -0700179 ImageHandlerMock *hashImageMock, *imageMock2;
180
Patrick Venture19044e12019-05-23 19:30:28 -0700181 std::vector<HandlerPack> blobs;
Patrick Ventured4e20de2019-07-18 12:48:05 -0700182
Patrick Venture19044e12019-05-23 19:30:28 -0700183 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Ventured4e20de2019-07-18 12:48:05 -0700184
Patrick Venture6d7735d2019-06-21 10:03:19 -0700185 TriggerMock* prepareMockPtr;
Patrick Venture1d66fe62019-06-03 14:57:27 -0700186 TriggerMock* verifyMockPtr;
187 TriggerMock* updateMockPtr;
Patrick Venture16ab2a12019-05-31 08:52:51 -0700188
189 std::uint16_t session = 1;
190 std::uint16_t flags =
Patrick Venture84778b82019-06-26 20:11:09 -0700191 blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::ipmi;
Patrick Venture9a69f732019-06-17 14:05:13 -0700192
Benjamin Fair12901982019-11-12 13:55:46 -0800193 blobs::BlobMeta expectedIdleMeta = {0xff00, 0, {}};
194
Patrick Venture9a69f732019-06-17 14:05:13 -0700195 std::vector<std::string> startingBlobs = {staticLayoutBlobId, hashBlobId};
Patrick Venture8a4f2aa2019-05-23 08:40:21 -0700196};
197
Patrick Venture1361a122019-05-20 07:36:05 -0700198class IpmiOnlyFirmwareTest : public ::testing::Test
199{
200 protected:
Patrick Ventured4e20de2019-07-18 12:48:05 -0700201 ImageHandlerMock *hashImageMock, *imageMock;
Patrick Venture1361a122019-05-20 07:36:05 -0700202 std::vector<HandlerPack> blobs;
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700203 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Venture1361a122019-05-20 07:36:05 -0700204
205 void SetUp() override
206 {
Patrick Ventured4e20de2019-07-18 12:48:05 -0700207 std::unique_ptr<ImageHandlerInterface> image =
208 std::make_unique<ImageHandlerMock>();
209 hashImageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
Patrick Venturec6ba8ff2020-09-23 12:42:57 -0700210 blobs.emplace_back(hashBlobId, std::move(image));
Patrick Ventured4e20de2019-07-18 12:48:05 -0700211
212 image = std::make_unique<ImageHandlerMock>();
213 imageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
Patrick Venturec6ba8ff2020-09-23 12:42:57 -0700214 blobs.emplace_back("asdf", std::move(image));
Patrick Ventured4e20de2019-07-18 12:48:05 -0700215
Patrick Venture4934daa2020-09-22 16:37:44 -0700216 std::vector<DataHandlerPack> data;
217 data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr);
218
Patrick Venture1361a122019-05-20 07:36:05 -0700219 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Ventureda4d4a42020-09-28 11:41:05 -0700220 std::move(blobs), std::move(data), CreateActionMap("asdf"));
Patrick Venture1361a122019-05-20 07:36:05 -0700221 }
222};
223
224class FakeLpcFirmwareTest : public ::testing::Test
225{
226 protected:
Patrick Venture4934daa2020-09-22 16:37:44 -0700227 DataHandlerMock* dataMock;
Patrick Ventured4e20de2019-07-18 12:48:05 -0700228 ImageHandlerMock *hashImageMock, *imageMock;
Patrick Venture1361a122019-05-20 07:36:05 -0700229 std::vector<HandlerPack> blobs;
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700230 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Venture1361a122019-05-20 07:36:05 -0700231
232 void SetUp() override
233 {
Patrick Ventured4e20de2019-07-18 12:48:05 -0700234 std::unique_ptr<ImageHandlerInterface> image =
235 std::make_unique<ImageHandlerMock>();
236 hashImageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
Patrick Venturec6ba8ff2020-09-23 12:42:57 -0700237 blobs.emplace_back(hashBlobId, std::move(image));
Patrick Ventured4e20de2019-07-18 12:48:05 -0700238
239 image = std::make_unique<ImageHandlerMock>();
240 imageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
Patrick Venturec6ba8ff2020-09-23 12:42:57 -0700241 blobs.emplace_back("asdf", std::move(image));
Patrick Ventured4e20de2019-07-18 12:48:05 -0700242
Patrick Venture4934daa2020-09-22 16:37:44 -0700243 auto dataMockInstance = std::make_unique<DataHandlerMock>();
244 dataMock = dataMockInstance.get();
245
246 std::vector<DataHandlerPack> data;
247 data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr);
248 data.emplace_back(FirmwareFlags::UpdateFlags::lpc,
249 std::move(dataMockInstance));
Patrick Venture1361a122019-05-20 07:36:05 -0700250 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Ventureda4d4a42020-09-28 11:41:05 -0700251 std::move(blobs), std::move(data), CreateActionMap("asdf"));
Patrick Venture1361a122019-05-20 07:36:05 -0700252 }
253};
254
Patrick Venture16ab2a12019-05-31 08:52:51 -0700255} // namespace
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700256} // namespace ipmi_flash