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