blob: 0c722222da03a86238d813ac2e8dbe3503ca8857 [file] [log] [blame]
Ed Tanous11e8f602023-08-24 14:25:18 -07001#include "credential_pipe.hpp"
2
Ed Tanousf0b59af2024-03-20 13:38:04 -07003#include <unistd.h>
4
Ed Tanous11e8f602023-08-24 14:25:18 -07005#include <boost/asio/io_context.hpp>
6#include <boost/beast/core/file_posix.hpp>
Ed Tanousf0b59af2024-03-20 13:38:04 -07007#include <boost/system/detail/error_code.hpp>
Ed Tanous11e8f602023-08-24 14:25:18 -07008
Ed Tanousf0b59af2024-03-20 13:38:04 -07009#include <array>
10#include <cstddef>
11#include <functional>
Ed Tanous11e8f602023-08-24 14:25:18 -070012#include <string>
13
14#include <gmock/gmock.h>
Ed Tanousf0b59af2024-03-20 13:38:04 -070015#include <gtest/gtest.h>
Ed Tanous11e8f602023-08-24 14:25:18 -070016
17using ::testing::ElementsAre;
18
19static 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
27TEST(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}