blob: 987fd1e39668e2f3d2d013586a1708c8589265c8 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
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 "talk/base/logging.h"
29#include "talk/base/gunit.h"
30#include "talk/base/helpers.h"
31#include "talk/base/physicalsocketserver.h"
32#include "talk/base/scoped_ptr.h"
33#include "talk/base/socketadapters.h"
34#include "talk/base/socketaddress.h"
mallinath@webrtc.org5a59ccb2014-02-07 23:22:00 +000035#include "talk/base/ssladapter.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036#include "talk/base/thread.h"
37#include "talk/base/virtualsocketserver.h"
38#include "talk/p2p/base/basicpacketsocketfactory.h"
39#include "talk/p2p/base/relayport.h"
40#include "talk/p2p/base/relayserver.h"
41
42using talk_base::SocketAddress;
43
44static const SocketAddress kLocalAddress = SocketAddress("192.168.1.2", 0);
45static const SocketAddress kRelayUdpAddr = SocketAddress("99.99.99.1", 5000);
46static const SocketAddress kRelayTcpAddr = SocketAddress("99.99.99.2", 5001);
47static const SocketAddress kRelaySslAddr = SocketAddress("99.99.99.3", 443);
48static const SocketAddress kRelayExtAddr = SocketAddress("99.99.99.3", 5002);
49
50static const int kTimeoutMs = 1000;
51static const int kMaxTimeoutMs = 5000;
52
53// Tests connecting a RelayPort to a fake relay server
54// (cricket::RelayServer) using all currently available protocols. The
55// network layer is faked out by using a VirtualSocketServer for
56// creating sockets. The test will monitor the current state of the
57// RelayPort and created sockets by listening for signals such as,
58// SignalConnectFailure, SignalConnectTimeout, SignalSocketClosed and
59// SignalReadPacket.
60class RelayPortTest : public testing::Test,
61 public sigslot::has_slots<> {
62 public:
63 RelayPortTest()
64 : main_(talk_base::Thread::Current()),
65 physical_socket_server_(new talk_base::PhysicalSocketServer),
66 virtual_socket_server_(new talk_base::VirtualSocketServer(
67 physical_socket_server_.get())),
68 ss_scope_(virtual_socket_server_.get()),
69 network_("unittest", "unittest", talk_base::IPAddress(INADDR_ANY), 32),
70 socket_factory_(talk_base::Thread::Current()),
71 username_(talk_base::CreateRandomString(16)),
72 password_(talk_base::CreateRandomString(16)),
73 relay_port_(cricket::RelayPort::Create(main_, &socket_factory_,
74 &network_,
75 kLocalAddress.ipaddr(),
76 0, 0, username_, password_)),
77 relay_server_(new cricket::RelayServer(main_)) {
78 }
79
80 void OnReadPacket(talk_base::AsyncPacketSocket* socket,
81 const char* data, size_t size,
wu@webrtc.orga9890802013-12-13 00:21:03 +000082 const talk_base::SocketAddress& remote_addr,
83 const talk_base::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084 received_packet_count_[socket]++;
85 }
86
87 void OnConnectFailure(const cricket::ProtocolAddress* addr) {
88 failed_connections_.push_back(*addr);
89 }
90
91 void OnSoftTimeout(const cricket::ProtocolAddress* addr) {
92 soft_timedout_connections_.push_back(*addr);
93 }
94
95 protected:
96 static void SetUpTestCase() {
mallinath@webrtc.org5a59ccb2014-02-07 23:22:00 +000097 talk_base::InitializeSSL();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 }
99
mallinath@webrtc.org5a59ccb2014-02-07 23:22:00 +0000100 static void TearDownTestCase() {
101 talk_base::CleanupSSL();
102 }
103
104
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 virtual void SetUp() {
106 // The relay server needs an external socket to work properly.
107 talk_base::AsyncUDPSocket* ext_socket =
108 CreateAsyncUdpSocket(kRelayExtAddr);
109 relay_server_->AddExternalSocket(ext_socket);
110
111 // Listen for failures.
112 relay_port_->SignalConnectFailure.
113 connect(this, &RelayPortTest::OnConnectFailure);
114
115 // Listen for soft timeouts.
116 relay_port_->SignalSoftTimeout.
117 connect(this, &RelayPortTest::OnSoftTimeout);
118 }
119
120 // Udp has the highest 'goodness' value of the three different
121 // protocols used for connecting to the relay server. As soon as
122 // PrepareAddress is called, the RelayPort will start trying to
123 // connect to the given UDP address. As soon as a response to the
124 // sent STUN allocate request message has been received, the
125 // RelayPort will consider the connection to be complete and will
126 // abort any other connection attempts.
127 void TestConnectUdp() {
128 // Add a UDP socket to the relay server.
129 talk_base::AsyncUDPSocket* internal_udp_socket =
130 CreateAsyncUdpSocket(kRelayUdpAddr);
131 talk_base::AsyncSocket* server_socket = CreateServerSocket(kRelayTcpAddr);
132
133 relay_server_->AddInternalSocket(internal_udp_socket);
134 relay_server_->AddInternalServerSocket(server_socket, cricket::PROTO_TCP);
135
136 // Now add our relay addresses to the relay port and let it start.
137 relay_port_->AddServerAddress(
138 cricket::ProtocolAddress(kRelayUdpAddr, cricket::PROTO_UDP));
139 relay_port_->AddServerAddress(
140 cricket::ProtocolAddress(kRelayTcpAddr, cricket::PROTO_TCP));
141 relay_port_->PrepareAddress();
142
143 // Should be connected.
144 EXPECT_TRUE_WAIT(relay_port_->IsReady(), kTimeoutMs);
145
146 // Make sure that we are happy with UDP, ie. not continuing with
147 // TCP, SSLTCP, etc.
148 WAIT(relay_server_->HasConnection(kRelayTcpAddr), kTimeoutMs);
149
150 // Should have only one connection.
151 EXPECT_EQ(1, relay_server_->GetConnectionCount());
152
153 // Should be the UDP address.
154 EXPECT_TRUE(relay_server_->HasConnection(kRelayUdpAddr));
155 }
156
157 // TCP has the second best 'goodness' value, and as soon as UDP
158 // connection has failed, the RelayPort will attempt to connect via
159 // TCP. Here we add a fake UDP address together with a real TCP
160 // address to simulate an UDP failure. As soon as UDP has failed the
161 // RelayPort will try the TCP adress and succed.
162 void TestConnectTcp() {
163 // Create a fake UDP address for relay port to simulate a failure.
164 cricket::ProtocolAddress fake_protocol_address =
165 cricket::ProtocolAddress(kRelayUdpAddr, cricket::PROTO_UDP);
166
167 // Create a server socket for the RelayServer.
168 talk_base::AsyncSocket* server_socket = CreateServerSocket(kRelayTcpAddr);
169 relay_server_->AddInternalServerSocket(server_socket, cricket::PROTO_TCP);
170
171 // Add server addresses to the relay port and let it start.
172 relay_port_->AddServerAddress(
173 cricket::ProtocolAddress(fake_protocol_address));
174 relay_port_->AddServerAddress(
175 cricket::ProtocolAddress(kRelayTcpAddr, cricket::PROTO_TCP));
176 relay_port_->PrepareAddress();
177
178 EXPECT_FALSE(relay_port_->IsReady());
179
180 // Should have timed out in 200 + 200 + 400 + 800 + 1600 ms.
181 EXPECT_TRUE_WAIT(HasFailed(&fake_protocol_address), 3600);
182
183 // Wait until relayport is ready.
184 EXPECT_TRUE_WAIT(relay_port_->IsReady(), kMaxTimeoutMs);
185
186 // Should have only one connection.
187 EXPECT_EQ(1, relay_server_->GetConnectionCount());
188
189 // Should be the TCP address.
190 EXPECT_TRUE(relay_server_->HasConnection(kRelayTcpAddr));
191 }
192
193 void TestConnectSslTcp() {
194 // Create a fake TCP address for relay port to simulate a failure.
195 // We skip UDP here since transition from UDP to TCP has been
196 // tested above.
197 cricket::ProtocolAddress fake_protocol_address =
198 cricket::ProtocolAddress(kRelayTcpAddr, cricket::PROTO_TCP);
199
200 // Create a ssl server socket for the RelayServer.
201 talk_base::AsyncSocket* ssl_server_socket =
202 CreateServerSocket(kRelaySslAddr);
203 relay_server_->AddInternalServerSocket(ssl_server_socket,
204 cricket::PROTO_SSLTCP);
205
206 // Create a tcp server socket that listens on the fake address so
207 // the relay port can attempt to connect to it.
208 talk_base::scoped_ptr<talk_base::AsyncSocket> tcp_server_socket(
209 CreateServerSocket(kRelayTcpAddr));
210
211 // Add server addresses to the relay port and let it start.
212 relay_port_->AddServerAddress(fake_protocol_address);
213 relay_port_->AddServerAddress(
214 cricket::ProtocolAddress(kRelaySslAddr, cricket::PROTO_SSLTCP));
215 relay_port_->PrepareAddress();
216 EXPECT_FALSE(relay_port_->IsReady());
217
218 // Should have timed out in 3000 ms(relayport.cc, kSoftConnectTimeoutMs).
219 EXPECT_TRUE_WAIT_MARGIN(HasTimedOut(&fake_protocol_address), 3000, 100);
220
221 // Wait until relayport is ready.
222 EXPECT_TRUE_WAIT(relay_port_->IsReady(), kMaxTimeoutMs);
223
224 // Should have only one connection.
225 EXPECT_EQ(1, relay_server_->GetConnectionCount());
226
227 // Should be the SSLTCP address.
228 EXPECT_TRUE(relay_server_->HasConnection(kRelaySslAddr));
229 }
230
231 private:
232 talk_base::AsyncUDPSocket* CreateAsyncUdpSocket(const SocketAddress addr) {
233 talk_base::AsyncSocket* socket =
234 virtual_socket_server_->CreateAsyncSocket(SOCK_DGRAM);
235 talk_base::AsyncUDPSocket* packet_socket =
236 talk_base::AsyncUDPSocket::Create(socket, addr);
237 EXPECT_TRUE(packet_socket != NULL);
238 packet_socket->SignalReadPacket.connect(this, &RelayPortTest::OnReadPacket);
239 return packet_socket;
240 }
241
242 talk_base::AsyncSocket* CreateServerSocket(const SocketAddress addr) {
243 talk_base::AsyncSocket* socket =
244 virtual_socket_server_->CreateAsyncSocket(SOCK_STREAM);
245 EXPECT_GE(socket->Bind(addr), 0);
246 EXPECT_GE(socket->Listen(5), 0);
247 return socket;
248 }
249
250 bool HasFailed(cricket::ProtocolAddress* addr) {
251 for (size_t i = 0; i < failed_connections_.size(); i++) {
252 if (failed_connections_[i].address == addr->address &&
253 failed_connections_[i].proto == addr->proto) {
254 return true;
255 }
256 }
257 return false;
258 }
259
260 bool HasTimedOut(cricket::ProtocolAddress* addr) {
261 for (size_t i = 0; i < soft_timedout_connections_.size(); i++) {
262 if (soft_timedout_connections_[i].address == addr->address &&
263 soft_timedout_connections_[i].proto == addr->proto) {
264 return true;
265 }
266 }
267 return false;
268 }
269
270 typedef std::map<talk_base::AsyncPacketSocket*, int> PacketMap;
271
272 talk_base::Thread* main_;
273 talk_base::scoped_ptr<talk_base::PhysicalSocketServer>
274 physical_socket_server_;
275 talk_base::scoped_ptr<talk_base::VirtualSocketServer> virtual_socket_server_;
276 talk_base::SocketServerScope ss_scope_;
277 talk_base::Network network_;
278 talk_base::BasicPacketSocketFactory socket_factory_;
279 std::string username_;
280 std::string password_;
281 talk_base::scoped_ptr<cricket::RelayPort> relay_port_;
282 talk_base::scoped_ptr<cricket::RelayServer> relay_server_;
283 std::vector<cricket::ProtocolAddress> failed_connections_;
284 std::vector<cricket::ProtocolAddress> soft_timedout_connections_;
285 PacketMap received_packet_count_;
286};
287
288TEST_F(RelayPortTest, ConnectUdp) {
289 TestConnectUdp();
290}
291
292TEST_F(RelayPortTest, ConnectTcp) {
293 TestConnectTcp();
294}
295
296TEST_F(RelayPortTest, ConnectSslTcp) {
297 TestConnectSslTcp();
298}