summaryrefslogtreecommitdiffstats
path: root/tools/test/tools_bt_unittest.cpp
blob: cca0b3447f32c18272c220e000b1f1751c87d7c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "bt.hpp"
#include "internal_sys_mock.hpp"
#include "progress_mock.hpp"

#include <cstring>
#include <ipmiblob/test/blob_interface_mock.hpp>

#include <gtest/gtest.h>

namespace host_tool
{
namespace
{

using ::testing::_;
using ::testing::ContainerEq;
using ::testing::Eq;
using ::testing::Invoke;
using ::testing::NotNull;
using ::testing::Return;

TEST(BtHandlerTest, verifySendsFileContents)
{
    /* In this very basic test, we'll feed the bt handler data from the internal
     * syscall mock and catch the writes via the blob mock.
     */
    internal::InternalSysMock sysMock;
    ipmiblob::BlobInterfaceMock blobMock;
    ProgressMock progMock;

    BtDataHandler handler(&blobMock, &progMock, &sysMock);
    std::string filePath = "/asdf";
    int fd = 1;
    std::uint16_t session = 0xbeef;
    std::vector<std::uint8_t> bytes = {'1', '2', '3', '4'};
    const int fakeFileSize = 100;

    EXPECT_CALL(sysMock, open(Eq(filePath), _)).WillOnce(Return(fd));
    EXPECT_CALL(sysMock, getSize(Eq(filePath))).WillOnce(Return(fakeFileSize));
    EXPECT_CALL(sysMock, read(fd, NotNull(), _))
        .WillOnce(Invoke([&](int fd, void* buf, std::size_t count) {
            EXPECT_TRUE(count > bytes.size());
            std::memcpy(buf, bytes.data(), bytes.size());
            return bytes.size();
        }))
        .WillOnce(Return(0));
    EXPECT_CALL(sysMock, close(fd)).WillOnce(Return(0));

    EXPECT_CALL(blobMock, writeBytes(session, 0, ContainerEq(bytes)));

    EXPECT_TRUE(handler.sendContents(filePath, session));
}

} // namespace
} // namespace host_tool
OpenPOWER on IntegriCloud