2018-11-02 17:08:01 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <libabyss.hpp>
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <ev.h>
|
|
|
|
#include <threading.hpp>
|
|
|
|
#include <net.hpp>
|
2018-11-02 17:08:01 +00:00
|
|
|
|
|
|
|
struct AbyssTestBase : public ::testing::Test
|
|
|
|
{
|
2018-12-20 17:14:21 +00:00
|
|
|
llarp::Crypto crypto;
|
2018-12-21 12:54:02 +00:00
|
|
|
llarp_threadpool* threadpool = nullptr;
|
|
|
|
llarp_ev_loop* loop = nullptr;
|
|
|
|
std::unique_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
|
|
|
|
2018-12-20 17:14:21 +00:00
|
|
|
AbyssTestBase() : crypto(llarp::Crypto::sodium{})
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-02 17:08:01 +00:00
|
|
|
void
|
|
|
|
AssertMethod(const std::string& meth) const
|
|
|
|
{
|
|
|
|
ASSERT_TRUE(meth == method);
|
|
|
|
}
|
|
|
|
|
2018-11-02 17:49:01 +00:00
|
|
|
void
|
|
|
|
SetUp()
|
|
|
|
{
|
2018-12-21 12:54:02 +00:00
|
|
|
llarp::SetLogLevel(llarp::eLogDebug);
|
2018-12-11 00:53:11 +00:00
|
|
|
// for llarp::randint
|
2018-11-02 17:49:01 +00:00
|
|
|
}
|
|
|
|
|
2018-11-02 17:56:48 +00:00
|
|
|
static void
|
2018-11-07 15:30:22 +00:00
|
|
|
CancelIt(void* u, __attribute__((unused)) uint64_t orig, uint64_t left)
|
2018-11-02 17:56:48 +00:00
|
|
|
{
|
|
|
|
if(left)
|
|
|
|
return;
|
2018-11-02 18:02:45 +00:00
|
|
|
static_cast< AbyssTestBase* >(u)->Stop();
|
2018-11-02 17:56:48 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 12:54:02 +00:00
|
|
|
static void
|
|
|
|
StopIt(void* u)
|
|
|
|
{
|
|
|
|
static_cast< AbyssTestBase* >(u)->Stop();
|
|
|
|
}
|
|
|
|
|
2018-11-02 17:08:01 +00:00
|
|
|
void
|
|
|
|
Start()
|
|
|
|
{
|
|
|
|
threadpool = llarp_init_same_process_threadpool();
|
|
|
|
llarp_ev_loop_alloc(&loop);
|
2018-12-21 12:54:02 +00:00
|
|
|
logic.reset(new llarp::Logic(threadpool));
|
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)
|
|
|
|
{
|
2018-12-21 12:54:02 +00:00
|
|
|
if(server->ServeAsync(loop, logic.get(), a))
|
2018-11-02 17:08:01 +00:00
|
|
|
{
|
|
|
|
client->RunAsync(loop, a.ToString());
|
2018-12-10 14:14:55 +00:00
|
|
|
logic->call_later({1000, this, &CancelIt});
|
2018-11-02 17:08:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Stop()
|
|
|
|
{
|
2018-12-21 12:54:02 +00:00
|
|
|
llarp::LogDebug("test case Stop() called");
|
2018-11-02 17:08:01 +00:00
|
|
|
if(server)
|
|
|
|
server->Close();
|
|
|
|
llarp_ev_loop_stop(loop);
|
2018-12-21 12:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncStop()
|
|
|
|
{
|
|
|
|
logic->queue_job({this, &StopIt});
|
2018-11-02 17:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TearDown()
|
|
|
|
{
|
2018-12-21 12:54:02 +00:00
|
|
|
logic.reset();
|
|
|
|
llarp_ev_loop_free(&loop);
|
|
|
|
llarp_free_threadpool(&threadpool);
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
ASSERT_TRUE(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-11-07 15:30:22 +00:00
|
|
|
PopulateReqHeaders(__attribute__((unused)) abyss::http::Headers_t& hdr)
|
2018-11-02 17:08:01 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-11-21 17:46:33 +00:00
|
|
|
HandleResponse(__attribute__((unused)) 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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-11-07 15:30:22 +00:00
|
|
|
HandleJSONRPC(Method_t method, __attribute__((unused)) const Params& params,
|
|
|
|
__attribute__((unused)) Response& response)
|
2018-11-02 17:08:01 +00:00
|
|
|
{
|
|
|
|
test->AssertMethod(method);
|
2018-11-02 18:02:45 +00:00
|
|
|
test->called = true;
|
2018-11-02 17:08:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct AbyssTest : public AbyssTestBase,
|
|
|
|
public abyss::http::JSONRPC,
|
|
|
|
public abyss::httpd::BaseReqHandler
|
|
|
|
{
|
|
|
|
AbyssTest()
|
|
|
|
: AbyssTestBase()
|
|
|
|
, abyss::http::JSONRPC()
|
|
|
|
, abyss::httpd::BaseReqHandler(1000)
|
|
|
|
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
SetUp()
|
|
|
|
{
|
2018-11-02 17:49:01 +00:00
|
|
|
AbyssTestBase::SetUp();
|
2018-11-02 17:08:01 +00:00
|
|
|
client = this;
|
|
|
|
server = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
FlushIt(void* u)
|
|
|
|
{
|
|
|
|
static_cast< AbyssTest* >(u)->Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncFlush()
|
|
|
|
{
|
2018-12-10 14:14:55 +00:00
|
|
|
logic->queue_job({this, &FlushIt});
|
2018-11-02 17:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RunLoop()
|
|
|
|
{
|
2018-12-21 12:54:02 +00:00
|
|
|
llarp_ev_loop_run_single_process(loop, threadpool, logic.get());
|
2018-11-02 17:08:01 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(AbyssTest, TestClientAndServer)
|
|
|
|
{
|
|
|
|
Start();
|
|
|
|
abyss::json::Value params;
|
|
|
|
params.SetObject();
|
|
|
|
QueueRPC(method, std::move(params),
|
|
|
|
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);
|
2018-11-06 22:48:17 +00:00
|
|
|
};
|