blob: 947c2ba0dfde09ceaac74761927d277e5b485e4a [file] [log] [blame]
Patrick Venture1361a122019-05-20 07:36:05 -07001#pragma once
2
Patrick Venture27ac5822019-05-20 17:39:31 -07003#include "bmc_update_mock.hpp"
Patrick Venture1361a122019-05-20 07:36:05 -07004#include "data_mock.hpp"
5#include "firmware_handler.hpp"
6#include "image_mock.hpp"
7#include "verification_mock.hpp"
8
9#include <memory>
10#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
32 std::unique_ptr<VerificationInterface> verifyMock =
33 std::make_unique<VerificationMock>();
34 verifyMockPtr = reinterpret_cast<VerificationMock*>(verifyMock.get());
35
Patrick Venture1a406fe2019-05-31 07:29:56 -070036 std::unique_ptr<UpdateInterface> updateMock =
37 std::make_unique<UpdateMock>();
38 updateMockPtr = reinterpret_cast<UpdateMock*>(updateMock.get());
39
Patrick Venture8a4f2aa2019-05-23 08:40:21 -070040 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture1a406fe2019-05-31 07:29:56 -070041 blobs, data, std::move(verifyMock), std::move(updateMock));
Patrick Venture8a4f2aa2019-05-23 08:40:21 -070042 }
Patrick Venture19044e12019-05-23 19:30:28 -070043
Patrick Venture6fdd02e2019-05-28 13:02:04 -070044 void expectedState(FirmwareBlobHandler::UpdateState state)
45 {
46 auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get());
47 EXPECT_EQ(state, realHandler->getCurrentState());
48 }
49
Patrick Venture16ab2a12019-05-31 08:52:51 -070050 void openToInProgress(const std::string& blobId)
51 {
52 EXPECT_CALL(imageMock, open(blobId)).WillOnce(Return(true));
53 EXPECT_TRUE(handler->open(session, flags, blobId));
54 expectedState(FirmwareBlobHandler::UpdateState::uploadInProgress);
55 }
56
57 void getToVerificationPending(const std::string& blobId)
58 {
59 openToInProgress(blobId);
60
61 EXPECT_CALL(imageMock, close()).WillRepeatedly(Return());
62 handler->close(session);
63 expectedState(FirmwareBlobHandler::UpdateState::verificationPending);
64 }
65
66 void getToVerificationStarted(const std::string& blobId)
67 {
68 getToVerificationPending(blobId);
69
70 EXPECT_TRUE(handler->open(session, flags, verifyBlobId));
71 EXPECT_CALL(*verifyMockPtr, triggerVerification())
72 .WillOnce(Return(true));
73
74 EXPECT_TRUE(handler->commit(session, {}));
75 expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
76 }
77
78 void getToVerificationCompleted(VerifyCheckResponses checkResponse)
79 {
80 getToVerificationStarted(staticLayoutBlobId);
81
Patrick Venturef1f0f652019-06-03 09:10:19 -070082 EXPECT_CALL(*verifyMockPtr, status()).WillOnce(Return(checkResponse));
Patrick Venture16ab2a12019-05-31 08:52:51 -070083 blobs::BlobMeta meta;
84 EXPECT_TRUE(handler->stat(session, &meta));
85 expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
86 }
87
88 void getToUpdatePending()
89 {
90 getToVerificationCompleted(VerifyCheckResponses::success);
91
92 handler->close(session);
93 expectedState(FirmwareBlobHandler::UpdateState::updatePending);
94 }
95
96 void getToUpdateStarted()
97 {
98 getToUpdatePending();
99 EXPECT_TRUE(handler->open(session, flags, updateBlobId));
100
101 EXPECT_CALL(*updateMockPtr, triggerUpdate()).WillOnce(Return(true));
102 EXPECT_TRUE(handler->commit(session, {}));
103 expectedState(FirmwareBlobHandler::UpdateState::updateStarted);
104 }
105
Patrick Ventureab1e9622019-06-03 10:45:06 -0700106 void getToUpdateCompleted(UpdateStatus result)
107 {
108 getToUpdateStarted();
109 EXPECT_CALL(*updateMockPtr, status()).WillOnce(Return(result));
110
111 blobs::BlobMeta meta;
112 EXPECT_TRUE(handler->stat(session, &meta));
113 expectedState(FirmwareBlobHandler::UpdateState::updateCompleted);
114 }
115
Patrick Venture19044e12019-05-23 19:30:28 -0700116 ImageHandlerMock imageMock;
117 std::vector<HandlerPack> blobs;
118 std::vector<DataHandlerPack> data = {
119 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}};
120 std::unique_ptr<blobs::GenericBlobInterface> handler;
121 VerificationMock* verifyMockPtr;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700122 UpdateMock* updateMockPtr;
Patrick Venture16ab2a12019-05-31 08:52:51 -0700123
124 std::uint16_t session = 1;
125 std::uint16_t flags =
126 blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi;
Patrick Venture8a4f2aa2019-05-23 08:40:21 -0700127};
128
Patrick Venture1361a122019-05-20 07:36:05 -0700129class IpmiOnlyFirmwareTest : public ::testing::Test
130{
131 protected:
132 ImageHandlerMock imageMock;
133 std::vector<HandlerPack> blobs;
134 std::vector<DataHandlerPack> data = {
135 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}};
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700136 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Venture1361a122019-05-20 07:36:05 -0700137
138 void SetUp() override
139 {
140 blobs = {
141 {hashBlobId, &imageMock},
142 {"asdf", &imageMock},
143 };
144 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture27ac5822019-05-20 17:39:31 -0700145 blobs, data, CreateVerifyMock(), CreateUpdateMock());
Patrick Venture1361a122019-05-20 07:36:05 -0700146 }
147};
148
149class FakeLpcFirmwareTest : public ::testing::Test
150{
151 protected:
152 DataHandlerMock dataMock;
153 ImageHandlerMock imageMock;
154 std::vector<HandlerPack> blobs;
155 std::vector<DataHandlerPack> data;
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700156 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Venture1361a122019-05-20 07:36:05 -0700157
158 void SetUp() override
159 {
160 blobs = {
161 {hashBlobId, &imageMock},
162 {"asdf", &imageMock},
163 };
164 data = {
165 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
166 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
167 };
168 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture27ac5822019-05-20 17:39:31 -0700169 blobs, data, CreateVerifyMock(), CreateUpdateMock());
Patrick Venture1361a122019-05-20 07:36:05 -0700170 }
171};
172
Patrick Venture16ab2a12019-05-31 08:52:51 -0700173} // namespace
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700174} // namespace ipmi_flash