blob: 8d92fe819deb39696dd3fbd6ff4848cf29b802a0 [file] [log] [blame]
Patrick Venture1361a122019-05-20 07:36:05 -07001#pragma once
2
3#include "data_mock.hpp"
4#include "firmware_handler.hpp"
5#include "image_mock.hpp"
Patrick Venture1d66fe62019-06-03 14:57:27 -07006#include "triggerable_mock.hpp"
Patrick Venture1361a122019-05-20 07:36:05 -07007
8#include <memory>
Patrick Venture9a69f732019-06-17 14:05:13 -07009#include <string>
Patrick Venture1361a122019-05-20 07:36:05 -070010#include <vector>
11
12#include <gmock/gmock.h>
13#include <gtest/gtest.h>
14
Patrick Venture1d5a31c2019-05-20 11:38:22 -070015namespace ipmi_flash
Patrick Venture1361a122019-05-20 07:36:05 -070016{
Patrick Venture16ab2a12019-05-31 08:52:51 -070017namespace
18{
19
20using ::testing::Return;
Patrick Venture1361a122019-05-20 07:36:05 -070021
Patrick Venture8a4f2aa2019-05-23 08:40:21 -070022class IpmiOnlyFirmwareStaticTest : public ::testing::Test
23{
24 protected:
Patrick Venture8a4f2aa2019-05-23 08:40:21 -070025 void SetUp() override
26 {
27 blobs = {
28 {hashBlobId, &imageMock},
29 {staticLayoutBlobId, &imageMock},
30 };
Patrick Venture19044e12019-05-23 19:30:28 -070031
Patrick Venture6d7735d2019-06-21 10:03:19 -070032 std::unique_ptr<TriggerableActionInterface> prepareMock =
33 std::make_unique<TriggerMock>();
34 prepareMockPtr = reinterpret_cast<TriggerMock*>(prepareMock.get());
35
Patrick Venture1d66fe62019-06-03 14:57:27 -070036 std::unique_ptr<TriggerableActionInterface> verifyMock =
37 std::make_unique<TriggerMock>();
38 verifyMockPtr = reinterpret_cast<TriggerMock*>(verifyMock.get());
Patrick Venture19044e12019-05-23 19:30:28 -070039
Patrick Venture1d66fe62019-06-03 14:57:27 -070040 std::unique_ptr<TriggerableActionInterface> updateMock =
41 std::make_unique<TriggerMock>();
42 updateMockPtr = reinterpret_cast<TriggerMock*>(updateMock.get());
Patrick Venture1a406fe2019-05-31 07:29:56 -070043
Patrick Venture8a4f2aa2019-05-23 08:40:21 -070044 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture6d7735d2019-06-21 10:03:19 -070045 blobs, data, std::move(prepareMock), std::move(verifyMock),
46 std::move(updateMock));
Patrick Venture8a4f2aa2019-05-23 08:40:21 -070047 }
Patrick Venture19044e12019-05-23 19:30:28 -070048
Patrick Venture6fdd02e2019-05-28 13:02:04 -070049 void expectedState(FirmwareBlobHandler::UpdateState state)
50 {
51 auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
52 EXPECT_EQ(state, realHandler->getCurrentState());
53 }
54
Patrick Venture16ab2a12019-05-31 08:52:51 -070055 void openToInProgress(const std::string& blobId)
56 {
57 EXPECT_CALL(imageMock, open(blobId)).WillOnce(Return(true));
Patrick Venture6d7735d2019-06-21 10:03:19 -070058 EXPECT_CALL(*prepareMockPtr, trigger()).WillOnce(Return(true));
Patrick Venture16ab2a12019-05-31 08:52:51 -070059 EXPECT_TRUE(handler->open(session, flags, blobId));
60 expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
61 }
62
63 void getToVerificationPending(const std::string& blobId)
64 {
65 openToInProgress(blobId);
66
67 EXPECT_CALL(imageMock, close()).WillRepeatedly(Return());
68 handler->close(session);
69 expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
70 }
71
72 void getToVerificationStarted(const std::string& blobId)
73 {
74 getToVerificationPending(blobId);
75
76 EXPECT_TRUE(handler->open(session, flags, verifyBlobId));
Patrick Venture1d66fe62019-06-03 14:57:27 -070077 EXPECT_CALL(*verifyMockPtr, trigger()).WillOnce(Return(true));
Patrick Venture16ab2a12019-05-31 08:52:51 -070078
79 EXPECT_TRUE(handler->commit(session, {}));
80 expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
81 }
82
Patrick Ventureda66fd82019-06-03 11:11:24 -070083 void getToVerificationCompleted(ActionStatus checkResponse)
Patrick Venture16ab2a12019-05-31 08:52:51 -070084 {
85 getToVerificationStarted(staticLayoutBlobId);
86
Patrick Venturef1f0f652019-06-03 09:10:19 -070087 EXPECT_CALL(*verifyMockPtr, status()).WillOnce(Return(checkResponse));
Patrick Venture16ab2a12019-05-31 08:52:51 -070088 blobs::BlobMeta meta;
89 EXPECT_TRUE(handler->stat(session, &meta));
90 expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
91 }
92
93 void getToUpdatePending()
94 {
Patrick Ventureda66fd82019-06-03 11:11:24 -070095 getToVerificationCompleted(ActionStatus::success);
Patrick Venture16ab2a12019-05-31 08:52:51 -070096
97 handler->close(session);
98 expectedState(FirmwareBlobHandler::UpdateState::updatePending);
99 }
100
101 void getToUpdateStarted()
102 {
103 getToUpdatePending();
104 EXPECT_TRUE(handler->open(session, flags, updateBlobId));
105
Patrick Venture1d66fe62019-06-03 14:57:27 -0700106 EXPECT_CALL(*updateMockPtr, trigger()).WillOnce(Return(true));
Patrick Venture16ab2a12019-05-31 08:52:51 -0700107 EXPECT_TRUE(handler->commit(session, {}));
108 expectedState(FirmwareBlobHandler::UpdateState::updateStarted);
109 }
110
Patrick Ventureda66fd82019-06-03 11:11:24 -0700111 void getToUpdateCompleted(ActionStatus result)
Patrick Ventureab1e9622019-06-03 10:45:06 -0700112 {
113 getToUpdateStarted();
114 EXPECT_CALL(*updateMockPtr, status()).WillOnce(Return(result));
115
116 blobs::BlobMeta meta;
117 EXPECT_TRUE(handler->stat(session, &meta));
118 expectedState(FirmwareBlobHandler::UpdateState::updateCompleted);
119 }
120
Patrick Venture19044e12019-05-23 19:30:28 -0700121 ImageHandlerMock imageMock;
122 std::vector<HandlerPack> blobs;
123 std::vector<DataHandlerPack> data = {
124 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}};
125 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Venture6d7735d2019-06-21 10:03:19 -0700126 TriggerMock* prepareMockPtr;
Patrick Venture1d66fe62019-06-03 14:57:27 -0700127 TriggerMock* verifyMockPtr;
128 TriggerMock* updateMockPtr;
Patrick Venture16ab2a12019-05-31 08:52:51 -0700129
130 std::uint16_t session = 1;
131 std::uint16_t flags =
132 blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi;
Patrick Venture9a69f732019-06-17 14:05:13 -0700133
134 std::vector<std::string> startingBlobs = {staticLayoutBlobId, hashBlobId};
Patrick Venture8a4f2aa2019-05-23 08:40:21 -0700135};
136
Patrick Venture1361a122019-05-20 07:36:05 -0700137class IpmiOnlyFirmwareTest : public ::testing::Test
138{
139 protected:
140 ImageHandlerMock imageMock;
141 std::vector<HandlerPack> blobs;
142 std::vector<DataHandlerPack> data = {
143 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}};
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700144 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Venture1361a122019-05-20 07:36:05 -0700145
146 void SetUp() override
147 {
148 blobs = {
149 {hashBlobId, &imageMock},
150 {"asdf", &imageMock},
151 };
152 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture6d7735d2019-06-21 10:03:19 -0700153 blobs, data, CreateTriggerMock(), CreateTriggerMock(),
154 CreateTriggerMock());
Patrick Venture1361a122019-05-20 07:36:05 -0700155 }
156};
157
158class FakeLpcFirmwareTest : public ::testing::Test
159{
160 protected:
161 DataHandlerMock dataMock;
162 ImageHandlerMock imageMock;
163 std::vector<HandlerPack> blobs;
164 std::vector<DataHandlerPack> data;
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700165 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Venture1361a122019-05-20 07:36:05 -0700166
167 void SetUp() override
168 {
169 blobs = {
170 {hashBlobId, &imageMock},
171 {"asdf", &imageMock},
172 };
173 data = {
174 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
175 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
176 };
177 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture6d7735d2019-06-21 10:03:19 -0700178 blobs, data, CreateTriggerMock(), CreateTriggerMock(),
179 CreateTriggerMock());
Patrick Venture1361a122019-05-20 07:36:05 -0700180 }
181};
182
Patrick Venture16ab2a12019-05-31 08:52:51 -0700183} // namespace
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700184} // namespace ipmi_flash