Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "data_mock.hpp" |
| 4 | #include "firmware_handler.hpp" |
| 5 | #include "image_mock.hpp" |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 6 | #include "triggerable_mock.hpp" |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 7 | |
| 8 | #include <memory> |
Patrick Venture | 9a69f73 | 2019-06-17 14:05:13 -0700 | [diff] [blame] | 9 | #include <string> |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
| 12 | #include <gmock/gmock.h> |
| 13 | #include <gtest/gtest.h> |
| 14 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 15 | namespace ipmi_flash |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 16 | { |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 17 | namespace |
| 18 | { |
| 19 | |
| 20 | using ::testing::Return; |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 21 | |
Patrick Venture | 8a4f2aa | 2019-05-23 08:40:21 -0700 | [diff] [blame] | 22 | class IpmiOnlyFirmwareStaticTest : public ::testing::Test |
| 23 | { |
| 24 | protected: |
Patrick Venture | 8a4f2aa | 2019-05-23 08:40:21 -0700 | [diff] [blame] | 25 | void SetUp() override |
| 26 | { |
| 27 | blobs = { |
| 28 | {hashBlobId, &imageMock}, |
| 29 | {staticLayoutBlobId, &imageMock}, |
| 30 | }; |
Patrick Venture | 19044e1 | 2019-05-23 19:30:28 -0700 | [diff] [blame] | 31 | |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 32 | std::unique_ptr<TriggerableActionInterface> verifyMock = |
| 33 | std::make_unique<TriggerMock>(); |
| 34 | verifyMockPtr = reinterpret_cast<TriggerMock*>(verifyMock.get()); |
Patrick Venture | 19044e1 | 2019-05-23 19:30:28 -0700 | [diff] [blame] | 35 | |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 36 | std::unique_ptr<TriggerableActionInterface> updateMock = |
| 37 | std::make_unique<TriggerMock>(); |
| 38 | updateMockPtr = reinterpret_cast<TriggerMock*>(updateMock.get()); |
Patrick Venture | 1a406fe | 2019-05-31 07:29:56 -0700 | [diff] [blame] | 39 | |
Patrick Venture | 8a4f2aa | 2019-05-23 08:40:21 -0700 | [diff] [blame] | 40 | handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | 1a406fe | 2019-05-31 07:29:56 -0700 | [diff] [blame] | 41 | blobs, data, std::move(verifyMock), std::move(updateMock)); |
Patrick Venture | 8a4f2aa | 2019-05-23 08:40:21 -0700 | [diff] [blame] | 42 | } |
Patrick Venture | 19044e1 | 2019-05-23 19:30:28 -0700 | [diff] [blame] | 43 | |
Patrick Venture | 6fdd02e | 2019-05-28 13:02:04 -0700 | [diff] [blame] | 44 | void expectedState(FirmwareBlobHandler::UpdateState state) |
| 45 | { |
| 46 | auto realHandler = dynamic_cast<FirmwareBlobHandler*>(handler.get()); |
| 47 | EXPECT_EQ(state, realHandler->getCurrentState()); |
| 48 | } |
| 49 | |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 50 | 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 Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 71 | EXPECT_CALL(*verifyMockPtr, trigger()).WillOnce(Return(true)); |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 72 | |
| 73 | EXPECT_TRUE(handler->commit(session, {})); |
| 74 | expectedState(FirmwareBlobHandler::UpdateState::verificationStarted); |
| 75 | } |
| 76 | |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 77 | void getToVerificationCompleted(ActionStatus checkResponse) |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 78 | { |
| 79 | getToVerificationStarted(staticLayoutBlobId); |
| 80 | |
Patrick Venture | f1f0f65 | 2019-06-03 09:10:19 -0700 | [diff] [blame] | 81 | EXPECT_CALL(*verifyMockPtr, status()).WillOnce(Return(checkResponse)); |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 82 | blobs::BlobMeta meta; |
| 83 | EXPECT_TRUE(handler->stat(session, &meta)); |
| 84 | expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted); |
| 85 | } |
| 86 | |
| 87 | void getToUpdatePending() |
| 88 | { |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 89 | getToVerificationCompleted(ActionStatus::success); |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 90 | |
| 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 Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 100 | EXPECT_CALL(*updateMockPtr, trigger()).WillOnce(Return(true)); |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 101 | EXPECT_TRUE(handler->commit(session, {})); |
| 102 | expectedState(FirmwareBlobHandler::UpdateState::updateStarted); |
| 103 | } |
| 104 | |
Patrick Venture | da66fd8 | 2019-06-03 11:11:24 -0700 | [diff] [blame] | 105 | void getToUpdateCompleted(ActionStatus result) |
Patrick Venture | ab1e962 | 2019-06-03 10:45:06 -0700 | [diff] [blame] | 106 | { |
| 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 Venture | 19044e1 | 2019-05-23 19:30:28 -0700 | [diff] [blame] | 115 | 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 Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 120 | TriggerMock* verifyMockPtr; |
| 121 | TriggerMock* updateMockPtr; |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 122 | |
| 123 | std::uint16_t session = 1; |
| 124 | std::uint16_t flags = |
| 125 | blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi; |
Patrick Venture | 9a69f73 | 2019-06-17 14:05:13 -0700 | [diff] [blame] | 126 | |
| 127 | std::vector<std::string> startingBlobs = {staticLayoutBlobId, hashBlobId}; |
Patrick Venture | 8a4f2aa | 2019-05-23 08:40:21 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 130 | class 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 Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 137 | std::unique_ptr<blobs::GenericBlobInterface> handler; |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 138 | |
| 139 | void SetUp() override |
| 140 | { |
| 141 | blobs = { |
| 142 | {hashBlobId, &imageMock}, |
| 143 | {"asdf", &imageMock}, |
| 144 | }; |
| 145 | handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 146 | blobs, data, CreateTriggerMock(), CreateTriggerMock()); |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 147 | } |
| 148 | }; |
| 149 | |
| 150 | class FakeLpcFirmwareTest : public ::testing::Test |
| 151 | { |
| 152 | protected: |
| 153 | DataHandlerMock dataMock; |
| 154 | ImageHandlerMock imageMock; |
| 155 | std::vector<HandlerPack> blobs; |
| 156 | std::vector<DataHandlerPack> data; |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 157 | std::unique_ptr<blobs::GenericBlobInterface> handler; |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 158 | |
| 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 Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 170 | blobs, data, CreateTriggerMock(), CreateTriggerMock()); |
Patrick Venture | 1361a12 | 2019-05-20 07:36:05 -0700 | [diff] [blame] | 171 | } |
| 172 | }; |
| 173 | |
Patrick Venture | 16ab2a1 | 2019-05-31 08:52:51 -0700 | [diff] [blame] | 174 | } // namespace |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 175 | } // namespace ipmi_flash |