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
11 #include <memory>
12 #include <string>
13
14 #include "rtc_base/gunit.h"
15 #include "rtc_base/proxy_server.h"
16 #include "rtc_base/socket_adapters.h"
17 #include "rtc_base/test_client.h"
18 #include "rtc_base/test_echo_server.h"
19 #include "rtc_base/virtual_socket_server.h"
20
21 using rtc::Socket;
22 using rtc::SocketAddress;
23
24 static const SocketAddress kSocksProxyIntAddr("1.2.3.4", 1080);
25 static const SocketAddress kSocksProxyExtAddr("1.2.3.5", 0);
26 static const SocketAddress kBogusProxyIntAddr("1.2.3.4", 999);
27
28 // Sets up a virtual socket server and a SOCKS5 proxy server.
29 class ProxyTest : public ::testing::Test {
30 public:
ProxyTest()31 ProxyTest() : ss_(new rtc::VirtualSocketServer()), thread_(ss_.get()) {
32 socks_.reset(new rtc::SocksProxyServer(ss_.get(), kSocksProxyIntAddr,
33 ss_.get(), kSocksProxyExtAddr));
34 }
35
ss()36 rtc::SocketServer* ss() { return ss_.get(); }
37
38 private:
39 std::unique_ptr<rtc::SocketServer> ss_;
40 rtc::AutoSocketServerThread thread_;
41 std::unique_ptr<rtc::SocksProxyServer> socks_;
42 };
43
44 // Tests whether we can use a SOCKS5 proxy to connect to a server.
TEST_F(ProxyTest,TestSocks5Connect)45 TEST_F(ProxyTest, TestSocks5Connect) {
46 rtc::AsyncSocket* socket =
47 ss()->CreateAsyncSocket(kSocksProxyIntAddr.family(), SOCK_STREAM);
48 rtc::AsyncSocksProxySocket* proxy_socket = new rtc::AsyncSocksProxySocket(
49 socket, kSocksProxyIntAddr, "", rtc::CryptString());
50 // TODO: IPv6-ize these tests when proxy supports IPv6.
51
52 rtc::TestEchoServer server(rtc::Thread::Current(),
53 SocketAddress(INADDR_ANY, 0));
54
55 std::unique_ptr<rtc::AsyncTCPSocket> packet_socket(
56 rtc::AsyncTCPSocket::Create(proxy_socket, SocketAddress(INADDR_ANY, 0),
57 server.address()));
58 EXPECT_TRUE(packet_socket != nullptr);
59 rtc::TestClient client(std::move(packet_socket));
60
61 EXPECT_EQ(Socket::CS_CONNECTING, proxy_socket->GetState());
62 EXPECT_TRUE(client.CheckConnected());
63 EXPECT_EQ(Socket::CS_CONNECTED, proxy_socket->GetState());
64 EXPECT_EQ(server.address(), client.remote_address());
65 client.Send("foo", 3);
66 EXPECT_TRUE(client.CheckNextPacket("foo", 3, nullptr));
67 EXPECT_TRUE(client.CheckNoPacket());
68 }
69