1 #include "bt.hpp"
2 #include "internal_sys_mock.hpp"
3 #include "progress_mock.hpp"
4 
5 #include <ipmiblob/blob_errors.hpp>
6 #include <ipmiblob/test/blob_interface_mock.hpp>
7 
8 #include <cstring>
9 #include <memory>
10 #include <string>
11 #include <vector>
12 
13 #include <gmock/gmock.h>
14 #include <gtest/gtest.h>
15 
16 namespace host_tool
17 {
18 namespace
19 {
20 
21 using ::testing::_;
22 using ::testing::ContainerEq;
23 using ::testing::Eq;
24 using ::testing::Invoke;
25 using ::testing::NotNull;
26 using ::testing::Return;
27 using ::testing::Throw;
28 
29 class BtHandlerTest : public ::testing::Test
30 {
31   protected:
32     static constexpr std::uint16_t session = 0xbeef;
33 
BtHandlerTest()34     BtHandlerTest() :
35         handler(std::make_unique<BtDataHandler>(&blobMock, &progMock, &sysMock))
36     {}
37 
38     internal::InternalSysMock sysMock;
39     ipmiblob::BlobInterfaceMock blobMock;
40     ProgressMock progMock;
41     std::unique_ptr<BtDataHandler> handler;
42     const std::string filePath = "/asdf";
43 };
44 
TEST_F(BtHandlerTest,verifySendsFileContents)45 TEST_F(BtHandlerTest, verifySendsFileContents)
46 {
47     /* In this very basic test, we'll feed the bt handler data from the internal
48      * syscall mock and catch the writes via the blob mock.
49      */
50     int fd = 1;
51     std::vector<std::uint8_t> bytes = {'1', '2', '3', '4'};
52     const int fakeFileSize = 100;
53 
54     EXPECT_CALL(sysMock, open(Eq(filePath), _)).WillOnce(Return(fd));
55     EXPECT_CALL(sysMock, getSize(Eq(filePath))).WillOnce(Return(fakeFileSize));
56 
57     EXPECT_CALL(progMock, start(fakeFileSize));
58 
59     EXPECT_CALL(sysMock, read(fd, NotNull(), _))
60         .WillOnce(Invoke([&](int, void* buf, std::size_t count) {
61         EXPECT_TRUE(count > bytes.size());
62         std::memcpy(buf, bytes.data(), bytes.size());
63         return bytes.size();
64     })).WillOnce(Return(0));
65 
66     EXPECT_CALL(progMock, updateProgress(bytes.size()));
67 
68     EXPECT_CALL(sysMock, close(fd)).WillOnce(Return(0));
69 
70     EXPECT_CALL(blobMock, writeBytes(session, 0, ContainerEq(bytes)));
71 
72     EXPECT_TRUE(handler->sendContents(filePath, session));
73 }
74 
TEST_F(BtHandlerTest,sendContentsFailsToOpenFile)75 TEST_F(BtHandlerTest, sendContentsFailsToOpenFile)
76 {
77     /* If the file doesn't exist or the permissions are wrong, the sendContents
78      * will return failure.
79      */
80     EXPECT_CALL(sysMock, open(Eq(filePath), _)).WillOnce(Return(-1));
81 
82     EXPECT_FALSE(handler->sendContents(filePath, session));
83 }
84 
TEST_F(BtHandlerTest,sendContentsFailsWhenBlobHandlerThrows)85 TEST_F(BtHandlerTest, sendContentsFailsWhenBlobHandlerThrows)
86 {
87     /* The blob handler throws an exception which is caught, the file is closed,
88      * and false is returned.
89      */
90 
91     int fd = 1;
92     std::vector<std::uint8_t> bytes = {'1', '2', '3', '4'};
93     const int fakeFileSize = 100;
94 
95     EXPECT_CALL(sysMock, open(Eq(filePath), _)).WillOnce(Return(fd));
96     EXPECT_CALL(sysMock, getSize(Eq(filePath))).WillOnce(Return(fakeFileSize));
97 
98     EXPECT_CALL(progMock, start(fakeFileSize));
99 
100     EXPECT_CALL(sysMock, read(fd, NotNull(), _))
101         .WillOnce(Invoke([&](int, void* buf, std::size_t count) {
102         EXPECT_TRUE(count > bytes.size());
103         std::memcpy(buf, bytes.data(), bytes.size());
104         return bytes.size();
105     }));
106 
107     EXPECT_CALL(blobMock, writeBytes(session, 0, ContainerEq(bytes)))
108         .WillOnce(Throw(ipmiblob::BlobException("failure")));
109 
110     EXPECT_CALL(sysMock, close(fd)).WillOnce(Return(0));
111 
112     EXPECT_FALSE(handler->sendContents(filePath, session));
113 }
114 
115 } // namespace
116 } // namespace host_tool
117