blob: 3bb208886628633a473baf522beb964b416cd1e7 [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"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "api/media_types.h"
21#include "api/peer_connection_interface.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010022#include "api/scoped_refptr.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000023#include "api/sctp_transport_interface.h"
Danil Chapovalov53d45ba2019-07-03 14:56:33 +020024#include "api/task_queue/default_task_queue_factory.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000025#include "api/task_queue/task_queue_factory.h"
26#include "api/transport/sctp_transport_factory_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "media/base/fake_media_engine.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "media/base/media_engine.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#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"
Markus Handella1b82012021-05-26 18:56:30 +020033#include "pc/peer_connection_proxy.h"
Steve Anton10542f22019-01-11 09:11:00 -080034#include "pc/peer_connection_wrapper.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000035#include "pc/sctp_transport.h"
Steve Anton10542f22019-01-11 09:11:00 -080036#include "pc/sdp_utils.h"
37#include "pc/session_description.h"
38#include "pc/test/mock_peer_connection_observers.h"
Yves Gerey3e707812018-11-28 16:47:49 +010039#include "rtc_base/checks.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000040#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080041#include "rtc_base/rtc_certificate_generator.h"
Yves Gerey3e707812018-11-28 16:47:49 +010042#include "rtc_base/thread.h"
Harald Alvestrand4aa11922019-05-14 22:00:01 +020043#include "test/gmock.h"
Yves Gerey3e707812018-11-28 16:47:49 +010044#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
Steve Anton10542f22019-01-11 09:11:00 -080048#include "rtc_base/virtual_socket_server.h"
Per Kjellander2bca0082020-08-28 09:15:15 +020049#include "test/pc/sctp/fake_sctp_transport.h"
Steve Antonda6c0952017-10-23 11:41:54 -070050
51namespace webrtc {
52
53using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
54using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
Harald Alvestrand4aa11922019-05-14 22:00:01 +020055using ::testing::HasSubstr;
56using ::testing::Not;
Steve Antonda6c0952017-10-23 11:41:54 -070057using ::testing::Values;
58
Bjorn Mellem175aa2e2018-11-08 11:23:22 -080059namespace {
60
Per Kjellander2bca0082020-08-28 09:15:15 +020061PeerConnectionFactoryDependencies CreatePeerConnectionFactoryDependencies() {
Bjorn Mellem175aa2e2018-11-08 11:23:22 -080062 PeerConnectionFactoryDependencies deps;
Per Kjellander2bca0082020-08-28 09:15:15 +020063 deps.network_thread = rtc::Thread::Current();
64 deps.worker_thread = rtc::Thread::Current();
65 deps.signaling_thread = rtc::Thread::Current();
Danil Chapovalov53d45ba2019-07-03 14:56:33 +020066 deps.task_queue_factory = CreateDefaultTaskQueueFactory();
Per Kjellander2bca0082020-08-28 09:15:15 +020067 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 11:23:22 -080070 return deps;
71}
72
73} // namespace
74
Steve Antonda6c0952017-10-23 11:41:54 -070075class 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 12:07:33 +010088 absl::optional<std::string> sctp_mid() {
89 return GetInternalPeerConnection()->sctp_mid();
Steve Antonda6c0952017-10-23 11:41:54 -070090 }
91
Danil Chapovalov66cadcc2018-06-19 16:47:43 +020092 absl::optional<std::string> sctp_transport_name() {
Steve Antonda6c0952017-10-23 11:41:54 -070093 return GetInternalPeerConnection()->sctp_transport_name();
94 }
95
96 PeerConnection* GetInternalPeerConnection() {
Mirko Bonadeie97de912017-12-13 11:29:34 +010097 auto* pci =
98 static_cast<PeerConnectionProxyWithInternal<PeerConnectionInterface>*>(
99 pc());
100 return static_cast<PeerConnection*>(pci->internal());
Steve Antonda6c0952017-10-23 11:41:54 -0700101 }
102
103 private:
104 FakeSctpTransportFactory* sctp_transport_factory_ = nullptr;
105};
106
Steve Antondbf9d032018-01-19 15:23:40 -0800107class PeerConnectionDataChannelBaseTest : public ::testing::Test {
Steve Antonda6c0952017-10-23 11:41:54 -0700108 protected:
109 typedef std::unique_ptr<PeerConnectionWrapperForDataChannelTest> WrapperPtr;
110
Steve Antondbf9d032018-01-19 15:23:40 -0800111 explicit PeerConnectionDataChannelBaseTest(SdpSemantics sdp_semantics)
112 : vss_(new rtc::VirtualSocketServer()),
113 main_(vss_.get()),
114 sdp_semantics_(sdp_semantics) {
Steve Antonda6c0952017-10-23 11:41:54 -0700115#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 09:15:15 +0200132 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 11:41:54 -0700137 pc_factory->SetOptions(factory_options);
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200138 auto observer = std::make_unique<MockPeerConnectionObserver>();
Steve Antondbf9d032018-01-19 15:23:40 -0800139 RTCConfiguration modified_config = config;
140 modified_config.sdp_semantics = sdp_semantics_;
Florent Castelli72424402022-04-06 03:45:10 +0200141 auto result = pc_factory->CreatePeerConnectionOrError(
142 modified_config, PeerConnectionDependencies(observer.get()));
143 if (!result.ok()) {
Steve Antonda6c0952017-10-23 11:41:54 -0700144 return nullptr;
145 }
146
Niels Möllerafb246b2022-04-20 14:26:50 +0200147 observer->SetPeerConnectionInterface(result.value().get());
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200148 auto wrapper = std::make_unique<PeerConnectionWrapperForDataChannelTest>(
Florent Castelli72424402022-04-06 03:45:10 +0200149 pc_factory, result.MoveValue(), std::move(observer));
Per Kjellander2bca0082020-08-28 09:15:15 +0200150 wrapper->set_sctp_transport_factory(fake_sctp_transport_factory);
Steve Antonda6c0952017-10-23 11:41:54 -0700151 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 }
Florent Castelli72424402022-04-06 03:45:10 +0200162 EXPECT_TRUE(wrapper->pc()->CreateDataChannelOrError("dc", nullptr).ok());
Steve Antonda6c0952017-10-23 11:41:54 -0700163 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 11:41:54 -0700169 auto* data_content = cricket::GetFirstDataContent(desc);
170 RTC_DCHECK(data_content);
Harald Alvestrand5fc28b12019-05-13 13:36:16 +0200171 auto* data_desc = data_content->media_description()->as_sctp();
172 RTC_DCHECK(data_desc);
173 data_desc->set_port(port);
Steve Antonda6c0952017-10-23 11:41:54 -0700174 }
175
176 std::unique_ptr<rtc::VirtualSocketServer> vss_;
177 rtc::AutoSocketServerThread main_;
Steve Antondbf9d032018-01-19 15:23:40 -0800178 const SdpSemantics sdp_semantics_;
Steve Antonda6c0952017-10-23 11:41:54 -0700179};
180
Steve Antondbf9d032018-01-19 15:23:40 -0800181class PeerConnectionDataChannelTest
182 : public PeerConnectionDataChannelBaseTest,
183 public ::testing::WithParamInterface<SdpSemantics> {
184 protected:
185 PeerConnectionDataChannelTest()
186 : PeerConnectionDataChannelBaseTest(GetParam()) {}
187};
188
Steve Antonc8ff1602020-02-05 13:53:38 -0800189class PeerConnectionDataChannelUnifiedPlanTest
190 : public PeerConnectionDataChannelBaseTest {
191 protected:
192 PeerConnectionDataChannelUnifiedPlanTest()
193 : PeerConnectionDataChannelBaseTest(SdpSemantics::kUnifiedPlan) {}
194};
195
Bjorn A Mellembc3eebc2019-09-23 14:53:54 -0700196TEST_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 12:07:33 +0100210// Test that sctp_mid/sctp_transport_name (used for stats) are correct
Steve Antonda6c0952017-10-23 11:41:54 -0700211// before and after BUNDLE is negotiated.
Steve Antondbf9d032018-01-19 15:23:40 -0800212TEST_P(PeerConnectionDataChannelTest, SctpContentAndTransportNameSetCorrectly) {
Steve Antonda6c0952017-10-23 11:41:54 -0700213 auto caller = CreatePeerConnection();
214 auto callee = CreatePeerConnection();
215
216 // Initially these fields should be empty.
Harald Alvestrand1cb929f2020-02-05 12:07:33 +0100217 EXPECT_FALSE(caller->sctp_mid());
Steve Antonda6c0952017-10-23 11:41:54 -0700218 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");
Florent Castelli72424402022-04-06 03:45:10 +0200225 caller->pc()->CreateDataChannelOrError("dc", nullptr);
Steve Antondbf9d032018-01-19 15:23:40 -0800226
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 11:41:54 -0700239
Harald Alvestrand1cb929f2020-02-05 12:07:33 +0100240 ASSERT_TRUE(caller->sctp_mid());
241 EXPECT_EQ(data_mid, *caller->sctp_mid());
Steve Antonda6c0952017-10-23 11:41:54 -0700242 ASSERT_TRUE(caller->sctp_transport_name());
Steve Antondbf9d032018-01-19 15:23:40 -0800243 EXPECT_EQ(data_mid, *caller->sctp_transport_name());
Steve Antonda6c0952017-10-23 11:41:54 -0700244
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 12:07:33 +0100252 ASSERT_TRUE(caller->sctp_mid());
253 EXPECT_EQ(data_mid, *caller->sctp_mid());
Steve Antonda6c0952017-10-23 11:41:54 -0700254 ASSERT_TRUE(caller->sctp_transport_name());
Steve Antondbf9d032018-01-19 15:23:40 -0800255 EXPECT_EQ(audio_mid, *caller->sctp_transport_name());
Steve Antonda6c0952017-10-23 11:41:54 -0700256}
257
Steve Antondbf9d032018-01-19 15:23:40 -0800258TEST_P(PeerConnectionDataChannelTest,
Steve Antonda6c0952017-10-23 11:41:54 -0700259 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 15:23:40 -0800267TEST_P(PeerConnectionDataChannelTest,
Steve Antonda6c0952017-10-23 11:41:54 -0700268 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 15:23:40 -0800276 auto* data_content = cricket::GetFirstDataContent(answer->description());
Steve Antonda6c0952017-10-23 11:41:54 -0700277 ASSERT_TRUE(data_content);
278 EXPECT_FALSE(data_content->rejected);
Steve Antondbf9d032018-01-19 15:23:40 -0800279 EXPECT_TRUE(
280 answer->description()->GetTransportInfoByName(data_content->name));
Steve Antonda6c0952017-10-23 11:41:54 -0700281}
282
Steve Antondbf9d032018-01-19 15:23:40 -0800283TEST_P(PeerConnectionDataChannelTest, SctpPortPropagatedFromSdpToTransport) {
Steve Antonda6c0952017-10-23 11:41:54 -0700284 constexpr int kNewSendPort = 9998;
285 constexpr int kNewRecvPort = 7775;
286
287 auto caller = CreatePeerConnectionWithDataChannel();
288 auto callee = CreatePeerConnectionWithDataChannel();
289
290 auto offer = caller->CreateOffer();
291 ChangeSctpPortOnDescription(offer->description(), kNewSendPort);
292 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
293
294 auto answer = callee->CreateAnswer();
295 ChangeSctpPortOnDescription(answer->description(), kNewRecvPort);
Harald Alvestrand7af57c62021-04-16 11:12:14 +0000296 std::string sdp;
297 answer->ToString(&sdp);
Steve Antonda6c0952017-10-23 11:41:54 -0700298 ASSERT_TRUE(callee->SetLocalDescription(std::move(answer)));
Steve Antonda6c0952017-10-23 11:41:54 -0700299 auto* callee_transport =
300 callee->sctp_transport_factory()->last_fake_sctp_transport();
301 ASSERT_TRUE(callee_transport);
302 EXPECT_EQ(kNewSendPort, callee_transport->remote_port());
303 EXPECT_EQ(kNewRecvPort, callee_transport->local_port());
304}
305
Guido Urdanetacecf87f2019-05-31 10:17:38 +0000306TEST_P(PeerConnectionDataChannelTest, ModernSdpSyntaxByDefault) {
Harald Alvestrand4aa11922019-05-14 22:00:01 +0200307 PeerConnectionInterface::RTCOfferAnswerOptions options;
308 auto caller = CreatePeerConnectionWithDataChannel();
309 auto offer = caller->CreateOffer(options);
310 EXPECT_FALSE(cricket::GetFirstSctpDataContentDescription(offer->description())
311 ->use_sctpmap());
312 std::string sdp;
313 offer->ToString(&sdp);
314 RTC_LOG(LS_ERROR) << sdp;
315 EXPECT_THAT(sdp, HasSubstr(" UDP/DTLS/SCTP webrtc-datachannel"));
316 EXPECT_THAT(sdp, Not(HasSubstr("a=sctpmap:")));
317}
318
319TEST_P(PeerConnectionDataChannelTest, ObsoleteSdpSyntaxIfSet) {
320 PeerConnectionInterface::RTCOfferAnswerOptions options;
321 options.use_obsolete_sctp_sdp = true;
322 auto caller = CreatePeerConnectionWithDataChannel();
323 auto offer = caller->CreateOffer(options);
324 EXPECT_TRUE(cricket::GetFirstSctpDataContentDescription(offer->description())
325 ->use_sctpmap());
326 std::string sdp;
327 offer->ToString(&sdp);
328 EXPECT_THAT(sdp, Not(HasSubstr(" UDP/DTLS/SCTP webrtc-datachannel")));
329 EXPECT_THAT(sdp, HasSubstr("a=sctpmap:"));
330}
331
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100332INSTANTIATE_TEST_SUITE_P(PeerConnectionDataChannelTest,
333 PeerConnectionDataChannelTest,
Florent Castelli15a38de2022-04-06 00:38:21 +0200334 Values(SdpSemantics::kPlanB_DEPRECATED,
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100335 SdpSemantics::kUnifiedPlan));
Steve Antondbf9d032018-01-19 15:23:40 -0800336
Steve Antonda6c0952017-10-23 11:41:54 -0700337} // namespace webrtc