blob: 1a93492819c6152bae432537830e19b32d1d0874 [file] [log] [blame]
Harald Alvestrand39993842021-02-17 09:05:31 +00001/*
2 * Copyright 2012 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 <stdint.h>
12
13#include <algorithm>
14#include <memory>
15#include <string>
16#include <vector>
17
18#include "absl/types/optional.h"
19#include "api/data_channel_interface.h"
20#include "api/dtmf_sender_interface.h"
21#include "api/peer_connection_interface.h"
22#include "api/scoped_refptr.h"
23#include "api/units/time_delta.h"
24#include "pc/test/integration_test_helpers.h"
25#include "pc/test/mock_peer_connection_observers.h"
26#include "rtc_base/fake_clock.h"
27#include "rtc_base/gunit.h"
28#include "rtc_base/ref_counted_object.h"
29#include "rtc_base/virtual_socket_server.h"
30
31namespace webrtc {
32
33namespace {
34
35class DataChannelIntegrationTest
36 : public PeerConnectionIntegrationBaseTest,
37 public ::testing::WithParamInterface<SdpSemantics> {
38 protected:
39 DataChannelIntegrationTest()
40 : PeerConnectionIntegrationBaseTest(GetParam()) {}
41};
42
Bjorn Terelius3208bf12021-03-04 10:53:08 +010043GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(DataChannelIntegrationTest);
44
Harald Alvestrand39993842021-02-17 09:05:31 +000045// Fake clock must be set before threads are started to prevent race on
46// Set/GetClockForTesting().
47// To achieve that, multiple inheritance is used as a mixin pattern
48// where order of construction is finely controlled.
49// This also ensures peerconnection is closed before switching back to non-fake
50// clock, avoiding other races and DCHECK failures such as in rtp_sender.cc.
51class FakeClockForTest : public rtc::ScopedFakeClock {
52 protected:
53 FakeClockForTest() {
54 // Some things use a time of "0" as a special value, so we need to start out
55 // the fake clock at a nonzero time.
56 // TODO(deadbeef): Fix this.
57 AdvanceTime(webrtc::TimeDelta::Seconds(1));
58 }
59
60 // Explicit handle.
61 ScopedFakeClock& FakeClock() { return *this; }
62};
63
64// Ensure FakeClockForTest is constructed first (see class for rationale).
65class DataChannelIntegrationTestWithFakeClock
66 : public FakeClockForTest,
67 public DataChannelIntegrationTest {};
68
69class DataChannelIntegrationTestPlanB
70 : public PeerConnectionIntegrationBaseTest {
71 protected:
72 DataChannelIntegrationTestPlanB()
73 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
74};
75
Bjorn Terelius3208bf12021-03-04 10:53:08 +010076GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(
77 DataChannelIntegrationTestWithFakeClock);
78
Harald Alvestrand39993842021-02-17 09:05:31 +000079class DataChannelIntegrationTestUnifiedPlan
80 : public PeerConnectionIntegrationBaseTest {
81 protected:
82 DataChannelIntegrationTestUnifiedPlan()
83 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
84};
85
86class DummyDtmfObserver : public DtmfSenderObserverInterface {
87 public:
88 DummyDtmfObserver() : completed_(false) {}
89
90 // Implements DtmfSenderObserverInterface.
91 void OnToneChange(const std::string& tone) override {
92 tones_.push_back(tone);
93 if (tone.empty()) {
94 completed_ = true;
95 }
96 }
97
98 const std::vector<std::string>& tones() const { return tones_; }
99 bool completed() const { return completed_; }
100
101 private:
102 bool completed_;
103 std::vector<std::string> tones_;
104};
105
106#ifdef WEBRTC_HAVE_SCTP
107
108// This test causes a PeerConnection to enter Disconnected state, and
109// sends data on a DataChannel while disconnected.
110// The data should be surfaced when the connection reestablishes.
111TEST_P(DataChannelIntegrationTest, DataChannelWhileDisconnected) {
112 CreatePeerConnectionWrappers();
113 ConnectFakeSignaling();
114 caller()->CreateDataChannel();
115 caller()->CreateAndSetAndSignalOffer();
116 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
117 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
118 std::string data1 = "hello first";
119 caller()->data_channel()->Send(DataBuffer(data1));
120 EXPECT_EQ_WAIT(data1, callee()->data_observer()->last_message(),
121 kDefaultTimeout);
122 // Cause a network outage
123 virtual_socket_server()->set_drop_probability(1.0);
124 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
125 caller()->standardized_ice_connection_state(),
126 kDefaultTimeout);
127 std::string data2 = "hello second";
128 caller()->data_channel()->Send(DataBuffer(data2));
129 // Remove the network outage. The connection should reestablish.
130 virtual_socket_server()->set_drop_probability(0.0);
131 EXPECT_EQ_WAIT(data2, callee()->data_observer()->last_message(),
132 kDefaultTimeout);
133}
134
135// This test causes a PeerConnection to enter Disconnected state,
136// sends data on a DataChannel while disconnected, and then triggers
137// an ICE restart.
138// The data should be surfaced when the connection reestablishes.
139TEST_P(DataChannelIntegrationTest, DataChannelWhileDisconnectedIceRestart) {
140 CreatePeerConnectionWrappers();
141 ConnectFakeSignaling();
142 caller()->CreateDataChannel();
143 caller()->CreateAndSetAndSignalOffer();
144 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
145 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
146 std::string data1 = "hello first";
147 caller()->data_channel()->Send(DataBuffer(data1));
148 EXPECT_EQ_WAIT(data1, callee()->data_observer()->last_message(),
149 kDefaultTimeout);
150 // Cause a network outage
151 virtual_socket_server()->set_drop_probability(1.0);
152 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
153 caller()->standardized_ice_connection_state(),
154 kDefaultTimeout);
155 std::string data2 = "hello second";
156 caller()->data_channel()->Send(DataBuffer(data2));
157
158 // Trigger an ICE restart. The signaling channel is not affected by
159 // the network outage.
160 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
161 caller()->CreateAndSetAndSignalOffer();
162 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
163 // Remove the network outage. The connection should reestablish.
164 virtual_socket_server()->set_drop_probability(0.0);
165 EXPECT_EQ_WAIT(data2, callee()->data_observer()->last_message(),
166 kDefaultTimeout);
167}
168
169#endif // WEBRTC_HAVE_SCTP
170
171// This test sets up a call between two parties with audio, video and an RTP
172// data channel.
173TEST_P(DataChannelIntegrationTest, EndToEndCallWithRtpDataChannel) {
174 PeerConnectionInterface::RTCConfiguration rtc_config;
175 rtc_config.enable_rtp_data_channel = true;
176 rtc_config.enable_dtls_srtp = false;
177 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
178 ConnectFakeSignaling();
179 // Expect that data channel created on caller side will show up for callee as
180 // well.
181 caller()->CreateDataChannel();
182 caller()->AddAudioVideoTracks();
183 callee()->AddAudioVideoTracks();
184 caller()->CreateAndSetAndSignalOffer();
185 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
186 // Ensure the existence of the RTP data channel didn't impede audio/video.
187 MediaExpectations media_expectations;
188 media_expectations.ExpectBidirectionalAudioAndVideo();
189 ASSERT_TRUE(ExpectNewFrames(media_expectations));
190 ASSERT_NE(nullptr, caller()->data_channel());
191 ASSERT_NE(nullptr, callee()->data_channel());
192 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
193 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
194
195 // Ensure data can be sent in both directions.
196 std::string data = "hello world";
197 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
198 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
199 kDefaultTimeout);
200 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
201 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
202 kDefaultTimeout);
203}
204
205TEST_P(DataChannelIntegrationTest, RtpDataChannelWorksAfterRollback) {
206 PeerConnectionInterface::RTCConfiguration rtc_config;
207 rtc_config.enable_rtp_data_channel = true;
208 rtc_config.enable_dtls_srtp = false;
209 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
210 ConnectFakeSignaling();
211 auto data_channel = caller()->pc()->CreateDataChannel("label_1", nullptr);
212 ASSERT_TRUE(data_channel.get() != nullptr);
213 caller()->CreateAndSetAndSignalOffer();
214 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
215
216 caller()->CreateDataChannel("label_2", nullptr);
217 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
218 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
219 caller()->pc()->SetLocalDescription(observer,
220 caller()->CreateOfferAndWait().release());
221 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
222 caller()->Rollback();
223
224 std::string data = "hello world";
225 SendRtpDataWithRetries(data_channel, data, 5);
226 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
227 kDefaultTimeout);
228}
229
230// Ensure that an RTP data channel is signaled as closed for the caller when
231// the callee rejects it in a subsequent offer.
232TEST_P(DataChannelIntegrationTest, RtpDataChannelSignaledClosedInCalleeOffer) {
233 // Same procedure as above test.
234 PeerConnectionInterface::RTCConfiguration rtc_config;
235 rtc_config.enable_rtp_data_channel = true;
236 rtc_config.enable_dtls_srtp = false;
237 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
238 ConnectFakeSignaling();
239 caller()->CreateDataChannel();
240 caller()->AddAudioVideoTracks();
241 callee()->AddAudioVideoTracks();
242 caller()->CreateAndSetAndSignalOffer();
243 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
244 ASSERT_NE(nullptr, caller()->data_channel());
245 ASSERT_NE(nullptr, callee()->data_channel());
246 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
247 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
248
249 // Close the data channel on the callee, and do an updated offer/answer.
250 callee()->data_channel()->Close();
251 callee()->CreateAndSetAndSignalOffer();
252 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
253 EXPECT_FALSE(caller()->data_observer()->IsOpen());
254 EXPECT_FALSE(callee()->data_observer()->IsOpen());
255}
256
257#if !defined(THREAD_SANITIZER)
258// This test provokes TSAN errors. See bugs.webrtc.org/11282
259
260// Tests that data is buffered in an RTP data channel until an observer is
261// registered for it.
262//
263// NOTE: RTP data channels can receive data before the underlying
264// transport has detected that a channel is writable and thus data can be
265// received before the data channel state changes to open. That is hard to test
266// but the same buffering is expected to be used in that case.
267//
268// Use fake clock and simulated network delay so that we predictably can wait
269// until an SCTP message has been delivered without "sleep()"ing.
270TEST_P(DataChannelIntegrationTestWithFakeClock,
271 DataBufferedUntilRtpDataChannelObserverRegistered) {
272 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
273 virtual_socket_server()->UpdateDelayDistribution();
274
275 PeerConnectionInterface::RTCConfiguration rtc_config;
276 rtc_config.enable_rtp_data_channel = true;
277 rtc_config.enable_dtls_srtp = false;
278 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
279 ConnectFakeSignaling();
280 caller()->CreateDataChannel();
281 caller()->CreateAndSetAndSignalOffer();
282 ASSERT_TRUE(caller()->data_channel() != nullptr);
283 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
284 kDefaultTimeout, FakeClock());
285 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
286 kDefaultTimeout, FakeClock());
287 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
288 callee()->data_channel()->state(), kDefaultTimeout,
289 FakeClock());
290
291 // Unregister the observer which is normally automatically registered.
292 callee()->data_channel()->UnregisterObserver();
293 // Send data and advance fake clock until it should have been received.
294 std::string data = "hello world";
295 caller()->data_channel()->Send(DataBuffer(data));
296 SIMULATED_WAIT(false, 50, FakeClock());
297
298 // Attach data channel and expect data to be received immediately. Note that
299 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
300 // further, but data can be received even if the callback is asynchronous.
301 MockDataChannelObserver new_observer(callee()->data_channel());
302 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
303 FakeClock());
304}
305
306#endif // !defined(THREAD_SANITIZER)
307
308// This test sets up a call between two parties with audio, video and but only
309// the caller client supports RTP data channels.
310TEST_P(DataChannelIntegrationTest, RtpDataChannelsRejectedByCallee) {
311 PeerConnectionInterface::RTCConfiguration rtc_config_1;
312 rtc_config_1.enable_rtp_data_channel = true;
313 // Must disable DTLS to make negotiation succeed.
314 rtc_config_1.enable_dtls_srtp = false;
315 PeerConnectionInterface::RTCConfiguration rtc_config_2;
316 rtc_config_2.enable_dtls_srtp = false;
317 rtc_config_2.enable_dtls_srtp = false;
318 ASSERT_TRUE(
319 CreatePeerConnectionWrappersWithConfig(rtc_config_1, rtc_config_2));
320 ConnectFakeSignaling();
321 caller()->CreateDataChannel();
322 ASSERT_TRUE(caller()->data_channel() != nullptr);
323 caller()->AddAudioVideoTracks();
324 callee()->AddAudioVideoTracks();
325 caller()->CreateAndSetAndSignalOffer();
326 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
327 // The caller should still have a data channel, but it should be closed, and
328 // one should ever have been created for the callee.
329 EXPECT_TRUE(caller()->data_channel() != nullptr);
330 EXPECT_FALSE(caller()->data_observer()->IsOpen());
331 EXPECT_EQ(nullptr, callee()->data_channel());
332}
333
334// This test sets up a call between two parties with audio, and video. When
335// audio and video is setup and flowing, an RTP data channel is negotiated.
336TEST_P(DataChannelIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
337 PeerConnectionInterface::RTCConfiguration rtc_config;
338 rtc_config.enable_rtp_data_channel = true;
339 rtc_config.enable_dtls_srtp = false;
340 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
341 ConnectFakeSignaling();
342 // Do initial offer/answer with audio/video.
343 caller()->AddAudioVideoTracks();
344 callee()->AddAudioVideoTracks();
345 caller()->CreateAndSetAndSignalOffer();
346 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
347 // Create data channel and do new offer and answer.
348 caller()->CreateDataChannel();
349 caller()->CreateAndSetAndSignalOffer();
350 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
351 ASSERT_NE(nullptr, caller()->data_channel());
352 ASSERT_NE(nullptr, callee()->data_channel());
353 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
354 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
355 // Ensure data can be sent in both directions.
356 std::string data = "hello world";
357 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
358 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
359 kDefaultTimeout);
360 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
361 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
362 kDefaultTimeout);
363}
364
365#ifdef WEBRTC_HAVE_SCTP
366
367// This test sets up a call between two parties with audio, video and an SCTP
368// data channel.
369TEST_P(DataChannelIntegrationTest, EndToEndCallWithSctpDataChannel) {
370 ASSERT_TRUE(CreatePeerConnectionWrappers());
371 ConnectFakeSignaling();
372 // Expect that data channel created on caller side will show up for callee as
373 // well.
374 caller()->CreateDataChannel();
375 caller()->AddAudioVideoTracks();
376 callee()->AddAudioVideoTracks();
377 caller()->CreateAndSetAndSignalOffer();
378 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
379 // Ensure the existence of the SCTP data channel didn't impede audio/video.
380 MediaExpectations media_expectations;
381 media_expectations.ExpectBidirectionalAudioAndVideo();
382 ASSERT_TRUE(ExpectNewFrames(media_expectations));
383 // Caller data channel should already exist (it created one). Callee data
384 // channel may not exist yet, since negotiation happens in-band, not in SDP.
385 ASSERT_NE(nullptr, caller()->data_channel());
386 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
387 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
388 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
389
390 // Ensure data can be sent in both directions.
391 std::string data = "hello world";
392 caller()->data_channel()->Send(DataBuffer(data));
393 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
394 kDefaultTimeout);
395 callee()->data_channel()->Send(DataBuffer(data));
396 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
397 kDefaultTimeout);
398}
399
Harald Alvestrand7087b832021-03-11 17:21:13 +0000400// This test sets up a call between two parties with an SCTP
401// data channel only, and sends messages of various sizes.
402TEST_P(DataChannelIntegrationTest,
403 EndToEndCallWithSctpDataChannelVariousSizes) {
404 ASSERT_TRUE(CreatePeerConnectionWrappers());
405 ConnectFakeSignaling();
406 // Expect that data channel created on caller side will show up for callee as
407 // well.
408 caller()->CreateDataChannel();
409 caller()->CreateAndSetAndSignalOffer();
410 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
411 // Caller data channel should already exist (it created one). Callee data
412 // channel may not exist yet, since negotiation happens in-band, not in SDP.
413 ASSERT_NE(nullptr, caller()->data_channel());
414 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
415 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
416 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
417
418 for (int message_size = 1; message_size < 100000; message_size *= 2) {
419 std::string data(message_size, 'a');
420 caller()->data_channel()->Send(DataBuffer(data));
421 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
422 kDefaultTimeout);
423 callee()->data_channel()->Send(DataBuffer(data));
424 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
425 kDefaultTimeout);
426 }
427 // Specifically probe the area around the MTU size.
428 for (int message_size = 1100; message_size < 1300; message_size += 1) {
429 std::string data(message_size, 'a');
430 caller()->data_channel()->Send(DataBuffer(data));
431 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
432 kDefaultTimeout);
433 callee()->data_channel()->Send(DataBuffer(data));
434 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
435 kDefaultTimeout);
436 }
437}
438
439TEST_P(DataChannelIntegrationTest,
440 EndToEndCallWithSctpDataChannelLowestSafeMtu) {
441 // The lowest payload size limit that's tested and found safe for this
442 // application. Note that this is not the safe limit under all conditions;
443 // in particular, the default is not the largest DTLS signature, and
444 // this test does not use TURN.
445 const size_t kLowestSafePayloadSizeLimit = 1225;
446
447 ASSERT_TRUE(CreatePeerConnectionWrappers());
448 ConnectFakeSignaling();
449 // Expect that data channel created on caller side will show up for callee as
450 // well.
451 caller()->CreateDataChannel();
452 caller()->CreateAndSetAndSignalOffer();
453 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
454 // Caller data channel should already exist (it created one). Callee data
455 // channel may not exist yet, since negotiation happens in-band, not in SDP.
456 ASSERT_NE(nullptr, caller()->data_channel());
457 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
458 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
459 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
460
461 virtual_socket_server()->set_max_udp_payload(kLowestSafePayloadSizeLimit);
462 for (int message_size = 1140; message_size < 1240; message_size += 1) {
463 std::string data(message_size, 'a');
464 caller()->data_channel()->Send(DataBuffer(data));
465 ASSERT_EQ_WAIT(data, callee()->data_observer()->last_message(),
466 kDefaultTimeout);
467 callee()->data_channel()->Send(DataBuffer(data));
468 ASSERT_EQ_WAIT(data, caller()->data_observer()->last_message(),
469 kDefaultTimeout);
470 }
471}
472
473// This test verifies that lowering the MTU of the connection will cause
474// the datachannel to not transmit reliably.
475// The purpose of this test is to ensure that we know how a too-small MTU
476// error manifests itself.
477TEST_P(DataChannelIntegrationTest, EndToEndCallWithSctpDataChannelHarmfulMtu) {
478 // The lowest payload size limit that's tested and found safe for this
479 // application in this configuration (see test above).
480 const size_t kLowestSafePayloadSizeLimit = 1225;
481 // The size of the smallest message that fails to be delivered.
482 const size_t kMessageSizeThatIsNotDelivered = 1157;
483
484 ASSERT_TRUE(CreatePeerConnectionWrappers());
485 ConnectFakeSignaling();
486 caller()->CreateDataChannel();
487 caller()->CreateAndSetAndSignalOffer();
488 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
489 ASSERT_NE(nullptr, caller()->data_channel());
490 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
491 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
492 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
493
494 virtual_socket_server()->set_max_udp_payload(kLowestSafePayloadSizeLimit - 1);
495 // Probe for an undelivered or slowly delivered message. The exact
496 // size limit seems to be dependent on the message history, so make the
497 // code easily able to find the current value.
498 bool failure_seen = false;
499 for (size_t message_size = 1110; message_size < 1400; message_size++) {
500 const size_t message_count =
501 callee()->data_observer()->received_message_count();
502 const std::string data(message_size, 'a');
503 caller()->data_channel()->Send(DataBuffer(data));
504 // Wait a very short time for the message to be delivered.
Harald Alvestrand9d1e0702021-03-16 06:15:01 +0000505 // Note: Waiting only 10 ms is too short for Windows bots; they will
506 // flakily fail at a random frame.
Harald Alvestrand7087b832021-03-11 17:21:13 +0000507 WAIT(callee()->data_observer()->received_message_count() > message_count,
Harald Alvestrand9d1e0702021-03-16 06:15:01 +0000508 100);
Harald Alvestrand7087b832021-03-11 17:21:13 +0000509 if (callee()->data_observer()->received_message_count() == message_count) {
510 ASSERT_EQ(kMessageSizeThatIsNotDelivered, message_size);
511 failure_seen = true;
512 break;
513 }
514 }
515 ASSERT_TRUE(failure_seen);
516}
517
Harald Alvestrand39993842021-02-17 09:05:31 +0000518// Ensure that when the callee closes an SCTP data channel, the closing
519// procedure results in the data channel being closed for the caller as well.
520TEST_P(DataChannelIntegrationTest, CalleeClosesSctpDataChannel) {
521 // Same procedure as above test.
522 ASSERT_TRUE(CreatePeerConnectionWrappers());
523 ConnectFakeSignaling();
524 caller()->CreateDataChannel();
525 caller()->AddAudioVideoTracks();
526 callee()->AddAudioVideoTracks();
527 caller()->CreateAndSetAndSignalOffer();
528 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
529 ASSERT_NE(nullptr, caller()->data_channel());
530 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
531 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
532 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
533
534 // Close the data channel on the callee side, and wait for it to reach the
535 // "closed" state on both sides.
536 callee()->data_channel()->Close();
537 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
538 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
539}
540
541TEST_P(DataChannelIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
542 ASSERT_TRUE(CreatePeerConnectionWrappers());
543 ConnectFakeSignaling();
544 webrtc::DataChannelInit init;
545 init.id = 53;
546 init.maxRetransmits = 52;
547 caller()->CreateDataChannel("data-channel", &init);
548 caller()->AddAudioVideoTracks();
549 callee()->AddAudioVideoTracks();
550 caller()->CreateAndSetAndSignalOffer();
551 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
552 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
553 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
554 // Since "negotiated" is false, the "id" parameter should be ignored.
555 EXPECT_NE(init.id, callee()->data_channel()->id());
556 EXPECT_EQ("data-channel", callee()->data_channel()->label());
557 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
558 EXPECT_FALSE(callee()->data_channel()->negotiated());
559}
560
561// Test usrsctp's ability to process unordered data stream, where data actually
562// arrives out of order using simulated delays. Previously there have been some
563// bugs in this area.
564TEST_P(DataChannelIntegrationTest, StressTestUnorderedSctpDataChannel) {
565 // Introduce random network delays.
566 // Otherwise it's not a true "unordered" test.
567 virtual_socket_server()->set_delay_mean(20);
568 virtual_socket_server()->set_delay_stddev(5);
569 virtual_socket_server()->UpdateDelayDistribution();
570 // Normal procedure, but with unordered data channel config.
571 ASSERT_TRUE(CreatePeerConnectionWrappers());
572 ConnectFakeSignaling();
573 webrtc::DataChannelInit init;
574 init.ordered = false;
575 caller()->CreateDataChannel(&init);
576 caller()->CreateAndSetAndSignalOffer();
577 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
578 ASSERT_NE(nullptr, caller()->data_channel());
579 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
580 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
581 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
582
583 static constexpr int kNumMessages = 100;
584 // Deliberately chosen to be larger than the MTU so messages get fragmented.
585 static constexpr size_t kMaxMessageSize = 4096;
586 // Create and send random messages.
587 std::vector<std::string> sent_messages;
588 for (int i = 0; i < kNumMessages; ++i) {
589 size_t length =
590 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
591 std::string message;
592 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
593 caller()->data_channel()->Send(DataBuffer(message));
594 callee()->data_channel()->Send(DataBuffer(message));
595 sent_messages.push_back(message);
596 }
597
598 // Wait for all messages to be received.
599 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
600 caller()->data_observer()->received_message_count(),
601 kDefaultTimeout);
602 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
603 callee()->data_observer()->received_message_count(),
604 kDefaultTimeout);
605
606 // Sort and compare to make sure none of the messages were corrupted.
607 std::vector<std::string> caller_received_messages =
608 caller()->data_observer()->messages();
609 std::vector<std::string> callee_received_messages =
610 callee()->data_observer()->messages();
611 absl::c_sort(sent_messages);
612 absl::c_sort(caller_received_messages);
613 absl::c_sort(callee_received_messages);
614 EXPECT_EQ(sent_messages, caller_received_messages);
615 EXPECT_EQ(sent_messages, callee_received_messages);
616}
617
618// This test sets up a call between two parties with audio, and video. When
619// audio and video are setup and flowing, an SCTP data channel is negotiated.
620TEST_P(DataChannelIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
621 ASSERT_TRUE(CreatePeerConnectionWrappers());
622 ConnectFakeSignaling();
623 // Do initial offer/answer with audio/video.
624 caller()->AddAudioVideoTracks();
625 callee()->AddAudioVideoTracks();
626 caller()->CreateAndSetAndSignalOffer();
627 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
628 // Create data channel and do new offer and answer.
629 caller()->CreateDataChannel();
630 caller()->CreateAndSetAndSignalOffer();
631 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
632 // Caller data channel should already exist (it created one). Callee data
633 // channel may not exist yet, since negotiation happens in-band, not in SDP.
634 ASSERT_NE(nullptr, caller()->data_channel());
635 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
636 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
637 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
638 // Ensure data can be sent in both directions.
639 std::string data = "hello world";
640 caller()->data_channel()->Send(DataBuffer(data));
641 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
642 kDefaultTimeout);
643 callee()->data_channel()->Send(DataBuffer(data));
644 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
645 kDefaultTimeout);
646}
647
648// Set up a connection initially just using SCTP data channels, later upgrading
649// to audio/video, ensuring frames are received end-to-end. Effectively the
650// inverse of the test above.
651// This was broken in M57; see https://crbug.com/711243
652TEST_P(DataChannelIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
653 ASSERT_TRUE(CreatePeerConnectionWrappers());
654 ConnectFakeSignaling();
655 // Do initial offer/answer with just data channel.
656 caller()->CreateDataChannel();
657 caller()->CreateAndSetAndSignalOffer();
658 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
659 // Wait until data can be sent over the data channel.
660 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
661 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
662 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
663
664 // Do subsequent offer/answer with two-way audio and video. Audio and video
665 // should end up bundled on the DTLS/ICE transport already used for data.
666 caller()->AddAudioVideoTracks();
667 callee()->AddAudioVideoTracks();
668 caller()->CreateAndSetAndSignalOffer();
669 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
670 MediaExpectations media_expectations;
671 media_expectations.ExpectBidirectionalAudioAndVideo();
672 ASSERT_TRUE(ExpectNewFrames(media_expectations));
673}
674
675static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
676 cricket::SctpDataContentDescription* dcd_offer =
677 GetFirstSctpDataContentDescription(desc);
678 // See https://crbug.com/webrtc/11211 - this function is a no-op
679 ASSERT_TRUE(dcd_offer);
680 dcd_offer->set_use_sctpmap(false);
681 dcd_offer->set_protocol("UDP/DTLS/SCTP");
682}
683
684// Test that the data channel works when a spec-compliant SCTP m= section is
685// offered (using "a=sctp-port" instead of "a=sctpmap", and using
686// "UDP/DTLS/SCTP" as the protocol).
687TEST_P(DataChannelIntegrationTest,
688 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
689 ASSERT_TRUE(CreatePeerConnectionWrappers());
690 ConnectFakeSignaling();
691 caller()->CreateDataChannel();
692 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
693 caller()->CreateAndSetAndSignalOffer();
694 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
695 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
696 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
697 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
698
699 // Ensure data can be sent in both directions.
700 std::string data = "hello world";
701 caller()->data_channel()->Send(DataBuffer(data));
702 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
703 kDefaultTimeout);
704 callee()->data_channel()->Send(DataBuffer(data));
705 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
706 kDefaultTimeout);
707}
708
709#endif // WEBRTC_HAVE_SCTP
710
711// Test that after closing PeerConnections, they stop sending any packets (ICE,
712// DTLS, RTP...).
713TEST_P(DataChannelIntegrationTest, ClosingConnectionStopsPacketFlow) {
714 // Set up audio/video/data, wait for some frames to be received.
715 ASSERT_TRUE(CreatePeerConnectionWrappers());
716 ConnectFakeSignaling();
717 caller()->AddAudioVideoTracks();
718#ifdef WEBRTC_HAVE_SCTP
719 caller()->CreateDataChannel();
720#endif
721 caller()->CreateAndSetAndSignalOffer();
722 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
723 MediaExpectations media_expectations;
724 media_expectations.CalleeExpectsSomeAudioAndVideo();
725 ASSERT_TRUE(ExpectNewFrames(media_expectations));
726 // Close PeerConnections.
727 ClosePeerConnections();
728 // Pump messages for a second, and ensure no new packets end up sent.
729 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
730 WAIT(false, 1000);
731 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
732 EXPECT_EQ(sent_packets_a, sent_packets_b);
733}
734
735// Test that transport stats are generated by the RTCStatsCollector for a
736// connection that only involves data channels. This is a regression test for
737// crbug.com/826972.
738#ifdef WEBRTC_HAVE_SCTP
739TEST_P(DataChannelIntegrationTest,
740 TransportStatsReportedForDataChannelOnlyConnection) {
741 ASSERT_TRUE(CreatePeerConnectionWrappers());
742 ConnectFakeSignaling();
743 caller()->CreateDataChannel();
744
745 caller()->CreateAndSetAndSignalOffer();
746 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
747 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
748
749 auto caller_report = caller()->NewGetStats();
750 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
751 auto callee_report = callee()->NewGetStats();
752 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
753}
754
755INSTANTIATE_TEST_SUITE_P(DataChannelIntegrationTest,
756 DataChannelIntegrationTest,
757 Values(SdpSemantics::kPlanB,
758 SdpSemantics::kUnifiedPlan));
759
760INSTANTIATE_TEST_SUITE_P(DataChannelIntegrationTest,
761 DataChannelIntegrationTestWithFakeClock,
762 Values(SdpSemantics::kPlanB,
763 SdpSemantics::kUnifiedPlan));
764
765TEST_F(DataChannelIntegrationTestUnifiedPlan,
766 EndToEndCallWithBundledSctpDataChannel) {
767 ASSERT_TRUE(CreatePeerConnectionWrappers());
768 ConnectFakeSignaling();
769 caller()->CreateDataChannel();
770 caller()->AddAudioVideoTracks();
771 callee()->AddAudioVideoTracks();
772 caller()->CreateAndSetAndSignalOffer();
773 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
774 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
775 ASSERT_EQ_WAIT(SctpTransportState::kConnected,
776 caller()->pc()->GetSctpTransport()->Information().state(),
777 kDefaultTimeout);
778 });
779 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
780 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
781}
782
783TEST_F(DataChannelIntegrationTestUnifiedPlan,
784 EndToEndCallWithDataChannelOnlyConnects) {
785 ASSERT_TRUE(CreatePeerConnectionWrappers());
786 ConnectFakeSignaling();
787 caller()->CreateDataChannel();
788 caller()->CreateAndSetAndSignalOffer();
789 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
790 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
791 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
792 ASSERT_TRUE(caller()->data_observer()->IsOpen());
793}
794
795TEST_F(DataChannelIntegrationTestUnifiedPlan, DataChannelClosesWhenClosed) {
796 ASSERT_TRUE(CreatePeerConnectionWrappers());
797 ConnectFakeSignaling();
798 caller()->CreateDataChannel();
799 caller()->CreateAndSetAndSignalOffer();
800 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
801 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
802 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
803 caller()->data_channel()->Close();
804 ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
805}
806
807TEST_F(DataChannelIntegrationTestUnifiedPlan,
808 DataChannelClosesWhenClosedReverse) {
809 ASSERT_TRUE(CreatePeerConnectionWrappers());
810 ConnectFakeSignaling();
811 caller()->CreateDataChannel();
812 caller()->CreateAndSetAndSignalOffer();
813 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
814 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
815 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
816 callee()->data_channel()->Close();
817 ASSERT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
818}
819
820TEST_F(DataChannelIntegrationTestUnifiedPlan,
821 DataChannelClosesWhenPeerConnectionClosed) {
822 ASSERT_TRUE(CreatePeerConnectionWrappers());
823 ConnectFakeSignaling();
824 caller()->CreateDataChannel();
825 caller()->CreateAndSetAndSignalOffer();
826 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
827 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
828 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
829 caller()->pc()->Close();
830 ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
831}
832
833#endif // WEBRTC_HAVE_SCTP
834
835} // namespace
836
837} // namespace webrtc