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