blob: c764f544b91d509e3a9aac28f27a3ced0429a421 [file] [log] [blame]
Patrick Venture907f3a72019-01-15 14:13:37 -08001#include "bt.hpp"
2#include "internal_sys_mock.hpp"
Patrick Venturecf9b2192019-06-27 12:09:52 -07003#include "progress_mock.hpp"
Patrick Venture907f3a72019-01-15 14:13:37 -08004
Patrick Venture664c5bc2019-03-07 08:09:45 -08005#include <ipmiblob/test/blob_interface_mock.hpp>
Patrick Venture907f3a72019-01-15 14:13:37 -08006
Patrick Venture9b37b092020-05-28 20:58:57 -07007#include <cstring>
8
Patrick Venture907f3a72019-01-15 14:13:37 -08009#include <gtest/gtest.h>
10
11namespace host_tool
12{
Patrick Venturecf9b2192019-06-27 12:09:52 -070013namespace
14{
Patrick Venture907f3a72019-01-15 14:13:37 -080015
16using ::testing::_;
17using ::testing::ContainerEq;
18using ::testing::Eq;
19using ::testing::Invoke;
20using ::testing::NotNull;
21using ::testing::Return;
22
23TEST(BtHandlerTest, verifySendsFileContents)
24{
25 /* In this very basic test, we'll feed the bt handler data from the internal
26 * syscall mock and catch the writes via the blob mock.
27 */
28 internal::InternalSysMock sysMock;
Patrick Venture664c5bc2019-03-07 08:09:45 -080029 ipmiblob::BlobInterfaceMock blobMock;
Patrick Venturecf9b2192019-06-27 12:09:52 -070030 ProgressMock progMock;
Patrick Venture907f3a72019-01-15 14:13:37 -080031
Patrick Venturecf9b2192019-06-27 12:09:52 -070032 BtDataHandler handler(&blobMock, &progMock, &sysMock);
Patrick Venture907f3a72019-01-15 14:13:37 -080033 std::string filePath = "/asdf";
34 int fd = 1;
35 std::uint16_t session = 0xbeef;
36 std::vector<std::uint8_t> bytes = {'1', '2', '3', '4'};
Patrick Venturecf9b2192019-06-27 12:09:52 -070037 const int fakeFileSize = 100;
Patrick Venture907f3a72019-01-15 14:13:37 -080038
39 EXPECT_CALL(sysMock, open(Eq(filePath), _)).WillOnce(Return(fd));
Patrick Venturecf9b2192019-06-27 12:09:52 -070040 EXPECT_CALL(sysMock, getSize(Eq(filePath))).WillOnce(Return(fakeFileSize));
Patrick Venture907f3a72019-01-15 14:13:37 -080041 EXPECT_CALL(sysMock, read(fd, NotNull(), _))
42 .WillOnce(Invoke([&](int fd, void* buf, std::size_t count) {
43 EXPECT_TRUE(count > bytes.size());
44 std::memcpy(buf, bytes.data(), bytes.size());
45 return bytes.size();
46 }))
47 .WillOnce(Return(0));
48 EXPECT_CALL(sysMock, close(fd)).WillOnce(Return(0));
49
50 EXPECT_CALL(blobMock, writeBytes(session, 0, ContainerEq(bytes)));
51
52 EXPECT_TRUE(handler.sendContents(filePath, session));
53}
54
Patrick Venturecf9b2192019-06-27 12:09:52 -070055} // namespace
Patrick Venture907f3a72019-01-15 14:13:37 -080056} // namespace host_tool