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 28#include <string> 29#include "talk/base/autodetectproxy.h" 30#include "talk/base/gunit.h" 31#include "talk/base/httpserver.h" 32#include "talk/base/proxyserver.h" 33#include "talk/base/socketadapters.h" 34#include "talk/base/testclient.h" 35#include "talk/base/testechoserver.h" 36#include "talk/base/virtualsocketserver.h" 37 38using talk_base::Socket; 39using talk_base::Thread; 40using talk_base::SocketAddress; 41 42static const SocketAddress kSocksProxyIntAddr("1.2.3.4", 1080); 43static const SocketAddress kSocksProxyExtAddr("1.2.3.5", 0); 44static const SocketAddress kHttpsProxyIntAddr("1.2.3.4", 443); 45static const SocketAddress kHttpsProxyExtAddr("1.2.3.5", 0); 46static const SocketAddress kBogusProxyIntAddr("1.2.3.4", 999); 47 48// Used to run a proxy detect on the current thread. Otherwise we would need 49// to make both threads share the same VirtualSocketServer. 50class AutoDetectProxyRunner : public talk_base::AutoDetectProxy { 51 public: 52 explicit AutoDetectProxyRunner(const std::string& agent) 53 : AutoDetectProxy(agent) {} 54 void Run() { 55 DoWork(); 56 Thread::Current()->Restart(); // needed to reset the messagequeue 57 } 58}; 59 60// Sets up a virtual socket server and HTTPS/SOCKS5 proxy servers. 61class ProxyTest : public testing::Test { 62 public: 63 ProxyTest() : ss_(new talk_base::VirtualSocketServer(NULL)) { 64 Thread::Current()->set_socketserver(ss_.get()); 65 socks_.reset(new talk_base::SocksProxyServer( 66 ss_.get(), kSocksProxyIntAddr, ss_.get(), kSocksProxyExtAddr)); 67 https_.reset(new talk_base::HttpListenServer()); 68 https_->Listen(kHttpsProxyIntAddr); 69 } 70 ~ProxyTest() { 71 Thread::Current()->set_socketserver(NULL); 72 } 73 74 talk_base::SocketServer* ss() { return ss_.get(); } 75 76 talk_base::ProxyType DetectProxyType(const SocketAddress& address) { 77 talk_base::ProxyType type; 78 AutoDetectProxyRunner* detect = new AutoDetectProxyRunner("unittest/1.0"); 79 detect->set_proxy(address); 80 detect->Run(); // blocks until done 81 type = detect->proxy().type; 82 detect->Destroy(false); 83 return type; 84 } 85 86 private: 87 talk_base::scoped_ptr<talk_base::SocketServer> ss_; 88 talk_base::scoped_ptr<talk_base::SocksProxyServer> socks_; 89 // TODO: Make this a real HTTPS proxy server. 90 talk_base::scoped_ptr<talk_base::HttpListenServer> https_; 91}; 92 93// Tests whether we can use a SOCKS5 proxy to connect to a server. 94TEST_F(ProxyTest, TestSocks5Connect) { 95 talk_base::AsyncSocket* socket = 96 ss()->CreateAsyncSocket(kSocksProxyIntAddr.family(), SOCK_STREAM); 97 talk_base::AsyncSocksProxySocket* proxy_socket = 98 new talk_base::AsyncSocksProxySocket(socket, kSocksProxyIntAddr, 99 "", talk_base::CryptString()); 100 // TODO: IPv6-ize these tests when proxy supports IPv6. 101 102 talk_base::TestEchoServer server(Thread::Current(), 103 SocketAddress(INADDR_ANY, 0)); 104 105 talk_base::AsyncTCPSocket* packet_socket = talk_base::AsyncTCPSocket::Create( 106 proxy_socket, SocketAddress(INADDR_ANY, 0), server.address()); 107 EXPECT_TRUE(packet_socket != NULL); 108 talk_base::TestClient client(packet_socket); 109 110 EXPECT_EQ(Socket::CS_CONNECTING, proxy_socket->GetState()); 111 EXPECT_TRUE(client.CheckConnected()); 112 EXPECT_EQ(Socket::CS_CONNECTED, proxy_socket->GetState()); 113 EXPECT_EQ(server.address(), client.remote_address()); 114 client.Send("foo", 3); 115 EXPECT_TRUE(client.CheckNextPacket("foo", 3, NULL)); 116 EXPECT_TRUE(client.CheckNoPacket()); 117} 118 119/* 120// Tests whether we can use a HTTPS proxy to connect to a server. 121TEST_F(ProxyTest, TestHttpsConnect) { 122 AsyncSocket* socket = ss()->CreateAsyncSocket(SOCK_STREAM); 123 AsyncHttpsProxySocket* proxy_socket = new AsyncHttpsProxySocket( 124 socket, "unittest/1.0", kHttpsProxyIntAddress, "", CryptString()); 125 TestClient client(new AsyncTCPSocket(proxy_socket)); 126 TestEchoServer server(Thread::Current(), SocketAddress()); 127 128 EXPECT_TRUE(client.Connect(server.address())); 129 EXPECT_TRUE(client.CheckConnected()); 130 EXPECT_EQ(server.address(), client.remote_address()); 131 client.Send("foo", 3); 132 EXPECT_TRUE(client.CheckNextPacket("foo", 3, NULL)); 133 EXPECT_TRUE(client.CheckNoPacket()); 134} 135*/ 136 137// Tests whether we can autodetect a SOCKS5 proxy. 138TEST_F(ProxyTest, TestAutoDetectSocks5) { 139 EXPECT_EQ(talk_base::PROXY_SOCKS5, DetectProxyType(kSocksProxyIntAddr)); 140} 141 142/* 143// Tests whether we can autodetect a HTTPS proxy. 144TEST_F(ProxyTest, TestAutoDetectHttps) { 145 EXPECT_EQ(talk_base::PROXY_HTTPS, DetectProxyType(kHttpsProxyIntAddr)); 146} 147*/ 148 149// Tests whether we fail properly for no proxy. 150TEST_F(ProxyTest, TestAutoDetectBogus) { 151 EXPECT_EQ(talk_base::PROXY_UNKNOWN, DetectProxyType(kBogusProxyIntAddr)); 152} 153