blob: 8fde8751fd89e37ec667899d1bce7ed40e8fdf05 [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
11#include "p2p/base/fakeportallocator.h"
12#include "p2p/base/teststunserver.h"
13#include "p2p/client/basicportallocator.h"
14#include "pc/mediasession.h"
Qingsi Wange1692722017-11-29 13:27:20 -080015#include "pc/peerconnection.h"
Steve Antonf1c6db12017-10-13 11:13:35 -070016#include "pc/peerconnectionwrapper.h"
17#include "pc/sdputils.h"
18#ifdef WEBRTC_ANDROID
19#include "pc/test/androidtestinitializer.h"
20#endif
Karl Wiberg918f50c2018-07-05 11:40:33 +020021#include "absl/memory/memory.h"
Karl Wiberg1b0eae32017-10-17 14:48:54 +020022#include "api/audio_codecs/builtin_audio_decoder_factory.h"
23#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Qingsi Wange1692722017-11-29 13:27:20 -080024#include "api/peerconnectionproxy.h"
Yves Gerey2e00abc2018-10-05 15:39:24 +020025#include "api/umametrics.h"
Anders Carlsson67537952018-05-03 11:28:29 +020026#include "api/video_codecs/builtin_video_decoder_factory.h"
27#include "api/video_codecs/builtin_video_encoder_factory.h"
Steve Antonf1c6db12017-10-13 11:13:35 -070028#include "pc/test/fakeaudiocapturemodule.h"
29#include "rtc_base/fakenetwork.h"
30#include "rtc_base/gunit.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020031#include "rtc_base/strings/string_builder.h"
Steve Antonf1c6db12017-10-13 11:13:35 -070032#include "rtc_base/virtualsocketserver.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020033#include "system_wrappers/include/metrics.h"
Steve Antonf1c6db12017-10-13 11:13:35 -070034
35namespace webrtc {
36
37using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
38using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
39using rtc::SocketAddress;
Steve Anton46d926a2018-01-23 10:23:06 -080040using ::testing::Combine;
Steve Antonf1c6db12017-10-13 11:13:35 -070041using ::testing::Values;
42
43constexpr int kIceCandidatesTimeout = 10000;
44
Steve Anton46d926a2018-01-23 10:23:06 -080045class PeerConnectionWrapperForIceTest : public PeerConnectionWrapper {
Steve Antonf1c6db12017-10-13 11:13:35 -070046 public:
47 using PeerConnectionWrapper::PeerConnectionWrapper;
48
49 // Adds a new ICE candidate to the first transport.
50 bool AddIceCandidate(cricket::Candidate* candidate) {
51 RTC_DCHECK(pc()->remote_description());
52 const auto* desc = pc()->remote_description()->description();
53 RTC_DCHECK(desc->contents().size() > 0);
54 const auto& first_content = desc->contents()[0];
55 candidate->set_transport_name(first_content.name);
Steve Anton27ab0e52018-07-23 15:11:53 -070056 std::unique_ptr<IceCandidateInterface> jsep_candidate =
57 CreateIceCandidate(first_content.name, 0, *candidate);
58 return pc()->AddIceCandidate(jsep_candidate.get());
Steve Antonf1c6db12017-10-13 11:13:35 -070059 }
60
61 // Returns ICE candidates from the remote session description.
62 std::vector<const IceCandidateInterface*>
63 GetIceCandidatesFromRemoteDescription() {
64 const SessionDescriptionInterface* sdesc = pc()->remote_description();
65 RTC_DCHECK(sdesc);
66 std::vector<const IceCandidateInterface*> candidates;
67 for (size_t mline_index = 0; mline_index < sdesc->number_of_mediasections();
68 mline_index++) {
69 const auto* candidate_collection = sdesc->candidates(mline_index);
70 for (size_t i = 0; i < candidate_collection->count(); i++) {
71 candidates.push_back(candidate_collection->at(i));
72 }
73 }
74 return candidates;
75 }
76
77 rtc::FakeNetworkManager* network() { return network_; }
78
79 void set_network(rtc::FakeNetworkManager* network) { network_ = network; }
80
Jonas Oreland1cd39fa2018-10-11 07:47:12 +020081 // The port allocator used by this PC.
82 cricket::PortAllocator* port_allocator_;
83
Steve Antonf1c6db12017-10-13 11:13:35 -070084 private:
85 rtc::FakeNetworkManager* network_;
86};
87
Steve Anton46d926a2018-01-23 10:23:06 -080088class PeerConnectionIceBaseTest : public ::testing::Test {
Steve Antonf1c6db12017-10-13 11:13:35 -070089 protected:
Steve Anton46d926a2018-01-23 10:23:06 -080090 typedef std::unique_ptr<PeerConnectionWrapperForIceTest> WrapperPtr;
Steve Antonf1c6db12017-10-13 11:13:35 -070091
Steve Anton46d926a2018-01-23 10:23:06 -080092 explicit PeerConnectionIceBaseTest(SdpSemantics sdp_semantics)
93 : vss_(new rtc::VirtualSocketServer()),
94 main_(vss_.get()),
95 sdp_semantics_(sdp_semantics) {
Steve Antonf1c6db12017-10-13 11:13:35 -070096#ifdef WEBRTC_ANDROID
97 InitializeAndroidObjects();
98#endif
99 pc_factory_ = CreatePeerConnectionFactory(
100 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Anders Carlsson67537952018-05-03 11:28:29 +0200101 rtc::scoped_refptr<AudioDeviceModule>(FakeAudioCaptureModule::Create()),
102 CreateBuiltinAudioEncoderFactory(), CreateBuiltinAudioDecoderFactory(),
103 CreateBuiltinVideoEncoderFactory(), CreateBuiltinVideoDecoderFactory(),
104 nullptr /* audio_mixer */, nullptr /* audio_processing */);
Steve Antonf1c6db12017-10-13 11:13:35 -0700105 }
106
107 WrapperPtr CreatePeerConnection() {
108 return CreatePeerConnection(RTCConfiguration());
109 }
110
111 WrapperPtr CreatePeerConnection(const RTCConfiguration& config) {
112 auto* fake_network = NewFakeNetwork();
113 auto port_allocator =
Karl Wiberg918f50c2018-07-05 11:40:33 +0200114 absl::make_unique<cricket::BasicPortAllocator>(fake_network);
Steve Antonf1c6db12017-10-13 11:13:35 -0700115 port_allocator->set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
116 cricket::PORTALLOCATOR_DISABLE_RELAY);
117 port_allocator->set_step_delay(cricket::kMinimumStepDelay);
Steve Anton46d926a2018-01-23 10:23:06 -0800118 RTCConfiguration modified_config = config;
119 modified_config.sdp_semantics = sdp_semantics_;
Karl Wiberg918f50c2018-07-05 11:40:33 +0200120 auto observer = absl::make_unique<MockPeerConnectionObserver>();
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200121 auto port_allocator_copy = port_allocator.get();
Steve Antonf1c6db12017-10-13 11:13:35 -0700122 auto pc = pc_factory_->CreatePeerConnection(
Steve Anton46d926a2018-01-23 10:23:06 -0800123 modified_config, std::move(port_allocator), nullptr, observer.get());
Steve Antonf1c6db12017-10-13 11:13:35 -0700124 if (!pc) {
125 return nullptr;
126 }
127
Yves Gerey4e933292018-10-31 15:36:05 +0100128 observer->SetPeerConnectionInterface(pc.get());
Karl Wiberg918f50c2018-07-05 11:40:33 +0200129 auto wrapper = absl::make_unique<PeerConnectionWrapperForIceTest>(
Steve Antonf1c6db12017-10-13 11:13:35 -0700130 pc_factory_, pc, std::move(observer));
131 wrapper->set_network(fake_network);
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200132 wrapper->port_allocator_ = port_allocator_copy;
Steve Antonf1c6db12017-10-13 11:13:35 -0700133 return wrapper;
134 }
135
136 // Accepts the same arguments as CreatePeerConnection and adds default audio
137 // and video tracks.
138 template <typename... Args>
139 WrapperPtr CreatePeerConnectionWithAudioVideo(Args&&... args) {
140 auto wrapper = CreatePeerConnection(std::forward<Args>(args)...);
141 if (!wrapper) {
142 return nullptr;
143 }
Steve Anton8d3444d2017-10-20 15:30:51 -0700144 wrapper->AddAudioTrack("a");
145 wrapper->AddVideoTrack("v");
Steve Antonf1c6db12017-10-13 11:13:35 -0700146 return wrapper;
147 }
148
149 cricket::Candidate CreateLocalUdpCandidate(
150 const rtc::SocketAddress& address) {
151 cricket::Candidate candidate;
152 candidate.set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT);
153 candidate.set_protocol(cricket::UDP_PROTOCOL_NAME);
154 candidate.set_address(address);
155 candidate.set_type(cricket::LOCAL_PORT_TYPE);
156 return candidate;
157 }
158
159 // Remove all ICE ufrag/pwd lines from the given session description.
160 void RemoveIceUfragPwd(SessionDescriptionInterface* sdesc) {
161 SetIceUfragPwd(sdesc, "", "");
162 }
163
164 // Sets all ICE ufrag/pwds on the given session description.
165 void SetIceUfragPwd(SessionDescriptionInterface* sdesc,
166 const std::string& ufrag,
167 const std::string& pwd) {
168 auto* desc = sdesc->description();
169 for (const auto& content : desc->contents()) {
170 auto* transport_info = desc->GetTransportInfoByName(content.name);
171 transport_info->description.ice_ufrag = ufrag;
172 transport_info->description.ice_pwd = pwd;
173 }
174 }
175
Qingsi Wange1692722017-11-29 13:27:20 -0800176 // Set ICE mode on the given session description.
177 void SetIceMode(SessionDescriptionInterface* sdesc,
178 const cricket::IceMode ice_mode) {
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_mode = ice_mode;
183 }
184 }
185
Steve Antonf1c6db12017-10-13 11:13:35 -0700186 cricket::TransportDescription* GetFirstTransportDescription(
187 SessionDescriptionInterface* sdesc) {
188 auto* desc = sdesc->description();
189 RTC_DCHECK(desc->contents().size() > 0);
190 auto* transport_info =
191 desc->GetTransportInfoByName(desc->contents()[0].name);
192 RTC_DCHECK(transport_info);
193 return &transport_info->description;
194 }
195
196 const cricket::TransportDescription* GetFirstTransportDescription(
197 const SessionDescriptionInterface* sdesc) {
198 auto* desc = sdesc->description();
199 RTC_DCHECK(desc->contents().size() > 0);
200 auto* transport_info =
201 desc->GetTransportInfoByName(desc->contents()[0].name);
202 RTC_DCHECK(transport_info);
203 return &transport_info->description;
204 }
205
Qingsi Wange1692722017-11-29 13:27:20 -0800206 // TODO(qingsi): Rewrite this method in terms of the standard IceTransport
207 // after it is implemented.
208 cricket::IceRole GetIceRole(const WrapperPtr& pc_wrapper_ptr) {
Mirko Bonadeie97de912017-12-13 11:29:34 +0100209 auto* pc_proxy =
210 static_cast<PeerConnectionProxyWithInternal<PeerConnectionInterface>*>(
211 pc_wrapper_ptr->pc());
212 PeerConnection* pc = static_cast<PeerConnection*>(pc_proxy->internal());
Steve Antonb8867112018-02-13 10:07:54 -0800213 for (auto transceiver : pc->GetTransceiversInternal()) {
Steve Anton69470252018-02-09 11:43:08 -0800214 if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
Steve Anton46d926a2018-01-23 10:23:06 -0800215 cricket::BaseChannel* channel = transceiver->internal()->channel();
216 if (channel) {
Zhi Huange830e682018-03-30 10:48:35 -0700217 auto dtls_transport = static_cast<cricket::DtlsTransportInternal*>(
218 channel->rtp_packet_transport());
219 return dtls_transport->ice_transport()->GetIceRole();
Steve Anton46d926a2018-01-23 10:23:06 -0800220 }
221 }
222 }
223 RTC_NOTREACHED();
224 return cricket::ICEROLE_UNKNOWN;
Qingsi Wange1692722017-11-29 13:27:20 -0800225 }
226
Steve Antonf1c6db12017-10-13 11:13:35 -0700227 bool AddCandidateToFirstTransport(cricket::Candidate* candidate,
228 SessionDescriptionInterface* sdesc) {
229 auto* desc = sdesc->description();
230 RTC_DCHECK(desc->contents().size() > 0);
231 const auto& first_content = desc->contents()[0];
232 candidate->set_transport_name(first_content.name);
Steve Anton27ab0e52018-07-23 15:11:53 -0700233 std::unique_ptr<IceCandidateInterface> jsep_candidate =
234 CreateIceCandidate(first_content.name, 0, *candidate);
235 return sdesc->AddCandidate(jsep_candidate.get());
Steve Antonf1c6db12017-10-13 11:13:35 -0700236 }
237
238 rtc::FakeNetworkManager* NewFakeNetwork() {
239 // The PeerConnection's port allocator is tied to the PeerConnection's
240 // lifetime and expects the underlying NetworkManager to outlive it. That
241 // prevents us from having the PeerConnectionWrapper own the fake network.
242 // Therefore, the test fixture will own all the fake networks even though
243 // tests should access the fake network through the PeerConnectionWrapper.
244 auto* fake_network = new rtc::FakeNetworkManager();
245 fake_networks_.emplace_back(fake_network);
246 return fake_network;
247 }
248
249 std::unique_ptr<rtc::VirtualSocketServer> vss_;
250 rtc::AutoSocketServerThread main_;
251 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_;
252 std::vector<std::unique_ptr<rtc::FakeNetworkManager>> fake_networks_;
Steve Anton46d926a2018-01-23 10:23:06 -0800253 const SdpSemantics sdp_semantics_;
254};
255
256class PeerConnectionIceTest
257 : public PeerConnectionIceBaseTest,
258 public ::testing::WithParamInterface<SdpSemantics> {
259 protected:
Harald Alvestrand76829d72018-07-18 23:24:36 +0200260 PeerConnectionIceTest() : PeerConnectionIceBaseTest(GetParam()) {
261 webrtc::metrics::Reset();
262 }
Steve Antonf1c6db12017-10-13 11:13:35 -0700263};
264
265::testing::AssertionResult AssertCandidatesEqual(const char* a_expr,
266 const char* b_expr,
267 const cricket::Candidate& a,
268 const cricket::Candidate& b) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200269 rtc::StringBuilder failure_info;
Steve Antonf1c6db12017-10-13 11:13:35 -0700270 if (a.component() != b.component()) {
271 failure_info << "\ncomponent: " << a.component() << " != " << b.component();
272 }
273 if (a.protocol() != b.protocol()) {
274 failure_info << "\nprotocol: " << a.protocol() << " != " << b.protocol();
275 }
276 if (a.address() != b.address()) {
277 failure_info << "\naddress: " << a.address().ToString()
278 << " != " << b.address().ToString();
279 }
280 if (a.type() != b.type()) {
281 failure_info << "\ntype: " << a.type() << " != " << b.type();
282 }
283 std::string failure_info_str = failure_info.str();
284 if (failure_info_str.empty()) {
285 return ::testing::AssertionSuccess();
286 } else {
287 return ::testing::AssertionFailure()
288 << a_expr << " and " << b_expr << " are not equal"
289 << failure_info_str;
290 }
291}
292
Steve Anton46d926a2018-01-23 10:23:06 -0800293TEST_P(PeerConnectionIceTest, OfferContainsGatheredCandidates) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700294 const SocketAddress kLocalAddress("1.1.1.1", 0);
295
296 auto caller = CreatePeerConnectionWithAudioVideo();
297 caller->network()->AddInterface(kLocalAddress);
298
299 // Start ICE candidate gathering by setting the local offer.
300 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
301
302 EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kIceCandidatesTimeout);
303
304 auto offer = caller->CreateOffer();
305 EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(0).size());
306 EXPECT_EQ(caller->observer()->GetCandidatesByMline(0).size(),
307 offer->candidates(0)->count());
308 EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(1).size());
309 EXPECT_EQ(caller->observer()->GetCandidatesByMline(1).size(),
310 offer->candidates(1)->count());
311}
312
Steve Anton46d926a2018-01-23 10:23:06 -0800313TEST_P(PeerConnectionIceTest, AnswerContainsGatheredCandidates) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700314 const SocketAddress kCallerAddress("1.1.1.1", 0);
315
316 auto caller = CreatePeerConnectionWithAudioVideo();
317 auto callee = CreatePeerConnectionWithAudioVideo();
318 caller->network()->AddInterface(kCallerAddress);
319
320 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
321 ASSERT_TRUE(callee->SetLocalDescription(callee->CreateAnswer()));
322
323 EXPECT_TRUE_WAIT(callee->IsIceGatheringDone(), kIceCandidatesTimeout);
324
Steve Antondffead82018-02-06 10:31:29 -0800325 auto* answer = callee->pc()->local_description();
Steve Antonf1c6db12017-10-13 11:13:35 -0700326 EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(0).size());
327 EXPECT_EQ(callee->observer()->GetCandidatesByMline(0).size(),
328 answer->candidates(0)->count());
329 EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(1).size());
330 EXPECT_EQ(callee->observer()->GetCandidatesByMline(1).size(),
331 answer->candidates(1)->count());
332}
333
Steve Anton46d926a2018-01-23 10:23:06 -0800334TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700335 CanSetRemoteSessionDescriptionWithRemoteCandidates) {
336 const SocketAddress kCallerAddress("1.1.1.1", 1111);
337
338 auto caller = CreatePeerConnectionWithAudioVideo();
339 auto callee = CreatePeerConnectionWithAudioVideo();
340
341 auto offer = caller->CreateOfferAndSetAsLocal();
342 cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress);
343 AddCandidateToFirstTransport(&candidate, offer.get());
344
345 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
346 auto remote_candidates = callee->GetIceCandidatesFromRemoteDescription();
347 ASSERT_EQ(1u, remote_candidates.size());
348 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate,
349 remote_candidates[0]->candidate());
350}
351
Steve Anton46d926a2018-01-23 10:23:06 -0800352TEST_P(PeerConnectionIceTest, SetLocalDescriptionFailsIfNoIceCredentials) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700353 auto caller = CreatePeerConnectionWithAudioVideo();
354
355 auto offer = caller->CreateOffer();
356 RemoveIceUfragPwd(offer.get());
357
358 EXPECT_FALSE(caller->SetLocalDescription(std::move(offer)));
359}
360
Steve Anton46d926a2018-01-23 10:23:06 -0800361TEST_P(PeerConnectionIceTest, SetRemoteDescriptionFailsIfNoIceCredentials) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700362 auto caller = CreatePeerConnectionWithAudioVideo();
363 auto callee = CreatePeerConnectionWithAudioVideo();
364
365 auto offer = caller->CreateOfferAndSetAsLocal();
366 RemoveIceUfragPwd(offer.get());
367
368 EXPECT_FALSE(callee->SetRemoteDescription(std::move(offer)));
369}
370
Steve Antonf764cf42018-05-01 14:32:17 -0700371// Test that doing an offer/answer exchange with no transport (i.e., no data
372// channel or media) results in the ICE connection state staying at New.
373TEST_P(PeerConnectionIceTest,
374 OfferAnswerWithNoTransportsDoesNotChangeIceConnectionState) {
375 auto caller = CreatePeerConnection();
376 auto callee = CreatePeerConnection();
377
378 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
379
380 EXPECT_EQ(PeerConnectionInterface::kIceConnectionNew,
381 caller->pc()->ice_connection_state());
382 EXPECT_EQ(PeerConnectionInterface::kIceConnectionNew,
383 callee->pc()->ice_connection_state());
384}
385
Steve Antonf1c6db12017-10-13 11:13:35 -0700386// The following group tests that ICE candidates are not generated before
387// SetLocalDescription is called on a PeerConnection.
388
Steve Anton46d926a2018-01-23 10:23:06 -0800389TEST_P(PeerConnectionIceTest, NoIceCandidatesBeforeSetLocalDescription) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700390 const SocketAddress kLocalAddress("1.1.1.1", 0);
391
392 auto caller = CreatePeerConnectionWithAudioVideo();
393 caller->network()->AddInterface(kLocalAddress);
394
395 // Pump for 1 second and verify that no candidates are generated.
396 rtc::Thread::Current()->ProcessMessages(1000);
397
398 EXPECT_EQ(0u, caller->observer()->candidates_.size());
399}
Steve Anton46d926a2018-01-23 10:23:06 -0800400TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700401 NoIceCandidatesBeforeAnswerSetAsLocalDescription) {
402 const SocketAddress kCallerAddress("1.1.1.1", 1111);
403
404 auto caller = CreatePeerConnectionWithAudioVideo();
405 auto callee = CreatePeerConnectionWithAudioVideo();
406 caller->network()->AddInterface(kCallerAddress);
407
408 auto offer = caller->CreateOfferAndSetAsLocal();
409 cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress);
410 AddCandidateToFirstTransport(&candidate, offer.get());
411 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
412
413 // Pump for 1 second and verify that no candidates are generated.
414 rtc::Thread::Current()->ProcessMessages(1000);
415
416 EXPECT_EQ(0u, callee->observer()->candidates_.size());
417}
418
Steve Anton46d926a2018-01-23 10:23:06 -0800419TEST_P(PeerConnectionIceTest, CannotAddCandidateWhenRemoteDescriptionNotSet) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700420 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
421
422 auto caller = CreatePeerConnectionWithAudioVideo();
423 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
Steve Anton27ab0e52018-07-23 15:11:53 -0700424 std::unique_ptr<IceCandidateInterface> jsep_candidate =
425 CreateIceCandidate(cricket::CN_AUDIO, 0, candidate);
Steve Antonf1c6db12017-10-13 11:13:35 -0700426
Steve Anton27ab0e52018-07-23 15:11:53 -0700427 EXPECT_FALSE(caller->pc()->AddIceCandidate(jsep_candidate.get()));
Steve Antonf1c6db12017-10-13 11:13:35 -0700428
429 caller->CreateOfferAndSetAsLocal();
430
Steve Anton27ab0e52018-07-23 15:11:53 -0700431 EXPECT_FALSE(caller->pc()->AddIceCandidate(jsep_candidate.get()));
Harald Alvestrand76829d72018-07-18 23:24:36 +0200432 EXPECT_EQ(
433 2, webrtc::metrics::NumSamples("WebRTC.PeerConnection.AddIceCandidate"));
434 EXPECT_EQ(
435 2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.AddIceCandidate",
436 kAddIceCandidateFailNoRemoteDescription));
Steve Antonf1c6db12017-10-13 11:13:35 -0700437}
438
Steve Antonc79268f2018-04-24 09:54:10 -0700439TEST_P(PeerConnectionIceTest, CannotAddCandidateWhenPeerConnectionClosed) {
440 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
441
442 auto caller = CreatePeerConnectionWithAudioVideo();
443 auto callee = CreatePeerConnectionWithAudioVideo();
444
445 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
446
447 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
448 auto* audio_content = cricket::GetFirstAudioContent(
449 caller->pc()->local_description()->description());
Steve Anton27ab0e52018-07-23 15:11:53 -0700450 std::unique_ptr<IceCandidateInterface> jsep_candidate =
451 CreateIceCandidate(audio_content->name, 0, candidate);
Steve Antonc79268f2018-04-24 09:54:10 -0700452
453 caller->pc()->Close();
454
Steve Anton27ab0e52018-07-23 15:11:53 -0700455 EXPECT_FALSE(caller->pc()->AddIceCandidate(jsep_candidate.get()));
Steve Antonc79268f2018-04-24 09:54:10 -0700456}
457
Steve Anton46d926a2018-01-23 10:23:06 -0800458TEST_P(PeerConnectionIceTest, DuplicateIceCandidateIgnoredWhenAdded) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700459 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
460
461 auto caller = CreatePeerConnectionWithAudioVideo();
462 auto callee = CreatePeerConnectionWithAudioVideo();
463
464 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
465 ASSERT_TRUE(
466 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
467
468 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
469 caller->AddIceCandidate(&candidate);
470 EXPECT_TRUE(caller->AddIceCandidate(&candidate));
471 EXPECT_EQ(1u, caller->GetIceCandidatesFromRemoteDescription().size());
472}
473
Steve Anton46d926a2018-01-23 10:23:06 -0800474TEST_P(PeerConnectionIceTest,
Steve Antonc79268f2018-04-24 09:54:10 -0700475 CannotRemoveIceCandidatesWhenPeerConnectionClosed) {
476 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
477
478 auto caller = CreatePeerConnectionWithAudioVideo();
479 auto callee = CreatePeerConnectionWithAudioVideo();
480
481 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
482
483 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
484 auto* audio_content = cricket::GetFirstAudioContent(
485 caller->pc()->local_description()->description());
Steve Anton27ab0e52018-07-23 15:11:53 -0700486 std::unique_ptr<IceCandidateInterface> ice_candidate =
487 CreateIceCandidate(audio_content->name, 0, candidate);
Steve Antonc79268f2018-04-24 09:54:10 -0700488
Steve Anton27ab0e52018-07-23 15:11:53 -0700489 ASSERT_TRUE(caller->pc()->AddIceCandidate(ice_candidate.get()));
Steve Antonc79268f2018-04-24 09:54:10 -0700490
491 caller->pc()->Close();
492
493 EXPECT_FALSE(caller->pc()->RemoveIceCandidates({candidate}));
494}
495
496TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700497 AddRemoveCandidateWithEmptyTransportDoesNotCrash) {
498 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
499
500 auto caller = CreatePeerConnectionWithAudioVideo();
501 auto callee = CreatePeerConnectionWithAudioVideo();
502
503 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
504 ASSERT_TRUE(
505 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
506
507 // |candidate.transport_name()| is empty.
508 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
Steve Anton46d926a2018-01-23 10:23:06 -0800509 auto* audio_content = cricket::GetFirstAudioContent(
510 caller->pc()->local_description()->description());
Steve Anton27ab0e52018-07-23 15:11:53 -0700511 std::unique_ptr<IceCandidateInterface> ice_candidate =
512 CreateIceCandidate(audio_content->name, 0, candidate);
513 EXPECT_TRUE(caller->pc()->AddIceCandidate(ice_candidate.get()));
Steve Antonf1c6db12017-10-13 11:13:35 -0700514 EXPECT_TRUE(caller->pc()->RemoveIceCandidates({candidate}));
515}
516
Steve Anton46d926a2018-01-23 10:23:06 -0800517TEST_P(PeerConnectionIceTest, RemoveCandidateRemovesFromRemoteDescription) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700518 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
519
520 auto caller = CreatePeerConnectionWithAudioVideo();
521 auto callee = CreatePeerConnectionWithAudioVideo();
522
523 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
524 ASSERT_TRUE(
525 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
526
527 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
528 ASSERT_TRUE(caller->AddIceCandidate(&candidate));
529 EXPECT_TRUE(caller->pc()->RemoveIceCandidates({candidate}));
530 EXPECT_EQ(0u, caller->GetIceCandidatesFromRemoteDescription().size());
531}
532
533// Test that if a candidate is added via AddIceCandidate and via an updated
534// remote description, then both candidates appear in the stored remote
535// description.
Steve Anton46d926a2018-01-23 10:23:06 -0800536TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700537 CandidateInSubsequentOfferIsAddedToRemoteDescription) {
538 const SocketAddress kCallerAddress1("1.1.1.1", 1111);
539 const SocketAddress kCallerAddress2("2.2.2.2", 2222);
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 // Add one candidate via |AddIceCandidate|.
549 cricket::Candidate candidate1 = CreateLocalUdpCandidate(kCallerAddress1);
550 ASSERT_TRUE(callee->AddIceCandidate(&candidate1));
551
552 // Add the second candidate via a reoffer.
553 auto offer = caller->CreateOffer();
554 cricket::Candidate candidate2 = CreateLocalUdpCandidate(kCallerAddress2);
555 AddCandidateToFirstTransport(&candidate2, offer.get());
556
557 // Expect both candidates to appear in the callee's remote description.
558 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
559 EXPECT_EQ(2u, callee->GetIceCandidatesFromRemoteDescription().size());
560}
561
562// The follow test verifies that SetLocal/RemoteDescription fails when an offer
563// has either ICE ufrag/pwd too short or too long and succeeds otherwise.
564// The standard (https://tools.ietf.org/html/rfc5245#section-15.4) says that
565// pwd must be 22-256 characters and ufrag must be 4-256 characters.
Steve Anton46d926a2018-01-23 10:23:06 -0800566TEST_P(PeerConnectionIceTest, VerifyUfragPwdLength) {
Yves Gerey665174f2018-06-19 15:03:05 +0200567 auto set_local_description_with_ufrag_pwd_length = [this](int ufrag_len,
568 int pwd_len) {
569 auto pc = CreatePeerConnectionWithAudioVideo();
570 auto offer = pc->CreateOffer();
571 SetIceUfragPwd(offer.get(), std::string(ufrag_len, 'x'),
572 std::string(pwd_len, 'x'));
573 return pc->SetLocalDescription(std::move(offer));
574 };
Steve Antonf1c6db12017-10-13 11:13:35 -0700575
Yves Gerey665174f2018-06-19 15:03:05 +0200576 auto set_remote_description_with_ufrag_pwd_length = [this](int ufrag_len,
577 int pwd_len) {
578 auto pc = CreatePeerConnectionWithAudioVideo();
579 auto offer = pc->CreateOffer();
580 SetIceUfragPwd(offer.get(), std::string(ufrag_len, 'x'),
581 std::string(pwd_len, 'x'));
582 return pc->SetRemoteDescription(std::move(offer));
583 };
Steve Antonf1c6db12017-10-13 11:13:35 -0700584
585 EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(3, 22));
586 EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(3, 22));
587 EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(257, 22));
588 EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(257, 22));
589 EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(4, 21));
590 EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(4, 21));
591 EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(4, 257));
592 EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(4, 257));
593 EXPECT_TRUE(set_local_description_with_ufrag_pwd_length(4, 22));
594 EXPECT_TRUE(set_remote_description_with_ufrag_pwd_length(4, 22));
595 EXPECT_TRUE(set_local_description_with_ufrag_pwd_length(256, 256));
596 EXPECT_TRUE(set_remote_description_with_ufrag_pwd_length(256, 256));
597}
598
599::testing::AssertionResult AssertIpInCandidates(
600 const char* address_expr,
601 const char* candidates_expr,
602 const SocketAddress& address,
603 const std::vector<IceCandidateInterface*> candidates) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200604 rtc::StringBuilder candidate_hosts;
Steve Antonf1c6db12017-10-13 11:13:35 -0700605 for (const auto* candidate : candidates) {
606 const auto& candidate_ip = candidate->candidate().address().ipaddr();
607 if (candidate_ip == address.ipaddr()) {
608 return ::testing::AssertionSuccess();
609 }
Jonas Olssonabbe8412018-04-03 13:40:05 +0200610 candidate_hosts << "\n" << candidate_ip.ToString();
Steve Antonf1c6db12017-10-13 11:13:35 -0700611 }
612 return ::testing::AssertionFailure()
613 << address_expr << " (host " << address.HostAsURIString()
614 << ") not in " << candidates_expr
615 << " which have the following address hosts:" << candidate_hosts.str();
616}
617
Steve Anton46d926a2018-01-23 10:23:06 -0800618TEST_P(PeerConnectionIceTest, CandidatesGeneratedForEachLocalInterface) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700619 const SocketAddress kLocalAddress1("1.1.1.1", 0);
620 const SocketAddress kLocalAddress2("2.2.2.2", 0);
621
622 auto caller = CreatePeerConnectionWithAudioVideo();
623 caller->network()->AddInterface(kLocalAddress1);
624 caller->network()->AddInterface(kLocalAddress2);
625
626 caller->CreateOfferAndSetAsLocal();
627 EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kIceCandidatesTimeout);
628
629 auto candidates = caller->observer()->GetCandidatesByMline(0);
630 EXPECT_PRED_FORMAT2(AssertIpInCandidates, kLocalAddress1, candidates);
631 EXPECT_PRED_FORMAT2(AssertIpInCandidates, kLocalAddress2, candidates);
632}
633
Steve Anton46d926a2018-01-23 10:23:06 -0800634TEST_P(PeerConnectionIceTest, TrickledSingleCandidateAddedToRemoteDescription) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700635 const SocketAddress kCallerAddress("1.1.1.1", 1111);
636
637 auto caller = CreatePeerConnectionWithAudioVideo();
638 auto callee = CreatePeerConnectionWithAudioVideo();
639
640 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
641
642 cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress);
643 callee->AddIceCandidate(&candidate);
644 auto candidates = callee->GetIceCandidatesFromRemoteDescription();
645 ASSERT_EQ(1u, candidates.size());
646 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate,
647 candidates[0]->candidate());
648}
649
Steve Anton46d926a2018-01-23 10:23:06 -0800650TEST_P(PeerConnectionIceTest, TwoTrickledCandidatesAddedToRemoteDescription) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700651 const SocketAddress kCalleeAddress1("1.1.1.1", 1111);
652 const SocketAddress kCalleeAddress2("2.2.2.2", 2222);
653
654 auto caller = CreatePeerConnectionWithAudioVideo();
655 auto callee = CreatePeerConnectionWithAudioVideo();
656
657 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
658 ASSERT_TRUE(
659 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
660
661 cricket::Candidate candidate1 = CreateLocalUdpCandidate(kCalleeAddress1);
662 caller->AddIceCandidate(&candidate1);
663
664 cricket::Candidate candidate2 = CreateLocalUdpCandidate(kCalleeAddress2);
665 caller->AddIceCandidate(&candidate2);
666
667 auto candidates = caller->GetIceCandidatesFromRemoteDescription();
668 ASSERT_EQ(2u, candidates.size());
669 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate1,
670 candidates[0]->candidate());
671 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate2,
672 candidates[1]->candidate());
673}
674
Steve Anton46d926a2018-01-23 10:23:06 -0800675TEST_P(PeerConnectionIceTest, LocalDescriptionUpdatedWhenContinualGathering) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700676 const SocketAddress kLocalAddress("1.1.1.1", 0);
677
678 RTCConfiguration config;
679 config.continual_gathering_policy =
680 PeerConnectionInterface::GATHER_CONTINUALLY;
681 auto caller = CreatePeerConnectionWithAudioVideo(config);
682 caller->network()->AddInterface(kLocalAddress);
683
684 // Start ICE candidate gathering by setting the local offer.
685 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
686
687 // Since we're using continual gathering, we won't get "gathering done".
688 EXPECT_TRUE_WAIT(
689 caller->pc()->local_description()->candidates(0)->count() > 0,
690 kIceCandidatesTimeout);
691}
692
693// Test that when continual gathering is enabled, and a network interface goes
694// down, the candidate is signaled as removed and removed from the local
695// description.
Steve Anton46d926a2018-01-23 10:23:06 -0800696TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700697 LocalCandidatesRemovedWhenNetworkDownIfGatheringContinually) {
698 const SocketAddress kLocalAddress("1.1.1.1", 0);
699
700 RTCConfiguration config;
701 config.continual_gathering_policy =
702 PeerConnectionInterface::GATHER_CONTINUALLY;
703 auto caller = CreatePeerConnectionWithAudioVideo(config);
704 caller->network()->AddInterface(kLocalAddress);
705
706 // Start ICE candidate gathering by setting the local offer.
707 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
708
709 EXPECT_TRUE_WAIT(
710 caller->pc()->local_description()->candidates(0)->count() > 0,
711 kIceCandidatesTimeout);
712
713 // Remove the only network interface, causing the PeerConnection to signal
714 // the removal of all candidates derived from this interface.
715 caller->network()->RemoveInterface(kLocalAddress);
716
717 EXPECT_EQ_WAIT(0u, caller->pc()->local_description()->candidates(0)->count(),
718 kIceCandidatesTimeout);
719 EXPECT_LT(0, caller->observer()->num_candidates_removed_);
720}
721
Steve Anton46d926a2018-01-23 10:23:06 -0800722TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700723 LocalCandidatesNotRemovedWhenNetworkDownIfGatheringOnce) {
724 const SocketAddress kLocalAddress("1.1.1.1", 0);
725
726 RTCConfiguration config;
727 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
728 auto caller = CreatePeerConnectionWithAudioVideo(config);
729 caller->network()->AddInterface(kLocalAddress);
730
731 // Start ICE candidate gathering by setting the local offer.
732 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
733
734 EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kIceCandidatesTimeout);
735
736 caller->network()->RemoveInterface(kLocalAddress);
737
738 // Verify that the local candidates are not removed;
739 rtc::Thread::Current()->ProcessMessages(1000);
740 EXPECT_EQ(0, caller->observer()->num_candidates_removed_);
741}
742
743// The following group tests that when an offer includes a new ufrag or pwd
744// (indicating an ICE restart) the old candidates are removed and new candidates
745// added to the remote description.
746
Steve Anton46d926a2018-01-23 10:23:06 -0800747TEST_P(PeerConnectionIceTest, IceRestartOfferClearsExistingCandidate) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700748 const SocketAddress kCallerAddress("1.1.1.1", 1111);
749
750 auto caller = CreatePeerConnectionWithAudioVideo();
751 auto callee = CreatePeerConnectionWithAudioVideo();
752
753 auto offer = caller->CreateOffer();
754 cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress);
755 AddCandidateToFirstTransport(&candidate, offer.get());
756
757 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
758
759 RTCOfferAnswerOptions options;
760 options.ice_restart = true;
761 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOffer(options)));
762
763 EXPECT_EQ(0u, callee->GetIceCandidatesFromRemoteDescription().size());
764}
Steve Anton46d926a2018-01-23 10:23:06 -0800765TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700766 IceRestartOfferCandidateReplacesExistingCandidate) {
767 const SocketAddress kFirstCallerAddress("1.1.1.1", 1111);
768 const SocketAddress kRestartedCallerAddress("2.2.2.2", 2222);
769
770 auto caller = CreatePeerConnectionWithAudioVideo();
771 auto callee = CreatePeerConnectionWithAudioVideo();
772
773 auto offer = caller->CreateOffer();
774 cricket::Candidate old_candidate =
775 CreateLocalUdpCandidate(kFirstCallerAddress);
776 AddCandidateToFirstTransport(&old_candidate, offer.get());
777
778 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
779
780 RTCOfferAnswerOptions options;
781 options.ice_restart = true;
782 auto restart_offer = caller->CreateOffer(options);
783 cricket::Candidate new_candidate =
784 CreateLocalUdpCandidate(kRestartedCallerAddress);
785 AddCandidateToFirstTransport(&new_candidate, restart_offer.get());
786
787 ASSERT_TRUE(callee->SetRemoteDescription(std::move(restart_offer)));
788
789 auto remote_candidates = callee->GetIceCandidatesFromRemoteDescription();
790 ASSERT_EQ(1u, remote_candidates.size());
791 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, new_candidate,
792 remote_candidates[0]->candidate());
793}
794
795// Test that if there is not an ICE restart (i.e., nothing changes), then the
796// answer to a later offer should have the same ufrag/pwd as the first answer.
Steve Anton46d926a2018-01-23 10:23:06 -0800797TEST_P(PeerConnectionIceTest, LaterAnswerHasSameIceCredentialsIfNoIceRestart) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700798 auto caller = CreatePeerConnectionWithAudioVideo();
799 auto callee = CreatePeerConnectionWithAudioVideo();
800
801 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
802 ASSERT_TRUE(
803 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
804
805 // Re-offer.
806 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
807
808 auto answer = callee->CreateAnswer();
809 auto* answer_transport_desc = GetFirstTransportDescription(answer.get());
810 auto* local_transport_desc =
811 GetFirstTransportDescription(callee->pc()->local_description());
812
813 EXPECT_EQ(answer_transport_desc->ice_ufrag, local_transport_desc->ice_ufrag);
814 EXPECT_EQ(answer_transport_desc->ice_pwd, local_transport_desc->ice_pwd);
815}
816
817// The following parameterized test verifies that if an offer is sent with a
818// modified ICE ufrag and/or ICE pwd, then the answer should identify that the
819// other side has initiated an ICE restart and generate a new ufrag and pwd.
820// RFC 5245 says: "If the offer contained a change in the a=ice-ufrag or
821// a=ice-pwd attributes compared to the previous SDP from the peer, it
822// indicates that ICE is restarting for this media stream."
823
Steve Anton46d926a2018-01-23 10:23:06 -0800824class PeerConnectionIceUfragPwdAnswerTest
825 : public PeerConnectionIceBaseTest,
826 public ::testing::WithParamInterface<
827 std::tuple<SdpSemantics, std::tuple<bool, bool>>> {
Steve Antonf1c6db12017-10-13 11:13:35 -0700828 protected:
Steve Anton46d926a2018-01-23 10:23:06 -0800829 PeerConnectionIceUfragPwdAnswerTest()
830 : PeerConnectionIceBaseTest(std::get<0>(GetParam())) {
831 auto param = std::get<1>(GetParam());
832 offer_new_ufrag_ = std::get<0>(param);
833 offer_new_pwd_ = std::get<1>(param);
Steve Antonf1c6db12017-10-13 11:13:35 -0700834 }
835
836 bool offer_new_ufrag_;
837 bool offer_new_pwd_;
838};
839
Steve Anton46d926a2018-01-23 10:23:06 -0800840TEST_P(PeerConnectionIceUfragPwdAnswerTest, TestIncludedInAnswer) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700841 auto caller = CreatePeerConnectionWithAudioVideo();
842 auto callee = CreatePeerConnectionWithAudioVideo();
843
844 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
845 ASSERT_TRUE(
846 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
847
848 auto offer = caller->CreateOffer();
849 auto* offer_transport_desc = GetFirstTransportDescription(offer.get());
850 if (offer_new_ufrag_) {
851 offer_transport_desc->ice_ufrag += "_new";
852 }
853 if (offer_new_pwd_) {
854 offer_transport_desc->ice_pwd += "_new";
855 }
856
857 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
858
859 auto answer = callee->CreateAnswer();
860 auto* answer_transport_desc = GetFirstTransportDescription(answer.get());
861 auto* local_transport_desc =
862 GetFirstTransportDescription(callee->pc()->local_description());
863
864 EXPECT_NE(answer_transport_desc->ice_ufrag, local_transport_desc->ice_ufrag);
865 EXPECT_NE(answer_transport_desc->ice_pwd, local_transport_desc->ice_pwd);
866}
867
868INSTANTIATE_TEST_CASE_P(
Steve Anton46d926a2018-01-23 10:23:06 -0800869 PeerConnectionIceTest,
870 PeerConnectionIceUfragPwdAnswerTest,
871 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
872 Values(std::make_pair(true, true), // Both changed.
873 std::make_pair(true, false), // Only ufrag changed.
874 std::make_pair(false, true)))); // Only pwd changed.
Steve Antonf1c6db12017-10-13 11:13:35 -0700875
876// Test that if an ICE restart is offered on one media section, then the answer
877// will only change ICE ufrag/pwd for that section and keep the other sections
878// the same.
879// Note that this only works if we have disabled BUNDLE, otherwise all media
880// sections will share the same transport.
Steve Anton46d926a2018-01-23 10:23:06 -0800881TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700882 CreateAnswerHasNewUfragPwdForOnlyMediaSectionWhichRestarted) {
883 auto caller = CreatePeerConnectionWithAudioVideo();
884 auto callee = CreatePeerConnectionWithAudioVideo();
885
886 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
887 ASSERT_TRUE(
888 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
889
890 RTCOfferAnswerOptions disable_bundle_options;
891 disable_bundle_options.use_rtp_mux = false;
892
893 auto offer = caller->CreateOffer(disable_bundle_options);
894
895 // Signal ICE restart on the first media section.
896 auto* offer_transport_desc = GetFirstTransportDescription(offer.get());
897 offer_transport_desc->ice_ufrag += "_new";
898 offer_transport_desc->ice_pwd += "_new";
899
900 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
901
902 auto answer = callee->CreateAnswer(disable_bundle_options);
903 const auto& answer_transports = answer->description()->transport_infos();
904 const auto& local_transports =
905 callee->pc()->local_description()->description()->transport_infos();
906
907 EXPECT_NE(answer_transports[0].description.ice_ufrag,
908 local_transports[0].description.ice_ufrag);
909 EXPECT_NE(answer_transports[0].description.ice_pwd,
910 local_transports[0].description.ice_pwd);
911 EXPECT_EQ(answer_transports[1].description.ice_ufrag,
912 local_transports[1].description.ice_ufrag);
913 EXPECT_EQ(answer_transports[1].description.ice_pwd,
914 local_transports[1].description.ice_pwd);
915}
916
Qingsi Wange1692722017-11-29 13:27:20 -0800917// Test that when the initial offerer (caller) uses the lite implementation of
918// ICE and the callee uses the full implementation, the caller takes the
919// CONTROLLED role and the callee takes the CONTROLLING role. This is specified
920// in RFC5245 Section 5.1.1.
Steve Anton46d926a2018-01-23 10:23:06 -0800921TEST_P(PeerConnectionIceTest,
Qingsi Wange1692722017-11-29 13:27:20 -0800922 OfferFromLiteIceControlledAndAnswerFromFullIceControlling) {
923 auto caller = CreatePeerConnectionWithAudioVideo();
924 auto callee = CreatePeerConnectionWithAudioVideo();
925
926 auto offer = caller->CreateOffer();
927 SetIceMode(offer.get(), cricket::IceMode::ICEMODE_LITE);
928 ASSERT_TRUE(
929 caller->SetLocalDescription(CloneSessionDescription(offer.get())));
930 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
931
932 auto answer = callee->CreateAnswer();
933 SetIceMode(answer.get(), cricket::IceMode::ICEMODE_FULL);
934 ASSERT_TRUE(
935 callee->SetLocalDescription(CloneSessionDescription(answer.get())));
936 ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer)));
937
938 EXPECT_EQ(cricket::ICEROLE_CONTROLLED, GetIceRole(caller));
939 EXPECT_EQ(cricket::ICEROLE_CONTROLLING, GetIceRole(callee));
940}
941
942// Test that when the caller and the callee both use the lite implementation of
943// ICE, the initial offerer (caller) takes the CONTROLLING role and the callee
944// takes the CONTROLLED role. This is specified in RFC5245 Section 5.1.1.
Steve Anton46d926a2018-01-23 10:23:06 -0800945TEST_P(PeerConnectionIceTest,
Qingsi Wange1692722017-11-29 13:27:20 -0800946 OfferFromLiteIceControllingAndAnswerFromLiteIceControlled) {
947 auto caller = CreatePeerConnectionWithAudioVideo();
948 auto callee = CreatePeerConnectionWithAudioVideo();
949
950 auto offer = caller->CreateOffer();
951 SetIceMode(offer.get(), cricket::IceMode::ICEMODE_LITE);
952 ASSERT_TRUE(
953 caller->SetLocalDescription(CloneSessionDescription(offer.get())));
954 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
955
956 auto answer = callee->CreateAnswer();
957 SetIceMode(answer.get(), cricket::IceMode::ICEMODE_LITE);
958 ASSERT_TRUE(
959 callee->SetLocalDescription(CloneSessionDescription(answer.get())));
960 ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer)));
961
962 EXPECT_EQ(cricket::ICEROLE_CONTROLLING, GetIceRole(caller));
963 EXPECT_EQ(cricket::ICEROLE_CONTROLLED, GetIceRole(callee));
964}
965
Steve Anton46d926a2018-01-23 10:23:06 -0800966INSTANTIATE_TEST_CASE_P(PeerConnectionIceTest,
967 PeerConnectionIceTest,
968 Values(SdpSemantics::kPlanB,
969 SdpSemantics::kUnifiedPlan));
970
Qingsi Wang4ff54432018-03-01 18:25:20 -0800971class PeerConnectionIceConfigTest : public testing::Test {
972 protected:
973 void SetUp() override {
974 pc_factory_ = CreatePeerConnectionFactory(
975 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
976 FakeAudioCaptureModule::Create(), CreateBuiltinAudioEncoderFactory(),
Anders Carlsson67537952018-05-03 11:28:29 +0200977 CreateBuiltinAudioDecoderFactory(), CreateBuiltinVideoEncoderFactory(),
978 CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
979 nullptr /* audio_processing */);
Qingsi Wang4ff54432018-03-01 18:25:20 -0800980 }
981 void CreatePeerConnection(const RTCConfiguration& config) {
982 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
983 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
984 port_allocator_ = port_allocator.get();
985 rtc::scoped_refptr<PeerConnectionInterface> pc(
Niels Möllerf06f9232018-08-07 12:32:18 +0200986 pc_factory_->CreatePeerConnection(config, std::move(port_allocator),
987 nullptr /* cert_generator */,
988 &observer_));
Qingsi Wang4ff54432018-03-01 18:25:20 -0800989 EXPECT_TRUE(pc.get());
990 pc_ = std::move(pc.get());
991 }
992
993 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_ = nullptr;
994 rtc::scoped_refptr<PeerConnectionInterface> pc_ = nullptr;
995 cricket::FakePortAllocator* port_allocator_ = nullptr;
996
997 MockPeerConnectionObserver observer_;
998};
999
1000TEST_F(PeerConnectionIceConfigTest, SetStunCandidateKeepaliveInterval) {
1001 RTCConfiguration config;
1002 config.stun_candidate_keepalive_interval = 123;
1003 config.ice_candidate_pool_size = 1;
1004 CreatePeerConnection(config);
1005 ASSERT_NE(port_allocator_, nullptr);
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02001006 absl::optional<int> actual_stun_keepalive_interval =
Qingsi Wang4ff54432018-03-01 18:25:20 -08001007 port_allocator_->stun_candidate_keepalive_interval();
1008 EXPECT_EQ(actual_stun_keepalive_interval.value_or(-1), 123);
1009 config.stun_candidate_keepalive_interval = 321;
1010 RTCError error;
1011 pc_->SetConfiguration(config, &error);
1012 actual_stun_keepalive_interval =
1013 port_allocator_->stun_candidate_keepalive_interval();
1014 EXPECT_EQ(actual_stun_keepalive_interval.value_or(-1), 321);
1015}
1016
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001017TEST_P(PeerConnectionIceTest, IceCredentialsCreateOffer) {
1018 RTCConfiguration config;
1019 config.ice_candidate_pool_size = 1;
1020 auto pc = CreatePeerConnectionWithAudioVideo(config);
1021 ASSERT_NE(pc->port_allocator_, nullptr);
1022 auto offer = pc->CreateOffer();
1023 auto credentials = pc->port_allocator_->GetPooledIceCredentials();
1024 ASSERT_EQ(1u, credentials.size());
1025
1026 auto* desc = offer->description();
1027 for (const auto& content : desc->contents()) {
1028 auto* transport_info = desc->GetTransportInfoByName(content.name);
1029 EXPECT_EQ(transport_info->description.ice_ufrag, credentials[0].ufrag);
1030 EXPECT_EQ(transport_info->description.ice_pwd, credentials[0].pwd);
1031 }
1032}
1033
1034TEST_P(PeerConnectionIceTest, IceCredentialsCreateAnswer) {
1035 RTCConfiguration config;
1036 config.ice_candidate_pool_size = 1;
1037 auto pc = CreatePeerConnectionWithAudioVideo(config);
1038 ASSERT_NE(pc->port_allocator_, nullptr);
1039 auto offer = pc->CreateOffer();
1040 ASSERT_TRUE(pc->SetRemoteDescription(std::move(offer)));
1041 auto answer = pc->CreateAnswer();
1042
1043 auto credentials = pc->port_allocator_->GetPooledIceCredentials();
1044 ASSERT_EQ(1u, credentials.size());
1045
1046 auto* desc = answer->description();
1047 for (const auto& content : desc->contents()) {
1048 auto* transport_info = desc->GetTransportInfoByName(content.name);
1049 EXPECT_EQ(transport_info->description.ice_ufrag, credentials[0].ufrag);
1050 EXPECT_EQ(transport_info->description.ice_pwd, credentials[0].pwd);
1051 }
1052}
1053
Steve Antonf1c6db12017-10-13 11:13:35 -07001054} // namespace webrtc