blob: cca0b3447f32c18272c220e000b1f1751c87d7c8 [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
5#include <cstring>
Patrick Venture664c5bc2019-03-07 08:09:45 -08006#include <ipmiblob/test/blob_interface_mock.hpp>
Patrick Venture907f3a72019-01-15 14:13:37 -08007
8#include <gtest/gtest.h>
9
10namespace host_tool
11{
Patrick Venturecf9b2192019-06-27 12:09:52 -070012namespace
13{
Patrick Venture907f3a72019-01-15 14:13:37 -080014
15using ::testing::_;
16using ::testing::ContainerEq;
17using ::testing::Eq;
18using ::testing::Invoke;
19using ::testing::NotNull;
20using ::testing::Return;
21
22TEST(BtHandlerTest, verifySendsFileContents)
23{
24 /* In this very basic test, we'll feed the bt handler data from the internal
25 * syscall mock and catch the writes via the blob mock.
26 */
27 internal::InternalSysMock sysMock;
Patrick Venture664c5bc2019-03-07 08:09:45 -080028 ipmiblob::BlobInterfaceMock blobMock;
Patrick Venturecf9b2192019-06-27 12:09:52 -070029 ProgressMock progMock;
Patrick Venture907f3a72019-01-15 14:13:37 -080030
Patrick Venturecf9b2192019-06-27 12:09:52 -070031 BtDataHandler handler(&blobMock, &progMock, &sysMock);
Patrick Venture907f3a72019-01-15 14:13:37 -080032 std::string filePath = "/asdf";
33 int fd = 1;
34 std::uint16_t session = 0xbeef;
35 std::vector<std::uint8_t> bytes = {'1', '2', '3', '4'};
Patrick Venturecf9b2192019-06-27 12:09:52 -070036 const int fakeFileSize = 100;
Patrick Venture907f3a72019-01-15 14:13:37 -080037
38 EXPECT_CALL(sysMock, open(Eq(filePath), _)).WillOnce(Return(fd));
Patrick Venturecf9b2192019-06-27 12:09:52 -070039 EXPECT_CALL(sysMock, getSize(Eq(filePath))).WillOnce(Return(fakeFileSize));
Patrick Venture907f3a72019-01-15 14:13:37 -080040 EXPECT_CALL(sysMock, read(fd, NotNull(), _))
41 .WillOnce(Invoke([&](int fd, void* buf, std::size_t count) {
42 EXPECT_TRUE(count > bytes.size());
43 std::memcpy(buf, bytes.data(), bytes.size());
44 return bytes.size();
45 }))
46 .WillOnce(Return(0));
47 EXPECT_CALL(sysMock, close(fd)).WillOnce(Return(0));
48
49 EXPECT_CALL(blobMock, writeBytes(session, 0, ContainerEq(bytes)));
50
51 EXPECT_TRUE(handler.sendContents(filePath, session));
52}
53
Patrick Venturecf9b2192019-06-27 12:09:52 -070054} // namespace
Patrick Venture907f3a72019-01-15 14:13:37 -080055} // namespace host_tool