blob: 4ca73babdb7ac071c120415a94df6e96157582c9 [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.
henrik.lundin@webrtc.org22362672014-11-03 13:38:50 +0000218#if defined(MEMORY_SANITIZER)
219// Fails under MemorySanitizer:
220// See https://code.google.com/p/webrtc/issues/detail?id=3980.
221#define MAYBE_CreateDataChannelAfterNegotiate DISABLED_CreateDataChannelAfterNegotiate
222#else
223#define MAYBE_CreateDataChannelAfterNegotiate CreateDataChannelAfterNegotiate
224#endif
225TEST_F(PeerConnectionEndToEndTest, MAYBE_CreateDataChannelAfterNegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000226 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000227
228 CreatePcs();
229
230 webrtc::DataChannelInit init;
231
232 // This DataChannel is for creating the data content in the negotiation.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000233 rtc::scoped_refptr<DataChannelInterface> dummy(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000234 caller_->CreateDataChannel("data", init));
235 Negotiate();
236 WaitForConnection();
237
238 // Creates new DataChannels after the negotiation and verifies their states.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000239 rtc::scoped_refptr<DataChannelInterface> caller_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000240 caller_->CreateDataChannel("hello", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000241 rtc::scoped_refptr<DataChannelInterface> callee_dc(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000242 callee_->CreateDataChannel("hello", init));
243
244 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
245 WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0);
246
247 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
248 TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0]);
249
250 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
251 CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0);
252}
253
254// Verifies that DataChannel IDs are even/odd based on the DTLS roles.
255TEST_F(PeerConnectionEndToEndTest, DataChannelIdAssignment) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000256 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000257
258 CreatePcs();
259
260 webrtc::DataChannelInit init;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000261 rtc::scoped_refptr<DataChannelInterface> caller_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000262 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000263 rtc::scoped_refptr<DataChannelInterface> callee_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000264 callee_->CreateDataChannel("data", init));
265
266 Negotiate();
267 WaitForConnection();
268
269 EXPECT_EQ(1U, caller_dc_1->id() % 2);
270 EXPECT_EQ(0U, callee_dc_1->id() % 2);
271
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000272 rtc::scoped_refptr<DataChannelInterface> caller_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000273 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000274 rtc::scoped_refptr<DataChannelInterface> callee_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000275 callee_->CreateDataChannel("data", init));
276
277 EXPECT_EQ(1U, caller_dc_2->id() % 2);
278 EXPECT_EQ(0U, callee_dc_2->id() % 2);
279}
280
281// Verifies that the message is received by the right remote DataChannel when
282// there are multiple DataChannels.
283TEST_F(PeerConnectionEndToEndTest,
284 MessageTransferBetweenTwoPairsOfDataChannels) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000285 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000286
287 CreatePcs();
288
289 webrtc::DataChannelInit init;
290
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000291 rtc::scoped_refptr<DataChannelInterface> caller_dc_1(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000292 caller_->CreateDataChannel("data", init));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000293 rtc::scoped_refptr<DataChannelInterface> caller_dc_2(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000294 caller_->CreateDataChannel("data", init));
295
296 Negotiate();
297 WaitForConnection();
298 WaitForDataChannelsToOpen(caller_dc_1, callee_signaled_data_channels_, 0);
299 WaitForDataChannelsToOpen(caller_dc_2, callee_signaled_data_channels_, 1);
300
kwibergd1fe2812016-04-27 06:47:29 -0700301 std::unique_ptr<webrtc::MockDataChannelObserver> dc_1_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000302 new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[0]));
303
kwibergd1fe2812016-04-27 06:47:29 -0700304 std::unique_ptr<webrtc::MockDataChannelObserver> dc_2_observer(
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000305 new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[1]));
306
307 const std::string message_1 = "hello 1";
308 const std::string message_2 = "hello 2";
309
310 caller_dc_1->Send(webrtc::DataBuffer(message_1));
311 EXPECT_EQ_WAIT(message_1, dc_1_observer->last_message(), kMaxWait);
312
313 caller_dc_2->Send(webrtc::DataBuffer(message_2));
314 EXPECT_EQ_WAIT(message_2, dc_2_observer->last_message(), kMaxWait);
315
316 EXPECT_EQ(1U, dc_1_observer->received_message_count());
317 EXPECT_EQ(1U, dc_2_observer->received_message_count());
318}
deadbeefab9b2d12015-10-14 11:33:11 -0700319
320// Verifies that a DataChannel added from an OPEN message functions after
321// a channel has been previously closed (webrtc issue 3778).
322// This previously failed because the new channel re-uses the ID of the closed
323// channel, and the closed channel was incorrectly still assigned to the id.
324// TODO(deadbeef): This is disabled because there's currently a race condition
325// caused by the fact that a data channel signals that it's closed before it
326// really is. Re-enable this test once that's fixed.
327TEST_F(PeerConnectionEndToEndTest,
328 DISABLED_DataChannelFromOpenWorksAfterClose) {
329 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
330
331 CreatePcs();
332
333 webrtc::DataChannelInit init;
334 rtc::scoped_refptr<DataChannelInterface> caller_dc(
335 caller_->CreateDataChannel("data", init));
336
337 Negotiate();
338 WaitForConnection();
339
340 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
341 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 0);
342
343 // Create a new channel and ensure it works after closing the previous one.
344 caller_dc = caller_->CreateDataChannel("data2", init);
345
346 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
347 TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
348
349 CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
350}
deadbeefbd292462015-12-14 18:15:29 -0800351
352// This tests that if a data channel is closed remotely while not referenced
353// by the application (meaning only the PeerConnection contributes to its
354// reference count), no memory access violation will occur.
355// See: https://code.google.com/p/chromium/issues/detail?id=565048
356TEST_F(PeerConnectionEndToEndTest, CloseDataChannelRemotelyWhileNotReferenced) {
357 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
358
359 CreatePcs();
360
361 webrtc::DataChannelInit init;
362 rtc::scoped_refptr<DataChannelInterface> caller_dc(
363 caller_->CreateDataChannel("data", init));
364
365 Negotiate();
366 WaitForConnection();
367
368 WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
369 // This removes the reference to the remote data channel that we hold.
370 callee_signaled_data_channels_.clear();
371 caller_dc->Close();
372 EXPECT_EQ_WAIT(DataChannelInterface::kClosed, caller_dc->state(), kMaxWait);
373
374 // Wait for a bit longer so the remote data channel will receive the
375 // close message and be destroyed.
376 rtc::Thread::Current()->ProcessMessages(100);
377}