wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 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. |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 11 | #include <memory> |
| 12 | |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame^] | 13 | #include "webrtc/api/audio_codecs/builtin_audio_decoder_factory.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 14 | #include "webrtc/base/gunit.h" |
| 15 | #include "webrtc/base/logging.h" |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame^] | 16 | #include "webrtc/base/ptr_util.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 17 | #include "webrtc/base/ssladapter.h" |
| 18 | #include "webrtc/base/sslstreamadapter.h" |
| 19 | #include "webrtc/base/stringencode.h" |
| 20 | #include "webrtc/base/stringutils.h" |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame^] | 21 | #include "webrtc/base/thread.h" |
ossu | 7bb87ee | 2017-01-23 04:56:25 -0800 | [diff] [blame] | 22 | #ifdef WEBRTC_ANDROID |
| 23 | #include "webrtc/pc/test/androidtestinitializer.h" |
| 24 | #endif |
| 25 | #include "webrtc/pc/test/peerconnectiontestwrapper.h" |
| 26 | // Notice that mockpeerconnectionobservers.h must be included after the above! |
| 27 | #include "webrtc/pc/test/mockpeerconnectionobservers.h" |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame^] | 28 | #include "webrtc/test/mock_audio_decoder.h" |
| 29 | #include "webrtc/test/mock_audio_decoder_factory.h" |
| 30 | |
| 31 | using testing::AtLeast; |
| 32 | using testing::Invoke; |
| 33 | using testing::StrictMock; |
| 34 | using testing::_; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 35 | |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 36 | using webrtc::DataChannelInterface; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 37 | using webrtc::FakeConstraints; |
| 38 | using webrtc::MediaConstraintsInterface; |
| 39 | using webrtc::MediaStreamInterface; |
| 40 | using webrtc::PeerConnectionInterface; |
| 41 | |
| 42 | namespace { |
| 43 | |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 44 | const int kMaxWait = 10000; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 45 | |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 46 | } // namespace |
| 47 | |
| 48 | class PeerConnectionEndToEndTest |
| 49 | : public sigslot::has_slots<>, |
| 50 | public testing::Test { |
| 51 | public: |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 52 | typedef std::vector<rtc::scoped_refptr<DataChannelInterface> > |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 53 | DataChannelList; |
| 54 | |
perkj | 57db652 | 2016-04-08 08:16:33 -0700 | [diff] [blame] | 55 | PeerConnectionEndToEndTest() { |
danilchap | e9021a3 | 2016-05-17 01:52:02 -0700 | [diff] [blame] | 56 | RTC_CHECK(network_thread_.Start()); |
perkj | 57db652 | 2016-04-08 08:16:33 -0700 | [diff] [blame] | 57 | RTC_CHECK(worker_thread_.Start()); |
| 58 | caller_ = new rtc::RefCountedObject<PeerConnectionTestWrapper>( |
danilchap | e9021a3 | 2016-05-17 01:52:02 -0700 | [diff] [blame] | 59 | "caller", &network_thread_, &worker_thread_); |
perkj | 57db652 | 2016-04-08 08:16:33 -0700 | [diff] [blame] | 60 | callee_ = new rtc::RefCountedObject<PeerConnectionTestWrapper>( |
danilchap | e9021a3 | 2016-05-17 01:52:02 -0700 | [diff] [blame] | 61 | "callee", &network_thread_, &worker_thread_); |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 62 | webrtc::PeerConnectionInterface::IceServer ice_server; |
| 63 | ice_server.uri = "stun:stun.l.google.com:19302"; |
| 64 | config_.servers.push_back(ice_server); |
| 65 | |
phoglund | 37ebcf0 | 2016-01-08 05:04:57 -0800 | [diff] [blame] | 66 | #ifdef WEBRTC_ANDROID |
| 67 | webrtc::InitializeAndroidObjects(); |
| 68 | #endif |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 69 | } |
| 70 | |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame^] | 71 | void CreatePcs( |
| 72 | const MediaConstraintsInterface* pc_constraints, |
| 73 | rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory, |
| 74 | rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory) { |
| 75 | EXPECT_TRUE(caller_->CreatePc( |
| 76 | pc_constraints, config_, audio_encoder_factory, audio_decoder_factory)); |
| 77 | EXPECT_TRUE(callee_->CreatePc( |
| 78 | pc_constraints, config_, audio_encoder_factory, audio_decoder_factory)); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 79 | PeerConnectionTestWrapper::Connect(caller_.get(), callee_.get()); |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 80 | |
| 81 | caller_->SignalOnDataChannel.connect( |
| 82 | this, &PeerConnectionEndToEndTest::OnCallerAddedDataChanel); |
| 83 | callee_->SignalOnDataChannel.connect( |
| 84 | this, &PeerConnectionEndToEndTest::OnCalleeAddedDataChannel); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void GetAndAddUserMedia() { |
| 88 | FakeConstraints audio_constraints; |
| 89 | FakeConstraints video_constraints; |
| 90 | GetAndAddUserMedia(true, audio_constraints, true, video_constraints); |
| 91 | } |
| 92 | |
| 93 | void GetAndAddUserMedia(bool audio, FakeConstraints audio_constraints, |
| 94 | bool video, FakeConstraints video_constraints) { |
| 95 | caller_->GetAndAddUserMedia(audio, audio_constraints, |
| 96 | video, video_constraints); |
| 97 | callee_->GetAndAddUserMedia(audio, audio_constraints, |
| 98 | video, video_constraints); |
| 99 | } |
| 100 | |
| 101 | void Negotiate() { |
| 102 | caller_->CreateOffer(NULL); |
| 103 | } |
| 104 | |
| 105 | void WaitForCallEstablished() { |
| 106 | caller_->WaitForCallEstablished(); |
| 107 | callee_->WaitForCallEstablished(); |
| 108 | } |
| 109 | |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 110 | void WaitForConnection() { |
| 111 | caller_->WaitForConnection(); |
| 112 | callee_->WaitForConnection(); |
| 113 | } |
| 114 | |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 115 | void OnCallerAddedDataChanel(DataChannelInterface* dc) { |
| 116 | caller_signaled_data_channels_.push_back(dc); |
| 117 | } |
| 118 | |
| 119 | void OnCalleeAddedDataChannel(DataChannelInterface* dc) { |
| 120 | callee_signaled_data_channels_.push_back(dc); |
| 121 | } |
| 122 | |
| 123 | // Tests that |dc1| and |dc2| can send to and receive from each other. |
| 124 | void TestDataChannelSendAndReceive( |
| 125 | DataChannelInterface* dc1, DataChannelInterface* dc2) { |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 126 | std::unique_ptr<webrtc::MockDataChannelObserver> dc1_observer( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 127 | new webrtc::MockDataChannelObserver(dc1)); |
| 128 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 129 | std::unique_ptr<webrtc::MockDataChannelObserver> dc2_observer( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 130 | new webrtc::MockDataChannelObserver(dc2)); |
| 131 | |
| 132 | static const std::string kDummyData = "abcdefg"; |
| 133 | webrtc::DataBuffer buffer(kDummyData); |
| 134 | EXPECT_TRUE(dc1->Send(buffer)); |
| 135 | EXPECT_EQ_WAIT(kDummyData, dc2_observer->last_message(), kMaxWait); |
| 136 | |
| 137 | EXPECT_TRUE(dc2->Send(buffer)); |
| 138 | EXPECT_EQ_WAIT(kDummyData, dc1_observer->last_message(), kMaxWait); |
| 139 | |
| 140 | EXPECT_EQ(1U, dc1_observer->received_message_count()); |
| 141 | EXPECT_EQ(1U, dc2_observer->received_message_count()); |
| 142 | } |
| 143 | |
| 144 | void WaitForDataChannelsToOpen(DataChannelInterface* local_dc, |
| 145 | const DataChannelList& remote_dc_list, |
| 146 | size_t remote_dc_index) { |
| 147 | EXPECT_EQ_WAIT(DataChannelInterface::kOpen, local_dc->state(), kMaxWait); |
| 148 | |
| 149 | EXPECT_TRUE_WAIT(remote_dc_list.size() > remote_dc_index, kMaxWait); |
| 150 | EXPECT_EQ_WAIT(DataChannelInterface::kOpen, |
| 151 | remote_dc_list[remote_dc_index]->state(), |
| 152 | kMaxWait); |
| 153 | EXPECT_EQ(local_dc->id(), remote_dc_list[remote_dc_index]->id()); |
| 154 | } |
| 155 | |
| 156 | void CloseDataChannels(DataChannelInterface* local_dc, |
| 157 | const DataChannelList& remote_dc_list, |
| 158 | size_t remote_dc_index) { |
| 159 | local_dc->Close(); |
| 160 | EXPECT_EQ_WAIT(DataChannelInterface::kClosed, local_dc->state(), kMaxWait); |
| 161 | EXPECT_EQ_WAIT(DataChannelInterface::kClosed, |
| 162 | remote_dc_list[remote_dc_index]->state(), |
| 163 | kMaxWait); |
| 164 | } |
| 165 | |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 166 | protected: |
danilchap | e9021a3 | 2016-05-17 01:52:02 -0700 | [diff] [blame] | 167 | rtc::Thread network_thread_; |
perkj | 57db652 | 2016-04-08 08:16:33 -0700 | [diff] [blame] | 168 | rtc::Thread worker_thread_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 169 | rtc::scoped_refptr<PeerConnectionTestWrapper> caller_; |
| 170 | rtc::scoped_refptr<PeerConnectionTestWrapper> callee_; |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 171 | DataChannelList caller_signaled_data_channels_; |
| 172 | DataChannelList callee_signaled_data_channels_; |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 173 | webrtc::PeerConnectionInterface::RTCConfiguration config_; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 174 | }; |
| 175 | |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame^] | 176 | std::unique_ptr<webrtc::AudioDecoder> CreateForwardingMockDecoder( |
| 177 | std::unique_ptr<webrtc::AudioDecoder> real_decoder) { |
| 178 | class ForwardingMockDecoder : public StrictMock<webrtc::MockAudioDecoder> { |
| 179 | public: |
| 180 | ForwardingMockDecoder(std::unique_ptr<AudioDecoder> decoder) |
| 181 | : decoder_(std::move(decoder)) {} |
| 182 | |
| 183 | private: |
| 184 | std::unique_ptr<AudioDecoder> decoder_; |
| 185 | }; |
| 186 | |
| 187 | const auto dec = real_decoder.get(); // For lambda capturing. |
| 188 | auto mock_decoder = |
| 189 | rtc::MakeUnique<ForwardingMockDecoder>(std::move(real_decoder)); |
| 190 | EXPECT_CALL(*mock_decoder, Channels()) |
| 191 | .Times(AtLeast(1)) |
| 192 | .WillRepeatedly(Invoke([dec] { return dec->Channels(); })); |
| 193 | EXPECT_CALL(*mock_decoder, DecodeInternal(_, _, _, _, _)) |
| 194 | .Times(AtLeast(1)) |
| 195 | .WillRepeatedly( |
| 196 | Invoke([dec](const uint8_t* encoded, size_t encoded_len, |
| 197 | int sample_rate_hz, int16_t* decoded, |
| 198 | webrtc::AudioDecoder::SpeechType* speech_type) { |
| 199 | return dec->Decode(encoded, encoded_len, sample_rate_hz, |
| 200 | std::numeric_limits<size_t>::max(), decoded, |
| 201 | speech_type); |
| 202 | })); |
| 203 | EXPECT_CALL(*mock_decoder, Die()); |
| 204 | EXPECT_CALL(*mock_decoder, HasDecodePlc()).WillRepeatedly(Invoke([dec] { |
| 205 | return dec->HasDecodePlc(); |
| 206 | })); |
| 207 | EXPECT_CALL(*mock_decoder, IncomingPacket(_, _, _, _, _)) |
| 208 | .Times(AtLeast(1)) |
| 209 | .WillRepeatedly(Invoke([dec](const uint8_t* payload, size_t payload_len, |
| 210 | uint16_t rtp_sequence_number, |
| 211 | uint32_t rtp_timestamp, |
| 212 | uint32_t arrival_timestamp) { |
| 213 | return dec->IncomingPacket(payload, payload_len, rtp_sequence_number, |
| 214 | rtp_timestamp, arrival_timestamp); |
| 215 | })); |
| 216 | EXPECT_CALL(*mock_decoder, PacketDuration(_, _)) |
| 217 | .Times(AtLeast(1)) |
| 218 | .WillRepeatedly(Invoke([dec](const uint8_t* encoded, size_t encoded_len) { |
| 219 | return dec->PacketDuration(encoded, encoded_len); |
| 220 | })); |
| 221 | EXPECT_CALL(*mock_decoder, SampleRateHz()) |
| 222 | .Times(AtLeast(1)) |
| 223 | .WillRepeatedly(Invoke([dec] { return dec->SampleRateHz(); })); |
| 224 | |
| 225 | return std::move(mock_decoder); |
| 226 | } |
| 227 | |
| 228 | rtc::scoped_refptr<webrtc::AudioDecoderFactory> |
| 229 | CreateForwardingMockDecoderFactory( |
| 230 | webrtc::AudioDecoderFactory* real_decoder_factory) { |
| 231 | rtc::scoped_refptr<webrtc::MockAudioDecoderFactory> mock_decoder_factory = |
| 232 | new rtc::RefCountedObject<StrictMock<webrtc::MockAudioDecoderFactory>>; |
| 233 | EXPECT_CALL(*mock_decoder_factory, GetSupportedDecoders()) |
| 234 | .Times(AtLeast(1)) |
| 235 | .WillRepeatedly(Invoke([real_decoder_factory] { |
| 236 | return real_decoder_factory->GetSupportedDecoders(); |
| 237 | })); |
| 238 | EXPECT_CALL(*mock_decoder_factory, IsSupportedDecoder(_)) |
| 239 | .Times(AtLeast(1)) |
| 240 | .WillRepeatedly( |
| 241 | Invoke([real_decoder_factory](const webrtc::SdpAudioFormat& format) { |
| 242 | return real_decoder_factory->IsSupportedDecoder(format); |
| 243 | })); |
| 244 | EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _)) |
| 245 | .Times(AtLeast(2)) |
| 246 | .WillRepeatedly( |
| 247 | Invoke([real_decoder_factory]( |
| 248 | const webrtc::SdpAudioFormat& format, |
| 249 | std::unique_ptr<webrtc::AudioDecoder>* return_value) { |
| 250 | auto real_decoder = real_decoder_factory->MakeAudioDecoder(format); |
| 251 | *return_value = |
| 252 | real_decoder |
| 253 | ? CreateForwardingMockDecoder(std::move(real_decoder)) |
| 254 | : nullptr; |
| 255 | })); |
| 256 | return mock_decoder_factory; |
| 257 | } |
| 258 | |
kjellander@webrtc.org | 70c0e29 | 2015-11-30 21:45:35 +0100 | [diff] [blame] | 259 | // Disabled for TSan v2, see |
| 260 | // https://bugs.chromium.org/p/webrtc/issues/detail?id=4719 for details. |
kjellander@webrtc.org | 3c28d0d | 2015-12-02 22:53:26 +0100 | [diff] [blame] | 261 | // Disabled for Mac, see |
| 262 | // https://bugs.chromium.org/p/webrtc/issues/detail?id=5231 for details. |
| 263 | #if !defined(THREAD_SANITIZER) && !defined(WEBRTC_MAC) |
deadbeef | ee8c6d3 | 2015-08-13 14:27:18 -0700 | [diff] [blame] | 264 | TEST_F(PeerConnectionEndToEndTest, Call) { |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame^] | 265 | rtc::scoped_refptr<webrtc::AudioDecoderFactory> real_decoder_factory = |
| 266 | webrtc::CreateBuiltinAudioDecoderFactory(); |
| 267 | CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(), |
| 268 | CreateForwardingMockDecoderFactory(real_decoder_factory.get())); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 269 | GetAndAddUserMedia(); |
| 270 | Negotiate(); |
| 271 | WaitForCallEstablished(); |
| 272 | } |
kjellander@webrtc.org | 3c28d0d | 2015-12-02 22:53:26 +0100 | [diff] [blame] | 273 | #endif // if !defined(THREAD_SANITIZER) && !defined(WEBRTC_MAC) |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 274 | |
philipel | 7703b27 | 2016-11-28 16:23:12 +0100 | [diff] [blame] | 275 | #if !defined(ADDRESS_SANITIZER) |
deadbeef | c9be007 | 2015-12-14 18:27:57 -0800 | [diff] [blame] | 276 | TEST_F(PeerConnectionEndToEndTest, CallWithLegacySdp) { |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 277 | FakeConstraints pc_constraints; |
| 278 | pc_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp, |
| 279 | false); |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame^] | 280 | CreatePcs(&pc_constraints, webrtc::CreateBuiltinAudioEncoderFactory(), |
| 281 | webrtc::CreateBuiltinAudioDecoderFactory()); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 282 | GetAndAddUserMedia(); |
| 283 | Negotiate(); |
| 284 | WaitForCallEstablished(); |
| 285 | } |
philipel | 7703b27 | 2016-11-28 16:23:12 +0100 | [diff] [blame] | 286 | #endif // !defined(ADDRESS_SANITIZER) |
wu@webrtc.org | b43202d | 2013-11-22 19:14:25 +0000 | [diff] [blame] | 287 | |
deadbeef | 40610e2 | 2016-12-22 10:53:38 -0800 | [diff] [blame] | 288 | #ifdef HAVE_SCTP |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 289 | // Verifies that a DataChannel created before the negotiation can transition to |
| 290 | // "OPEN" and transfer data. |
| 291 | TEST_F(PeerConnectionEndToEndTest, CreateDataChannelBeforeNegotiate) { |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame^] | 292 | CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(), |
| 293 | webrtc::CreateBuiltinAudioDecoderFactory()); |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 294 | |
| 295 | webrtc::DataChannelInit init; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 296 | rtc::scoped_refptr<DataChannelInterface> caller_dc( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 297 | caller_->CreateDataChannel("data", init)); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 298 | rtc::scoped_refptr<DataChannelInterface> callee_dc( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 299 | callee_->CreateDataChannel("data", init)); |
| 300 | |
| 301 | Negotiate(); |
| 302 | WaitForConnection(); |
| 303 | |
| 304 | WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0); |
| 305 | WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0); |
| 306 | |
| 307 | TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[0]); |
| 308 | TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0]); |
| 309 | |
| 310 | CloseDataChannels(caller_dc, callee_signaled_data_channels_, 0); |
| 311 | CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0); |
| 312 | } |
| 313 | |
| 314 | // Verifies that a DataChannel created after the negotiation can transition to |
| 315 | // "OPEN" and transfer data. |
Taylor Brandstetter | bf2f569 | 2016-06-29 11:22:47 -0700 | [diff] [blame] | 316 | TEST_F(PeerConnectionEndToEndTest, CreateDataChannelAfterNegotiate) { |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame^] | 317 | CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(), |
| 318 | webrtc::CreateBuiltinAudioDecoderFactory()); |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 319 | |
| 320 | webrtc::DataChannelInit init; |
| 321 | |
| 322 | // This DataChannel is for creating the data content in the negotiation. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 323 | rtc::scoped_refptr<DataChannelInterface> dummy( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 324 | caller_->CreateDataChannel("data", init)); |
| 325 | Negotiate(); |
| 326 | WaitForConnection(); |
| 327 | |
Taylor Brandstetter | bf2f569 | 2016-06-29 11:22:47 -0700 | [diff] [blame] | 328 | // Wait for the data channel created pre-negotiation to be opened. |
| 329 | WaitForDataChannelsToOpen(dummy, callee_signaled_data_channels_, 0); |
| 330 | |
| 331 | // Create new DataChannels after the negotiation and verify their states. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 332 | rtc::scoped_refptr<DataChannelInterface> caller_dc( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 333 | caller_->CreateDataChannel("hello", init)); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 334 | rtc::scoped_refptr<DataChannelInterface> callee_dc( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 335 | callee_->CreateDataChannel("hello", init)); |
| 336 | |
| 337 | WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1); |
| 338 | WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0); |
| 339 | |
| 340 | TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]); |
| 341 | TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0]); |
| 342 | |
| 343 | CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1); |
| 344 | CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0); |
| 345 | } |
| 346 | |
| 347 | // Verifies that DataChannel IDs are even/odd based on the DTLS roles. |
| 348 | TEST_F(PeerConnectionEndToEndTest, DataChannelIdAssignment) { |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame^] | 349 | CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(), |
| 350 | webrtc::CreateBuiltinAudioDecoderFactory()); |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 351 | |
| 352 | webrtc::DataChannelInit init; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 353 | rtc::scoped_refptr<DataChannelInterface> caller_dc_1( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 354 | caller_->CreateDataChannel("data", init)); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 355 | rtc::scoped_refptr<DataChannelInterface> callee_dc_1( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 356 | callee_->CreateDataChannel("data", init)); |
| 357 | |
| 358 | Negotiate(); |
| 359 | WaitForConnection(); |
| 360 | |
| 361 | EXPECT_EQ(1U, caller_dc_1->id() % 2); |
| 362 | EXPECT_EQ(0U, callee_dc_1->id() % 2); |
| 363 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 364 | rtc::scoped_refptr<DataChannelInterface> caller_dc_2( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 365 | caller_->CreateDataChannel("data", init)); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 366 | rtc::scoped_refptr<DataChannelInterface> callee_dc_2( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 367 | callee_->CreateDataChannel("data", init)); |
| 368 | |
| 369 | EXPECT_EQ(1U, caller_dc_2->id() % 2); |
| 370 | EXPECT_EQ(0U, callee_dc_2->id() % 2); |
| 371 | } |
| 372 | |
| 373 | // Verifies that the message is received by the right remote DataChannel when |
| 374 | // there are multiple DataChannels. |
| 375 | TEST_F(PeerConnectionEndToEndTest, |
| 376 | MessageTransferBetweenTwoPairsOfDataChannels) { |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame^] | 377 | CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(), |
| 378 | webrtc::CreateBuiltinAudioDecoderFactory()); |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 379 | |
| 380 | webrtc::DataChannelInit init; |
| 381 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 382 | rtc::scoped_refptr<DataChannelInterface> caller_dc_1( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 383 | caller_->CreateDataChannel("data", init)); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 384 | rtc::scoped_refptr<DataChannelInterface> caller_dc_2( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 385 | caller_->CreateDataChannel("data", init)); |
| 386 | |
| 387 | Negotiate(); |
| 388 | WaitForConnection(); |
| 389 | WaitForDataChannelsToOpen(caller_dc_1, callee_signaled_data_channels_, 0); |
| 390 | WaitForDataChannelsToOpen(caller_dc_2, callee_signaled_data_channels_, 1); |
| 391 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 392 | std::unique_ptr<webrtc::MockDataChannelObserver> dc_1_observer( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 393 | new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[0])); |
| 394 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 395 | std::unique_ptr<webrtc::MockDataChannelObserver> dc_2_observer( |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 396 | new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[1])); |
| 397 | |
| 398 | const std::string message_1 = "hello 1"; |
| 399 | const std::string message_2 = "hello 2"; |
| 400 | |
| 401 | caller_dc_1->Send(webrtc::DataBuffer(message_1)); |
| 402 | EXPECT_EQ_WAIT(message_1, dc_1_observer->last_message(), kMaxWait); |
| 403 | |
| 404 | caller_dc_2->Send(webrtc::DataBuffer(message_2)); |
| 405 | EXPECT_EQ_WAIT(message_2, dc_2_observer->last_message(), kMaxWait); |
| 406 | |
| 407 | EXPECT_EQ(1U, dc_1_observer->received_message_count()); |
| 408 | EXPECT_EQ(1U, dc_2_observer->received_message_count()); |
| 409 | } |
deadbeef | 40610e2 | 2016-12-22 10:53:38 -0800 | [diff] [blame] | 410 | #endif // HAVE_SCTP |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 411 | |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 412 | #ifdef HAVE_QUIC |
| 413 | // Test that QUIC data channels can be used and that messages go to the correct |
| 414 | // remote data channel when both peers want to use QUIC. It is assumed that the |
| 415 | // application has externally negotiated the data channel parameters. |
| 416 | TEST_F(PeerConnectionEndToEndTest, MessageTransferBetweenQuicDataChannels) { |
| 417 | config_.enable_quic = true; |
| 418 | CreatePcs(); |
| 419 | |
| 420 | webrtc::DataChannelInit init_1; |
| 421 | init_1.id = 0; |
| 422 | init_1.ordered = false; |
| 423 | init_1.reliable = true; |
| 424 | |
| 425 | webrtc::DataChannelInit init_2; |
| 426 | init_2.id = 1; |
| 427 | init_2.ordered = false; |
| 428 | init_2.reliable = true; |
| 429 | |
| 430 | rtc::scoped_refptr<DataChannelInterface> caller_dc_1( |
| 431 | caller_->CreateDataChannel("data", init_1)); |
| 432 | ASSERT_NE(nullptr, caller_dc_1); |
| 433 | rtc::scoped_refptr<DataChannelInterface> caller_dc_2( |
| 434 | caller_->CreateDataChannel("data", init_2)); |
| 435 | ASSERT_NE(nullptr, caller_dc_2); |
| 436 | rtc::scoped_refptr<DataChannelInterface> callee_dc_1( |
| 437 | callee_->CreateDataChannel("data", init_1)); |
| 438 | ASSERT_NE(nullptr, callee_dc_1); |
| 439 | rtc::scoped_refptr<DataChannelInterface> callee_dc_2( |
| 440 | callee_->CreateDataChannel("data", init_2)); |
| 441 | ASSERT_NE(nullptr, callee_dc_2); |
| 442 | |
| 443 | Negotiate(); |
| 444 | WaitForConnection(); |
| 445 | EXPECT_TRUE_WAIT(caller_dc_1->state() == webrtc::DataChannelInterface::kOpen, |
| 446 | kMaxWait); |
| 447 | EXPECT_TRUE_WAIT(callee_dc_1->state() == webrtc::DataChannelInterface::kOpen, |
| 448 | kMaxWait); |
| 449 | EXPECT_TRUE_WAIT(caller_dc_2->state() == webrtc::DataChannelInterface::kOpen, |
| 450 | kMaxWait); |
| 451 | EXPECT_TRUE_WAIT(callee_dc_2->state() == webrtc::DataChannelInterface::kOpen, |
| 452 | kMaxWait); |
| 453 | |
| 454 | std::unique_ptr<webrtc::MockDataChannelObserver> dc_1_observer( |
| 455 | new webrtc::MockDataChannelObserver(callee_dc_1.get())); |
| 456 | |
| 457 | std::unique_ptr<webrtc::MockDataChannelObserver> dc_2_observer( |
| 458 | new webrtc::MockDataChannelObserver(callee_dc_2.get())); |
| 459 | |
| 460 | const std::string message_1 = "hello 1"; |
| 461 | const std::string message_2 = "hello 2"; |
| 462 | |
| 463 | // Send data from caller to callee. |
| 464 | caller_dc_1->Send(webrtc::DataBuffer(message_1)); |
| 465 | EXPECT_EQ_WAIT(message_1, dc_1_observer->last_message(), kMaxWait); |
| 466 | |
| 467 | caller_dc_2->Send(webrtc::DataBuffer(message_2)); |
| 468 | EXPECT_EQ_WAIT(message_2, dc_2_observer->last_message(), kMaxWait); |
| 469 | |
| 470 | EXPECT_EQ(1U, dc_1_observer->received_message_count()); |
| 471 | EXPECT_EQ(1U, dc_2_observer->received_message_count()); |
| 472 | |
| 473 | // Send data from callee to caller. |
| 474 | dc_1_observer.reset(new webrtc::MockDataChannelObserver(caller_dc_1.get())); |
| 475 | dc_2_observer.reset(new webrtc::MockDataChannelObserver(caller_dc_2.get())); |
| 476 | |
| 477 | callee_dc_1->Send(webrtc::DataBuffer(message_1)); |
| 478 | EXPECT_EQ_WAIT(message_1, dc_1_observer->last_message(), kMaxWait); |
| 479 | |
| 480 | callee_dc_2->Send(webrtc::DataBuffer(message_2)); |
| 481 | EXPECT_EQ_WAIT(message_2, dc_2_observer->last_message(), kMaxWait); |
| 482 | |
| 483 | EXPECT_EQ(1U, dc_1_observer->received_message_count()); |
| 484 | EXPECT_EQ(1U, dc_2_observer->received_message_count()); |
| 485 | } |
| 486 | #endif // HAVE_QUIC |
| 487 | |
deadbeef | 40610e2 | 2016-12-22 10:53:38 -0800 | [diff] [blame] | 488 | #ifdef HAVE_SCTP |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 489 | // Verifies that a DataChannel added from an OPEN message functions after |
| 490 | // a channel has been previously closed (webrtc issue 3778). |
| 491 | // This previously failed because the new channel re-uses the ID of the closed |
| 492 | // channel, and the closed channel was incorrectly still assigned to the id. |
| 493 | // TODO(deadbeef): This is disabled because there's currently a race condition |
| 494 | // caused by the fact that a data channel signals that it's closed before it |
| 495 | // really is. Re-enable this test once that's fixed. |
deadbeef | e2213ce | 2016-11-03 16:01:57 -0700 | [diff] [blame] | 496 | // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=4453 |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 497 | TEST_F(PeerConnectionEndToEndTest, |
| 498 | DISABLED_DataChannelFromOpenWorksAfterClose) { |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame^] | 499 | CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(), |
| 500 | webrtc::CreateBuiltinAudioDecoderFactory()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 501 | |
| 502 | webrtc::DataChannelInit init; |
| 503 | rtc::scoped_refptr<DataChannelInterface> caller_dc( |
| 504 | caller_->CreateDataChannel("data", init)); |
| 505 | |
| 506 | Negotiate(); |
| 507 | WaitForConnection(); |
| 508 | |
| 509 | WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0); |
| 510 | CloseDataChannels(caller_dc, callee_signaled_data_channels_, 0); |
| 511 | |
| 512 | // Create a new channel and ensure it works after closing the previous one. |
| 513 | caller_dc = caller_->CreateDataChannel("data2", init); |
| 514 | |
| 515 | WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1); |
| 516 | TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]); |
| 517 | |
| 518 | CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1); |
| 519 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 520 | |
| 521 | // This tests that if a data channel is closed remotely while not referenced |
| 522 | // by the application (meaning only the PeerConnection contributes to its |
| 523 | // reference count), no memory access violation will occur. |
| 524 | // See: https://code.google.com/p/chromium/issues/detail?id=565048 |
| 525 | TEST_F(PeerConnectionEndToEndTest, CloseDataChannelRemotelyWhileNotReferenced) { |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame^] | 526 | CreatePcs(nullptr, webrtc::CreateBuiltinAudioEncoderFactory(), |
| 527 | webrtc::CreateBuiltinAudioDecoderFactory()); |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 528 | |
| 529 | webrtc::DataChannelInit init; |
| 530 | rtc::scoped_refptr<DataChannelInterface> caller_dc( |
| 531 | caller_->CreateDataChannel("data", init)); |
| 532 | |
| 533 | Negotiate(); |
| 534 | WaitForConnection(); |
| 535 | |
| 536 | WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0); |
| 537 | // This removes the reference to the remote data channel that we hold. |
| 538 | callee_signaled_data_channels_.clear(); |
| 539 | caller_dc->Close(); |
| 540 | EXPECT_EQ_WAIT(DataChannelInterface::kClosed, caller_dc->state(), kMaxWait); |
| 541 | |
| 542 | // Wait for a bit longer so the remote data channel will receive the |
| 543 | // close message and be destroyed. |
| 544 | rtc::Thread::Current()->ProcessMessages(100); |
| 545 | } |
deadbeef | 40610e2 | 2016-12-22 10:53:38 -0800 | [diff] [blame] | 546 | #endif // HAVE_SCTP |