Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 1 | #include "data_mock.hpp" |
| 2 | #include "firmware_handler.hpp" |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 3 | #include "flags.hpp" |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 4 | #include "image_mock.hpp" |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 5 | #include "triggerable_mock.hpp" |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 6 | #include "util.hpp" |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 7 | |
Patrick Venture | 481cd4a | 2019-05-17 17:53:11 -0700 | [diff] [blame] | 8 | #include <memory> |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
| 11 | #include <gtest/gtest.h> |
| 12 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 13 | namespace ipmi_flash |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 14 | { |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 15 | namespace |
| 16 | { |
Patrick Venture | cabc117 | 2018-11-16 16:14:26 -0800 | [diff] [blame] | 17 | using ::testing::_; |
| 18 | using ::testing::IsNull; |
| 19 | using ::testing::NotNull; |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 20 | using ::testing::Return; |
Patrick Venture | cabc117 | 2018-11-16 16:14:26 -0800 | [diff] [blame] | 21 | using ::testing::StrEq; |
| 22 | using ::testing::StrictMock; |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 23 | |
Patrick Venture | 481cd4a | 2019-05-17 17:53:11 -0700 | [diff] [blame] | 24 | class FirmwareHandlerCommitTest : public ::testing::Test |
| 25 | { |
| 26 | protected: |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 27 | ImageHandlerMock *imageMock1, *imageMock2; |
Patrick Venture | 481cd4a | 2019-05-17 17:53:11 -0700 | [diff] [blame] | 28 | std::vector<HandlerPack> blobs; |
| 29 | std::vector<DataHandlerPack> data; |
| 30 | |
| 31 | void SetUp() override |
| 32 | { |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 33 | std::unique_ptr<ImageHandlerInterface> image = |
| 34 | std::make_unique<ImageHandlerMock>(); |
| 35 | imageMock1 = reinterpret_cast<ImageHandlerMock*>(image.get()); |
| 36 | blobs.push_back(std::move(HandlerPack(hashBlobId, std::move(image)))); |
| 37 | |
| 38 | image = std::make_unique<ImageHandlerMock>(); |
| 39 | imageMock2 = reinterpret_cast<ImageHandlerMock*>(image.get()); |
| 40 | blobs.push_back(std::move(HandlerPack("asdf", std::move(image)))); |
Patrick Venture | 481cd4a | 2019-05-17 17:53:11 -0700 | [diff] [blame] | 41 | |
| 42 | data = { |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 43 | {FirmwareFlags::UpdateFlags::ipmi, nullptr}, |
Patrick Venture | 481cd4a | 2019-05-17 17:53:11 -0700 | [diff] [blame] | 44 | }; |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | TEST_F(FirmwareHandlerCommitTest, VerifyCannotCommitOnFlashImage) |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 49 | { |
| 50 | /* Verify the flash image returns failure on this command. It's a fairly |
| 51 | * artificial test. |
| 52 | */ |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 53 | |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 54 | /* Verify it doesn't get called by using StrictMock. */ |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 55 | std::unique_ptr<TriggerableActionInterface> verifyMock = |
| 56 | std::make_unique<StrictMock<TriggerMock>>(); |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 57 | |
| 58 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | 7b78343 | 2020-09-22 15:55:08 -0700 | [diff] [blame^] | 59 | std::move(blobs), std::move(data), std::move(CreateActionMap("asdf"))); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 60 | |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 61 | EXPECT_CALL(*imageMock2, open("asdf")).WillOnce(Return(true)); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 62 | |
| 63 | EXPECT_TRUE(handler->open( |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 64 | 0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::ipmi, "asdf")); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 65 | |
| 66 | EXPECT_FALSE(handler->commit(0, {})); |
| 67 | } |
| 68 | |
Patrick Venture | 481cd4a | 2019-05-17 17:53:11 -0700 | [diff] [blame] | 69 | TEST_F(FirmwareHandlerCommitTest, VerifyCannotCommitOnHashFile) |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 70 | { |
| 71 | /* Verify the hash file returns failure on this command. It's a fairly |
| 72 | * artificial test. |
| 73 | */ |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 74 | |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 75 | /* Verify it doesn't get called by using StrictMock. */ |
Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 76 | std::unique_ptr<TriggerableActionInterface> verifyMock = |
| 77 | std::make_unique<StrictMock<TriggerMock>>(); |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 78 | |
| 79 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | 7b78343 | 2020-09-22 15:55:08 -0700 | [diff] [blame^] | 80 | std::move(blobs), std::move(data), std::move(CreateActionMap("asdf"))); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 81 | |
Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 82 | EXPECT_CALL(*imageMock1, open(StrEq(hashBlobId))).WillOnce(Return(true)); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 83 | |
| 84 | EXPECT_TRUE(handler->open( |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 85 | 0, blobs::OpenFlags::write | FirmwareFlags::UpdateFlags::ipmi, |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 86 | hashBlobId)); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 87 | |
| 88 | EXPECT_FALSE(handler->commit(0, {})); |
| 89 | } |
| 90 | |
Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 91 | } // namespace |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 92 | } // namespace ipmi_flash |