| Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 1 | #include "bt.hpp" | 
|  | 2 | #include "internal_sys_mock.hpp" | 
| Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 3 | #include "progress_mock.hpp" | 
| Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 4 |  | 
| Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 5 | #include <ipmiblob/blob_errors.hpp> | 
| Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 6 | #include <ipmiblob/test/blob_interface_mock.hpp> | 
| Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 7 |  | 
| Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 8 | #include <cstring> | 
| Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 9 | #include <memory> | 
|  | 10 | #include <string> | 
|  | 11 | #include <vector> | 
| Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 12 |  | 
| Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 13 | #include <gmock/gmock.h> | 
| Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 14 | #include <gtest/gtest.h> | 
|  | 15 |  | 
|  | 16 | namespace host_tool | 
|  | 17 | { | 
| Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 18 | namespace | 
|  | 19 | { | 
| Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 20 |  | 
|  | 21 | using ::testing::_; | 
|  | 22 | using ::testing::ContainerEq; | 
|  | 23 | using ::testing::Eq; | 
|  | 24 | using ::testing::Invoke; | 
|  | 25 | using ::testing::NotNull; | 
|  | 26 | using ::testing::Return; | 
| Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 27 | using ::testing::Throw; | 
| Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 28 |  | 
| Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 29 | class 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 |  | 
|  | 45 | TEST_F(BtHandlerTest, verifySendsFileContents) | 
| Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 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 | */ | 
| Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 50 | int fd = 1; | 
| Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 51 | std::vector<std::uint8_t> bytes = {'1', '2', '3', '4'}; | 
| Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 52 | const int fakeFileSize = 100; | 
| Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 53 |  | 
|  | 54 | EXPECT_CALL(sysMock, open(Eq(filePath), _)).WillOnce(Return(fd)); | 
| Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 55 | EXPECT_CALL(sysMock, getSize(Eq(filePath))).WillOnce(Return(fakeFileSize)); | 
| Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 56 |  | 
|  | 57 | EXPECT_CALL(progMock, start(fakeFileSize)); | 
|  | 58 |  | 
| Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 59 | EXPECT_CALL(sysMock, read(fd, NotNull(), _)) | 
| Willy Tu | b487eb4 | 2021-09-16 21:44:43 -0700 | [diff] [blame] | 60 | .WillOnce(Invoke([&](int, void* buf, std::size_t count) { | 
| Patrick Williams | 42a44c2 | 2024-08-16 15:21:32 -0400 | [diff] [blame] | 61 | EXPECT_TRUE(count > bytes.size()); | 
|  | 62 | std::memcpy(buf, bytes.data(), bytes.size()); | 
|  | 63 | return bytes.size(); | 
|  | 64 | })) | 
|  | 65 | .WillOnce(Return(0)); | 
| Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 66 |  | 
|  | 67 | EXPECT_CALL(progMock, updateProgress(bytes.size())); | 
|  | 68 |  | 
| Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 69 | EXPECT_CALL(sysMock, close(fd)).WillOnce(Return(0)); | 
|  | 70 |  | 
|  | 71 | EXPECT_CALL(blobMock, writeBytes(session, 0, ContainerEq(bytes))); | 
|  | 72 |  | 
| Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 73 | EXPECT_TRUE(handler->sendContents(filePath, session)); | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | TEST_F(BtHandlerTest, sendContentsFailsToOpenFile) | 
|  | 77 | { | 
|  | 78 | /* If the file doesn't exist or the permissions are wrong, the sendContents | 
|  | 79 | * will return failure. | 
|  | 80 | */ | 
|  | 81 | EXPECT_CALL(sysMock, open(Eq(filePath), _)).WillOnce(Return(-1)); | 
|  | 82 |  | 
|  | 83 | EXPECT_FALSE(handler->sendContents(filePath, session)); | 
|  | 84 | } | 
|  | 85 |  | 
| Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 86 | TEST_F(BtHandlerTest, sendContentsFailsWhenBlobHandlerThrows) | 
|  | 87 | { | 
|  | 88 | /* The blob handler throws an exception which is caught, the file is closed, | 
|  | 89 | * and false is returned. | 
|  | 90 | */ | 
|  | 91 |  | 
|  | 92 | int fd = 1; | 
|  | 93 | std::vector<std::uint8_t> bytes = {'1', '2', '3', '4'}; | 
|  | 94 | const int fakeFileSize = 100; | 
|  | 95 |  | 
|  | 96 | EXPECT_CALL(sysMock, open(Eq(filePath), _)).WillOnce(Return(fd)); | 
|  | 97 | EXPECT_CALL(sysMock, getSize(Eq(filePath))).WillOnce(Return(fakeFileSize)); | 
|  | 98 |  | 
|  | 99 | EXPECT_CALL(progMock, start(fakeFileSize)); | 
|  | 100 |  | 
|  | 101 | EXPECT_CALL(sysMock, read(fd, NotNull(), _)) | 
| Willy Tu | b487eb4 | 2021-09-16 21:44:43 -0700 | [diff] [blame] | 102 | .WillOnce(Invoke([&](int, void* buf, std::size_t count) { | 
| Patrick Williams | 42a44c2 | 2024-08-16 15:21:32 -0400 | [diff] [blame] | 103 | EXPECT_TRUE(count > bytes.size()); | 
|  | 104 | std::memcpy(buf, bytes.data(), bytes.size()); | 
|  | 105 | return bytes.size(); | 
|  | 106 | })); | 
| Patrick Venture | 8cdf964 | 2020-09-30 09:41:51 -0700 | [diff] [blame] | 107 |  | 
|  | 108 | EXPECT_CALL(blobMock, writeBytes(session, 0, ContainerEq(bytes))) | 
|  | 109 | .WillOnce(Throw(ipmiblob::BlobException("failure"))); | 
|  | 110 |  | 
|  | 111 | EXPECT_CALL(sysMock, close(fd)).WillOnce(Return(0)); | 
|  | 112 |  | 
|  | 113 | EXPECT_FALSE(handler->sendContents(filePath, session)); | 
| Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 114 | } | 
|  | 115 |  | 
| Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 116 | } // namespace | 
| Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 117 | } // namespace host_tool |