blob: 3d7fa4e3ce8534a4725e23d97a821168001235be [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());
35 blobs.push_back(std::move(HandlerPack(hashBlobId, std::move(image))));
36
37 image = std::make_unique<ImageHandlerMock>();
38 imageMock2 = reinterpret_cast<ImageHandlerMock*>(image.get());
39 blobs.push_back(
40 std::move(HandlerPack(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 Venture8a4f2aa2019-05-23 08:40:21 -070062 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Ventured4e20de2019-07-18 12:48:05 -070063 std::move(blobs), data, std::move(packs));
Patrick Venture8a4f2aa2019-05-23 08:40:21 -070064 }
Patrick Venture19044e12019-05-23 19:30:28 -070065
Patrick Venture6fdd02e2019-05-28 13:02:04 -070066 void expectedState(FirmwareBlobHandler::UpdateState state)
67 {
68 auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
69 EXPECT_EQ(state, realHandler->getCurrentState());
70 }
71
Patrick Venture16ab2a12019-05-31 08:52:51 -070072 void openToInProgress(const std::string& blobId)
73 {
Patrick Ventured4e20de2019-07-18 12:48:05 -070074 if (blobId == hashBlobId)
75 {
76 EXPECT_CALL(*hashImageMock, open(blobId)).WillOnce(Return(true));
77 }
78 else
79 {
80 EXPECT_CALL(*imageMock2, open(blobId)).WillOnce(Return(true));
81 }
82
Patrick Venturefa06a5f2019-07-01 09:22:38 -070083 if (blobId != hashBlobId)
84 {
85 EXPECT_CALL(*prepareMockPtr, trigger()).WillOnce(Return(true));
86 }
Patrick Venture16ab2a12019-05-31 08:52:51 -070087 EXPECT_TRUE(handler->open(session, flags, blobId));
88 expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
89 }
90
91 void getToVerificationPending(const std::string& blobId)
92 {
93 openToInProgress(blobId);
94
Patrick Ventured4e20de2019-07-18 12:48:05 -070095 if (blobId == hashBlobId)
96 {
97 EXPECT_CALL(*hashImageMock, close()).WillRepeatedly(Return());
98 }
99 else
100 {
101 EXPECT_CALL(*imageMock2, close()).WillRepeatedly(Return());
102 }
Patrick Venture16ab2a12019-05-31 08:52:51 -0700103 handler->close(session);
104 expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
105 }
106
107 void getToVerificationStarted(const std::string& blobId)
108 {
109 getToVerificationPending(blobId);
110
111 EXPECT_TRUE(handler->open(session, flags, verifyBlobId));
Patrick Venture1d66fe62019-06-03 14:57:27 -0700112 EXPECT_CALL(*verifyMockPtr, trigger()).WillOnce(Return(true));
Patrick Venture16ab2a12019-05-31 08:52:51 -0700113
114 EXPECT_TRUE(handler->commit(session, {}));
115 expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
116 }
117
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700118 void getToVerificationStartedWitHashBlob()
119 {
120 /* Open both static and hash to check for activeHashBlobId. */
121 getToVerificationPending(staticLayoutBlobId);
122
123 openToInProgress(hashBlobId);
Patrick Ventured4e20de2019-07-18 12:48:05 -0700124 EXPECT_CALL(*hashImageMock, close()).WillRepeatedly(Return());
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700125 handler->close(session);
126 expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
127
128 /* Now the hash is active AND the static image is active. */
129 EXPECT_TRUE(handler->open(session, flags, verifyBlobId));
130 EXPECT_CALL(*verifyMockPtr, trigger()).WillOnce(Return(true));
131
132 EXPECT_TRUE(handler->commit(session, {}));
133 expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
134 }
135
Patrick Ventureda66fd82019-06-03 11:11:24 -0700136 void getToVerificationCompleted(ActionStatus checkResponse)
Patrick Venture16ab2a12019-05-31 08:52:51 -0700137 {
138 getToVerificationStarted(staticLayoutBlobId);
139
Patrick Venturef1f0f652019-06-03 09:10:19 -0700140 EXPECT_CALL(*verifyMockPtr, status()).WillOnce(Return(checkResponse));
Patrick Venture16ab2a12019-05-31 08:52:51 -0700141 blobs::BlobMeta meta;
142 EXPECT_TRUE(handler->stat(session, &meta));
143 expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
144 }
145
146 void getToUpdatePending()
147 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700148 getToVerificationCompleted(ActionStatus::success);
Patrick Venture16ab2a12019-05-31 08:52:51 -0700149
150 handler->close(session);
151 expectedState(FirmwareBlobHandler::UpdateState::updatePending);
152 }
153
154 void getToUpdateStarted()
155 {
156 getToUpdatePending();
157 EXPECT_TRUE(handler->open(session, flags, updateBlobId));
158
Patrick Venture1d66fe62019-06-03 14:57:27 -0700159 EXPECT_CALL(*updateMockPtr, trigger()).WillOnce(Return(true));
Patrick Venture16ab2a12019-05-31 08:52:51 -0700160 EXPECT_TRUE(handler->commit(session, {}));
161 expectedState(FirmwareBlobHandler::UpdateState::updateStarted);
162 }
163
Patrick Ventureda66fd82019-06-03 11:11:24 -0700164 void getToUpdateCompleted(ActionStatus result)
Patrick Ventureab1e9622019-06-03 10:45:06 -0700165 {
166 getToUpdateStarted();
167 EXPECT_CALL(*updateMockPtr, status()).WillOnce(Return(result));
168
169 blobs::BlobMeta meta;
170 EXPECT_TRUE(handler->stat(session, &meta));
171 expectedState(FirmwareBlobHandler::UpdateState::updateCompleted);
172 }
173
Patrick Ventured4e20de2019-07-18 12:48:05 -0700174 ImageHandlerMock *hashImageMock, *imageMock2;
175
Patrick Venture19044e12019-05-23 19:30:28 -0700176 std::vector<HandlerPack> blobs;
177 std::vector<DataHandlerPack> data = {
Patrick Venture84778b82019-06-26 20:11:09 -0700178 {FirmwareFlags::UpdateFlags::ipmi, nullptr}};
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;
200 std::vector<DataHandlerPack> data = {
Patrick Venture84778b82019-06-26 20:11:09 -0700201 {FirmwareFlags::UpdateFlags::ipmi, nullptr}};
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700202 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Venture1361a122019-05-20 07:36:05 -0700203
204 void SetUp() override
205 {
Patrick Ventured4e20de2019-07-18 12:48:05 -0700206 std::unique_ptr<ImageHandlerInterface> image =
207 std::make_unique<ImageHandlerMock>();
208 hashImageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
209 blobs.push_back(std::move(HandlerPack(hashBlobId, std::move(image))));
210
211 image = std::make_unique<ImageHandlerMock>();
212 imageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
213 blobs.push_back(std::move(HandlerPack("asdf", std::move(image))));
214
Patrick Venture1361a122019-05-20 07:36:05 -0700215 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Ventured4e20de2019-07-18 12:48:05 -0700216 std::move(blobs), data, std::move(CreateActionMap("asdf")));
Patrick Venture1361a122019-05-20 07:36:05 -0700217 }
218};
219
220class FakeLpcFirmwareTest : public ::testing::Test
221{
222 protected:
223 DataHandlerMock dataMock;
Patrick Ventured4e20de2019-07-18 12:48:05 -0700224 ImageHandlerMock *hashImageMock, *imageMock;
Patrick Venture1361a122019-05-20 07:36:05 -0700225 std::vector<HandlerPack> blobs;
226 std::vector<DataHandlerPack> data;
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());
234 blobs.push_back(std::move(HandlerPack(hashBlobId, std::move(image))));
235
236 image = std::make_unique<ImageHandlerMock>();
237 imageMock = reinterpret_cast<ImageHandlerMock*>(image.get());
238 blobs.push_back(std::move(HandlerPack("asdf", std::move(image))));
239
Patrick Venture1361a122019-05-20 07:36:05 -0700240 data = {
Patrick Venture84778b82019-06-26 20:11:09 -0700241 {FirmwareFlags::UpdateFlags::ipmi, nullptr},
242 {FirmwareFlags::UpdateFlags::lpc, &dataMock},
Patrick Venture1361a122019-05-20 07:36:05 -0700243 };
244 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Ventured4e20de2019-07-18 12:48:05 -0700245 std::move(blobs), data, std::move(CreateActionMap("asdf")));
Patrick Venture1361a122019-05-20 07:36:05 -0700246 }
247};
248
Patrick Venture16ab2a12019-05-31 08:52:51 -0700249} // namespace
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700250} // namespace ipmi_flash