blob: 84a81eb7b67d047a6d6ae5bc070744fdc8356ded [file] [log] [blame]
Patrick Venture8e1b2332019-01-17 15:22:45 -08001#include "internal_sys_mock.hpp"
Patrick Venture46bdadc2019-01-18 09:04:16 -08002#include "io_mock.hpp"
Patrick Venture8e1b2332019-01-17 15:22:45 -08003#include "lpc.hpp"
4
5#include <cstring>
Patrick Venture664c5bc2019-03-07 08:09:45 -08006#include <ipmiblob/test/blob_interface_mock.hpp>
Patrick Venture8e1b2332019-01-17 15:22:45 -08007
8#include <gtest/gtest.h>
9
10namespace host_tool
11{
12
Patrick Venture7c79b252019-06-24 16:06:21 -070013using ::testing::_;
Patrick Venture8e1b2332019-01-17 15:22:45 -080014using ::testing::ContainerEq;
Patrick Venture7c79b252019-06-24 16:06:21 -070015using ::testing::Gt;
16using ::testing::Invoke;
17using ::testing::NotNull;
18using ::testing::Return;
19using ::testing::StrEq;
Patrick Venture8e1b2332019-01-17 15:22:45 -080020
21TEST(LpcHandleTest, verifySendsFileContents)
22{
Patrick Venture69a9e192019-01-17 16:02:33 -080023 internal::InternalSysMock sysMock;
Patrick Venture664c5bc2019-03-07 08:09:45 -080024 ipmiblob::BlobInterfaceMock blobMock;
Patrick Venture46bdadc2019-01-18 09:04:16 -080025 HostIoInterfaceMock ioMock;
Patrick Venture8e1b2332019-01-17 15:22:45 -080026
Patrick Venture7c79b252019-06-24 16:06:21 -070027 const std::uint32_t address = 0xfedc1000;
28 const std::uint32_t length = 0x1000;
29
30 LpcDataHandler handler(&blobMock, &ioMock, address, length, &sysMock);
Patrick Venture8e1b2332019-01-17 15:22:45 -080031 std::uint16_t session = 0xbeef;
32 std::string filePath = "/asdf";
Patrick Venture7c79b252019-06-24 16:06:21 -070033 int fileDescriptor = 5;
Patrick Venture8e1b2332019-01-17 15:22:45 -080034
35 LpcRegion host_lpc_buf;
Patrick Venture7c79b252019-06-24 16:06:21 -070036 host_lpc_buf.address = address;
37 host_lpc_buf.length = length;
Patrick Venture8e1b2332019-01-17 15:22:45 -080038
39 std::vector<std::uint8_t> bytes(sizeof(host_lpc_buf));
40 std::memcpy(bytes.data(), &host_lpc_buf, sizeof(host_lpc_buf));
41
42 EXPECT_CALL(blobMock, writeMeta(session, 0, ContainerEq(bytes)));
43
Patrick Venture7c79b252019-06-24 16:06:21 -070044 std::vector<std::uint8_t> data = {0x01, 0x02, 0x03};
45
46 EXPECT_CALL(sysMock, open(StrEq(filePath.c_str()), _))
47 .WillOnce(Return(fileDescriptor));
48 EXPECT_CALL(sysMock, read(_, NotNull(), Gt(data.size())))
49 .WillOnce(Invoke([&data](int, void* buf, std::size_t) {
50 std::memcpy(buf, data.data(), data.size());
51 return data.size();
52 }))
53 .WillOnce(Return(0));
54
55 EXPECT_CALL(ioMock, write(_, data.size(), _))
56 .WillOnce(Invoke([&data](const std::size_t, const std::size_t length,
57 const void* const source) {
58 EXPECT_THAT(std::memcmp(source, data.data(), data.size()), 0);
59 return true;
60 }));
61
62 EXPECT_CALL(blobMock, writeBytes(session, 0, _));
63
64 EXPECT_CALL(sysMock, close(fileDescriptor)).WillOnce(Return(0));
65
66 EXPECT_TRUE(handler.sendContents(filePath, session));
Patrick Venture8e1b2332019-01-17 15:22:45 -080067}
68
69} // namespace host_tool