1 #include "internal_sys_mock.hpp" 2 #include "io_mock.hpp" 3 #include "lpc.hpp" 4 #include "progress_mock.hpp" 5 6 #include <ipmiblob/test/blob_interface_mock.hpp> 7 8 #include <cstring> 9 10 #include <gtest/gtest.h> 11 12 namespace host_tool 13 { 14 namespace 15 { 16 17 using ::testing::_; 18 using ::testing::ContainerEq; 19 using ::testing::Gt; 20 using ::testing::Invoke; 21 using ::testing::NotNull; 22 using ::testing::Return; 23 using ::testing::StrEq; 24 25 TEST(LpcHandleTest, verifySendsFileContents) 26 { 27 internal::InternalSysMock sysMock; 28 ipmiblob::BlobInterfaceMock blobMock; 29 HostIoInterfaceMock ioMock; 30 ProgressMock progMock; 31 32 const std::uint32_t address = 0xfedc1000; 33 const std::uint32_t length = 0x1000; 34 35 LpcDataHandler handler(&blobMock, &ioMock, address, length, &progMock, 36 &sysMock); 37 std::uint16_t session = 0xbeef; 38 std::string filePath = "/asdf"; 39 int fileDescriptor = 5; 40 const int fakeFileSize = 100; 41 42 LpcRegion host_lpc_buf; 43 host_lpc_buf.address = address; 44 host_lpc_buf.length = length; 45 46 std::vector<std::uint8_t> bytes(sizeof(host_lpc_buf)); 47 std::memcpy(bytes.data(), &host_lpc_buf, sizeof(host_lpc_buf)); 48 49 EXPECT_CALL(blobMock, writeMeta(session, 0, ContainerEq(bytes))); 50 51 std::vector<std::uint8_t> data = {0x01, 0x02, 0x03}; 52 53 EXPECT_CALL(sysMock, open(StrEq(filePath.c_str()), _)) 54 .WillOnce(Return(fileDescriptor)); 55 EXPECT_CALL(sysMock, getSize(StrEq(filePath.c_str()))) 56 .WillOnce(Return(fakeFileSize)); 57 EXPECT_CALL(sysMock, read(_, NotNull(), Gt(data.size()))) 58 .WillOnce(Invoke([&data](int, void* buf, std::size_t) { 59 std::memcpy(buf, data.data(), data.size()); 60 return data.size(); 61 })) 62 .WillOnce(Return(0)); 63 64 EXPECT_CALL(ioMock, write(_, data.size(), _)) 65 .WillOnce(Invoke([&data](const std::size_t, const std::size_t, 66 const void* const source) { 67 EXPECT_THAT(std::memcmp(source, data.data(), data.size()), 0); 68 return true; 69 })); 70 71 EXPECT_CALL(blobMock, writeBytes(session, 0, _)); 72 73 EXPECT_CALL(sysMock, close(fileDescriptor)).WillOnce(Return(0)); 74 75 EXPECT_TRUE(handler.sendContents(filePath, session)); 76 } 77 78 } // namespace 79 } // namespace host_tool 80