blob: e01daa9439d0c043458ba2fb7470d2054882eef7 [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_);
phoglund37ebcf02016-01-08 05:04:57 -080059#ifdef WEBRTC_ANDROID
60 webrtc::InitializeAndroidObjects();
61#endif
wu@webrtc.org364f2042013-11-20 21:49:41 +000062 }
63
64 void CreatePcs() {
65 CreatePcs(NULL);
66 }
67
68 void CreatePcs(const MediaConstraintsInterface* pc_constraints) {
69 EXPECT_TRUE(caller_->CreatePc(pc_constraints));
70 EXPECT_TRUE(callee_->CreatePc(pc_constraints));
71 PeerConnectionTestWrapper::Connect(caller_.get(), callee_.get());
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000072
73 caller_->SignalOnDataChannel.connect(
74 this, &PeerConnectionEndToEndTest::OnCallerAddedDataChanel);
75 callee_->SignalOnDataChannel.connect(
76 this, &PeerConnectionEndToEndTest::OnCalleeAddedDataChannel);
wu@webrtc.org364f2042013-11-20 21:49:41 +000077 }
78
79 void GetAndAddUserMedia() {
80 FakeConstraints audio_constraints;
81 FakeConstraints video_constraints;
82 GetAndAddUserMedia(true, audio_constraints, true, video_constraints);
83 }
84
85 void GetAndAddUserMedia(bool audio, FakeConstraints audio_constraints,
86 bool video, FakeConstraints video_constraints) {
87 caller_->GetAndAddUserMedia(audio, audio_constraints,
88 video, video_constraints);
89 callee_->GetAndAddUserMedia(audio, audio_constraints,
90 video, video_constraints);
91 }
92
93 void Negotiate() {
94 caller_->CreateOffer(NULL);
95 }
96
97 void WaitForCallEstablished() {
98 caller_->WaitForCallEstablished();
99 callee_->WaitForCallEstablished();
100 }
101
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000102 void WaitForConnection() {
103 caller_->WaitForConnection();
104 callee_->WaitForConnection();
105 }
106
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000107 void OnCallerAddedDataChanel(DataChannelInterface* dc) {
108 caller_signaled_data_channels_.push_back(dc);
109 }
110
111 void OnCalleeAddedDataChannel(DataChannelInterface* dc) {
112 callee_signaled_data_channels_.push_back(dc);
113 }
114
115 // Tests that |dc1| and |dc2| can send to and receive from each other.
116 void TestDataChannelSendAndReceive(
117 DataChannelInterface* dc1, DataChannelInterface* dc2) {
kwibergd1fe2812016-04-27 06:47:29 -0700118 std::unique_ptr<webrtc::MockDataChannelObserver> dc1_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000119 new webrtc::MockDataChannelObserver(dc1));
120
kwibergd1fe2812016-04-27 06:47:29 -0700121 std::unique_ptr<webrtc::MockDataChannelObserver> dc2_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000122 new webrtc::MockDataChannelObserver(dc2));
123
124 static const std::string kDummyData = "abcdefg";
125 webrtc::DataBuffer buffer(kDummyData);
126 EXPECT_TRUE(dc1->Send(buffer));
127 EXPECT_EQ_WAIT(kDummyData, dc2_observer->last_message(), kMaxWait);
128
129 EXPECT_TRUE(dc2->Send(buffer));
130 EXPECT_EQ_WAIT(kDummyData, dc1_observer->last_message(), kMaxWait);
131
132 EXPECT_EQ(1U, dc1_observer->received_message_count());
133 EXPECT_EQ(1U, dc2_observer->received_message_count());
134 }
135
136 void WaitForDataChannelsToOpen(DataChannelInterface* local_dc,
137 const DataChannelList& remote_dc_list,
138 size_t remote_dc_index) {
139 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, local_dc->state(), kMaxWait);
140
141 EXPECT_TRUE_WAIT(remote_dc_list.size() > remote_dc_index, kMaxWait);
142 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
143 remote_dc_list[remote_dc_index]->state(),
144 kMaxWait);
145 EXPECT_EQ(local_dc->id(), remote_dc_list[remote_dc_index]->id());
146 }
147
148 void CloseDataChannels(DataChannelInterface* local_dc,
149 const DataChannelList& remote_dc_list,
150 size_t remote_dc_index) {
151 local_dc->Close();
152 EXPECT_EQ_WAIT(DataChannelInterface::kClosed, local_dc->state(), kMaxWait);
153 EXPECT_EQ_WAIT(DataChannelInterface::kClosed,
154 remote_dc_list[remote_dc_index]->state(),
155 kMaxWait);
156 }
157
wu@webrtc.org364f2042013-11-20 21:49:41 +0000158 protected:
danilchape9021a32016-05-17 01:52:02 -0700159 rtc::Thread network_thread_;
perkj57db6522016-04-08 08:16:33 -0700160 rtc::Thread worker_thread_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000161 rtc::scoped_refptr<PeerConnectionTestWrapper> caller_;
162 rtc::scoped_refptr<PeerConnectionTestWrapper> callee_;
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000163 DataChannelList caller_signaled_data_channels_;
164 DataChannelList callee_signaled_data_channels_;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000165};
166
kjellander@webrtc.org70c0e292015-11-30 21:45:35 +0100167// Disabled for TSan v2, see
168// https://bugs.chromium.org/p/webrtc/issues/detail?id=4719 for details.
kjellander@webrtc.org3c28d0d2015-12-02 22:53:26 +0100169// Disabled for Mac, see
170// https://bugs.chromium.org/p/webrtc/issues/detail?id=5231 for details.
171#if !defined(THREAD_SANITIZER) && !defined(WEBRTC_MAC)
deadbeefee8c6d32015-08-13 14:27:18 -0700172TEST_F(PeerConnectionEndToEndTest, Call) {
wu@webrtc.org364f2042013-11-20 21:49:41 +0000173 CreatePcs();
174 GetAndAddUserMedia();
175 Negotiate();
176 WaitForCallEstablished();
177}
kjellander@webrtc.org3c28d0d2015-12-02 22:53:26 +0100178#endif // if !defined(THREAD_SANITIZER) && !defined(WEBRTC_MAC)
wu@webrtc.org364f2042013-11-20 21:49:41 +0000179
deadbeefc9be0072015-12-14 18:27:57 -0800180TEST_F(PeerConnectionEndToEndTest, CallWithLegacySdp) {
wu@webrtc.org364f2042013-11-20 21:49:41 +0000181 FakeConstraints pc_constraints;
182 pc_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
183 false);
184 CreatePcs(&pc_constraints);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000185 GetAndAddUserMedia();
186 Negotiate();
187 WaitForCallEstablished();
188}
wu@webrtc.orgb43202d2013-11-22 19:14:25 +0000189
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000190// Verifies that a DataChannel created before the negotiation can transition to
191// "OPEN" and transfer data.
192TEST_F(PeerConnectionEndToEndTest, CreateDataChannelBeforeNegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000193 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000194
195 CreatePcs();
196
197 webrtc::DataChannelInit init;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000198 rtc::scoped_refptr<DataChannelInterface> caller_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000199 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000200 rtc::scoped_refptr<DataChannelInterface> callee_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000201 callee_->CreateDataChannel("data", init));
202
203 Negotiate();
204 WaitForConnection();
205
206 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
207 WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0);
208
209 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[0]);
210 TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0]);
211
212 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 0);
213 CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0);
214}
215
216// Verifies that a DataChannel created after the negotiation can transition to
217// "OPEN" and transfer data.
Taylor Brandstetterbf2f5692016-06-29 11:22:47 -0700218TEST_F(PeerConnectionEndToEndTest, CreateDataChannelAfterNegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000219 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000220
221 CreatePcs();
222
223 webrtc::DataChannelInit init;
224
225 // This DataChannel is for creating the data content in the negotiation.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000226 rtc::scoped_refptr<DataChannelInterface> dummy(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000227 caller_->CreateDataChannel("data", init));
228 Negotiate();
229 WaitForConnection();
230
Taylor Brandstetterbf2f5692016-06-29 11:22:47 -0700231 // Wait for the data channel created pre-negotiation to be opened.
232 WaitForDataChannelsToOpen(dummy, callee_signaled_data_channels_, 0);
233
234 // Create new DataChannels after the negotiation and verify their states.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000235 rtc::scoped_refptr<DataChannelInterface> caller_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000236 caller_->CreateDataChannel("hello", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000237 rtc::scoped_refptr<DataChannelInterface> callee_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000238 callee_->CreateDataChannel("hello", init));
239
240 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
241 WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0);
242
243 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
244 TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0]);
245
246 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
247 CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0);
248}
249
250// Verifies that DataChannel IDs are even/odd based on the DTLS roles.
251TEST_F(PeerConnectionEndToEndTest, DataChannelIdAssignment) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000252 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000253
254 CreatePcs();
255
256 webrtc::DataChannelInit init;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000257 rtc::scoped_refptr<DataChannelInterface> caller_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000258 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000259 rtc::scoped_refptr<DataChannelInterface> callee_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000260 callee_->CreateDataChannel("data", init));
261
262 Negotiate();
263 WaitForConnection();
264
265 EXPECT_EQ(1U, caller_dc_1->id() % 2);
266 EXPECT_EQ(0U, callee_dc_1->id() % 2);
267
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000268 rtc::scoped_refptr<DataChannelInterface> caller_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000269 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000270 rtc::scoped_refptr<DataChannelInterface> callee_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000271 callee_->CreateDataChannel("data", init));
272
273 EXPECT_EQ(1U, caller_dc_2->id() % 2);
274 EXPECT_EQ(0U, callee_dc_2->id() % 2);
275}
276
277// Verifies that the message is received by the right remote DataChannel when
278// there are multiple DataChannels.
279TEST_F(PeerConnectionEndToEndTest,
280 MessageTransferBetweenTwoPairsOfDataChannels) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000281 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000282
283 CreatePcs();
284
285 webrtc::DataChannelInit init;
286
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000287 rtc::scoped_refptr<DataChannelInterface> caller_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000288 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000289 rtc::scoped_refptr<DataChannelInterface> caller_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000290 caller_->CreateDataChannel("data", init));
291
292 Negotiate();
293 WaitForConnection();
294 WaitForDataChannelsToOpen(caller_dc_1, callee_signaled_data_channels_, 0);
295 WaitForDataChannelsToOpen(caller_dc_2, callee_signaled_data_channels_, 1);
296
kwibergd1fe2812016-04-27 06:47:29 -0700297 std::unique_ptr<webrtc::MockDataChannelObserver> dc_1_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000298 new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[0]));
299
kwibergd1fe2812016-04-27 06:47:29 -0700300 std::unique_ptr<webrtc::MockDataChannelObserver> dc_2_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000301 new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[1]));
302
303 const std::string message_1 = "hello 1";
304 const std::string message_2 = "hello 2";
305
306 caller_dc_1->Send(webrtc::DataBuffer(message_1));
307 EXPECT_EQ_WAIT(message_1, dc_1_observer->last_message(), kMaxWait);
308
309 caller_dc_2->Send(webrtc::DataBuffer(message_2));
310 EXPECT_EQ_WAIT(message_2, dc_2_observer->last_message(), kMaxWait);
311
312 EXPECT_EQ(1U, dc_1_observer->received_message_count());
313 EXPECT_EQ(1U, dc_2_observer->received_message_count());
314}
deadbeefab9b2d12015-10-14 11:33:11 -0700315
316// Verifies that a DataChannel added from an OPEN message functions after
317// a channel has been previously closed (webrtc issue 3778).
318// This previously failed because the new channel re-uses the ID of the closed
319// channel, and the closed channel was incorrectly still assigned to the id.
320// TODO(deadbeef): This is disabled because there's currently a race condition
321// caused by the fact that a data channel signals that it's closed before it
322// really is. Re-enable this test once that's fixed.
323TEST_F(PeerConnectionEndToEndTest,
324 DISABLED_DataChannelFromOpenWorksAfterClose) {
325 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
326
327 CreatePcs();
328
329 webrtc::DataChannelInit init;
330 rtc::scoped_refptr<DataChannelInterface> caller_dc(
331 caller_->CreateDataChannel("data", init));
332
333 Negotiate();
334 WaitForConnection();
335
336 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
337 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 0);
338
339 // Create a new channel and ensure it works after closing the previous one.
340 caller_dc = caller_->CreateDataChannel("data2", init);
341
342 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
343 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
344
345 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
346}
deadbeefbd292462015-12-14 18:15:29 -0800347
348// This tests that if a data channel is closed remotely while not referenced
349// by the application (meaning only the PeerConnection contributes to its
350// reference count), no memory access violation will occur.
351// See: https://code.google.com/p/chromium/issues/detail?id=565048
352TEST_F(PeerConnectionEndToEndTest, CloseDataChannelRemotelyWhileNotReferenced) {
353 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
354
355 CreatePcs();
356
357 webrtc::DataChannelInit init;
358 rtc::scoped_refptr<DataChannelInterface> caller_dc(
359 caller_->CreateDataChannel("data", init));
360
361 Negotiate();
362 WaitForConnection();
363
364 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
365 // This removes the reference to the remote data channel that we hold.
366 callee_signaled_data_channels_.clear();
367 caller_dc->Close();
368 EXPECT_EQ_WAIT(DataChannelInterface::kClosed, caller_dc->state(), kMaxWait);
369
370 // Wait for a bit longer so the remote data channel will receive the
371 // close message and be destroyed.
372 rtc::Thread::Current()->ProcessMessages(100);
373}