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 | |
| 12 | namespace blobs |
| 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( |
| 57 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, "asdf")); |
| 58 | |
| 59 | EXPECT_FALSE(handler->commit(0, {})); |
| 60 | } |
| 61 | |
Patrick Venture | 481cd4a | 2019-05-17 17:53:11 -0700 | [diff] [blame^] | 62 | TEST_F(FirmwareHandlerCommitTest, VerifyCannotCommitOnHashFile) |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 63 | { |
| 64 | /* Verify the hash file returns failure on this command. It's a fairly |
| 65 | * artificial test. |
| 66 | */ |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 67 | |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 68 | /* Verify it doesn't get called by using StrictMock. */ |
| 69 | std::unique_ptr<VerificationInterface> verifyMock = |
| 70 | std::make_unique<StrictMock<VerificationMock>>(); |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 71 | |
| 72 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 73 | blobs, data, std::move(verifyMock)); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 74 | |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 75 | EXPECT_CALL(imageMock1, open(StrEq(hashBlobId))).WillOnce(Return(true)); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 76 | |
| 77 | EXPECT_TRUE(handler->open( |
| 78 | 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi, |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 79 | hashBlobId)); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 80 | |
| 81 | EXPECT_FALSE(handler->commit(0, {})); |
| 82 | } |
| 83 | |
Patrick Venture | 481cd4a | 2019-05-17 17:53:11 -0700 | [diff] [blame^] | 84 | TEST_F(FirmwareHandlerCommitTest, VerifyCommitAcceptedOnVerifyBlob) |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 85 | { |
| 86 | /* Verify the verify blob lets you call this command, and it returns |
| 87 | * success. |
| 88 | */ |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 89 | auto verifyMock = CreateVerifyMock(); |
| 90 | auto verifyMockPtr = reinterpret_cast<VerificationMock*>(verifyMock.get()); |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 91 | |
| 92 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 93 | blobs, data, std::move(verifyMock)); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 94 | |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 95 | EXPECT_TRUE(handler->open(0, OpenFlags::write, verifyBlobId)); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 96 | |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 97 | EXPECT_CALL(*verifyMockPtr, triggerVerification()) |
| 98 | .WillRepeatedly(Return(true)); |
| 99 | |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 100 | EXPECT_TRUE(handler->commit(0, {})); |
| 101 | } |
| 102 | |
Patrick Venture | 481cd4a | 2019-05-17 17:53:11 -0700 | [diff] [blame^] | 103 | TEST_F(FirmwareHandlerCommitTest, VerifyCommitCanOnlyBeCalledOnceForEffect) |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 104 | { |
Patrick Venture | be19870 | 2019-05-15 09:46:02 -0700 | [diff] [blame] | 105 | /* Verify you cannot call the commit() command once verification is |
| 106 | * started, after which it will just return true. |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 107 | */ |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 108 | auto verifyMock = CreateVerifyMock(); |
| 109 | auto verifyMockPtr = reinterpret_cast<VerificationMock*>(verifyMock.get()); |
Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 110 | |
| 111 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 112 | blobs, data, std::move(verifyMock)); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 113 | |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 114 | EXPECT_TRUE(handler->open(0, OpenFlags::write, verifyBlobId)); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 115 | |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 116 | EXPECT_CALL(*verifyMockPtr, triggerVerification()) |
| 117 | .WillRepeatedly(Return(true)); |
Patrick Venture | cabc117 | 2018-11-16 16:14:26 -0800 | [diff] [blame] | 118 | |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 119 | EXPECT_TRUE(handler->commit(0, {})); |
Patrick Venture | be19870 | 2019-05-15 09:46:02 -0700 | [diff] [blame] | 120 | EXPECT_TRUE(handler->commit(0, {})); |
Patrick Venture | ffcc550 | 2018-11-16 12:32:38 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | } // namespace blobs |