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

Side by Side Diff: webrtc/p2p/client/portallocator_unittest.cc

Issue 1274013002: Bug 4865: Enable connectivity when the remote peer is on public internet. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: Created 5 years, 4 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
« no previous file with comments | « webrtc/p2p/client/basicportallocator.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 } // namespace cricket 81 } // namespace cricket
82 82
83 class PortAllocatorTest : public testing::Test, public sigslot::has_slots<> { 83 class PortAllocatorTest : public testing::Test, public sigslot::has_slots<> {
84 public: 84 public:
85 PortAllocatorTest() 85 PortAllocatorTest()
86 : pss_(new rtc::PhysicalSocketServer), 86 : pss_(new rtc::PhysicalSocketServer),
87 vss_(new rtc::VirtualSocketServer(pss_.get())), 87 vss_(new rtc::VirtualSocketServer(pss_.get())),
88 fss_(new rtc::FirewallSocketServer(vss_.get())), 88 fss_(new rtc::FirewallSocketServer(vss_.get())),
89 ss_scope_(fss_.get()), 89 ss_scope_(fss_.get()),
90 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr), 90 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
91 nat_socket_factory_(&nat_factory_), 91 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
92 stun_server_(cricket::TestStunServer::Create(Thread::Current(), 92 stun_server_(cricket::TestStunServer::Create(Thread::Current(),
93 kStunAddr)), 93 kStunAddr)),
94 relay_server_(Thread::Current(), kRelayUdpIntAddr, kRelayUdpExtAddr, 94 relay_server_(Thread::Current(), kRelayUdpIntAddr, kRelayUdpExtAddr,
95 kRelayTcpIntAddr, kRelayTcpExtAddr, 95 kRelayTcpIntAddr, kRelayTcpExtAddr,
96 kRelaySslTcpIntAddr, kRelaySslTcpExtAddr), 96 kRelaySslTcpIntAddr, kRelaySslTcpExtAddr),
97 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr), 97 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
98 candidate_allocation_done_(false) { 98 candidate_allocation_done_(false) {
99 cricket::ServerAddresses stun_servers; 99 cricket::ServerAddresses stun_servers;
100 stun_servers.insert(kStunAddr); 100 stun_servers.insert(kStunAddr);
101 // Passing the addresses of GTURN servers will enable GTURN in 101 // Passing the addresses of GTURN servers will enable GTURN in
102 // Basicportallocator. 102 // Basicportallocator.
103 allocator_.reset(new cricket::BasicPortAllocator( 103 allocator_.reset(new cricket::BasicPortAllocator(
104 &network_manager_, 104 &network_manager_,
105 stun_servers, 105 stun_servers,
106 kRelayUdpIntAddr, kRelayTcpIntAddr, kRelaySslTcpIntAddr)); 106 kRelayUdpIntAddr, kRelayTcpIntAddr, kRelaySslTcpIntAddr));
107 allocator_->set_step_delay(cricket::kMinimumStepDelay); 107 allocator_->set_step_delay(cricket::kMinimumStepDelay);
108 } 108 }
109 109
110 void AddInterface(const SocketAddress& addr) { 110 void AddInterface(const SocketAddress& addr) {
111 network_manager_.AddInterface(addr); 111 network_manager_.AddInterface(addr);
112 } 112 }
113 void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
114 AddInterface(addr);
115 // When a binding comes from the any address, the |addr| will be used as the
116 // srflx address.
117 vss_->SetDefaultRoute(addr.ipaddr());
118 }
113 bool SetPortRange(int min_port, int max_port) { 119 bool SetPortRange(int min_port, int max_port) {
114 return allocator_->SetPortRange(min_port, max_port); 120 return allocator_->SetPortRange(min_port, max_port);
115 } 121 }
122 // No STUN/TURN configuration.
123 void ResetWithNoServers() {
124 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
125 allocator_->set_step_delay(cricket::kMinimumStepDelay);
126 }
116 void ResetWithNatServer(const rtc::SocketAddress& stun_server) { 127 void ResetWithNatServer(const rtc::SocketAddress& stun_server) {
117 nat_server_.reset(new rtc::NATServer( 128 nat_server_.reset(new rtc::NATServer(
118 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(), 129 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
119 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0))); 130 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
120 131
121 ServerAddresses stun_servers; 132 ServerAddresses stun_servers;
122 if (!stun_server.IsNil()) { 133 if (!stun_server.IsNil()) {
123 stun_servers.insert(stun_server); 134 stun_servers.insert(stun_server);
124 } 135 }
125 allocator_.reset(new cricket::BasicPortAllocator( 136 allocator_.reset(new cricket::BasicPortAllocator(
126 &network_manager_, &nat_socket_factory_, stun_servers)); 137 &network_manager_, nat_socket_factory_.get(), stun_servers));
127 allocator().set_step_delay(cricket::kMinimumStepDelay); 138 allocator().set_step_delay(cricket::kMinimumStepDelay);
128 } 139 }
129 140
141 void ResetWithStunServer(const rtc::SocketAddress& stun_server) {
142 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
143
144 ServerAddresses stun_servers;
145 if (!stun_server.IsNil()) {
146 stun_servers.insert(stun_server);
147 }
148 allocator_.reset(new cricket::BasicPortAllocator(
149 &network_manager_, nat_socket_factory_.get(), stun_servers));
150 allocator().set_step_delay(cricket::kMinimumStepDelay);
151 }
152
130 // Create a BasicPortAllocator without GTURN and add the TURN servers. 153 // Create a BasicPortAllocator without GTURN and add the TURN servers.
131 void ResetWithTurnServers(const rtc::SocketAddress& udp_turn, 154 void ResetWithTurnServers(const rtc::SocketAddress& udp_turn,
132 const rtc::SocketAddress& tcp_turn) { 155 const rtc::SocketAddress& tcp_turn) {
133 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_)); 156 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
134 allocator().set_step_delay(cricket::kMinimumStepDelay); 157 allocator().set_step_delay(cricket::kMinimumStepDelay);
135 AddTurnServers(udp_turn, tcp_turn); 158 AddTurnServers(udp_turn, tcp_turn);
136 } 159 }
137 160
138 void AddTurnServers(const rtc::SocketAddress& udp_turn, 161 void AddTurnServers(const rtc::SocketAddress& udp_turn,
139 const rtc::SocketAddress& tcp_turn) { 162 const rtc::SocketAddress& tcp_turn) {
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, 248 (*it)->GetOption(rtc::Socket::OPT_SNDBUF,
226 &send_buffer_size)); 249 &send_buffer_size));
227 } else { 250 } else {
228 EXPECT_EQ(0, (*it)->GetOption(rtc::Socket::OPT_SNDBUF, 251 EXPECT_EQ(0, (*it)->GetOption(rtc::Socket::OPT_SNDBUF,
229 &send_buffer_size)); 252 &send_buffer_size));
230 ASSERT_EQ(expected, send_buffer_size); 253 ASSERT_EQ(expected, send_buffer_size);
231 } 254 }
232 } 255 }
233 } 256 }
234 257
235 void CheckDisableAdapterEnumeration() { 258 // This function starts the port/address gathering and check the existence of
259 // candidates as specified. When |expect_stun_candidate| is true,
260 // |stun_candidate_addr| carries the expected reflective address, which is
261 // also the related address for TURN candidate if it is expected. Otherwise,
262 // it should be ignore.
263 void CheckDisableAdapterEnumeration(
264 uint32 total_ports,
265 const rtc::IPAddress& stun_candidate_addr,
266 const rtc::IPAddress& relay_candidate_udp_transport_addr,
267 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
236 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 268 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
237 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION); 269 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
270 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
271 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
272 allocator().set_allow_tcp_listen(false);
238 session_->StartGettingPorts(); 273 session_->StartGettingPorts();
239 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); 274 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
240 275
241 // Only 2 candidates as local UDP/TCP are all 0s and get trimmed out. 276 uint32 total_candidates = 0;
242 EXPECT_EQ(2U, candidates_.size()); 277 if (!IPIsUnspec(stun_candidate_addr)) {
juberti1 2015/08/14 23:22:43 I think we typically use .IsNil for this
243 EXPECT_EQ(2U, ports_.size()); // One stunport and one turnport. 278 ++total_candidates;
279 EXPECT_PRED5(CheckCandidate, candidates_[0],
280 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
281 rtc::SocketAddress(stun_candidate_addr, 0));
282 EXPECT_EQ(
283 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()),
284 candidates_[0].related_address());
285 }
286 if (!IPIsUnspec(relay_candidate_udp_transport_addr)) {
287 ++total_candidates;
288 EXPECT_PRED5(CheckCandidate, candidates_[1],
289 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
290 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0));
291 EXPECT_EQ(stun_candidate_addr, candidates_[1].related_address().ipaddr());
292 }
293 if (!IPIsUnspec(relay_candidate_tcp_transport_addr)) {
294 ++total_candidates;
295 EXPECT_PRED5(CheckCandidate, candidates_[2],
296 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
297 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0));
298 EXPECT_EQ(stun_candidate_addr, candidates_[2].related_address().ipaddr());
299 }
244 300
245 EXPECT_PRED5(CheckCandidate, candidates_[0], 301 EXPECT_EQ(total_candidates, candidates_.size());
246 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", 302 EXPECT_EQ(total_ports, ports_.size());
247 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
248 EXPECT_EQ(
249 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()),
250 candidates_[0].related_address());
251
252 EXPECT_PRED5(CheckCandidate, candidates_[1],
253 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
254 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
255 EXPECT_EQ(kNatUdpAddr.ipaddr(), candidates_[1].related_address().ipaddr());
256 } 303 }
257 304
258 protected: 305 protected:
259 cricket::BasicPortAllocator& allocator() { 306 cricket::BasicPortAllocator& allocator() {
260 return *allocator_; 307 return *allocator_;
261 } 308 }
262 309
263 void OnPortReady(cricket::PortAllocatorSession* ses, 310 void OnPortReady(cricket::PortAllocatorSession* ses,
264 cricket::PortInterface* port) { 311 cricket::PortInterface* port) {
265 LOG(LS_INFO) << "OnPortReady: " << port->ToString(); 312 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
(...skipping 20 matching lines...) Expand all
286 } 333 }
287 return false; 334 return false;
288 } 335 }
289 336
290 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_; 337 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
291 rtc::scoped_ptr<rtc::VirtualSocketServer> vss_; 338 rtc::scoped_ptr<rtc::VirtualSocketServer> vss_;
292 rtc::scoped_ptr<rtc::FirewallSocketServer> fss_; 339 rtc::scoped_ptr<rtc::FirewallSocketServer> fss_;
293 rtc::SocketServerScope ss_scope_; 340 rtc::SocketServerScope ss_scope_;
294 rtc::scoped_ptr<rtc::NATServer> nat_server_; 341 rtc::scoped_ptr<rtc::NATServer> nat_server_;
295 rtc::NATSocketFactory nat_factory_; 342 rtc::NATSocketFactory nat_factory_;
296 rtc::BasicPacketSocketFactory nat_socket_factory_; 343 rtc::scoped_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
297 rtc::scoped_ptr<cricket::TestStunServer> stun_server_; 344 rtc::scoped_ptr<cricket::TestStunServer> stun_server_;
298 cricket::TestRelayServer relay_server_; 345 cricket::TestRelayServer relay_server_;
299 cricket::TestTurnServer turn_server_; 346 cricket::TestTurnServer turn_server_;
300 rtc::FakeNetworkManager network_manager_; 347 rtc::FakeNetworkManager network_manager_;
301 rtc::scoped_ptr<cricket::BasicPortAllocator> allocator_; 348 rtc::scoped_ptr<cricket::BasicPortAllocator> allocator_;
302 rtc::scoped_ptr<cricket::PortAllocatorSession> session_; 349 rtc::scoped_ptr<cricket::PortAllocatorSession> session_;
303 std::vector<cricket::PortInterface*> ports_; 350 std::vector<cricket::PortInterface*> ports_;
304 std::vector<cricket::Candidate> candidates_; 351 std::vector<cricket::Candidate> candidates_;
305 bool candidate_allocation_done_; 352 bool candidate_allocation_done_;
306 }; 353 };
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 495 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
449 session_->StartGettingPorts(); 496 session_->StartGettingPorts();
450 rtc::Thread::Current()->ProcessMessages(100); 497 rtc::Thread::Current()->ProcessMessages(100);
451 // Without network adapter, we should not get any candidate. 498 // Without network adapter, we should not get any candidate.
452 EXPECT_EQ(0U, candidates_.size()); 499 EXPECT_EQ(0U, candidates_.size());
453 EXPECT_TRUE(candidate_allocation_done_); 500 EXPECT_TRUE(candidate_allocation_done_);
454 } 501 }
455 502
456 // Test that we should only get STUN and TURN candidates when adapter 503 // Test that we should only get STUN and TURN candidates when adapter
457 // enumeration is disabled. 504 // enumeration is disabled.
458 TEST_F(PortAllocatorTest, TestDisableAdapterEnumeration) { 505 TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationBehindNat) {
459 AddInterface(kClientAddr); 506 AddInterface(kClientAddr);
460 // GTURN is not configured here. 507 // GTURN is not configured here.
461 ResetWithNatServer(kStunAddr); 508 ResetWithNatServer(kStunAddr);
462 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress()); 509 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
463 510 // Expect to see 3 ports: STUN, TURN/UDP and TCP ports, and both STUN and
464 CheckDisableAdapterEnumeration(); 511 // TURN/UDP candidates.
512 CheckDisableAdapterEnumeration(3U, kNatUdpAddr.ipaddr(),
513 kTurnUdpExtAddr.ipaddr(), rtc::IPAddress());
465 } 514 }
466 515
467 // Test that even with multiple interfaces, the result should be only 1 Stun 516 // Test that even with multiple interfaces, the result should still be one STUN
468 // candidate since we bind to any address (i.e. all 0s). 517 // and one TURN candidate since we bind to any address (i.e. all 0s).
469 TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationMultipleInterfaces) { 518 TEST_F(PortAllocatorTest,
519 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
470 AddInterface(kPrivateAddr); 520 AddInterface(kPrivateAddr);
471 AddInterface(kPrivateAddr2); 521 AddInterface(kPrivateAddr2);
472 ResetWithNatServer(kStunAddr); 522 ResetWithNatServer(kStunAddr);
473 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress()); 523 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
524 // Expect to see 3 ports: STUN, TURN/UDP and TCP ports, and both STUN and
525 // TURN/UDP candidates.
526 CheckDisableAdapterEnumeration(3U, kNatUdpAddr.ipaddr(),
527 kTurnUdpExtAddr.ipaddr(), rtc::IPAddress());
528 }
474 529
475 CheckDisableAdapterEnumeration(); 530 // Test that we should get STUN, TURN/UDP and TURN/TCP candidates when a
531 // TURN/TCP server is specified.
532 TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
533 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
534 AddInterface(kClientAddr);
535 // GTURN is not configured here.
536 ResetWithNatServer(kStunAddr);
537 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
538 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. STUN,
539 // TURN/UDP, and TURN/TCP candidates.
540 CheckDisableAdapterEnumeration(4U, kNatUdpAddr.ipaddr(),
541 kTurnUdpExtAddr.ipaddr(),
542 kTurnUdpExtAddr.ipaddr());
543 }
544
545 // Test that we should only get STUN and TURN candidates when adapter
546 // enumeration is disabled. Since the endpoint is not behind NAT, the srflx
547 // address should be the public client interface.
548 TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationWithoutNat) {
549 AddInterfaceAsDefaultRoute(kClientAddr);
550 ResetWithStunServer(kStunAddr);
551 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
552 // Expect to see 3 ports: STUN, TURN/UDP and TCP ports, but only both STUN and
553 // TURN candidates. The STUN candidate should have kClientAddr as srflx
554 // address, and TURN candidate with kClientAddr as the related address.
555 CheckDisableAdapterEnumeration(3U, kClientAddr.ipaddr(),
556 kTurnUdpExtAddr.ipaddr(), rtc::IPAddress());
557 }
558
559 // Test that when adapter enumeration is disabled, for endpoints without
560 // STUN/TURN specified, no candidate is generated.
561 TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationWithoutNatOrServers) {
562 AddInterfaceAsDefaultRoute(kClientAddr);
563 ResetWithNoServers();
564 // Expect to see 2 ports: STUN and TCP ports, but no candidate.
565 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
566 rtc::IPAddress());
476 } 567 }
477 568
478 // Disable for asan, see 569 // Disable for asan, see
479 // https://code.google.com/p/webrtc/issues/detail?id=4743 for details. 570 // https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
480 #if !defined(ADDRESS_SANITIZER) 571 #if !defined(ADDRESS_SANITIZER)
481 572
482 // Test that we can get OnCandidatesAllocationDone callback when all the ports 573 // Test that we can get OnCandidatesAllocationDone callback when all the ports
483 // are disabled. 574 // are disabled.
484 TEST_F(PortAllocatorTest, TestDisableAllPorts) { 575 TEST_F(PortAllocatorTest, TestDisableAllPorts) {
485 AddInterface(kClientAddr); 576 AddInterface(kClientAddr);
(...skipping 698 matching lines...) Expand 10 before | Expand all | Expand 10 after
1184 AllocationSequenceForTest alloc_sequence( 1275 AllocationSequenceForTest alloc_sequence(
1185 static_cast<cricket::BasicPortAllocatorSession*>(session_.get()), 1276 static_cast<cricket::BasicPortAllocatorSession*>(session_.get()),
1186 &network1, &config, flag); 1277 &network1, &config, flag);
1187 // This simply tests it will not crash if udp_socket_ in the 1278 // This simply tests it will not crash if udp_socket_ in the
1188 // AllocationSequence is null, which is chosen in the constructor. 1279 // AllocationSequence is null, which is chosen in the constructor.
1189 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN); 1280 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
1190 relay_server.ports.push_back( 1281 relay_server.ports.push_back(
1191 cricket::ProtocolAddress(kTurnUdpIntAddr, cricket::PROTO_UDP, false)); 1282 cricket::ProtocolAddress(kTurnUdpIntAddr, cricket::PROTO_UDP, false));
1192 alloc_sequence.CreateTurnPort(relay_server); 1283 alloc_sequence.CreateTurnPort(relay_server);
1193 } 1284 }
OLDNEW
« no previous file with comments | « webrtc/p2p/client/basicportallocator.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698