20#include <xrpld/core/ConfigSections.h>
21#include <xrpl/basics/make_SSLContext.h>
22#include <xrpl/beast/rfc2616.h>
23#include <xrpl/beast/unit_test.h>
24#include <xrpl/server/Server.h>
25#include <xrpl/server/Session.h>
28#include <test/jtx/CaptureLogs.h>
29#include <test/jtx/envconfig.h>
30#include <test/unit_test/SuiteJournal.h>
32#include <boost/asio.hpp>
33#include <boost/beast/core/tcp_stream.hpp>
34#include <boost/beast/ssl/ssl_stream.hpp>
35#include <boost/utility/in_place_factory.hpp>
61 ,
thread_([&]() { this->io_service_.run(); })
71 boost::asio::io_service&
116 boost::asio::ip::tcp::endpoint remote_address)
125 boost::asio::ip::tcp::endpoint remote_address)
161 template <
class Socket>
163 connect(Socket& s,
typename Socket::endpoint_type
const& ep)
180 template <
class SyncWriteStream>
186 boost::asio::write(s, boost::asio::buffer(text));
198 template <
class SyncReadStream>
202 boost::asio::streambuf b(1000);
205 auto const n = boost::asio::read_until(s, b,
'\n');
206 if (BEAST_EXPECT(n ==
match.size()))
210 boost::asio::buffer_copy(
211 boost::asio::buffer(&got[0], n), b.data());
212 return BEAST_EXPECT(got ==
match);
229 boost::asio::io_service ios;
230 using socket = boost::asio::ip::tcp::socket;
239 "Connection: close\r\n"
246 boost::system::error_code ec;
247 s.shutdown(socket::shutdown_both, ec);
255 boost::asio::io_service ios;
256 using socket = boost::asio::ip::tcp::socket;
265 "Connection: Keep-Alive\r\n"
275 "Connection: close\r\n"
282 boost::system::error_code ec;
283 s.shutdown(socket::shutdown_both, ec);
297 serverPort.
back().ip =
299 serverPort.
back().port = 0;
300 serverPort.
back().protocol.insert(
"http");
301 auto eps = s->ports(serverPort);
316 onAccept(
Session& session, boost::asio::ip::tcp::endpoint endpoint)
326 boost::asio::ip::tcp::endpoint remote_address)
335 boost::asio::ip::tcp::endpoint remote_address)
353 onClose(
Session& session, boost::system::error_code
const&)
367 for (
int i = 0; i < 1000; ++i)
372 serverPort.
back().ip =
374 serverPort.
back().port = 0;
375 serverPort.
back().protocol.insert(
"http");
376 s->ports(serverPort);
384 testcase(
"Server config - invalid options");
385 using namespace test::jtx;
393 (*cfg).deprecatedClearSection(
"port_rpc");
396 std::make_unique<CaptureLogs>(&messages)};
399 messages.
find(
"Missing 'ip' in [port_rpc]") != std::string::npos);
405 (*cfg).deprecatedClearSection(
"port_rpc");
409 std::make_unique<CaptureLogs>(&messages)};
412 messages.
find(
"Missing 'port' in [port_rpc]") != std::string::npos);
418 (*cfg).deprecatedClearSection(
"port_rpc");
420 (*cfg)[
"port_rpc"].set(
"port",
"0");
423 std::make_unique<CaptureLogs>(&messages)};
426 messages.
find(
"Invalid value '0' for key 'port' in [port_rpc]") ==
433 (*cfg)[
"server"].set(
"port",
"0");
436 std::make_unique<CaptureLogs>(&messages)};
439 messages.
find(
"Invalid value '0' for key 'port' in [server]") !=
446 (*cfg).deprecatedClearSection(
"port_rpc");
448 (*cfg)[
"port_rpc"].set(
"port",
"8081");
449 (*cfg)[
"port_rpc"].set(
"protocol",
"");
452 std::make_unique<CaptureLogs>(&messages)};
455 messages.
find(
"Missing 'protocol' in [port_rpc]") !=
465 cfg = std::make_unique<Config>();
470 cfg->deprecatedClearSection(
472 cfg->legacy(
"database_path",
"");
473 cfg->setupControl(
true,
true,
true);
475 (*cfg)[
"port_peer"].set(
"port",
"8080");
476 (*cfg)[
"port_peer"].set(
"protocol",
"peer");
478 (*cfg)[
"port_rpc"].set(
"port",
"8081");
479 (*cfg)[
"port_rpc"].set(
"protocol",
"http,ws2");
482 (*cfg)[
"port_ws"].set(
"port",
"8082");
483 (*cfg)[
"port_ws"].set(
"protocol",
"ws");
487 std::make_unique<CaptureLogs>(&messages)};
490 messages.
find(
"Required section [server] is missing") !=
499 cfg = std::make_unique<Config>();
504 cfg->deprecatedClearSection(
506 cfg->legacy(
"database_path",
"");
507 cfg->setupControl(
true,
true,
true);
508 (*cfg)[
"server"].append(
"port_peer");
509 (*cfg)[
"server"].append(
"port_rpc");
510 (*cfg)[
"server"].append(
"port_ws");
513 std::make_unique<CaptureLogs>(&messages)};
516 messages.
find(
"Missing section: [port_peer]") != std::string::npos);
Abstraction for the underlying message destination.
virtual Severity threshold() const
Returns the minimum severity level this sink will report.
A generic endpoint for log messages.
log_os< char > log
Logging output stream.
void pass()
Record a successful test condition.
testcase_t testcase
Memberspace for declaring test cases.
bool except(F &&f, String const &reason)
void fail(String const &reason, char const *file, int line)
Record a failure.
Persistent state information for a connection session.
virtual void close(bool graceful)=0
Close the session.
virtual http_request_type & request()=0
Returns the current HTTP request.
void write(std::string const &s)
Send a copy of data asynchronously.
virtual void complete()=0
Indicate that the response is complete.
TestSink(beast::unit_test::suite &suite)
void write(beast::severities::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
beast::unit_test::suite & suite_
boost::asio::io_service & get_io_service()
std::optional< boost::asio::io_service::work > work_
boost::asio::io_service io_service_
bool expect_read(SyncReadStream &s, std::string const &match)
bool connect(Socket &s, typename Socket::endpoint_type const &ep)
bool write(SyncWriteStream &s, std::string const &text)
void test_keepalive(boost::asio::ip::tcp::endpoint const &ep)
void test_request(boost::asio::ip::tcp::endpoint const &ep)
void run() override
Runs the suite.
A transaction testing environment.
bool is_keep_alive(boost::beast::http::message< isRequest, Body, Fields > const &m)
A namespace for easy access to logging severity values.
Severity
Severity level / threshold of a Journal message.
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
boost::beast::tcp_stream socket_type
const char * getEnvLocalhostAddr()
boost::beast::ssl_stream< socket_type > stream_type
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
boost::beast::http::request< boost::beast::http::dynamic_body > http_request_type
std::unique_ptr< Server > make_Server(Handler &handler, boost::asio::io_service &io_service, beast::Journal journal)
Create the HTTP server using the specified handler.
static std::string nodeDatabase()
static std::string importNodeDatabase()
Used to indicate the result of a server connection handoff.
void onClose(Session &session, boost::system::error_code const &)
bool onAccept(Session &session, boost::asio::ip::tcp::endpoint endpoint)
Handoff onHandoff(Session &session, std::unique_ptr< stream_type > &&bundle, http_request_type &&request, boost::asio::ip::tcp::endpoint remote_address)
void onWSMessage(std::shared_ptr< WSSession > session, std::vector< boost::asio::const_buffer > const &)
void onRequest(Session &session)
void onStopped(Server &server)
Handoff onHandoff(Session &session, http_request_type &&request, boost::asio::ip::tcp::endpoint remote_address)