1 #include "credential_pipe.hpp"
2 
3 #include <unistd.h>
4 
5 #include <boost/asio/io_context.hpp>
6 #include <boost/asio/read.hpp>
7 #include <boost/asio/readable_pipe.hpp>
8 #include <boost/system/error_code.hpp>
9 
10 #include <array>
11 #include <cstddef>
12 #include <functional>
13 #include <string>
14 
15 #include <gmock/gmock.h>
16 #include <gtest/gtest.h>
17 
18 using ::testing::ElementsAre;
19 
20 static void handler(boost::asio::io_context& io,
21                     const boost::system::error_code& ec, size_t sent)
22 {
23     io.stop();
24     EXPECT_FALSE(ec);
25     EXPECT_EQ(sent, 18);
26 }
27 
28 TEST(CredentialsPipe, BasicSend)
29 {
30     boost::asio::io_context io;
31     boost::asio::readable_pipe testPipe(io);
32     {
33         CredentialsPipe pipe(io);
34         testPipe = boost::asio::readable_pipe(io, pipe.releaseFd());
35         ASSERT_GT(testPipe.native_handle(), 0);
36         pipe.asyncWrite("username", "password",
37                         std::bind_front(handler, std::ref(io)));
38     }
39     io.run();
40     std::array<char, 18> buff{};
41     boost::system::error_code ec;
42     boost::asio::read(testPipe, boost::asio::buffer(buff), ec);
43     ASSERT_FALSE(ec);
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