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

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

Issue 2063823008: Adding IceConfig option to assume TURN/TURN candidate pairs will work. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Formatting. Created 4 years, 6 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 2009 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2009 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 10
(...skipping 14 matching lines...) Expand all
25 #include "webrtc/base/logging.h" 25 #include "webrtc/base/logging.h"
26 #include "webrtc/base/natserver.h" 26 #include "webrtc/base/natserver.h"
27 #include "webrtc/base/natsocketfactory.h" 27 #include "webrtc/base/natsocketfactory.h"
28 #include "webrtc/base/physicalsocketserver.h" 28 #include "webrtc/base/physicalsocketserver.h"
29 #include "webrtc/base/proxyserver.h" 29 #include "webrtc/base/proxyserver.h"
30 #include "webrtc/base/socketaddress.h" 30 #include "webrtc/base/socketaddress.h"
31 #include "webrtc/base/ssladapter.h" 31 #include "webrtc/base/ssladapter.h"
32 #include "webrtc/base/thread.h" 32 #include "webrtc/base/thread.h"
33 #include "webrtc/base/virtualsocketserver.h" 33 #include "webrtc/base/virtualsocketserver.h"
34 34
35 using cricket::kDefaultPortAllocatorFlags; 35 namespace {
36 using cricket::kMinimumStepDelay; 36
37 using cricket::kDefaultStepDelay;
38 using cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET;
39 using cricket::ServerAddresses;
40 using cricket::MIN_PINGS_AT_WEAK_PING_INTERVAL;
41 using rtc::SocketAddress; 37 using rtc::SocketAddress;
42 38
43 static const int kDefaultTimeout = 1000; 39 // Default timeout for tests in this file.
40 // Should be large enough for slow buildbots to run the tests reliably.
41 static const int kDefaultTimeout = 10000;
42
44 static const int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN | 43 static const int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
45 cricket::PORTALLOCATOR_DISABLE_RELAY | 44 cricket::PORTALLOCATOR_DISABLE_RELAY |
46 cricket::PORTALLOCATOR_DISABLE_TCP; 45 cricket::PORTALLOCATOR_DISABLE_TCP;
47 // Addresses on the public internet. 46 // Addresses on the public internet.
48 static const SocketAddress kPublicAddrs[2] = 47 static const SocketAddress kPublicAddrs[2] =
49 { SocketAddress("11.11.11.11", 0), SocketAddress("22.22.22.22", 0) }; 48 { SocketAddress("11.11.11.11", 0), SocketAddress("22.22.22.22", 0) };
50 // IPv6 Addresses on the public internet. 49 // IPv6 Addresses on the public internet.
51 static const SocketAddress kIPv6PublicAddrs[2] = { 50 static const SocketAddress kIPv6PublicAddrs[2] = {
52 SocketAddress("2400:4030:1:2c00:be30:abcd:efab:cdef", 0), 51 SocketAddress("2400:4030:1:2c00:be30:abcd:efab:cdef", 0),
53 SocketAddress("2620:0:1000:1b03:2e41:38ff:fea6:f2a4", 0) 52 SocketAddress("2620:0:1000:1b03:2e41:38ff:fea6:f2a4", 0)
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 static const char* kIcePwd[4] = {"TESTICEPWD00000000000000", 96 static const char* kIcePwd[4] = {"TESTICEPWD00000000000000",
98 "TESTICEPWD00000000000001", 97 "TESTICEPWD00000000000001",
99 "TESTICEPWD00000000000002", 98 "TESTICEPWD00000000000002",
100 "TESTICEPWD00000000000003"}; 99 "TESTICEPWD00000000000003"};
101 100
102 static const uint64_t kTiebreaker1 = 11111; 101 static const uint64_t kTiebreaker1 = 11111;
103 static const uint64_t kTiebreaker2 = 22222; 102 static const uint64_t kTiebreaker2 = 22222;
104 103
105 enum { MSG_ADD_CANDIDATES, MSG_REMOVE_CANDIDATES }; 104 enum { MSG_ADD_CANDIDATES, MSG_REMOVE_CANDIDATES };
106 105
107 static cricket::IceConfig CreateIceConfig(int receiving_timeout, 106 cricket::IceConfig CreateIceConfig(int receiving_timeout,
108 bool gather_continually, 107 bool gather_continually,
109 int backup_ping_interval = -1) { 108 int backup_ping_interval = -1) {
110 cricket::IceConfig config; 109 cricket::IceConfig config;
111 config.receiving_timeout = receiving_timeout; 110 config.receiving_timeout = receiving_timeout;
112 config.gather_continually = gather_continually; 111 config.gather_continually = gather_continually;
113 config.backup_connection_ping_interval = backup_ping_interval; 112 config.backup_connection_ping_interval = backup_ping_interval;
114 return config; 113 return config;
115 } 114 }
116 115
116 cricket::Candidate CreateHostCandidate(const std::string& ip,
pthatcher1 2016/06/15 19:12:47 CreateUdpHostCandidate?
Taylor Brandstetter 2016/06/16 00:13:41 Done.
117 int port,
118 int priority,
119 const std::string& ufrag = "") {
120 cricket::Candidate c;
121 c.set_address(rtc::SocketAddress(ip, port));
122 c.set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT);
123 c.set_protocol(cricket::UDP_PROTOCOL_NAME);
124 c.set_priority(priority);
125 c.set_username(ufrag);
126 c.set_type(cricket::LOCAL_PORT_TYPE);
127 return c;
128 }
129
130 cricket::Candidate CreateRelayCandidate(const std::string& ip,
pthatcher1 2016/06/15 19:12:47 CreateUdpRelayCandidate?
Taylor Brandstetter 2016/06/16 00:13:41 Done.
131 int port,
132 int priority,
133 const std::string& ufrag = "") {
134 cricket::Candidate c;
135 c.set_address(rtc::SocketAddress(ip, port));
136 c.set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT);
137 c.set_protocol(cricket::UDP_PROTOCOL_NAME);
138 c.set_priority(priority);
139 c.set_username(ufrag);
140 c.set_type(cricket::RELAY_PORT_TYPE);
141 return c;
pthatcher1 2016/06/15 19:12:47 It seems like this could use a common CreateUdpCan
Taylor Brandstetter 2016/06/16 00:13:41 Done.
142 }
143
144 } // namespace {
145
146 namespace cricket {
147
117 // This test simulates 2 P2P endpoints that want to establish connectivity 148 // This test simulates 2 P2P endpoints that want to establish connectivity
118 // with each other over various network topologies and conditions, which can be 149 // with each other over various network topologies and conditions, which can be
119 // specified in each individial test. 150 // specified in each individial test.
120 // A virtual network (via VirtualSocketServer) along with virtual firewalls and 151 // A virtual network (via VirtualSocketServer) along with virtual firewalls and
121 // NATs (via Firewall/NATSocketServer) are used to simulate the various network 152 // NATs (via Firewall/NATSocketServer) are used to simulate the various network
122 // conditions. We can configure the IP addresses of the endpoints, 153 // conditions. We can configure the IP addresses of the endpoints,
123 // block various types of connectivity, or add arbitrary levels of NAT. 154 // block various types of connectivity, or add arbitrary levels of NAT.
124 // We also run a STUN server and a relay server on the virtual network to allow 155 // We also run a STUN server and a relay server on the virtual network to allow
125 // our typical P2P mechanisms to do their thing. 156 // our typical P2P mechanisms to do their thing.
126 // For each case, we expect the P2P stack to eventually settle on a specific 157 // For each case, we expect the P2P stack to eventually settle on a specific
127 // form of connectivity to the other side. The test checks that the P2P 158 // form of connectivity to the other side. The test checks that the P2P
128 // negotiation successfully establishes connectivity within a certain time, 159 // negotiation successfully establishes connectivity within a certain time,
129 // and that the result is what we expect. 160 // and that the result is what we expect.
130 // Note that this class is a base class for use by other tests, who will provide 161 // Note that this class is a base class for use by other tests, who will provide
131 // specialized test behavior. 162 // specialized test behavior.
132 class P2PTransportChannelTestBase : public testing::Test, 163 class P2PTransportChannelTestBase : public testing::Test,
133 public rtc::MessageHandler, 164 public rtc::MessageHandler,
134 public sigslot::has_slots<> { 165 public sigslot::has_slots<> {
135 public: 166 public:
136 P2PTransportChannelTestBase() 167 P2PTransportChannelTestBase()
137 : main_(rtc::Thread::Current()), 168 : main_(rtc::Thread::Current()),
138 pss_(new rtc::PhysicalSocketServer), 169 pss_(new rtc::PhysicalSocketServer),
139 vss_(new rtc::VirtualSocketServer(pss_.get())), 170 vss_(new rtc::VirtualSocketServer(pss_.get())),
140 nss_(new rtc::NATSocketServer(vss_.get())), 171 nss_(new rtc::NATSocketServer(vss_.get())),
141 ss_(new rtc::FirewallSocketServer(nss_.get())), 172 ss_(new rtc::FirewallSocketServer(nss_.get())),
142 ss_scope_(ss_.get()), 173 ss_scope_(ss_.get()),
143 stun_server_(cricket::TestStunServer::Create(main_, kStunAddr)), 174 stun_server_(TestStunServer::Create(main_, kStunAddr)),
144 turn_server_(main_, kTurnUdpIntAddr, kTurnUdpExtAddr), 175 turn_server_(main_, kTurnUdpIntAddr, kTurnUdpExtAddr),
145 relay_server_(main_, kRelayUdpIntAddr, kRelayUdpExtAddr, 176 relay_server_(main_,
146 kRelayTcpIntAddr, kRelayTcpExtAddr, 177 kRelayUdpIntAddr,
147 kRelaySslTcpIntAddr, kRelaySslTcpExtAddr), 178 kRelayUdpExtAddr,
148 socks_server1_(ss_.get(), kSocksProxyAddrs[0], 179 kRelayTcpIntAddr,
149 ss_.get(), kSocksProxyAddrs[0]), 180 kRelayTcpExtAddr,
150 socks_server2_(ss_.get(), kSocksProxyAddrs[1], 181 kRelaySslTcpIntAddr,
151 ss_.get(), kSocksProxyAddrs[1]), 182 kRelaySslTcpExtAddr),
183 socks_server1_(ss_.get(),
184 kSocksProxyAddrs[0],
185 ss_.get(),
186 kSocksProxyAddrs[0]),
187 socks_server2_(ss_.get(),
188 kSocksProxyAddrs[1],
189 ss_.get(),
190 kSocksProxyAddrs[1]),
152 clear_remote_candidates_ufrag_pwd_(false), 191 clear_remote_candidates_ufrag_pwd_(false),
153 force_relay_(false) { 192 force_relay_(false) {
154 ep1_.role_ = cricket::ICEROLE_CONTROLLING; 193 ep1_.role_ = ICEROLE_CONTROLLING;
155 ep2_.role_ = cricket::ICEROLE_CONTROLLED; 194 ep2_.role_ = ICEROLE_CONTROLLED;
156 195
157 ServerAddresses stun_servers; 196 ServerAddresses stun_servers;
158 stun_servers.insert(kStunAddr); 197 stun_servers.insert(kStunAddr);
159 ep1_.allocator_.reset(new cricket::BasicPortAllocator( 198 ep1_.allocator_.reset(new BasicPortAllocator(
160 &ep1_.network_manager_, 199 &ep1_.network_manager_, stun_servers, kRelayUdpIntAddr,
161 stun_servers, kRelayUdpIntAddr, kRelayTcpIntAddr, kRelaySslTcpIntAddr)); 200 kRelayTcpIntAddr, kRelaySslTcpIntAddr));
162 ep2_.allocator_.reset(new cricket::BasicPortAllocator( 201 ep2_.allocator_.reset(new BasicPortAllocator(
163 &ep2_.network_manager_, 202 &ep2_.network_manager_, stun_servers, kRelayUdpIntAddr,
164 stun_servers, kRelayUdpIntAddr, kRelayTcpIntAddr, kRelaySslTcpIntAddr)); 203 kRelayTcpIntAddr, kRelaySslTcpIntAddr));
165 } 204 }
166 205
167 protected: 206 protected:
168 enum Config { 207 enum Config {
169 OPEN, // Open to the Internet 208 OPEN, // Open to the Internet
170 NAT_FULL_CONE, // NAT, no filtering 209 NAT_FULL_CONE, // NAT, no filtering
171 NAT_ADDR_RESTRICTED, // NAT, must send to an addr to recv 210 NAT_ADDR_RESTRICTED, // NAT, must send to an addr to recv
172 NAT_PORT_RESTRICTED, // NAT, must send to an addr+port to recv 211 NAT_PORT_RESTRICTED, // NAT, must send to an addr+port to recv
173 NAT_SYMMETRIC, // NAT, endpoint-dependent bindings 212 NAT_SYMMETRIC, // NAT, endpoint-dependent bindings
174 NAT_DOUBLE_CONE, // Double NAT, both cone 213 NAT_DOUBLE_CONE, // Double NAT, both cone
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 if (!ch_packets_.empty()) { 247 if (!ch_packets_.empty()) {
209 std::string packet = ch_packets_.front(); 248 std::string packet = ch_packets_.front();
210 ret = (packet == std::string(data, len)); 249 ret = (packet == std::string(data, len));
211 ch_packets_.pop_front(); 250 ch_packets_.pop_front();
212 } 251 }
213 return ret; 252 return ret;
214 } 253 }
215 254
216 std::string name_; // TODO - Currently not used. 255 std::string name_; // TODO - Currently not used.
217 std::list<std::string> ch_packets_; 256 std::list<std::string> ch_packets_;
218 std::unique_ptr<cricket::P2PTransportChannel> ch_; 257 std::unique_ptr<P2PTransportChannel> ch_;
219 }; 258 };
220 259
221 struct CandidatesData : public rtc::MessageData { 260 struct CandidatesData : public rtc::MessageData {
222 CandidatesData(cricket::TransportChannel* ch, const cricket::Candidate& c) 261 CandidatesData(TransportChannel* ch, const Candidate& c)
223 : channel(ch), candidates(1, c) {} 262 : channel(ch), candidates(1, c) {}
224 CandidatesData(cricket::TransportChannel* ch, 263 CandidatesData(TransportChannel* ch, const std::vector<Candidate>& cc)
225 const std::vector<cricket::Candidate>& cc)
226 : channel(ch), candidates(cc) {} 264 : channel(ch), candidates(cc) {}
227 cricket::TransportChannel* channel; 265 TransportChannel* channel;
228 cricket::Candidates candidates; 266 Candidates candidates;
229 }; 267 };
230 268
231 struct Endpoint { 269 struct Endpoint {
232 Endpoint() 270 Endpoint()
233 : role_(cricket::ICEROLE_UNKNOWN), 271 : role_(ICEROLE_UNKNOWN),
234 tiebreaker_(0), 272 tiebreaker_(0),
235 role_conflict_(false), 273 role_conflict_(false),
236 save_candidates_(false) {} 274 save_candidates_(false) {}
237 bool HasChannel(cricket::TransportChannel* ch) { 275 bool HasChannel(TransportChannel* ch) {
238 return (ch == cd1_.ch_.get() || ch == cd2_.ch_.get()); 276 return (ch == cd1_.ch_.get() || ch == cd2_.ch_.get());
239 } 277 }
240 ChannelData* GetChannelData(cricket::TransportChannel* ch) { 278 ChannelData* GetChannelData(TransportChannel* ch) {
241 if (!HasChannel(ch)) return NULL; 279 if (!HasChannel(ch)) return NULL;
242 if (cd1_.ch_.get() == ch) 280 if (cd1_.ch_.get() == ch)
243 return &cd1_; 281 return &cd1_;
244 else 282 else
245 return &cd2_; 283 return &cd2_;
246 } 284 }
247 285
248 void SetIceRole(cricket::IceRole role) { role_ = role; } 286 void SetIceRole(IceRole role) { role_ = role; }
249 cricket::IceRole ice_role() { return role_; } 287 IceRole ice_role() { return role_; }
250 void SetIceTiebreaker(uint64_t tiebreaker) { tiebreaker_ = tiebreaker; } 288 void SetIceTiebreaker(uint64_t tiebreaker) { tiebreaker_ = tiebreaker; }
251 uint64_t GetIceTiebreaker() { return tiebreaker_; } 289 uint64_t GetIceTiebreaker() { return tiebreaker_; }
252 void OnRoleConflict(bool role_conflict) { role_conflict_ = role_conflict; } 290 void OnRoleConflict(bool role_conflict) { role_conflict_ = role_conflict; }
253 bool role_conflict() { return role_conflict_; } 291 bool role_conflict() { return role_conflict_; }
254 void SetAllocationStepDelay(uint32_t delay) { 292 void SetAllocationStepDelay(uint32_t delay) {
255 allocator_->set_step_delay(delay); 293 allocator_->set_step_delay(delay);
256 } 294 }
257 void SetAllowTcpListen(bool allow_tcp_listen) { 295 void SetAllowTcpListen(bool allow_tcp_listen) {
258 allocator_->set_allow_tcp_listen(allow_tcp_listen); 296 allocator_->set_allow_tcp_listen(allow_tcp_listen);
259 } 297 }
260 298
261 rtc::FakeNetworkManager network_manager_; 299 rtc::FakeNetworkManager network_manager_;
262 std::unique_ptr<cricket::BasicPortAllocator> allocator_; 300 std::unique_ptr<BasicPortAllocator> allocator_;
263 ChannelData cd1_; 301 ChannelData cd1_;
264 ChannelData cd2_; 302 ChannelData cd2_;
265 cricket::IceRole role_; 303 IceRole role_;
266 uint64_t tiebreaker_; 304 uint64_t tiebreaker_;
267 bool role_conflict_; 305 bool role_conflict_;
268 bool save_candidates_; 306 bool save_candidates_;
269 std::vector<CandidatesData*> saved_candidates_; 307 std::vector<CandidatesData*> saved_candidates_;
270 }; 308 };
271 309
272 ChannelData* GetChannelData(cricket::TransportChannel* channel) { 310 ChannelData* GetChannelData(TransportChannel* channel) {
273 if (ep1_.HasChannel(channel)) 311 if (ep1_.HasChannel(channel))
274 return ep1_.GetChannelData(channel); 312 return ep1_.GetChannelData(channel);
275 else 313 else
276 return ep2_.GetChannelData(channel); 314 return ep2_.GetChannelData(channel);
277 } 315 }
278 316
279 void CreateChannels(int num) { 317 void CreateChannels(int num) {
280 std::string ice_ufrag_ep1_cd1_ch = kIceUfrag[0]; 318 std::string ice_ufrag_ep1_cd1_ch = kIceUfrag[0];
281 std::string ice_pwd_ep1_cd1_ch = kIcePwd[0]; 319 std::string ice_pwd_ep1_cd1_ch = kIcePwd[0];
282 std::string ice_ufrag_ep2_cd1_ch = kIceUfrag[1]; 320 std::string ice_ufrag_ep2_cd1_ch = kIceUfrag[1];
283 std::string ice_pwd_ep2_cd1_ch = kIcePwd[1]; 321 std::string ice_pwd_ep2_cd1_ch = kIcePwd[1];
284 ep1_.cd1_.ch_.reset(CreateChannel( 322 ep1_.cd1_.ch_.reset(CreateChannel(
285 0, cricket::ICE_CANDIDATE_COMPONENT_DEFAULT, 323 0, ICE_CANDIDATE_COMPONENT_DEFAULT, ice_ufrag_ep1_cd1_ch,
286 ice_ufrag_ep1_cd1_ch, ice_pwd_ep1_cd1_ch, 324 ice_pwd_ep1_cd1_ch, ice_ufrag_ep2_cd1_ch, ice_pwd_ep2_cd1_ch));
287 ice_ufrag_ep2_cd1_ch, ice_pwd_ep2_cd1_ch));
288 ep2_.cd1_.ch_.reset(CreateChannel( 325 ep2_.cd1_.ch_.reset(CreateChannel(
289 1, cricket::ICE_CANDIDATE_COMPONENT_DEFAULT, 326 1, ICE_CANDIDATE_COMPONENT_DEFAULT, ice_ufrag_ep2_cd1_ch,
290 ice_ufrag_ep2_cd1_ch, ice_pwd_ep2_cd1_ch, 327 ice_pwd_ep2_cd1_ch, ice_ufrag_ep1_cd1_ch, ice_pwd_ep1_cd1_ch));
291 ice_ufrag_ep1_cd1_ch, ice_pwd_ep1_cd1_ch));
292 ep1_.cd1_.ch_->MaybeStartGathering(); 328 ep1_.cd1_.ch_->MaybeStartGathering();
293 ep2_.cd1_.ch_->MaybeStartGathering(); 329 ep2_.cd1_.ch_->MaybeStartGathering();
294 if (num == 2) { 330 if (num == 2) {
295 std::string ice_ufrag_ep1_cd2_ch = kIceUfrag[2]; 331 std::string ice_ufrag_ep1_cd2_ch = kIceUfrag[2];
296 std::string ice_pwd_ep1_cd2_ch = kIcePwd[2]; 332 std::string ice_pwd_ep1_cd2_ch = kIcePwd[2];
297 std::string ice_ufrag_ep2_cd2_ch = kIceUfrag[3]; 333 std::string ice_ufrag_ep2_cd2_ch = kIceUfrag[3];
298 std::string ice_pwd_ep2_cd2_ch = kIcePwd[3]; 334 std::string ice_pwd_ep2_cd2_ch = kIcePwd[3];
299 ep1_.cd2_.ch_.reset(CreateChannel( 335 ep1_.cd2_.ch_.reset(CreateChannel(
300 0, cricket::ICE_CANDIDATE_COMPONENT_DEFAULT, 336 0, ICE_CANDIDATE_COMPONENT_DEFAULT, ice_ufrag_ep1_cd2_ch,
301 ice_ufrag_ep1_cd2_ch, ice_pwd_ep1_cd2_ch, 337 ice_pwd_ep1_cd2_ch, ice_ufrag_ep2_cd2_ch, ice_pwd_ep2_cd2_ch));
302 ice_ufrag_ep2_cd2_ch, ice_pwd_ep2_cd2_ch));
303 ep2_.cd2_.ch_.reset(CreateChannel( 338 ep2_.cd2_.ch_.reset(CreateChannel(
304 1, cricket::ICE_CANDIDATE_COMPONENT_DEFAULT, 339 1, ICE_CANDIDATE_COMPONENT_DEFAULT, ice_ufrag_ep2_cd2_ch,
305 ice_ufrag_ep2_cd2_ch, ice_pwd_ep2_cd2_ch, 340 ice_pwd_ep2_cd2_ch, ice_ufrag_ep1_cd2_ch, ice_pwd_ep1_cd2_ch));
306 ice_ufrag_ep1_cd2_ch, ice_pwd_ep1_cd2_ch));
307 ep1_.cd2_.ch_->MaybeStartGathering(); 341 ep1_.cd2_.ch_->MaybeStartGathering();
308 ep2_.cd2_.ch_->MaybeStartGathering(); 342 ep2_.cd2_.ch_->MaybeStartGathering();
309 } 343 }
310 } 344 }
311 cricket::P2PTransportChannel* CreateChannel( 345 P2PTransportChannel* CreateChannel(int endpoint,
312 int endpoint, 346 int component,
313 int component, 347 const std::string& local_ice_ufrag,
314 const std::string& local_ice_ufrag, 348 const std::string& local_ice_pwd,
315 const std::string& local_ice_pwd, 349 const std::string& remote_ice_ufrag,
316 const std::string& remote_ice_ufrag, 350 const std::string& remote_ice_pwd) {
317 const std::string& remote_ice_pwd) { 351 P2PTransportChannel* channel = new P2PTransportChannel(
318 cricket::P2PTransportChannel* channel = new cricket::P2PTransportChannel(
319 "test content name", component, GetAllocator(endpoint)); 352 "test content name", component, GetAllocator(endpoint));
320 channel->SignalCandidateGathered.connect( 353 channel->SignalCandidateGathered.connect(
321 this, &P2PTransportChannelTestBase::OnCandidateGathered); 354 this, &P2PTransportChannelTestBase::OnCandidateGathered);
322 channel->SignalCandidatesRemoved.connect( 355 channel->SignalCandidatesRemoved.connect(
323 this, &P2PTransportChannelTestBase::OnCandidatesRemoved); 356 this, &P2PTransportChannelTestBase::OnCandidatesRemoved);
324 channel->SignalReadPacket.connect( 357 channel->SignalReadPacket.connect(
325 this, &P2PTransportChannelTestBase::OnReadPacket); 358 this, &P2PTransportChannelTestBase::OnReadPacket);
326 channel->SignalRoleConflict.connect( 359 channel->SignalRoleConflict.connect(
327 this, &P2PTransportChannelTestBase::OnRoleConflict); 360 this, &P2PTransportChannelTestBase::OnRoleConflict);
328 channel->SetIceCredentials(local_ice_ufrag, local_ice_pwd); 361 channel->SetIceCredentials(local_ice_ufrag, local_ice_pwd);
329 if (clear_remote_candidates_ufrag_pwd_) { 362 if (clear_remote_candidates_ufrag_pwd_) {
330 // This only needs to be set if we're clearing them from the 363 // This only needs to be set if we're clearing them from the
331 // candidates. Some unit tests rely on this not being set. 364 // candidates. Some unit tests rely on this not being set.
332 channel->SetRemoteIceCredentials(remote_ice_ufrag, remote_ice_pwd); 365 channel->SetRemoteIceCredentials(remote_ice_ufrag, remote_ice_pwd);
333 } 366 }
334 channel->SetIceRole(GetEndpoint(endpoint)->ice_role()); 367 channel->SetIceRole(GetEndpoint(endpoint)->ice_role());
335 channel->SetIceTiebreaker(GetEndpoint(endpoint)->GetIceTiebreaker()); 368 channel->SetIceTiebreaker(GetEndpoint(endpoint)->GetIceTiebreaker());
336 channel->Connect(); 369 channel->Connect();
337 return channel; 370 return channel;
338 } 371 }
339 void DestroyChannels() { 372 void DestroyChannels() {
340 ep1_.cd1_.ch_.reset(); 373 ep1_.cd1_.ch_.reset();
341 ep2_.cd1_.ch_.reset(); 374 ep2_.cd1_.ch_.reset();
342 ep1_.cd2_.ch_.reset(); 375 ep1_.cd2_.ch_.reset();
343 ep2_.cd2_.ch_.reset(); 376 ep2_.cd2_.ch_.reset();
344 } 377 }
345 cricket::P2PTransportChannel* ep1_ch1() { return ep1_.cd1_.ch_.get(); } 378 P2PTransportChannel* ep1_ch1() { return ep1_.cd1_.ch_.get(); }
346 cricket::P2PTransportChannel* ep1_ch2() { return ep1_.cd2_.ch_.get(); } 379 P2PTransportChannel* ep1_ch2() { return ep1_.cd2_.ch_.get(); }
347 cricket::P2PTransportChannel* ep2_ch1() { return ep2_.cd1_.ch_.get(); } 380 P2PTransportChannel* ep2_ch1() { return ep2_.cd1_.ch_.get(); }
348 cricket::P2PTransportChannel* ep2_ch2() { return ep2_.cd2_.ch_.get(); } 381 P2PTransportChannel* ep2_ch2() { return ep2_.cd2_.ch_.get(); }
349 382
350 // Common results. 383 // Common results.
351 static const Result kLocalUdpToLocalUdp; 384 static const Result kLocalUdpToLocalUdp;
352 static const Result kLocalUdpToStunUdp; 385 static const Result kLocalUdpToStunUdp;
353 static const Result kLocalUdpToPrflxUdp; 386 static const Result kLocalUdpToPrflxUdp;
354 static const Result kPrflxUdpToLocalUdp; 387 static const Result kPrflxUdpToLocalUdp;
355 static const Result kStunUdpToLocalUdp; 388 static const Result kStunUdpToLocalUdp;
356 static const Result kStunUdpToStunUdp; 389 static const Result kStunUdpToStunUdp;
357 static const Result kPrflxUdpToStunUdp; 390 static const Result kPrflxUdpToStunUdp;
358 static const Result kLocalUdpToRelayUdp; 391 static const Result kLocalUdpToRelayUdp;
359 static const Result kPrflxUdpToRelayUdp; 392 static const Result kPrflxUdpToRelayUdp;
360 static const Result kLocalTcpToLocalTcp; 393 static const Result kLocalTcpToLocalTcp;
361 static const Result kLocalTcpToPrflxTcp; 394 static const Result kLocalTcpToPrflxTcp;
362 static const Result kPrflxTcpToLocalTcp; 395 static const Result kPrflxTcpToLocalTcp;
363 396
364 rtc::NATSocketServer* nat() { return nss_.get(); } 397 rtc::NATSocketServer* nat() { return nss_.get(); }
365 rtc::FirewallSocketServer* fw() { return ss_.get(); } 398 rtc::FirewallSocketServer* fw() { return ss_.get(); }
366 399
367 Endpoint* GetEndpoint(int endpoint) { 400 Endpoint* GetEndpoint(int endpoint) {
368 if (endpoint == 0) { 401 if (endpoint == 0) {
369 return &ep1_; 402 return &ep1_;
370 } else if (endpoint == 1) { 403 } else if (endpoint == 1) {
371 return &ep2_; 404 return &ep2_;
372 } else { 405 } else {
373 return NULL; 406 return NULL;
374 } 407 }
375 } 408 }
376 cricket::PortAllocator* GetAllocator(int endpoint) { 409 PortAllocator* GetAllocator(int endpoint) {
377 return GetEndpoint(endpoint)->allocator_.get(); 410 return GetEndpoint(endpoint)->allocator_.get();
378 } 411 }
379 void AddAddress(int endpoint, const SocketAddress& addr) { 412 void AddAddress(int endpoint, const SocketAddress& addr) {
380 GetEndpoint(endpoint)->network_manager_.AddInterface(addr); 413 GetEndpoint(endpoint)->network_manager_.AddInterface(addr);
381 } 414 }
382 void AddAddress(int endpoint, 415 void AddAddress(int endpoint,
383 const SocketAddress& addr, 416 const SocketAddress& addr,
384 const std::string& ifname, 417 const std::string& ifname,
385 rtc::AdapterType adapter_type) { 418 rtc::AdapterType adapter_type) {
386 GetEndpoint(endpoint)->network_manager_.AddInterface(addr, ifname, 419 GetEndpoint(endpoint)->network_manager_.AddInterface(addr, ifname,
387 adapter_type); 420 adapter_type);
388 } 421 }
389 void RemoveAddress(int endpoint, const SocketAddress& addr) { 422 void RemoveAddress(int endpoint, const SocketAddress& addr) {
390 GetEndpoint(endpoint)->network_manager_.RemoveInterface(addr); 423 GetEndpoint(endpoint)->network_manager_.RemoveInterface(addr);
391 } 424 }
392 void SetProxy(int endpoint, rtc::ProxyType type) { 425 void SetProxy(int endpoint, rtc::ProxyType type) {
393 rtc::ProxyInfo info; 426 rtc::ProxyInfo info;
394 info.type = type; 427 info.type = type;
395 info.address = (type == rtc::PROXY_HTTPS) ? 428 info.address = (type == rtc::PROXY_HTTPS) ?
396 kHttpsProxyAddrs[endpoint] : kSocksProxyAddrs[endpoint]; 429 kHttpsProxyAddrs[endpoint] : kSocksProxyAddrs[endpoint];
397 GetAllocator(endpoint)->set_proxy("unittest/1.0", info); 430 GetAllocator(endpoint)->set_proxy("unittest/1.0", info);
398 } 431 }
399 void SetAllocatorFlags(int endpoint, int flags) { 432 void SetAllocatorFlags(int endpoint, int flags) {
400 GetAllocator(endpoint)->set_flags(flags); 433 GetAllocator(endpoint)->set_flags(flags);
401 } 434 }
402 void SetIceRole(int endpoint, cricket::IceRole role) { 435 void SetIceRole(int endpoint, IceRole role) {
403 GetEndpoint(endpoint)->SetIceRole(role); 436 GetEndpoint(endpoint)->SetIceRole(role);
404 } 437 }
405 void SetIceTiebreaker(int endpoint, uint64_t tiebreaker) { 438 void SetIceTiebreaker(int endpoint, uint64_t tiebreaker) {
406 GetEndpoint(endpoint)->SetIceTiebreaker(tiebreaker); 439 GetEndpoint(endpoint)->SetIceTiebreaker(tiebreaker);
407 } 440 }
408 bool GetRoleConflict(int endpoint) { 441 bool GetRoleConflict(int endpoint) {
409 return GetEndpoint(endpoint)->role_conflict(); 442 return GetEndpoint(endpoint)->role_conflict();
410 } 443 }
411 void SetAllocationStepDelay(int endpoint, uint32_t delay) { 444 void SetAllocationStepDelay(int endpoint, uint32_t delay) {
412 return GetEndpoint(endpoint)->SetAllocationStepDelay(delay); 445 return GetEndpoint(endpoint)->SetAllocationStepDelay(delay);
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
584 // restart the ice gathering. Finally it waits for the transport to select a 617 // restart the ice gathering. Finally it waits for the transport to select a
585 // new connection using the newly generated ice candidates. 618 // new connection using the newly generated ice candidates.
586 // Before calling this function the end points must be configured. 619 // Before calling this function the end points must be configured.
587 void TestHandleIceUfragPasswordChanged() { 620 void TestHandleIceUfragPasswordChanged() {
588 ep1_ch1()->SetRemoteIceCredentials(kIceUfrag[1], kIcePwd[1]); 621 ep1_ch1()->SetRemoteIceCredentials(kIceUfrag[1], kIcePwd[1]);
589 ep2_ch1()->SetRemoteIceCredentials(kIceUfrag[0], kIcePwd[0]); 622 ep2_ch1()->SetRemoteIceCredentials(kIceUfrag[0], kIcePwd[0]);
590 EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() && 623 EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
591 ep2_ch1()->receiving() && ep2_ch1()->writable(), 624 ep2_ch1()->receiving() && ep2_ch1()->writable(),
592 1000, 1000); 625 1000, 1000);
593 626
594 const cricket::Candidate* old_local_candidate1 = LocalCandidate(ep1_ch1()); 627 const Candidate* old_local_candidate1 = LocalCandidate(ep1_ch1());
595 const cricket::Candidate* old_local_candidate2 = LocalCandidate(ep2_ch1()); 628 const Candidate* old_local_candidate2 = LocalCandidate(ep2_ch1());
596 const cricket::Candidate* old_remote_candidate1 = 629 const Candidate* old_remote_candidate1 = RemoteCandidate(ep1_ch1());
597 RemoteCandidate(ep1_ch1()); 630 const Candidate* old_remote_candidate2 = RemoteCandidate(ep2_ch1());
598 const cricket::Candidate* old_remote_candidate2 =
599 RemoteCandidate(ep2_ch1());
600 631
601 ep1_ch1()->SetIceCredentials(kIceUfrag[2], kIcePwd[2]); 632 ep1_ch1()->SetIceCredentials(kIceUfrag[2], kIcePwd[2]);
602 ep1_ch1()->SetRemoteIceCredentials(kIceUfrag[3], kIcePwd[3]); 633 ep1_ch1()->SetRemoteIceCredentials(kIceUfrag[3], kIcePwd[3]);
603 ep1_ch1()->MaybeStartGathering(); 634 ep1_ch1()->MaybeStartGathering();
604 ep2_ch1()->SetIceCredentials(kIceUfrag[3], kIcePwd[3]); 635 ep2_ch1()->SetIceCredentials(kIceUfrag[3], kIcePwd[3]);
605 ep2_ch1()->SetRemoteIceCredentials(kIceUfrag[2], kIcePwd[2]); 636 ep2_ch1()->SetRemoteIceCredentials(kIceUfrag[2], kIcePwd[2]);
606 ep2_ch1()->MaybeStartGathering(); 637 ep2_ch1()->MaybeStartGathering();
607 638
608 EXPECT_TRUE_WAIT_MARGIN(LocalCandidate(ep1_ch1())->generation() != 639 EXPECT_TRUE_WAIT_MARGIN(LocalCandidate(ep1_ch1())->generation() !=
609 old_local_candidate1->generation(), 640 old_local_candidate1->generation(),
610 1000, 1000); 641 1000, 1000);
611 EXPECT_TRUE_WAIT_MARGIN(LocalCandidate(ep2_ch1())->generation() != 642 EXPECT_TRUE_WAIT_MARGIN(LocalCandidate(ep2_ch1())->generation() !=
612 old_local_candidate2->generation(), 643 old_local_candidate2->generation(),
613 1000, 1000); 644 1000, 1000);
614 EXPECT_TRUE_WAIT_MARGIN(RemoteCandidate(ep1_ch1())->generation() != 645 EXPECT_TRUE_WAIT_MARGIN(RemoteCandidate(ep1_ch1())->generation() !=
615 old_remote_candidate1->generation(), 646 old_remote_candidate1->generation(),
616 1000, 1000); 647 1000, 1000);
617 EXPECT_TRUE_WAIT_MARGIN(RemoteCandidate(ep2_ch1())->generation() != 648 EXPECT_TRUE_WAIT_MARGIN(RemoteCandidate(ep2_ch1())->generation() !=
618 old_remote_candidate2->generation(), 649 old_remote_candidate2->generation(),
619 1000, 1000); 650 1000, 1000);
620 EXPECT_EQ(1u, RemoteCandidate(ep2_ch1())->generation()); 651 EXPECT_EQ(1u, RemoteCandidate(ep2_ch1())->generation());
621 EXPECT_EQ(1u, RemoteCandidate(ep1_ch1())->generation()); 652 EXPECT_EQ(1u, RemoteCandidate(ep1_ch1())->generation());
622 } 653 }
623 654
624 void TestSignalRoleConflict() { 655 void TestSignalRoleConflict() {
625 SetIceTiebreaker(0, kTiebreaker1); // Default EP1 is in controlling state. 656 SetIceTiebreaker(0, kTiebreaker1); // Default EP1 is in controlling state.
626 657
627 SetIceRole(1, cricket::ICEROLE_CONTROLLING); 658 SetIceRole(1, ICEROLE_CONTROLLING);
628 SetIceTiebreaker(1, kTiebreaker2); 659 SetIceTiebreaker(1, kTiebreaker2);
629 660
630 // Creating channels with both channels role set to CONTROLLING. 661 // Creating channels with both channels role set to CONTROLLING.
631 CreateChannels(1); 662 CreateChannels(1);
632 // Since both the channels initiated with controlling state and channel2 663 // Since both the channels initiated with controlling state and channel2
633 // has higher tiebreaker value, channel1 should receive SignalRoleConflict. 664 // has higher tiebreaker value, channel1 should receive SignalRoleConflict.
634 EXPECT_TRUE_WAIT(GetRoleConflict(0), 1000); 665 EXPECT_TRUE_WAIT(GetRoleConflict(0), 1000);
635 EXPECT_FALSE(GetRoleConflict(1)); 666 EXPECT_FALSE(GetRoleConflict(1));
636 667
637 EXPECT_TRUE_WAIT(ep1_ch1()->receiving() && 668 EXPECT_TRUE_WAIT(ep1_ch1()->receiving() &&
638 ep1_ch1()->writable() && 669 ep1_ch1()->writable() &&
639 ep2_ch1()->receiving() && 670 ep2_ch1()->receiving() &&
640 ep2_ch1()->writable(), 671 ep2_ch1()->writable(),
641 1000); 672 1000);
642 673
643 EXPECT_TRUE(ep1_ch1()->best_connection() && 674 EXPECT_TRUE(ep1_ch1()->best_connection() &&
644 ep2_ch1()->best_connection()); 675 ep2_ch1()->best_connection());
645 676
646 TestSendRecv(1); 677 TestSendRecv(1);
647 } 678 }
648 679
649 // We pass the candidates directly to the other side. 680 // We pass the candidates directly to the other side.
650 void OnCandidateGathered(cricket::TransportChannelImpl* ch, 681 void OnCandidateGathered(TransportChannelImpl* ch, const Candidate& c) {
651 const cricket::Candidate& c) { 682 if (force_relay_ && c.type() != RELAY_PORT_TYPE)
652 if (force_relay_ && c.type() != cricket::RELAY_PORT_TYPE)
653 return; 683 return;
654 684
655 if (GetEndpoint(ch)->save_candidates_) { 685 if (GetEndpoint(ch)->save_candidates_) {
656 GetEndpoint(ch)->saved_candidates_.push_back(new CandidatesData(ch, c)); 686 GetEndpoint(ch)->saved_candidates_.push_back(new CandidatesData(ch, c));
657 } else { 687 } else {
658 main_->Post(RTC_FROM_HERE, this, MSG_ADD_CANDIDATES, 688 main_->Post(RTC_FROM_HERE, this, MSG_ADD_CANDIDATES,
659 new CandidatesData(ch, c)); 689 new CandidatesData(ch, c));
660 } 690 }
661 } 691 }
662 692
663 void PauseCandidates(int endpoint) { 693 void PauseCandidates(int endpoint) {
664 GetEndpoint(endpoint)->save_candidates_ = true; 694 GetEndpoint(endpoint)->save_candidates_ = true;
665 } 695 }
666 696
667 void OnCandidatesRemoved(cricket::TransportChannelImpl* ch, 697 void OnCandidatesRemoved(TransportChannelImpl* ch,
668 const std::vector<cricket::Candidate>& candidates) { 698 const std::vector<Candidate>& candidates) {
669 // Candidate removals are not paused. 699 // Candidate removals are not paused.
670 CandidatesData* candidates_data = new CandidatesData(ch, candidates); 700 CandidatesData* candidates_data = new CandidatesData(ch, candidates);
671 main_->Post(RTC_FROM_HERE, this, MSG_REMOVE_CANDIDATES, candidates_data); 701 main_->Post(RTC_FROM_HERE, this, MSG_REMOVE_CANDIDATES, candidates_data);
672 } 702 }
673 703
674 // Tcp candidate verification has to be done when they are generated. 704 // Tcp candidate verification has to be done when they are generated.
675 void VerifySavedTcpCandidates(int endpoint, const std::string& tcptype) { 705 void VerifySavedTcpCandidates(int endpoint, const std::string& tcptype) {
676 for (auto& data : GetEndpoint(endpoint)->saved_candidates_) { 706 for (auto& data : GetEndpoint(endpoint)->saved_candidates_) {
677 for (auto& candidate : data->candidates) { 707 for (auto& candidate : data->candidates) {
678 EXPECT_EQ(candidate.protocol(), cricket::TCP_PROTOCOL_NAME); 708 EXPECT_EQ(candidate.protocol(), TCP_PROTOCOL_NAME);
679 EXPECT_EQ(candidate.tcptype(), tcptype); 709 EXPECT_EQ(candidate.tcptype(), tcptype);
680 if (candidate.tcptype() == cricket::TCPTYPE_ACTIVE_STR) { 710 if (candidate.tcptype() == TCPTYPE_ACTIVE_STR) {
681 EXPECT_EQ(candidate.address().port(), cricket::DISCARD_PORT); 711 EXPECT_EQ(candidate.address().port(), DISCARD_PORT);
682 } else if (candidate.tcptype() == cricket::TCPTYPE_PASSIVE_STR) { 712 } else if (candidate.tcptype() == TCPTYPE_PASSIVE_STR) {
683 EXPECT_NE(candidate.address().port(), cricket::DISCARD_PORT); 713 EXPECT_NE(candidate.address().port(), DISCARD_PORT);
684 } else { 714 } else {
685 FAIL() << "Unknown tcptype: " << candidate.tcptype(); 715 FAIL() << "Unknown tcptype: " << candidate.tcptype();
686 } 716 }
687 } 717 }
688 } 718 }
689 } 719 }
690 720
691 void ResumeCandidates(int endpoint) { 721 void ResumeCandidates(int endpoint) {
692 Endpoint* ed = GetEndpoint(endpoint); 722 Endpoint* ed = GetEndpoint(endpoint);
693 std::vector<CandidatesData*>::iterator it = ed->saved_candidates_.begin(); 723 std::vector<CandidatesData*>::iterator it = ed->saved_candidates_.begin();
694 for (; it != ed->saved_candidates_.end(); ++it) { 724 for (; it != ed->saved_candidates_.end(); ++it) {
695 main_->Post(RTC_FROM_HERE, this, MSG_ADD_CANDIDATES, *it); 725 main_->Post(RTC_FROM_HERE, this, MSG_ADD_CANDIDATES, *it);
696 } 726 }
697 ed->saved_candidates_.clear(); 727 ed->saved_candidates_.clear();
698 ed->save_candidates_ = false; 728 ed->save_candidates_ = false;
699 } 729 }
700 730
701 void OnMessage(rtc::Message* msg) { 731 void OnMessage(rtc::Message* msg) {
702 switch (msg->message_id) { 732 switch (msg->message_id) {
703 case MSG_ADD_CANDIDATES: { 733 case MSG_ADD_CANDIDATES: {
704 std::unique_ptr<CandidatesData> data( 734 std::unique_ptr<CandidatesData> data(
705 static_cast<CandidatesData*>(msg->pdata)); 735 static_cast<CandidatesData*>(msg->pdata));
706 cricket::P2PTransportChannel* rch = GetRemoteChannel(data->channel); 736 P2PTransportChannel* rch = GetRemoteChannel(data->channel);
737 if (!rch) {
738 return;
739 }
707 for (auto& c : data->candidates) { 740 for (auto& c : data->candidates) {
708 if (clear_remote_candidates_ufrag_pwd_) { 741 if (clear_remote_candidates_ufrag_pwd_) {
709 c.set_username(""); 742 c.set_username("");
710 c.set_password(""); 743 c.set_password("");
711 } 744 }
712 LOG(LS_INFO) << "Candidate(" << data->channel->component() << "->" 745 LOG(LS_INFO) << "Candidate(" << data->channel->component() << "->"
713 << rch->component() << "): " << c.ToString(); 746 << rch->component() << "): " << c.ToString();
714 rch->AddRemoteCandidate(c); 747 rch->AddRemoteCandidate(c);
715 } 748 }
716 break; 749 break;
717 } 750 }
718 case MSG_REMOVE_CANDIDATES: { 751 case MSG_REMOVE_CANDIDATES: {
719 std::unique_ptr<CandidatesData> data( 752 std::unique_ptr<CandidatesData> data(
720 static_cast<CandidatesData*>(msg->pdata)); 753 static_cast<CandidatesData*>(msg->pdata));
721 cricket::P2PTransportChannel* rch = GetRemoteChannel(data->channel); 754 P2PTransportChannel* rch = GetRemoteChannel(data->channel);
722 for (cricket::Candidate& c : data->candidates) { 755 if (!rch) {
756 return;
757 }
758 for (Candidate& c : data->candidates) {
723 LOG(LS_INFO) << "Removed remote candidate " << c.ToString(); 759 LOG(LS_INFO) << "Removed remote candidate " << c.ToString();
724 rch->RemoveRemoteCandidate(c); 760 rch->RemoveRemoteCandidate(c);
725 } 761 }
726 break; 762 break;
727 } 763 }
728 } 764 }
729 } 765 }
730 void OnReadPacket(cricket::TransportChannel* channel, const char* data, 766 void OnReadPacket(TransportChannel* channel,
731 size_t len, const rtc::PacketTime& packet_time, 767 const char* data,
768 size_t len,
769 const rtc::PacketTime& packet_time,
732 int flags) { 770 int flags) {
733 std::list<std::string>& packets = GetPacketList(channel); 771 std::list<std::string>& packets = GetPacketList(channel);
734 packets.push_front(std::string(data, len)); 772 packets.push_front(std::string(data, len));
735 } 773 }
736 void OnRoleConflict(cricket::TransportChannelImpl* channel) { 774 void OnRoleConflict(TransportChannelImpl* channel) {
737 GetEndpoint(channel)->OnRoleConflict(true); 775 GetEndpoint(channel)->OnRoleConflict(true);
738 cricket::IceRole new_role = 776 IceRole new_role = GetEndpoint(channel)->ice_role() == ICEROLE_CONTROLLING
739 GetEndpoint(channel)->ice_role() == cricket::ICEROLE_CONTROLLING ? 777 ? ICEROLE_CONTROLLED
740 cricket::ICEROLE_CONTROLLED : cricket::ICEROLE_CONTROLLING; 778 : ICEROLE_CONTROLLING;
741 channel->SetIceRole(new_role); 779 channel->SetIceRole(new_role);
742 } 780 }
743 781
744 int SendData(cricket::TransportChannel* channel, 782 int SendData(TransportChannel* channel, const char* data, size_t len) {
745 const char* data, size_t len) {
746 rtc::PacketOptions options; 783 rtc::PacketOptions options;
747 return channel->SendPacket(data, len, options, 0); 784 return channel->SendPacket(data, len, options, 0);
748 } 785 }
749 bool CheckDataOnChannel(cricket::TransportChannel* channel, 786 bool CheckDataOnChannel(TransportChannel* channel,
750 const char* data, int len) { 787 const char* data,
788 int len) {
751 return GetChannelData(channel)->CheckData(data, len); 789 return GetChannelData(channel)->CheckData(data, len);
752 } 790 }
753 static const cricket::Candidate* LocalCandidate( 791 static const Candidate* LocalCandidate(P2PTransportChannel* ch) {
754 cricket::P2PTransportChannel* ch) {
755 return (ch && ch->best_connection()) ? 792 return (ch && ch->best_connection()) ?
756 &ch->best_connection()->local_candidate() : NULL; 793 &ch->best_connection()->local_candidate() : NULL;
757 } 794 }
758 static const cricket::Candidate* RemoteCandidate( 795 static const Candidate* RemoteCandidate(P2PTransportChannel* ch) {
759 cricket::P2PTransportChannel* ch) {
760 return (ch && ch->best_connection()) ? 796 return (ch && ch->best_connection()) ?
761 &ch->best_connection()->remote_candidate() : NULL; 797 &ch->best_connection()->remote_candidate() : NULL;
762 } 798 }
763 Endpoint* GetEndpoint(cricket::TransportChannel* ch) { 799 Endpoint* GetEndpoint(TransportChannel* ch) {
764 if (ep1_.HasChannel(ch)) { 800 if (ep1_.HasChannel(ch)) {
765 return &ep1_; 801 return &ep1_;
766 } else if (ep2_.HasChannel(ch)) { 802 } else if (ep2_.HasChannel(ch)) {
767 return &ep2_; 803 return &ep2_;
768 } else { 804 } else {
769 return NULL; 805 return NULL;
770 } 806 }
771 } 807 }
772 cricket::P2PTransportChannel* GetRemoteChannel( 808 P2PTransportChannel* GetRemoteChannel(TransportChannel* ch) {
773 cricket::TransportChannel* ch) {
774 if (ch == ep1_ch1()) 809 if (ch == ep1_ch1())
775 return ep2_ch1(); 810 return ep2_ch1();
776 else if (ch == ep1_ch2()) 811 else if (ch == ep1_ch2())
777 return ep2_ch2(); 812 return ep2_ch2();
778 else if (ch == ep2_ch1()) 813 else if (ch == ep2_ch1())
779 return ep1_ch1(); 814 return ep1_ch1();
780 else if (ch == ep2_ch2()) 815 else if (ch == ep2_ch2())
781 return ep1_ch2(); 816 return ep1_ch2();
782 else 817 else
783 return NULL; 818 return NULL;
784 } 819 }
785 std::list<std::string>& GetPacketList(cricket::TransportChannel* ch) { 820 std::list<std::string>& GetPacketList(TransportChannel* ch) {
786 return GetChannelData(ch)->ch_packets_; 821 return GetChannelData(ch)->ch_packets_;
787 } 822 }
788 823
789 void set_clear_remote_candidates_ufrag_pwd(bool clear) { 824 void set_clear_remote_candidates_ufrag_pwd(bool clear) {
790 clear_remote_candidates_ufrag_pwd_ = clear; 825 clear_remote_candidates_ufrag_pwd_ = clear;
791 } 826 }
792 827
793 void set_force_relay(bool relay) { 828 void set_force_relay(bool relay) {
794 force_relay_ = relay; 829 force_relay_ = relay;
795 } 830 }
796 831
797 private: 832 private:
798 rtc::Thread* main_; 833 rtc::Thread* main_;
799 std::unique_ptr<rtc::PhysicalSocketServer> pss_; 834 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
800 std::unique_ptr<rtc::VirtualSocketServer> vss_; 835 std::unique_ptr<rtc::VirtualSocketServer> vss_;
801 std::unique_ptr<rtc::NATSocketServer> nss_; 836 std::unique_ptr<rtc::NATSocketServer> nss_;
802 std::unique_ptr<rtc::FirewallSocketServer> ss_; 837 std::unique_ptr<rtc::FirewallSocketServer> ss_;
803 rtc::SocketServerScope ss_scope_; 838 rtc::SocketServerScope ss_scope_;
804 std::unique_ptr<cricket::TestStunServer> stun_server_; 839 std::unique_ptr<TestStunServer> stun_server_;
805 cricket::TestTurnServer turn_server_; 840 TestTurnServer turn_server_;
806 cricket::TestRelayServer relay_server_; 841 TestRelayServer relay_server_;
807 rtc::SocksProxyServer socks_server1_; 842 rtc::SocksProxyServer socks_server1_;
808 rtc::SocksProxyServer socks_server2_; 843 rtc::SocksProxyServer socks_server2_;
809 Endpoint ep1_; 844 Endpoint ep1_;
810 Endpoint ep2_; 845 Endpoint ep2_;
811 bool clear_remote_candidates_ufrag_pwd_; 846 bool clear_remote_candidates_ufrag_pwd_;
812 bool force_relay_; 847 bool force_relay_;
813 }; 848 };
814 849
815 // The tests have only a few outcomes, which we predefine. 850 // The tests have only a few outcomes, which we predefine.
816 const P2PTransportChannelTestBase::Result P2PTransportChannelTestBase:: 851 const P2PTransportChannelTestBase::Result P2PTransportChannelTestBase::
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
854 // Just test every combination of the configs in the Config enum. 889 // Just test every combination of the configs in the Config enum.
855 class P2PTransportChannelTest : public P2PTransportChannelTestBase { 890 class P2PTransportChannelTest : public P2PTransportChannelTestBase {
856 protected: 891 protected:
857 static const Result* kMatrix[NUM_CONFIGS][NUM_CONFIGS]; 892 static const Result* kMatrix[NUM_CONFIGS][NUM_CONFIGS];
858 void ConfigureEndpoints(Config config1, 893 void ConfigureEndpoints(Config config1,
859 Config config2, 894 Config config2,
860 int allocator_flags1, 895 int allocator_flags1,
861 int allocator_flags2) { 896 int allocator_flags2) {
862 ServerAddresses stun_servers; 897 ServerAddresses stun_servers;
863 stun_servers.insert(kStunAddr); 898 stun_servers.insert(kStunAddr);
864 GetEndpoint(0)->allocator_.reset( 899 GetEndpoint(0)->allocator_.reset(new BasicPortAllocator(
865 new cricket::BasicPortAllocator(&(GetEndpoint(0)->network_manager_), 900 &(GetEndpoint(0)->network_manager_), stun_servers, rtc::SocketAddress(),
866 stun_servers, 901 rtc::SocketAddress(), rtc::SocketAddress()));
867 rtc::SocketAddress(), rtc::SocketAddress(), 902 GetEndpoint(1)->allocator_.reset(new BasicPortAllocator(
868 rtc::SocketAddress())); 903 &(GetEndpoint(1)->network_manager_), stun_servers, rtc::SocketAddress(),
869 GetEndpoint(1)->allocator_.reset( 904 rtc::SocketAddress(), rtc::SocketAddress()));
870 new cricket::BasicPortAllocator(&(GetEndpoint(1)->network_manager_),
871 stun_servers,
872 rtc::SocketAddress(), rtc::SocketAddress(),
873 rtc::SocketAddress()));
874 905
875 cricket::RelayServerConfig turn_server(cricket::RELAY_TURN); 906 RelayServerConfig turn_server(RELAY_TURN);
876 turn_server.credentials = kRelayCredentials; 907 turn_server.credentials = kRelayCredentials;
877 turn_server.ports.push_back( 908 turn_server.ports.push_back(
878 cricket::ProtocolAddress(kTurnUdpIntAddr, cricket::PROTO_UDP, false)); 909 ProtocolAddress(kTurnUdpIntAddr, PROTO_UDP, false));
879 GetEndpoint(0)->allocator_->AddTurnServer(turn_server); 910 GetEndpoint(0)->allocator_->AddTurnServer(turn_server);
880 GetEndpoint(1)->allocator_->AddTurnServer(turn_server); 911 GetEndpoint(1)->allocator_->AddTurnServer(turn_server);
881 912
882 int delay = kMinimumStepDelay; 913 int delay = kMinimumStepDelay;
883 ConfigureEndpoint(0, config1); 914 ConfigureEndpoint(0, config1);
884 SetAllocatorFlags(0, allocator_flags1); 915 SetAllocatorFlags(0, allocator_flags1);
885 SetAllocationStepDelay(0, delay); 916 SetAllocationStepDelay(0, delay);
886 ConfigureEndpoint(1, config2); 917 ConfigureEndpoint(1, config2);
887 SetAllocatorFlags(1, allocator_flags2); 918 SetAllocatorFlags(1, allocator_flags2);
888 SetAllocationStepDelay(1, delay); 919 SetAllocationStepDelay(1, delay);
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
1069 1100
1070 // Test the operation of GetStats. 1101 // Test the operation of GetStats.
1071 TEST_F(P2PTransportChannelTest, GetStats) { 1102 TEST_F(P2PTransportChannelTest, GetStats) {
1072 ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, 1103 ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags,
1073 kDefaultPortAllocatorFlags); 1104 kDefaultPortAllocatorFlags);
1074 CreateChannels(1); 1105 CreateChannels(1);
1075 EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() && 1106 EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
1076 ep2_ch1()->receiving() && ep2_ch1()->writable(), 1107 ep2_ch1()->receiving() && ep2_ch1()->writable(),
1077 1000, 1000); 1108 1000, 1000);
1078 TestSendRecv(1); 1109 TestSendRecv(1);
1079 cricket::ConnectionInfos infos; 1110 ConnectionInfos infos;
1080 ASSERT_TRUE(ep1_ch1()->GetStats(&infos)); 1111 ASSERT_TRUE(ep1_ch1()->GetStats(&infos));
1081 ASSERT_TRUE(infos.size() >= 1); 1112 ASSERT_TRUE(infos.size() >= 1);
1082 cricket::ConnectionInfo* best_conn_info = nullptr; 1113 ConnectionInfo* best_conn_info = nullptr;
1083 for (cricket::ConnectionInfo& info : infos) { 1114 for (ConnectionInfo& info : infos) {
1084 if (info.best_connection) { 1115 if (info.best_connection) {
1085 best_conn_info = &info; 1116 best_conn_info = &info;
1086 break; 1117 break;
1087 } 1118 }
1088 } 1119 }
1089 ASSERT_TRUE(best_conn_info != nullptr); 1120 ASSERT_TRUE(best_conn_info != nullptr);
1090 EXPECT_TRUE(best_conn_info->new_connection); 1121 EXPECT_TRUE(best_conn_info->new_connection);
1091 EXPECT_TRUE(best_conn_info->receiving); 1122 EXPECT_TRUE(best_conn_info->receiving);
1092 EXPECT_TRUE(best_conn_info->writable); 1123 EXPECT_TRUE(best_conn_info->writable);
1093 EXPECT_FALSE(best_conn_info->timeout); 1124 EXPECT_FALSE(best_conn_info->timeout);
(...skipping 15 matching lines...) Expand all
1109 CreateChannels(1); 1140 CreateChannels(1);
1110 // Only have remote credentials come in for ep2, not ep1. 1141 // Only have remote credentials come in for ep2, not ep1.
1111 ep2_ch1()->SetRemoteIceCredentials(kIceUfrag[0], kIcePwd[0]); 1142 ep2_ch1()->SetRemoteIceCredentials(kIceUfrag[0], kIcePwd[0]);
1112 1143
1113 // Pause sending ep2's candidates to ep1 until ep1 receives the peer reflexive 1144 // Pause sending ep2's candidates to ep1 until ep1 receives the peer reflexive
1114 // candidate. 1145 // candidate.
1115 PauseCandidates(1); 1146 PauseCandidates(1);
1116 1147
1117 // The caller should have the best connection connected to the peer reflexive 1148 // The caller should have the best connection connected to the peer reflexive
1118 // candidate. 1149 // candidate.
1119 const cricket::Connection* best_connection = NULL; 1150 const Connection* best_connection = NULL;
1120 WAIT((best_connection = ep1_ch1()->best_connection()) != NULL, 2000); 1151 WAIT((best_connection = ep1_ch1()->best_connection()) != NULL, 2000);
1121 EXPECT_EQ("prflx", ep1_ch1()->best_connection()->remote_candidate().type()); 1152 EXPECT_EQ("prflx", ep1_ch1()->best_connection()->remote_candidate().type());
1122 1153
1123 // Because we don't have a remote pwd, we don't ping yet. 1154 // Because we don't have a remote pwd, we don't ping yet.
1124 EXPECT_EQ(kIceUfrag[1], 1155 EXPECT_EQ(kIceUfrag[1],
1125 ep1_ch1()->best_connection()->remote_candidate().username()); 1156 ep1_ch1()->best_connection()->remote_candidate().username());
1126 EXPECT_EQ("", ep1_ch1()->best_connection()->remote_candidate().password()); 1157 EXPECT_EQ("", ep1_ch1()->best_connection()->remote_candidate().password());
1127 // Because we don't have ICE credentials yet, we don't know the generation. 1158 // Because we don't have ICE credentials yet, we don't know the generation.
1128 EXPECT_EQ(0u, ep1_ch1()->best_connection()->remote_candidate().generation()); 1159 EXPECT_EQ(0u, ep1_ch1()->best_connection()->remote_candidate().generation());
1129 EXPECT_TRUE(nullptr == ep1_ch1()->FindNextPingableConnection()); 1160 EXPECT_TRUE(nullptr == ep1_ch1()->FindNextPingableConnection());
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1183 ep1_ch1()->SetRemoteIceCredentials(kIceUfrag[3], kIcePwd[3]); 1214 ep1_ch1()->SetRemoteIceCredentials(kIceUfrag[3], kIcePwd[3]);
1184 ep1_ch1()->SetRemoteIceCredentials(kIceUfrag[1], kIcePwd[1]); 1215 ep1_ch1()->SetRemoteIceCredentials(kIceUfrag[1], kIcePwd[1]);
1185 // After setting the remote ICE credentials, the password and generation 1216 // After setting the remote ICE credentials, the password and generation
1186 // of the peer reflexive candidate should be updated. 1217 // of the peer reflexive candidate should be updated.
1187 EXPECT_EQ(kIcePwd[1], 1218 EXPECT_EQ(kIcePwd[1],
1188 ep1_ch1()->best_connection()->remote_candidate().password()); 1219 ep1_ch1()->best_connection()->remote_candidate().password());
1189 EXPECT_EQ(1u, ep1_ch1()->best_connection()->remote_candidate().generation()); 1220 EXPECT_EQ(1u, ep1_ch1()->best_connection()->remote_candidate().generation());
1190 1221
1191 ResumeCandidates(1); 1222 ResumeCandidates(1);
1192 1223
1193 const cricket::Connection* best_connection = NULL; 1224 const Connection* best_connection = NULL;
1194 WAIT((best_connection = ep2_ch1()->best_connection()) != NULL, 2000); 1225 WAIT((best_connection = ep2_ch1()->best_connection()) != NULL, 2000);
1195 1226
1196 // Wait to verify the connection is not culled. 1227 // Wait to verify the connection is not culled.
1197 WAIT(ep1_ch1()->writable(), 2000); 1228 WAIT(ep1_ch1()->writable(), 2000);
1198 EXPECT_EQ(ep2_ch1()->best_connection(), best_connection); 1229 EXPECT_EQ(ep2_ch1()->best_connection(), best_connection);
1199 EXPECT_EQ("prflx", ep1_ch1()->best_connection()->remote_candidate().type()); 1230 EXPECT_EQ("prflx", ep1_ch1()->best_connection()->remote_candidate().type());
1200 DestroyChannels(); 1231 DestroyChannels();
1201 } 1232 }
1202 1233
1203 // Test that if remote candidates don't have ufrag and pwd, we still work. 1234 // Test that if remote candidates don't have ufrag and pwd, we still work.
1204 TEST_F(P2PTransportChannelTest, RemoteCandidatesWithoutUfragPwd) { 1235 TEST_F(P2PTransportChannelTest, RemoteCandidatesWithoutUfragPwd) {
1205 set_clear_remote_candidates_ufrag_pwd(true); 1236 set_clear_remote_candidates_ufrag_pwd(true);
1206 ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, 1237 ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags,
1207 kDefaultPortAllocatorFlags); 1238 kDefaultPortAllocatorFlags);
1208 CreateChannels(1); 1239 CreateChannels(1);
1209 const cricket::Connection* best_connection = NULL; 1240 const Connection* best_connection = NULL;
1210 // Wait until the callee's connections are created. 1241 // Wait until the callee's connections are created.
1211 WAIT((best_connection = ep2_ch1()->best_connection()) != NULL, 1000); 1242 WAIT((best_connection = ep2_ch1()->best_connection()) != NULL, 1000);
1212 // Wait to see if they get culled; they shouldn't. 1243 // Wait to see if they get culled; they shouldn't.
1213 WAIT(ep2_ch1()->best_connection() != best_connection, 1000); 1244 WAIT(ep2_ch1()->best_connection() != best_connection, 1000);
1214 EXPECT_TRUE(ep2_ch1()->best_connection() == best_connection); 1245 EXPECT_TRUE(ep2_ch1()->best_connection() == best_connection);
1215 DestroyChannels(); 1246 DestroyChannels();
1216 } 1247 }
1217 1248
1218 // Test that a host behind NAT cannot be reached when incoming_only 1249 // Test that a host behind NAT cannot be reached when incoming_only
1219 // is set to true. 1250 // is set to true.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1254 DestroyChannels(); 1285 DestroyChannels();
1255 } 1286 }
1256 1287
1257 TEST_F(P2PTransportChannelTest, TestTcpConnectionsFromActiveToPassive) { 1288 TEST_F(P2PTransportChannelTest, TestTcpConnectionsFromActiveToPassive) {
1258 AddAddress(0, kPublicAddrs[0]); 1289 AddAddress(0, kPublicAddrs[0]);
1259 AddAddress(1, kPublicAddrs[1]); 1290 AddAddress(1, kPublicAddrs[1]);
1260 1291
1261 SetAllocationStepDelay(0, kMinimumStepDelay); 1292 SetAllocationStepDelay(0, kMinimumStepDelay);
1262 SetAllocationStepDelay(1, kMinimumStepDelay); 1293 SetAllocationStepDelay(1, kMinimumStepDelay);
1263 1294
1264 int kOnlyLocalTcpPorts = cricket::PORTALLOCATOR_DISABLE_UDP | 1295 int kOnlyLocalTcpPorts = PORTALLOCATOR_DISABLE_UDP |
1265 cricket::PORTALLOCATOR_DISABLE_STUN | 1296 PORTALLOCATOR_DISABLE_STUN |
1266 cricket::PORTALLOCATOR_DISABLE_RELAY; 1297 PORTALLOCATOR_DISABLE_RELAY;
1267 // Disable all protocols except TCP. 1298 // Disable all protocols except TCP.
1268 SetAllocatorFlags(0, kOnlyLocalTcpPorts); 1299 SetAllocatorFlags(0, kOnlyLocalTcpPorts);
1269 SetAllocatorFlags(1, kOnlyLocalTcpPorts); 1300 SetAllocatorFlags(1, kOnlyLocalTcpPorts);
1270 1301
1271 SetAllowTcpListen(0, true); // actpass. 1302 SetAllowTcpListen(0, true); // actpass.
1272 SetAllowTcpListen(1, false); // active. 1303 SetAllowTcpListen(1, false); // active.
1273 1304
1274 // Pause candidate so we could verify the candidate properties. 1305 // Pause candidate so we could verify the candidate properties.
1275 PauseCandidates(0); 1306 PauseCandidates(0);
1276 PauseCandidates(1); 1307 PauseCandidates(1);
1277 CreateChannels(1); 1308 CreateChannels(1);
1278 1309
1279 // Verify tcp candidates. 1310 // Verify tcp candidates.
1280 VerifySavedTcpCandidates(0, cricket::TCPTYPE_PASSIVE_STR); 1311 VerifySavedTcpCandidates(0, TCPTYPE_PASSIVE_STR);
1281 VerifySavedTcpCandidates(1, cricket::TCPTYPE_ACTIVE_STR); 1312 VerifySavedTcpCandidates(1, TCPTYPE_ACTIVE_STR);
1282 1313
1283 // Resume candidates. 1314 // Resume candidates.
1284 ResumeCandidates(0); 1315 ResumeCandidates(0);
1285 ResumeCandidates(1); 1316 ResumeCandidates(1);
1286 1317
1287 EXPECT_TRUE_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && 1318 EXPECT_TRUE_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
1288 ep2_ch1()->receiving() && ep2_ch1()->writable(), 1319 ep2_ch1()->receiving() && ep2_ch1()->writable(),
1289 1000); 1320 1000);
1290 EXPECT_TRUE( 1321 EXPECT_TRUE(
1291 ep1_ch1()->best_connection() && ep2_ch1()->best_connection() && 1322 ep1_ch1()->best_connection() && ep2_ch1()->best_connection() &&
1292 LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && 1323 LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) &&
1293 RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); 1324 RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1]));
1294 1325
1295 TestSendRecv(1); 1326 TestSendRecv(1);
1296 DestroyChannels(); 1327 DestroyChannels();
1297 } 1328 }
1298 1329
1299 TEST_F(P2PTransportChannelTest, TestIceRoleConflict) { 1330 TEST_F(P2PTransportChannelTest, TestIceRoleConflict) {
1300 AddAddress(0, kPublicAddrs[0]); 1331 AddAddress(0, kPublicAddrs[0]);
1301 AddAddress(1, kPublicAddrs[1]); 1332 AddAddress(1, kPublicAddrs[1]);
1302 TestSignalRoleConflict(); 1333 TestSignalRoleConflict();
1303 } 1334 }
1304 1335
1305 // Tests that the ice configs (protocol, tiebreaker and role) can be passed 1336 // Tests that the ice configs (protocol, tiebreaker and role) can be passed
1306 // down to ports. 1337 // down to ports.
1307 TEST_F(P2PTransportChannelTest, TestIceConfigWillPassDownToPort) { 1338 TEST_F(P2PTransportChannelTest, TestIceConfigWillPassDownToPort) {
1308 AddAddress(0, kPublicAddrs[0]); 1339 AddAddress(0, kPublicAddrs[0]);
1309 AddAddress(1, kPublicAddrs[1]); 1340 AddAddress(1, kPublicAddrs[1]);
1310 1341
1311 SetIceRole(0, cricket::ICEROLE_CONTROLLING); 1342 SetIceRole(0, ICEROLE_CONTROLLING);
1312 SetIceTiebreaker(0, kTiebreaker1); 1343 SetIceTiebreaker(0, kTiebreaker1);
1313 SetIceRole(1, cricket::ICEROLE_CONTROLLING); 1344 SetIceRole(1, ICEROLE_CONTROLLING);
1314 SetIceTiebreaker(1, kTiebreaker2); 1345 SetIceTiebreaker(1, kTiebreaker2);
1315 1346
1316 CreateChannels(1); 1347 CreateChannels(1);
1317 1348
1318 EXPECT_EQ_WAIT(2u, ep1_ch1()->ports().size(), 1000); 1349 EXPECT_EQ_WAIT(2u, ep1_ch1()->ports().size(), 1000);
1319 1350
1320 const std::vector<cricket::PortInterface *> ports_before = ep1_ch1()->ports(); 1351 const std::vector<PortInterface*> ports_before = ep1_ch1()->ports();
1321 for (size_t i = 0; i < ports_before.size(); ++i) { 1352 for (size_t i = 0; i < ports_before.size(); ++i) {
1322 EXPECT_EQ(cricket::ICEROLE_CONTROLLING, ports_before[i]->GetIceRole()); 1353 EXPECT_EQ(ICEROLE_CONTROLLING, ports_before[i]->GetIceRole());
1323 EXPECT_EQ(kTiebreaker1, ports_before[i]->IceTiebreaker()); 1354 EXPECT_EQ(kTiebreaker1, ports_before[i]->IceTiebreaker());
1324 } 1355 }
1325 1356
1326 ep1_ch1()->SetIceRole(cricket::ICEROLE_CONTROLLED); 1357 ep1_ch1()->SetIceRole(ICEROLE_CONTROLLED);
1327 ep1_ch1()->SetIceTiebreaker(kTiebreaker2); 1358 ep1_ch1()->SetIceTiebreaker(kTiebreaker2);
1328 1359
1329 const std::vector<cricket::PortInterface *> ports_after = ep1_ch1()->ports(); 1360 const std::vector<PortInterface*> ports_after = ep1_ch1()->ports();
1330 for (size_t i = 0; i < ports_after.size(); ++i) { 1361 for (size_t i = 0; i < ports_after.size(); ++i) {
1331 EXPECT_EQ(cricket::ICEROLE_CONTROLLED, ports_before[i]->GetIceRole()); 1362 EXPECT_EQ(ICEROLE_CONTROLLED, ports_before[i]->GetIceRole());
pthatcher1 2016/06/15 19:12:47 Could we split this CL in 2, one for doing this cl
1332 // SetIceTiebreaker after Connect() has been called will fail. So expect the 1363 // SetIceTiebreaker after Connect() has been called will fail. So expect the
1333 // original value. 1364 // original value.
1334 EXPECT_EQ(kTiebreaker1, ports_before[i]->IceTiebreaker()); 1365 EXPECT_EQ(kTiebreaker1, ports_before[i]->IceTiebreaker());
1335 } 1366 }
1336 1367
1337 EXPECT_TRUE_WAIT(ep1_ch1()->receiving() && 1368 EXPECT_TRUE_WAIT(ep1_ch1()->receiving() &&
1338 ep1_ch1()->writable() && 1369 ep1_ch1()->writable() &&
1339 ep2_ch1()->receiving() && 1370 ep2_ch1()->receiving() &&
1340 ep2_ch1()->writable(), 1371 ep2_ch1()->writable(),
1341 1000); 1372 1000);
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1379 TEST_F(P2PTransportChannelTest, TestIPv6Connections) { 1410 TEST_F(P2PTransportChannelTest, TestIPv6Connections) {
1380 AddAddress(0, kIPv6PublicAddrs[0]); 1411 AddAddress(0, kIPv6PublicAddrs[0]);
1381 AddAddress(0, kPublicAddrs[0]); 1412 AddAddress(0, kPublicAddrs[0]);
1382 AddAddress(1, kIPv6PublicAddrs[1]); 1413 AddAddress(1, kIPv6PublicAddrs[1]);
1383 AddAddress(1, kPublicAddrs[1]); 1414 AddAddress(1, kPublicAddrs[1]);
1384 1415
1385 SetAllocationStepDelay(0, kMinimumStepDelay); 1416 SetAllocationStepDelay(0, kMinimumStepDelay);
1386 SetAllocationStepDelay(1, kMinimumStepDelay); 1417 SetAllocationStepDelay(1, kMinimumStepDelay);
1387 1418
1388 // Enable IPv6 1419 // Enable IPv6
1389 SetAllocatorFlags(0, cricket::PORTALLOCATOR_ENABLE_IPV6); 1420 SetAllocatorFlags(0, PORTALLOCATOR_ENABLE_IPV6);
1390 SetAllocatorFlags(1, cricket::PORTALLOCATOR_ENABLE_IPV6); 1421 SetAllocatorFlags(1, PORTALLOCATOR_ENABLE_IPV6);
1391 1422
1392 CreateChannels(1); 1423 CreateChannels(1);
1393 1424
1394 EXPECT_TRUE_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && 1425 EXPECT_TRUE_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
1395 ep2_ch1()->receiving() && ep2_ch1()->writable(), 1426 ep2_ch1()->receiving() && ep2_ch1()->writable(),
1396 1000); 1427 1000);
1397 EXPECT_TRUE( 1428 EXPECT_TRUE(
1398 ep1_ch1()->best_connection() && ep2_ch1()->best_connection() && 1429 ep1_ch1()->best_connection() && ep2_ch1()->best_connection() &&
1399 LocalCandidate(ep1_ch1())->address().EqualIPs(kIPv6PublicAddrs[0]) && 1430 LocalCandidate(ep1_ch1())->address().EqualIPs(kIPv6PublicAddrs[0]) &&
1400 RemoteCandidate(ep1_ch1())->address().EqualIPs(kIPv6PublicAddrs[1])); 1431 RemoteCandidate(ep1_ch1())->address().EqualIPs(kIPv6PublicAddrs[1]));
1401 1432
1402 TestSendRecv(1); 1433 TestSendRecv(1);
1403 DestroyChannels(); 1434 DestroyChannels();
1404 } 1435 }
1405 1436
1406 // Testing forceful TURN connections. 1437 // Testing forceful TURN connections.
1407 TEST_F(P2PTransportChannelTest, TestForceTurn) { 1438 TEST_F(P2PTransportChannelTest, TestForceTurn) {
1408 ConfigureEndpoints( 1439 ConfigureEndpoints(
1409 NAT_PORT_RESTRICTED, NAT_SYMMETRIC, 1440 NAT_PORT_RESTRICTED, NAT_SYMMETRIC,
1410 kDefaultPortAllocatorFlags | cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET, 1441 kDefaultPortAllocatorFlags | PORTALLOCATOR_ENABLE_SHARED_SOCKET,
1411 kDefaultPortAllocatorFlags | cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET); 1442 kDefaultPortAllocatorFlags | PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1412 set_force_relay(true); 1443 set_force_relay(true);
1413 1444
1414 SetAllocationStepDelay(0, kMinimumStepDelay); 1445 SetAllocationStepDelay(0, kMinimumStepDelay);
1415 SetAllocationStepDelay(1, kMinimumStepDelay); 1446 SetAllocationStepDelay(1, kMinimumStepDelay);
1416 1447
1417 CreateChannels(1); 1448 CreateChannels(1);
1418 1449
1419 EXPECT_TRUE_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && 1450 EXPECT_TRUE_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
1420 ep2_ch1()->receiving() && ep2_ch1()->writable(), 1451 ep2_ch1()->receiving() && ep2_ch1()->writable(),
1421 2000); 1452 2000);
(...skipping 11 matching lines...) Expand all
1433 } 1464 }
1434 1465
1435 // Test that if continual gathering is set to true, ICE gathering state will 1466 // Test that if continual gathering is set to true, ICE gathering state will
1436 // not change to "Complete", and vice versa. 1467 // not change to "Complete", and vice versa.
1437 TEST_F(P2PTransportChannelTest, TestContinualGathering) { 1468 TEST_F(P2PTransportChannelTest, TestContinualGathering) {
1438 ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, 1469 ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags,
1439 kDefaultPortAllocatorFlags); 1470 kDefaultPortAllocatorFlags);
1440 SetAllocationStepDelay(0, kDefaultStepDelay); 1471 SetAllocationStepDelay(0, kDefaultStepDelay);
1441 SetAllocationStepDelay(1, kDefaultStepDelay); 1472 SetAllocationStepDelay(1, kDefaultStepDelay);
1442 CreateChannels(1); 1473 CreateChannels(1);
1443 cricket::IceConfig config = CreateIceConfig(1000, true); 1474 IceConfig config = CreateIceConfig(1000, true);
1444 ep1_ch1()->SetIceConfig(config); 1475 ep1_ch1()->SetIceConfig(config);
1445 // By default, ep2 does not gather continually. 1476 // By default, ep2 does not gather continually.
1446 1477
1447 EXPECT_TRUE_WAIT_MARGIN(ep1_ch1() != NULL && ep2_ch1() != NULL && 1478 EXPECT_TRUE_WAIT_MARGIN(ep1_ch1() != NULL && ep2_ch1() != NULL &&
1448 ep1_ch1()->receiving() && ep1_ch1()->writable() && 1479 ep1_ch1()->receiving() && ep1_ch1()->writable() &&
1449 ep2_ch1()->receiving() && ep2_ch1()->writable(), 1480 ep2_ch1()->receiving() && ep2_ch1()->writable(),
1450 1000, 1000); 1481 1000, 1000);
1451 WAIT(cricket::IceGatheringState::kIceGatheringComplete == 1482 WAIT(IceGatheringState::kIceGatheringComplete == ep1_ch1()->gathering_state(),
1452 ep1_ch1()->gathering_state(),
1453 1000); 1483 1000);
1454 EXPECT_EQ(cricket::IceGatheringState::kIceGatheringGathering, 1484 EXPECT_EQ(IceGatheringState::kIceGatheringGathering,
1455 ep1_ch1()->gathering_state()); 1485 ep1_ch1()->gathering_state());
1456 // By now, ep2 should have completed gathering. 1486 // By now, ep2 should have completed gathering.
1457 EXPECT_EQ(cricket::IceGatheringState::kIceGatheringComplete, 1487 EXPECT_EQ(IceGatheringState::kIceGatheringComplete,
1458 ep2_ch1()->gathering_state()); 1488 ep2_ch1()->gathering_state());
1459 1489
1460 DestroyChannels(); 1490 DestroyChannels();
1461 } 1491 }
1462 1492
1463 // Test that a connection succeeds when the P2PTransportChannel uses a pooled 1493 // Test that a connection succeeds when the P2PTransportChannel uses a pooled
1464 // PortAllocatorSession that has not yet finished gathering candidates. 1494 // PortAllocatorSession that has not yet finished gathering candidates.
1465 TEST_F(P2PTransportChannelTest, TestUsingPooledSessionBeforeDoneGathering) { 1495 TEST_F(P2PTransportChannelTest, TestUsingPooledSessionBeforeDoneGathering) {
1466 ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, 1496 ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags,
1467 kDefaultPortAllocatorFlags); 1497 kDefaultPortAllocatorFlags);
1468 // First create a pooled session for each endpoint. 1498 // First create a pooled session for each endpoint.
1469 auto& allocator_1 = GetEndpoint(0)->allocator_; 1499 auto& allocator_1 = GetEndpoint(0)->allocator_;
1470 auto& allocator_2 = GetEndpoint(1)->allocator_; 1500 auto& allocator_2 = GetEndpoint(1)->allocator_;
1471 int pool_size = 1; 1501 int pool_size = 1;
1472 allocator_1->SetConfiguration(allocator_1->stun_servers(), 1502 allocator_1->SetConfiguration(allocator_1->stun_servers(),
1473 allocator_1->turn_servers(), pool_size); 1503 allocator_1->turn_servers(), pool_size);
1474 allocator_2->SetConfiguration(allocator_2->stun_servers(), 1504 allocator_2->SetConfiguration(allocator_2->stun_servers(),
1475 allocator_2->turn_servers(), pool_size); 1505 allocator_2->turn_servers(), pool_size);
1476 const cricket::PortAllocatorSession* pooled_session_1 = 1506 const PortAllocatorSession* pooled_session_1 =
1477 allocator_1->GetPooledSession(); 1507 allocator_1->GetPooledSession();
1478 const cricket::PortAllocatorSession* pooled_session_2 = 1508 const PortAllocatorSession* pooled_session_2 =
1479 allocator_2->GetPooledSession(); 1509 allocator_2->GetPooledSession();
1480 ASSERT_NE(nullptr, pooled_session_1); 1510 ASSERT_NE(nullptr, pooled_session_1);
1481 ASSERT_NE(nullptr, pooled_session_2); 1511 ASSERT_NE(nullptr, pooled_session_2);
1482 // Sanity check that pooled sessions haven't gathered anything yet. 1512 // Sanity check that pooled sessions haven't gathered anything yet.
1483 EXPECT_TRUE(pooled_session_1->ReadyPorts().empty()); 1513 EXPECT_TRUE(pooled_session_1->ReadyPorts().empty());
1484 EXPECT_TRUE(pooled_session_1->ReadyCandidates().empty()); 1514 EXPECT_TRUE(pooled_session_1->ReadyCandidates().empty());
1485 EXPECT_TRUE(pooled_session_2->ReadyPorts().empty()); 1515 EXPECT_TRUE(pooled_session_2->ReadyPorts().empty());
1486 EXPECT_TRUE(pooled_session_2->ReadyCandidates().empty()); 1516 EXPECT_TRUE(pooled_session_2->ReadyCandidates().empty());
1487 // Now let the endpoints connect and try exchanging some data. 1517 // Now let the endpoints connect and try exchanging some data.
1488 CreateChannels(1); 1518 CreateChannels(1);
(...skipping 20 matching lines...) Expand all
1509 ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, 1539 ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags,
1510 kDefaultPortAllocatorFlags); 1540 kDefaultPortAllocatorFlags);
1511 // First create a pooled session for each endpoint. 1541 // First create a pooled session for each endpoint.
1512 auto& allocator_1 = GetEndpoint(0)->allocator_; 1542 auto& allocator_1 = GetEndpoint(0)->allocator_;
1513 auto& allocator_2 = GetEndpoint(1)->allocator_; 1543 auto& allocator_2 = GetEndpoint(1)->allocator_;
1514 int pool_size = 1; 1544 int pool_size = 1;
1515 allocator_1->SetConfiguration(allocator_1->stun_servers(), 1545 allocator_1->SetConfiguration(allocator_1->stun_servers(),
1516 allocator_1->turn_servers(), pool_size); 1546 allocator_1->turn_servers(), pool_size);
1517 allocator_2->SetConfiguration(allocator_2->stun_servers(), 1547 allocator_2->SetConfiguration(allocator_2->stun_servers(),
1518 allocator_2->turn_servers(), pool_size); 1548 allocator_2->turn_servers(), pool_size);
1519 const cricket::PortAllocatorSession* pooled_session_1 = 1549 const PortAllocatorSession* pooled_session_1 =
1520 allocator_1->GetPooledSession(); 1550 allocator_1->GetPooledSession();
1521 const cricket::PortAllocatorSession* pooled_session_2 = 1551 const PortAllocatorSession* pooled_session_2 =
1522 allocator_2->GetPooledSession(); 1552 allocator_2->GetPooledSession();
1523 ASSERT_NE(nullptr, pooled_session_1); 1553 ASSERT_NE(nullptr, pooled_session_1);
1524 ASSERT_NE(nullptr, pooled_session_2); 1554 ASSERT_NE(nullptr, pooled_session_2);
1525 // Wait for the pooled sessions to finish gathering before the 1555 // Wait for the pooled sessions to finish gathering before the
1526 // P2PTransportChannels try to use them. 1556 // P2PTransportChannels try to use them.
1527 EXPECT_TRUE_WAIT(pooled_session_1->CandidatesAllocationDone() && 1557 EXPECT_TRUE_WAIT(pooled_session_1->CandidatesAllocationDone() &&
1528 pooled_session_2->CandidatesAllocationDone(), 1558 pooled_session_2->CandidatesAllocationDone(),
1529 kDefaultTimeout); 1559 kDefaultTimeout);
1530 // Now let the endpoints connect and try exchanging some data. 1560 // Now let the endpoints connect and try exchanging some data.
1531 CreateChannels(1); 1561 CreateChannels(1);
1532 EXPECT_TRUE_WAIT_MARGIN(ep1_ch1() != NULL && ep2_ch1() != NULL && 1562 EXPECT_TRUE_WAIT_MARGIN(ep1_ch1() != NULL && ep2_ch1() != NULL &&
1533 ep1_ch1()->receiving() && ep1_ch1()->writable() && 1563 ep1_ch1()->receiving() && ep1_ch1()->writable() &&
1534 ep2_ch1()->receiving() && ep2_ch1()->writable(), 1564 ep2_ch1()->receiving() && ep2_ch1()->writable(),
1535 1000, 1000); 1565 1000, 1000);
1536 TestSendRecv(1); 1566 TestSendRecv(1);
1537 // Make sure the P2PTransportChannels are actually using ports from the 1567 // Make sure the P2PTransportChannels are actually using ports from the
1538 // pooled sessions. 1568 // pooled sessions.
1539 auto pooled_ports_1 = pooled_session_1->ReadyPorts(); 1569 auto pooled_ports_1 = pooled_session_1->ReadyPorts();
1540 auto pooled_ports_2 = pooled_session_2->ReadyPorts(); 1570 auto pooled_ports_2 = pooled_session_2->ReadyPorts();
1541 EXPECT_NE(pooled_ports_1.end(), 1571 EXPECT_NE(pooled_ports_1.end(),
1542 std::find(pooled_ports_1.begin(), pooled_ports_1.end(), 1572 std::find(pooled_ports_1.begin(), pooled_ports_1.end(),
1543 ep1_ch1()->best_connection()->port())); 1573 ep1_ch1()->best_connection()->port()));
1544 EXPECT_NE(pooled_ports_2.end(), 1574 EXPECT_NE(pooled_ports_2.end(),
1545 std::find(pooled_ports_2.begin(), pooled_ports_2.end(), 1575 std::find(pooled_ports_2.begin(), pooled_ports_2.end(),
1546 ep2_ch1()->best_connection()->port())); 1576 ep2_ch1()->best_connection()->port()));
1547 } 1577 }
1548 1578
1579 // Test that when the "turn_to_turn_createpermission_needed" flag is set to
1580 // false and there's a TURN-TURN candidate pair, it's assumed to be writable
pthatcher1 2016/06/15 19:12:47 I think it's more "presumed" than "assumed": http
Taylor Brandstetter 2016/06/16 00:13:41 Done. I think you take more care to make sure your
1581 // as soon as it's created.
1582 TEST_F(P2PTransportChannelTest, TestTurnToTurnProbablyWritable) {
1583 ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags,
1584 kDefaultPortAllocatorFlags);
1585 // Only configure one channel so we can control when the remote candidate
1586 // is added.
1587 GetEndpoint(0)->cd1_.ch_.reset(
1588 CreateChannel(0, ICE_CANDIDATE_COMPONENT_DEFAULT, kIceUfrag[0],
1589 kIcePwd[0], kIceUfrag[1], kIcePwd[1]));
1590 IceConfig config;
1591 config.turn_to_turn_createpermission_needed = false;
1592 ep1_ch1()->SetIceConfig(config);
1593 ep1_ch1()->MaybeStartGathering();
1594 EXPECT_EQ_WAIT(IceGatheringState::kIceGatheringComplete,
1595 ep1_ch1()->gathering_state(), kDefaultTimeout);
1596 // Add two remote candidates; a host candidate (with higher priority)
1597 // and TURN candidate.
1598 ep1_ch1()->AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 100));
1599 ep1_ch1()->AddRemoteCandidate(CreateRelayCandidate("2.2.2.2", 2, 0));
1600 // Expect that the TURN-TURN candidate pair will be prioritized since it's
1601 // "probably writable".
1602 EXPECT_TRUE(ep1_ch1()->best_connection() != nullptr);
1603 EXPECT_EQ(RELAY_PORT_TYPE, LocalCandidate(ep1_ch1())->type());
1604 EXPECT_EQ(RELAY_PORT_TYPE, RemoteCandidate(ep1_ch1())->type());
1605 // Also expect that the channel instantly indicates that it's writable since
1606 // it has a TURN-TURN pair.
1607 EXPECT_TRUE(ep1_ch1()->writable());
1608 }
1609
1549 // Test what happens when we have 2 users behind the same NAT. This can lead 1610 // Test what happens when we have 2 users behind the same NAT. This can lead
1550 // to interesting behavior because the STUN server will only give out the 1611 // to interesting behavior because the STUN server will only give out the
1551 // address of the outermost NAT. 1612 // address of the outermost NAT.
1552 class P2PTransportChannelSameNatTest : public P2PTransportChannelTestBase { 1613 class P2PTransportChannelSameNatTest : public P2PTransportChannelTestBase {
1553 protected: 1614 protected:
1554 void ConfigureEndpoints(Config nat_type, Config config1, Config config2) { 1615 void ConfigureEndpoints(Config nat_type, Config config1, Config config2) {
1555 ASSERT(nat_type >= NAT_FULL_CONE && nat_type <= NAT_SYMMETRIC); 1616 ASSERT(nat_type >= NAT_FULL_CONE && nat_type <= NAT_SYMMETRIC);
1556 rtc::NATSocketServer::Translator* outer_nat = 1617 rtc::NATSocketServer::Translator* outer_nat =
1557 nat()->AddTranslator(kPublicAddrs[0], kNatAddrs[0], 1618 nat()->AddTranslator(kPublicAddrs[0], kNatAddrs[0],
1558 static_cast<rtc::NATType>(nat_type - NAT_FULL_CONE)); 1619 static_cast<rtc::NATType>(nat_type - NAT_FULL_CONE));
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
1613 1674
1614 EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() && 1675 EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
1615 ep2_ch1()->receiving() && ep2_ch1()->writable(), 1676 ep2_ch1()->receiving() && ep2_ch1()->writable(),
1616 1000, 1000); 1677 1000, 1000);
1617 EXPECT_TRUE( 1678 EXPECT_TRUE(
1618 ep1_ch1()->best_connection() && ep2_ch1()->best_connection() && 1679 ep1_ch1()->best_connection() && ep2_ch1()->best_connection() &&
1619 LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && 1680 LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) &&
1620 RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); 1681 RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1]));
1621 1682
1622 // Make the receiving timeout shorter for testing. 1683 // Make the receiving timeout shorter for testing.
1623 cricket::IceConfig config = CreateIceConfig(1000, false); 1684 IceConfig config = CreateIceConfig(1000, false);
1624 ep1_ch1()->SetIceConfig(config); 1685 ep1_ch1()->SetIceConfig(config);
1625 ep2_ch1()->SetIceConfig(config); 1686 ep2_ch1()->SetIceConfig(config);
1626 1687
1627 // Blackhole any traffic to or from the public addrs. 1688 // Blackhole any traffic to or from the public addrs.
1628 LOG(LS_INFO) << "Failing over..."; 1689 LOG(LS_INFO) << "Failing over...";
1629 fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[1]); 1690 fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[1]);
1630 // The best connections will switch, so keep references to them. 1691 // The best connections will switch, so keep references to them.
1631 const cricket::Connection* best_connection1 = ep1_ch1()->best_connection(); 1692 const Connection* best_connection1 = ep1_ch1()->best_connection();
1632 const cricket::Connection* best_connection2 = ep2_ch1()->best_connection(); 1693 const Connection* best_connection2 = ep2_ch1()->best_connection();
1633 // We should detect loss of receiving within 1 second or so. 1694 // We should detect loss of receiving within 1 second or so.
1634 EXPECT_TRUE_WAIT( 1695 EXPECT_TRUE_WAIT(
1635 !best_connection1->receiving() && !best_connection2->receiving(), 3000); 1696 !best_connection1->receiving() && !best_connection2->receiving(), 3000);
1636 1697
1637 // We should switch over to use the alternate addr immediately on both sides 1698 // We should switch over to use the alternate addr immediately on both sides
1638 // when we are not receiving. 1699 // when we are not receiving.
1639 EXPECT_TRUE_WAIT( 1700 EXPECT_TRUE_WAIT(
1640 ep1_ch1()->best_connection()->receiving() && 1701 ep1_ch1()->best_connection()->receiving() &&
1641 ep2_ch1()->best_connection()->receiving(), 1000); 1702 ep2_ch1()->best_connection()->receiving(), 1000);
1642 EXPECT_TRUE(LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0])); 1703 EXPECT_TRUE(LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]));
(...skipping 22 matching lines...) Expand all
1665 CreateChannels(1); 1726 CreateChannels(1);
1666 EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() && 1727 EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
1667 ep2_ch1()->receiving() && ep2_ch1()->writable(), 1728 ep2_ch1()->receiving() && ep2_ch1()->writable(),
1668 1000, 1000); 1729 1000, 1000);
1669 EXPECT_TRUE( 1730 EXPECT_TRUE(
1670 ep1_ch1()->best_connection() && ep2_ch1()->best_connection() && 1731 ep1_ch1()->best_connection() && ep2_ch1()->best_connection() &&
1671 LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && 1732 LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) &&
1672 RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); 1733 RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1]));
1673 1734
1674 // Make the receiving timeout shorter for testing. 1735 // Make the receiving timeout shorter for testing.
1675 cricket::IceConfig config = CreateIceConfig(1000, false); 1736 IceConfig config = CreateIceConfig(1000, false);
1676 ep1_ch1()->SetIceConfig(config); 1737 ep1_ch1()->SetIceConfig(config);
1677 ep2_ch1()->SetIceConfig(config); 1738 ep2_ch1()->SetIceConfig(config);
1678 1739
1679 // Blackhole any traffic to or from the public addrs. 1740 // Blackhole any traffic to or from the public addrs.
1680 LOG(LS_INFO) << "Failing over..."; 1741 LOG(LS_INFO) << "Failing over...";
1681 fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[0]); 1742 fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[0]);
1682 // The best connections will switch, so keep references to them. 1743 // The best connections will switch, so keep references to them.
1683 const cricket::Connection* best_connection1 = ep1_ch1()->best_connection(); 1744 const Connection* best_connection1 = ep1_ch1()->best_connection();
1684 const cricket::Connection* best_connection2 = ep2_ch1()->best_connection(); 1745 const Connection* best_connection2 = ep2_ch1()->best_connection();
1685 // We should detect loss of receiving within 1 second or so. 1746 // We should detect loss of receiving within 1 second or so.
1686 EXPECT_TRUE_WAIT( 1747 EXPECT_TRUE_WAIT(
1687 !best_connection1->receiving() && !best_connection2->receiving(), 3000); 1748 !best_connection1->receiving() && !best_connection2->receiving(), 3000);
1688 1749
1689 // We should switch over to use the alternate addr immediately on both sides 1750 // We should switch over to use the alternate addr immediately on both sides
1690 // when we are not receiving. 1751 // when we are not receiving.
1691 EXPECT_TRUE_WAIT( 1752 EXPECT_TRUE_WAIT(
1692 ep1_ch1()->best_connection()->receiving() && 1753 ep1_ch1()->best_connection()->receiving() &&
1693 ep2_ch1()->best_connection()->receiving(), 1000); 1754 ep2_ch1()->best_connection()->receiving(), 1000);
1694 EXPECT_TRUE( 1755 EXPECT_TRUE(
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
1777 1838
1778 // Create channels and let them go writable, as usual. 1839 // Create channels and let them go writable, as usual.
1779 CreateChannels(1); 1840 CreateChannels(1);
1780 EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() && 1841 EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() &&
1781 ep2_ch1()->receiving() && ep2_ch1()->writable(), 1842 ep2_ch1()->receiving() && ep2_ch1()->writable(),
1782 1000, 1000); 1843 1000, 1000);
1783 int backup_ping_interval = 2000; 1844 int backup_ping_interval = 2000;
1784 ep2_ch1()->SetIceConfig(CreateIceConfig(2000, false, backup_ping_interval)); 1845 ep2_ch1()->SetIceConfig(CreateIceConfig(2000, false, backup_ping_interval));
1785 // After the state becomes COMPLETED, the backup connection will be pinged 1846 // After the state becomes COMPLETED, the backup connection will be pinged
1786 // once every |backup_ping_interval| milliseconds. 1847 // once every |backup_ping_interval| milliseconds.
1787 ASSERT_TRUE_WAIT(ep2_ch1()->GetState() == cricket::STATE_COMPLETED, 1000); 1848 ASSERT_TRUE_WAIT(ep2_ch1()->GetState() == STATE_COMPLETED, 1000);
1788 const std::vector<cricket::Connection*>& connections = 1849 const std::vector<Connection*>& connections = ep2_ch1()->connections();
1789 ep2_ch1()->connections();
1790 ASSERT_EQ(2U, connections.size()); 1850 ASSERT_EQ(2U, connections.size());
1791 cricket::Connection* backup_conn = connections[1]; 1851 Connection* backup_conn = connections[1];
1792 EXPECT_TRUE_WAIT(backup_conn->writable(), 3000); 1852 EXPECT_TRUE_WAIT(backup_conn->writable(), 3000);
1793 int64_t last_ping_response_ms = backup_conn->last_ping_response_received(); 1853 int64_t last_ping_response_ms = backup_conn->last_ping_response_received();
1794 EXPECT_TRUE_WAIT( 1854 EXPECT_TRUE_WAIT(
1795 last_ping_response_ms < backup_conn->last_ping_response_received(), 5000); 1855 last_ping_response_ms < backup_conn->last_ping_response_received(), 5000);
1796 int time_elapsed = 1856 int time_elapsed =
1797 backup_conn->last_ping_response_received() - last_ping_response_ms; 1857 backup_conn->last_ping_response_received() - last_ping_response_ms;
1798 LOG(LS_INFO) << "Time elapsed: " << time_elapsed; 1858 LOG(LS_INFO) << "Time elapsed: " << time_elapsed;
1799 EXPECT_GE(time_elapsed, backup_ping_interval); 1859 EXPECT_GE(time_elapsed, backup_ping_interval);
1800 } 1860 }
1801 1861
1802 TEST_F(P2PTransportChannelMultihomedTest, TestGetState) { 1862 TEST_F(P2PTransportChannelMultihomedTest, TestGetState) {
1803 AddAddress(0, kAlternateAddrs[0]); 1863 AddAddress(0, kAlternateAddrs[0]);
1804 AddAddress(0, kPublicAddrs[0]); 1864 AddAddress(0, kPublicAddrs[0]);
1805 AddAddress(1, kPublicAddrs[1]); 1865 AddAddress(1, kPublicAddrs[1]);
1806 // Create channels and let them go writable, as usual. 1866 // Create channels and let them go writable, as usual.
1807 CreateChannels(1); 1867 CreateChannels(1);
1808 1868
1809 // Both transport channels will reach STATE_COMPLETED quickly. 1869 // Both transport channels will reach STATE_COMPLETED quickly.
1810 EXPECT_EQ_WAIT(cricket::TransportChannelState::STATE_COMPLETED, 1870 EXPECT_EQ_WAIT(TransportChannelState::STATE_COMPLETED, ep1_ch1()->GetState(),
1811 ep1_ch1()->GetState(), 1000); 1871 1000);
1812 EXPECT_EQ_WAIT(cricket::TransportChannelState::STATE_COMPLETED, 1872 EXPECT_EQ_WAIT(TransportChannelState::STATE_COMPLETED, ep2_ch1()->GetState(),
1813 ep2_ch1()->GetState(), 1000); 1873 1000);
1814 } 1874 }
1815 1875
1816 // Tests that when a network interface becomes inactive, if and only if 1876 // Tests that when a network interface becomes inactive, if and only if
1817 // Continual Gathering is enabled, the ports associated with that network 1877 // Continual Gathering is enabled, the ports associated with that network
1818 // will be removed from the port list of the channel, and the respective 1878 // will be removed from the port list of the channel, and the respective
1819 // remote candidates on the other participant will be removed eventually. 1879 // remote candidates on the other participant will be removed eventually.
1820 TEST_F(P2PTransportChannelMultihomedTest, TestNetworkBecomesInactive) { 1880 TEST_F(P2PTransportChannelMultihomedTest, TestNetworkBecomesInactive) {
1821 AddAddress(0, kPublicAddrs[0]); 1881 AddAddress(0, kPublicAddrs[0]);
1822 AddAddress(1, kPublicAddrs[1]); 1882 AddAddress(1, kPublicAddrs[1]);
1823 // Create channels and let them go writable, as usual. 1883 // Create channels and let them go writable, as usual.
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
1902 // pings. 1962 // pings.
1903 class P2PTransportChannelPingTest : public testing::Test, 1963 class P2PTransportChannelPingTest : public testing::Test,
1904 public sigslot::has_slots<> { 1964 public sigslot::has_slots<> {
1905 public: 1965 public:
1906 P2PTransportChannelPingTest() 1966 P2PTransportChannelPingTest()
1907 : pss_(new rtc::PhysicalSocketServer), 1967 : pss_(new rtc::PhysicalSocketServer),
1908 vss_(new rtc::VirtualSocketServer(pss_.get())), 1968 vss_(new rtc::VirtualSocketServer(pss_.get())),
1909 ss_scope_(vss_.get()) {} 1969 ss_scope_(vss_.get()) {}
1910 1970
1911 protected: 1971 protected:
1912 void PrepareChannel(cricket::P2PTransportChannel* ch) { 1972 void PrepareChannel(P2PTransportChannel* ch) {
1913 ch->SetIceRole(cricket::ICEROLE_CONTROLLING); 1973 ch->SetIceRole(ICEROLE_CONTROLLING);
1914 ch->SetIceCredentials(kIceUfrag[0], kIcePwd[0]); 1974 ch->SetIceCredentials(kIceUfrag[0], kIcePwd[0]);
1915 ch->SetRemoteIceCredentials(kIceUfrag[1], kIcePwd[1]); 1975 ch->SetRemoteIceCredentials(kIceUfrag[1], kIcePwd[1]);
1916 ch->SignalSelectedCandidatePairChanged.connect( 1976 ch->SignalSelectedCandidatePairChanged.connect(
1917 this, &P2PTransportChannelPingTest::OnSelectedCandidatePairChanged); 1977 this, &P2PTransportChannelPingTest::OnSelectedCandidatePairChanged);
1918 ch->SignalReadyToSend.connect(this, 1978 ch->SignalReadyToSend.connect(this,
1919 &P2PTransportChannelPingTest::OnReadyToSend); 1979 &P2PTransportChannelPingTest::OnReadyToSend);
1920 ch->SignalStateChanged.connect( 1980 ch->SignalStateChanged.connect(
1921 this, &P2PTransportChannelPingTest::OnChannelStateChanged); 1981 this, &P2PTransportChannelPingTest::OnChannelStateChanged);
1922 } 1982 }
1923 1983
1924 cricket::Candidate CreateHostCandidate(const std::string& ip, 1984 Connection* WaitForConnectionTo(P2PTransportChannel* ch,
1925 int port, 1985 const std::string& ip,
1926 int priority, 1986 int port_num) {
1927 const std::string& ufrag = "") {
1928 cricket::Candidate c;
1929 c.set_address(rtc::SocketAddress(ip, port));
1930 c.set_component(1);
1931 c.set_protocol(cricket::UDP_PROTOCOL_NAME);
1932 c.set_priority(priority);
1933 c.set_username(ufrag);
1934 c.set_type(cricket::LOCAL_PORT_TYPE);
1935 return c;
1936 }
1937
1938 cricket::Connection* WaitForConnectionTo(cricket::P2PTransportChannel* ch,
1939 const std::string& ip,
1940 int port_num) {
1941 EXPECT_TRUE_WAIT(GetConnectionTo(ch, ip, port_num) != nullptr, 3000); 1987 EXPECT_TRUE_WAIT(GetConnectionTo(ch, ip, port_num) != nullptr, 3000);
1942 return GetConnectionTo(ch, ip, port_num); 1988 return GetConnectionTo(ch, ip, port_num);
1943 } 1989 }
1944 1990
1945 cricket::Port* GetPort(cricket::P2PTransportChannel* ch) { 1991 Port* GetPort(P2PTransportChannel* ch) {
1946 if (ch->ports().empty()) { 1992 if (ch->ports().empty()) {
1947 return nullptr; 1993 return nullptr;
1948 } 1994 }
1949 return static_cast<cricket::Port*>(ch->ports()[0]); 1995 return static_cast<Port*>(ch->ports()[0]);
1950 } 1996 }
1951 1997
1952 cricket::Connection* GetConnectionTo(cricket::P2PTransportChannel* ch, 1998 Connection* GetConnectionTo(P2PTransportChannel* ch,
1953 const std::string& ip, 1999 const std::string& ip,
1954 int port_num) { 2000 int port_num) {
1955 cricket::Port* port = GetPort(ch); 2001 Port* port = GetPort(ch);
1956 if (!port) { 2002 if (!port) {
1957 return nullptr; 2003 return nullptr;
1958 } 2004 }
1959 return port->GetConnection(rtc::SocketAddress(ip, port_num)); 2005 return port->GetConnection(rtc::SocketAddress(ip, port_num));
1960 } 2006 }
1961 2007
1962 cricket::Connection* FindNextPingableConnectionAndPingIt( 2008 Connection* FindNextPingableConnectionAndPingIt(P2PTransportChannel* ch) {
1963 cricket::P2PTransportChannel* ch) { 2009 Connection* conn = ch->FindNextPingableConnection();
1964 cricket::Connection* conn = ch->FindNextPingableConnection();
1965 if (conn) { 2010 if (conn) {
1966 ch->MarkConnectionPinged(conn); 2011 ch->MarkConnectionPinged(conn);
1967 } 2012 }
1968 return conn; 2013 return conn;
1969 } 2014 }
1970 2015
1971 int SendData(cricket::TransportChannel& channel, 2016 int SendData(TransportChannel& channel,
1972 const char* data, 2017 const char* data,
1973 size_t len, 2018 size_t len,
1974 int packet_id) { 2019 int packet_id) {
1975 rtc::PacketOptions options; 2020 rtc::PacketOptions options;
1976 options.packet_id = packet_id; 2021 options.packet_id = packet_id;
1977 return channel.SendPacket(data, len, options, 0); 2022 return channel.SendPacket(data, len, options, 0);
1978 } 2023 }
1979 2024
1980 void OnSelectedCandidatePairChanged( 2025 void OnSelectedCandidatePairChanged(
1981 cricket::TransportChannel* transport_channel, 2026 TransportChannel* transport_channel,
1982 cricket::CandidatePairInterface* selected_candidate_pair, 2027 CandidatePairInterface* selected_candidate_pair,
1983 int last_sent_packet_id) { 2028 int last_sent_packet_id) {
1984 last_selected_candidate_pair_ = selected_candidate_pair; 2029 last_selected_candidate_pair_ = selected_candidate_pair;
1985 last_sent_packet_id_ = last_sent_packet_id; 2030 last_sent_packet_id_ = last_sent_packet_id;
1986 } 2031 }
1987 2032
1988 void ReceivePingOnConnection(cricket::Connection* conn, 2033 void ReceivePingOnConnection(Connection* conn,
1989 const std::string& remote_ufrag, 2034 const std::string& remote_ufrag,
1990 int priority) { 2035 int priority) {
1991 cricket::IceMessage msg; 2036 IceMessage msg;
1992 msg.SetType(cricket::STUN_BINDING_REQUEST); 2037 msg.SetType(STUN_BINDING_REQUEST);
1993 msg.AddAttribute(new cricket::StunByteStringAttribute( 2038 msg.AddAttribute(new StunByteStringAttribute(
1994 cricket::STUN_ATTR_USERNAME, 2039 STUN_ATTR_USERNAME,
1995 conn->local_candidate().username() + ":" + remote_ufrag)); 2040 conn->local_candidate().username() + ":" + remote_ufrag));
1996 msg.AddAttribute(new cricket::StunUInt32Attribute( 2041 msg.AddAttribute(new StunUInt32Attribute(STUN_ATTR_PRIORITY, priority));
1997 cricket::STUN_ATTR_PRIORITY, priority)); 2042 msg.SetTransactionID(rtc::CreateRandomString(kStunTransactionIdLength));
1998 msg.SetTransactionID(
1999 rtc::CreateRandomString(cricket::kStunTransactionIdLength));
2000 msg.AddMessageIntegrity(conn->local_candidate().password()); 2043 msg.AddMessageIntegrity(conn->local_candidate().password());
2001 msg.AddFingerprint(); 2044 msg.AddFingerprint();
2002 rtc::ByteBufferWriter buf; 2045 rtc::ByteBufferWriter buf;
2003 msg.Write(&buf); 2046 msg.Write(&buf);
2004 conn->OnReadPacket(buf.Data(), buf.Length(), rtc::CreatePacketTime(0)); 2047 conn->OnReadPacket(buf.Data(), buf.Length(), rtc::CreatePacketTime(0));
2005 } 2048 }
2006 2049
2007 void OnReadyToSend(cricket::TransportChannel* channel) { 2050 void OnReadyToSend(TransportChannel* channel) {
2008 channel_ready_to_send_ = true; 2051 channel_ready_to_send_ = true;
2009 } 2052 }
2010 void OnChannelStateChanged(cricket::TransportChannelImpl* channel) { 2053 void OnChannelStateChanged(TransportChannelImpl* channel) {
2011 channel_state_ = channel->GetState(); 2054 channel_state_ = channel->GetState();
2012 } 2055 }
2013 2056
2014 cricket::CandidatePairInterface* last_selected_candidate_pair() { 2057 CandidatePairInterface* last_selected_candidate_pair() {
2015 return last_selected_candidate_pair_; 2058 return last_selected_candidate_pair_;
2016 } 2059 }
2017 int last_sent_packet_id() { return last_sent_packet_id_; } 2060 int last_sent_packet_id() { return last_sent_packet_id_; }
2018 bool channel_ready_to_send() { return channel_ready_to_send_; } 2061 bool channel_ready_to_send() { return channel_ready_to_send_; }
2019 void reset_channel_ready_to_send() { channel_ready_to_send_ = false; } 2062 void reset_channel_ready_to_send() { channel_ready_to_send_ = false; }
2020 cricket::TransportChannelState channel_state() { return channel_state_; } 2063 TransportChannelState channel_state() { return channel_state_; }
2021 2064
2022 private: 2065 private:
2023 std::unique_ptr<rtc::PhysicalSocketServer> pss_; 2066 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
2024 std::unique_ptr<rtc::VirtualSocketServer> vss_; 2067 std::unique_ptr<rtc::VirtualSocketServer> vss_;
2025 rtc::SocketServerScope ss_scope_; 2068 rtc::SocketServerScope ss_scope_;
2026 cricket::CandidatePairInterface* last_selected_candidate_pair_ = nullptr; 2069 CandidatePairInterface* last_selected_candidate_pair_ = nullptr;
2027 int last_sent_packet_id_ = -1; 2070 int last_sent_packet_id_ = -1;
2028 bool channel_ready_to_send_ = false; 2071 bool channel_ready_to_send_ = false;
2029 cricket::TransportChannelState channel_state_ = cricket::STATE_INIT; 2072 TransportChannelState channel_state_ = STATE_INIT;
2030 }; 2073 };
2031 2074
2032 TEST_F(P2PTransportChannelPingTest, TestTriggeredChecks) { 2075 TEST_F(P2PTransportChannelPingTest, TestTriggeredChecks) {
2033 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2076 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2034 cricket::P2PTransportChannel ch("trigger checks", 1, &pa); 2077 P2PTransportChannel ch("trigger checks", 1, &pa);
2035 PrepareChannel(&ch); 2078 PrepareChannel(&ch);
2036 ch.Connect(); 2079 ch.Connect();
2037 ch.MaybeStartGathering(); 2080 ch.MaybeStartGathering();
2038 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1)); 2081 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1));
2039 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 2)); 2082 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 2));
2040 2083
2041 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2084 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2042 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2085 Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2043 ASSERT_TRUE(conn1 != nullptr); 2086 ASSERT_TRUE(conn1 != nullptr);
2044 ASSERT_TRUE(conn2 != nullptr); 2087 ASSERT_TRUE(conn2 != nullptr);
2045 2088
2046 // Before a triggered check, the first connection to ping is the 2089 // Before a triggered check, the first connection to ping is the
2047 // highest priority one. 2090 // highest priority one.
2048 EXPECT_EQ(conn2, FindNextPingableConnectionAndPingIt(&ch)); 2091 EXPECT_EQ(conn2, FindNextPingableConnectionAndPingIt(&ch));
2049 2092
2050 // Receiving a ping causes a triggered check which should make conn1 2093 // Receiving a ping causes a triggered check which should make conn1
2051 // be pinged first instead of conn2, even though conn2 has a higher 2094 // be pinged first instead of conn2, even though conn2 has a higher
2052 // priority. 2095 // priority.
2053 conn1->ReceivedPing(); 2096 conn1->ReceivedPing();
2054 EXPECT_EQ(conn1, FindNextPingableConnectionAndPingIt(&ch)); 2097 EXPECT_EQ(conn1, FindNextPingableConnectionAndPingIt(&ch));
2055 } 2098 }
2056 2099
2057 TEST_F(P2PTransportChannelPingTest, TestAllConnectionsPingedSufficiently) { 2100 TEST_F(P2PTransportChannelPingTest, TestAllConnectionsPingedSufficiently) {
2058 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2101 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2059 cricket::P2PTransportChannel ch("ping sufficiently", 1, &pa); 2102 P2PTransportChannel ch("ping sufficiently", 1, &pa);
2060 PrepareChannel(&ch); 2103 PrepareChannel(&ch);
2061 ch.Connect(); 2104 ch.Connect();
2062 ch.MaybeStartGathering(); 2105 ch.MaybeStartGathering();
2063 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1)); 2106 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1));
2064 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 2)); 2107 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 2));
2065 2108
2066 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2109 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2067 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2110 Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2068 ASSERT_TRUE(conn1 != nullptr); 2111 ASSERT_TRUE(conn1 != nullptr);
2069 ASSERT_TRUE(conn2 != nullptr); 2112 ASSERT_TRUE(conn2 != nullptr);
2070 2113
2071 // Low-priority connection becomes writable so that the other connection 2114 // Low-priority connection becomes writable so that the other connection
2072 // is not pruned. 2115 // is not pruned.
2073 conn1->ReceivedPingResponse(); 2116 conn1->ReceivedPingResponse();
2074 EXPECT_TRUE_WAIT( 2117 EXPECT_TRUE_WAIT(
2075 conn1->num_pings_sent() >= MIN_PINGS_AT_WEAK_PING_INTERVAL && 2118 conn1->num_pings_sent() >= MIN_PINGS_AT_WEAK_PING_INTERVAL &&
2076 conn2->num_pings_sent() >= MIN_PINGS_AT_WEAK_PING_INTERVAL, 2119 conn2->num_pings_sent() >= MIN_PINGS_AT_WEAK_PING_INTERVAL,
2077 kDefaultTimeout); 2120 kDefaultTimeout);
2078 } 2121 }
2079 2122
2080 TEST_F(P2PTransportChannelPingTest, TestNoTriggeredChecksWhenWritable) { 2123 TEST_F(P2PTransportChannelPingTest, TestNoTriggeredChecksWhenWritable) {
2081 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2124 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2082 cricket::P2PTransportChannel ch("trigger checks", 1, &pa); 2125 P2PTransportChannel ch("trigger checks", 1, &pa);
2083 PrepareChannel(&ch); 2126 PrepareChannel(&ch);
2084 ch.Connect(); 2127 ch.Connect();
2085 ch.MaybeStartGathering(); 2128 ch.MaybeStartGathering();
2086 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1)); 2129 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1));
2087 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 2)); 2130 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 2));
2088 2131
2089 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2132 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2090 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2133 Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2091 ASSERT_TRUE(conn1 != nullptr); 2134 ASSERT_TRUE(conn1 != nullptr);
2092 ASSERT_TRUE(conn2 != nullptr); 2135 ASSERT_TRUE(conn2 != nullptr);
2093 2136
2094 EXPECT_EQ(conn2, FindNextPingableConnectionAndPingIt(&ch)); 2137 EXPECT_EQ(conn2, FindNextPingableConnectionAndPingIt(&ch));
2095 EXPECT_EQ(conn1, FindNextPingableConnectionAndPingIt(&ch)); 2138 EXPECT_EQ(conn1, FindNextPingableConnectionAndPingIt(&ch));
2096 conn1->ReceivedPingResponse(); 2139 conn1->ReceivedPingResponse();
2097 ASSERT_TRUE(conn1->writable()); 2140 ASSERT_TRUE(conn1->writable());
2098 conn1->ReceivedPing(); 2141 conn1->ReceivedPing();
2099 2142
2100 // Ping received, but the connection is already writable, so no 2143 // Ping received, but the connection is already writable, so no
2101 // "triggered check" and conn2 is pinged before conn1 because it has 2144 // "triggered check" and conn2 is pinged before conn1 because it has
2102 // a higher priority. 2145 // a higher priority.
2103 EXPECT_EQ(conn2, FindNextPingableConnectionAndPingIt(&ch)); 2146 EXPECT_EQ(conn2, FindNextPingableConnectionAndPingIt(&ch));
2104 } 2147 }
2105 2148
2106 TEST_F(P2PTransportChannelPingTest, TestSignalStateChanged) { 2149 TEST_F(P2PTransportChannelPingTest, TestSignalStateChanged) {
2107 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2150 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2108 cricket::P2PTransportChannel ch("state change", 1, &pa); 2151 P2PTransportChannel ch("state change", 1, &pa);
2109 PrepareChannel(&ch); 2152 PrepareChannel(&ch);
2110 ch.Connect(); 2153 ch.Connect();
2111 ch.MaybeStartGathering(); 2154 ch.MaybeStartGathering();
2112 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1)); 2155 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1));
2113 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2156 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2114 ASSERT_TRUE(conn1 != nullptr); 2157 ASSERT_TRUE(conn1 != nullptr);
2115 // Pruning the connection reduces the set of active connections and changes 2158 // Pruning the connection reduces the set of active connections and changes
2116 // the channel state. 2159 // the channel state.
2117 conn1->Prune(); 2160 conn1->Prune();
2118 EXPECT_EQ_WAIT(cricket::STATE_FAILED, channel_state(), kDefaultTimeout); 2161 EXPECT_EQ_WAIT(STATE_FAILED, channel_state(), kDefaultTimeout);
2119 } 2162 }
2120 2163
2121 // Test adding remote candidates with different ufrags. If a remote candidate 2164 // Test adding remote candidates with different ufrags. If a remote candidate
2122 // is added with an old ufrag, it will be discarded. If it is added with a 2165 // is added with an old ufrag, it will be discarded. If it is added with a
2123 // ufrag that was not seen before, it will be used to create connections 2166 // ufrag that was not seen before, it will be used to create connections
2124 // although the ICE pwd in the remote candidate will be set when the ICE 2167 // although the ICE pwd in the remote candidate will be set when the ICE
2125 // credentials arrive. If a remote candidate is added with the current ICE 2168 // credentials arrive. If a remote candidate is added with the current ICE
2126 // ufrag, its pwd and generation will be set properly. 2169 // ufrag, its pwd and generation will be set properly.
2127 TEST_F(P2PTransportChannelPingTest, TestAddRemoteCandidateWithVariousUfrags) { 2170 TEST_F(P2PTransportChannelPingTest, TestAddRemoteCandidateWithVariousUfrags) {
2128 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2171 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2129 cricket::P2PTransportChannel ch("add candidate", 1, &pa); 2172 P2PTransportChannel ch("add candidate", 1, &pa);
2130 PrepareChannel(&ch); 2173 PrepareChannel(&ch);
2131 ch.Connect(); 2174 ch.Connect();
2132 ch.MaybeStartGathering(); 2175 ch.MaybeStartGathering();
2133 // Add a candidate with a future ufrag. 2176 // Add a candidate with a future ufrag.
2134 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1, kIceUfrag[2])); 2177 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1, kIceUfrag[2]));
2135 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2178 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2136 ASSERT_TRUE(conn1 != nullptr); 2179 ASSERT_TRUE(conn1 != nullptr);
2137 const cricket::Candidate& candidate = conn1->remote_candidate(); 2180 const Candidate& candidate = conn1->remote_candidate();
2138 EXPECT_EQ(kIceUfrag[2], candidate.username()); 2181 EXPECT_EQ(kIceUfrag[2], candidate.username());
2139 EXPECT_TRUE(candidate.password().empty()); 2182 EXPECT_TRUE(candidate.password().empty());
2140 EXPECT_TRUE(FindNextPingableConnectionAndPingIt(&ch) == nullptr); 2183 EXPECT_TRUE(FindNextPingableConnectionAndPingIt(&ch) == nullptr);
2141 2184
2142 // Set the remote credentials with the "future" ufrag. 2185 // Set the remote credentials with the "future" ufrag.
2143 // This should set the ICE pwd in the remote candidate of |conn1|, making 2186 // This should set the ICE pwd in the remote candidate of |conn1|, making
2144 // it pingable. 2187 // it pingable.
2145 ch.SetRemoteIceCredentials(kIceUfrag[2], kIcePwd[2]); 2188 ch.SetRemoteIceCredentials(kIceUfrag[2], kIcePwd[2]);
2146 EXPECT_EQ(kIceUfrag[2], candidate.username()); 2189 EXPECT_EQ(kIceUfrag[2], candidate.username());
2147 EXPECT_EQ(kIcePwd[2], candidate.password()); 2190 EXPECT_EQ(kIcePwd[2], candidate.password());
2148 EXPECT_EQ(conn1, FindNextPingableConnectionAndPingIt(&ch)); 2191 EXPECT_EQ(conn1, FindNextPingableConnectionAndPingIt(&ch));
2149 2192
2150 // Add a candidate with an old ufrag. No connection will be created. 2193 // Add a candidate with an old ufrag. No connection will be created.
2151 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 2, kIceUfrag[1])); 2194 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 2, kIceUfrag[1]));
2152 rtc::Thread::Current()->ProcessMessages(500); 2195 rtc::Thread::Current()->ProcessMessages(500);
2153 EXPECT_TRUE(GetConnectionTo(&ch, "2.2.2.2", 2) == nullptr); 2196 EXPECT_TRUE(GetConnectionTo(&ch, "2.2.2.2", 2) == nullptr);
2154 2197
2155 // Add a candidate with the current ufrag, its pwd and generation will be 2198 // Add a candidate with the current ufrag, its pwd and generation will be
2156 // assigned, even if the generation is not set. 2199 // assigned, even if the generation is not set.
2157 ch.AddRemoteCandidate(CreateHostCandidate("3.3.3.3", 3, 0, kIceUfrag[2])); 2200 ch.AddRemoteCandidate(CreateHostCandidate("3.3.3.3", 3, 0, kIceUfrag[2]));
2158 cricket::Connection* conn3 = nullptr; 2201 Connection* conn3 = nullptr;
2159 ASSERT_TRUE_WAIT((conn3 = GetConnectionTo(&ch, "3.3.3.3", 3)) != nullptr, 2202 ASSERT_TRUE_WAIT((conn3 = GetConnectionTo(&ch, "3.3.3.3", 3)) != nullptr,
2160 3000); 2203 3000);
2161 const cricket::Candidate& new_candidate = conn3->remote_candidate(); 2204 const Candidate& new_candidate = conn3->remote_candidate();
2162 EXPECT_EQ(kIcePwd[2], new_candidate.password()); 2205 EXPECT_EQ(kIcePwd[2], new_candidate.password());
2163 EXPECT_EQ(1U, new_candidate.generation()); 2206 EXPECT_EQ(1U, new_candidate.generation());
2164 2207
2165 // Check that the pwd of all remote candidates are properly assigned. 2208 // Check that the pwd of all remote candidates are properly assigned.
2166 for (const cricket::RemoteCandidate& candidate : ch.remote_candidates()) { 2209 for (const RemoteCandidate& candidate : ch.remote_candidates()) {
2167 EXPECT_TRUE(candidate.username() == kIceUfrag[1] || 2210 EXPECT_TRUE(candidate.username() == kIceUfrag[1] ||
2168 candidate.username() == kIceUfrag[2]); 2211 candidate.username() == kIceUfrag[2]);
2169 if (candidate.username() == kIceUfrag[1]) { 2212 if (candidate.username() == kIceUfrag[1]) {
2170 EXPECT_EQ(kIcePwd[1], candidate.password()); 2213 EXPECT_EQ(kIcePwd[1], candidate.password());
2171 } else if (candidate.username() == kIceUfrag[2]) { 2214 } else if (candidate.username() == kIceUfrag[2]) {
2172 EXPECT_EQ(kIcePwd[2], candidate.password()); 2215 EXPECT_EQ(kIcePwd[2], candidate.password());
2173 } 2216 }
2174 } 2217 }
2175 } 2218 }
2176 2219
2177 TEST_F(P2PTransportChannelPingTest, ConnectionResurrection) { 2220 TEST_F(P2PTransportChannelPingTest, ConnectionResurrection) {
2178 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2221 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2179 cricket::P2PTransportChannel ch("connection resurrection", 1, &pa); 2222 P2PTransportChannel ch("connection resurrection", 1, &pa);
2180 PrepareChannel(&ch); 2223 PrepareChannel(&ch);
2181 ch.Connect(); 2224 ch.Connect();
2182 ch.MaybeStartGathering(); 2225 ch.MaybeStartGathering();
2183 2226
2184 // Create conn1 and keep track of original candidate priority. 2227 // Create conn1 and keep track of original candidate priority.
2185 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1)); 2228 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1));
2186 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2229 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2187 ASSERT_TRUE(conn1 != nullptr); 2230 ASSERT_TRUE(conn1 != nullptr);
2188 uint32_t remote_priority = conn1->remote_candidate().priority(); 2231 uint32_t remote_priority = conn1->remote_candidate().priority();
2189 2232
2190 // Create a higher priority candidate and make the connection 2233 // Create a higher priority candidate and make the connection
2191 // receiving/writable. This will prune conn1. 2234 // receiving/writable. This will prune conn1.
2192 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 2)); 2235 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 2));
2193 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2236 Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2194 ASSERT_TRUE(conn2 != nullptr); 2237 ASSERT_TRUE(conn2 != nullptr);
2195 conn2->ReceivedPing(); 2238 conn2->ReceivedPing();
2196 conn2->ReceivedPingResponse(); 2239 conn2->ReceivedPingResponse();
2197 2240
2198 // Wait for conn1 to be pruned. 2241 // Wait for conn1 to be pruned.
2199 EXPECT_TRUE_WAIT(conn1->pruned(), 3000); 2242 EXPECT_TRUE_WAIT(conn1->pruned(), 3000);
2200 // Destroy the connection to test SignalUnknownAddress. 2243 // Destroy the connection to test SignalUnknownAddress.
2201 conn1->Destroy(); 2244 conn1->Destroy();
2202 EXPECT_TRUE_WAIT(GetConnectionTo(&ch, "1.1.1.1", 1) == nullptr, 1000); 2245 EXPECT_TRUE_WAIT(GetConnectionTo(&ch, "1.1.1.1", 1) == nullptr, 1000);
2203 2246
2204 // Create a minimal STUN message with prflx priority. 2247 // Create a minimal STUN message with prflx priority.
2205 cricket::IceMessage request; 2248 IceMessage request;
2206 request.SetType(cricket::STUN_BINDING_REQUEST); 2249 request.SetType(STUN_BINDING_REQUEST);
2207 request.AddAttribute(new cricket::StunByteStringAttribute( 2250 request.AddAttribute(
2208 cricket::STUN_ATTR_USERNAME, kIceUfrag[1])); 2251 new StunByteStringAttribute(STUN_ATTR_USERNAME, kIceUfrag[1]));
2209 uint32_t prflx_priority = cricket::ICE_TYPE_PREFERENCE_PRFLX << 24; 2252 uint32_t prflx_priority = ICE_TYPE_PREFERENCE_PRFLX << 24;
2210 request.AddAttribute(new cricket::StunUInt32Attribute( 2253 request.AddAttribute(
2211 cricket::STUN_ATTR_PRIORITY, prflx_priority)); 2254 new StunUInt32Attribute(STUN_ATTR_PRIORITY, prflx_priority));
2212 EXPECT_NE(prflx_priority, remote_priority); 2255 EXPECT_NE(prflx_priority, remote_priority);
2213 2256
2214 cricket::Port* port = GetPort(&ch); 2257 Port* port = GetPort(&ch);
2215 // conn1 should be resurrected with original priority. 2258 // conn1 should be resurrected with original priority.
2216 port->SignalUnknownAddress(port, rtc::SocketAddress("1.1.1.1", 1), 2259 port->SignalUnknownAddress(port, rtc::SocketAddress("1.1.1.1", 1), PROTO_UDP,
2217 cricket::PROTO_UDP, &request, kIceUfrag[1], false); 2260 &request, kIceUfrag[1], false);
2218 conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2261 conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2219 ASSERT_TRUE(conn1 != nullptr); 2262 ASSERT_TRUE(conn1 != nullptr);
2220 EXPECT_EQ(conn1->remote_candidate().priority(), remote_priority); 2263 EXPECT_EQ(conn1->remote_candidate().priority(), remote_priority);
2221 2264
2222 // conn3, a real prflx connection, should have prflx priority. 2265 // conn3, a real prflx connection, should have prflx priority.
2223 port->SignalUnknownAddress(port, rtc::SocketAddress("3.3.3.3", 1), 2266 port->SignalUnknownAddress(port, rtc::SocketAddress("3.3.3.3", 1), PROTO_UDP,
2224 cricket::PROTO_UDP, &request, kIceUfrag[1], false); 2267 &request, kIceUfrag[1], false);
2225 cricket::Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 1); 2268 Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 1);
2226 ASSERT_TRUE(conn3 != nullptr); 2269 ASSERT_TRUE(conn3 != nullptr);
2227 EXPECT_EQ(conn3->remote_candidate().priority(), prflx_priority); 2270 EXPECT_EQ(conn3->remote_candidate().priority(), prflx_priority);
2228 } 2271 }
2229 2272
2230 TEST_F(P2PTransportChannelPingTest, TestReceivingStateChange) { 2273 TEST_F(P2PTransportChannelPingTest, TestReceivingStateChange) {
2231 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2274 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2232 cricket::P2PTransportChannel ch("receiving state change", 1, &pa); 2275 P2PTransportChannel ch("receiving state change", 1, &pa);
2233 PrepareChannel(&ch); 2276 PrepareChannel(&ch);
2234 // Default receiving timeout and checking receiving interval should not be too 2277 // Default receiving timeout and checking receiving interval should not be too
2235 // small. 2278 // small.
2236 EXPECT_LE(1000, ch.receiving_timeout()); 2279 EXPECT_LE(1000, ch.receiving_timeout());
2237 EXPECT_LE(200, ch.check_receiving_interval()); 2280 EXPECT_LE(200, ch.check_receiving_interval());
2238 ch.SetIceConfig(CreateIceConfig(500, false)); 2281 ch.SetIceConfig(CreateIceConfig(500, false));
2239 EXPECT_EQ(500, ch.receiving_timeout()); 2282 EXPECT_EQ(500, ch.receiving_timeout());
2240 EXPECT_EQ(50, ch.check_receiving_interval()); 2283 EXPECT_EQ(50, ch.check_receiving_interval());
2241 ch.Connect(); 2284 ch.Connect();
2242 ch.MaybeStartGathering(); 2285 ch.MaybeStartGathering();
2243 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1)); 2286 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1));
2244 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2287 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2245 ASSERT_TRUE(conn1 != nullptr); 2288 ASSERT_TRUE(conn1 != nullptr);
2246 2289
2247 conn1->ReceivedPing(); 2290 conn1->ReceivedPing();
2248 conn1->OnReadPacket("ABC", 3, rtc::CreatePacketTime(0)); 2291 conn1->OnReadPacket("ABC", 3, rtc::CreatePacketTime(0));
2249 EXPECT_TRUE_WAIT(ch.best_connection() != nullptr, 1000); 2292 EXPECT_TRUE_WAIT(ch.best_connection() != nullptr, 1000);
2250 EXPECT_TRUE_WAIT(ch.receiving(), 1000); 2293 EXPECT_TRUE_WAIT(ch.receiving(), 1000);
2251 EXPECT_TRUE_WAIT(!ch.receiving(), 1000); 2294 EXPECT_TRUE_WAIT(!ch.receiving(), 1000);
2252 } 2295 }
2253 2296
2254 // The controlled side will select a connection as the "best connection" based 2297 // The controlled side will select a connection as the "best connection" based
2255 // on priority until the controlling side nominates a connection, at which 2298 // on priority until the controlling side nominates a connection, at which
2256 // point the controlled side will select that connection as the 2299 // point the controlled side will select that connection as the
2257 // "best connection". Plus, SignalSelectedCandidatePair will be fired if the 2300 // "best connection". Plus, SignalSelectedCandidatePair will be fired if the
2258 // best connection changes and SignalReadyToSend will be fired if the new best 2301 // best connection changes and SignalReadyToSend will be fired if the new best
2259 // connection is writable. 2302 // connection is writable.
2260 TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBeforeNomination) { 2303 TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBeforeNomination) {
2261 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2304 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2262 cricket::P2PTransportChannel ch("receiving state change", 1, &pa); 2305 P2PTransportChannel ch("receiving state change", 1, &pa);
2263 PrepareChannel(&ch); 2306 PrepareChannel(&ch);
2264 ch.SetIceRole(cricket::ICEROLE_CONTROLLED); 2307 ch.SetIceRole(ICEROLE_CONTROLLED);
2265 ch.Connect(); 2308 ch.Connect();
2266 ch.MaybeStartGathering(); 2309 ch.MaybeStartGathering();
2267 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1)); 2310 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1));
2268 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2311 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2269 ASSERT_TRUE(conn1 != nullptr); 2312 ASSERT_TRUE(conn1 != nullptr);
2270 EXPECT_EQ(conn1, ch.best_connection()); 2313 EXPECT_EQ(conn1, ch.best_connection());
2271 EXPECT_EQ(conn1, last_selected_candidate_pair()); 2314 EXPECT_EQ(conn1, last_selected_candidate_pair());
2272 EXPECT_EQ(-1, last_sent_packet_id()); 2315 EXPECT_EQ(-1, last_sent_packet_id());
2273 // Channel is not ready to send because it is not writable. 2316 // Channel is not ready to send because it is not writable.
2274 EXPECT_FALSE(channel_ready_to_send()); 2317 EXPECT_FALSE(channel_ready_to_send());
2275 2318
2276 int last_packet_id = 0; 2319 int last_packet_id = 0;
2277 const char* data = "ABCDEFGH"; 2320 const char* data = "ABCDEFGH";
2278 int len = static_cast<int>(strlen(data)); 2321 int len = static_cast<int>(strlen(data));
2279 SendData(ch, data, len, ++last_packet_id); 2322 SendData(ch, data, len, ++last_packet_id);
2280 // When a higher priority candidate comes in, the new connection is chosen 2323 // When a higher priority candidate comes in, the new connection is chosen
2281 // as the best connection. 2324 // as the best connection.
2282 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 10)); 2325 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 10));
2283 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2326 Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2284 ASSERT_TRUE(conn2 != nullptr); 2327 ASSERT_TRUE(conn2 != nullptr);
2285 EXPECT_EQ(conn2, ch.best_connection()); 2328 EXPECT_EQ(conn2, ch.best_connection());
2286 EXPECT_EQ(conn2, last_selected_candidate_pair()); 2329 EXPECT_EQ(conn2, last_selected_candidate_pair());
2287 EXPECT_EQ(last_packet_id, last_sent_packet_id()); 2330 EXPECT_EQ(last_packet_id, last_sent_packet_id());
2288 EXPECT_FALSE(channel_ready_to_send()); 2331 EXPECT_FALSE(channel_ready_to_send());
2289 2332
2290 // If a stun request with use-candidate attribute arrives, the receiving 2333 // If a stun request with use-candidate attribute arrives, the receiving
2291 // connection will be set as the best connection, even though 2334 // connection will be set as the best connection, even though
2292 // its priority is lower. 2335 // its priority is lower.
2293 SendData(ch, data, len, ++last_packet_id); 2336 SendData(ch, data, len, ++last_packet_id);
2294 ch.AddRemoteCandidate(CreateHostCandidate("3.3.3.3", 3, 1)); 2337 ch.AddRemoteCandidate(CreateHostCandidate("3.3.3.3", 3, 1));
2295 cricket::Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3); 2338 Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3);
2296 ASSERT_TRUE(conn3 != nullptr); 2339 ASSERT_TRUE(conn3 != nullptr);
2297 // Because it has a lower priority, the best connection is still conn2. 2340 // Because it has a lower priority, the best connection is still conn2.
2298 EXPECT_EQ(conn2, ch.best_connection()); 2341 EXPECT_EQ(conn2, ch.best_connection());
2299 conn3->ReceivedPingResponse(); // Become writable. 2342 conn3->ReceivedPingResponse(); // Become writable.
2300 // But if it is nominated via use_candidate, it is chosen as the best 2343 // But if it is nominated via use_candidate, it is chosen as the best
2301 // connection. 2344 // connection.
2302 conn3->set_nominated(true); 2345 conn3->set_nominated(true);
2303 conn3->SignalNominated(conn3); 2346 conn3->SignalNominated(conn3);
2304 EXPECT_EQ(conn3, ch.best_connection()); 2347 EXPECT_EQ(conn3, ch.best_connection());
2305 EXPECT_EQ(conn3, last_selected_candidate_pair()); 2348 EXPECT_EQ(conn3, last_selected_candidate_pair());
2306 EXPECT_EQ(last_packet_id, last_sent_packet_id()); 2349 EXPECT_EQ(last_packet_id, last_sent_packet_id());
2307 EXPECT_TRUE(channel_ready_to_send()); 2350 EXPECT_TRUE(channel_ready_to_send());
2308 2351
2309 // Even if another higher priority candidate arrives, 2352 // Even if another higher priority candidate arrives,
2310 // it will not be set as the best connection because the best connection 2353 // it will not be set as the best connection because the best connection
2311 // is nominated by the controlling side. 2354 // is nominated by the controlling side.
2312 SendData(ch, data, len, ++last_packet_id); 2355 SendData(ch, data, len, ++last_packet_id);
2313 ch.AddRemoteCandidate(CreateHostCandidate("4.4.4.4", 4, 100)); 2356 ch.AddRemoteCandidate(CreateHostCandidate("4.4.4.4", 4, 100));
2314 cricket::Connection* conn4 = WaitForConnectionTo(&ch, "4.4.4.4", 4); 2357 Connection* conn4 = WaitForConnectionTo(&ch, "4.4.4.4", 4);
2315 ASSERT_TRUE(conn4 != nullptr); 2358 ASSERT_TRUE(conn4 != nullptr);
2316 EXPECT_EQ(conn3, ch.best_connection()); 2359 EXPECT_EQ(conn3, ch.best_connection());
2317 // But if it is nominated via use_candidate and writable, it will be set as 2360 // But if it is nominated via use_candidate and writable, it will be set as
2318 // the best connection. 2361 // the best connection.
2319 conn4->set_nominated(true); 2362 conn4->set_nominated(true);
2320 conn4->SignalNominated(conn4); 2363 conn4->SignalNominated(conn4);
2321 // Not switched yet because conn4 is not writable. 2364 // Not switched yet because conn4 is not writable.
2322 EXPECT_EQ(conn3, ch.best_connection()); 2365 EXPECT_EQ(conn3, ch.best_connection());
2323 reset_channel_ready_to_send(); 2366 reset_channel_ready_to_send();
2324 // The best connection switches after conn4 becomes writable. 2367 // The best connection switches after conn4 becomes writable.
2325 conn4->ReceivedPingResponse(); 2368 conn4->ReceivedPingResponse();
2326 EXPECT_EQ(conn4, ch.best_connection()); 2369 EXPECT_EQ(conn4, ch.best_connection());
2327 EXPECT_EQ(conn4, last_selected_candidate_pair()); 2370 EXPECT_EQ(conn4, last_selected_candidate_pair());
2328 EXPECT_EQ(last_packet_id, last_sent_packet_id()); 2371 EXPECT_EQ(last_packet_id, last_sent_packet_id());
2329 // SignalReadyToSend is fired again because conn4 is writable. 2372 // SignalReadyToSend is fired again because conn4 is writable.
2330 EXPECT_TRUE(channel_ready_to_send()); 2373 EXPECT_TRUE(channel_ready_to_send());
2331 } 2374 }
2332 2375
2333 // The controlled side will select a connection as the "best connection" based 2376 // The controlled side will select a connection as the "best connection" based
2334 // on requests from an unknown address before the controlling side nominates 2377 // on requests from an unknown address before the controlling side nominates
2335 // a connection, and will nominate a connection from an unknown address if the 2378 // a connection, and will nominate a connection from an unknown address if the
2336 // request contains the use_candidate attribute. Plus, it will also sends back 2379 // request contains the use_candidate attribute. Plus, it will also sends back
2337 // a ping response and set the ICE pwd in the remote candidate appropriately. 2380 // a ping response and set the ICE pwd in the remote candidate appropriately.
2338 TEST_F(P2PTransportChannelPingTest, TestSelectConnectionFromUnknownAddress) { 2381 TEST_F(P2PTransportChannelPingTest, TestSelectConnectionFromUnknownAddress) {
2339 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2382 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2340 cricket::P2PTransportChannel ch("receiving state change", 1, &pa); 2383 P2PTransportChannel ch("receiving state change", 1, &pa);
2341 PrepareChannel(&ch); 2384 PrepareChannel(&ch);
2342 ch.SetIceRole(cricket::ICEROLE_CONTROLLED); 2385 ch.SetIceRole(ICEROLE_CONTROLLED);
2343 ch.Connect(); 2386 ch.Connect();
2344 ch.MaybeStartGathering(); 2387 ch.MaybeStartGathering();
2345 // A minimal STUN message with prflx priority. 2388 // A minimal STUN message with prflx priority.
2346 cricket::IceMessage request; 2389 IceMessage request;
2347 request.SetType(cricket::STUN_BINDING_REQUEST); 2390 request.SetType(STUN_BINDING_REQUEST);
2348 request.AddAttribute(new cricket::StunByteStringAttribute( 2391 request.AddAttribute(
2349 cricket::STUN_ATTR_USERNAME, kIceUfrag[1])); 2392 new StunByteStringAttribute(STUN_ATTR_USERNAME, kIceUfrag[1]));
2350 uint32_t prflx_priority = cricket::ICE_TYPE_PREFERENCE_PRFLX << 24; 2393 uint32_t prflx_priority = ICE_TYPE_PREFERENCE_PRFLX << 24;
2351 request.AddAttribute(new cricket::StunUInt32Attribute( 2394 request.AddAttribute(
2352 cricket::STUN_ATTR_PRIORITY, prflx_priority)); 2395 new StunUInt32Attribute(STUN_ATTR_PRIORITY, prflx_priority));
2353 cricket::TestUDPPort* port = static_cast<cricket::TestUDPPort*>(GetPort(&ch)); 2396 TestUDPPort* port = static_cast<TestUDPPort*>(GetPort(&ch));
2354 port->SignalUnknownAddress(port, rtc::SocketAddress("1.1.1.1", 1), 2397 port->SignalUnknownAddress(port, rtc::SocketAddress("1.1.1.1", 1), PROTO_UDP,
2355 cricket::PROTO_UDP, &request, kIceUfrag[1], false); 2398 &request, kIceUfrag[1], false);
2356 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2399 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2357 ASSERT_TRUE(conn1 != nullptr); 2400 ASSERT_TRUE(conn1 != nullptr);
2358 EXPECT_TRUE(port->sent_binding_response()); 2401 EXPECT_TRUE(port->sent_binding_response());
2359 EXPECT_EQ(conn1, ch.best_connection()); 2402 EXPECT_EQ(conn1, ch.best_connection());
2360 conn1->ReceivedPingResponse(); 2403 conn1->ReceivedPingResponse();
2361 EXPECT_EQ(conn1, ch.best_connection()); 2404 EXPECT_EQ(conn1, ch.best_connection());
2362 port->set_sent_binding_response(false); 2405 port->set_sent_binding_response(false);
2363 2406
2364 // Another connection is nominated via use_candidate. 2407 // Another connection is nominated via use_candidate.
2365 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 1)); 2408 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 1));
2366 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2409 Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2367 ASSERT_TRUE(conn2 != nullptr); 2410 ASSERT_TRUE(conn2 != nullptr);
2368 // Because it has a lower priority, the best connection is still conn1. 2411 // Because it has a lower priority, the best connection is still conn1.
2369 EXPECT_EQ(conn1, ch.best_connection()); 2412 EXPECT_EQ(conn1, ch.best_connection());
2370 // When it is nominated via use_candidate and writable, it is chosen as the 2413 // When it is nominated via use_candidate and writable, it is chosen as the
2371 // best connection. 2414 // best connection.
2372 conn2->ReceivedPingResponse(); // Become writable. 2415 conn2->ReceivedPingResponse(); // Become writable.
2373 conn2->set_nominated(true); 2416 conn2->set_nominated(true);
2374 conn2->SignalNominated(conn2); 2417 conn2->SignalNominated(conn2);
2375 EXPECT_EQ(conn2, ch.best_connection()); 2418 EXPECT_EQ(conn2, ch.best_connection());
2376 2419
2377 // Another request with unknown address, it will not be set as the best 2420 // Another request with unknown address, it will not be set as the best
2378 // connection because the best connection was nominated by the controlling 2421 // connection because the best connection was nominated by the controlling
2379 // side. 2422 // side.
2380 port->SignalUnknownAddress(port, rtc::SocketAddress("3.3.3.3", 3), 2423 port->SignalUnknownAddress(port, rtc::SocketAddress("3.3.3.3", 3), PROTO_UDP,
2381 cricket::PROTO_UDP, &request, kIceUfrag[1], false); 2424 &request, kIceUfrag[1], false);
2382 cricket::Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3); 2425 Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3);
2383 ASSERT_TRUE(conn3 != nullptr); 2426 ASSERT_TRUE(conn3 != nullptr);
2384 EXPECT_TRUE(port->sent_binding_response()); 2427 EXPECT_TRUE(port->sent_binding_response());
2385 conn3->ReceivedPingResponse(); // Become writable. 2428 conn3->ReceivedPingResponse(); // Become writable.
2386 EXPECT_EQ(conn2, ch.best_connection()); 2429 EXPECT_EQ(conn2, ch.best_connection());
2387 port->set_sent_binding_response(false); 2430 port->set_sent_binding_response(false);
2388 2431
2389 // However if the request contains use_candidate attribute, it will be 2432 // However if the request contains use_candidate attribute, it will be
2390 // selected as the best connection. 2433 // selected as the best connection.
2391 request.AddAttribute( 2434 request.AddAttribute(new StunByteStringAttribute(STUN_ATTR_USE_CANDIDATE));
2392 new cricket::StunByteStringAttribute(cricket::STUN_ATTR_USE_CANDIDATE)); 2435 port->SignalUnknownAddress(port, rtc::SocketAddress("4.4.4.4", 4), PROTO_UDP,
2393 port->SignalUnknownAddress(port, rtc::SocketAddress("4.4.4.4", 4), 2436 &request, kIceUfrag[1], false);
2394 cricket::PROTO_UDP, &request, kIceUfrag[1], false); 2437 Connection* conn4 = WaitForConnectionTo(&ch, "4.4.4.4", 4);
2395 cricket::Connection* conn4 = WaitForConnectionTo(&ch, "4.4.4.4", 4);
2396 ASSERT_TRUE(conn4 != nullptr); 2438 ASSERT_TRUE(conn4 != nullptr);
2397 EXPECT_TRUE(port->sent_binding_response()); 2439 EXPECT_TRUE(port->sent_binding_response());
2398 // conn4 is not the best connection yet because it is not writable. 2440 // conn4 is not the best connection yet because it is not writable.
2399 EXPECT_EQ(conn2, ch.best_connection()); 2441 EXPECT_EQ(conn2, ch.best_connection());
2400 conn4->ReceivedPingResponse(); // Become writable. 2442 conn4->ReceivedPingResponse(); // Become writable.
2401 EXPECT_EQ(conn4, ch.best_connection()); 2443 EXPECT_EQ(conn4, ch.best_connection());
2402 2444
2403 // Test that the request from an unknown address contains a ufrag from an old 2445 // Test that the request from an unknown address contains a ufrag from an old
2404 // generation. 2446 // generation.
2405 port->set_sent_binding_response(false); 2447 port->set_sent_binding_response(false);
2406 ch.SetRemoteIceCredentials(kIceUfrag[2], kIcePwd[2]); 2448 ch.SetRemoteIceCredentials(kIceUfrag[2], kIcePwd[2]);
2407 ch.SetRemoteIceCredentials(kIceUfrag[3], kIcePwd[3]); 2449 ch.SetRemoteIceCredentials(kIceUfrag[3], kIcePwd[3]);
2408 port->SignalUnknownAddress(port, rtc::SocketAddress("5.5.5.5", 5), 2450 port->SignalUnknownAddress(port, rtc::SocketAddress("5.5.5.5", 5), PROTO_UDP,
2409 cricket::PROTO_UDP, &request, kIceUfrag[2], false); 2451 &request, kIceUfrag[2], false);
2410 cricket::Connection* conn5 = WaitForConnectionTo(&ch, "5.5.5.5", 5); 2452 Connection* conn5 = WaitForConnectionTo(&ch, "5.5.5.5", 5);
2411 ASSERT_TRUE(conn5 != nullptr); 2453 ASSERT_TRUE(conn5 != nullptr);
2412 EXPECT_TRUE(port->sent_binding_response()); 2454 EXPECT_TRUE(port->sent_binding_response());
2413 EXPECT_EQ(kIcePwd[2], conn5->remote_candidate().password()); 2455 EXPECT_EQ(kIcePwd[2], conn5->remote_candidate().password());
2414 } 2456 }
2415 2457
2416 // The controlled side will select a connection as the "best connection" 2458 // The controlled side will select a connection as the "best connection"
2417 // based on media received until the controlling side nominates a connection, 2459 // based on media received until the controlling side nominates a connection,
2418 // at which point the controlled side will select that connection as 2460 // at which point the controlled side will select that connection as
2419 // the "best connection". 2461 // the "best connection".
2420 TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBasedOnMediaReceived) { 2462 TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBasedOnMediaReceived) {
2421 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2463 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2422 cricket::P2PTransportChannel ch("receiving state change", 1, &pa); 2464 P2PTransportChannel ch("receiving state change", 1, &pa);
2423 PrepareChannel(&ch); 2465 PrepareChannel(&ch);
2424 ch.SetIceRole(cricket::ICEROLE_CONTROLLED); 2466 ch.SetIceRole(ICEROLE_CONTROLLED);
2425 ch.Connect(); 2467 ch.Connect();
2426 ch.MaybeStartGathering(); 2468 ch.MaybeStartGathering();
2427 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 10)); 2469 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 10));
2428 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2470 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2429 ASSERT_TRUE(conn1 != nullptr); 2471 ASSERT_TRUE(conn1 != nullptr);
2430 EXPECT_EQ(conn1, ch.best_connection()); 2472 EXPECT_EQ(conn1, ch.best_connection());
2431 2473
2432 // If a data packet is received on conn2, the best connection should 2474 // If a data packet is received on conn2, the best connection should
2433 // switch to conn2 because the controlled side must mirror the media path 2475 // switch to conn2 because the controlled side must mirror the media path
2434 // chosen by the controlling side. 2476 // chosen by the controlling side.
2435 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 1)); 2477 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 1));
2436 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2478 Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2437 ASSERT_TRUE(conn2 != nullptr); 2479 ASSERT_TRUE(conn2 != nullptr);
2438 conn2->ReceivedPing(); // Start receiving. 2480 conn2->ReceivedPing(); // Start receiving.
2439 // Do not switch because it is not writable. 2481 // Do not switch because it is not writable.
2440 conn2->OnReadPacket("ABC", 3, rtc::CreatePacketTime(0)); 2482 conn2->OnReadPacket("ABC", 3, rtc::CreatePacketTime(0));
2441 EXPECT_EQ(conn1, ch.best_connection()); 2483 EXPECT_EQ(conn1, ch.best_connection());
2442 2484
2443 conn2->ReceivedPingResponse(); // Become writable. 2485 conn2->ReceivedPingResponse(); // Become writable.
2444 // Switch because it is writable. 2486 // Switch because it is writable.
2445 conn2->OnReadPacket("DEF", 3, rtc::CreatePacketTime(0)); 2487 conn2->OnReadPacket("DEF", 3, rtc::CreatePacketTime(0));
2446 EXPECT_EQ(conn2, ch.best_connection()); 2488 EXPECT_EQ(conn2, ch.best_connection());
2447 2489
2448 // Now another STUN message with an unknown address and use_candidate will 2490 // Now another STUN message with an unknown address and use_candidate will
2449 // nominate the best connection. 2491 // nominate the best connection.
2450 cricket::IceMessage request; 2492 IceMessage request;
2451 request.SetType(cricket::STUN_BINDING_REQUEST); 2493 request.SetType(STUN_BINDING_REQUEST);
2452 request.AddAttribute(new cricket::StunByteStringAttribute(
2453 cricket::STUN_ATTR_USERNAME, kIceUfrag[1]));
2454 uint32_t prflx_priority = cricket::ICE_TYPE_PREFERENCE_PRFLX << 24;
2455 request.AddAttribute(new cricket::StunUInt32Attribute(
2456 cricket::STUN_ATTR_PRIORITY, prflx_priority));
2457 request.AddAttribute( 2494 request.AddAttribute(
2458 new cricket::StunByteStringAttribute(cricket::STUN_ATTR_USE_CANDIDATE)); 2495 new StunByteStringAttribute(STUN_ATTR_USERNAME, kIceUfrag[1]));
2459 cricket::Port* port = GetPort(&ch); 2496 uint32_t prflx_priority = ICE_TYPE_PREFERENCE_PRFLX << 24;
2460 port->SignalUnknownAddress(port, rtc::SocketAddress("3.3.3.3", 3), 2497 request.AddAttribute(
2461 cricket::PROTO_UDP, &request, kIceUfrag[1], false); 2498 new StunUInt32Attribute(STUN_ATTR_PRIORITY, prflx_priority));
2462 cricket::Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3); 2499 request.AddAttribute(new StunByteStringAttribute(STUN_ATTR_USE_CANDIDATE));
2500 Port* port = GetPort(&ch);
2501 port->SignalUnknownAddress(port, rtc::SocketAddress("3.3.3.3", 3), PROTO_UDP,
2502 &request, kIceUfrag[1], false);
2503 Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3);
2463 ASSERT_TRUE(conn3 != nullptr); 2504 ASSERT_TRUE(conn3 != nullptr);
2464 EXPECT_EQ(conn2, ch.best_connection()); // Not writable yet. 2505 EXPECT_EQ(conn2, ch.best_connection()); // Not writable yet.
2465 conn3->ReceivedPingResponse(); // Become writable. 2506 conn3->ReceivedPingResponse(); // Become writable.
2466 EXPECT_EQ(conn3, ch.best_connection()); 2507 EXPECT_EQ(conn3, ch.best_connection());
2467 2508
2468 // Now another data packet will not switch the best connection because the 2509 // Now another data packet will not switch the best connection because the
2469 // best connection was nominated by the controlling side. 2510 // best connection was nominated by the controlling side.
2470 conn2->ReceivedPing(); 2511 conn2->ReceivedPing();
2471 conn2->ReceivedPingResponse(); 2512 conn2->ReceivedPingResponse();
2472 conn2->OnReadPacket("XYZ", 3, rtc::CreatePacketTime(0)); 2513 conn2->OnReadPacket("XYZ", 3, rtc::CreatePacketTime(0));
2473 EXPECT_EQ(conn3, ch.best_connection()); 2514 EXPECT_EQ(conn3, ch.best_connection());
2474 } 2515 }
2475 2516
2476 // Test that if a new remote candidate has the same address and port with 2517 // Test that if a new remote candidate has the same address and port with
2477 // an old one, it will be used to create a new connection. 2518 // an old one, it will be used to create a new connection.
2478 TEST_F(P2PTransportChannelPingTest, TestAddRemoteCandidateWithAddressReuse) { 2519 TEST_F(P2PTransportChannelPingTest, TestAddRemoteCandidateWithAddressReuse) {
2479 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2520 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2480 cricket::P2PTransportChannel ch("candidate reuse", 1, &pa); 2521 P2PTransportChannel ch("candidate reuse", 1, &pa);
2481 PrepareChannel(&ch); 2522 PrepareChannel(&ch);
2482 ch.Connect(); 2523 ch.Connect();
2483 ch.MaybeStartGathering(); 2524 ch.MaybeStartGathering();
2484 const std::string host_address = "1.1.1.1"; 2525 const std::string host_address = "1.1.1.1";
2485 const int port_num = 1; 2526 const int port_num = 1;
2486 2527
2487 // kIceUfrag[1] is the current generation ufrag. 2528 // kIceUfrag[1] is the current generation ufrag.
2488 cricket::Candidate candidate = 2529 Candidate candidate =
2489 CreateHostCandidate(host_address, port_num, 1, kIceUfrag[1]); 2530 CreateHostCandidate(host_address, port_num, 1, kIceUfrag[1]);
2490 ch.AddRemoteCandidate(candidate); 2531 ch.AddRemoteCandidate(candidate);
2491 cricket::Connection* conn1 = WaitForConnectionTo(&ch, host_address, port_num); 2532 Connection* conn1 = WaitForConnectionTo(&ch, host_address, port_num);
2492 ASSERT_TRUE(conn1 != nullptr); 2533 ASSERT_TRUE(conn1 != nullptr);
2493 EXPECT_EQ(0u, conn1->remote_candidate().generation()); 2534 EXPECT_EQ(0u, conn1->remote_candidate().generation());
2494 2535
2495 // Simply adding the same candidate again won't create a new connection. 2536 // Simply adding the same candidate again won't create a new connection.
2496 ch.AddRemoteCandidate(candidate); 2537 ch.AddRemoteCandidate(candidate);
2497 cricket::Connection* conn2 = GetConnectionTo(&ch, host_address, port_num); 2538 Connection* conn2 = GetConnectionTo(&ch, host_address, port_num);
2498 EXPECT_EQ(conn1, conn2); 2539 EXPECT_EQ(conn1, conn2);
2499 2540
2500 // Update the ufrag of the candidate and add it again. 2541 // Update the ufrag of the candidate and add it again.
2501 candidate.set_username(kIceUfrag[2]); 2542 candidate.set_username(kIceUfrag[2]);
2502 ch.AddRemoteCandidate(candidate); 2543 ch.AddRemoteCandidate(candidate);
2503 conn2 = GetConnectionTo(&ch, host_address, port_num); 2544 conn2 = GetConnectionTo(&ch, host_address, port_num);
2504 EXPECT_NE(conn1, conn2); 2545 EXPECT_NE(conn1, conn2);
2505 EXPECT_EQ(kIceUfrag[2], conn2->remote_candidate().username()); 2546 EXPECT_EQ(kIceUfrag[2], conn2->remote_candidate().username());
2506 EXPECT_EQ(1u, conn2->remote_candidate().generation()); 2547 EXPECT_EQ(1u, conn2->remote_candidate().generation());
2507 2548
2508 // Verify that a ping with the new ufrag can be received on the new 2549 // Verify that a ping with the new ufrag can be received on the new
2509 // connection. 2550 // connection.
2510 EXPECT_EQ(0, conn2->last_ping_received()); 2551 EXPECT_EQ(0, conn2->last_ping_received());
2511 ReceivePingOnConnection(conn2, kIceUfrag[2], 1 /* priority */); 2552 ReceivePingOnConnection(conn2, kIceUfrag[2], 1 /* priority */);
2512 EXPECT_TRUE(conn2->last_ping_received() > 0); 2553 EXPECT_TRUE(conn2->last_ping_received() > 0);
2513 } 2554 }
2514 2555
2515 // When the current best connection is strong, lower-priority connections will 2556 // When the current best connection is strong, lower-priority connections will
2516 // be pruned. Otherwise, lower-priority connections are kept. 2557 // be pruned. Otherwise, lower-priority connections are kept.
2517 TEST_F(P2PTransportChannelPingTest, TestDontPruneWhenWeak) { 2558 TEST_F(P2PTransportChannelPingTest, TestDontPruneWhenWeak) {
2518 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2559 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2519 cricket::P2PTransportChannel ch("test channel", 1, &pa); 2560 P2PTransportChannel ch("test channel", 1, &pa);
2520 PrepareChannel(&ch); 2561 PrepareChannel(&ch);
2521 ch.SetIceRole(cricket::ICEROLE_CONTROLLED); 2562 ch.SetIceRole(ICEROLE_CONTROLLED);
2522 ch.Connect(); 2563 ch.Connect();
2523 ch.MaybeStartGathering(); 2564 ch.MaybeStartGathering();
2524 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1)); 2565 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1));
2525 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2566 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2526 ASSERT_TRUE(conn1 != nullptr); 2567 ASSERT_TRUE(conn1 != nullptr);
2527 EXPECT_EQ(conn1, ch.best_connection()); 2568 EXPECT_EQ(conn1, ch.best_connection());
2528 conn1->ReceivedPingResponse(); // Becomes writable and receiving 2569 conn1->ReceivedPingResponse(); // Becomes writable and receiving
2529 2570
2530 // When a higher-priority, nominated candidate comes in, the connections with 2571 // When a higher-priority, nominated candidate comes in, the connections with
2531 // lower-priority are pruned. 2572 // lower-priority are pruned.
2532 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 10)); 2573 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 10));
2533 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2574 Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2534 ASSERT_TRUE(conn2 != nullptr); 2575 ASSERT_TRUE(conn2 != nullptr);
2535 conn2->ReceivedPingResponse(); // Becomes writable and receiving 2576 conn2->ReceivedPingResponse(); // Becomes writable and receiving
2536 conn2->set_nominated(true); 2577 conn2->set_nominated(true);
2537 conn2->SignalNominated(conn2); 2578 conn2->SignalNominated(conn2);
2538 EXPECT_TRUE_WAIT(conn1->pruned(), 3000); 2579 EXPECT_TRUE_WAIT(conn1->pruned(), 3000);
2539 2580
2540 ch.SetIceConfig(CreateIceConfig(500, false)); 2581 ch.SetIceConfig(CreateIceConfig(500, false));
2541 // Wait until conn2 becomes not receiving. 2582 // Wait until conn2 becomes not receiving.
2542 EXPECT_TRUE_WAIT(!conn2->receiving(), 3000); 2583 EXPECT_TRUE_WAIT(!conn2->receiving(), 3000);
2543 2584
2544 ch.AddRemoteCandidate(CreateHostCandidate("3.3.3.3", 3, 1)); 2585 ch.AddRemoteCandidate(CreateHostCandidate("3.3.3.3", 3, 1));
2545 cricket::Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3); 2586 Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3);
2546 ASSERT_TRUE(conn3 != nullptr); 2587 ASSERT_TRUE(conn3 != nullptr);
2547 // The best connection should still be conn2. Even through conn3 has lower 2588 // The best connection should still be conn2. Even through conn3 has lower
2548 // priority and is not receiving/writable, it is not pruned because the best 2589 // priority and is not receiving/writable, it is not pruned because the best
2549 // connection is not receiving. 2590 // connection is not receiving.
2550 WAIT(conn3->pruned(), 1000); 2591 WAIT(conn3->pruned(), 1000);
2551 EXPECT_FALSE(conn3->pruned()); 2592 EXPECT_FALSE(conn3->pruned());
2552 } 2593 }
2553 2594
2554 // Test that GetState returns the state correctly. 2595 // Test that GetState returns the state correctly.
2555 TEST_F(P2PTransportChannelPingTest, TestGetState) { 2596 TEST_F(P2PTransportChannelPingTest, TestGetState) {
2556 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2597 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2557 cricket::P2PTransportChannel ch("test channel", 1, &pa); 2598 P2PTransportChannel ch("test channel", 1, &pa);
2558 PrepareChannel(&ch); 2599 PrepareChannel(&ch);
2559 ch.Connect(); 2600 ch.Connect();
2560 ch.MaybeStartGathering(); 2601 ch.MaybeStartGathering();
2561 EXPECT_EQ(cricket::TransportChannelState::STATE_INIT, ch.GetState()); 2602 EXPECT_EQ(TransportChannelState::STATE_INIT, ch.GetState());
2562 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 100)); 2603 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 100));
2563 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 1)); 2604 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 1));
2564 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2605 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2565 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2606 Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2566 ASSERT_TRUE(conn1 != nullptr); 2607 ASSERT_TRUE(conn1 != nullptr);
2567 ASSERT_TRUE(conn2 != nullptr); 2608 ASSERT_TRUE(conn2 != nullptr);
2568 // Now there are two connections, so the transport channel is connecting. 2609 // Now there are two connections, so the transport channel is connecting.
2569 EXPECT_EQ(cricket::TransportChannelState::STATE_CONNECTING, ch.GetState()); 2610 EXPECT_EQ(TransportChannelState::STATE_CONNECTING, ch.GetState());
2570 // |conn1| becomes writable and receiving; it then should prune |conn2|. 2611 // |conn1| becomes writable and receiving; it then should prune |conn2|.
2571 conn1->ReceivedPingResponse(); 2612 conn1->ReceivedPingResponse();
2572 EXPECT_TRUE_WAIT(conn2->pruned(), 1000); 2613 EXPECT_TRUE_WAIT(conn2->pruned(), 1000);
2573 EXPECT_EQ(cricket::TransportChannelState::STATE_COMPLETED, ch.GetState()); 2614 EXPECT_EQ(TransportChannelState::STATE_COMPLETED, ch.GetState());
2574 conn1->Prune(); // All connections are pruned. 2615 conn1->Prune(); // All connections are pruned.
2575 // Need to wait until the channel state is updated. 2616 // Need to wait until the channel state is updated.
2576 EXPECT_EQ_WAIT(cricket::TransportChannelState::STATE_FAILED, ch.GetState(), 2617 EXPECT_EQ_WAIT(TransportChannelState::STATE_FAILED, ch.GetState(), 1000);
2577 1000);
2578 } 2618 }
2579 2619
2580 // Test that when a low-priority connection is pruned, it is not deleted 2620 // Test that when a low-priority connection is pruned, it is not deleted
2581 // right away, and it can become active and be pruned again. 2621 // right away, and it can become active and be pruned again.
2582 TEST_F(P2PTransportChannelPingTest, TestConnectionPrunedAgain) { 2622 TEST_F(P2PTransportChannelPingTest, TestConnectionPrunedAgain) {
2583 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2623 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2584 cricket::P2PTransportChannel ch("test channel", 1, &pa); 2624 P2PTransportChannel ch("test channel", 1, &pa);
2585 PrepareChannel(&ch); 2625 PrepareChannel(&ch);
2586 ch.SetIceConfig(CreateIceConfig(1000, false)); 2626 ch.SetIceConfig(CreateIceConfig(1000, false));
2587 ch.Connect(); 2627 ch.Connect();
2588 ch.MaybeStartGathering(); 2628 ch.MaybeStartGathering();
2589 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 100)); 2629 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 100));
2590 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2630 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2591 ASSERT_TRUE(conn1 != nullptr); 2631 ASSERT_TRUE(conn1 != nullptr);
2592 EXPECT_EQ(conn1, ch.best_connection()); 2632 EXPECT_EQ(conn1, ch.best_connection());
2593 conn1->ReceivedPingResponse(); // Becomes writable and receiving 2633 conn1->ReceivedPingResponse(); // Becomes writable and receiving
2594 2634
2595 // Add a low-priority connection |conn2|, which will be pruned, but it will 2635 // Add a low-priority connection |conn2|, which will be pruned, but it will
2596 // not be deleted right away. Once the current best connection becomes not 2636 // not be deleted right away. Once the current best connection becomes not
2597 // receiving, |conn2| will start to ping and upon receiving the ping response, 2637 // receiving, |conn2| will start to ping and upon receiving the ping response,
2598 // it will become the best connection. 2638 // it will become the best connection.
2599 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 1)); 2639 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 1));
2600 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2640 Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2601 ASSERT_TRUE(conn2 != nullptr); 2641 ASSERT_TRUE(conn2 != nullptr);
2602 EXPECT_TRUE_WAIT(!conn2->active(), 1000); 2642 EXPECT_TRUE_WAIT(!conn2->active(), 1000);
2603 // |conn2| should not send a ping yet. 2643 // |conn2| should not send a ping yet.
2604 EXPECT_EQ(cricket::Connection::STATE_WAITING, conn2->state()); 2644 EXPECT_EQ(Connection::STATE_WAITING, conn2->state());
2605 EXPECT_EQ(cricket::TransportChannelState::STATE_COMPLETED, ch.GetState()); 2645 EXPECT_EQ(TransportChannelState::STATE_COMPLETED, ch.GetState());
2606 // Wait for |conn1| becoming not receiving. 2646 // Wait for |conn1| becoming not receiving.
2607 EXPECT_TRUE_WAIT(!conn1->receiving(), 3000); 2647 EXPECT_TRUE_WAIT(!conn1->receiving(), 3000);
2608 // Make sure conn2 is not deleted. 2648 // Make sure conn2 is not deleted.
2609 conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2649 conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2610 ASSERT_TRUE(conn2 != nullptr); 2650 ASSERT_TRUE(conn2 != nullptr);
2611 EXPECT_EQ_WAIT(cricket::Connection::STATE_INPROGRESS, conn2->state(), 1000); 2651 EXPECT_EQ_WAIT(Connection::STATE_INPROGRESS, conn2->state(), 1000);
2612 conn2->ReceivedPingResponse(); 2652 conn2->ReceivedPingResponse();
2613 EXPECT_EQ_WAIT(conn2, ch.best_connection(), 1000); 2653 EXPECT_EQ_WAIT(conn2, ch.best_connection(), 1000);
2614 EXPECT_EQ(cricket::TransportChannelState::STATE_CONNECTING, ch.GetState()); 2654 EXPECT_EQ(TransportChannelState::STATE_CONNECTING, ch.GetState());
2615 2655
2616 // When |conn1| comes back again, |conn2| will be pruned again. 2656 // When |conn1| comes back again, |conn2| will be pruned again.
2617 conn1->ReceivedPingResponse(); 2657 conn1->ReceivedPingResponse();
2618 EXPECT_EQ_WAIT(conn1, ch.best_connection(), 1000); 2658 EXPECT_EQ_WAIT(conn1, ch.best_connection(), 1000);
2619 EXPECT_TRUE_WAIT(!conn2->active(), 1000); 2659 EXPECT_TRUE_WAIT(!conn2->active(), 1000);
2620 EXPECT_EQ(cricket::TransportChannelState::STATE_COMPLETED, ch.GetState()); 2660 EXPECT_EQ(TransportChannelState::STATE_COMPLETED, ch.GetState());
2621 } 2661 }
2622 2662
2623 // Test that if all connections in a channel has timed out on writing, they 2663 // Test that if all connections in a channel has timed out on writing, they
2624 // will all be deleted. We use Prune to simulate write_time_out. 2664 // will all be deleted. We use Prune to simulate write_time_out.
2625 TEST_F(P2PTransportChannelPingTest, TestDeleteConnectionsIfAllWriteTimedout) { 2665 TEST_F(P2PTransportChannelPingTest, TestDeleteConnectionsIfAllWriteTimedout) {
2626 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2666 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2627 cricket::P2PTransportChannel ch("test channel", 1, &pa); 2667 P2PTransportChannel ch("test channel", 1, &pa);
2628 PrepareChannel(&ch); 2668 PrepareChannel(&ch);
2629 ch.Connect(); 2669 ch.Connect();
2630 ch.MaybeStartGathering(); 2670 ch.MaybeStartGathering();
2631 // Have one connection only but later becomes write-time-out. 2671 // Have one connection only but later becomes write-time-out.
2632 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 100)); 2672 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 100));
2633 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2673 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2634 ASSERT_TRUE(conn1 != nullptr); 2674 ASSERT_TRUE(conn1 != nullptr);
2635 conn1->ReceivedPing(); // Becomes receiving 2675 conn1->ReceivedPing(); // Becomes receiving
2636 conn1->Prune(); 2676 conn1->Prune();
2637 EXPECT_TRUE_WAIT(ch.connections().empty(), 1000); 2677 EXPECT_TRUE_WAIT(ch.connections().empty(), 1000);
2638 2678
2639 // Have two connections but both become write-time-out later. 2679 // Have two connections but both become write-time-out later.
2640 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 1)); 2680 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 1));
2641 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2681 Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2642 ASSERT_TRUE(conn2 != nullptr); 2682 ASSERT_TRUE(conn2 != nullptr);
2643 conn2->ReceivedPing(); // Becomes receiving 2683 conn2->ReceivedPing(); // Becomes receiving
2644 ch.AddRemoteCandidate(CreateHostCandidate("3.3.3.3", 3, 2)); 2684 ch.AddRemoteCandidate(CreateHostCandidate("3.3.3.3", 3, 2));
2645 cricket::Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3); 2685 Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3);
2646 ASSERT_TRUE(conn3 != nullptr); 2686 ASSERT_TRUE(conn3 != nullptr);
2647 conn3->ReceivedPing(); // Becomes receiving 2687 conn3->ReceivedPing(); // Becomes receiving
2648 // Now prune both conn2 and conn3; they will be deleted soon. 2688 // Now prune both conn2 and conn3; they will be deleted soon.
2649 conn2->Prune(); 2689 conn2->Prune();
2650 conn3->Prune(); 2690 conn3->Prune();
2651 EXPECT_TRUE_WAIT(ch.connections().empty(), 1000); 2691 EXPECT_TRUE_WAIT(ch.connections().empty(), 1000);
2652 } 2692 }
2653 2693
2654 // Tests that after a port allocator session is started, it will be stopped 2694 // Tests that after a port allocator session is started, it will be stopped
2655 // when a new connection becomes writable and receiving. Also tests that if a 2695 // when a new connection becomes writable and receiving. Also tests that if a
2656 // connection belonging to an old session becomes writable, it won't stop 2696 // connection belonging to an old session becomes writable, it won't stop
2657 // the current port allocator session. 2697 // the current port allocator session.
2658 TEST_F(P2PTransportChannelPingTest, TestStopPortAllocatorSessions) { 2698 TEST_F(P2PTransportChannelPingTest, TestStopPortAllocatorSessions) {
2659 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2699 FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2660 cricket::P2PTransportChannel ch("test channel", 1, &pa); 2700 P2PTransportChannel ch("test channel", 1, &pa);
2661 PrepareChannel(&ch); 2701 PrepareChannel(&ch);
2662 ch.SetIceConfig(CreateIceConfig(2000, false)); 2702 ch.SetIceConfig(CreateIceConfig(2000, false));
2663 ch.Connect(); 2703 ch.Connect();
2664 ch.MaybeStartGathering(); 2704 ch.MaybeStartGathering();
2665 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 100)); 2705 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 100));
2666 cricket::Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2706 Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2667 ASSERT_TRUE(conn1 != nullptr); 2707 ASSERT_TRUE(conn1 != nullptr);
2668 conn1->ReceivedPingResponse(); // Becomes writable and receiving 2708 conn1->ReceivedPingResponse(); // Becomes writable and receiving
2669 EXPECT_TRUE(!ch.allocator_session()->IsGettingPorts()); 2709 EXPECT_TRUE(!ch.allocator_session()->IsGettingPorts());
2670 2710
2671 // Start a new session. Even though conn1, which belongs to an older 2711 // Start a new session. Even though conn1, which belongs to an older
2672 // session, becomes unwritable and writable again, it should not stop the 2712 // session, becomes unwritable and writable again, it should not stop the
2673 // current session. 2713 // current session.
2674 ch.SetIceCredentials(kIceUfrag[1], kIcePwd[1]); 2714 ch.SetIceCredentials(kIceUfrag[1], kIcePwd[1]);
2675 ch.MaybeStartGathering(); 2715 ch.MaybeStartGathering();
2676 conn1->Prune(); 2716 conn1->Prune();
2677 conn1->ReceivedPingResponse(); 2717 conn1->ReceivedPingResponse();
2678 EXPECT_TRUE(ch.allocator_session()->IsGettingPorts()); 2718 EXPECT_TRUE(ch.allocator_session()->IsGettingPorts());
2679 2719
2680 // But if a new connection created from the new session becomes writable, 2720 // But if a new connection created from the new session becomes writable,
2681 // it will stop the current session. 2721 // it will stop the current session.
2682 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 100)); 2722 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 100));
2683 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2723 Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2684 ASSERT_TRUE(conn2 != nullptr); 2724 ASSERT_TRUE(conn2 != nullptr);
2685 conn2->ReceivedPingResponse(); // Becomes writable and receiving 2725 conn2->ReceivedPingResponse(); // Becomes writable and receiving
2686 EXPECT_TRUE(!ch.allocator_session()->IsGettingPorts()); 2726 EXPECT_TRUE(!ch.allocator_session()->IsGettingPorts());
2687 } 2727 }
2688 2728
2689 class P2PTransportChannelMostLikelyToWorkFirstTest 2729 class P2PTransportChannelMostLikelyToWorkFirstTest
2690 : public P2PTransportChannelPingTest { 2730 : public P2PTransportChannelPingTest {
2691 public: 2731 public:
2692 P2PTransportChannelMostLikelyToWorkFirstTest() 2732 P2PTransportChannelMostLikelyToWorkFirstTest()
2693 : turn_server_(rtc::Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr) { 2733 : turn_server_(rtc::Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr) {
2694 network_manager_.AddInterface(kPublicAddrs[0]); 2734 network_manager_.AddInterface(kPublicAddrs[0]);
2695 allocator_.reset(new cricket::BasicPortAllocator( 2735 allocator_.reset(new BasicPortAllocator(
2696 &network_manager_, ServerAddresses(), rtc::SocketAddress(), 2736 &network_manager_, ServerAddresses(), rtc::SocketAddress(),
2697 rtc::SocketAddress(), rtc::SocketAddress())); 2737 rtc::SocketAddress(), rtc::SocketAddress()));
2698 allocator_->set_flags(allocator_->flags() | 2738 allocator_->set_flags(allocator_->flags() | PORTALLOCATOR_DISABLE_STUN |
2699 cricket::PORTALLOCATOR_DISABLE_STUN | 2739 PORTALLOCATOR_DISABLE_TCP);
2700 cricket::PORTALLOCATOR_DISABLE_TCP); 2740 RelayServerConfig config(RELAY_TURN);
2701 cricket::RelayServerConfig config(cricket::RELAY_TURN);
2702 config.credentials = kRelayCredentials; 2741 config.credentials = kRelayCredentials;
2703 config.ports.push_back( 2742 config.ports.push_back(ProtocolAddress(kTurnUdpIntAddr, PROTO_UDP, false));
2704 cricket::ProtocolAddress(kTurnUdpIntAddr, cricket::PROTO_UDP, false));
2705 allocator_->AddTurnServer(config); 2743 allocator_->AddTurnServer(config);
2706 allocator_->set_step_delay(kMinimumStepDelay); 2744 allocator_->set_step_delay(kMinimumStepDelay);
2707 } 2745 }
2708 2746
2709 cricket::P2PTransportChannel& StartTransportChannel( 2747 P2PTransportChannel& StartTransportChannel(
2710 bool prioritize_most_likely_to_work, 2748 bool prioritize_most_likely_to_work,
2711 int max_strong_interval) { 2749 int max_strong_interval) {
2712 channel_.reset( 2750 channel_.reset(new P2PTransportChannel("checks", 1, nullptr, allocator()));
2713 new cricket::P2PTransportChannel("checks", 1, nullptr, allocator())); 2751 IceConfig config = channel_->config();
2714 cricket::IceConfig config = channel_->config();
2715 config.prioritize_most_likely_candidate_pairs = 2752 config.prioritize_most_likely_candidate_pairs =
2716 prioritize_most_likely_to_work; 2753 prioritize_most_likely_to_work;
2717 config.max_strong_interval = max_strong_interval; 2754 config.max_strong_interval = max_strong_interval;
2718 channel_->SetIceConfig(config); 2755 channel_->SetIceConfig(config);
2719 PrepareChannel(channel_.get()); 2756 PrepareChannel(channel_.get());
2720 channel_->Connect(); 2757 channel_->Connect();
2721 channel_->MaybeStartGathering(); 2758 channel_->MaybeStartGathering();
2722 return *channel_.get(); 2759 return *channel_.get();
2723 } 2760 }
2724 2761
2725 cricket::BasicPortAllocator* allocator() { return allocator_.get(); } 2762 BasicPortAllocator* allocator() { return allocator_.get(); }
2726 cricket::TestTurnServer* turn_server() { return &turn_server_; } 2763 TestTurnServer* turn_server() { return &turn_server_; }
2727 2764
2728 // This verifies the next pingable connection has the expected candidates' 2765 // This verifies the next pingable connection has the expected candidates'
2729 // types and, for relay local candidate, the expected relay protocol and ping 2766 // types and, for relay local candidate, the expected relay protocol and ping
2730 // it. 2767 // it.
2731 void VerifyNextPingableConnection( 2768 void VerifyNextPingableConnection(
2732 const std::string& local_candidate_type, 2769 const std::string& local_candidate_type,
2733 const std::string& remote_candidate_type, 2770 const std::string& remote_candidate_type,
2734 const std::string& relay_protocol_type = cricket::UDP_PROTOCOL_NAME) { 2771 const std::string& relay_protocol_type = UDP_PROTOCOL_NAME) {
2735 cricket::Connection* conn = 2772 Connection* conn = FindNextPingableConnectionAndPingIt(channel_.get());
2736 FindNextPingableConnectionAndPingIt(channel_.get());
2737 EXPECT_EQ(conn->local_candidate().type(), local_candidate_type); 2773 EXPECT_EQ(conn->local_candidate().type(), local_candidate_type);
2738 if (conn->local_candidate().type() == cricket::RELAY_PORT_TYPE) { 2774 if (conn->local_candidate().type() == RELAY_PORT_TYPE) {
2739 EXPECT_EQ(conn->local_candidate().relay_protocol(), relay_protocol_type); 2775 EXPECT_EQ(conn->local_candidate().relay_protocol(), relay_protocol_type);
2740 } 2776 }
2741 EXPECT_EQ(conn->remote_candidate().type(), remote_candidate_type); 2777 EXPECT_EQ(conn->remote_candidate().type(), remote_candidate_type);
2742 } 2778 }
2743 2779
2744 cricket::Candidate CreateRelayCandidate(const std::string& ip, 2780 Candidate CreateRelayCandidate(const std::string& ip,
2745 int port, 2781 int port,
2746 int priority, 2782 int priority,
2747 const std::string& ufrag = "") { 2783 const std::string& ufrag = "") {
2748 cricket::Candidate c = CreateHostCandidate(ip, port, priority, ufrag); 2784 Candidate c = CreateHostCandidate(ip, port, priority, ufrag);
2749 c.set_type(cricket::RELAY_PORT_TYPE); 2785 c.set_type(RELAY_PORT_TYPE);
2750 return c; 2786 return c;
2751 } 2787 }
2752 2788
2753 private: 2789 private:
2754 std::unique_ptr<cricket::BasicPortAllocator> allocator_; 2790 std::unique_ptr<BasicPortAllocator> allocator_;
2755 rtc::FakeNetworkManager network_manager_; 2791 rtc::FakeNetworkManager network_manager_;
2756 cricket::TestTurnServer turn_server_; 2792 TestTurnServer turn_server_;
2757 std::unique_ptr<cricket::P2PTransportChannel> channel_; 2793 std::unique_ptr<P2PTransportChannel> channel_;
2758 }; 2794 };
2759 2795
2760 // Test that Relay/Relay connections will be pinged first when no other 2796 // Test that Relay/Relay connections will be pinged first when no other
2761 // connections have been pinged yet, unless we need to ping a trigger check or 2797 // connections have been pinged yet, unless we need to ping a trigger check or
2762 // we have a best connection. 2798 // we have a best connection.
2763 TEST_F(P2PTransportChannelMostLikelyToWorkFirstTest, 2799 TEST_F(P2PTransportChannelMostLikelyToWorkFirstTest,
2764 TestRelayRelayFirstWhenNothingPingedYet) { 2800 TestRelayRelayFirstWhenNothingPingedYet) {
2765 const int max_strong_interval = 100; 2801 const int max_strong_interval = 100;
2766 cricket::P2PTransportChannel& ch = 2802 P2PTransportChannel& ch = StartTransportChannel(true, max_strong_interval);
2767 StartTransportChannel(true, max_strong_interval);
2768 EXPECT_TRUE_WAIT(ch.ports().size() == 2, 5000); 2803 EXPECT_TRUE_WAIT(ch.ports().size() == 2, 5000);
2769 EXPECT_EQ(ch.ports()[0]->Type(), cricket::LOCAL_PORT_TYPE); 2804 EXPECT_EQ(ch.ports()[0]->Type(), LOCAL_PORT_TYPE);
2770 EXPECT_EQ(ch.ports()[1]->Type(), cricket::RELAY_PORT_TYPE); 2805 EXPECT_EQ(ch.ports()[1]->Type(), RELAY_PORT_TYPE);
2771 2806
2772 ch.AddRemoteCandidate(CreateRelayCandidate("1.1.1.1", 1, 1)); 2807 ch.AddRemoteCandidate(CreateRelayCandidate("1.1.1.1", 1, 1));
2773 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 2)); 2808 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 2));
2774 2809
2775 EXPECT_TRUE_WAIT(ch.connections().size() == 4, 5000); 2810 EXPECT_TRUE_WAIT(ch.connections().size() == 4, 5000);
2776 2811
2777 // Relay/Relay should be the first pingable connection. 2812 // Relay/Relay should be the first pingable connection.
2778 cricket::Connection* conn = FindNextPingableConnectionAndPingIt(&ch); 2813 Connection* conn = FindNextPingableConnectionAndPingIt(&ch);
2779 EXPECT_EQ(conn->local_candidate().type(), cricket::RELAY_PORT_TYPE); 2814 EXPECT_EQ(conn->local_candidate().type(), RELAY_PORT_TYPE);
2780 EXPECT_EQ(conn->remote_candidate().type(), cricket::RELAY_PORT_TYPE); 2815 EXPECT_EQ(conn->remote_candidate().type(), RELAY_PORT_TYPE);
2781 2816
2782 // Unless that we have a trigger check waiting to be pinged. 2817 // Unless that we have a trigger check waiting to be pinged.
2783 cricket::Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); 2818 Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2);
2784 EXPECT_EQ(conn2->local_candidate().type(), cricket::LOCAL_PORT_TYPE); 2819 EXPECT_EQ(conn2->local_candidate().type(), LOCAL_PORT_TYPE);
2785 EXPECT_EQ(conn2->remote_candidate().type(), cricket::LOCAL_PORT_TYPE); 2820 EXPECT_EQ(conn2->remote_candidate().type(), LOCAL_PORT_TYPE);
2786 conn2->ReceivedPing(); 2821 conn2->ReceivedPing();
2787 EXPECT_EQ(conn2, FindNextPingableConnectionAndPingIt(&ch)); 2822 EXPECT_EQ(conn2, FindNextPingableConnectionAndPingIt(&ch));
2788 2823
2789 // Make conn3 the best connection. 2824 // Make conn3 the best connection.
2790 cricket::Connection* conn3 = WaitForConnectionTo(&ch, "1.1.1.1", 1); 2825 Connection* conn3 = WaitForConnectionTo(&ch, "1.1.1.1", 1);
2791 EXPECT_EQ(conn3->local_candidate().type(), cricket::LOCAL_PORT_TYPE); 2826 EXPECT_EQ(conn3->local_candidate().type(), LOCAL_PORT_TYPE);
2792 EXPECT_EQ(conn3->remote_candidate().type(), cricket::RELAY_PORT_TYPE); 2827 EXPECT_EQ(conn3->remote_candidate().type(), RELAY_PORT_TYPE);
2793 conn3->ReceivedPingResponse(); 2828 conn3->ReceivedPingResponse();
2794 ASSERT_TRUE(conn3->writable()); 2829 ASSERT_TRUE(conn3->writable());
2795 conn3->ReceivedPing(); 2830 conn3->ReceivedPing();
2796 2831
2797 /* 2832 /*
2798 2833
2799 TODO(honghaiz): Re-enable this once we use fake clock for this test to fix 2834 TODO(honghaiz): Re-enable this once we use fake clock for this test to fix
2800 the flakiness. The following test becomes flaky because we now ping the 2835 the flakiness. The following test becomes flaky because we now ping the
2801 connections with fast rates until every connection is pinged at least three 2836 connections with fast rates until every connection is pinged at least three
2802 times. The best connection may have been pinged before |max_strong_interval|, 2837 times. The best connection may have been pinged before |max_strong_interval|,
2803 so it may not be the next connection to be pinged as expected in the test. 2838 so it may not be the next connection to be pinged as expected in the test.
2804 2839
2805 // Verify that conn3 will be the "best connection" since it is readable and 2840 // Verify that conn3 will be the "best connection" since it is readable and
2806 // writable. After |MAX_CURRENT_STRONG_INTERVAL|, it should be the next 2841 // writable. After |MAX_CURRENT_STRONG_INTERVAL|, it should be the next
2807 // pingable connection. 2842 // pingable connection.
2808 EXPECT_TRUE_WAIT(conn3 == ch.best_connection(), 5000); 2843 EXPECT_TRUE_WAIT(conn3 == ch.best_connection(), 5000);
2809 WAIT(false, max_strong_interval + 100); 2844 WAIT(false, max_strong_interval + 100);
2810 conn3->ReceivedPingResponse(); 2845 conn3->ReceivedPingResponse();
2811 ASSERT_TRUE(conn3->writable()); 2846 ASSERT_TRUE(conn3->writable());
2812 EXPECT_EQ(conn3, FindNextPingableConnectionAndPingIt(&ch)); 2847 EXPECT_EQ(conn3, FindNextPingableConnectionAndPingIt(&ch));
2813 2848
2814 */ 2849 */
2815 } 2850 }
2816 2851
2817 // Test that Relay/Relay connections will be pinged first when everything has 2852 // Test that Relay/Relay connections will be pinged first when everything has
2818 // been pinged even if the Relay/Relay connection wasn't the first to be pinged 2853 // been pinged even if the Relay/Relay connection wasn't the first to be pinged
2819 // in the first round. 2854 // in the first round.
2820 TEST_F(P2PTransportChannelMostLikelyToWorkFirstTest, 2855 TEST_F(P2PTransportChannelMostLikelyToWorkFirstTest,
2821 TestRelayRelayFirstWhenEverythingPinged) { 2856 TestRelayRelayFirstWhenEverythingPinged) {
2822 cricket::P2PTransportChannel& ch = StartTransportChannel(true, 100); 2857 P2PTransportChannel& ch = StartTransportChannel(true, 100);
2823 EXPECT_TRUE_WAIT(ch.ports().size() == 2, 5000); 2858 EXPECT_TRUE_WAIT(ch.ports().size() == 2, 5000);
2824 EXPECT_EQ(ch.ports()[0]->Type(), cricket::LOCAL_PORT_TYPE); 2859 EXPECT_EQ(ch.ports()[0]->Type(), LOCAL_PORT_TYPE);
2825 EXPECT_EQ(ch.ports()[1]->Type(), cricket::RELAY_PORT_TYPE); 2860 EXPECT_EQ(ch.ports()[1]->Type(), RELAY_PORT_TYPE);
2826 2861
2827 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1)); 2862 ch.AddRemoteCandidate(CreateHostCandidate("1.1.1.1", 1, 1));
2828 EXPECT_TRUE_WAIT(ch.connections().size() == 2, 5000); 2863 EXPECT_TRUE_WAIT(ch.connections().size() == 2, 5000);
2829 2864
2830 // Initially, only have Local/Local and Local/Relay. 2865 // Initially, only have Local/Local and Local/Relay.
2831 VerifyNextPingableConnection(cricket::LOCAL_PORT_TYPE, 2866 VerifyNextPingableConnection(LOCAL_PORT_TYPE, LOCAL_PORT_TYPE);
2832 cricket::LOCAL_PORT_TYPE); 2867 VerifyNextPingableConnection(RELAY_PORT_TYPE, LOCAL_PORT_TYPE);
2833 VerifyNextPingableConnection(cricket::RELAY_PORT_TYPE,
2834 cricket::LOCAL_PORT_TYPE);
2835 2868
2836 // Remote Relay candidate arrives. 2869 // Remote Relay candidate arrives.
2837 ch.AddRemoteCandidate(CreateRelayCandidate("2.2.2.2", 2, 2)); 2870 ch.AddRemoteCandidate(CreateRelayCandidate("2.2.2.2", 2, 2));
2838 EXPECT_TRUE_WAIT(ch.connections().size() == 4, 5000); 2871 EXPECT_TRUE_WAIT(ch.connections().size() == 4, 5000);
2839 2872
2840 // Relay/Relay should be the first since it hasn't been pinged before. 2873 // Relay/Relay should be the first since it hasn't been pinged before.
2841 VerifyNextPingableConnection(cricket::RELAY_PORT_TYPE, 2874 VerifyNextPingableConnection(RELAY_PORT_TYPE, RELAY_PORT_TYPE);
2842 cricket::RELAY_PORT_TYPE);
2843 2875
2844 // Local/Relay is the final one. 2876 // Local/Relay is the final one.
2845 VerifyNextPingableConnection(cricket::LOCAL_PORT_TYPE, 2877 VerifyNextPingableConnection(LOCAL_PORT_TYPE, RELAY_PORT_TYPE);
2846 cricket::RELAY_PORT_TYPE);
2847 2878
2848 // Now, every connection has been pinged once. The next one should be 2879 // Now, every connection has been pinged once. The next one should be
2849 // Relay/Relay. 2880 // Relay/Relay.
2850 VerifyNextPingableConnection(cricket::RELAY_PORT_TYPE, 2881 VerifyNextPingableConnection(RELAY_PORT_TYPE, RELAY_PORT_TYPE);
2851 cricket::RELAY_PORT_TYPE);
2852 } 2882 }
2853 2883
2854 // Test that when we receive a new remote candidate, they will be tried first 2884 // Test that when we receive a new remote candidate, they will be tried first
2855 // before we re-ping Relay/Relay connections again. 2885 // before we re-ping Relay/Relay connections again.
2856 TEST_F(P2PTransportChannelMostLikelyToWorkFirstTest, 2886 TEST_F(P2PTransportChannelMostLikelyToWorkFirstTest,
2857 TestNoStarvationOnNonRelayConnection) { 2887 TestNoStarvationOnNonRelayConnection) {
2858 cricket::P2PTransportChannel& ch = StartTransportChannel(true, 100); 2888 P2PTransportChannel& ch = StartTransportChannel(true, 100);
2859 EXPECT_TRUE_WAIT(ch.ports().size() == 2, 5000); 2889 EXPECT_TRUE_WAIT(ch.ports().size() == 2, 5000);
2860 EXPECT_EQ(ch.ports()[0]->Type(), cricket::LOCAL_PORT_TYPE); 2890 EXPECT_EQ(ch.ports()[0]->Type(), LOCAL_PORT_TYPE);
2861 EXPECT_EQ(ch.ports()[1]->Type(), cricket::RELAY_PORT_TYPE); 2891 EXPECT_EQ(ch.ports()[1]->Type(), RELAY_PORT_TYPE);
2862 2892
2863 ch.AddRemoteCandidate(CreateRelayCandidate("1.1.1.1", 1, 1)); 2893 ch.AddRemoteCandidate(CreateRelayCandidate("1.1.1.1", 1, 1));
2864 EXPECT_TRUE_WAIT(ch.connections().size() == 2, 5000); 2894 EXPECT_TRUE_WAIT(ch.connections().size() == 2, 5000);
2865 2895
2866 // Initially, only have Relay/Relay and Local/Relay. Ping Relay/Relay first. 2896 // Initially, only have Relay/Relay and Local/Relay. Ping Relay/Relay first.
2867 VerifyNextPingableConnection(cricket::RELAY_PORT_TYPE, 2897 VerifyNextPingableConnection(RELAY_PORT_TYPE, RELAY_PORT_TYPE);
2868 cricket::RELAY_PORT_TYPE);
2869 2898
2870 // Next, ping Local/Relay. 2899 // Next, ping Local/Relay.
2871 VerifyNextPingableConnection(cricket::LOCAL_PORT_TYPE, 2900 VerifyNextPingableConnection(LOCAL_PORT_TYPE, RELAY_PORT_TYPE);
2872 cricket::RELAY_PORT_TYPE);
2873 2901
2874 // Remote Local candidate arrives. 2902 // Remote Local candidate arrives.
2875 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 2)); 2903 ch.AddRemoteCandidate(CreateHostCandidate("2.2.2.2", 2, 2));
2876 EXPECT_TRUE_WAIT(ch.connections().size() == 4, 5000); 2904 EXPECT_TRUE_WAIT(ch.connections().size() == 4, 5000);
2877 2905
2878 // Local/Local should be the first since it hasn't been pinged before. 2906 // Local/Local should be the first since it hasn't been pinged before.
2879 VerifyNextPingableConnection(cricket::LOCAL_PORT_TYPE, 2907 VerifyNextPingableConnection(LOCAL_PORT_TYPE, LOCAL_PORT_TYPE);
2880 cricket::LOCAL_PORT_TYPE);
2881 2908
2882 // Relay/Local is the final one. 2909 // Relay/Local is the final one.
2883 VerifyNextPingableConnection(cricket::RELAY_PORT_TYPE, 2910 VerifyNextPingableConnection(RELAY_PORT_TYPE, LOCAL_PORT_TYPE);
2884 cricket::LOCAL_PORT_TYPE);
2885 2911
2886 // Now, every connection has been pinged once. The next one should be 2912 // Now, every connection has been pinged once. The next one should be
2887 // Relay/Relay. 2913 // Relay/Relay.
2888 VerifyNextPingableConnection(cricket::RELAY_PORT_TYPE, 2914 VerifyNextPingableConnection(RELAY_PORT_TYPE, RELAY_PORT_TYPE);
2889 cricket::RELAY_PORT_TYPE);
2890 } 2915 }
2891 2916
2892 // Test the ping sequence is UDP Relay/Relay followed by TCP Relay/Relay, 2917 // Test the ping sequence is UDP Relay/Relay followed by TCP Relay/Relay,
2893 // followed by the rest. 2918 // followed by the rest.
2894 TEST_F(P2PTransportChannelMostLikelyToWorkFirstTest, TestTcpTurn) { 2919 TEST_F(P2PTransportChannelMostLikelyToWorkFirstTest, TestTcpTurn) {
2895 // Add a Tcp Turn server. 2920 // Add a Tcp Turn server.
2896 turn_server()->AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP); 2921 turn_server()->AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
2897 cricket::RelayServerConfig config(cricket::RELAY_TURN); 2922 RelayServerConfig config(RELAY_TURN);
2898 config.credentials = kRelayCredentials; 2923 config.credentials = kRelayCredentials;
2899 config.ports.push_back( 2924 config.ports.push_back(ProtocolAddress(kTurnTcpIntAddr, PROTO_TCP, false));
2900 cricket::ProtocolAddress(kTurnTcpIntAddr, cricket::PROTO_TCP, false));
2901 allocator()->AddTurnServer(config); 2925 allocator()->AddTurnServer(config);
2902 2926
2903 cricket::P2PTransportChannel& ch = StartTransportChannel(true, 100); 2927 P2PTransportChannel& ch = StartTransportChannel(true, 100);
2904 EXPECT_TRUE_WAIT(ch.ports().size() == 3, 5000); 2928 EXPECT_TRUE_WAIT(ch.ports().size() == 3, 5000);
2905 EXPECT_EQ(ch.ports()[0]->Type(), cricket::LOCAL_PORT_TYPE); 2929 EXPECT_EQ(ch.ports()[0]->Type(), LOCAL_PORT_TYPE);
2906 EXPECT_EQ(ch.ports()[1]->Type(), cricket::RELAY_PORT_TYPE); 2930 EXPECT_EQ(ch.ports()[1]->Type(), RELAY_PORT_TYPE);
2907 EXPECT_EQ(ch.ports()[2]->Type(), cricket::RELAY_PORT_TYPE); 2931 EXPECT_EQ(ch.ports()[2]->Type(), RELAY_PORT_TYPE);
2908 2932
2909 // Remote Relay candidate arrives. 2933 // Remote Relay candidate arrives.
2910 ch.AddRemoteCandidate(CreateRelayCandidate("1.1.1.1", 1, 1)); 2934 ch.AddRemoteCandidate(CreateRelayCandidate("1.1.1.1", 1, 1));
2911 EXPECT_TRUE_WAIT(ch.connections().size() == 3, 5000); 2935 EXPECT_TRUE_WAIT(ch.connections().size() == 3, 5000);
2912 2936
2913 // UDP Relay/Relay should be pinged first. 2937 // UDP Relay/Relay should be pinged first.
2914 VerifyNextPingableConnection(cricket::RELAY_PORT_TYPE, 2938 VerifyNextPingableConnection(RELAY_PORT_TYPE, RELAY_PORT_TYPE);
2915 cricket::RELAY_PORT_TYPE);
2916 2939
2917 // TCP Relay/Relay is the next. 2940 // TCP Relay/Relay is the next.
2918 VerifyNextPingableConnection(cricket::RELAY_PORT_TYPE, 2941 VerifyNextPingableConnection(RELAY_PORT_TYPE, RELAY_PORT_TYPE,
2919 cricket::RELAY_PORT_TYPE, 2942 TCP_PROTOCOL_NAME);
2920 cricket::TCP_PROTOCOL_NAME);
2921 2943
2922 // Finally, Local/Relay will be pinged. 2944 // Finally, Local/Relay will be pinged.
2923 VerifyNextPingableConnection(cricket::LOCAL_PORT_TYPE, 2945 VerifyNextPingableConnection(LOCAL_PORT_TYPE, RELAY_PORT_TYPE);
2924 cricket::RELAY_PORT_TYPE);
2925 } 2946 }
2947
2948 } // namespace cricket {
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698