3
* Copyright 2009, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
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.
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.
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"
38
using talk_base::Socket;
39
using talk_base::Thread;
40
using talk_base::SocketAddress;
42
static const SocketAddress kSocksProxyIntAddr("1.2.3.4", 1080);
43
static const SocketAddress kSocksProxyExtAddr("1.2.3.5", 0);
44
static const SocketAddress kHttpsProxyIntAddr("1.2.3.4", 443);
45
static const SocketAddress kHttpsProxyExtAddr("1.2.3.5", 0);
46
static const SocketAddress kBogusProxyIntAddr("1.2.3.4", 999);
48
// Used to run a proxy detect on the current thread. Otherwise we would need
49
// to make both threads share the same VirtualSocketServer.
50
class AutoDetectProxyRunner : public talk_base::AutoDetectProxy {
52
explicit AutoDetectProxyRunner(const std::string& agent)
53
: AutoDetectProxy(agent) {}
56
Thread::Current()->Restart(); // needed to reset the messagequeue
60
// Sets up a virtual socket server and HTTPS/SOCKS5 proxy servers.
61
class ProxyTest : public testing::Test {
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);
71
Thread::Current()->set_socketserver(NULL);
74
talk_base::SocketServer* ss() { return ss_.get(); }
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);
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_;
93
// Tests whether we can use a SOCKS5 proxy to connect to a server.
94
TEST_F(ProxyTest, TestSocks5Connect) {
95
talk_base::AsyncSocket* socket = ss()->CreateAsyncSocket(SOCK_STREAM);
96
talk_base::AsyncSocksProxySocket* proxy_socket =
97
new talk_base::AsyncSocksProxySocket(socket, kSocksProxyIntAddr,
98
"", talk_base::CryptString());
100
talk_base::TestEchoServer server(Thread::Current(), SocketAddress());
102
talk_base::AsyncTCPSocket* packet_socket = talk_base::AsyncTCPSocket::Create(
103
proxy_socket, SocketAddress(), server.address());
104
EXPECT_TRUE(packet_socket != NULL);
105
talk_base::TestClient client(packet_socket);
107
EXPECT_EQ(Socket::CS_CONNECTING, proxy_socket->GetState());
108
EXPECT_TRUE(client.CheckConnected());
109
EXPECT_EQ(Socket::CS_CONNECTED, proxy_socket->GetState());
110
EXPECT_EQ(server.address(), client.remote_address());
111
client.Send("foo", 3);
112
EXPECT_TRUE(client.CheckNextPacket("foo", 3, NULL));
113
EXPECT_TRUE(client.CheckNoPacket());
117
// Tests whether we can use a HTTPS proxy to connect to a server.
118
TEST_F(ProxyTest, TestHttpsConnect) {
119
AsyncSocket* socket = ss()->CreateAsyncSocket(SOCK_STREAM);
120
AsyncHttpsProxySocket* proxy_socket = new AsyncHttpsProxySocket(
121
socket, "unittest/1.0", kHttpsProxyIntAddress, "", CryptString());
122
TestClient client(new AsyncTCPSocket(proxy_socket));
123
TestEchoServer server(Thread::Current(), SocketAddress());
125
EXPECT_TRUE(client.Connect(server.address()));
126
EXPECT_TRUE(client.CheckConnected());
127
EXPECT_EQ(server.address(), client.remote_address());
128
client.Send("foo", 3);
129
EXPECT_TRUE(client.CheckNextPacket("foo", 3, NULL));
130
EXPECT_TRUE(client.CheckNoPacket());
134
// Tests whether we can autodetect a SOCKS5 proxy.
135
TEST_F(ProxyTest, TestAutoDetectSocks5) {
136
EXPECT_EQ(talk_base::PROXY_SOCKS5, DetectProxyType(kSocksProxyIntAddr));
140
// Tests whether we can autodetect a HTTPS proxy.
141
TEST_F(ProxyTest, TestAutoDetectHttps) {
142
EXPECT_EQ(talk_base::PROXY_HTTPS, DetectProxyType(kHttpsProxyIntAddr));
146
// Tests whether we fail properly for no proxy.
147
TEST_F(ProxyTest, TestAutoDetectBogus) {
148
EXPECT_EQ(talk_base::PROXY_UNKNOWN, DetectProxyType(kBogusProxyIntAddr));