Index: webrtc/p2p/base/port_unittest.cc |
diff --git a/webrtc/p2p/base/port_unittest.cc b/webrtc/p2p/base/port_unittest.cc |
index eb547605c978958d2dada49d71ec135edc115c57..757c1b31bff009c3b300a0a6a6c19fabcc2a2e40 100644 |
--- a/webrtc/p2p/base/port_unittest.cc |
+++ b/webrtc/p2p/base/port_unittest.cc |
@@ -8,6 +8,7 @@ |
* be found in the AUTHORS file in the root of the source tree. |
*/ |
+#include <list> |
#include <memory> |
#include "webrtc/p2p/base/basicpacketsocketfactory.h" |
@@ -109,7 +110,6 @@ class TestPort : public Port { |
const std::string& type, |
rtc::PacketSocketFactory* factory, |
rtc::Network* network, |
- const rtc::IPAddress& ip, |
uint16_t min_port, |
uint16_t max_port, |
const std::string& username_fragment, |
@@ -118,7 +118,6 @@ class TestPort : public Port { |
type, |
factory, |
network, |
- ip, |
min_port, |
max_port, |
username_fragment, |
@@ -144,7 +143,9 @@ class TestPort : public Port { |
} |
virtual void PrepareAddress() { |
- rtc::SocketAddress addr(ip(), min_port()); |
+ // Act as if the socket was bound to the best IP on the network, to the |
+ // first port in the allowed range. |
+ rtc::SocketAddress addr(Network()->GetBestIP(), min_port()); |
AddAddress(addr, addr, rtc::SocketAddress(), "udp", "", "", Type(), |
ICE_TYPE_PREFERENCE_HOST, 0, "", true); |
} |
@@ -382,7 +383,6 @@ class PortTest : public testing::Test, public sigslot::has_slots<> { |
PortTest() |
: ss_(new rtc::VirtualSocketServer()), |
main_(ss_.get()), |
- network_("unittest", "unittest", rtc::IPAddress(INADDR_ANY), 32), |
socket_factory_(rtc::Thread::Current()), |
nat_factory1_(ss_.get(), kNatAddr1, SocketAddress()), |
nat_factory2_(ss_.get(), kNatAddr2, SocketAddress()), |
@@ -401,7 +401,6 @@ class PortTest : public testing::Test, public sigslot::has_slots<> { |
password_(rtc::CreateRandomString(ICE_PWD_LENGTH)), |
role_conflict_(false), |
ports_destroyed_(0) { |
- network_.AddIP(rtc::IPAddress(INADDR_ANY)); |
} |
protected: |
@@ -483,32 +482,36 @@ class PortTest : public testing::Test, public sigslot::has_slots<> { |
TestConnectivity("ssltcp", port1, RelayName(rtype, proto), port2, |
rtype == RELAY_GTURN, false, true, true); |
} |
+ |
+ rtc::Network* MakeNetwork(const SocketAddress& addr) { |
+ networks_.emplace_back("unittest", "unittest", addr.ipaddr(), 32); |
+ networks_.back().AddIP(addr.ipaddr()); |
+ return &networks_.back(); |
+ } |
+ |
// helpers for above functions |
UDPPort* CreateUdpPort(const SocketAddress& addr) { |
return CreateUdpPort(addr, &socket_factory_); |
} |
UDPPort* CreateUdpPort(const SocketAddress& addr, |
PacketSocketFactory* socket_factory) { |
- return UDPPort::Create(&main_, socket_factory, &network_, addr.ipaddr(), 0, |
- 0, username_, password_, std::string(), true); |
+ return UDPPort::Create(&main_, socket_factory, MakeNetwork(addr), 0, 0, |
+ username_, password_, std::string(), true); |
} |
TCPPort* CreateTcpPort(const SocketAddress& addr) { |
return CreateTcpPort(addr, &socket_factory_); |
} |
TCPPort* CreateTcpPort(const SocketAddress& addr, |
PacketSocketFactory* socket_factory) { |
- return TCPPort::Create(&main_, socket_factory, &network_, |
- addr.ipaddr(), 0, 0, username_, password_, |
- true); |
+ return TCPPort::Create(&main_, socket_factory, MakeNetwork(addr), 0, 0, |
+ username_, password_, true); |
} |
StunPort* CreateStunPort(const SocketAddress& addr, |
rtc::PacketSocketFactory* factory) { |
ServerAddresses stun_servers; |
stun_servers.insert(kStunAddr); |
- return StunPort::Create(&main_, factory, &network_, |
- addr.ipaddr(), 0, 0, |
- username_, password_, stun_servers, |
- std::string()); |
+ return StunPort::Create(&main_, factory, MakeNetwork(addr), 0, 0, username_, |
+ password_, stun_servers, std::string()); |
} |
Port* CreateRelayPort(const SocketAddress& addr, RelayType rtype, |
ProtocolType int_proto, ProtocolType ext_proto) { |
@@ -530,8 +533,8 @@ class PortTest : public testing::Test, public sigslot::has_slots<> { |
PacketSocketFactory* socket_factory, |
ProtocolType int_proto, ProtocolType ext_proto, |
const rtc::SocketAddress& server_addr) { |
- return TurnPort::Create(&main_, socket_factory, &network_, addr.ipaddr(), 0, |
- 0, username_, password_, |
+ return TurnPort::Create(&main_, socket_factory, MakeNetwork(addr), 0, 0, |
+ username_, password_, |
ProtocolAddress(server_addr, int_proto), |
kRelayCredentials, 0, std::string()); |
} |
@@ -547,8 +550,8 @@ class PortTest : public testing::Test, public sigslot::has_slots<> { |
// TODO(pthatcher): Remove GTURN. |
// Generate a username with length of 16 for Gturn only. |
std::string username = rtc::CreateRandomString(kGturnUserNameLength); |
- return RelayPort::Create(&main_, &socket_factory_, &network_, addr.ipaddr(), |
- 0, 0, username, password_); |
+ return RelayPort::Create(&main_, &socket_factory_, MakeNetwork(addr), 0, 0, |
+ username, password_); |
// TODO: Add an external address for ext_proto, so that the |
// other side can connect to this port using a non-UDP protocol. |
} |
@@ -600,10 +603,6 @@ class PortTest : public testing::Test, public sigslot::has_slots<> { |
} |
} |
- void SetNetworkType(rtc::AdapterType adapter_type) { |
- network_.set_type(adapter_type); |
- } |
- |
void TestCrossFamilyPorts(int type); |
void ExpectPortsCanConnect(bool can_connect, Port* p1, Port* p2); |
@@ -764,8 +763,8 @@ class PortTest : public testing::Test, public sigslot::has_slots<> { |
TestPort* CreateTestPort(const rtc::SocketAddress& addr, |
const std::string& username, |
const std::string& password) { |
- TestPort* port = new TestPort(&main_, "test", &socket_factory_, &network_, |
- addr.ipaddr(), 0, 0, username, password); |
+ TestPort* port = new TestPort(&main_, "test", &socket_factory_, |
+ MakeNetwork(addr), 0, 0, username, password); |
port->SignalRoleConflict.connect(this, &PortTest::OnRoleConflict); |
return port; |
} |
@@ -779,6 +778,15 @@ class PortTest : public testing::Test, public sigslot::has_slots<> { |
port->SetIceTiebreaker(tiebreaker); |
return port; |
} |
+ // Overload to create a test port given an rtc::Network directly. |
+ TestPort* CreateTestPort(rtc::Network* network, |
+ const std::string& username, |
+ const std::string& password) { |
+ TestPort* port = new TestPort(&main_, "test", &socket_factory_, network, 0, |
+ 0, username, password); |
+ port->SignalRoleConflict.connect(this, &PortTest::OnRoleConflict); |
+ return port; |
+ } |
void OnRoleConflict(PortInterface* port) { |
role_conflict_ = true; |
@@ -799,9 +807,12 @@ class PortTest : public testing::Test, public sigslot::has_slots<> { |
rtc::VirtualSocketServer* vss() { return ss_.get(); } |
private: |
+ // When a "create port" helper method is called with an IP, we create a |
+ // Network with that IP and add it to this list. Using a list instead of a |
+ // vector so that when it grows, pointers aren't invalidated. |
+ std::list<rtc::Network> networks_; |
std::unique_ptr<rtc::VirtualSocketServer> ss_; |
rtc::AutoSocketServerThread main_; |
- rtc::Network network_; |
rtc::BasicPacketSocketFactory socket_factory_; |
std::unique_ptr<rtc::NATServer> nat_server1_; |
std::unique_ptr<rtc::NATServer> nat_server2_; |
@@ -1931,10 +1942,11 @@ TEST_F(PortTest, TestUseCandidateAttribute) { |
// change, the network cost of the local candidates will change. Also tests that |
// the remote network costs are updated with the stun binding requests. |
TEST_F(PortTest, TestNetworkCostChange) { |
+ rtc::Network* test_network = MakeNetwork(kLocalAddr1); |
std::unique_ptr<TestPort> lport( |
- CreateTestPort(kLocalAddr1, "lfrag", "lpass")); |
+ CreateTestPort(test_network, "lfrag", "lpass")); |
std::unique_ptr<TestPort> rport( |
- CreateTestPort(kLocalAddr2, "rfrag", "rpass")); |
+ CreateTestPort(test_network, "rfrag", "rpass")); |
lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
lport->SetIceTiebreaker(kTiebreaker1); |
rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
@@ -1950,7 +1962,7 @@ TEST_F(PortTest, TestNetworkCostChange) { |
} |
// Change the network type to wifi. |
- SetNetworkType(rtc::ADAPTER_TYPE_WIFI); |
+ test_network->set_type(rtc::ADAPTER_TYPE_WIFI); |
EXPECT_EQ(rtc::kNetworkCostLow, lport->network_cost()); |
for (const cricket::Candidate& candidate : lport->Candidates()) { |
EXPECT_EQ(rtc::kNetworkCostLow, candidate.network_cost()); |
@@ -1960,16 +1972,16 @@ TEST_F(PortTest, TestNetworkCostChange) { |
Connection* lconn = |
lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
// Change the network type to cellular. |
- SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR); |
+ test_network->set_type(rtc::ADAPTER_TYPE_CELLULAR); |
EXPECT_EQ(rtc::kNetworkCostHigh, lport->network_cost()); |
for (const cricket::Candidate& candidate : lport->Candidates()) { |
EXPECT_EQ(rtc::kNetworkCostHigh, candidate.network_cost()); |
} |
- SetNetworkType(rtc::ADAPTER_TYPE_WIFI); |
+ test_network->set_type(rtc::ADAPTER_TYPE_WIFI); |
Connection* rconn = |
rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
- SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR); |
+ test_network->set_type(rtc::ADAPTER_TYPE_CELLULAR); |
lconn->Ping(0); |
// The rconn's remote candidate cost is rtc::kNetworkCostLow, but the ping |
// contains an attribute of network cost of rtc::kNetworkCostHigh. Once the |
@@ -1988,10 +2000,11 @@ TEST_F(PortTest, TestNetworkCostChange) { |
} |
TEST_F(PortTest, TestNetworkInfoAttribute) { |
+ rtc::Network* test_network = MakeNetwork(kLocalAddr1); |
std::unique_ptr<TestPort> lport( |
- CreateTestPort(kLocalAddr1, "lfrag", "lpass")); |
+ CreateTestPort(test_network, "lfrag", "lpass")); |
std::unique_ptr<TestPort> rport( |
- CreateTestPort(kLocalAddr2, "rfrag", "rpass")); |
+ CreateTestPort(test_network, "rfrag", "rpass")); |
lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
lport->SetIceTiebreaker(kTiebreaker1); |
rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
@@ -2017,7 +2030,7 @@ TEST_F(PortTest, TestNetworkInfoAttribute) { |
// Set the network type to be cellular so its cost will be kNetworkCostHigh. |
// Send a fake ping from rport to lport. |
- SetNetworkType(rtc::ADAPTER_TYPE_CELLULAR); |
+ test_network->set_type(rtc::ADAPTER_TYPE_CELLULAR); |
uint16_t rnetwork_id = 8; |
rport->Network()->set_id(rnetwork_id); |
Connection* rconn = |