~ubuntu-branches/debian/experimental/kopete/experimental

« back to all changes in this revision

Viewing changes to protocols/jabber/googletalk/libjingle/talk/base/proxy_unittest.cc

  • Committer: Package Import Robot
  • Author(s): Maximiliano Curia
  • Date: 2015-02-24 11:32:57 UTC
  • mfrom: (1.1.41 vivid)
  • Revision ID: package-import@ubuntu.com-20150224113257-gnupg4v7lzz18ij0
Tags: 4:14.12.2-1
* New upstream release (14.12.2).
* Bump Standards-Version to 3.9.6, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
38
 
using talk_base::Socket;
39
 
using talk_base::Thread;
40
 
using talk_base::SocketAddress;
41
 
 
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);
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.
50
 
class 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.
61
 
class 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.
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());
99
 
 
100
 
  talk_base::TestEchoServer server(Thread::Current(), SocketAddress());
101
 
 
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);
106
 
 
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());
114
 
}
115
 
 
116
 
/*
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());
124
 
 
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());
131
 
}
132
 
*/
133
 
 
134
 
// Tests whether we can autodetect a SOCKS5 proxy.
135
 
TEST_F(ProxyTest, TestAutoDetectSocks5) {
136
 
  EXPECT_EQ(talk_base::PROXY_SOCKS5, DetectProxyType(kSocksProxyIntAddr));
137
 
}
138
 
 
139
 
/*
140
 
// Tests whether we can autodetect a HTTPS proxy.
141
 
TEST_F(ProxyTest, TestAutoDetectHttps) {
142
 
  EXPECT_EQ(talk_base::PROXY_HTTPS, DetectProxyType(kHttpsProxyIntAddr));
143
 
}
144
 
*/
145
 
 
146
 
// Tests whether we fail properly for no proxy.
147
 
TEST_F(ProxyTest, TestAutoDetectBogus) {
148
 
  EXPECT_EQ(talk_base::PROXY_UNKNOWN, DetectProxyType(kBogusProxyIntAddr));
149
 
}