blob: 18a053c51e830ac9dd6d76b3092c61ed62c8565c [file] [log] [blame]
Steve Antonf1c6db12017-10-13 11:13:35 -07001/*
2 * Copyright 2017 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
Mirko Bonadei317a1f02019-09-17 17:06:18 +020011#include <memory>
12
Steve Anton10542f22019-01-11 09:11:00 -080013#include "p2p/base/fake_port_allocator.h"
14#include "p2p/base/test_stun_server.h"
15#include "p2p/client/basic_port_allocator.h"
16#include "pc/media_session.h"
17#include "pc/peer_connection.h"
18#include "pc/peer_connection_wrapper.h"
19#include "pc/sdp_utils.h"
Steve Antonf1c6db12017-10-13 11:13:35 -070020#ifdef WEBRTC_ANDROID
Steve Anton10542f22019-01-11 09:11:00 -080021#include "pc/test/android_test_initializer.h"
Steve Antonf1c6db12017-10-13 11:13:35 -070022#endif
Karl Wiberg1b0eae32017-10-17 14:48:54 +020023#include "api/audio_codecs/builtin_audio_decoder_factory.h"
24#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Mirko Bonadei2ff3f492018-11-22 09:00:13 +010025#include "api/create_peerconnection_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "api/peer_connection_proxy.h"
27#include "api/uma_metrics.h"
Anders Carlsson67537952018-05-03 11:28:29 +020028#include "api/video_codecs/builtin_video_decoder_factory.h"
29#include "api/video_codecs/builtin_video_encoder_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080030#include "pc/test/fake_audio_capture_module.h"
Henrik Boströmee6f4f62019-11-06 12:36:12 +010031#include "pc/test/mock_peer_connection_observers.h"
Steve Anton10542f22019-01-11 09:11:00 -080032#include "rtc_base/fake_network.h"
Steve Antonf1c6db12017-10-13 11:13:35 -070033#include "rtc_base/gunit.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020034#include "rtc_base/strings/string_builder.h"
Steve Anton10542f22019-01-11 09:11:00 -080035#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020036#include "system_wrappers/include/metrics.h"
Steve Antonb443dfe2019-03-05 14:09:49 -080037#include "test/gmock.h"
Steve Antonf1c6db12017-10-13 11:13:35 -070038
39namespace webrtc {
40
41using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
42using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
43using rtc::SocketAddress;
Steve Anton46d926a2018-01-23 10:23:06 -080044using ::testing::Combine;
Steve Antonb443dfe2019-03-05 14:09:49 -080045using ::testing::ElementsAre;
46using ::testing::Pair;
Steve Antonf1c6db12017-10-13 11:13:35 -070047using ::testing::Values;
48
49constexpr int kIceCandidatesTimeout = 10000;
Henrik Boströmee6f4f62019-11-06 12:36:12 +010050constexpr int64_t kWaitTimeout = 10000;
Steve Antonf1c6db12017-10-13 11:13:35 -070051
Steve Anton46d926a2018-01-23 10:23:06 -080052class PeerConnectionWrapperForIceTest : public PeerConnectionWrapper {
Steve Antonf1c6db12017-10-13 11:13:35 -070053 public:
54 using PeerConnectionWrapper::PeerConnectionWrapper;
55
Henrik Boströmee6f4f62019-11-06 12:36:12 +010056 std::unique_ptr<IceCandidateInterface> CreateJsepCandidateForFirstTransport(
57 cricket::Candidate* candidate) {
Steve Antonf1c6db12017-10-13 11:13:35 -070058 RTC_DCHECK(pc()->remote_description());
59 const auto* desc = pc()->remote_description()->description();
60 RTC_DCHECK(desc->contents().size() > 0);
61 const auto& first_content = desc->contents()[0];
62 candidate->set_transport_name(first_content.name);
Henrik Boströmee6f4f62019-11-06 12:36:12 +010063 return CreateIceCandidate(first_content.name, -1, *candidate);
64 }
65
66 // Adds a new ICE candidate to the first transport.
67 bool AddIceCandidate(cricket::Candidate* candidate) {
68 return pc()->AddIceCandidate(
69 CreateJsepCandidateForFirstTransport(candidate).get());
Steve Antonf1c6db12017-10-13 11:13:35 -070070 }
71
72 // Returns ICE candidates from the remote session description.
73 std::vector<const IceCandidateInterface*>
74 GetIceCandidatesFromRemoteDescription() {
75 const SessionDescriptionInterface* sdesc = pc()->remote_description();
76 RTC_DCHECK(sdesc);
77 std::vector<const IceCandidateInterface*> candidates;
78 for (size_t mline_index = 0; mline_index < sdesc->number_of_mediasections();
79 mline_index++) {
80 const auto* candidate_collection = sdesc->candidates(mline_index);
81 for (size_t i = 0; i < candidate_collection->count(); i++) {
82 candidates.push_back(candidate_collection->at(i));
83 }
84 }
85 return candidates;
86 }
87
88 rtc::FakeNetworkManager* network() { return network_; }
89
90 void set_network(rtc::FakeNetworkManager* network) { network_ = network; }
91
Jonas Oreland1cd39fa2018-10-11 07:47:12 +020092 // The port allocator used by this PC.
93 cricket::PortAllocator* port_allocator_;
94
Steve Antonf1c6db12017-10-13 11:13:35 -070095 private:
96 rtc::FakeNetworkManager* network_;
97};
98
Steve Anton46d926a2018-01-23 10:23:06 -080099class PeerConnectionIceBaseTest : public ::testing::Test {
Steve Antonf1c6db12017-10-13 11:13:35 -0700100 protected:
Steve Anton46d926a2018-01-23 10:23:06 -0800101 typedef std::unique_ptr<PeerConnectionWrapperForIceTest> WrapperPtr;
Steve Antonf1c6db12017-10-13 11:13:35 -0700102
Steve Anton46d926a2018-01-23 10:23:06 -0800103 explicit PeerConnectionIceBaseTest(SdpSemantics sdp_semantics)
104 : vss_(new rtc::VirtualSocketServer()),
105 main_(vss_.get()),
106 sdp_semantics_(sdp_semantics) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700107#ifdef WEBRTC_ANDROID
108 InitializeAndroidObjects();
109#endif
110 pc_factory_ = CreatePeerConnectionFactory(
111 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Anders Carlsson67537952018-05-03 11:28:29 +0200112 rtc::scoped_refptr<AudioDeviceModule>(FakeAudioCaptureModule::Create()),
113 CreateBuiltinAudioEncoderFactory(), CreateBuiltinAudioDecoderFactory(),
114 CreateBuiltinVideoEncoderFactory(), CreateBuiltinVideoDecoderFactory(),
115 nullptr /* audio_mixer */, nullptr /* audio_processing */);
Steve Antonf1c6db12017-10-13 11:13:35 -0700116 }
117
118 WrapperPtr CreatePeerConnection() {
119 return CreatePeerConnection(RTCConfiguration());
120 }
121
122 WrapperPtr CreatePeerConnection(const RTCConfiguration& config) {
123 auto* fake_network = NewFakeNetwork();
124 auto port_allocator =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200125 std::make_unique<cricket::BasicPortAllocator>(fake_network);
Steve Antonf1c6db12017-10-13 11:13:35 -0700126 port_allocator->set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
127 cricket::PORTALLOCATOR_DISABLE_RELAY);
128 port_allocator->set_step_delay(cricket::kMinimumStepDelay);
Steve Anton46d926a2018-01-23 10:23:06 -0800129 RTCConfiguration modified_config = config;
130 modified_config.sdp_semantics = sdp_semantics_;
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200131 auto observer = std::make_unique<MockPeerConnectionObserver>();
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200132 auto port_allocator_copy = port_allocator.get();
Steve Antonf1c6db12017-10-13 11:13:35 -0700133 auto pc = pc_factory_->CreatePeerConnection(
Steve Anton46d926a2018-01-23 10:23:06 -0800134 modified_config, std::move(port_allocator), nullptr, observer.get());
Steve Antonf1c6db12017-10-13 11:13:35 -0700135 if (!pc) {
136 return nullptr;
137 }
138
Yves Gerey4e933292018-10-31 15:36:05 +0100139 observer->SetPeerConnectionInterface(pc.get());
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200140 auto wrapper = std::make_unique<PeerConnectionWrapperForIceTest>(
Steve Antonf1c6db12017-10-13 11:13:35 -0700141 pc_factory_, pc, std::move(observer));
142 wrapper->set_network(fake_network);
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200143 wrapper->port_allocator_ = port_allocator_copy;
Steve Antonf1c6db12017-10-13 11:13:35 -0700144 return wrapper;
145 }
146
147 // Accepts the same arguments as CreatePeerConnection and adds default audio
148 // and video tracks.
149 template <typename... Args>
150 WrapperPtr CreatePeerConnectionWithAudioVideo(Args&&... args) {
151 auto wrapper = CreatePeerConnection(std::forward<Args>(args)...);
152 if (!wrapper) {
153 return nullptr;
154 }
Steve Anton8d3444d2017-10-20 15:30:51 -0700155 wrapper->AddAudioTrack("a");
156 wrapper->AddVideoTrack("v");
Steve Antonf1c6db12017-10-13 11:13:35 -0700157 return wrapper;
158 }
159
160 cricket::Candidate CreateLocalUdpCandidate(
161 const rtc::SocketAddress& address) {
162 cricket::Candidate candidate;
163 candidate.set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT);
164 candidate.set_protocol(cricket::UDP_PROTOCOL_NAME);
165 candidate.set_address(address);
166 candidate.set_type(cricket::LOCAL_PORT_TYPE);
167 return candidate;
168 }
169
170 // Remove all ICE ufrag/pwd lines from the given session description.
171 void RemoveIceUfragPwd(SessionDescriptionInterface* sdesc) {
172 SetIceUfragPwd(sdesc, "", "");
173 }
174
175 // Sets all ICE ufrag/pwds on the given session description.
176 void SetIceUfragPwd(SessionDescriptionInterface* sdesc,
177 const std::string& ufrag,
178 const std::string& pwd) {
179 auto* desc = sdesc->description();
180 for (const auto& content : desc->contents()) {
181 auto* transport_info = desc->GetTransportInfoByName(content.name);
182 transport_info->description.ice_ufrag = ufrag;
183 transport_info->description.ice_pwd = pwd;
184 }
185 }
186
Qingsi Wange1692722017-11-29 13:27:20 -0800187 // Set ICE mode on the given session description.
188 void SetIceMode(SessionDescriptionInterface* sdesc,
189 const cricket::IceMode ice_mode) {
190 auto* desc = sdesc->description();
191 for (const auto& content : desc->contents()) {
192 auto* transport_info = desc->GetTransportInfoByName(content.name);
193 transport_info->description.ice_mode = ice_mode;
194 }
195 }
196
Steve Antonf1c6db12017-10-13 11:13:35 -0700197 cricket::TransportDescription* GetFirstTransportDescription(
198 SessionDescriptionInterface* sdesc) {
199 auto* desc = sdesc->description();
200 RTC_DCHECK(desc->contents().size() > 0);
201 auto* transport_info =
202 desc->GetTransportInfoByName(desc->contents()[0].name);
203 RTC_DCHECK(transport_info);
204 return &transport_info->description;
205 }
206
207 const cricket::TransportDescription* GetFirstTransportDescription(
208 const SessionDescriptionInterface* sdesc) {
209 auto* desc = sdesc->description();
210 RTC_DCHECK(desc->contents().size() > 0);
211 auto* transport_info =
212 desc->GetTransportInfoByName(desc->contents()[0].name);
213 RTC_DCHECK(transport_info);
214 return &transport_info->description;
215 }
216
Qingsi Wange1692722017-11-29 13:27:20 -0800217 // TODO(qingsi): Rewrite this method in terms of the standard IceTransport
218 // after it is implemented.
219 cricket::IceRole GetIceRole(const WrapperPtr& pc_wrapper_ptr) {
Mirko Bonadeie97de912017-12-13 11:29:34 +0100220 auto* pc_proxy =
221 static_cast<PeerConnectionProxyWithInternal<PeerConnectionInterface>*>(
222 pc_wrapper_ptr->pc());
223 PeerConnection* pc = static_cast<PeerConnection*>(pc_proxy->internal());
Mirko Bonadei739baf02019-01-27 17:29:42 +0100224 for (const auto& transceiver : pc->GetTransceiversInternal()) {
Steve Anton69470252018-02-09 11:43:08 -0800225 if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
Bjorn A Mellem3a1b9272019-05-24 16:13:08 -0700226 auto dtls_transport = pc->LookupDtlsTransportByMidInternal(
227 transceiver->internal()->channel()->content_name());
228 return dtls_transport->ice_transport()->internal()->GetIceRole();
Steve Anton46d926a2018-01-23 10:23:06 -0800229 }
230 }
231 RTC_NOTREACHED();
232 return cricket::ICEROLE_UNKNOWN;
Qingsi Wange1692722017-11-29 13:27:20 -0800233 }
234
Henrik Boström79b69802019-07-18 11:16:56 +0200235 // Returns a list of (ufrag, pwd) pairs in the order that they appear in
236 // |description|, or the empty list if |description| is null.
237 std::vector<std::pair<std::string, std::string>> GetIceCredentials(
238 const SessionDescriptionInterface* description) {
239 std::vector<std::pair<std::string, std::string>> ice_credentials;
240 if (!description)
241 return ice_credentials;
242 const auto* desc = description->description();
243 for (const auto& content_info : desc->contents()) {
244 const auto* transport_info =
245 desc->GetTransportInfoByName(content_info.name);
246 if (transport_info) {
247 ice_credentials.push_back(
248 std::make_pair(transport_info->description.ice_ufrag,
249 transport_info->description.ice_pwd));
250 }
251 }
252 return ice_credentials;
253 }
254
Steve Antonf1c6db12017-10-13 11:13:35 -0700255 bool AddCandidateToFirstTransport(cricket::Candidate* candidate,
256 SessionDescriptionInterface* sdesc) {
257 auto* desc = sdesc->description();
258 RTC_DCHECK(desc->contents().size() > 0);
259 const auto& first_content = desc->contents()[0];
260 candidate->set_transport_name(first_content.name);
Steve Anton27ab0e52018-07-23 15:11:53 -0700261 std::unique_ptr<IceCandidateInterface> jsep_candidate =
262 CreateIceCandidate(first_content.name, 0, *candidate);
263 return sdesc->AddCandidate(jsep_candidate.get());
Steve Antonf1c6db12017-10-13 11:13:35 -0700264 }
265
266 rtc::FakeNetworkManager* NewFakeNetwork() {
267 // The PeerConnection's port allocator is tied to the PeerConnection's
268 // lifetime and expects the underlying NetworkManager to outlive it. That
269 // prevents us from having the PeerConnectionWrapper own the fake network.
270 // Therefore, the test fixture will own all the fake networks even though
271 // tests should access the fake network through the PeerConnectionWrapper.
272 auto* fake_network = new rtc::FakeNetworkManager();
273 fake_networks_.emplace_back(fake_network);
274 return fake_network;
275 }
276
277 std::unique_ptr<rtc::VirtualSocketServer> vss_;
278 rtc::AutoSocketServerThread main_;
279 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_;
280 std::vector<std::unique_ptr<rtc::FakeNetworkManager>> fake_networks_;
Steve Anton46d926a2018-01-23 10:23:06 -0800281 const SdpSemantics sdp_semantics_;
282};
283
284class PeerConnectionIceTest
285 : public PeerConnectionIceBaseTest,
286 public ::testing::WithParamInterface<SdpSemantics> {
287 protected:
Harald Alvestrand76829d72018-07-18 23:24:36 +0200288 PeerConnectionIceTest() : PeerConnectionIceBaseTest(GetParam()) {
289 webrtc::metrics::Reset();
290 }
Steve Antonf1c6db12017-10-13 11:13:35 -0700291};
292
293::testing::AssertionResult AssertCandidatesEqual(const char* a_expr,
294 const char* b_expr,
295 const cricket::Candidate& a,
296 const cricket::Candidate& b) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200297 rtc::StringBuilder failure_info;
Steve Antonf1c6db12017-10-13 11:13:35 -0700298 if (a.component() != b.component()) {
299 failure_info << "\ncomponent: " << a.component() << " != " << b.component();
300 }
301 if (a.protocol() != b.protocol()) {
302 failure_info << "\nprotocol: " << a.protocol() << " != " << b.protocol();
303 }
304 if (a.address() != b.address()) {
305 failure_info << "\naddress: " << a.address().ToString()
306 << " != " << b.address().ToString();
307 }
308 if (a.type() != b.type()) {
309 failure_info << "\ntype: " << a.type() << " != " << b.type();
310 }
311 std::string failure_info_str = failure_info.str();
312 if (failure_info_str.empty()) {
313 return ::testing::AssertionSuccess();
314 } else {
315 return ::testing::AssertionFailure()
316 << a_expr << " and " << b_expr << " are not equal"
317 << failure_info_str;
318 }
319}
320
Steve Anton46d926a2018-01-23 10:23:06 -0800321TEST_P(PeerConnectionIceTest, OfferContainsGatheredCandidates) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700322 const SocketAddress kLocalAddress("1.1.1.1", 0);
323
324 auto caller = CreatePeerConnectionWithAudioVideo();
325 caller->network()->AddInterface(kLocalAddress);
326
327 // Start ICE candidate gathering by setting the local offer.
328 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
329
330 EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kIceCandidatesTimeout);
331
332 auto offer = caller->CreateOffer();
333 EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(0).size());
334 EXPECT_EQ(caller->observer()->GetCandidatesByMline(0).size(),
335 offer->candidates(0)->count());
336 EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(1).size());
337 EXPECT_EQ(caller->observer()->GetCandidatesByMline(1).size(),
338 offer->candidates(1)->count());
339}
340
Steve Anton46d926a2018-01-23 10:23:06 -0800341TEST_P(PeerConnectionIceTest, AnswerContainsGatheredCandidates) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700342 const SocketAddress kCallerAddress("1.1.1.1", 0);
343
344 auto caller = CreatePeerConnectionWithAudioVideo();
345 auto callee = CreatePeerConnectionWithAudioVideo();
346 caller->network()->AddInterface(kCallerAddress);
347
348 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
349 ASSERT_TRUE(callee->SetLocalDescription(callee->CreateAnswer()));
350
351 EXPECT_TRUE_WAIT(callee->IsIceGatheringDone(), kIceCandidatesTimeout);
352
Steve Antondffead82018-02-06 10:31:29 -0800353 auto* answer = callee->pc()->local_description();
Steve Antonf1c6db12017-10-13 11:13:35 -0700354 EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(0).size());
355 EXPECT_EQ(callee->observer()->GetCandidatesByMline(0).size(),
356 answer->candidates(0)->count());
357 EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(1).size());
358 EXPECT_EQ(callee->observer()->GetCandidatesByMline(1).size(),
359 answer->candidates(1)->count());
360}
361
Steve Anton46d926a2018-01-23 10:23:06 -0800362TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700363 CanSetRemoteSessionDescriptionWithRemoteCandidates) {
364 const SocketAddress kCallerAddress("1.1.1.1", 1111);
365
366 auto caller = CreatePeerConnectionWithAudioVideo();
367 auto callee = CreatePeerConnectionWithAudioVideo();
368
369 auto offer = caller->CreateOfferAndSetAsLocal();
370 cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress);
371 AddCandidateToFirstTransport(&candidate, offer.get());
372
373 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
374 auto remote_candidates = callee->GetIceCandidatesFromRemoteDescription();
375 ASSERT_EQ(1u, remote_candidates.size());
376 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate,
377 remote_candidates[0]->candidate());
378}
379
Steve Anton46d926a2018-01-23 10:23:06 -0800380TEST_P(PeerConnectionIceTest, SetLocalDescriptionFailsIfNoIceCredentials) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700381 auto caller = CreatePeerConnectionWithAudioVideo();
382
383 auto offer = caller->CreateOffer();
384 RemoveIceUfragPwd(offer.get());
385
386 EXPECT_FALSE(caller->SetLocalDescription(std::move(offer)));
387}
388
Steve Anton46d926a2018-01-23 10:23:06 -0800389TEST_P(PeerConnectionIceTest, SetRemoteDescriptionFailsIfNoIceCredentials) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700390 auto caller = CreatePeerConnectionWithAudioVideo();
391 auto callee = CreatePeerConnectionWithAudioVideo();
392
393 auto offer = caller->CreateOfferAndSetAsLocal();
394 RemoveIceUfragPwd(offer.get());
395
396 EXPECT_FALSE(callee->SetRemoteDescription(std::move(offer)));
397}
398
Steve Antonf764cf42018-05-01 14:32:17 -0700399// Test that doing an offer/answer exchange with no transport (i.e., no data
400// channel or media) results in the ICE connection state staying at New.
401TEST_P(PeerConnectionIceTest,
402 OfferAnswerWithNoTransportsDoesNotChangeIceConnectionState) {
403 auto caller = CreatePeerConnection();
404 auto callee = CreatePeerConnection();
405
406 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
407
408 EXPECT_EQ(PeerConnectionInterface::kIceConnectionNew,
409 caller->pc()->ice_connection_state());
410 EXPECT_EQ(PeerConnectionInterface::kIceConnectionNew,
411 callee->pc()->ice_connection_state());
412}
413
Steve Antonf1c6db12017-10-13 11:13:35 -0700414// The following group tests that ICE candidates are not generated before
415// SetLocalDescription is called on a PeerConnection.
416
Steve Anton46d926a2018-01-23 10:23:06 -0800417TEST_P(PeerConnectionIceTest, NoIceCandidatesBeforeSetLocalDescription) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700418 const SocketAddress kLocalAddress("1.1.1.1", 0);
419
420 auto caller = CreatePeerConnectionWithAudioVideo();
421 caller->network()->AddInterface(kLocalAddress);
422
423 // Pump for 1 second and verify that no candidates are generated.
424 rtc::Thread::Current()->ProcessMessages(1000);
425
426 EXPECT_EQ(0u, caller->observer()->candidates_.size());
427}
Steve Anton46d926a2018-01-23 10:23:06 -0800428TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700429 NoIceCandidatesBeforeAnswerSetAsLocalDescription) {
430 const SocketAddress kCallerAddress("1.1.1.1", 1111);
431
432 auto caller = CreatePeerConnectionWithAudioVideo();
433 auto callee = CreatePeerConnectionWithAudioVideo();
434 caller->network()->AddInterface(kCallerAddress);
435
436 auto offer = caller->CreateOfferAndSetAsLocal();
437 cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress);
438 AddCandidateToFirstTransport(&candidate, offer.get());
439 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
440
441 // Pump for 1 second and verify that no candidates are generated.
442 rtc::Thread::Current()->ProcessMessages(1000);
443
444 EXPECT_EQ(0u, callee->observer()->candidates_.size());
445}
446
Steve Anton46d926a2018-01-23 10:23:06 -0800447TEST_P(PeerConnectionIceTest, CannotAddCandidateWhenRemoteDescriptionNotSet) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700448 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
449
450 auto caller = CreatePeerConnectionWithAudioVideo();
451 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
Steve Anton27ab0e52018-07-23 15:11:53 -0700452 std::unique_ptr<IceCandidateInterface> jsep_candidate =
453 CreateIceCandidate(cricket::CN_AUDIO, 0, candidate);
Steve Antonf1c6db12017-10-13 11:13:35 -0700454
Steve Anton27ab0e52018-07-23 15:11:53 -0700455 EXPECT_FALSE(caller->pc()->AddIceCandidate(jsep_candidate.get()));
Steve Antonf1c6db12017-10-13 11:13:35 -0700456
457 caller->CreateOfferAndSetAsLocal();
458
Steve Anton27ab0e52018-07-23 15:11:53 -0700459 EXPECT_FALSE(caller->pc()->AddIceCandidate(jsep_candidate.get()));
Steve Antonb443dfe2019-03-05 14:09:49 -0800460 EXPECT_THAT(webrtc::metrics::Samples("WebRTC.PeerConnection.AddIceCandidate"),
461 ElementsAre(Pair(kAddIceCandidateFailNoRemoteDescription, 2)));
Steve Antonf1c6db12017-10-13 11:13:35 -0700462}
463
Steve Antonc79268f2018-04-24 09:54:10 -0700464TEST_P(PeerConnectionIceTest, CannotAddCandidateWhenPeerConnectionClosed) {
465 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
466
467 auto caller = CreatePeerConnectionWithAudioVideo();
468 auto callee = CreatePeerConnectionWithAudioVideo();
469
470 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
471
472 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
473 auto* audio_content = cricket::GetFirstAudioContent(
474 caller->pc()->local_description()->description());
Steve Anton27ab0e52018-07-23 15:11:53 -0700475 std::unique_ptr<IceCandidateInterface> jsep_candidate =
476 CreateIceCandidate(audio_content->name, 0, candidate);
Steve Antonc79268f2018-04-24 09:54:10 -0700477
478 caller->pc()->Close();
479
Steve Anton27ab0e52018-07-23 15:11:53 -0700480 EXPECT_FALSE(caller->pc()->AddIceCandidate(jsep_candidate.get()));
Steve Antonc79268f2018-04-24 09:54:10 -0700481}
482
Steve Anton46d926a2018-01-23 10:23:06 -0800483TEST_P(PeerConnectionIceTest, DuplicateIceCandidateIgnoredWhenAdded) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700484 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
485
486 auto caller = CreatePeerConnectionWithAudioVideo();
487 auto callee = CreatePeerConnectionWithAudioVideo();
488
489 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
490 ASSERT_TRUE(
491 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
492
493 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
494 caller->AddIceCandidate(&candidate);
495 EXPECT_TRUE(caller->AddIceCandidate(&candidate));
496 EXPECT_EQ(1u, caller->GetIceCandidatesFromRemoteDescription().size());
497}
498
Steve Anton46d926a2018-01-23 10:23:06 -0800499TEST_P(PeerConnectionIceTest,
Steve Antonc79268f2018-04-24 09:54:10 -0700500 CannotRemoveIceCandidatesWhenPeerConnectionClosed) {
501 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
502
503 auto caller = CreatePeerConnectionWithAudioVideo();
504 auto callee = CreatePeerConnectionWithAudioVideo();
505
506 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
507
508 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
509 auto* audio_content = cricket::GetFirstAudioContent(
510 caller->pc()->local_description()->description());
Steve Anton27ab0e52018-07-23 15:11:53 -0700511 std::unique_ptr<IceCandidateInterface> ice_candidate =
512 CreateIceCandidate(audio_content->name, 0, candidate);
Steve Antonc79268f2018-04-24 09:54:10 -0700513
Steve Anton27ab0e52018-07-23 15:11:53 -0700514 ASSERT_TRUE(caller->pc()->AddIceCandidate(ice_candidate.get()));
Steve Antonc79268f2018-04-24 09:54:10 -0700515
516 caller->pc()->Close();
517
518 EXPECT_FALSE(caller->pc()->RemoveIceCandidates({candidate}));
519}
520
521TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700522 AddRemoveCandidateWithEmptyTransportDoesNotCrash) {
523 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
524
525 auto caller = CreatePeerConnectionWithAudioVideo();
526 auto callee = CreatePeerConnectionWithAudioVideo();
527
528 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
529 ASSERT_TRUE(
530 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
531
532 // |candidate.transport_name()| is empty.
533 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
Steve Anton46d926a2018-01-23 10:23:06 -0800534 auto* audio_content = cricket::GetFirstAudioContent(
535 caller->pc()->local_description()->description());
Steve Anton27ab0e52018-07-23 15:11:53 -0700536 std::unique_ptr<IceCandidateInterface> ice_candidate =
537 CreateIceCandidate(audio_content->name, 0, candidate);
538 EXPECT_TRUE(caller->pc()->AddIceCandidate(ice_candidate.get()));
Steve Antonf1c6db12017-10-13 11:13:35 -0700539 EXPECT_TRUE(caller->pc()->RemoveIceCandidates({candidate}));
540}
541
Steve Anton46d926a2018-01-23 10:23:06 -0800542TEST_P(PeerConnectionIceTest, RemoveCandidateRemovesFromRemoteDescription) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700543 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
544
545 auto caller = CreatePeerConnectionWithAudioVideo();
546 auto callee = CreatePeerConnectionWithAudioVideo();
547
548 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
549 ASSERT_TRUE(
550 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
551
552 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
553 ASSERT_TRUE(caller->AddIceCandidate(&candidate));
554 EXPECT_TRUE(caller->pc()->RemoveIceCandidates({candidate}));
555 EXPECT_EQ(0u, caller->GetIceCandidatesFromRemoteDescription().size());
556}
557
558// Test that if a candidate is added via AddIceCandidate and via an updated
559// remote description, then both candidates appear in the stored remote
560// description.
Steve Anton46d926a2018-01-23 10:23:06 -0800561TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700562 CandidateInSubsequentOfferIsAddedToRemoteDescription) {
563 const SocketAddress kCallerAddress1("1.1.1.1", 1111);
564 const SocketAddress kCallerAddress2("2.2.2.2", 2222);
565
566 auto caller = CreatePeerConnectionWithAudioVideo();
567 auto callee = CreatePeerConnectionWithAudioVideo();
568
569 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
570 ASSERT_TRUE(
571 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
572
573 // Add one candidate via |AddIceCandidate|.
574 cricket::Candidate candidate1 = CreateLocalUdpCandidate(kCallerAddress1);
575 ASSERT_TRUE(callee->AddIceCandidate(&candidate1));
576
577 // Add the second candidate via a reoffer.
578 auto offer = caller->CreateOffer();
579 cricket::Candidate candidate2 = CreateLocalUdpCandidate(kCallerAddress2);
580 AddCandidateToFirstTransport(&candidate2, offer.get());
581
582 // Expect both candidates to appear in the callee's remote description.
583 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
584 EXPECT_EQ(2u, callee->GetIceCandidatesFromRemoteDescription().size());
585}
586
587// The follow test verifies that SetLocal/RemoteDescription fails when an offer
588// has either ICE ufrag/pwd too short or too long and succeeds otherwise.
589// The standard (https://tools.ietf.org/html/rfc5245#section-15.4) says that
590// pwd must be 22-256 characters and ufrag must be 4-256 characters.
Steve Anton46d926a2018-01-23 10:23:06 -0800591TEST_P(PeerConnectionIceTest, VerifyUfragPwdLength) {
Yves Gerey665174f2018-06-19 15:03:05 +0200592 auto set_local_description_with_ufrag_pwd_length = [this](int ufrag_len,
593 int pwd_len) {
594 auto pc = CreatePeerConnectionWithAudioVideo();
595 auto offer = pc->CreateOffer();
596 SetIceUfragPwd(offer.get(), std::string(ufrag_len, 'x'),
597 std::string(pwd_len, 'x'));
598 return pc->SetLocalDescription(std::move(offer));
599 };
Steve Antonf1c6db12017-10-13 11:13:35 -0700600
Yves Gerey665174f2018-06-19 15:03:05 +0200601 auto set_remote_description_with_ufrag_pwd_length = [this](int ufrag_len,
602 int pwd_len) {
603 auto pc = CreatePeerConnectionWithAudioVideo();
604 auto offer = pc->CreateOffer();
605 SetIceUfragPwd(offer.get(), std::string(ufrag_len, 'x'),
606 std::string(pwd_len, 'x'));
607 return pc->SetRemoteDescription(std::move(offer));
608 };
Steve Antonf1c6db12017-10-13 11:13:35 -0700609
610 EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(3, 22));
611 EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(3, 22));
612 EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(257, 22));
613 EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(257, 22));
614 EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(4, 21));
615 EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(4, 21));
616 EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(4, 257));
617 EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(4, 257));
618 EXPECT_TRUE(set_local_description_with_ufrag_pwd_length(4, 22));
619 EXPECT_TRUE(set_remote_description_with_ufrag_pwd_length(4, 22));
620 EXPECT_TRUE(set_local_description_with_ufrag_pwd_length(256, 256));
621 EXPECT_TRUE(set_remote_description_with_ufrag_pwd_length(256, 256));
622}
623
624::testing::AssertionResult AssertIpInCandidates(
625 const char* address_expr,
626 const char* candidates_expr,
627 const SocketAddress& address,
628 const std::vector<IceCandidateInterface*> candidates) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200629 rtc::StringBuilder candidate_hosts;
Steve Antonf1c6db12017-10-13 11:13:35 -0700630 for (const auto* candidate : candidates) {
631 const auto& candidate_ip = candidate->candidate().address().ipaddr();
632 if (candidate_ip == address.ipaddr()) {
633 return ::testing::AssertionSuccess();
634 }
Jonas Olssonabbe8412018-04-03 13:40:05 +0200635 candidate_hosts << "\n" << candidate_ip.ToString();
Steve Antonf1c6db12017-10-13 11:13:35 -0700636 }
637 return ::testing::AssertionFailure()
638 << address_expr << " (host " << address.HostAsURIString()
639 << ") not in " << candidates_expr
640 << " which have the following address hosts:" << candidate_hosts.str();
641}
642
Steve Anton46d926a2018-01-23 10:23:06 -0800643TEST_P(PeerConnectionIceTest, CandidatesGeneratedForEachLocalInterface) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700644 const SocketAddress kLocalAddress1("1.1.1.1", 0);
645 const SocketAddress kLocalAddress2("2.2.2.2", 0);
646
647 auto caller = CreatePeerConnectionWithAudioVideo();
648 caller->network()->AddInterface(kLocalAddress1);
649 caller->network()->AddInterface(kLocalAddress2);
650
651 caller->CreateOfferAndSetAsLocal();
652 EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kIceCandidatesTimeout);
653
654 auto candidates = caller->observer()->GetCandidatesByMline(0);
655 EXPECT_PRED_FORMAT2(AssertIpInCandidates, kLocalAddress1, candidates);
656 EXPECT_PRED_FORMAT2(AssertIpInCandidates, kLocalAddress2, candidates);
657}
658
Steve Anton46d926a2018-01-23 10:23:06 -0800659TEST_P(PeerConnectionIceTest, TrickledSingleCandidateAddedToRemoteDescription) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700660 const SocketAddress kCallerAddress("1.1.1.1", 1111);
661
662 auto caller = CreatePeerConnectionWithAudioVideo();
663 auto callee = CreatePeerConnectionWithAudioVideo();
664
665 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
666
667 cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress);
668 callee->AddIceCandidate(&candidate);
669 auto candidates = callee->GetIceCandidatesFromRemoteDescription();
670 ASSERT_EQ(1u, candidates.size());
671 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate,
672 candidates[0]->candidate());
673}
674
Steve Anton46d926a2018-01-23 10:23:06 -0800675TEST_P(PeerConnectionIceTest, TwoTrickledCandidatesAddedToRemoteDescription) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700676 const SocketAddress kCalleeAddress1("1.1.1.1", 1111);
677 const SocketAddress kCalleeAddress2("2.2.2.2", 2222);
678
679 auto caller = CreatePeerConnectionWithAudioVideo();
680 auto callee = CreatePeerConnectionWithAudioVideo();
681
682 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
683 ASSERT_TRUE(
684 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
685
686 cricket::Candidate candidate1 = CreateLocalUdpCandidate(kCalleeAddress1);
687 caller->AddIceCandidate(&candidate1);
688
689 cricket::Candidate candidate2 = CreateLocalUdpCandidate(kCalleeAddress2);
690 caller->AddIceCandidate(&candidate2);
691
692 auto candidates = caller->GetIceCandidatesFromRemoteDescription();
693 ASSERT_EQ(2u, candidates.size());
694 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate1,
695 candidates[0]->candidate());
696 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate2,
697 candidates[1]->candidate());
698}
699
Henrik Boströmee6f4f62019-11-06 12:36:12 +0100700TEST_P(PeerConnectionIceTest, AsyncAddIceCandidateIsAddedToRemoteDescription) {
701 auto candidate = CreateLocalUdpCandidate(SocketAddress("1.1.1.1", 1111));
702
703 auto caller = CreatePeerConnectionWithAudioVideo();
704 auto callee = CreatePeerConnectionWithAudioVideo();
705
706 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
707
708 auto jsep_candidate =
709 callee->CreateJsepCandidateForFirstTransport(&candidate);
710 bool operation_completed = false;
711 callee->pc()->AddIceCandidate(std::move(jsep_candidate),
712 [&operation_completed](RTCError result) {
713 EXPECT_TRUE(result.ok());
714 operation_completed = true;
715 });
716 EXPECT_TRUE_WAIT(operation_completed, kWaitTimeout);
717
718 auto candidates = callee->GetIceCandidatesFromRemoteDescription();
719 ASSERT_EQ(1u, candidates.size());
720 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate,
721 candidates[0]->candidate());
722}
723
724TEST_P(PeerConnectionIceTest,
725 AsyncAddIceCandidateCompletesImmediatelyIfNoPendingOperation) {
726 auto candidate = CreateLocalUdpCandidate(SocketAddress("1.1.1.1", 1111));
727
728 auto caller = CreatePeerConnectionWithAudioVideo();
729 auto callee = CreatePeerConnectionWithAudioVideo();
730
731 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
732
733 auto jsep_candidate =
734 callee->CreateJsepCandidateForFirstTransport(&candidate);
735 bool operation_completed = false;
736 callee->pc()->AddIceCandidate(
737 std::move(jsep_candidate),
738 [&operation_completed](RTCError result) { operation_completed = true; });
739 EXPECT_TRUE(operation_completed);
740}
741
742TEST_P(PeerConnectionIceTest,
743 AsyncAddIceCandidateCompletesWhenPendingOperationCompletes) {
744 auto candidate = CreateLocalUdpCandidate(SocketAddress("1.1.1.1", 1111));
745
746 auto caller = CreatePeerConnectionWithAudioVideo();
747 auto callee = CreatePeerConnectionWithAudioVideo();
748
749 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
750
751 // Chain an operation that will block AddIceCandidate() from executing.
752 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> answer_observer(
753 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
754 callee->pc()->CreateAnswer(answer_observer, RTCOfferAnswerOptions());
755
756 auto jsep_candidate =
757 callee->CreateJsepCandidateForFirstTransport(&candidate);
758 bool operation_completed = false;
759 callee->pc()->AddIceCandidate(
760 std::move(jsep_candidate),
761 [&operation_completed](RTCError result) { operation_completed = true; });
762 // The operation will not be able to complete until we EXPECT_TRUE_WAIT()
763 // allowing CreateAnswer() to complete.
764 EXPECT_FALSE(operation_completed);
765 EXPECT_TRUE_WAIT(answer_observer->called(), kWaitTimeout);
766 // As soon as it does, AddIceCandidate() will execute without delay, so it
767 // must also have completed.
768 EXPECT_TRUE(operation_completed);
769}
770
771TEST_P(PeerConnectionIceTest,
772 AsyncAddIceCandidateFailsBeforeSetRemoteDescription) {
773 auto candidate = CreateLocalUdpCandidate(SocketAddress("1.1.1.1", 1111));
774
775 auto caller = CreatePeerConnectionWithAudioVideo();
776 std::unique_ptr<IceCandidateInterface> jsep_candidate =
777 CreateIceCandidate(cricket::CN_AUDIO, 0, candidate);
778
779 bool operation_completed = false;
780 caller->pc()->AddIceCandidate(
781 std::move(jsep_candidate), [&operation_completed](RTCError result) {
782 EXPECT_FALSE(result.ok());
783 EXPECT_EQ(result.message(),
784 std::string("Error processing ICE candidate"));
785 operation_completed = true;
786 });
787 EXPECT_TRUE_WAIT(operation_completed, kWaitTimeout);
788}
789
790TEST_P(PeerConnectionIceTest,
791 AsyncAddIceCandidateFailsIfPeerConnectionDestroyed) {
792 auto candidate = CreateLocalUdpCandidate(SocketAddress("1.1.1.1", 1111));
793
794 auto caller = CreatePeerConnectionWithAudioVideo();
795 auto callee = CreatePeerConnectionWithAudioVideo();
796
797 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
798
799 // Chain an operation that will block AddIceCandidate() from executing.
800 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> answer_observer(
801 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
802 callee->pc()->CreateAnswer(answer_observer, RTCOfferAnswerOptions());
803
804 auto jsep_candidate =
805 callee->CreateJsepCandidateForFirstTransport(&candidate);
806 bool operation_completed = false;
807 callee->pc()->AddIceCandidate(
808 std::move(jsep_candidate), [&operation_completed](RTCError result) {
809 EXPECT_FALSE(result.ok());
810 EXPECT_EQ(
811 result.message(),
812 std::string(
813 "AddIceCandidate failed because the session was shut down"));
814 operation_completed = true;
815 });
816 // The operation will not be able to run until EXPECT_TRUE_WAIT(), giving us
817 // time to remove all references to the PeerConnection.
818 EXPECT_FALSE(operation_completed);
819 // This should delete the callee PC.
820 callee = nullptr;
821 EXPECT_TRUE_WAIT(operation_completed, kWaitTimeout);
822}
823
Steve Anton46d926a2018-01-23 10:23:06 -0800824TEST_P(PeerConnectionIceTest, LocalDescriptionUpdatedWhenContinualGathering) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700825 const SocketAddress kLocalAddress("1.1.1.1", 0);
826
827 RTCConfiguration config;
828 config.continual_gathering_policy =
829 PeerConnectionInterface::GATHER_CONTINUALLY;
830 auto caller = CreatePeerConnectionWithAudioVideo(config);
831 caller->network()->AddInterface(kLocalAddress);
832
833 // Start ICE candidate gathering by setting the local offer.
834 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
835
836 // Since we're using continual gathering, we won't get "gathering done".
837 EXPECT_TRUE_WAIT(
838 caller->pc()->local_description()->candidates(0)->count() > 0,
839 kIceCandidatesTimeout);
840}
841
842// Test that when continual gathering is enabled, and a network interface goes
843// down, the candidate is signaled as removed and removed from the local
844// description.
Steve Anton46d926a2018-01-23 10:23:06 -0800845TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700846 LocalCandidatesRemovedWhenNetworkDownIfGatheringContinually) {
847 const SocketAddress kLocalAddress("1.1.1.1", 0);
848
849 RTCConfiguration config;
850 config.continual_gathering_policy =
851 PeerConnectionInterface::GATHER_CONTINUALLY;
852 auto caller = CreatePeerConnectionWithAudioVideo(config);
853 caller->network()->AddInterface(kLocalAddress);
854
855 // Start ICE candidate gathering by setting the local offer.
856 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
857
858 EXPECT_TRUE_WAIT(
859 caller->pc()->local_description()->candidates(0)->count() > 0,
860 kIceCandidatesTimeout);
861
862 // Remove the only network interface, causing the PeerConnection to signal
863 // the removal of all candidates derived from this interface.
864 caller->network()->RemoveInterface(kLocalAddress);
865
866 EXPECT_EQ_WAIT(0u, caller->pc()->local_description()->candidates(0)->count(),
867 kIceCandidatesTimeout);
868 EXPECT_LT(0, caller->observer()->num_candidates_removed_);
869}
870
Steve Anton46d926a2018-01-23 10:23:06 -0800871TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700872 LocalCandidatesNotRemovedWhenNetworkDownIfGatheringOnce) {
873 const SocketAddress kLocalAddress("1.1.1.1", 0);
874
875 RTCConfiguration config;
876 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
877 auto caller = CreatePeerConnectionWithAudioVideo(config);
878 caller->network()->AddInterface(kLocalAddress);
879
880 // Start ICE candidate gathering by setting the local offer.
881 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
882
883 EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kIceCandidatesTimeout);
884
885 caller->network()->RemoveInterface(kLocalAddress);
886
887 // Verify that the local candidates are not removed;
888 rtc::Thread::Current()->ProcessMessages(1000);
889 EXPECT_EQ(0, caller->observer()->num_candidates_removed_);
890}
891
892// The following group tests that when an offer includes a new ufrag or pwd
893// (indicating an ICE restart) the old candidates are removed and new candidates
894// added to the remote description.
895
Steve Anton46d926a2018-01-23 10:23:06 -0800896TEST_P(PeerConnectionIceTest, IceRestartOfferClearsExistingCandidate) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700897 const SocketAddress kCallerAddress("1.1.1.1", 1111);
898
899 auto caller = CreatePeerConnectionWithAudioVideo();
900 auto callee = CreatePeerConnectionWithAudioVideo();
901
Amit Hilbuchae3df542019-01-07 12:13:08 -0800902 auto offer = caller->CreateOfferAndSetAsLocal();
Steve Antonf1c6db12017-10-13 11:13:35 -0700903 cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress);
904 AddCandidateToFirstTransport(&candidate, offer.get());
905
906 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
907
908 RTCOfferAnswerOptions options;
909 options.ice_restart = true;
Amit Hilbuchae3df542019-01-07 12:13:08 -0800910 ASSERT_TRUE(
911 callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal(options)));
Steve Antonf1c6db12017-10-13 11:13:35 -0700912
913 EXPECT_EQ(0u, callee->GetIceCandidatesFromRemoteDescription().size());
914}
Steve Anton46d926a2018-01-23 10:23:06 -0800915TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700916 IceRestartOfferCandidateReplacesExistingCandidate) {
917 const SocketAddress kFirstCallerAddress("1.1.1.1", 1111);
918 const SocketAddress kRestartedCallerAddress("2.2.2.2", 2222);
919
920 auto caller = CreatePeerConnectionWithAudioVideo();
921 auto callee = CreatePeerConnectionWithAudioVideo();
922
Amit Hilbuchae3df542019-01-07 12:13:08 -0800923 auto offer = caller->CreateOfferAndSetAsLocal();
Steve Antonf1c6db12017-10-13 11:13:35 -0700924 cricket::Candidate old_candidate =
925 CreateLocalUdpCandidate(kFirstCallerAddress);
926 AddCandidateToFirstTransport(&old_candidate, offer.get());
927
928 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
929
930 RTCOfferAnswerOptions options;
931 options.ice_restart = true;
Amit Hilbuchae3df542019-01-07 12:13:08 -0800932 auto restart_offer = caller->CreateOfferAndSetAsLocal(options);
Steve Antonf1c6db12017-10-13 11:13:35 -0700933 cricket::Candidate new_candidate =
934 CreateLocalUdpCandidate(kRestartedCallerAddress);
935 AddCandidateToFirstTransport(&new_candidate, restart_offer.get());
936
937 ASSERT_TRUE(callee->SetRemoteDescription(std::move(restart_offer)));
938
939 auto remote_candidates = callee->GetIceCandidatesFromRemoteDescription();
940 ASSERT_EQ(1u, remote_candidates.size());
941 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, new_candidate,
942 remote_candidates[0]->candidate());
943}
944
945// Test that if there is not an ICE restart (i.e., nothing changes), then the
946// answer to a later offer should have the same ufrag/pwd as the first answer.
Steve Anton46d926a2018-01-23 10:23:06 -0800947TEST_P(PeerConnectionIceTest, LaterAnswerHasSameIceCredentialsIfNoIceRestart) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700948 auto caller = CreatePeerConnectionWithAudioVideo();
949 auto callee = CreatePeerConnectionWithAudioVideo();
950
951 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
952 ASSERT_TRUE(
953 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
954
955 // Re-offer.
956 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
957
958 auto answer = callee->CreateAnswer();
959 auto* answer_transport_desc = GetFirstTransportDescription(answer.get());
960 auto* local_transport_desc =
961 GetFirstTransportDescription(callee->pc()->local_description());
962
963 EXPECT_EQ(answer_transport_desc->ice_ufrag, local_transport_desc->ice_ufrag);
964 EXPECT_EQ(answer_transport_desc->ice_pwd, local_transport_desc->ice_pwd);
965}
966
Henrik Boström79b69802019-07-18 11:16:56 +0200967TEST_P(PeerConnectionIceTest, RestartIceGeneratesNewCredentials) {
968 auto caller = CreatePeerConnectionWithAudioVideo();
969 auto callee = CreatePeerConnectionWithAudioVideo();
970
971 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
972 auto initial_ice_credentials =
973 GetIceCredentials(caller->pc()->local_description());
974 caller->pc()->RestartIce();
975 ASSERT_TRUE(caller->CreateOfferAndSetAsLocal());
976 auto restarted_ice_credentials =
977 GetIceCredentials(caller->pc()->local_description());
978 EXPECT_NE(initial_ice_credentials, restarted_ice_credentials);
979}
980
981TEST_P(PeerConnectionIceTest,
982 RestartIceWhileLocalOfferIsPendingGeneratesNewCredentialsInNextOffer) {
983 auto caller = CreatePeerConnectionWithAudioVideo();
984 auto callee = CreatePeerConnectionWithAudioVideo();
985
986 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
987 auto initial_ice_credentials =
988 GetIceCredentials(caller->pc()->local_description());
989 // ICE restart becomes needed while an O/A is pending and |caller| is the
990 // offerer.
991 caller->pc()->RestartIce();
992 ASSERT_TRUE(
993 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
994 ASSERT_TRUE(caller->CreateOfferAndSetAsLocal());
995 auto restarted_ice_credentials =
996 GetIceCredentials(caller->pc()->local_description());
997 EXPECT_NE(initial_ice_credentials, restarted_ice_credentials);
998}
999
1000TEST_P(PeerConnectionIceTest,
1001 RestartIceWhileRemoteOfferIsPendingGeneratesNewCredentialsInNextOffer) {
1002 auto caller = CreatePeerConnectionWithAudioVideo();
1003 auto callee = CreatePeerConnectionWithAudioVideo();
1004
1005 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1006 auto initial_ice_credentials =
1007 GetIceCredentials(caller->pc()->local_description());
1008 ASSERT_TRUE(caller->SetRemoteDescription(callee->CreateOfferAndSetAsLocal()));
1009 // ICE restart becomes needed while an O/A is pending and |caller| is the
1010 // answerer.
1011 caller->pc()->RestartIce();
1012 ASSERT_TRUE(
1013 callee->SetRemoteDescription(caller->CreateAnswerAndSetAsLocal()));
1014 ASSERT_TRUE(caller->CreateOfferAndSetAsLocal());
1015 auto restarted_ice_credentials =
1016 GetIceCredentials(caller->pc()->local_description());
1017 EXPECT_NE(initial_ice_credentials, restarted_ice_credentials);
1018}
1019
1020TEST_P(PeerConnectionIceTest, RestartIceTriggeredByRemoteSide) {
1021 auto caller = CreatePeerConnectionWithAudioVideo();
1022 auto callee = CreatePeerConnectionWithAudioVideo();
1023
1024 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1025 auto initial_ice_credentials =
1026 GetIceCredentials(caller->pc()->local_description());
1027
1028 // Remote restart and O/A exchange with |caller| as the answerer should
1029 // restart ICE locally as well.
1030 callee->pc()->RestartIce();
1031 ASSERT_TRUE(callee->ExchangeOfferAnswerWith(caller.get()));
1032
1033 auto restarted_ice_credentials =
1034 GetIceCredentials(caller->pc()->local_description());
1035 EXPECT_NE(initial_ice_credentials, restarted_ice_credentials);
1036}
1037
1038TEST_P(PeerConnectionIceTest, RestartIceCausesNegotiationNeeded) {
1039 auto caller = CreatePeerConnectionWithAudioVideo();
1040 auto callee = CreatePeerConnectionWithAudioVideo();
1041
1042 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1043 caller->observer()->clear_negotiation_needed();
1044 caller->pc()->RestartIce();
1045 EXPECT_TRUE(caller->observer()->negotiation_needed());
1046}
1047
1048// In Unified Plan, "onnegotiationneeded" is spec-compliant, including not
1049// firing multipe times in a row, or firing when returning to the stable
1050// signaling state if negotiation is still needed. In Plan B it fires any time
1051// something changes. As such, some tests are SdpSemantics-specific.
1052class PeerConnectionIceTestUnifiedPlan : public PeerConnectionIceBaseTest {
1053 protected:
1054 PeerConnectionIceTestUnifiedPlan()
1055 : PeerConnectionIceBaseTest(SdpSemantics::kUnifiedPlan) {}
1056};
1057
1058TEST_F(PeerConnectionIceTestUnifiedPlan,
1059 RestartIceWhileLocalOfferIsPendingCausesNegotiationNeededWhenStable) {
1060 auto caller = CreatePeerConnectionWithAudioVideo();
1061 auto callee = CreatePeerConnectionWithAudioVideo();
1062
1063 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
1064 // ICE restart becomes needed while an O/A is pending and |caller| is the
1065 // offerer.
1066 caller->observer()->clear_negotiation_needed();
1067 caller->pc()->RestartIce();
1068 // In Unified Plan, the event should not fire until we are back in the stable
1069 // signaling state.
1070 EXPECT_FALSE(caller->observer()->negotiation_needed());
1071 ASSERT_TRUE(
1072 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
1073 EXPECT_TRUE(caller->observer()->negotiation_needed());
1074}
1075
1076TEST_F(PeerConnectionIceTestUnifiedPlan,
1077 RestartIceWhileRemoteOfferIsPendingCausesNegotiationNeededWhenStable) {
1078 auto caller = CreatePeerConnectionWithAudioVideo();
1079 auto callee = CreatePeerConnectionWithAudioVideo();
1080
1081 // Establish initial credentials as the caller.
1082 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1083 ASSERT_TRUE(caller->SetRemoteDescription(callee->CreateOfferAndSetAsLocal()));
1084 // ICE restart becomes needed while an O/A is pending and |caller| is the
1085 // answerer.
1086 caller->observer()->clear_negotiation_needed();
1087 caller->pc()->RestartIce();
1088 // In Unified Plan, the event should not fire until we are back in the stable
1089 // signaling state.
1090 EXPECT_FALSE(caller->observer()->negotiation_needed());
1091 ASSERT_TRUE(
1092 callee->SetRemoteDescription(caller->CreateAnswerAndSetAsLocal()));
1093 EXPECT_TRUE(caller->observer()->negotiation_needed());
1094}
1095
1096TEST_F(PeerConnectionIceTestUnifiedPlan,
1097 RestartIceTriggeredByRemoteSideCauseNegotiationNotNeeded) {
1098 auto caller = CreatePeerConnectionWithAudioVideo();
1099 auto callee = CreatePeerConnectionWithAudioVideo();
1100
1101 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1102 // Local restart.
1103 caller->pc()->RestartIce();
1104 caller->observer()->clear_negotiation_needed();
1105 // Remote restart and O/A exchange with |caller| as the answerer should
1106 // restart ICE locally as well.
1107 callee->pc()->RestartIce();
1108 ASSERT_TRUE(callee->ExchangeOfferAnswerWith(caller.get()));
1109 // Having restarted ICE by the remote offer, we do not need to renegotiate ICE
1110 // credentials when back in the stable signaling state.
1111 EXPECT_FALSE(caller->observer()->negotiation_needed());
1112}
1113
1114TEST_F(PeerConnectionIceTestUnifiedPlan,
1115 RestartIceTwiceDoesNotFireNegotiationNeededTwice) {
1116 auto caller = CreatePeerConnectionWithAudioVideo();
1117 auto callee = CreatePeerConnectionWithAudioVideo();
1118
1119 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1120 caller->pc()->RestartIce();
1121 EXPECT_TRUE(caller->observer()->negotiation_needed());
1122 caller->observer()->clear_negotiation_needed();
1123 caller->pc()->RestartIce();
1124 EXPECT_FALSE(caller->observer()->negotiation_needed());
1125}
1126
1127// In Plan B, "onnegotiationneeded" is not spec-compliant, firing based on if
1128// something changed rather than if negotiation is needed. In Unified Plan it
1129// fires according to spec. As such, some tests are SdpSemantics-specific.
1130class PeerConnectionIceTestPlanB : public PeerConnectionIceBaseTest {
1131 protected:
1132 PeerConnectionIceTestPlanB()
1133 : PeerConnectionIceBaseTest(SdpSemantics::kPlanB) {}
1134};
1135
1136TEST_F(PeerConnectionIceTestPlanB,
1137 RestartIceWhileOfferIsPendingCausesNegotiationNeededImmediately) {
1138 auto caller = CreatePeerConnectionWithAudioVideo();
1139 auto callee = CreatePeerConnectionWithAudioVideo();
1140
1141 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
1142 caller->observer()->clear_negotiation_needed();
1143 caller->pc()->RestartIce();
1144 EXPECT_TRUE(caller->observer()->negotiation_needed());
1145 caller->observer()->clear_negotiation_needed();
1146 ASSERT_TRUE(
1147 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
1148 // In Plan B, the event fired early so we don't expect it to fire now. This is
1149 // not spec-compliant but follows the pattern of existing Plan B behavior.
1150 EXPECT_FALSE(caller->observer()->negotiation_needed());
1151}
1152
1153TEST_F(PeerConnectionIceTestPlanB,
1154 RestartIceTwiceDoesFireNegotiationNeededTwice) {
1155 auto caller = CreatePeerConnectionWithAudioVideo();
1156 auto callee = CreatePeerConnectionWithAudioVideo();
1157
1158 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1159 caller->observer()->clear_negotiation_needed();
1160 caller->pc()->RestartIce();
1161 EXPECT_TRUE(caller->observer()->negotiation_needed());
1162 caller->observer()->clear_negotiation_needed();
1163 caller->pc()->RestartIce();
1164 // In Plan B, the event fires every time something changed, even if we have
1165 // already fired the event. This is not spec-compliant but follows the same
1166 // pattern of existing Plan B behavior.
1167 EXPECT_TRUE(caller->observer()->negotiation_needed());
1168}
1169
Steve Antonf1c6db12017-10-13 11:13:35 -07001170// The following parameterized test verifies that if an offer is sent with a
1171// modified ICE ufrag and/or ICE pwd, then the answer should identify that the
1172// other side has initiated an ICE restart and generate a new ufrag and pwd.
1173// RFC 5245 says: "If the offer contained a change in the a=ice-ufrag or
1174// a=ice-pwd attributes compared to the previous SDP from the peer, it
1175// indicates that ICE is restarting for this media stream."
1176
Steve Anton46d926a2018-01-23 10:23:06 -08001177class PeerConnectionIceUfragPwdAnswerTest
1178 : public PeerConnectionIceBaseTest,
1179 public ::testing::WithParamInterface<
1180 std::tuple<SdpSemantics, std::tuple<bool, bool>>> {
Steve Antonf1c6db12017-10-13 11:13:35 -07001181 protected:
Steve Anton46d926a2018-01-23 10:23:06 -08001182 PeerConnectionIceUfragPwdAnswerTest()
1183 : PeerConnectionIceBaseTest(std::get<0>(GetParam())) {
1184 auto param = std::get<1>(GetParam());
1185 offer_new_ufrag_ = std::get<0>(param);
1186 offer_new_pwd_ = std::get<1>(param);
Steve Antonf1c6db12017-10-13 11:13:35 -07001187 }
1188
1189 bool offer_new_ufrag_;
1190 bool offer_new_pwd_;
1191};
1192
Steve Anton46d926a2018-01-23 10:23:06 -08001193TEST_P(PeerConnectionIceUfragPwdAnswerTest, TestIncludedInAnswer) {
Steve Antonf1c6db12017-10-13 11:13:35 -07001194 auto caller = CreatePeerConnectionWithAudioVideo();
1195 auto callee = CreatePeerConnectionWithAudioVideo();
1196
1197 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
1198 ASSERT_TRUE(
1199 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
1200
1201 auto offer = caller->CreateOffer();
1202 auto* offer_transport_desc = GetFirstTransportDescription(offer.get());
1203 if (offer_new_ufrag_) {
1204 offer_transport_desc->ice_ufrag += "_new";
1205 }
1206 if (offer_new_pwd_) {
1207 offer_transport_desc->ice_pwd += "_new";
1208 }
1209
1210 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
1211
1212 auto answer = callee->CreateAnswer();
1213 auto* answer_transport_desc = GetFirstTransportDescription(answer.get());
1214 auto* local_transport_desc =
1215 GetFirstTransportDescription(callee->pc()->local_description());
1216
1217 EXPECT_NE(answer_transport_desc->ice_ufrag, local_transport_desc->ice_ufrag);
1218 EXPECT_NE(answer_transport_desc->ice_pwd, local_transport_desc->ice_pwd);
1219}
1220
Mirko Bonadeic84f6612019-01-31 12:20:57 +01001221INSTANTIATE_TEST_SUITE_P(
Steve Anton46d926a2018-01-23 10:23:06 -08001222 PeerConnectionIceTest,
1223 PeerConnectionIceUfragPwdAnswerTest,
1224 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
1225 Values(std::make_pair(true, true), // Both changed.
1226 std::make_pair(true, false), // Only ufrag changed.
1227 std::make_pair(false, true)))); // Only pwd changed.
Steve Antonf1c6db12017-10-13 11:13:35 -07001228
1229// Test that if an ICE restart is offered on one media section, then the answer
1230// will only change ICE ufrag/pwd for that section and keep the other sections
1231// the same.
1232// Note that this only works if we have disabled BUNDLE, otherwise all media
1233// sections will share the same transport.
Steve Anton46d926a2018-01-23 10:23:06 -08001234TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -07001235 CreateAnswerHasNewUfragPwdForOnlyMediaSectionWhichRestarted) {
1236 auto caller = CreatePeerConnectionWithAudioVideo();
1237 auto callee = CreatePeerConnectionWithAudioVideo();
1238
1239 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
1240 ASSERT_TRUE(
1241 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
1242
1243 RTCOfferAnswerOptions disable_bundle_options;
1244 disable_bundle_options.use_rtp_mux = false;
1245
1246 auto offer = caller->CreateOffer(disable_bundle_options);
1247
1248 // Signal ICE restart on the first media section.
1249 auto* offer_transport_desc = GetFirstTransportDescription(offer.get());
1250 offer_transport_desc->ice_ufrag += "_new";
1251 offer_transport_desc->ice_pwd += "_new";
1252
1253 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
1254
1255 auto answer = callee->CreateAnswer(disable_bundle_options);
1256 const auto& answer_transports = answer->description()->transport_infos();
1257 const auto& local_transports =
1258 callee->pc()->local_description()->description()->transport_infos();
1259
1260 EXPECT_NE(answer_transports[0].description.ice_ufrag,
1261 local_transports[0].description.ice_ufrag);
1262 EXPECT_NE(answer_transports[0].description.ice_pwd,
1263 local_transports[0].description.ice_pwd);
1264 EXPECT_EQ(answer_transports[1].description.ice_ufrag,
1265 local_transports[1].description.ice_ufrag);
1266 EXPECT_EQ(answer_transports[1].description.ice_pwd,
1267 local_transports[1].description.ice_pwd);
1268}
1269
Qingsi Wange1692722017-11-29 13:27:20 -08001270// Test that when the initial offerer (caller) uses the lite implementation of
1271// ICE and the callee uses the full implementation, the caller takes the
1272// CONTROLLED role and the callee takes the CONTROLLING role. This is specified
1273// in RFC5245 Section 5.1.1.
Steve Anton46d926a2018-01-23 10:23:06 -08001274TEST_P(PeerConnectionIceTest,
Qingsi Wange1692722017-11-29 13:27:20 -08001275 OfferFromLiteIceControlledAndAnswerFromFullIceControlling) {
1276 auto caller = CreatePeerConnectionWithAudioVideo();
1277 auto callee = CreatePeerConnectionWithAudioVideo();
1278
1279 auto offer = caller->CreateOffer();
1280 SetIceMode(offer.get(), cricket::IceMode::ICEMODE_LITE);
1281 ASSERT_TRUE(
1282 caller->SetLocalDescription(CloneSessionDescription(offer.get())));
1283 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
1284
1285 auto answer = callee->CreateAnswer();
1286 SetIceMode(answer.get(), cricket::IceMode::ICEMODE_FULL);
1287 ASSERT_TRUE(
1288 callee->SetLocalDescription(CloneSessionDescription(answer.get())));
1289 ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer)));
1290
1291 EXPECT_EQ(cricket::ICEROLE_CONTROLLED, GetIceRole(caller));
1292 EXPECT_EQ(cricket::ICEROLE_CONTROLLING, GetIceRole(callee));
1293}
1294
1295// Test that when the caller and the callee both use the lite implementation of
1296// ICE, the initial offerer (caller) takes the CONTROLLING role and the callee
1297// takes the CONTROLLED role. This is specified in RFC5245 Section 5.1.1.
Steve Anton46d926a2018-01-23 10:23:06 -08001298TEST_P(PeerConnectionIceTest,
Qingsi Wange1692722017-11-29 13:27:20 -08001299 OfferFromLiteIceControllingAndAnswerFromLiteIceControlled) {
1300 auto caller = CreatePeerConnectionWithAudioVideo();
1301 auto callee = CreatePeerConnectionWithAudioVideo();
1302
1303 auto offer = caller->CreateOffer();
1304 SetIceMode(offer.get(), cricket::IceMode::ICEMODE_LITE);
1305 ASSERT_TRUE(
1306 caller->SetLocalDescription(CloneSessionDescription(offer.get())));
1307 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
1308
1309 auto answer = callee->CreateAnswer();
1310 SetIceMode(answer.get(), cricket::IceMode::ICEMODE_LITE);
1311 ASSERT_TRUE(
1312 callee->SetLocalDescription(CloneSessionDescription(answer.get())));
1313 ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer)));
1314
1315 EXPECT_EQ(cricket::ICEROLE_CONTROLLING, GetIceRole(caller));
1316 EXPECT_EQ(cricket::ICEROLE_CONTROLLED, GetIceRole(callee));
1317}
1318
Mirko Bonadeic84f6612019-01-31 12:20:57 +01001319INSTANTIATE_TEST_SUITE_P(PeerConnectionIceTest,
1320 PeerConnectionIceTest,
1321 Values(SdpSemantics::kPlanB,
1322 SdpSemantics::kUnifiedPlan));
Steve Anton46d926a2018-01-23 10:23:06 -08001323
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001324class PeerConnectionIceConfigTest : public ::testing::Test {
Qingsi Wang4ff54432018-03-01 18:25:20 -08001325 protected:
1326 void SetUp() override {
1327 pc_factory_ = CreatePeerConnectionFactory(
1328 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
1329 FakeAudioCaptureModule::Create(), CreateBuiltinAudioEncoderFactory(),
Anders Carlsson67537952018-05-03 11:28:29 +02001330 CreateBuiltinAudioDecoderFactory(), CreateBuiltinVideoEncoderFactory(),
1331 CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
1332 nullptr /* audio_processing */);
Qingsi Wang4ff54432018-03-01 18:25:20 -08001333 }
1334 void CreatePeerConnection(const RTCConfiguration& config) {
1335 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1336 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1337 port_allocator_ = port_allocator.get();
1338 rtc::scoped_refptr<PeerConnectionInterface> pc(
Niels Möllerf06f9232018-08-07 12:32:18 +02001339 pc_factory_->CreatePeerConnection(config, std::move(port_allocator),
1340 nullptr /* cert_generator */,
1341 &observer_));
Qingsi Wang4ff54432018-03-01 18:25:20 -08001342 EXPECT_TRUE(pc.get());
Mirko Bonadei1c546052019-02-04 14:50:38 +01001343 pc_ = std::move(pc);
Qingsi Wang4ff54432018-03-01 18:25:20 -08001344 }
1345
1346 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_ = nullptr;
1347 rtc::scoped_refptr<PeerConnectionInterface> pc_ = nullptr;
1348 cricket::FakePortAllocator* port_allocator_ = nullptr;
1349
1350 MockPeerConnectionObserver observer_;
1351};
1352
1353TEST_F(PeerConnectionIceConfigTest, SetStunCandidateKeepaliveInterval) {
1354 RTCConfiguration config;
1355 config.stun_candidate_keepalive_interval = 123;
1356 config.ice_candidate_pool_size = 1;
1357 CreatePeerConnection(config);
1358 ASSERT_NE(port_allocator_, nullptr);
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02001359 absl::optional<int> actual_stun_keepalive_interval =
Qingsi Wang4ff54432018-03-01 18:25:20 -08001360 port_allocator_->stun_candidate_keepalive_interval();
1361 EXPECT_EQ(actual_stun_keepalive_interval.value_or(-1), 123);
1362 config.stun_candidate_keepalive_interval = 321;
Niels Möller2579f0c2019-08-19 09:58:17 +02001363 ASSERT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wang4ff54432018-03-01 18:25:20 -08001364 actual_stun_keepalive_interval =
1365 port_allocator_->stun_candidate_keepalive_interval();
1366 EXPECT_EQ(actual_stun_keepalive_interval.value_or(-1), 321);
1367}
1368
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001369TEST_P(PeerConnectionIceTest, IceCredentialsCreateOffer) {
1370 RTCConfiguration config;
1371 config.ice_candidate_pool_size = 1;
1372 auto pc = CreatePeerConnectionWithAudioVideo(config);
1373 ASSERT_NE(pc->port_allocator_, nullptr);
1374 auto offer = pc->CreateOffer();
1375 auto credentials = pc->port_allocator_->GetPooledIceCredentials();
1376 ASSERT_EQ(1u, credentials.size());
1377
1378 auto* desc = offer->description();
1379 for (const auto& content : desc->contents()) {
1380 auto* transport_info = desc->GetTransportInfoByName(content.name);
1381 EXPECT_EQ(transport_info->description.ice_ufrag, credentials[0].ufrag);
1382 EXPECT_EQ(transport_info->description.ice_pwd, credentials[0].pwd);
1383 }
1384}
1385
1386TEST_P(PeerConnectionIceTest, IceCredentialsCreateAnswer) {
1387 RTCConfiguration config;
1388 config.ice_candidate_pool_size = 1;
1389 auto pc = CreatePeerConnectionWithAudioVideo(config);
1390 ASSERT_NE(pc->port_allocator_, nullptr);
1391 auto offer = pc->CreateOffer();
1392 ASSERT_TRUE(pc->SetRemoteDescription(std::move(offer)));
1393 auto answer = pc->CreateAnswer();
1394
1395 auto credentials = pc->port_allocator_->GetPooledIceCredentials();
1396 ASSERT_EQ(1u, credentials.size());
1397
1398 auto* desc = answer->description();
1399 for (const auto& content : desc->contents()) {
1400 auto* transport_info = desc->GetTransportInfoByName(content.name);
1401 EXPECT_EQ(transport_info->description.ice_ufrag, credentials[0].ufrag);
1402 EXPECT_EQ(transport_info->description.ice_pwd, credentials[0].pwd);
1403 }
1404}
1405
Steve Antonf1c6db12017-10-13 11:13:35 -07001406} // namespace webrtc