blob: 129b940332fdfa32d0144364f038a746bcbcff55 [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 {
29 blobs = {
30 {hashBlobId, &imageMock},
31 {staticLayoutBlobId, &imageMock},
32 };
Patrick Venture19044e12019-05-23 19:30:28 -070033
Patrick Venture6d7735d2019-06-21 10:03:19 -070034 std::unique_ptr<TriggerableActionInterface> prepareMock =
35 std::make_unique<TriggerMock>();
36 prepareMockPtr = reinterpret_cast<TriggerMock*>(prepareMock.get());
37
Patrick Venture1d66fe62019-06-03 14:57:27 -070038 std::unique_ptr<TriggerableActionInterface> verifyMock =
39 std::make_unique<TriggerMock>();
40 verifyMockPtr = reinterpret_cast<TriggerMock*>(verifyMock.get());
Patrick Venture19044e12019-05-23 19:30:28 -070041
Patrick Venture1d66fe62019-06-03 14:57:27 -070042 std::unique_ptr<TriggerableActionInterface> updateMock =
43 std::make_unique<TriggerMock>();
44 updateMockPtr = reinterpret_cast<TriggerMock*>(updateMock.get());
Patrick Venture1a406fe2019-05-31 07:29:56 -070045
Patrick Venturefa06a5f2019-07-01 09:22:38 -070046 std::unique_ptr<ActionPack> actionPack = std::make_unique<ActionPack>();
47 actionPack->preparation = std::move(prepareMock);
48 actionPack->verification = std::move(verifyMock);
49 actionPack->update = std::move(updateMock);
50
51 ActionMap packs;
52 packs[staticLayoutBlobId] = std::move(actionPack);
53
Patrick Venture8a4f2aa2019-05-23 08:40:21 -070054 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venturefa06a5f2019-07-01 09:22:38 -070055 blobs, data, std::move(packs));
Patrick Venture8a4f2aa2019-05-23 08:40:21 -070056 }
Patrick Venture19044e12019-05-23 19:30:28 -070057
Patrick Venture6fdd02e2019-05-28 13:02:04 -070058 void expectedState(FirmwareBlobHandler::UpdateState state)
59 {
60 auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
61 EXPECT_EQ(state, realHandler->getCurrentState());
62 }
63
Patrick Venture16ab2a12019-05-31 08:52:51 -070064 void openToInProgress(const std::string& blobId)
65 {
66 EXPECT_CALL(imageMock, open(blobId)).WillOnce(Return(true));
Patrick Venturefa06a5f2019-07-01 09:22:38 -070067 if (blobId != hashBlobId)
68 {
69 EXPECT_CALL(*prepareMockPtr, trigger()).WillOnce(Return(true));
70 }
Patrick Venture16ab2a12019-05-31 08:52:51 -070071 EXPECT_TRUE(handler->open(session, flags, blobId));
72 expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
73 }
74
75 void getToVerificationPending(const std::string& blobId)
76 {
77 openToInProgress(blobId);
78
79 EXPECT_CALL(imageMock, close()).WillRepeatedly(Return());
80 handler->close(session);
81 expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
82 }
83
84 void getToVerificationStarted(const std::string& blobId)
85 {
86 getToVerificationPending(blobId);
87
88 EXPECT_TRUE(handler->open(session, flags, verifyBlobId));
Patrick Venture1d66fe62019-06-03 14:57:27 -070089 EXPECT_CALL(*verifyMockPtr, trigger()).WillOnce(Return(true));
Patrick Venture16ab2a12019-05-31 08:52:51 -070090
91 EXPECT_TRUE(handler->commit(session, {}));
92 expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
93 }
94
Patrick Venturefa06a5f2019-07-01 09:22:38 -070095 void getToVerificationStartedWitHashBlob()
96 {
97 /* Open both static and hash to check for activeHashBlobId. */
98 getToVerificationPending(staticLayoutBlobId);
99
100 openToInProgress(hashBlobId);
101 EXPECT_CALL(imageMock, close()).WillRepeatedly(Return());
102 handler->close(session);
103 expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
104
105 /* Now the hash is active AND the static image is active. */
106 EXPECT_TRUE(handler->open(session, flags, verifyBlobId));
107 EXPECT_CALL(*verifyMockPtr, trigger()).WillOnce(Return(true));
108
109 EXPECT_TRUE(handler->commit(session, {}));
110 expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
111 }
112
Patrick Ventureda66fd82019-06-03 11:11:24 -0700113 void getToVerificationCompleted(ActionStatus checkResponse)
Patrick Venture16ab2a12019-05-31 08:52:51 -0700114 {
115 getToVerificationStarted(staticLayoutBlobId);
116
Patrick Venturef1f0f652019-06-03 09:10:19 -0700117 EXPECT_CALL(*verifyMockPtr, status()).WillOnce(Return(checkResponse));
Patrick Venture16ab2a12019-05-31 08:52:51 -0700118 blobs::BlobMeta meta;
119 EXPECT_TRUE(handler->stat(session, &meta));
120 expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
121 }
122
123 void getToUpdatePending()
124 {
Patrick Ventureda66fd82019-06-03 11:11:24 -0700125 getToVerificationCompleted(ActionStatus::success);
Patrick Venture16ab2a12019-05-31 08:52:51 -0700126
127 handler->close(session);
128 expectedState(FirmwareBlobHandler::UpdateState::updatePending);
129 }
130
131 void getToUpdateStarted()
132 {
133 getToUpdatePending();
134 EXPECT_TRUE(handler->open(session, flags, updateBlobId));
135
Patrick Venture1d66fe62019-06-03 14:57:27 -0700136 EXPECT_CALL(*updateMockPtr, trigger()).WillOnce(Return(true));
Patrick Venture16ab2a12019-05-31 08:52:51 -0700137 EXPECT_TRUE(handler->commit(session, {}));
138 expectedState(FirmwareBlobHandler::UpdateState::updateStarted);
139 }
140
Patrick Ventureda66fd82019-06-03 11:11:24 -0700141 void getToUpdateCompleted(ActionStatus result)
Patrick Ventureab1e9622019-06-03 10:45:06 -0700142 {
143 getToUpdateStarted();
144 EXPECT_CALL(*updateMockPtr, status()).WillOnce(Return(result));
145
146 blobs::BlobMeta meta;
147 EXPECT_TRUE(handler->stat(session, &meta));
148 expectedState(FirmwareBlobHandler::UpdateState::updateCompleted);
149 }
150
Patrick Venture19044e12019-05-23 19:30:28 -0700151 ImageHandlerMock imageMock;
152 std::vector<HandlerPack> blobs;
153 std::vector<DataHandlerPack> data = {
Patrick Venture84778b82019-06-26 20:11:09 -0700154 {FirmwareFlags::UpdateFlags::ipmi, nullptr}};
Patrick Venture19044e12019-05-23 19:30:28 -0700155 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Venture6d7735d2019-06-21 10:03:19 -0700156 TriggerMock* prepareMockPtr;
Patrick Venture1d66fe62019-06-03 14:57:27 -0700157 TriggerMock* verifyMockPtr;
158 TriggerMock* updateMockPtr;
Patrick Venture16ab2a12019-05-31 08:52:51 -0700159
160 std::uint16_t session = 1;
161 std::uint16_t flags =
Patrick Venture84778b82019-06-26 20:11:09 -0700162 blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::ipmi;
Patrick Venture9a69f732019-06-17 14:05:13 -0700163
164 std::vector<std::string> startingBlobs = {staticLayoutBlobId, hashBlobId};
Patrick Venture8a4f2aa2019-05-23 08:40:21 -0700165};
166
Patrick Venture1361a122019-05-20 07:36:05 -0700167class IpmiOnlyFirmwareTest : public ::testing::Test
168{
169 protected:
170 ImageHandlerMock imageMock;
171 std::vector<HandlerPack> blobs;
172 std::vector<DataHandlerPack> data = {
Patrick Venture84778b82019-06-26 20:11:09 -0700173 {FirmwareFlags::UpdateFlags::ipmi, nullptr}};
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700174 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Venture1361a122019-05-20 07:36:05 -0700175
176 void SetUp() override
177 {
178 blobs = {
179 {hashBlobId, &imageMock},
180 {"asdf", &imageMock},
181 };
182 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700183 blobs, data, std::move(CreateActionMap("asdf")));
Patrick Venture1361a122019-05-20 07:36:05 -0700184 }
185};
186
187class FakeLpcFirmwareTest : public ::testing::Test
188{
189 protected:
190 DataHandlerMock dataMock;
191 ImageHandlerMock imageMock;
192 std::vector<HandlerPack> blobs;
193 std::vector<DataHandlerPack> data;
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700194 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Venture1361a122019-05-20 07:36:05 -0700195
196 void SetUp() override
197 {
198 blobs = {
199 {hashBlobId, &imageMock},
200 {"asdf", &imageMock},
201 };
202 data = {
Patrick Venture84778b82019-06-26 20:11:09 -0700203 {FirmwareFlags::UpdateFlags::ipmi, nullptr},
204 {FirmwareFlags::UpdateFlags::lpc, &dataMock},
Patrick Venture1361a122019-05-20 07:36:05 -0700205 };
206 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venturefa06a5f2019-07-01 09:22:38 -0700207 blobs, data, std::move(CreateActionMap("asdf")));
Patrick Venture1361a122019-05-20 07:36:05 -0700208 }
209};
210
Patrick Venture16ab2a12019-05-31 08:52:51 -0700211} // namespace
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700212} // namespace ipmi_flash