Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 1 | /* |
| 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 Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 11 | #include <memory> |
| 12 | #include <string> |
| 13 | #include <type_traits> |
| 14 | #include <utility> |
| 15 | #include <vector> |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 16 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 17 | #include "absl/types/optional.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 18 | #include "api/call/call_factory_interface.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 19 | #include "api/jsep.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "api/media_types.h" |
| 21 | #include "api/peer_connection_interface.h" |
| 22 | #include "api/peer_connection_proxy.h" |
Mirko Bonadei | d970807 | 2019-01-25 20:26:48 +0100 | [diff] [blame] | 23 | #include "api/scoped_refptr.h" |
Danil Chapovalov | 53d45ba | 2019-07-03 14:56:33 +0200 | [diff] [blame] | 24 | #include "api/task_queue/default_task_queue_factory.h" |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 25 | #include "api/test/fake_media_transport.h" |
Niels Möller | 65f17ca | 2019-09-12 13:59:36 +0200 | [diff] [blame] | 26 | #include "api/transport/media/media_transport_interface.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 27 | #include "media/base/codec.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 28 | #include "media/base/fake_media_engine.h" |
| 29 | #include "media/base/media_constants.h" |
| 30 | #include "media/base/media_engine.h" |
| 31 | #include "media/sctp/sctp_transport_internal.h" |
| 32 | #include "p2p/base/p2p_constants.h" |
| 33 | #include "p2p/base/port_allocator.h" |
| 34 | #include "pc/media_session.h" |
| 35 | #include "pc/peer_connection.h" |
| 36 | #include "pc/peer_connection_factory.h" |
| 37 | #include "pc/peer_connection_wrapper.h" |
| 38 | #include "pc/sdp_utils.h" |
| 39 | #include "pc/session_description.h" |
| 40 | #include "pc/test/mock_peer_connection_observers.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 41 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 42 | #include "rtc_base/ref_counted_object.h" |
| 43 | #include "rtc_base/rtc_certificate_generator.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 44 | #include "rtc_base/thread.h" |
Harald Alvestrand | 4aa1192 | 2019-05-14 22:00:01 +0200 | [diff] [blame] | 45 | #include "test/gmock.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 46 | #include "test/gtest.h" |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 47 | #ifdef WEBRTC_ANDROID |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 48 | #include "pc/test/android_test_initializer.h" |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 49 | #endif |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 50 | #include "pc/test/fake_sctp_transport.h" |
| 51 | #include "rtc_base/virtual_socket_server.h" |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 52 | |
| 53 | namespace webrtc { |
| 54 | |
| 55 | using RTCConfiguration = PeerConnectionInterface::RTCConfiguration; |
| 56 | using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions; |
Harald Alvestrand | 4aa1192 | 2019-05-14 22:00:01 +0200 | [diff] [blame] | 57 | using ::testing::HasSubstr; |
| 58 | using ::testing::Not; |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 59 | using ::testing::Values; |
| 60 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 61 | namespace { |
| 62 | |
| 63 | PeerConnectionFactoryDependencies CreatePeerConnectionFactoryDependencies( |
| 64 | rtc::Thread* network_thread, |
| 65 | rtc::Thread* worker_thread, |
| 66 | rtc::Thread* signaling_thread, |
| 67 | std::unique_ptr<cricket::MediaEngineInterface> media_engine, |
| 68 | std::unique_ptr<CallFactoryInterface> call_factory, |
| 69 | std::unique_ptr<MediaTransportFactory> media_transport_factory) { |
| 70 | PeerConnectionFactoryDependencies deps; |
| 71 | deps.network_thread = network_thread; |
| 72 | deps.worker_thread = worker_thread; |
| 73 | deps.signaling_thread = signaling_thread; |
Danil Chapovalov | 53d45ba | 2019-07-03 14:56:33 +0200 | [diff] [blame] | 74 | deps.task_queue_factory = CreateDefaultTaskQueueFactory(); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 75 | deps.media_engine = std::move(media_engine); |
| 76 | deps.call_factory = std::move(call_factory); |
| 77 | deps.media_transport_factory = std::move(media_transport_factory); |
| 78 | return deps; |
| 79 | } |
| 80 | |
| 81 | } // namespace |
| 82 | |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 83 | class PeerConnectionFactoryForDataChannelTest |
| 84 | : public rtc::RefCountedObject<PeerConnectionFactory> { |
| 85 | public: |
| 86 | PeerConnectionFactoryForDataChannelTest() |
| 87 | : rtc::RefCountedObject<PeerConnectionFactory>( |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 88 | CreatePeerConnectionFactoryDependencies( |
| 89 | rtc::Thread::Current(), |
| 90 | rtc::Thread::Current(), |
| 91 | rtc::Thread::Current(), |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 92 | std::make_unique<cricket::FakeMediaEngine>(), |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 93 | CreateCallFactory(), |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 94 | std::make_unique<FakeMediaTransportFactory>())) {} |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 95 | |
| 96 | std::unique_ptr<cricket::SctpTransportInternalFactory> |
| 97 | CreateSctpTransportInternalFactory() { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 98 | auto factory = std::make_unique<FakeSctpTransportFactory>(); |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 99 | last_fake_sctp_transport_factory_ = factory.get(); |
| 100 | return factory; |
| 101 | } |
| 102 | |
| 103 | FakeSctpTransportFactory* last_fake_sctp_transport_factory_ = nullptr; |
| 104 | }; |
| 105 | |
| 106 | class PeerConnectionWrapperForDataChannelTest : public PeerConnectionWrapper { |
| 107 | public: |
| 108 | using PeerConnectionWrapper::PeerConnectionWrapper; |
| 109 | |
| 110 | FakeSctpTransportFactory* sctp_transport_factory() { |
| 111 | return sctp_transport_factory_; |
| 112 | } |
| 113 | |
| 114 | void set_sctp_transport_factory( |
| 115 | FakeSctpTransportFactory* sctp_transport_factory) { |
| 116 | sctp_transport_factory_ = sctp_transport_factory; |
| 117 | } |
| 118 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 119 | absl::optional<std::string> sctp_content_name() { |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 120 | return GetInternalPeerConnection()->sctp_content_name(); |
| 121 | } |
| 122 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 123 | absl::optional<std::string> sctp_transport_name() { |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 124 | return GetInternalPeerConnection()->sctp_transport_name(); |
| 125 | } |
| 126 | |
| 127 | PeerConnection* GetInternalPeerConnection() { |
Mirko Bonadei | e97de91 | 2017-12-13 11:29:34 +0100 | [diff] [blame] | 128 | auto* pci = |
| 129 | static_cast<PeerConnectionProxyWithInternal<PeerConnectionInterface>*>( |
| 130 | pc()); |
| 131 | return static_cast<PeerConnection*>(pci->internal()); |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | private: |
| 135 | FakeSctpTransportFactory* sctp_transport_factory_ = nullptr; |
| 136 | }; |
| 137 | |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 138 | class PeerConnectionDataChannelBaseTest : public ::testing::Test { |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 139 | protected: |
| 140 | typedef std::unique_ptr<PeerConnectionWrapperForDataChannelTest> WrapperPtr; |
| 141 | |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 142 | explicit PeerConnectionDataChannelBaseTest(SdpSemantics sdp_semantics) |
| 143 | : vss_(new rtc::VirtualSocketServer()), |
| 144 | main_(vss_.get()), |
| 145 | sdp_semantics_(sdp_semantics) { |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 146 | #ifdef WEBRTC_ANDROID |
| 147 | InitializeAndroidObjects(); |
| 148 | #endif |
| 149 | } |
| 150 | |
| 151 | WrapperPtr CreatePeerConnection() { |
| 152 | return CreatePeerConnection(RTCConfiguration()); |
| 153 | } |
| 154 | |
| 155 | WrapperPtr CreatePeerConnection(const RTCConfiguration& config) { |
| 156 | return CreatePeerConnection(config, |
| 157 | PeerConnectionFactoryInterface::Options()); |
| 158 | } |
| 159 | |
| 160 | WrapperPtr CreatePeerConnection( |
| 161 | const RTCConfiguration& config, |
| 162 | const PeerConnectionFactoryInterface::Options factory_options) { |
| 163 | rtc::scoped_refptr<PeerConnectionFactoryForDataChannelTest> pc_factory( |
| 164 | new PeerConnectionFactoryForDataChannelTest()); |
| 165 | pc_factory->SetOptions(factory_options); |
| 166 | RTC_CHECK(pc_factory->Initialize()); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 167 | auto observer = std::make_unique<MockPeerConnectionObserver>(); |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 168 | RTCConfiguration modified_config = config; |
| 169 | modified_config.sdp_semantics = sdp_semantics_; |
| 170 | auto pc = pc_factory->CreatePeerConnection(modified_config, nullptr, |
| 171 | nullptr, observer.get()); |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 172 | if (!pc) { |
| 173 | return nullptr; |
| 174 | } |
| 175 | |
Yves Gerey | 4e93329 | 2018-10-31 15:36:05 +0100 | [diff] [blame] | 176 | observer->SetPeerConnectionInterface(pc.get()); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 177 | auto wrapper = std::make_unique<PeerConnectionWrapperForDataChannelTest>( |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 178 | pc_factory, pc, std::move(observer)); |
| 179 | RTC_DCHECK(pc_factory->last_fake_sctp_transport_factory_); |
| 180 | wrapper->set_sctp_transport_factory( |
| 181 | pc_factory->last_fake_sctp_transport_factory_); |
| 182 | return wrapper; |
| 183 | } |
| 184 | |
| 185 | // Accepts the same arguments as CreatePeerConnection and adds a default data |
| 186 | // channel. |
| 187 | template <typename... Args> |
| 188 | WrapperPtr CreatePeerConnectionWithDataChannel(Args&&... args) { |
| 189 | auto wrapper = CreatePeerConnection(std::forward<Args>(args)...); |
| 190 | if (!wrapper) { |
| 191 | return nullptr; |
| 192 | } |
| 193 | EXPECT_TRUE(wrapper->pc()->CreateDataChannel("dc", nullptr)); |
| 194 | return wrapper; |
| 195 | } |
| 196 | |
| 197 | // Changes the SCTP data channel port on the given session description. |
| 198 | void ChangeSctpPortOnDescription(cricket::SessionDescription* desc, |
| 199 | int port) { |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 200 | auto* data_content = cricket::GetFirstDataContent(desc); |
| 201 | RTC_DCHECK(data_content); |
Harald Alvestrand | 5fc28b1 | 2019-05-13 13:36:16 +0200 | [diff] [blame] | 202 | auto* data_desc = data_content->media_description()->as_sctp(); |
| 203 | RTC_DCHECK(data_desc); |
| 204 | data_desc->set_port(port); |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | std::unique_ptr<rtc::VirtualSocketServer> vss_; |
| 208 | rtc::AutoSocketServerThread main_; |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 209 | const SdpSemantics sdp_semantics_; |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 210 | }; |
| 211 | |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 212 | class PeerConnectionDataChannelTest |
| 213 | : public PeerConnectionDataChannelBaseTest, |
| 214 | public ::testing::WithParamInterface<SdpSemantics> { |
| 215 | protected: |
| 216 | PeerConnectionDataChannelTest() |
| 217 | : PeerConnectionDataChannelBaseTest(GetParam()) {} |
| 218 | }; |
| 219 | |
| 220 | TEST_P(PeerConnectionDataChannelTest, |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 221 | NoSctpTransportCreatedIfRtpDataChannelEnabled) { |
| 222 | RTCConfiguration config; |
| 223 | config.enable_rtp_data_channel = true; |
| 224 | auto caller = CreatePeerConnectionWithDataChannel(config); |
| 225 | |
| 226 | ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer())); |
| 227 | EXPECT_FALSE(caller->sctp_transport_factory()->last_fake_sctp_transport()); |
| 228 | } |
| 229 | |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 230 | TEST_P(PeerConnectionDataChannelTest, |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 231 | RtpDataChannelCreatedEvenIfSctpAvailable) { |
| 232 | RTCConfiguration config; |
| 233 | config.enable_rtp_data_channel = true; |
| 234 | PeerConnectionFactoryInterface::Options options; |
| 235 | options.disable_sctp_data_channels = false; |
| 236 | auto caller = CreatePeerConnectionWithDataChannel(config, options); |
| 237 | |
| 238 | ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer())); |
| 239 | EXPECT_FALSE(caller->sctp_transport_factory()->last_fake_sctp_transport()); |
| 240 | } |
| 241 | |
Bjorn A Mellem | bc3eebc | 2019-09-23 14:53:54 -0700 | [diff] [blame] | 242 | TEST_P(PeerConnectionDataChannelTest, InternalSctpTransportDeletedOnTeardown) { |
| 243 | auto caller = CreatePeerConnectionWithDataChannel(); |
| 244 | |
| 245 | ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer())); |
| 246 | EXPECT_TRUE(caller->sctp_transport_factory()->last_fake_sctp_transport()); |
| 247 | |
| 248 | rtc::scoped_refptr<SctpTransportInterface> sctp_transport = |
| 249 | caller->GetInternalPeerConnection()->GetSctpTransport(); |
| 250 | |
| 251 | caller.reset(); |
| 252 | EXPECT_EQ(static_cast<SctpTransport*>(sctp_transport.get())->internal(), |
| 253 | nullptr); |
| 254 | } |
| 255 | |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 256 | // Test that sctp_content_name/sctp_transport_name (used for stats) are correct |
| 257 | // before and after BUNDLE is negotiated. |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 258 | TEST_P(PeerConnectionDataChannelTest, SctpContentAndTransportNameSetCorrectly) { |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 259 | auto caller = CreatePeerConnection(); |
| 260 | auto callee = CreatePeerConnection(); |
| 261 | |
| 262 | // Initially these fields should be empty. |
| 263 | EXPECT_FALSE(caller->sctp_content_name()); |
| 264 | EXPECT_FALSE(caller->sctp_transport_name()); |
| 265 | |
| 266 | // Create offer with audio/video/data. |
| 267 | // Default bundle policy is "balanced", so data should be using its own |
| 268 | // transport. |
| 269 | caller->AddAudioTrack("a"); |
| 270 | caller->AddVideoTrack("v"); |
| 271 | caller->pc()->CreateDataChannel("dc", nullptr); |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 272 | |
| 273 | auto offer = caller->CreateOffer(); |
| 274 | const auto& offer_contents = offer->description()->contents(); |
| 275 | ASSERT_EQ(cricket::MEDIA_TYPE_AUDIO, |
| 276 | offer_contents[0].media_description()->type()); |
| 277 | std::string audio_mid = offer_contents[0].name; |
| 278 | ASSERT_EQ(cricket::MEDIA_TYPE_DATA, |
| 279 | offer_contents[2].media_description()->type()); |
| 280 | std::string data_mid = offer_contents[2].name; |
| 281 | |
| 282 | ASSERT_TRUE( |
| 283 | caller->SetLocalDescription(CloneSessionDescription(offer.get()))); |
| 284 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 285 | |
| 286 | ASSERT_TRUE(caller->sctp_content_name()); |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 287 | EXPECT_EQ(data_mid, *caller->sctp_content_name()); |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 288 | ASSERT_TRUE(caller->sctp_transport_name()); |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 289 | EXPECT_EQ(data_mid, *caller->sctp_transport_name()); |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 290 | |
| 291 | // Create answer that finishes BUNDLE negotiation, which means everything |
| 292 | // should be bundled on the first transport (audio). |
| 293 | RTCOfferAnswerOptions options; |
| 294 | options.use_rtp_mux = true; |
| 295 | ASSERT_TRUE( |
| 296 | caller->SetRemoteDescription(callee->CreateAnswerAndSetAsLocal())); |
| 297 | |
| 298 | ASSERT_TRUE(caller->sctp_content_name()); |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 299 | EXPECT_EQ(data_mid, *caller->sctp_content_name()); |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 300 | ASSERT_TRUE(caller->sctp_transport_name()); |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 301 | EXPECT_EQ(audio_mid, *caller->sctp_transport_name()); |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 304 | TEST_P(PeerConnectionDataChannelTest, |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 305 | CreateOfferWithNoDataChannelsGivesNoDataSection) { |
| 306 | auto caller = CreatePeerConnection(); |
| 307 | auto offer = caller->CreateOffer(); |
| 308 | |
| 309 | EXPECT_FALSE(offer->description()->GetContentByName(cricket::CN_DATA)); |
| 310 | EXPECT_FALSE(offer->description()->GetTransportInfoByName(cricket::CN_DATA)); |
| 311 | } |
| 312 | |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 313 | TEST_P(PeerConnectionDataChannelTest, |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 314 | CreateAnswerWithRemoteSctpDataChannelIncludesDataSection) { |
| 315 | auto caller = CreatePeerConnectionWithDataChannel(); |
| 316 | auto callee = CreatePeerConnection(); |
| 317 | |
| 318 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 319 | |
| 320 | auto answer = callee->CreateAnswer(); |
| 321 | ASSERT_TRUE(answer); |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 322 | auto* data_content = cricket::GetFirstDataContent(answer->description()); |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 323 | ASSERT_TRUE(data_content); |
| 324 | EXPECT_FALSE(data_content->rejected); |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 325 | EXPECT_TRUE( |
| 326 | answer->description()->GetTransportInfoByName(data_content->name)); |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 329 | TEST_P(PeerConnectionDataChannelTest, |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 330 | CreateDataChannelWithDtlsDisabledSucceeds) { |
| 331 | RTCConfiguration config; |
| 332 | config.enable_dtls_srtp.emplace(false); |
| 333 | auto caller = CreatePeerConnection(); |
| 334 | |
| 335 | EXPECT_TRUE(caller->pc()->CreateDataChannel("dc", nullptr)); |
| 336 | } |
| 337 | |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 338 | TEST_P(PeerConnectionDataChannelTest, CreateDataChannelWithSctpDisabledFails) { |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 339 | PeerConnectionFactoryInterface::Options options; |
| 340 | options.disable_sctp_data_channels = true; |
| 341 | auto caller = CreatePeerConnection(RTCConfiguration(), options); |
| 342 | |
| 343 | EXPECT_FALSE(caller->pc()->CreateDataChannel("dc", nullptr)); |
| 344 | } |
| 345 | |
| 346 | // Test that if a callee has SCTP disabled and receives an offer with an SCTP |
| 347 | // data channel, the data section is rejected and no SCTP transport is created |
| 348 | // on the callee. |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 349 | TEST_P(PeerConnectionDataChannelTest, |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 350 | DataSectionRejectedIfCalleeHasSctpDisabled) { |
| 351 | auto caller = CreatePeerConnectionWithDataChannel(); |
| 352 | PeerConnectionFactoryInterface::Options options; |
| 353 | options.disable_sctp_data_channels = true; |
| 354 | auto callee = CreatePeerConnection(RTCConfiguration(), options); |
| 355 | |
| 356 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 357 | |
| 358 | EXPECT_FALSE(callee->sctp_transport_factory()->last_fake_sctp_transport()); |
| 359 | |
| 360 | auto answer = callee->CreateAnswer(); |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 361 | auto* data_content = cricket::GetFirstDataContent(answer->description()); |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 362 | ASSERT_TRUE(data_content); |
| 363 | EXPECT_TRUE(data_content->rejected); |
| 364 | } |
| 365 | |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 366 | TEST_P(PeerConnectionDataChannelTest, SctpPortPropagatedFromSdpToTransport) { |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 367 | constexpr int kNewSendPort = 9998; |
| 368 | constexpr int kNewRecvPort = 7775; |
| 369 | |
| 370 | auto caller = CreatePeerConnectionWithDataChannel(); |
| 371 | auto callee = CreatePeerConnectionWithDataChannel(); |
| 372 | |
| 373 | auto offer = caller->CreateOffer(); |
| 374 | ChangeSctpPortOnDescription(offer->description(), kNewSendPort); |
| 375 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 376 | |
| 377 | auto answer = callee->CreateAnswer(); |
| 378 | ChangeSctpPortOnDescription(answer->description(), kNewRecvPort); |
| 379 | ASSERT_TRUE(callee->SetLocalDescription(std::move(answer))); |
| 380 | |
| 381 | auto* callee_transport = |
| 382 | callee->sctp_transport_factory()->last_fake_sctp_transport(); |
| 383 | ASSERT_TRUE(callee_transport); |
| 384 | EXPECT_EQ(kNewSendPort, callee_transport->remote_port()); |
| 385 | EXPECT_EQ(kNewRecvPort, callee_transport->local_port()); |
| 386 | } |
| 387 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 388 | TEST_P(PeerConnectionDataChannelTest, |
| 389 | NoSctpTransportCreatedIfMediaTransportDataChannelsEnabled) { |
| 390 | RTCConfiguration config; |
| 391 | config.use_media_transport_for_data_channels = true; |
| 392 | config.enable_dtls_srtp = false; // SDES is required to use media transport. |
| 393 | auto caller = CreatePeerConnectionWithDataChannel(config); |
| 394 | |
| 395 | ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer())); |
| 396 | EXPECT_FALSE(caller->sctp_transport_factory()->last_fake_sctp_transport()); |
| 397 | } |
| 398 | |
| 399 | TEST_P(PeerConnectionDataChannelTest, |
| 400 | MediaTransportDataChannelCreatedEvenIfSctpAvailable) { |
| 401 | RTCConfiguration config; |
| 402 | config.use_media_transport_for_data_channels = true; |
| 403 | config.enable_dtls_srtp = false; // SDES is required to use media transport. |
| 404 | PeerConnectionFactoryInterface::Options options; |
| 405 | options.disable_sctp_data_channels = false; |
| 406 | auto caller = CreatePeerConnectionWithDataChannel(config, options); |
| 407 | |
| 408 | ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer())); |
| 409 | EXPECT_FALSE(caller->sctp_transport_factory()->last_fake_sctp_transport()); |
| 410 | } |
| 411 | |
| 412 | TEST_P(PeerConnectionDataChannelTest, |
| 413 | CannotEnableBothMediaTransportAndRtpDataChannels) { |
| 414 | RTCConfiguration config; |
| 415 | config.enable_rtp_data_channel = true; |
| 416 | config.use_media_transport_for_data_channels = true; |
| 417 | config.enable_dtls_srtp = false; // SDES is required to use media transport. |
| 418 | EXPECT_EQ(CreatePeerConnection(config), nullptr); |
| 419 | } |
| 420 | |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 11:14:05 -0800 | [diff] [blame] | 421 | // This test now DCHECKs, instead of failing to SetLocalDescription. |
| 422 | TEST_P(PeerConnectionDataChannelTest, MediaTransportWithoutSdesFails) { |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 423 | RTCConfiguration config; |
| 424 | config.use_media_transport_for_data_channels = true; |
| 425 | config.enable_dtls_srtp = true; // Disables SDES for data sections. |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 11:14:05 -0800 | [diff] [blame] | 426 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 427 | auto caller = CreatePeerConnectionWithDataChannel(config); |
| 428 | |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 11:14:05 -0800 | [diff] [blame] | 429 | EXPECT_EQ(nullptr, caller); |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 430 | } |
| 431 | |
Guido Urdaneta | cecf87f | 2019-05-31 10:17:38 +0000 | [diff] [blame] | 432 | TEST_P(PeerConnectionDataChannelTest, ModernSdpSyntaxByDefault) { |
Harald Alvestrand | 4aa1192 | 2019-05-14 22:00:01 +0200 | [diff] [blame] | 433 | PeerConnectionInterface::RTCOfferAnswerOptions options; |
| 434 | auto caller = CreatePeerConnectionWithDataChannel(); |
| 435 | auto offer = caller->CreateOffer(options); |
| 436 | EXPECT_FALSE(cricket::GetFirstSctpDataContentDescription(offer->description()) |
| 437 | ->use_sctpmap()); |
| 438 | std::string sdp; |
| 439 | offer->ToString(&sdp); |
| 440 | RTC_LOG(LS_ERROR) << sdp; |
| 441 | EXPECT_THAT(sdp, HasSubstr(" UDP/DTLS/SCTP webrtc-datachannel")); |
| 442 | EXPECT_THAT(sdp, Not(HasSubstr("a=sctpmap:"))); |
| 443 | } |
| 444 | |
| 445 | TEST_P(PeerConnectionDataChannelTest, ObsoleteSdpSyntaxIfSet) { |
| 446 | PeerConnectionInterface::RTCOfferAnswerOptions options; |
| 447 | options.use_obsolete_sctp_sdp = true; |
| 448 | auto caller = CreatePeerConnectionWithDataChannel(); |
| 449 | auto offer = caller->CreateOffer(options); |
| 450 | EXPECT_TRUE(cricket::GetFirstSctpDataContentDescription(offer->description()) |
| 451 | ->use_sctpmap()); |
| 452 | std::string sdp; |
| 453 | offer->ToString(&sdp); |
| 454 | EXPECT_THAT(sdp, Not(HasSubstr(" UDP/DTLS/SCTP webrtc-datachannel"))); |
| 455 | EXPECT_THAT(sdp, HasSubstr("a=sctpmap:")); |
| 456 | } |
| 457 | |
Mirko Bonadei | c84f661 | 2019-01-31 12:20:57 +0100 | [diff] [blame] | 458 | INSTANTIATE_TEST_SUITE_P(PeerConnectionDataChannelTest, |
| 459 | PeerConnectionDataChannelTest, |
| 460 | Values(SdpSemantics::kPlanB, |
| 461 | SdpSemantics::kUnifiedPlan)); |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 462 | |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 463 | } // namespace webrtc |