1 #include "credential_pipe.hpp"
2 
3 #include <boost/asio/buffer.hpp>
4 #include <boost/asio/io_context.hpp>
5 #include <boost/asio/read.hpp>
6 #include <boost/asio/readable_pipe.hpp>
7 #include <boost/system/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 
handler(boost::asio::io_context & io,const boost::system::error_code & ec,size_t sent)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 
TEST(CredentialsPipe,BasicSend)27 TEST(CredentialsPipe, BasicSend)
28 {
29     boost::asio::io_context io;
30     boost::asio::readable_pipe testPipe(io);
31     {
32         CredentialsPipe pipe(io);
33         testPipe = boost::asio::readable_pipe(io, pipe.releaseFd());
34         ASSERT_GT(testPipe.native_handle(), 0);
35         pipe.asyncWrite("username", "password",
36                         std::bind_front(handler, std::ref(io)));
37     }
38     io.run();
39     std::array<char, 18> buff{};
40     boost::system::error_code ec;
41     boost::asio::read(testPipe, boost::asio::buffer(buff), ec);
42     ASSERT_FALSE(ec);
43 
44     EXPECT_THAT(buff,
45                 ElementsAre('u', 's', 'e', 'r', 'n', 'a', 'm', 'e', '\0', 'p',
46                             'a', 's', 's', 'w', 'o', 'r', 'd', '\0'));
47 }
48