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