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