2#include <test/jtx/CaptureLogs.h>
3#include <test/jtx/envconfig.h>
4#include <test/unit_test/SuiteJournal.h>
6#include <xrpld/core/ConfigSections.h>
8#include <xrpl/basics/make_SSLContext.h>
9#include <xrpl/beast/rfc2616.h>
10#include <xrpl/beast/unit_test.h>
11#include <xrpl/server/Server.h>
12#include <xrpl/server/Session.h>
14#include <boost/asio.hpp>
15#include <boost/asio/executor_work_guard.hpp>
16#include <boost/beast/core/tcp_stream.hpp>
17#include <boost/beast/ssl/ssl_stream.hpp>
18#include <boost/utility/in_place_factory.hpp>
45 ,
thread_([&]() { this->io_context_.run(); })
55 boost::asio::io_context&
105 boost::asio::ip::tcp::endpoint remote_address)
114 boost::asio::ip::tcp::endpoint remote_address)
154 template <
class Socket>
156 connect(Socket& s,
typename Socket::endpoint_type
const& ep)
173 template <
class SyncWriteStream>
179 boost::asio::write(s, boost::asio::buffer(text));
191 template <
class SyncReadStream>
195 boost::asio::streambuf b(1000);
198 auto const n = boost::asio::read_until(s, b,
'\n');
199 if (BEAST_EXPECT(n ==
match.size()))
203 boost::asio::buffer_copy(boost::asio::buffer(&got[0], n), b.data());
204 return BEAST_EXPECT(got ==
match);
221 boost::asio::io_context ios;
222 using socket = boost::asio::ip::tcp::socket;
231 "Connection: close\r\n"
238 boost::system::error_code ec;
239 s.shutdown(socket::shutdown_both, ec);
247 boost::asio::io_context ios;
248 using socket = boost::asio::ip::tcp::socket;
257 "Connection: Keep-Alive\r\n"
267 "Connection: close\r\n"
274 boost::system::error_code ec;
275 s.shutdown(socket::shutdown_both, ec);
290 serverPort.
back().port = 0;
291 serverPort.
back().protocol.insert(
"http");
292 auto eps = s->ports(serverPort);
307 onAccept(
Session& session, boost::asio::ip::tcp::endpoint endpoint)
317 boost::asio::ip::tcp::endpoint remote_address)
326 boost::asio::ip::tcp::endpoint remote_address)
344 onClose(
Session& session, boost::system::error_code
const&)
358 for (
int i = 0; i < 1000; ++i)
364 serverPort.
back().port = 0;
365 serverPort.
back().protocol.insert(
"http");
366 s->ports(serverPort);
374 testcase(
"Server config - invalid options");
375 using namespace test::jtx;
383 (*cfg).deprecatedClearSection(
"port_rpc");
388 BEAST_EXPECT(messages.
find(
"Missing 'ip' in [port_rpc]") != std::string::npos);
394 (*cfg).deprecatedClearSection(
"port_rpc");
400 BEAST_EXPECT(messages.
find(
"Missing 'port' in [port_rpc]") != std::string::npos);
406 (*cfg).deprecatedClearSection(
"port_rpc");
408 (*cfg)[
"port_rpc"].set(
"port",
"0");
414 messages.
find(
"Invalid value '0' for key 'port' in [port_rpc]") == std::string::npos);
420 (*cfg)[
"server"].set(
"port",
"0");
426 messages.
find(
"Invalid value '0' for key 'port' in [server]") != std::string::npos);
432 (*cfg).deprecatedClearSection(
"port_rpc");
434 (*cfg)[
"port_rpc"].set(
"port",
"8081");
435 (*cfg)[
"port_rpc"].set(
"protocol",
"");
440 BEAST_EXPECT(messages.
find(
"Missing 'protocol' in [port_rpc]") != std::string::npos);
452 cfg->legacy(
"database_path",
"");
453 cfg->setupControl(
true,
true,
true);
455 (*cfg)[
"port_peer"].set(
"port",
"8080");
456 (*cfg)[
"port_peer"].set(
"protocol",
"peer");
458 (*cfg)[
"port_rpc"].set(
"port",
"8081");
459 (*cfg)[
"port_rpc"].set(
"protocol",
"http,ws2");
462 (*cfg)[
"port_ws"].set(
"port",
"8082");
463 (*cfg)[
"port_ws"].set(
"protocol",
"ws");
469 BEAST_EXPECT(messages.
find(
"Required section [server] is missing") != std::string::npos);
481 cfg->legacy(
"database_path",
"");
482 cfg->setupControl(
true,
true,
true);
483 (*cfg)[
"server"].append(
"port_peer");
484 (*cfg)[
"server"].append(
"port_rpc");
485 (*cfg)[
"server"].append(
"port_ws");
490 BEAST_EXPECT(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 void complete()=0
Indicate that the response is complete.
virtual http_request_type & request()=0
Returns the current HTTP request.
void write(std::string const &s)
Send a copy of data asynchronously.
TestSink(beast::unit_test::suite &suite)
beast::unit_test::suite & suite_
void writeAlways(beast::severities::Severity level, std::string const &text) override
Bypass filter and write text to the sink at the specified severity.
void write(beast::severities::Severity level, std::string const &text) override
Write text to the sink at the specified severity.
std::optional< boost::asio::executor_work_guard< boost::asio::io_context::executor_type > > work_
boost::asio::io_context io_context_
boost::asio::io_context & get_io_context()
bool write(SyncWriteStream &s, std::string const &text)
bool connect(Socket &s, typename Socket::endpoint_type const &ep)
void test_request(boost::asio::ip::tcp::endpoint const &ep)
void test_keepalive(boost::asio::ip::tcp::endpoint const &ep)
bool expect_read(SyncReadStream &s, std::string const &match)
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
char const * 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_context &io_context, 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.
static void onRequest(Session &session)
void onStopped(Server &server)
static Handoff onHandoff(Session &session, std::unique_ptr< stream_type > const &bundle, http_request_type const &request, boost::asio::ip::tcp::endpoint remote_address)
void onClose(Session &session, boost::system::error_code const &)
static bool onAccept(Session &session, boost::asio::ip::tcp::endpoint endpoint)
static Handoff onHandoff(Session &session, http_request_type const &request, boost::asio::ip::tcp::endpoint remote_address)
void onWSMessage(std::shared_ptr< WSSession > session, std::vector< boost::asio::const_buffer > const &)