blob: ac51d29b38e3fec9887094eee5dcac10889704d1 [file] [log] [blame]
Ed Tanous11e8f602023-08-24 14:25:18 -07001#include "credential_pipe.hpp"
2
Ed Tanous41fe81c2024-09-02 15:08:41 -07003#include <boost/asio/buffer.hpp>
Ed Tanous11e8f602023-08-24 14:25:18 -07004#include <boost/asio/io_context.hpp>
Ed Tanous3bfa3b22024-01-31 12:18:03 -08005#include <boost/asio/read.hpp>
6#include <boost/asio/readable_pipe.hpp>
7#include <boost/system/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{
Ed Tanous3bfa3b22024-01-31 12:18:03 -080029 boost::asio::io_context io;
30 boost::asio::readable_pipe testPipe(io);
Ed Tanous11e8f602023-08-24 14:25:18 -070031 {
Ed Tanous11e8f602023-08-24 14:25:18 -070032 CredentialsPipe pipe(io);
Ed Tanous3bfa3b22024-01-31 12:18:03 -080033 testPipe = boost::asio::readable_pipe(io, pipe.releaseFd());
34 ASSERT_GT(testPipe.native_handle(), 0);
Ed Tanous11e8f602023-08-24 14:25:18 -070035 pipe.asyncWrite("username", "password",
36 std::bind_front(handler, std::ref(io)));
Ed Tanous11e8f602023-08-24 14:25:18 -070037 }
Ed Tanous3bfa3b22024-01-31 12:18:03 -080038 io.run();
Ed Tanous11e8f602023-08-24 14:25:18 -070039 std::array<char, 18> buff{};
40 boost::system::error_code ec;
Ed Tanous3bfa3b22024-01-31 12:18:03 -080041 boost::asio::read(testPipe, boost::asio::buffer(buff), ec);
Ed Tanous11e8f602023-08-24 14:25:18 -070042 ASSERT_FALSE(ec);
Ed Tanous11e8f602023-08-24 14:25:18 -070043
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}