blob: f37943f8e44ef151e48820a0a302265d3e5a683a [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
Tomas Gunnarsson8cb97062021-02-08 18:57:04 +0100500TEST_P(PeerConnectionIceTest, ErrorOnInvalidRemoteIceCandidateAdded) {
501 auto caller = CreatePeerConnectionWithAudioVideo();
502 auto callee = CreatePeerConnectionWithAudioVideo();
503 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
504 // Add a candidate to the remote description with a candidate that has an
505 // invalid address (port number == 2).
506 auto answer = callee->CreateAnswerAndSetAsLocal();
507 cricket::Candidate bad_candidate =
508 CreateLocalUdpCandidate(SocketAddress("2.2.2.2", 2));
509 RTC_LOG(LS_INFO) << "Bad candidate: " << bad_candidate.ToString();
510 AddCandidateToFirstTransport(&bad_candidate, answer.get());
511 // Now the call to SetRemoteDescription should fail.
512 EXPECT_FALSE(caller->SetRemoteDescription(std::move(answer)));
513}
514
Steve Anton46d926a2018-01-23 10:23:06 -0800515TEST_P(PeerConnectionIceTest,
Steve Antonc79268f2018-04-24 09:54:10 -0700516 CannotRemoveIceCandidatesWhenPeerConnectionClosed) {
517 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
518
519 auto caller = CreatePeerConnectionWithAudioVideo();
520 auto callee = CreatePeerConnectionWithAudioVideo();
521
522 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
523
524 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
525 auto* audio_content = cricket::GetFirstAudioContent(
526 caller->pc()->local_description()->description());
Steve Anton27ab0e52018-07-23 15:11:53 -0700527 std::unique_ptr<IceCandidateInterface> ice_candidate =
528 CreateIceCandidate(audio_content->name, 0, candidate);
Steve Antonc79268f2018-04-24 09:54:10 -0700529
Steve Anton27ab0e52018-07-23 15:11:53 -0700530 ASSERT_TRUE(caller->pc()->AddIceCandidate(ice_candidate.get()));
Steve Antonc79268f2018-04-24 09:54:10 -0700531
532 caller->pc()->Close();
533
534 EXPECT_FALSE(caller->pc()->RemoveIceCandidates({candidate}));
535}
536
537TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700538 AddRemoveCandidateWithEmptyTransportDoesNotCrash) {
539 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
540
541 auto caller = CreatePeerConnectionWithAudioVideo();
542 auto callee = CreatePeerConnectionWithAudioVideo();
543
544 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
545 ASSERT_TRUE(
546 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
547
548 // |candidate.transport_name()| is empty.
549 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
Steve Anton46d926a2018-01-23 10:23:06 -0800550 auto* audio_content = cricket::GetFirstAudioContent(
551 caller->pc()->local_description()->description());
Steve Anton27ab0e52018-07-23 15:11:53 -0700552 std::unique_ptr<IceCandidateInterface> ice_candidate =
553 CreateIceCandidate(audio_content->name, 0, candidate);
554 EXPECT_TRUE(caller->pc()->AddIceCandidate(ice_candidate.get()));
Steve Antonf1c6db12017-10-13 11:13:35 -0700555 EXPECT_TRUE(caller->pc()->RemoveIceCandidates({candidate}));
556}
557
Steve Anton46d926a2018-01-23 10:23:06 -0800558TEST_P(PeerConnectionIceTest, RemoveCandidateRemovesFromRemoteDescription) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700559 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
560
561 auto caller = CreatePeerConnectionWithAudioVideo();
562 auto callee = CreatePeerConnectionWithAudioVideo();
563
564 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
565 ASSERT_TRUE(
566 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
567
568 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
569 ASSERT_TRUE(caller->AddIceCandidate(&candidate));
570 EXPECT_TRUE(caller->pc()->RemoveIceCandidates({candidate}));
571 EXPECT_EQ(0u, caller->GetIceCandidatesFromRemoteDescription().size());
572}
573
574// Test that if a candidate is added via AddIceCandidate and via an updated
575// remote description, then both candidates appear in the stored remote
576// description.
Steve Anton46d926a2018-01-23 10:23:06 -0800577TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700578 CandidateInSubsequentOfferIsAddedToRemoteDescription) {
579 const SocketAddress kCallerAddress1("1.1.1.1", 1111);
580 const SocketAddress kCallerAddress2("2.2.2.2", 2222);
581
582 auto caller = CreatePeerConnectionWithAudioVideo();
583 auto callee = CreatePeerConnectionWithAudioVideo();
584
585 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
586 ASSERT_TRUE(
587 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
588
589 // Add one candidate via |AddIceCandidate|.
590 cricket::Candidate candidate1 = CreateLocalUdpCandidate(kCallerAddress1);
591 ASSERT_TRUE(callee->AddIceCandidate(&candidate1));
592
593 // Add the second candidate via a reoffer.
594 auto offer = caller->CreateOffer();
595 cricket::Candidate candidate2 = CreateLocalUdpCandidate(kCallerAddress2);
596 AddCandidateToFirstTransport(&candidate2, offer.get());
597
598 // Expect both candidates to appear in the callee's remote description.
599 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
600 EXPECT_EQ(2u, callee->GetIceCandidatesFromRemoteDescription().size());
601}
602
603// The follow test verifies that SetLocal/RemoteDescription fails when an offer
604// has either ICE ufrag/pwd too short or too long and succeeds otherwise.
605// The standard (https://tools.ietf.org/html/rfc5245#section-15.4) says that
606// pwd must be 22-256 characters and ufrag must be 4-256 characters.
Steve Anton46d926a2018-01-23 10:23:06 -0800607TEST_P(PeerConnectionIceTest, VerifyUfragPwdLength) {
Yves Gerey665174f2018-06-19 15:03:05 +0200608 auto set_local_description_with_ufrag_pwd_length = [this](int ufrag_len,
609 int pwd_len) {
610 auto pc = CreatePeerConnectionWithAudioVideo();
611 auto offer = pc->CreateOffer();
612 SetIceUfragPwd(offer.get(), std::string(ufrag_len, 'x'),
613 std::string(pwd_len, 'x'));
614 return pc->SetLocalDescription(std::move(offer));
615 };
Steve Antonf1c6db12017-10-13 11:13:35 -0700616
Yves Gerey665174f2018-06-19 15:03:05 +0200617 auto set_remote_description_with_ufrag_pwd_length = [this](int ufrag_len,
618 int pwd_len) {
619 auto pc = CreatePeerConnectionWithAudioVideo();
620 auto offer = pc->CreateOffer();
621 SetIceUfragPwd(offer.get(), std::string(ufrag_len, 'x'),
622 std::string(pwd_len, 'x'));
623 return pc->SetRemoteDescription(std::move(offer));
624 };
Steve Antonf1c6db12017-10-13 11:13:35 -0700625
626 EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(3, 22));
627 EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(3, 22));
628 EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(257, 22));
629 EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(257, 22));
630 EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(4, 21));
631 EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(4, 21));
632 EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(4, 257));
633 EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(4, 257));
634 EXPECT_TRUE(set_local_description_with_ufrag_pwd_length(4, 22));
635 EXPECT_TRUE(set_remote_description_with_ufrag_pwd_length(4, 22));
636 EXPECT_TRUE(set_local_description_with_ufrag_pwd_length(256, 256));
637 EXPECT_TRUE(set_remote_description_with_ufrag_pwd_length(256, 256));
638}
639
640::testing::AssertionResult AssertIpInCandidates(
641 const char* address_expr,
642 const char* candidates_expr,
643 const SocketAddress& address,
644 const std::vector<IceCandidateInterface*> candidates) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200645 rtc::StringBuilder candidate_hosts;
Steve Antonf1c6db12017-10-13 11:13:35 -0700646 for (const auto* candidate : candidates) {
647 const auto& candidate_ip = candidate->candidate().address().ipaddr();
648 if (candidate_ip == address.ipaddr()) {
649 return ::testing::AssertionSuccess();
650 }
Jonas Olssonabbe8412018-04-03 13:40:05 +0200651 candidate_hosts << "\n" << candidate_ip.ToString();
Steve Antonf1c6db12017-10-13 11:13:35 -0700652 }
653 return ::testing::AssertionFailure()
654 << address_expr << " (host " << address.HostAsURIString()
655 << ") not in " << candidates_expr
656 << " which have the following address hosts:" << candidate_hosts.str();
657}
658
Steve Anton46d926a2018-01-23 10:23:06 -0800659TEST_P(PeerConnectionIceTest, CandidatesGeneratedForEachLocalInterface) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700660 const SocketAddress kLocalAddress1("1.1.1.1", 0);
661 const SocketAddress kLocalAddress2("2.2.2.2", 0);
662
663 auto caller = CreatePeerConnectionWithAudioVideo();
664 caller->network()->AddInterface(kLocalAddress1);
665 caller->network()->AddInterface(kLocalAddress2);
666
667 caller->CreateOfferAndSetAsLocal();
668 EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kIceCandidatesTimeout);
669
670 auto candidates = caller->observer()->GetCandidatesByMline(0);
671 EXPECT_PRED_FORMAT2(AssertIpInCandidates, kLocalAddress1, candidates);
672 EXPECT_PRED_FORMAT2(AssertIpInCandidates, kLocalAddress2, candidates);
673}
674
Steve Anton46d926a2018-01-23 10:23:06 -0800675TEST_P(PeerConnectionIceTest, TrickledSingleCandidateAddedToRemoteDescription) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700676 const SocketAddress kCallerAddress("1.1.1.1", 1111);
677
678 auto caller = CreatePeerConnectionWithAudioVideo();
679 auto callee = CreatePeerConnectionWithAudioVideo();
680
681 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
682
683 cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress);
684 callee->AddIceCandidate(&candidate);
685 auto candidates = callee->GetIceCandidatesFromRemoteDescription();
686 ASSERT_EQ(1u, candidates.size());
687 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate,
688 candidates[0]->candidate());
689}
690
Steve Anton46d926a2018-01-23 10:23:06 -0800691TEST_P(PeerConnectionIceTest, TwoTrickledCandidatesAddedToRemoteDescription) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700692 const SocketAddress kCalleeAddress1("1.1.1.1", 1111);
693 const SocketAddress kCalleeAddress2("2.2.2.2", 2222);
694
695 auto caller = CreatePeerConnectionWithAudioVideo();
696 auto callee = CreatePeerConnectionWithAudioVideo();
697
698 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
699 ASSERT_TRUE(
700 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
701
702 cricket::Candidate candidate1 = CreateLocalUdpCandidate(kCalleeAddress1);
703 caller->AddIceCandidate(&candidate1);
704
705 cricket::Candidate candidate2 = CreateLocalUdpCandidate(kCalleeAddress2);
706 caller->AddIceCandidate(&candidate2);
707
708 auto candidates = caller->GetIceCandidatesFromRemoteDescription();
709 ASSERT_EQ(2u, candidates.size());
710 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate1,
711 candidates[0]->candidate());
712 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate2,
713 candidates[1]->candidate());
714}
715
Henrik Boströmee6f4f62019-11-06 12:36:12 +0100716TEST_P(PeerConnectionIceTest, AsyncAddIceCandidateIsAddedToRemoteDescription) {
717 auto candidate = CreateLocalUdpCandidate(SocketAddress("1.1.1.1", 1111));
718
719 auto caller = CreatePeerConnectionWithAudioVideo();
720 auto callee = CreatePeerConnectionWithAudioVideo();
721
722 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
723
724 auto jsep_candidate =
725 callee->CreateJsepCandidateForFirstTransport(&candidate);
726 bool operation_completed = false;
727 callee->pc()->AddIceCandidate(std::move(jsep_candidate),
728 [&operation_completed](RTCError result) {
729 EXPECT_TRUE(result.ok());
730 operation_completed = true;
731 });
732 EXPECT_TRUE_WAIT(operation_completed, kWaitTimeout);
733
734 auto candidates = callee->GetIceCandidatesFromRemoteDescription();
735 ASSERT_EQ(1u, candidates.size());
736 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate,
737 candidates[0]->candidate());
738}
739
740TEST_P(PeerConnectionIceTest,
741 AsyncAddIceCandidateCompletesImmediatelyIfNoPendingOperation) {
742 auto candidate = CreateLocalUdpCandidate(SocketAddress("1.1.1.1", 1111));
743
744 auto caller = CreatePeerConnectionWithAudioVideo();
745 auto callee = CreatePeerConnectionWithAudioVideo();
746
747 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
748
749 auto jsep_candidate =
750 callee->CreateJsepCandidateForFirstTransport(&candidate);
751 bool operation_completed = false;
752 callee->pc()->AddIceCandidate(
753 std::move(jsep_candidate),
754 [&operation_completed](RTCError result) { operation_completed = true; });
755 EXPECT_TRUE(operation_completed);
756}
757
758TEST_P(PeerConnectionIceTest,
759 AsyncAddIceCandidateCompletesWhenPendingOperationCompletes) {
760 auto candidate = CreateLocalUdpCandidate(SocketAddress("1.1.1.1", 1111));
761
762 auto caller = CreatePeerConnectionWithAudioVideo();
763 auto callee = CreatePeerConnectionWithAudioVideo();
764
765 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
766
767 // Chain an operation that will block AddIceCandidate() from executing.
768 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> answer_observer(
769 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
770 callee->pc()->CreateAnswer(answer_observer, RTCOfferAnswerOptions());
771
772 auto jsep_candidate =
773 callee->CreateJsepCandidateForFirstTransport(&candidate);
774 bool operation_completed = false;
775 callee->pc()->AddIceCandidate(
776 std::move(jsep_candidate),
777 [&operation_completed](RTCError result) { operation_completed = true; });
778 // The operation will not be able to complete until we EXPECT_TRUE_WAIT()
779 // allowing CreateAnswer() to complete.
780 EXPECT_FALSE(operation_completed);
781 EXPECT_TRUE_WAIT(answer_observer->called(), kWaitTimeout);
782 // As soon as it does, AddIceCandidate() will execute without delay, so it
783 // must also have completed.
784 EXPECT_TRUE(operation_completed);
785}
786
787TEST_P(PeerConnectionIceTest,
788 AsyncAddIceCandidateFailsBeforeSetRemoteDescription) {
789 auto candidate = CreateLocalUdpCandidate(SocketAddress("1.1.1.1", 1111));
790
791 auto caller = CreatePeerConnectionWithAudioVideo();
792 std::unique_ptr<IceCandidateInterface> jsep_candidate =
793 CreateIceCandidate(cricket::CN_AUDIO, 0, candidate);
794
795 bool operation_completed = false;
796 caller->pc()->AddIceCandidate(
797 std::move(jsep_candidate), [&operation_completed](RTCError result) {
798 EXPECT_FALSE(result.ok());
799 EXPECT_EQ(result.message(),
800 std::string("Error processing ICE candidate"));
801 operation_completed = true;
802 });
803 EXPECT_TRUE_WAIT(operation_completed, kWaitTimeout);
804}
805
806TEST_P(PeerConnectionIceTest,
807 AsyncAddIceCandidateFailsIfPeerConnectionDestroyed) {
808 auto candidate = CreateLocalUdpCandidate(SocketAddress("1.1.1.1", 1111));
809
810 auto caller = CreatePeerConnectionWithAudioVideo();
811 auto callee = CreatePeerConnectionWithAudioVideo();
812
813 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
814
815 // Chain an operation that will block AddIceCandidate() from executing.
816 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> answer_observer(
817 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
818 callee->pc()->CreateAnswer(answer_observer, RTCOfferAnswerOptions());
819
820 auto jsep_candidate =
821 callee->CreateJsepCandidateForFirstTransport(&candidate);
822 bool operation_completed = false;
823 callee->pc()->AddIceCandidate(
824 std::move(jsep_candidate), [&operation_completed](RTCError result) {
825 EXPECT_FALSE(result.ok());
826 EXPECT_EQ(
827 result.message(),
828 std::string(
829 "AddIceCandidate failed because the session was shut down"));
830 operation_completed = true;
831 });
832 // The operation will not be able to run until EXPECT_TRUE_WAIT(), giving us
833 // time to remove all references to the PeerConnection.
834 EXPECT_FALSE(operation_completed);
835 // This should delete the callee PC.
836 callee = nullptr;
837 EXPECT_TRUE_WAIT(operation_completed, kWaitTimeout);
838}
839
Steve Anton46d926a2018-01-23 10:23:06 -0800840TEST_P(PeerConnectionIceTest, LocalDescriptionUpdatedWhenContinualGathering) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700841 const SocketAddress kLocalAddress("1.1.1.1", 0);
842
843 RTCConfiguration config;
844 config.continual_gathering_policy =
845 PeerConnectionInterface::GATHER_CONTINUALLY;
846 auto caller = CreatePeerConnectionWithAudioVideo(config);
847 caller->network()->AddInterface(kLocalAddress);
848
849 // Start ICE candidate gathering by setting the local offer.
850 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
851
852 // Since we're using continual gathering, we won't get "gathering done".
853 EXPECT_TRUE_WAIT(
854 caller->pc()->local_description()->candidates(0)->count() > 0,
855 kIceCandidatesTimeout);
856}
857
858// Test that when continual gathering is enabled, and a network interface goes
859// down, the candidate is signaled as removed and removed from the local
860// description.
Steve Anton46d926a2018-01-23 10:23:06 -0800861TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700862 LocalCandidatesRemovedWhenNetworkDownIfGatheringContinually) {
863 const SocketAddress kLocalAddress("1.1.1.1", 0);
864
865 RTCConfiguration config;
866 config.continual_gathering_policy =
867 PeerConnectionInterface::GATHER_CONTINUALLY;
868 auto caller = CreatePeerConnectionWithAudioVideo(config);
869 caller->network()->AddInterface(kLocalAddress);
870
871 // Start ICE candidate gathering by setting the local offer.
872 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
873
874 EXPECT_TRUE_WAIT(
875 caller->pc()->local_description()->candidates(0)->count() > 0,
876 kIceCandidatesTimeout);
877
878 // Remove the only network interface, causing the PeerConnection to signal
879 // the removal of all candidates derived from this interface.
880 caller->network()->RemoveInterface(kLocalAddress);
881
882 EXPECT_EQ_WAIT(0u, caller->pc()->local_description()->candidates(0)->count(),
883 kIceCandidatesTimeout);
884 EXPECT_LT(0, caller->observer()->num_candidates_removed_);
885}
886
Steve Anton46d926a2018-01-23 10:23:06 -0800887TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700888 LocalCandidatesNotRemovedWhenNetworkDownIfGatheringOnce) {
889 const SocketAddress kLocalAddress("1.1.1.1", 0);
890
891 RTCConfiguration config;
892 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
893 auto caller = CreatePeerConnectionWithAudioVideo(config);
894 caller->network()->AddInterface(kLocalAddress);
895
896 // Start ICE candidate gathering by setting the local offer.
897 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
898
899 EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kIceCandidatesTimeout);
900
901 caller->network()->RemoveInterface(kLocalAddress);
902
903 // Verify that the local candidates are not removed;
904 rtc::Thread::Current()->ProcessMessages(1000);
905 EXPECT_EQ(0, caller->observer()->num_candidates_removed_);
906}
907
908// The following group tests that when an offer includes a new ufrag or pwd
909// (indicating an ICE restart) the old candidates are removed and new candidates
910// added to the remote description.
911
Steve Anton46d926a2018-01-23 10:23:06 -0800912TEST_P(PeerConnectionIceTest, IceRestartOfferClearsExistingCandidate) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700913 const SocketAddress kCallerAddress("1.1.1.1", 1111);
914
915 auto caller = CreatePeerConnectionWithAudioVideo();
916 auto callee = CreatePeerConnectionWithAudioVideo();
917
Amit Hilbuchae3df542019-01-07 12:13:08 -0800918 auto offer = caller->CreateOfferAndSetAsLocal();
Steve Antonf1c6db12017-10-13 11:13:35 -0700919 cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress);
920 AddCandidateToFirstTransport(&candidate, offer.get());
921
922 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
923
924 RTCOfferAnswerOptions options;
925 options.ice_restart = true;
Amit Hilbuchae3df542019-01-07 12:13:08 -0800926 ASSERT_TRUE(
927 callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal(options)));
Steve Antonf1c6db12017-10-13 11:13:35 -0700928
929 EXPECT_EQ(0u, callee->GetIceCandidatesFromRemoteDescription().size());
930}
Steve Anton46d926a2018-01-23 10:23:06 -0800931TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700932 IceRestartOfferCandidateReplacesExistingCandidate) {
933 const SocketAddress kFirstCallerAddress("1.1.1.1", 1111);
934 const SocketAddress kRestartedCallerAddress("2.2.2.2", 2222);
935
936 auto caller = CreatePeerConnectionWithAudioVideo();
937 auto callee = CreatePeerConnectionWithAudioVideo();
938
Amit Hilbuchae3df542019-01-07 12:13:08 -0800939 auto offer = caller->CreateOfferAndSetAsLocal();
Steve Antonf1c6db12017-10-13 11:13:35 -0700940 cricket::Candidate old_candidate =
941 CreateLocalUdpCandidate(kFirstCallerAddress);
942 AddCandidateToFirstTransport(&old_candidate, offer.get());
943
944 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
945
946 RTCOfferAnswerOptions options;
947 options.ice_restart = true;
Amit Hilbuchae3df542019-01-07 12:13:08 -0800948 auto restart_offer = caller->CreateOfferAndSetAsLocal(options);
Steve Antonf1c6db12017-10-13 11:13:35 -0700949 cricket::Candidate new_candidate =
950 CreateLocalUdpCandidate(kRestartedCallerAddress);
951 AddCandidateToFirstTransport(&new_candidate, restart_offer.get());
952
953 ASSERT_TRUE(callee->SetRemoteDescription(std::move(restart_offer)));
954
955 auto remote_candidates = callee->GetIceCandidatesFromRemoteDescription();
956 ASSERT_EQ(1u, remote_candidates.size());
957 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, new_candidate,
958 remote_candidates[0]->candidate());
959}
960
961// Test that if there is not an ICE restart (i.e., nothing changes), then the
962// answer to a later offer should have the same ufrag/pwd as the first answer.
Steve Anton46d926a2018-01-23 10:23:06 -0800963TEST_P(PeerConnectionIceTest, LaterAnswerHasSameIceCredentialsIfNoIceRestart) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700964 auto caller = CreatePeerConnectionWithAudioVideo();
965 auto callee = CreatePeerConnectionWithAudioVideo();
966
967 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
968 ASSERT_TRUE(
969 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
970
971 // Re-offer.
972 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
973
974 auto answer = callee->CreateAnswer();
975 auto* answer_transport_desc = GetFirstTransportDescription(answer.get());
976 auto* local_transport_desc =
977 GetFirstTransportDescription(callee->pc()->local_description());
978
979 EXPECT_EQ(answer_transport_desc->ice_ufrag, local_transport_desc->ice_ufrag);
980 EXPECT_EQ(answer_transport_desc->ice_pwd, local_transport_desc->ice_pwd);
981}
982
Henrik Boström79b69802019-07-18 11:16:56 +0200983TEST_P(PeerConnectionIceTest, RestartIceGeneratesNewCredentials) {
984 auto caller = CreatePeerConnectionWithAudioVideo();
985 auto callee = CreatePeerConnectionWithAudioVideo();
986
987 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
988 auto initial_ice_credentials =
989 GetIceCredentials(caller->pc()->local_description());
990 caller->pc()->RestartIce();
991 ASSERT_TRUE(caller->CreateOfferAndSetAsLocal());
992 auto restarted_ice_credentials =
993 GetIceCredentials(caller->pc()->local_description());
994 EXPECT_NE(initial_ice_credentials, restarted_ice_credentials);
995}
996
997TEST_P(PeerConnectionIceTest,
998 RestartIceWhileLocalOfferIsPendingGeneratesNewCredentialsInNextOffer) {
999 auto caller = CreatePeerConnectionWithAudioVideo();
1000 auto callee = CreatePeerConnectionWithAudioVideo();
1001
1002 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
1003 auto initial_ice_credentials =
1004 GetIceCredentials(caller->pc()->local_description());
1005 // ICE restart becomes needed while an O/A is pending and |caller| is the
1006 // offerer.
1007 caller->pc()->RestartIce();
1008 ASSERT_TRUE(
1009 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
1010 ASSERT_TRUE(caller->CreateOfferAndSetAsLocal());
1011 auto restarted_ice_credentials =
1012 GetIceCredentials(caller->pc()->local_description());
1013 EXPECT_NE(initial_ice_credentials, restarted_ice_credentials);
1014}
1015
1016TEST_P(PeerConnectionIceTest,
1017 RestartIceWhileRemoteOfferIsPendingGeneratesNewCredentialsInNextOffer) {
1018 auto caller = CreatePeerConnectionWithAudioVideo();
1019 auto callee = CreatePeerConnectionWithAudioVideo();
1020
1021 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1022 auto initial_ice_credentials =
1023 GetIceCredentials(caller->pc()->local_description());
1024 ASSERT_TRUE(caller->SetRemoteDescription(callee->CreateOfferAndSetAsLocal()));
1025 // ICE restart becomes needed while an O/A is pending and |caller| is the
1026 // answerer.
1027 caller->pc()->RestartIce();
1028 ASSERT_TRUE(
1029 callee->SetRemoteDescription(caller->CreateAnswerAndSetAsLocal()));
1030 ASSERT_TRUE(caller->CreateOfferAndSetAsLocal());
1031 auto restarted_ice_credentials =
1032 GetIceCredentials(caller->pc()->local_description());
1033 EXPECT_NE(initial_ice_credentials, restarted_ice_credentials);
1034}
1035
1036TEST_P(PeerConnectionIceTest, RestartIceTriggeredByRemoteSide) {
1037 auto caller = CreatePeerConnectionWithAudioVideo();
1038 auto callee = CreatePeerConnectionWithAudioVideo();
1039
1040 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1041 auto initial_ice_credentials =
1042 GetIceCredentials(caller->pc()->local_description());
1043
1044 // Remote restart and O/A exchange with |caller| as the answerer should
1045 // restart ICE locally as well.
1046 callee->pc()->RestartIce();
1047 ASSERT_TRUE(callee->ExchangeOfferAnswerWith(caller.get()));
1048
1049 auto restarted_ice_credentials =
1050 GetIceCredentials(caller->pc()->local_description());
1051 EXPECT_NE(initial_ice_credentials, restarted_ice_credentials);
1052}
1053
1054TEST_P(PeerConnectionIceTest, RestartIceCausesNegotiationNeeded) {
1055 auto caller = CreatePeerConnectionWithAudioVideo();
1056 auto callee = CreatePeerConnectionWithAudioVideo();
1057
1058 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
Henrik Boströme574a312020-08-25 10:20:11 +02001059 caller->observer()->clear_legacy_renegotiation_needed();
1060 caller->observer()->clear_latest_negotiation_needed_event();
Henrik Boström79b69802019-07-18 11:16:56 +02001061 caller->pc()->RestartIce();
Henrik Boströme574a312020-08-25 10:20:11 +02001062 EXPECT_TRUE(caller->observer()->legacy_renegotiation_needed());
1063 EXPECT_TRUE(caller->observer()->has_negotiation_needed_event());
Henrik Boström79b69802019-07-18 11:16:56 +02001064}
1065
1066// In Unified Plan, "onnegotiationneeded" is spec-compliant, including not
1067// firing multipe times in a row, or firing when returning to the stable
1068// signaling state if negotiation is still needed. In Plan B it fires any time
1069// something changes. As such, some tests are SdpSemantics-specific.
1070class PeerConnectionIceTestUnifiedPlan : public PeerConnectionIceBaseTest {
1071 protected:
1072 PeerConnectionIceTestUnifiedPlan()
1073 : PeerConnectionIceBaseTest(SdpSemantics::kUnifiedPlan) {}
1074};
1075
1076TEST_F(PeerConnectionIceTestUnifiedPlan,
1077 RestartIceWhileLocalOfferIsPendingCausesNegotiationNeededWhenStable) {
1078 auto caller = CreatePeerConnectionWithAudioVideo();
1079 auto callee = CreatePeerConnectionWithAudioVideo();
1080
1081 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
1082 // ICE restart becomes needed while an O/A is pending and |caller| is the
1083 // offerer.
Henrik Boströme574a312020-08-25 10:20:11 +02001084 caller->observer()->clear_legacy_renegotiation_needed();
1085 caller->observer()->clear_latest_negotiation_needed_event();
Henrik Boström79b69802019-07-18 11:16:56 +02001086 caller->pc()->RestartIce();
1087 // In Unified Plan, the event should not fire until we are back in the stable
1088 // signaling state.
Henrik Boströme574a312020-08-25 10:20:11 +02001089 EXPECT_FALSE(caller->observer()->legacy_renegotiation_needed());
1090 EXPECT_FALSE(caller->observer()->has_negotiation_needed_event());
Henrik Boström79b69802019-07-18 11:16:56 +02001091 ASSERT_TRUE(
1092 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
Henrik Boströme574a312020-08-25 10:20:11 +02001093 EXPECT_TRUE(caller->observer()->legacy_renegotiation_needed());
1094 EXPECT_TRUE(caller->observer()->has_negotiation_needed_event());
Henrik Boström79b69802019-07-18 11:16:56 +02001095}
1096
1097TEST_F(PeerConnectionIceTestUnifiedPlan,
1098 RestartIceWhileRemoteOfferIsPendingCausesNegotiationNeededWhenStable) {
1099 auto caller = CreatePeerConnectionWithAudioVideo();
1100 auto callee = CreatePeerConnectionWithAudioVideo();
1101
1102 // Establish initial credentials as the caller.
1103 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1104 ASSERT_TRUE(caller->SetRemoteDescription(callee->CreateOfferAndSetAsLocal()));
1105 // ICE restart becomes needed while an O/A is pending and |caller| is the
1106 // answerer.
Henrik Boströme574a312020-08-25 10:20:11 +02001107 caller->observer()->clear_legacy_renegotiation_needed();
1108 caller->observer()->clear_latest_negotiation_needed_event();
Henrik Boström79b69802019-07-18 11:16:56 +02001109 caller->pc()->RestartIce();
1110 // In Unified Plan, the event should not fire until we are back in the stable
1111 // signaling state.
Henrik Boströme574a312020-08-25 10:20:11 +02001112 EXPECT_FALSE(caller->observer()->legacy_renegotiation_needed());
1113 EXPECT_FALSE(caller->observer()->has_negotiation_needed_event());
Henrik Boström79b69802019-07-18 11:16:56 +02001114 ASSERT_TRUE(
1115 callee->SetRemoteDescription(caller->CreateAnswerAndSetAsLocal()));
Henrik Boströme574a312020-08-25 10:20:11 +02001116 EXPECT_TRUE(caller->observer()->legacy_renegotiation_needed());
1117 EXPECT_TRUE(caller->observer()->has_negotiation_needed_event());
Henrik Boström79b69802019-07-18 11:16:56 +02001118}
1119
1120TEST_F(PeerConnectionIceTestUnifiedPlan,
1121 RestartIceTriggeredByRemoteSideCauseNegotiationNotNeeded) {
1122 auto caller = CreatePeerConnectionWithAudioVideo();
1123 auto callee = CreatePeerConnectionWithAudioVideo();
1124
1125 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1126 // Local restart.
1127 caller->pc()->RestartIce();
Henrik Boströme574a312020-08-25 10:20:11 +02001128 caller->observer()->clear_legacy_renegotiation_needed();
1129 caller->observer()->clear_latest_negotiation_needed_event();
Henrik Boström79b69802019-07-18 11:16:56 +02001130 // Remote restart and O/A exchange with |caller| as the answerer should
1131 // restart ICE locally as well.
1132 callee->pc()->RestartIce();
1133 ASSERT_TRUE(callee->ExchangeOfferAnswerWith(caller.get()));
1134 // Having restarted ICE by the remote offer, we do not need to renegotiate ICE
1135 // credentials when back in the stable signaling state.
Henrik Boströme574a312020-08-25 10:20:11 +02001136 EXPECT_FALSE(caller->observer()->legacy_renegotiation_needed());
1137 EXPECT_FALSE(caller->observer()->has_negotiation_needed_event());
Henrik Boström79b69802019-07-18 11:16:56 +02001138}
1139
1140TEST_F(PeerConnectionIceTestUnifiedPlan,
1141 RestartIceTwiceDoesNotFireNegotiationNeededTwice) {
1142 auto caller = CreatePeerConnectionWithAudioVideo();
1143 auto callee = CreatePeerConnectionWithAudioVideo();
1144
1145 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
1146 caller->pc()->RestartIce();
Henrik Boströme574a312020-08-25 10:20:11 +02001147 EXPECT_TRUE(caller->observer()->legacy_renegotiation_needed());
1148 EXPECT_TRUE(caller->observer()->has_negotiation_needed_event());
1149 caller->observer()->clear_legacy_renegotiation_needed();
1150 caller->observer()->clear_latest_negotiation_needed_event();
Henrik Boström79b69802019-07-18 11:16:56 +02001151 caller->pc()->RestartIce();
Henrik Boströme574a312020-08-25 10:20:11 +02001152 EXPECT_FALSE(caller->observer()->legacy_renegotiation_needed());
1153 EXPECT_FALSE(caller->observer()->has_negotiation_needed_event());
Henrik Boström79b69802019-07-18 11:16:56 +02001154}
1155
1156// In Plan B, "onnegotiationneeded" is not spec-compliant, firing based on if
1157// something changed rather than if negotiation is needed. In Unified Plan it
1158// fires according to spec. As such, some tests are SdpSemantics-specific.
1159class PeerConnectionIceTestPlanB : public PeerConnectionIceBaseTest {
1160 protected:
1161 PeerConnectionIceTestPlanB()
1162 : PeerConnectionIceBaseTest(SdpSemantics::kPlanB) {}
1163};
1164
1165TEST_F(PeerConnectionIceTestPlanB,
1166 RestartIceWhileOfferIsPendingCausesNegotiationNeededImmediately) {
1167 auto caller = CreatePeerConnectionWithAudioVideo();
1168 auto callee = CreatePeerConnectionWithAudioVideo();
1169
1170 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
Henrik Boströme574a312020-08-25 10:20:11 +02001171 caller->observer()->clear_legacy_renegotiation_needed();
1172 caller->observer()->clear_latest_negotiation_needed_event();
Henrik Boström79b69802019-07-18 11:16:56 +02001173 caller->pc()->RestartIce();
Henrik Boströme574a312020-08-25 10:20:11 +02001174 EXPECT_TRUE(caller->observer()->legacy_renegotiation_needed());
1175 EXPECT_TRUE(caller->observer()->has_negotiation_needed_event());
1176 caller->observer()->clear_legacy_renegotiation_needed();
1177 caller->observer()->clear_latest_negotiation_needed_event();
Henrik Boström79b69802019-07-18 11:16:56 +02001178 ASSERT_TRUE(
1179 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
1180 // In Plan B, the event fired early so we don't expect it to fire now. This is
1181 // not spec-compliant but follows the pattern of existing Plan B behavior.
Henrik Boströme574a312020-08-25 10:20:11 +02001182 EXPECT_FALSE(caller->observer()->legacy_renegotiation_needed());
1183 EXPECT_FALSE(caller->observer()->has_negotiation_needed_event());
Henrik Boström79b69802019-07-18 11:16:56 +02001184}
1185
1186TEST_F(PeerConnectionIceTestPlanB,
1187 RestartIceTwiceDoesFireNegotiationNeededTwice) {
1188 auto caller = CreatePeerConnectionWithAudioVideo();
1189 auto callee = CreatePeerConnectionWithAudioVideo();
1190
1191 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
Henrik Boströme574a312020-08-25 10:20:11 +02001192 caller->observer()->clear_legacy_renegotiation_needed();
1193 caller->observer()->clear_latest_negotiation_needed_event();
Henrik Boström79b69802019-07-18 11:16:56 +02001194 caller->pc()->RestartIce();
Henrik Boströme574a312020-08-25 10:20:11 +02001195 EXPECT_TRUE(caller->observer()->legacy_renegotiation_needed());
1196 EXPECT_TRUE(caller->observer()->has_negotiation_needed_event());
1197 caller->observer()->clear_legacy_renegotiation_needed();
1198 caller->observer()->clear_latest_negotiation_needed_event();
Henrik Boström79b69802019-07-18 11:16:56 +02001199 caller->pc()->RestartIce();
1200 // In Plan B, the event fires every time something changed, even if we have
1201 // already fired the event. This is not spec-compliant but follows the same
1202 // pattern of existing Plan B behavior.
Henrik Boströme574a312020-08-25 10:20:11 +02001203 EXPECT_TRUE(caller->observer()->legacy_renegotiation_needed());
1204 EXPECT_TRUE(caller->observer()->has_negotiation_needed_event());
Henrik Boström79b69802019-07-18 11:16:56 +02001205}
1206
Steve Antonf1c6db12017-10-13 11:13:35 -07001207// The following parameterized test verifies that if an offer is sent with a
1208// modified ICE ufrag and/or ICE pwd, then the answer should identify that the
1209// other side has initiated an ICE restart and generate a new ufrag and pwd.
1210// RFC 5245 says: "If the offer contained a change in the a=ice-ufrag or
1211// a=ice-pwd attributes compared to the previous SDP from the peer, it
1212// indicates that ICE is restarting for this media stream."
1213
Steve Anton46d926a2018-01-23 10:23:06 -08001214class PeerConnectionIceUfragPwdAnswerTest
1215 : public PeerConnectionIceBaseTest,
1216 public ::testing::WithParamInterface<
1217 std::tuple<SdpSemantics, std::tuple<bool, bool>>> {
Steve Antonf1c6db12017-10-13 11:13:35 -07001218 protected:
Steve Anton46d926a2018-01-23 10:23:06 -08001219 PeerConnectionIceUfragPwdAnswerTest()
1220 : PeerConnectionIceBaseTest(std::get<0>(GetParam())) {
1221 auto param = std::get<1>(GetParam());
1222 offer_new_ufrag_ = std::get<0>(param);
1223 offer_new_pwd_ = std::get<1>(param);
Steve Antonf1c6db12017-10-13 11:13:35 -07001224 }
1225
1226 bool offer_new_ufrag_;
1227 bool offer_new_pwd_;
1228};
1229
Steve Anton46d926a2018-01-23 10:23:06 -08001230TEST_P(PeerConnectionIceUfragPwdAnswerTest, TestIncludedInAnswer) {
Steve Antonf1c6db12017-10-13 11:13:35 -07001231 auto caller = CreatePeerConnectionWithAudioVideo();
1232 auto callee = CreatePeerConnectionWithAudioVideo();
1233
1234 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
1235 ASSERT_TRUE(
1236 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
1237
1238 auto offer = caller->CreateOffer();
1239 auto* offer_transport_desc = GetFirstTransportDescription(offer.get());
1240 if (offer_new_ufrag_) {
Steve Anton71ff0732020-01-24 16:28:15 -08001241 offer_transport_desc->ice_ufrag += "+new";
Steve Antonf1c6db12017-10-13 11:13:35 -07001242 }
1243 if (offer_new_pwd_) {
Steve Anton71ff0732020-01-24 16:28:15 -08001244 offer_transport_desc->ice_pwd += "+new";
Steve Antonf1c6db12017-10-13 11:13:35 -07001245 }
1246
1247 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
1248
1249 auto answer = callee->CreateAnswer();
1250 auto* answer_transport_desc = GetFirstTransportDescription(answer.get());
1251 auto* local_transport_desc =
1252 GetFirstTransportDescription(callee->pc()->local_description());
1253
1254 EXPECT_NE(answer_transport_desc->ice_ufrag, local_transport_desc->ice_ufrag);
1255 EXPECT_NE(answer_transport_desc->ice_pwd, local_transport_desc->ice_pwd);
1256}
1257
Mirko Bonadeic84f6612019-01-31 12:20:57 +01001258INSTANTIATE_TEST_SUITE_P(
Steve Anton46d926a2018-01-23 10:23:06 -08001259 PeerConnectionIceTest,
1260 PeerConnectionIceUfragPwdAnswerTest,
1261 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
1262 Values(std::make_pair(true, true), // Both changed.
1263 std::make_pair(true, false), // Only ufrag changed.
1264 std::make_pair(false, true)))); // Only pwd changed.
Steve Antonf1c6db12017-10-13 11:13:35 -07001265
1266// Test that if an ICE restart is offered on one media section, then the answer
1267// will only change ICE ufrag/pwd for that section and keep the other sections
1268// the same.
1269// Note that this only works if we have disabled BUNDLE, otherwise all media
1270// sections will share the same transport.
Steve Anton46d926a2018-01-23 10:23:06 -08001271TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -07001272 CreateAnswerHasNewUfragPwdForOnlyMediaSectionWhichRestarted) {
1273 auto caller = CreatePeerConnectionWithAudioVideo();
1274 auto callee = CreatePeerConnectionWithAudioVideo();
1275
1276 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
1277 ASSERT_TRUE(
1278 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
1279
1280 RTCOfferAnswerOptions disable_bundle_options;
1281 disable_bundle_options.use_rtp_mux = false;
1282
1283 auto offer = caller->CreateOffer(disable_bundle_options);
1284
1285 // Signal ICE restart on the first media section.
1286 auto* offer_transport_desc = GetFirstTransportDescription(offer.get());
Steve Anton71ff0732020-01-24 16:28:15 -08001287 offer_transport_desc->ice_ufrag += "+new";
1288 offer_transport_desc->ice_pwd += "+new";
Steve Antonf1c6db12017-10-13 11:13:35 -07001289
1290 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
1291
1292 auto answer = callee->CreateAnswer(disable_bundle_options);
1293 const auto& answer_transports = answer->description()->transport_infos();
1294 const auto& local_transports =
1295 callee->pc()->local_description()->description()->transport_infos();
1296
1297 EXPECT_NE(answer_transports[0].description.ice_ufrag,
1298 local_transports[0].description.ice_ufrag);
1299 EXPECT_NE(answer_transports[0].description.ice_pwd,
1300 local_transports[0].description.ice_pwd);
1301 EXPECT_EQ(answer_transports[1].description.ice_ufrag,
1302 local_transports[1].description.ice_ufrag);
1303 EXPECT_EQ(answer_transports[1].description.ice_pwd,
1304 local_transports[1].description.ice_pwd);
1305}
1306
Qingsi Wange1692722017-11-29 13:27:20 -08001307// Test that when the initial offerer (caller) uses the lite implementation of
1308// ICE and the callee uses the full implementation, the caller takes the
1309// CONTROLLED role and the callee takes the CONTROLLING role. This is specified
1310// in RFC5245 Section 5.1.1.
Steve Anton46d926a2018-01-23 10:23:06 -08001311TEST_P(PeerConnectionIceTest,
Qingsi Wange1692722017-11-29 13:27:20 -08001312 OfferFromLiteIceControlledAndAnswerFromFullIceControlling) {
1313 auto caller = CreatePeerConnectionWithAudioVideo();
1314 auto callee = CreatePeerConnectionWithAudioVideo();
1315
1316 auto offer = caller->CreateOffer();
1317 SetIceMode(offer.get(), cricket::IceMode::ICEMODE_LITE);
1318 ASSERT_TRUE(
1319 caller->SetLocalDescription(CloneSessionDescription(offer.get())));
1320 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
1321
1322 auto answer = callee->CreateAnswer();
1323 SetIceMode(answer.get(), cricket::IceMode::ICEMODE_FULL);
1324 ASSERT_TRUE(
1325 callee->SetLocalDescription(CloneSessionDescription(answer.get())));
1326 ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer)));
1327
1328 EXPECT_EQ(cricket::ICEROLE_CONTROLLED, GetIceRole(caller));
1329 EXPECT_EQ(cricket::ICEROLE_CONTROLLING, GetIceRole(callee));
1330}
1331
1332// Test that when the caller and the callee both use the lite implementation of
1333// ICE, the initial offerer (caller) takes the CONTROLLING role and the callee
1334// takes the CONTROLLED role. This is specified in RFC5245 Section 5.1.1.
Steve Anton46d926a2018-01-23 10:23:06 -08001335TEST_P(PeerConnectionIceTest,
Qingsi Wange1692722017-11-29 13:27:20 -08001336 OfferFromLiteIceControllingAndAnswerFromLiteIceControlled) {
1337 auto caller = CreatePeerConnectionWithAudioVideo();
1338 auto callee = CreatePeerConnectionWithAudioVideo();
1339
1340 auto offer = caller->CreateOffer();
1341 SetIceMode(offer.get(), cricket::IceMode::ICEMODE_LITE);
1342 ASSERT_TRUE(
1343 caller->SetLocalDescription(CloneSessionDescription(offer.get())));
1344 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
1345
1346 auto answer = callee->CreateAnswer();
1347 SetIceMode(answer.get(), cricket::IceMode::ICEMODE_LITE);
1348 ASSERT_TRUE(
1349 callee->SetLocalDescription(CloneSessionDescription(answer.get())));
1350 ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer)));
1351
1352 EXPECT_EQ(cricket::ICEROLE_CONTROLLING, GetIceRole(caller));
1353 EXPECT_EQ(cricket::ICEROLE_CONTROLLED, GetIceRole(callee));
1354}
1355
Mirko Bonadeic84f6612019-01-31 12:20:57 +01001356INSTANTIATE_TEST_SUITE_P(PeerConnectionIceTest,
1357 PeerConnectionIceTest,
1358 Values(SdpSemantics::kPlanB,
1359 SdpSemantics::kUnifiedPlan));
Steve Anton46d926a2018-01-23 10:23:06 -08001360
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001361class PeerConnectionIceConfigTest : public ::testing::Test {
Qingsi Wang4ff54432018-03-01 18:25:20 -08001362 protected:
1363 void SetUp() override {
1364 pc_factory_ = CreatePeerConnectionFactory(
1365 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
1366 FakeAudioCaptureModule::Create(), CreateBuiltinAudioEncoderFactory(),
Anders Carlsson67537952018-05-03 11:28:29 +02001367 CreateBuiltinAudioDecoderFactory(), CreateBuiltinVideoEncoderFactory(),
1368 CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
1369 nullptr /* audio_processing */);
Qingsi Wang4ff54432018-03-01 18:25:20 -08001370 }
1371 void CreatePeerConnection(const RTCConfiguration& config) {
1372 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1373 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1374 port_allocator_ = port_allocator.get();
1375 rtc::scoped_refptr<PeerConnectionInterface> pc(
Niels Möllerf06f9232018-08-07 12:32:18 +02001376 pc_factory_->CreatePeerConnection(config, std::move(port_allocator),
1377 nullptr /* cert_generator */,
1378 &observer_));
Qingsi Wang4ff54432018-03-01 18:25:20 -08001379 EXPECT_TRUE(pc.get());
Mirko Bonadei1c546052019-02-04 14:50:38 +01001380 pc_ = std::move(pc);
Qingsi Wang4ff54432018-03-01 18:25:20 -08001381 }
1382
1383 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_ = nullptr;
1384 rtc::scoped_refptr<PeerConnectionInterface> pc_ = nullptr;
1385 cricket::FakePortAllocator* port_allocator_ = nullptr;
1386
1387 MockPeerConnectionObserver observer_;
1388};
1389
1390TEST_F(PeerConnectionIceConfigTest, SetStunCandidateKeepaliveInterval) {
1391 RTCConfiguration config;
1392 config.stun_candidate_keepalive_interval = 123;
1393 config.ice_candidate_pool_size = 1;
1394 CreatePeerConnection(config);
1395 ASSERT_NE(port_allocator_, nullptr);
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02001396 absl::optional<int> actual_stun_keepalive_interval =
Qingsi Wang4ff54432018-03-01 18:25:20 -08001397 port_allocator_->stun_candidate_keepalive_interval();
1398 EXPECT_EQ(actual_stun_keepalive_interval.value_or(-1), 123);
1399 config.stun_candidate_keepalive_interval = 321;
Niels Möller2579f0c2019-08-19 09:58:17 +02001400 ASSERT_TRUE(pc_->SetConfiguration(config).ok());
Qingsi Wang4ff54432018-03-01 18:25:20 -08001401 actual_stun_keepalive_interval =
1402 port_allocator_->stun_candidate_keepalive_interval();
1403 EXPECT_EQ(actual_stun_keepalive_interval.value_or(-1), 321);
1404}
1405
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001406TEST_P(PeerConnectionIceTest, IceCredentialsCreateOffer) {
1407 RTCConfiguration config;
1408 config.ice_candidate_pool_size = 1;
1409 auto pc = CreatePeerConnectionWithAudioVideo(config);
1410 ASSERT_NE(pc->port_allocator_, nullptr);
1411 auto offer = pc->CreateOffer();
1412 auto credentials = pc->port_allocator_->GetPooledIceCredentials();
1413 ASSERT_EQ(1u, credentials.size());
1414
1415 auto* desc = offer->description();
1416 for (const auto& content : desc->contents()) {
1417 auto* transport_info = desc->GetTransportInfoByName(content.name);
1418 EXPECT_EQ(transport_info->description.ice_ufrag, credentials[0].ufrag);
1419 EXPECT_EQ(transport_info->description.ice_pwd, credentials[0].pwd);
1420 }
1421}
1422
1423TEST_P(PeerConnectionIceTest, IceCredentialsCreateAnswer) {
1424 RTCConfiguration config;
1425 config.ice_candidate_pool_size = 1;
1426 auto pc = CreatePeerConnectionWithAudioVideo(config);
1427 ASSERT_NE(pc->port_allocator_, nullptr);
1428 auto offer = pc->CreateOffer();
1429 ASSERT_TRUE(pc->SetRemoteDescription(std::move(offer)));
1430 auto answer = pc->CreateAnswer();
1431
1432 auto credentials = pc->port_allocator_->GetPooledIceCredentials();
1433 ASSERT_EQ(1u, credentials.size());
1434
1435 auto* desc = answer->description();
1436 for (const auto& content : desc->contents()) {
1437 auto* transport_info = desc->GetTransportInfoByName(content.name);
1438 EXPECT_EQ(transport_info->description.ice_ufrag, credentials[0].ufrag);
1439 EXPECT_EQ(transport_info->description.ice_pwd, credentials[0].pwd);
1440 }
1441}
1442
Steve Antonec47b572020-01-24 14:53:37 -08001443// Regression test for https://bugs.chromium.org/p/webrtc/issues/detail?id=4728
1444TEST_P(PeerConnectionIceTest, CloseDoesNotTransitionGatheringStateToComplete) {
1445 auto pc = CreatePeerConnectionWithAudioVideo();
1446 pc->pc()->Close();
1447 EXPECT_FALSE(pc->IsIceGatheringDone());
1448 EXPECT_EQ(PeerConnectionInterface::kIceGatheringNew,
1449 pc->pc()->ice_gathering_state());
1450}
1451
Steve Antonf1c6db12017-10-13 11:13:35 -07001452} // namespace webrtc