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 | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 5 | #include <ipmiblob/test/blob_interface_mock.hpp> |
Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 6 | |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 7 | #include <cstring> |
| 8 | |
Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 9 | #include <gtest/gtest.h> |
| 10 | |
| 11 | namespace host_tool |
| 12 | { |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 13 | namespace |
| 14 | { |
Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 15 | |
| 16 | using ::testing::_; |
| 17 | using ::testing::ContainerEq; |
| 18 | using ::testing::Eq; |
| 19 | using ::testing::Invoke; |
| 20 | using ::testing::NotNull; |
| 21 | using ::testing::Return; |
| 22 | |
| 23 | TEST(BtHandlerTest, verifySendsFileContents) |
| 24 | { |
| 25 | /* In this very basic test, we'll feed the bt handler data from the internal |
| 26 | * syscall mock and catch the writes via the blob mock. |
| 27 | */ |
| 28 | internal::InternalSysMock sysMock; |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 29 | ipmiblob::BlobInterfaceMock blobMock; |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 30 | ProgressMock progMock; |
Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 31 | |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 32 | BtDataHandler handler(&blobMock, &progMock, &sysMock); |
Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 33 | std::string filePath = "/asdf"; |
| 34 | int fd = 1; |
| 35 | std::uint16_t session = 0xbeef; |
| 36 | std::vector<std::uint8_t> bytes = {'1', '2', '3', '4'}; |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 37 | const int fakeFileSize = 100; |
Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 38 | |
| 39 | EXPECT_CALL(sysMock, open(Eq(filePath), _)).WillOnce(Return(fd)); |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 40 | EXPECT_CALL(sysMock, getSize(Eq(filePath))).WillOnce(Return(fakeFileSize)); |
Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 41 | EXPECT_CALL(sysMock, read(fd, NotNull(), _)) |
| 42 | .WillOnce(Invoke([&](int fd, void* buf, std::size_t count) { |
| 43 | EXPECT_TRUE(count > bytes.size()); |
| 44 | std::memcpy(buf, bytes.data(), bytes.size()); |
| 45 | return bytes.size(); |
| 46 | })) |
| 47 | .WillOnce(Return(0)); |
| 48 | EXPECT_CALL(sysMock, close(fd)).WillOnce(Return(0)); |
| 49 | |
| 50 | EXPECT_CALL(blobMock, writeBytes(session, 0, ContainerEq(bytes))); |
| 51 | |
| 52 | EXPECT_TRUE(handler.sendContents(filePath, session)); |
| 53 | } |
| 54 | |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 55 | } // namespace |
Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 56 | } // namespace host_tool |