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