Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: webrtc/p2p/base/turnport_unittest.cc

Issue 2097793003: Using fake clock for TURN port tests and un-disabling some tests. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2012 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2012 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 #if defined(WEBRTC_POSIX) 10 #if defined(WEBRTC_POSIX)
11 #include <dirent.h> 11 #include <dirent.h>
12 #endif 12 #endif
13 13
14 #include <memory> 14 #include <memory>
15 15
16 #include "webrtc/p2p/base/basicpacketsocketfactory.h" 16 #include "webrtc/p2p/base/basicpacketsocketfactory.h"
17 #include "webrtc/p2p/base/p2pconstants.h" 17 #include "webrtc/p2p/base/p2pconstants.h"
18 #include "webrtc/p2p/base/portallocator.h" 18 #include "webrtc/p2p/base/portallocator.h"
19 #include "webrtc/p2p/base/tcpport.h" 19 #include "webrtc/p2p/base/tcpport.h"
20 #include "webrtc/p2p/base/testturnserver.h" 20 #include "webrtc/p2p/base/testturnserver.h"
21 #include "webrtc/p2p/base/turnport.h" 21 #include "webrtc/p2p/base/turnport.h"
22 #include "webrtc/p2p/base/udpport.h" 22 #include "webrtc/p2p/base/udpport.h"
23 #include "webrtc/base/asynctcpsocket.h" 23 #include "webrtc/base/asynctcpsocket.h"
24 #include "webrtc/base/buffer.h" 24 #include "webrtc/base/buffer.h"
25 #include "webrtc/base/dscp.h" 25 #include "webrtc/base/dscp.h"
26 #include "webrtc/base/fakeclock.h"
26 #include "webrtc/base/firewallsocketserver.h" 27 #include "webrtc/base/firewallsocketserver.h"
27 #include "webrtc/base/gunit.h" 28 #include "webrtc/base/gunit.h"
28 #include "webrtc/base/helpers.h" 29 #include "webrtc/base/helpers.h"
29 #include "webrtc/base/logging.h" 30 #include "webrtc/base/logging.h"
30 #include "webrtc/base/physicalsocketserver.h" 31 #include "webrtc/base/physicalsocketserver.h"
31 #include "webrtc/base/socketaddress.h" 32 #include "webrtc/base/socketaddress.h"
32 #include "webrtc/base/ssladapter.h" 33 #include "webrtc/base/ssladapter.h"
33 #include "webrtc/base/thread.h" 34 #include "webrtc/base/thread.h"
34 #include "webrtc/base/virtualsocketserver.h" 35 #include "webrtc/base/virtualsocketserver.h"
35 36
36 using rtc::SocketAddress; 37 using rtc::SocketAddress;
37 using cricket::Connection;
38 using cricket::Port;
39 using cricket::PortInterface;
40 using cricket::TurnPort;
41 using cricket::UDPPort;
42 38
43 static const SocketAddress kLocalAddr1("11.11.11.11", 0); 39 static const SocketAddress kLocalAddr1("11.11.11.11", 0);
44 static const SocketAddress kLocalAddr2("22.22.22.22", 0); 40 static const SocketAddress kLocalAddr2("22.22.22.22", 0);
45 static const SocketAddress kLocalIPv6Addr( 41 static const SocketAddress kLocalIPv6Addr(
46 "2401:fa00:4:1000:be30:5bff:fee5:c3", 0); 42 "2401:fa00:4:1000:be30:5bff:fee5:c3", 0);
47 static const SocketAddress kLocalIPv6Addr2( 43 static const SocketAddress kLocalIPv6Addr2(
48 "2401:fa00:4:2000:be30:5bff:fee5:d4", 0); 44 "2401:fa00:4:2000:be30:5bff:fee5:d4", 0);
49 static const SocketAddress kTurnUdpIntAddr("99.99.99.3", 45 static const SocketAddress kTurnUdpIntAddr("99.99.99.3",
50 cricket::TURN_SERVER_PORT); 46 cricket::TURN_SERVER_PORT);
51 static const SocketAddress kTurnTcpIntAddr("99.99.99.4", 47 static const SocketAddress kTurnTcpIntAddr("99.99.99.4",
(...skipping 10 matching lines...) Expand all
62 "2400:4030:1:2c00:be30:abcd:efab:cdef", cricket::TURN_SERVER_PORT); 58 "2400:4030:1:2c00:be30:abcd:efab:cdef", cricket::TURN_SERVER_PORT);
63 59
64 static const char kCandidateFoundation[] = "foundation"; 60 static const char kCandidateFoundation[] = "foundation";
65 static const char kIceUfrag1[] = "TESTICEUFRAG0001"; 61 static const char kIceUfrag1[] = "TESTICEUFRAG0001";
66 static const char kIceUfrag2[] = "TESTICEUFRAG0002"; 62 static const char kIceUfrag2[] = "TESTICEUFRAG0002";
67 static const char kIcePwd1[] = "TESTICEPWD00000000000001"; 63 static const char kIcePwd1[] = "TESTICEPWD00000000000001";
68 static const char kIcePwd2[] = "TESTICEPWD00000000000002"; 64 static const char kIcePwd2[] = "TESTICEPWD00000000000002";
69 static const char kTurnUsername[] = "test"; 65 static const char kTurnUsername[] = "test";
70 static const char kTurnPassword[] = "test"; 66 static const char kTurnPassword[] = "test";
71 static const char kTestOrigin[] = "http://example.com"; 67 static const char kTestOrigin[] = "http://example.com";
72 static const unsigned int kTimeout = 1000; 68 // This test configures the virtual socket server to simulate delay so that we
69 // can verify operations take no more than the expected number of round trips.
70 static constexpr unsigned int kSimulatedRtt = 50;
Taylor Brandstetter 2016/06/24 16:33:40 This might be overkill, but I just thought "if we'
71 // Connection destruction may happen asynchronously, but it should only
72 // take one simulated clock tick.
73 static constexpr unsigned int kConnectionDestructionDelay = 1;
74 // This used to be 1 second, but that's not always enough for getaddrinfo().
75 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5191
76 static constexpr unsigned int kResolverTimeout = 10000;
73 77
74 static const cricket::ProtocolAddress kTurnUdpProtoAddr( 78 static const cricket::ProtocolAddress kTurnUdpProtoAddr(
75 kTurnUdpIntAddr, cricket::PROTO_UDP); 79 kTurnUdpIntAddr, cricket::PROTO_UDP);
76 static const cricket::ProtocolAddress kTurnTcpProtoAddr( 80 static const cricket::ProtocolAddress kTurnTcpProtoAddr(
77 kTurnTcpIntAddr, cricket::PROTO_TCP); 81 kTurnTcpIntAddr, cricket::PROTO_TCP);
78 static const cricket::ProtocolAddress kTurnUdpIPv6ProtoAddr( 82 static const cricket::ProtocolAddress kTurnUdpIPv6ProtoAddr(
79 kTurnUdpIPv6IntAddr, cricket::PROTO_UDP); 83 kTurnUdpIPv6IntAddr, cricket::PROTO_UDP);
80 84
81 static const unsigned int MSG_TESTFINISH = 0; 85 static const unsigned int MSG_TESTFINISH = 0;
82 86
83 #if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID) 87 #if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)
84 static int GetFDCount() { 88 static int GetFDCount() {
85 struct dirent *dp; 89 struct dirent *dp;
86 int fd_count = 0; 90 int fd_count = 0;
87 DIR *dir = opendir("/proc/self/fd/"); 91 DIR *dir = opendir("/proc/self/fd/");
88 while ((dp = readdir(dir)) != NULL) { 92 while ((dp = readdir(dir)) != NULL) {
89 if (dp->d_name[0] == '.') 93 if (dp->d_name[0] == '.')
90 continue; 94 continue;
91 ++fd_count; 95 ++fd_count;
92 } 96 }
93 closedir(dir); 97 closedir(dir);
94 return fd_count; 98 return fd_count;
95 } 99 }
96 #endif 100 #endif
97 101
102 namespace cricket {
103
98 class TurnPortTestVirtualSocketServer : public rtc::VirtualSocketServer { 104 class TurnPortTestVirtualSocketServer : public rtc::VirtualSocketServer {
99 public: 105 public:
100 explicit TurnPortTestVirtualSocketServer(SocketServer* ss) 106 explicit TurnPortTestVirtualSocketServer(SocketServer* ss)
101 : VirtualSocketServer(ss) {} 107 : VirtualSocketServer(ss) {
108 // This configures the virtual socket server to always add a simulated
109 // delay of exactly half of kSimulatedRtt.
110 set_delay_mean(kSimulatedRtt / 2);
111 UpdateDelayDistribution();
112 }
102 113
103 using rtc::VirtualSocketServer::LookupBinding; 114 using rtc::VirtualSocketServer::LookupBinding;
104 }; 115 };
105 116
106 class TestConnectionWrapper : public sigslot::has_slots<> { 117 class TestConnectionWrapper : public sigslot::has_slots<> {
107 public: 118 public:
108 TestConnectionWrapper(Connection* conn) : connection_(conn) { 119 TestConnectionWrapper(Connection* conn) : connection_(conn) {
109 conn->SignalDestroyed.connect( 120 conn->SignalDestroyed.connect(
110 this, &TestConnectionWrapper::OnConnectionDestroyed); 121 this, &TestConnectionWrapper::OnConnectionDestroyed);
111 } 122 }
112 123
113 Connection* connection() { return connection_; } 124 Connection* connection() { return connection_; }
114 125
115 private: 126 private:
116 void OnConnectionDestroyed(Connection* conn) { 127 void OnConnectionDestroyed(Connection* conn) {
117 ASSERT_TRUE(conn == connection_); 128 ASSERT_TRUE(conn == connection_);
118 connection_ = nullptr; 129 connection_ = nullptr;
119 } 130 }
120 131
121 Connection* connection_; 132 Connection* connection_;
122 }; 133 };
123 134
135 // Note: This test uses a fake clock.
124 class TurnPortTest : public testing::Test, 136 class TurnPortTest : public testing::Test,
125 public sigslot::has_slots<>, 137 public sigslot::has_slots<>,
126 public rtc::MessageHandler { 138 public rtc::MessageHandler {
127 public: 139 public:
128 TurnPortTest() 140 TurnPortTest()
129 : main_(rtc::Thread::Current()), 141 : main_(rtc::Thread::Current()),
130 pss_(new rtc::PhysicalSocketServer), 142 pss_(new rtc::PhysicalSocketServer),
131 ss_(new TurnPortTestVirtualSocketServer(pss_.get())), 143 ss_(new TurnPortTestVirtualSocketServer(pss_.get())),
132 ss_scope_(ss_.get()), 144 ss_scope_(ss_.get()),
133 network_("unittest", "unittest", rtc::IPAddress(INADDR_ANY), 32), 145 network_("unittest", "unittest", rtc::IPAddress(INADDR_ANY), 32),
134 socket_factory_(rtc::Thread::Current()), 146 socket_factory_(rtc::Thread::Current()),
135 turn_server_(main_, kTurnUdpIntAddr, kTurnUdpExtAddr), 147 turn_server_(main_, kTurnUdpIntAddr, kTurnUdpExtAddr),
136 turn_ready_(false), 148 turn_ready_(false),
137 turn_error_(false), 149 turn_error_(false),
138 turn_unknown_address_(false), 150 turn_unknown_address_(false),
139 turn_create_permission_success_(false), 151 turn_create_permission_success_(false),
140 udp_ready_(false), 152 udp_ready_(false),
141 test_finish_(false) { 153 test_finish_(false) {
154 // Some code uses "last received time == 0" to represent "nothing received
155 // so far", so we need to start the fake clock at a nonzero time...
156 // TODO(deadbeef): Fix this.
157 fake_clock_.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
142 network_.AddIP(rtc::IPAddress(INADDR_ANY)); 158 network_.AddIP(rtc::IPAddress(INADDR_ANY));
143 } 159 }
144 160
145 virtual void OnMessage(rtc::Message* msg) { 161 virtual void OnMessage(rtc::Message* msg) {
146 ASSERT(msg->message_id == MSG_TESTFINISH); 162 ASSERT(msg->message_id == MSG_TESTFINISH);
147 if (msg->message_id == MSG_TESTFINISH) 163 if (msg->message_id == MSG_TESTFINISH)
148 test_finish_ = true; 164 test_finish_ = true;
149 } 165 }
150 166
151 void ConnectSignalAddressReadyToSetLocalhostAsAltenertativeLocalAddress() { 167 void ConnectSignalAddressReadyToSetLocalhostAsAltenertativeLocalAddress() {
(...skipping 10 matching lines...) Expand all
162 SocketAddress local_address("127.0.0.1", 2000); 178 SocketAddress local_address("127.0.0.1", 2000);
163 socket->SetAlternativeLocalAddress(local_address); 179 socket->SetAlternativeLocalAddress(local_address);
164 } 180 }
165 181
166 void OnTurnPortComplete(Port* port) { 182 void OnTurnPortComplete(Port* port) {
167 turn_ready_ = true; 183 turn_ready_ = true;
168 } 184 }
169 void OnTurnPortError(Port* port) { 185 void OnTurnPortError(Port* port) {
170 turn_error_ = true; 186 turn_error_ = true;
171 } 187 }
172 void OnTurnUnknownAddress(PortInterface* port, const SocketAddress& addr, 188 void OnTurnUnknownAddress(PortInterface* port,
173 cricket::ProtocolType proto, 189 const SocketAddress& addr,
174 cricket::IceMessage* msg, const std::string& rf, 190 ProtocolType proto,
191 IceMessage* msg,
192 const std::string& rf,
175 bool /*port_muxed*/) { 193 bool /*port_muxed*/) {
176 turn_unknown_address_ = true; 194 turn_unknown_address_ = true;
177 } 195 }
178 void OnTurnCreatePermissionResult(TurnPort* port, 196 void OnTurnCreatePermissionResult(TurnPort* port,
179 const SocketAddress& addr, 197 const SocketAddress& addr,
180 int code) { 198 int code) {
181 // Ignoring the address. 199 // Ignoring the address.
182 turn_create_permission_success_ = (code == 0); 200 turn_create_permission_success_ = (code == 0);
183 } 201 }
184 202
(...skipping 20 matching lines...) Expand all
205 } 223 }
206 rtc::AsyncSocket* CreateServerSocket(const SocketAddress addr) { 224 rtc::AsyncSocket* CreateServerSocket(const SocketAddress addr) {
207 rtc::AsyncSocket* socket = ss_->CreateAsyncSocket(SOCK_STREAM); 225 rtc::AsyncSocket* socket = ss_->CreateAsyncSocket(SOCK_STREAM);
208 EXPECT_GE(socket->Bind(addr), 0); 226 EXPECT_GE(socket->Bind(addr), 0);
209 EXPECT_GE(socket->Listen(5), 0); 227 EXPECT_GE(socket->Listen(5), 0);
210 return socket; 228 return socket;
211 } 229 }
212 230
213 void CreateTurnPort(const std::string& username, 231 void CreateTurnPort(const std::string& username,
214 const std::string& password, 232 const std::string& password,
215 const cricket::ProtocolAddress& server_address) { 233 const ProtocolAddress& server_address) {
216 CreateTurnPort(kLocalAddr1, username, password, server_address); 234 CreateTurnPort(kLocalAddr1, username, password, server_address);
217 } 235 }
218 void CreateTurnPort(const rtc::SocketAddress& local_address, 236 void CreateTurnPort(const rtc::SocketAddress& local_address,
219 const std::string& username, 237 const std::string& username,
220 const std::string& password, 238 const std::string& password,
221 const cricket::ProtocolAddress& server_address) { 239 const ProtocolAddress& server_address) {
222 cricket::RelayCredentials credentials(username, password); 240 RelayCredentials credentials(username, password);
223 turn_port_.reset(TurnPort::Create(main_, &socket_factory_, &network_, 241 turn_port_.reset(TurnPort::Create(main_, &socket_factory_, &network_,
224 local_address.ipaddr(), 0, 0, 242 local_address.ipaddr(), 0, 0,
225 kIceUfrag1, kIcePwd1, 243 kIceUfrag1, kIcePwd1,
226 server_address, credentials, 0, 244 server_address, credentials, 0,
227 std::string())); 245 std::string()));
228 // This TURN port will be the controlling. 246 // This TURN port will be the controlling.
229 turn_port_->SetIceRole(cricket::ICEROLE_CONTROLLING); 247 turn_port_->SetIceRole(ICEROLE_CONTROLLING);
230 ConnectSignals(); 248 ConnectSignals();
231 } 249 }
232 250
233 // Should be identical to CreateTurnPort but specifies an origin value 251 // Should be identical to CreateTurnPort but specifies an origin value
234 // when creating the instance of TurnPort. 252 // when creating the instance of TurnPort.
235 void CreateTurnPortWithOrigin(const rtc::SocketAddress& local_address, 253 void CreateTurnPortWithOrigin(const rtc::SocketAddress& local_address,
236 const std::string& username, 254 const std::string& username,
237 const std::string& password, 255 const std::string& password,
238 const cricket::ProtocolAddress& server_address, 256 const ProtocolAddress& server_address,
239 const std::string& origin) { 257 const std::string& origin) {
240 cricket::RelayCredentials credentials(username, password); 258 RelayCredentials credentials(username, password);
241 turn_port_.reset(TurnPort::Create(main_, &socket_factory_, &network_, 259 turn_port_.reset(TurnPort::Create(main_, &socket_factory_, &network_,
242 local_address.ipaddr(), 0, 0, 260 local_address.ipaddr(), 0, 0,
243 kIceUfrag1, kIcePwd1, 261 kIceUfrag1, kIcePwd1,
244 server_address, credentials, 0, 262 server_address, credentials, 0,
245 origin)); 263 origin));
246 // This TURN port will be the controlling. 264 // This TURN port will be the controlling.
247 turn_port_->SetIceRole(cricket::ICEROLE_CONTROLLING); 265 turn_port_->SetIceRole(ICEROLE_CONTROLLING);
248 ConnectSignals(); 266 ConnectSignals();
249 } 267 }
250 268
251 void CreateSharedTurnPort(const std::string& username, 269 void CreateSharedTurnPort(const std::string& username,
252 const std::string& password, 270 const std::string& password,
253 const cricket::ProtocolAddress& server_address) { 271 const ProtocolAddress& server_address) {
254 ASSERT(server_address.proto == cricket::PROTO_UDP); 272 ASSERT(server_address.proto == PROTO_UDP);
255 273
256 if (!socket_) { 274 if (!socket_) {
257 socket_.reset(socket_factory_.CreateUdpSocket( 275 socket_.reset(socket_factory_.CreateUdpSocket(
258 rtc::SocketAddress(kLocalAddr1.ipaddr(), 0), 0, 0)); 276 rtc::SocketAddress(kLocalAddr1.ipaddr(), 0), 0, 0));
259 ASSERT_TRUE(socket_ != NULL); 277 ASSERT_TRUE(socket_ != NULL);
260 socket_->SignalReadPacket.connect( 278 socket_->SignalReadPacket.connect(
261 this, &TurnPortTest::OnSocketReadPacket); 279 this, &TurnPortTest::OnSocketReadPacket);
262 } 280 }
263 281
264 cricket::RelayCredentials credentials(username, password); 282 RelayCredentials credentials(username, password);
265 turn_port_.reset(cricket::TurnPort::Create( 283 turn_port_.reset(TurnPort::Create(
266 main_, &socket_factory_, &network_, socket_.get(), 284 main_, &socket_factory_, &network_, socket_.get(), kIceUfrag1, kIcePwd1,
267 kIceUfrag1, kIcePwd1, server_address, credentials, 0, std::string())); 285 server_address, credentials, 0, std::string()));
268 // This TURN port will be the controlling. 286 // This TURN port will be the controlling.
269 turn_port_->SetIceRole(cricket::ICEROLE_CONTROLLING); 287 turn_port_->SetIceRole(ICEROLE_CONTROLLING);
270 ConnectSignals(); 288 ConnectSignals();
271 } 289 }
272 290
273 void ConnectSignals() { 291 void ConnectSignals() {
274 turn_port_->SignalPortComplete.connect(this, 292 turn_port_->SignalPortComplete.connect(this,
275 &TurnPortTest::OnTurnPortComplete); 293 &TurnPortTest::OnTurnPortComplete);
276 turn_port_->SignalPortError.connect(this, 294 turn_port_->SignalPortError.connect(this,
277 &TurnPortTest::OnTurnPortError); 295 &TurnPortTest::OnTurnPortError);
278 turn_port_->SignalUnknownAddress.connect(this, 296 turn_port_->SignalUnknownAddress.connect(this,
279 &TurnPortTest::OnTurnUnknownAddress); 297 &TurnPortTest::OnTurnUnknownAddress);
280 turn_port_->SignalCreatePermissionResult.connect(this, 298 turn_port_->SignalCreatePermissionResult.connect(this,
281 &TurnPortTest::OnTurnCreatePermissionResult); 299 &TurnPortTest::OnTurnCreatePermissionResult);
282 turn_port_->SignalTurnRefreshResult.connect( 300 turn_port_->SignalTurnRefreshResult.connect(
283 this, &TurnPortTest::OnTurnRefreshResult); 301 this, &TurnPortTest::OnTurnRefreshResult);
284 } 302 }
285 303
286 void CreateUdpPort() { CreateUdpPort(kLocalAddr2); } 304 void CreateUdpPort() { CreateUdpPort(kLocalAddr2); }
287 305
288 void CreateUdpPort(const SocketAddress& address) { 306 void CreateUdpPort(const SocketAddress& address) {
289 udp_port_.reset(UDPPort::Create(main_, &socket_factory_, &network_, 307 udp_port_.reset(UDPPort::Create(main_, &socket_factory_, &network_,
290 address.ipaddr(), 0, 0, kIceUfrag2, 308 address.ipaddr(), 0, 0, kIceUfrag2,
291 kIcePwd2, std::string(), false)); 309 kIcePwd2, std::string(), false));
292 // UDP port will be controlled. 310 // UDP port will be controlled.
293 udp_port_->SetIceRole(cricket::ICEROLE_CONTROLLED); 311 udp_port_->SetIceRole(ICEROLE_CONTROLLED);
294 udp_port_->SignalPortComplete.connect( 312 udp_port_->SignalPortComplete.connect(
295 this, &TurnPortTest::OnUdpPortComplete); 313 this, &TurnPortTest::OnUdpPortComplete);
296 } 314 }
297 315
298 void PrepareTurnAndUdpPorts() { 316 void PrepareTurnAndUdpPorts(ProtocolType protocol_type) {
299 // turn_port_ should have been created. 317 // turn_port_ should have been created.
300 ASSERT_TRUE(turn_port_ != nullptr); 318 ASSERT_TRUE(turn_port_ != nullptr);
301 turn_port_->PrepareAddress(); 319 turn_port_->PrepareAddress();
302 ASSERT_TRUE_WAIT(turn_ready_, kTimeout); 320 // Two round trips are required to allocate a TURN candidate.
321 // Plus, an extra round trip is needed for TCP.
322 ASSERT_TRUE_SIMULATED_WAIT(
323 turn_ready_,
324 protocol_type == PROTO_TCP ? kSimulatedRtt * 3 : kSimulatedRtt * 2,
325 fake_clock_);
303 326
304 CreateUdpPort(); 327 CreateUdpPort();
305 udp_port_->PrepareAddress(); 328 udp_port_->PrepareAddress();
306 ASSERT_TRUE_WAIT(udp_ready_, kTimeout); 329 ASSERT_TRUE_SIMULATED_WAIT(udp_ready_, kSimulatedRtt, fake_clock_);
307 } 330 }
308 331
309 bool CheckConnectionFailedAndPruned(Connection* conn) { 332 bool CheckConnectionFailedAndPruned(Connection* conn) {
310 return conn && !conn->active() && conn->state() == Connection::STATE_FAILED; 333 return conn && !conn->active() && conn->state() == Connection::STATE_FAILED;
311 } 334 }
312 335
313 // Checks that |turn_port_| has a nonempty set of connections and they are all 336 // Checks that |turn_port_| has a nonempty set of connections and they are all
314 // failed and pruned. 337 // failed and pruned.
315 bool CheckAllConnectionsFailedAndPruned() { 338 bool CheckAllConnectionsFailedAndPruned() {
316 auto& connections = turn_port_->connections(); 339 auto& connections = turn_port_->connections();
317 if (connections.empty()) { 340 if (connections.empty()) {
318 return false; 341 return false;
319 } 342 }
320 for (auto kv : connections) { 343 for (auto kv : connections) {
321 if (!CheckConnectionFailedAndPruned(kv.second)) { 344 if (!CheckConnectionFailedAndPruned(kv.second)) {
322 return false; 345 return false;
323 } 346 }
324 } 347 }
325 return true; 348 return true;
326 } 349 }
327 350
328 void TestTurnAlternateServer(cricket::ProtocolType protocol_type) { 351 void TestTurnAlternateServer(ProtocolType protocol_type) {
329 std::vector<rtc::SocketAddress> redirect_addresses; 352 std::vector<rtc::SocketAddress> redirect_addresses;
330 redirect_addresses.push_back(kTurnAlternateIntAddr); 353 redirect_addresses.push_back(kTurnAlternateIntAddr);
331 354
332 cricket::TestTurnRedirector redirector(redirect_addresses); 355 TestTurnRedirector redirector(redirect_addresses);
333 356
334 turn_server_.AddInternalSocket(kTurnIntAddr, protocol_type); 357 turn_server_.AddInternalSocket(kTurnIntAddr, protocol_type);
335 turn_server_.AddInternalSocket(kTurnAlternateIntAddr, protocol_type); 358 turn_server_.AddInternalSocket(kTurnAlternateIntAddr, protocol_type);
336 turn_server_.set_redirect_hook(&redirector); 359 turn_server_.set_redirect_hook(&redirector);
337 CreateTurnPort(kTurnUsername, kTurnPassword, 360 CreateTurnPort(kTurnUsername, kTurnPassword,
338 cricket::ProtocolAddress(kTurnIntAddr, protocol_type)); 361 ProtocolAddress(kTurnIntAddr, protocol_type));
339 362
340 // Retrieve the address before we run the state machine. 363 // Retrieve the address before we run the state machine.
341 const SocketAddress old_addr = turn_port_->server_address().address; 364 const SocketAddress old_addr = turn_port_->server_address().address;
342 365
343 turn_port_->PrepareAddress(); 366 turn_port_->PrepareAddress();
344 EXPECT_TRUE_WAIT(turn_ready_, kTimeout * 100); 367 // Extra round trip due to "use alternate server" error response.
368 EXPECT_TRUE_SIMULATED_WAIT(
369 turn_ready_,
370 (protocol_type == PROTO_TCP ? kSimulatedRtt * 4 : kSimulatedRtt * 3),
371 fake_clock_);
345 // Retrieve the address again, the turn port's address should be 372 // Retrieve the address again, the turn port's address should be
346 // changed. 373 // changed.
347 const SocketAddress new_addr = turn_port_->server_address().address; 374 const SocketAddress new_addr = turn_port_->server_address().address;
348 EXPECT_NE(old_addr, new_addr); 375 EXPECT_NE(old_addr, new_addr);
349 ASSERT_EQ(1U, turn_port_->Candidates().size()); 376 ASSERT_EQ(1U, turn_port_->Candidates().size());
350 EXPECT_EQ(kTurnUdpExtAddr.ipaddr(), 377 EXPECT_EQ(kTurnUdpExtAddr.ipaddr(),
351 turn_port_->Candidates()[0].address().ipaddr()); 378 turn_port_->Candidates()[0].address().ipaddr());
352 EXPECT_NE(0, turn_port_->Candidates()[0].address().port()); 379 EXPECT_NE(0, turn_port_->Candidates()[0].address().port());
353 } 380 }
354 381
355 void TestTurnAlternateServerV4toV6(cricket::ProtocolType protocol_type) { 382 void TestTurnAlternateServerV4toV6(ProtocolType protocol_type) {
356 std::vector<rtc::SocketAddress> redirect_addresses; 383 std::vector<rtc::SocketAddress> redirect_addresses;
357 redirect_addresses.push_back(kTurnIPv6IntAddr); 384 redirect_addresses.push_back(kTurnIPv6IntAddr);
358 385
359 cricket::TestTurnRedirector redirector(redirect_addresses); 386 TestTurnRedirector redirector(redirect_addresses);
360 turn_server_.AddInternalSocket(kTurnIntAddr, protocol_type); 387 turn_server_.AddInternalSocket(kTurnIntAddr, protocol_type);
361 turn_server_.set_redirect_hook(&redirector); 388 turn_server_.set_redirect_hook(&redirector);
362 CreateTurnPort(kTurnUsername, kTurnPassword, 389 CreateTurnPort(kTurnUsername, kTurnPassword,
363 cricket::ProtocolAddress(kTurnIntAddr, protocol_type)); 390 ProtocolAddress(kTurnIntAddr, protocol_type));
364 turn_port_->PrepareAddress(); 391 turn_port_->PrepareAddress();
365 EXPECT_TRUE_WAIT(turn_error_, kTimeout); 392 EXPECT_TRUE_SIMULATED_WAIT(turn_error_, kSimulatedRtt * 2, fake_clock_);
366 } 393 }
367 394
368 void TestTurnAlternateServerPingPong(cricket::ProtocolType protocol_type) { 395 void TestTurnAlternateServerPingPong(ProtocolType protocol_type) {
369 std::vector<rtc::SocketAddress> redirect_addresses; 396 std::vector<rtc::SocketAddress> redirect_addresses;
370 redirect_addresses.push_back(kTurnAlternateIntAddr); 397 redirect_addresses.push_back(kTurnAlternateIntAddr);
371 redirect_addresses.push_back(kTurnIntAddr); 398 redirect_addresses.push_back(kTurnIntAddr);
372 399
373 cricket::TestTurnRedirector redirector(redirect_addresses); 400 TestTurnRedirector redirector(redirect_addresses);
374 401
375 turn_server_.AddInternalSocket(kTurnIntAddr, protocol_type); 402 turn_server_.AddInternalSocket(kTurnIntAddr, protocol_type);
376 turn_server_.AddInternalSocket(kTurnAlternateIntAddr, protocol_type); 403 turn_server_.AddInternalSocket(kTurnAlternateIntAddr, protocol_type);
377 turn_server_.set_redirect_hook(&redirector); 404 turn_server_.set_redirect_hook(&redirector);
378 CreateTurnPort(kTurnUsername, kTurnPassword, 405 CreateTurnPort(kTurnUsername, kTurnPassword,
379 cricket::ProtocolAddress(kTurnIntAddr, protocol_type)); 406 ProtocolAddress(kTurnIntAddr, protocol_type));
380 407
381 turn_port_->PrepareAddress(); 408 turn_port_->PrepareAddress();
382 EXPECT_TRUE_WAIT(turn_error_, kTimeout); 409 // Extra round trip due to "use alternate server" error response.
410 EXPECT_TRUE_SIMULATED_WAIT(
411 turn_error_,
412 (protocol_type == PROTO_TCP ? kSimulatedRtt * 4 : kSimulatedRtt * 3),
413 fake_clock_);
383 ASSERT_EQ(0U, turn_port_->Candidates().size()); 414 ASSERT_EQ(0U, turn_port_->Candidates().size());
384 rtc::SocketAddress address; 415 rtc::SocketAddress address;
385 // Verify that we have exhausted all alternate servers instead of 416 // Verify that we have exhausted all alternate servers instead of
386 // failure caused by other errors. 417 // failure caused by other errors.
387 EXPECT_FALSE(redirector.ShouldRedirect(address, &address)); 418 EXPECT_FALSE(redirector.ShouldRedirect(address, &address));
388 } 419 }
389 420
390 void TestTurnAlternateServerDetectRepetition( 421 void TestTurnAlternateServerDetectRepetition(ProtocolType protocol_type) {
391 cricket::ProtocolType protocol_type) {
392 std::vector<rtc::SocketAddress> redirect_addresses; 422 std::vector<rtc::SocketAddress> redirect_addresses;
393 redirect_addresses.push_back(kTurnAlternateIntAddr); 423 redirect_addresses.push_back(kTurnAlternateIntAddr);
394 redirect_addresses.push_back(kTurnAlternateIntAddr); 424 redirect_addresses.push_back(kTurnAlternateIntAddr);
395 425
396 cricket::TestTurnRedirector redirector(redirect_addresses); 426 TestTurnRedirector redirector(redirect_addresses);
397 427
398 turn_server_.AddInternalSocket(kTurnIntAddr, protocol_type); 428 turn_server_.AddInternalSocket(kTurnIntAddr, protocol_type);
399 turn_server_.AddInternalSocket(kTurnAlternateIntAddr, protocol_type); 429 turn_server_.AddInternalSocket(kTurnAlternateIntAddr, protocol_type);
400 turn_server_.set_redirect_hook(&redirector); 430 turn_server_.set_redirect_hook(&redirector);
401 CreateTurnPort(kTurnUsername, kTurnPassword, 431 CreateTurnPort(kTurnUsername, kTurnPassword,
402 cricket::ProtocolAddress(kTurnIntAddr, protocol_type)); 432 ProtocolAddress(kTurnIntAddr, protocol_type));
403 433
404 turn_port_->PrepareAddress(); 434 turn_port_->PrepareAddress();
405 EXPECT_TRUE_WAIT(turn_error_, kTimeout); 435 // Extra round trip due to "use alternate server" error response.
436 EXPECT_TRUE_SIMULATED_WAIT(
437 turn_error_,
438 (protocol_type == PROTO_TCP ? kSimulatedRtt * 4 : kSimulatedRtt * 3),
439 fake_clock_);
406 ASSERT_EQ(0U, turn_port_->Candidates().size()); 440 ASSERT_EQ(0U, turn_port_->Candidates().size());
407 } 441 }
408 442
409 void TestTurnConnection() { 443 void TestTurnConnection(ProtocolType protocol_type) {
410 // Create ports and prepare addresses. 444 // Create ports and prepare addresses.
411 PrepareTurnAndUdpPorts(); 445 PrepareTurnAndUdpPorts(protocol_type);
412 446
413 // Send ping from UDP to TURN. 447 // Send ping from UDP to TURN.
414 Connection* conn1 = udp_port_->CreateConnection( 448 Connection* conn1 = udp_port_->CreateConnection(
415 turn_port_->Candidates()[0], Port::ORIGIN_MESSAGE); 449 turn_port_->Candidates()[0], Port::ORIGIN_MESSAGE);
416 ASSERT_TRUE(conn1 != NULL); 450 ASSERT_TRUE(conn1 != NULL);
417 conn1->Ping(0); 451 conn1->Ping(0);
418 WAIT(!turn_unknown_address_, kTimeout); 452 SIMULATED_WAIT(!turn_unknown_address_, kSimulatedRtt * 2, fake_clock_);
419 EXPECT_FALSE(turn_unknown_address_); 453 EXPECT_FALSE(turn_unknown_address_);
420 EXPECT_FALSE(conn1->receiving()); 454 EXPECT_FALSE(conn1->receiving());
421 EXPECT_EQ(Connection::STATE_WRITE_INIT, conn1->write_state()); 455 EXPECT_EQ(Connection::STATE_WRITE_INIT, conn1->write_state());
422 456
423 // Send ping from TURN to UDP. 457 // Send ping from TURN to UDP.
424 Connection* conn2 = turn_port_->CreateConnection( 458 Connection* conn2 = turn_port_->CreateConnection(
425 udp_port_->Candidates()[0], Port::ORIGIN_MESSAGE); 459 udp_port_->Candidates()[0], Port::ORIGIN_MESSAGE);
426 ASSERT_TRUE(conn2 != NULL); 460 ASSERT_TRUE(conn2 != NULL);
427 ASSERT_TRUE_WAIT(turn_create_permission_success_, kTimeout); 461 ASSERT_TRUE_SIMULATED_WAIT(turn_create_permission_success_, kSimulatedRtt,
462 fake_clock_);
428 conn2->Ping(0); 463 conn2->Ping(0);
429 464
430 EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, conn2->write_state(), kTimeout); 465 // Two hops from TURN port to UDP port through TURN server, thus two RTTs.
466 EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE, conn2->write_state(),
467 kSimulatedRtt * 2, fake_clock_);
431 EXPECT_TRUE(conn1->receiving()); 468 EXPECT_TRUE(conn1->receiving());
432 EXPECT_TRUE(conn2->receiving()); 469 EXPECT_TRUE(conn2->receiving());
433 EXPECT_EQ(Connection::STATE_WRITE_INIT, conn1->write_state()); 470 EXPECT_EQ(Connection::STATE_WRITE_INIT, conn1->write_state());
434 471
435 // Send another ping from UDP to TURN. 472 // Send another ping from UDP to TURN.
436 conn1->Ping(0); 473 conn1->Ping(0);
437 EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, conn1->write_state(), kTimeout); 474 EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE, conn1->write_state(),
475 kSimulatedRtt * 2, fake_clock_);
438 EXPECT_TRUE(conn2->receiving()); 476 EXPECT_TRUE(conn2->receiving());
439 } 477 }
440 478
441 void TestDestroyTurnConnection() { 479 void TestDestroyTurnConnection() {
442 PrepareTurnAndUdpPorts(); 480 PrepareTurnAndUdpPorts(PROTO_UDP);
443 481
444 // Create connections on both ends. 482 // Create connections on both ends.
445 Connection* conn1 = udp_port_->CreateConnection(turn_port_->Candidates()[0], 483 Connection* conn1 = udp_port_->CreateConnection(turn_port_->Candidates()[0],
446 Port::ORIGIN_MESSAGE); 484 Port::ORIGIN_MESSAGE);
447 Connection* conn2 = turn_port_->CreateConnection(udp_port_->Candidates()[0], 485 Connection* conn2 = turn_port_->CreateConnection(udp_port_->Candidates()[0],
448 Port::ORIGIN_MESSAGE); 486 Port::ORIGIN_MESSAGE);
449 ASSERT_TRUE(conn2 != NULL); 487 ASSERT_TRUE(conn2 != NULL);
450 ASSERT_TRUE_WAIT(turn_create_permission_success_, kTimeout); 488 ASSERT_TRUE_SIMULATED_WAIT(turn_create_permission_success_, kSimulatedRtt,
489 fake_clock_);
451 // Make sure turn connection can receive. 490 // Make sure turn connection can receive.
452 conn1->Ping(0); 491 conn1->Ping(0);
453 EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, conn1->write_state(), kTimeout); 492 EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE, conn1->write_state(),
493 kSimulatedRtt * 2, fake_clock_);
454 EXPECT_FALSE(turn_unknown_address_); 494 EXPECT_FALSE(turn_unknown_address_);
455 495
456 // Destroy the connection on the turn port. The TurnEntry is still 496 // Destroy the connection on the TURN port. The TurnEntry still exists, so
457 // there. So the turn port gets ping from unknown address if it is pinged. 497 // the TURN port should still process a ping from an unknown address.
458 conn2->Destroy(); 498 conn2->Destroy();
459 conn1->Ping(0); 499 conn1->Ping(0);
460 EXPECT_TRUE_WAIT(turn_unknown_address_, kTimeout); 500 EXPECT_TRUE_SIMULATED_WAIT(turn_unknown_address_, kSimulatedRtt,
501 fake_clock_);
461 502
462 // Flush all requests in the invoker to destroy the TurnEntry. 503 // Flush all requests in the invoker to destroy the TurnEntry.
463 // However, the TURN port should still signal the unknown address. 504 // Expect that it still processes an incoming ping and signals the
505 // unknown address.
464 turn_unknown_address_ = false; 506 turn_unknown_address_ = false;
465 turn_port_->invoker()->Flush(rtc::Thread::Current()); 507 turn_port_->invoker()->Flush(rtc::Thread::Current());
466 conn1->Ping(0); 508 conn1->Ping(0);
467 EXPECT_TRUE_WAIT(turn_unknown_address_, kTimeout); 509 EXPECT_TRUE_SIMULATED_WAIT(turn_unknown_address_, kSimulatedRtt,
510 fake_clock_);
468 511
469 // If the connection is created again, it will start to receive pings. 512 // If the connection is created again, it will start to receive pings.
470 conn2 = turn_port_->CreateConnection(udp_port_->Candidates()[0], 513 conn2 = turn_port_->CreateConnection(udp_port_->Candidates()[0],
471 Port::ORIGIN_MESSAGE); 514 Port::ORIGIN_MESSAGE);
472 conn1->Ping(0); 515 conn1->Ping(0);
473 EXPECT_TRUE_WAIT(conn2->receiving(), kTimeout); 516 EXPECT_TRUE_SIMULATED_WAIT(conn2->receiving(), kSimulatedRtt, fake_clock_);
474 } 517 }
475 518
476 void TestTurnSendData() { 519 void TestTurnSendData(ProtocolType protocol_type) {
477 PrepareTurnAndUdpPorts(); 520 PrepareTurnAndUdpPorts(protocol_type);
478 521
479 // Create connections and send pings. 522 // Create connections and send pings.
480 Connection* conn1 = turn_port_->CreateConnection( 523 Connection* conn1 = turn_port_->CreateConnection(
481 udp_port_->Candidates()[0], Port::ORIGIN_MESSAGE); 524 udp_port_->Candidates()[0], Port::ORIGIN_MESSAGE);
482 Connection* conn2 = udp_port_->CreateConnection( 525 Connection* conn2 = udp_port_->CreateConnection(
483 turn_port_->Candidates()[0], Port::ORIGIN_MESSAGE); 526 turn_port_->Candidates()[0], Port::ORIGIN_MESSAGE);
484 ASSERT_TRUE(conn1 != NULL); 527 ASSERT_TRUE(conn1 != NULL);
485 ASSERT_TRUE(conn2 != NULL); 528 ASSERT_TRUE(conn2 != NULL);
486 conn1->SignalReadPacket.connect(static_cast<TurnPortTest*>(this), 529 conn1->SignalReadPacket.connect(static_cast<TurnPortTest*>(this),
487 &TurnPortTest::OnTurnReadPacket); 530 &TurnPortTest::OnTurnReadPacket);
488 conn2->SignalReadPacket.connect(static_cast<TurnPortTest*>(this), 531 conn2->SignalReadPacket.connect(static_cast<TurnPortTest*>(this),
489 &TurnPortTest::OnUdpReadPacket); 532 &TurnPortTest::OnUdpReadPacket);
490 conn1->Ping(0); 533 conn1->Ping(0);
491 EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, conn1->write_state(), kTimeout); 534 EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE, conn1->write_state(),
535 kSimulatedRtt * 2, fake_clock_);
492 conn2->Ping(0); 536 conn2->Ping(0);
493 EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, conn2->write_state(), kTimeout); 537 EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE, conn2->write_state(),
538 kSimulatedRtt * 2, fake_clock_);
494 539
495 // Send some data. 540 // Send some data.
496 size_t num_packets = 256; 541 size_t num_packets = 256;
497 for (size_t i = 0; i < num_packets; ++i) { 542 for (size_t i = 0; i < num_packets; ++i) {
498 unsigned char buf[256] = { 0 }; 543 unsigned char buf[256] = { 0 };
499 for (size_t j = 0; j < i + 1; ++j) { 544 for (size_t j = 0; j < i + 1; ++j) {
500 buf[j] = 0xFF - static_cast<unsigned char>(j); 545 buf[j] = 0xFF - static_cast<unsigned char>(j);
501 } 546 }
502 conn1->Send(buf, i + 1, options); 547 conn1->Send(buf, i + 1, options);
503 conn2->Send(buf, i + 1, options); 548 conn2->Send(buf, i + 1, options);
504 main_->ProcessMessages(0); 549 SIMULATED_WAIT(false, kSimulatedRtt, fake_clock_);
505 } 550 }
506 551
507 // Check the data. 552 // Check the data.
508 ASSERT_EQ_WAIT(num_packets, turn_packets_.size(), kTimeout); 553 ASSERT_EQ(num_packets, turn_packets_.size());
509 ASSERT_EQ_WAIT(num_packets, udp_packets_.size(), kTimeout); 554 ASSERT_EQ(num_packets, udp_packets_.size());
510 for (size_t i = 0; i < num_packets; ++i) { 555 for (size_t i = 0; i < num_packets; ++i) {
511 EXPECT_EQ(i + 1, turn_packets_[i].size()); 556 EXPECT_EQ(i + 1, turn_packets_[i].size());
512 EXPECT_EQ(i + 1, udp_packets_[i].size()); 557 EXPECT_EQ(i + 1, udp_packets_[i].size());
513 EXPECT_EQ(turn_packets_[i], udp_packets_[i]); 558 EXPECT_EQ(turn_packets_[i], udp_packets_[i]);
514 } 559 }
515 } 560 }
516 561
517 protected: 562 protected:
563 rtc::ScopedFakeClock fake_clock_;
518 rtc::Thread* main_; 564 rtc::Thread* main_;
519 std::unique_ptr<rtc::PhysicalSocketServer> pss_; 565 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
520 std::unique_ptr<TurnPortTestVirtualSocketServer> ss_; 566 std::unique_ptr<TurnPortTestVirtualSocketServer> ss_;
521 rtc::SocketServerScope ss_scope_; 567 rtc::SocketServerScope ss_scope_;
522 rtc::Network network_; 568 rtc::Network network_;
523 rtc::BasicPacketSocketFactory socket_factory_; 569 rtc::BasicPacketSocketFactory socket_factory_;
524 std::unique_ptr<rtc::AsyncPacketSocket> socket_; 570 std::unique_ptr<rtc::AsyncPacketSocket> socket_;
525 cricket::TestTurnServer turn_server_; 571 TestTurnServer turn_server_;
526 std::unique_ptr<TurnPort> turn_port_; 572 std::unique_ptr<TurnPort> turn_port_;
527 std::unique_ptr<UDPPort> udp_port_; 573 std::unique_ptr<UDPPort> udp_port_;
528 bool turn_ready_; 574 bool turn_ready_;
529 bool turn_error_; 575 bool turn_error_;
530 bool turn_unknown_address_; 576 bool turn_unknown_address_;
531 bool turn_create_permission_success_; 577 bool turn_create_permission_success_;
532 bool udp_ready_; 578 bool udp_ready_;
533 bool test_finish_; 579 bool test_finish_;
534 bool turn_refresh_success_ = false; 580 bool turn_refresh_success_ = false;
535 std::vector<rtc::Buffer> turn_packets_; 581 std::vector<rtc::Buffer> turn_packets_;
536 std::vector<rtc::Buffer> udp_packets_; 582 std::vector<rtc::Buffer> udp_packets_;
537 rtc::PacketOptions options; 583 rtc::PacketOptions options;
538 }; 584 };
539 585
540 // Do a normal TURN allocation. 586 // Do a normal TURN allocation.
541 TEST_F(TurnPortTest, TestTurnAllocate) { 587 TEST_F(TurnPortTest, TestTurnAllocate) {
542 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 588 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr);
543 EXPECT_EQ(0, turn_port_->SetOption(rtc::Socket::OPT_SNDBUF, 10*1024)); 589 EXPECT_EQ(0, turn_port_->SetOption(rtc::Socket::OPT_SNDBUF, 10*1024));
544 turn_port_->PrepareAddress(); 590 turn_port_->PrepareAddress();
545 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 591 EXPECT_TRUE_SIMULATED_WAIT(turn_ready_, kSimulatedRtt * 2, fake_clock_);
546 ASSERT_EQ(1U, turn_port_->Candidates().size()); 592 ASSERT_EQ(1U, turn_port_->Candidates().size());
547 EXPECT_EQ(kTurnUdpExtAddr.ipaddr(), 593 EXPECT_EQ(kTurnUdpExtAddr.ipaddr(),
548 turn_port_->Candidates()[0].address().ipaddr()); 594 turn_port_->Candidates()[0].address().ipaddr());
549 EXPECT_NE(0, turn_port_->Candidates()[0].address().port()); 595 EXPECT_NE(0, turn_port_->Candidates()[0].address().port());
550 } 596 }
551 597
552 // Testing a normal UDP allocation using TCP connection. 598 // Testing a normal UDP allocation using TCP connection.
553 TEST_F(TurnPortTest, TestTurnTcpAllocate) { 599 TEST_F(TurnPortTest, TestTurnTcpAllocate) {
554 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP); 600 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
555 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnTcpProtoAddr); 601 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnTcpProtoAddr);
556 EXPECT_EQ(0, turn_port_->SetOption(rtc::Socket::OPT_SNDBUF, 10*1024)); 602 EXPECT_EQ(0, turn_port_->SetOption(rtc::Socket::OPT_SNDBUF, 10*1024));
557 turn_port_->PrepareAddress(); 603 turn_port_->PrepareAddress();
558 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 604 EXPECT_TRUE_SIMULATED_WAIT(turn_ready_, kSimulatedRtt * 3, fake_clock_);
559 ASSERT_EQ(1U, turn_port_->Candidates().size()); 605 ASSERT_EQ(1U, turn_port_->Candidates().size());
560 EXPECT_EQ(kTurnUdpExtAddr.ipaddr(), 606 EXPECT_EQ(kTurnUdpExtAddr.ipaddr(),
561 turn_port_->Candidates()[0].address().ipaddr()); 607 turn_port_->Candidates()[0].address().ipaddr());
562 EXPECT_NE(0, turn_port_->Candidates()[0].address().port()); 608 EXPECT_NE(0, turn_port_->Candidates()[0].address().port());
563 } 609 }
564 610
565 // Test case for WebRTC issue 3927 where a proxy binds to the local host address 611 // Test case for WebRTC issue 3927 where a proxy binds to the local host address
566 // instead the address that TurnPort originally bound to. The candidate pair 612 // instead the address that TurnPort originally bound to. The candidate pair
567 // impacted by this behavior should still be used. 613 // impacted by this behavior should still be used.
568 TEST_F(TurnPortTest, TestTurnTcpAllocationWhenProxyChangesAddressToLocalHost) { 614 TEST_F(TurnPortTest, TestTurnTcpAllocationWhenProxyChangesAddressToLocalHost) {
569 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP); 615 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
570 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnTcpProtoAddr); 616 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnTcpProtoAddr);
571 EXPECT_EQ(0, turn_port_->SetOption(rtc::Socket::OPT_SNDBUF, 10 * 1024)); 617 EXPECT_EQ(0, turn_port_->SetOption(rtc::Socket::OPT_SNDBUF, 10 * 1024));
572 turn_port_->PrepareAddress(); 618 turn_port_->PrepareAddress();
573 ConnectSignalAddressReadyToSetLocalhostAsAltenertativeLocalAddress(); 619 ConnectSignalAddressReadyToSetLocalhostAsAltenertativeLocalAddress();
574 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 620 EXPECT_TRUE_SIMULATED_WAIT(turn_ready_, kSimulatedRtt * 3, fake_clock_);
575 ASSERT_EQ(1U, turn_port_->Candidates().size()); 621 ASSERT_EQ(1U, turn_port_->Candidates().size());
576 EXPECT_EQ(kTurnUdpExtAddr.ipaddr(), 622 EXPECT_EQ(kTurnUdpExtAddr.ipaddr(),
577 turn_port_->Candidates()[0].address().ipaddr()); 623 turn_port_->Candidates()[0].address().ipaddr());
578 EXPECT_NE(0, turn_port_->Candidates()[0].address().port()); 624 EXPECT_NE(0, turn_port_->Candidates()[0].address().port());
579 } 625 }
580 626
581 // Testing turn port will attempt to create TCP socket on address resolution 627 // Testing turn port will attempt to create TCP socket on address resolution
582 // failure. 628 // failure.
583 TEST_F(TurnPortTest, DISABLED_TestTurnTcpOnAddressResolveFailure) { 629 TEST_F(TurnPortTest, TestTurnTcpOnAddressResolveFailure) {
584 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP); 630 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
585 CreateTurnPort(kTurnUsername, kTurnPassword, cricket::ProtocolAddress( 631 CreateTurnPort(kTurnUsername, kTurnPassword,
586 rtc::SocketAddress("www.webrtc-blah-blah.com", 3478), 632 ProtocolAddress(rtc::SocketAddress("www.google.invalid", 3478),
587 cricket::PROTO_TCP)); 633 PROTO_TCP));
588 turn_port_->PrepareAddress(); 634 turn_port_->PrepareAddress();
589 EXPECT_TRUE_WAIT(turn_error_, kTimeout); 635 EXPECT_TRUE_WAIT(turn_error_, kResolverTimeout);
590 // As VSS doesn't provide a DNS resolution, name resolve will fail. TurnPort 636 // As VSS doesn't provide a DNS resolution, name resolve will fail. TurnPort
591 // will proceed in creating a TCP socket which will fail as there is no 637 // will proceed in creating a TCP socket which will fail as there is no
592 // server on the above domain and error will be set to SOCKET_ERROR. 638 // server on the above domain and error will be set to SOCKET_ERROR.
593 EXPECT_EQ(SOCKET_ERROR, turn_port_->error()); 639 EXPECT_EQ(SOCKET_ERROR, turn_port_->error());
594 } 640 }
595 641
596 // In case of UDP on address resolve failure, TurnPort will not create socket 642 // In case of UDP on address resolve failure, TurnPort will not create socket
597 // and return allocate failure. 643 // and return allocate failure.
598 TEST_F(TurnPortTest, DISABLED_TestTurnUdpOnAddressResolveFailure) { 644 TEST_F(TurnPortTest, TestTurnUdpOnAddressResolveFailure) {
599 CreateTurnPort(kTurnUsername, kTurnPassword, cricket::ProtocolAddress( 645 CreateTurnPort(kTurnUsername, kTurnPassword,
600 rtc::SocketAddress("www.webrtc-blah-blah.com", 3478), 646 ProtocolAddress(rtc::SocketAddress("www.google.invalid", 3478),
601 cricket::PROTO_UDP)); 647 PROTO_UDP));
602 turn_port_->PrepareAddress(); 648 turn_port_->PrepareAddress();
603 EXPECT_TRUE_WAIT(turn_error_, kTimeout); 649 EXPECT_TRUE_WAIT(turn_error_, kResolverTimeout);
604 // Error from turn port will not be socket error. 650 // Error from turn port will not be socket error.
605 EXPECT_NE(SOCKET_ERROR, turn_port_->error()); 651 EXPECT_NE(SOCKET_ERROR, turn_port_->error());
606 } 652 }
607 653
608 // Try to do a TURN allocation with an invalid password. 654 // Try to do a TURN allocation with an invalid password.
609 TEST_F(TurnPortTest, TestTurnAllocateBadPassword) { 655 TEST_F(TurnPortTest, TestTurnAllocateBadPassword) {
610 CreateTurnPort(kTurnUsername, "bad", kTurnUdpProtoAddr); 656 CreateTurnPort(kTurnUsername, "bad", kTurnUdpProtoAddr);
611 turn_port_->PrepareAddress(); 657 turn_port_->PrepareAddress();
612 EXPECT_TRUE_WAIT(turn_error_, kTimeout); 658 EXPECT_TRUE_SIMULATED_WAIT(turn_error_, kSimulatedRtt * 2, fake_clock_);
613 ASSERT_EQ(0U, turn_port_->Candidates().size()); 659 ASSERT_EQ(0U, turn_port_->Candidates().size());
614 } 660 }
615 661
616 // Tests that TURN port nonce will be reset when receiving an ALLOCATE MISMATCH 662 // Tests that TURN port nonce will be reset when receiving an ALLOCATE MISMATCH
617 // error. 663 // error.
618 TEST_F(TurnPortTest, TestTurnAllocateNonceResetAfterAllocateMismatch) { 664 TEST_F(TurnPortTest, TestTurnAllocateNonceResetAfterAllocateMismatch) {
619 // Do a normal allocation first. 665 // Do a normal allocation first.
620 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 666 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr);
621 turn_port_->PrepareAddress(); 667 turn_port_->PrepareAddress();
622 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 668 EXPECT_TRUE_SIMULATED_WAIT(turn_ready_, kSimulatedRtt * 2, fake_clock_);
623 rtc::SocketAddress first_addr(turn_port_->socket()->GetLocalAddress()); 669 rtc::SocketAddress first_addr(turn_port_->socket()->GetLocalAddress());
624 // Destroy the turnport while keeping the drop probability to 1 to 670 // Destroy the turnport while keeping the drop probability to 1 to
625 // suppress the release of the allocation at the server. 671 // suppress the release of the allocation at the server.
626 ss_->set_drop_probability(1.0); 672 ss_->set_drop_probability(1.0);
627 turn_port_.reset(); 673 turn_port_.reset();
628 rtc::Thread::Current()->ProcessMessages(0); 674 SIMULATED_WAIT(false, kSimulatedRtt, fake_clock_);
629 ss_->set_drop_probability(0.0); 675 ss_->set_drop_probability(0.0);
630 676
631 // Force the socket server to assign the same port. 677 // Force the socket server to assign the same port.
632 ss_->SetNextPortForTesting(first_addr.port()); 678 ss_->SetNextPortForTesting(first_addr.port());
633 turn_ready_ = false; 679 turn_ready_ = false;
634 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 680 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr);
635 681
636 // It is expected that the turn port will first get a nonce from the server 682 // It is expected that the turn port will first get a nonce from the server
637 // using timestamp |ts_before| but then get an allocate mismatch error and 683 // using timestamp |ts_before| but then get an allocate mismatch error and
638 // receive an even newer nonce based on the system clock. |ts_before| is 684 // receive an even newer nonce based on the system clock. |ts_before| is
639 // chosen so that the two NONCEs generated by the server will be different. 685 // chosen so that the two NONCEs generated by the server will be different.
640 int64_t ts_before = rtc::TimeMillis() - 1; 686 int64_t ts_before = rtc::TimeMillis() - 1;
641 std::string first_nonce = 687 std::string first_nonce =
642 turn_server_.server()->SetTimestampForNextNonce(ts_before); 688 turn_server_.server()->SetTimestampForNextNonce(ts_before);
643 turn_port_->PrepareAddress(); 689 turn_port_->PrepareAddress();
644 690
645 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 691 // Four round trips; first we'll get "stale nonce", then
692 // "allocate mismatch", then "stale nonce" again, then finaly it will
693 // succeed.
694 EXPECT_TRUE_SIMULATED_WAIT(turn_ready_, kSimulatedRtt * 4, fake_clock_);
646 EXPECT_NE(first_nonce, turn_port_->nonce()); 695 EXPECT_NE(first_nonce, turn_port_->nonce());
647 } 696 }
648 697
649 // Tests that a new local address is created after 698 // Tests that a new local address is created after
650 // STUN_ERROR_ALLOCATION_MISMATCH. 699 // STUN_ERROR_ALLOCATION_MISMATCH.
651 TEST_F(TurnPortTest, TestTurnAllocateMismatch) { 700 TEST_F(TurnPortTest, TestTurnAllocateMismatch) {
652 // Do a normal allocation first. 701 // Do a normal allocation first.
653 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 702 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr);
654 turn_port_->PrepareAddress(); 703 turn_port_->PrepareAddress();
655 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 704 EXPECT_TRUE_SIMULATED_WAIT(turn_ready_, kSimulatedRtt * 2, fake_clock_);
656 rtc::SocketAddress first_addr(turn_port_->socket()->GetLocalAddress()); 705 rtc::SocketAddress first_addr(turn_port_->socket()->GetLocalAddress());
657 706
658 // Clear connected_ flag on turnport to suppress the release of 707 // Clear connected_ flag on turnport to suppress the release of
659 // the allocation. 708 // the allocation.
660 turn_port_->OnSocketClose(turn_port_->socket(), 0); 709 turn_port_->OnSocketClose(turn_port_->socket(), 0);
661 710
662 // Forces the socket server to assign the same port. 711 // Forces the socket server to assign the same port.
663 ss_->SetNextPortForTesting(first_addr.port()); 712 ss_->SetNextPortForTesting(first_addr.port());
664 713
665 turn_ready_ = false; 714 turn_ready_ = false;
666 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 715 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr);
667 turn_port_->PrepareAddress(); 716 turn_port_->PrepareAddress();
668 717
669 // Verifies that the new port has the same address. 718 // Verifies that the new port has the same address.
670 EXPECT_EQ(first_addr, turn_port_->socket()->GetLocalAddress()); 719 EXPECT_EQ(first_addr, turn_port_->socket()->GetLocalAddress());
671 720
672 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 721 // Four round trips; first we'll get "stale nonce", then
722 // "allocate mismatch", then "stale nonce" again, then finaly it will
723 // succeed.
724 EXPECT_TRUE_SIMULATED_WAIT(turn_ready_, kSimulatedRtt * 4, fake_clock_);
673 725
674 // Verifies that the new port has a different address now. 726 // Verifies that the new port has a different address now.
675 EXPECT_NE(first_addr, turn_port_->socket()->GetLocalAddress()); 727 EXPECT_NE(first_addr, turn_port_->socket()->GetLocalAddress());
676 728
677 // Verify that all packets received from the shared socket are ignored. 729 // Verify that all packets received from the shared socket are ignored.
678 std::string test_packet = "Test packet"; 730 std::string test_packet = "Test packet";
679 EXPECT_FALSE(turn_port_->HandleIncomingPacket( 731 EXPECT_FALSE(turn_port_->HandleIncomingPacket(
680 socket_.get(), test_packet.data(), test_packet.size(), 732 socket_.get(), test_packet.data(), test_packet.size(),
681 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0), 733 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
682 rtc::CreatePacketTime(0))); 734 rtc::CreatePacketTime(0)));
683 } 735 }
684 736
685 // Tests that a shared-socket-TurnPort creates its own socket after 737 // Tests that a shared-socket-TurnPort creates its own socket after
686 // STUN_ERROR_ALLOCATION_MISMATCH. 738 // STUN_ERROR_ALLOCATION_MISMATCH.
687 TEST_F(TurnPortTest, TestSharedSocketAllocateMismatch) { 739 TEST_F(TurnPortTest, TestSharedSocketAllocateMismatch) {
688 // Do a normal allocation first. 740 // Do a normal allocation first.
689 CreateSharedTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 741 CreateSharedTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr);
690 turn_port_->PrepareAddress(); 742 turn_port_->PrepareAddress();
691 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 743 EXPECT_TRUE_SIMULATED_WAIT(turn_ready_, kSimulatedRtt * 2, fake_clock_);
692 rtc::SocketAddress first_addr(turn_port_->socket()->GetLocalAddress()); 744 rtc::SocketAddress first_addr(turn_port_->socket()->GetLocalAddress());
693 745
694 // Clear connected_ flag on turnport to suppress the release of 746 // Clear connected_ flag on turnport to suppress the release of
695 // the allocation. 747 // the allocation.
696 turn_port_->OnSocketClose(turn_port_->socket(), 0); 748 turn_port_->OnSocketClose(turn_port_->socket(), 0);
697 749
698 turn_ready_ = false; 750 turn_ready_ = false;
699 CreateSharedTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 751 CreateSharedTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr);
700 752
701 // Verifies that the new port has the same address. 753 // Verifies that the new port has the same address.
702 EXPECT_EQ(first_addr, turn_port_->socket()->GetLocalAddress()); 754 EXPECT_EQ(first_addr, turn_port_->socket()->GetLocalAddress());
703 EXPECT_TRUE(turn_port_->SharedSocket()); 755 EXPECT_TRUE(turn_port_->SharedSocket());
704 756
705 turn_port_->PrepareAddress(); 757 turn_port_->PrepareAddress();
706 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 758 // Extra 2 round trips due to allocate mismatch.
759 EXPECT_TRUE_SIMULATED_WAIT(turn_ready_, kSimulatedRtt * 4, fake_clock_);
707 760
708 // Verifies that the new port has a different address now. 761 // Verifies that the new port has a different address now.
709 EXPECT_NE(first_addr, turn_port_->socket()->GetLocalAddress()); 762 EXPECT_NE(first_addr, turn_port_->socket()->GetLocalAddress());
710 EXPECT_FALSE(turn_port_->SharedSocket()); 763 EXPECT_FALSE(turn_port_->SharedSocket());
711 } 764 }
712 765
713 TEST_F(TurnPortTest, TestTurnTcpAllocateMismatch) { 766 TEST_F(TurnPortTest, TestTurnTcpAllocateMismatch) {
714 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP); 767 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
715 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnTcpProtoAddr); 768 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnTcpProtoAddr);
716 769
717 // Do a normal allocation first. 770 // Do a normal allocation first.
718 turn_port_->PrepareAddress(); 771 turn_port_->PrepareAddress();
719 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 772 EXPECT_TRUE_SIMULATED_WAIT(turn_ready_, kSimulatedRtt * 3, fake_clock_);
720 rtc::SocketAddress first_addr(turn_port_->socket()->GetLocalAddress()); 773 rtc::SocketAddress first_addr(turn_port_->socket()->GetLocalAddress());
721 774
722 // Clear connected_ flag on turnport to suppress the release of 775 // Clear connected_ flag on turnport to suppress the release of
723 // the allocation. 776 // the allocation.
724 turn_port_->OnSocketClose(turn_port_->socket(), 0); 777 turn_port_->OnSocketClose(turn_port_->socket(), 0);
725 778
726 // Forces the socket server to assign the same port. 779 // Forces the socket server to assign the same port.
727 ss_->SetNextPortForTesting(first_addr.port()); 780 ss_->SetNextPortForTesting(first_addr.port());
728 781
729 turn_ready_ = false; 782 turn_ready_ = false;
730 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnTcpProtoAddr); 783 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnTcpProtoAddr);
731 turn_port_->PrepareAddress(); 784 turn_port_->PrepareAddress();
732 785
733 // Verifies that the new port has the same address. 786 // Verifies that the new port has the same address.
734 EXPECT_EQ(first_addr, turn_port_->socket()->GetLocalAddress()); 787 EXPECT_EQ(first_addr, turn_port_->socket()->GetLocalAddress());
735 788
736 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 789 // Extra 2 round trips due to allocate mismatch.
790 EXPECT_TRUE_SIMULATED_WAIT(turn_ready_, kSimulatedRtt * 5, fake_clock_);
737 791
738 // Verifies that the new port has a different address now. 792 // Verifies that the new port has a different address now.
739 EXPECT_NE(first_addr, turn_port_->socket()->GetLocalAddress()); 793 EXPECT_NE(first_addr, turn_port_->socket()->GetLocalAddress());
740 } 794 }
741 795
742 TEST_F(TurnPortTest, TestRefreshRequestGetsErrorResponse) { 796 TEST_F(TurnPortTest, TestRefreshRequestGetsErrorResponse) {
743 rtc::ScopedFakeClock clock;
744 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 797 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr);
745 PrepareTurnAndUdpPorts(); 798 PrepareTurnAndUdpPorts(PROTO_UDP);
746 turn_port_->CreateConnection(udp_port_->Candidates()[0], 799 turn_port_->CreateConnection(udp_port_->Candidates()[0],
747 Port::ORIGIN_MESSAGE); 800 Port::ORIGIN_MESSAGE);
748 // Set bad credentials. 801 // Set bad credentials.
749 cricket::RelayCredentials bad_credentials("bad_user", "bad_pwd"); 802 RelayCredentials bad_credentials("bad_user", "bad_pwd");
750 turn_port_->set_credentials(bad_credentials); 803 turn_port_->set_credentials(bad_credentials);
751 turn_refresh_success_ = false; 804 turn_refresh_success_ = false;
752 // This sends out the first RefreshRequest with correct credentials. 805 // This sends out the first RefreshRequest with correct credentials.
753 // When this succeeds, it will schedule a new RefreshRequest with the bad 806 // When this succeeds, it will schedule a new RefreshRequest with the bad
754 // credential. 807 // credential.
755 turn_port_->FlushRequests(cricket::TURN_REFRESH_REQUEST); 808 turn_port_->FlushRequests(TURN_REFRESH_REQUEST);
756 EXPECT_TRUE_SIMULATED_WAIT(turn_refresh_success_, kTimeout, clock); 809 EXPECT_TRUE_SIMULATED_WAIT(turn_refresh_success_, kSimulatedRtt, fake_clock_);
757 // Flush it again, it will receive a bad response. 810 // Flush it again, it will receive a bad response.
758 turn_port_->FlushRequests(cricket::TURN_REFRESH_REQUEST); 811 turn_port_->FlushRequests(TURN_REFRESH_REQUEST);
759 EXPECT_TRUE_SIMULATED_WAIT(!turn_refresh_success_, kTimeout, clock); 812 EXPECT_TRUE_SIMULATED_WAIT(!turn_refresh_success_, kSimulatedRtt,
760 EXPECT_TRUE_SIMULATED_WAIT(!turn_port_->connected(), kTimeout, clock); 813 fake_clock_);
761 EXPECT_TRUE_SIMULATED_WAIT(CheckAllConnectionsFailedAndPruned(), kTimeout, 814 EXPECT_FALSE(turn_port_->connected());
762 clock); 815 EXPECT_TRUE(CheckAllConnectionsFailedAndPruned());
763 EXPECT_TRUE_SIMULATED_WAIT(!turn_port_->HasRequests(), kTimeout, clock); 816 EXPECT_FALSE(turn_port_->HasRequests());
764 } 817 }
765 818
766 // Test that TurnPort will not handle any incoming packets once it has been 819 // Test that TurnPort will not handle any incoming packets once it has been
767 // closed. 820 // closed.
768 TEST_F(TurnPortTest, TestStopProcessingPacketsAfterClosed) { 821 TEST_F(TurnPortTest, TestStopProcessingPacketsAfterClosed) {
769 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 822 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr);
770 PrepareTurnAndUdpPorts(); 823 PrepareTurnAndUdpPorts(PROTO_UDP);
771 Connection* conn1 = turn_port_->CreateConnection(udp_port_->Candidates()[0], 824 Connection* conn1 = turn_port_->CreateConnection(udp_port_->Candidates()[0],
772 Port::ORIGIN_MESSAGE); 825 Port::ORIGIN_MESSAGE);
773 Connection* conn2 = udp_port_->CreateConnection(turn_port_->Candidates()[0], 826 Connection* conn2 = udp_port_->CreateConnection(turn_port_->Candidates()[0],
774 Port::ORIGIN_MESSAGE); 827 Port::ORIGIN_MESSAGE);
775 ASSERT_TRUE(conn1 != NULL); 828 ASSERT_TRUE(conn1 != NULL);
776 ASSERT_TRUE(conn2 != NULL); 829 ASSERT_TRUE(conn2 != NULL);
777 // Make sure conn2 is writable. 830 // Make sure conn2 is writable.
778 conn2->Ping(0); 831 conn2->Ping(0);
779 EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, conn2->write_state(), kTimeout); 832 EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE, conn2->write_state(),
833 kSimulatedRtt * 2, fake_clock_);
780 834
781 turn_port_->Close(); 835 turn_port_->Close();
782 rtc::Thread::Current()->ProcessMessages(0); 836 SIMULATED_WAIT(false, kSimulatedRtt, fake_clock_);
783 turn_unknown_address_ = false; 837 turn_unknown_address_ = false;
784 conn2->Ping(0); 838 conn2->Ping(0);
785 rtc::Thread::Current()->ProcessMessages(500); 839 SIMULATED_WAIT(false, kSimulatedRtt, fake_clock_);
786 // Since the turn port does not handle packets any more, it should not 840 // Since the turn port does not handle packets any more, it should not
787 // SignalUnknownAddress. 841 // SignalUnknownAddress.
788 EXPECT_FALSE(turn_unknown_address_); 842 EXPECT_FALSE(turn_unknown_address_);
789 } 843 }
790 844
791 // Test that CreateConnection will return null if port becomes disconnected. 845 // Test that CreateConnection will return null if port becomes disconnected.
792 TEST_F(TurnPortTest, TestCreateConnectionWhenSocketClosed) { 846 TEST_F(TurnPortTest, TestCreateConnectionWhenSocketClosed) {
793 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP); 847 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
794 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnTcpProtoAddr); 848 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnTcpProtoAddr);
795 PrepareTurnAndUdpPorts(); 849 PrepareTurnAndUdpPorts(PROTO_TCP);
796 // Create a connection. 850 // Create a connection.
797 Connection* conn1 = turn_port_->CreateConnection(udp_port_->Candidates()[0], 851 Connection* conn1 = turn_port_->CreateConnection(udp_port_->Candidates()[0],
798 Port::ORIGIN_MESSAGE); 852 Port::ORIGIN_MESSAGE);
799 ASSERT_TRUE(conn1 != NULL); 853 ASSERT_TRUE(conn1 != NULL);
800 854
801 // Close the socket and create a connection again. 855 // Close the socket and create a connection again.
802 turn_port_->OnSocketClose(turn_port_->socket(), 1); 856 turn_port_->OnSocketClose(turn_port_->socket(), 1);
803 conn1 = turn_port_->CreateConnection(udp_port_->Candidates()[0], 857 conn1 = turn_port_->CreateConnection(udp_port_->Candidates()[0],
804 Port::ORIGIN_MESSAGE); 858 Port::ORIGIN_MESSAGE);
805 ASSERT_TRUE(conn1 == NULL); 859 ASSERT_TRUE(conn1 == NULL);
806 } 860 }
807 861
808 // Tests that when a TCP socket is closed, the respective TURN connection will 862 // Tests that when a TCP socket is closed, the respective TURN connection will
809 // be destroyed. 863 // be destroyed.
810 TEST_F(TurnPortTest, TestSocketCloseWillDestroyConnection) { 864 TEST_F(TurnPortTest, TestSocketCloseWillDestroyConnection) {
811 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP); 865 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
812 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnTcpProtoAddr); 866 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnTcpProtoAddr);
813 PrepareTurnAndUdpPorts(); 867 PrepareTurnAndUdpPorts(PROTO_TCP);
814 Connection* conn = turn_port_->CreateConnection(udp_port_->Candidates()[0], 868 Connection* conn = turn_port_->CreateConnection(udp_port_->Candidates()[0],
815 Port::ORIGIN_MESSAGE); 869 Port::ORIGIN_MESSAGE);
816 EXPECT_NE(nullptr, conn); 870 EXPECT_NE(nullptr, conn);
817 EXPECT_TRUE(!turn_port_->connections().empty()); 871 EXPECT_TRUE(!turn_port_->connections().empty());
818 turn_port_->socket()->SignalClose(turn_port_->socket(), 1); 872 turn_port_->socket()->SignalClose(turn_port_->socket(), 1);
819 EXPECT_TRUE_WAIT(turn_port_->connections().empty(), kTimeout); 873 EXPECT_TRUE_SIMULATED_WAIT(turn_port_->connections().empty(),
874 kConnectionDestructionDelay, fake_clock_);
820 } 875 }
821 876
822 // Test try-alternate-server feature. 877 // Test try-alternate-server feature.
823 TEST_F(TurnPortTest, TestTurnAlternateServerUDP) { 878 TEST_F(TurnPortTest, TestTurnAlternateServerUDP) {
824 TestTurnAlternateServer(cricket::PROTO_UDP); 879 TestTurnAlternateServer(PROTO_UDP);
825 } 880 }
826 881
827 TEST_F(TurnPortTest, TestTurnAlternateServerTCP) { 882 TEST_F(TurnPortTest, TestTurnAlternateServerTCP) {
828 TestTurnAlternateServer(cricket::PROTO_TCP); 883 TestTurnAlternateServer(PROTO_TCP);
829 } 884 }
830 885
831 // Test that we fail when we redirect to an address different from 886 // Test that we fail when we redirect to an address different from
832 // current IP family. 887 // current IP family.
833 TEST_F(TurnPortTest, TestTurnAlternateServerV4toV6UDP) { 888 TEST_F(TurnPortTest, TestTurnAlternateServerV4toV6UDP) {
834 TestTurnAlternateServerV4toV6(cricket::PROTO_UDP); 889 TestTurnAlternateServerV4toV6(PROTO_UDP);
835 } 890 }
836 891
837 TEST_F(TurnPortTest, TestTurnAlternateServerV4toV6TCP) { 892 TEST_F(TurnPortTest, TestTurnAlternateServerV4toV6TCP) {
838 TestTurnAlternateServerV4toV6(cricket::PROTO_TCP); 893 TestTurnAlternateServerV4toV6(PROTO_TCP);
839 } 894 }
840 895
841 // Test try-alternate-server catches the case of pingpong. 896 // Test try-alternate-server catches the case of pingpong.
842 TEST_F(TurnPortTest, TestTurnAlternateServerPingPongUDP) { 897 TEST_F(TurnPortTest, TestTurnAlternateServerPingPongUDP) {
843 TestTurnAlternateServerPingPong(cricket::PROTO_UDP); 898 TestTurnAlternateServerPingPong(PROTO_UDP);
844 } 899 }
845 900
846 TEST_F(TurnPortTest, TestTurnAlternateServerPingPongTCP) { 901 TEST_F(TurnPortTest, TestTurnAlternateServerPingPongTCP) {
847 TestTurnAlternateServerPingPong(cricket::PROTO_TCP); 902 TestTurnAlternateServerPingPong(PROTO_TCP);
848 } 903 }
849 904
850 // Test try-alternate-server catch the case of repeated server. 905 // Test try-alternate-server catch the case of repeated server.
851 TEST_F(TurnPortTest, TestTurnAlternateServerDetectRepetitionUDP) { 906 TEST_F(TurnPortTest, TestTurnAlternateServerDetectRepetitionUDP) {
852 TestTurnAlternateServerDetectRepetition(cricket::PROTO_UDP); 907 TestTurnAlternateServerDetectRepetition(PROTO_UDP);
853 } 908 }
854 909
855 TEST_F(TurnPortTest, TestTurnAlternateServerDetectRepetitionTCP) { 910 TEST_F(TurnPortTest, TestTurnAlternateServerDetectRepetitionTCP) {
856 TestTurnAlternateServerDetectRepetition(cricket::PROTO_TCP); 911 TestTurnAlternateServerDetectRepetition(PROTO_TCP);
857 } 912 }
858 913
859 // Do a TURN allocation and try to send a packet to it from the outside. 914 // Do a TURN allocation and try to send a packet to it from the outside.
860 // The packet should be dropped. Then, try to send a packet from TURN to the 915 // The packet should be dropped. Then, try to send a packet from TURN to the
861 // outside. It should reach its destination. Finally, try again from the 916 // outside. It should reach its destination. Finally, try again from the
862 // outside. It should now work as well. 917 // outside. It should now work as well.
863 TEST_F(TurnPortTest, TestTurnConnection) { 918 TEST_F(TurnPortTest, TestTurnConnection) {
864 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 919 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr);
865 TestTurnConnection(); 920 TestTurnConnection(PROTO_UDP);
866 } 921 }
867 922
868 // Similar to above, except that this test will use the shared socket. 923 // Similar to above, except that this test will use the shared socket.
869 TEST_F(TurnPortTest, TestTurnConnectionUsingSharedSocket) { 924 TEST_F(TurnPortTest, TestTurnConnectionUsingSharedSocket) {
870 CreateSharedTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 925 CreateSharedTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr);
871 TestTurnConnection(); 926 TestTurnConnection(PROTO_UDP);
872 } 927 }
873 928
874 // Test that we can establish a TCP connection with TURN server. 929 // Test that we can establish a TCP connection with TURN server.
875 TEST_F(TurnPortTest, TestTurnTcpConnection) { 930 TEST_F(TurnPortTest, TestTurnTcpConnection) {
876 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP); 931 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
877 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnTcpProtoAddr); 932 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnTcpProtoAddr);
878 TestTurnConnection(); 933 TestTurnConnection(PROTO_TCP);
879 } 934 }
880 935
881 // Test that if a connection on a TURN port is destroyed, the TURN port can 936 // Test that if a connection on a TURN port is destroyed, the TURN port can
882 // still receive ping on that connection as if it is from an unknown address. 937 // still receive ping on that connection as if it is from an unknown address.
883 // If the connection is created again, it will be used to receive ping. 938 // If the connection is created again, it will be used to receive ping.
884 TEST_F(TurnPortTest, TestDestroyTurnConnection) { 939 TEST_F(TurnPortTest, TestDestroyTurnConnection) {
885 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 940 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr);
886 TestDestroyTurnConnection(); 941 TestDestroyTurnConnection();
887 } 942 }
888 943
889 // Similar to above, except that this test will use the shared socket. 944 // Similar to above, except that this test will use the shared socket.
890 TEST_F(TurnPortTest, TestDestroyTurnConnectionUsingSharedSocket) { 945 TEST_F(TurnPortTest, TestDestroyTurnConnectionUsingSharedSocket) {
891 CreateSharedTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 946 CreateSharedTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr);
892 TestDestroyTurnConnection(); 947 TestDestroyTurnConnection();
893 } 948 }
894 949
895 // Test that we fail to create a connection when we want to use TLS over TCP. 950 // Test that we fail to create a connection when we want to use TLS over TCP.
896 // This test should be removed once we have TLS support. 951 // This test should be removed once we have TLS support.
897 TEST_F(TurnPortTest, TestTurnTlsTcpConnectionFails) { 952 TEST_F(TurnPortTest, TestTurnTlsTcpConnectionFails) {
898 cricket::ProtocolAddress secure_addr(kTurnTcpProtoAddr.address, 953 ProtocolAddress secure_addr(kTurnTcpProtoAddr.address,
899 kTurnTcpProtoAddr.proto, 954 kTurnTcpProtoAddr.proto, true);
900 true);
901 CreateTurnPort(kTurnUsername, kTurnPassword, secure_addr); 955 CreateTurnPort(kTurnUsername, kTurnPassword, secure_addr);
902 turn_port_->PrepareAddress(); 956 turn_port_->PrepareAddress();
903 EXPECT_TRUE_WAIT(turn_error_, kTimeout); 957 EXPECT_TRUE_SIMULATED_WAIT(turn_error_, kSimulatedRtt * 2, fake_clock_);
904 ASSERT_EQ(0U, turn_port_->Candidates().size()); 958 ASSERT_EQ(0U, turn_port_->Candidates().size());
905 } 959 }
906 960
907 // Run TurnConnectionTest with one-time-use nonce feature. 961 // Run TurnConnectionTest with one-time-use nonce feature.
908 // Here server will send a 438 STALE_NONCE error message for 962 // Here server will send a 438 STALE_NONCE error message for
909 // every TURN transaction. 963 // every TURN transaction.
910 TEST_F(TurnPortTest, TestTurnConnectionUsingOTUNonce) { 964 TEST_F(TurnPortTest, TestTurnConnectionUsingOTUNonce) {
911 turn_server_.set_enable_otu_nonce(true); 965 turn_server_.set_enable_otu_nonce(true);
912 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 966 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr);
913 TestTurnConnection(); 967 TestTurnConnection(PROTO_UDP);
914 } 968 }
915 969
916 // Test that CreatePermissionRequest will be scheduled after the success 970 // Test that CreatePermissionRequest will be scheduled after the success
917 // of the first create permission request and the request will get an 971 // of the first create permission request and the request will get an
918 // ErrorResponse if the ufrag and pwd are incorrect. 972 // ErrorResponse if the ufrag and pwd are incorrect.
919 TEST_F(TurnPortTest, TestRefreshCreatePermissionRequest) { 973 TEST_F(TurnPortTest, TestRefreshCreatePermissionRequest) {
920 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 974 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr);
921 PrepareTurnAndUdpPorts(); 975 PrepareTurnAndUdpPorts(PROTO_UDP);
922 976
923 Connection* conn = turn_port_->CreateConnection(udp_port_->Candidates()[0], 977 Connection* conn = turn_port_->CreateConnection(udp_port_->Candidates()[0],
924 Port::ORIGIN_MESSAGE); 978 Port::ORIGIN_MESSAGE);
925 ASSERT_TRUE(conn != NULL); 979 ASSERT_TRUE(conn != NULL);
926 EXPECT_TRUE_WAIT(turn_create_permission_success_, kTimeout); 980 EXPECT_TRUE_SIMULATED_WAIT(turn_create_permission_success_, kSimulatedRtt,
981 fake_clock_);
927 turn_create_permission_success_ = false; 982 turn_create_permission_success_ = false;
928 // A create-permission-request should be pending. 983 // A create-permission-request should be pending.
929 // After the next create-permission-response is received, it will schedule 984 // After the next create-permission-response is received, it will schedule
930 // another request with bad_ufrag and bad_pwd. 985 // another request with bad_ufrag and bad_pwd.
931 cricket::RelayCredentials bad_credentials("bad_user", "bad_pwd"); 986 RelayCredentials bad_credentials("bad_user", "bad_pwd");
932 turn_port_->set_credentials(bad_credentials); 987 turn_port_->set_credentials(bad_credentials);
933 turn_port_->FlushRequests(cricket::kAllRequests); 988 turn_port_->FlushRequests(kAllRequests);
934 EXPECT_TRUE_WAIT(turn_create_permission_success_, kTimeout); 989 EXPECT_TRUE_SIMULATED_WAIT(turn_create_permission_success_, kSimulatedRtt,
990 fake_clock_);
935 // Flush the requests again; the create-permission-request will fail. 991 // Flush the requests again; the create-permission-request will fail.
936 turn_port_->FlushRequests(cricket::kAllRequests); 992 turn_port_->FlushRequests(kAllRequests);
937 EXPECT_TRUE_WAIT(!turn_create_permission_success_, kTimeout); 993 EXPECT_TRUE_SIMULATED_WAIT(!turn_create_permission_success_, kSimulatedRtt,
938 EXPECT_TRUE_WAIT(CheckConnectionFailedAndPruned(conn), kTimeout); 994 fake_clock_);
995 EXPECT_TRUE(CheckConnectionFailedAndPruned(conn));
939 } 996 }
940 997
941 TEST_F(TurnPortTest, TestChannelBindGetErrorResponse) { 998 TEST_F(TurnPortTest, TestChannelBindGetErrorResponse) {
942 rtc::ScopedFakeClock clock;
943 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 999 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr);
944 PrepareTurnAndUdpPorts(); 1000 PrepareTurnAndUdpPorts(PROTO_UDP);
945 Connection* conn1 = turn_port_->CreateConnection(udp_port_->Candidates()[0], 1001 Connection* conn1 = turn_port_->CreateConnection(udp_port_->Candidates()[0],
946 Port::ORIGIN_MESSAGE); 1002 Port::ORIGIN_MESSAGE);
947 ASSERT_TRUE(conn1 != nullptr); 1003 ASSERT_TRUE(conn1 != nullptr);
948 Connection* conn2 = udp_port_->CreateConnection(turn_port_->Candidates()[0], 1004 Connection* conn2 = udp_port_->CreateConnection(turn_port_->Candidates()[0],
949 Port::ORIGIN_MESSAGE); 1005 Port::ORIGIN_MESSAGE);
950 1006
951 ASSERT_TRUE(conn2 != nullptr); 1007 ASSERT_TRUE(conn2 != nullptr);
952 conn1->Ping(0); 1008 conn1->Ping(0);
953 EXPECT_TRUE_SIMULATED_WAIT(conn1->writable(), kTimeout, clock); 1009 EXPECT_TRUE_SIMULATED_WAIT(conn1->writable(), kSimulatedRtt * 2, fake_clock_);
954 bool success = 1010 // TODO(deadbeef): SetEntryChannelId should not be a public method.
955 turn_port_->SetEntryChannelId(udp_port_->Candidates()[0].address(), -1); 1011 // Instead we should set an option on the fake TURN server to force it to
956 ASSERT_TRUE(success); 1012 // send a channel bind errors.
1013 ASSERT_TRUE(
1014 turn_port_->SetEntryChannelId(udp_port_->Candidates()[0].address(), -1));
957 1015
958 std::string data = "ABC"; 1016 std::string data = "ABC";
959 conn1->Send(data.data(), data.length(), options); 1017 conn1->Send(data.data(), data.length(), options);
960 1018
961 EXPECT_TRUE_SIMULATED_WAIT(CheckConnectionFailedAndPruned(conn1), kTimeout, 1019 EXPECT_TRUE_SIMULATED_WAIT(CheckConnectionFailedAndPruned(conn1),
962 clock); 1020 kSimulatedRtt, fake_clock_);
963 // Verify that no packet can be sent after a bind request error. 1021 // Verify that no packet can be sent after a bind request error.
964 conn2->SignalReadPacket.connect(static_cast<TurnPortTest*>(this), 1022 conn2->SignalReadPacket.connect(static_cast<TurnPortTest*>(this),
965 &TurnPortTest::OnUdpReadPacket); 1023 &TurnPortTest::OnUdpReadPacket);
966 conn1->Send(data.data(), data.length(), options); 1024 conn1->Send(data.data(), data.length(), options);
967 SIMULATED_WAIT(!udp_packets_.empty(), kTimeout, clock); 1025 SIMULATED_WAIT(!udp_packets_.empty(), kSimulatedRtt, fake_clock_);
968 EXPECT_TRUE(udp_packets_.empty()); 1026 EXPECT_TRUE(udp_packets_.empty());
969 } 1027 }
970 1028
971 // Do a TURN allocation, establish a UDP connection, and send some data. 1029 // Do a TURN allocation, establish a UDP connection, and send some data.
972 TEST_F(TurnPortTest, TestTurnSendDataTurnUdpToUdp) { 1030 TEST_F(TurnPortTest, TestTurnSendDataTurnUdpToUdp) {
973 // Create ports and prepare addresses. 1031 // Create ports and prepare addresses.
974 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 1032 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr);
975 TestTurnSendData(); 1033 TestTurnSendData(PROTO_UDP);
976 EXPECT_EQ(cricket::UDP_PROTOCOL_NAME, 1034 EXPECT_EQ(UDP_PROTOCOL_NAME, turn_port_->Candidates()[0].relay_protocol());
977 turn_port_->Candidates()[0].relay_protocol());
978 } 1035 }
979 1036
980 // Do a TURN allocation, establish a TCP connection, and send some data. 1037 // Do a TURN allocation, establish a TCP connection, and send some data.
981 TEST_F(TurnPortTest, TestTurnSendDataTurnTcpToUdp) { 1038 TEST_F(TurnPortTest, TestTurnSendDataTurnTcpToUdp) {
982 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP); 1039 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
983 // Create ports and prepare addresses. 1040 // Create ports and prepare addresses.
984 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnTcpProtoAddr); 1041 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnTcpProtoAddr);
985 TestTurnSendData(); 1042 TestTurnSendData(PROTO_TCP);
986 EXPECT_EQ(cricket::TCP_PROTOCOL_NAME, 1043 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_port_->Candidates()[0].relay_protocol());
987 turn_port_->Candidates()[0].relay_protocol());
988 } 1044 }
989 1045
990 // Test TURN fails to make a connection from IPv6 address to a server which has 1046 // Test TURN fails to make a connection from IPv6 address to a server which has
991 // IPv4 address. 1047 // IPv4 address.
992 TEST_F(TurnPortTest, TestTurnLocalIPv6AddressServerIPv4) { 1048 TEST_F(TurnPortTest, TestTurnLocalIPv6AddressServerIPv4) {
993 turn_server_.AddInternalSocket(kTurnUdpIPv6IntAddr, cricket::PROTO_UDP); 1049 turn_server_.AddInternalSocket(kTurnUdpIPv6IntAddr, PROTO_UDP);
994 CreateTurnPort(kLocalIPv6Addr, kTurnUsername, kTurnPassword, 1050 CreateTurnPort(kLocalIPv6Addr, kTurnUsername, kTurnPassword,
995 kTurnUdpProtoAddr); 1051 kTurnUdpProtoAddr);
996 turn_port_->PrepareAddress(); 1052 turn_port_->PrepareAddress();
997 ASSERT_TRUE_WAIT(turn_error_, kTimeout); 1053 ASSERT_TRUE_SIMULATED_WAIT(turn_error_, kSimulatedRtt, fake_clock_);
998 EXPECT_TRUE(turn_port_->Candidates().empty()); 1054 EXPECT_TRUE(turn_port_->Candidates().empty());
999 } 1055 }
1000 1056
1001 // Test TURN make a connection from IPv6 address to a server which has 1057 // Test TURN make a connection from IPv6 address to a server which has
1002 // IPv6 intenal address. But in this test external address is a IPv4 address, 1058 // IPv6 intenal address. But in this test external address is a IPv4 address,
1003 // hence allocated address will be a IPv4 address. 1059 // hence allocated address will be a IPv4 address.
1004 TEST_F(TurnPortTest, TestTurnLocalIPv6AddressServerIPv6ExtenalIPv4) { 1060 TEST_F(TurnPortTest, TestTurnLocalIPv6AddressServerIPv6ExtenalIPv4) {
1005 turn_server_.AddInternalSocket(kTurnUdpIPv6IntAddr, cricket::PROTO_UDP); 1061 turn_server_.AddInternalSocket(kTurnUdpIPv6IntAddr, PROTO_UDP);
1006 CreateTurnPort(kLocalIPv6Addr, kTurnUsername, kTurnPassword, 1062 CreateTurnPort(kLocalIPv6Addr, kTurnUsername, kTurnPassword,
1007 kTurnUdpIPv6ProtoAddr); 1063 kTurnUdpIPv6ProtoAddr);
1008 turn_port_->PrepareAddress(); 1064 turn_port_->PrepareAddress();
1009 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 1065 EXPECT_TRUE_SIMULATED_WAIT(turn_ready_, kSimulatedRtt * 2, fake_clock_);
1010 ASSERT_EQ(1U, turn_port_->Candidates().size()); 1066 ASSERT_EQ(1U, turn_port_->Candidates().size());
1011 EXPECT_EQ(kTurnUdpExtAddr.ipaddr(), 1067 EXPECT_EQ(kTurnUdpExtAddr.ipaddr(),
1012 turn_port_->Candidates()[0].address().ipaddr()); 1068 turn_port_->Candidates()[0].address().ipaddr());
1013 EXPECT_NE(0, turn_port_->Candidates()[0].address().port()); 1069 EXPECT_NE(0, turn_port_->Candidates()[0].address().port());
1014 } 1070 }
1015 1071
1016 // Tests that the local and remote candidate address families should match when 1072 // Tests that the local and remote candidate address families should match when
1017 // a connection is created. Specifically, if a TURN port has an IPv6 address, 1073 // a connection is created. Specifically, if a TURN port has an IPv6 address,
1018 // its local candidate will still be an IPv4 address and it can only create 1074 // its local candidate will still be an IPv4 address and it can only create
1019 // connections with IPv4 remote candidates. 1075 // connections with IPv4 remote candidates.
1020 TEST_F(TurnPortTest, TestCandidateAddressFamilyMatch) { 1076 TEST_F(TurnPortTest, TestCandidateAddressFamilyMatch) {
1021 turn_server_.AddInternalSocket(kTurnUdpIPv6IntAddr, cricket::PROTO_UDP); 1077 turn_server_.AddInternalSocket(kTurnUdpIPv6IntAddr, PROTO_UDP);
1022 1078
1023 CreateTurnPort(kLocalIPv6Addr, kTurnUsername, kTurnPassword, 1079 CreateTurnPort(kLocalIPv6Addr, kTurnUsername, kTurnPassword,
1024 kTurnUdpIPv6ProtoAddr); 1080 kTurnUdpIPv6ProtoAddr);
1025 turn_port_->PrepareAddress(); 1081 turn_port_->PrepareAddress();
1026 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 1082 EXPECT_TRUE_SIMULATED_WAIT(turn_ready_, kSimulatedRtt * 2, fake_clock_);
1027 ASSERT_EQ(1U, turn_port_->Candidates().size()); 1083 ASSERT_EQ(1U, turn_port_->Candidates().size());
1028 1084
1029 // Create an IPv4 candidate. It will match the TURN candidate. 1085 // Create an IPv4 candidate. It will match the TURN candidate.
1030 cricket::Candidate remote_candidate(cricket::ICE_CANDIDATE_COMPONENT_RTP, 1086 Candidate remote_candidate(ICE_CANDIDATE_COMPONENT_RTP, "udp", kLocalAddr2, 0,
1031 "udp", kLocalAddr2, 0, "", "", "local", 0, 1087 "", "", "local", 0, kCandidateFoundation);
1032 kCandidateFoundation);
1033 remote_candidate.set_address(kLocalAddr2); 1088 remote_candidate.set_address(kLocalAddr2);
1034 Connection* conn = 1089 Connection* conn =
1035 turn_port_->CreateConnection(remote_candidate, Port::ORIGIN_MESSAGE); 1090 turn_port_->CreateConnection(remote_candidate, Port::ORIGIN_MESSAGE);
1036 EXPECT_NE(nullptr, conn); 1091 EXPECT_NE(nullptr, conn);
1037 1092
1038 // Set the candidate address family to IPv6. It won't match the TURN 1093 // Set the candidate address family to IPv6. It won't match the TURN
1039 // candidate. 1094 // candidate.
1040 remote_candidate.set_address(kLocalIPv6Addr2); 1095 remote_candidate.set_address(kLocalIPv6Addr2);
1041 conn = turn_port_->CreateConnection(remote_candidate, Port::ORIGIN_MESSAGE); 1096 conn = turn_port_->CreateConnection(remote_candidate, Port::ORIGIN_MESSAGE);
1042 EXPECT_EQ(nullptr, conn); 1097 EXPECT_EQ(nullptr, conn);
1043 } 1098 }
1044 1099
1045 TEST_F(TurnPortTest, TestOriginHeader) { 1100 TEST_F(TurnPortTest, TestOriginHeader) {
1046 CreateTurnPortWithOrigin(kLocalAddr1, kTurnUsername, kTurnPassword, 1101 CreateTurnPortWithOrigin(kLocalAddr1, kTurnUsername, kTurnPassword,
1047 kTurnUdpProtoAddr, kTestOrigin); 1102 kTurnUdpProtoAddr, kTestOrigin);
1048 turn_port_->PrepareAddress(); 1103 turn_port_->PrepareAddress();
1049 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 1104 EXPECT_TRUE_SIMULATED_WAIT(turn_ready_, kSimulatedRtt * 2, fake_clock_);
1050 ASSERT_GT(turn_server_.server()->allocations().size(), 0U); 1105 ASSERT_GT(turn_server_.server()->allocations().size(), 0U);
1051 SocketAddress local_address = turn_port_->GetLocalAddress(); 1106 SocketAddress local_address = turn_port_->GetLocalAddress();
1052 ASSERT_TRUE(turn_server_.FindAllocation(local_address) != NULL); 1107 ASSERT_TRUE(turn_server_.FindAllocation(local_address) != NULL);
1053 EXPECT_EQ(kTestOrigin, turn_server_.FindAllocation(local_address)->origin()); 1108 EXPECT_EQ(kTestOrigin, turn_server_.FindAllocation(local_address)->origin());
1054 } 1109 }
1055 1110
1056 // Test that a CreatePermission failure will result in the connection being 1111 // Test that a CreatePermission failure will result in the connection being
1057 // pruned and failed. 1112 // pruned and failed.
1058 TEST_F(TurnPortTest, TestConnectionFaildAndPrunedOnCreatePermissionFailure) { 1113 TEST_F(TurnPortTest, TestConnectionFailedAndPrunedOnCreatePermissionFailure) {
1059 rtc::ScopedFakeClock clock; 1114 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
1060 SIMULATED_WAIT(false, 101, clock);
1061 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
1062 turn_server_.server()->set_reject_private_addresses(true); 1115 turn_server_.server()->set_reject_private_addresses(true);
1063 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnTcpProtoAddr); 1116 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnTcpProtoAddr);
1064 turn_port_->PrepareAddress(); 1117 turn_port_->PrepareAddress();
1065 EXPECT_TRUE_SIMULATED_WAIT(turn_ready_, kTimeout, clock); 1118 EXPECT_TRUE_SIMULATED_WAIT(turn_ready_, kSimulatedRtt * 3, fake_clock_);
1066 1119
1067 CreateUdpPort(SocketAddress("10.0.0.10", 0)); 1120 CreateUdpPort(SocketAddress("10.0.0.10", 0));
1068 udp_port_->PrepareAddress(); 1121 udp_port_->PrepareAddress();
1069 EXPECT_TRUE_SIMULATED_WAIT(udp_ready_, kTimeout, clock); 1122 EXPECT_TRUE_SIMULATED_WAIT(udp_ready_, kSimulatedRtt, fake_clock_);
1070 // Create a connection. 1123 // Create a connection.
1071 TestConnectionWrapper conn(turn_port_->CreateConnection( 1124 TestConnectionWrapper conn(turn_port_->CreateConnection(
1072 udp_port_->Candidates()[0], Port::ORIGIN_MESSAGE)); 1125 udp_port_->Candidates()[0], Port::ORIGIN_MESSAGE));
1073 EXPECT_TRUE(conn.connection() != nullptr); 1126 EXPECT_TRUE(conn.connection() != nullptr);
1074 1127
1075 // Asynchronously, CreatePermission request should be sent and fail, which 1128 // Asynchronously, CreatePermission request should be sent and fail, which
1076 // will make the connection pruned and failed. 1129 // will make the connection pruned and failed.
1077 EXPECT_TRUE_SIMULATED_WAIT(CheckConnectionFailedAndPruned(conn.connection()), 1130 EXPECT_TRUE_SIMULATED_WAIT(CheckConnectionFailedAndPruned(conn.connection()),
1078 kTimeout, clock); 1131 kSimulatedRtt, fake_clock_);
1079 EXPECT_TRUE_SIMULATED_WAIT(!turn_create_permission_success_, kTimeout, clock); 1132 EXPECT_TRUE_SIMULATED_WAIT(!turn_create_permission_success_, kSimulatedRtt,
1133 fake_clock_);
1080 // Check that the connection is not deleted asynchronously. 1134 // Check that the connection is not deleted asynchronously.
1081 SIMULATED_WAIT(conn.connection() == nullptr, kTimeout, clock); 1135 SIMULATED_WAIT(conn.connection() == nullptr, kConnectionDestructionDelay,
1082 EXPECT_TRUE(conn.connection() != nullptr); 1136 fake_clock_);
1137 EXPECT_NE(nullptr, conn.connection());
1083 } 1138 }
1084 1139
1085 // Test that a TURN allocation is released when the port is closed. 1140 // Test that a TURN allocation is released when the port is closed.
1086 TEST_F(TurnPortTest, TestTurnReleaseAllocation) { 1141 TEST_F(TurnPortTest, TestTurnReleaseAllocation) {
1087 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 1142 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr);
1088 turn_port_->PrepareAddress(); 1143 turn_port_->PrepareAddress();
1089 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 1144 EXPECT_TRUE_SIMULATED_WAIT(turn_ready_, kSimulatedRtt * 2, fake_clock_);
1090 1145
1091 ASSERT_GT(turn_server_.server()->allocations().size(), 0U); 1146 ASSERT_GT(turn_server_.server()->allocations().size(), 0U);
1092 turn_port_.reset(); 1147 turn_port_.reset();
1093 EXPECT_EQ_WAIT(0U, turn_server_.server()->allocations().size(), kTimeout); 1148 EXPECT_EQ_SIMULATED_WAIT(0U, turn_server_.server()->allocations().size(),
1149 kSimulatedRtt, fake_clock_);
1094 } 1150 }
1095 1151
1096 // Test that a TURN TCP allocation is released when the port is closed. 1152 // Test that a TURN TCP allocation is released when the port is closed.
1097 TEST_F(TurnPortTest, DISABLED_TestTurnTCPReleaseAllocation) { 1153 TEST_F(TurnPortTest, TestTurnTCPReleaseAllocation) {
1098 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP); 1154 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
1099 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnTcpProtoAddr); 1155 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnTcpProtoAddr);
1100 turn_port_->PrepareAddress(); 1156 turn_port_->PrepareAddress();
1101 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 1157 EXPECT_TRUE_SIMULATED_WAIT(turn_ready_, kSimulatedRtt * 3, fake_clock_);
1102 1158
1103 ASSERT_GT(turn_server_.server()->allocations().size(), 0U); 1159 ASSERT_GT(turn_server_.server()->allocations().size(), 0U);
1104 turn_port_.reset(); 1160 turn_port_.reset();
1105 EXPECT_EQ_WAIT(0U, turn_server_.server()->allocations().size(), kTimeout); 1161 EXPECT_EQ_SIMULATED_WAIT(0U, turn_server_.server()->allocations().size(),
1162 kSimulatedRtt, fake_clock_);
1106 } 1163 }
1107 1164
1108 // This test verifies any FD's are not leaked after TurnPort is destroyed. 1165 // This test verifies any FD's are not leaked after TurnPort is destroyed.
1109 // https://code.google.com/p/webrtc/issues/detail?id=2651 1166 // https://code.google.com/p/webrtc/issues/detail?id=2651
1110 #if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID) 1167 #if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)
1111 // 1 second is not always enough for getaddrinfo().
1112 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5191
1113 static const unsigned int kResolverTimeout = 10000;
1114 1168
1115 TEST_F(TurnPortTest, TestResolverShutdown) { 1169 TEST_F(TurnPortTest, TestResolverShutdown) {
1116 turn_server_.AddInternalSocket(kTurnUdpIPv6IntAddr, cricket::PROTO_UDP); 1170 turn_server_.AddInternalSocket(kTurnUdpIPv6IntAddr, PROTO_UDP);
1117 int last_fd_count = GetFDCount(); 1171 int last_fd_count = GetFDCount();
1118 // Need to supply unresolved address to kick off resolver. 1172 // Need to supply unresolved address to kick off resolver.
1119 CreateTurnPort(kLocalIPv6Addr, kTurnUsername, kTurnPassword, 1173 CreateTurnPort(kLocalIPv6Addr, kTurnUsername, kTurnPassword,
1120 cricket::ProtocolAddress(rtc::SocketAddress( 1174 ProtocolAddress(rtc::SocketAddress("www.google.invalid", 3478),
1121 "www.google.invalid", 3478), cricket::PROTO_UDP)); 1175 PROTO_UDP));
1122 turn_port_->PrepareAddress(); 1176 turn_port_->PrepareAddress();
1123 ASSERT_TRUE_WAIT(turn_error_, kResolverTimeout); 1177 ASSERT_TRUE_WAIT(turn_error_, kResolverTimeout);
1124 EXPECT_TRUE(turn_port_->Candidates().empty()); 1178 EXPECT_TRUE(turn_port_->Candidates().empty());
1125 turn_port_.reset(); 1179 turn_port_.reset();
1126 rtc::Thread::Current()->Post(RTC_FROM_HERE, this, MSG_TESTFINISH); 1180 rtc::Thread::Current()->Post(RTC_FROM_HERE, this, MSG_TESTFINISH);
1127 // Waiting for above message to be processed. 1181 // Waiting for above message to be processed.
1128 ASSERT_TRUE_WAIT(test_finish_, kTimeout); 1182 ASSERT_TRUE_SIMULATED_WAIT(test_finish_, 1, fake_clock_);
1129 EXPECT_EQ(last_fd_count, GetFDCount()); 1183 EXPECT_EQ(last_fd_count, GetFDCount());
1130 } 1184 }
1131 #endif 1185 #endif
1186
1187 } // namespace cricket
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698