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