#include "data_interface_mock.hpp" #include "status.hpp" #include "updater.hpp" #include "updater_mock.hpp" #include "util.hpp" #include #include #include #include namespace host_tool { using ::testing::_; using ::testing::Eq; using ::testing::Return; using ::testing::TypedEq; class UpdateHandlerTest : public ::testing::Test { protected: const std::uint16_t session = 0xbeef; DataInterfaceMock handlerMock; ipmiblob::BlobInterfaceMock blobMock; UpdateHandler updater{&blobMock, &handlerMock}; }; TEST_F(UpdateHandlerTest, CheckAvailableSuccess) { ipmiblob::StatResponse statObj = {}; statObj.blob_state = ipmi_flash::FirmwareBlobHandler::UpdateFlags::ipmi | ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc; EXPECT_CALL(blobMock, getBlobList()) .WillOnce( Return(std::vector({ipmi_flash::staticLayoutBlobId}))); EXPECT_CALL(blobMock, getStat(TypedEq( ipmi_flash::staticLayoutBlobId))) .WillOnce(Return(statObj)); EXPECT_CALL(handlerMock, supportedType()) .WillOnce(Return(ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc)); EXPECT_TRUE(updater.checkAvailable(ipmi_flash::staticLayoutBlobId)); } TEST_F(UpdateHandlerTest, SendFileSuccess) { /* Call sendFile to verify it does what we expect. */ std::string firmwareImage = "image.bin"; std::uint16_t supported = static_cast( ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc) | static_cast(blobs::OpenFlags::write); EXPECT_CALL(handlerMock, supportedType()) .WillOnce(Return(ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc)); EXPECT_CALL(blobMock, openBlob(ipmi_flash::staticLayoutBlobId, supported)) .WillOnce(Return(session)); EXPECT_CALL(handlerMock, sendContents(firmwareImage, session)) .WillOnce(Return(true)); EXPECT_CALL(blobMock, closeBlob(session)).Times(1); updater.sendFile(ipmi_flash::staticLayoutBlobId, firmwareImage); } TEST_F(UpdateHandlerTest, VerifyFileHandleReturnsTrueOnSuccess) { EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _)) .WillOnce(Return(session)); EXPECT_CALL(blobMock, commit(session, _)).WillOnce(Return()); ipmiblob::StatResponse verificationResponse = {}; /* the other details of the response are ignored, and should be. */ verificationResponse.metadata.push_back( static_cast(ipmi_flash::ActionStatus::success)); EXPECT_CALL(blobMock, getStat(TypedEq(session))) .WillOnce(Return(verificationResponse)); EXPECT_CALL(blobMock, closeBlob(session)).WillOnce(Return()); EXPECT_TRUE(updater.verifyFile(ipmi_flash::verifyBlobId)); } class UpdaterTest : public ::testing::Test { protected: ipmiblob::BlobInterfaceMock blobMock; std::uint16_t session = 0xbeef; }; TEST_F(UpdaterTest, PollStatusReturnsAfterSuccess) { ipmiblob::StatResponse verificationResponse = {}; /* the other details of the response are ignored, and should be. */ verificationResponse.metadata.push_back( static_cast(ipmi_flash::ActionStatus::success)); EXPECT_CALL(blobMock, getStat(TypedEq(session))) .WillOnce(Return(verificationResponse)); EXPECT_TRUE(pollStatus(session, &blobMock)); } TEST_F(UpdaterTest, PollStatusReturnsAfterFailure) { ipmiblob::StatResponse verificationResponse = {}; /* the other details of the response are ignored, and should be. */ verificationResponse.metadata.push_back( static_cast(ipmi_flash::ActionStatus::failed)); EXPECT_CALL(blobMock, getStat(TypedEq(session))) .WillOnce(Return(verificationResponse)); EXPECT_FALSE(pollStatus(session, &blobMock)); } TEST_F(UpdaterTest, UpdateMainReturnsSuccessIfAllSuccess) { const std::string image = "image.bin"; const std::string signature = "signature.bin"; UpdateHandlerMock handler; EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true)); EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return()); EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return()); EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId)) .WillOnce(Return(true)); EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId)) .WillOnce(Return(true)); updaterMain(&handler, image, signature); } } // namespace host_tool