| Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 1 | #include "data_interface_mock.hpp" | 
| Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 2 | #include "flags.hpp" | 
| Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 3 | #include "status.hpp" | 
| Patrick Venture | 5f2fcc4 | 2019-06-20 07:21:05 -0700 | [diff] [blame] | 4 | #include "tool_errors.hpp" | 
| Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 5 | #include "updater.hpp" | 
| Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 6 | #include "updater_mock.hpp" | 
| Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 7 | #include "util.hpp" | 
| Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 8 |  | 
| Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 9 | #include <ipmiblob/test/blob_interface_mock.hpp> | 
| Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 10 | #include <string> | 
|  | 11 |  | 
|  | 12 | #include <gtest/gtest.h> | 
|  | 13 |  | 
| Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 14 | namespace host_tool | 
|  | 15 | { | 
|  | 16 |  | 
| Patrick Venture | 7dcca5d | 2019-05-15 12:32:33 -0700 | [diff] [blame] | 17 | using ::testing::_; | 
| Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 18 | using ::testing::Eq; | 
|  | 19 | using ::testing::Return; | 
| Patrick Venture | b58f561 | 2019-05-07 09:22:07 -0700 | [diff] [blame] | 20 | using ::testing::TypedEq; | 
| Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 21 |  | 
| Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 22 | class UpdateHandlerTest : public ::testing::Test | 
| Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 23 | { | 
| Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 24 | protected: | 
|  | 25 | const std::uint16_t session = 0xbeef; | 
|  | 26 |  | 
| Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 27 | DataInterfaceMock handlerMock; | 
|  | 28 | ipmiblob::BlobInterfaceMock blobMock; | 
| Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 29 | UpdateHandler updater{&blobMock, &handlerMock}; | 
|  | 30 | }; | 
| Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 31 |  | 
| Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 32 | TEST_F(UpdateHandlerTest, CheckAvailableSuccess) | 
|  | 33 | { | 
|  | 34 | ipmiblob::StatResponse statObj = {}; | 
| Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 35 | statObj.blob_state = ipmi_flash::FirmwareFlags::UpdateFlags::ipmi | | 
|  | 36 | ipmi_flash::FirmwareFlags::UpdateFlags::lpc; | 
| Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 37 |  | 
|  | 38 | EXPECT_CALL(blobMock, getBlobList()) | 
| Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 39 | .WillOnce( | 
| Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 40 | Return(std::vector<std::string>({ipmi_flash::staticLayoutBlobId}))); | 
|  | 41 | EXPECT_CALL(blobMock, getStat(TypedEq<const std::string&>( | 
|  | 42 | ipmi_flash::staticLayoutBlobId))) | 
| Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 43 | .WillOnce(Return(statObj)); | 
|  | 44 |  | 
|  | 45 | EXPECT_CALL(handlerMock, supportedType()) | 
| Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 46 | .WillOnce(Return(ipmi_flash::FirmwareFlags::UpdateFlags::lpc)); | 
| Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 47 |  | 
| Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 48 | EXPECT_TRUE(updater.checkAvailable(ipmi_flash::staticLayoutBlobId)); | 
| Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 49 | } | 
|  | 50 |  | 
| Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 51 | TEST_F(UpdateHandlerTest, SendFileSuccess) | 
| Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 52 | { | 
|  | 53 | /* Call sendFile to verify it does what we expect. */ | 
| Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 54 | std::string firmwareImage = "image.bin"; | 
|  | 55 |  | 
|  | 56 | std::uint16_t supported = | 
|  | 57 | static_cast<std::uint16_t>( | 
| Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 58 | ipmi_flash::FirmwareFlags::UpdateFlags::lpc) | | 
|  | 59 | static_cast<std::uint16_t>( | 
|  | 60 | ipmi_flash::FirmwareFlags::UpdateFlags::openWrite); | 
| Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 61 |  | 
|  | 62 | EXPECT_CALL(handlerMock, supportedType()) | 
| Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 63 | .WillOnce(Return(ipmi_flash::FirmwareFlags::UpdateFlags::lpc)); | 
| Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 64 |  | 
| Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 65 | EXPECT_CALL(blobMock, openBlob(ipmi_flash::staticLayoutBlobId, supported)) | 
| Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 66 | .WillOnce(Return(session)); | 
|  | 67 |  | 
| Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 68 | EXPECT_CALL(handlerMock, sendContents(firmwareImage, session)) | 
| Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 69 | .WillOnce(Return(true)); | 
|  | 70 |  | 
|  | 71 | EXPECT_CALL(blobMock, closeBlob(session)).Times(1); | 
|  | 72 |  | 
| Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 73 | updater.sendFile(ipmi_flash::staticLayoutBlobId, firmwareImage); | 
| Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 74 | } | 
|  | 75 |  | 
| Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 76 | TEST_F(UpdateHandlerTest, VerifyFileHandleReturnsTrueOnSuccess) | 
| Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 77 | { | 
| Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 78 | EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _)) | 
| Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 79 | .WillOnce(Return(session)); | 
| Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 80 | EXPECT_CALL(blobMock, commit(session, _)).WillOnce(Return()); | 
| Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 81 | ipmiblob::StatResponse verificationResponse = {}; | 
|  | 82 | /* the other details of the response are ignored, and should be. */ | 
|  | 83 | verificationResponse.metadata.push_back( | 
|  | 84 | static_cast<std::uint8_t>(ipmi_flash::ActionStatus::success)); | 
| Patrick Venture | 7dcca5d | 2019-05-15 12:32:33 -0700 | [diff] [blame] | 85 |  | 
| Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 86 | EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session))) | 
|  | 87 | .WillOnce(Return(verificationResponse)); | 
|  | 88 | EXPECT_CALL(blobMock, closeBlob(session)).WillOnce(Return()); | 
|  | 89 |  | 
| Brandon Kim | 6749ba1 | 2019-09-19 13:31:37 -0700 | [diff] [blame^] | 90 | EXPECT_TRUE(updater.verifyFile(ipmi_flash::verifyBlobId, false)); | 
| Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 91 | } | 
|  | 92 |  | 
|  | 93 | class UpdaterTest : public ::testing::Test | 
|  | 94 | { | 
|  | 95 | protected: | 
|  | 96 | ipmiblob::BlobInterfaceMock blobMock; | 
|  | 97 | std::uint16_t session = 0xbeef; | 
| Brandon Kim | 6749ba1 | 2019-09-19 13:31:37 -0700 | [diff] [blame^] | 98 | bool defaultIgnore = false; | 
| Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 99 | }; | 
|  | 100 |  | 
| Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 101 | TEST_F(UpdaterTest, UpdateMainReturnsSuccessIfAllSuccess) | 
|  | 102 | { | 
|  | 103 | const std::string image = "image.bin"; | 
|  | 104 | const std::string signature = "signature.bin"; | 
|  | 105 | UpdateHandlerMock handler; | 
|  | 106 |  | 
|  | 107 | EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true)); | 
|  | 108 | EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return()); | 
|  | 109 | EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return()); | 
| Brandon Kim | 6749ba1 | 2019-09-19 13:31:37 -0700 | [diff] [blame^] | 110 | EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore)) | 
| Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 111 | .WillOnce(Return(true)); | 
| Brandon Kim | 6749ba1 | 2019-09-19 13:31:37 -0700 | [diff] [blame^] | 112 | EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId, defaultIgnore)) | 
| Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 113 | .WillOnce(Return(true)); | 
|  | 114 |  | 
| Brandon Kim | 6749ba1 | 2019-09-19 13:31:37 -0700 | [diff] [blame^] | 115 | updaterMain(&handler, image, signature, "static", defaultIgnore); | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | TEST_F(UpdaterTest, UpdateMainReturnsSuccessWithIgnoreUpdate) | 
|  | 119 | { | 
|  | 120 | const std::string image = "image.bin"; | 
|  | 121 | const std::string signature = "signature.bin"; | 
|  | 122 | UpdateHandlerMock handler; | 
|  | 123 | bool updateIgnore = true; | 
|  | 124 |  | 
|  | 125 | EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true)); | 
|  | 126 | EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return()); | 
|  | 127 | EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return()); | 
|  | 128 | EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore)) | 
|  | 129 | .WillOnce(Return(true)); | 
|  | 130 | EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId, updateIgnore)) | 
|  | 131 | .WillOnce(Return(true)); | 
|  | 132 |  | 
|  | 133 | updaterMain(&handler, image, signature, "static", updateIgnore); | 
| Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 134 | } | 
| Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 135 |  | 
| Patrick Venture | 5f2fcc4 | 2019-06-20 07:21:05 -0700 | [diff] [blame] | 136 | TEST_F(UpdaterTest, UpdateMainCleansUpOnFailure) | 
|  | 137 | { | 
|  | 138 | const std::string image = "image.bin"; | 
|  | 139 | const std::string signature = "signature.bin"; | 
|  | 140 | UpdateHandlerMock handler; | 
|  | 141 |  | 
|  | 142 | EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true)); | 
|  | 143 | EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return()); | 
|  | 144 | EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return()); | 
| Brandon Kim | 6749ba1 | 2019-09-19 13:31:37 -0700 | [diff] [blame^] | 145 | EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId, defaultIgnore)) | 
| Patrick Venture | 5f2fcc4 | 2019-06-20 07:21:05 -0700 | [diff] [blame] | 146 | .WillOnce(Return(false)); | 
|  | 147 | EXPECT_CALL(handler, cleanArtifacts()).WillOnce(Return()); | 
|  | 148 |  | 
| Brandon Kim | 6749ba1 | 2019-09-19 13:31:37 -0700 | [diff] [blame^] | 149 | EXPECT_THROW( | 
|  | 150 | updaterMain(&handler, image, signature, "static", defaultIgnore), | 
|  | 151 | ToolException); | 
| Patrick Venture | 5f2fcc4 | 2019-06-20 07:21:05 -0700 | [diff] [blame] | 152 | } | 
|  | 153 |  | 
| Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 154 | } // namespace host_tool |