blob: 4110db07c92e18aaa1ff31e3dc67c70f38b4d952 [file] [log] [blame]
wu@webrtc.org364f2042013-11-20 21:49:41 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
wu@webrtc.org364f2042013-11-20 21:49:41 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.org364f2042013-11-20 21:49:41 +00009 */
10
kwibergd1fe2812016-04-27 06:47:29 -070011#include <memory>
12
Henrik Kjellander15583c12016-02-10 10:53:12 +010013#include "webrtc/api/test/peerconnectiontestwrapper.h"
kjellandera96e2d72016-02-04 23:52:28 -080014// Notice that mockpeerconnectionobservers.h must be included after the above!
Henrik Kjellander15583c12016-02-10 10:53:12 +010015#include "webrtc/api/test/mockpeerconnectionobservers.h"
phoglund37ebcf02016-01-08 05:04:57 -080016#ifdef WEBRTC_ANDROID
Henrik Kjellander15583c12016-02-10 10:53:12 +010017#include "webrtc/api/test/androidtestinitializer.h"
phoglund37ebcf02016-01-08 05:04:57 -080018#endif
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000019#include "webrtc/base/gunit.h"
20#include "webrtc/base/logging.h"
21#include "webrtc/base/ssladapter.h"
perkj57db6522016-04-08 08:16:33 -070022#include "webrtc/base/thread.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000023#include "webrtc/base/sslstreamadapter.h"
24#include "webrtc/base/stringencode.h"
25#include "webrtc/base/stringutils.h"
wu@webrtc.org364f2042013-11-20 21:49:41 +000026
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000027#define MAYBE_SKIP_TEST(feature) \
28 if (!(feature())) { \
29 LOG(LS_INFO) << "Feature disabled... skipping"; \
30 return; \
31 }
32
33using webrtc::DataChannelInterface;
wu@webrtc.org364f2042013-11-20 21:49:41 +000034using webrtc::FakeConstraints;
35using webrtc::MediaConstraintsInterface;
36using webrtc::MediaStreamInterface;
37using webrtc::PeerConnectionInterface;
38
39namespace {
40
Honghai Zhang82d78622016-05-06 11:29:15 -070041const int kMaxWait = 10000;
wu@webrtc.org364f2042013-11-20 21:49:41 +000042
wu@webrtc.org364f2042013-11-20 21:49:41 +000043} // namespace
44
45class PeerConnectionEndToEndTest
46 : public sigslot::has_slots<>,
47 public testing::Test {
48 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000049 typedef std::vector<rtc::scoped_refptr<DataChannelInterface> >
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000050 DataChannelList;
51
perkj57db6522016-04-08 08:16:33 -070052 PeerConnectionEndToEndTest() {
danilchape9021a32016-05-17 01:52:02 -070053 RTC_CHECK(network_thread_.Start());
perkj57db6522016-04-08 08:16:33 -070054 RTC_CHECK(worker_thread_.Start());
55 caller_ = new rtc::RefCountedObject<PeerConnectionTestWrapper>(
danilchape9021a32016-05-17 01:52:02 -070056 "caller", &network_thread_, &worker_thread_);
perkj57db6522016-04-08 08:16:33 -070057 callee_ = new rtc::RefCountedObject<PeerConnectionTestWrapper>(
danilchape9021a32016-05-17 01:52:02 -070058 "callee", &network_thread_, &worker_thread_);
zhihuang9763d562016-08-05 11:14:50 -070059 webrtc::PeerConnectionInterface::IceServer ice_server;
60 ice_server.uri = "stun:stun.l.google.com:19302";
61 config_.servers.push_back(ice_server);
62
phoglund37ebcf02016-01-08 05:04:57 -080063#ifdef WEBRTC_ANDROID
64 webrtc::InitializeAndroidObjects();
65#endif
wu@webrtc.org364f2042013-11-20 21:49:41 +000066 }
67
68 void CreatePcs() {
69 CreatePcs(NULL);
70 }
71
72 void CreatePcs(const MediaConstraintsInterface* pc_constraints) {
zhihuang9763d562016-08-05 11:14:50 -070073 EXPECT_TRUE(caller_->CreatePc(pc_constraints, config_));
74 EXPECT_TRUE(callee_->CreatePc(pc_constraints, config_));
wu@webrtc.org364f2042013-11-20 21:49:41 +000075 PeerConnectionTestWrapper::Connect(caller_.get(), callee_.get());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000076
77 caller_->SignalOnDataChannel.connect(
78 this, &PeerConnectionEndToEndTest::OnCallerAddedDataChanel);
79 callee_->SignalOnDataChannel.connect(
80 this, &PeerConnectionEndToEndTest::OnCalleeAddedDataChannel);
wu@webrtc.org364f2042013-11-20 21:49:41 +000081 }
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.org1a6c6282014-06-12 21:59:29 +0000106 void WaitForConnection() {
107 caller_->WaitForConnection();
108 callee_->WaitForConnection();
109 }
110
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000111 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) {
kwibergd1fe2812016-04-27 06:47:29 -0700122 std::unique_ptr<webrtc::MockDataChannelObserver> dc1_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000123 new webrtc::MockDataChannelObserver(dc1));
124
kwibergd1fe2812016-04-27 06:47:29 -0700125 std::unique_ptr<webrtc::MockDataChannelObserver> dc2_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000126 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.org364f2042013-11-20 21:49:41 +0000162 protected:
danilchape9021a32016-05-17 01:52:02 -0700163 rtc::Thread network_thread_;
perkj57db6522016-04-08 08:16:33 -0700164 rtc::Thread worker_thread_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000165 rtc::scoped_refptr<PeerConnectionTestWrapper> caller_;
166 rtc::scoped_refptr<PeerConnectionTestWrapper> callee_;
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000167 DataChannelList caller_signaled_data_channels_;
168 DataChannelList callee_signaled_data_channels_;
zhihuang9763d562016-08-05 11:14:50 -0700169 webrtc::PeerConnectionInterface::RTCConfiguration config_;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000170};
171
kjellander@webrtc.org70c0e292015-11-30 21:45:35 +0100172// Disabled for TSan v2, see
173// https://bugs.chromium.org/p/webrtc/issues/detail?id=4719 for details.
kjellander@webrtc.org3c28d0d2015-12-02 22:53:26 +0100174// 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)
deadbeefee8c6d32015-08-13 14:27:18 -0700177TEST_F(PeerConnectionEndToEndTest, Call) {
wu@webrtc.org364f2042013-11-20 21:49:41 +0000178 CreatePcs();
179 GetAndAddUserMedia();
180 Negotiate();
181 WaitForCallEstablished();
182}
kjellander@webrtc.org3c28d0d2015-12-02 22:53:26 +0100183#endif // if !defined(THREAD_SANITIZER) && !defined(WEBRTC_MAC)
wu@webrtc.org364f2042013-11-20 21:49:41 +0000184
philipel7703b272016-11-28 16:23:12 +0100185#if !defined(ADDRESS_SANITIZER)
deadbeefc9be0072015-12-14 18:27:57 -0800186TEST_F(PeerConnectionEndToEndTest, CallWithLegacySdp) {
wu@webrtc.org364f2042013-11-20 21:49:41 +0000187 FakeConstraints pc_constraints;
188 pc_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
189 false);
190 CreatePcs(&pc_constraints);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000191 GetAndAddUserMedia();
192 Negotiate();
193 WaitForCallEstablished();
194}
philipel7703b272016-11-28 16:23:12 +0100195#endif // !defined(ADDRESS_SANITIZER)
wu@webrtc.orgb43202d2013-11-22 19:14:25 +0000196
deadbeef40610e22016-12-22 10:53:38 -0800197#ifdef HAVE_SCTP
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000198// Verifies that a DataChannel created before the negotiation can transition to
199// "OPEN" and transfer data.
200TEST_F(PeerConnectionEndToEndTest, CreateDataChannelBeforeNegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000201 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000202
203 CreatePcs();
204
205 webrtc::DataChannelInit init;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000206 rtc::scoped_refptr<DataChannelInterface> caller_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000207 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000208 rtc::scoped_refptr<DataChannelInterface> callee_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000209 callee_->CreateDataChannel("data", init));
210
211 Negotiate();
212 WaitForConnection();
213
214 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
215 WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0);
216
217 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[0]);
218 TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0]);
219
220 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 0);
221 CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0);
222}
223
224// Verifies that a DataChannel created after the negotiation can transition to
225// "OPEN" and transfer data.
Taylor Brandstetterbf2f5692016-06-29 11:22:47 -0700226TEST_F(PeerConnectionEndToEndTest, CreateDataChannelAfterNegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000227 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000228
229 CreatePcs();
230
231 webrtc::DataChannelInit init;
232
233 // This DataChannel is for creating the data content in the negotiation.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000234 rtc::scoped_refptr<DataChannelInterface> dummy(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000235 caller_->CreateDataChannel("data", init));
236 Negotiate();
237 WaitForConnection();
238
Taylor Brandstetterbf2f5692016-06-29 11:22:47 -0700239 // Wait for the data channel created pre-negotiation to be opened.
240 WaitForDataChannelsToOpen(dummy, callee_signaled_data_channels_, 0);
241
242 // Create new DataChannels after the negotiation and verify their states.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000243 rtc::scoped_refptr<DataChannelInterface> caller_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000244 caller_->CreateDataChannel("hello", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000245 rtc::scoped_refptr<DataChannelInterface> callee_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000246 callee_->CreateDataChannel("hello", init));
247
248 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
249 WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0);
250
251 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
252 TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0]);
253
254 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
255 CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0);
256}
257
258// Verifies that DataChannel IDs are even/odd based on the DTLS roles.
259TEST_F(PeerConnectionEndToEndTest, DataChannelIdAssignment) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000260 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000261
262 CreatePcs();
263
264 webrtc::DataChannelInit init;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000265 rtc::scoped_refptr<DataChannelInterface> caller_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000266 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000267 rtc::scoped_refptr<DataChannelInterface> callee_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000268 callee_->CreateDataChannel("data", init));
269
270 Negotiate();
271 WaitForConnection();
272
273 EXPECT_EQ(1U, caller_dc_1->id() % 2);
274 EXPECT_EQ(0U, callee_dc_1->id() % 2);
275
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000276 rtc::scoped_refptr<DataChannelInterface> caller_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000277 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000278 rtc::scoped_refptr<DataChannelInterface> callee_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000279 callee_->CreateDataChannel("data", init));
280
281 EXPECT_EQ(1U, caller_dc_2->id() % 2);
282 EXPECT_EQ(0U, callee_dc_2->id() % 2);
283}
284
285// Verifies that the message is received by the right remote DataChannel when
286// there are multiple DataChannels.
287TEST_F(PeerConnectionEndToEndTest,
288 MessageTransferBetweenTwoPairsOfDataChannels) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000289 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000290
291 CreatePcs();
292
293 webrtc::DataChannelInit init;
294
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000295 rtc::scoped_refptr<DataChannelInterface> caller_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000296 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000297 rtc::scoped_refptr<DataChannelInterface> caller_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000298 caller_->CreateDataChannel("data", init));
299
300 Negotiate();
301 WaitForConnection();
302 WaitForDataChannelsToOpen(caller_dc_1, callee_signaled_data_channels_, 0);
303 WaitForDataChannelsToOpen(caller_dc_2, callee_signaled_data_channels_, 1);
304
kwibergd1fe2812016-04-27 06:47:29 -0700305 std::unique_ptr<webrtc::MockDataChannelObserver> dc_1_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000306 new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[0]));
307
kwibergd1fe2812016-04-27 06:47:29 -0700308 std::unique_ptr<webrtc::MockDataChannelObserver> dc_2_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000309 new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[1]));
310
311 const std::string message_1 = "hello 1";
312 const std::string message_2 = "hello 2";
313
314 caller_dc_1->Send(webrtc::DataBuffer(message_1));
315 EXPECT_EQ_WAIT(message_1, dc_1_observer->last_message(), kMaxWait);
316
317 caller_dc_2->Send(webrtc::DataBuffer(message_2));
318 EXPECT_EQ_WAIT(message_2, dc_2_observer->last_message(), kMaxWait);
319
320 EXPECT_EQ(1U, dc_1_observer->received_message_count());
321 EXPECT_EQ(1U, dc_2_observer->received_message_count());
322}
deadbeef40610e22016-12-22 10:53:38 -0800323#endif // HAVE_SCTP
deadbeefab9b2d12015-10-14 11:33:11 -0700324
zhihuang9763d562016-08-05 11:14:50 -0700325#ifdef HAVE_QUIC
326// Test that QUIC data channels can be used and that messages go to the correct
327// remote data channel when both peers want to use QUIC. It is assumed that the
328// application has externally negotiated the data channel parameters.
329TEST_F(PeerConnectionEndToEndTest, MessageTransferBetweenQuicDataChannels) {
330 config_.enable_quic = true;
331 CreatePcs();
332
333 webrtc::DataChannelInit init_1;
334 init_1.id = 0;
335 init_1.ordered = false;
336 init_1.reliable = true;
337
338 webrtc::DataChannelInit init_2;
339 init_2.id = 1;
340 init_2.ordered = false;
341 init_2.reliable = true;
342
343 rtc::scoped_refptr<DataChannelInterface> caller_dc_1(
344 caller_->CreateDataChannel("data", init_1));
345 ASSERT_NE(nullptr, caller_dc_1);
346 rtc::scoped_refptr<DataChannelInterface> caller_dc_2(
347 caller_->CreateDataChannel("data", init_2));
348 ASSERT_NE(nullptr, caller_dc_2);
349 rtc::scoped_refptr<DataChannelInterface> callee_dc_1(
350 callee_->CreateDataChannel("data", init_1));
351 ASSERT_NE(nullptr, callee_dc_1);
352 rtc::scoped_refptr<DataChannelInterface> callee_dc_2(
353 callee_->CreateDataChannel("data", init_2));
354 ASSERT_NE(nullptr, callee_dc_2);
355
356 Negotiate();
357 WaitForConnection();
358 EXPECT_TRUE_WAIT(caller_dc_1->state() == webrtc::DataChannelInterface::kOpen,
359 kMaxWait);
360 EXPECT_TRUE_WAIT(callee_dc_1->state() == webrtc::DataChannelInterface::kOpen,
361 kMaxWait);
362 EXPECT_TRUE_WAIT(caller_dc_2->state() == webrtc::DataChannelInterface::kOpen,
363 kMaxWait);
364 EXPECT_TRUE_WAIT(callee_dc_2->state() == webrtc::DataChannelInterface::kOpen,
365 kMaxWait);
366
367 std::unique_ptr<webrtc::MockDataChannelObserver> dc_1_observer(
368 new webrtc::MockDataChannelObserver(callee_dc_1.get()));
369
370 std::unique_ptr<webrtc::MockDataChannelObserver> dc_2_observer(
371 new webrtc::MockDataChannelObserver(callee_dc_2.get()));
372
373 const std::string message_1 = "hello 1";
374 const std::string message_2 = "hello 2";
375
376 // Send data from caller to callee.
377 caller_dc_1->Send(webrtc::DataBuffer(message_1));
378 EXPECT_EQ_WAIT(message_1, dc_1_observer->last_message(), kMaxWait);
379
380 caller_dc_2->Send(webrtc::DataBuffer(message_2));
381 EXPECT_EQ_WAIT(message_2, dc_2_observer->last_message(), kMaxWait);
382
383 EXPECT_EQ(1U, dc_1_observer->received_message_count());
384 EXPECT_EQ(1U, dc_2_observer->received_message_count());
385
386 // Send data from callee to caller.
387 dc_1_observer.reset(new webrtc::MockDataChannelObserver(caller_dc_1.get()));
388 dc_2_observer.reset(new webrtc::MockDataChannelObserver(caller_dc_2.get()));
389
390 callee_dc_1->Send(webrtc::DataBuffer(message_1));
391 EXPECT_EQ_WAIT(message_1, dc_1_observer->last_message(), kMaxWait);
392
393 callee_dc_2->Send(webrtc::DataBuffer(message_2));
394 EXPECT_EQ_WAIT(message_2, dc_2_observer->last_message(), kMaxWait);
395
396 EXPECT_EQ(1U, dc_1_observer->received_message_count());
397 EXPECT_EQ(1U, dc_2_observer->received_message_count());
398}
399#endif // HAVE_QUIC
400
deadbeef40610e22016-12-22 10:53:38 -0800401#ifdef HAVE_SCTP
deadbeefab9b2d12015-10-14 11:33:11 -0700402// Verifies that a DataChannel added from an OPEN message functions after
403// a channel has been previously closed (webrtc issue 3778).
404// This previously failed because the new channel re-uses the ID of the closed
405// channel, and the closed channel was incorrectly still assigned to the id.
406// TODO(deadbeef): This is disabled because there's currently a race condition
407// caused by the fact that a data channel signals that it's closed before it
408// really is. Re-enable this test once that's fixed.
deadbeefe2213ce2016-11-03 16:01:57 -0700409// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=4453
deadbeefab9b2d12015-10-14 11:33:11 -0700410TEST_F(PeerConnectionEndToEndTest,
411 DISABLED_DataChannelFromOpenWorksAfterClose) {
412 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
413
414 CreatePcs();
415
416 webrtc::DataChannelInit init;
417 rtc::scoped_refptr<DataChannelInterface> caller_dc(
418 caller_->CreateDataChannel("data", init));
419
420 Negotiate();
421 WaitForConnection();
422
423 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
424 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 0);
425
426 // Create a new channel and ensure it works after closing the previous one.
427 caller_dc = caller_->CreateDataChannel("data2", init);
428
429 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
430 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
431
432 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
433}
deadbeefbd292462015-12-14 18:15:29 -0800434
435// This tests that if a data channel is closed remotely while not referenced
436// by the application (meaning only the PeerConnection contributes to its
437// reference count), no memory access violation will occur.
438// See: https://code.google.com/p/chromium/issues/detail?id=565048
439TEST_F(PeerConnectionEndToEndTest, CloseDataChannelRemotelyWhileNotReferenced) {
440 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
441
442 CreatePcs();
443
444 webrtc::DataChannelInit init;
445 rtc::scoped_refptr<DataChannelInterface> caller_dc(
446 caller_->CreateDataChannel("data", init));
447
448 Negotiate();
449 WaitForConnection();
450
451 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
452 // This removes the reference to the remote data channel that we hold.
453 callee_signaled_data_channels_.clear();
454 caller_dc->Close();
455 EXPECT_EQ_WAIT(DataChannelInterface::kClosed, caller_dc->state(), kMaxWait);
456
457 // Wait for a bit longer so the remote data channel will receive the
458 // close message and be destroyed.
459 rtc::Thread::Current()->ProcessMessages(100);
460}
deadbeef40610e22016-12-22 10:53:38 -0800461#endif // HAVE_SCTP