blob: 3a750b4669f28dc81c571865f35790cac9311043 [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>
Ed Tanous3bfa3b22024-01-31 12:18:03 -08006#include <boost/asio/read.hpp>
7#include <boost/asio/readable_pipe.hpp>
8#include <boost/system/error_code.hpp>
Ed Tanous11e8f602023-08-24 14:25:18 -07009
Ed Tanousf0b59af2024-03-20 13:38:04 -070010#include <array>
11#include <cstddef>
12#include <functional>
Ed Tanous11e8f602023-08-24 14:25:18 -070013#include <string>
14
15#include <gmock/gmock.h>
Ed Tanousf0b59af2024-03-20 13:38:04 -070016#include <gtest/gtest.h>
Ed Tanous11e8f602023-08-24 14:25:18 -070017
18using ::testing::ElementsAre;
19
20static 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
28TEST(CredentialsPipe, BasicSend)
29{
Ed Tanous3bfa3b22024-01-31 12:18:03 -080030 boost::asio::io_context io;
31 boost::asio::readable_pipe testPipe(io);
Ed Tanous11e8f602023-08-24 14:25:18 -070032 {
Ed Tanous11e8f602023-08-24 14:25:18 -070033 CredentialsPipe pipe(io);
Ed Tanous3bfa3b22024-01-31 12:18:03 -080034 testPipe = boost::asio::readable_pipe(io, pipe.releaseFd());
35 ASSERT_GT(testPipe.native_handle(), 0);
Ed Tanous11e8f602023-08-24 14:25:18 -070036 pipe.asyncWrite("username", "password",
37 std::bind_front(handler, std::ref(io)));
Ed Tanous11e8f602023-08-24 14:25:18 -070038 }
Ed Tanous3bfa3b22024-01-31 12:18:03 -080039 io.run();
Ed Tanous11e8f602023-08-24 14:25:18 -070040 std::array<char, 18> buff{};
41 boost::system::error_code ec;
Ed Tanous3bfa3b22024-01-31 12:18:03 -080042 boost::asio::read(testPipe, boost::asio::buffer(buff), ec);
Ed Tanous11e8f602023-08-24 14:25:18 -070043 ASSERT_FALSE(ec);
Ed Tanous11e8f602023-08-24 14:25:18 -070044
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}