blob: 9e417ed1cef7b490fe80cacd8a08a4621bb02524 [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
6#include <cstring>
Patrick Venture664c5bc2019-03-07 08:09:45 -08007#include <ipmiblob/test/blob_interface_mock.hpp>
Patrick Venture8e1b2332019-01-17 15:22:45 -08008
9#include <gtest/gtest.h>
10
11namespace host_tool
12{
Patrick Venturecf9b2192019-06-27 12:09:52 -070013namespace
14{
Patrick Venture8e1b2332019-01-17 15:22:45 -080015
Patrick Venture7c79b252019-06-24 16:06:21 -070016using ::testing::_;
Patrick Venture8e1b2332019-01-17 15:22:45 -080017using ::testing::ContainerEq;
Patrick Venture7c79b252019-06-24 16:06:21 -070018using ::testing::Gt;
19using ::testing::Invoke;
20using ::testing::NotNull;
21using ::testing::Return;
22using ::testing::StrEq;
Patrick Venture8e1b2332019-01-17 15:22:45 -080023
24TEST(LpcHandleTest, verifySendsFileContents)
25{
Patrick Venture69a9e192019-01-17 16:02:33 -080026 internal::InternalSysMock sysMock;
Patrick Venture664c5bc2019-03-07 08:09:45 -080027 ipmiblob::BlobInterfaceMock blobMock;
Patrick Venture46bdadc2019-01-18 09:04:16 -080028 HostIoInterfaceMock ioMock;
Patrick Venturecf9b2192019-06-27 12:09:52 -070029 ProgressMock progMock;
Patrick Venture8e1b2332019-01-17 15:22:45 -080030
Patrick Venture7c79b252019-06-24 16:06:21 -070031 const std::uint32_t address = 0xfedc1000;
32 const std::uint32_t length = 0x1000;
33
Patrick Venturecf9b2192019-06-27 12:09:52 -070034 LpcDataHandler handler(&blobMock, &ioMock, address, length, &progMock,
35 &sysMock);
Patrick Venture8e1b2332019-01-17 15:22:45 -080036 std::uint16_t session = 0xbeef;
37 std::string filePath = "/asdf";
Patrick Venture7c79b252019-06-24 16:06:21 -070038 int fileDescriptor = 5;
Patrick Venturecf9b2192019-06-27 12:09:52 -070039 const int fakeFileSize = 100;
Patrick Venture8e1b2332019-01-17 15:22:45 -080040
41 LpcRegion host_lpc_buf;
Patrick Venture7c79b252019-06-24 16:06:21 -070042 host_lpc_buf.address = address;
43 host_lpc_buf.length = length;
Patrick Venture8e1b2332019-01-17 15:22:45 -080044
45 std::vector<std::uint8_t> bytes(sizeof(host_lpc_buf));
46 std::memcpy(bytes.data(), &host_lpc_buf, sizeof(host_lpc_buf));
47
48 EXPECT_CALL(blobMock, writeMeta(session, 0, ContainerEq(bytes)));
49
Patrick Venture7c79b252019-06-24 16:06:21 -070050 std::vector<std::uint8_t> data = {0x01, 0x02, 0x03};
51
52 EXPECT_CALL(sysMock, open(StrEq(filePath.c_str()), _))
53 .WillOnce(Return(fileDescriptor));
Patrick Venturecf9b2192019-06-27 12:09:52 -070054 EXPECT_CALL(sysMock, getSize(StrEq(filePath.c_str())))
55 .WillOnce(Return(fakeFileSize));
Patrick Venture7c79b252019-06-24 16:06:21 -070056 EXPECT_CALL(sysMock, read(_, NotNull(), Gt(data.size())))
57 .WillOnce(Invoke([&data](int, void* buf, std::size_t) {
58 std::memcpy(buf, data.data(), data.size());
59 return data.size();
60 }))
61 .WillOnce(Return(0));
62
63 EXPECT_CALL(ioMock, write(_, data.size(), _))
64 .WillOnce(Invoke([&data](const std::size_t, const std::size_t length,
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));
Patrick Venture8e1b2332019-01-17 15:22:45 -080075}
76
Patrick Venturecf9b2192019-06-27 12:09:52 -070077} // namespace
Patrick Venture8e1b2332019-01-17 15:22:45 -080078} // namespace host_tool