diff options
Diffstat (limited to 'src/test_relayproc.cpp')
| -rw-r--r-- | src/test_relayproc.cpp | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/test_relayproc.cpp b/src/test_relayproc.cpp new file mode 100644 index 0000000..f9d7f07 --- /dev/null +++ b/src/test_relayproc.cpp | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | // Tests the tunnel against a SEPARATE running bitflashd relay process (not an | ||
| 2 | // in-process relay) -- i.e. exactly the VPS deployment shape, on loopback. | ||
| 3 | // Usage: test_relayproc.exe <port> (a bitflashd must already listen there) | ||
| 4 | // | ||
| 5 | // Build: g++ -std=gnu++14 -O2 -pthread test_relayproc.cpp btftunnel.cpp btfrv.cpp btfchan.cpp -lsodium -lws2_32 -o test_relayproc.exe | ||
| 6 | |||
| 7 | #include "btftunnel.h" | ||
| 8 | #include "btfrv.h" | ||
| 9 | #include "btfchan.h" | ||
| 10 | #include <winsock2.h> | ||
| 11 | #include <cstdio> | ||
| 12 | #include <cstdlib> | ||
| 13 | #include <cstring> | ||
| 14 | #include <string> | ||
| 15 | #include <vector> | ||
| 16 | #include <thread> | ||
| 17 | #include <chrono> | ||
| 18 | |||
| 19 | using namespace btf; | ||
| 20 | static int g_fail = 0; | ||
| 21 | #define CHECK(c,n) do{ if(c){printf(" ok %s\n",n);} else {printf(" FAIL %s\n",n); g_fail++;} }while(0) | ||
| 22 | |||
| 23 | static unsigned short PORT = 18446; | ||
| 24 | static const char* HOST = "127.0.0.1"; | ||
| 25 | static bool RecvN(SOCKET s, void* b, int n){char*p=(char*)b;int o=0;while(o<n){int r=recv(s,p+o,n-o,0);if(r<=0)return false;o+=r;}return true;} | ||
| 26 | static bool SendN(SOCKET s, const void* b, int n){const char*p=(const char*)b;int o=0;while(o<n){int r=send(s,p+o,n-o,0);if(r<=0)return false;o+=r;}return true;} | ||
| 27 | |||
| 28 | static bool g_ok=false; static std::string g_got; | ||
| 29 | static void Service(std::vector<unsigned char> id, std::vector<unsigned char> sk, int len) | ||
| 30 | { | ||
| 31 | RvSocket rv = RvServiceRegister(HOST, PORT, id.data()); | ||
| 32 | if (rv==RV_INVALID) return; | ||
| 33 | btf_socket_t app = BtfServiceWrap(rv, sk.data()); | ||
| 34 | if (app==INVALID_SOCKET) return; | ||
| 35 | std::vector<char> buf(len); | ||
| 36 | if(!RecvN((SOCKET)app, buf.data(), len)){closesocket((SOCKET)app);return;} | ||
| 37 | g_got.assign(buf.data(), len); | ||
| 38 | g_ok = SendN((SOCKET)app, buf.data(), len); | ||
| 39 | closesocket((SOCKET)app); | ||
| 40 | } | ||
| 41 | |||
| 42 | int main(int argc, char* argv[]) | ||
| 43 | { | ||
| 44 | if (argc>=2) HOST=argv[1]; if (argc>=3) PORT=(unsigned short)atoi(argv[2]); | ||
| 45 | if(!TunnelInit()){printf("init failed\n");return 2;} | ||
| 46 | unsigned char pk[32], sk[32]; ChanKeypair(pk,sk); | ||
| 47 | std::vector<unsigned char> id(32); for(int i=0;i<32;i++) id[i]=(unsigned char)(i*11+3); | ||
| 48 | std::string msg="anonymous hop through a real bitflashd relay process"; | ||
| 49 | int len=(int)msg.size(); | ||
| 50 | |||
| 51 | printf("tunnel_through_external_relay_process (port %u)\n", PORT); | ||
| 52 | std::thread svc(Service, id, std::vector<unsigned char>(sk,sk+32), len); | ||
| 53 | std::this_thread::sleep_for(std::chrono::milliseconds(400)); | ||
| 54 | |||
| 55 | btf_socket_t app = BtfClientTunnel(HOST, PORT, id.data(), pk); | ||
| 56 | CHECK(app!=INVALID_SOCKET, "client tunneled through the running bitflashd"); | ||
| 57 | if(app!=INVALID_SOCKET){ | ||
| 58 | CHECK(SendN((SOCKET)app,msg.data(),len), "app writes plaintext"); | ||
| 59 | std::vector<char> echo(len); | ||
| 60 | CHECK(RecvN((SOCKET)app,echo.data(),len), "app reads echo"); | ||
| 61 | CHECK(std::string(echo.data(),len)==msg, "round-trips end-to-end via the relay process"); | ||
| 62 | closesocket((SOCKET)app); | ||
| 63 | } | ||
| 64 | svc.join(); | ||
| 65 | CHECK(g_ok && g_got==msg, "service saw correct plaintext"); | ||
| 66 | printf("\n%s (%d failures)\n", g_fail==0?"ALL TESTS PASSED":"TESTS FAILED", g_fail); | ||
| 67 | return g_fail==0?0:1; | ||
| 68 | } | ||