blob: d0778186c07f3cb4d5734a18231b0f9c647819b3 [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 Venture19044e12019-05-23 19:30:28 -0700106 ImageHandlerMock imageMock;
107 std::vector<HandlerPack> blobs;
108 std::vector<DataHandlerPack> data = {
109 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}};
110 std::unique_ptr<blobs::GenericBlobInterface> handler;
111 VerificationMock* verifyMockPtr;
Patrick Venture1a406fe2019-05-31 07:29:56 -0700112 UpdateMock* updateMockPtr;
Patrick Venture16ab2a12019-05-31 08:52:51 -0700113
114 std::uint16_t session = 1;
115 std::uint16_t flags =
116 blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi;
Patrick Venture8a4f2aa2019-05-23 08:40:21 -0700117};
118
Patrick Venture1361a122019-05-20 07:36:05 -0700119class IpmiOnlyFirmwareTest : public ::testing::Test
120{
121 protected:
122 ImageHandlerMock imageMock;
123 std::vector<HandlerPack> blobs;
124 std::vector<DataHandlerPack> data = {
125 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}};
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700126 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Venture1361a122019-05-20 07:36:05 -0700127
128 void SetUp() override
129 {
130 blobs = {
131 {hashBlobId, &imageMock},
132 {"asdf", &imageMock},
133 };
134 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture27ac5822019-05-20 17:39:31 -0700135 blobs, data, CreateVerifyMock(), CreateUpdateMock());
Patrick Venture1361a122019-05-20 07:36:05 -0700136 }
137};
138
139class FakeLpcFirmwareTest : public ::testing::Test
140{
141 protected:
142 DataHandlerMock dataMock;
143 ImageHandlerMock imageMock;
144 std::vector<HandlerPack> blobs;
145 std::vector<DataHandlerPack> data;
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700146 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Venture1361a122019-05-20 07:36:05 -0700147
148 void SetUp() override
149 {
150 blobs = {
151 {hashBlobId, &imageMock},
152 {"asdf", &imageMock},
153 };
154 data = {
155 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
156 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
157 };
158 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture27ac5822019-05-20 17:39:31 -0700159 blobs, data, CreateVerifyMock(), CreateUpdateMock());
Patrick Venture1361a122019-05-20 07:36:05 -0700160 }
161};
162
Patrick Venture16ab2a12019-05-31 08:52:51 -0700163} // namespace
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700164} // namespace ipmi_flash