21#include <test/jtx/CaptureLogs.h>
22#include <test/jtx/envconfig.h>
23#include <test/unit_test/SuiteJournal.h>
25#include <xrpld/core/ConfigSections.h>
27#include <xrpl/basics/make_SSLContext.h>
28#include <xrpl/beast/rfc2616.h>
29#include <xrpl/beast/unit_test.h>
30#include <xrpl/server/Server.h>
31#include <xrpl/server/Session.h>
33#include <boost/asio.hpp>
34#include <boost/beast/core/tcp_stream.hpp>
35#include <boost/beast/ssl/ssl_stream.hpp>
36#include <boost/utility/in_place_factory.hpp>
62 ,
thread_([&]() { this->io_service_.run(); })
72 boost::asio::io_service&
124 boost::asio::ip::tcp::endpoint remote_address)
133 boost::asio::ip::tcp::endpoint remote_address)
169 template <
class Socket>
171 connect(Socket& s,
typename Socket::endpoint_type
const& ep)
188 template <
class SyncWriteStream>
194 boost::asio::write(s, boost::asio::buffer(text));
206 template <
class SyncReadStream>
210 boost::asio::streambuf b(1000);
213 auto const n = boost::asio::read_until(s, b,
'\n');
214 if (BEAST_EXPECT(n ==
match.size()))
218 boost::asio::buffer_copy(
219 boost::asio::buffer(&got[0], n), b.data());
220 return BEAST_EXPECT(got ==
match);
237 boost::asio::io_service ios;
238 using socket = boost::asio::ip::tcp::socket;
247 "Connection: close\r\n"
254 boost::system::error_code ec;
255 s.shutdown(socket::shutdown_both, ec);
263 boost::asio::io_service ios;
264 using socket = boost::asio::ip::tcp::socket;
273 "Connection: Keep-Alive\r\n"
283 "Connection: close\r\n"
290 boost::system::error_code ec;
291 s.shutdown(socket::shutdown_both, ec);
305 serverPort.
back().ip =
307 serverPort.
back().port = 0;
308 serverPort.
back().protocol.insert(
"http");
309 auto eps = s->ports(serverPort);
324 onAccept(
Session& session, boost::asio::ip::tcp::endpoint endpoint)
334 boost::asio::ip::tcp::endpoint remote_address)
343 boost::asio::ip::tcp::endpoint remote_address)
361 onClose(
Session& session, boost::system::error_code
const&)
375 for (
int i = 0; i < 1000; ++i)
380 serverPort.
back().ip =
382 serverPort.
back().port = 0;
383 serverPort.
back().protocol.insert(
"http");
384 s->ports(serverPort);
392 testcase(
"Server config - invalid options");
393 using namespace test::jtx;
401 (*cfg).deprecatedClearSection(
"port_rpc");
404 std::make_unique<CaptureLogs>(&messages)};
407 messages.
find(
"Missing 'ip' in [port_rpc]") != std::string::npos);
413 (*cfg).deprecatedClearSection(
"port_rpc");
417 std::make_unique<CaptureLogs>(&messages)};
420 messages.
find(
"Missing 'port' in [port_rpc]") != std::string::npos);
426 (*cfg).deprecatedClearSection(
"port_rpc");
428 (*cfg)[
"port_rpc"].set(
"port",
"0");
431 std::make_unique<CaptureLogs>(&messages)};
434 messages.
find(
"Invalid value '0' for key 'port' in [port_rpc]") ==
441 (*cfg)[
"server"].set(
"port",
"0");
444 std::make_unique<CaptureLogs>(&messages)};
447 messages.
find(
"Invalid value '0' for key 'port' in [server]") !=
454 (*cfg).deprecatedClearSection(
"port_rpc");
456 (*cfg)[
"port_rpc"].set(
"port",
"8081");
457 (*cfg)[
"port_rpc"].set(
"protocol",
"");
460 std::make_unique<CaptureLogs>(&messages)};
463 messages.
find(
"Missing 'protocol' in [port_rpc]") !=
473 cfg = std::make_unique<Config>();
478 cfg->deprecatedClearSection(
480 cfg->legacy(
"database_path",
"");
481 cfg->setupControl(
true,
true,
true);
483 (*cfg)[
"port_peer"].set(
"port",
"8080");
484 (*cfg)[
"port_peer"].set(
"protocol",
"peer");
486 (*cfg)[
"port_rpc"].set(
"port",
"8081");
487 (*cfg)[
"port_rpc"].set(
"protocol",
"http,ws2");
490 (*cfg)[
"port_ws"].set(
"port",
"8082");
491 (*cfg)[
"port_ws"].set(
"protocol",
"ws");
495 std::make_unique<CaptureLogs>(&messages)};
498 messages.
find(
"Required section [server] is missing") !=
507 cfg = std::make_unique<Config>();
512 cfg->deprecatedClearSection(
514 cfg->legacy(
"database_path",
"");
515 cfg->setupControl(
true,
true,
true);
516 (*cfg)[
"server"].append(
"port_peer");
517 (*cfg)[
"server"].append(
"port_rpc");
518 (*cfg)[
"server"].append(
"port_ws");
521 std::make_unique<CaptureLogs>(&messages)};
524 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.
void writeAlways(beast::severities::Severity level, std::string const &text) override
Bypass filter and write text to the sink at the specified severity.
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
boost::beast::ssl_stream< socket_type > stream_type
char const * getEnvLocalhostAddr()
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)