blob: 8cb295b204fa3fc4076d766e9372146ff424ae2c [file] [log] [blame]
Steve Antonda6c0952017-10-23 11:41:54 -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
Yves Gerey3e707812018-11-28 16:47:49 +010011#include <memory>
12#include <string>
13#include <type_traits>
14#include <utility>
15#include <vector>
Steve Antonda6c0952017-10-23 11:41:54 -070016
Yves Gerey3e707812018-11-28 16:47:49 +010017#include "absl/types/optional.h"
18#include "api/call/callfactoryinterface.h"
19#include "api/jsep.h"
20#include "api/media_transport_interface.h"
21#include "api/mediatypes.h"
22#include "api/peerconnectioninterface.h"
Steve Antonda6c0952017-10-23 11:41:54 -070023#include "api/peerconnectionproxy.h"
Bjorn Mellem175aa2e2018-11-08 11:23:22 -080024#include "api/test/fake_media_transport.h"
Yves Gerey3e707812018-11-28 16:47:49 +010025#include "media/base/codec.h"
Steve Antonda6c0952017-10-23 11:41:54 -070026#include "media/base/fakemediaengine.h"
Yves Gerey3e707812018-11-28 16:47:49 +010027#include "media/base/mediaconstants.h"
28#include "media/base/mediaengine.h"
29#include "media/sctp/sctptransportinternal.h"
30#include "p2p/base/p2pconstants.h"
31#include "p2p/base/portallocator.h"
Steve Antonda6c0952017-10-23 11:41:54 -070032#include "pc/mediasession.h"
33#include "pc/peerconnection.h"
34#include "pc/peerconnectionfactory.h"
35#include "pc/peerconnectionwrapper.h"
Steve Antondbf9d032018-01-19 15:23:40 -080036#include "pc/sdputils.h"
Yves Gerey3e707812018-11-28 16:47:49 +010037#include "pc/sessiondescription.h"
38#include "pc/test/mockpeerconnectionobservers.h"
39#include "rtc_base/checks.h"
40#include "rtc_base/refcountedobject.h"
41#include "rtc_base/rtccertificategenerator.h"
42#include "rtc_base/scoped_ref_ptr.h"
43#include "rtc_base/thread.h"
44#include "test/gtest.h"
Steve Antonda6c0952017-10-23 11:41:54 -070045#ifdef WEBRTC_ANDROID
46#include "pc/test/androidtestinitializer.h"
47#endif
Karl Wiberg918f50c2018-07-05 11:40:33 +020048#include "absl/memory/memory.h"
Steve Antonda6c0952017-10-23 11:41:54 -070049#include "pc/test/fakesctptransport.h"
Steve Antonda6c0952017-10-23 11:41:54 -070050#include "rtc_base/virtualsocketserver.h"
51
52namespace webrtc {
53
54using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
55using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
56using ::testing::Values;
57
Bjorn Mellem175aa2e2018-11-08 11:23:22 -080058namespace {
59
60PeerConnectionFactoryDependencies CreatePeerConnectionFactoryDependencies(
61 rtc::Thread* network_thread,
62 rtc::Thread* worker_thread,
63 rtc::Thread* signaling_thread,
64 std::unique_ptr<cricket::MediaEngineInterface> media_engine,
65 std::unique_ptr<CallFactoryInterface> call_factory,
66 std::unique_ptr<MediaTransportFactory> media_transport_factory) {
67 PeerConnectionFactoryDependencies deps;
68 deps.network_thread = network_thread;
69 deps.worker_thread = worker_thread;
70 deps.signaling_thread = signaling_thread;
71 deps.media_engine = std::move(media_engine);
72 deps.call_factory = std::move(call_factory);
73 deps.media_transport_factory = std::move(media_transport_factory);
74 return deps;
75}
76
77} // namespace
78
Steve Antonda6c0952017-10-23 11:41:54 -070079class PeerConnectionFactoryForDataChannelTest
80 : public rtc::RefCountedObject<PeerConnectionFactory> {
81 public:
82 PeerConnectionFactoryForDataChannelTest()
83 : rtc::RefCountedObject<PeerConnectionFactory>(
Bjorn Mellem175aa2e2018-11-08 11:23:22 -080084 CreatePeerConnectionFactoryDependencies(
85 rtc::Thread::Current(),
86 rtc::Thread::Current(),
87 rtc::Thread::Current(),
88 absl::make_unique<cricket::FakeMediaEngine>(),
89 CreateCallFactory(),
90 absl::make_unique<FakeMediaTransportFactory>())) {}
Steve Antonda6c0952017-10-23 11:41:54 -070091
92 std::unique_ptr<cricket::SctpTransportInternalFactory>
93 CreateSctpTransportInternalFactory() {
Karl Wiberg918f50c2018-07-05 11:40:33 +020094 auto factory = absl::make_unique<FakeSctpTransportFactory>();
Steve Antonda6c0952017-10-23 11:41:54 -070095 last_fake_sctp_transport_factory_ = factory.get();
96 return factory;
97 }
98
99 FakeSctpTransportFactory* last_fake_sctp_transport_factory_ = nullptr;
100};
101
102class PeerConnectionWrapperForDataChannelTest : public PeerConnectionWrapper {
103 public:
104 using PeerConnectionWrapper::PeerConnectionWrapper;
105
106 FakeSctpTransportFactory* sctp_transport_factory() {
107 return sctp_transport_factory_;
108 }
109
110 void set_sctp_transport_factory(
111 FakeSctpTransportFactory* sctp_transport_factory) {
112 sctp_transport_factory_ = sctp_transport_factory;
113 }
114
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200115 absl::optional<std::string> sctp_content_name() {
Steve Antonda6c0952017-10-23 11:41:54 -0700116 return GetInternalPeerConnection()->sctp_content_name();
117 }
118
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200119 absl::optional<std::string> sctp_transport_name() {
Steve Antonda6c0952017-10-23 11:41:54 -0700120 return GetInternalPeerConnection()->sctp_transport_name();
121 }
122
123 PeerConnection* GetInternalPeerConnection() {
Mirko Bonadeie97de912017-12-13 11:29:34 +0100124 auto* pci =
125 static_cast<PeerConnectionProxyWithInternal<PeerConnectionInterface>*>(
126 pc());
127 return static_cast<PeerConnection*>(pci->internal());
Steve Antonda6c0952017-10-23 11:41:54 -0700128 }
129
130 private:
131 FakeSctpTransportFactory* sctp_transport_factory_ = nullptr;
132};
133
Steve Antondbf9d032018-01-19 15:23:40 -0800134class PeerConnectionDataChannelBaseTest : public ::testing::Test {
Steve Antonda6c0952017-10-23 11:41:54 -0700135 protected:
136 typedef std::unique_ptr<PeerConnectionWrapperForDataChannelTest> WrapperPtr;
137
Steve Antondbf9d032018-01-19 15:23:40 -0800138 explicit PeerConnectionDataChannelBaseTest(SdpSemantics sdp_semantics)
139 : vss_(new rtc::VirtualSocketServer()),
140 main_(vss_.get()),
141 sdp_semantics_(sdp_semantics) {
Steve Antonda6c0952017-10-23 11:41:54 -0700142#ifdef WEBRTC_ANDROID
143 InitializeAndroidObjects();
144#endif
145 }
146
147 WrapperPtr CreatePeerConnection() {
148 return CreatePeerConnection(RTCConfiguration());
149 }
150
151 WrapperPtr CreatePeerConnection(const RTCConfiguration& config) {
152 return CreatePeerConnection(config,
153 PeerConnectionFactoryInterface::Options());
154 }
155
156 WrapperPtr CreatePeerConnection(
157 const RTCConfiguration& config,
158 const PeerConnectionFactoryInterface::Options factory_options) {
159 rtc::scoped_refptr<PeerConnectionFactoryForDataChannelTest> pc_factory(
160 new PeerConnectionFactoryForDataChannelTest());
161 pc_factory->SetOptions(factory_options);
162 RTC_CHECK(pc_factory->Initialize());
Karl Wiberg918f50c2018-07-05 11:40:33 +0200163 auto observer = absl::make_unique<MockPeerConnectionObserver>();
Steve Antondbf9d032018-01-19 15:23:40 -0800164 RTCConfiguration modified_config = config;
165 modified_config.sdp_semantics = sdp_semantics_;
166 auto pc = pc_factory->CreatePeerConnection(modified_config, nullptr,
167 nullptr, observer.get());
Steve Antonda6c0952017-10-23 11:41:54 -0700168 if (!pc) {
169 return nullptr;
170 }
171
Yves Gerey4e933292018-10-31 15:36:05 +0100172 observer->SetPeerConnectionInterface(pc.get());
Karl Wiberg918f50c2018-07-05 11:40:33 +0200173 auto wrapper = absl::make_unique<PeerConnectionWrapperForDataChannelTest>(
Steve Antonda6c0952017-10-23 11:41:54 -0700174 pc_factory, pc, std::move(observer));
175 RTC_DCHECK(pc_factory->last_fake_sctp_transport_factory_);
176 wrapper->set_sctp_transport_factory(
177 pc_factory->last_fake_sctp_transport_factory_);
178 return wrapper;
179 }
180
181 // Accepts the same arguments as CreatePeerConnection and adds a default data
182 // channel.
183 template <typename... Args>
184 WrapperPtr CreatePeerConnectionWithDataChannel(Args&&... args) {
185 auto wrapper = CreatePeerConnection(std::forward<Args>(args)...);
186 if (!wrapper) {
187 return nullptr;
188 }
189 EXPECT_TRUE(wrapper->pc()->CreateDataChannel("dc", nullptr));
190 return wrapper;
191 }
192
193 // Changes the SCTP data channel port on the given session description.
194 void ChangeSctpPortOnDescription(cricket::SessionDescription* desc,
195 int port) {
196 cricket::DataCodec sctp_codec(cricket::kGoogleSctpDataCodecPlType,
197 cricket::kGoogleSctpDataCodecName);
198 sctp_codec.SetParam(cricket::kCodecParamPort, port);
199
200 auto* data_content = cricket::GetFirstDataContent(desc);
201 RTC_DCHECK(data_content);
Steve Antonb1c1de12017-12-21 15:14:30 -0800202 auto* data_desc = data_content->media_description()->as_data();
Steve Antonda6c0952017-10-23 11:41:54 -0700203 data_desc->set_codecs({sctp_codec});
204 }
205
206 std::unique_ptr<rtc::VirtualSocketServer> vss_;
207 rtc::AutoSocketServerThread main_;
Steve Antondbf9d032018-01-19 15:23:40 -0800208 const SdpSemantics sdp_semantics_;
Steve Antonda6c0952017-10-23 11:41:54 -0700209};
210
Steve Antondbf9d032018-01-19 15:23:40 -0800211class PeerConnectionDataChannelTest
212 : public PeerConnectionDataChannelBaseTest,
213 public ::testing::WithParamInterface<SdpSemantics> {
214 protected:
215 PeerConnectionDataChannelTest()
216 : PeerConnectionDataChannelBaseTest(GetParam()) {}
217};
218
219TEST_P(PeerConnectionDataChannelTest,
Steve Antonda6c0952017-10-23 11:41:54 -0700220 NoSctpTransportCreatedIfRtpDataChannelEnabled) {
221 RTCConfiguration config;
222 config.enable_rtp_data_channel = true;
223 auto caller = CreatePeerConnectionWithDataChannel(config);
224
225 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
226 EXPECT_FALSE(caller->sctp_transport_factory()->last_fake_sctp_transport());
227}
228
Steve Antondbf9d032018-01-19 15:23:40 -0800229TEST_P(PeerConnectionDataChannelTest,
Steve Antonda6c0952017-10-23 11:41:54 -0700230 RtpDataChannelCreatedEvenIfSctpAvailable) {
231 RTCConfiguration config;
232 config.enable_rtp_data_channel = true;
233 PeerConnectionFactoryInterface::Options options;
234 options.disable_sctp_data_channels = false;
235 auto caller = CreatePeerConnectionWithDataChannel(config, options);
236
237 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
238 EXPECT_FALSE(caller->sctp_transport_factory()->last_fake_sctp_transport());
239}
240
241// Test that sctp_content_name/sctp_transport_name (used for stats) are correct
242// before and after BUNDLE is negotiated.
Steve Antondbf9d032018-01-19 15:23:40 -0800243TEST_P(PeerConnectionDataChannelTest, SctpContentAndTransportNameSetCorrectly) {
Steve Antonda6c0952017-10-23 11:41:54 -0700244 auto caller = CreatePeerConnection();
245 auto callee = CreatePeerConnection();
246
247 // Initially these fields should be empty.
248 EXPECT_FALSE(caller->sctp_content_name());
249 EXPECT_FALSE(caller->sctp_transport_name());
250
251 // Create offer with audio/video/data.
252 // Default bundle policy is "balanced", so data should be using its own
253 // transport.
254 caller->AddAudioTrack("a");
255 caller->AddVideoTrack("v");
256 caller->pc()->CreateDataChannel("dc", nullptr);
Steve Antondbf9d032018-01-19 15:23:40 -0800257
258 auto offer = caller->CreateOffer();
259 const auto& offer_contents = offer->description()->contents();
260 ASSERT_EQ(cricket::MEDIA_TYPE_AUDIO,
261 offer_contents[0].media_description()->type());
262 std::string audio_mid = offer_contents[0].name;
263 ASSERT_EQ(cricket::MEDIA_TYPE_DATA,
264 offer_contents[2].media_description()->type());
265 std::string data_mid = offer_contents[2].name;
266
267 ASSERT_TRUE(
268 caller->SetLocalDescription(CloneSessionDescription(offer.get())));
269 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
Steve Antonda6c0952017-10-23 11:41:54 -0700270
271 ASSERT_TRUE(caller->sctp_content_name());
Steve Antondbf9d032018-01-19 15:23:40 -0800272 EXPECT_EQ(data_mid, *caller->sctp_content_name());
Steve Antonda6c0952017-10-23 11:41:54 -0700273 ASSERT_TRUE(caller->sctp_transport_name());
Steve Antondbf9d032018-01-19 15:23:40 -0800274 EXPECT_EQ(data_mid, *caller->sctp_transport_name());
Steve Antonda6c0952017-10-23 11:41:54 -0700275
276 // Create answer that finishes BUNDLE negotiation, which means everything
277 // should be bundled on the first transport (audio).
278 RTCOfferAnswerOptions options;
279 options.use_rtp_mux = true;
280 ASSERT_TRUE(
281 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
282
283 ASSERT_TRUE(caller->sctp_content_name());
Steve Antondbf9d032018-01-19 15:23:40 -0800284 EXPECT_EQ(data_mid, *caller->sctp_content_name());
Steve Antonda6c0952017-10-23 11:41:54 -0700285 ASSERT_TRUE(caller->sctp_transport_name());
Steve Antondbf9d032018-01-19 15:23:40 -0800286 EXPECT_EQ(audio_mid, *caller->sctp_transport_name());
Steve Antonda6c0952017-10-23 11:41:54 -0700287}
288
Steve Antondbf9d032018-01-19 15:23:40 -0800289TEST_P(PeerConnectionDataChannelTest,
Steve Antonda6c0952017-10-23 11:41:54 -0700290 CreateOfferWithNoDataChannelsGivesNoDataSection) {
291 auto caller = CreatePeerConnection();
292 auto offer = caller->CreateOffer();
293
294 EXPECT_FALSE(offer->description()->GetContentByName(cricket::CN_DATA));
295 EXPECT_FALSE(offer->description()->GetTransportInfoByName(cricket::CN_DATA));
296}
297
Steve Antondbf9d032018-01-19 15:23:40 -0800298TEST_P(PeerConnectionDataChannelTest,
Steve Antonda6c0952017-10-23 11:41:54 -0700299 CreateAnswerWithRemoteSctpDataChannelIncludesDataSection) {
300 auto caller = CreatePeerConnectionWithDataChannel();
301 auto callee = CreatePeerConnection();
302
303 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
304
305 auto answer = callee->CreateAnswer();
306 ASSERT_TRUE(answer);
Steve Antondbf9d032018-01-19 15:23:40 -0800307 auto* data_content = cricket::GetFirstDataContent(answer->description());
Steve Antonda6c0952017-10-23 11:41:54 -0700308 ASSERT_TRUE(data_content);
309 EXPECT_FALSE(data_content->rejected);
Steve Antondbf9d032018-01-19 15:23:40 -0800310 EXPECT_TRUE(
311 answer->description()->GetTransportInfoByName(data_content->name));
Steve Antonda6c0952017-10-23 11:41:54 -0700312}
313
Steve Antondbf9d032018-01-19 15:23:40 -0800314TEST_P(PeerConnectionDataChannelTest,
Steve Antonda6c0952017-10-23 11:41:54 -0700315 CreateDataChannelWithDtlsDisabledSucceeds) {
316 RTCConfiguration config;
317 config.enable_dtls_srtp.emplace(false);
318 auto caller = CreatePeerConnection();
319
320 EXPECT_TRUE(caller->pc()->CreateDataChannel("dc", nullptr));
321}
322
Steve Antondbf9d032018-01-19 15:23:40 -0800323TEST_P(PeerConnectionDataChannelTest, CreateDataChannelWithSctpDisabledFails) {
Steve Antonda6c0952017-10-23 11:41:54 -0700324 PeerConnectionFactoryInterface::Options options;
325 options.disable_sctp_data_channels = true;
326 auto caller = CreatePeerConnection(RTCConfiguration(), options);
327
328 EXPECT_FALSE(caller->pc()->CreateDataChannel("dc", nullptr));
329}
330
331// Test that if a callee has SCTP disabled and receives an offer with an SCTP
332// data channel, the data section is rejected and no SCTP transport is created
333// on the callee.
Steve Antondbf9d032018-01-19 15:23:40 -0800334TEST_P(PeerConnectionDataChannelTest,
Steve Antonda6c0952017-10-23 11:41:54 -0700335 DataSectionRejectedIfCalleeHasSctpDisabled) {
336 auto caller = CreatePeerConnectionWithDataChannel();
337 PeerConnectionFactoryInterface::Options options;
338 options.disable_sctp_data_channels = true;
339 auto callee = CreatePeerConnection(RTCConfiguration(), options);
340
341 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
342
343 EXPECT_FALSE(callee->sctp_transport_factory()->last_fake_sctp_transport());
344
345 auto answer = callee->CreateAnswer();
Steve Antondbf9d032018-01-19 15:23:40 -0800346 auto* data_content = cricket::GetFirstDataContent(answer->description());
Steve Antonda6c0952017-10-23 11:41:54 -0700347 ASSERT_TRUE(data_content);
348 EXPECT_TRUE(data_content->rejected);
349}
350
Steve Antondbf9d032018-01-19 15:23:40 -0800351TEST_P(PeerConnectionDataChannelTest, SctpPortPropagatedFromSdpToTransport) {
Steve Antonda6c0952017-10-23 11:41:54 -0700352 constexpr int kNewSendPort = 9998;
353 constexpr int kNewRecvPort = 7775;
354
355 auto caller = CreatePeerConnectionWithDataChannel();
356 auto callee = CreatePeerConnectionWithDataChannel();
357
358 auto offer = caller->CreateOffer();
359 ChangeSctpPortOnDescription(offer->description(), kNewSendPort);
360 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
361
362 auto answer = callee->CreateAnswer();
363 ChangeSctpPortOnDescription(answer->description(), kNewRecvPort);
364 ASSERT_TRUE(callee->SetLocalDescription(std::move(answer)));
365
366 auto* callee_transport =
367 callee->sctp_transport_factory()->last_fake_sctp_transport();
368 ASSERT_TRUE(callee_transport);
369 EXPECT_EQ(kNewSendPort, callee_transport->remote_port());
370 EXPECT_EQ(kNewRecvPort, callee_transport->local_port());
371}
372
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800373TEST_P(PeerConnectionDataChannelTest,
374 NoSctpTransportCreatedIfMediaTransportDataChannelsEnabled) {
375 RTCConfiguration config;
376 config.use_media_transport_for_data_channels = true;
377 config.enable_dtls_srtp = false; // SDES is required to use media transport.
378 auto caller = CreatePeerConnectionWithDataChannel(config);
379
380 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
381 EXPECT_FALSE(caller->sctp_transport_factory()->last_fake_sctp_transport());
382}
383
384TEST_P(PeerConnectionDataChannelTest,
385 MediaTransportDataChannelCreatedEvenIfSctpAvailable) {
386 RTCConfiguration config;
387 config.use_media_transport_for_data_channels = true;
388 config.enable_dtls_srtp = false; // SDES is required to use media transport.
389 PeerConnectionFactoryInterface::Options options;
390 options.disable_sctp_data_channels = false;
391 auto caller = CreatePeerConnectionWithDataChannel(config, options);
392
393 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
394 EXPECT_FALSE(caller->sctp_transport_factory()->last_fake_sctp_transport());
395}
396
397TEST_P(PeerConnectionDataChannelTest,
398 CannotEnableBothMediaTransportAndRtpDataChannels) {
399 RTCConfiguration config;
400 config.enable_rtp_data_channel = true;
401 config.use_media_transport_for_data_channels = true;
402 config.enable_dtls_srtp = false; // SDES is required to use media transport.
403 EXPECT_EQ(CreatePeerConnection(config), nullptr);
404}
405
406TEST_P(PeerConnectionDataChannelTest,
407 MediaTransportDataChannelFailsWithoutSdes) {
408 RTCConfiguration config;
409 config.use_media_transport_for_data_channels = true;
410 config.enable_dtls_srtp = true; // Disables SDES for data sections.
411 auto caller = CreatePeerConnectionWithDataChannel(config);
412
413 std::string error;
414 ASSERT_FALSE(caller->SetLocalDescription(caller->CreateOffer(), &error));
415 EXPECT_EQ(error,
416 "Failed to set local offer sdp: Failed to create data channel.");
417}
418
Steve Antondbf9d032018-01-19 15:23:40 -0800419INSTANTIATE_TEST_CASE_P(PeerConnectionDataChannelTest,
420 PeerConnectionDataChannelTest,
421 Values(SdpSemantics::kPlanB,
422 SdpSemantics::kUnifiedPlan));
423
Steve Antonda6c0952017-10-23 11:41:54 -0700424} // namespace webrtc