Ed Tanous | 11e8f60 | 2023-08-24 14:25:18 -0700 | [diff] [blame] | 1 | #include "credential_pipe.hpp" |
| 2 | |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 3 | #include <unistd.h> |
| 4 | |
Ed Tanous | 11e8f60 | 2023-08-24 14:25:18 -0700 | [diff] [blame] | 5 | #include <boost/asio/io_context.hpp> |
| 6 | #include <boost/beast/core/file_posix.hpp> |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 7 | #include <boost/system/detail/error_code.hpp> |
Ed Tanous | 11e8f60 | 2023-08-24 14:25:18 -0700 | [diff] [blame] | 8 | |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 9 | #include <array> |
| 10 | #include <cstddef> |
| 11 | #include <functional> |
Ed Tanous | 11e8f60 | 2023-08-24 14:25:18 -0700 | [diff] [blame] | 12 | #include <string> |
| 13 | |
| 14 | #include <gmock/gmock.h> |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 15 | #include <gtest/gtest.h> |
Ed Tanous | 11e8f60 | 2023-08-24 14:25:18 -0700 | [diff] [blame] | 16 | |
| 17 | using ::testing::ElementsAre; |
| 18 | |
| 19 | static void handler(boost::asio::io_context& io, |
| 20 | const boost::system::error_code& ec, size_t sent) |
| 21 | { |
| 22 | io.stop(); |
| 23 | EXPECT_FALSE(ec); |
| 24 | EXPECT_EQ(sent, 18); |
| 25 | } |
| 26 | |
| 27 | TEST(CredentialsPipe, BasicSend) |
| 28 | { |
| 29 | boost::beast::file_posix file; |
| 30 | { |
| 31 | boost::asio::io_context io; |
| 32 | CredentialsPipe pipe(io); |
| 33 | file.native_handle(dup(pipe.fd())); |
| 34 | ASSERT_GT(file.native_handle(), 0); |
| 35 | pipe.asyncWrite("username", "password", |
| 36 | std::bind_front(handler, std::ref(io))); |
| 37 | io.run(); |
| 38 | } |
| 39 | std::array<char, 18> buff{}; |
| 40 | boost::system::error_code ec; |
| 41 | size_t r = file.read(buff.data(), buff.size(), ec); |
| 42 | ASSERT_FALSE(ec); |
| 43 | ASSERT_EQ(r, 18); |
| 44 | |
| 45 | EXPECT_THAT(buff, |
| 46 | ElementsAre('u', 's', 'e', 'r', 'n', 'a', 'm', 'e', '\0', 'p', |
| 47 | 'a', 's', 's', 'w', 'o', 'r', 'd', '\0')); |
| 48 | } |