blob: 7699609fe9f82f09216a7a0f2f9042b9d0b57abc [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"
Patrick Venturecf9b2192019-06-27 12:09:52 -07004#include "progress_mock.hpp"
Patrick Venture8e1b2332019-01-17 15:22:45 -08005
Patrick Venture664c5bc2019-03-07 08:09:45 -08006#include <ipmiblob/test/blob_interface_mock.hpp>
Patrick Venture8e1b2332019-01-17 15:22:45 -08007
Patrick Venture9b37b092020-05-28 20:58:57 -07008#include <cstring>
9
Patrick Venture8e1b2332019-01-17 15:22:45 -080010#include <gtest/gtest.h>
11
12namespace host_tool
13{
Patrick Venturecf9b2192019-06-27 12:09:52 -070014namespace
15{
Patrick Venture8e1b2332019-01-17 15:22:45 -080016
Patrick Venture7c79b252019-06-24 16:06:21 -070017using ::testing::_;
Patrick Venture8e1b2332019-01-17 15:22:45 -080018using ::testing::ContainerEq;
Patrick Venture7c79b252019-06-24 16:06:21 -070019using ::testing::Gt;
20using ::testing::Invoke;
21using ::testing::NotNull;
22using ::testing::Return;
23using ::testing::StrEq;
Patrick Venture8e1b2332019-01-17 15:22:45 -080024
25TEST(LpcHandleTest, verifySendsFileContents)
26{
Patrick Venture69a9e192019-01-17 16:02:33 -080027 internal::InternalSysMock sysMock;
Patrick Venture664c5bc2019-03-07 08:09:45 -080028 ipmiblob::BlobInterfaceMock blobMock;
Patrick Venture46bdadc2019-01-18 09:04:16 -080029 HostIoInterfaceMock ioMock;
Patrick Venturecf9b2192019-06-27 12:09:52 -070030 ProgressMock progMock;
Patrick Venture8e1b2332019-01-17 15:22:45 -080031
Patrick Venture7c79b252019-06-24 16:06:21 -070032 const std::uint32_t address = 0xfedc1000;
33 const std::uint32_t length = 0x1000;
34
Patrick Venturecf9b2192019-06-27 12:09:52 -070035 LpcDataHandler handler(&blobMock, &ioMock, address, length, &progMock,
36 &sysMock);
Patrick Venture8e1b2332019-01-17 15:22:45 -080037 std::uint16_t session = 0xbeef;
38 std::string filePath = "/asdf";
Patrick Venture7c79b252019-06-24 16:06:21 -070039 int fileDescriptor = 5;
Patrick Venturecf9b2192019-06-27 12:09:52 -070040 const int fakeFileSize = 100;
Patrick Venture8e1b2332019-01-17 15:22:45 -080041
42 LpcRegion host_lpc_buf;
Patrick Venture7c79b252019-06-24 16:06:21 -070043 host_lpc_buf.address = address;
44 host_lpc_buf.length = length;
Patrick Venture8e1b2332019-01-17 15:22:45 -080045
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
Patrick Venture7c79b252019-06-24 16:06:21 -070051 std::vector<std::uint8_t> data = {0x01, 0x02, 0x03};
52
53 EXPECT_CALL(sysMock, open(StrEq(filePath.c_str()), _))
54 .WillOnce(Return(fileDescriptor));
Patrick Venturecf9b2192019-06-27 12:09:52 -070055 EXPECT_CALL(sysMock, getSize(StrEq(filePath.c_str())))
56 .WillOnce(Return(fakeFileSize));
Patrick Venture7c79b252019-06-24 16:06:21 -070057 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 }))
62 .WillOnce(Return(0));
63
64 EXPECT_CALL(ioMock, write(_, data.size(), _))
65 .WillOnce(Invoke([&data](const std::size_t, const std::size_t length,
66 const void* const source) {
67 EXPECT_THAT(std::memcmp(source, data.data(), data.size()), 0);
68 return true;
69 }));
70
71 EXPECT_CALL(blobMock, writeBytes(session, 0, _));
72
73 EXPECT_CALL(sysMock, close(fileDescriptor)).WillOnce(Return(0));
74
75 EXPECT_TRUE(handler.sendContents(filePath, session));
Patrick Venture8e1b2332019-01-17 15:22:45 -080076}
77
Patrick Venturecf9b2192019-06-27 12:09:52 -070078} // namespace
Patrick Venture8e1b2332019-01-17 15:22:45 -080079} // namespace host_tool