blob: d73d9de038e7fdef0c80ce4076470b921d8b37d6 [file] [log] [blame]
deadbeeff137e972017-03-23 22:45:491/*
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>
Jonas Olssona4d87372019-07-05 17:08:3313
Mirko Bonadei92ea95e2017-09-15 04:47:3114#include "rtc_base/gunit.h"
Steve Anton10542f22019-01-11 17:11:0015#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"
deadbeeff137e972017-03-23 22:45:4920
21using rtc::Socket;
deadbeeff137e972017-03-23 22:45:4922using rtc::SocketAddress;
23
24static const SocketAddress kSocksProxyIntAddr("1.2.3.4", 1080);
25static const SocketAddress kSocksProxyExtAddr("1.2.3.5", 0);
deadbeeff137e972017-03-23 22:45:4926static const SocketAddress kBogusProxyIntAddr("1.2.3.4", 999);
27
Niels Möller83da5522018-09-26 14:18:3828// Sets up a virtual socket server and a SOCKS5 proxy server.
Mirko Bonadei6a489f22019-04-09 13:11:1229class ProxyTest : public ::testing::Test {
deadbeeff137e972017-03-23 22:45:4930 public:
deadbeef98e186c2017-05-17 01:00:0631 ProxyTest() : ss_(new rtc::VirtualSocketServer()), thread_(ss_.get()) {
Yves Gerey665174f2018-06-19 13:03:0532 socks_.reset(new rtc::SocksProxyServer(ss_.get(), kSocksProxyIntAddr,
33 ss_.get(), kSocksProxyExtAddr));
deadbeeff137e972017-03-23 22:45:4934 }
deadbeeff137e972017-03-23 22:45:4935
36 rtc::SocketServer* ss() { return ss_.get(); }
37
38 private:
39 std::unique_ptr<rtc::SocketServer> ss_;
nisse7eaa4ea2017-05-08 12:25:4140 rtc::AutoSocketServerThread thread_;
deadbeeff137e972017-03-23 22:45:4941 std::unique_ptr<rtc::SocksProxyServer> socks_;
deadbeeff137e972017-03-23 22:45:4942};
43
44// Tests whether we can use a SOCKS5 proxy to connect to a server.
45TEST_F(ProxyTest, TestSocks5Connect) {
46 rtc::AsyncSocket* socket =
47 ss()->CreateAsyncSocket(kSocksProxyIntAddr.family(), SOCK_STREAM);
Yves Gerey665174f2018-06-19 13:03:0548 rtc::AsyncSocksProxySocket* proxy_socket = new rtc::AsyncSocksProxySocket(
49 socket, kSocksProxyIntAddr, "", rtc::CryptString());
deadbeeff137e972017-03-23 22:45:4950 // TODO: IPv6-ize these tests when proxy supports IPv6.
51
Danil Chapovalov6dbf0e42018-11-13 17:07:4652 rtc::TestEchoServer server(rtc::Thread::Current(),
53 SocketAddress(INADDR_ANY, 0));
deadbeeff137e972017-03-23 22:45:4954
nisse32f25052017-05-08 08:57:1855 std::unique_ptr<rtc::AsyncTCPSocket> packet_socket(
56 rtc::AsyncTCPSocket::Create(proxy_socket, SocketAddress(INADDR_ANY, 0),
57 server.address()));
deadbeeff137e972017-03-23 22:45:4958 EXPECT_TRUE(packet_socket != nullptr);
nisse32f25052017-05-08 08:57:1859 rtc::TestClient client(std::move(packet_socket));
deadbeeff137e972017-03-23 22:45:4960
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}