blob: 4080dd98bb31534f7678f7b27fd9d1fb39072c4a [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"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "api/call/call_factory_interface.h"
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "api/jsep.h"
20#include "api/media_transport_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "api/media_types.h"
22#include "api/peer_connection_interface.h"
23#include "api/peer_connection_proxy.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010024#include "api/scoped_refptr.h"
Bjorn Mellem175aa2e2018-11-08 11:23:22 -080025#include "api/test/fake_media_transport.h"
Yves Gerey3e707812018-11-28 16:47:49 +010026#include "media/base/codec.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "media/base/fake_media_engine.h"
28#include "media/base/media_constants.h"
29#include "media/base/media_engine.h"
30#include "media/sctp/sctp_transport_internal.h"
31#include "p2p/base/p2p_constants.h"
32#include "p2p/base/port_allocator.h"
33#include "pc/media_session.h"
34#include "pc/peer_connection.h"
35#include "pc/peer_connection_factory.h"
36#include "pc/peer_connection_wrapper.h"
37#include "pc/sdp_utils.h"
38#include "pc/session_description.h"
39#include "pc/test/mock_peer_connection_observers.h"
Yves Gerey3e707812018-11-28 16:47:49 +010040#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080041#include "rtc_base/ref_counted_object.h"
42#include "rtc_base/rtc_certificate_generator.h"
Yves Gerey3e707812018-11-28 16:47:49 +010043#include "rtc_base/thread.h"
44#include "test/gtest.h"
Steve Antonda6c0952017-10-23 11:41:54 -070045#ifdef WEBRTC_ANDROID
Steve Anton10542f22019-01-11 09:11:00 -080046#include "pc/test/android_test_initializer.h"
Steve Antonda6c0952017-10-23 11:41:54 -070047#endif
Karl Wiberg918f50c2018-07-05 11:40:33 +020048#include "absl/memory/memory.h"
Steve Anton10542f22019-01-11 09:11:00 -080049#include "pc/test/fake_sctp_transport.h"
50#include "rtc_base/virtual_socket_server.h"
Steve Antonda6c0952017-10-23 11:41:54 -070051
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) {
Steve Antonda6c0952017-10-23 11:41:54 -0700196 auto* data_content = cricket::GetFirstDataContent(desc);
197 RTC_DCHECK(data_content);
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200198 auto* data_desc = data_content->media_description()->as_sctp();
199 RTC_DCHECK(data_desc);
200 data_desc->set_port(port);
Steve Antonda6c0952017-10-23 11:41:54 -0700201 }
202
203 std::unique_ptr<rtc::VirtualSocketServer> vss_;
204 rtc::AutoSocketServerThread main_;
Steve Antondbf9d032018-01-19 15:23:40 -0800205 const SdpSemantics sdp_semantics_;
Steve Antonda6c0952017-10-23 11:41:54 -0700206};
207
Steve Antondbf9d032018-01-19 15:23:40 -0800208class PeerConnectionDataChannelTest
209 : public PeerConnectionDataChannelBaseTest,
210 public ::testing::WithParamInterface<SdpSemantics> {
211 protected:
212 PeerConnectionDataChannelTest()
213 : PeerConnectionDataChannelBaseTest(GetParam()) {}
214};
215
216TEST_P(PeerConnectionDataChannelTest,
Steve Antonda6c0952017-10-23 11:41:54 -0700217 NoSctpTransportCreatedIfRtpDataChannelEnabled) {
218 RTCConfiguration config;
219 config.enable_rtp_data_channel = true;
220 auto caller = CreatePeerConnectionWithDataChannel(config);
221
222 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
223 EXPECT_FALSE(caller->sctp_transport_factory()->last_fake_sctp_transport());
224}
225
Steve Antondbf9d032018-01-19 15:23:40 -0800226TEST_P(PeerConnectionDataChannelTest,
Steve Antonda6c0952017-10-23 11:41:54 -0700227 RtpDataChannelCreatedEvenIfSctpAvailable) {
228 RTCConfiguration config;
229 config.enable_rtp_data_channel = true;
230 PeerConnectionFactoryInterface::Options options;
231 options.disable_sctp_data_channels = false;
232 auto caller = CreatePeerConnectionWithDataChannel(config, options);
233
234 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
235 EXPECT_FALSE(caller->sctp_transport_factory()->last_fake_sctp_transport());
236}
237
238// Test that sctp_content_name/sctp_transport_name (used for stats) are correct
239// before and after BUNDLE is negotiated.
Steve Antondbf9d032018-01-19 15:23:40 -0800240TEST_P(PeerConnectionDataChannelTest, SctpContentAndTransportNameSetCorrectly) {
Steve Antonda6c0952017-10-23 11:41:54 -0700241 auto caller = CreatePeerConnection();
242 auto callee = CreatePeerConnection();
243
244 // Initially these fields should be empty.
245 EXPECT_FALSE(caller->sctp_content_name());
246 EXPECT_FALSE(caller->sctp_transport_name());
247
248 // Create offer with audio/video/data.
249 // Default bundle policy is "balanced", so data should be using its own
250 // transport.
251 caller->AddAudioTrack("a");
252 caller->AddVideoTrack("v");
253 caller->pc()->CreateDataChannel("dc", nullptr);
Steve Antondbf9d032018-01-19 15:23:40 -0800254
255 auto offer = caller->CreateOffer();
256 const auto& offer_contents = offer->description()->contents();
257 ASSERT_EQ(cricket::MEDIA_TYPE_AUDIO,
258 offer_contents[0].media_description()->type());
259 std::string audio_mid = offer_contents[0].name;
260 ASSERT_EQ(cricket::MEDIA_TYPE_DATA,
261 offer_contents[2].media_description()->type());
262 std::string data_mid = offer_contents[2].name;
263
264 ASSERT_TRUE(
265 caller->SetLocalDescription(CloneSessionDescription(offer.get())));
266 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
Steve Antonda6c0952017-10-23 11:41:54 -0700267
268 ASSERT_TRUE(caller->sctp_content_name());
Steve Antondbf9d032018-01-19 15:23:40 -0800269 EXPECT_EQ(data_mid, *caller->sctp_content_name());
Steve Antonda6c0952017-10-23 11:41:54 -0700270 ASSERT_TRUE(caller->sctp_transport_name());
Steve Antondbf9d032018-01-19 15:23:40 -0800271 EXPECT_EQ(data_mid, *caller->sctp_transport_name());
Steve Antonda6c0952017-10-23 11:41:54 -0700272
273 // Create answer that finishes BUNDLE negotiation, which means everything
274 // should be bundled on the first transport (audio).
275 RTCOfferAnswerOptions options;
276 options.use_rtp_mux = true;
277 ASSERT_TRUE(
278 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
279
280 ASSERT_TRUE(caller->sctp_content_name());
Steve Antondbf9d032018-01-19 15:23:40 -0800281 EXPECT_EQ(data_mid, *caller->sctp_content_name());
Steve Antonda6c0952017-10-23 11:41:54 -0700282 ASSERT_TRUE(caller->sctp_transport_name());
Steve Antondbf9d032018-01-19 15:23:40 -0800283 EXPECT_EQ(audio_mid, *caller->sctp_transport_name());
Steve Antonda6c0952017-10-23 11:41:54 -0700284}
285
Steve Antondbf9d032018-01-19 15:23:40 -0800286TEST_P(PeerConnectionDataChannelTest,
Steve Antonda6c0952017-10-23 11:41:54 -0700287 CreateOfferWithNoDataChannelsGivesNoDataSection) {
288 auto caller = CreatePeerConnection();
289 auto offer = caller->CreateOffer();
290
291 EXPECT_FALSE(offer->description()->GetContentByName(cricket::CN_DATA));
292 EXPECT_FALSE(offer->description()->GetTransportInfoByName(cricket::CN_DATA));
293}
294
Steve Antondbf9d032018-01-19 15:23:40 -0800295TEST_P(PeerConnectionDataChannelTest,
Steve Antonda6c0952017-10-23 11:41:54 -0700296 CreateAnswerWithRemoteSctpDataChannelIncludesDataSection) {
297 auto caller = CreatePeerConnectionWithDataChannel();
298 auto callee = CreatePeerConnection();
299
300 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
301
302 auto answer = callee->CreateAnswer();
303 ASSERT_TRUE(answer);
Steve Antondbf9d032018-01-19 15:23:40 -0800304 auto* data_content = cricket::GetFirstDataContent(answer->description());
Steve Antonda6c0952017-10-23 11:41:54 -0700305 ASSERT_TRUE(data_content);
306 EXPECT_FALSE(data_content->rejected);
Steve Antondbf9d032018-01-19 15:23:40 -0800307 EXPECT_TRUE(
308 answer->description()->GetTransportInfoByName(data_content->name));
Steve Antonda6c0952017-10-23 11:41:54 -0700309}
310
Steve Antondbf9d032018-01-19 15:23:40 -0800311TEST_P(PeerConnectionDataChannelTest,
Steve Antonda6c0952017-10-23 11:41:54 -0700312 CreateDataChannelWithDtlsDisabledSucceeds) {
313 RTCConfiguration config;
314 config.enable_dtls_srtp.emplace(false);
315 auto caller = CreatePeerConnection();
316
317 EXPECT_TRUE(caller->pc()->CreateDataChannel("dc", nullptr));
318}
319
Steve Antondbf9d032018-01-19 15:23:40 -0800320TEST_P(PeerConnectionDataChannelTest, CreateDataChannelWithSctpDisabledFails) {
Steve Antonda6c0952017-10-23 11:41:54 -0700321 PeerConnectionFactoryInterface::Options options;
322 options.disable_sctp_data_channels = true;
323 auto caller = CreatePeerConnection(RTCConfiguration(), options);
324
325 EXPECT_FALSE(caller->pc()->CreateDataChannel("dc", nullptr));
326}
327
328// Test that if a callee has SCTP disabled and receives an offer with an SCTP
329// data channel, the data section is rejected and no SCTP transport is created
330// on the callee.
Steve Antondbf9d032018-01-19 15:23:40 -0800331TEST_P(PeerConnectionDataChannelTest,
Steve Antonda6c0952017-10-23 11:41:54 -0700332 DataSectionRejectedIfCalleeHasSctpDisabled) {
333 auto caller = CreatePeerConnectionWithDataChannel();
334 PeerConnectionFactoryInterface::Options options;
335 options.disable_sctp_data_channels = true;
336 auto callee = CreatePeerConnection(RTCConfiguration(), options);
337
338 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
339
340 EXPECT_FALSE(callee->sctp_transport_factory()->last_fake_sctp_transport());
341
342 auto answer = callee->CreateAnswer();
Steve Antondbf9d032018-01-19 15:23:40 -0800343 auto* data_content = cricket::GetFirstDataContent(answer->description());
Steve Antonda6c0952017-10-23 11:41:54 -0700344 ASSERT_TRUE(data_content);
345 EXPECT_TRUE(data_content->rejected);
346}
347
Steve Antondbf9d032018-01-19 15:23:40 -0800348TEST_P(PeerConnectionDataChannelTest, SctpPortPropagatedFromSdpToTransport) {
Steve Antonda6c0952017-10-23 11:41:54 -0700349 constexpr int kNewSendPort = 9998;
350 constexpr int kNewRecvPort = 7775;
351
352 auto caller = CreatePeerConnectionWithDataChannel();
353 auto callee = CreatePeerConnectionWithDataChannel();
354
355 auto offer = caller->CreateOffer();
356 ChangeSctpPortOnDescription(offer->description(), kNewSendPort);
357 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
358
359 auto answer = callee->CreateAnswer();
360 ChangeSctpPortOnDescription(answer->description(), kNewRecvPort);
361 ASSERT_TRUE(callee->SetLocalDescription(std::move(answer)));
362
363 auto* callee_transport =
364 callee->sctp_transport_factory()->last_fake_sctp_transport();
365 ASSERT_TRUE(callee_transport);
366 EXPECT_EQ(kNewSendPort, callee_transport->remote_port());
367 EXPECT_EQ(kNewRecvPort, callee_transport->local_port());
368}
369
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800370TEST_P(PeerConnectionDataChannelTest,
371 NoSctpTransportCreatedIfMediaTransportDataChannelsEnabled) {
372 RTCConfiguration config;
373 config.use_media_transport_for_data_channels = true;
374 config.enable_dtls_srtp = false; // SDES is required to use media transport.
375 auto caller = CreatePeerConnectionWithDataChannel(config);
376
377 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
378 EXPECT_FALSE(caller->sctp_transport_factory()->last_fake_sctp_transport());
379}
380
381TEST_P(PeerConnectionDataChannelTest,
382 MediaTransportDataChannelCreatedEvenIfSctpAvailable) {
383 RTCConfiguration config;
384 config.use_media_transport_for_data_channels = true;
385 config.enable_dtls_srtp = false; // SDES is required to use media transport.
386 PeerConnectionFactoryInterface::Options options;
387 options.disable_sctp_data_channels = false;
388 auto caller = CreatePeerConnectionWithDataChannel(config, options);
389
390 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
391 EXPECT_FALSE(caller->sctp_transport_factory()->last_fake_sctp_transport());
392}
393
394TEST_P(PeerConnectionDataChannelTest,
395 CannotEnableBothMediaTransportAndRtpDataChannels) {
396 RTCConfiguration config;
397 config.enable_rtp_data_channel = true;
398 config.use_media_transport_for_data_channels = true;
399 config.enable_dtls_srtp = false; // SDES is required to use media transport.
400 EXPECT_EQ(CreatePeerConnection(config), nullptr);
401}
402
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -0800403// This test now DCHECKs, instead of failing to SetLocalDescription.
404TEST_P(PeerConnectionDataChannelTest, MediaTransportWithoutSdesFails) {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800405 RTCConfiguration config;
406 config.use_media_transport_for_data_channels = true;
407 config.enable_dtls_srtp = true; // Disables SDES for data sections.
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -0800408
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800409 auto caller = CreatePeerConnectionWithDataChannel(config);
410
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -0800411 EXPECT_EQ(nullptr, caller);
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800412}
413
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100414INSTANTIATE_TEST_SUITE_P(PeerConnectionDataChannelTest,
415 PeerConnectionDataChannelTest,
416 Values(SdpSemantics::kPlanB,
417 SdpSemantics::kUnifiedPlan));
Steve Antondbf9d032018-01-19 15:23:40 -0800418
Steve Antonda6c0952017-10-23 11:41:54 -0700419} // namespace webrtc