blob: 445523c1bb4456b76eaa1ccac0b6c4784920c9cb [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 Venture1361a122019-05-20 07:36:05 -07008
9#include <memory>
Patrick Venture9a69f732019-06-17 14:05:13 -070010#include <string>
Patrick Venturefa06a5f2019-07-01 09:22:38 -070011#include <unordered_map>
Patrick Venture1361a122019-05-20 07:36:05 -070012#include <vector>
13
14#include <gmock/gmock.h>
15#include <gtest/gtest.h>
16
Patrick Venture1d5a31c2019-05-20 11:38:22 -070017namespace ipmi_flash
Patrick Venture1361a122019-05-20 07:36:05 -070018{
Patrick Venture16ab2a12019-05-31 08:52:51 -070019namespace
20{
21
22using ::testing::Return;
Patrick Venture1361a122019-05-20 07:36:05 -070023
Patrick Venture8a4f2aa2019-05-23 08:40:21 -070024class IpmiOnlyFirmwareStaticTest : public ::testing::Test
25{
26 protected:
Patrick Venture8a4f2aa2019-05-23 08:40:21 -070027 void SetUp() override
28 {
Patrick Ventured4e20de2019-07-18 12:48:05 -070029 /* 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());
Patrick Venturec6ba8ff2020-09-23 12:42:57 -070035 blobs.emplace_back(hashBlobId, std::move(image));
Patrick Ventured4e20de2019-07-18 12:48:05 -070036
37 image = std::make_unique<ImageHandlerMock>();
38 imageMock2 = reinterpret_cast<ImageHandlerMock*>(image.get());
Patrick Venturec6ba8ff2020-09-23 12:42:57 -070039 blobs.emplace_back(staticLayoutBlobId, std::move(image));
Patrick Venture19044e12019-05-23 19:30:28 -070040
Patrick Venture6d7735d2019-06-21 10:03:19 -070041 std::unique_ptr<TriggerableActionInterface> prepareMock =
42 std::make_unique<TriggerMock>();
43 prepareMockPtr = reinterpret_cast<TriggerMock*>(prepareMock.get());
44
Patrick Venture1d66fe62019-06-03 14:57:27 -070045 std::unique_ptr<TriggerableActionInterface> verifyMock =
46 std::make_unique<TriggerMock>();
47 verifyMockPtr = reinterpret_cast<TriggerMock*>(verifyMock.get());
Patrick Venture19044e12019-05-23 19:30:28 -070048
Patrick Venture1d66fe62019-06-03 14:57:27 -070049 std::unique_ptr<TriggerableActionInterface> updateMock =
50 std::make_unique<TriggerMock>();
51 updateMockPtr = reinterpret_cast<TriggerMock*>(updateMock.get());
Patrick Venture1a406fe2019-05-31 07:29:56 -070052
Patrick Venturefa06a5f2019-07-01 09:22:38 -070053 std::unique_ptr<ActionPack> actionPack = std::make_unique<ActionPack>();
54 actionPack->preparation = std::move(prepareMock);
55 actionPack->verification = std::move(verifyMock);
56 actionPack->update = std::move(updateMock);
57
58 ActionMap packs;
59 packs[staticLayoutBlobId] = std::move(actionPack);
60
Patrick Venture4934daa2020-09-22 16:37:44 -070061 std::vector<DataHandlerPack> data;
62 data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr);
63
Patrick Venture8a4f2aa2019-05-23 08:40:21 -070064 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture7b783432020-09-22 15:55:08 -070065 std::move(blobs), std::move(data), std::move(packs));
Patrick Venture8a4f2aa2019-05-23 08:40:21 -070066 }
Patrick Venture19044e12019-05-23 19:30:28 -070067
Patrick Venture6fdd02e2019-05-28 13:02:04 -070068 void expectedState(FirmwareBlobHandler::UpdateState state)
69 {
70 auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
71 EXPECT_EQ(state, realHandler->getCurrentState());
72 }
73
Patrick Venture16ab2a12019-05-31 08:52:51 -070074 void openToInProgress(const std::string& blobId)
75 {
Patrick Ventured4e20de2019-07-18 12:48:05 -070076 if (blobId == hashBlobId)
77 {
78 EXPECT_CALL(*hashImageMock, open(blobId)).WillOnce(Return(true));
79 }
80 else
81 {
82 EXPECT_CALL(*imageMock2, open(blobId)).WillOnce(Return(true));
83 }
84
Patrick Venturefa06a5f2019-07-01 09:22:38 -070085 if (blobId != hashBlobId)
86 {
87 EXPECT_CALL(*prepareMockPtr, trigger()).WillOnce(Return(true));
88 }
Patrick Venture16ab2a12019-05-31 08:52:51 -070089 EXPECT_TRUE(handler->open(session, flags, blobId));
90 expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
91 }
92
93 void getToVerificationPending(const std::string& blobId)
94 {
95 openToInProgress(blobId);
96
Patrick Ventured4e20de2019-07-18 12:48:05 -070097 if (blobId == hashBlobId)
98 {
99 EXPECT_CALL(*hashImageMock, close()).WillRepeatedly(Return());
100 }
101 else
102 {
103 EXPECT_CALL(*imageMock2, close()).WillRepeatedly(Return());
104 }
Patrick Venture16ab2a12019-05-31 08:52:51 -0700105 handler->close(session);
106 expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
107 }
108
109 void getToVerificationStarted(const std::string& blobId)
110 {
111 getToVerificationPending(blobId);
112
113 EXPECT_TRUE(handler->open(session, flags, verifyBlobId));
Patrick Venture1d66fe62019-06-03 14:57:27 -0700114 EXPECT_CALL(*verifyMockPtr, trigger()).WillOnce(Return(true));
Patrick Venture16ab2a12019-05-31 08:52:51 -0700115
116 EXPECT_TRUE(handler->commit(session, {}));
117 expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
118 }
119
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700120 void getToVerificationStartedWitHashBlob()
121 {
122 /* Open both static and hash to check for activeHashBlobId. */
123 getToVerificationPending(staticLayoutBlobId);
124
125 openToInProgress(hashBlobId);
Patrick Ventured4e20de2019-07-18 12:48:05 -0700126 EXPECT_CALL(*hashImageMock, close()).WillRepeatedly(Return());
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700127 handler->close(session);
128 expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
129
130 /* Now the hash is active AND the static image is active. */
131 EXPECT_TRUE(handler->open(session, flags, verifyBlobId));
132 EXPECT_CALL(*verifyMockPtr, trigger()).WillOnce(Return(true));
133
134 EXPECT_TRUE(handler->commit(session, {}));
135 expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
136 }
137
Patrick Ventureda66fd82019-06-03 11:11:24 -0700138 void getToVerificationCompleted(ActionStatus checkResponse)
Patrick Venture16ab2a12019-05-31 08:52:51 -0700139 {
140 getToVerificationStarted(staticLayoutBlobId);
141
Patrick Venturef1f0f652019-06-03 09:10:19 -0700142 EXPECT_CALL(*verifyMockPtr, status()).WillOnce(Return(checkResponse));
Patrick Venture16ab2a12019-05-31 08:52:51 -0700143 blobs::BlobMeta meta;
144 EXPECT_TRUE(handler->stat(session, &meta));
145 expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
146 }
147
148 void getToUpdatePending()
149 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700150 getToVerificationCompleted(ActionStatus::success);
Patrick Venture16ab2a12019-05-31 08:52:51 -0700151
152 handler->close(session);
153 expectedState(FirmwareBlobHandler::UpdateState::updatePending);
154 }
155
156 void getToUpdateStarted()
157 {
158 getToUpdatePending();
159 EXPECT_TRUE(handler->open(session, flags, updateBlobId));
160
Patrick Venture1d66fe62019-06-03 14:57:27 -0700161 EXPECT_CALL(*updateMockPtr, trigger()).WillOnce(Return(true));
Patrick Venture16ab2a12019-05-31 08:52:51 -0700162 EXPECT_TRUE(handler->commit(session, {}));
163 expectedState(FirmwareBlobHandler::UpdateState::updateStarted);
164 }
165
Patrick Ventureda66fd82019-06-03 11:11:24 -0700166 void getToUpdateCompleted(ActionStatus result)
Patrick Ventureab1e9622019-06-03 10:45:06 -0700167 {
168 getToUpdateStarted();
169 EXPECT_CALL(*updateMockPtr, status()).WillOnce(Return(result));
170
171 blobs::BlobMeta meta;
172 EXPECT_TRUE(handler->stat(session, &meta));
173 expectedState(FirmwareBlobHandler::UpdateState::updateCompleted);
174 }
175
Patrick Ventured4e20de2019-07-18 12:48:05 -0700176 ImageHandlerMock *hashImageMock, *imageMock2;
177
Patrick Venture19044e12019-05-23 19:30:28 -0700178 std::vector<HandlerPack> blobs;
Patrick Ventured4e20de2019-07-18 12:48:05 -0700179
Patrick Venture19044e12019-05-23 19:30:28 -0700180 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Ventured4e20de2019-07-18 12:48:05 -0700181
Patrick Venture6d7735d2019-06-21 10:03:19 -0700182 TriggerMock* prepareMockPtr;
Patrick Venture1d66fe62019-06-03 14:57:27 -0700183 TriggerMock* verifyMockPtr;
184 TriggerMock* updateMockPtr;
Patrick Venture16ab2a12019-05-31 08:52:51 -0700185
186 std::uint16_t session = 1;
187 std::uint16_t flags =
Patrick Venture84778b82019-06-26 20:11:09 -0700188 blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::ipmi;
Patrick Venture9a69f732019-06-17 14:05:13 -0700189
Benjamin Fair12901982019-11-12 13:55:46 -0800190 blobs::BlobMeta expectedIdleMeta = {0xff00, 0, {}};
191
Patrick Venture9a69f732019-06-17 14:05:13 -0700192 std::vector<std::string> startingBlobs = {staticLayoutBlobId, hashBlobId};
Patrick Venture8a4f2aa2019-05-23 08:40:21 -0700193};
194
Patrick Venture1361a122019-05-20 07:36:05 -0700195class IpmiOnlyFirmwareTest : public ::testing::Test
196{
197 protected:
Patrick Ventured4e20de2019-07-18 12:48:05 -0700198 ImageHandlerMock *hashImageMock, *imageMock;
Patrick Venture1361a122019-05-20 07:36:05 -0700199 std::vector<HandlerPack> blobs;
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700200 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Venture1361a122019-05-20 07:36:05 -0700201
202 void SetUp() override
203 {
Patrick Ventured4e20de2019-07-18 12:48:05 -0700204 std::unique_ptr<ImageHandlerInterface> image =
205 std::make_unique<ImageHandlerMock>();
206 hashImageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
Patrick Venturec6ba8ff2020-09-23 12:42:57 -0700207 blobs.emplace_back(hashBlobId, std::move(image));
Patrick Ventured4e20de2019-07-18 12:48:05 -0700208
209 image = std::make_unique<ImageHandlerMock>();
210 imageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
Patrick Venturec6ba8ff2020-09-23 12:42:57 -0700211 blobs.emplace_back("asdf", std::move(image));
Patrick Ventured4e20de2019-07-18 12:48:05 -0700212
Patrick Venture4934daa2020-09-22 16:37:44 -0700213 std::vector<DataHandlerPack> data;
214 data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr);
215
Patrick Venture1361a122019-05-20 07:36:05 -0700216 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Ventureda4d4a42020-09-28 11:41:05 -0700217 std::move(blobs), std::move(data), CreateActionMap("asdf"));
Patrick Venture1361a122019-05-20 07:36:05 -0700218 }
219};
220
221class FakeLpcFirmwareTest : public ::testing::Test
222{
223 protected:
Patrick Venture4934daa2020-09-22 16:37:44 -0700224 DataHandlerMock* dataMock;
Patrick Ventured4e20de2019-07-18 12:48:05 -0700225 ImageHandlerMock *hashImageMock, *imageMock;
Patrick Venture1361a122019-05-20 07:36:05 -0700226 std::vector<HandlerPack> blobs;
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700227 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Venture1361a122019-05-20 07:36:05 -0700228
229 void SetUp() override
230 {
Patrick Ventured4e20de2019-07-18 12:48:05 -0700231 std::unique_ptr<ImageHandlerInterface> image =
232 std::make_unique<ImageHandlerMock>();
233 hashImageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
Patrick Venturec6ba8ff2020-09-23 12:42:57 -0700234 blobs.emplace_back(hashBlobId, std::move(image));
Patrick Ventured4e20de2019-07-18 12:48:05 -0700235
236 image = std::make_unique<ImageHandlerMock>();
237 imageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
Patrick Venturec6ba8ff2020-09-23 12:42:57 -0700238 blobs.emplace_back("asdf", std::move(image));
Patrick Ventured4e20de2019-07-18 12:48:05 -0700239
Patrick Venture4934daa2020-09-22 16:37:44 -0700240 auto dataMockInstance = std::make_unique<DataHandlerMock>();
241 dataMock = dataMockInstance.get();
242
243 std::vector<DataHandlerPack> data;
244 data.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr);
245 data.emplace_back(FirmwareFlags::UpdateFlags::lpc,
246 std::move(dataMockInstance));
Patrick Venture1361a122019-05-20 07:36:05 -0700247 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Ventureda4d4a42020-09-28 11:41:05 -0700248 std::move(blobs), std::move(data), CreateActionMap("asdf"));
Patrick Venture1361a122019-05-20 07:36:05 -0700249 }
250};
251
Patrick Venture16ab2a12019-05-31 08:52:51 -0700252} // namespace
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700253} // namespace ipmi_flash