Use fake clock in some more networks tests.

BUG=b/34822484

Review-Url: https://codereview.webrtc.org/2680233002
Cr-Commit-Position: refs/heads/master@{#16502}
This commit is contained in:
pthatcher 2017-02-08 13:18:00 -08:00 committed by Commit bot
parent 4da058c0dd
commit 1749bc372e
8 changed files with 324 additions and 223 deletions

View File

@ -56,6 +56,16 @@ class ScopedFakeClock : public FakeClock {
ClockInterface* prev_clock_;
};
// Helper class to "undo" the fake clock temporarily.
class ScopedRealClock {
public:
ScopedRealClock();
~ScopedRealClock();
private:
ClockInterface* prev_clock_;
};
} // namespace rtc
#endif // WEBRTC_BASE_FAKECLOCK_H_

View File

@ -92,6 +92,7 @@
// Wait until "ex" is true, or "timeout" expires, using fake clock where
// messages are processed every millisecond.
// TODO(pthatcher): Allow tests to control how many milliseconds to advance.
#define SIMULATED_WAIT(ex, timeout, clock) \
for (int64_t start = rtc::TimeMillis(); \
!(ex) && rtc::TimeMillis() < start + (timeout);) { \

View File

@ -146,9 +146,10 @@ void MessageQueueManager::ProcessAllMessageQueuesInternal() {
{
DebugNonReentrantCritScope cs(&crit_, &locked_);
for (MessageQueue* queue : message_queues_) {
if (queue->IsQuitting()) {
// If the queue is quitting, it's done processing messages so it can
// be ignored. If we tried to post a message to it, it would be dropped.
if (!queue->IsProcessingMessages()) {
// If the queue is not processing messages, it can
// be ignored. If we tried to post a message to it, it would be dropped
// or ignored.
continue;
}
queue->PostDelayed(RTC_FROM_HERE, 0, nullptr, MQID_DISPOSE,
@ -251,6 +252,10 @@ bool MessageQueue::IsQuitting() {
return AtomicOps::AcquireLoad(&stop_) != 0;
}
bool MessageQueue::IsProcessingMessages() {
return !IsQuitting();
}
void MessageQueue::Restart() {
AtomicOps::ReleaseStore(&stop_, 0);
}

View File

@ -209,6 +209,10 @@ class MessageQueue {
virtual void Quit();
virtual bool IsQuitting();
virtual void Restart();
// Not all message queues actually process messages (such as SignalThread).
// In those cases, it's important to know, before posting, that it won't be
// Processed. Normally, this would be true until IsQuitting() is true.
virtual bool IsProcessingMessages();
// Get() will process I/O until:
// 1) A message is available (returns true)

View File

@ -147,4 +147,8 @@ void SignalThread::OnMainThreadDestroyed() {
main_ = NULL;
}
bool SignalThread::Worker::IsProcessingMessages() {
return false;
}
} // namespace rtc

View File

@ -111,6 +111,7 @@ class SignalThread
parent_(parent) {}
~Worker() override;
void Run() override;
bool IsProcessingMessages() override;
private:
SignalThread* parent_;

View File

@ -43,10 +43,9 @@ static const int kHighCostPortKeepaliveLifetimeMs = 2 * 60 * 1000;
// Tests connecting a StunPort to a fake STUN server (cricket::StunServer)
// TODO: Use a VirtualSocketServer here. We have to use a
// PhysicalSocketServer right now since DNS is not part of SocketServer yet.
class StunPortTest : public testing::Test,
public sigslot::has_slots<> {
class StunPortTestBase : public testing::Test, public sigslot::has_slots<> {
public:
StunPortTest()
StunPortTestBase()
: pss_(new rtc::PhysicalSocketServer),
ss_(new rtc::VirtualSocketServer(pss_.get())),
ss_scope_(ss_.get()),
@ -58,7 +57,7 @@ class StunPortTest : public testing::Test,
kStunAddr2)),
done_(false),
error_(false),
stun_keepalive_delay_(0),
stun_keepalive_delay_(1),
stun_keepalive_lifetime_(-1) {}
cricket::UDPPort* port() const { return stun_port_.get(); }
@ -87,16 +86,15 @@ class StunPortTest : public testing::Test,
stun_port_->set_stun_keepalive_lifetime(stun_keepalive_lifetime_);
}
stun_port_->SignalPortComplete.connect(this,
&StunPortTest::OnPortComplete);
stun_port_->SignalPortError.connect(this,
&StunPortTest::OnPortError);
&StunPortTestBase::OnPortComplete);
stun_port_->SignalPortError.connect(this, &StunPortTestBase::OnPortError);
}
void CreateSharedUdpPort(const rtc::SocketAddress& server_addr) {
socket_.reset(socket_factory_.CreateUdpSocket(
rtc::SocketAddress(kLocalAddr.ipaddr(), 0), 0, 0));
ASSERT_TRUE(socket_ != NULL);
socket_->SignalReadPacket.connect(this, &StunPortTest::OnReadPacket);
socket_->SignalReadPacket.connect(this, &StunPortTestBase::OnReadPacket);
stun_port_.reset(cricket::UDPPort::Create(
rtc::Thread::Current(), &socket_factory_,
&network_, socket_.get(),
@ -107,9 +105,8 @@ class StunPortTest : public testing::Test,
stun_servers.insert(server_addr);
stun_port_->set_server_addresses(stun_servers);
stun_port_->SignalPortComplete.connect(this,
&StunPortTest::OnPortComplete);
stun_port_->SignalPortError.connect(this,
&StunPortTest::OnPortError);
&StunPortTestBase::OnPortComplete);
stun_port_->SignalPortError.connect(this, &StunPortTestBase::OnPortError);
}
void PrepareAddress() {
@ -176,6 +173,15 @@ class StunPortTest : public testing::Test,
int stun_keepalive_lifetime_;
};
class StunPortTestWithRealClock : public StunPortTestBase {};
class FakeClockBase {
public:
rtc::ScopedFakeClock fake_clock;
};
class StunPortTest : public FakeClockBase, public StunPortTestBase {};
// Test that we can create a STUN port.
TEST_F(StunPortTest, TestCreateStunPort) {
CreateStunPort(kStunAddr1);
@ -194,7 +200,7 @@ TEST_F(StunPortTest, TestCreateUdpPort) {
TEST_F(StunPortTest, TestPrepareAddress) {
CreateStunPort(kStunAddr1);
PrepareAddress();
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
ASSERT_EQ(1U, port()->Candidates().size());
EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
@ -206,7 +212,7 @@ TEST_F(StunPortTest, TestPrepareAddress) {
TEST_F(StunPortTest, TestPrepareAddressFail) {
CreateStunPort(kBadAddr);
PrepareAddress();
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
EXPECT_TRUE(error());
EXPECT_EQ(0U, port()->Candidates().size());
}
@ -215,14 +221,14 @@ TEST_F(StunPortTest, TestPrepareAddressFail) {
TEST_F(StunPortTest, TestPrepareAddressHostname) {
CreateStunPort(kStunHostnameAddr);
PrepareAddress();
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
ASSERT_EQ(1U, port()->Candidates().size());
EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
EXPECT_EQ(kStunCandidatePriority, port()->Candidates()[0].priority());
}
// Test that we handle hostname lookup failures properly.
TEST_F(StunPortTest, TestPrepareAddressHostnameFail) {
TEST_F(StunPortTestWithRealClock, TestPrepareAddressHostnameFail) {
CreateStunPort(kBadHostnameAddr);
PrepareAddress();
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
@ -236,20 +242,18 @@ TEST_F(StunPortTest, TestKeepAliveResponse) {
SetKeepaliveDelay(500); // 500ms of keepalive delay.
CreateStunPort(kStunHostnameAddr);
PrepareAddress();
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
ASSERT_EQ(1U, port()->Candidates().size());
EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
// Waiting for 1 seond, which will allow us to process
// response for keepalive binding request. 500 ms is the keepalive delay.
rtc::Thread::Current()->ProcessMessages(1000);
ASSERT_EQ(1U, port()->Candidates().size());
SIMULATED_WAIT(false, 1000, fake_clock);
EXPECT_EQ(1U, port()->Candidates().size());
}
// Test that a local candidate can be generated using a shared socket.
TEST_F(StunPortTest, TestSharedSocketPrepareAddress) {
CreateSharedUdpPort(kStunAddr1);
PrepareAddress();
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
ASSERT_EQ(1U, port()->Candidates().size());
EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
}
@ -257,7 +261,8 @@ TEST_F(StunPortTest, TestSharedSocketPrepareAddress) {
// Test that we still a get a local candidate with invalid stun server hostname.
// Also verifing that UDPPort can receive packets when stun address can't be
// resolved.
TEST_F(StunPortTest, TestSharedSocketPrepareAddressInvalidHostname) {
TEST_F(StunPortTestWithRealClock,
TestSharedSocketPrepareAddressInvalidHostname) {
CreateSharedUdpPort(kBadHostnameAddr);
PrepareAddress();
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
@ -279,7 +284,7 @@ TEST_F(StunPortTest, TestNoDuplicatedAddressWithTwoStunServers) {
CreateStunPort(stun_servers);
EXPECT_EQ("stun", port()->Type());
PrepareAddress();
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
EXPECT_EQ(1U, port()->Candidates().size());
EXPECT_EQ(port()->Candidates()[0].relay_protocol(), "");
}
@ -293,7 +298,7 @@ TEST_F(StunPortTest, TestMultipleStunServersWithBadServer) {
CreateStunPort(stun_servers);
EXPECT_EQ("stun", port()->Type());
PrepareAddress();
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
EXPECT_EQ(1U, port()->Candidates().size());
}
@ -311,7 +316,7 @@ TEST_F(StunPortTest, TestTwoCandidatesWithTwoStunServersAcrossNat) {
CreateStunPort(stun_servers);
EXPECT_EQ("stun", port()->Type());
PrepareAddress();
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
EXPECT_EQ(2U, port()->Candidates().size());
EXPECT_EQ(port()->Candidates()[0].relay_protocol(), "");
EXPECT_EQ(port()->Candidates()[1].relay_protocol(), "");
@ -360,9 +365,10 @@ TEST_F(StunPortTest, TestStunBindingRequestShortLifetime) {
SetKeepaliveLifetime(100);
CreateStunPort(kStunAddr1);
PrepareAddress();
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
EXPECT_TRUE_WAIT(!port()->HasPendingRequest(cricket::STUN_BINDING_REQUEST),
2000);
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
EXPECT_TRUE_SIMULATED_WAIT(
!port()->HasPendingRequest(cricket::STUN_BINDING_REQUEST), 2000,
fake_clock);
}
// Test that by default, the STUN binding requests will last for a long time.
@ -370,7 +376,8 @@ TEST_F(StunPortTest, TestStunBindingRequestLongLifetime) {
SetKeepaliveDelay(101);
CreateStunPort(kStunAddr1);
PrepareAddress();
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
rtc::Thread::Current()->ProcessMessages(1000);
EXPECT_TRUE(port()->HasPendingRequest(cricket::STUN_BINDING_REQUEST));
EXPECT_TRUE_SIMULATED_WAIT(done(), kTimeoutMs, fake_clock);
EXPECT_TRUE_SIMULATED_WAIT(
port()->HasPendingRequest(cricket::STUN_BINDING_REQUEST), 1000,
fake_clock);
}

View File

@ -18,6 +18,7 @@
#include "webrtc/p2p/base/teststunserver.h"
#include "webrtc/p2p/base/testturnserver.h"
#include "webrtc/p2p/client/basicportallocator.h"
#include "webrtc/base/fakeclock.h"
#include "webrtc/base/fakenetwork.h"
#include "webrtc/base/firewallsocketserver.h"
#include "webrtc/base/gunit.h"
@ -107,10 +108,10 @@ std::ostream& operator<<(std::ostream& os,
return os;
}
class BasicPortAllocatorTest : public testing::Test,
public sigslot::has_slots<> {
class BasicPortAllocatorTestBase : public testing::Test,
public sigslot::has_slots<> {
public:
BasicPortAllocatorTest()
BasicPortAllocatorTestBase()
: pss_(new rtc::PhysicalSocketServer),
vss_(new rtc::VirtualSocketServer(pss_.get())),
fss_(new rtc::FirewallSocketServer(vss_.get())),
@ -243,15 +244,15 @@ class BasicPortAllocatorTest : public testing::Test,
std::unique_ptr<PortAllocatorSession> session =
allocator_->CreateSession(content_name, component, ice_ufrag, ice_pwd);
session->SignalPortReady.connect(this,
&BasicPortAllocatorTest::OnPortReady);
session->SignalPortsPruned.connect(this,
&BasicPortAllocatorTest::OnPortsPruned);
&BasicPortAllocatorTestBase::OnPortReady);
session->SignalPortsPruned.connect(
this, &BasicPortAllocatorTestBase::OnPortsPruned);
session->SignalCandidatesReady.connect(
this, &BasicPortAllocatorTest::OnCandidatesReady);
this, &BasicPortAllocatorTestBase::OnCandidatesReady);
session->SignalCandidatesRemoved.connect(
this, &BasicPortAllocatorTest::OnCandidatesRemoved);
this, &BasicPortAllocatorTestBase::OnCandidatesRemoved);
session->SignalCandidatesAllocationDone.connect(
this, &BasicPortAllocatorTest::OnCandidatesAllocationDone);
this, &BasicPortAllocatorTestBase::OnCandidatesAllocationDone);
return session;
}
@ -367,61 +368,6 @@ class BasicPortAllocatorTest : public testing::Test,
}
}
// This function starts the port/address gathering and check the existence of
// candidates as specified. When |expect_stun_candidate| is true,
// |stun_candidate_addr| carries the expected reflective address, which is
// also the related address for TURN candidate if it is expected. Otherwise,
// it should be ignore.
void CheckDisableAdapterEnumeration(
uint32_t total_ports,
const rtc::IPAddress& host_candidate_addr,
const rtc::IPAddress& stun_candidate_addr,
const rtc::IPAddress& relay_candidate_udp_transport_addr,
const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
rtc::IPAddress());
if (!session_) {
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
}
session_->set_flags(session_->flags() |
PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
PORTALLOCATOR_ENABLE_SHARED_SOCKET);
allocator().set_allow_tcp_listen(false);
session_->StartGettingPorts();
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
uint32_t total_candidates = 0;
if (!host_candidate_addr.IsNil()) {
EXPECT_PRED4(HasCandidate, candidates_, "local", "udp",
rtc::SocketAddress(kPrivateAddr.ipaddr(), 0));
++total_candidates;
}
if (!stun_candidate_addr.IsNil()) {
rtc::SocketAddress related_address(host_candidate_addr, 0);
if (host_candidate_addr.IsNil()) {
related_address.SetIP(rtc::GetAnyIP(stun_candidate_addr.family()));
}
EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "stun", "udp",
rtc::SocketAddress(stun_candidate_addr, 0), related_address);
++total_candidates;
}
if (!relay_candidate_udp_transport_addr.IsNil()) {
EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
rtc::SocketAddress(relay_candidate_udp_transport_addr, 0),
rtc::SocketAddress(stun_candidate_addr, 0));
++total_candidates;
}
if (!relay_candidate_tcp_transport_addr.IsNil()) {
EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0),
rtc::SocketAddress(stun_candidate_addr, 0));
++total_candidates;
}
EXPECT_EQ(total_candidates, candidates_.size());
EXPECT_EQ(total_ports, ports_.size());
}
rtc::VirtualSocketServer* virtual_socket_server() { return vss_.get(); }
protected:
@ -514,43 +460,89 @@ class BasicPortAllocatorTest : public testing::Test,
allocator().set_step_delay(kMinimumStepDelay);
}
void TestUdpTurnPortPrunesTcpTurnPort() {
turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
AddInterface(kClientAddr);
allocator_.reset(new BasicPortAllocator(&network_manager_));
allocator_->SetConfiguration(allocator_->stun_servers(),
allocator_->turn_servers(), 0, true);
AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
allocator_->set_step_delay(kMinimumStepDelay);
allocator_->set_flags(allocator().flags() |
PORTALLOCATOR_ENABLE_SHARED_SOCKET |
PORTALLOCATOR_DISABLE_TCP);
std::unique_ptr<rtc::PhysicalSocketServer> pss_;
std::unique_ptr<rtc::VirtualSocketServer> vss_;
std::unique_ptr<rtc::FirewallSocketServer> fss_;
rtc::SocketServerScope ss_scope_;
std::unique_ptr<rtc::NATServer> nat_server_;
rtc::NATSocketFactory nat_factory_;
std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
std::unique_ptr<TestStunServer> stun_server_;
TestRelayServer relay_server_;
TestTurnServer turn_server_;
rtc::FakeNetworkManager network_manager_;
std::unique_ptr<BasicPortAllocator> allocator_;
std::unique_ptr<PortAllocatorSession> session_;
std::vector<PortInterface*> ports_;
std::vector<Candidate> candidates_;
bool candidate_allocation_done_;
};
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
class BasicPortAllocatorTestWithRealClock : public BasicPortAllocatorTestBase {
};
class FakeClockBase {
public:
rtc::ScopedFakeClock fake_clock;
};
class BasicPortAllocatorTest : public FakeClockBase,
public BasicPortAllocatorTestBase {
public:
// This function starts the port/address gathering and check the existence of
// candidates as specified. When |expect_stun_candidate| is true,
// |stun_candidate_addr| carries the expected reflective address, which is
// also the related address for TURN candidate if it is expected. Otherwise,
// it should be ignore.
void CheckDisableAdapterEnumeration(
uint32_t total_ports,
const rtc::IPAddress& host_candidate_addr,
const rtc::IPAddress& stun_candidate_addr,
const rtc::IPAddress& relay_candidate_udp_transport_addr,
const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
rtc::IPAddress());
if (!session_) {
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
}
session_->set_flags(session_->flags() |
PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
PORTALLOCATOR_ENABLE_SHARED_SOCKET);
allocator().set_allow_tcp_listen(false);
session_->StartGettingPorts();
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
// Only 2 ports (one STUN and one TURN) are actually being used.
EXPECT_EQ(2U, session_->ReadyPorts().size());
// We have verified that each port, when it is added to |ports_|, it is
// found in |ready_ports|, and when it is pruned, it is not found in
// |ready_ports|, so we only need to verify the content in one of them.
EXPECT_EQ(2U, ports_.size());
EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_TCP, kClientAddr));
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
kDefaultAllocationTimeout, fake_clock);
// Now that we remove candidates when a TURN port is pruned, |candidates_|
// should only contains two candidates regardless whether the TCP TURN port
// is created before or after the UDP turn port.
EXPECT_EQ(2U, candidates_.size());
// There will only be 2 candidates in |ready_candidates| because it only
// includes the candidates in the ready ports.
const std::vector<Candidate>& ready_candidates =
session_->ReadyCandidates();
EXPECT_EQ(2U, ready_candidates.size());
EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
uint32_t total_candidates = 0;
if (!host_candidate_addr.IsNil()) {
EXPECT_PRED4(HasCandidate, candidates_, "local", "udp",
rtc::SocketAddress(kPrivateAddr.ipaddr(), 0));
++total_candidates;
}
if (!stun_candidate_addr.IsNil()) {
rtc::SocketAddress related_address(host_candidate_addr, 0);
if (host_candidate_addr.IsNil()) {
related_address.SetIP(rtc::GetAnyIP(stun_candidate_addr.family()));
}
EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "stun", "udp",
rtc::SocketAddress(stun_candidate_addr, 0), related_address);
++total_candidates;
}
if (!relay_candidate_udp_transport_addr.IsNil()) {
EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
rtc::SocketAddress(relay_candidate_udp_transport_addr, 0),
rtc::SocketAddress(stun_candidate_addr, 0));
++total_candidates;
}
if (!relay_candidate_tcp_transport_addr.IsNil()) {
EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0),
rtc::SocketAddress(stun_candidate_addr, 0));
++total_candidates;
}
EXPECT_EQ(total_candidates, candidates_.size());
EXPECT_EQ(total_ports, ports_.size());
}
void TestIPv6TurnPortPrunesIPv4TurnPort() {
@ -571,7 +563,8 @@ class BasicPortAllocatorTest : public testing::Test,
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
kDefaultAllocationTimeout, fake_clock);
// Three ports (one IPv4 STUN, one IPv6 STUN and one TURN) will be ready.
EXPECT_EQ(3U, session_->ReadyPorts().size());
EXPECT_EQ(3U, ports_.size());
@ -591,6 +584,46 @@ class BasicPortAllocatorTest : public testing::Test,
rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
}
void TestUdpTurnPortPrunesTcpTurnPort() {
turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
AddInterface(kClientAddr);
allocator_.reset(new BasicPortAllocator(&network_manager_));
allocator_->SetConfiguration(allocator_->stun_servers(),
allocator_->turn_servers(), 0, true);
AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
allocator_->set_step_delay(kMinimumStepDelay);
allocator_->set_flags(allocator().flags() |
PORTALLOCATOR_ENABLE_SHARED_SOCKET |
PORTALLOCATOR_DISABLE_TCP);
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
kDefaultAllocationTimeout, fake_clock);
// Only 2 ports (one STUN and one TURN) are actually being used.
EXPECT_EQ(2U, session_->ReadyPorts().size());
// We have verified that each port, when it is added to |ports_|, it is
// found in |ready_ports|, and when it is pruned, it is not found in
// |ready_ports|, so we only need to verify the content in one of them.
EXPECT_EQ(2U, ports_.size());
EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_TCP, kClientAddr));
// Now that we remove candidates when a TURN port is pruned, |candidates_|
// should only contains two candidates regardless whether the TCP TURN port
// is created before or after the UDP turn port.
EXPECT_EQ(2U, candidates_.size());
// There will only be 2 candidates in |ready_candidates| because it only
// includes the candidates in the ready ports.
const std::vector<Candidate>& ready_candidates =
session_->ReadyCandidates();
EXPECT_EQ(2U, ready_candidates.size());
EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
}
void TestEachInterfaceHasItsOwnTurnPorts() {
turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
@ -613,7 +646,8 @@ class BasicPortAllocatorTest : public testing::Test,
PORTALLOCATOR_ENABLE_IPV6);
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
kDefaultAllocationTimeout, fake_clock);
// 10 ports (4 STUN and 1 TURN ports on each interface) will be ready to
// use.
EXPECT_EQ(10U, session_->ReadyPorts().size());
@ -650,23 +684,6 @@ class BasicPortAllocatorTest : public testing::Test,
EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
}
std::unique_ptr<rtc::PhysicalSocketServer> pss_;
std::unique_ptr<rtc::VirtualSocketServer> vss_;
std::unique_ptr<rtc::FirewallSocketServer> fss_;
rtc::SocketServerScope ss_scope_;
std::unique_ptr<rtc::NATServer> nat_server_;
rtc::NATSocketFactory nat_factory_;
std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
std::unique_ptr<TestStunServer> stun_server_;
TestRelayServer relay_server_;
TestTurnServer turn_server_;
rtc::FakeNetworkManager network_manager_;
std::unique_ptr<BasicPortAllocator> allocator_;
std::unique_ptr<PortAllocatorSession> session_;
std::vector<PortInterface*> ports_;
std::vector<Candidate> candidates_;
bool candidate_allocation_done_;
};
// Tests that we can init the port allocator and create a session.
@ -702,7 +719,8 @@ TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) {
session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
PORTALLOCATOR_DISABLE_TCP);
session_->StartGettingPorts();
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
kDefaultAllocationTimeout, fake_clock);
EXPECT_EQ(4U, candidates_.size());
for (Candidate candidate : candidates_) {
EXPECT_LT(candidate.address().ip(), 0x12345604U);
@ -723,7 +741,8 @@ TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) {
session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
PORTALLOCATOR_DISABLE_TCP);
session_->StartGettingPorts();
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
kDefaultAllocationTimeout, fake_clock);
EXPECT_EQ(1U, candidates_.size());
EXPECT_EQ(0x12345602U, candidates_[0].address().ip());
}
@ -745,7 +764,8 @@ TEST_F(BasicPortAllocatorTest, TestGatherLowCostNetworkOnly) {
cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
kDefaultAllocationTimeout, fake_clock);
EXPECT_EQ(1U, candidates_.size());
EXPECT_TRUE(addr_wifi.EqualIPs(candidates_[0].address()));
@ -757,7 +777,8 @@ TEST_F(BasicPortAllocatorTest, TestGatherLowCostNetworkOnly) {
AddInterface(addr_unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
AddInterface(addr_unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
session_->StartGettingPorts();
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
kDefaultAllocationTimeout, fake_clock);
EXPECT_EQ(2U, candidates_.size());
EXPECT_TRUE((addr_unknown1.EqualIPs(candidates_[0].address()) &&
addr_unknown2.EqualIPs(candidates_[1].address())) ||
@ -770,7 +791,8 @@ TEST_F(BasicPortAllocatorTest, TestGatherLowCostNetworkOnly) {
candidate_allocation_done_ = false;
AddInterface(addr_wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
session_->StartGettingPorts();
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
kDefaultAllocationTimeout, fake_clock);
EXPECT_EQ(1U, candidates_.size());
EXPECT_TRUE(addr_wifi.EqualIPs(candidates_[0].address()));
}
@ -783,7 +805,8 @@ TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
PORTALLOCATOR_DISABLE_TCP);
session_->StartGettingPorts();
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
kDefaultAllocationTimeout, fake_clock);
EXPECT_EQ(1U, candidates_.size());
}
@ -792,7 +815,8 @@ TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
AddInterface(kClientAddr);
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
fake_clock);
EXPECT_EQ(4U, ports_.size());
EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
@ -813,7 +837,8 @@ TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
AddInterface(kClientAddr, if_name);
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
fake_clock);
EXPECT_EQ(4U, ports_.size());
EXPECT_TRUE(candidate_allocation_done_);
candidate_allocation_done_ = false;
@ -821,14 +846,16 @@ TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
ports_.clear();
RemoveInterface(kClientAddr);
ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
ASSERT_EQ_SIMULATED_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout,
fake_clock);
EXPECT_EQ(0U, ports_.size());
EXPECT_FALSE(candidate_allocation_done_);
// When the same interfaces are added again, new candidates/ports should be
// generated.
AddInterface(kClientAddr, if_name);
ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
fake_clock);
EXPECT_EQ(4U, ports_.size());
EXPECT_TRUE(candidate_allocation_done_);
}
@ -841,7 +868,8 @@ TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
AddInterface(kClientAddr, if_name);
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
fake_clock);
EXPECT_EQ(4U, ports_.size());
EXPECT_TRUE(candidate_allocation_done_);
session_->StopGettingPorts();
@ -849,13 +877,15 @@ TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
ports_.clear();
RemoveInterface(kClientAddr);
ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
ASSERT_EQ_SIMULATED_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout,
fake_clock);
EXPECT_EQ(0U, ports_.size());
// When the same interfaces are added again, new candidates/ports should not
// be generated because the session has stopped.
AddInterface(kClientAddr, if_name);
ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
ASSERT_EQ_SIMULATED_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout,
fake_clock);
EXPECT_EQ(0U, ports_.size());
EXPECT_TRUE(candidate_allocation_done_);
}
@ -866,17 +896,17 @@ TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
allocator_->set_step_delay(kDefaultStepDelay);
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
EXPECT_EQ(2U, ports_.size());
ASSERT_EQ_WAIT(4U, candidates_.size(), 2000);
ASSERT_EQ_SIMULATED_WAIT(4U, candidates_.size(), 2000, fake_clock);
EXPECT_EQ(3U, ports_.size());
EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
ASSERT_EQ_WAIT(6U, candidates_.size(), 1500);
ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 1500, fake_clock);
EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
EXPECT_EQ(4U, ports_.size());
ASSERT_EQ_WAIT(7U, candidates_.size(), 2000);
ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), 2000, fake_clock);
EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
kRelaySslTcpIntAddr);
EXPECT_EQ(4U, ports_.size());
@ -889,7 +919,8 @@ TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
AddInterface(kClientAddr);
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
session_->StartGettingPorts();
ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
fake_clock);
EXPECT_TRUE(candidate_allocation_done_);
// If we Stop gathering now, we shouldn't get a second "done" callback.
session_->StopGettingPorts();
@ -922,7 +953,8 @@ TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
fake_clock);
EXPECT_EQ(4U, ports_.size());
int num_nonrelay_candidates = 0;
@ -955,7 +987,8 @@ TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
network_manager_.set_ipv6_enabled(false);
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
kDefaultAllocationTimeout, fake_clock);
EXPECT_EQ(4U, ports_.size());
EXPECT_EQ(1, CountPorts(ports_, "stun", PROTO_UDP, kAnyAddr));
EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kAnyAddr));
@ -1092,7 +1125,8 @@ TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
PORTALLOCATOR_ENABLE_SHARED_SOCKET);
session_->StartGettingPorts();
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
kDefaultAllocationTimeout, fake_clock);
// Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
// TURN/TCP candidates.
@ -1118,9 +1152,8 @@ TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
session_->StartGettingPorts();
rtc::Thread::Current()->ProcessMessages(100);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
EXPECT_EQ(0U, candidates_.size());
EXPECT_TRUE(candidate_allocation_done_);
}
// Test that we don't crash or malfunction if we can't create UDP sockets.
@ -1129,7 +1162,8 @@ TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
fss_->set_udp_sockets_enabled(false);
EXPECT_TRUE(CreateSession(1));
session_->StartGettingPorts();
ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
ASSERT_EQ_SIMULATED_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout,
fake_clock);
EXPECT_EQ(2U, ports_.size());
EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
@ -1151,7 +1185,8 @@ TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
fss_->set_tcp_listen_enabled(false);
EXPECT_TRUE(CreateSession(1));
session_->StartGettingPorts();
ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
ASSERT_EQ_SIMULATED_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout,
fake_clock);
EXPECT_EQ(2U, ports_.size());
EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
@ -1182,23 +1217,24 @@ TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
AddInterface(kClientAddr);
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
EXPECT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
fake_clock);
EXPECT_EQ(2U, ports_.size());
EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
// RelayPort connection timeout is 3sec. TCP connection with RelayServer
// will be tried after 3 seconds.
// TODO(deadbeef): Use simulated clock here, waiting for exactly 3 seconds.
EXPECT_EQ_WAIT(6U, candidates_.size(), kStunTimeoutMs);
// will be tried after about 3 seconds.
EXPECT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 3500, fake_clock);
EXPECT_EQ(3U, ports_.size());
EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
kRelaySslTcpIntAddr);
EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
// Stun Timeout is 9.5sec.
// TODO(deadbeef): Use simulated clock here, waiting exactly 6.5 seconds.
EXPECT_TRUE_WAIT(candidate_allocation_done_, kStunTimeoutMs);
// We wait at least for a full STUN timeout, which is currently 9.5
// seconds. But since 3-3.5 seconds already passed (see above), we
// only need 6.5 more seconds.
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 6500, fake_clock);
}
TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
@ -1210,7 +1246,8 @@ TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
PORTALLOCATOR_DISABLE_RELAY);
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
kDefaultAllocationTimeout, fake_clock);
ASSERT_EQ(2U, candidates_.size());
EXPECT_EQ(2U, ports_.size());
// Candidates priorities should be different.
@ -1222,7 +1259,8 @@ TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
AddInterface(kClientAddr);
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
EXPECT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
fake_clock);
EXPECT_EQ(4U, ports_.size());
EXPECT_TRUE(candidate_allocation_done_);
// TODO(deadbeef): Extend this to verify ICE restart.
@ -1240,7 +1278,8 @@ TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
session_->StartGettingPorts();
// 7 candidates and 4 ports is what we would normally get (see the
// TestGetAllPorts* tests).
EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
EXPECT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
fake_clock);
EXPECT_EQ(4U, ports_.size());
EXPECT_TRUE(candidate_allocation_done_);
}
@ -1257,7 +1296,8 @@ TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
allocator().set_candidate_filter(CF_RELAY);
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
kDefaultAllocationTimeout, fake_clock);
EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
@ -1275,7 +1315,8 @@ TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
allocator().set_candidate_filter(CF_HOST);
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
kDefaultAllocationTimeout, fake_clock);
EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
for (const Candidate& candidate : candidates_) {
@ -1292,7 +1333,8 @@ TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
allocator().set_candidate_filter(CF_REFLEXIVE);
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
kDefaultAllocationTimeout, fake_clock);
// Host is behind NAT, no private address will be exposed. Hence only UDP
// port with STUN candidate will be sent outside.
EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
@ -1310,7 +1352,8 @@ TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
allocator().set_candidate_filter(CF_REFLEXIVE);
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
kDefaultAllocationTimeout, fake_clock);
// Host has a public address, both UDP and TCP candidates will be exposed.
EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
@ -1324,7 +1367,8 @@ TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
AddInterface(kClientAddr);
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
fake_clock);
EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
@ -1346,10 +1390,12 @@ TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
PORTALLOCATOR_ENABLE_SHARED_SOCKET);
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout,
fake_clock);
EXPECT_EQ(3U, ports_.size());
EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
kDefaultAllocationTimeout, fake_clock);
}
// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
@ -1363,12 +1409,14 @@ TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
PORTALLOCATOR_ENABLE_SHARED_SOCKET);
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
fake_clock);
ASSERT_EQ(2U, ports_.size());
EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
kDefaultAllocationTimeout, fake_clock);
EXPECT_EQ(3U, candidates_.size());
}
@ -1388,14 +1436,16 @@ TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
fake_clock);
ASSERT_EQ(3U, ports_.size());
EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
kDefaultAllocationTimeout, fake_clock);
EXPECT_EQ(3U, candidates_.size());
}
@ -1469,7 +1519,11 @@ TEST_F(BasicPortAllocatorTest,
// Testing DNS resolve for the TURN server, this will test AllocationSequence
// handling the unresolved address signal from TurnPort.
TEST_F(BasicPortAllocatorTest, TestSharedSocketWithServerAddressResolve) {
// TODO(pthatcher): Make this test work with SIMULATED_WAIT. It
// appears that it doesn't currently because of the DNS look up not
// using the fake clock.
TEST_F(BasicPortAllocatorTestWithRealClock,
TestSharedSocketWithServerAddressResolve) {
turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
PROTO_UDP);
AddInterface(kClientAddr);
@ -1508,14 +1562,16 @@ TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
fake_clock);
ASSERT_EQ(2U, ports_.size());
EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
kDefaultAllocationTimeout, fake_clock);
EXPECT_EQ(3U, candidates_.size());
// Local port will be created first and then TURN port.
EXPECT_EQ(2U, ports_[0]->Candidates().size());
@ -1543,7 +1599,8 @@ TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
fake_clock);
EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
Candidate stun_candidate;
EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
@ -1552,7 +1609,8 @@ TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
stun_candidate.address());
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
kDefaultAllocationTimeout, fake_clock);
EXPECT_EQ(3U, candidates_.size());
// Local port will be created first and then TURN port.
EXPECT_EQ(2U, ports_[0]->Candidates().size());
@ -1575,12 +1633,14 @@ TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
fake_clock);
ASSERT_EQ(2U, ports_.size());
EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
kDefaultAllocationTimeout, fake_clock);
EXPECT_EQ(2U, candidates_.size());
EXPECT_EQ(1U, ports_[0]->Candidates().size());
EXPECT_EQ(1U, ports_[1]->Candidates().size());
@ -1602,7 +1662,8 @@ TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
fake_clock);
ASSERT_EQ(3U, ports_.size());
EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
Candidate stun_candidate;
@ -1616,7 +1677,8 @@ TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
// should be different than the TURN request's server reflexive address.
EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
kDefaultAllocationTimeout, fake_clock);
EXPECT_EQ(3U, candidates_.size());
EXPECT_EQ(1U, ports_[0]->Candidates().size());
EXPECT_EQ(1U, ports_[1]->Candidates().size());
@ -1640,7 +1702,8 @@ TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
fake_clock);
EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
Candidate stun_candidate;
EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
@ -1664,11 +1727,13 @@ TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
AddInterface(kClientAddr);
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
ASSERT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
fake_clock);
EXPECT_EQ(1U, candidates_.size());
EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
// STUN timeout is 9.5sec. We need to wait to get candidate done signal.
EXPECT_TRUE_WAIT(candidate_allocation_done_, kStunTimeoutMs);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs,
fake_clock);
EXPECT_EQ(1U, candidates_.size());
}
@ -1688,7 +1753,8 @@ TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
session_->StartGettingPorts();
EXPECT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
EXPECT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
fake_clock);
EXPECT_EQ(1U, candidates_.size());
EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kPrivateAddr);
EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
@ -1704,9 +1770,11 @@ TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
allocator_->set_step_delay(kMinimumStepDelay);
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
ASSERT_EQ_SIMULATED_WAIT(4U, ports_.size(), kDefaultAllocationTimeout,
fake_clock);
EXPECT_EQ(4U, candidates_.size());
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
kDefaultAllocationTimeout, fake_clock);
EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientIPv6Addr);
@ -1719,10 +1787,10 @@ TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
allocator_->set_step_delay(kDefaultStepDelay);
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
EXPECT_EQ(2U, ports_.size());
session_->StopGettingPorts();
EXPECT_TRUE_WAIT(candidate_allocation_done_, 1000);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
// After stopping getting ports, adding a new interface will not start
// getting ports again.
@ -1731,7 +1799,7 @@ TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
ports_.clear();
candidate_allocation_done_ = false;
network_manager_.AddInterface(kClientAddr2);
rtc::Thread::Current()->ProcessMessages(1000);
SIMULATED_WAIT(false, 1000, fake_clock);
EXPECT_EQ(0U, candidates_.size());
EXPECT_EQ(0U, ports_.size());
}
@ -1741,10 +1809,10 @@ TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
allocator_->set_step_delay(kDefaultStepDelay);
EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
session_->StartGettingPorts();
ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
EXPECT_EQ(2U, ports_.size());
session_->ClearGettingPorts();
EXPECT_TRUE_WAIT(candidate_allocation_done_, 1000);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
// After clearing getting ports, adding a new interface will start getting
// ports again.
@ -1753,9 +1821,10 @@ TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
ports_.clear();
candidate_allocation_done_ = false;
network_manager_.AddInterface(kClientAddr2);
ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
EXPECT_EQ(2U, ports_.size());
EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
kDefaultAllocationTimeout, fake_clock);
}
// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
@ -1767,8 +1836,8 @@ TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
allocator_->turn_servers(), pool_size, false);
const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
ASSERT_NE(nullptr, peeked_session);
EXPECT_EQ_WAIT(true, peeked_session->CandidatesAllocationDone(),
kDefaultAllocationTimeout);
EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
kDefaultAllocationTimeout, fake_clock);
// Expect that when TakePooledSession is called,
// UpdateTransportInformationInternal will be called and the
// BasicPortAllocatorSession will update the ufrag/pwd of ports and
@ -1803,8 +1872,8 @@ TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
allocator_->turn_servers(), pool_size, false);
const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
ASSERT_NE(nullptr, peeked_session);
EXPECT_EQ_WAIT(true, peeked_session->CandidatesAllocationDone(),
kDefaultAllocationTimeout);
EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
kDefaultAllocationTimeout, fake_clock);
size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
size_t initial_ports_size = peeked_session->ReadyPorts().size();
allocator_->set_candidate_filter(CF_RELAY);