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