You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2009 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <memory>
  11. #include <string>
  12. #include "rtc_base/gunit.h"
  13. #include "rtc_base/proxy_server.h"
  14. #include "rtc_base/socket_adapters.h"
  15. #include "rtc_base/test_client.h"
  16. #include "rtc_base/test_echo_server.h"
  17. #include "rtc_base/virtual_socket_server.h"
  18. using rtc::Socket;
  19. using rtc::SocketAddress;
  20. static const SocketAddress kSocksProxyIntAddr("1.2.3.4", 1080);
  21. static const SocketAddress kSocksProxyExtAddr("1.2.3.5", 0);
  22. static const SocketAddress kBogusProxyIntAddr("1.2.3.4", 999);
  23. // Sets up a virtual socket server and a SOCKS5 proxy server.
  24. class ProxyTest : public ::testing::Test {
  25. public:
  26. ProxyTest() : ss_(new rtc::VirtualSocketServer()), thread_(ss_.get()) {
  27. socks_.reset(new rtc::SocksProxyServer(ss_.get(), kSocksProxyIntAddr,
  28. ss_.get(), kSocksProxyExtAddr));
  29. }
  30. rtc::SocketServer* ss() { return ss_.get(); }
  31. private:
  32. std::unique_ptr<rtc::SocketServer> ss_;
  33. rtc::AutoSocketServerThread thread_;
  34. std::unique_ptr<rtc::SocksProxyServer> socks_;
  35. };
  36. // Tests whether we can use a SOCKS5 proxy to connect to a server.
  37. TEST_F(ProxyTest, TestSocks5Connect) {
  38. rtc::AsyncSocket* socket =
  39. ss()->CreateAsyncSocket(kSocksProxyIntAddr.family(), SOCK_STREAM);
  40. rtc::AsyncSocksProxySocket* proxy_socket = new rtc::AsyncSocksProxySocket(
  41. socket, kSocksProxyIntAddr, "", rtc::CryptString());
  42. // TODO: IPv6-ize these tests when proxy supports IPv6.
  43. rtc::TestEchoServer server(rtc::Thread::Current(),
  44. SocketAddress(INADDR_ANY, 0));
  45. std::unique_ptr<rtc::AsyncTCPSocket> packet_socket(
  46. rtc::AsyncTCPSocket::Create(proxy_socket, SocketAddress(INADDR_ANY, 0),
  47. server.address()));
  48. EXPECT_TRUE(packet_socket != nullptr);
  49. rtc::TestClient client(std::move(packet_socket));
  50. EXPECT_EQ(Socket::CS_CONNECTING, proxy_socket->GetState());
  51. EXPECT_TRUE(client.CheckConnected());
  52. EXPECT_EQ(Socket::CS_CONNECTED, proxy_socket->GetState());
  53. EXPECT_EQ(server.address(), client.remote_address());
  54. client.Send("foo", 3);
  55. EXPECT_TRUE(client.CheckNextPacket("foo", 3, nullptr));
  56. EXPECT_TRUE(client.CheckNoPacket());
  57. }