OLD | NEW |
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 |
11 #include <algorithm> | |
12 #include <memory> | 11 #include <memory> |
13 | 12 |
14 #include "webrtc/p2p/base/fakeportallocator.h" | |
15 #include "webrtc/p2p/base/p2ptransportchannel.h" | 13 #include "webrtc/p2p/base/p2ptransportchannel.h" |
16 #include "webrtc/p2p/base/testrelayserver.h" | 14 #include "webrtc/p2p/base/testrelayserver.h" |
17 #include "webrtc/p2p/base/teststunserver.h" | 15 #include "webrtc/p2p/base/teststunserver.h" |
18 #include "webrtc/p2p/base/testturnserver.h" | 16 #include "webrtc/p2p/base/testturnserver.h" |
19 #include "webrtc/p2p/client/basicportallocator.h" | 17 #include "webrtc/p2p/client/basicportallocator.h" |
| 18 #include "webrtc/p2p/client/fakeportallocator.h" |
20 #include "webrtc/base/dscp.h" | 19 #include "webrtc/base/dscp.h" |
21 #include "webrtc/base/fakenetwork.h" | 20 #include "webrtc/base/fakenetwork.h" |
22 #include "webrtc/base/firewallsocketserver.h" | 21 #include "webrtc/base/firewallsocketserver.h" |
23 #include "webrtc/base/gunit.h" | 22 #include "webrtc/base/gunit.h" |
24 #include "webrtc/base/helpers.h" | 23 #include "webrtc/base/helpers.h" |
25 #include "webrtc/base/logging.h" | 24 #include "webrtc/base/logging.h" |
26 #include "webrtc/base/natserver.h" | 25 #include "webrtc/base/natserver.h" |
27 #include "webrtc/base/natsocketfactory.h" | 26 #include "webrtc/base/natsocketfactory.h" |
28 #include "webrtc/base/physicalsocketserver.h" | 27 #include "webrtc/base/physicalsocketserver.h" |
29 #include "webrtc/base/proxyserver.h" | 28 #include "webrtc/base/proxyserver.h" |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 std::string ice_ufrag_ep2_cd1_ch = kIceUfrag[1]; | 280 std::string ice_ufrag_ep2_cd1_ch = kIceUfrag[1]; |
282 std::string ice_pwd_ep2_cd1_ch = kIcePwd[1]; | 281 std::string ice_pwd_ep2_cd1_ch = kIcePwd[1]; |
283 ep1_.cd1_.ch_.reset(CreateChannel( | 282 ep1_.cd1_.ch_.reset(CreateChannel( |
284 0, cricket::ICE_CANDIDATE_COMPONENT_DEFAULT, | 283 0, cricket::ICE_CANDIDATE_COMPONENT_DEFAULT, |
285 ice_ufrag_ep1_cd1_ch, ice_pwd_ep1_cd1_ch, | 284 ice_ufrag_ep1_cd1_ch, ice_pwd_ep1_cd1_ch, |
286 ice_ufrag_ep2_cd1_ch, ice_pwd_ep2_cd1_ch)); | 285 ice_ufrag_ep2_cd1_ch, ice_pwd_ep2_cd1_ch)); |
287 ep2_.cd1_.ch_.reset(CreateChannel( | 286 ep2_.cd1_.ch_.reset(CreateChannel( |
288 1, cricket::ICE_CANDIDATE_COMPONENT_DEFAULT, | 287 1, cricket::ICE_CANDIDATE_COMPONENT_DEFAULT, |
289 ice_ufrag_ep2_cd1_ch, ice_pwd_ep2_cd1_ch, | 288 ice_ufrag_ep2_cd1_ch, ice_pwd_ep2_cd1_ch, |
290 ice_ufrag_ep1_cd1_ch, ice_pwd_ep1_cd1_ch)); | 289 ice_ufrag_ep1_cd1_ch, ice_pwd_ep1_cd1_ch)); |
291 ep1_.cd1_.ch_->MaybeStartGathering(); | |
292 ep2_.cd1_.ch_->MaybeStartGathering(); | |
293 if (num == 2) { | 290 if (num == 2) { |
294 std::string ice_ufrag_ep1_cd2_ch = kIceUfrag[2]; | 291 std::string ice_ufrag_ep1_cd2_ch = kIceUfrag[2]; |
295 std::string ice_pwd_ep1_cd2_ch = kIcePwd[2]; | 292 std::string ice_pwd_ep1_cd2_ch = kIcePwd[2]; |
296 std::string ice_ufrag_ep2_cd2_ch = kIceUfrag[3]; | 293 std::string ice_ufrag_ep2_cd2_ch = kIceUfrag[3]; |
297 std::string ice_pwd_ep2_cd2_ch = kIcePwd[3]; | 294 std::string ice_pwd_ep2_cd2_ch = kIcePwd[3]; |
298 ep1_.cd2_.ch_.reset(CreateChannel( | 295 ep1_.cd2_.ch_.reset(CreateChannel( |
299 0, cricket::ICE_CANDIDATE_COMPONENT_DEFAULT, | 296 0, cricket::ICE_CANDIDATE_COMPONENT_DEFAULT, |
300 ice_ufrag_ep1_cd2_ch, ice_pwd_ep1_cd2_ch, | 297 ice_ufrag_ep1_cd2_ch, ice_pwd_ep1_cd2_ch, |
301 ice_ufrag_ep2_cd2_ch, ice_pwd_ep2_cd2_ch)); | 298 ice_ufrag_ep2_cd2_ch, ice_pwd_ep2_cd2_ch)); |
302 ep2_.cd2_.ch_.reset(CreateChannel( | 299 ep2_.cd2_.ch_.reset(CreateChannel( |
303 1, cricket::ICE_CANDIDATE_COMPONENT_DEFAULT, | 300 1, cricket::ICE_CANDIDATE_COMPONENT_DEFAULT, |
304 ice_ufrag_ep2_cd2_ch, ice_pwd_ep2_cd2_ch, | 301 ice_ufrag_ep2_cd2_ch, ice_pwd_ep2_cd2_ch, |
305 ice_ufrag_ep1_cd2_ch, ice_pwd_ep1_cd2_ch)); | 302 ice_ufrag_ep1_cd2_ch, ice_pwd_ep1_cd2_ch)); |
306 ep1_.cd2_.ch_->MaybeStartGathering(); | |
307 ep2_.cd2_.ch_->MaybeStartGathering(); | |
308 } | 303 } |
309 } | 304 } |
310 cricket::P2PTransportChannel* CreateChannel( | 305 cricket::P2PTransportChannel* CreateChannel( |
311 int endpoint, | 306 int endpoint, |
312 int component, | 307 int component, |
313 const std::string& local_ice_ufrag, | 308 const std::string& local_ice_ufrag, |
314 const std::string& local_ice_pwd, | 309 const std::string& local_ice_pwd, |
315 const std::string& remote_ice_ufrag, | 310 const std::string& remote_ice_ufrag, |
316 const std::string& remote_ice_pwd) { | 311 const std::string& remote_ice_pwd) { |
317 cricket::P2PTransportChannel* channel = new cricket::P2PTransportChannel( | 312 cricket::P2PTransportChannel* channel = new cricket::P2PTransportChannel( |
318 "test content name", component, GetAllocator(endpoint)); | 313 "test content name", component, GetAllocator(endpoint)); |
319 channel->SignalCandidateGathered.connect( | 314 channel->SignalCandidateGathered.connect( |
320 this, &P2PTransportChannelTestBase::OnCandidateGathered); | 315 this, &P2PTransportChannelTestBase::OnCandidateGathered); |
321 channel->SignalCandidatesRemoved.connect( | 316 channel->SignalCandidatesRemoved.connect( |
322 this, &P2PTransportChannelTestBase::OnCandidatesRemoved); | 317 this, &P2PTransportChannelTestBase::OnCandidatesRemoved); |
323 channel->SignalReadPacket.connect( | 318 channel->SignalReadPacket.connect( |
324 this, &P2PTransportChannelTestBase::OnReadPacket); | 319 this, &P2PTransportChannelTestBase::OnReadPacket); |
325 channel->SignalRoleConflict.connect( | 320 channel->SignalRoleConflict.connect( |
326 this, &P2PTransportChannelTestBase::OnRoleConflict); | 321 this, &P2PTransportChannelTestBase::OnRoleConflict); |
327 channel->SetIceCredentials(local_ice_ufrag, local_ice_pwd); | 322 channel->SetIceCredentials(local_ice_ufrag, local_ice_pwd); |
328 if (clear_remote_candidates_ufrag_pwd_) { | 323 if (clear_remote_candidates_ufrag_pwd_) { |
329 // This only needs to be set if we're clearing them from the | 324 // This only needs to be set if we're clearing them from the |
330 // candidates. Some unit tests rely on this not being set. | 325 // candidates. Some unit tests rely on this not being set. |
331 channel->SetRemoteIceCredentials(remote_ice_ufrag, remote_ice_pwd); | 326 channel->SetRemoteIceCredentials(remote_ice_ufrag, remote_ice_pwd); |
332 } | 327 } |
333 channel->SetIceRole(GetEndpoint(endpoint)->ice_role()); | 328 channel->SetIceRole(GetEndpoint(endpoint)->ice_role()); |
334 channel->SetIceTiebreaker(GetEndpoint(endpoint)->GetIceTiebreaker()); | 329 channel->SetIceTiebreaker(GetEndpoint(endpoint)->GetIceTiebreaker()); |
335 channel->Connect(); | 330 channel->Connect(); |
| 331 channel->MaybeStartGathering(); |
336 return channel; | 332 return channel; |
337 } | 333 } |
338 void DestroyChannels() { | 334 void DestroyChannels() { |
339 ep1_.cd1_.ch_.reset(); | 335 ep1_.cd1_.ch_.reset(); |
340 ep2_.cd1_.ch_.reset(); | 336 ep2_.cd1_.ch_.reset(); |
341 ep1_.cd2_.ch_.reset(); | 337 ep1_.cd2_.ch_.reset(); |
342 ep2_.cd2_.ch_.reset(); | 338 ep2_.cd2_.ch_.reset(); |
343 } | 339 } |
344 cricket::P2PTransportChannel* ep1_ch1() { return ep1_.cd1_.ch_.get(); } | 340 cricket::P2PTransportChannel* ep1_ch1() { return ep1_.cd1_.ch_.get(); } |
345 cricket::P2PTransportChannel* ep1_ch2() { return ep1_.cd2_.ch_.get(); } | 341 cricket::P2PTransportChannel* ep1_ch2() { return ep1_.cd2_.ch_.get(); } |
(...skipping 1198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1544 1000); | 1540 1000); |
1545 EXPECT_EQ(cricket::IceGatheringState::kIceGatheringGathering, | 1541 EXPECT_EQ(cricket::IceGatheringState::kIceGatheringGathering, |
1546 ep1_ch1()->gathering_state()); | 1542 ep1_ch1()->gathering_state()); |
1547 // By now, ep2 should have completed gathering. | 1543 // By now, ep2 should have completed gathering. |
1548 EXPECT_EQ(cricket::IceGatheringState::kIceGatheringComplete, | 1544 EXPECT_EQ(cricket::IceGatheringState::kIceGatheringComplete, |
1549 ep2_ch1()->gathering_state()); | 1545 ep2_ch1()->gathering_state()); |
1550 | 1546 |
1551 DestroyChannels(); | 1547 DestroyChannels(); |
1552 } | 1548 } |
1553 | 1549 |
1554 // Test that a connection succeeds when the P2PTransportChannel uses a pooled | |
1555 // PortAllocatorSession that has not yet finished gathering candidates. | |
1556 TEST_F(P2PTransportChannelTest, TestUsingPooledSessionBeforeDoneGathering) { | |
1557 ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, | |
1558 kDefaultPortAllocatorFlags); | |
1559 // First create a pooled session for each endpoint. | |
1560 auto& allocator_1 = GetEndpoint(0)->allocator_; | |
1561 auto& allocator_2 = GetEndpoint(1)->allocator_; | |
1562 int pool_size = 1; | |
1563 allocator_1->SetConfiguration(allocator_1->stun_servers(), | |
1564 allocator_1->turn_servers(), pool_size); | |
1565 allocator_2->SetConfiguration(allocator_2->stun_servers(), | |
1566 allocator_2->turn_servers(), pool_size); | |
1567 const cricket::PortAllocatorSession* pooled_session_1 = | |
1568 allocator_1->GetPooledSession(); | |
1569 const cricket::PortAllocatorSession* pooled_session_2 = | |
1570 allocator_2->GetPooledSession(); | |
1571 ASSERT_NE(nullptr, pooled_session_1); | |
1572 ASSERT_NE(nullptr, pooled_session_2); | |
1573 // Sanity check that pooled sessions haven't gathered anything yet. | |
1574 EXPECT_TRUE(pooled_session_1->ReadyPorts().empty()); | |
1575 EXPECT_TRUE(pooled_session_1->ReadyCandidates().empty()); | |
1576 EXPECT_TRUE(pooled_session_2->ReadyPorts().empty()); | |
1577 EXPECT_TRUE(pooled_session_2->ReadyCandidates().empty()); | |
1578 // Now let the endpoints connect and try exchanging some data. | |
1579 CreateChannels(1); | |
1580 EXPECT_TRUE_WAIT_MARGIN(ep1_ch1() != NULL && ep2_ch1() != NULL && | |
1581 ep1_ch1()->receiving() && ep1_ch1()->writable() && | |
1582 ep2_ch1()->receiving() && ep2_ch1()->writable(), | |
1583 1000, 1000); | |
1584 TestSendRecv(1); | |
1585 // Make sure the P2PTransportChannels are actually using ports from the | |
1586 // pooled sessions. | |
1587 auto pooled_ports_1 = pooled_session_1->ReadyPorts(); | |
1588 auto pooled_ports_2 = pooled_session_2->ReadyPorts(); | |
1589 EXPECT_NE(pooled_ports_1.end(), | |
1590 std::find(pooled_ports_1.begin(), pooled_ports_1.end(), | |
1591 ep1_ch1()->best_connection()->port())); | |
1592 EXPECT_NE(pooled_ports_2.end(), | |
1593 std::find(pooled_ports_2.begin(), pooled_ports_2.end(), | |
1594 ep2_ch1()->best_connection()->port())); | |
1595 } | |
1596 | |
1597 // Test that a connection succeeds when the P2PTransportChannel uses a pooled | |
1598 // PortAllocatorSession that already finished gathering candidates. | |
1599 TEST_F(P2PTransportChannelTest, TestUsingPooledSessionAfterDoneGathering) { | |
1600 ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, | |
1601 kDefaultPortAllocatorFlags); | |
1602 // First create a pooled session for each endpoint. | |
1603 auto& allocator_1 = GetEndpoint(0)->allocator_; | |
1604 auto& allocator_2 = GetEndpoint(1)->allocator_; | |
1605 int pool_size = 1; | |
1606 allocator_1->SetConfiguration(allocator_1->stun_servers(), | |
1607 allocator_1->turn_servers(), pool_size); | |
1608 allocator_2->SetConfiguration(allocator_2->stun_servers(), | |
1609 allocator_2->turn_servers(), pool_size); | |
1610 const cricket::PortAllocatorSession* pooled_session_1 = | |
1611 allocator_1->GetPooledSession(); | |
1612 const cricket::PortAllocatorSession* pooled_session_2 = | |
1613 allocator_2->GetPooledSession(); | |
1614 ASSERT_NE(nullptr, pooled_session_1); | |
1615 ASSERT_NE(nullptr, pooled_session_2); | |
1616 // Wait for the pooled sessions to finish gathering before the | |
1617 // P2PTransportChannels try to use them. | |
1618 EXPECT_TRUE_WAIT(pooled_session_1->CandidatesAllocationDone() && | |
1619 pooled_session_2->CandidatesAllocationDone(), | |
1620 kDefaultTimeout); | |
1621 // Now let the endpoints connect and try exchanging some data. | |
1622 CreateChannels(1); | |
1623 EXPECT_TRUE_WAIT_MARGIN(ep1_ch1() != NULL && ep2_ch1() != NULL && | |
1624 ep1_ch1()->receiving() && ep1_ch1()->writable() && | |
1625 ep2_ch1()->receiving() && ep2_ch1()->writable(), | |
1626 1000, 1000); | |
1627 TestSendRecv(1); | |
1628 // Make sure the P2PTransportChannels are actually using ports from the | |
1629 // pooled sessions. | |
1630 auto pooled_ports_1 = pooled_session_1->ReadyPorts(); | |
1631 auto pooled_ports_2 = pooled_session_2->ReadyPorts(); | |
1632 EXPECT_NE(pooled_ports_1.end(), | |
1633 std::find(pooled_ports_1.begin(), pooled_ports_1.end(), | |
1634 ep1_ch1()->best_connection()->port())); | |
1635 EXPECT_NE(pooled_ports_2.end(), | |
1636 std::find(pooled_ports_2.begin(), pooled_ports_2.end(), | |
1637 ep2_ch1()->best_connection()->port())); | |
1638 } | |
1639 | |
1640 // Test what happens when we have 2 users behind the same NAT. This can lead | 1550 // Test what happens when we have 2 users behind the same NAT. This can lead |
1641 // to interesting behavior because the STUN server will only give out the | 1551 // to interesting behavior because the STUN server will only give out the |
1642 // address of the outermost NAT. | 1552 // address of the outermost NAT. |
1643 class P2PTransportChannelSameNatTest : public P2PTransportChannelTestBase { | 1553 class P2PTransportChannelSameNatTest : public P2PTransportChannelTestBase { |
1644 protected: | 1554 protected: |
1645 void ConfigureEndpoints(Config nat_type, Config config1, Config config2) { | 1555 void ConfigureEndpoints(Config nat_type, Config config1, Config config2) { |
1646 ASSERT(nat_type >= NAT_FULL_CONE && nat_type <= NAT_SYMMETRIC); | 1556 ASSERT(nat_type >= NAT_FULL_CONE && nat_type <= NAT_SYMMETRIC); |
1647 rtc::NATSocketServer::Translator* outer_nat = | 1557 rtc::NATSocketServer::Translator* outer_nat = |
1648 nat()->AddTranslator(kPublicAddrs[0], kNatAddrs[0], | 1558 nat()->AddTranslator(kPublicAddrs[0], kNatAddrs[0], |
1649 static_cast<rtc::NATType>(nat_type - NAT_FULL_CONE)); | 1559 static_cast<rtc::NATType>(nat_type - NAT_FULL_CONE)); |
(...skipping 1244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2894 | 2804 |
2895 // TCP Relay/Relay is the next. | 2805 // TCP Relay/Relay is the next. |
2896 VerifyNextPingableConnection(cricket::RELAY_PORT_TYPE, | 2806 VerifyNextPingableConnection(cricket::RELAY_PORT_TYPE, |
2897 cricket::RELAY_PORT_TYPE, | 2807 cricket::RELAY_PORT_TYPE, |
2898 cricket::TCP_PROTOCOL_NAME); | 2808 cricket::TCP_PROTOCOL_NAME); |
2899 | 2809 |
2900 // Finally, Local/Relay will be pinged. | 2810 // Finally, Local/Relay will be pinged. |
2901 VerifyNextPingableConnection(cricket::LOCAL_PORT_TYPE, | 2811 VerifyNextPingableConnection(cricket::LOCAL_PORT_TYPE, |
2902 cricket::RELAY_PORT_TYPE); | 2812 cricket::RELAY_PORT_TYPE); |
2903 } | 2813 } |
OLD | NEW |