Index: webrtc/p2p/client/portallocator_unittest.cc |
diff --git a/webrtc/p2p/client/portallocator_unittest.cc b/webrtc/p2p/client/portallocator_unittest.cc |
index 017c37a6f6e673cee11f721905fdbe93761b58ac..6940ea17ad2272b5265260f2137f480da1028ef3 100644 |
--- a/webrtc/p2p/client/portallocator_unittest.cc |
+++ b/webrtc/p2p/client/portallocator_unittest.cc |
@@ -40,7 +40,8 @@ static const SocketAddress kPrivateAddr2("192.168.1.12", 0); |
static const SocketAddress kClientIPv6Addr( |
"2401:fa00:4:1000:be30:5bff:fee5:c3", 0); |
static const SocketAddress kClientAddr2("22.22.22.22", 0); |
-static const SocketAddress kNatAddr("77.77.77.77", rtc::NAT_SERVER_PORT); |
+static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT); |
+static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT); |
static const SocketAddress kRemoteClientAddr("22.22.22.22", 0); |
static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT); |
static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000); |
@@ -85,7 +86,7 @@ class PortAllocatorTest : public testing::Test, public sigslot::has_slots<> { |
vss_(new rtc::VirtualSocketServer(pss_.get())), |
fss_(new rtc::FirewallSocketServer(vss_.get())), |
ss_scope_(fss_.get()), |
- nat_factory_(vss_.get(), kNatAddr), |
+ nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr), |
nat_socket_factory_(&nat_factory_), |
stun_server_(cricket::TestStunServer::Create(Thread::Current(), |
kStunAddr)), |
@@ -113,7 +114,8 @@ class PortAllocatorTest : public testing::Test, public sigslot::has_slots<> { |
} |
void ResetWithNatServer(const rtc::SocketAddress& stun_server) { |
nat_server_.reset(new rtc::NATServer( |
- rtc::NAT_OPEN_CONE, vss_.get(), kNatAddr, vss_.get(), kNatAddr)); |
+ rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(), |
+ rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0))); |
ServerAddresses stun_servers; |
if (!stun_server.IsNil()) { |
@@ -241,7 +243,7 @@ class PortAllocatorTest : public testing::Test, public sigslot::has_slots<> { |
EXPECT_PRED5(CheckCandidate, candidates_[0], |
cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", |
- rtc::SocketAddress(kNatAddr.ipaddr(), 0)); |
+ rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)); |
EXPECT_EQ( |
rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()), |
candidates_[0].related_address()); |
@@ -249,7 +251,7 @@ class PortAllocatorTest : public testing::Test, public sigslot::has_slots<> { |
EXPECT_PRED5(CheckCandidate, candidates_[1], |
cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", |
rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)); |
- EXPECT_EQ(kNatAddr.ipaddr(), candidates_[1].related_address().ipaddr()); |
+ EXPECT_EQ(kNatUdpAddr.ipaddr(), candidates_[1].related_address().ipaddr()); |
} |
protected: |
@@ -779,12 +781,12 @@ TEST_F(PortAllocatorTest, TestSharedSocketWithNat) { |
cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr); |
EXPECT_PRED5(CheckCandidate, candidates_[1], |
cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", |
- rtc::SocketAddress(kNatAddr.ipaddr(), 0)); |
+ rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)); |
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); |
EXPECT_EQ(3U, candidates_.size()); |
} |
-// Test TURN port in shared socket mode with UDP and TCP TURN server adderesses. |
+// Test TURN port in shared socket mode with UDP and TCP TURN server addresses. |
TEST_F(PortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) { |
turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP); |
AddInterface(kClientAddr); |
@@ -865,7 +867,7 @@ TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurn) { |
cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr); |
EXPECT_PRED5(CheckCandidate, candidates_[1], |
cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", |
- rtc::SocketAddress(kNatAddr.ipaddr(), 0)); |
+ rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)); |
EXPECT_PRED5(CheckCandidate, candidates_[2], |
cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", |
rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)); |
@@ -903,7 +905,7 @@ TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) { |
cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr); |
EXPECT_PRED5(CheckCandidate, candidates_[1], |
cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", |
- rtc::SocketAddress(kNatAddr.ipaddr(), 0)); |
+ rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)); |
EXPECT_PRED5(CheckCandidate, candidates_[2], |
cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", |
rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)); |
@@ -916,6 +918,111 @@ TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) { |
EXPECT_EQ(1U, ports_[1]->Candidates().size()); |
} |
+// Test that when only a TCP TURN server is available, we do NOT use it as |
+// a UDP STUN server, as this could leak our IP address. Thus we should only |
+// expect two ports, a UDPPort and TurnPort. |
+TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) { |
+ turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP); |
+ AddInterface(kClientAddr); |
+ ResetWithNatServer(rtc::SocketAddress()); |
+ AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr); |
+ |
+ allocator_->set_flags(allocator().flags() | |
+ cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG | |
+ cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
+ cricket::PORTALLOCATOR_DISABLE_TCP); |
+ |
+ EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); |
+ session_->StartGettingPorts(); |
+ |
+ ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout); |
+ ASSERT_EQ(2U, ports_.size()); |
+ EXPECT_PRED5(CheckCandidate, candidates_[0], |
+ cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", |
+ kClientAddr); |
+ EXPECT_PRED5(CheckCandidate, candidates_[1], |
+ cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", |
+ rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)); |
+ EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); |
+ EXPECT_EQ(2U, candidates_.size()); |
+ EXPECT_EQ(1U, ports_[0]->Candidates().size()); |
+ EXPECT_EQ(1U, ports_[1]->Candidates().size()); |
+} |
+ |
+// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the |
+// TURN server is used as the STUN server and we get 'local', 'stun', and |
+// 'relay' candidates. |
+// TODO(deadbeef): Remove this test when support for non-shared socket mode |
+// is removed. |
+TEST_F(PortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) { |
+ AddInterface(kClientAddr); |
+ // Use an empty SocketAddress to add a NAT without STUN server. |
+ ResetWithNatServer(SocketAddress()); |
+ AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress()); |
+ |
+ allocator_->set_flags(allocator().flags() | |
+ cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG | |
+ cricket::PORTALLOCATOR_DISABLE_TCP); |
+ |
+ EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); |
+ session_->StartGettingPorts(); |
+ |
+ ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout); |
+ ASSERT_EQ(3U, ports_.size()); |
+ EXPECT_PRED5(CheckCandidate, candidates_[0], |
+ cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", |
+ kClientAddr); |
+ EXPECT_PRED5(CheckCandidate, candidates_[1], |
+ cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", |
+ rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)); |
+ EXPECT_PRED5(CheckCandidate, candidates_[2], |
+ cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", |
+ rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)); |
+ // Not using shared socket, so the STUN request's server reflexive address |
+ // should be different than the TURN request's server reflexive address. |
+ EXPECT_NE(candidates_[2].related_address(), candidates_[1].address()); |
+ |
+ EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); |
+ EXPECT_EQ(3U, candidates_.size()); |
+ EXPECT_EQ(1U, ports_[0]->Candidates().size()); |
+ EXPECT_EQ(1U, ports_[1]->Candidates().size()); |
+ EXPECT_EQ(1U, ports_[2]->Candidates().size()); |
+} |
+ |
+// Test that even when both a STUN and TURN server are configured, the TURN |
+// server is used as a STUN server and we get a 'stun' candidate. |
+TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) { |
+ AddInterface(kClientAddr); |
+ // Configure with STUN server but destroy it, so we can ensure that it's |
+ // the TURN server actually being used as a STUN server. |
+ ResetWithNatServer(kStunAddr); |
+ stun_server_.reset(); |
+ AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress()); |
+ |
+ allocator_->set_flags(allocator().flags() | |
+ cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG | |
+ cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
+ cricket::PORTALLOCATOR_DISABLE_TCP); |
+ |
+ EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); |
+ session_->StartGettingPorts(); |
+ |
+ ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout); |
+ EXPECT_PRED5(CheckCandidate, candidates_[0], |
+ cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", |
+ kClientAddr); |
+ EXPECT_PRED5(CheckCandidate, candidates_[1], |
+ cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", |
+ rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)); |
+ EXPECT_PRED5(CheckCandidate, candidates_[2], |
+ cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", |
+ rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)); |
+ EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address()); |
+ |
+ // Don't bother waiting for STUN timeout, since we already verified |
+ // that we got a STUN candidate from the TURN server. |
+} |
+ |
// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled |
// and fail to generate STUN candidate, local UDP candidate is generated |
// properly. |