blob: e1a091b24c5fb9fe6f4dd916bb532ce61b7c7084 [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 Venture8cdf9642020-09-30 09:41:51 -07005#include <ipmiblob/blob_errors.hpp>
Patrick Venture664c5bc2019-03-07 08:09:45 -08006#include <ipmiblob/test/blob_interface_mock.hpp>
Patrick Venture907f3a72019-01-15 14:13:37 -08007
Patrick Venture9b37b092020-05-28 20:58:57 -07008#include <cstring>
Patrick Venture8cdf9642020-09-30 09:41:51 -07009#include <memory>
10#include <string>
11#include <vector>
Patrick Venture9b37b092020-05-28 20:58:57 -070012
Patrick Venture8cdf9642020-09-30 09:41:51 -070013#include <gmock/gmock.h>
Patrick Venture907f3a72019-01-15 14:13:37 -080014#include <gtest/gtest.h>
15
16namespace host_tool
17{
Patrick Venturecf9b2192019-06-27 12:09:52 -070018namespace
19{
Patrick Venture907f3a72019-01-15 14:13:37 -080020
21using ::testing::_;
22using ::testing::ContainerEq;
23using ::testing::Eq;
24using ::testing::Invoke;
25using ::testing::NotNull;
26using ::testing::Return;
Patrick Venture8cdf9642020-09-30 09:41:51 -070027using ::testing::Throw;
Patrick Venture907f3a72019-01-15 14:13:37 -080028
Patrick Venture8cdf9642020-09-30 09:41:51 -070029class BtHandlerTest : public ::testing::Test
30{
31 protected:
32 static constexpr std::uint16_t session = 0xbeef;
33
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
45TEST_F(BtHandlerTest, verifySendsFileContents)
Patrick Venture907f3a72019-01-15 14:13:37 -080046{
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 */
Patrick Venture907f3a72019-01-15 14:13:37 -080050 int fd = 1;
Patrick Venture907f3a72019-01-15 14:13:37 -080051 std::vector<std::uint8_t> bytes = {'1', '2', '3', '4'};
Patrick Venturecf9b2192019-06-27 12:09:52 -070052 const int fakeFileSize = 100;
Patrick Venture907f3a72019-01-15 14:13:37 -080053
54 EXPECT_CALL(sysMock, open(Eq(filePath), _)).WillOnce(Return(fd));
Patrick Venturecf9b2192019-06-27 12:09:52 -070055 EXPECT_CALL(sysMock, getSize(Eq(filePath))).WillOnce(Return(fakeFileSize));
Patrick Venture8cdf9642020-09-30 09:41:51 -070056
57 EXPECT_CALL(progMock, start(fakeFileSize));
58
Patrick Venture907f3a72019-01-15 14:13:37 -080059 EXPECT_CALL(sysMock, read(fd, NotNull(), _))
Willy Tub487eb42021-09-16 21:44:43 -070060 .WillOnce(Invoke([&](int, void* buf, std::size_t count) {
Patrick Williamsa9423462023-10-20 11:19:36 -050061 EXPECT_TRUE(count > bytes.size());
62 std::memcpy(buf, bytes.data(), bytes.size());
63 return bytes.size();
64 })).WillOnce(Return(0));
Patrick Venture8cdf9642020-09-30 09:41:51 -070065
66 EXPECT_CALL(progMock, updateProgress(bytes.size()));
67
Patrick Venture907f3a72019-01-15 14:13:37 -080068 EXPECT_CALL(sysMock, close(fd)).WillOnce(Return(0));
69
70 EXPECT_CALL(blobMock, writeBytes(session, 0, ContainerEq(bytes)));
71
Patrick Venture8cdf9642020-09-30 09:41:51 -070072 EXPECT_TRUE(handler->sendContents(filePath, session));
73}
74
75TEST_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
Patrick Venture8cdf9642020-09-30 09:41:51 -070085TEST_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(), _))
Willy Tub487eb42021-09-16 21:44:43 -0700101 .WillOnce(Invoke([&](int, void* buf, std::size_t count) {
Patrick Williamsa9423462023-10-20 11:19:36 -0500102 EXPECT_TRUE(count > bytes.size());
103 std::memcpy(buf, bytes.data(), bytes.size());
104 return bytes.size();
105 }));
Patrick Venture8cdf9642020-09-30 09:41:51 -0700106
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));
Patrick Venture907f3a72019-01-15 14:13:37 -0800113}
114
Patrick Venturecf9b2192019-06-27 12:09:52 -0700115} // namespace
Patrick Venture907f3a72019-01-15 14:13:37 -0800116} // namespace host_tool