blob: 807a8e1cf87c8d13d2238eb57ba6e12dbec05bfb [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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "p2p/base/fake_port_allocator.h"
12#include "p2p/base/test_stun_server.h"
13#include "p2p/client/basic_port_allocator.h"
14#include "pc/media_session.h"
15#include "pc/peer_connection.h"
16#include "pc/peer_connection_wrapper.h"
17#include "pc/sdp_utils.h"
Steve Antonf1c6db12017-10-13 11:13:35 -070018#ifdef WEBRTC_ANDROID
Steve Anton10542f22019-01-11 09:11:00 -080019#include "pc/test/android_test_initializer.h"
Steve Antonf1c6db12017-10-13 11:13:35 -070020#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"
Mirko Bonadei2ff3f492018-11-22 09:00:13 +010024#include "api/create_peerconnection_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "api/peer_connection_proxy.h"
26#include "api/uma_metrics.h"
Anders Carlsson67537952018-05-03 11:28:29 +020027#include "api/video_codecs/builtin_video_decoder_factory.h"
28#include "api/video_codecs/builtin_video_encoder_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#include "pc/test/fake_audio_capture_module.h"
30#include "rtc_base/fake_network.h"
Steve Antonf1c6db12017-10-13 11:13:35 -070031#include "rtc_base/gunit.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020032#include "rtc_base/strings/string_builder.h"
Steve Anton10542f22019-01-11 09:11:00 -080033#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020034#include "system_wrappers/include/metrics.h"
Steve Antonb443dfe2019-03-05 14:09:49 -080035#include "test/gmock.h"
Steve Antonf1c6db12017-10-13 11:13:35 -070036
37namespace webrtc {
38
39using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
40using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
41using rtc::SocketAddress;
Steve Anton46d926a2018-01-23 10:23:06 -080042using ::testing::Combine;
Steve Antonb443dfe2019-03-05 14:09:49 -080043using ::testing::ElementsAre;
44using ::testing::Pair;
Steve Antonf1c6db12017-10-13 11:13:35 -070045using ::testing::Values;
46
47constexpr int kIceCandidatesTimeout = 10000;
48
Steve Anton46d926a2018-01-23 10:23:06 -080049class PeerConnectionWrapperForIceTest : public PeerConnectionWrapper {
Steve Antonf1c6db12017-10-13 11:13:35 -070050 public:
51 using PeerConnectionWrapper::PeerConnectionWrapper;
52
53 // Adds a new ICE candidate to the first transport.
54 bool AddIceCandidate(cricket::Candidate* candidate) {
55 RTC_DCHECK(pc()->remote_description());
56 const auto* desc = pc()->remote_description()->description();
57 RTC_DCHECK(desc->contents().size() > 0);
58 const auto& first_content = desc->contents()[0];
59 candidate->set_transport_name(first_content.name);
Steve Anton27ab0e52018-07-23 15:11:53 -070060 std::unique_ptr<IceCandidateInterface> jsep_candidate =
61 CreateIceCandidate(first_content.name, 0, *candidate);
62 return pc()->AddIceCandidate(jsep_candidate.get());
Steve Antonf1c6db12017-10-13 11:13:35 -070063 }
64
65 // Returns ICE candidates from the remote session description.
66 std::vector<const IceCandidateInterface*>
67 GetIceCandidatesFromRemoteDescription() {
68 const SessionDescriptionInterface* sdesc = pc()->remote_description();
69 RTC_DCHECK(sdesc);
70 std::vector<const IceCandidateInterface*> candidates;
71 for (size_t mline_index = 0; mline_index < sdesc->number_of_mediasections();
72 mline_index++) {
73 const auto* candidate_collection = sdesc->candidates(mline_index);
74 for (size_t i = 0; i < candidate_collection->count(); i++) {
75 candidates.push_back(candidate_collection->at(i));
76 }
77 }
78 return candidates;
79 }
80
81 rtc::FakeNetworkManager* network() { return network_; }
82
83 void set_network(rtc::FakeNetworkManager* network) { network_ = network; }
84
Jonas Oreland1cd39fa2018-10-11 07:47:12 +020085 // The port allocator used by this PC.
86 cricket::PortAllocator* port_allocator_;
87
Steve Antonf1c6db12017-10-13 11:13:35 -070088 private:
89 rtc::FakeNetworkManager* network_;
90};
91
Steve Anton46d926a2018-01-23 10:23:06 -080092class PeerConnectionIceBaseTest : public ::testing::Test {
Steve Antonf1c6db12017-10-13 11:13:35 -070093 protected:
Steve Anton46d926a2018-01-23 10:23:06 -080094 typedef std::unique_ptr<PeerConnectionWrapperForIceTest> WrapperPtr;
Steve Antonf1c6db12017-10-13 11:13:35 -070095
Steve Anton46d926a2018-01-23 10:23:06 -080096 explicit PeerConnectionIceBaseTest(SdpSemantics sdp_semantics)
97 : vss_(new rtc::VirtualSocketServer()),
98 main_(vss_.get()),
99 sdp_semantics_(sdp_semantics) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700100#ifdef WEBRTC_ANDROID
101 InitializeAndroidObjects();
102#endif
103 pc_factory_ = CreatePeerConnectionFactory(
104 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Anders Carlsson67537952018-05-03 11:28:29 +0200105 rtc::scoped_refptr<AudioDeviceModule>(FakeAudioCaptureModule::Create()),
106 CreateBuiltinAudioEncoderFactory(), CreateBuiltinAudioDecoderFactory(),
107 CreateBuiltinVideoEncoderFactory(), CreateBuiltinVideoDecoderFactory(),
108 nullptr /* audio_mixer */, nullptr /* audio_processing */);
Steve Antonf1c6db12017-10-13 11:13:35 -0700109 }
110
111 WrapperPtr CreatePeerConnection() {
112 return CreatePeerConnection(RTCConfiguration());
113 }
114
115 WrapperPtr CreatePeerConnection(const RTCConfiguration& config) {
116 auto* fake_network = NewFakeNetwork();
117 auto port_allocator =
Karl Wiberg918f50c2018-07-05 11:40:33 +0200118 absl::make_unique<cricket::BasicPortAllocator>(fake_network);
Steve Antonf1c6db12017-10-13 11:13:35 -0700119 port_allocator->set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
120 cricket::PORTALLOCATOR_DISABLE_RELAY);
121 port_allocator->set_step_delay(cricket::kMinimumStepDelay);
Steve Anton46d926a2018-01-23 10:23:06 -0800122 RTCConfiguration modified_config = config;
123 modified_config.sdp_semantics = sdp_semantics_;
Karl Wiberg918f50c2018-07-05 11:40:33 +0200124 auto observer = absl::make_unique<MockPeerConnectionObserver>();
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200125 auto port_allocator_copy = port_allocator.get();
Steve Antonf1c6db12017-10-13 11:13:35 -0700126 auto pc = pc_factory_->CreatePeerConnection(
Steve Anton46d926a2018-01-23 10:23:06 -0800127 modified_config, std::move(port_allocator), nullptr, observer.get());
Steve Antonf1c6db12017-10-13 11:13:35 -0700128 if (!pc) {
129 return nullptr;
130 }
131
Yves Gerey4e933292018-10-31 15:36:05 +0100132 observer->SetPeerConnectionInterface(pc.get());
Karl Wiberg918f50c2018-07-05 11:40:33 +0200133 auto wrapper = absl::make_unique<PeerConnectionWrapperForIceTest>(
Steve Antonf1c6db12017-10-13 11:13:35 -0700134 pc_factory_, pc, std::move(observer));
135 wrapper->set_network(fake_network);
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200136 wrapper->port_allocator_ = port_allocator_copy;
Steve Antonf1c6db12017-10-13 11:13:35 -0700137 return wrapper;
138 }
139
140 // Accepts the same arguments as CreatePeerConnection and adds default audio
141 // and video tracks.
142 template <typename... Args>
143 WrapperPtr CreatePeerConnectionWithAudioVideo(Args&&... args) {
144 auto wrapper = CreatePeerConnection(std::forward<Args>(args)...);
145 if (!wrapper) {
146 return nullptr;
147 }
Steve Anton8d3444d2017-10-20 15:30:51 -0700148 wrapper->AddAudioTrack("a");
149 wrapper->AddVideoTrack("v");
Steve Antonf1c6db12017-10-13 11:13:35 -0700150 return wrapper;
151 }
152
153 cricket::Candidate CreateLocalUdpCandidate(
154 const rtc::SocketAddress& address) {
155 cricket::Candidate candidate;
156 candidate.set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT);
157 candidate.set_protocol(cricket::UDP_PROTOCOL_NAME);
158 candidate.set_address(address);
159 candidate.set_type(cricket::LOCAL_PORT_TYPE);
160 return candidate;
161 }
162
163 // Remove all ICE ufrag/pwd lines from the given session description.
164 void RemoveIceUfragPwd(SessionDescriptionInterface* sdesc) {
165 SetIceUfragPwd(sdesc, "", "");
166 }
167
168 // Sets all ICE ufrag/pwds on the given session description.
169 void SetIceUfragPwd(SessionDescriptionInterface* sdesc,
170 const std::string& ufrag,
171 const std::string& pwd) {
172 auto* desc = sdesc->description();
173 for (const auto& content : desc->contents()) {
174 auto* transport_info = desc->GetTransportInfoByName(content.name);
175 transport_info->description.ice_ufrag = ufrag;
176 transport_info->description.ice_pwd = pwd;
177 }
178 }
179
Qingsi Wange1692722017-11-29 13:27:20 -0800180 // Set ICE mode on the given session description.
181 void SetIceMode(SessionDescriptionInterface* sdesc,
182 const cricket::IceMode ice_mode) {
183 auto* desc = sdesc->description();
184 for (const auto& content : desc->contents()) {
185 auto* transport_info = desc->GetTransportInfoByName(content.name);
186 transport_info->description.ice_mode = ice_mode;
187 }
188 }
189
Steve Antonf1c6db12017-10-13 11:13:35 -0700190 cricket::TransportDescription* GetFirstTransportDescription(
191 SessionDescriptionInterface* sdesc) {
192 auto* desc = sdesc->description();
193 RTC_DCHECK(desc->contents().size() > 0);
194 auto* transport_info =
195 desc->GetTransportInfoByName(desc->contents()[0].name);
196 RTC_DCHECK(transport_info);
197 return &transport_info->description;
198 }
199
200 const cricket::TransportDescription* GetFirstTransportDescription(
201 const SessionDescriptionInterface* sdesc) {
202 auto* desc = sdesc->description();
203 RTC_DCHECK(desc->contents().size() > 0);
204 auto* transport_info =
205 desc->GetTransportInfoByName(desc->contents()[0].name);
206 RTC_DCHECK(transport_info);
207 return &transport_info->description;
208 }
209
Qingsi Wange1692722017-11-29 13:27:20 -0800210 // TODO(qingsi): Rewrite this method in terms of the standard IceTransport
211 // after it is implemented.
212 cricket::IceRole GetIceRole(const WrapperPtr& pc_wrapper_ptr) {
Mirko Bonadeie97de912017-12-13 11:29:34 +0100213 auto* pc_proxy =
214 static_cast<PeerConnectionProxyWithInternal<PeerConnectionInterface>*>(
215 pc_wrapper_ptr->pc());
216 PeerConnection* pc = static_cast<PeerConnection*>(pc_proxy->internal());
Mirko Bonadei739baf02019-01-27 17:29:42 +0100217 for (const auto& transceiver : pc->GetTransceiversInternal()) {
Steve Anton69470252018-02-09 11:43:08 -0800218 if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800219 // TODO(amithi): This test seems to be using a method that should not
220 // be public |rtp_packet_transport|. Because the test is not mocking
221 // the channels or transceiver, workaround will be to |static_cast|
222 // the channel until the method is rewritten.
223 cricket::BaseChannel* channel = static_cast<cricket::BaseChannel*>(
224 transceiver->internal()->channel());
Steve Anton46d926a2018-01-23 10:23:06 -0800225 if (channel) {
Zhi Huange830e682018-03-30 10:48:35 -0700226 auto dtls_transport = static_cast<cricket::DtlsTransportInternal*>(
227 channel->rtp_packet_transport());
228 return dtls_transport->ice_transport()->GetIceRole();
Steve Anton46d926a2018-01-23 10:23:06 -0800229 }
230 }
231 }
232 RTC_NOTREACHED();
233 return cricket::ICEROLE_UNKNOWN;
Qingsi Wange1692722017-11-29 13:27:20 -0800234 }
235
Steve Antonf1c6db12017-10-13 11:13:35 -0700236 bool AddCandidateToFirstTransport(cricket::Candidate* candidate,
237 SessionDescriptionInterface* sdesc) {
238 auto* desc = sdesc->description();
239 RTC_DCHECK(desc->contents().size() > 0);
240 const auto& first_content = desc->contents()[0];
241 candidate->set_transport_name(first_content.name);
Steve Anton27ab0e52018-07-23 15:11:53 -0700242 std::unique_ptr<IceCandidateInterface> jsep_candidate =
243 CreateIceCandidate(first_content.name, 0, *candidate);
244 return sdesc->AddCandidate(jsep_candidate.get());
Steve Antonf1c6db12017-10-13 11:13:35 -0700245 }
246
247 rtc::FakeNetworkManager* NewFakeNetwork() {
248 // The PeerConnection's port allocator is tied to the PeerConnection's
249 // lifetime and expects the underlying NetworkManager to outlive it. That
250 // prevents us from having the PeerConnectionWrapper own the fake network.
251 // Therefore, the test fixture will own all the fake networks even though
252 // tests should access the fake network through the PeerConnectionWrapper.
253 auto* fake_network = new rtc::FakeNetworkManager();
254 fake_networks_.emplace_back(fake_network);
255 return fake_network;
256 }
257
258 std::unique_ptr<rtc::VirtualSocketServer> vss_;
259 rtc::AutoSocketServerThread main_;
260 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_;
261 std::vector<std::unique_ptr<rtc::FakeNetworkManager>> fake_networks_;
Steve Anton46d926a2018-01-23 10:23:06 -0800262 const SdpSemantics sdp_semantics_;
263};
264
265class PeerConnectionIceTest
266 : public PeerConnectionIceBaseTest,
267 public ::testing::WithParamInterface<SdpSemantics> {
268 protected:
Harald Alvestrand76829d72018-07-18 23:24:36 +0200269 PeerConnectionIceTest() : PeerConnectionIceBaseTest(GetParam()) {
270 webrtc::metrics::Reset();
271 }
Steve Antonf1c6db12017-10-13 11:13:35 -0700272};
273
274::testing::AssertionResult AssertCandidatesEqual(const char* a_expr,
275 const char* b_expr,
276 const cricket::Candidate& a,
277 const cricket::Candidate& b) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200278 rtc::StringBuilder failure_info;
Steve Antonf1c6db12017-10-13 11:13:35 -0700279 if (a.component() != b.component()) {
280 failure_info << "\ncomponent: " << a.component() << " != " << b.component();
281 }
282 if (a.protocol() != b.protocol()) {
283 failure_info << "\nprotocol: " << a.protocol() << " != " << b.protocol();
284 }
285 if (a.address() != b.address()) {
286 failure_info << "\naddress: " << a.address().ToString()
287 << " != " << b.address().ToString();
288 }
289 if (a.type() != b.type()) {
290 failure_info << "\ntype: " << a.type() << " != " << b.type();
291 }
292 std::string failure_info_str = failure_info.str();
293 if (failure_info_str.empty()) {
294 return ::testing::AssertionSuccess();
295 } else {
296 return ::testing::AssertionFailure()
297 << a_expr << " and " << b_expr << " are not equal"
298 << failure_info_str;
299 }
300}
301
Steve Anton46d926a2018-01-23 10:23:06 -0800302TEST_P(PeerConnectionIceTest, OfferContainsGatheredCandidates) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700303 const SocketAddress kLocalAddress("1.1.1.1", 0);
304
305 auto caller = CreatePeerConnectionWithAudioVideo();
306 caller->network()->AddInterface(kLocalAddress);
307
308 // Start ICE candidate gathering by setting the local offer.
309 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
310
311 EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kIceCandidatesTimeout);
312
313 auto offer = caller->CreateOffer();
314 EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(0).size());
315 EXPECT_EQ(caller->observer()->GetCandidatesByMline(0).size(),
316 offer->candidates(0)->count());
317 EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(1).size());
318 EXPECT_EQ(caller->observer()->GetCandidatesByMline(1).size(),
319 offer->candidates(1)->count());
320}
321
Steve Anton46d926a2018-01-23 10:23:06 -0800322TEST_P(PeerConnectionIceTest, AnswerContainsGatheredCandidates) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700323 const SocketAddress kCallerAddress("1.1.1.1", 0);
324
325 auto caller = CreatePeerConnectionWithAudioVideo();
326 auto callee = CreatePeerConnectionWithAudioVideo();
327 caller->network()->AddInterface(kCallerAddress);
328
329 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
330 ASSERT_TRUE(callee->SetLocalDescription(callee->CreateAnswer()));
331
332 EXPECT_TRUE_WAIT(callee->IsIceGatheringDone(), kIceCandidatesTimeout);
333
Steve Antondffead82018-02-06 10:31:29 -0800334 auto* answer = callee->pc()->local_description();
Steve Antonf1c6db12017-10-13 11:13:35 -0700335 EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(0).size());
336 EXPECT_EQ(callee->observer()->GetCandidatesByMline(0).size(),
337 answer->candidates(0)->count());
338 EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(1).size());
339 EXPECT_EQ(callee->observer()->GetCandidatesByMline(1).size(),
340 answer->candidates(1)->count());
341}
342
Steve Anton46d926a2018-01-23 10:23:06 -0800343TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700344 CanSetRemoteSessionDescriptionWithRemoteCandidates) {
345 const SocketAddress kCallerAddress("1.1.1.1", 1111);
346
347 auto caller = CreatePeerConnectionWithAudioVideo();
348 auto callee = CreatePeerConnectionWithAudioVideo();
349
350 auto offer = caller->CreateOfferAndSetAsLocal();
351 cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress);
352 AddCandidateToFirstTransport(&candidate, offer.get());
353
354 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
355 auto remote_candidates = callee->GetIceCandidatesFromRemoteDescription();
356 ASSERT_EQ(1u, remote_candidates.size());
357 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate,
358 remote_candidates[0]->candidate());
359}
360
Steve Anton46d926a2018-01-23 10:23:06 -0800361TEST_P(PeerConnectionIceTest, SetLocalDescriptionFailsIfNoIceCredentials) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700362 auto caller = CreatePeerConnectionWithAudioVideo();
363
364 auto offer = caller->CreateOffer();
365 RemoveIceUfragPwd(offer.get());
366
367 EXPECT_FALSE(caller->SetLocalDescription(std::move(offer)));
368}
369
Steve Anton46d926a2018-01-23 10:23:06 -0800370TEST_P(PeerConnectionIceTest, SetRemoteDescriptionFailsIfNoIceCredentials) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700371 auto caller = CreatePeerConnectionWithAudioVideo();
372 auto callee = CreatePeerConnectionWithAudioVideo();
373
374 auto offer = caller->CreateOfferAndSetAsLocal();
375 RemoveIceUfragPwd(offer.get());
376
377 EXPECT_FALSE(callee->SetRemoteDescription(std::move(offer)));
378}
379
Steve Antonf764cf42018-05-01 14:32:17 -0700380// Test that doing an offer/answer exchange with no transport (i.e., no data
381// channel or media) results in the ICE connection state staying at New.
382TEST_P(PeerConnectionIceTest,
383 OfferAnswerWithNoTransportsDoesNotChangeIceConnectionState) {
384 auto caller = CreatePeerConnection();
385 auto callee = CreatePeerConnection();
386
387 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
388
389 EXPECT_EQ(PeerConnectionInterface::kIceConnectionNew,
390 caller->pc()->ice_connection_state());
391 EXPECT_EQ(PeerConnectionInterface::kIceConnectionNew,
392 callee->pc()->ice_connection_state());
393}
394
Steve Antonf1c6db12017-10-13 11:13:35 -0700395// The following group tests that ICE candidates are not generated before
396// SetLocalDescription is called on a PeerConnection.
397
Steve Anton46d926a2018-01-23 10:23:06 -0800398TEST_P(PeerConnectionIceTest, NoIceCandidatesBeforeSetLocalDescription) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700399 const SocketAddress kLocalAddress("1.1.1.1", 0);
400
401 auto caller = CreatePeerConnectionWithAudioVideo();
402 caller->network()->AddInterface(kLocalAddress);
403
404 // Pump for 1 second and verify that no candidates are generated.
405 rtc::Thread::Current()->ProcessMessages(1000);
406
407 EXPECT_EQ(0u, caller->observer()->candidates_.size());
408}
Steve Anton46d926a2018-01-23 10:23:06 -0800409TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700410 NoIceCandidatesBeforeAnswerSetAsLocalDescription) {
411 const SocketAddress kCallerAddress("1.1.1.1", 1111);
412
413 auto caller = CreatePeerConnectionWithAudioVideo();
414 auto callee = CreatePeerConnectionWithAudioVideo();
415 caller->network()->AddInterface(kCallerAddress);
416
417 auto offer = caller->CreateOfferAndSetAsLocal();
418 cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress);
419 AddCandidateToFirstTransport(&candidate, offer.get());
420 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
421
422 // Pump for 1 second and verify that no candidates are generated.
423 rtc::Thread::Current()->ProcessMessages(1000);
424
425 EXPECT_EQ(0u, callee->observer()->candidates_.size());
426}
427
Steve Anton46d926a2018-01-23 10:23:06 -0800428TEST_P(PeerConnectionIceTest, CannotAddCandidateWhenRemoteDescriptionNotSet) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700429 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
430
431 auto caller = CreatePeerConnectionWithAudioVideo();
432 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
Steve Anton27ab0e52018-07-23 15:11:53 -0700433 std::unique_ptr<IceCandidateInterface> jsep_candidate =
434 CreateIceCandidate(cricket::CN_AUDIO, 0, candidate);
Steve Antonf1c6db12017-10-13 11:13:35 -0700435
Steve Anton27ab0e52018-07-23 15:11:53 -0700436 EXPECT_FALSE(caller->pc()->AddIceCandidate(jsep_candidate.get()));
Steve Antonf1c6db12017-10-13 11:13:35 -0700437
438 caller->CreateOfferAndSetAsLocal();
439
Steve Anton27ab0e52018-07-23 15:11:53 -0700440 EXPECT_FALSE(caller->pc()->AddIceCandidate(jsep_candidate.get()));
Steve Antonb443dfe2019-03-05 14:09:49 -0800441 EXPECT_THAT(webrtc::metrics::Samples("WebRTC.PeerConnection.AddIceCandidate"),
442 ElementsAre(Pair(kAddIceCandidateFailNoRemoteDescription, 2)));
Steve Antonf1c6db12017-10-13 11:13:35 -0700443}
444
Steve Antonc79268f2018-04-24 09:54:10 -0700445TEST_P(PeerConnectionIceTest, CannotAddCandidateWhenPeerConnectionClosed) {
446 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
447
448 auto caller = CreatePeerConnectionWithAudioVideo();
449 auto callee = CreatePeerConnectionWithAudioVideo();
450
451 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
452
453 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
454 auto* audio_content = cricket::GetFirstAudioContent(
455 caller->pc()->local_description()->description());
Steve Anton27ab0e52018-07-23 15:11:53 -0700456 std::unique_ptr<IceCandidateInterface> jsep_candidate =
457 CreateIceCandidate(audio_content->name, 0, candidate);
Steve Antonc79268f2018-04-24 09:54:10 -0700458
459 caller->pc()->Close();
460
Steve Anton27ab0e52018-07-23 15:11:53 -0700461 EXPECT_FALSE(caller->pc()->AddIceCandidate(jsep_candidate.get()));
Steve Antonc79268f2018-04-24 09:54:10 -0700462}
463
Steve Anton46d926a2018-01-23 10:23:06 -0800464TEST_P(PeerConnectionIceTest, DuplicateIceCandidateIgnoredWhenAdded) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700465 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
466
467 auto caller = CreatePeerConnectionWithAudioVideo();
468 auto callee = CreatePeerConnectionWithAudioVideo();
469
470 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
471 ASSERT_TRUE(
472 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
473
474 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
475 caller->AddIceCandidate(&candidate);
476 EXPECT_TRUE(caller->AddIceCandidate(&candidate));
477 EXPECT_EQ(1u, caller->GetIceCandidatesFromRemoteDescription().size());
478}
479
Steve Anton46d926a2018-01-23 10:23:06 -0800480TEST_P(PeerConnectionIceTest,
Steve Antonc79268f2018-04-24 09:54:10 -0700481 CannotRemoveIceCandidatesWhenPeerConnectionClosed) {
482 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
483
484 auto caller = CreatePeerConnectionWithAudioVideo();
485 auto callee = CreatePeerConnectionWithAudioVideo();
486
487 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
488
489 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
490 auto* audio_content = cricket::GetFirstAudioContent(
491 caller->pc()->local_description()->description());
Steve Anton27ab0e52018-07-23 15:11:53 -0700492 std::unique_ptr<IceCandidateInterface> ice_candidate =
493 CreateIceCandidate(audio_content->name, 0, candidate);
Steve Antonc79268f2018-04-24 09:54:10 -0700494
Steve Anton27ab0e52018-07-23 15:11:53 -0700495 ASSERT_TRUE(caller->pc()->AddIceCandidate(ice_candidate.get()));
Steve Antonc79268f2018-04-24 09:54:10 -0700496
497 caller->pc()->Close();
498
499 EXPECT_FALSE(caller->pc()->RemoveIceCandidates({candidate}));
500}
501
502TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700503 AddRemoveCandidateWithEmptyTransportDoesNotCrash) {
504 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
505
506 auto caller = CreatePeerConnectionWithAudioVideo();
507 auto callee = CreatePeerConnectionWithAudioVideo();
508
509 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
510 ASSERT_TRUE(
511 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
512
513 // |candidate.transport_name()| is empty.
514 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
Steve Anton46d926a2018-01-23 10:23:06 -0800515 auto* audio_content = cricket::GetFirstAudioContent(
516 caller->pc()->local_description()->description());
Steve Anton27ab0e52018-07-23 15:11:53 -0700517 std::unique_ptr<IceCandidateInterface> ice_candidate =
518 CreateIceCandidate(audio_content->name, 0, candidate);
519 EXPECT_TRUE(caller->pc()->AddIceCandidate(ice_candidate.get()));
Steve Antonf1c6db12017-10-13 11:13:35 -0700520 EXPECT_TRUE(caller->pc()->RemoveIceCandidates({candidate}));
521}
522
Steve Anton46d926a2018-01-23 10:23:06 -0800523TEST_P(PeerConnectionIceTest, RemoveCandidateRemovesFromRemoteDescription) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700524 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
525
526 auto caller = CreatePeerConnectionWithAudioVideo();
527 auto callee = CreatePeerConnectionWithAudioVideo();
528
529 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
530 ASSERT_TRUE(
531 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
532
533 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
534 ASSERT_TRUE(caller->AddIceCandidate(&candidate));
535 EXPECT_TRUE(caller->pc()->RemoveIceCandidates({candidate}));
536 EXPECT_EQ(0u, caller->GetIceCandidatesFromRemoteDescription().size());
537}
538
539// Test that if a candidate is added via AddIceCandidate and via an updated
540// remote description, then both candidates appear in the stored remote
541// description.
Steve Anton46d926a2018-01-23 10:23:06 -0800542TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700543 CandidateInSubsequentOfferIsAddedToRemoteDescription) {
544 const SocketAddress kCallerAddress1("1.1.1.1", 1111);
545 const SocketAddress kCallerAddress2("2.2.2.2", 2222);
546
547 auto caller = CreatePeerConnectionWithAudioVideo();
548 auto callee = CreatePeerConnectionWithAudioVideo();
549
550 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
551 ASSERT_TRUE(
552 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
553
554 // Add one candidate via |AddIceCandidate|.
555 cricket::Candidate candidate1 = CreateLocalUdpCandidate(kCallerAddress1);
556 ASSERT_TRUE(callee->AddIceCandidate(&candidate1));
557
558 // Add the second candidate via a reoffer.
559 auto offer = caller->CreateOffer();
560 cricket::Candidate candidate2 = CreateLocalUdpCandidate(kCallerAddress2);
561 AddCandidateToFirstTransport(&candidate2, offer.get());
562
563 // Expect both candidates to appear in the callee's remote description.
564 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
565 EXPECT_EQ(2u, callee->GetIceCandidatesFromRemoteDescription().size());
566}
567
568// The follow test verifies that SetLocal/RemoteDescription fails when an offer
569// has either ICE ufrag/pwd too short or too long and succeeds otherwise.
570// The standard (https://tools.ietf.org/html/rfc5245#section-15.4) says that
571// pwd must be 22-256 characters and ufrag must be 4-256 characters.
Steve Anton46d926a2018-01-23 10:23:06 -0800572TEST_P(PeerConnectionIceTest, VerifyUfragPwdLength) {
Yves Gerey665174f2018-06-19 15:03:05 +0200573 auto set_local_description_with_ufrag_pwd_length = [this](int ufrag_len,
574 int pwd_len) {
575 auto pc = CreatePeerConnectionWithAudioVideo();
576 auto offer = pc->CreateOffer();
577 SetIceUfragPwd(offer.get(), std::string(ufrag_len, 'x'),
578 std::string(pwd_len, 'x'));
579 return pc->SetLocalDescription(std::move(offer));
580 };
Steve Antonf1c6db12017-10-13 11:13:35 -0700581
Yves Gerey665174f2018-06-19 15:03:05 +0200582 auto set_remote_description_with_ufrag_pwd_length = [this](int ufrag_len,
583 int pwd_len) {
584 auto pc = CreatePeerConnectionWithAudioVideo();
585 auto offer = pc->CreateOffer();
586 SetIceUfragPwd(offer.get(), std::string(ufrag_len, 'x'),
587 std::string(pwd_len, 'x'));
588 return pc->SetRemoteDescription(std::move(offer));
589 };
Steve Antonf1c6db12017-10-13 11:13:35 -0700590
591 EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(3, 22));
592 EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(3, 22));
593 EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(257, 22));
594 EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(257, 22));
595 EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(4, 21));
596 EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(4, 21));
597 EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(4, 257));
598 EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(4, 257));
599 EXPECT_TRUE(set_local_description_with_ufrag_pwd_length(4, 22));
600 EXPECT_TRUE(set_remote_description_with_ufrag_pwd_length(4, 22));
601 EXPECT_TRUE(set_local_description_with_ufrag_pwd_length(256, 256));
602 EXPECT_TRUE(set_remote_description_with_ufrag_pwd_length(256, 256));
603}
604
605::testing::AssertionResult AssertIpInCandidates(
606 const char* address_expr,
607 const char* candidates_expr,
608 const SocketAddress& address,
609 const std::vector<IceCandidateInterface*> candidates) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200610 rtc::StringBuilder candidate_hosts;
Steve Antonf1c6db12017-10-13 11:13:35 -0700611 for (const auto* candidate : candidates) {
612 const auto& candidate_ip = candidate->candidate().address().ipaddr();
613 if (candidate_ip == address.ipaddr()) {
614 return ::testing::AssertionSuccess();
615 }
Jonas Olssonabbe8412018-04-03 13:40:05 +0200616 candidate_hosts << "\n" << candidate_ip.ToString();
Steve Antonf1c6db12017-10-13 11:13:35 -0700617 }
618 return ::testing::AssertionFailure()
619 << address_expr << " (host " << address.HostAsURIString()
620 << ") not in " << candidates_expr
621 << " which have the following address hosts:" << candidate_hosts.str();
622}
623
Steve Anton46d926a2018-01-23 10:23:06 -0800624TEST_P(PeerConnectionIceTest, CandidatesGeneratedForEachLocalInterface) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700625 const SocketAddress kLocalAddress1("1.1.1.1", 0);
626 const SocketAddress kLocalAddress2("2.2.2.2", 0);
627
628 auto caller = CreatePeerConnectionWithAudioVideo();
629 caller->network()->AddInterface(kLocalAddress1);
630 caller->network()->AddInterface(kLocalAddress2);
631
632 caller->CreateOfferAndSetAsLocal();
633 EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kIceCandidatesTimeout);
634
635 auto candidates = caller->observer()->GetCandidatesByMline(0);
636 EXPECT_PRED_FORMAT2(AssertIpInCandidates, kLocalAddress1, candidates);
637 EXPECT_PRED_FORMAT2(AssertIpInCandidates, kLocalAddress2, candidates);
638}
639
Steve Anton46d926a2018-01-23 10:23:06 -0800640TEST_P(PeerConnectionIceTest, TrickledSingleCandidateAddedToRemoteDescription) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700641 const SocketAddress kCallerAddress("1.1.1.1", 1111);
642
643 auto caller = CreatePeerConnectionWithAudioVideo();
644 auto callee = CreatePeerConnectionWithAudioVideo();
645
646 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
647
648 cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress);
649 callee->AddIceCandidate(&candidate);
650 auto candidates = callee->GetIceCandidatesFromRemoteDescription();
651 ASSERT_EQ(1u, candidates.size());
652 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate,
653 candidates[0]->candidate());
654}
655
Steve Anton46d926a2018-01-23 10:23:06 -0800656TEST_P(PeerConnectionIceTest, TwoTrickledCandidatesAddedToRemoteDescription) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700657 const SocketAddress kCalleeAddress1("1.1.1.1", 1111);
658 const SocketAddress kCalleeAddress2("2.2.2.2", 2222);
659
660 auto caller = CreatePeerConnectionWithAudioVideo();
661 auto callee = CreatePeerConnectionWithAudioVideo();
662
663 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
664 ASSERT_TRUE(
665 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
666
667 cricket::Candidate candidate1 = CreateLocalUdpCandidate(kCalleeAddress1);
668 caller->AddIceCandidate(&candidate1);
669
670 cricket::Candidate candidate2 = CreateLocalUdpCandidate(kCalleeAddress2);
671 caller->AddIceCandidate(&candidate2);
672
673 auto candidates = caller->GetIceCandidatesFromRemoteDescription();
674 ASSERT_EQ(2u, candidates.size());
675 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate1,
676 candidates[0]->candidate());
677 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate2,
678 candidates[1]->candidate());
679}
680
Steve Anton46d926a2018-01-23 10:23:06 -0800681TEST_P(PeerConnectionIceTest, LocalDescriptionUpdatedWhenContinualGathering) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700682 const SocketAddress kLocalAddress("1.1.1.1", 0);
683
684 RTCConfiguration config;
685 config.continual_gathering_policy =
686 PeerConnectionInterface::GATHER_CONTINUALLY;
687 auto caller = CreatePeerConnectionWithAudioVideo(config);
688 caller->network()->AddInterface(kLocalAddress);
689
690 // Start ICE candidate gathering by setting the local offer.
691 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
692
693 // Since we're using continual gathering, we won't get "gathering done".
694 EXPECT_TRUE_WAIT(
695 caller->pc()->local_description()->candidates(0)->count() > 0,
696 kIceCandidatesTimeout);
697}
698
699// Test that when continual gathering is enabled, and a network interface goes
700// down, the candidate is signaled as removed and removed from the local
701// description.
Steve Anton46d926a2018-01-23 10:23:06 -0800702TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700703 LocalCandidatesRemovedWhenNetworkDownIfGatheringContinually) {
704 const SocketAddress kLocalAddress("1.1.1.1", 0);
705
706 RTCConfiguration config;
707 config.continual_gathering_policy =
708 PeerConnectionInterface::GATHER_CONTINUALLY;
709 auto caller = CreatePeerConnectionWithAudioVideo(config);
710 caller->network()->AddInterface(kLocalAddress);
711
712 // Start ICE candidate gathering by setting the local offer.
713 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
714
715 EXPECT_TRUE_WAIT(
716 caller->pc()->local_description()->candidates(0)->count() > 0,
717 kIceCandidatesTimeout);
718
719 // Remove the only network interface, causing the PeerConnection to signal
720 // the removal of all candidates derived from this interface.
721 caller->network()->RemoveInterface(kLocalAddress);
722
723 EXPECT_EQ_WAIT(0u, caller->pc()->local_description()->candidates(0)->count(),
724 kIceCandidatesTimeout);
725 EXPECT_LT(0, caller->observer()->num_candidates_removed_);
726}
727
Steve Anton46d926a2018-01-23 10:23:06 -0800728TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700729 LocalCandidatesNotRemovedWhenNetworkDownIfGatheringOnce) {
730 const SocketAddress kLocalAddress("1.1.1.1", 0);
731
732 RTCConfiguration config;
733 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
734 auto caller = CreatePeerConnectionWithAudioVideo(config);
735 caller->network()->AddInterface(kLocalAddress);
736
737 // Start ICE candidate gathering by setting the local offer.
738 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
739
740 EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kIceCandidatesTimeout);
741
742 caller->network()->RemoveInterface(kLocalAddress);
743
744 // Verify that the local candidates are not removed;
745 rtc::Thread::Current()->ProcessMessages(1000);
746 EXPECT_EQ(0, caller->observer()->num_candidates_removed_);
747}
748
749// The following group tests that when an offer includes a new ufrag or pwd
750// (indicating an ICE restart) the old candidates are removed and new candidates
751// added to the remote description.
752
Steve Anton46d926a2018-01-23 10:23:06 -0800753TEST_P(PeerConnectionIceTest, IceRestartOfferClearsExistingCandidate) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700754 const SocketAddress kCallerAddress("1.1.1.1", 1111);
755
756 auto caller = CreatePeerConnectionWithAudioVideo();
757 auto callee = CreatePeerConnectionWithAudioVideo();
758
Amit Hilbuchae3df542019-01-07 12:13:08 -0800759 auto offer = caller->CreateOfferAndSetAsLocal();
Steve Antonf1c6db12017-10-13 11:13:35 -0700760 cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress);
761 AddCandidateToFirstTransport(&candidate, offer.get());
762
763 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
764
765 RTCOfferAnswerOptions options;
766 options.ice_restart = true;
Amit Hilbuchae3df542019-01-07 12:13:08 -0800767 ASSERT_TRUE(
768 callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal(options)));
Steve Antonf1c6db12017-10-13 11:13:35 -0700769
770 EXPECT_EQ(0u, callee->GetIceCandidatesFromRemoteDescription().size());
771}
Steve Anton46d926a2018-01-23 10:23:06 -0800772TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700773 IceRestartOfferCandidateReplacesExistingCandidate) {
774 const SocketAddress kFirstCallerAddress("1.1.1.1", 1111);
775 const SocketAddress kRestartedCallerAddress("2.2.2.2", 2222);
776
777 auto caller = CreatePeerConnectionWithAudioVideo();
778 auto callee = CreatePeerConnectionWithAudioVideo();
779
Amit Hilbuchae3df542019-01-07 12:13:08 -0800780 auto offer = caller->CreateOfferAndSetAsLocal();
Steve Antonf1c6db12017-10-13 11:13:35 -0700781 cricket::Candidate old_candidate =
782 CreateLocalUdpCandidate(kFirstCallerAddress);
783 AddCandidateToFirstTransport(&old_candidate, offer.get());
784
785 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
786
787 RTCOfferAnswerOptions options;
788 options.ice_restart = true;
Amit Hilbuchae3df542019-01-07 12:13:08 -0800789 auto restart_offer = caller->CreateOfferAndSetAsLocal(options);
Steve Antonf1c6db12017-10-13 11:13:35 -0700790 cricket::Candidate new_candidate =
791 CreateLocalUdpCandidate(kRestartedCallerAddress);
792 AddCandidateToFirstTransport(&new_candidate, restart_offer.get());
793
794 ASSERT_TRUE(callee->SetRemoteDescription(std::move(restart_offer)));
795
796 auto remote_candidates = callee->GetIceCandidatesFromRemoteDescription();
797 ASSERT_EQ(1u, remote_candidates.size());
798 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, new_candidate,
799 remote_candidates[0]->candidate());
800}
801
802// Test that if there is not an ICE restart (i.e., nothing changes), then the
803// answer to a later offer should have the same ufrag/pwd as the first answer.
Steve Anton46d926a2018-01-23 10:23:06 -0800804TEST_P(PeerConnectionIceTest, LaterAnswerHasSameIceCredentialsIfNoIceRestart) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700805 auto caller = CreatePeerConnectionWithAudioVideo();
806 auto callee = CreatePeerConnectionWithAudioVideo();
807
808 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
809 ASSERT_TRUE(
810 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
811
812 // Re-offer.
813 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
814
815 auto answer = callee->CreateAnswer();
816 auto* answer_transport_desc = GetFirstTransportDescription(answer.get());
817 auto* local_transport_desc =
818 GetFirstTransportDescription(callee->pc()->local_description());
819
820 EXPECT_EQ(answer_transport_desc->ice_ufrag, local_transport_desc->ice_ufrag);
821 EXPECT_EQ(answer_transport_desc->ice_pwd, local_transport_desc->ice_pwd);
822}
823
824// The following parameterized test verifies that if an offer is sent with a
825// modified ICE ufrag and/or ICE pwd, then the answer should identify that the
826// other side has initiated an ICE restart and generate a new ufrag and pwd.
827// RFC 5245 says: "If the offer contained a change in the a=ice-ufrag or
828// a=ice-pwd attributes compared to the previous SDP from the peer, it
829// indicates that ICE is restarting for this media stream."
830
Steve Anton46d926a2018-01-23 10:23:06 -0800831class PeerConnectionIceUfragPwdAnswerTest
832 : public PeerConnectionIceBaseTest,
833 public ::testing::WithParamInterface<
834 std::tuple<SdpSemantics, std::tuple<bool, bool>>> {
Steve Antonf1c6db12017-10-13 11:13:35 -0700835 protected:
Steve Anton46d926a2018-01-23 10:23:06 -0800836 PeerConnectionIceUfragPwdAnswerTest()
837 : PeerConnectionIceBaseTest(std::get<0>(GetParam())) {
838 auto param = std::get<1>(GetParam());
839 offer_new_ufrag_ = std::get<0>(param);
840 offer_new_pwd_ = std::get<1>(param);
Steve Antonf1c6db12017-10-13 11:13:35 -0700841 }
842
843 bool offer_new_ufrag_;
844 bool offer_new_pwd_;
845};
846
Steve Anton46d926a2018-01-23 10:23:06 -0800847TEST_P(PeerConnectionIceUfragPwdAnswerTest, TestIncludedInAnswer) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700848 auto caller = CreatePeerConnectionWithAudioVideo();
849 auto callee = CreatePeerConnectionWithAudioVideo();
850
851 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
852 ASSERT_TRUE(
853 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
854
855 auto offer = caller->CreateOffer();
856 auto* offer_transport_desc = GetFirstTransportDescription(offer.get());
857 if (offer_new_ufrag_) {
858 offer_transport_desc->ice_ufrag += "_new";
859 }
860 if (offer_new_pwd_) {
861 offer_transport_desc->ice_pwd += "_new";
862 }
863
864 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
865
866 auto answer = callee->CreateAnswer();
867 auto* answer_transport_desc = GetFirstTransportDescription(answer.get());
868 auto* local_transport_desc =
869 GetFirstTransportDescription(callee->pc()->local_description());
870
871 EXPECT_NE(answer_transport_desc->ice_ufrag, local_transport_desc->ice_ufrag);
872 EXPECT_NE(answer_transport_desc->ice_pwd, local_transport_desc->ice_pwd);
873}
874
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100875INSTANTIATE_TEST_SUITE_P(
Steve Anton46d926a2018-01-23 10:23:06 -0800876 PeerConnectionIceTest,
877 PeerConnectionIceUfragPwdAnswerTest,
878 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
879 Values(std::make_pair(true, true), // Both changed.
880 std::make_pair(true, false), // Only ufrag changed.
881 std::make_pair(false, true)))); // Only pwd changed.
Steve Antonf1c6db12017-10-13 11:13:35 -0700882
883// Test that if an ICE restart is offered on one media section, then the answer
884// will only change ICE ufrag/pwd for that section and keep the other sections
885// the same.
886// Note that this only works if we have disabled BUNDLE, otherwise all media
887// sections will share the same transport.
Steve Anton46d926a2018-01-23 10:23:06 -0800888TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700889 CreateAnswerHasNewUfragPwdForOnlyMediaSectionWhichRestarted) {
890 auto caller = CreatePeerConnectionWithAudioVideo();
891 auto callee = CreatePeerConnectionWithAudioVideo();
892
893 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
894 ASSERT_TRUE(
895 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
896
897 RTCOfferAnswerOptions disable_bundle_options;
898 disable_bundle_options.use_rtp_mux = false;
899
900 auto offer = caller->CreateOffer(disable_bundle_options);
901
902 // Signal ICE restart on the first media section.
903 auto* offer_transport_desc = GetFirstTransportDescription(offer.get());
904 offer_transport_desc->ice_ufrag += "_new";
905 offer_transport_desc->ice_pwd += "_new";
906
907 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
908
909 auto answer = callee->CreateAnswer(disable_bundle_options);
910 const auto& answer_transports = answer->description()->transport_infos();
911 const auto& local_transports =
912 callee->pc()->local_description()->description()->transport_infos();
913
914 EXPECT_NE(answer_transports[0].description.ice_ufrag,
915 local_transports[0].description.ice_ufrag);
916 EXPECT_NE(answer_transports[0].description.ice_pwd,
917 local_transports[0].description.ice_pwd);
918 EXPECT_EQ(answer_transports[1].description.ice_ufrag,
919 local_transports[1].description.ice_ufrag);
920 EXPECT_EQ(answer_transports[1].description.ice_pwd,
921 local_transports[1].description.ice_pwd);
922}
923
Qingsi Wange1692722017-11-29 13:27:20 -0800924// Test that when the initial offerer (caller) uses the lite implementation of
925// ICE and the callee uses the full implementation, the caller takes the
926// CONTROLLED role and the callee takes the CONTROLLING role. This is specified
927// in RFC5245 Section 5.1.1.
Steve Anton46d926a2018-01-23 10:23:06 -0800928TEST_P(PeerConnectionIceTest,
Qingsi Wange1692722017-11-29 13:27:20 -0800929 OfferFromLiteIceControlledAndAnswerFromFullIceControlling) {
930 auto caller = CreatePeerConnectionWithAudioVideo();
931 auto callee = CreatePeerConnectionWithAudioVideo();
932
933 auto offer = caller->CreateOffer();
934 SetIceMode(offer.get(), cricket::IceMode::ICEMODE_LITE);
935 ASSERT_TRUE(
936 caller->SetLocalDescription(CloneSessionDescription(offer.get())));
937 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
938
939 auto answer = callee->CreateAnswer();
940 SetIceMode(answer.get(), cricket::IceMode::ICEMODE_FULL);
941 ASSERT_TRUE(
942 callee->SetLocalDescription(CloneSessionDescription(answer.get())));
943 ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer)));
944
945 EXPECT_EQ(cricket::ICEROLE_CONTROLLED, GetIceRole(caller));
946 EXPECT_EQ(cricket::ICEROLE_CONTROLLING, GetIceRole(callee));
947}
948
949// Test that when the caller and the callee both use the lite implementation of
950// ICE, the initial offerer (caller) takes the CONTROLLING role and the callee
951// takes the CONTROLLED role. This is specified in RFC5245 Section 5.1.1.
Steve Anton46d926a2018-01-23 10:23:06 -0800952TEST_P(PeerConnectionIceTest,
Qingsi Wange1692722017-11-29 13:27:20 -0800953 OfferFromLiteIceControllingAndAnswerFromLiteIceControlled) {
954 auto caller = CreatePeerConnectionWithAudioVideo();
955 auto callee = CreatePeerConnectionWithAudioVideo();
956
957 auto offer = caller->CreateOffer();
958 SetIceMode(offer.get(), cricket::IceMode::ICEMODE_LITE);
959 ASSERT_TRUE(
960 caller->SetLocalDescription(CloneSessionDescription(offer.get())));
961 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
962
963 auto answer = callee->CreateAnswer();
964 SetIceMode(answer.get(), cricket::IceMode::ICEMODE_LITE);
965 ASSERT_TRUE(
966 callee->SetLocalDescription(CloneSessionDescription(answer.get())));
967 ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer)));
968
969 EXPECT_EQ(cricket::ICEROLE_CONTROLLING, GetIceRole(caller));
970 EXPECT_EQ(cricket::ICEROLE_CONTROLLED, GetIceRole(callee));
971}
972
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100973INSTANTIATE_TEST_SUITE_P(PeerConnectionIceTest,
974 PeerConnectionIceTest,
975 Values(SdpSemantics::kPlanB,
976 SdpSemantics::kUnifiedPlan));
Steve Anton46d926a2018-01-23 10:23:06 -0800977
Qingsi Wang4ff54432018-03-01 18:25:20 -0800978class PeerConnectionIceConfigTest : public testing::Test {
979 protected:
980 void SetUp() override {
981 pc_factory_ = CreatePeerConnectionFactory(
982 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
983 FakeAudioCaptureModule::Create(), CreateBuiltinAudioEncoderFactory(),
Anders Carlsson67537952018-05-03 11:28:29 +0200984 CreateBuiltinAudioDecoderFactory(), CreateBuiltinVideoEncoderFactory(),
985 CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
986 nullptr /* audio_processing */);
Qingsi Wang4ff54432018-03-01 18:25:20 -0800987 }
988 void CreatePeerConnection(const RTCConfiguration& config) {
989 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
990 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
991 port_allocator_ = port_allocator.get();
992 rtc::scoped_refptr<PeerConnectionInterface> pc(
Niels Möllerf06f9232018-08-07 12:32:18 +0200993 pc_factory_->CreatePeerConnection(config, std::move(port_allocator),
994 nullptr /* cert_generator */,
995 &observer_));
Qingsi Wang4ff54432018-03-01 18:25:20 -0800996 EXPECT_TRUE(pc.get());
Mirko Bonadei1c546052019-02-04 14:50:38 +0100997 pc_ = std::move(pc);
Qingsi Wang4ff54432018-03-01 18:25:20 -0800998 }
999
1000 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_ = nullptr;
1001 rtc::scoped_refptr<PeerConnectionInterface> pc_ = nullptr;
1002 cricket::FakePortAllocator* port_allocator_ = nullptr;
1003
1004 MockPeerConnectionObserver observer_;
1005};
1006
1007TEST_F(PeerConnectionIceConfigTest, SetStunCandidateKeepaliveInterval) {
1008 RTCConfiguration config;
1009 config.stun_candidate_keepalive_interval = 123;
1010 config.ice_candidate_pool_size = 1;
1011 CreatePeerConnection(config);
1012 ASSERT_NE(port_allocator_, nullptr);
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02001013 absl::optional<int> actual_stun_keepalive_interval =
Qingsi Wang4ff54432018-03-01 18:25:20 -08001014 port_allocator_->stun_candidate_keepalive_interval();
1015 EXPECT_EQ(actual_stun_keepalive_interval.value_or(-1), 123);
1016 config.stun_candidate_keepalive_interval = 321;
1017 RTCError error;
1018 pc_->SetConfiguration(config, &error);
1019 actual_stun_keepalive_interval =
1020 port_allocator_->stun_candidate_keepalive_interval();
1021 EXPECT_EQ(actual_stun_keepalive_interval.value_or(-1), 321);
1022}
1023
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001024TEST_P(PeerConnectionIceTest, IceCredentialsCreateOffer) {
1025 RTCConfiguration config;
1026 config.ice_candidate_pool_size = 1;
1027 auto pc = CreatePeerConnectionWithAudioVideo(config);
1028 ASSERT_NE(pc->port_allocator_, nullptr);
1029 auto offer = pc->CreateOffer();
1030 auto credentials = pc->port_allocator_->GetPooledIceCredentials();
1031 ASSERT_EQ(1u, credentials.size());
1032
1033 auto* desc = offer->description();
1034 for (const auto& content : desc->contents()) {
1035 auto* transport_info = desc->GetTransportInfoByName(content.name);
1036 EXPECT_EQ(transport_info->description.ice_ufrag, credentials[0].ufrag);
1037 EXPECT_EQ(transport_info->description.ice_pwd, credentials[0].pwd);
1038 }
1039}
1040
1041TEST_P(PeerConnectionIceTest, IceCredentialsCreateAnswer) {
1042 RTCConfiguration config;
1043 config.ice_candidate_pool_size = 1;
1044 auto pc = CreatePeerConnectionWithAudioVideo(config);
1045 ASSERT_NE(pc->port_allocator_, nullptr);
1046 auto offer = pc->CreateOffer();
1047 ASSERT_TRUE(pc->SetRemoteDescription(std::move(offer)));
1048 auto answer = pc->CreateAnswer();
1049
1050 auto credentials = pc->port_allocator_->GetPooledIceCredentials();
1051 ASSERT_EQ(1u, credentials.size());
1052
1053 auto* desc = answer->description();
1054 for (const auto& content : desc->contents()) {
1055 auto* transport_info = desc->GetTransportInfoByName(content.name);
1056 EXPECT_EQ(transport_info->description.ice_ufrag, credentials[0].ufrag);
1057 EXPECT_EQ(transport_info->description.ice_pwd, credentials[0].pwd);
1058 }
1059}
1060
Steve Antonf1c6db12017-10-13 11:13:35 -07001061} // namespace webrtc