2018-11-02 17:08:01 +00:00
|
|
|
#include <libabyss.hpp>
|
2019-01-15 23:45:13 +00:00
|
|
|
|
|
|
|
#include <crypto/crypto.hpp>
|
2019-01-26 15:40:58 +00:00
|
|
|
#include <crypto/crypto_libsodium.hpp>
|
2019-12-10 15:43:27 +00:00
|
|
|
#include <ev/ev.hpp>
|
2019-01-11 01:59:44 +00:00
|
|
|
#include <net/net.hpp>
|
2019-09-01 13:26:16 +00:00
|
|
|
#include <util/thread/threading.hpp>
|
2018-11-02 17:08:01 +00:00
|
|
|
|
2019-01-15 23:45:13 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2018-11-02 17:08:01 +00:00
|
|
|
struct AbyssTestBase : public ::testing::Test
|
|
|
|
{
|
2019-01-26 15:40:58 +00:00
|
|
|
llarp::sodium::CryptoLibSodium crypto;
|
2019-12-10 15:43:27 +00:00
|
|
|
llarp_ev_loop_ptr loop = nullptr;
|
2019-05-22 16:20:03 +00:00
|
|
|
std::shared_ptr< llarp::Logic > logic;
|
2018-11-02 17:08:01 +00:00
|
|
|
abyss::httpd::BaseReqHandler* server = nullptr;
|
|
|
|
abyss::http::JSONRPC* client = nullptr;
|
|
|
|
const std::string method = "test.method";
|
2018-11-02 18:02:45 +00:00
|
|
|
bool called = false;
|
2018-11-02 17:08:01 +00:00
|
|
|
|
2019-01-26 15:40:58 +00:00
|
|
|
AbyssTestBase()
|
2018-12-20 17:14:21 +00:00
|
|
|
{
|
2019-04-02 09:03:56 +00:00
|
|
|
llarp::SetLogLevel(llarp::eLogDebug);
|
2018-12-20 17:14:21 +00:00
|
|
|
}
|
|
|
|
|
2018-11-02 17:08:01 +00:00
|
|
|
void
|
|
|
|
AssertMethod(const std::string& meth) const
|
|
|
|
{
|
2019-02-08 22:44:21 +00:00
|
|
|
ASSERT_EQ(meth, method);
|
2018-11-02 17:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Start()
|
|
|
|
{
|
2019-12-10 15:43:27 +00:00
|
|
|
loop = llarp_make_ev_loop();
|
|
|
|
logic = std::make_shared< llarp::Logic >();
|
|
|
|
loop->set_logic(logic);
|
2018-11-02 17:08:01 +00:00
|
|
|
sockaddr_in addr;
|
|
|
|
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
2018-12-11 00:53:11 +00:00
|
|
|
addr.sin_port = htons((llarp::randint() % 2000) + 2000);
|
2018-11-02 17:08:01 +00:00
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
llarp::Addr a(addr);
|
|
|
|
while(true)
|
|
|
|
{
|
2019-05-22 16:20:03 +00:00
|
|
|
if(server->ServeAsync(loop, logic, a))
|
2018-11-02 17:08:01 +00:00
|
|
|
{
|
|
|
|
client->RunAsync(loop, a.ToString());
|
2020-02-24 19:40:45 +00:00
|
|
|
logic->call_later(1s, std::bind(&AbyssTestBase::Stop, this));
|
2018-11-02 17:08:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-02-24 19:40:45 +00:00
|
|
|
std::this_thread::sleep_for(1s);
|
2018-11-02 17:08:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Stop()
|
|
|
|
{
|
2018-12-21 12:54:02 +00:00
|
|
|
llarp::LogDebug("test case Stop() called");
|
2019-06-02 21:17:05 +00:00
|
|
|
llarp_ev_loop_stop(loop);
|
2018-12-21 12:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncStop()
|
|
|
|
{
|
2019-12-19 12:19:47 +00:00
|
|
|
LogicCall(logic, std::bind(&AbyssTestBase::Stop, this));
|
2018-11-02 17:08:01 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:03:56 +00:00
|
|
|
~AbyssTestBase()
|
2018-11-02 17:08:01 +00:00
|
|
|
{
|
2018-12-21 12:54:02 +00:00
|
|
|
logic.reset();
|
|
|
|
llarp::SetLogLevel(llarp::eLogInfo);
|
2018-11-02 17:08:01 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ClientHandler : public abyss::http::IRPCClientHandler
|
|
|
|
{
|
|
|
|
AbyssTestBase* test;
|
|
|
|
ClientHandler(abyss::http::ConnImpl* impl, AbyssTestBase* parent)
|
|
|
|
: abyss::http::IRPCClientHandler(impl), test(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HandleError()
|
|
|
|
{
|
2019-03-02 02:27:38 +00:00
|
|
|
FAIL() << "unexpected error";
|
2018-11-02 17:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-23 19:35:25 +00:00
|
|
|
PopulateReqHeaders(abyss::http::Headers_t& /*hdr*/)
|
2018-11-02 17:08:01 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-02-23 19:35:25 +00:00
|
|
|
HandleResponse(abyss::http::RPC_Response /*response*/)
|
2018-11-02 17:08:01 +00:00
|
|
|
{
|
2018-12-21 12:54:02 +00:00
|
|
|
test->AsyncStop();
|
2018-11-02 17:08:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ServerHandler : public abyss::httpd::IRPCHandler
|
|
|
|
{
|
2018-11-02 18:02:45 +00:00
|
|
|
AbyssTestBase* test;
|
|
|
|
ServerHandler(abyss::httpd::ConnImpl* impl, AbyssTestBase* parent)
|
2018-11-02 17:08:01 +00:00
|
|
|
: abyss::httpd::IRPCHandler(impl), test(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-03-04 18:59:33 +00:00
|
|
|
bool
|
|
|
|
ValidateHost(const std::string & /*hostname */) const override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-19 21:58:01 +00:00
|
|
|
Response
|
2020-02-23 19:35:25 +00:00
|
|
|
HandleJSONRPC(Method_t method, const Params& /*params*/)
|
2018-11-02 17:08:01 +00:00
|
|
|
{
|
|
|
|
test->AssertMethod(method);
|
2018-11-02 18:02:45 +00:00
|
|
|
test->called = true;
|
2019-03-02 02:27:38 +00:00
|
|
|
return Response();
|
2018-11-02 17:08:01 +00:00
|
|
|
}
|
2019-04-02 09:03:56 +00:00
|
|
|
|
|
|
|
~ServerHandler()
|
|
|
|
{
|
|
|
|
}
|
2018-11-02 17:08:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct AbyssTest : public AbyssTestBase,
|
|
|
|
public abyss::http::JSONRPC,
|
|
|
|
public abyss::httpd::BaseReqHandler
|
|
|
|
{
|
|
|
|
AbyssTest()
|
|
|
|
: AbyssTestBase()
|
|
|
|
, abyss::http::JSONRPC()
|
2020-02-24 19:40:45 +00:00
|
|
|
, abyss::httpd::BaseReqHandler(1s)
|
2018-11-02 17:08:01 +00:00
|
|
|
{
|
2019-04-02 09:03:56 +00:00
|
|
|
client = this;
|
|
|
|
server = this;
|
2018-11-02 17:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
abyss::http::IRPCClientHandler*
|
|
|
|
NewConn(abyss::http::ConnImpl* impl)
|
|
|
|
{
|
|
|
|
return new ClientHandler(impl, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
abyss::httpd::IRPCHandler*
|
2018-11-02 18:02:45 +00:00
|
|
|
CreateHandler(abyss::httpd::ConnImpl* impl)
|
2018-11-02 17:08:01 +00:00
|
|
|
{
|
|
|
|
return new ServerHandler(impl, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncFlush()
|
|
|
|
{
|
2019-12-19 12:19:47 +00:00
|
|
|
LogicCall(logic, std::bind(&AbyssTest::Flush, this));
|
2018-11-02 17:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RunLoop()
|
|
|
|
{
|
2019-07-09 13:47:24 +00:00
|
|
|
llarp_ev_loop_run_single_process(loop, logic);
|
2018-11-02 17:08:01 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(AbyssTest, TestClientAndServer)
|
|
|
|
{
|
2019-07-18 22:12:14 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
GTEST_SKIP();
|
|
|
|
#else
|
2018-11-02 17:08:01 +00:00
|
|
|
Start();
|
2019-03-02 02:27:38 +00:00
|
|
|
QueueRPC(method, nlohmann::json::object(),
|
2018-11-02 17:08:01 +00:00
|
|
|
std::bind(&AbyssTest::NewConn, this, std::placeholders::_1));
|
2018-11-06 22:48:17 +00:00
|
|
|
|
2018-11-02 17:08:01 +00:00
|
|
|
AsyncFlush();
|
|
|
|
RunLoop();
|
2018-11-02 18:02:45 +00:00
|
|
|
ASSERT_TRUE(called);
|
2019-07-18 22:12:14 +00:00
|
|
|
#endif
|
2019-04-25 23:21:19 +00:00
|
|
|
}
|