1 #include "data_interface_mock.hpp" 2 #include "status.hpp" 3 #include "tool_errors.hpp" 4 #include "updater.hpp" 5 #include "updater_mock.hpp" 6 #include "util.hpp" 7 8 #include <blobs-ipmid/blobs.hpp> 9 #include <ipmiblob/test/blob_interface_mock.hpp> 10 #include <string> 11 12 #include <gtest/gtest.h> 13 14 namespace host_tool 15 { 16 17 using ::testing::_; 18 using ::testing::Eq; 19 using ::testing::Return; 20 using ::testing::TypedEq; 21 22 class UpdateHandlerTest : public ::testing::Test 23 { 24 protected: 25 const std::uint16_t session = 0xbeef; 26 27 DataInterfaceMock handlerMock; 28 ipmiblob::BlobInterfaceMock blobMock; 29 UpdateHandler updater{&blobMock, &handlerMock}; 30 }; 31 32 TEST_F(UpdateHandlerTest, CheckAvailableSuccess) 33 { 34 ipmiblob::StatResponse statObj = {}; 35 statObj.blob_state = ipmi_flash::FirmwareBlobHandler::UpdateFlags::ipmi | 36 ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc; 37 38 EXPECT_CALL(blobMock, getBlobList()) 39 .WillOnce( 40 Return(std::vector<std::string>({ipmi_flash::staticLayoutBlobId}))); 41 EXPECT_CALL(blobMock, getStat(TypedEq<const std::string&>( 42 ipmi_flash::staticLayoutBlobId))) 43 .WillOnce(Return(statObj)); 44 45 EXPECT_CALL(handlerMock, supportedType()) 46 .WillOnce(Return(ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc)); 47 48 EXPECT_TRUE(updater.checkAvailable(ipmi_flash::staticLayoutBlobId)); 49 } 50 51 TEST_F(UpdateHandlerTest, SendFileSuccess) 52 { 53 /* Call sendFile to verify it does what we expect. */ 54 std::string firmwareImage = "image.bin"; 55 56 std::uint16_t supported = 57 static_cast<std::uint16_t>( 58 ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc) | 59 static_cast<std::uint16_t>(blobs::OpenFlags::write); 60 61 EXPECT_CALL(handlerMock, supportedType()) 62 .WillOnce(Return(ipmi_flash::FirmwareBlobHandler::UpdateFlags::lpc)); 63 64 EXPECT_CALL(blobMock, openBlob(ipmi_flash::staticLayoutBlobId, supported)) 65 .WillOnce(Return(session)); 66 67 EXPECT_CALL(handlerMock, sendContents(firmwareImage, session)) 68 .WillOnce(Return(true)); 69 70 EXPECT_CALL(blobMock, closeBlob(session)).Times(1); 71 72 updater.sendFile(ipmi_flash::staticLayoutBlobId, firmwareImage); 73 } 74 75 TEST_F(UpdateHandlerTest, VerifyFileHandleReturnsTrueOnSuccess) 76 { 77 EXPECT_CALL(blobMock, openBlob(ipmi_flash::verifyBlobId, _)) 78 .WillOnce(Return(session)); 79 EXPECT_CALL(blobMock, commit(session, _)).WillOnce(Return()); 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)); 84 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 99 TEST_F(UpdaterTest, PollStatusReturnsAfterSuccess) 100 { 101 ipmiblob::StatResponse verificationResponse = {}; 102 /* the other details of the response are ignored, and should be. */ 103 verificationResponse.metadata.push_back( 104 static_cast<std::uint8_t>(ipmi_flash::ActionStatus::success)); 105 106 EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session))) 107 .WillOnce(Return(verificationResponse)); 108 109 EXPECT_TRUE(pollStatus(session, &blobMock)); 110 } 111 112 TEST_F(UpdaterTest, PollStatusReturnsAfterFailure) 113 { 114 ipmiblob::StatResponse verificationResponse = {}; 115 /* the other details of the response are ignored, and should be. */ 116 verificationResponse.metadata.push_back( 117 static_cast<std::uint8_t>(ipmi_flash::ActionStatus::failed)); 118 119 EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session))) 120 .WillOnce(Return(verificationResponse)); 121 122 EXPECT_FALSE(pollStatus(session, &blobMock)); 123 } 124 125 TEST_F(UpdaterTest, UpdateMainReturnsSuccessIfAllSuccess) 126 { 127 const std::string image = "image.bin"; 128 const std::string signature = "signature.bin"; 129 UpdateHandlerMock handler; 130 131 EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true)); 132 EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return()); 133 EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return()); 134 EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId)) 135 .WillOnce(Return(true)); 136 EXPECT_CALL(handler, verifyFile(ipmi_flash::updateBlobId)) 137 .WillOnce(Return(true)); 138 139 updaterMain(&handler, image, signature); 140 } 141 142 TEST_F(UpdaterTest, UpdateMainCleansUpOnFailure) 143 { 144 const std::string image = "image.bin"; 145 const std::string signature = "signature.bin"; 146 UpdateHandlerMock handler; 147 148 EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true)); 149 EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return()); 150 EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return()); 151 EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId)) 152 .WillOnce(Return(false)); 153 EXPECT_CALL(handler, cleanArtifacts()).WillOnce(Return()); 154 155 EXPECT_THROW(updaterMain(&handler, image, signature), ToolException); 156 } 157 158 } // namespace host_tool 159