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

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

Issue 1998813002: Fixing the behavior of the candidate filter with pooled candidates. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Undoing unintentional "git cl format" of unrelated files. Created 4 years, 7 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 16 matching lines...) Expand all
27 #include "webrtc/base/logging.h" 27 #include "webrtc/base/logging.h"
28 #include "webrtc/base/natserver.h" 28 #include "webrtc/base/natserver.h"
29 #include "webrtc/base/natsocketfactory.h" 29 #include "webrtc/base/natsocketfactory.h"
30 #include "webrtc/base/network.h" 30 #include "webrtc/base/network.h"
31 #include "webrtc/base/physicalsocketserver.h" 31 #include "webrtc/base/physicalsocketserver.h"
32 #include "webrtc/base/socketaddress.h" 32 #include "webrtc/base/socketaddress.h"
33 #include "webrtc/base/ssladapter.h" 33 #include "webrtc/base/ssladapter.h"
34 #include "webrtc/base/thread.h" 34 #include "webrtc/base/thread.h"
35 #include "webrtc/base/virtualsocketserver.h" 35 #include "webrtc/base/virtualsocketserver.h"
36 36
37 using cricket::ServerAddresses;
38 using rtc::IPAddress; 37 using rtc::IPAddress;
39 using rtc::SocketAddress; 38 using rtc::SocketAddress;
40 using rtc::Thread; 39 using rtc::Thread;
41 40
42 static const SocketAddress kClientAddr("11.11.11.11", 0); 41 static const SocketAddress kClientAddr("11.11.11.11", 0);
43 static const SocketAddress kLoopbackAddr("127.0.0.1", 0); 42 static const SocketAddress kLoopbackAddr("127.0.0.1", 0);
44 static const SocketAddress kPrivateAddr("192.168.1.11", 0); 43 static const SocketAddress kPrivateAddr("192.168.1.11", 0);
45 static const SocketAddress kPrivateAddr2("192.168.1.12", 0); 44 static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
46 static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3", 45 static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3",
47 0); 46 0);
(...skipping 29 matching lines...) Expand all
77 76
78 // STUN timeout (with all retries) is 9500ms. 77 // STUN timeout (with all retries) is 9500ms.
79 // Add some margin of error for slow bots. 78 // Add some margin of error for slow bots.
80 // TODO(deadbeef): Use simulated clock instead of just increasing timeouts to 79 // TODO(deadbeef): Use simulated clock instead of just increasing timeouts to
81 // fix flaky tests. 80 // fix flaky tests.
82 static const int kStunTimeoutMs = 15000; 81 static const int kStunTimeoutMs = 15000;
83 82
84 namespace cricket { 83 namespace cricket {
85 84
86 // Helper for dumping candidates 85 // Helper for dumping candidates
87 std::ostream& operator<<(std::ostream& os, const cricket::Candidate& c) { 86 std::ostream& operator<<(std::ostream& os, const Candidate& c) {
88 os << c.ToString(); 87 os << c.ToString();
89 return os; 88 return os;
90 } 89 }
91 90
92 } // namespace cricket
93
94 class BasicPortAllocatorTest : public testing::Test, 91 class BasicPortAllocatorTest : public testing::Test,
95 public sigslot::has_slots<> { 92 public sigslot::has_slots<> {
96 public: 93 public:
97 BasicPortAllocatorTest() 94 BasicPortAllocatorTest()
98 : pss_(new rtc::PhysicalSocketServer), 95 : pss_(new rtc::PhysicalSocketServer),
99 vss_(new rtc::VirtualSocketServer(pss_.get())), 96 vss_(new rtc::VirtualSocketServer(pss_.get())),
100 fss_(new rtc::FirewallSocketServer(vss_.get())), 97 fss_(new rtc::FirewallSocketServer(vss_.get())),
101 ss_scope_(fss_.get()), 98 ss_scope_(fss_.get()),
102 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr), 99 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
103 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)), 100 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
104 stun_server_( 101 stun_server_(TestStunServer::Create(Thread::Current(), kStunAddr)),
105 cricket::TestStunServer::Create(Thread::Current(), kStunAddr)),
106 relay_server_(Thread::Current(), 102 relay_server_(Thread::Current(),
107 kRelayUdpIntAddr, 103 kRelayUdpIntAddr,
108 kRelayUdpExtAddr, 104 kRelayUdpExtAddr,
109 kRelayTcpIntAddr, 105 kRelayTcpIntAddr,
110 kRelayTcpExtAddr, 106 kRelayTcpExtAddr,
111 kRelaySslTcpIntAddr, 107 kRelaySslTcpIntAddr,
112 kRelaySslTcpExtAddr), 108 kRelaySslTcpExtAddr),
113 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr), 109 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
114 candidate_allocation_done_(false) { 110 candidate_allocation_done_(false) {
115 cricket::ServerAddresses stun_servers; 111 ServerAddresses stun_servers;
116 stun_servers.insert(kStunAddr); 112 stun_servers.insert(kStunAddr);
117 // Passing the addresses of GTURN servers will enable GTURN in 113 // Passing the addresses of GTURN servers will enable GTURN in
118 // Basicportallocator. 114 // Basicportallocator.
119 allocator_.reset(new cricket::BasicPortAllocator( 115 allocator_.reset(new BasicPortAllocator(&network_manager_, stun_servers,
120 &network_manager_, stun_servers, kRelayUdpIntAddr, kRelayTcpIntAddr, 116 kRelayUdpIntAddr, kRelayTcpIntAddr,
121 kRelaySslTcpIntAddr)); 117 kRelaySslTcpIntAddr));
122 allocator_->set_step_delay(cricket::kMinimumStepDelay); 118 allocator_->set_step_delay(kMinimumStepDelay);
123 } 119 }
124 120
125 void AddInterface(const SocketAddress& addr) { 121 void AddInterface(const SocketAddress& addr) {
126 network_manager_.AddInterface(addr); 122 network_manager_.AddInterface(addr);
127 } 123 }
128 void AddInterface(const SocketAddress& addr, const std::string& if_name) { 124 void AddInterface(const SocketAddress& addr, const std::string& if_name) {
129 network_manager_.AddInterface(addr, if_name); 125 network_manager_.AddInterface(addr, if_name);
130 } 126 }
131 void AddInterface(const SocketAddress& addr, 127 void AddInterface(const SocketAddress& addr,
132 const std::string& if_name, 128 const std::string& if_name,
(...skipping 13 matching lines...) Expand all
146 vss_->SetDefaultRoute(addr.ipaddr()); 142 vss_->SetDefaultRoute(addr.ipaddr());
147 } 143 }
148 void RemoveInterface(const SocketAddress& addr) { 144 void RemoveInterface(const SocketAddress& addr) {
149 network_manager_.RemoveInterface(addr); 145 network_manager_.RemoveInterface(addr);
150 } 146 }
151 bool SetPortRange(int min_port, int max_port) { 147 bool SetPortRange(int min_port, int max_port) {
152 return allocator_->SetPortRange(min_port, max_port); 148 return allocator_->SetPortRange(min_port, max_port);
153 } 149 }
154 // Endpoint is on the public network. No STUN or TURN. 150 // Endpoint is on the public network. No STUN or TURN.
155 void ResetWithNoServersOrNat() { 151 void ResetWithNoServersOrNat() {
156 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_)); 152 allocator_.reset(new BasicPortAllocator(&network_manager_));
157 allocator_->set_step_delay(cricket::kMinimumStepDelay); 153 allocator_->set_step_delay(kMinimumStepDelay);
158 } 154 }
159 // Endpoint is behind a NAT, with STUN specified. 155 // Endpoint is behind a NAT, with STUN specified.
160 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) { 156 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
161 ResetWithStunServer(stun_server, true); 157 ResetWithStunServer(stun_server, true);
162 } 158 }
163 // Endpoint is on the public network, with STUN specified. 159 // Endpoint is on the public network, with STUN specified.
164 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) { 160 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
165 ResetWithStunServer(stun_server, false); 161 ResetWithStunServer(stun_server, false);
166 } 162 }
167 // Endpoint is on the public network, with TURN specified. 163 // Endpoint is on the public network, with TURN specified.
168 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn, 164 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
169 const rtc::SocketAddress& tcp_turn) { 165 const rtc::SocketAddress& tcp_turn) {
170 ResetWithNoServersOrNat(); 166 ResetWithNoServersOrNat();
171 AddTurnServers(udp_turn, tcp_turn); 167 AddTurnServers(udp_turn, tcp_turn);
172 } 168 }
173 169
174 void AddTurnServers(const rtc::SocketAddress& udp_turn, 170 void AddTurnServers(const rtc::SocketAddress& udp_turn,
175 const rtc::SocketAddress& tcp_turn) { 171 const rtc::SocketAddress& tcp_turn) {
176 cricket::RelayServerConfig turn_server(cricket::RELAY_TURN); 172 RelayServerConfig turn_server(RELAY_TURN);
177 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword); 173 RelayCredentials credentials(kTurnUsername, kTurnPassword);
178 turn_server.credentials = credentials; 174 turn_server.credentials = credentials;
179 175
180 if (!udp_turn.IsNil()) { 176 if (!udp_turn.IsNil()) {
181 turn_server.ports.push_back( 177 turn_server.ports.push_back(
182 cricket::ProtocolAddress(kTurnUdpIntAddr, cricket::PROTO_UDP, false)); 178 ProtocolAddress(kTurnUdpIntAddr, PROTO_UDP, false));
183 } 179 }
184 if (!tcp_turn.IsNil()) { 180 if (!tcp_turn.IsNil()) {
185 turn_server.ports.push_back( 181 turn_server.ports.push_back(
186 cricket::ProtocolAddress(kTurnTcpIntAddr, cricket::PROTO_TCP, false)); 182 ProtocolAddress(kTurnTcpIntAddr, PROTO_TCP, false));
187 } 183 }
188 allocator_->AddTurnServer(turn_server); 184 allocator_->AddTurnServer(turn_server);
189 } 185 }
190 186
191 bool CreateSession(int component) { 187 bool CreateSession(int component) {
192 session_ = CreateSession("session", component); 188 session_ = CreateSession("session", component);
193 if (!session_) { 189 if (!session_) {
194 return false; 190 return false;
195 } 191 }
196 return true; 192 return true;
197 } 193 }
198 194
199 bool CreateSession(int component, const std::string& content_name) { 195 bool CreateSession(int component, const std::string& content_name) {
200 session_ = CreateSession("session", content_name, component); 196 session_ = CreateSession("session", content_name, component);
201 if (!session_) { 197 if (!session_) {
202 return false; 198 return false;
203 } 199 }
204 return true; 200 return true;
205 } 201 }
206 202
207 std::unique_ptr<cricket::PortAllocatorSession> CreateSession( 203 std::unique_ptr<PortAllocatorSession> CreateSession(const std::string& sid,
208 const std::string& sid, 204 int component) {
209 int component) {
210 return CreateSession(sid, kContentName, component); 205 return CreateSession(sid, kContentName, component);
211 } 206 }
212 207
213 std::unique_ptr<cricket::PortAllocatorSession> CreateSession( 208 std::unique_ptr<PortAllocatorSession> CreateSession(
214 const std::string& sid, 209 const std::string& sid,
215 const std::string& content_name, 210 const std::string& content_name,
216 int component) { 211 int component) {
217 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0); 212 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
218 } 213 }
219 214
220 std::unique_ptr<cricket::PortAllocatorSession> CreateSession( 215 std::unique_ptr<PortAllocatorSession> CreateSession(
221 const std::string& sid, 216 const std::string& sid,
222 const std::string& content_name, 217 const std::string& content_name,
223 int component, 218 int component,
224 const std::string& ice_ufrag, 219 const std::string& ice_ufrag,
225 const std::string& ice_pwd) { 220 const std::string& ice_pwd) {
226 std::unique_ptr<cricket::PortAllocatorSession> session = 221 std::unique_ptr<PortAllocatorSession> session = allocator_->CreateSession(
227 allocator_->CreateSession(sid, content_name, component, ice_ufrag, 222 sid, content_name, component, ice_ufrag, ice_pwd);
228 ice_pwd);
229 session->SignalPortReady.connect(this, 223 session->SignalPortReady.connect(this,
230 &BasicPortAllocatorTest::OnPortReady); 224 &BasicPortAllocatorTest::OnPortReady);
231 session->SignalCandidatesReady.connect( 225 session->SignalCandidatesReady.connect(
232 this, &BasicPortAllocatorTest::OnCandidatesReady); 226 this, &BasicPortAllocatorTest::OnCandidatesReady);
233 session->SignalCandidatesAllocationDone.connect( 227 session->SignalCandidatesAllocationDone.connect(
234 this, &BasicPortAllocatorTest::OnCandidatesAllocationDone); 228 this, &BasicPortAllocatorTest::OnCandidatesAllocationDone);
235 return session; 229 return session;
236 } 230 }
237 231
238 static bool CheckCandidate(const cricket::Candidate& c, 232 static bool CheckCandidate(const Candidate& c,
239 int component, 233 int component,
240 const std::string& type, 234 const std::string& type,
241 const std::string& proto, 235 const std::string& proto,
242 const SocketAddress& addr) { 236 const SocketAddress& addr) {
243 return (c.component() == component && c.type() == type && 237 return (c.component() == component && c.type() == type &&
244 c.protocol() == proto && c.address().ipaddr() == addr.ipaddr() && 238 c.protocol() == proto && c.address().ipaddr() == addr.ipaddr() &&
245 ((addr.port() == 0 && (c.address().port() != 0)) || 239 ((addr.port() == 0 && (c.address().port() != 0)) ||
246 (c.address().port() == addr.port()))); 240 (c.address().port() == addr.port())));
247 } 241 }
248 static bool CheckPort(const rtc::SocketAddress& addr, 242 static bool CheckPort(const rtc::SocketAddress& addr,
249 int min_port, 243 int min_port,
250 int max_port) { 244 int max_port) {
251 return (addr.port() >= min_port && addr.port() <= max_port); 245 return (addr.port() >= min_port && addr.port() <= max_port);
252 } 246 }
253 247
254 void OnCandidatesAllocationDone(cricket::PortAllocatorSession* session) { 248 void OnCandidatesAllocationDone(PortAllocatorSession* session) {
255 // We should only get this callback once, except in the mux test where 249 // We should only get this callback once, except in the mux test where
256 // we have multiple port allocation sessions. 250 // we have multiple port allocation sessions.
257 if (session == session_.get()) { 251 if (session == session_.get()) {
258 ASSERT_FALSE(candidate_allocation_done_); 252 ASSERT_FALSE(candidate_allocation_done_);
259 candidate_allocation_done_ = true; 253 candidate_allocation_done_ = true;
260 } 254 }
261 EXPECT_TRUE(session->CandidatesAllocationDone()); 255 EXPECT_TRUE(session->CandidatesAllocationDone());
262 } 256 }
263 257
264 // Check if all ports allocated have send-buffer size |expected|. If 258 // Check if all ports allocated have send-buffer size |expected|. If
265 // |expected| == -1, check if GetOptions returns SOCKET_ERROR. 259 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
266 void CheckSendBufferSizesOfAllPorts(int expected) { 260 void CheckSendBufferSizesOfAllPorts(int expected) {
267 std::vector<cricket::PortInterface*>::iterator it; 261 std::vector<PortInterface*>::iterator it;
268 for (it = ports_.begin(); it < ports_.end(); ++it) { 262 for (it = ports_.begin(); it < ports_.end(); ++it) {
269 int send_buffer_size; 263 int send_buffer_size;
270 if (expected == -1) { 264 if (expected == -1) {
271 EXPECT_EQ(SOCKET_ERROR, 265 EXPECT_EQ(SOCKET_ERROR,
272 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size)); 266 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
273 } else { 267 } else {
274 EXPECT_EQ(0, 268 EXPECT_EQ(0,
275 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size)); 269 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
276 ASSERT_EQ(expected, send_buffer_size); 270 ASSERT_EQ(expected, send_buffer_size);
277 } 271 }
278 } 272 }
279 } 273 }
280 274
281 // This function starts the port/address gathering and check the existence of 275 // This function starts the port/address gathering and check the existence of
282 // candidates as specified. When |expect_stun_candidate| is true, 276 // candidates as specified. When |expect_stun_candidate| is true,
283 // |stun_candidate_addr| carries the expected reflective address, which is 277 // |stun_candidate_addr| carries the expected reflective address, which is
284 // also the related address for TURN candidate if it is expected. Otherwise, 278 // also the related address for TURN candidate if it is expected. Otherwise,
285 // it should be ignore. 279 // it should be ignore.
286 void CheckDisableAdapterEnumeration( 280 void CheckDisableAdapterEnumeration(
287 uint32_t total_ports, 281 uint32_t total_ports,
288 const rtc::IPAddress& host_candidate_addr, 282 const rtc::IPAddress& host_candidate_addr,
289 const rtc::IPAddress& stun_candidate_addr, 283 const rtc::IPAddress& stun_candidate_addr,
290 const rtc::IPAddress& relay_candidate_udp_transport_addr, 284 const rtc::IPAddress& relay_candidate_udp_transport_addr,
291 const rtc::IPAddress& relay_candidate_tcp_transport_addr) { 285 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
292 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(), 286 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
293 rtc::IPAddress()); 287 rtc::IPAddress());
294 if (!session_) { 288 if (!session_) {
295 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 289 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
296 } 290 }
297 session_->set_flags(session_->flags() | 291 session_->set_flags(session_->flags() |
298 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION | 292 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
299 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET); 293 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
300 allocator().set_allow_tcp_listen(false); 294 allocator().set_allow_tcp_listen(false);
301 session_->StartGettingPorts(); 295 session_->StartGettingPorts();
302 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); 296 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
303 297
304 uint32_t total_candidates = 0; 298 uint32_t total_candidates = 0;
305 if (!host_candidate_addr.IsNil()) { 299 if (!host_candidate_addr.IsNil()) {
306 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates], 300 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
307 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", 301 ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
308 rtc::SocketAddress(kPrivateAddr.ipaddr(), 0)); 302 rtc::SocketAddress(kPrivateAddr.ipaddr(), 0));
309 ++total_candidates; 303 ++total_candidates;
310 } 304 }
311 if (!stun_candidate_addr.IsNil()) { 305 if (!stun_candidate_addr.IsNil()) {
312 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates], 306 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
313 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", 307 ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
314 rtc::SocketAddress(stun_candidate_addr, 0)); 308 rtc::SocketAddress(stun_candidate_addr, 0));
315 rtc::IPAddress related_address = host_candidate_addr; 309 rtc::IPAddress related_address = host_candidate_addr;
316 if (host_candidate_addr.IsNil()) { 310 if (host_candidate_addr.IsNil()) {
317 related_address = 311 related_address =
318 rtc::GetAnyIP(candidates_[total_candidates].address().family()); 312 rtc::GetAnyIP(candidates_[total_candidates].address().family());
319 } 313 }
320 EXPECT_EQ(related_address, 314 EXPECT_EQ(related_address,
321 candidates_[total_candidates].related_address().ipaddr()); 315 candidates_[total_candidates].related_address().ipaddr());
322 ++total_candidates; 316 ++total_candidates;
323 } 317 }
324 if (!relay_candidate_udp_transport_addr.IsNil()) { 318 if (!relay_candidate_udp_transport_addr.IsNil()) {
325 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates], 319 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
326 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", 320 ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
327 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0)); 321 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0));
328 EXPECT_EQ(stun_candidate_addr, 322 EXPECT_EQ(stun_candidate_addr,
329 candidates_[total_candidates].related_address().ipaddr()); 323 candidates_[total_candidates].related_address().ipaddr());
330 ++total_candidates; 324 ++total_candidates;
331 } 325 }
332 if (!relay_candidate_tcp_transport_addr.IsNil()) { 326 if (!relay_candidate_tcp_transport_addr.IsNil()) {
333 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates], 327 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
334 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", 328 ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
335 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0)); 329 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0));
336 EXPECT_EQ(stun_candidate_addr, 330 EXPECT_EQ(stun_candidate_addr,
337 candidates_[total_candidates].related_address().ipaddr()); 331 candidates_[total_candidates].related_address().ipaddr());
338 ++total_candidates; 332 ++total_candidates;
339 } 333 }
340 334
341 EXPECT_EQ(total_candidates, candidates_.size()); 335 EXPECT_EQ(total_candidates, candidates_.size());
342 EXPECT_EQ(total_ports, ports_.size()); 336 EXPECT_EQ(total_ports, ports_.size());
343 } 337 }
344 338
345 protected: 339 protected:
346 cricket::BasicPortAllocator& allocator() { return *allocator_; } 340 BasicPortAllocator& allocator() { return *allocator_; }
347 341
348 void OnPortReady(cricket::PortAllocatorSession* ses, 342 void OnPortReady(PortAllocatorSession* ses, PortInterface* port) {
349 cricket::PortInterface* port) {
350 LOG(LS_INFO) << "OnPortReady: " << port->ToString(); 343 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
351 ports_.push_back(port); 344 ports_.push_back(port);
352 // Make sure the new port is added to ReadyPorts. 345 // Make sure the new port is added to ReadyPorts.
353 auto ready_ports = ses->ReadyPorts(); 346 auto ready_ports = ses->ReadyPorts();
354 EXPECT_NE(ready_ports.end(), 347 EXPECT_NE(ready_ports.end(),
355 std::find(ready_ports.begin(), ready_ports.end(), port)); 348 std::find(ready_ports.begin(), ready_ports.end(), port));
356 } 349 }
357 void OnCandidatesReady(cricket::PortAllocatorSession* ses, 350 void OnCandidatesReady(PortAllocatorSession* ses,
358 const std::vector<cricket::Candidate>& candidates) { 351 const std::vector<Candidate>& candidates) {
359 for (size_t i = 0; i < candidates.size(); ++i) { 352 for (size_t i = 0; i < candidates.size(); ++i) {
360 LOG(LS_INFO) << "OnCandidatesReady: " << candidates[i].ToString(); 353 LOG(LS_INFO) << "OnCandidatesReady: " << candidates[i].ToString();
361 candidates_.push_back(candidates[i]); 354 candidates_.push_back(candidates[i]);
362 } 355 }
363 // Make sure the new candidates are added to Candidates. 356 // Make sure the new candidates are added to Candidates.
364 auto ses_candidates = ses->ReadyCandidates(); 357 auto ses_candidates = ses->ReadyCandidates();
365 for (const cricket::Candidate& candidate : candidates) { 358 for (const Candidate& candidate : candidates) {
366 EXPECT_NE( 359 EXPECT_NE(
367 ses_candidates.end(), 360 ses_candidates.end(),
368 std::find(ses_candidates.begin(), ses_candidates.end(), candidate)); 361 std::find(ses_candidates.begin(), ses_candidates.end(), candidate));
369 } 362 }
370 } 363 }
371 364
372 bool HasRelayAddress(const cricket::ProtocolAddress& proto_addr) { 365 bool HasRelayAddress(const ProtocolAddress& proto_addr) {
373 for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) { 366 for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) {
374 cricket::RelayServerConfig server_config = allocator_->turn_servers()[i]; 367 RelayServerConfig server_config = allocator_->turn_servers()[i];
375 cricket::PortList::const_iterator relay_port; 368 PortList::const_iterator relay_port;
376 for (relay_port = server_config.ports.begin(); 369 for (relay_port = server_config.ports.begin();
377 relay_port != server_config.ports.end(); ++relay_port) { 370 relay_port != server_config.ports.end(); ++relay_port) {
378 if (proto_addr.address == relay_port->address && 371 if (proto_addr.address == relay_port->address &&
379 proto_addr.proto == relay_port->proto) 372 proto_addr.proto == relay_port->proto)
380 return true; 373 return true;
381 } 374 }
382 } 375 }
383 return false; 376 return false;
384 } 377 }
385 378
386 void ResetWithStunServer(const rtc::SocketAddress& stun_server, 379 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
387 bool with_nat) { 380 bool with_nat) {
388 if (with_nat) { 381 if (with_nat) {
389 nat_server_.reset(new rtc::NATServer( 382 nat_server_.reset(new rtc::NATServer(
390 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(), 383 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
391 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0))); 384 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
392 } else { 385 } else {
393 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory()); 386 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
394 } 387 }
395 388
396 ServerAddresses stun_servers; 389 ServerAddresses stun_servers;
397 if (!stun_server.IsNil()) { 390 if (!stun_server.IsNil()) {
398 stun_servers.insert(stun_server); 391 stun_servers.insert(stun_server);
399 } 392 }
400 allocator_.reset(new cricket::BasicPortAllocator( 393 allocator_.reset(new BasicPortAllocator(
401 &network_manager_, nat_socket_factory_.get(), stun_servers)); 394 &network_manager_, nat_socket_factory_.get(), stun_servers));
402 allocator().set_step_delay(cricket::kMinimumStepDelay); 395 allocator().set_step_delay(kMinimumStepDelay);
403 } 396 }
404 397
405 std::unique_ptr<rtc::PhysicalSocketServer> pss_; 398 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
406 std::unique_ptr<rtc::VirtualSocketServer> vss_; 399 std::unique_ptr<rtc::VirtualSocketServer> vss_;
407 std::unique_ptr<rtc::FirewallSocketServer> fss_; 400 std::unique_ptr<rtc::FirewallSocketServer> fss_;
408 rtc::SocketServerScope ss_scope_; 401 rtc::SocketServerScope ss_scope_;
409 std::unique_ptr<rtc::NATServer> nat_server_; 402 std::unique_ptr<rtc::NATServer> nat_server_;
410 rtc::NATSocketFactory nat_factory_; 403 rtc::NATSocketFactory nat_factory_;
411 std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_; 404 std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
412 std::unique_ptr<cricket::TestStunServer> stun_server_; 405 std::unique_ptr<TestStunServer> stun_server_;
413 cricket::TestRelayServer relay_server_; 406 TestRelayServer relay_server_;
414 cricket::TestTurnServer turn_server_; 407 TestTurnServer turn_server_;
415 rtc::FakeNetworkManager network_manager_; 408 rtc::FakeNetworkManager network_manager_;
416 std::unique_ptr<cricket::BasicPortAllocator> allocator_; 409 std::unique_ptr<BasicPortAllocator> allocator_;
417 std::unique_ptr<cricket::PortAllocatorSession> session_; 410 std::unique_ptr<PortAllocatorSession> session_;
418 std::vector<cricket::PortInterface*> ports_; 411 std::vector<PortInterface*> ports_;
419 std::vector<cricket::Candidate> candidates_; 412 std::vector<Candidate> candidates_;
420 bool candidate_allocation_done_; 413 bool candidate_allocation_done_;
421 }; 414 };
422 415
423 // Tests that we can init the port allocator and create a session. 416 // Tests that we can init the port allocator and create a session.
424 TEST_F(BasicPortAllocatorTest, TestBasic) { 417 TEST_F(BasicPortAllocatorTest, TestBasic) {
425 EXPECT_EQ(&network_manager_, allocator().network_manager()); 418 EXPECT_EQ(&network_manager_, allocator().network_manager());
426 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin()); 419 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
427 ASSERT_EQ(1u, allocator().turn_servers().size()); 420 ASSERT_EQ(1u, allocator().turn_servers().size());
428 EXPECT_EQ(cricket::RELAY_GTURN, allocator().turn_servers()[0].type); 421 EXPECT_EQ(RELAY_GTURN, allocator().turn_servers()[0].type);
429 // Empty relay credentials are used for GTURN. 422 // Empty relay credentials are used for GTURN.
430 EXPECT_TRUE(allocator().turn_servers()[0].credentials.username.empty()); 423 EXPECT_TRUE(allocator().turn_servers()[0].credentials.username.empty());
431 EXPECT_TRUE(allocator().turn_servers()[0].credentials.password.empty()); 424 EXPECT_TRUE(allocator().turn_servers()[0].credentials.password.empty());
432 EXPECT_TRUE(HasRelayAddress( 425 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayUdpIntAddr, PROTO_UDP)));
433 cricket::ProtocolAddress(kRelayUdpIntAddr, cricket::PROTO_UDP))); 426 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayTcpIntAddr, PROTO_TCP)));
434 EXPECT_TRUE(HasRelayAddress( 427 EXPECT_TRUE(
435 cricket::ProtocolAddress(kRelayTcpIntAddr, cricket::PROTO_TCP))); 428 HasRelayAddress(ProtocolAddress(kRelaySslTcpIntAddr, PROTO_SSLTCP)));
436 EXPECT_TRUE(HasRelayAddress( 429 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
437 cricket::ProtocolAddress(kRelaySslTcpIntAddr, cricket::PROTO_SSLTCP)));
438 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
439 EXPECT_FALSE(session_->CandidatesAllocationDone()); 430 EXPECT_FALSE(session_->CandidatesAllocationDone());
440 } 431 }
441 432
442 // Tests that our network filtering works properly. 433 // Tests that our network filtering works properly.
443 TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) { 434 TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) {
444 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0", 435 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
445 rtc::ADAPTER_TYPE_ETHERNET); 436 rtc::ADAPTER_TYPE_ETHERNET);
446 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0", 437 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
447 rtc::ADAPTER_TYPE_WIFI); 438 rtc::ADAPTER_TYPE_WIFI);
448 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0", 439 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
449 rtc::ADAPTER_TYPE_CELLULAR); 440 rtc::ADAPTER_TYPE_CELLULAR);
450 AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0", 441 AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0",
451 rtc::ADAPTER_TYPE_VPN); 442 rtc::ADAPTER_TYPE_VPN);
452 AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo", 443 AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo",
453 rtc::ADAPTER_TYPE_LOOPBACK); 444 rtc::ADAPTER_TYPE_LOOPBACK);
454 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 445 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
455 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_STUN | 446 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
456 cricket::PORTALLOCATOR_DISABLE_RELAY | 447 PORTALLOCATOR_DISABLE_TCP);
457 cricket::PORTALLOCATOR_DISABLE_TCP);
458 session_->StartGettingPorts(); 448 session_->StartGettingPorts();
459 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); 449 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
460 EXPECT_EQ(4U, candidates_.size()); 450 EXPECT_EQ(4U, candidates_.size());
461 for (cricket::Candidate candidate : candidates_) { 451 for (Candidate candidate : candidates_) {
462 EXPECT_LT(candidate.address().ip(), 0x12345604U); 452 EXPECT_LT(candidate.address().ip(), 0x12345604U);
463 } 453 }
464 } 454 }
465 455
466 TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) { 456 TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) {
467 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0", 457 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
468 rtc::ADAPTER_TYPE_ETHERNET); 458 rtc::ADAPTER_TYPE_ETHERNET);
469 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0", 459 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
470 rtc::ADAPTER_TYPE_WIFI); 460 rtc::ADAPTER_TYPE_WIFI);
471 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0", 461 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
472 rtc::ADAPTER_TYPE_CELLULAR); 462 rtc::ADAPTER_TYPE_CELLULAR);
473 allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET | 463 allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET |
474 rtc::ADAPTER_TYPE_LOOPBACK | 464 rtc::ADAPTER_TYPE_LOOPBACK |
475 rtc::ADAPTER_TYPE_WIFI); 465 rtc::ADAPTER_TYPE_WIFI);
476 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 466 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
477 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_STUN | 467 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
478 cricket::PORTALLOCATOR_DISABLE_RELAY | 468 PORTALLOCATOR_DISABLE_TCP);
479 cricket::PORTALLOCATOR_DISABLE_TCP);
480 session_->StartGettingPorts(); 469 session_->StartGettingPorts();
481 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); 470 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
482 EXPECT_EQ(1U, candidates_.size()); 471 EXPECT_EQ(1U, candidates_.size());
483 EXPECT_EQ(0x12345602U, candidates_[0].address().ip()); 472 EXPECT_EQ(0x12345602U, candidates_[0].address().ip());
484 } 473 }
485 474
486 // Tests that we allocator session not trying to allocate ports for every 250ms. 475 // Tests that we allocator session not trying to allocate ports for every 250ms.
487 TEST_F(BasicPortAllocatorTest, TestNoNetworkInterface) { 476 TEST_F(BasicPortAllocatorTest, TestNoNetworkInterface) {
488 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 477 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
489 session_->StartGettingPorts(); 478 session_->StartGettingPorts();
490 // Waiting for one second to make sure BasicPortAllocatorSession has not 479 // Waiting for one second to make sure BasicPortAllocatorSession has not
491 // called OnAllocate multiple times. In old behavior it's called every 250ms. 480 // called OnAllocate multiple times. In old behavior it's called every 250ms.
492 // When there are no network interfaces, each execution of OnAllocate will 481 // When there are no network interfaces, each execution of OnAllocate will
493 // result in SignalCandidatesAllocationDone signal. 482 // result in SignalCandidatesAllocationDone signal.
494 rtc::Thread::Current()->ProcessMessages(1000); 483 rtc::Thread::Current()->ProcessMessages(1000);
495 EXPECT_TRUE(candidate_allocation_done_); 484 EXPECT_TRUE(candidate_allocation_done_);
496 EXPECT_EQ(0U, candidates_.size()); 485 EXPECT_EQ(0U, candidates_.size());
497 } 486 }
498 487
499 // Test that we could use loopback interface as host candidate. 488 // Test that we could use loopback interface as host candidate.
500 TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) { 489 TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
501 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK); 490 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
502 allocator_->SetNetworkIgnoreMask(0); 491 allocator_->SetNetworkIgnoreMask(0);
503 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 492 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
504 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_STUN | 493 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
505 cricket::PORTALLOCATOR_DISABLE_RELAY | 494 PORTALLOCATOR_DISABLE_TCP);
506 cricket::PORTALLOCATOR_DISABLE_TCP);
507 session_->StartGettingPorts(); 495 session_->StartGettingPorts();
508 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); 496 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
509 EXPECT_EQ(1U, candidates_.size()); 497 EXPECT_EQ(1U, candidates_.size());
510 } 498 }
511 499
512 // Tests that we can get all the desired addresses successfully. 500 // Tests that we can get all the desired addresses successfully.
513 TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) { 501 TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
514 AddInterface(kClientAddr); 502 AddInterface(kClientAddr);
515 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 503 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
516 session_->StartGettingPorts(); 504 session_->StartGettingPorts();
517 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout); 505 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
518 EXPECT_EQ(4U, ports_.size()); 506 EXPECT_EQ(4U, ports_.size());
519 EXPECT_PRED5(CheckCandidate, candidates_[0], 507 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
520 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", 508 "local", "udp", kClientAddr);
521 kClientAddr); 509 EXPECT_PRED5(CheckCandidate, candidates_[1], ICE_CANDIDATE_COMPONENT_RTP,
522 EXPECT_PRED5(CheckCandidate, candidates_[1], 510 "stun", "udp", kClientAddr);
523 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", 511 EXPECT_PRED5(CheckCandidate, candidates_[2], ICE_CANDIDATE_COMPONENT_RTP,
524 kClientAddr); 512 "relay", "udp", kRelayUdpIntAddr);
525 EXPECT_PRED5(CheckCandidate, candidates_[2], 513 EXPECT_PRED5(CheckCandidate, candidates_[3], ICE_CANDIDATE_COMPONENT_RTP,
526 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", 514 "relay", "udp", kRelayUdpExtAddr);
527 kRelayUdpIntAddr); 515 EXPECT_PRED5(CheckCandidate, candidates_[4], ICE_CANDIDATE_COMPONENT_RTP,
528 EXPECT_PRED5(CheckCandidate, candidates_[3], 516 "relay", "tcp", kRelayTcpIntAddr);
529 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", 517 EXPECT_PRED5(CheckCandidate, candidates_[5], ICE_CANDIDATE_COMPONENT_RTP,
530 kRelayUdpExtAddr); 518 "local", "tcp", kClientAddr);
531 EXPECT_PRED5(CheckCandidate, candidates_[4], 519 EXPECT_PRED5(CheckCandidate, candidates_[6], ICE_CANDIDATE_COMPONENT_RTP,
532 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", 520 "relay", "ssltcp", kRelaySslTcpIntAddr);
533 kRelayTcpIntAddr);
534 EXPECT_PRED5(CheckCandidate, candidates_[5],
535 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
536 kClientAddr);
537 EXPECT_PRED5(CheckCandidate, candidates_[6],
538 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp",
539 kRelaySslTcpIntAddr);
540 EXPECT_TRUE(candidate_allocation_done_); 521 EXPECT_TRUE(candidate_allocation_done_);
541 } 522 }
542 523
543 // Test that when the same network interface is brought down and up, the 524 // Test that when the same network interface is brought down and up, the
544 // port allocator session will restart a new allocation sequence if 525 // port allocator session will restart a new allocation sequence if
545 // it is not stopped. 526 // it is not stopped.
546 TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) { 527 TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
547 std::string if_name("test_net0"); 528 std::string if_name("test_net0");
548 AddInterface(kClientAddr, if_name); 529 AddInterface(kClientAddr, if_name);
549 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 530 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
550 session_->StartGettingPorts(); 531 session_->StartGettingPorts();
551 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout); 532 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
552 EXPECT_EQ(4U, ports_.size()); 533 EXPECT_EQ(4U, ports_.size());
553 EXPECT_TRUE(candidate_allocation_done_); 534 EXPECT_TRUE(candidate_allocation_done_);
554 candidate_allocation_done_ = false; 535 candidate_allocation_done_ = false;
555 candidates_.clear(); 536 candidates_.clear();
556 ports_.clear(); 537 ports_.clear();
557 538
558 RemoveInterface(kClientAddr); 539 RemoveInterface(kClientAddr);
559 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout); 540 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
560 EXPECT_EQ(0U, ports_.size()); 541 EXPECT_EQ(0U, ports_.size());
561 EXPECT_FALSE(candidate_allocation_done_); 542 EXPECT_FALSE(candidate_allocation_done_);
562 543
563 // When the same interfaces are added again, new candidates/ports should be 544 // When the same interfaces are added again, new candidates/ports should be
564 // generated. 545 // generated.
565 AddInterface(kClientAddr, if_name); 546 AddInterface(kClientAddr, if_name);
566 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout); 547 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
567 EXPECT_EQ(4U, ports_.size()); 548 EXPECT_EQ(4U, ports_.size());
568 EXPECT_TRUE(candidate_allocation_done_); 549 EXPECT_TRUE(candidate_allocation_done_);
569 } 550 }
570 551
571 // Test that when the same network interface is brought down and up, the 552 // Test that when the same network interface is brought down and up, the
572 // port allocator session will not restart a new allocation sequence if 553 // port allocator session will not restart a new allocation sequence if
573 // it is stopped. 554 // it is stopped.
574 TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) { 555 TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
575 std::string if_name("test_net0"); 556 std::string if_name("test_net0");
576 AddInterface(kClientAddr, if_name); 557 AddInterface(kClientAddr, if_name);
577 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 558 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
578 session_->StartGettingPorts(); 559 session_->StartGettingPorts();
579 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout); 560 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
580 EXPECT_EQ(4U, ports_.size()); 561 EXPECT_EQ(4U, ports_.size());
581 EXPECT_TRUE(candidate_allocation_done_); 562 EXPECT_TRUE(candidate_allocation_done_);
582 session_->StopGettingPorts(); 563 session_->StopGettingPorts();
583 candidates_.clear(); 564 candidates_.clear();
584 ports_.clear(); 565 ports_.clear();
585 566
586 RemoveInterface(kClientAddr); 567 RemoveInterface(kClientAddr);
587 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout); 568 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
588 EXPECT_EQ(0U, ports_.size()); 569 EXPECT_EQ(0U, ports_.size());
589 570
590 // When the same interfaces are added again, new candidates/ports should not 571 // When the same interfaces are added again, new candidates/ports should not
591 // be generated because the session has stopped. 572 // be generated because the session has stopped.
592 AddInterface(kClientAddr, if_name); 573 AddInterface(kClientAddr, if_name);
593 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout); 574 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
594 EXPECT_EQ(0U, ports_.size()); 575 EXPECT_EQ(0U, ports_.size());
595 EXPECT_TRUE(candidate_allocation_done_); 576 EXPECT_TRUE(candidate_allocation_done_);
596 } 577 }
597 578
598 // Verify candidates with default step delay of 1sec. 579 // Verify candidates with default step delay of 1sec.
599 TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) { 580 TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
600 AddInterface(kClientAddr); 581 AddInterface(kClientAddr);
601 allocator_->set_step_delay(cricket::kDefaultStepDelay); 582 allocator_->set_step_delay(kDefaultStepDelay);
602 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 583 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
603 session_->StartGettingPorts(); 584 session_->StartGettingPorts();
604 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000); 585 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
605 EXPECT_EQ(2U, ports_.size()); 586 EXPECT_EQ(2U, ports_.size());
606 ASSERT_EQ_WAIT(4U, candidates_.size(), 2000); 587 ASSERT_EQ_WAIT(4U, candidates_.size(), 2000);
607 EXPECT_EQ(3U, ports_.size()); 588 EXPECT_EQ(3U, ports_.size());
608 EXPECT_PRED5(CheckCandidate, candidates_[2], 589 EXPECT_PRED5(CheckCandidate, candidates_[2], ICE_CANDIDATE_COMPONENT_RTP,
609 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", 590 "relay", "udp", kRelayUdpIntAddr);
610 kRelayUdpIntAddr); 591 EXPECT_PRED5(CheckCandidate, candidates_[3], ICE_CANDIDATE_COMPONENT_RTP,
611 EXPECT_PRED5(CheckCandidate, candidates_[3], 592 "relay", "udp", kRelayUdpExtAddr);
612 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
613 kRelayUdpExtAddr);
614 ASSERT_EQ_WAIT(6U, candidates_.size(), 1500); 593 ASSERT_EQ_WAIT(6U, candidates_.size(), 1500);
615 EXPECT_PRED5(CheckCandidate, candidates_[4], 594 EXPECT_PRED5(CheckCandidate, candidates_[4], ICE_CANDIDATE_COMPONENT_RTP,
616 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", 595 "relay", "tcp", kRelayTcpIntAddr);
617 kRelayTcpIntAddr); 596 EXPECT_PRED5(CheckCandidate, candidates_[5], ICE_CANDIDATE_COMPONENT_RTP,
618 EXPECT_PRED5(CheckCandidate, candidates_[5], 597 "local", "tcp", kClientAddr);
619 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
620 kClientAddr);
621 EXPECT_EQ(4U, ports_.size()); 598 EXPECT_EQ(4U, ports_.size());
622 ASSERT_EQ_WAIT(7U, candidates_.size(), 2000); 599 ASSERT_EQ_WAIT(7U, candidates_.size(), 2000);
623 EXPECT_PRED5(CheckCandidate, candidates_[6], 600 EXPECT_PRED5(CheckCandidate, candidates_[6], ICE_CANDIDATE_COMPONENT_RTP,
624 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp", 601 "relay", "ssltcp", kRelaySslTcpIntAddr);
625 kRelaySslTcpIntAddr);
626 EXPECT_EQ(4U, ports_.size()); 602 EXPECT_EQ(4U, ports_.size());
627 EXPECT_TRUE(candidate_allocation_done_); 603 EXPECT_TRUE(candidate_allocation_done_);
628 // If we Stop gathering now, we shouldn't get a second "done" callback. 604 // If we Stop gathering now, we shouldn't get a second "done" callback.
629 session_->StopGettingPorts(); 605 session_->StopGettingPorts();
630 } 606 }
631 607
632 TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) { 608 TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
633 AddInterface(kClientAddr); 609 AddInterface(kClientAddr);
634 EXPECT_TRUE( 610 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
635 CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP, cricket::CN_VIDEO));
636 session_->StartGettingPorts(); 611 session_->StartGettingPorts();
637 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout); 612 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
638 EXPECT_TRUE(candidate_allocation_done_); 613 EXPECT_TRUE(candidate_allocation_done_);
639 // If we Stop gathering now, we shouldn't get a second "done" callback. 614 // If we Stop gathering now, we shouldn't get a second "done" callback.
640 session_->StopGettingPorts(); 615 session_->StopGettingPorts();
641 616
642 // All ports should have unset send-buffer sizes. 617 // All ports should have unset send-buffer sizes.
643 CheckSendBufferSizesOfAllPorts(-1); 618 CheckSendBufferSizesOfAllPorts(-1);
644 } 619 }
645 620
646 // Tests that we can get callback after StopGetAllPorts. 621 // Tests that we can get callback after StopGetAllPorts.
647 TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) { 622 TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
648 AddInterface(kClientAddr); 623 AddInterface(kClientAddr);
649 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 624 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
650 session_->StartGettingPorts(); 625 session_->StartGettingPorts();
651 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout); 626 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
652 EXPECT_EQ(2U, ports_.size()); 627 EXPECT_EQ(2U, ports_.size());
653 session_->StopGettingPorts(); 628 session_->StopGettingPorts();
654 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); 629 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
655 } 630 }
656 631
657 // Test that we restrict client ports appropriately when a port range is set. 632 // Test that we restrict client ports appropriately when a port range is set.
658 // We check the candidates for udp/stun/tcp ports, and the from address 633 // We check the candidates for udp/stun/tcp ports, and the from address
659 // for relay ports. 634 // for relay ports.
660 TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) { 635 TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
661 AddInterface(kClientAddr); 636 AddInterface(kClientAddr);
662 // Check that an invalid port range fails. 637 // Check that an invalid port range fails.
663 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort)); 638 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
664 // Check that a null port range succeeds. 639 // Check that a null port range succeeds.
665 EXPECT_TRUE(SetPortRange(0, 0)); 640 EXPECT_TRUE(SetPortRange(0, 0));
666 // Check that a valid port range succeeds. 641 // Check that a valid port range succeeds.
667 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort)); 642 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
668 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 643 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
669 session_->StartGettingPorts(); 644 session_->StartGettingPorts();
670 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout); 645 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
671 EXPECT_EQ(4U, ports_.size()); 646 EXPECT_EQ(4U, ports_.size());
672 // Check the port number for the UDP port object. 647 // Check the port number for the UDP port object.
673 EXPECT_PRED3(CheckPort, candidates_[0].address(), kMinPort, kMaxPort); 648 EXPECT_PRED3(CheckPort, candidates_[0].address(), kMinPort, kMaxPort);
674 // Check the port number for the STUN port object. 649 // Check the port number for the STUN port object.
675 EXPECT_PRED3(CheckPort, candidates_[1].address(), kMinPort, kMaxPort); 650 EXPECT_PRED3(CheckPort, candidates_[1].address(), kMinPort, kMaxPort);
676 // Check the port number used to connect to the relay server. 651 // Check the port number used to connect to the relay server.
677 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(), kMinPort, 652 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(), kMinPort,
678 kMaxPort); 653 kMaxPort);
679 // Check the port number for the TCP port object. 654 // Check the port number for the TCP port object.
680 EXPECT_PRED3(CheckPort, candidates_[5].address(), kMinPort, kMaxPort); 655 EXPECT_PRED3(CheckPort, candidates_[5].address(), kMinPort, kMaxPort);
681 EXPECT_TRUE(candidate_allocation_done_); 656 EXPECT_TRUE(candidate_allocation_done_);
682 } 657 }
683 658
684 // Test that we don't crash or malfunction if we have no network adapters. 659 // Test that we don't crash or malfunction if we have no network adapters.
685 TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) { 660 TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
686 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 661 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
687 session_->StartGettingPorts(); 662 session_->StartGettingPorts();
688 rtc::Thread::Current()->ProcessMessages(100); 663 rtc::Thread::Current()->ProcessMessages(100);
689 // Without network adapter, we should not get any candidate. 664 // Without network adapter, we should not get any candidate.
690 EXPECT_EQ(0U, candidates_.size()); 665 EXPECT_EQ(0U, candidates_.size());
691 EXPECT_TRUE(candidate_allocation_done_); 666 EXPECT_TRUE(candidate_allocation_done_);
692 } 667 }
693 668
694 // Test that when enumeration is disabled, we should not have any ports when 669 // Test that when enumeration is disabled, we should not have any ports when
695 // candidate_filter() is set to CF_RELAY and no relay is specified. 670 // candidate_filter() is set to CF_RELAY and no relay is specified.
696 TEST_F(BasicPortAllocatorTest, 671 TEST_F(BasicPortAllocatorTest,
697 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) { 672 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
698 ResetWithStunServerNoNat(kStunAddr); 673 ResetWithStunServerNoNat(kStunAddr);
699 allocator().set_candidate_filter(cricket::CF_RELAY); 674 allocator().set_candidate_filter(CF_RELAY);
700 // Expect to see no ports and no candidates. 675 // Expect to see no ports and no candidates.
701 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(), 676 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
702 rtc::IPAddress(), rtc::IPAddress()); 677 rtc::IPAddress(), rtc::IPAddress());
703 } 678 }
704 679
705 // Test that even with multiple interfaces, the result should still be a single 680 // Test that even with multiple interfaces, the result should still be a single
706 // default private, one STUN and one TURN candidate since we bind to any address 681 // default private, one STUN and one TURN candidate since we bind to any address
707 // (i.e. all 0s). 682 // (i.e. all 0s).
708 TEST_F(BasicPortAllocatorTest, 683 TEST_F(BasicPortAllocatorTest,
709 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) { 684 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
710 AddInterface(kPrivateAddr); 685 AddInterface(kPrivateAddr);
711 AddInterface(kPrivateAddr2); 686 AddInterface(kPrivateAddr2);
712 ResetWithStunServerAndNat(kStunAddr); 687 ResetWithStunServerAndNat(kStunAddr);
713 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress()); 688 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
714 689
715 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default 690 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
716 // address set and we have no IPv6 STUN server, there should be no IPv6 691 // address set and we have no IPv6 STUN server, there should be no IPv6
717 // candidates. 692 // candidates.
718 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 693 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
719 session_->set_flags(cricket::PORTALLOCATOR_ENABLE_IPV6); 694 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
720 695
721 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports 696 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
722 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and 697 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
723 // TURN/UDP candidates. 698 // TURN/UDP candidates.
724 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(), 699 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
725 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(), 700 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
726 rtc::IPAddress()); 701 rtc::IPAddress());
727 } 702 }
728 703
729 // Test that we should get a default private, STUN, TURN/UDP and TURN/TCP 704 // Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
730 // candidates when both TURN/UDP and TURN/TCP servers are specified. 705 // candidates when both TURN/UDP and TURN/TCP servers are specified.
731 TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) { 706 TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
732 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP); 707 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
733 AddInterface(kPrivateAddr); 708 AddInterface(kPrivateAddr);
734 ResetWithStunServerAndNat(kStunAddr); 709 ResetWithStunServerAndNat(kStunAddr);
735 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr); 710 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
736 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default 711 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
737 // private, STUN, TURN/UDP, and TURN/TCP candidates. 712 // private, STUN, TURN/UDP, and TURN/TCP candidates.
738 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(), 713 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
739 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(), 714 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
740 kTurnUdpExtAddr.ipaddr()); 715 kTurnUdpExtAddr.ipaddr());
741 } 716 }
742 717
743 // Test that when adapter enumeration is disabled, for endpoints without 718 // Test that when adapter enumeration is disabled, for endpoints without
744 // STUN/TURN specified, a default private candidate is still generated. 719 // STUN/TURN specified, a default private candidate is still generated.
745 TEST_F(BasicPortAllocatorTest, 720 TEST_F(BasicPortAllocatorTest,
746 TestDisableAdapterEnumerationWithoutNatOrServers) { 721 TestDisableAdapterEnumerationWithoutNatOrServers) {
747 ResetWithNoServersOrNat(); 722 ResetWithNoServersOrNat();
748 // Expect to see 2 ports: STUN and TCP ports, one default private candidate. 723 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
749 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(), 724 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
750 rtc::IPAddress(), rtc::IPAddress()); 725 rtc::IPAddress(), rtc::IPAddress());
751 } 726 }
752 727
753 // Test that when adapter enumeration is disabled, with 728 // Test that when adapter enumeration is disabled, with
754 // PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind 729 // PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
755 // a NAT, there is no local candidate. 730 // a NAT, there is no local candidate.
756 TEST_F(BasicPortAllocatorTest, 731 TEST_F(BasicPortAllocatorTest,
757 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) { 732 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
758 ResetWithStunServerNoNat(kStunAddr); 733 ResetWithStunServerNoNat(kStunAddr);
759 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 734 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
760 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE); 735 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
761 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN 736 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
762 // candidate. 737 // candidate.
763 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(), 738 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
764 rtc::IPAddress(), rtc::IPAddress()); 739 rtc::IPAddress(), rtc::IPAddress());
765 } 740 }
766 741
767 // Test that when adapter enumeration is disabled, with 742 // Test that when adapter enumeration is disabled, with
768 // PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind 743 // PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
769 // a NAT, there is no local candidate. However, this specified default route 744 // a NAT, there is no local candidate. However, this specified default route
770 // (kClientAddr) which was discovered when sending STUN requests, will become 745 // (kClientAddr) which was discovered when sending STUN requests, will become
771 // the srflx addresses. 746 // the srflx addresses.
772 TEST_F( 747 TEST_F(
773 BasicPortAllocatorTest, 748 BasicPortAllocatorTest,
774 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabledWithDiffere ntDefaultRoute) { 749 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabledWithDiffere ntDefaultRoute) {
775 ResetWithStunServerNoNat(kStunAddr); 750 ResetWithStunServerNoNat(kStunAddr);
776 AddInterfaceAsDefaultRoute(kClientAddr); 751 AddInterfaceAsDefaultRoute(kClientAddr);
777 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 752 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
778 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE); 753 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
779 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN 754 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
780 // candidate. 755 // candidate.
781 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(), 756 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
782 rtc::IPAddress(), rtc::IPAddress()); 757 rtc::IPAddress(), rtc::IPAddress());
783 } 758 }
784 759
785 // Test that when adapter enumeration is disabled, with 760 // Test that when adapter enumeration is disabled, with
786 // PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a 761 // PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
787 // NAT, there is only one STUN candidate. 762 // NAT, there is only one STUN candidate.
788 TEST_F(BasicPortAllocatorTest, 763 TEST_F(BasicPortAllocatorTest,
789 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) { 764 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
790 ResetWithStunServerAndNat(kStunAddr); 765 ResetWithStunServerAndNat(kStunAddr);
791 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 766 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
792 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE); 767 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
793 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate. 768 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
794 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(), 769 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
795 rtc::IPAddress(), rtc::IPAddress()); 770 rtc::IPAddress(), rtc::IPAddress());
796 } 771 }
797 772
798 // Test that we disable relay over UDP, and only TCP is used when connecting to 773 // Test that we disable relay over UDP, and only TCP is used when connecting to
799 // the relay server. 774 // the relay server.
800 TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) { 775 TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
801 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP); 776 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
802 AddInterface(kClientAddr); 777 AddInterface(kClientAddr);
803 ResetWithStunServerAndNat(kStunAddr); 778 ResetWithStunServerAndNat(kStunAddr);
804 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr); 779 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
805 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 780 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
806 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_UDP_RELAY | 781 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
807 cricket::PORTALLOCATOR_DISABLE_UDP | 782 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
808 cricket::PORTALLOCATOR_DISABLE_STUN | 783 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
809 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
810 784
811 session_->StartGettingPorts(); 785 session_->StartGettingPorts();
812 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); 786 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
813 787
814 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and 788 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
815 // TURN/TCP candidates. 789 // TURN/TCP candidates.
816 EXPECT_EQ(2U, ports_.size()); 790 EXPECT_EQ(2U, ports_.size());
817 EXPECT_EQ(2U, candidates_.size()); 791 EXPECT_EQ(2U, candidates_.size());
818 EXPECT_PRED5(CheckCandidate, candidates_[0], 792 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
819 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", 793 "relay", "udp", kTurnUdpExtAddr);
820 kTurnUdpExtAddr);
821 // The TURN candidate should use TCP to contact the TURN server. 794 // The TURN candidate should use TCP to contact the TURN server.
822 EXPECT_EQ(cricket::TCP_PROTOCOL_NAME, candidates_[0].relay_protocol()); 795 EXPECT_EQ(TCP_PROTOCOL_NAME, candidates_[0].relay_protocol());
823 EXPECT_PRED5(CheckCandidate, candidates_[1], 796 EXPECT_PRED5(CheckCandidate, candidates_[1], ICE_CANDIDATE_COMPONENT_RTP,
824 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", 797 "local", "tcp", kClientAddr);
825 kClientAddr);
826 } 798 }
827 799
828 // Disable for asan, see 800 // Disable for asan, see
829 // https://code.google.com/p/webrtc/issues/detail?id=4743 for details. 801 // https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
830 #if !defined(ADDRESS_SANITIZER) 802 #if !defined(ADDRESS_SANITIZER)
831 803
832 // Test that we can get OnCandidatesAllocationDone callback when all the ports 804 // Test that we can get OnCandidatesAllocationDone callback when all the ports
833 // are disabled. 805 // are disabled.
834 TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) { 806 TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
835 AddInterface(kClientAddr); 807 AddInterface(kClientAddr);
836 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 808 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
837 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_UDP | 809 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
838 cricket::PORTALLOCATOR_DISABLE_STUN | 810 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
839 cricket::PORTALLOCATOR_DISABLE_RELAY |
840 cricket::PORTALLOCATOR_DISABLE_TCP);
841 session_->StartGettingPorts(); 811 session_->StartGettingPorts();
842 rtc::Thread::Current()->ProcessMessages(100); 812 rtc::Thread::Current()->ProcessMessages(100);
843 EXPECT_EQ(0U, candidates_.size()); 813 EXPECT_EQ(0U, candidates_.size());
844 EXPECT_TRUE(candidate_allocation_done_); 814 EXPECT_TRUE(candidate_allocation_done_);
845 } 815 }
846 816
847 // Test that we don't crash or malfunction if we can't create UDP sockets. 817 // Test that we don't crash or malfunction if we can't create UDP sockets.
848 TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) { 818 TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
849 AddInterface(kClientAddr); 819 AddInterface(kClientAddr);
850 fss_->set_udp_sockets_enabled(false); 820 fss_->set_udp_sockets_enabled(false);
851 EXPECT_TRUE(CreateSession(1)); 821 EXPECT_TRUE(CreateSession(1));
852 session_->StartGettingPorts(); 822 session_->StartGettingPorts();
853 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout); 823 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
854 EXPECT_EQ(2U, ports_.size()); 824 EXPECT_EQ(2U, ports_.size());
855 EXPECT_PRED5(CheckCandidate, candidates_[0], 825 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
856 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", 826 "relay", "udp", kRelayUdpIntAddr);
857 kRelayUdpIntAddr); 827 EXPECT_PRED5(CheckCandidate, candidates_[1], ICE_CANDIDATE_COMPONENT_RTP,
858 EXPECT_PRED5(CheckCandidate, candidates_[1], 828 "relay", "udp", kRelayUdpExtAddr);
859 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", 829 EXPECT_PRED5(CheckCandidate, candidates_[2], ICE_CANDIDATE_COMPONENT_RTP,
860 kRelayUdpExtAddr); 830 "relay", "tcp", kRelayTcpIntAddr);
861 EXPECT_PRED5(CheckCandidate, candidates_[2], 831 EXPECT_PRED5(CheckCandidate, candidates_[3], ICE_CANDIDATE_COMPONENT_RTP,
862 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", 832 "local", "tcp", kClientAddr);
863 kRelayTcpIntAddr); 833 EXPECT_PRED5(CheckCandidate, candidates_[4], ICE_CANDIDATE_COMPONENT_RTP,
864 EXPECT_PRED5(CheckCandidate, candidates_[3], 834 "relay", "ssltcp", kRelaySslTcpIntAddr);
865 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
866 kClientAddr);
867 EXPECT_PRED5(CheckCandidate, candidates_[4],
868 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp",
869 kRelaySslTcpIntAddr);
870 EXPECT_TRUE(candidate_allocation_done_); 835 EXPECT_TRUE(candidate_allocation_done_);
871 } 836 }
872 837
873 #endif // if !defined(ADDRESS_SANITIZER) 838 #endif // if !defined(ADDRESS_SANITIZER)
874 839
875 // Test that we don't crash or malfunction if we can't create UDP sockets or 840 // Test that we don't crash or malfunction if we can't create UDP sockets or
876 // listen on TCP sockets. We still give out a local TCP address, since 841 // listen on TCP sockets. We still give out a local TCP address, since
877 // apparently this is needed for the remote side to accept our connection. 842 // apparently this is needed for the remote side to accept our connection.
878 TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) { 843 TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
879 AddInterface(kClientAddr); 844 AddInterface(kClientAddr);
(...skipping 14 matching lines...) Expand all
894 kRelaySslTcpIntAddr); 859 kRelaySslTcpIntAddr);
895 EXPECT_TRUE(candidate_allocation_done_); 860 EXPECT_TRUE(candidate_allocation_done_);
896 } 861 }
897 862
898 // Test that we don't crash or malfunction if we can't create any sockets. 863 // Test that we don't crash or malfunction if we can't create any sockets.
899 // TODO(deadbeef): Find a way to exit early here. 864 // TODO(deadbeef): Find a way to exit early here.
900 TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) { 865 TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
901 AddInterface(kClientAddr); 866 AddInterface(kClientAddr);
902 fss_->set_tcp_sockets_enabled(false); 867 fss_->set_tcp_sockets_enabled(false);
903 fss_->set_udp_sockets_enabled(false); 868 fss_->set_udp_sockets_enabled(false);
904 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 869 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
905 session_->StartGettingPorts(); 870 session_->StartGettingPorts();
906 WAIT(candidates_.size() > 0, 2000); 871 WAIT(candidates_.size() > 0, 2000);
907 // TODO(deadbeef): Check candidate_allocation_done signal. 872 // TODO(deadbeef): Check candidate_allocation_done signal.
908 // In case of Relay, ports creation will succeed but sockets will fail. 873 // In case of Relay, ports creation will succeed but sockets will fail.
909 // There is no error reporting from RelayEntry to handle this failure. 874 // There is no error reporting from RelayEntry to handle this failure.
910 } 875 }
911 876
912 // Testing STUN timeout. 877 // Testing STUN timeout.
913 TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) { 878 TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
914 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr); 879 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
915 AddInterface(kClientAddr); 880 AddInterface(kClientAddr);
916 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 881 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
917 session_->StartGettingPorts(); 882 session_->StartGettingPorts();
918 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout); 883 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
919 EXPECT_EQ(2U, ports_.size()); 884 EXPECT_EQ(2U, ports_.size());
920 EXPECT_PRED5(CheckCandidate, candidates_[0], 885 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
921 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", 886 "local", "udp", kClientAddr);
922 kClientAddr); 887 EXPECT_PRED5(CheckCandidate, candidates_[1], ICE_CANDIDATE_COMPONENT_RTP,
923 EXPECT_PRED5(CheckCandidate, candidates_[1], 888 "local", "tcp", kClientAddr);
924 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
925 kClientAddr);
926 // RelayPort connection timeout is 3sec. TCP connection with RelayServer 889 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
927 // will be tried after 3 seconds. 890 // will be tried after 3 seconds.
928 EXPECT_EQ_WAIT(6U, candidates_.size(), 4000); 891 EXPECT_EQ_WAIT(6U, candidates_.size(), 4000);
929 EXPECT_EQ(3U, ports_.size()); 892 EXPECT_EQ(3U, ports_.size());
930 EXPECT_PRED5(CheckCandidate, candidates_[2], 893 EXPECT_PRED5(CheckCandidate, candidates_[2], ICE_CANDIDATE_COMPONENT_RTP,
931 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", 894 "relay", "udp", kRelayUdpIntAddr);
932 kRelayUdpIntAddr); 895 EXPECT_PRED5(CheckCandidate, candidates_[3], ICE_CANDIDATE_COMPONENT_RTP,
933 EXPECT_PRED5(CheckCandidate, candidates_[3], 896 "relay", "tcp", kRelayTcpIntAddr);
934 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", 897 EXPECT_PRED5(CheckCandidate, candidates_[4], ICE_CANDIDATE_COMPONENT_RTP,
935 kRelayTcpIntAddr); 898 "relay", "ssltcp", kRelaySslTcpIntAddr);
936 EXPECT_PRED5(CheckCandidate, candidates_[4], 899 EXPECT_PRED5(CheckCandidate, candidates_[5], ICE_CANDIDATE_COMPONENT_RTP,
937 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp", 900 "relay", "udp", kRelayUdpExtAddr);
938 kRelaySslTcpIntAddr);
939 EXPECT_PRED5(CheckCandidate, candidates_[5],
940 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
941 kRelayUdpExtAddr);
942 // Stun Timeout is 9sec. 901 // Stun Timeout is 9sec.
943 EXPECT_TRUE_WAIT(candidate_allocation_done_, 9000); 902 EXPECT_TRUE_WAIT(candidate_allocation_done_, 9000);
944 } 903 }
945 904
946 TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) { 905 TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
947 AddInterface(kClientAddr); 906 AddInterface(kClientAddr);
948 AddInterface(kClientAddr2); 907 AddInterface(kClientAddr2);
949 // Allocating only host UDP ports. This is done purely for testing 908 // Allocating only host UDP ports. This is done purely for testing
950 // convenience. 909 // convenience.
951 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_TCP | 910 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
952 cricket::PORTALLOCATOR_DISABLE_STUN | 911 PORTALLOCATOR_DISABLE_RELAY);
953 cricket::PORTALLOCATOR_DISABLE_RELAY); 912 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
954 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
955 session_->StartGettingPorts(); 913 session_->StartGettingPorts();
956 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); 914 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
957 ASSERT_EQ(2U, candidates_.size()); 915 ASSERT_EQ(2U, candidates_.size());
958 EXPECT_EQ(2U, ports_.size()); 916 EXPECT_EQ(2U, ports_.size());
959 // Candidates priorities should be different. 917 // Candidates priorities should be different.
960 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority()); 918 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
961 } 919 }
962 920
963 // Test to verify ICE restart process. 921 // Test to verify ICE restart process.
964 TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) { 922 TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
965 AddInterface(kClientAddr); 923 AddInterface(kClientAddr);
966 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 924 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
967 session_->StartGettingPorts(); 925 session_->StartGettingPorts();
968 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout); 926 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
969 EXPECT_EQ(4U, ports_.size()); 927 EXPECT_EQ(4U, ports_.size());
970 EXPECT_TRUE(candidate_allocation_done_); 928 EXPECT_TRUE(candidate_allocation_done_);
971 // TODO(deadbeef): Extend this to verify ICE restart. 929 // TODO(deadbeef): Extend this to verify ICE restart.
972 } 930 }
973 931
932 // Test that the allocator session uses the candidate filter it's created with,
933 // rather than the filter of its parent allocator.
934 // The filter of the allocator should only affect the next gathering phase,
935 // according to JSEP, which means the *next* allocator session returned.
936 TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
937 AddInterface(kClientAddr);
938 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
939 // Set candidate filter *after* creating the session. Should have no effect.
940 allocator().set_candidate_filter(CF_RELAY);
941 session_->StartGettingPorts();
942 // 7 candidates and 4 ports is what we would normally get (see the
943 // TestGetAllPorts* tests).
944 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
945 EXPECT_EQ(4U, ports_.size());
946 EXPECT_TRUE(candidate_allocation_done_);
947 }
948
974 // Test ICE candidate filter mechanism with options Relay/Host/Reflexive. 949 // Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
975 // This test also verifies that when the allocator is only allowed to use 950 // This test also verifies that when the allocator is only allowed to use
976 // relay (i.e. IceTransportsType is relay), the raddr is an empty 951 // relay (i.e. IceTransportsType is relay), the raddr is an empty
977 // address with the correct family. This is to prevent any local 952 // address with the correct family. This is to prevent any local
978 // reflective address leakage in the sdp line. 953 // reflective address leakage in the sdp line.
979 TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) { 954 TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
980 AddInterface(kClientAddr); 955 AddInterface(kClientAddr);
981 // GTURN is not configured here. 956 // GTURN is not configured here.
982 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress()); 957 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
983 allocator().set_candidate_filter(cricket::CF_RELAY); 958 allocator().set_candidate_filter(CF_RELAY);
984 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 959 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
985 session_->StartGettingPorts(); 960 session_->StartGettingPorts();
986 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); 961 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
987 EXPECT_PRED5(CheckCandidate, candidates_[0], 962 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
988 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", 963 "relay", "udp", rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
989 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
990 964
991 EXPECT_EQ(1U, candidates_.size()); 965 EXPECT_EQ(1U, candidates_.size());
992 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state. 966 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
993 for (size_t i = 0; i < candidates_.size(); ++i) { 967 for (size_t i = 0; i < candidates_.size(); ++i) {
994 EXPECT_EQ(std::string(cricket::RELAY_PORT_TYPE), candidates_[i].type()); 968 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[i].type());
995 EXPECT_EQ( 969 EXPECT_EQ(
996 candidates_[0].related_address(), 970 candidates_[0].related_address(),
997 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family())); 971 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
998 } 972 }
999 } 973 }
1000 974
1001 TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) { 975 TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
1002 AddInterface(kClientAddr); 976 AddInterface(kClientAddr);
1003 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET); 977 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1004 allocator().set_candidate_filter(cricket::CF_HOST); 978 allocator().set_candidate_filter(CF_HOST);
1005 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 979 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1006 session_->StartGettingPorts(); 980 session_->StartGettingPorts();
1007 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); 981 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1008 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only. 982 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1009 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only. 983 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
1010 for (size_t i = 0; i < candidates_.size(); ++i) { 984 for (size_t i = 0; i < candidates_.size(); ++i) {
1011 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type()); 985 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidates_[i].type());
1012 } 986 }
1013 } 987 }
1014 988
1015 // Host is behind the NAT. 989 // Host is behind the NAT.
1016 TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) { 990 TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
1017 AddInterface(kPrivateAddr); 991 AddInterface(kPrivateAddr);
1018 ResetWithStunServerAndNat(kStunAddr); 992 ResetWithStunServerAndNat(kStunAddr);
1019 993
1020 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET); 994 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1021 allocator().set_candidate_filter(cricket::CF_REFLEXIVE); 995 allocator().set_candidate_filter(CF_REFLEXIVE);
1022 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 996 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1023 session_->StartGettingPorts(); 997 session_->StartGettingPorts();
1024 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); 998 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1025 // Host is behind NAT, no private address will be exposed. Hence only UDP 999 // Host is behind NAT, no private address will be exposed. Hence only UDP
1026 // port with STUN candidate will be sent outside. 1000 // port with STUN candidate will be sent outside.
1027 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate. 1001 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1028 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state. 1002 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
1029 for (size_t i = 0; i < candidates_.size(); ++i) { 1003 for (size_t i = 0; i < candidates_.size(); ++i) {
1030 EXPECT_EQ(std::string(cricket::STUN_PORT_TYPE), candidates_[i].type()); 1004 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[i].type());
1031 EXPECT_EQ( 1005 EXPECT_EQ(
1032 candidates_[0].related_address(), 1006 candidates_[0].related_address(),
1033 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family())); 1007 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
1034 } 1008 }
1035 } 1009 }
1036 1010
1037 // Host is not behind the NAT. 1011 // Host is not behind the NAT.
1038 TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) { 1012 TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
1039 AddInterface(kClientAddr); 1013 AddInterface(kClientAddr);
1040 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET); 1014 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1041 allocator().set_candidate_filter(cricket::CF_REFLEXIVE); 1015 allocator().set_candidate_filter(CF_REFLEXIVE);
1042 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 1016 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1043 session_->StartGettingPorts(); 1017 session_->StartGettingPorts();
1044 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); 1018 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1045 // Host has a public address, both UDP and TCP candidates will be exposed. 1019 // Host has a public address, both UDP and TCP candidates will be exposed.
1046 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate. 1020 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
1047 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state. 1021 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
1048 for (size_t i = 0; i < candidates_.size(); ++i) { 1022 for (size_t i = 0; i < candidates_.size(); ++i) {
1049 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type()); 1023 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidates_[i].type());
1050 } 1024 }
1051 } 1025 }
1052 1026
1053 // Test that we get the same ufrag and pwd for all candidates. 1027 // Test that we get the same ufrag and pwd for all candidates.
1054 TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) { 1028 TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
1055 AddInterface(kClientAddr); 1029 AddInterface(kClientAddr);
1056 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 1030 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1057 session_->StartGettingPorts(); 1031 session_->StartGettingPorts();
1058 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout); 1032 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
1059 EXPECT_PRED5(CheckCandidate, candidates_[0], 1033 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
1060 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", 1034 "local", "udp", kClientAddr);
1061 kClientAddr); 1035 EXPECT_PRED5(CheckCandidate, candidates_[1], ICE_CANDIDATE_COMPONENT_RTP,
1062 EXPECT_PRED5(CheckCandidate, candidates_[1], 1036 "stun", "udp", kClientAddr);
1063 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", 1037 EXPECT_PRED5(CheckCandidate, candidates_[5], ICE_CANDIDATE_COMPONENT_RTP,
1064 kClientAddr); 1038 "local", "tcp", kClientAddr);
1065 EXPECT_PRED5(CheckCandidate, candidates_[5],
1066 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1067 kClientAddr);
1068 EXPECT_EQ(4U, ports_.size()); 1039 EXPECT_EQ(4U, ports_.size());
1069 EXPECT_EQ(kIceUfrag0, candidates_[0].username()); 1040 EXPECT_EQ(kIceUfrag0, candidates_[0].username());
1070 EXPECT_EQ(kIceUfrag0, candidates_[1].username()); 1041 EXPECT_EQ(kIceUfrag0, candidates_[1].username());
1071 EXPECT_EQ(kIceUfrag0, candidates_[2].username()); 1042 EXPECT_EQ(kIceUfrag0, candidates_[2].username());
1072 EXPECT_EQ(kIcePwd0, candidates_[0].password()); 1043 EXPECT_EQ(kIcePwd0, candidates_[0].password());
1073 EXPECT_EQ(kIcePwd0, candidates_[1].password()); 1044 EXPECT_EQ(kIcePwd0, candidates_[1].password());
1074 EXPECT_TRUE(candidate_allocation_done_); 1045 EXPECT_TRUE(candidate_allocation_done_);
1075 } 1046 }
1076 1047
1077 // Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port 1048 // Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1078 // is allocated for udp and stun. Also verify there is only one candidate 1049 // is allocated for udp and stun. Also verify there is only one candidate
1079 // (local) if stun candidate is same as local candidate, which will be the case 1050 // (local) if stun candidate is same as local candidate, which will be the case
1080 // in a public network like the below test. 1051 // in a public network like the below test.
1081 TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) { 1052 TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
1082 AddInterface(kClientAddr); 1053 AddInterface(kClientAddr);
1083 allocator_->set_flags(allocator().flags() | 1054 allocator_->set_flags(allocator().flags() |
1084 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET); 1055 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1085 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 1056 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1086 session_->StartGettingPorts(); 1057 session_->StartGettingPorts();
1087 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout); 1058 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
1088 EXPECT_EQ(3U, ports_.size()); 1059 EXPECT_EQ(3U, ports_.size());
1089 EXPECT_PRED5(CheckCandidate, candidates_[0], 1060 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
1090 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", 1061 "local", "udp", kClientAddr);
1091 kClientAddr);
1092 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); 1062 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1093 } 1063 }
1094 1064
1095 // Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port 1065 // Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1096 // is allocated for udp and stun. In this test we should expect both stun and 1066 // is allocated for udp and stun. In this test we should expect both stun and
1097 // local candidates as client behind a nat. 1067 // local candidates as client behind a nat.
1098 TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) { 1068 TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
1099 AddInterface(kClientAddr); 1069 AddInterface(kClientAddr);
1100 ResetWithStunServerAndNat(kStunAddr); 1070 ResetWithStunServerAndNat(kStunAddr);
1101 1071
1102 allocator_->set_flags(allocator().flags() | 1072 allocator_->set_flags(allocator().flags() |
1103 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET); 1073 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1104 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 1074 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1105 session_->StartGettingPorts(); 1075 session_->StartGettingPorts();
1106 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout); 1076 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1107 ASSERT_EQ(2U, ports_.size()); 1077 ASSERT_EQ(2U, ports_.size());
1108 EXPECT_PRED5(CheckCandidate, candidates_[0], 1078 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
1109 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", 1079 "local", "udp", kClientAddr);
1110 kClientAddr); 1080 EXPECT_PRED5(CheckCandidate, candidates_[1], ICE_CANDIDATE_COMPONENT_RTP,
1111 EXPECT_PRED5(CheckCandidate, candidates_[1], 1081 "stun", "udp", rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1112 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1113 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1114 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); 1082 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1115 EXPECT_EQ(3U, candidates_.size()); 1083 EXPECT_EQ(3U, candidates_.size());
1116 } 1084 }
1117 1085
1118 // Test TURN port in shared socket mode with UDP and TCP TURN server addresses. 1086 // Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
1119 TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) { 1087 TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
1120 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP); 1088 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
1121 AddInterface(kClientAddr); 1089 AddInterface(kClientAddr);
1122 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_)); 1090 allocator_.reset(new BasicPortAllocator(&network_manager_));
1123 1091
1124 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr); 1092 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1125 1093
1126 allocator_->set_step_delay(cricket::kMinimumStepDelay); 1094 allocator_->set_step_delay(kMinimumStepDelay);
1127 allocator_->set_flags(allocator().flags() | 1095 allocator_->set_flags(allocator().flags() |
1128 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | 1096 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1129 cricket::PORTALLOCATOR_DISABLE_TCP); 1097 PORTALLOCATOR_DISABLE_TCP);
1130 1098
1131 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 1099 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1132 session_->StartGettingPorts(); 1100 session_->StartGettingPorts();
1133 1101
1134 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout); 1102 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1135 ASSERT_EQ(3U, ports_.size()); 1103 ASSERT_EQ(3U, ports_.size());
1136 EXPECT_PRED5(CheckCandidate, candidates_[0], 1104 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
1137 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", 1105 "local", "udp", kClientAddr);
1138 kClientAddr); 1106 EXPECT_PRED5(CheckCandidate, candidates_[1], ICE_CANDIDATE_COMPONENT_RTP,
1139 EXPECT_PRED5(CheckCandidate, candidates_[1], 1107 "relay", "udp", rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1140 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", 1108 EXPECT_PRED5(CheckCandidate, candidates_[2], ICE_CANDIDATE_COMPONENT_RTP,
1141 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)); 1109 "relay", "udp", rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1142 EXPECT_PRED5(CheckCandidate, candidates_[2],
1143 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1144 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1145 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); 1110 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1146 EXPECT_EQ(3U, candidates_.size()); 1111 EXPECT_EQ(3U, candidates_.size());
1147 } 1112 }
1148 1113
1149 // Testing DNS resolve for the TURN server, this will test AllocationSequence 1114 // Testing DNS resolve for the TURN server, this will test AllocationSequence
1150 // handling the unresolved address signal from TurnPort. 1115 // handling the unresolved address signal from TurnPort.
1151 TEST_F(BasicPortAllocatorTest, TestSharedSocketWithServerAddressResolve) { 1116 TEST_F(BasicPortAllocatorTest, TestSharedSocketWithServerAddressResolve) {
1152 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478), 1117 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
1153 cricket::PROTO_UDP); 1118 PROTO_UDP);
1154 AddInterface(kClientAddr); 1119 AddInterface(kClientAddr);
1155 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_)); 1120 allocator_.reset(new BasicPortAllocator(&network_manager_));
1156 cricket::RelayServerConfig turn_server(cricket::RELAY_TURN); 1121 RelayServerConfig turn_server(RELAY_TURN);
1157 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword); 1122 RelayCredentials credentials(kTurnUsername, kTurnPassword);
1158 turn_server.credentials = credentials; 1123 turn_server.credentials = credentials;
1159 turn_server.ports.push_back(cricket::ProtocolAddress( 1124 turn_server.ports.push_back(
1160 rtc::SocketAddress("localhost", 3478), cricket::PROTO_UDP, false)); 1125 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP, false));
1161 allocator_->AddTurnServer(turn_server); 1126 allocator_->AddTurnServer(turn_server);
1162 1127
1163 allocator_->set_step_delay(cricket::kMinimumStepDelay); 1128 allocator_->set_step_delay(kMinimumStepDelay);
1164 allocator_->set_flags(allocator().flags() | 1129 allocator_->set_flags(allocator().flags() |
1165 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | 1130 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1166 cricket::PORTALLOCATOR_DISABLE_TCP); 1131 PORTALLOCATOR_DISABLE_TCP);
1167 1132
1168 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 1133 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1169 session_->StartGettingPorts(); 1134 session_->StartGettingPorts();
1170 1135
1171 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout); 1136 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1172 } 1137 }
1173 1138
1174 // Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port 1139 // Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1175 // is allocated for udp/stun/turn. In this test we should expect all local, 1140 // is allocated for udp/stun/turn. In this test we should expect all local,
1176 // stun and turn candidates. 1141 // stun and turn candidates.
1177 TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) { 1142 TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
1178 AddInterface(kClientAddr); 1143 AddInterface(kClientAddr);
1179 ResetWithStunServerAndNat(kStunAddr); 1144 ResetWithStunServerAndNat(kStunAddr);
1180 1145
1181 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress()); 1146 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1182 1147
1183 allocator_->set_flags(allocator().flags() | 1148 allocator_->set_flags(allocator().flags() |
1184 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | 1149 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1185 cricket::PORTALLOCATOR_DISABLE_TCP); 1150 PORTALLOCATOR_DISABLE_TCP);
1186 1151
1187 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 1152 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1188 session_->StartGettingPorts(); 1153 session_->StartGettingPorts();
1189 1154
1190 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout); 1155 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1191 ASSERT_EQ(2U, ports_.size()); 1156 ASSERT_EQ(2U, ports_.size());
1192 EXPECT_PRED5(CheckCandidate, candidates_[0], 1157 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
1193 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", 1158 "local", "udp", kClientAddr);
1194 kClientAddr); 1159 EXPECT_PRED5(CheckCandidate, candidates_[1], ICE_CANDIDATE_COMPONENT_RTP,
1195 EXPECT_PRED5(CheckCandidate, candidates_[1], 1160 "stun", "udp", rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1196 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", 1161 EXPECT_PRED5(CheckCandidate, candidates_[2], ICE_CANDIDATE_COMPONENT_RTP,
1197 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)); 1162 "relay", "udp", rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1198 EXPECT_PRED5(CheckCandidate, candidates_[2],
1199 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1200 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1201 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); 1163 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1202 EXPECT_EQ(3U, candidates_.size()); 1164 EXPECT_EQ(3U, candidates_.size());
1203 // Local port will be created first and then TURN port. 1165 // Local port will be created first and then TURN port.
1204 EXPECT_EQ(2U, ports_[0]->Candidates().size()); 1166 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1205 EXPECT_EQ(1U, ports_[1]->Candidates().size()); 1167 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1206 } 1168 }
1207 1169
1208 // Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN 1170 // Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1209 // server is also used as the STUN server, we should get 'local', 'stun', and 1171 // server is also used as the STUN server, we should get 'local', 'stun', and
1210 // 'relay' candidates. 1172 // 'relay' candidates.
1211 TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) { 1173 TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
1212 AddInterface(kClientAddr); 1174 AddInterface(kClientAddr);
1213 // Use an empty SocketAddress to add a NAT without STUN server. 1175 // Use an empty SocketAddress to add a NAT without STUN server.
1214 ResetWithStunServerAndNat(SocketAddress()); 1176 ResetWithStunServerAndNat(SocketAddress());
1215 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress()); 1177 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1216 1178
1217 // Must set the step delay to 0 to make sure the relay allocation phase is 1179 // Must set the step delay to 0 to make sure the relay allocation phase is
1218 // started before the STUN candidates are obtained, so that the STUN binding 1180 // started before the STUN candidates are obtained, so that the STUN binding
1219 // response is processed when both StunPort and TurnPort exist to reproduce 1181 // response is processed when both StunPort and TurnPort exist to reproduce
1220 // webrtc issue 3537. 1182 // webrtc issue 3537.
1221 allocator_->set_step_delay(0); 1183 allocator_->set_step_delay(0);
1222 allocator_->set_flags(allocator().flags() | 1184 allocator_->set_flags(allocator().flags() |
1223 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | 1185 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1224 cricket::PORTALLOCATOR_DISABLE_TCP); 1186 PORTALLOCATOR_DISABLE_TCP);
1225 1187
1226 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 1188 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1227 session_->StartGettingPorts(); 1189 session_->StartGettingPorts();
1228 1190
1229 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout); 1191 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1230 EXPECT_PRED5(CheckCandidate, candidates_[0], 1192 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
1231 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", 1193 "local", "udp", kClientAddr);
1232 kClientAddr); 1194 EXPECT_PRED5(CheckCandidate, candidates_[1], ICE_CANDIDATE_COMPONENT_RTP,
1233 EXPECT_PRED5(CheckCandidate, candidates_[1], 1195 "stun", "udp", rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1234 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", 1196 EXPECT_PRED5(CheckCandidate, candidates_[2], ICE_CANDIDATE_COMPONENT_RTP,
1235 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)); 1197 "relay", "udp", rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1236 EXPECT_PRED5(CheckCandidate, candidates_[2],
1237 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1238 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1239 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address()); 1198 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
1240 1199
1241 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); 1200 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1242 EXPECT_EQ(3U, candidates_.size()); 1201 EXPECT_EQ(3U, candidates_.size());
1243 // Local port will be created first and then TURN port. 1202 // Local port will be created first and then TURN port.
1244 EXPECT_EQ(2U, ports_[0]->Candidates().size()); 1203 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1245 EXPECT_EQ(1U, ports_[1]->Candidates().size()); 1204 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1246 } 1205 }
1247 1206
1248 // Test that when only a TCP TURN server is available, we do NOT use it as 1207 // Test that when only a TCP TURN server is available, we do NOT use it as
1249 // a UDP STUN server, as this could leak our IP address. Thus we should only 1208 // a UDP STUN server, as this could leak our IP address. Thus we should only
1250 // expect two ports, a UDPPort and TurnPort. 1209 // expect two ports, a UDPPort and TurnPort.
1251 TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) { 1210 TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
1252 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP); 1211 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
1253 AddInterface(kClientAddr); 1212 AddInterface(kClientAddr);
1254 ResetWithStunServerAndNat(rtc::SocketAddress()); 1213 ResetWithStunServerAndNat(rtc::SocketAddress());
1255 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr); 1214 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1256 1215
1257 allocator_->set_flags(allocator().flags() | 1216 allocator_->set_flags(allocator().flags() |
1258 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | 1217 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1259 cricket::PORTALLOCATOR_DISABLE_TCP); 1218 PORTALLOCATOR_DISABLE_TCP);
1260 1219
1261 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 1220 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1262 session_->StartGettingPorts(); 1221 session_->StartGettingPorts();
1263 1222
1264 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout); 1223 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
1265 ASSERT_EQ(2U, ports_.size()); 1224 ASSERT_EQ(2U, ports_.size());
1266 EXPECT_PRED5(CheckCandidate, candidates_[0], 1225 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
1267 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", 1226 "local", "udp", kClientAddr);
1268 kClientAddr); 1227 EXPECT_PRED5(CheckCandidate, candidates_[1], ICE_CANDIDATE_COMPONENT_RTP,
1269 EXPECT_PRED5(CheckCandidate, candidates_[1], 1228 "relay", "udp", rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1270 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1271 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1272 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); 1229 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1273 EXPECT_EQ(2U, candidates_.size()); 1230 EXPECT_EQ(2U, candidates_.size());
1274 EXPECT_EQ(1U, ports_[0]->Candidates().size()); 1231 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1275 EXPECT_EQ(1U, ports_[1]->Candidates().size()); 1232 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1276 } 1233 }
1277 1234
1278 // Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the 1235 // Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1279 // TURN server is used as the STUN server and we get 'local', 'stun', and 1236 // TURN server is used as the STUN server and we get 'local', 'stun', and
1280 // 'relay' candidates. 1237 // 'relay' candidates.
1281 // TODO(deadbeef): Remove this test when support for non-shared socket mode 1238 // TODO(deadbeef): Remove this test when support for non-shared socket mode
1282 // is removed. 1239 // is removed.
1283 TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) { 1240 TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
1284 AddInterface(kClientAddr); 1241 AddInterface(kClientAddr);
1285 // Use an empty SocketAddress to add a NAT without STUN server. 1242 // Use an empty SocketAddress to add a NAT without STUN server.
1286 ResetWithStunServerAndNat(SocketAddress()); 1243 ResetWithStunServerAndNat(SocketAddress());
1287 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress()); 1244 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1288 1245
1289 allocator_->set_flags(allocator().flags() | 1246 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
1290 cricket::PORTALLOCATOR_DISABLE_TCP);
1291 1247
1292 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 1248 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1293 session_->StartGettingPorts(); 1249 session_->StartGettingPorts();
1294 1250
1295 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout); 1251 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1296 ASSERT_EQ(3U, ports_.size()); 1252 ASSERT_EQ(3U, ports_.size());
1297 EXPECT_PRED5(CheckCandidate, candidates_[0], 1253 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
1298 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", 1254 "local", "udp", kClientAddr);
1299 kClientAddr); 1255 EXPECT_PRED5(CheckCandidate, candidates_[1], ICE_CANDIDATE_COMPONENT_RTP,
1300 EXPECT_PRED5(CheckCandidate, candidates_[1], 1256 "stun", "udp", rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1301 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", 1257 EXPECT_PRED5(CheckCandidate, candidates_[2], ICE_CANDIDATE_COMPONENT_RTP,
1302 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)); 1258 "relay", "udp", rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1303 EXPECT_PRED5(CheckCandidate, candidates_[2],
1304 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1305 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1306 // Not using shared socket, so the STUN request's server reflexive address 1259 // Not using shared socket, so the STUN request's server reflexive address
1307 // should be different than the TURN request's server reflexive address. 1260 // should be different than the TURN request's server reflexive address.
1308 EXPECT_NE(candidates_[2].related_address(), candidates_[1].address()); 1261 EXPECT_NE(candidates_[2].related_address(), candidates_[1].address());
1309 1262
1310 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); 1263 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1311 EXPECT_EQ(3U, candidates_.size()); 1264 EXPECT_EQ(3U, candidates_.size());
1312 EXPECT_EQ(1U, ports_[0]->Candidates().size()); 1265 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1313 EXPECT_EQ(1U, ports_[1]->Candidates().size()); 1266 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1314 EXPECT_EQ(1U, ports_[2]->Candidates().size()); 1267 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1315 } 1268 }
1316 1269
1317 // Test that even when both a STUN and TURN server are configured, the TURN 1270 // Test that even when both a STUN and TURN server are configured, the TURN
1318 // server is used as a STUN server and we get a 'stun' candidate. 1271 // server is used as a STUN server and we get a 'stun' candidate.
1319 TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) { 1272 TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
1320 AddInterface(kClientAddr); 1273 AddInterface(kClientAddr);
1321 // Configure with STUN server but destroy it, so we can ensure that it's 1274 // Configure with STUN server but destroy it, so we can ensure that it's
1322 // the TURN server actually being used as a STUN server. 1275 // the TURN server actually being used as a STUN server.
1323 ResetWithStunServerAndNat(kStunAddr); 1276 ResetWithStunServerAndNat(kStunAddr);
1324 stun_server_.reset(); 1277 stun_server_.reset();
1325 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress()); 1278 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1326 1279
1327 allocator_->set_flags(allocator().flags() | 1280 allocator_->set_flags(allocator().flags() |
1328 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | 1281 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1329 cricket::PORTALLOCATOR_DISABLE_TCP); 1282 PORTALLOCATOR_DISABLE_TCP);
1330 1283
1331 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 1284 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1332 session_->StartGettingPorts(); 1285 session_->StartGettingPorts();
1333 1286
1334 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout); 1287 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1335 EXPECT_PRED5(CheckCandidate, candidates_[0], 1288 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
1336 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", 1289 "local", "udp", kClientAddr);
1337 kClientAddr); 1290 EXPECT_PRED5(CheckCandidate, candidates_[1], ICE_CANDIDATE_COMPONENT_RTP,
1338 EXPECT_PRED5(CheckCandidate, candidates_[1], 1291 "stun", "udp", rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1339 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", 1292 EXPECT_PRED5(CheckCandidate, candidates_[2], ICE_CANDIDATE_COMPONENT_RTP,
1340 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)); 1293 "relay", "udp", rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1341 EXPECT_PRED5(CheckCandidate, candidates_[2],
1342 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1343 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1344 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address()); 1294 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
1345 1295
1346 // Don't bother waiting for STUN timeout, since we already verified 1296 // Don't bother waiting for STUN timeout, since we already verified
1347 // that we got a STUN candidate from the TURN server. 1297 // that we got a STUN candidate from the TURN server.
1348 } 1298 }
1349 1299
1350 // This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled 1300 // This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1351 // and fail to generate STUN candidate, local UDP candidate is generated 1301 // and fail to generate STUN candidate, local UDP candidate is generated
1352 // properly. 1302 // properly.
1353 TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) { 1303 TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
1354 allocator().set_flags(allocator().flags() | 1304 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1355 cricket::PORTALLOCATOR_DISABLE_RELAY | 1305 PORTALLOCATOR_DISABLE_TCP |
1356 cricket::PORTALLOCATOR_DISABLE_TCP | 1306 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1357 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1358 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr); 1307 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1359 AddInterface(kClientAddr); 1308 AddInterface(kClientAddr);
1360 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 1309 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1361 session_->StartGettingPorts(); 1310 session_->StartGettingPorts();
1362 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout); 1311 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1363 EXPECT_EQ(1U, candidates_.size()); 1312 EXPECT_EQ(1U, candidates_.size());
1364 EXPECT_PRED5(CheckCandidate, candidates_[0], 1313 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
1365 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", 1314 "local", "udp", kClientAddr);
1366 kClientAddr);
1367 // STUN timeout is 9.5sec. We need to wait to get candidate done signal. 1315 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
1368 EXPECT_TRUE_WAIT(candidate_allocation_done_, kStunTimeoutMs); 1316 EXPECT_TRUE_WAIT(candidate_allocation_done_, kStunTimeoutMs);
1369 EXPECT_EQ(1U, candidates_.size()); 1317 EXPECT_EQ(1U, candidates_.size());
1370 } 1318 }
1371 1319
1372 // Test that when the NetworkManager doesn't have permission to enumerate 1320 // Test that when the NetworkManager doesn't have permission to enumerate
1373 // adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified 1321 // adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1374 // automatically. 1322 // automatically.
1375 TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) { 1323 TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
1376 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(), 1324 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
1377 rtc::IPAddress()); 1325 rtc::IPAddress());
1378 network_manager_.set_enumeration_permission( 1326 network_manager_.set_enumeration_permission(
1379 rtc::NetworkManager::ENUMERATION_BLOCKED); 1327 rtc::NetworkManager::ENUMERATION_BLOCKED);
1380 allocator().set_flags(allocator().flags() | 1328 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1381 cricket::PORTALLOCATOR_DISABLE_RELAY | 1329 PORTALLOCATOR_DISABLE_TCP |
1382 cricket::PORTALLOCATOR_DISABLE_TCP | 1330 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1383 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET); 1331 EXPECT_EQ(0U,
1384 EXPECT_EQ(0U, allocator_->flags() & 1332 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1385 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION); 1333 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1386 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 1334 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1387 EXPECT_EQ(0U, session_->flags() &
1388 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1389 session_->StartGettingPorts(); 1335 session_->StartGettingPorts();
1390 EXPECT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout); 1336 EXPECT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1391 EXPECT_EQ(1U, candidates_.size()); 1337 EXPECT_EQ(1U, candidates_.size());
1392 EXPECT_PRED5(CheckCandidate, candidates_[0], 1338 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
1393 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", 1339 "local", "udp", kPrivateAddr);
1394 kPrivateAddr); 1340 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1395 EXPECT_NE(0U, session_->flags() &
1396 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1397 } 1341 }
1398 1342
1399 // This test verifies allocator can use IPv6 addresses along with IPv4. 1343 // This test verifies allocator can use IPv6 addresses along with IPv4.
1400 TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) { 1344 TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
1401 allocator().set_flags(allocator().flags() | 1345 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1402 cricket::PORTALLOCATOR_DISABLE_RELAY | 1346 PORTALLOCATOR_ENABLE_IPV6 |
1403 cricket::PORTALLOCATOR_ENABLE_IPV6 | 1347 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1404 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1405 AddInterface(kClientIPv6Addr); 1348 AddInterface(kClientIPv6Addr);
1406 AddInterface(kClientAddr); 1349 AddInterface(kClientAddr);
1407 allocator_->set_step_delay(cricket::kMinimumStepDelay); 1350 allocator_->set_step_delay(kMinimumStepDelay);
1408 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 1351 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1409 session_->StartGettingPorts(); 1352 session_->StartGettingPorts();
1410 ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout); 1353 ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
1411 EXPECT_EQ(4U, candidates_.size()); 1354 EXPECT_EQ(4U, candidates_.size());
1412 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); 1355 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1413 EXPECT_PRED5(CheckCandidate, candidates_[0], 1356 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
1414 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", 1357 "local", "udp", kClientIPv6Addr);
1415 kClientIPv6Addr); 1358 EXPECT_PRED5(CheckCandidate, candidates_[1], ICE_CANDIDATE_COMPONENT_RTP,
1416 EXPECT_PRED5(CheckCandidate, candidates_[1], 1359 "local", "udp", kClientAddr);
1417 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", 1360 EXPECT_PRED5(CheckCandidate, candidates_[2], ICE_CANDIDATE_COMPONENT_RTP,
1418 kClientAddr); 1361 "local", "tcp", kClientIPv6Addr);
1419 EXPECT_PRED5(CheckCandidate, candidates_[2], 1362 EXPECT_PRED5(CheckCandidate, candidates_[3], ICE_CANDIDATE_COMPONENT_RTP,
1420 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", 1363 "local", "tcp", kClientAddr);
1421 kClientIPv6Addr);
1422 EXPECT_PRED5(CheckCandidate, candidates_[3],
1423 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1424 kClientAddr);
1425 EXPECT_EQ(4U, candidates_.size()); 1364 EXPECT_EQ(4U, candidates_.size());
1426 } 1365 }
1427 1366
1428 TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) { 1367 TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
1429 AddInterface(kClientAddr); 1368 AddInterface(kClientAddr);
1430 allocator_->set_step_delay(cricket::kDefaultStepDelay); 1369 allocator_->set_step_delay(kDefaultStepDelay);
1431 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 1370 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1432 session_->StartGettingPorts(); 1371 session_->StartGettingPorts();
1433 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000); 1372 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1434 EXPECT_EQ(2U, ports_.size()); 1373 EXPECT_EQ(2U, ports_.size());
1435 session_->StopGettingPorts(); 1374 session_->StopGettingPorts();
1436 EXPECT_TRUE_WAIT(candidate_allocation_done_, 1000); 1375 EXPECT_TRUE_WAIT(candidate_allocation_done_, 1000);
1437 1376
1438 // After stopping getting ports, adding a new interface will not start 1377 // After stopping getting ports, adding a new interface will not start
1439 // getting ports again. 1378 // getting ports again.
1440 candidates_.clear(); 1379 candidates_.clear();
1441 ports_.clear(); 1380 ports_.clear();
1442 candidate_allocation_done_ = false; 1381 candidate_allocation_done_ = false;
1443 network_manager_.AddInterface(kClientAddr2); 1382 network_manager_.AddInterface(kClientAddr2);
1444 rtc::Thread::Current()->ProcessMessages(1000); 1383 rtc::Thread::Current()->ProcessMessages(1000);
1445 EXPECT_EQ(0U, candidates_.size()); 1384 EXPECT_EQ(0U, candidates_.size());
1446 EXPECT_EQ(0U, ports_.size()); 1385 EXPECT_EQ(0U, ports_.size());
1447 } 1386 }
1448 1387
1449 TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) { 1388 TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
1450 AddInterface(kClientAddr); 1389 AddInterface(kClientAddr);
1451 allocator_->set_step_delay(cricket::kDefaultStepDelay); 1390 allocator_->set_step_delay(kDefaultStepDelay);
1452 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); 1391 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1453 session_->StartGettingPorts(); 1392 session_->StartGettingPorts();
1454 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000); 1393 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1455 EXPECT_EQ(2U, ports_.size()); 1394 EXPECT_EQ(2U, ports_.size());
1456 session_->ClearGettingPorts(); 1395 session_->ClearGettingPorts();
1457 WAIT(candidate_allocation_done_, 1000); 1396 WAIT(candidate_allocation_done_, 1000);
1458 EXPECT_FALSE(candidate_allocation_done_); 1397 EXPECT_FALSE(candidate_allocation_done_);
1459 1398
1460 // After clearing getting ports, adding a new interface will start getting 1399 // After clearing getting ports, adding a new interface will start getting
1461 // ports again. 1400 // ports again.
1462 candidates_.clear(); 1401 candidates_.clear();
1463 ports_.clear(); 1402 ports_.clear();
1464 candidate_allocation_done_ = false; 1403 candidate_allocation_done_ = false;
1465 network_manager_.AddInterface(kClientAddr2); 1404 network_manager_.AddInterface(kClientAddr2);
1466 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000); 1405 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1467 EXPECT_EQ(2U, ports_.size()); 1406 EXPECT_EQ(2U, ports_.size());
1468 } 1407 }
1469 1408
1470 // Test that the ports and candidates are updated with new ufrag/pwd/etc. when 1409 // Test that the ports and candidates are updated with new ufrag/pwd/etc. when
1471 // a pooled session is taken out of the pool. 1410 // a pooled session is taken out of the pool.
1472 TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) { 1411 TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
1473 AddInterface(kClientAddr); 1412 AddInterface(kClientAddr);
1474 int pool_size = 1; 1413 int pool_size = 1;
1475 allocator_->SetConfiguration(allocator_->stun_servers(), 1414 allocator_->SetConfiguration(allocator_->stun_servers(),
1476 allocator_->turn_servers(), pool_size); 1415 allocator_->turn_servers(), pool_size);
1477 const cricket::PortAllocatorSession* peeked_session = 1416 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
1478 allocator_->GetPooledSession();
1479 ASSERT_NE(nullptr, peeked_session); 1417 ASSERT_NE(nullptr, peeked_session);
1480 EXPECT_EQ_WAIT(true, peeked_session->CandidatesAllocationDone(), 1418 EXPECT_EQ_WAIT(true, peeked_session->CandidatesAllocationDone(),
1481 kDefaultAllocationTimeout); 1419 kDefaultAllocationTimeout);
1482 // Expect that when TakePooledSession is called, 1420 // Expect that when TakePooledSession is called,
1483 // UpdateTransportInformationInternal will be called and the 1421 // UpdateTransportInformationInternal will be called and the
1484 // BasicPortAllocatorSession will update the ufrag/pwd of ports and 1422 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
1485 // candidates. 1423 // candidates.
1486 session_ = 1424 session_ =
1487 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0); 1425 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1488 ASSERT_NE(nullptr, session_.get()); 1426 ASSERT_NE(nullptr, session_.get());
1489 auto ready_ports = session_->ReadyPorts(); 1427 auto ready_ports = session_->ReadyPorts();
1490 auto candidates = session_->ReadyCandidates(); 1428 auto candidates = session_->ReadyCandidates();
1491 EXPECT_FALSE(ready_ports.empty()); 1429 EXPECT_FALSE(ready_ports.empty());
1492 EXPECT_FALSE(candidates.empty()); 1430 EXPECT_FALSE(candidates.empty());
1493 for (const cricket::PortInterface* port_interface : ready_ports) { 1431 for (const PortInterface* port_interface : ready_ports) {
1494 const cricket::Port* port = 1432 const Port* port = static_cast<const Port*>(port_interface);
1495 static_cast<const cricket::Port*>(port_interface);
1496 EXPECT_EQ(kContentName, port->content_name()); 1433 EXPECT_EQ(kContentName, port->content_name());
1497 EXPECT_EQ(1, port->component()); 1434 EXPECT_EQ(1, port->component());
1498 EXPECT_EQ(kIceUfrag0, port->username_fragment()); 1435 EXPECT_EQ(kIceUfrag0, port->username_fragment());
1499 EXPECT_EQ(kIcePwd0, port->password()); 1436 EXPECT_EQ(kIcePwd0, port->password());
1500 } 1437 }
1501 for (const cricket::Candidate& candidate : candidates) { 1438 for (const Candidate& candidate : candidates) {
1502 EXPECT_EQ(1, candidate.component()); 1439 EXPECT_EQ(1, candidate.component());
1503 EXPECT_EQ(kIceUfrag0, candidate.username()); 1440 EXPECT_EQ(kIceUfrag0, candidate.username());
1504 EXPECT_EQ(kIcePwd0, candidate.password()); 1441 EXPECT_EQ(kIcePwd0, candidate.password());
1505 } 1442 }
1506 } 1443 }
1444
1445 // Test that a new candidate filter takes effect even on already-gathered
1446 // candidates.
1447 TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
1448 AddInterface(kClientAddr);
1449 int pool_size = 1;
1450 allocator_->SetConfiguration(allocator_->stun_servers(),
1451 allocator_->turn_servers(), pool_size);
1452 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
1453 ASSERT_NE(nullptr, peeked_session);
1454 EXPECT_EQ_WAIT(true, peeked_session->CandidatesAllocationDone(),
1455 kDefaultAllocationTimeout);
1456 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
1457 size_t initial_ports_size = peeked_session->ReadyPorts().size();
1458 allocator_->set_candidate_filter(CF_RELAY);
1459 // Assume that when TakePooledSession is called, the candidate filter will be
1460 // applied to the pooled session. This is tested by PortAllocatorTest.
1461 session_ =
1462 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1463 ASSERT_NE(nullptr, session_.get());
1464 auto candidates = session_->ReadyCandidates();
1465 auto ports = session_->ReadyPorts();
1466 // Sanity check that the number of candidates and ports decreased.
1467 EXPECT_GT(initial_candidates_size, candidates.size());
1468 EXPECT_GT(initial_ports_size, ports.size());
1469 for (const PortInterface* port : ports) {
1470 // Expect only relay ports.
1471 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
1472 }
1473 for (const Candidate& candidate : candidates) {
1474 // Expect only relay candidates now that the filter is applied.
1475 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
1476 // Expect that the raddr is emptied due to the CF_RELAY filter.
1477 EXPECT_EQ(candidate.related_address(),
1478 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
1479 }
1480 }
1481
1482 } // namespace cricket
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