blob: 0b697266973f690809369db1e81a5d1fe5c01f64 [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
Harald Alvestrand39993842021-02-17 09:05:31 +000086#ifdef WEBRTC_HAVE_SCTP
87
88// This test causes a PeerConnection to enter Disconnected state, and
89// sends data on a DataChannel while disconnected.
90// The data should be surfaced when the connection reestablishes.
91TEST_P(DataChannelIntegrationTest, DataChannelWhileDisconnected) {
92 CreatePeerConnectionWrappers();
93 ConnectFakeSignaling();
94 caller()->CreateDataChannel();
95 caller()->CreateAndSetAndSignalOffer();
96 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
97 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
98 std::string data1 = "hello first";
99 caller()->data_channel()->Send(DataBuffer(data1));
100 EXPECT_EQ_WAIT(data1, callee()->data_observer()->last_message(),
101 kDefaultTimeout);
102 // Cause a network outage
103 virtual_socket_server()->set_drop_probability(1.0);
104 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
105 caller()->standardized_ice_connection_state(),
106 kDefaultTimeout);
107 std::string data2 = "hello second";
108 caller()->data_channel()->Send(DataBuffer(data2));
109 // Remove the network outage. The connection should reestablish.
110 virtual_socket_server()->set_drop_probability(0.0);
111 EXPECT_EQ_WAIT(data2, callee()->data_observer()->last_message(),
112 kDefaultTimeout);
113}
114
115// This test causes a PeerConnection to enter Disconnected state,
116// sends data on a DataChannel while disconnected, and then triggers
117// an ICE restart.
118// The data should be surfaced when the connection reestablishes.
119TEST_P(DataChannelIntegrationTest, DataChannelWhileDisconnectedIceRestart) {
120 CreatePeerConnectionWrappers();
121 ConnectFakeSignaling();
122 caller()->CreateDataChannel();
123 caller()->CreateAndSetAndSignalOffer();
124 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
125 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
126 std::string data1 = "hello first";
127 caller()->data_channel()->Send(DataBuffer(data1));
128 EXPECT_EQ_WAIT(data1, callee()->data_observer()->last_message(),
129 kDefaultTimeout);
130 // Cause a network outage
131 virtual_socket_server()->set_drop_probability(1.0);
132 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
133 caller()->standardized_ice_connection_state(),
134 kDefaultTimeout);
135 std::string data2 = "hello second";
136 caller()->data_channel()->Send(DataBuffer(data2));
137
138 // Trigger an ICE restart. The signaling channel is not affected by
139 // the network outage.
140 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
141 caller()->CreateAndSetAndSignalOffer();
142 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
143 // Remove the network outage. The connection should reestablish.
144 virtual_socket_server()->set_drop_probability(0.0);
145 EXPECT_EQ_WAIT(data2, callee()->data_observer()->last_message(),
146 kDefaultTimeout);
147}
148
Harald Alvestrand39993842021-02-17 09:05:31 +0000149// This test sets up a call between two parties with audio, video and an SCTP
150// data channel.
151TEST_P(DataChannelIntegrationTest, EndToEndCallWithSctpDataChannel) {
152 ASSERT_TRUE(CreatePeerConnectionWrappers());
153 ConnectFakeSignaling();
154 // Expect that data channel created on caller side will show up for callee as
155 // well.
156 caller()->CreateDataChannel();
157 caller()->AddAudioVideoTracks();
158 callee()->AddAudioVideoTracks();
159 caller()->CreateAndSetAndSignalOffer();
160 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
161 // Ensure the existence of the SCTP data channel didn't impede audio/video.
162 MediaExpectations media_expectations;
163 media_expectations.ExpectBidirectionalAudioAndVideo();
164 ASSERT_TRUE(ExpectNewFrames(media_expectations));
165 // Caller data channel should already exist (it created one). Callee data
166 // channel may not exist yet, since negotiation happens in-band, not in SDP.
167 ASSERT_NE(nullptr, caller()->data_channel());
168 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
169 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
170 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
171
172 // Ensure data can be sent in both directions.
173 std::string data = "hello world";
174 caller()->data_channel()->Send(DataBuffer(data));
175 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
176 kDefaultTimeout);
177 callee()->data_channel()->Send(DataBuffer(data));
178 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
179 kDefaultTimeout);
180}
181
Harald Alvestrand7087b832021-03-11 17:21:13 +0000182// This test sets up a call between two parties with an SCTP
183// data channel only, and sends messages of various sizes.
184TEST_P(DataChannelIntegrationTest,
185 EndToEndCallWithSctpDataChannelVariousSizes) {
186 ASSERT_TRUE(CreatePeerConnectionWrappers());
187 ConnectFakeSignaling();
188 // Expect that data channel created on caller side will show up for callee as
189 // well.
190 caller()->CreateDataChannel();
191 caller()->CreateAndSetAndSignalOffer();
192 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
193 // Caller data channel should already exist (it created one). Callee data
194 // channel may not exist yet, since negotiation happens in-band, not in SDP.
195 ASSERT_NE(nullptr, caller()->data_channel());
196 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
197 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
198 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
199
200 for (int message_size = 1; message_size < 100000; message_size *= 2) {
201 std::string data(message_size, 'a');
202 caller()->data_channel()->Send(DataBuffer(data));
203 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
204 kDefaultTimeout);
205 callee()->data_channel()->Send(DataBuffer(data));
206 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
207 kDefaultTimeout);
208 }
209 // Specifically probe the area around the MTU size.
210 for (int message_size = 1100; message_size < 1300; message_size += 1) {
211 std::string data(message_size, 'a');
212 caller()->data_channel()->Send(DataBuffer(data));
213 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
214 kDefaultTimeout);
215 callee()->data_channel()->Send(DataBuffer(data));
216 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
217 kDefaultTimeout);
218 }
219}
220
221TEST_P(DataChannelIntegrationTest,
222 EndToEndCallWithSctpDataChannelLowestSafeMtu) {
223 // The lowest payload size limit that's tested and found safe for this
224 // application. Note that this is not the safe limit under all conditions;
225 // in particular, the default is not the largest DTLS signature, and
226 // this test does not use TURN.
227 const size_t kLowestSafePayloadSizeLimit = 1225;
228
229 ASSERT_TRUE(CreatePeerConnectionWrappers());
230 ConnectFakeSignaling();
231 // Expect that data channel created on caller side will show up for callee as
232 // well.
233 caller()->CreateDataChannel();
234 caller()->CreateAndSetAndSignalOffer();
235 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
236 // Caller data channel should already exist (it created one). Callee data
237 // channel may not exist yet, since negotiation happens in-band, not in SDP.
238 ASSERT_NE(nullptr, caller()->data_channel());
239 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
240 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
241 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
242
243 virtual_socket_server()->set_max_udp_payload(kLowestSafePayloadSizeLimit);
244 for (int message_size = 1140; message_size < 1240; message_size += 1) {
245 std::string data(message_size, 'a');
246 caller()->data_channel()->Send(DataBuffer(data));
247 ASSERT_EQ_WAIT(data, callee()->data_observer()->last_message(),
248 kDefaultTimeout);
249 callee()->data_channel()->Send(DataBuffer(data));
250 ASSERT_EQ_WAIT(data, caller()->data_observer()->last_message(),
251 kDefaultTimeout);
252 }
253}
254
255// This test verifies that lowering the MTU of the connection will cause
256// the datachannel to not transmit reliably.
257// The purpose of this test is to ensure that we know how a too-small MTU
258// error manifests itself.
259TEST_P(DataChannelIntegrationTest, EndToEndCallWithSctpDataChannelHarmfulMtu) {
260 // The lowest payload size limit that's tested and found safe for this
261 // application in this configuration (see test above).
262 const size_t kLowestSafePayloadSizeLimit = 1225;
263 // The size of the smallest message that fails to be delivered.
264 const size_t kMessageSizeThatIsNotDelivered = 1157;
265
266 ASSERT_TRUE(CreatePeerConnectionWrappers());
267 ConnectFakeSignaling();
268 caller()->CreateDataChannel();
269 caller()->CreateAndSetAndSignalOffer();
270 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
271 ASSERT_NE(nullptr, caller()->data_channel());
272 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
273 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
274 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
275
276 virtual_socket_server()->set_max_udp_payload(kLowestSafePayloadSizeLimit - 1);
277 // Probe for an undelivered or slowly delivered message. The exact
278 // size limit seems to be dependent on the message history, so make the
279 // code easily able to find the current value.
280 bool failure_seen = false;
281 for (size_t message_size = 1110; message_size < 1400; message_size++) {
282 const size_t message_count =
283 callee()->data_observer()->received_message_count();
284 const std::string data(message_size, 'a');
285 caller()->data_channel()->Send(DataBuffer(data));
286 // Wait a very short time for the message to be delivered.
Harald Alvestrand9d1e0702021-03-16 06:15:01 +0000287 // Note: Waiting only 10 ms is too short for Windows bots; they will
288 // flakily fail at a random frame.
Harald Alvestrand7087b832021-03-11 17:21:13 +0000289 WAIT(callee()->data_observer()->received_message_count() > message_count,
Harald Alvestrand9d1e0702021-03-16 06:15:01 +0000290 100);
Harald Alvestrand7087b832021-03-11 17:21:13 +0000291 if (callee()->data_observer()->received_message_count() == message_count) {
292 ASSERT_EQ(kMessageSizeThatIsNotDelivered, message_size);
293 failure_seen = true;
294 break;
295 }
296 }
297 ASSERT_TRUE(failure_seen);
298}
299
Harald Alvestrand39993842021-02-17 09:05:31 +0000300// Ensure that when the callee closes an SCTP data channel, the closing
301// procedure results in the data channel being closed for the caller as well.
302TEST_P(DataChannelIntegrationTest, CalleeClosesSctpDataChannel) {
303 // Same procedure as above test.
304 ASSERT_TRUE(CreatePeerConnectionWrappers());
305 ConnectFakeSignaling();
306 caller()->CreateDataChannel();
307 caller()->AddAudioVideoTracks();
308 callee()->AddAudioVideoTracks();
309 caller()->CreateAndSetAndSignalOffer();
310 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
311 ASSERT_NE(nullptr, caller()->data_channel());
312 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
313 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
314 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
315
316 // Close the data channel on the callee side, and wait for it to reach the
317 // "closed" state on both sides.
318 callee()->data_channel()->Close();
319 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
320 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
321}
322
323TEST_P(DataChannelIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
324 ASSERT_TRUE(CreatePeerConnectionWrappers());
325 ConnectFakeSignaling();
326 webrtc::DataChannelInit init;
327 init.id = 53;
328 init.maxRetransmits = 52;
329 caller()->CreateDataChannel("data-channel", &init);
330 caller()->AddAudioVideoTracks();
331 callee()->AddAudioVideoTracks();
332 caller()->CreateAndSetAndSignalOffer();
333 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
334 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
335 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
336 // Since "negotiated" is false, the "id" parameter should be ignored.
337 EXPECT_NE(init.id, callee()->data_channel()->id());
338 EXPECT_EQ("data-channel", callee()->data_channel()->label());
339 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
340 EXPECT_FALSE(callee()->data_channel()->negotiated());
341}
342
343// Test usrsctp's ability to process unordered data stream, where data actually
344// arrives out of order using simulated delays. Previously there have been some
345// bugs in this area.
346TEST_P(DataChannelIntegrationTest, StressTestUnorderedSctpDataChannel) {
347 // Introduce random network delays.
348 // Otherwise it's not a true "unordered" test.
349 virtual_socket_server()->set_delay_mean(20);
350 virtual_socket_server()->set_delay_stddev(5);
351 virtual_socket_server()->UpdateDelayDistribution();
352 // Normal procedure, but with unordered data channel config.
353 ASSERT_TRUE(CreatePeerConnectionWrappers());
354 ConnectFakeSignaling();
355 webrtc::DataChannelInit init;
356 init.ordered = false;
357 caller()->CreateDataChannel(&init);
358 caller()->CreateAndSetAndSignalOffer();
359 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
360 ASSERT_NE(nullptr, caller()->data_channel());
361 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
362 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
363 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
364
365 static constexpr int kNumMessages = 100;
366 // Deliberately chosen to be larger than the MTU so messages get fragmented.
367 static constexpr size_t kMaxMessageSize = 4096;
368 // Create and send random messages.
369 std::vector<std::string> sent_messages;
370 for (int i = 0; i < kNumMessages; ++i) {
371 size_t length =
372 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
373 std::string message;
374 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
375 caller()->data_channel()->Send(DataBuffer(message));
376 callee()->data_channel()->Send(DataBuffer(message));
377 sent_messages.push_back(message);
378 }
379
380 // Wait for all messages to be received.
381 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
382 caller()->data_observer()->received_message_count(),
383 kDefaultTimeout);
384 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
385 callee()->data_observer()->received_message_count(),
386 kDefaultTimeout);
387
388 // Sort and compare to make sure none of the messages were corrupted.
389 std::vector<std::string> caller_received_messages =
390 caller()->data_observer()->messages();
391 std::vector<std::string> callee_received_messages =
392 callee()->data_observer()->messages();
393 absl::c_sort(sent_messages);
394 absl::c_sort(caller_received_messages);
395 absl::c_sort(callee_received_messages);
396 EXPECT_EQ(sent_messages, caller_received_messages);
397 EXPECT_EQ(sent_messages, callee_received_messages);
398}
399
400// This test sets up a call between two parties with audio, and video. When
401// audio and video are setup and flowing, an SCTP data channel is negotiated.
402TEST_P(DataChannelIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
403 ASSERT_TRUE(CreatePeerConnectionWrappers());
404 ConnectFakeSignaling();
405 // Do initial offer/answer with audio/video.
406 caller()->AddAudioVideoTracks();
407 callee()->AddAudioVideoTracks();
408 caller()->CreateAndSetAndSignalOffer();
409 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
410 // Create data channel and do new offer and answer.
411 caller()->CreateDataChannel();
412 caller()->CreateAndSetAndSignalOffer();
413 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
414 // Caller data channel should already exist (it created one). Callee data
415 // channel may not exist yet, since negotiation happens in-band, not in SDP.
416 ASSERT_NE(nullptr, caller()->data_channel());
417 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
418 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
419 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
420 // Ensure data can be sent in both directions.
421 std::string data = "hello world";
422 caller()->data_channel()->Send(DataBuffer(data));
423 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
424 kDefaultTimeout);
425 callee()->data_channel()->Send(DataBuffer(data));
426 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
427 kDefaultTimeout);
428}
429
430// Set up a connection initially just using SCTP data channels, later upgrading
431// to audio/video, ensuring frames are received end-to-end. Effectively the
432// inverse of the test above.
433// This was broken in M57; see https://crbug.com/711243
434TEST_P(DataChannelIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
435 ASSERT_TRUE(CreatePeerConnectionWrappers());
436 ConnectFakeSignaling();
437 // Do initial offer/answer with just data channel.
438 caller()->CreateDataChannel();
439 caller()->CreateAndSetAndSignalOffer();
440 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
441 // Wait until data can be sent over the data channel.
442 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
443 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
444 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
445
446 // Do subsequent offer/answer with two-way audio and video. Audio and video
447 // should end up bundled on the DTLS/ICE transport already used for data.
448 caller()->AddAudioVideoTracks();
449 callee()->AddAudioVideoTracks();
450 caller()->CreateAndSetAndSignalOffer();
451 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
452 MediaExpectations media_expectations;
453 media_expectations.ExpectBidirectionalAudioAndVideo();
454 ASSERT_TRUE(ExpectNewFrames(media_expectations));
455}
456
457static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
458 cricket::SctpDataContentDescription* dcd_offer =
459 GetFirstSctpDataContentDescription(desc);
460 // See https://crbug.com/webrtc/11211 - this function is a no-op
461 ASSERT_TRUE(dcd_offer);
462 dcd_offer->set_use_sctpmap(false);
463 dcd_offer->set_protocol("UDP/DTLS/SCTP");
464}
465
466// Test that the data channel works when a spec-compliant SCTP m= section is
467// offered (using "a=sctp-port" instead of "a=sctpmap", and using
468// "UDP/DTLS/SCTP" as the protocol).
469TEST_P(DataChannelIntegrationTest,
470 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
471 ASSERT_TRUE(CreatePeerConnectionWrappers());
472 ConnectFakeSignaling();
473 caller()->CreateDataChannel();
474 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
475 caller()->CreateAndSetAndSignalOffer();
476 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
477 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
478 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
479 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
480
481 // Ensure data can be sent in both directions.
482 std::string data = "hello world";
483 caller()->data_channel()->Send(DataBuffer(data));
484 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
485 kDefaultTimeout);
486 callee()->data_channel()->Send(DataBuffer(data));
487 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
488 kDefaultTimeout);
489}
490
491#endif // WEBRTC_HAVE_SCTP
492
493// Test that after closing PeerConnections, they stop sending any packets (ICE,
494// DTLS, RTP...).
495TEST_P(DataChannelIntegrationTest, ClosingConnectionStopsPacketFlow) {
496 // Set up audio/video/data, wait for some frames to be received.
497 ASSERT_TRUE(CreatePeerConnectionWrappers());
498 ConnectFakeSignaling();
499 caller()->AddAudioVideoTracks();
500#ifdef WEBRTC_HAVE_SCTP
501 caller()->CreateDataChannel();
502#endif
503 caller()->CreateAndSetAndSignalOffer();
504 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
505 MediaExpectations media_expectations;
506 media_expectations.CalleeExpectsSomeAudioAndVideo();
507 ASSERT_TRUE(ExpectNewFrames(media_expectations));
508 // Close PeerConnections.
509 ClosePeerConnections();
510 // Pump messages for a second, and ensure no new packets end up sent.
511 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
512 WAIT(false, 1000);
513 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
514 EXPECT_EQ(sent_packets_a, sent_packets_b);
515}
516
517// Test that transport stats are generated by the RTCStatsCollector for a
518// connection that only involves data channels. This is a regression test for
519// crbug.com/826972.
520#ifdef WEBRTC_HAVE_SCTP
521TEST_P(DataChannelIntegrationTest,
522 TransportStatsReportedForDataChannelOnlyConnection) {
523 ASSERT_TRUE(CreatePeerConnectionWrappers());
524 ConnectFakeSignaling();
525 caller()->CreateDataChannel();
526
527 caller()->CreateAndSetAndSignalOffer();
528 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
529 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
530
531 auto caller_report = caller()->NewGetStats();
532 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
533 auto callee_report = callee()->NewGetStats();
534 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
535}
536
537INSTANTIATE_TEST_SUITE_P(DataChannelIntegrationTest,
538 DataChannelIntegrationTest,
539 Values(SdpSemantics::kPlanB,
540 SdpSemantics::kUnifiedPlan));
541
542INSTANTIATE_TEST_SUITE_P(DataChannelIntegrationTest,
543 DataChannelIntegrationTestWithFakeClock,
544 Values(SdpSemantics::kPlanB,
545 SdpSemantics::kUnifiedPlan));
546
547TEST_F(DataChannelIntegrationTestUnifiedPlan,
548 EndToEndCallWithBundledSctpDataChannel) {
549 ASSERT_TRUE(CreatePeerConnectionWrappers());
550 ConnectFakeSignaling();
551 caller()->CreateDataChannel();
552 caller()->AddAudioVideoTracks();
553 callee()->AddAudioVideoTracks();
554 caller()->CreateAndSetAndSignalOffer();
555 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Harald Alvestrand7af57c62021-04-16 11:12:14 +0000556 ASSERT_TRUE_WAIT(caller()->pc()->GetSctpTransport(), kDefaultTimeout);
557 ASSERT_EQ_WAIT(SctpTransportState::kConnected,
558 caller()->pc()->GetSctpTransport()->Information().state(),
559 kDefaultTimeout);
Harald Alvestrand39993842021-02-17 09:05:31 +0000560 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
561 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
562}
563
564TEST_F(DataChannelIntegrationTestUnifiedPlan,
565 EndToEndCallWithDataChannelOnlyConnects) {
566 ASSERT_TRUE(CreatePeerConnectionWrappers());
567 ConnectFakeSignaling();
568 caller()->CreateDataChannel();
569 caller()->CreateAndSetAndSignalOffer();
570 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
571 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
572 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
573 ASSERT_TRUE(caller()->data_observer()->IsOpen());
574}
575
576TEST_F(DataChannelIntegrationTestUnifiedPlan, DataChannelClosesWhenClosed) {
577 ASSERT_TRUE(CreatePeerConnectionWrappers());
578 ConnectFakeSignaling();
579 caller()->CreateDataChannel();
580 caller()->CreateAndSetAndSignalOffer();
581 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
582 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
583 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
584 caller()->data_channel()->Close();
585 ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
586}
587
588TEST_F(DataChannelIntegrationTestUnifiedPlan,
589 DataChannelClosesWhenClosedReverse) {
590 ASSERT_TRUE(CreatePeerConnectionWrappers());
591 ConnectFakeSignaling();
592 caller()->CreateDataChannel();
593 caller()->CreateAndSetAndSignalOffer();
594 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
595 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
596 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
597 callee()->data_channel()->Close();
598 ASSERT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
599}
600
601TEST_F(DataChannelIntegrationTestUnifiedPlan,
602 DataChannelClosesWhenPeerConnectionClosed) {
603 ASSERT_TRUE(CreatePeerConnectionWrappers());
604 ConnectFakeSignaling();
605 caller()->CreateDataChannel();
606 caller()->CreateAndSetAndSignalOffer();
607 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
608 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
609 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
610 caller()->pc()->Close();
611 ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
612}
613
614#endif // WEBRTC_HAVE_SCTP
615
616} // namespace
617
618} // namespace webrtc