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