blob: d4a259b608d5007825445e30a48f1dae8f2cbe16 [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.
505 WAIT(callee()->data_observer()->received_message_count() > message_count,
506 10);
507 if (callee()->data_observer()->received_message_count() == message_count) {
508 ASSERT_EQ(kMessageSizeThatIsNotDelivered, message_size);
509 failure_seen = true;
510 break;
511 }
512 }
513 ASSERT_TRUE(failure_seen);
514}
515
Harald Alvestrand39993842021-02-17 09:05:31 +0000516// Ensure that when the callee closes an SCTP data channel, the closing
517// procedure results in the data channel being closed for the caller as well.
518TEST_P(DataChannelIntegrationTest, CalleeClosesSctpDataChannel) {
519 // Same procedure as above test.
520 ASSERT_TRUE(CreatePeerConnectionWrappers());
521 ConnectFakeSignaling();
522 caller()->CreateDataChannel();
523 caller()->AddAudioVideoTracks();
524 callee()->AddAudioVideoTracks();
525 caller()->CreateAndSetAndSignalOffer();
526 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
527 ASSERT_NE(nullptr, caller()->data_channel());
528 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
529 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
530 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
531
532 // Close the data channel on the callee side, and wait for it to reach the
533 // "closed" state on both sides.
534 callee()->data_channel()->Close();
535 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
536 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
537}
538
539TEST_P(DataChannelIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
540 ASSERT_TRUE(CreatePeerConnectionWrappers());
541 ConnectFakeSignaling();
542 webrtc::DataChannelInit init;
543 init.id = 53;
544 init.maxRetransmits = 52;
545 caller()->CreateDataChannel("data-channel", &init);
546 caller()->AddAudioVideoTracks();
547 callee()->AddAudioVideoTracks();
548 caller()->CreateAndSetAndSignalOffer();
549 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
550 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
551 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
552 // Since "negotiated" is false, the "id" parameter should be ignored.
553 EXPECT_NE(init.id, callee()->data_channel()->id());
554 EXPECT_EQ("data-channel", callee()->data_channel()->label());
555 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
556 EXPECT_FALSE(callee()->data_channel()->negotiated());
557}
558
559// Test usrsctp's ability to process unordered data stream, where data actually
560// arrives out of order using simulated delays. Previously there have been some
561// bugs in this area.
562TEST_P(DataChannelIntegrationTest, StressTestUnorderedSctpDataChannel) {
563 // Introduce random network delays.
564 // Otherwise it's not a true "unordered" test.
565 virtual_socket_server()->set_delay_mean(20);
566 virtual_socket_server()->set_delay_stddev(5);
567 virtual_socket_server()->UpdateDelayDistribution();
568 // Normal procedure, but with unordered data channel config.
569 ASSERT_TRUE(CreatePeerConnectionWrappers());
570 ConnectFakeSignaling();
571 webrtc::DataChannelInit init;
572 init.ordered = false;
573 caller()->CreateDataChannel(&init);
574 caller()->CreateAndSetAndSignalOffer();
575 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
576 ASSERT_NE(nullptr, caller()->data_channel());
577 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
578 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
579 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
580
581 static constexpr int kNumMessages = 100;
582 // Deliberately chosen to be larger than the MTU so messages get fragmented.
583 static constexpr size_t kMaxMessageSize = 4096;
584 // Create and send random messages.
585 std::vector<std::string> sent_messages;
586 for (int i = 0; i < kNumMessages; ++i) {
587 size_t length =
588 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
589 std::string message;
590 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
591 caller()->data_channel()->Send(DataBuffer(message));
592 callee()->data_channel()->Send(DataBuffer(message));
593 sent_messages.push_back(message);
594 }
595
596 // Wait for all messages to be received.
597 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
598 caller()->data_observer()->received_message_count(),
599 kDefaultTimeout);
600 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
601 callee()->data_observer()->received_message_count(),
602 kDefaultTimeout);
603
604 // Sort and compare to make sure none of the messages were corrupted.
605 std::vector<std::string> caller_received_messages =
606 caller()->data_observer()->messages();
607 std::vector<std::string> callee_received_messages =
608 callee()->data_observer()->messages();
609 absl::c_sort(sent_messages);
610 absl::c_sort(caller_received_messages);
611 absl::c_sort(callee_received_messages);
612 EXPECT_EQ(sent_messages, caller_received_messages);
613 EXPECT_EQ(sent_messages, callee_received_messages);
614}
615
616// This test sets up a call between two parties with audio, and video. When
617// audio and video are setup and flowing, an SCTP data channel is negotiated.
618TEST_P(DataChannelIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
619 ASSERT_TRUE(CreatePeerConnectionWrappers());
620 ConnectFakeSignaling();
621 // Do initial offer/answer with audio/video.
622 caller()->AddAudioVideoTracks();
623 callee()->AddAudioVideoTracks();
624 caller()->CreateAndSetAndSignalOffer();
625 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
626 // Create data channel and do new offer and answer.
627 caller()->CreateDataChannel();
628 caller()->CreateAndSetAndSignalOffer();
629 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
630 // Caller data channel should already exist (it created one). Callee data
631 // channel may not exist yet, since negotiation happens in-band, not in SDP.
632 ASSERT_NE(nullptr, caller()->data_channel());
633 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
634 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
635 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
636 // Ensure data can be sent in both directions.
637 std::string data = "hello world";
638 caller()->data_channel()->Send(DataBuffer(data));
639 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
640 kDefaultTimeout);
641 callee()->data_channel()->Send(DataBuffer(data));
642 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
643 kDefaultTimeout);
644}
645
646// Set up a connection initially just using SCTP data channels, later upgrading
647// to audio/video, ensuring frames are received end-to-end. Effectively the
648// inverse of the test above.
649// This was broken in M57; see https://crbug.com/711243
650TEST_P(DataChannelIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
651 ASSERT_TRUE(CreatePeerConnectionWrappers());
652 ConnectFakeSignaling();
653 // Do initial offer/answer with just data channel.
654 caller()->CreateDataChannel();
655 caller()->CreateAndSetAndSignalOffer();
656 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
657 // Wait until data can be sent over the data channel.
658 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
659 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
660 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
661
662 // Do subsequent offer/answer with two-way audio and video. Audio and video
663 // should end up bundled on the DTLS/ICE transport already used for data.
664 caller()->AddAudioVideoTracks();
665 callee()->AddAudioVideoTracks();
666 caller()->CreateAndSetAndSignalOffer();
667 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
668 MediaExpectations media_expectations;
669 media_expectations.ExpectBidirectionalAudioAndVideo();
670 ASSERT_TRUE(ExpectNewFrames(media_expectations));
671}
672
673static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
674 cricket::SctpDataContentDescription* dcd_offer =
675 GetFirstSctpDataContentDescription(desc);
676 // See https://crbug.com/webrtc/11211 - this function is a no-op
677 ASSERT_TRUE(dcd_offer);
678 dcd_offer->set_use_sctpmap(false);
679 dcd_offer->set_protocol("UDP/DTLS/SCTP");
680}
681
682// Test that the data channel works when a spec-compliant SCTP m= section is
683// offered (using "a=sctp-port" instead of "a=sctpmap", and using
684// "UDP/DTLS/SCTP" as the protocol).
685TEST_P(DataChannelIntegrationTest,
686 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
687 ASSERT_TRUE(CreatePeerConnectionWrappers());
688 ConnectFakeSignaling();
689 caller()->CreateDataChannel();
690 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
691 caller()->CreateAndSetAndSignalOffer();
692 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
693 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
694 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
695 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
696
697 // Ensure data can be sent in both directions.
698 std::string data = "hello world";
699 caller()->data_channel()->Send(DataBuffer(data));
700 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
701 kDefaultTimeout);
702 callee()->data_channel()->Send(DataBuffer(data));
703 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
704 kDefaultTimeout);
705}
706
707#endif // WEBRTC_HAVE_SCTP
708
709// Test that after closing PeerConnections, they stop sending any packets (ICE,
710// DTLS, RTP...).
711TEST_P(DataChannelIntegrationTest, ClosingConnectionStopsPacketFlow) {
712 // Set up audio/video/data, wait for some frames to be received.
713 ASSERT_TRUE(CreatePeerConnectionWrappers());
714 ConnectFakeSignaling();
715 caller()->AddAudioVideoTracks();
716#ifdef WEBRTC_HAVE_SCTP
717 caller()->CreateDataChannel();
718#endif
719 caller()->CreateAndSetAndSignalOffer();
720 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
721 MediaExpectations media_expectations;
722 media_expectations.CalleeExpectsSomeAudioAndVideo();
723 ASSERT_TRUE(ExpectNewFrames(media_expectations));
724 // Close PeerConnections.
725 ClosePeerConnections();
726 // Pump messages for a second, and ensure no new packets end up sent.
727 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
728 WAIT(false, 1000);
729 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
730 EXPECT_EQ(sent_packets_a, sent_packets_b);
731}
732
733// Test that transport stats are generated by the RTCStatsCollector for a
734// connection that only involves data channels. This is a regression test for
735// crbug.com/826972.
736#ifdef WEBRTC_HAVE_SCTP
737TEST_P(DataChannelIntegrationTest,
738 TransportStatsReportedForDataChannelOnlyConnection) {
739 ASSERT_TRUE(CreatePeerConnectionWrappers());
740 ConnectFakeSignaling();
741 caller()->CreateDataChannel();
742
743 caller()->CreateAndSetAndSignalOffer();
744 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
745 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
746
747 auto caller_report = caller()->NewGetStats();
748 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
749 auto callee_report = callee()->NewGetStats();
750 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
751}
752
753INSTANTIATE_TEST_SUITE_P(DataChannelIntegrationTest,
754 DataChannelIntegrationTest,
755 Values(SdpSemantics::kPlanB,
756 SdpSemantics::kUnifiedPlan));
757
758INSTANTIATE_TEST_SUITE_P(DataChannelIntegrationTest,
759 DataChannelIntegrationTestWithFakeClock,
760 Values(SdpSemantics::kPlanB,
761 SdpSemantics::kUnifiedPlan));
762
763TEST_F(DataChannelIntegrationTestUnifiedPlan,
764 EndToEndCallWithBundledSctpDataChannel) {
765 ASSERT_TRUE(CreatePeerConnectionWrappers());
766 ConnectFakeSignaling();
767 caller()->CreateDataChannel();
768 caller()->AddAudioVideoTracks();
769 callee()->AddAudioVideoTracks();
770 caller()->CreateAndSetAndSignalOffer();
771 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
772 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
773 ASSERT_EQ_WAIT(SctpTransportState::kConnected,
774 caller()->pc()->GetSctpTransport()->Information().state(),
775 kDefaultTimeout);
776 });
777 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
778 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
779}
780
781TEST_F(DataChannelIntegrationTestUnifiedPlan,
782 EndToEndCallWithDataChannelOnlyConnects) {
783 ASSERT_TRUE(CreatePeerConnectionWrappers());
784 ConnectFakeSignaling();
785 caller()->CreateDataChannel();
786 caller()->CreateAndSetAndSignalOffer();
787 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
788 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
789 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
790 ASSERT_TRUE(caller()->data_observer()->IsOpen());
791}
792
793TEST_F(DataChannelIntegrationTestUnifiedPlan, DataChannelClosesWhenClosed) {
794 ASSERT_TRUE(CreatePeerConnectionWrappers());
795 ConnectFakeSignaling();
796 caller()->CreateDataChannel();
797 caller()->CreateAndSetAndSignalOffer();
798 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
799 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
800 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
801 caller()->data_channel()->Close();
802 ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
803}
804
805TEST_F(DataChannelIntegrationTestUnifiedPlan,
806 DataChannelClosesWhenClosedReverse) {
807 ASSERT_TRUE(CreatePeerConnectionWrappers());
808 ConnectFakeSignaling();
809 caller()->CreateDataChannel();
810 caller()->CreateAndSetAndSignalOffer();
811 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
812 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
813 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
814 callee()->data_channel()->Close();
815 ASSERT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
816}
817
818TEST_F(DataChannelIntegrationTestUnifiedPlan,
819 DataChannelClosesWhenPeerConnectionClosed) {
820 ASSERT_TRUE(CreatePeerConnectionWrappers());
821 ConnectFakeSignaling();
822 caller()->CreateDataChannel();
823 caller()->CreateAndSetAndSignalOffer();
824 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
825 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
826 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
827 caller()->pc()->Close();
828 ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
829}
830
831#endif // WEBRTC_HAVE_SCTP
832
833} // namespace
834
835} // namespace webrtc