blob: 3b8b4db951acf280b88de750e0acce1f3b58a6bd [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 =
Guido Urdaneta41633172019-05-23 20:12:29 +020061 CreateIceCandidate(first_content.name, -1, *candidate);
Steve Anton27ab0e52018-07-23 15:11:53 -070062 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) {
Bjorn A Mellem3a1b9272019-05-24 16:13:08 -0700219 auto dtls_transport = pc->LookupDtlsTransportByMidInternal(
220 transceiver->internal()->channel()->content_name());
221 return dtls_transport->ice_transport()->internal()->GetIceRole();
Steve Anton46d926a2018-01-23 10:23:06 -0800222 }
223 }
224 RTC_NOTREACHED();
225 return cricket::ICEROLE_UNKNOWN;
Qingsi Wange1692722017-11-29 13:27:20 -0800226 }
227
Steve Antonf1c6db12017-10-13 11:13:35 -0700228 bool AddCandidateToFirstTransport(cricket::Candidate* candidate,
229 SessionDescriptionInterface* sdesc) {
230 auto* desc = sdesc->description();
231 RTC_DCHECK(desc->contents().size() > 0);
232 const auto& first_content = desc->contents()[0];
233 candidate->set_transport_name(first_content.name);
Steve Anton27ab0e52018-07-23 15:11:53 -0700234 std::unique_ptr<IceCandidateInterface> jsep_candidate =
235 CreateIceCandidate(first_content.name, 0, *candidate);
236 return sdesc->AddCandidate(jsep_candidate.get());
Steve Antonf1c6db12017-10-13 11:13:35 -0700237 }
238
239 rtc::FakeNetworkManager* NewFakeNetwork() {
240 // The PeerConnection's port allocator is tied to the PeerConnection's
241 // lifetime and expects the underlying NetworkManager to outlive it. That
242 // prevents us from having the PeerConnectionWrapper own the fake network.
243 // Therefore, the test fixture will own all the fake networks even though
244 // tests should access the fake network through the PeerConnectionWrapper.
245 auto* fake_network = new rtc::FakeNetworkManager();
246 fake_networks_.emplace_back(fake_network);
247 return fake_network;
248 }
249
250 std::unique_ptr<rtc::VirtualSocketServer> vss_;
251 rtc::AutoSocketServerThread main_;
252 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_;
253 std::vector<std::unique_ptr<rtc::FakeNetworkManager>> fake_networks_;
Steve Anton46d926a2018-01-23 10:23:06 -0800254 const SdpSemantics sdp_semantics_;
255};
256
257class PeerConnectionIceTest
258 : public PeerConnectionIceBaseTest,
259 public ::testing::WithParamInterface<SdpSemantics> {
260 protected:
Harald Alvestrand76829d72018-07-18 23:24:36 +0200261 PeerConnectionIceTest() : PeerConnectionIceBaseTest(GetParam()) {
262 webrtc::metrics::Reset();
263 }
Steve Antonf1c6db12017-10-13 11:13:35 -0700264};
265
266::testing::AssertionResult AssertCandidatesEqual(const char* a_expr,
267 const char* b_expr,
268 const cricket::Candidate& a,
269 const cricket::Candidate& b) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200270 rtc::StringBuilder failure_info;
Steve Antonf1c6db12017-10-13 11:13:35 -0700271 if (a.component() != b.component()) {
272 failure_info << "\ncomponent: " << a.component() << " != " << b.component();
273 }
274 if (a.protocol() != b.protocol()) {
275 failure_info << "\nprotocol: " << a.protocol() << " != " << b.protocol();
276 }
277 if (a.address() != b.address()) {
278 failure_info << "\naddress: " << a.address().ToString()
279 << " != " << b.address().ToString();
280 }
281 if (a.type() != b.type()) {
282 failure_info << "\ntype: " << a.type() << " != " << b.type();
283 }
284 std::string failure_info_str = failure_info.str();
285 if (failure_info_str.empty()) {
286 return ::testing::AssertionSuccess();
287 } else {
288 return ::testing::AssertionFailure()
289 << a_expr << " and " << b_expr << " are not equal"
290 << failure_info_str;
291 }
292}
293
Steve Anton46d926a2018-01-23 10:23:06 -0800294TEST_P(PeerConnectionIceTest, OfferContainsGatheredCandidates) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700295 const SocketAddress kLocalAddress("1.1.1.1", 0);
296
297 auto caller = CreatePeerConnectionWithAudioVideo();
298 caller->network()->AddInterface(kLocalAddress);
299
300 // Start ICE candidate gathering by setting the local offer.
301 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
302
303 EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kIceCandidatesTimeout);
304
305 auto offer = caller->CreateOffer();
306 EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(0).size());
307 EXPECT_EQ(caller->observer()->GetCandidatesByMline(0).size(),
308 offer->candidates(0)->count());
309 EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(1).size());
310 EXPECT_EQ(caller->observer()->GetCandidatesByMline(1).size(),
311 offer->candidates(1)->count());
312}
313
Steve Anton46d926a2018-01-23 10:23:06 -0800314TEST_P(PeerConnectionIceTest, AnswerContainsGatheredCandidates) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700315 const SocketAddress kCallerAddress("1.1.1.1", 0);
316
317 auto caller = CreatePeerConnectionWithAudioVideo();
318 auto callee = CreatePeerConnectionWithAudioVideo();
319 caller->network()->AddInterface(kCallerAddress);
320
321 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
322 ASSERT_TRUE(callee->SetLocalDescription(callee->CreateAnswer()));
323
324 EXPECT_TRUE_WAIT(callee->IsIceGatheringDone(), kIceCandidatesTimeout);
325
Steve Antondffead82018-02-06 10:31:29 -0800326 auto* answer = callee->pc()->local_description();
Steve Antonf1c6db12017-10-13 11:13:35 -0700327 EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(0).size());
328 EXPECT_EQ(callee->observer()->GetCandidatesByMline(0).size(),
329 answer->candidates(0)->count());
330 EXPECT_LT(0u, caller->observer()->GetCandidatesByMline(1).size());
331 EXPECT_EQ(callee->observer()->GetCandidatesByMline(1).size(),
332 answer->candidates(1)->count());
333}
334
Steve Anton46d926a2018-01-23 10:23:06 -0800335TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700336 CanSetRemoteSessionDescriptionWithRemoteCandidates) {
337 const SocketAddress kCallerAddress("1.1.1.1", 1111);
338
339 auto caller = CreatePeerConnectionWithAudioVideo();
340 auto callee = CreatePeerConnectionWithAudioVideo();
341
342 auto offer = caller->CreateOfferAndSetAsLocal();
343 cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress);
344 AddCandidateToFirstTransport(&candidate, offer.get());
345
346 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
347 auto remote_candidates = callee->GetIceCandidatesFromRemoteDescription();
348 ASSERT_EQ(1u, remote_candidates.size());
349 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate,
350 remote_candidates[0]->candidate());
351}
352
Steve Anton46d926a2018-01-23 10:23:06 -0800353TEST_P(PeerConnectionIceTest, SetLocalDescriptionFailsIfNoIceCredentials) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700354 auto caller = CreatePeerConnectionWithAudioVideo();
355
356 auto offer = caller->CreateOffer();
357 RemoveIceUfragPwd(offer.get());
358
359 EXPECT_FALSE(caller->SetLocalDescription(std::move(offer)));
360}
361
Steve Anton46d926a2018-01-23 10:23:06 -0800362TEST_P(PeerConnectionIceTest, SetRemoteDescriptionFailsIfNoIceCredentials) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700363 auto caller = CreatePeerConnectionWithAudioVideo();
364 auto callee = CreatePeerConnectionWithAudioVideo();
365
366 auto offer = caller->CreateOfferAndSetAsLocal();
367 RemoveIceUfragPwd(offer.get());
368
369 EXPECT_FALSE(callee->SetRemoteDescription(std::move(offer)));
370}
371
Steve Antonf764cf42018-05-01 14:32:17 -0700372// Test that doing an offer/answer exchange with no transport (i.e., no data
373// channel or media) results in the ICE connection state staying at New.
374TEST_P(PeerConnectionIceTest,
375 OfferAnswerWithNoTransportsDoesNotChangeIceConnectionState) {
376 auto caller = CreatePeerConnection();
377 auto callee = CreatePeerConnection();
378
379 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
380
381 EXPECT_EQ(PeerConnectionInterface::kIceConnectionNew,
382 caller->pc()->ice_connection_state());
383 EXPECT_EQ(PeerConnectionInterface::kIceConnectionNew,
384 callee->pc()->ice_connection_state());
385}
386
Steve Antonf1c6db12017-10-13 11:13:35 -0700387// The following group tests that ICE candidates are not generated before
388// SetLocalDescription is called on a PeerConnection.
389
Steve Anton46d926a2018-01-23 10:23:06 -0800390TEST_P(PeerConnectionIceTest, NoIceCandidatesBeforeSetLocalDescription) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700391 const SocketAddress kLocalAddress("1.1.1.1", 0);
392
393 auto caller = CreatePeerConnectionWithAudioVideo();
394 caller->network()->AddInterface(kLocalAddress);
395
396 // Pump for 1 second and verify that no candidates are generated.
397 rtc::Thread::Current()->ProcessMessages(1000);
398
399 EXPECT_EQ(0u, caller->observer()->candidates_.size());
400}
Steve Anton46d926a2018-01-23 10:23:06 -0800401TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700402 NoIceCandidatesBeforeAnswerSetAsLocalDescription) {
403 const SocketAddress kCallerAddress("1.1.1.1", 1111);
404
405 auto caller = CreatePeerConnectionWithAudioVideo();
406 auto callee = CreatePeerConnectionWithAudioVideo();
407 caller->network()->AddInterface(kCallerAddress);
408
409 auto offer = caller->CreateOfferAndSetAsLocal();
410 cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress);
411 AddCandidateToFirstTransport(&candidate, offer.get());
412 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
413
414 // Pump for 1 second and verify that no candidates are generated.
415 rtc::Thread::Current()->ProcessMessages(1000);
416
417 EXPECT_EQ(0u, callee->observer()->candidates_.size());
418}
419
Steve Anton46d926a2018-01-23 10:23:06 -0800420TEST_P(PeerConnectionIceTest, CannotAddCandidateWhenRemoteDescriptionNotSet) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700421 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
422
423 auto caller = CreatePeerConnectionWithAudioVideo();
424 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
Steve Anton27ab0e52018-07-23 15:11:53 -0700425 std::unique_ptr<IceCandidateInterface> jsep_candidate =
426 CreateIceCandidate(cricket::CN_AUDIO, 0, candidate);
Steve Antonf1c6db12017-10-13 11:13:35 -0700427
Steve Anton27ab0e52018-07-23 15:11:53 -0700428 EXPECT_FALSE(caller->pc()->AddIceCandidate(jsep_candidate.get()));
Steve Antonf1c6db12017-10-13 11:13:35 -0700429
430 caller->CreateOfferAndSetAsLocal();
431
Steve Anton27ab0e52018-07-23 15:11:53 -0700432 EXPECT_FALSE(caller->pc()->AddIceCandidate(jsep_candidate.get()));
Steve Antonb443dfe2019-03-05 14:09:49 -0800433 EXPECT_THAT(webrtc::metrics::Samples("WebRTC.PeerConnection.AddIceCandidate"),
434 ElementsAre(Pair(kAddIceCandidateFailNoRemoteDescription, 2)));
Steve Antonf1c6db12017-10-13 11:13:35 -0700435}
436
Steve Antonc79268f2018-04-24 09:54:10 -0700437TEST_P(PeerConnectionIceTest, CannotAddCandidateWhenPeerConnectionClosed) {
438 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
439
440 auto caller = CreatePeerConnectionWithAudioVideo();
441 auto callee = CreatePeerConnectionWithAudioVideo();
442
443 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
444
445 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
446 auto* audio_content = cricket::GetFirstAudioContent(
447 caller->pc()->local_description()->description());
Steve Anton27ab0e52018-07-23 15:11:53 -0700448 std::unique_ptr<IceCandidateInterface> jsep_candidate =
449 CreateIceCandidate(audio_content->name, 0, candidate);
Steve Antonc79268f2018-04-24 09:54:10 -0700450
451 caller->pc()->Close();
452
Steve Anton27ab0e52018-07-23 15:11:53 -0700453 EXPECT_FALSE(caller->pc()->AddIceCandidate(jsep_candidate.get()));
Steve Antonc79268f2018-04-24 09:54:10 -0700454}
455
Steve Anton46d926a2018-01-23 10:23:06 -0800456TEST_P(PeerConnectionIceTest, DuplicateIceCandidateIgnoredWhenAdded) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700457 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
458
459 auto caller = CreatePeerConnectionWithAudioVideo();
460 auto callee = CreatePeerConnectionWithAudioVideo();
461
462 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
463 ASSERT_TRUE(
464 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
465
466 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
467 caller->AddIceCandidate(&candidate);
468 EXPECT_TRUE(caller->AddIceCandidate(&candidate));
469 EXPECT_EQ(1u, caller->GetIceCandidatesFromRemoteDescription().size());
470}
471
Steve Anton46d926a2018-01-23 10:23:06 -0800472TEST_P(PeerConnectionIceTest,
Steve Antonc79268f2018-04-24 09:54:10 -0700473 CannotRemoveIceCandidatesWhenPeerConnectionClosed) {
474 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
475
476 auto caller = CreatePeerConnectionWithAudioVideo();
477 auto callee = CreatePeerConnectionWithAudioVideo();
478
479 ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
480
481 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
482 auto* audio_content = cricket::GetFirstAudioContent(
483 caller->pc()->local_description()->description());
Steve Anton27ab0e52018-07-23 15:11:53 -0700484 std::unique_ptr<IceCandidateInterface> ice_candidate =
485 CreateIceCandidate(audio_content->name, 0, candidate);
Steve Antonc79268f2018-04-24 09:54:10 -0700486
Steve Anton27ab0e52018-07-23 15:11:53 -0700487 ASSERT_TRUE(caller->pc()->AddIceCandidate(ice_candidate.get()));
Steve Antonc79268f2018-04-24 09:54:10 -0700488
489 caller->pc()->Close();
490
491 EXPECT_FALSE(caller->pc()->RemoveIceCandidates({candidate}));
492}
493
494TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700495 AddRemoveCandidateWithEmptyTransportDoesNotCrash) {
496 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
497
498 auto caller = CreatePeerConnectionWithAudioVideo();
499 auto callee = CreatePeerConnectionWithAudioVideo();
500
501 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
502 ASSERT_TRUE(
503 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
504
505 // |candidate.transport_name()| is empty.
506 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
Steve Anton46d926a2018-01-23 10:23:06 -0800507 auto* audio_content = cricket::GetFirstAudioContent(
508 caller->pc()->local_description()->description());
Steve Anton27ab0e52018-07-23 15:11:53 -0700509 std::unique_ptr<IceCandidateInterface> ice_candidate =
510 CreateIceCandidate(audio_content->name, 0, candidate);
511 EXPECT_TRUE(caller->pc()->AddIceCandidate(ice_candidate.get()));
Steve Antonf1c6db12017-10-13 11:13:35 -0700512 EXPECT_TRUE(caller->pc()->RemoveIceCandidates({candidate}));
513}
514
Steve Anton46d926a2018-01-23 10:23:06 -0800515TEST_P(PeerConnectionIceTest, RemoveCandidateRemovesFromRemoteDescription) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700516 const SocketAddress kCalleeAddress("1.1.1.1", 1111);
517
518 auto caller = CreatePeerConnectionWithAudioVideo();
519 auto callee = CreatePeerConnectionWithAudioVideo();
520
521 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
522 ASSERT_TRUE(
523 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
524
525 cricket::Candidate candidate = CreateLocalUdpCandidate(kCalleeAddress);
526 ASSERT_TRUE(caller->AddIceCandidate(&candidate));
527 EXPECT_TRUE(caller->pc()->RemoveIceCandidates({candidate}));
528 EXPECT_EQ(0u, caller->GetIceCandidatesFromRemoteDescription().size());
529}
530
531// Test that if a candidate is added via AddIceCandidate and via an updated
532// remote description, then both candidates appear in the stored remote
533// description.
Steve Anton46d926a2018-01-23 10:23:06 -0800534TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700535 CandidateInSubsequentOfferIsAddedToRemoteDescription) {
536 const SocketAddress kCallerAddress1("1.1.1.1", 1111);
537 const SocketAddress kCallerAddress2("2.2.2.2", 2222);
538
539 auto caller = CreatePeerConnectionWithAudioVideo();
540 auto callee = CreatePeerConnectionWithAudioVideo();
541
542 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
543 ASSERT_TRUE(
544 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
545
546 // Add one candidate via |AddIceCandidate|.
547 cricket::Candidate candidate1 = CreateLocalUdpCandidate(kCallerAddress1);
548 ASSERT_TRUE(callee->AddIceCandidate(&candidate1));
549
550 // Add the second candidate via a reoffer.
551 auto offer = caller->CreateOffer();
552 cricket::Candidate candidate2 = CreateLocalUdpCandidate(kCallerAddress2);
553 AddCandidateToFirstTransport(&candidate2, offer.get());
554
555 // Expect both candidates to appear in the callee's remote description.
556 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
557 EXPECT_EQ(2u, callee->GetIceCandidatesFromRemoteDescription().size());
558}
559
560// The follow test verifies that SetLocal/RemoteDescription fails when an offer
561// has either ICE ufrag/pwd too short or too long and succeeds otherwise.
562// The standard (https://tools.ietf.org/html/rfc5245#section-15.4) says that
563// pwd must be 22-256 characters and ufrag must be 4-256 characters.
Steve Anton46d926a2018-01-23 10:23:06 -0800564TEST_P(PeerConnectionIceTest, VerifyUfragPwdLength) {
Yves Gerey665174f2018-06-19 15:03:05 +0200565 auto set_local_description_with_ufrag_pwd_length = [this](int ufrag_len,
566 int pwd_len) {
567 auto pc = CreatePeerConnectionWithAudioVideo();
568 auto offer = pc->CreateOffer();
569 SetIceUfragPwd(offer.get(), std::string(ufrag_len, 'x'),
570 std::string(pwd_len, 'x'));
571 return pc->SetLocalDescription(std::move(offer));
572 };
Steve Antonf1c6db12017-10-13 11:13:35 -0700573
Yves Gerey665174f2018-06-19 15:03:05 +0200574 auto set_remote_description_with_ufrag_pwd_length = [this](int ufrag_len,
575 int pwd_len) {
576 auto pc = CreatePeerConnectionWithAudioVideo();
577 auto offer = pc->CreateOffer();
578 SetIceUfragPwd(offer.get(), std::string(ufrag_len, 'x'),
579 std::string(pwd_len, 'x'));
580 return pc->SetRemoteDescription(std::move(offer));
581 };
Steve Antonf1c6db12017-10-13 11:13:35 -0700582
583 EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(3, 22));
584 EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(3, 22));
585 EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(257, 22));
586 EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(257, 22));
587 EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(4, 21));
588 EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(4, 21));
589 EXPECT_FALSE(set_local_description_with_ufrag_pwd_length(4, 257));
590 EXPECT_FALSE(set_remote_description_with_ufrag_pwd_length(4, 257));
591 EXPECT_TRUE(set_local_description_with_ufrag_pwd_length(4, 22));
592 EXPECT_TRUE(set_remote_description_with_ufrag_pwd_length(4, 22));
593 EXPECT_TRUE(set_local_description_with_ufrag_pwd_length(256, 256));
594 EXPECT_TRUE(set_remote_description_with_ufrag_pwd_length(256, 256));
595}
596
597::testing::AssertionResult AssertIpInCandidates(
598 const char* address_expr,
599 const char* candidates_expr,
600 const SocketAddress& address,
601 const std::vector<IceCandidateInterface*> candidates) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200602 rtc::StringBuilder candidate_hosts;
Steve Antonf1c6db12017-10-13 11:13:35 -0700603 for (const auto* candidate : candidates) {
604 const auto& candidate_ip = candidate->candidate().address().ipaddr();
605 if (candidate_ip == address.ipaddr()) {
606 return ::testing::AssertionSuccess();
607 }
Jonas Olssonabbe8412018-04-03 13:40:05 +0200608 candidate_hosts << "\n" << candidate_ip.ToString();
Steve Antonf1c6db12017-10-13 11:13:35 -0700609 }
610 return ::testing::AssertionFailure()
611 << address_expr << " (host " << address.HostAsURIString()
612 << ") not in " << candidates_expr
613 << " which have the following address hosts:" << candidate_hosts.str();
614}
615
Steve Anton46d926a2018-01-23 10:23:06 -0800616TEST_P(PeerConnectionIceTest, CandidatesGeneratedForEachLocalInterface) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700617 const SocketAddress kLocalAddress1("1.1.1.1", 0);
618 const SocketAddress kLocalAddress2("2.2.2.2", 0);
619
620 auto caller = CreatePeerConnectionWithAudioVideo();
621 caller->network()->AddInterface(kLocalAddress1);
622 caller->network()->AddInterface(kLocalAddress2);
623
624 caller->CreateOfferAndSetAsLocal();
625 EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kIceCandidatesTimeout);
626
627 auto candidates = caller->observer()->GetCandidatesByMline(0);
628 EXPECT_PRED_FORMAT2(AssertIpInCandidates, kLocalAddress1, candidates);
629 EXPECT_PRED_FORMAT2(AssertIpInCandidates, kLocalAddress2, candidates);
630}
631
Steve Anton46d926a2018-01-23 10:23:06 -0800632TEST_P(PeerConnectionIceTest, TrickledSingleCandidateAddedToRemoteDescription) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700633 const SocketAddress kCallerAddress("1.1.1.1", 1111);
634
635 auto caller = CreatePeerConnectionWithAudioVideo();
636 auto callee = CreatePeerConnectionWithAudioVideo();
637
638 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
639
640 cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress);
641 callee->AddIceCandidate(&candidate);
642 auto candidates = callee->GetIceCandidatesFromRemoteDescription();
643 ASSERT_EQ(1u, candidates.size());
644 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate,
645 candidates[0]->candidate());
646}
647
Steve Anton46d926a2018-01-23 10:23:06 -0800648TEST_P(PeerConnectionIceTest, TwoTrickledCandidatesAddedToRemoteDescription) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700649 const SocketAddress kCalleeAddress1("1.1.1.1", 1111);
650 const SocketAddress kCalleeAddress2("2.2.2.2", 2222);
651
652 auto caller = CreatePeerConnectionWithAudioVideo();
653 auto callee = CreatePeerConnectionWithAudioVideo();
654
655 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
656 ASSERT_TRUE(
657 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
658
659 cricket::Candidate candidate1 = CreateLocalUdpCandidate(kCalleeAddress1);
660 caller->AddIceCandidate(&candidate1);
661
662 cricket::Candidate candidate2 = CreateLocalUdpCandidate(kCalleeAddress2);
663 caller->AddIceCandidate(&candidate2);
664
665 auto candidates = caller->GetIceCandidatesFromRemoteDescription();
666 ASSERT_EQ(2u, candidates.size());
667 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate1,
668 candidates[0]->candidate());
669 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, candidate2,
670 candidates[1]->candidate());
671}
672
Steve Anton46d926a2018-01-23 10:23:06 -0800673TEST_P(PeerConnectionIceTest, LocalDescriptionUpdatedWhenContinualGathering) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700674 const SocketAddress kLocalAddress("1.1.1.1", 0);
675
676 RTCConfiguration config;
677 config.continual_gathering_policy =
678 PeerConnectionInterface::GATHER_CONTINUALLY;
679 auto caller = CreatePeerConnectionWithAudioVideo(config);
680 caller->network()->AddInterface(kLocalAddress);
681
682 // Start ICE candidate gathering by setting the local offer.
683 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
684
685 // Since we're using continual gathering, we won't get "gathering done".
686 EXPECT_TRUE_WAIT(
687 caller->pc()->local_description()->candidates(0)->count() > 0,
688 kIceCandidatesTimeout);
689}
690
691// Test that when continual gathering is enabled, and a network interface goes
692// down, the candidate is signaled as removed and removed from the local
693// description.
Steve Anton46d926a2018-01-23 10:23:06 -0800694TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700695 LocalCandidatesRemovedWhenNetworkDownIfGatheringContinually) {
696 const SocketAddress kLocalAddress("1.1.1.1", 0);
697
698 RTCConfiguration config;
699 config.continual_gathering_policy =
700 PeerConnectionInterface::GATHER_CONTINUALLY;
701 auto caller = CreatePeerConnectionWithAudioVideo(config);
702 caller->network()->AddInterface(kLocalAddress);
703
704 // Start ICE candidate gathering by setting the local offer.
705 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
706
707 EXPECT_TRUE_WAIT(
708 caller->pc()->local_description()->candidates(0)->count() > 0,
709 kIceCandidatesTimeout);
710
711 // Remove the only network interface, causing the PeerConnection to signal
712 // the removal of all candidates derived from this interface.
713 caller->network()->RemoveInterface(kLocalAddress);
714
715 EXPECT_EQ_WAIT(0u, caller->pc()->local_description()->candidates(0)->count(),
716 kIceCandidatesTimeout);
717 EXPECT_LT(0, caller->observer()->num_candidates_removed_);
718}
719
Steve Anton46d926a2018-01-23 10:23:06 -0800720TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700721 LocalCandidatesNotRemovedWhenNetworkDownIfGatheringOnce) {
722 const SocketAddress kLocalAddress("1.1.1.1", 0);
723
724 RTCConfiguration config;
725 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
726 auto caller = CreatePeerConnectionWithAudioVideo(config);
727 caller->network()->AddInterface(kLocalAddress);
728
729 // Start ICE candidate gathering by setting the local offer.
730 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
731
732 EXPECT_TRUE_WAIT(caller->IsIceGatheringDone(), kIceCandidatesTimeout);
733
734 caller->network()->RemoveInterface(kLocalAddress);
735
736 // Verify that the local candidates are not removed;
737 rtc::Thread::Current()->ProcessMessages(1000);
738 EXPECT_EQ(0, caller->observer()->num_candidates_removed_);
739}
740
741// The following group tests that when an offer includes a new ufrag or pwd
742// (indicating an ICE restart) the old candidates are removed and new candidates
743// added to the remote description.
744
Steve Anton46d926a2018-01-23 10:23:06 -0800745TEST_P(PeerConnectionIceTest, IceRestartOfferClearsExistingCandidate) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700746 const SocketAddress kCallerAddress("1.1.1.1", 1111);
747
748 auto caller = CreatePeerConnectionWithAudioVideo();
749 auto callee = CreatePeerConnectionWithAudioVideo();
750
Amit Hilbuchae3df542019-01-07 12:13:08 -0800751 auto offer = caller->CreateOfferAndSetAsLocal();
Steve Antonf1c6db12017-10-13 11:13:35 -0700752 cricket::Candidate candidate = CreateLocalUdpCandidate(kCallerAddress);
753 AddCandidateToFirstTransport(&candidate, offer.get());
754
755 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
756
757 RTCOfferAnswerOptions options;
758 options.ice_restart = true;
Amit Hilbuchae3df542019-01-07 12:13:08 -0800759 ASSERT_TRUE(
760 callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal(options)));
Steve Antonf1c6db12017-10-13 11:13:35 -0700761
762 EXPECT_EQ(0u, callee->GetIceCandidatesFromRemoteDescription().size());
763}
Steve Anton46d926a2018-01-23 10:23:06 -0800764TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700765 IceRestartOfferCandidateReplacesExistingCandidate) {
766 const SocketAddress kFirstCallerAddress("1.1.1.1", 1111);
767 const SocketAddress kRestartedCallerAddress("2.2.2.2", 2222);
768
769 auto caller = CreatePeerConnectionWithAudioVideo();
770 auto callee = CreatePeerConnectionWithAudioVideo();
771
Amit Hilbuchae3df542019-01-07 12:13:08 -0800772 auto offer = caller->CreateOfferAndSetAsLocal();
Steve Antonf1c6db12017-10-13 11:13:35 -0700773 cricket::Candidate old_candidate =
774 CreateLocalUdpCandidate(kFirstCallerAddress);
775 AddCandidateToFirstTransport(&old_candidate, offer.get());
776
777 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
778
779 RTCOfferAnswerOptions options;
780 options.ice_restart = true;
Amit Hilbuchae3df542019-01-07 12:13:08 -0800781 auto restart_offer = caller->CreateOfferAndSetAsLocal(options);
Steve Antonf1c6db12017-10-13 11:13:35 -0700782 cricket::Candidate new_candidate =
783 CreateLocalUdpCandidate(kRestartedCallerAddress);
784 AddCandidateToFirstTransport(&new_candidate, restart_offer.get());
785
786 ASSERT_TRUE(callee->SetRemoteDescription(std::move(restart_offer)));
787
788 auto remote_candidates = callee->GetIceCandidatesFromRemoteDescription();
789 ASSERT_EQ(1u, remote_candidates.size());
790 EXPECT_PRED_FORMAT2(AssertCandidatesEqual, new_candidate,
791 remote_candidates[0]->candidate());
792}
793
794// Test that if there is not an ICE restart (i.e., nothing changes), then the
795// answer to a later offer should have the same ufrag/pwd as the first answer.
Steve Anton46d926a2018-01-23 10:23:06 -0800796TEST_P(PeerConnectionIceTest, LaterAnswerHasSameIceCredentialsIfNoIceRestart) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700797 auto caller = CreatePeerConnectionWithAudioVideo();
798 auto callee = CreatePeerConnectionWithAudioVideo();
799
800 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
801 ASSERT_TRUE(
802 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
803
804 // Re-offer.
805 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
806
807 auto answer = callee->CreateAnswer();
808 auto* answer_transport_desc = GetFirstTransportDescription(answer.get());
809 auto* local_transport_desc =
810 GetFirstTransportDescription(callee->pc()->local_description());
811
812 EXPECT_EQ(answer_transport_desc->ice_ufrag, local_transport_desc->ice_ufrag);
813 EXPECT_EQ(answer_transport_desc->ice_pwd, local_transport_desc->ice_pwd);
814}
815
816// The following parameterized test verifies that if an offer is sent with a
817// modified ICE ufrag and/or ICE pwd, then the answer should identify that the
818// other side has initiated an ICE restart and generate a new ufrag and pwd.
819// RFC 5245 says: "If the offer contained a change in the a=ice-ufrag or
820// a=ice-pwd attributes compared to the previous SDP from the peer, it
821// indicates that ICE is restarting for this media stream."
822
Steve Anton46d926a2018-01-23 10:23:06 -0800823class PeerConnectionIceUfragPwdAnswerTest
824 : public PeerConnectionIceBaseTest,
825 public ::testing::WithParamInterface<
826 std::tuple<SdpSemantics, std::tuple<bool, bool>>> {
Steve Antonf1c6db12017-10-13 11:13:35 -0700827 protected:
Steve Anton46d926a2018-01-23 10:23:06 -0800828 PeerConnectionIceUfragPwdAnswerTest()
829 : PeerConnectionIceBaseTest(std::get<0>(GetParam())) {
830 auto param = std::get<1>(GetParam());
831 offer_new_ufrag_ = std::get<0>(param);
832 offer_new_pwd_ = std::get<1>(param);
Steve Antonf1c6db12017-10-13 11:13:35 -0700833 }
834
835 bool offer_new_ufrag_;
836 bool offer_new_pwd_;
837};
838
Steve Anton46d926a2018-01-23 10:23:06 -0800839TEST_P(PeerConnectionIceUfragPwdAnswerTest, TestIncludedInAnswer) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700840 auto caller = CreatePeerConnectionWithAudioVideo();
841 auto callee = CreatePeerConnectionWithAudioVideo();
842
843 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
844 ASSERT_TRUE(
845 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
846
847 auto offer = caller->CreateOffer();
848 auto* offer_transport_desc = GetFirstTransportDescription(offer.get());
849 if (offer_new_ufrag_) {
850 offer_transport_desc->ice_ufrag += "_new";
851 }
852 if (offer_new_pwd_) {
853 offer_transport_desc->ice_pwd += "_new";
854 }
855
856 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
857
858 auto answer = callee->CreateAnswer();
859 auto* answer_transport_desc = GetFirstTransportDescription(answer.get());
860 auto* local_transport_desc =
861 GetFirstTransportDescription(callee->pc()->local_description());
862
863 EXPECT_NE(answer_transport_desc->ice_ufrag, local_transport_desc->ice_ufrag);
864 EXPECT_NE(answer_transport_desc->ice_pwd, local_transport_desc->ice_pwd);
865}
866
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100867INSTANTIATE_TEST_SUITE_P(
Steve Anton46d926a2018-01-23 10:23:06 -0800868 PeerConnectionIceTest,
869 PeerConnectionIceUfragPwdAnswerTest,
870 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
871 Values(std::make_pair(true, true), // Both changed.
872 std::make_pair(true, false), // Only ufrag changed.
873 std::make_pair(false, true)))); // Only pwd changed.
Steve Antonf1c6db12017-10-13 11:13:35 -0700874
875// Test that if an ICE restart is offered on one media section, then the answer
876// will only change ICE ufrag/pwd for that section and keep the other sections
877// the same.
878// Note that this only works if we have disabled BUNDLE, otherwise all media
879// sections will share the same transport.
Steve Anton46d926a2018-01-23 10:23:06 -0800880TEST_P(PeerConnectionIceTest,
Steve Antonf1c6db12017-10-13 11:13:35 -0700881 CreateAnswerHasNewUfragPwdForOnlyMediaSectionWhichRestarted) {
882 auto caller = CreatePeerConnectionWithAudioVideo();
883 auto callee = CreatePeerConnectionWithAudioVideo();
884
885 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
886 ASSERT_TRUE(
887 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
888
889 RTCOfferAnswerOptions disable_bundle_options;
890 disable_bundle_options.use_rtp_mux = false;
891
892 auto offer = caller->CreateOffer(disable_bundle_options);
893
894 // Signal ICE restart on the first media section.
895 auto* offer_transport_desc = GetFirstTransportDescription(offer.get());
896 offer_transport_desc->ice_ufrag += "_new";
897 offer_transport_desc->ice_pwd += "_new";
898
899 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
900
901 auto answer = callee->CreateAnswer(disable_bundle_options);
902 const auto& answer_transports = answer->description()->transport_infos();
903 const auto& local_transports =
904 callee->pc()->local_description()->description()->transport_infos();
905
906 EXPECT_NE(answer_transports[0].description.ice_ufrag,
907 local_transports[0].description.ice_ufrag);
908 EXPECT_NE(answer_transports[0].description.ice_pwd,
909 local_transports[0].description.ice_pwd);
910 EXPECT_EQ(answer_transports[1].description.ice_ufrag,
911 local_transports[1].description.ice_ufrag);
912 EXPECT_EQ(answer_transports[1].description.ice_pwd,
913 local_transports[1].description.ice_pwd);
914}
915
Qingsi Wange1692722017-11-29 13:27:20 -0800916// Test that when the initial offerer (caller) uses the lite implementation of
917// ICE and the callee uses the full implementation, the caller takes the
918// CONTROLLED role and the callee takes the CONTROLLING role. This is specified
919// in RFC5245 Section 5.1.1.
Steve Anton46d926a2018-01-23 10:23:06 -0800920TEST_P(PeerConnectionIceTest,
Qingsi Wange1692722017-11-29 13:27:20 -0800921 OfferFromLiteIceControlledAndAnswerFromFullIceControlling) {
922 auto caller = CreatePeerConnectionWithAudioVideo();
923 auto callee = CreatePeerConnectionWithAudioVideo();
924
925 auto offer = caller->CreateOffer();
926 SetIceMode(offer.get(), cricket::IceMode::ICEMODE_LITE);
927 ASSERT_TRUE(
928 caller->SetLocalDescription(CloneSessionDescription(offer.get())));
929 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
930
931 auto answer = callee->CreateAnswer();
932 SetIceMode(answer.get(), cricket::IceMode::ICEMODE_FULL);
933 ASSERT_TRUE(
934 callee->SetLocalDescription(CloneSessionDescription(answer.get())));
935 ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer)));
936
937 EXPECT_EQ(cricket::ICEROLE_CONTROLLED, GetIceRole(caller));
938 EXPECT_EQ(cricket::ICEROLE_CONTROLLING, GetIceRole(callee));
939}
940
941// Test that when the caller and the callee both use the lite implementation of
942// ICE, the initial offerer (caller) takes the CONTROLLING role and the callee
943// takes the CONTROLLED role. This is specified in RFC5245 Section 5.1.1.
Steve Anton46d926a2018-01-23 10:23:06 -0800944TEST_P(PeerConnectionIceTest,
Qingsi Wange1692722017-11-29 13:27:20 -0800945 OfferFromLiteIceControllingAndAnswerFromLiteIceControlled) {
946 auto caller = CreatePeerConnectionWithAudioVideo();
947 auto callee = CreatePeerConnectionWithAudioVideo();
948
949 auto offer = caller->CreateOffer();
950 SetIceMode(offer.get(), cricket::IceMode::ICEMODE_LITE);
951 ASSERT_TRUE(
952 caller->SetLocalDescription(CloneSessionDescription(offer.get())));
953 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
954
955 auto answer = callee->CreateAnswer();
956 SetIceMode(answer.get(), cricket::IceMode::ICEMODE_LITE);
957 ASSERT_TRUE(
958 callee->SetLocalDescription(CloneSessionDescription(answer.get())));
959 ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer)));
960
961 EXPECT_EQ(cricket::ICEROLE_CONTROLLING, GetIceRole(caller));
962 EXPECT_EQ(cricket::ICEROLE_CONTROLLED, GetIceRole(callee));
963}
964
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100965INSTANTIATE_TEST_SUITE_P(PeerConnectionIceTest,
966 PeerConnectionIceTest,
967 Values(SdpSemantics::kPlanB,
968 SdpSemantics::kUnifiedPlan));
Steve Anton46d926a2018-01-23 10:23:06 -0800969
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200970class PeerConnectionIceConfigTest : public ::testing::Test {
Qingsi Wang4ff54432018-03-01 18:25:20 -0800971 protected:
972 void SetUp() override {
973 pc_factory_ = CreatePeerConnectionFactory(
974 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
975 FakeAudioCaptureModule::Create(), CreateBuiltinAudioEncoderFactory(),
Anders Carlsson67537952018-05-03 11:28:29 +0200976 CreateBuiltinAudioDecoderFactory(), CreateBuiltinVideoEncoderFactory(),
977 CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
978 nullptr /* audio_processing */);
Qingsi Wang4ff54432018-03-01 18:25:20 -0800979 }
980 void CreatePeerConnection(const RTCConfiguration& config) {
981 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
982 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
983 port_allocator_ = port_allocator.get();
984 rtc::scoped_refptr<PeerConnectionInterface> pc(
Niels Möllerf06f9232018-08-07 12:32:18 +0200985 pc_factory_->CreatePeerConnection(config, std::move(port_allocator),
986 nullptr /* cert_generator */,
987 &observer_));
Qingsi Wang4ff54432018-03-01 18:25:20 -0800988 EXPECT_TRUE(pc.get());
Mirko Bonadei1c546052019-02-04 14:50:38 +0100989 pc_ = std::move(pc);
Qingsi Wang4ff54432018-03-01 18:25:20 -0800990 }
991
992 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_ = nullptr;
993 rtc::scoped_refptr<PeerConnectionInterface> pc_ = nullptr;
994 cricket::FakePortAllocator* port_allocator_ = nullptr;
995
996 MockPeerConnectionObserver observer_;
997};
998
999TEST_F(PeerConnectionIceConfigTest, SetStunCandidateKeepaliveInterval) {
1000 RTCConfiguration config;
1001 config.stun_candidate_keepalive_interval = 123;
1002 config.ice_candidate_pool_size = 1;
1003 CreatePeerConnection(config);
1004 ASSERT_NE(port_allocator_, nullptr);
Danil Chapovalov66cadcc2018-06-19 16:47:43 +02001005 absl::optional<int> actual_stun_keepalive_interval =
Qingsi Wang4ff54432018-03-01 18:25:20 -08001006 port_allocator_->stun_candidate_keepalive_interval();
1007 EXPECT_EQ(actual_stun_keepalive_interval.value_or(-1), 123);
1008 config.stun_candidate_keepalive_interval = 321;
1009 RTCError error;
1010 pc_->SetConfiguration(config, &error);
1011 actual_stun_keepalive_interval =
1012 port_allocator_->stun_candidate_keepalive_interval();
1013 EXPECT_EQ(actual_stun_keepalive_interval.value_or(-1), 321);
1014}
1015
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001016TEST_P(PeerConnectionIceTest, IceCredentialsCreateOffer) {
1017 RTCConfiguration config;
1018 config.ice_candidate_pool_size = 1;
1019 auto pc = CreatePeerConnectionWithAudioVideo(config);
1020 ASSERT_NE(pc->port_allocator_, nullptr);
1021 auto offer = pc->CreateOffer();
1022 auto credentials = pc->port_allocator_->GetPooledIceCredentials();
1023 ASSERT_EQ(1u, credentials.size());
1024
1025 auto* desc = offer->description();
1026 for (const auto& content : desc->contents()) {
1027 auto* transport_info = desc->GetTransportInfoByName(content.name);
1028 EXPECT_EQ(transport_info->description.ice_ufrag, credentials[0].ufrag);
1029 EXPECT_EQ(transport_info->description.ice_pwd, credentials[0].pwd);
1030 }
1031}
1032
1033TEST_P(PeerConnectionIceTest, IceCredentialsCreateAnswer) {
1034 RTCConfiguration config;
1035 config.ice_candidate_pool_size = 1;
1036 auto pc = CreatePeerConnectionWithAudioVideo(config);
1037 ASSERT_NE(pc->port_allocator_, nullptr);
1038 auto offer = pc->CreateOffer();
1039 ASSERT_TRUE(pc->SetRemoteDescription(std::move(offer)));
1040 auto answer = pc->CreateAnswer();
1041
1042 auto credentials = pc->port_allocator_->GetPooledIceCredentials();
1043 ASSERT_EQ(1u, credentials.size());
1044
1045 auto* desc = answer->description();
1046 for (const auto& content : desc->contents()) {
1047 auto* transport_info = desc->GetTransportInfoByName(content.name);
1048 EXPECT_EQ(transport_info->description.ice_ufrag, credentials[0].ufrag);
1049 EXPECT_EQ(transport_info->description.ice_pwd, credentials[0].pwd);
1050 }
1051}
1052
Steve Antonf1c6db12017-10-13 11:13:35 -07001053} // namespace webrtc