1 #include "credential_pipe.hpp" 2 3 #include <unistd.h> 4 5 #include <boost/asio/io_context.hpp> 6 #include <boost/beast/core/file_posix.hpp> 7 #include <boost/system/detail/error_code.hpp> 8 9 #include <array> 10 #include <cstddef> 11 #include <functional> 12 #include <string> 13 14 #include <gmock/gmock.h> 15 #include <gtest/gtest.h> 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 } 49