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.

152 lines
6.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2009, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <string>
  28. #include "talk/base/autodetectproxy.h"
  29. #include "talk/base/gunit.h"
  30. #include "talk/base/httpserver.h"
  31. #include "talk/base/proxyserver.h"
  32. #include "talk/base/socketadapters.h"
  33. #include "talk/base/testclient.h"
  34. #include "talk/base/testechoserver.h"
  35. #include "talk/base/virtualsocketserver.h"
  36. using talk_base::Socket;
  37. using talk_base::Thread;
  38. using talk_base::SocketAddress;
  39. static const SocketAddress kSocksProxyIntAddr("1.2.3.4", 1080);
  40. static const SocketAddress kSocksProxyExtAddr("1.2.3.5", 0);
  41. static const SocketAddress kHttpsProxyIntAddr("1.2.3.4", 443);
  42. static const SocketAddress kHttpsProxyExtAddr("1.2.3.5", 0);
  43. static const SocketAddress kBogusProxyIntAddr("1.2.3.4", 999);
  44. // Used to run a proxy detect on the current thread. Otherwise we would need
  45. // to make both threads share the same VirtualSocketServer.
  46. class AutoDetectProxyRunner : public talk_base::AutoDetectProxy {
  47. public:
  48. explicit AutoDetectProxyRunner(const std::string& agent)
  49. : AutoDetectProxy(agent) {}
  50. void Run() {
  51. DoWork();
  52. Thread::Current()->Restart(); // needed to reset the messagequeue
  53. }
  54. };
  55. // Sets up a virtual socket server and HTTPS/SOCKS5 proxy servers.
  56. class ProxyTest : public testing::Test {
  57. public:
  58. ProxyTest() : ss_(new talk_base::VirtualSocketServer(NULL)) {
  59. Thread::Current()->set_socketserver(ss_.get());
  60. socks_.reset(new talk_base::SocksProxyServer(
  61. ss_.get(), kSocksProxyIntAddr, ss_.get(), kSocksProxyExtAddr));
  62. https_.reset(new talk_base::HttpListenServer());
  63. https_->Listen(kHttpsProxyIntAddr);
  64. }
  65. ~ProxyTest() {
  66. Thread::Current()->set_socketserver(NULL);
  67. }
  68. talk_base::SocketServer* ss() { return ss_.get(); }
  69. talk_base::ProxyType DetectProxyType(const SocketAddress& address) {
  70. talk_base::ProxyType type;
  71. AutoDetectProxyRunner* detect = new AutoDetectProxyRunner("unittest/1.0");
  72. detect->set_proxy(address);
  73. detect->Run(); // blocks until done
  74. type = detect->proxy().type;
  75. detect->Destroy(false);
  76. return type;
  77. }
  78. private:
  79. talk_base::scoped_ptr<talk_base::SocketServer> ss_;
  80. talk_base::scoped_ptr<talk_base::SocksProxyServer> socks_;
  81. // TODO: Make this a real HTTPS proxy server.
  82. talk_base::scoped_ptr<talk_base::HttpListenServer> https_;
  83. };
  84. // Tests whether we can use a SOCKS5 proxy to connect to a server.
  85. TEST_F(ProxyTest, TestSocks5Connect) {
  86. talk_base::AsyncSocket* socket =
  87. ss()->CreateAsyncSocket(kSocksProxyIntAddr.family(), SOCK_STREAM);
  88. talk_base::AsyncSocksProxySocket* proxy_socket =
  89. new talk_base::AsyncSocksProxySocket(socket, kSocksProxyIntAddr,
  90. "", talk_base::CryptString());
  91. // TODO: IPv6-ize these tests when proxy supports IPv6.
  92. talk_base::TestEchoServer server(Thread::Current(),
  93. SocketAddress(INADDR_ANY, 0));
  94. talk_base::AsyncTCPSocket* packet_socket = talk_base::AsyncTCPSocket::Create(
  95. proxy_socket, SocketAddress(INADDR_ANY, 0), server.address());
  96. EXPECT_TRUE(packet_socket != NULL);
  97. talk_base::TestClient client(packet_socket);
  98. EXPECT_EQ(Socket::CS_CONNECTING, proxy_socket->GetState());
  99. EXPECT_TRUE(client.CheckConnected());
  100. EXPECT_EQ(Socket::CS_CONNECTED, proxy_socket->GetState());
  101. EXPECT_EQ(server.address(), client.remote_address());
  102. client.Send("foo", 3);
  103. EXPECT_TRUE(client.CheckNextPacket("foo", 3, NULL));
  104. EXPECT_TRUE(client.CheckNoPacket());
  105. }
  106. /*
  107. // Tests whether we can use a HTTPS proxy to connect to a server.
  108. TEST_F(ProxyTest, TestHttpsConnect) {
  109. AsyncSocket* socket = ss()->CreateAsyncSocket(SOCK_STREAM);
  110. AsyncHttpsProxySocket* proxy_socket = new AsyncHttpsProxySocket(
  111. socket, "unittest/1.0", kHttpsProxyIntAddress, "", CryptString());
  112. TestClient client(new AsyncTCPSocket(proxy_socket));
  113. TestEchoServer server(Thread::Current(), SocketAddress());
  114. EXPECT_TRUE(client.Connect(server.address()));
  115. EXPECT_TRUE(client.CheckConnected());
  116. EXPECT_EQ(server.address(), client.remote_address());
  117. client.Send("foo", 3);
  118. EXPECT_TRUE(client.CheckNextPacket("foo", 3, NULL));
  119. EXPECT_TRUE(client.CheckNoPacket());
  120. }
  121. */
  122. // Tests whether we can autodetect a SOCKS5 proxy.
  123. TEST_F(ProxyTest, TestAutoDetectSocks5) {
  124. EXPECT_EQ(talk_base::PROXY_SOCKS5, DetectProxyType(kSocksProxyIntAddr));
  125. }
  126. /*
  127. // Tests whether we can autodetect a HTTPS proxy.
  128. TEST_F(ProxyTest, TestAutoDetectHttps) {
  129. EXPECT_EQ(talk_base::PROXY_HTTPS, DetectProxyType(kHttpsProxyIntAddr));
  130. }
  131. */
  132. // Tests whether we fail properly for no proxy.
  133. TEST_F(ProxyTest, TestAutoDetectBogus) {
  134. EXPECT_EQ(talk_base::PROXY_UNKNOWN, DetectProxyType(kBogusProxyIntAddr));
  135. }