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