Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. #include <cactus/test.h>
  2.  
  3. #include "socksd.h"
  4.  
  5. using namespace cactus;
  6.  
  7. const folly::SocketAddress kTestProxyAddress("127.0.0.1", 58432);
  8. const folly::SocketAddress kTestServerAddress("127.0.0.1", 58433);
  9.  
  10. // Using fake client. Implementing real client is a separate task, and we don't want to show our solution here.
  11. const std::string kFakeRequest = std::string("\x04\x01\xe4\x41\x7f\x00\x00\x01", 8) + std::string("prime\0", 6);
  12.  
  13. FIBER_TEST_CASE("Ping pong proxy") {
  14. SocksServer socks(kTestProxyAddress);
  15. auto pingPong = ListenTCP(kTestServerAddress);
  16.  
  17. ServerGroup g;
  18. g.Spawn([&] {
  19. socks.Serve();
  20. });
  21.  
  22. int clients = 0;
  23. g.Spawn([&] {
  24. std::shared_ptr<IConn> client = pingPong->Accept();
  25. ++clients;
  26. g.Spawn([client] {
  27. BufferedReader reader(client.get());
  28. reader.WriteTo(client.get());
  29. });
  30. });
  31.  
  32. auto conn = DialTCP(kTestProxyAddress);
  33. conn->Write(View(kFakeRequest));
  34.  
  35. std::array<char, 8> reply{};
  36. conn->ReadFull(View(reply));
  37.  
  38. SECTION("Echo") {
  39. for (int i = 0; i < 1024; ++i) {
  40. std::array<int, 1> ping{i};
  41. conn->Write(View(ping));
  42. conn->ReadFull(View(ping));
  43. REQUIRE(ping[0] == i);
  44. }
  45.  
  46. REQUIRE(1 == clients);
  47. }
  48.  
  49. SECTION("Half open shutdown") {
  50. std::string hello = "hello";
  51. conn->Write(View(hello));
  52. conn->CloseWrite();
  53.  
  54. REQUIRE("hello" == conn->ReadAllToString());
  55. REQUIRE(1 == clients);
  56. }
  57. }
  58.  
  59. FIBER_TEST_CASE("Invalid inputs") {
  60. SocksServer socks(kTestProxyAddress);
  61. ServerGroup g;
  62.  
  63. g.Spawn([&] {
  64. socks.Serve();
  65. });
  66.  
  67. SECTION("Username overflow") {
  68. std::string fakeHeader("\x04\x01\x16\x00\x08\x08\x08\x08", 8);
  69.  
  70. auto conn = DialTCP(kTestProxyAddress);
  71. conn->Write(View(fakeHeader));
  72.  
  73. auto overflowBuffer = [&] {
  74. BufferedWriter buf(conn.get());
  75. for (;;) {
  76. std::string smiley = ":)";
  77. buf.Write(View(smiley));
  78. }
  79. };
  80.  
  81. REQUIRE_THROWS_AS(overflowBuffer(), std::system_error);
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement