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