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