blob: a79cd4fe001f57d57ed75745e4ea89659c1e865b [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>
9#include <vector>
10
11#include <gmock/gmock.h>
12#include <gtest/gtest.h>
13
Patrick Venture1d5a31c2019-05-20 11:38:22 -070014namespace ipmi_flash
Patrick Venture1361a122019-05-20 07:36:05 -070015{
Patrick Venture16ab2a12019-05-31 08:52:51 -070016namespace
17{
18
19using ::testing::Return;
Patrick Venture1361a122019-05-20 07:36:05 -070020
Patrick Venture8a4f2aa2019-05-23 08:40:21 -070021class IpmiOnlyFirmwareStaticTest : public ::testing::Test
22{
23 protected:
Patrick Venture8a4f2aa2019-05-23 08:40:21 -070024 void SetUp() override
25 {
26 blobs = {
27 {hashBlobId, &imageMock},
28 {staticLayoutBlobId, &imageMock},
29 };
Patrick Venture19044e12019-05-23 19:30:28 -070030
Patrick Venture1d66fe62019-06-03 14:57:27 -070031 std::unique_ptr<TriggerableActionInterface> verifyMock =
32 std::make_unique<TriggerMock>();
33 verifyMockPtr = reinterpret_cast<TriggerMock*>(verifyMock.get());
Patrick Venture19044e12019-05-23 19:30:28 -070034
Patrick Venture1d66fe62019-06-03 14:57:27 -070035 std::unique_ptr<TriggerableActionInterface> updateMock =
36 std::make_unique<TriggerMock>();
37 updateMockPtr = reinterpret_cast<TriggerMock*>(updateMock.get());
Patrick Venture1a406fe2019-05-31 07:29:56 -070038
Patrick Venture8a4f2aa2019-05-23 08:40:21 -070039 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture1a406fe2019-05-31 07:29:56 -070040 blobs, data, std::move(verifyMock), std::move(updateMock));
Patrick Venture8a4f2aa2019-05-23 08:40:21 -070041 }
Patrick Venture19044e12019-05-23 19:30:28 -070042
Patrick Venture6fdd02e2019-05-28 13:02:04 -070043 void expectedState(FirmwareBlobHandler::UpdateState state)
44 {
45 auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
46 EXPECT_EQ(state, realHandler->getCurrentState());
47 }
48
Patrick Venture16ab2a12019-05-31 08:52:51 -070049 void openToInProgress(const std::string& blobId)
50 {
51 EXPECT_CALL(imageMock, open(blobId)).WillOnce(Return(true));
52 EXPECT_TRUE(handler->open(session, flags, blobId));
53 expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
54 }
55
56 void getToVerificationPending(const std::string& blobId)
57 {
58 openToInProgress(blobId);
59
60 EXPECT_CALL(imageMock, close()).WillRepeatedly(Return());
61 handler->close(session);
62 expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
63 }
64
65 void getToVerificationStarted(const std::string& blobId)
66 {
67 getToVerificationPending(blobId);
68
69 EXPECT_TRUE(handler->open(session, flags, verifyBlobId));
Patrick Venture1d66fe62019-06-03 14:57:27 -070070 EXPECT_CALL(*verifyMockPtr, trigger()).WillOnce(Return(true));
Patrick Venture16ab2a12019-05-31 08:52:51 -070071
72 EXPECT_TRUE(handler->commit(session, {}));
73 expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
74 }
75
Patrick Ventureda66fd82019-06-03 11:11:24 -070076 void getToVerificationCompleted(ActionStatus checkResponse)
Patrick Venture16ab2a12019-05-31 08:52:51 -070077 {
78 getToVerificationStarted(staticLayoutBlobId);
79
Patrick Venturef1f0f652019-06-03 09:10:19 -070080 EXPECT_CALL(*verifyMockPtr, status()).WillOnce(Return(checkResponse));
Patrick Venture16ab2a12019-05-31 08:52:51 -070081 blobs::BlobMeta meta;
82 EXPECT_TRUE(handler->stat(session, &meta));
83 expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
84 }
85
86 void getToUpdatePending()
87 {
Patrick Ventureda66fd82019-06-03 11:11:24 -070088 getToVerificationCompleted(ActionStatus::success);
Patrick Venture16ab2a12019-05-31 08:52:51 -070089
90 handler->close(session);
91 expectedState(FirmwareBlobHandler::UpdateState::updatePending);
92 }
93
94 void getToUpdateStarted()
95 {
96 getToUpdatePending();
97 EXPECT_TRUE(handler->open(session, flags, updateBlobId));
98
Patrick Venture1d66fe62019-06-03 14:57:27 -070099 EXPECT_CALL(*updateMockPtr, trigger()).WillOnce(Return(true));
Patrick Venture16ab2a12019-05-31 08:52:51 -0700100 EXPECT_TRUE(handler->commit(session, {}));
101 expectedState(FirmwareBlobHandler::UpdateState::updateStarted);
102 }
103
Patrick Ventureda66fd82019-06-03 11:11:24 -0700104 void getToUpdateCompleted(ActionStatus result)
Patrick Ventureab1e9622019-06-03 10:45:06 -0700105 {
106 getToUpdateStarted();
107 EXPECT_CALL(*updateMockPtr, status()).WillOnce(Return(result));
108
109 blobs::BlobMeta meta;
110 EXPECT_TRUE(handler->stat(session, &meta));
111 expectedState(FirmwareBlobHandler::UpdateState::updateCompleted);
112 }
113
Patrick Venture19044e12019-05-23 19:30:28 -0700114 ImageHandlerMock imageMock;
115 std::vector<HandlerPack> blobs;
116 std::vector<DataHandlerPack> data = {
117 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}};
118 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Venture1d66fe62019-06-03 14:57:27 -0700119 TriggerMock* verifyMockPtr;
120 TriggerMock* updateMockPtr;
Patrick Venture16ab2a12019-05-31 08:52:51 -0700121
122 std::uint16_t session = 1;
123 std::uint16_t flags =
124 blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi;
Patrick Venture8a4f2aa2019-05-23 08:40:21 -0700125};
126
Patrick Venture1361a122019-05-20 07:36:05 -0700127class IpmiOnlyFirmwareTest : public ::testing::Test
128{
129 protected:
130 ImageHandlerMock imageMock;
131 std::vector<HandlerPack> blobs;
132 std::vector<DataHandlerPack> data = {
133 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}};
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700134 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Venture1361a122019-05-20 07:36:05 -0700135
136 void SetUp() override
137 {
138 blobs = {
139 {hashBlobId, &imageMock},
140 {"asdf", &imageMock},
141 };
142 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture1d66fe62019-06-03 14:57:27 -0700143 blobs, data, CreateTriggerMock(), CreateTriggerMock());
Patrick Venture1361a122019-05-20 07:36:05 -0700144 }
145};
146
147class FakeLpcFirmwareTest : public ::testing::Test
148{
149 protected:
150 DataHandlerMock dataMock;
151 ImageHandlerMock imageMock;
152 std::vector<HandlerPack> blobs;
153 std::vector<DataHandlerPack> data;
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700154 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Venture1361a122019-05-20 07:36:05 -0700155
156 void SetUp() override
157 {
158 blobs = {
159 {hashBlobId, &imageMock},
160 {"asdf", &imageMock},
161 };
162 data = {
163 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
164 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
165 };
166 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture1d66fe62019-06-03 14:57:27 -0700167 blobs, data, CreateTriggerMock(), CreateTriggerMock());
Patrick Venture1361a122019-05-20 07:36:05 -0700168 }
169};
170
Patrick Venture16ab2a12019-05-31 08:52:51 -0700171} // namespace
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700172} // namespace ipmi_flash