blob: 8c1a764398e35038d4a39e7f8dbba2085d3301b8 [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()));
Ying Wangef3998f2019-12-09 13:06:53 +0100460 EXPECT_METRIC_THAT(
461 webrtc::metrics::Samples("WebRTC.PeerConnection.AddIceCandidate"),
462 ElementsAre(Pair(kAddIceCandidateFailNoRemoteDescription, 2)));
Steve Antonf1c6db12017-10-13 11:13:35 -0700463}
464
Steve Antonc79268f2018-04-24 09:54:10 -0700465TEST_P(PeerConnectionIceTest, CannotAddCandidateWhenPeerConnectionClosed) {
466 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
467
468 auto caller = CreatePeerConnectionWithAudioVideo();
469 auto callee = CreatePeerConnectionWithAudioVideo();
470
471 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
472
473 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
474 auto* audio_content = cricket::GetFirstAudioContent(
475 caller->pc()->local_description()->description());
Steve Anton27ab0e52018-07-23 15:11:53 -0700476 std::unique_ptr<IceCandidateInterface> jsep_candidate =
477 CreateIceCandidate(audio_content->name, 0, candidate);
Steve Antonc79268f2018-04-24 09:54:10 -0700478
479 caller->pc()->Close();
480
Steve Anton27ab0e52018-07-23 15:11:53 -0700481 EXPECT_FALSE(caller->pc()->AddIceCandidate(jsep_candidate.get()));
Steve Antonc79268f2018-04-24 09:54:10 -0700482}
483
Steve Anton46d926a2018-01-23 10:23:06 -0800484TEST_P(PeerConnectionIceTest, DuplicateIceCandidateIgnoredWhenAdded) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700485 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
486
487 auto caller = CreatePeerConnectionWithAudioVideo();
488 auto callee = CreatePeerConnectionWithAudioVideo();
489
490 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
491 ASSERT_TRUE(
492 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
493
494 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
495 caller->AddIceCandidate(&candidate);
496 EXPECT_TRUE(caller->AddIceCandidate(&candidate));
497 EXPECT_EQ(1u, caller->GetIceCandidatesFromRemoteDescription().size());
498}
499
Steve Anton46d926a2018-01-23 10:23:06 -0800500TEST_P(PeerConnectionIceTest,
Steve Antonc79268f2018-04-24 09:54:10 -0700501 CannotRemoveIceCandidatesWhenPeerConnectionClosed) {
502 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
503
504 auto caller = CreatePeerConnectionWithAudioVideo();
505 auto callee = CreatePeerConnectionWithAudioVideo();
506
507 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
508
509 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
510 auto* audio_content = cricket::GetFirstAudioContent(
511 caller->pc()->local_description()->description());
Steve Anton27ab0e52018-07-23 15:11:53 -0700512 std::unique_ptr<IceCandidateInterface> ice_candidate =
513 CreateIceCandidate(audio_content->name, 0, candidate);
Steve Antonc79268f2018-04-24 09:54:10 -0700514
Steve Anton27ab0e52018-07-23 15:11:53 -0700515 ASSERT_TRUE(caller->pc()->AddIceCandidate(ice_candidate.get()));
Steve Antonc79268f2018-04-24 09:54:10 -0700516
517 caller->pc()->Close();
518
519 EXPECT_FALSE(caller->pc()->RemoveIceCandidates({candidate}));
520}
521
522TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700523 AddRemoveCandidateWithEmptyTransportDoesNotCrash) {
524 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
525
526 auto caller = CreatePeerConnectionWithAudioVideo();
527 auto callee = CreatePeerConnectionWithAudioVideo();
528
529 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
530 ASSERT_TRUE(
531 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
532
533 // |candidate.transport_name()| is empty.
534 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
Steve Anton46d926a2018-01-23 10:23:06 -0800535 auto* audio_content = cricket::GetFirstAudioContent(
536 caller->pc()->local_description()->description());
Steve Anton27ab0e52018-07-23 15:11:53 -0700537 std::unique_ptr<IceCandidateInterface> ice_candidate =
538 CreateIceCandidate(audio_content->name, 0, candidate);
539 EXPECT_TRUE(caller->pc()->AddIceCandidate(ice_candidate.get()));
Steve Antonf1c6db12017-10-13 11:13:35 -0700540 EXPECT_TRUE(caller->pc()->RemoveIceCandidates({candidate}));
541}
542
Steve Anton46d926a2018-01-23 10:23:06 -0800543TEST_P(PeerConnectionIceTest, RemoveCandidateRemovesFromRemoteDescription) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700544 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
545
546 auto caller = CreatePeerConnectionWithAudioVideo();
547 auto callee = CreatePeerConnectionWithAudioVideo();
548
549 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
550 ASSERT_TRUE(
551 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
552
553 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
554 ASSERT_TRUE(caller->AddIceCandidate(&candidate));
555 EXPECT_TRUE(caller->pc()->RemoveIceCandidates({candidate}));
556 EXPECT_EQ(0u, caller->GetIceCandidatesFromRemoteDescription().size());
557}
558
559// Test that if a candidate is added via AddIceCandidate and via an updated
560// remote description, then both candidates appear in the stored remote
561// description.
Steve Anton46d926a2018-01-23 10:23:06 -0800562TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700563 CandidateInSubsequentOfferIsAddedToRemoteDescription) {
564 const SocketAddress kCallerAddress1("1.1.1.1", 1111);
565 const SocketAddress kCallerAddress2("2.2.2.2", 2222);
566
567 auto caller = CreatePeerConnectionWithAudioVideo();
568 auto callee = CreatePeerConnectionWithAudioVideo();
569
570 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
571 ASSERT_TRUE(
572 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
573
574 // Add one candidate via |AddIceCandidate|.
575 cricket::Candidate candidate1 = CreateLocalUdpCandidate(kCallerAddress1);
576 ASSERT_TRUE(callee->AddIceCandidate(&candidate1));
577
578 // Add the second candidate via a reoffer.
579 auto offer = caller->CreateOffer();
580 cricket::Candidate candidate2 = CreateLocalUdpCandidate(kCallerAddress2);
581 AddCandidateToFirstTransport(&candidate2, offer.get());
582
583 // Expect both candidates to appear in the callee's remote description.
584 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
585 EXPECT_EQ(2u, callee->GetIceCandidatesFromRemoteDescription().size());
586}
587
588// The follow test verifies that SetLocal/RemoteDescription fails when an offer
589// has either ICE ufrag/pwd too short or too long and succeeds otherwise.
590// The standard (https://tools.ietf.org/html/rfc5245#section-15.4) says that
591// pwd must be 22-256 characters and ufrag must be 4-256 characters.
Steve Anton46d926a2018-01-23 10:23:06 -0800592TEST_P(PeerConnectionIceTest, VerifyUfragPwdLength) {
Yves Gerey665174f2018-06-19 15:03:05 +0200593 auto set_local_description_with_ufrag_pwd_length = [this](int ufrag_len,
594 int pwd_len) {
595 auto pc = CreatePeerConnectionWithAudioVideo();
596 auto offer = pc->CreateOffer();
597 SetIceUfragPwd(offer.get(), std::string(ufrag_len, 'x'),
598 std::string(pwd_len, 'x'));
599 return pc->SetLocalDescription(std::move(offer));
600 };
Steve Antonf1c6db12017-10-13 11:13:35 -0700601
Yves Gerey665174f2018-06-19 15:03:05 +0200602 auto set_remote_description_with_ufrag_pwd_length = [this](int ufrag_len,
603 int pwd_len) {
604 auto pc = CreatePeerConnectionWithAudioVideo();
605 auto offer = pc->CreateOffer();
606 SetIceUfragPwd(offer.get(), std::string(ufrag_len, 'x'),
607 std::string(pwd_len, 'x'));
608 return pc->SetRemoteDescription(std::move(offer));
609 };
Steve Antonf1c6db12017-10-13 11:13:35 -0700610
611 EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(3, 22));
612 EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(3, 22));
613 EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(257, 22));
614 EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(257, 22));
615 EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(4, 21));
616 EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(4, 21));
617 EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(4, 257));
618 EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(4, 257));
619 EXPECT_TRUE(set_local_description_with_ufrag_pwd_length(4, 22));
620 EXPECT_TRUE(set_remote_description_with_ufrag_pwd_length(4, 22));
621 EXPECT_TRUE(set_local_description_with_ufrag_pwd_length(256, 256));
622 EXPECT_TRUE(set_remote_description_with_ufrag_pwd_length(256, 256));
623}
624
625::testing::AssertionResult AssertIpInCandidates(
626 const char* address_expr,
627 const char* candidates_expr,
628 const SocketAddress& address,
629 const std::vector<IceCandidateInterface*> candidates) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200630 rtc::StringBuilder candidate_hosts;
Steve Antonf1c6db12017-10-13 11:13:35 -0700631 for (const auto* candidate : candidates) {
632 const auto& candidate_ip = candidate->candidate().address().ipaddr();
633 if (candidate_ip == address.ipaddr()) {
634 return ::testing::AssertionSuccess();
635 }
Jonas Olssonabbe8412018-04-03 13:40:05 +0200636 candidate_hosts << "\n" << candidate_ip.ToString();
Steve Antonf1c6db12017-10-13 11:13:35 -0700637 }
638 return ::testing::AssertionFailure()
639 << address_expr << " (host " << address.HostAsURIString()
640 << ") not in " << candidates_expr
641 << " which have the following address hosts:" << candidate_hosts.str();
642}
643
Steve Anton46d926a2018-01-23 10:23:06 -0800644TEST_P(PeerConnectionIceTest, CandidatesGeneratedForEachLocalInterface) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700645 const SocketAddress kLocalAddress1("1.1.1.1", 0);
646 const SocketAddress kLocalAddress2("2.2.2.2", 0);
647
648 auto caller = CreatePeerConnectionWithAudioVideo();
649 caller->network()->AddInterface(kLocalAddress1);
650 caller->network()->AddInterface(kLocalAddress2);
651
652 caller->CreateOfferAndSetAsLocal();
653 EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kIceCandidatesTimeout);
654
655 auto candidates = caller->observer()->GetCandidatesByMline(0);
656 EXPECT_PRED_FORMAT2(AssertIpInCandidates, kLocalAddress1, candidates);
657 EXPECT_PRED_FORMAT2(AssertIpInCandidates, kLocalAddress2, candidates);
658}
659
Steve Anton46d926a2018-01-23 10:23:06 -0800660TEST_P(PeerConnectionIceTest, TrickledSingleCandidateAddedToRemoteDescription) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700661 const SocketAddress kCallerAddress("1.1.1.1", 1111);
662
663 auto caller = CreatePeerConnectionWithAudioVideo();
664 auto callee = CreatePeerConnectionWithAudioVideo();
665
666 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
667
668 cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress);
669 callee->AddIceCandidate(&candidate);
670 auto candidates = callee->GetIceCandidatesFromRemoteDescription();
671 ASSERT_EQ(1u, candidates.size());
672 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate,
673 candidates[0]->candidate());
674}
675
Steve Anton46d926a2018-01-23 10:23:06 -0800676TEST_P(PeerConnectionIceTest, TwoTrickledCandidatesAddedToRemoteDescription) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700677 const SocketAddress kCalleeAddress1("1.1.1.1", 1111);
678 const SocketAddress kCalleeAddress2("2.2.2.2", 2222);
679
680 auto caller = CreatePeerConnectionWithAudioVideo();
681 auto callee = CreatePeerConnectionWithAudioVideo();
682
683 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
684 ASSERT_TRUE(
685 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
686
687 cricket::Candidate candidate1 = CreateLocalUdpCandidate(kCalleeAddress1);
688 caller->AddIceCandidate(&candidate1);
689
690 cricket::Candidate candidate2 = CreateLocalUdpCandidate(kCalleeAddress2);
691 caller->AddIceCandidate(&candidate2);
692
693 auto candidates = caller->GetIceCandidatesFromRemoteDescription();
694 ASSERT_EQ(2u, candidates.size());
695 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate1,
696 candidates[0]->candidate());
697 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate2,
698 candidates[1]->candidate());
699}
700
Henrik Boströmee6f4f62019-11-06 12:36:12 +0100701TEST_P(PeerConnectionIceTest, AsyncAddIceCandidateIsAddedToRemoteDescription) {
702 auto candidate = CreateLocalUdpCandidate(SocketAddress("1.1.1.1", 1111));
703
704 auto caller = CreatePeerConnectionWithAudioVideo();
705 auto callee = CreatePeerConnectionWithAudioVideo();
706
707 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
708
709 auto jsep_candidate =
710 callee->CreateJsepCandidateForFirstTransport(&candidate);
711 bool operation_completed = false;
712 callee->pc()->AddIceCandidate(std::move(jsep_candidate),
713 [&operation_completed](RTCError result) {
714 EXPECT_TRUE(result.ok());
715 operation_completed = true;
716 });
717 EXPECT_TRUE_WAIT(operation_completed, kWaitTimeout);
718
719 auto candidates = callee->GetIceCandidatesFromRemoteDescription();
720 ASSERT_EQ(1u, candidates.size());
721 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate,
722 candidates[0]->candidate());
723}
724
725TEST_P(PeerConnectionIceTest,
726 AsyncAddIceCandidateCompletesImmediatelyIfNoPendingOperation) {
727 auto candidate = CreateLocalUdpCandidate(SocketAddress("1.1.1.1", 1111));
728
729 auto caller = CreatePeerConnectionWithAudioVideo();
730 auto callee = CreatePeerConnectionWithAudioVideo();
731
732 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
733
734 auto jsep_candidate =
735 callee->CreateJsepCandidateForFirstTransport(&candidate);
736 bool operation_completed = false;
737 callee->pc()->AddIceCandidate(
738 std::move(jsep_candidate),
739 [&operation_completed](RTCError result) { operation_completed = true; });
740 EXPECT_TRUE(operation_completed);
741}
742
743TEST_P(PeerConnectionIceTest,
744 AsyncAddIceCandidateCompletesWhenPendingOperationCompletes) {
745 auto candidate = CreateLocalUdpCandidate(SocketAddress("1.1.1.1", 1111));
746
747 auto caller = CreatePeerConnectionWithAudioVideo();
748 auto callee = CreatePeerConnectionWithAudioVideo();
749
750 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
751
752 // Chain an operation that will block AddIceCandidate() from executing.
753 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> answer_observer(
754 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
755 callee->pc()->CreateAnswer(answer_observer, RTCOfferAnswerOptions());
756
757 auto jsep_candidate =
758 callee->CreateJsepCandidateForFirstTransport(&candidate);
759 bool operation_completed = false;
760 callee->pc()->AddIceCandidate(
761 std::move(jsep_candidate),
762 [&operation_completed](RTCError result) { operation_completed = true; });
763 // The operation will not be able to complete until we EXPECT_TRUE_WAIT()
764 // allowing CreateAnswer() to complete.
765 EXPECT_FALSE(operation_completed);
766 EXPECT_TRUE_WAIT(answer_observer->called(), kWaitTimeout);
767 // As soon as it does, AddIceCandidate() will execute without delay, so it
768 // must also have completed.
769 EXPECT_TRUE(operation_completed);
770}
771
772TEST_P(PeerConnectionIceTest,
773 AsyncAddIceCandidateFailsBeforeSetRemoteDescription) {
774 auto candidate = CreateLocalUdpCandidate(SocketAddress("1.1.1.1", 1111));
775
776 auto caller = CreatePeerConnectionWithAudioVideo();
777 std::unique_ptr<IceCandidateInterface> jsep_candidate =
778 CreateIceCandidate(cricket::CN_AUDIO, 0, candidate);
779
780 bool operation_completed = false;
781 caller->pc()->AddIceCandidate(
782 std::move(jsep_candidate), [&operation_completed](RTCError result) {
783 EXPECT_FALSE(result.ok());
784 EXPECT_EQ(result.message(),
785 std::string("Error processing ICE candidate"));
786 operation_completed = true;
787 });
788 EXPECT_TRUE_WAIT(operation_completed, kWaitTimeout);
789}
790
791TEST_P(PeerConnectionIceTest,
792 AsyncAddIceCandidateFailsIfPeerConnectionDestroyed) {
793 auto candidate = CreateLocalUdpCandidate(SocketAddress("1.1.1.1", 1111));
794
795 auto caller = CreatePeerConnectionWithAudioVideo();
796 auto callee = CreatePeerConnectionWithAudioVideo();
797
798 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
799
800 // Chain an operation that will block AddIceCandidate() from executing.
801 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> answer_observer(
802 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
803 callee->pc()->CreateAnswer(answer_observer, RTCOfferAnswerOptions());
804
805 auto jsep_candidate =
806 callee->CreateJsepCandidateForFirstTransport(&candidate);
807 bool operation_completed = false;
808 callee->pc()->AddIceCandidate(
809 std::move(jsep_candidate), [&operation_completed](RTCError result) {
810 EXPECT_FALSE(result.ok());
811 EXPECT_EQ(
812 result.message(),
813 std::string(
814 "AddIceCandidate failed because the session was shut down"));
815 operation_completed = true;
816 });
817 // The operation will not be able to run until EXPECT_TRUE_WAIT(), giving us
818 // time to remove all references to the PeerConnection.
819 EXPECT_FALSE(operation_completed);
820 // This should delete the callee PC.
821 callee = nullptr;
822 EXPECT_TRUE_WAIT(operation_completed, kWaitTimeout);
823}
824
Steve Anton46d926a2018-01-23 10:23:06 -0800825TEST_P(PeerConnectionIceTest, LocalDescriptionUpdatedWhenContinualGathering) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700826 const SocketAddress kLocalAddress("1.1.1.1", 0);
827
828 RTCConfiguration config;
829 config.continual_gathering_policy =
830 PeerConnectionInterface::GATHER_CONTINUALLY;
831 auto caller = CreatePeerConnectionWithAudioVideo(config);
832 caller->network()->AddInterface(kLocalAddress);
833
834 // Start ICE candidate gathering by setting the local offer.
835 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
836
837 // Since we're using continual gathering, we won't get "gathering done".
838 EXPECT_TRUE_WAIT(
839 caller->pc()->local_description()->candidates(0)->count() > 0,
840 kIceCandidatesTimeout);
841}
842
843// Test that when continual gathering is enabled, and a network interface goes
844// down, the candidate is signaled as removed and removed from the local
845// description.
Steve Anton46d926a2018-01-23 10:23:06 -0800846TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700847 LocalCandidatesRemovedWhenNetworkDownIfGatheringContinually) {
848 const SocketAddress kLocalAddress("1.1.1.1", 0);
849
850 RTCConfiguration config;
851 config.continual_gathering_policy =
852 PeerConnectionInterface::GATHER_CONTINUALLY;
853 auto caller = CreatePeerConnectionWithAudioVideo(config);
854 caller->network()->AddInterface(kLocalAddress);
855
856 // Start ICE candidate gathering by setting the local offer.
857 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
858
859 EXPECT_TRUE_WAIT(
860 caller->pc()->local_description()->candidates(0)->count() > 0,
861 kIceCandidatesTimeout);
862
863 // Remove the only network interface, causing the PeerConnection to signal
864 // the removal of all candidates derived from this interface.
865 caller->network()->RemoveInterface(kLocalAddress);
866
867 EXPECT_EQ_WAIT(0u, caller->pc()->local_description()->candidates(0)->count(),
868 kIceCandidatesTimeout);
869 EXPECT_LT(0, caller->observer()->num_candidates_removed_);
870}
871
Steve Anton46d926a2018-01-23 10:23:06 -0800872TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700873 LocalCandidatesNotRemovedWhenNetworkDownIfGatheringOnce) {
874 const SocketAddress kLocalAddress("1.1.1.1", 0);
875
876 RTCConfiguration config;
877 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
878 auto caller = CreatePeerConnectionWithAudioVideo(config);
879 caller->network()->AddInterface(kLocalAddress);
880
881 // Start ICE candidate gathering by setting the local offer.
882 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
883
884 EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kIceCandidatesTimeout);
885
886 caller->network()->RemoveInterface(kLocalAddress);
887
888 // Verify that the local candidates are not removed;
889 rtc::Thread::Current()->ProcessMessages(1000);
890 EXPECT_EQ(0, caller->observer()->num_candidates_removed_);
891}
892
893// The following group tests that when an offer includes a new ufrag or pwd
894// (indicating an ICE restart) the old candidates are removed and new candidates
895// added to the remote description.
896
Steve Anton46d926a2018-01-23 10:23:06 -0800897TEST_P(PeerConnectionIceTest, IceRestartOfferClearsExistingCandidate) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700898 const SocketAddress kCallerAddress("1.1.1.1", 1111);
899
900 auto caller = CreatePeerConnectionWithAudioVideo();
901 auto callee = CreatePeerConnectionWithAudioVideo();
902
Amit Hilbuchae3df542019-01-07 12:13:08 -0800903 auto offer = caller->CreateOfferAndSetAsLocal();
Steve Antonf1c6db12017-10-13 11:13:35 -0700904 cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress);
905 AddCandidateToFirstTransport(&candidate, offer.get());
906
907 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
908
909 RTCOfferAnswerOptions options;
910 options.ice_restart = true;
Amit Hilbuchae3df542019-01-07 12:13:08 -0800911 ASSERT_TRUE(
912 callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal(options)));
Steve Antonf1c6db12017-10-13 11:13:35 -0700913
914 EXPECT_EQ(0u, callee->GetIceCandidatesFromRemoteDescription().size());
915}
Steve Anton46d926a2018-01-23 10:23:06 -0800916TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700917 IceRestartOfferCandidateReplacesExistingCandidate) {
918 const SocketAddress kFirstCallerAddress("1.1.1.1", 1111);
919 const SocketAddress kRestartedCallerAddress("2.2.2.2", 2222);
920
921 auto caller = CreatePeerConnectionWithAudioVideo();
922 auto callee = CreatePeerConnectionWithAudioVideo();
923
Amit Hilbuchae3df542019-01-07 12:13:08 -0800924 auto offer = caller->CreateOfferAndSetAsLocal();
Steve Antonf1c6db12017-10-13 11:13:35 -0700925 cricket::Candidate old_candidate =
926 CreateLocalUdpCandidate(kFirstCallerAddress);
927 AddCandidateToFirstTransport(&old_candidate, offer.get());
928
929 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
930
931 RTCOfferAnswerOptions options;
932 options.ice_restart = true;
Amit Hilbuchae3df542019-01-07 12:13:08 -0800933 auto restart_offer = caller->CreateOfferAndSetAsLocal(options);
Steve Antonf1c6db12017-10-13 11:13:35 -0700934 cricket::Candidate new_candidate =
935 CreateLocalUdpCandidate(kRestartedCallerAddress);
936 AddCandidateToFirstTransport(&new_candidate, restart_offer.get());
937
938 ASSERT_TRUE(callee->SetRemoteDescription(std::move(restart_offer)));
939
940 auto remote_candidates = callee->GetIceCandidatesFromRemoteDescription();
941 ASSERT_EQ(1u, remote_candidates.size());
942 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, new_candidate,
943 remote_candidates[0]->candidate());
944}
945
946// Test that if there is not an ICE restart (i.e., nothing changes), then the
947// answer to a later offer should have the same ufrag/pwd as the first answer.
Steve Anton46d926a2018-01-23 10:23:06 -0800948TEST_P(PeerConnectionIceTest, LaterAnswerHasSameIceCredentialsIfNoIceRestart) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700949 auto caller = CreatePeerConnectionWithAudioVideo();
950 auto callee = CreatePeerConnectionWithAudioVideo();
951
952 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
953 ASSERT_TRUE(
954 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
955
956 // Re-offer.
957 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
958
959 auto answer = callee->CreateAnswer();
960 auto* answer_transport_desc = GetFirstTransportDescription(answer.get());
961 auto* local_transport_desc =
962 GetFirstTransportDescription(callee->pc()->local_description());
963
964 EXPECT_EQ(answer_transport_desc->ice_ufrag, local_transport_desc->ice_ufrag);
965 EXPECT_EQ(answer_transport_desc->ice_pwd, local_transport_desc->ice_pwd);
966}
967
Henrik Boström79b69802019-07-18 11:16:56 +0200968TEST_P(PeerConnectionIceTest, RestartIceGeneratesNewCredentials) {
969 auto caller = CreatePeerConnectionWithAudioVideo();
970 auto callee = CreatePeerConnectionWithAudioVideo();
971
972 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
973 auto initial_ice_credentials =
974 GetIceCredentials(caller->pc()->local_description());
975 caller->pc()->RestartIce();
976 ASSERT_TRUE(caller->CreateOfferAndSetAsLocal());
977 auto restarted_ice_credentials =
978 GetIceCredentials(caller->pc()->local_description());
979 EXPECT_NE(initial_ice_credentials, restarted_ice_credentials);
980}
981
982TEST_P(PeerConnectionIceTest,
983 RestartIceWhileLocalOfferIsPendingGeneratesNewCredentialsInNextOffer) {
984 auto caller = CreatePeerConnectionWithAudioVideo();
985 auto callee = CreatePeerConnectionWithAudioVideo();
986
987 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
988 auto initial_ice_credentials =
989 GetIceCredentials(caller->pc()->local_description());
990 // ICE restart becomes needed while an O/A is pending and |caller| is the
991 // offerer.
992 caller->pc()->RestartIce();
993 ASSERT_TRUE(
994 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
995 ASSERT_TRUE(caller->CreateOfferAndSetAsLocal());
996 auto restarted_ice_credentials =
997 GetIceCredentials(caller->pc()->local_description());
998 EXPECT_NE(initial_ice_credentials, restarted_ice_credentials);
999}
1000
1001TEST_P(PeerConnectionIceTest,
1002 RestartIceWhileRemoteOfferIsPendingGeneratesNewCredentialsInNextOffer) {
1003 auto caller = CreatePeerConnectionWithAudioVideo();
1004 auto callee = CreatePeerConnectionWithAudioVideo();
1005
1006 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1007 auto initial_ice_credentials =
1008 GetIceCredentials(caller->pc()->local_description());
1009 ASSERT_TRUE(caller->SetRemoteDescription(callee->CreateOfferAndSetAsLocal()));
1010 // ICE restart becomes needed while an O/A is pending and |caller| is the
1011 // answerer.
1012 caller->pc()->RestartIce();
1013 ASSERT_TRUE(
1014 callee->SetRemoteDescription(caller->CreateAnswerAndSetAsLocal()));
1015 ASSERT_TRUE(caller->CreateOfferAndSetAsLocal());
1016 auto restarted_ice_credentials =
1017 GetIceCredentials(caller->pc()->local_description());
1018 EXPECT_NE(initial_ice_credentials, restarted_ice_credentials);
1019}
1020
1021TEST_P(PeerConnectionIceTest, RestartIceTriggeredByRemoteSide) {
1022 auto caller = CreatePeerConnectionWithAudioVideo();
1023 auto callee = CreatePeerConnectionWithAudioVideo();
1024
1025 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1026 auto initial_ice_credentials =
1027 GetIceCredentials(caller->pc()->local_description());
1028
1029 // Remote restart and O/A exchange with |caller| as the answerer should
1030 // restart ICE locally as well.
1031 callee->pc()->RestartIce();
1032 ASSERT_TRUE(callee->ExchangeOfferAnswerWith(caller.get()));
1033
1034 auto restarted_ice_credentials =
1035 GetIceCredentials(caller->pc()->local_description());
1036 EXPECT_NE(initial_ice_credentials, restarted_ice_credentials);
1037}
1038
1039TEST_P(PeerConnectionIceTest, RestartIceCausesNegotiationNeeded) {
1040 auto caller = CreatePeerConnectionWithAudioVideo();
1041 auto callee = CreatePeerConnectionWithAudioVideo();
1042
1043 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
Henrik Boströme574a312020-08-25 10:20:11 +02001044 caller->observer()->clear_legacy_renegotiation_needed();
1045 caller->observer()->clear_latest_negotiation_needed_event();
Henrik Boström79b69802019-07-18 11:16:56 +02001046 caller->pc()->RestartIce();
Henrik Boströme574a312020-08-25 10:20:11 +02001047 EXPECT_TRUE(caller->observer()->legacy_renegotiation_needed());
1048 EXPECT_TRUE(caller->observer()->has_negotiation_needed_event());
Henrik Boström79b69802019-07-18 11:16:56 +02001049}
1050
1051// In Unified Plan, "onnegotiationneeded" is spec-compliant, including not
1052// firing multipe times in a row, or firing when returning to the stable
1053// signaling state if negotiation is still needed. In Plan B it fires any time
1054// something changes. As such, some tests are SdpSemantics-specific.
1055class PeerConnectionIceTestUnifiedPlan : public PeerConnectionIceBaseTest {
1056 protected:
1057 PeerConnectionIceTestUnifiedPlan()
1058 : PeerConnectionIceBaseTest(SdpSemantics::kUnifiedPlan) {}
1059};
1060
1061TEST_F(PeerConnectionIceTestUnifiedPlan,
1062 RestartIceWhileLocalOfferIsPendingCausesNegotiationNeededWhenStable) {
1063 auto caller = CreatePeerConnectionWithAudioVideo();
1064 auto callee = CreatePeerConnectionWithAudioVideo();
1065
1066 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
1067 // ICE restart becomes needed while an O/A is pending and |caller| is the
1068 // offerer.
Henrik Boströme574a312020-08-25 10:20:11 +02001069 caller->observer()->clear_legacy_renegotiation_needed();
1070 caller->observer()->clear_latest_negotiation_needed_event();
Henrik Boström79b69802019-07-18 11:16:56 +02001071 caller->pc()->RestartIce();
1072 // In Unified Plan, the event should not fire until we are back in the stable
1073 // signaling state.
Henrik Boströme574a312020-08-25 10:20:11 +02001074 EXPECT_FALSE(caller->observer()->legacy_renegotiation_needed());
1075 EXPECT_FALSE(caller->observer()->has_negotiation_needed_event());
Henrik Boström79b69802019-07-18 11:16:56 +02001076 ASSERT_TRUE(
1077 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
Henrik Boströme574a312020-08-25 10:20:11 +02001078 EXPECT_TRUE(caller->observer()->legacy_renegotiation_needed());
1079 EXPECT_TRUE(caller->observer()->has_negotiation_needed_event());
Henrik Boström79b69802019-07-18 11:16:56 +02001080}
1081
1082TEST_F(PeerConnectionIceTestUnifiedPlan,
1083 RestartIceWhileRemoteOfferIsPendingCausesNegotiationNeededWhenStable) {
1084 auto caller = CreatePeerConnectionWithAudioVideo();
1085 auto callee = CreatePeerConnectionWithAudioVideo();
1086
1087 // Establish initial credentials as the caller.
1088 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1089 ASSERT_TRUE(caller->SetRemoteDescription(callee->CreateOfferAndSetAsLocal()));
1090 // ICE restart becomes needed while an O/A is pending and |caller| is the
1091 // answerer.
Henrik Boströme574a312020-08-25 10:20:11 +02001092 caller->observer()->clear_legacy_renegotiation_needed();
1093 caller->observer()->clear_latest_negotiation_needed_event();
Henrik Boström79b69802019-07-18 11:16:56 +02001094 caller->pc()->RestartIce();
1095 // In Unified Plan, the event should not fire until we are back in the stable
1096 // signaling state.
Henrik Boströme574a312020-08-25 10:20:11 +02001097 EXPECT_FALSE(caller->observer()->legacy_renegotiation_needed());
1098 EXPECT_FALSE(caller->observer()->has_negotiation_needed_event());
Henrik Boström79b69802019-07-18 11:16:56 +02001099 ASSERT_TRUE(
1100 callee->SetRemoteDescription(caller->CreateAnswerAndSetAsLocal()));
Henrik Boströme574a312020-08-25 10:20:11 +02001101 EXPECT_TRUE(caller->observer()->legacy_renegotiation_needed());
1102 EXPECT_TRUE(caller->observer()->has_negotiation_needed_event());
Henrik Boström79b69802019-07-18 11:16:56 +02001103}
1104
1105TEST_F(PeerConnectionIceTestUnifiedPlan,
1106 RestartIceTriggeredByRemoteSideCauseNegotiationNotNeeded) {
1107 auto caller = CreatePeerConnectionWithAudioVideo();
1108 auto callee = CreatePeerConnectionWithAudioVideo();
1109
1110 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1111 // Local restart.
1112 caller->pc()->RestartIce();
Henrik Boströme574a312020-08-25 10:20:11 +02001113 caller->observer()->clear_legacy_renegotiation_needed();
1114 caller->observer()->clear_latest_negotiation_needed_event();
Henrik Boström79b69802019-07-18 11:16:56 +02001115 // Remote restart and O/A exchange with |caller| as the answerer should
1116 // restart ICE locally as well.
1117 callee->pc()->RestartIce();
1118 ASSERT_TRUE(callee->ExchangeOfferAnswerWith(caller.get()));
1119 // Having restarted ICE by the remote offer, we do not need to renegotiate ICE
1120 // credentials when back in the stable signaling state.
Henrik Boströme574a312020-08-25 10:20:11 +02001121 EXPECT_FALSE(caller->observer()->legacy_renegotiation_needed());
1122 EXPECT_FALSE(caller->observer()->has_negotiation_needed_event());
Henrik Boström79b69802019-07-18 11:16:56 +02001123}
1124
1125TEST_F(PeerConnectionIceTestUnifiedPlan,
1126 RestartIceTwiceDoesNotFireNegotiationNeededTwice) {
1127 auto caller = CreatePeerConnectionWithAudioVideo();
1128 auto callee = CreatePeerConnectionWithAudioVideo();
1129
1130 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1131 caller->pc()->RestartIce();
Henrik Boströme574a312020-08-25 10:20:11 +02001132 EXPECT_TRUE(caller->observer()->legacy_renegotiation_needed());
1133 EXPECT_TRUE(caller->observer()->has_negotiation_needed_event());
1134 caller->observer()->clear_legacy_renegotiation_needed();
1135 caller->observer()->clear_latest_negotiation_needed_event();
Henrik Boström79b69802019-07-18 11:16:56 +02001136 caller->pc()->RestartIce();
Henrik Boströme574a312020-08-25 10:20:11 +02001137 EXPECT_FALSE(caller->observer()->legacy_renegotiation_needed());
1138 EXPECT_FALSE(caller->observer()->has_negotiation_needed_event());
Henrik Boström79b69802019-07-18 11:16:56 +02001139}
1140
1141// In Plan B, "onnegotiationneeded" is not spec-compliant, firing based on if
1142// something changed rather than if negotiation is needed. In Unified Plan it
1143// fires according to spec. As such, some tests are SdpSemantics-specific.
1144class PeerConnectionIceTestPlanB : public PeerConnectionIceBaseTest {
1145 protected:
1146 PeerConnectionIceTestPlanB()
1147 : PeerConnectionIceBaseTest(SdpSemantics::kPlanB) {}
1148};
1149
1150TEST_F(PeerConnectionIceTestPlanB,
1151 RestartIceWhileOfferIsPendingCausesNegotiationNeededImmediately) {
1152 auto caller = CreatePeerConnectionWithAudioVideo();
1153 auto callee = CreatePeerConnectionWithAudioVideo();
1154
1155 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
Henrik Boströme574a312020-08-25 10:20:11 +02001156 caller->observer()->clear_legacy_renegotiation_needed();
1157 caller->observer()->clear_latest_negotiation_needed_event();
Henrik Boström79b69802019-07-18 11:16:56 +02001158 caller->pc()->RestartIce();
Henrik Boströme574a312020-08-25 10:20:11 +02001159 EXPECT_TRUE(caller->observer()->legacy_renegotiation_needed());
1160 EXPECT_TRUE(caller->observer()->has_negotiation_needed_event());
1161 caller->observer()->clear_legacy_renegotiation_needed();
1162 caller->observer()->clear_latest_negotiation_needed_event();
Henrik Boström79b69802019-07-18 11:16:56 +02001163 ASSERT_TRUE(
1164 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
1165 // In Plan B, the event fired early so we don't expect it to fire now. This is
1166 // not spec-compliant but follows the pattern of existing Plan B behavior.
Henrik Boströme574a312020-08-25 10:20:11 +02001167 EXPECT_FALSE(caller->observer()->legacy_renegotiation_needed());
1168 EXPECT_FALSE(caller->observer()->has_negotiation_needed_event());
Henrik Boström79b69802019-07-18 11:16:56 +02001169}
1170
1171TEST_F(PeerConnectionIceTestPlanB,
1172 RestartIceTwiceDoesFireNegotiationNeededTwice) {
1173 auto caller = CreatePeerConnectionWithAudioVideo();
1174 auto callee = CreatePeerConnectionWithAudioVideo();
1175
1176 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
Henrik Boströme574a312020-08-25 10:20:11 +02001177 caller->observer()->clear_legacy_renegotiation_needed();
1178 caller->observer()->clear_latest_negotiation_needed_event();
Henrik Boström79b69802019-07-18 11:16:56 +02001179 caller->pc()->RestartIce();
Henrik Boströme574a312020-08-25 10:20:11 +02001180 EXPECT_TRUE(caller->observer()->legacy_renegotiation_needed());
1181 EXPECT_TRUE(caller->observer()->has_negotiation_needed_event());
1182 caller->observer()->clear_legacy_renegotiation_needed();
1183 caller->observer()->clear_latest_negotiation_needed_event();
Henrik Boström79b69802019-07-18 11:16:56 +02001184 caller->pc()->RestartIce();
1185 // In Plan B, the event fires every time something changed, even if we have
1186 // already fired the event. This is not spec-compliant but follows the same
1187 // pattern of existing Plan B behavior.
Henrik Boströme574a312020-08-25 10:20:11 +02001188 EXPECT_TRUE(caller->observer()->legacy_renegotiation_needed());
1189 EXPECT_TRUE(caller->observer()->has_negotiation_needed_event());
Henrik Boström79b69802019-07-18 11:16:56 +02001190}
1191
Steve Antonf1c6db12017-10-13 11:13:35 -07001192// The following parameterized test verifies that if an offer is sent with a
1193// modified ICE ufrag and/or ICE pwd, then the answer should identify that the
1194// other side has initiated an ICE restart and generate a new ufrag and pwd.
1195// RFC 5245 says: "If the offer contained a change in the a=ice-ufrag or
1196// a=ice-pwd attributes compared to the previous SDP from the peer, it
1197// indicates that ICE is restarting for this media stream."
1198
Steve Anton46d926a2018-01-23 10:23:06 -08001199class PeerConnectionIceUfragPwdAnswerTest
1200 : public PeerConnectionIceBaseTest,
1201 public ::testing::WithParamInterface<
1202 std::tuple<SdpSemantics, std::tuple<bool, bool>>> {
Steve Antonf1c6db12017-10-13 11:13:35 -07001203 protected:
Steve Anton46d926a2018-01-23 10:23:06 -08001204 PeerConnectionIceUfragPwdAnswerTest()
1205 : PeerConnectionIceBaseTest(std::get<0>(GetParam())) {
1206 auto param = std::get<1>(GetParam());
1207 offer_new_ufrag_ = std::get<0>(param);
1208 offer_new_pwd_ = std::get<1>(param);
Steve Antonf1c6db12017-10-13 11:13:35 -07001209 }
1210
1211 bool offer_new_ufrag_;
1212 bool offer_new_pwd_;
1213};
1214
Steve Anton46d926a2018-01-23 10:23:06 -08001215TEST_P(PeerConnectionIceUfragPwdAnswerTest, TestIncludedInAnswer) {
Steve Antonf1c6db12017-10-13 11:13:35 -07001216 auto caller = CreatePeerConnectionWithAudioVideo();
1217 auto callee = CreatePeerConnectionWithAudioVideo();
1218
1219 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
1220 ASSERT_TRUE(
1221 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
1222
1223 auto offer = caller->CreateOffer();
1224 auto* offer_transport_desc = GetFirstTransportDescription(offer.get());
1225 if (offer_new_ufrag_) {
Steve Anton71ff0732020-01-24 16:28:15 -08001226 offer_transport_desc->ice_ufrag += "+new";
Steve Antonf1c6db12017-10-13 11:13:35 -07001227 }
1228 if (offer_new_pwd_) {
Steve Anton71ff0732020-01-24 16:28:15 -08001229 offer_transport_desc->ice_pwd += "+new";
Steve Antonf1c6db12017-10-13 11:13:35 -07001230 }
1231
1232 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
1233
1234 auto answer = callee->CreateAnswer();
1235 auto* answer_transport_desc = GetFirstTransportDescription(answer.get());
1236 auto* local_transport_desc =
1237 GetFirstTransportDescription(callee->pc()->local_description());
1238
1239 EXPECT_NE(answer_transport_desc->ice_ufrag, local_transport_desc->ice_ufrag);
1240 EXPECT_NE(answer_transport_desc->ice_pwd, local_transport_desc->ice_pwd);
1241}
1242
Mirko Bonadeic84f6612019-01-31 12:20:57 +01001243INSTANTIATE_TEST_SUITE_P(
Steve Anton46d926a2018-01-23 10:23:06 -08001244 PeerConnectionIceTest,
1245 PeerConnectionIceUfragPwdAnswerTest,
1246 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
1247 Values(std::make_pair(true, true), // Both changed.
1248 std::make_pair(true, false), // Only ufrag changed.
1249 std::make_pair(false, true)))); // Only pwd changed.
Steve Antonf1c6db12017-10-13 11:13:35 -07001250
1251// Test that if an ICE restart is offered on one media section, then the answer
1252// will only change ICE ufrag/pwd for that section and keep the other sections
1253// the same.
1254// Note that this only works if we have disabled BUNDLE, otherwise all media
1255// sections will share the same transport.
Steve Anton46d926a2018-01-23 10:23:06 -08001256TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -07001257 CreateAnswerHasNewUfragPwdForOnlyMediaSectionWhichRestarted) {
1258 auto caller = CreatePeerConnectionWithAudioVideo();
1259 auto callee = CreatePeerConnectionWithAudioVideo();
1260
1261 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
1262 ASSERT_TRUE(
1263 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
1264
1265 RTCOfferAnswerOptions disable_bundle_options;
1266 disable_bundle_options.use_rtp_mux = false;
1267
1268 auto offer = caller->CreateOffer(disable_bundle_options);
1269
1270 // Signal ICE restart on the first media section.
1271 auto* offer_transport_desc = GetFirstTransportDescription(offer.get());
Steve Anton71ff0732020-01-24 16:28:15 -08001272 offer_transport_desc->ice_ufrag += "+new";
1273 offer_transport_desc->ice_pwd += "+new";
Steve Antonf1c6db12017-10-13 11:13:35 -07001274
1275 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
1276
1277 auto answer = callee->CreateAnswer(disable_bundle_options);
1278 const auto& answer_transports = answer->description()->transport_infos();
1279 const auto& local_transports =
1280 callee->pc()->local_description()->description()->transport_infos();
1281
1282 EXPECT_NE(answer_transports[0].description.ice_ufrag,
1283 local_transports[0].description.ice_ufrag);
1284 EXPECT_NE(answer_transports[0].description.ice_pwd,
1285 local_transports[0].description.ice_pwd);
1286 EXPECT_EQ(answer_transports[1].description.ice_ufrag,
1287 local_transports[1].description.ice_ufrag);
1288 EXPECT_EQ(answer_transports[1].description.ice_pwd,
1289 local_transports[1].description.ice_pwd);
1290}
1291
Qingsi Wange1692722017-11-29 13:27:20 -08001292// Test that when the initial offerer (caller) uses the lite implementation of
1293// ICE and the callee uses the full implementation, the caller takes the
1294// CONTROLLED role and the callee takes the CONTROLLING role. This is specified
1295// in RFC5245 Section 5.1.1.
Steve Anton46d926a2018-01-23 10:23:06 -08001296TEST_P(PeerConnectionIceTest,
Qingsi Wange1692722017-11-29 13:27:20 -08001297 OfferFromLiteIceControlledAndAnswerFromFullIceControlling) {
1298 auto caller = CreatePeerConnectionWithAudioVideo();
1299 auto callee = CreatePeerConnectionWithAudioVideo();
1300
1301 auto offer = caller->CreateOffer();
1302 SetIceMode(offer.get(), cricket::IceMode::ICEMODE_LITE);
1303 ASSERT_TRUE(
1304 caller->SetLocalDescription(CloneSessionDescription(offer.get())));
1305 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
1306
1307 auto answer = callee->CreateAnswer();
1308 SetIceMode(answer.get(), cricket::IceMode::ICEMODE_FULL);
1309 ASSERT_TRUE(
1310 callee->SetLocalDescription(CloneSessionDescription(answer.get())));
1311 ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer)));
1312
1313 EXPECT_EQ(cricket::ICEROLE_CONTROLLED, GetIceRole(caller));
1314 EXPECT_EQ(cricket::ICEROLE_CONTROLLING, GetIceRole(callee));
1315}
1316
1317// Test that when the caller and the callee both use the lite implementation of
1318// ICE, the initial offerer (caller) takes the CONTROLLING role and the callee
1319// takes the CONTROLLED role. This is specified in RFC5245 Section 5.1.1.
Steve Anton46d926a2018-01-23 10:23:06 -08001320TEST_P(PeerConnectionIceTest,
Qingsi Wange1692722017-11-29 13:27:20 -08001321 OfferFromLiteIceControllingAndAnswerFromLiteIceControlled) {
1322 auto caller = CreatePeerConnectionWithAudioVideo();
1323 auto callee = CreatePeerConnectionWithAudioVideo();
1324
1325 auto offer = caller->CreateOffer();
1326 SetIceMode(offer.get(), cricket::IceMode::ICEMODE_LITE);
1327 ASSERT_TRUE(
1328 caller->SetLocalDescription(CloneSessionDescription(offer.get())));
1329 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
1330
1331 auto answer = callee->CreateAnswer();
1332 SetIceMode(answer.get(), cricket::IceMode::ICEMODE_LITE);
1333 ASSERT_TRUE(
1334 callee->SetLocalDescription(CloneSessionDescription(answer.get())));
1335 ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer)));
1336
1337 EXPECT_EQ(cricket::ICEROLE_CONTROLLING, GetIceRole(caller));
1338 EXPECT_EQ(cricket::ICEROLE_CONTROLLED, GetIceRole(callee));
1339}
1340
Mirko Bonadeic84f6612019-01-31 12:20:57 +01001341INSTANTIATE_TEST_SUITE_P(PeerConnectionIceTest,
1342 PeerConnectionIceTest,
1343 Values(SdpSemantics::kPlanB,
1344 SdpSemantics::kUnifiedPlan));
Steve Anton46d926a2018-01-23 10:23:06 -08001345
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001346class PeerConnectionIceConfigTest : public ::testing::Test {
Qingsi Wang4ff54432018-03-01 18:25:20 -08001347 protected:
1348 void SetUp() override {
1349 pc_factory_ = CreatePeerConnectionFactory(
1350 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
1351 FakeAudioCaptureModule::Create(), CreateBuiltinAudioEncoderFactory(),
Anders Carlsson67537952018-05-03 11:28:29 +02001352 CreateBuiltinAudioDecoderFactory(), CreateBuiltinVideoEncoderFactory(),
1353 CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
1354 nullptr /* audio_processing */);
Qingsi Wang4ff54432018-03-01 18:25:20 -08001355 }
1356 void CreatePeerConnection(const RTCConfiguration& config) {
1357 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1358 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1359 port_allocator_ = port_allocator.get();
1360 rtc::scoped_refptr<PeerConnectionInterface> pc(
Niels Möllerf06f9232018-08-07 12:32:18 +02001361 pc_factory_->CreatePeerConnection(config, std::move(port_allocator),
1362 nullptr /* cert_generator */,
1363 &observer_));
Qingsi Wang4ff54432018-03-01 18:25:20 -08001364 EXPECT_TRUE(pc.get());
Mirko Bonadei1c546052019-02-04 14:50:38 +01001365 pc_ = std::move(pc);
Qingsi Wang4ff54432018-03-01 18:25:20 -08001366 }
1367
1368 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_ = nullptr;
1369 rtc::scoped_refptr<PeerConnectionInterface> pc_ = nullptr;
1370 cricket::FakePortAllocator* port_allocator_ = nullptr;
1371
1372 MockPeerConnectionObserver observer_;
1373};
1374
1375TEST_F(PeerConnectionIceConfigTest, SetStunCandidateKeepaliveInterval) {
1376 RTCConfiguration config;
1377 config.stun_candidate_keepalive_interval = 123;
1378 config.ice_candidate_pool_size = 1;
1379 CreatePeerConnection(config);
1380 ASSERT_NE(port_allocator_, nullptr);
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02001381 absl::optional<int> actual_stun_keepalive_interval =
Qingsi Wang4ff54432018-03-01 18:25:20 -08001382 port_allocator_->stun_candidate_keepalive_interval();
1383 EXPECT_EQ(actual_stun_keepalive_interval.value_or(-1), 123);
1384 config.stun_candidate_keepalive_interval = 321;
Niels Möller2579f0c2019-08-19 09:58:17 +02001385 ASSERT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wang4ff54432018-03-01 18:25:20 -08001386 actual_stun_keepalive_interval =
1387 port_allocator_->stun_candidate_keepalive_interval();
1388 EXPECT_EQ(actual_stun_keepalive_interval.value_or(-1), 321);
1389}
1390
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001391TEST_P(PeerConnectionIceTest, IceCredentialsCreateOffer) {
1392 RTCConfiguration config;
1393 config.ice_candidate_pool_size = 1;
1394 auto pc = CreatePeerConnectionWithAudioVideo(config);
1395 ASSERT_NE(pc->port_allocator_, nullptr);
1396 auto offer = pc->CreateOffer();
1397 auto credentials = pc->port_allocator_->GetPooledIceCredentials();
1398 ASSERT_EQ(1u, credentials.size());
1399
1400 auto* desc = offer->description();
1401 for (const auto& content : desc->contents()) {
1402 auto* transport_info = desc->GetTransportInfoByName(content.name);
1403 EXPECT_EQ(transport_info->description.ice_ufrag, credentials[0].ufrag);
1404 EXPECT_EQ(transport_info->description.ice_pwd, credentials[0].pwd);
1405 }
1406}
1407
1408TEST_P(PeerConnectionIceTest, IceCredentialsCreateAnswer) {
1409 RTCConfiguration config;
1410 config.ice_candidate_pool_size = 1;
1411 auto pc = CreatePeerConnectionWithAudioVideo(config);
1412 ASSERT_NE(pc->port_allocator_, nullptr);
1413 auto offer = pc->CreateOffer();
1414 ASSERT_TRUE(pc->SetRemoteDescription(std::move(offer)));
1415 auto answer = pc->CreateAnswer();
1416
1417 auto credentials = pc->port_allocator_->GetPooledIceCredentials();
1418 ASSERT_EQ(1u, credentials.size());
1419
1420 auto* desc = answer->description();
1421 for (const auto& content : desc->contents()) {
1422 auto* transport_info = desc->GetTransportInfoByName(content.name);
1423 EXPECT_EQ(transport_info->description.ice_ufrag, credentials[0].ufrag);
1424 EXPECT_EQ(transport_info->description.ice_pwd, credentials[0].pwd);
1425 }
1426}
1427
Steve Antonec47b572020-01-24 14:53:37 -08001428// Regression test for https://bugs.chromium.org/p/webrtc/issues/detail?id=4728
1429TEST_P(PeerConnectionIceTest, CloseDoesNotTransitionGatheringStateToComplete) {
1430 auto pc = CreatePeerConnectionWithAudioVideo();
1431 pc->pc()->Close();
1432 EXPECT_FALSE(pc->IsIceGatheringDone());
1433 EXPECT_EQ(PeerConnectionInterface::kIceGatheringNew,
1434 pc->pc()->ice_gathering_state());
1435}
1436
Steve Antonf1c6db12017-10-13 11:13:35 -07001437} // namespace webrtc