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" |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 4 | #include "progress_mock.hpp" |
Patrick Venture | 8e1b233 | 2019-01-17 15:22:45 -0800 | [diff] [blame] | 5 | |
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 | |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 8 | #include <cstring> |
| 9 | |
Patrick Venture | 8e1b233 | 2019-01-17 15:22:45 -0800 | [diff] [blame] | 10 | #include <gtest/gtest.h> |
| 11 | |
| 12 | namespace host_tool |
| 13 | { |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 14 | namespace |
| 15 | { |
Patrick Venture | 8e1b233 | 2019-01-17 15:22:45 -0800 | [diff] [blame] | 16 | |
Patrick Venture | 7c79b25 | 2019-06-24 16:06:21 -0700 | [diff] [blame] | 17 | using ::testing::_; |
Patrick Venture | 8e1b233 | 2019-01-17 15:22:45 -0800 | [diff] [blame] | 18 | using ::testing::ContainerEq; |
Patrick Venture | 7c79b25 | 2019-06-24 16:06:21 -0700 | [diff] [blame] | 19 | using ::testing::Gt; |
| 20 | using ::testing::Invoke; |
| 21 | using ::testing::NotNull; |
| 22 | using ::testing::Return; |
| 23 | using ::testing::StrEq; |
Patrick Venture | 8e1b233 | 2019-01-17 15:22:45 -0800 | [diff] [blame] | 24 | |
| 25 | TEST(LpcHandleTest, verifySendsFileContents) |
| 26 | { |
Patrick Venture | 69a9e19 | 2019-01-17 16:02:33 -0800 | [diff] [blame] | 27 | internal::InternalSysMock sysMock; |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 28 | ipmiblob::BlobInterfaceMock blobMock; |
Patrick Venture | 46bdadc | 2019-01-18 09:04:16 -0800 | [diff] [blame] | 29 | HostIoInterfaceMock ioMock; |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 30 | ProgressMock progMock; |
Patrick Venture | 8e1b233 | 2019-01-17 15:22:45 -0800 | [diff] [blame] | 31 | |
Patrick Venture | 7c79b25 | 2019-06-24 16:06:21 -0700 | [diff] [blame] | 32 | const std::uint32_t address = 0xfedc1000; |
| 33 | const std::uint32_t length = 0x1000; |
| 34 | |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 35 | LpcDataHandler handler(&blobMock, &ioMock, address, length, &progMock, |
| 36 | &sysMock); |
Patrick Venture | 8e1b233 | 2019-01-17 15:22:45 -0800 | [diff] [blame] | 37 | std::uint16_t session = 0xbeef; |
| 38 | std::string filePath = "/asdf"; |
Patrick Venture | 7c79b25 | 2019-06-24 16:06:21 -0700 | [diff] [blame] | 39 | int fileDescriptor = 5; |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 40 | const int fakeFileSize = 100; |
Patrick Venture | 8e1b233 | 2019-01-17 15:22:45 -0800 | [diff] [blame] | 41 | |
| 42 | LpcRegion host_lpc_buf; |
Patrick Venture | 7c79b25 | 2019-06-24 16:06:21 -0700 | [diff] [blame] | 43 | host_lpc_buf.address = address; |
| 44 | host_lpc_buf.length = length; |
Patrick Venture | 8e1b233 | 2019-01-17 15:22:45 -0800 | [diff] [blame] | 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 | |
Patrick Venture | 7c79b25 | 2019-06-24 16:06:21 -0700 | [diff] [blame] | 51 | std::vector<std::uint8_t> data = {0x01, 0x02, 0x03}; |
| 52 | |
| 53 | EXPECT_CALL(sysMock, open(StrEq(filePath.c_str()), _)) |
| 54 | .WillOnce(Return(fileDescriptor)); |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 55 | EXPECT_CALL(sysMock, getSize(StrEq(filePath.c_str()))) |
| 56 | .WillOnce(Return(fakeFileSize)); |
Patrick Venture | 7c79b25 | 2019-06-24 16:06:21 -0700 | [diff] [blame] | 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 length, |
| 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)); |
Patrick Venture | 8e1b233 | 2019-01-17 15:22:45 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 78 | } // namespace |
Patrick Venture | 8e1b233 | 2019-01-17 15:22:45 -0800 | [diff] [blame] | 79 | } // namespace host_tool |