blob: 2544473536e4b8e09acd0c0f026fa323342a89fb [file] [log] [blame]
Steve Antonda6c0952017-10-23 18:41:541/*
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 15:47:4911#include <memory>
12#include <string>
13#include <type_traits>
14#include <utility>
15#include <vector>
Steve Antonda6c0952017-10-23 18:41:5416
Yves Gerey3e707812018-11-28 15:47:4917#include "absl/types/optional.h"
Steve Anton10542f22019-01-11 17:11:0018#include "api/call/call_factory_interface.h"
Yves Gerey3e707812018-11-28 15:47:4919#include "api/jsep.h"
Steve Anton10542f22019-01-11 17:11:0020#include "api/media_types.h"
21#include "api/peer_connection_interface.h"
Mirko Bonadeid9708072019-01-25 19:26:4822#include "api/scoped_refptr.h"
Danil Chapovalov53d45ba2019-07-03 12:56:3323#include "api/task_queue/default_task_queue_factory.h"
Yves Gerey3e707812018-11-28 15:47:4924#include "media/base/codec.h"
Steve Anton10542f22019-01-11 17:11:0025#include "media/base/fake_media_engine.h"
26#include "media/base/media_constants.h"
27#include "media/base/media_engine.h"
28#include "media/sctp/sctp_transport_internal.h"
29#include "p2p/base/p2p_constants.h"
30#include "p2p/base/port_allocator.h"
31#include "pc/media_session.h"
32#include "pc/peer_connection.h"
33#include "pc/peer_connection_factory.h"
Markus Handella1b82012021-05-26 16:56:3034#include "pc/peer_connection_proxy.h"
Steve Anton10542f22019-01-11 17:11:0035#include "pc/peer_connection_wrapper.h"
36#include "pc/sdp_utils.h"
37#include "pc/session_description.h"
38#include "pc/test/mock_peer_connection_observers.h"
Yves Gerey3e707812018-11-28 15:47:4939#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 17:11:0040#include "rtc_base/ref_counted_object.h"
41#include "rtc_base/rtc_certificate_generator.h"
Yves Gerey3e707812018-11-28 15:47:4942#include "rtc_base/thread.h"
Harald Alvestrand4aa11922019-05-14 20:00:0143#include "test/gmock.h"
Yves Gerey3e707812018-11-28 15:47:4944#include "test/gtest.h"
Steve Antonda6c0952017-10-23 18:41:5445#ifdef WEBRTC_ANDROID
Steve Anton10542f22019-01-11 17:11:0046#include "pc/test/android_test_initializer.h"
Steve Antonda6c0952017-10-23 18:41:5447#endif
Steve Anton10542f22019-01-11 17:11:0048#include "rtc_base/virtual_socket_server.h"
Per Kjellander2bca0082020-08-28 07:15:1549#include "test/pc/sctp/fake_sctp_transport.h"
Steve Antonda6c0952017-10-23 18:41:5450
51namespace webrtc {
52
53using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
54using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
Harald Alvestrand4aa11922019-05-14 20:00:0155using ::testing::HasSubstr;
56using ::testing::Not;
Steve Antonda6c0952017-10-23 18:41:5457using ::testing::Values;
58
Bjorn Mellem175aa2e2018-11-08 19:23:2259namespace {
60
Per Kjellander2bca0082020-08-28 07:15:1561PeerConnectionFactoryDependencies CreatePeerConnectionFactoryDependencies() {
Bjorn Mellem175aa2e2018-11-08 19:23:2262 PeerConnectionFactoryDependencies deps;
Per Kjellander2bca0082020-08-28 07:15:1563 deps.network_thread = rtc::Thread::Current();
64 deps.worker_thread = rtc::Thread::Current();
65 deps.signaling_thread = rtc::Thread::Current();
Danil Chapovalov53d45ba2019-07-03 12:56:3366 deps.task_queue_factory = CreateDefaultTaskQueueFactory();
Per Kjellander2bca0082020-08-28 07:15:1567 deps.media_engine = std::make_unique<cricket::FakeMediaEngine>();
68 deps.call_factory = CreateCallFactory();
69 deps.sctp_factory = std::make_unique<FakeSctpTransportFactory>();
Bjorn Mellem175aa2e2018-11-08 19:23:2270 return deps;
71}
72
73} // namespace
74
Steve Antonda6c0952017-10-23 18:41:5475class PeerConnectionWrapperForDataChannelTest : public PeerConnectionWrapper {
76 public:
77 using PeerConnectionWrapper::PeerConnectionWrapper;
78
79 FakeSctpTransportFactory* sctp_transport_factory() {
80 return sctp_transport_factory_;
81 }
82
83 void set_sctp_transport_factory(
84 FakeSctpTransportFactory* sctp_transport_factory) {
85 sctp_transport_factory_ = sctp_transport_factory;
86 }
87
Harald Alvestrand1cb929f2020-02-05 11:07:3388 absl::optional<std::string> sctp_mid() {
89 return GetInternalPeerConnection()->sctp_mid();
Steve Antonda6c0952017-10-23 18:41:5490 }
91
Danil Chapovalov66cadcc2018-06-19 14:47:4392 absl::optional<std::string> sctp_transport_name() {
Steve Antonda6c0952017-10-23 18:41:5493 return GetInternalPeerConnection()->sctp_transport_name();
94 }
95
96 PeerConnection* GetInternalPeerConnection() {
Mirko Bonadeie97de912017-12-13 10:29:3497 auto* pci =
98 static_cast<PeerConnectionProxyWithInternal<PeerConnectionInterface>*>(
99 pc());
100 return static_cast<PeerConnection*>(pci->internal());
Steve Antonda6c0952017-10-23 18:41:54101 }
102
103 private:
104 FakeSctpTransportFactory* sctp_transport_factory_ = nullptr;
105};
106
Steve Antondbf9d032018-01-19 23:23:40107class PeerConnectionDataChannelBaseTest : public ::testing::Test {
Steve Antonda6c0952017-10-23 18:41:54108 protected:
109 typedef std::unique_ptr<PeerConnectionWrapperForDataChannelTest> WrapperPtr;
110
Steve Antondbf9d032018-01-19 23:23:40111 explicit PeerConnectionDataChannelBaseTest(SdpSemantics sdp_semantics)
112 : vss_(new rtc::VirtualSocketServer()),
113 main_(vss_.get()),
114 sdp_semantics_(sdp_semantics) {
Steve Antonda6c0952017-10-23 18:41:54115#ifdef WEBRTC_ANDROID
116 InitializeAndroidObjects();
117#endif
118 }
119
120 WrapperPtr CreatePeerConnection() {
121 return CreatePeerConnection(RTCConfiguration());
122 }
123
124 WrapperPtr CreatePeerConnection(const RTCConfiguration& config) {
125 return CreatePeerConnection(config,
126 PeerConnectionFactoryInterface::Options());
127 }
128
129 WrapperPtr CreatePeerConnection(
130 const RTCConfiguration& config,
131 const PeerConnectionFactoryInterface::Options factory_options) {
Per Kjellander2bca0082020-08-28 07:15:15132 auto factory_deps = CreatePeerConnectionFactoryDependencies();
133 FakeSctpTransportFactory* fake_sctp_transport_factory =
134 static_cast<FakeSctpTransportFactory*>(factory_deps.sctp_factory.get());
135 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory =
136 CreateModularPeerConnectionFactory(std::move(factory_deps));
Steve Antonda6c0952017-10-23 18:41:54137 pc_factory->SetOptions(factory_options);
Mirko Bonadei317a1f02019-09-17 15:06:18138 auto observer = std::make_unique<MockPeerConnectionObserver>();
Steve Antondbf9d032018-01-19 23:23:40139 RTCConfiguration modified_config = config;
140 modified_config.sdp_semantics = sdp_semantics_;
141 auto pc = pc_factory->CreatePeerConnection(modified_config, nullptr,
142 nullptr, observer.get());
Steve Antonda6c0952017-10-23 18:41:54143 if (!pc) {
144 return nullptr;
145 }
146
Yves Gerey4e933292018-10-31 14:36:05147 observer->SetPeerConnectionInterface(pc.get());
Mirko Bonadei317a1f02019-09-17 15:06:18148 auto wrapper = std::make_unique<PeerConnectionWrapperForDataChannelTest>(
Steve Antonda6c0952017-10-23 18:41:54149 pc_factory, pc, std::move(observer));
Per Kjellander2bca0082020-08-28 07:15:15150 wrapper->set_sctp_transport_factory(fake_sctp_transport_factory);
Steve Antonda6c0952017-10-23 18:41:54151 return wrapper;
152 }
153
154 // Accepts the same arguments as CreatePeerConnection and adds a default data
155 // channel.
156 template <typename... Args>
157 WrapperPtr CreatePeerConnectionWithDataChannel(Args&&... args) {
158 auto wrapper = CreatePeerConnection(std::forward<Args>(args)...);
159 if (!wrapper) {
160 return nullptr;
161 }
162 EXPECT_TRUE(wrapper->pc()->CreateDataChannel("dc", nullptr));
163 return wrapper;
164 }
165
166 // Changes the SCTP data channel port on the given session description.
167 void ChangeSctpPortOnDescription(cricket::SessionDescription* desc,
168 int port) {
Steve Antonda6c0952017-10-23 18:41:54169 auto* data_content = cricket::GetFirstDataContent(desc);
170 RTC_DCHECK(data_content);
Harald Alvestrand5fc28b12019-05-13 11:36:16171 auto* data_desc = data_content->media_description()->as_sctp();
172 RTC_DCHECK(data_desc);
173 data_desc->set_port(port);
Steve Antonda6c0952017-10-23 18:41:54174 }
175
176 std::unique_ptr<rtc::VirtualSocketServer> vss_;
177 rtc::AutoSocketServerThread main_;
Steve Antondbf9d032018-01-19 23:23:40178 const SdpSemantics sdp_semantics_;
Steve Antonda6c0952017-10-23 18:41:54179};
180
Steve Antondbf9d032018-01-19 23:23:40181class PeerConnectionDataChannelTest
182 : public PeerConnectionDataChannelBaseTest,
183 public ::testing::WithParamInterface<SdpSemantics> {
184 protected:
185 PeerConnectionDataChannelTest()
186 : PeerConnectionDataChannelBaseTest(GetParam()) {}
187};
188
Steve Antonc8ff1602020-02-05 21:53:38189class PeerConnectionDataChannelUnifiedPlanTest
190 : public PeerConnectionDataChannelBaseTest {
191 protected:
192 PeerConnectionDataChannelUnifiedPlanTest()
193 : PeerConnectionDataChannelBaseTest(SdpSemantics::kUnifiedPlan) {}
194};
195
Bjorn A Mellembc3eebc2019-09-23 21:53:54196TEST_P(PeerConnectionDataChannelTest, InternalSctpTransportDeletedOnTeardown) {
197 auto caller = CreatePeerConnectionWithDataChannel();
198
199 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
200 EXPECT_TRUE(caller->sctp_transport_factory()->last_fake_sctp_transport());
201
202 rtc::scoped_refptr<SctpTransportInterface> sctp_transport =
203 caller->GetInternalPeerConnection()->GetSctpTransport();
204
205 caller.reset();
206 EXPECT_EQ(static_cast<SctpTransport*>(sctp_transport.get())->internal(),
207 nullptr);
208}
209
Harald Alvestrand1cb929f2020-02-05 11:07:33210// Test that sctp_mid/sctp_transport_name (used for stats) are correct
Steve Antonda6c0952017-10-23 18:41:54211// before and after BUNDLE is negotiated.
Steve Antondbf9d032018-01-19 23:23:40212TEST_P(PeerConnectionDataChannelTest, SctpContentAndTransportNameSetCorrectly) {
Steve Antonda6c0952017-10-23 18:41:54213 auto caller = CreatePeerConnection();
214 auto callee = CreatePeerConnection();
215
216 // Initially these fields should be empty.
Harald Alvestrand1cb929f2020-02-05 11:07:33217 EXPECT_FALSE(caller->sctp_mid());
Steve Antonda6c0952017-10-23 18:41:54218 EXPECT_FALSE(caller->sctp_transport_name());
219
220 // Create offer with audio/video/data.
221 // Default bundle policy is "balanced", so data should be using its own
222 // transport.
223 caller->AddAudioTrack("a");
224 caller->AddVideoTrack("v");
225 caller->pc()->CreateDataChannel("dc", nullptr);
Steve Antondbf9d032018-01-19 23:23:40226
227 auto offer = caller->CreateOffer();
228 const auto& offer_contents = offer->description()->contents();
229 ASSERT_EQ(cricket::MEDIA_TYPE_AUDIO,
230 offer_contents[0].media_description()->type());
231 std::string audio_mid = offer_contents[0].name;
232 ASSERT_EQ(cricket::MEDIA_TYPE_DATA,
233 offer_contents[2].media_description()->type());
234 std::string data_mid = offer_contents[2].name;
235
236 ASSERT_TRUE(
237 caller->SetLocalDescription(CloneSessionDescription(offer.get())));
238 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
Steve Antonda6c0952017-10-23 18:41:54239
Harald Alvestrand1cb929f2020-02-05 11:07:33240 ASSERT_TRUE(caller->sctp_mid());
241 EXPECT_EQ(data_mid, *caller->sctp_mid());
Steve Antonda6c0952017-10-23 18:41:54242 ASSERT_TRUE(caller->sctp_transport_name());
Steve Antondbf9d032018-01-19 23:23:40243 EXPECT_EQ(data_mid, *caller->sctp_transport_name());
Steve Antonda6c0952017-10-23 18:41:54244
245 // Create answer that finishes BUNDLE negotiation, which means everything
246 // should be bundled on the first transport (audio).
247 RTCOfferAnswerOptions options;
248 options.use_rtp_mux = true;
249 ASSERT_TRUE(
250 caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal()));
251
Harald Alvestrand1cb929f2020-02-05 11:07:33252 ASSERT_TRUE(caller->sctp_mid());
253 EXPECT_EQ(data_mid, *caller->sctp_mid());
Steve Antonda6c0952017-10-23 18:41:54254 ASSERT_TRUE(caller->sctp_transport_name());
Steve Antondbf9d032018-01-19 23:23:40255 EXPECT_EQ(audio_mid, *caller->sctp_transport_name());
Steve Antonda6c0952017-10-23 18:41:54256}
257
Steve Antondbf9d032018-01-19 23:23:40258TEST_P(PeerConnectionDataChannelTest,
Steve Antonda6c0952017-10-23 18:41:54259 CreateOfferWithNoDataChannelsGivesNoDataSection) {
260 auto caller = CreatePeerConnection();
261 auto offer = caller->CreateOffer();
262
263 EXPECT_FALSE(offer->description()->GetContentByName(cricket::CN_DATA));
264 EXPECT_FALSE(offer->description()->GetTransportInfoByName(cricket::CN_DATA));
265}
266
Steve Antondbf9d032018-01-19 23:23:40267TEST_P(PeerConnectionDataChannelTest,
Steve Antonda6c0952017-10-23 18:41:54268 CreateAnswerWithRemoteSctpDataChannelIncludesDataSection) {
269 auto caller = CreatePeerConnectionWithDataChannel();
270 auto callee = CreatePeerConnection();
271
272 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
273
274 auto answer = callee->CreateAnswer();
275 ASSERT_TRUE(answer);
Steve Antondbf9d032018-01-19 23:23:40276 auto* data_content = cricket::GetFirstDataContent(answer->description());
Steve Antonda6c0952017-10-23 18:41:54277 ASSERT_TRUE(data_content);
278 EXPECT_FALSE(data_content->rejected);
Steve Antondbf9d032018-01-19 23:23:40279 EXPECT_TRUE(
280 answer->description()->GetTransportInfoByName(data_content->name));
Steve Antonda6c0952017-10-23 18:41:54281}
282
Steve Antondbf9d032018-01-19 23:23:40283TEST_P(PeerConnectionDataChannelTest,
Steve Antonda6c0952017-10-23 18:41:54284 CreateDataChannelWithDtlsDisabledSucceeds) {
285 RTCConfiguration config;
286 config.enable_dtls_srtp.emplace(false);
287 auto caller = CreatePeerConnection();
288
289 EXPECT_TRUE(caller->pc()->CreateDataChannel("dc", nullptr));
290}
291
Steve Antondbf9d032018-01-19 23:23:40292TEST_P(PeerConnectionDataChannelTest, SctpPortPropagatedFromSdpToTransport) {
Steve Antonda6c0952017-10-23 18:41:54293 constexpr int kNewSendPort = 9998;
294 constexpr int kNewRecvPort = 7775;
295
296 auto caller = CreatePeerConnectionWithDataChannel();
297 auto callee = CreatePeerConnectionWithDataChannel();
298
299 auto offer = caller->CreateOffer();
300 ChangeSctpPortOnDescription(offer->description(), kNewSendPort);
301 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
302
303 auto answer = callee->CreateAnswer();
304 ChangeSctpPortOnDescription(answer->description(), kNewRecvPort);
Harald Alvestrand7af57c62021-04-16 11:12:14305 std::string sdp;
306 answer->ToString(&sdp);
Steve Antonda6c0952017-10-23 18:41:54307 ASSERT_TRUE(callee->SetLocalDescription(std::move(answer)));
Steve Antonda6c0952017-10-23 18:41:54308 auto* callee_transport =
309 callee->sctp_transport_factory()->last_fake_sctp_transport();
310 ASSERT_TRUE(callee_transport);
311 EXPECT_EQ(kNewSendPort, callee_transport->remote_port());
312 EXPECT_EQ(kNewRecvPort, callee_transport->local_port());
313}
314
Guido Urdanetacecf87f2019-05-31 10:17:38315TEST_P(PeerConnectionDataChannelTest, ModernSdpSyntaxByDefault) {
Harald Alvestrand4aa11922019-05-14 20:00:01316 PeerConnectionInterface::RTCOfferAnswerOptions options;
317 auto caller = CreatePeerConnectionWithDataChannel();
318 auto offer = caller->CreateOffer(options);
319 EXPECT_FALSE(cricket::GetFirstSctpDataContentDescription(offer->description())
320 ->use_sctpmap());
321 std::string sdp;
322 offer->ToString(&sdp);
323 RTC_LOG(LS_ERROR) << sdp;
324 EXPECT_THAT(sdp, HasSubstr(" UDP/DTLS/SCTP webrtc-datachannel"));
325 EXPECT_THAT(sdp, Not(HasSubstr("a=sctpmap:")));
326}
327
328TEST_P(PeerConnectionDataChannelTest, ObsoleteSdpSyntaxIfSet) {
329 PeerConnectionInterface::RTCOfferAnswerOptions options;
330 options.use_obsolete_sctp_sdp = true;
331 auto caller = CreatePeerConnectionWithDataChannel();
332 auto offer = caller->CreateOffer(options);
333 EXPECT_TRUE(cricket::GetFirstSctpDataContentDescription(offer->description())
334 ->use_sctpmap());
335 std::string sdp;
336 offer->ToString(&sdp);
337 EXPECT_THAT(sdp, Not(HasSubstr(" UDP/DTLS/SCTP webrtc-datachannel")));
338 EXPECT_THAT(sdp, HasSubstr("a=sctpmap:"));
339}
340
Mirko Bonadeic84f6612019-01-31 11:20:57341INSTANTIATE_TEST_SUITE_P(PeerConnectionDataChannelTest,
342 PeerConnectionDataChannelTest,
343 Values(SdpSemantics::kPlanB,
344 SdpSemantics::kUnifiedPlan));
Steve Antondbf9d032018-01-19 23:23:40345
Steve Antonda6c0952017-10-23 18:41:54346} // namespace webrtc