1380832ccSPatrick Venture #include "internal_sys_mock.hpp"
2380832ccSPatrick Venture #include "io_mock.hpp"
3380832ccSPatrick Venture #include "lpc.hpp"
4cf9b2195SPatrick Venture #include "progress_mock.hpp"
5380832ccSPatrick Venture 
6380832ccSPatrick Venture #include <ipmiblob/test/blob_interface_mock.hpp>
7380832ccSPatrick Venture 
89b37b095SPatrick Venture #include <cstring>
99b37b095SPatrick Venture 
10380832ccSPatrick Venture #include <gtest/gtest.h>
11380832ccSPatrick Venture 
12380832ccSPatrick Venture namespace host_tool
13380832ccSPatrick Venture {
14cf9b2195SPatrick Venture namespace
15cf9b2195SPatrick Venture {
16380832ccSPatrick Venture 
177c79b252SPatrick Venture using ::testing::_;
18380832ccSPatrick Venture using ::testing::ContainerEq;
197c79b252SPatrick Venture using ::testing::Gt;
207c79b252SPatrick Venture using ::testing::Invoke;
217c79b252SPatrick Venture using ::testing::NotNull;
227c79b252SPatrick Venture using ::testing::Return;
237c79b252SPatrick Venture using ::testing::StrEq;
24380832ccSPatrick Venture 
TEST(LpcHandleTest,verifySendsFileContents)25380832ccSPatrick Venture TEST(LpcHandleTest, verifySendsFileContents)
26380832ccSPatrick Venture {
27380832ccSPatrick Venture     internal::InternalSysMock sysMock;
28380832ccSPatrick Venture     ipmiblob::BlobInterfaceMock blobMock;
29380832ccSPatrick Venture     HostIoInterfaceMock ioMock;
30cf9b2195SPatrick Venture     ProgressMock progMock;
31380832ccSPatrick Venture 
327c79b252SPatrick Venture     const std::uint32_t address = 0xfedc1000;
337c79b252SPatrick Venture     const std::uint32_t length = 0x1000;
347c79b252SPatrick Venture 
35cf9b2195SPatrick Venture     LpcDataHandler handler(&blobMock, &ioMock, address, length, &progMock,
36cf9b2195SPatrick Venture                            &sysMock);
37380832ccSPatrick Venture     std::uint16_t session = 0xbeef;
38380832ccSPatrick Venture     std::string filePath = "/asdf";
397c79b252SPatrick Venture     int fileDescriptor = 5;
40cf9b2195SPatrick Venture     const int fakeFileSize = 100;
41380832ccSPatrick Venture 
42380832ccSPatrick Venture     LpcRegion host_lpc_buf;
437c79b252SPatrick Venture     host_lpc_buf.address = address;
447c79b252SPatrick Venture     host_lpc_buf.length = length;
45380832ccSPatrick Venture 
46380832ccSPatrick Venture     std::vector<std::uint8_t> bytes(sizeof(host_lpc_buf));
47380832ccSPatrick Venture     std::memcpy(bytes.data(), &host_lpc_buf, sizeof(host_lpc_buf));
48380832ccSPatrick Venture 
49380832ccSPatrick Venture     EXPECT_CALL(blobMock, writeMeta(session, 0, ContainerEq(bytes)));
50380832ccSPatrick Venture 
517c79b252SPatrick Venture     std::vector<std::uint8_t> data = {0x01, 0x02, 0x03};
527c79b252SPatrick Venture 
537c79b252SPatrick Venture     EXPECT_CALL(sysMock, open(StrEq(filePath.c_str()), _))
547c79b252SPatrick Venture         .WillOnce(Return(fileDescriptor));
55cf9b2195SPatrick Venture     EXPECT_CALL(sysMock, getSize(StrEq(filePath.c_str())))
56cf9b2195SPatrick Venture         .WillOnce(Return(fakeFileSize));
577c79b252SPatrick Venture     EXPECT_CALL(sysMock, read(_, NotNull(), Gt(data.size())))
587c79b252SPatrick Venture         .WillOnce(Invoke([&data](int, void* buf, std::size_t) {
597c79b252SPatrick Venture         std::memcpy(buf, data.data(), data.size());
607c79b252SPatrick Venture         return data.size();
61*a9423469SPatrick Williams     })).WillOnce(Return(0));
627c79b252SPatrick Venture 
637c79b252SPatrick Venture     EXPECT_CALL(ioMock, write(_, data.size(), _))
64b487eb47SWilly Tu         .WillOnce(Invoke([&data](const std::size_t, const std::size_t,
657c79b252SPatrick Venture                                  const void* const source) {
667c79b252SPatrick Venture         EXPECT_THAT(std::memcmp(source, data.data(), data.size()), 0);
677c79b252SPatrick Venture         return true;
687c79b252SPatrick Venture     }));
697c79b252SPatrick Venture 
707c79b252SPatrick Venture     EXPECT_CALL(blobMock, writeBytes(session, 0, _));
717c79b252SPatrick Venture 
727c79b252SPatrick Venture     EXPECT_CALL(sysMock, close(fileDescriptor)).WillOnce(Return(0));
737c79b252SPatrick Venture 
747c79b252SPatrick Venture     EXPECT_TRUE(handler.sendContents(filePath, session));
75380832ccSPatrick Venture }
76380832ccSPatrick Venture 
77cf9b2195SPatrick Venture } // namespace
78380832ccSPatrick Venture } // namespace host_tool
79