Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 1 | #include "data_interface_mock.hpp" |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 2 | #include "status.hpp" |
Patrick Venture | 5f2fcc4 | 2019-06-20 07:21:05 -0700 | [diff] [blame] | 3 | #include "tool_errors.hpp" |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 4 | #include "updater.hpp" |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 5 | #include "updater_mock.hpp" |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 6 | #include "util.hpp" |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 7 | |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 8 | #include <blobs-ipmid/blobs.hpp> |
| 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 | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 35 | statObj.blob_state = ipmi_flash::FirmwareBlobHandler::UpdateFlags::ipmi | |
| 36 | ipmi_flash::FirmwareBlobHandler::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 | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 46 | .WillOnce(Return(ipmi_flash::FirmwareBlobHandler::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 | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 58 | ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc) | |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 59 | static_cast<std::uint16_t>(blobs::OpenFlags::write); |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 60 | |
| 61 | EXPECT_CALL(handlerMock, supportedType()) |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 62 | .WillOnce(Return(ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc)); |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 63 | |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 64 | EXPECT_CALL(blobMock, openBlob(ipmi_flash::staticLayoutBlobId, supported)) |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 65 | .WillOnce(Return(session)); |
| 66 | |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 67 | EXPECT_CALL(handlerMock, sendContents(firmwareImage, session)) |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 68 | .WillOnce(Return(true)); |
| 69 | |
| 70 | EXPECT_CALL(blobMock, closeBlob(session)).Times(1); |
| 71 | |
Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 72 | updater.sendFile(ipmi_flash::staticLayoutBlobId, firmwareImage); |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 75 | TEST_F(UpdateHandlerTest, VerifyFileHandleReturnsTrueOnSuccess) |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 76 | { |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 77 | EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _)) |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 78 | .WillOnce(Return(session)); |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 79 | EXPECT_CALL(blobMock, commit(session, _)).WillOnce(Return()); |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 80 | ipmiblob::StatResponse verificationResponse = {}; |
| 81 | /* the other details of the response are ignored, and should be. */ |
| 82 | verificationResponse.metadata.push_back( |
| 83 | static_cast<std::uint8_t>(ipmi_flash::ActionStatus::success)); |
Patrick Venture | 7dcca5d | 2019-05-15 12:32:33 -0700 | [diff] [blame] | 84 | |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 85 | EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session))) |
| 86 | .WillOnce(Return(verificationResponse)); |
| 87 | EXPECT_CALL(blobMock, closeBlob(session)).WillOnce(Return()); |
| 88 | |
| 89 | EXPECT_TRUE(updater.verifyFile(ipmi_flash::verifyBlobId)); |
| 90 | } |
| 91 | |
| 92 | class UpdaterTest : public ::testing::Test |
| 93 | { |
| 94 | protected: |
| 95 | ipmiblob::BlobInterfaceMock blobMock; |
| 96 | std::uint16_t session = 0xbeef; |
| 97 | }; |
| 98 | |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 99 | TEST_F(UpdaterTest, UpdateMainReturnsSuccessIfAllSuccess) |
| 100 | { |
| 101 | const std::string image = "image.bin"; |
| 102 | const std::string signature = "signature.bin"; |
| 103 | UpdateHandlerMock handler; |
| 104 | |
| 105 | EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true)); |
| 106 | EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return()); |
| 107 | EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return()); |
| 108 | EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId)) |
| 109 | .WillOnce(Return(true)); |
| 110 | EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId)) |
| 111 | .WillOnce(Return(true)); |
| 112 | |
| 113 | updaterMain(&handler, image, signature); |
| 114 | } |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 115 | |
Patrick Venture | 5f2fcc4 | 2019-06-20 07:21:05 -0700 | [diff] [blame] | 116 | TEST_F(UpdaterTest, UpdateMainCleansUpOnFailure) |
| 117 | { |
| 118 | const std::string image = "image.bin"; |
| 119 | const std::string signature = "signature.bin"; |
| 120 | UpdateHandlerMock handler; |
| 121 | |
| 122 | EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true)); |
| 123 | EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return()); |
| 124 | EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return()); |
| 125 | EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId)) |
| 126 | .WillOnce(Return(false)); |
| 127 | EXPECT_CALL(handler, cleanArtifacts()).WillOnce(Return()); |
| 128 | |
| 129 | EXPECT_THROW(updaterMain(&handler, image, signature), ToolException); |
| 130 | } |
| 131 | |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 132 | } // namespace host_tool |