55#include < BoostTestTargetConfig.h>
66#include " base/io-engine.hpp"
77#include " base/json.hpp"
8+ #include " base/tlsstream.hpp"
89#include < boost/algorithm/string/classification.hpp>
910#include < boost/algorithm/string/split.hpp>
1011#include < boost/asio/read_until.hpp>
1112#include < boost/asio/streambuf.hpp>
13+ #include < boost/asio/use_future.hpp>
1214#include < boost/beast/http.hpp>
1315#include < boost/beast/http/message.hpp>
1416#include < boost/beast/http/parser.hpp>
1517#include < boost/beast/http/string_body.hpp>
18+ #include < future>
1619
1720namespace icinga {
1821
19- class PerfdataWriterConnectionFixture
22+ class PerfdataWriterTargetFixture
2023{
2124public:
22- PerfdataWriterConnectionFixture ()
23- : m_Socket(IoEngine::Get().GetIoContext()),
25+ PerfdataWriterTargetFixture ()
26+ : icinga::PerfdataWriterTargetFixture(Shared<AsioTcpStream>::Make(IoEngine::Get().GetIoContext()))
27+ {
28+ }
29+
30+ explicit PerfdataWriterTargetFixture (const Shared<boost::asio::ssl::context>::Ptr& sslCtx)
31+ : icinga::PerfdataWriterTargetFixture(Shared<AsioTlsStream>::Make(IoEngine::Get().GetIoContext(), *sslCtx))
32+ {
33+ m_SslContext = sslCtx;
34+ }
35+
36+ explicit PerfdataWriterTargetFixture (AsioTlsOrTcpStream stream)
37+ : m_Stream(std::move(stream)),
2438 m_Acceptor(
2539 IoEngine::Get ().GetIoContext(),
2640 boost::asio::ip::tcp::endpoint{boost::asio::ip::address_v4::loopback (), 0 }
@@ -31,13 +45,29 @@ class PerfdataWriterConnectionFixture
3145 m_Acceptor.listen ();
3246 }
3347
34- unsigned int GetPort () { return m_Acceptor.local_endpoint ().port (); }
48+ unsigned short GetPort () { return m_Acceptor.local_endpoint ().port (); }
49+
50+ auto AsyncAccept ()
51+ {
52+ return std::visit (
53+ [&](auto & stream) { return m_Acceptor.async_accept (stream->lowest_layer (), boost::asio::use_future); },
54+ m_Stream
55+ );
56+ }
3557
3658 void Accept ()
3759 {
38- boost::system::error_code ec;
39- m_Acceptor.accept (m_Socket);
40- BOOST_REQUIRE_MESSAGE (!ec, ec.message ());
60+ auto f = AsyncAccept ();
61+ BOOST_REQUIRE_NO_THROW (f.get ());
62+ }
63+
64+ void Handshake ()
65+ {
66+ BOOST_REQUIRE (std::holds_alternative<Shared<AsioTlsStream>::Ptr>(m_Stream));
67+ using handshake_type = UnbufferedAsioTlsStream::handshake_type;
68+ auto & stream = std::get<Shared<AsioTlsStream>::Ptr>(m_Stream);
69+ BOOST_REQUIRE_NO_THROW (stream->next_layer ().handshake (handshake_type::server));
70+ BOOST_REQUIRE (stream->next_layer ().IsVerifyOK ());
4171 }
4272
4373 std::string GetRequestBody ()
@@ -46,9 +76,8 @@ class PerfdataWriterConnectionFixture
4676 using namespace boost ::beast;
4777
4878 boost::system::error_code ec;
49- // flat_buffer buf;
5079 http::request_parser<boost::beast::http::string_body> parser;
51- http::read (m_Socket , m_Buffer, parser, ec);
80+ std::visit ([&]( auto & stream) { http::read (*stream , m_Buffer, parser, ec); }, m_Stream );
5281 BOOST_REQUIRE (!ec);
5382
5483 return parser.get ().body ();
@@ -79,7 +108,10 @@ class PerfdataWriterConnectionFixture
79108 using namespace boost ::asio::ip;
80109
81110 boost::system::error_code ec;
82- auto bytesRead = boost::asio::read_until (m_Socket, m_Buffer, std::forward<T>(delim), ec);
111+ auto bytesRead = std::visit (
112+ [&](auto & stream) { return boost::asio::read_until (*stream, m_Buffer, std::forward<T>(delim), ec); },
113+ m_Stream
114+ );
83115 BOOST_REQUIRE_MESSAGE (!ec, ec.message ());
84116
85117 std::string ret{
@@ -98,16 +130,20 @@ class PerfdataWriterConnectionFixture
98130 boost::system::error_code ec;
99131 http::response<boost::beast::http::empty_body> response;
100132 response.result (status);
101- http::write (m_Socket , response, ec);
133+ std::visit ([&]( auto & stream) { http::write (*stream , response, ec); }, m_Stream );
102134 BOOST_REQUIRE_MESSAGE (!ec, ec.message ());
103135 }
104136
105- void CloseConnection () { m_Socket.lowest_layer ().close (); }
137+ void CloseConnection ()
138+ {
139+ std::visit ([&](auto & stream) { stream->lowest_layer ().close (); }, m_Stream);
140+ }
106141
107142private:
108143 boost::asio::streambuf m_Buffer;
109- boost::asio::ip::tcp::socket m_Socket ;
144+ AsioTlsOrTcpStream m_Stream ;
110145 boost::asio::ip::tcp::acceptor m_Acceptor;
146+ Shared<boost::asio::ssl::context>::Ptr m_SslContext;
111147};
112148
113149} // namespace icinga
0 commit comments