Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 1 | #include "data_mock.hpp" |
| 2 | #include "firmware_handler.hpp" |
| 3 | #include "image_mock.hpp" |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 4 | #include "util.hpp" |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 5 | #include "verification_mock.hpp" |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 6 | |
Patrick Venture | 481cd4a | 2019-05-17 17:53:11 -0700 | [diff] [blame] | 7 | #include <memory> |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
| 10 | #include <gtest/gtest.h> |
| 11 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame^] | 12 | namespace ipmi_flash |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 13 | { |
Patrick Venture | cabc117 | 2018-11-16 16:14:26 -0800 | [diff] [blame] | 14 | using ::testing::_; |
| 15 | using ::testing::IsNull; |
| 16 | using ::testing::NotNull; |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 17 | using ::testing::Return; |
Patrick Venture | cabc117 | 2018-11-16 16:14:26 -0800 | [diff] [blame] | 18 | using ::testing::StrEq; |
| 19 | using ::testing::StrictMock; |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 20 | |
Patrick Venture | 481cd4a | 2019-05-17 17:53:11 -0700 | [diff] [blame] | 21 | class FirmwareHandlerCommitTest : public ::testing::Test |
| 22 | { |
| 23 | protected: |
| 24 | ImageHandlerMock imageMock1, imageMock2; |
| 25 | std::vector<HandlerPack> blobs; |
| 26 | std::vector<DataHandlerPack> data; |
| 27 | |
| 28 | void SetUp() override |
| 29 | { |
| 30 | blobs = { |
| 31 | {hashBlobId, &imageMock1}, |
| 32 | {"asdf", &imageMock2}, |
| 33 | }; |
| 34 | |
| 35 | data = { |
| 36 | {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr}, |
| 37 | }; |
| 38 | } |
| 39 | }; |
| 40 | |
| 41 | TEST_F(FirmwareHandlerCommitTest, VerifyCannotCommitOnFlashImage) |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 42 | { |
| 43 | /* Verify the flash image returns failure on this command. It's a fairly |
| 44 | * artificial test. |
| 45 | */ |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 46 | |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 47 | /* Verify it doesn't get called by using StrictMock. */ |
| 48 | std::unique_ptr<VerificationInterface> verifyMock = |
| 49 | std::make_unique<StrictMock<VerificationMock>>(); |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 50 | |
| 51 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 52 | blobs, data, std::move(verifyMock)); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 53 | |
| 54 | EXPECT_CALL(imageMock2, open("asdf")).WillOnce(Return(true)); |
| 55 | |
| 56 | EXPECT_TRUE(handler->open( |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame^] | 57 | 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, |
| 58 | "asdf")); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 59 | |
| 60 | EXPECT_FALSE(handler->commit(0, {})); |
| 61 | } |
| 62 | |
Patrick Venture | 481cd4a | 2019-05-17 17:53:11 -0700 | [diff] [blame] | 63 | TEST_F(FirmwareHandlerCommitTest, VerifyCannotCommitOnHashFile) |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 64 | { |
| 65 | /* Verify the hash file returns failure on this command. It's a fairly |
| 66 | * artificial test. |
| 67 | */ |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 68 | |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 69 | /* Verify it doesn't get called by using StrictMock. */ |
| 70 | std::unique_ptr<VerificationInterface> verifyMock = |
| 71 | std::make_unique<StrictMock<VerificationMock>>(); |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 72 | |
| 73 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 74 | blobs, data, std::move(verifyMock)); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 75 | |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 76 | EXPECT_CALL(imageMock1, open(StrEq(hashBlobId))).WillOnce(Return(true)); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 77 | |
| 78 | EXPECT_TRUE(handler->open( |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame^] | 79 | 0, blobs::OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 80 | hashBlobId)); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 81 | |
| 82 | EXPECT_FALSE(handler->commit(0, {})); |
| 83 | } |
| 84 | |
Patrick Venture | 481cd4a | 2019-05-17 17:53:11 -0700 | [diff] [blame] | 85 | TEST_F(FirmwareHandlerCommitTest, VerifyCommitAcceptedOnVerifyBlob) |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 86 | { |
| 87 | /* Verify the verify blob lets you call this command, and it returns |
| 88 | * success. |
| 89 | */ |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 90 | auto verifyMock = CreateVerifyMock(); |
| 91 | auto verifyMockPtr = reinterpret_cast<VerificationMock*>(verifyMock.get()); |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 92 | |
| 93 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 94 | blobs, data, std::move(verifyMock)); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 95 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame^] | 96 | EXPECT_TRUE(handler->open(0, blobs::OpenFlags::write, verifyBlobId)); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 97 | |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 98 | EXPECT_CALL(*verifyMockPtr, triggerVerification()) |
| 99 | .WillRepeatedly(Return(true)); |
| 100 | |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 101 | EXPECT_TRUE(handler->commit(0, {})); |
| 102 | } |
| 103 | |
Patrick Venture | 481cd4a | 2019-05-17 17:53:11 -0700 | [diff] [blame] | 104 | TEST_F(FirmwareHandlerCommitTest, VerifyCommitCanOnlyBeCalledOnceForEffect) |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 105 | { |
Patrick Venture | be19870 | 2019-05-15 09:46:02 -0700 | [diff] [blame] | 106 | /* Verify you cannot call the commit() command once verification is |
| 107 | * started, after which it will just return true. |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 108 | */ |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 109 | auto verifyMock = CreateVerifyMock(); |
| 110 | auto verifyMockPtr = reinterpret_cast<VerificationMock*>(verifyMock.get()); |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 111 | |
| 112 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 113 | blobs, data, std::move(verifyMock)); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 114 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame^] | 115 | EXPECT_TRUE(handler->open(0, blobs::OpenFlags::write, verifyBlobId)); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 116 | |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 117 | EXPECT_CALL(*verifyMockPtr, triggerVerification()) |
| 118 | .WillRepeatedly(Return(true)); |
Patrick Venture | cabc117 | 2018-11-16 16:14:26 -0800 | [diff] [blame] | 119 | |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 120 | EXPECT_TRUE(handler->commit(0, {})); |
Patrick Venture | be19870 | 2019-05-15 09:46:02 -0700 | [diff] [blame] | 121 | EXPECT_TRUE(handler->commit(0, {})); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame^] | 124 | } // namespace ipmi_flash |