1 #include "internal_sys_mock.hpp"
2 #include "io_mock.hpp"
3 #include "lpc.hpp"
4 #include "progress_mock.hpp"
5 
6 #include <ipmiblob/test/blob_interface_mock.hpp>
7 
8 #include <cstring>
9 
10 #include <gtest/gtest.h>
11 
12 namespace host_tool
13 {
14 namespace
15 {
16 
17 using ::testing::_;
18 using ::testing::ContainerEq;
19 using ::testing::Gt;
20 using ::testing::Invoke;
21 using ::testing::NotNull;
22 using ::testing::Return;
23 using ::testing::StrEq;
24 
TEST(LpcHandleTest,verifySendsFileContents)25 TEST(LpcHandleTest, verifySendsFileContents)
26 {
27     internal::InternalSysMock sysMock;
28     ipmiblob::BlobInterfaceMock blobMock;
29     HostIoInterfaceMock ioMock;
30     ProgressMock progMock;
31 
32     const std::uint32_t address = 0xfedc1000;
33     const std::uint32_t length = 0x1000;
34 
35     LpcDataHandler handler(&blobMock, &ioMock, address, length, &progMock,
36                            &sysMock);
37     std::uint16_t session = 0xbeef;
38     std::string filePath = "/asdf";
39     int fileDescriptor = 5;
40     const int fakeFileSize = 100;
41 
42     LpcRegion host_lpc_buf;
43     host_lpc_buf.address = address;
44     host_lpc_buf.length = length;
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 
51     std::vector<std::uint8_t> data = {0x01, 0x02, 0x03};
52 
53     EXPECT_CALL(sysMock, open(StrEq(filePath.c_str()), _))
54         .WillOnce(Return(fileDescriptor));
55     EXPECT_CALL(sysMock, getSize(StrEq(filePath.c_str())))
56         .WillOnce(Return(fakeFileSize));
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     })).WillOnce(Return(0));
62 
63     EXPECT_CALL(ioMock, write(_, data.size(), _))
64         .WillOnce(Invoke([&data](const std::size_t, const std::size_t,
65                                  const void* const source) {
66         EXPECT_THAT(std::memcmp(source, data.data(), data.size()), 0);
67         return true;
68     }));
69 
70     EXPECT_CALL(blobMock, writeBytes(session, 0, _));
71 
72     EXPECT_CALL(sysMock, close(fileDescriptor)).WillOnce(Return(0));
73 
74     EXPECT_TRUE(handler.sendContents(filePath, session));
75 }
76 
77 } // namespace
78 } // namespace host_tool
79