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

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

Powered by Google App Engine
This is Rietveld 408576698