blob: 24511c38afd4e19bbe9a36bd71f16872d247ae75 [file] [log] [blame]
Patrick Venture907f3a72019-01-15 14:13:37 -08001#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
9namespace host_tool
10{
11
12using ::testing::_;
13using ::testing::ContainerEq;
14using ::testing::Eq;
15using ::testing::Invoke;
16using ::testing::NotNull;
17using ::testing::Return;
18
19TEST(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