diff --git a/net/CMakeLists.txt b/net/CMakeLists.txt index 386832cf37e11d2584c4de1ad3a07beaafcd2ee7..5af66370e1e966adee4cad7b533d347a1197f7c0 100644 --- a/net/CMakeLists.txt +++ b/net/CMakeLists.txt @@ -4,6 +4,9 @@ include (CheckIncludeFile) include (CheckFunctionExists) project (ftlnet) +include(CTest) +enable_testing() + #find_package(PkgConfig) #pkg_check_modules(GTKMM gtkmm-3.0) diff --git a/net/include/ftl/net/socket.hpp b/net/include/ftl/net/socket.hpp index 264f5fc3b003119d292f5b541054bfdbe0ec9d0d..9bbd375652ecb9ec69c8b8a956d5590c59ff9be2 100644 --- a/net/include/ftl/net/socket.hpp +++ b/net/include/ftl/net/socket.hpp @@ -17,6 +17,7 @@ #include <sstream> #include <tuple> +#include <vector> #include <type_traits> # define ENABLE_IF(...) \ diff --git a/net/src/listener.cpp b/net/src/listener.cpp index 497f1459a3ba40b9b17f9c33c9fa97fa58478fc1..4eb83458fdea5ba6d20ac147c27dc08de366b81d 100644 --- a/net/src/listener.cpp +++ b/net/src/listener.cpp @@ -90,7 +90,7 @@ int wsListen(URI &uri) { return INVALID_SOCKET; } -Listener::Listener(const char *pUri) { +Listener::Listener(const char *pUri) : default_proto_(NULL) { URI uri(pUri); descriptor_ = INVALID_SOCKET; diff --git a/net/src/net.cpp b/net/src/net.cpp index f3f329a08ffeebd406b8ca5ce1f74594485c5a46..cd15614f2215e0177bb8cf1554b3e223b0a3bead 100644 --- a/net/src/net.cpp +++ b/net/src/net.cpp @@ -165,6 +165,15 @@ bool _run(bool blocking, bool nodelay) { if (FD_ISSET(s->_socket(), &sfderror)) { s->error(); } + } else if (s != NULL) { + // Erase it + + for (auto i=sockets.begin(); i!=sockets.end(); i++) { + if ((*i) == s) { + std::cout << "REMOVING SOCKET" << std::endl; + sockets.erase(i); break; + } + } } } } diff --git a/net/src/socket.cpp b/net/src/socket.cpp index 6826b63de202474b457723b972bd8b054d6a6450..338b447192e0d0d68c73d6ed16d4b26f72565662 100644 --- a/net/src/socket.cpp +++ b/net/src/socket.cpp @@ -22,6 +22,8 @@ typedef int socklen_t; #endif #include <iostream> +#include <memory> +#include <algorithm> using namespace ftl; using ftl::net::Socket; @@ -187,7 +189,7 @@ void Socket::_updateURI() { } int Socket::close() { - if (isConnected()) { + if (sock_ != INVALID_SOCKET) { #ifndef WIN32 ::close(sock_); #else @@ -197,6 +199,9 @@ int Socket::close() { connected_ = false; // Attempt auto reconnect? + + //auto i = find(sockets.begin(),sockets.end(),this); + //sockets.erase(i); } return 0; } diff --git a/net/test/CMakeLists.txt b/net/test/CMakeLists.txt index f9a3d118508499360e2a14a05cff60f1ac4c091d..f491fd6851a8f2b954cc8af57c66ff37e9ea6bf0 100644 --- a/net/test/CMakeLists.txt +++ b/net/test/CMakeLists.txt @@ -1,30 +1,25 @@ -include(CTest) -enable_testing() - -add_executable(rpc_test EXCLUDE_FROM_ALL - ./tests.cpp - ./rpc.cpp - ../src/dispatcher.cpp - ../src/socket.cpp -) -target_include_directories(rpc_test PUBLIC ${PROJECT_SOURCE_DIR}/include) -target_link_libraries(rpc_test uriparser glog) - -add_executable(protocol_unit EXCLUDE_FROM_ALL +add_executable(protocol_unit ./tests.cpp ./protocol_unit.cpp ) target_include_directories(protocol_unit PUBLIC ${PROJECT_SOURCE_DIR}/include) target_link_libraries(protocol_unit glog) -add_executable(socket_unit EXCLUDE_FROM_ALL +add_executable(socket_unit ./tests.cpp ./socket_unit.cpp ) target_include_directories(socket_unit PUBLIC ${PROJECT_SOURCE_DIR}/include) target_link_libraries(socket_unit uriparser glog) -add_executable(net_integration EXCLUDE_FROM_ALL +add_executable(uri_unit + ./tests.cpp + ./uri_unit.cpp +) +target_include_directories(uri_unit PUBLIC ${PROJECT_SOURCE_DIR}/include) +target_link_libraries(uri_unit uriparser) + +add_executable(net_integration ./tests.cpp ./net_integration.cpp ../src/socket.cpp @@ -36,20 +31,11 @@ add_executable(net_integration EXCLUDE_FROM_ALL target_include_directories(net_integration PUBLIC ${PROJECT_SOURCE_DIR}/include) target_link_libraries(net_integration uriparser glog) -add_executable(socket_test EXCLUDE_FROM_ALL - ./tests.cpp - ./net_raw.cpp - ../src/net.cpp - ../src/socket.cpp - ../src/listener.cpp - ./ice.cpp - ../src/ice.cpp - ./uri.cpp - ../src/dispatcher.cpp -) -target_include_directories(socket_test PUBLIC ${PROJECT_SOURCE_DIR}/include) -target_link_libraries(socket_test uriparser glog) +add_test(URIUnitTest uri_unit) +add_test(ProtocolUnitTest protocol_unit) +add_test(SocketUnitTest socket_unit) +add_test(NetIntegrationTest net_integration) add_custom_target(tests) -add_dependencies(tests rpc_test socket_test socket_unit protocol_unit net_integration) +add_dependencies(tests socket_unit protocol_unit net_integration uri_unit) diff --git a/net/test/net_integration.cpp b/net/test/net_integration.cpp index 9d44c815dd40afdf80d378674fe19cf12daa0218..c63b635696a154fb825e3d1ba23e47d0661c40fd 100644 --- a/net/test/net_integration.cpp +++ b/net/test/net_integration.cpp @@ -4,11 +4,193 @@ #include <ftl/net/listener.hpp> #include <memory> +#include <iostream> using ftl::net::Socket; using ftl::net::Protocol; using std::shared_ptr; +// --- Support ----------------------------------------------------------------- + +#ifndef WIN32 +#include <unistd.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <netdb.h> +#include <arpa/inet.h> +#define INVALID_SOCKET -1 +#define SOCKET_ERROR -1 +#endif + +#ifdef WIN32 +#include <windows.h> +#include <winsock.h> +typedef int socklen_t; +#define MSG_WAITALL 0 +#endif + +static int ssock = INVALID_SOCKET; +static sockaddr_in slocalAddr; + +void fin_server() { + //int t = 1; + //setsockopt(ssock,SOL_SOCKET,SO_REUSEADDR,&t,sizeof(int)); + + #ifndef WIN32 + if (ssock != INVALID_SOCKET) close(ssock); + #else + if (ssock != INVALID_SOCKET) closesocket(ssock); + #endif + + ssock = INVALID_SOCKET; +} + +void init_server() { + //fin_server(); + int port = 7077; + + #ifdef WIN32 + WSAData wsaData; + //If Win32 then load winsock + if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) { + std::cerr << "Socket error\n"; + return; + } + #endif + + ssock = socket(AF_INET, SOCK_STREAM, 0); + if (ssock == INVALID_SOCKET) { + std::cerr << "Socket error 1\n"; + return; + } + + int enable = 1; + if (setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) + std::cerr << "setsockopt(SO_REUSEADDR) failed" << std::endl; + + //Specify listen port and address + //memset(&s_localAddr, 0, sizeof(s_localAddr)); + slocalAddr.sin_family = AF_INET; + slocalAddr.sin_addr.s_addr = htonl(INADDR_ANY); + slocalAddr.sin_port = htons(port); + + int rc = bind(ssock, (struct sockaddr*)&slocalAddr, sizeof(slocalAddr)); + + if (rc == SOCKET_ERROR) { + std::cerr << "Socket error 2\n"; + + #ifndef WIN32 + close(ssock); + #else + closesocket(ssock); + #endif + ssock = INVALID_SOCKET; + return; + } + + //Attempt to start listening for connection requests. + rc = ::listen(ssock, 1); + + if (rc == SOCKET_ERROR) { + std::cerr << "Socket error 3\n"; + + #ifndef WIN32 + close(ssock); + #else + closesocket(ssock); + #endif + ssock = INVALID_SOCKET; + return; + } +} + +// --- Tests ------------------------------------------------------------------- + +TEST_CASE("net::connect()", "[net]") { + init_server(); + shared_ptr<Socket> sock = nullptr; + + SECTION("valid tcp connection using ipv4") { + sock = ftl::net::connect("tcp://127.0.0.1:7077"); + REQUIRE(sock != nullptr); + REQUIRE(sock->isValid()); + } + + SECTION("valid tcp connection using hostname") { + sock = ftl::net::connect("tcp://localhost:7077"); + REQUIRE(sock->isValid()); + } + + SECTION("invalid protocol") { + sock = ftl::net::connect("http://127.0.0.1:7077"); + REQUIRE(!sock->isValid()); + } + + SECTION("empty uri") { + sock = ftl::net::connect(""); + REQUIRE(!sock->isValid()); + } + + SECTION("null uri") { + sock = ftl::net::connect(NULL); + REQUIRE(!sock->isValid()); + } + + // Disabled due to long timeout + /*SECTION("incorrect ipv4 address") { + sock = ftl::net::raw::connect("tcp://192.0.1.1:7077"); + REQUIRE(sock != NULL); + REQUIRE(sock->isConnected() == false); + sock = NULL; + }*/ + + // Removed as too slow + /*SECTION("incorrect dns address") { + sock = ftl::net::connect("tcp://xryyrrgrtgddgr.com:7077"); + REQUIRE(!sock->isValid()); + }*/ + + fin_server(); +} + +TEST_CASE("net::listen()", "[net]") { + + SECTION("tcp any interface") { + REQUIRE( ftl::net::listen("tcp://localhost:9001")->isListening() ); + + SECTION("can connect to listening socket") { + auto sock = ftl::net::connect("tcp://127.0.0.1:9001"); + REQUIRE(sock->isValid()); + ftl::net::wait(); // Handshake 1 + ftl::net::wait(); // Handshake 2 + REQUIRE(sock->isConnected()); + + // TODO Need way of knowing about connection + } + + ftl::net::stop(); + } + + SECTION("on connection event") { + auto l = ftl::net::listen("tcp://localhost:9002"); + REQUIRE( l->isListening() ); + + bool connected = false; + + l->onConnection([&](shared_ptr<Socket> s) { + ftl::net::wait(); // Wait for handshake + REQUIRE( s->isConnected() ); + connected = true; + }); + + auto sock = ftl::net::connect("tcp://127.0.0.1:9002"); + ftl::net::wait(); + REQUIRE( connected ); + ftl::net::stop(); + } +} + TEST_CASE("Net Integration", "[integrate]") { std::string data; diff --git a/net/test/net_raw.cpp b/net/test/net_raw.cpp deleted file mode 100644 index 3f2fdfd7e1560c26044436447e3ed226757ef182..0000000000000000000000000000000000000000 --- a/net/test/net_raw.cpp +++ /dev/null @@ -1,297 +0,0 @@ -#include "catch.hpp" -#include <string.h> -#include <ftl/net.hpp> -#include <ftl/net/socket.hpp> -#include <ftl/net/listener.hpp> -#include <iostream> -#include <memory> - -#ifndef WIN32 -#include <unistd.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <netdb.h> -#include <arpa/inet.h> -#define INVALID_SOCKET -1 -#define SOCKET_ERROR -1 -#endif - -#ifdef WIN32 -#include <windows.h> -#include <winsock.h> -typedef int socklen_t; -#define MSG_WAITALL 0 -#endif - - -// ---- MOCK Server Code ------------------------------------------------------- - -static bool server = false; -static bool running = false; -static int csock = INVALID_SOCKET; -static int ssock = INVALID_SOCKET; -static fd_set sfdread; -static fd_set sfderror; -static sockaddr_in slocalAddr; - -using ftl::net::Socket; -using std::shared_ptr; - -void fin_server() { - if (!server) return; - - //int t = 1; - //setsockopt(ssock,SOL_SOCKET,SO_REUSEADDR,&t,sizeof(int)); - - #ifndef WIN32 - if (csock != INVALID_SOCKET) close(csock); - if (ssock != INVALID_SOCKET) close(ssock); - #else - if (csock != INVALID_SOCKET) closesocket(csock); - if (ssock != INVALID_SOCKET) closesocket(ssock); - #endif - - csock = INVALID_SOCKET; - ssock = INVALID_SOCKET; - server = false; -} - -void init_server() { - fin_server(); - int port = 7077; - - #ifdef WIN32 - WSAData wsaData; - //If Win32 then load winsock - if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) { - std::cerr << "Socket error\n"; - return; - } - #endif - - ssock = socket(AF_INET, SOCK_STREAM, 0); - if (ssock == INVALID_SOCKET) { - std::cerr << "Socket error 1\n"; - return; - } - - int enable = 1; - if (setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) - std::cerr << "setsockopt(SO_REUSEADDR) failed" << std::endl; - - //Specify listen port and address - //memset(&s_localAddr, 0, sizeof(s_localAddr)); - slocalAddr.sin_family = AF_INET; - slocalAddr.sin_addr.s_addr = htonl(INADDR_ANY); - slocalAddr.sin_port = htons(port); - - int rc = bind(ssock, (struct sockaddr*)&slocalAddr, sizeof(slocalAddr)); - - if (rc == SOCKET_ERROR) { - std::cerr << "Socket error 2\n"; - - #ifndef WIN32 - close(ssock); - #else - closesocket(ssock); - #endif - csock = INVALID_SOCKET; - ssock = INVALID_SOCKET; - return; - } - - //Attempt to start listening for connection requests. - rc = ::listen(ssock, 1); - - if (rc == SOCKET_ERROR) { - std::cerr << "Socket error 3\n"; - - #ifndef WIN32 - close(ssock); - #else - closesocket(ssock); - #endif - csock = INVALID_SOCKET; - ssock = INVALID_SOCKET; - return; - } - - running = true; - server = true; -} - -void send_json(int service, const char *json) { - int flen = strlen(json) + 2 * sizeof(int); - char *buf = new char[flen]; - int *idat = (int*)buf; - idat[0] = strlen(json)+sizeof(int); - idat[1] = service; - strcpy((char*)&idat[2], json); - std::cerr << "SENDING " << flen << std::endl; - ::send(csock, buf, flen, MSG_DONTWAIT); - //delete buf; -} - -int setDescriptors() { - //Reset all file descriptors - FD_ZERO(&sfdread); - FD_ZERO(&sfderror); - - int n = 0; - - //Set file descriptor for the listening socket. - if (ssock != INVALID_SOCKET) { - FD_SET(ssock, &sfdread); - FD_SET(ssock, &sfderror); - n = ssock; - } - - //Set the file descriptors for each client - if (csock != INVALID_SOCKET) { - FD_SET(csock, &sfdread); - FD_SET(csock, &sfderror); - n = csock; - } - - return n; -} - -void mocksend(int sd, uint32_t service, const std::string &data) { - //std::cout << "HEX SEND: " << hexStr(data) << std::endl; - char buf[8+data.size()]; - ftl::net::Header *h = (ftl::net::Header*)&buf; - h->size = data.size()+4; - h->service = service; - std::memcpy(&buf[8],data.data(),data.size()); - - //std::cout << "HEX SEND2: " << hexStr(fakedata[sd]) << std::endl; - ::send(sd, buf, 8+data.size(), 0); -} - -void accept_connection() { - int n = setDescriptors(); - - //Wait for a network event or timeout in 3 seconds - timeval block; - block.tv_sec = 1; - block.tv_usec = 0; - int selres = select(n+1, &sfdread, 0, &sfderror, &block); - - if (selres > 0 && FD_ISSET(ssock, &sfdread)) { - int rsize = sizeof(sockaddr_storage); - sockaddr_storage addr; - - std::cout << "Accepted connection!" << std::endl; - - //Finally accept this client connection. - csock = accept(ssock, (sockaddr*)&addr, (socklen_t*)&rsize); - - ftl::net::Handshake hs1; - hs1.magic = ftl::net::MAGIC; - hs1.version = ftl::net::version(); - mocksend(csock, FTL_PROTOCOL_HS1, std::string((char*)&hs1, sizeof(hs1))); - } else { - - } -} - -// ---- END MOCK --------------------------------------------------------------- - -TEST_CASE("net::connect()", "[net]") { - init_server(); - REQUIRE(ssock != INVALID_SOCKET); - - shared_ptr<Socket> sock = nullptr; - - SECTION("valid tcp connection using ipv4") { - sock = ftl::net::connect("tcp://127.0.0.1:7077"); - REQUIRE(sock != nullptr); - REQUIRE(sock->isValid()); - accept_connection(); - } - - SECTION("valid tcp connection using hostname") { - sock = ftl::net::connect("tcp://localhost:7077"); - REQUIRE(sock->isValid()); - accept_connection(); - } - - SECTION("invalid protocol") { - sock = ftl::net::connect("http://127.0.0.1:7077"); - REQUIRE(!sock->isValid()); - } - - SECTION("empty uri") { - sock = ftl::net::connect(""); - REQUIRE(!sock->isValid()); - } - - SECTION("null uri") { - sock = ftl::net::connect(NULL); - REQUIRE(!sock->isValid()); - sock = nullptr; - } - - // Disabled due to long timeout - /*SECTION("incorrect ipv4 address") { - sock = ftl::net::raw::connect("tcp://192.0.1.1:7077"); - REQUIRE(sock != NULL); - REQUIRE(sock->isConnected() == false); - sock = NULL; - }*/ - - SECTION("incorrect dns address") { - sock = ftl::net::connect("tcp://xryyrrgrtgddgr.com:7077"); - REQUIRE(!sock->isValid()); - REQUIRE(sock->isConnected() == false); - sock = nullptr; - } - - if (sock && sock->isValid()) { - ftl::net::wait(); - //sock->data(); - REQUIRE(sock->isConnected()); - REQUIRE(csock != INVALID_SOCKET); - sock->close(); - } - fin_server(); -} - -TEST_CASE("net::listen()", "[net]") { - - SECTION("tcp any interface") { - REQUIRE( ftl::net::listen("tcp://*:7078")->isListening() ); - - SECTION("can connect to listening socket") { - auto sock = ftl::net::connect("tcp://127.0.0.1:7078"); - REQUIRE(sock->isValid()); - ftl::net::wait(); // Handshake 1 - ftl::net::wait(); // Handshake 2 - REQUIRE(sock->isConnected()); - - // TODO Need way of knowing about connection - } - - ftl::net::stop(); - } - - SECTION("on connection event") { - auto l = ftl::net::listen("tcp://*:7078"); - REQUIRE( l->isListening() ); - - bool connected = false; - - l->onConnection([&](shared_ptr<Socket> s) { - ftl::net::wait(); // Wait for handshake - REQUIRE( s->isConnected() ); - connected = true; - }); - - auto sock = ftl::net::connect("tcp://127.0.0.1:7078"); - ftl::net::wait(); - REQUIRE( connected ); - ftl::net::stop(); - } -} - diff --git a/net/test/rpc.cpp b/net/test/rpc.cpp deleted file mode 100644 index e48d775ec19b50343ca161b7d5b71b582065c32b..0000000000000000000000000000000000000000 --- a/net/test/rpc.cpp +++ /dev/null @@ -1,238 +0,0 @@ -#include "catch.hpp" -#include <ftl/net/socket.hpp> -#include <iostream> -#include <memory> -#include <map> - -/*struct FakeHeader { - uint32_t size; - uint32_t service; -};*/ - -static std::map<int, std::string> fakedata; - -/*static std::string hexStr(const std::string &s) -{ - const char *data = s.data(); - int len = s.size(); - std::stringstream ss; - ss << std::hex; - for(int i=0;i<len;++i) - ss << std::setw(2) << std::setfill('0') << (int)data[i]; - return ss.str(); -}*/ - -void fake_send(int sd, uint32_t service, const std::string &data) { - //std::cout << "HEX SEND: " << hexStr(data) << std::endl; - char buf[8+data.size()]; - ftl::net::Header *h = (ftl::net::Header*)&buf; - h->size = data.size()+4; - h->service = service; - std::memcpy(&buf[8],data.data(),data.size()); - fakedata[sd] = std::string(&buf[0], 8+data.size()); - - //std::cout << "HEX SEND2: " << hexStr(fakedata[sd]) << std::endl; -} - -extern ssize_t recv(int sd, void *buf, size_t n, int f) { - if (fakedata.count(sd) == 0) { - std::cout << "Unrecognised socket" << std::endl; - return 0; - } - - int l = fakedata[sd].size(); - - std::memcpy(buf, fakedata[sd].c_str(), l); - fakedata.erase(sd); - return l; -} - -extern ssize_t writev(int sd, const struct iovec *v, int cnt) { - // TODO Use count incase more sources exist... - size_t len = v[0].iov_len+v[1].iov_len; - char buf[len]; - std::memcpy(&buf[0],v[0].iov_base,v[0].iov_len); - std::memcpy(&buf[v[0].iov_len],v[1].iov_base,v[1].iov_len); - fakedata[sd] = std::string(&buf[0], len); - return 0; -} - -static std::function<void()> waithandler; - -bool ftl::net::wait() { - if (waithandler) waithandler(); - //waithandler = nullptr; - return true; -} - -TEST_CASE("Socket::bind()", "[rpc]") { - SECTION("no argument bind fake dispatch") { - auto s = new ftl::net::Socket(0); - bool called = false; - - s->bind("test1", [&]() { - called = true; - }); - - auto args_obj = std::make_tuple(); - auto call_obj = std::make_tuple(0,0,"test1",args_obj); - std::stringstream buf; - msgpack::pack(buf, call_obj); - - s->dispatchRPC(buf.str()); - REQUIRE( called ); - } - - SECTION("one argument bind fake dispatch") { - auto s = new ftl::net::Socket(0); - bool called = false; - - s->bind("test1", [&](int a) { - called = true; - REQUIRE( a == 5 ); - }); - - auto args_obj = std::make_tuple(5); - auto call_obj = std::make_tuple(0,0,"test1",args_obj); - std::stringstream buf; - msgpack::pack(buf, call_obj); - - s->dispatchRPC(buf.str()); - REQUIRE( called ); - } - - SECTION("two argument bind fake dispatch") { - auto s = new ftl::net::Socket(0); - bool called = false; - - s->bind("test1", [&](int a, float b) { - called = true; - REQUIRE( a == 5 ); - REQUIRE( b == 5.4f ); // Danger - }); - - auto args_obj = std::make_tuple(5, 5.4f); - auto call_obj = std::make_tuple(0,0,"test1",args_obj); - std::stringstream buf; - msgpack::pack(buf, call_obj); - - s->dispatchRPC(buf.str()); - REQUIRE( called ); - } - - SECTION("no argument bind fake data") { - auto s = new ftl::net::Socket(0); - bool called = false; - - s->bind("test1", [&]() { - called = true; - }); - - auto args_obj = std::make_tuple(); - auto call_obj = std::make_tuple(0,0,"test1",args_obj); - std::stringstream buf; - msgpack::pack(buf, call_obj); - - fake_send(0, FTL_PROTOCOL_RPC, buf.str()); - REQUIRE( s->data() ); - REQUIRE( called ); - } - - SECTION("non-void bind fake data") { - auto s = new ftl::net::Socket(0); - bool called = false; - - s->bind("test1", [&]() -> int { - called = true; - return 55; - }); - - auto args_obj = std::make_tuple(); - auto call_obj = std::make_tuple(0,0,"test1",args_obj); - std::stringstream buf; - msgpack::pack(buf, call_obj); - - fake_send(0, FTL_PROTOCOL_RPC, buf.str()); - REQUIRE( s->data() ); - REQUIRE( called ); - - // TODO Require that a writev occurred with result value - } -} - -TEST_CASE("Socket::call()", "[rpc]") { - SECTION("no argument call") { - auto s = new ftl::net::Socket(0); - - waithandler = [&]() { - // Read fakedata sent - // TODO Validate data - - // Do a fake send - auto res_obj = std::make_tuple(1,0,msgpack::object(),66); - std::stringstream buf; - msgpack::pack(buf, res_obj); - - fake_send(0, FTL_PROTOCOL_RPCRETURN, buf.str()); - s->data(); - }; - - int res = s->call<int>("test1"); - - REQUIRE( res == 66 ); - } - - SECTION("one argument call") { - auto s = new ftl::net::Socket(0); - - waithandler = [&]() { - // Read fakedata sent - // TODO Validate data - - // Do a fake send - auto res_obj = std::make_tuple(1,1,msgpack::object(),43); - std::stringstream buf; - msgpack::pack(buf, res_obj); - - fake_send(0, FTL_PROTOCOL_RPCRETURN, buf.str()); - s->data(); - }; - - int res = s->call<int>("test1", 78); - - REQUIRE( res == 43 ); - } - - waithandler = nullptr; -} - -TEST_CASE("Socket::call+bind loop", "[rpc]") { - auto s = new ftl::net::Socket(0); - - // Just loop the send back to the recv - waithandler = [&]() { - s->data(); - }; - - SECTION( "Loop a numeric result" ) { - s->bind("test1", [](int a) -> int { - std::cout << "Bind test1 called" << std::endl; - return a*2; - }); - - int res = s->call<int>("test1", 5); - REQUIRE( res == 10 ); - } - - SECTION( "Loop a string result" ) { - s->bind("test1", [](std::string a) -> std::string { - std::cout << "Test1 = " << a << std::endl; - return a + " world"; - }); - - auto res = s->call<std::string>("test1", "hello"); - - REQUIRE( res == "hello world" ); - } -} - diff --git a/net/test/uri.cpp b/net/test/uri_unit.cpp similarity index 100% rename from net/test/uri.cpp rename to net/test/uri_unit.cpp