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