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