blob: e36081d6166563a4d82cd47093d322efa31521cc [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 Venture1d66fe62019-06-03 14:57:27 -070032 std::unique_ptr<TriggerableActionInterface> verifyMock =
33 std::make_unique<TriggerMock>();
34 verifyMockPtr = reinterpret_cast<TriggerMock*>(verifyMock.get());
Patrick Venture19044e12019-05-23 19:30:28 -070035
Patrick Venture1d66fe62019-06-03 14:57:27 -070036 std::unique_ptr<TriggerableActionInterface> updateMock =
37 std::make_unique<TriggerMock>();
38 updateMockPtr = reinterpret_cast<TriggerMock*>(updateMock.get());
Patrick Venture1a406fe2019-05-31 07:29:56 -070039
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));
Patrick Venture1d66fe62019-06-03 14:57:27 -070071 EXPECT_CALL(*verifyMockPtr, trigger()).WillOnce(Return(true));
Patrick Venture16ab2a12019-05-31 08:52:51 -070072
73 EXPECT_TRUE(handler->commit(session, {}));
74 expectedState(FirmwareBlobHandler::UpdateState::verificationStarted);
75 }
76
Patrick Ventureda66fd82019-06-03 11:11:24 -070077 void getToVerificationCompleted(ActionStatus checkResponse)
Patrick Venture16ab2a12019-05-31 08:52:51 -070078 {
79 getToVerificationStarted(staticLayoutBlobId);
80
Patrick Venturef1f0f652019-06-03 09:10:19 -070081 EXPECT_CALL(*verifyMockPtr, status()).WillOnce(Return(checkResponse));
Patrick Venture16ab2a12019-05-31 08:52:51 -070082 blobs::BlobMeta meta;
83 EXPECT_TRUE(handler->stat(session, &meta));
84 expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
85 }
86
87 void getToUpdatePending()
88 {
Patrick Ventureda66fd82019-06-03 11:11:24 -070089 getToVerificationCompleted(ActionStatus::success);
Patrick Venture16ab2a12019-05-31 08:52:51 -070090
91 handler->close(session);
92 expectedState(FirmwareBlobHandler::UpdateState::updatePending);
93 }
94
95 void getToUpdateStarted()
96 {
97 getToUpdatePending();
98 EXPECT_TRUE(handler->open(session, flags, updateBlobId));
99
Patrick Venture1d66fe62019-06-03 14:57:27 -0700100 EXPECT_CALL(*updateMockPtr, trigger()).WillOnce(Return(true));
Patrick Venture16ab2a12019-05-31 08:52:51 -0700101 EXPECT_TRUE(handler->commit(session, {}));
102 expectedState(FirmwareBlobHandler::UpdateState::updateStarted);
103 }
104
Patrick Ventureda66fd82019-06-03 11:11:24 -0700105 void getToUpdateCompleted(ActionStatus result)
Patrick Ventureab1e9622019-06-03 10:45:06 -0700106 {
107 getToUpdateStarted();
108 EXPECT_CALL(*updateMockPtr, status()).WillOnce(Return(result));
109
110 blobs::BlobMeta meta;
111 EXPECT_TRUE(handler->stat(session, &meta));
112 expectedState(FirmwareBlobHandler::UpdateState::updateCompleted);
113 }
114
Patrick Venture19044e12019-05-23 19:30:28 -0700115 ImageHandlerMock imageMock;
116 std::vector<HandlerPack> blobs;
117 std::vector<DataHandlerPack> data = {
118 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}};
119 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Venture1d66fe62019-06-03 14:57:27 -0700120 TriggerMock* verifyMockPtr;
121 TriggerMock* updateMockPtr;
Patrick Venture16ab2a12019-05-31 08:52:51 -0700122
123 std::uint16_t session = 1;
124 std::uint16_t flags =
125 blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi;
Patrick Venture9a69f732019-06-17 14:05:13 -0700126
127 std::vector<std::string> startingBlobs = {staticLayoutBlobId, hashBlobId};
Patrick Venture8a4f2aa2019-05-23 08:40:21 -0700128};
129
Patrick Venture1361a122019-05-20 07:36:05 -0700130class IpmiOnlyFirmwareTest : public ::testing::Test
131{
132 protected:
133 ImageHandlerMock imageMock;
134 std::vector<HandlerPack> blobs;
135 std::vector<DataHandlerPack> data = {
136 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}};
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700137 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Venture1361a122019-05-20 07:36:05 -0700138
139 void SetUp() override
140 {
141 blobs = {
142 {hashBlobId, &imageMock},
143 {"asdf", &imageMock},
144 };
145 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture1d66fe62019-06-03 14:57:27 -0700146 blobs, data, CreateTriggerMock(), CreateTriggerMock());
Patrick Venture1361a122019-05-20 07:36:05 -0700147 }
148};
149
150class FakeLpcFirmwareTest : public ::testing::Test
151{
152 protected:
153 DataHandlerMock dataMock;
154 ImageHandlerMock imageMock;
155 std::vector<HandlerPack> blobs;
156 std::vector<DataHandlerPack> data;
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700157 std::unique_ptr<blobs::GenericBlobInterface> handler;
Patrick Venture1361a122019-05-20 07:36:05 -0700158
159 void SetUp() override
160 {
161 blobs = {
162 {hashBlobId, &imageMock},
163 {"asdf", &imageMock},
164 };
165 data = {
166 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
167 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
168 };
169 handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture1d66fe62019-06-03 14:57:27 -0700170 blobs, data, CreateTriggerMock(), CreateTriggerMock());
Patrick Venture1361a122019-05-20 07:36:05 -0700171 }
172};
173
Patrick Venture16ab2a12019-05-31 08:52:51 -0700174} // namespace
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700175} // namespace ipmi_flash