Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame^] | 1 | #include "blob_interface_mock.hpp" |
| 2 | #include "bt.hpp" |
| 3 | #include "internal_sys_mock.hpp" |
| 4 | |
| 5 | #include <cstring> |
| 6 | |
| 7 | #include <gtest/gtest.h> |
| 8 | |
| 9 | namespace host_tool |
| 10 | { |
| 11 | |
| 12 | using ::testing::_; |
| 13 | using ::testing::ContainerEq; |
| 14 | using ::testing::Eq; |
| 15 | using ::testing::Invoke; |
| 16 | using ::testing::NotNull; |
| 17 | using ::testing::Return; |
| 18 | |
| 19 | TEST(BtHandlerTest, verifySendsFileContents) |
| 20 | { |
| 21 | /* In this very basic test, we'll feed the bt handler data from the internal |
| 22 | * syscall mock and catch the writes via the blob mock. |
| 23 | */ |
| 24 | internal::InternalSysMock sysMock; |
| 25 | BlobInterfaceMock blobMock; |
| 26 | |
| 27 | BtDataHandler handler(&blobMock, &sysMock); |
| 28 | std::string filePath = "/asdf"; |
| 29 | int fd = 1; |
| 30 | std::uint16_t session = 0xbeef; |
| 31 | std::vector<std::uint8_t> bytes = {'1', '2', '3', '4'}; |
| 32 | |
| 33 | EXPECT_CALL(sysMock, open(Eq(filePath), _)).WillOnce(Return(fd)); |
| 34 | EXPECT_CALL(sysMock, read(fd, NotNull(), _)) |
| 35 | .WillOnce(Invoke([&](int fd, void* buf, std::size_t count) { |
| 36 | EXPECT_TRUE(count > bytes.size()); |
| 37 | std::memcpy(buf, bytes.data(), bytes.size()); |
| 38 | return bytes.size(); |
| 39 | })) |
| 40 | .WillOnce(Return(0)); |
| 41 | EXPECT_CALL(sysMock, close(fd)).WillOnce(Return(0)); |
| 42 | |
| 43 | EXPECT_CALL(blobMock, writeBytes(session, 0, ContainerEq(bytes))); |
| 44 | |
| 45 | EXPECT_TRUE(handler.sendContents(filePath, session)); |
| 46 | } |
| 47 | |
| 48 | } // namespace host_tool |