blob: c70078b0dfcfd593921212ad92f3ed83d18c3a85 [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"
Harald Alvestrand86bd92f2021-05-19 16:17:04 +000030#include "system_wrappers/include/field_trial.h"
Florent Castellia6983c62021-05-06 10:50:07 +020031#include "test/gtest.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000032
33namespace webrtc {
34
35namespace {
36
Harald Alvestrand20f94012021-05-21 07:04:27 +000037// All tests in this file require SCTP support.
38#ifdef WEBRTC_HAVE_SCTP
39
Victor Boiviefe968df2022-02-01 11:26:05 +010040#if defined(WEBRTC_ANDROID)
41// Disable heavy tests running on low-end Android devices.
42#define DISABLED_ON_ANDROID(t) DISABLED_##t
43#else
44#define DISABLED_ON_ANDROID(t) t
45#endif
46
Florent Castellia6983c62021-05-06 10:50:07 +020047class DataChannelIntegrationTest : public PeerConnectionIntegrationBaseTest,
48 public ::testing::WithParamInterface<
49 std::tuple<SdpSemantics, std::string>> {
Harald Alvestrand39993842021-02-17 09:05:31 +000050 protected:
51 DataChannelIntegrationTest()
Florent Castellia6983c62021-05-06 10:50:07 +020052 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam()),
53 std::get<1>(GetParam())) {}
Harald Alvestrand39993842021-02-17 09:05:31 +000054};
55
56// Fake clock must be set before threads are started to prevent race on
57// Set/GetClockForTesting().
58// To achieve that, multiple inheritance is used as a mixin pattern
59// where order of construction is finely controlled.
60// This also ensures peerconnection is closed before switching back to non-fake
61// clock, avoiding other races and DCHECK failures such as in rtp_sender.cc.
62class FakeClockForTest : public rtc::ScopedFakeClock {
63 protected:
64 FakeClockForTest() {
65 // Some things use a time of "0" as a special value, so we need to start out
66 // the fake clock at a nonzero time.
67 // TODO(deadbeef): Fix this.
68 AdvanceTime(webrtc::TimeDelta::Seconds(1));
69 }
70
71 // Explicit handle.
72 ScopedFakeClock& FakeClock() { return *this; }
73};
74
Harald Alvestrand39993842021-02-17 09:05:31 +000075class DataChannelIntegrationTestPlanB
76 : public PeerConnectionIntegrationBaseTest {
77 protected:
78 DataChannelIntegrationTestPlanB()
79 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
80};
81
Harald Alvestrand39993842021-02-17 09:05:31 +000082class DataChannelIntegrationTestUnifiedPlan
83 : public PeerConnectionIntegrationBaseTest {
84 protected:
85 DataChannelIntegrationTestUnifiedPlan()
86 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
87};
88
Harald Alvestrand39993842021-02-17 09:05:31 +000089// This test causes a PeerConnection to enter Disconnected state, and
90// sends data on a DataChannel while disconnected.
91// The data should be surfaced when the connection reestablishes.
92TEST_P(DataChannelIntegrationTest, DataChannelWhileDisconnected) {
93 CreatePeerConnectionWrappers();
94 ConnectFakeSignaling();
95 caller()->CreateDataChannel();
96 caller()->CreateAndSetAndSignalOffer();
97 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
98 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
99 std::string data1 = "hello first";
100 caller()->data_channel()->Send(DataBuffer(data1));
101 EXPECT_EQ_WAIT(data1, callee()->data_observer()->last_message(),
102 kDefaultTimeout);
103 // Cause a network outage
104 virtual_socket_server()->set_drop_probability(1.0);
105 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
106 caller()->standardized_ice_connection_state(),
107 kDefaultTimeout);
108 std::string data2 = "hello second";
109 caller()->data_channel()->Send(DataBuffer(data2));
110 // Remove the network outage. The connection should reestablish.
111 virtual_socket_server()->set_drop_probability(0.0);
112 EXPECT_EQ_WAIT(data2, callee()->data_observer()->last_message(),
113 kDefaultTimeout);
114}
115
116// This test causes a PeerConnection to enter Disconnected state,
117// sends data on a DataChannel while disconnected, and then triggers
118// an ICE restart.
119// The data should be surfaced when the connection reestablishes.
120TEST_P(DataChannelIntegrationTest, DataChannelWhileDisconnectedIceRestart) {
121 CreatePeerConnectionWrappers();
122 ConnectFakeSignaling();
123 caller()->CreateDataChannel();
124 caller()->CreateAndSetAndSignalOffer();
125 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
126 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
127 std::string data1 = "hello first";
128 caller()->data_channel()->Send(DataBuffer(data1));
129 EXPECT_EQ_WAIT(data1, callee()->data_observer()->last_message(),
130 kDefaultTimeout);
131 // Cause a network outage
132 virtual_socket_server()->set_drop_probability(1.0);
133 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
134 caller()->standardized_ice_connection_state(),
135 kDefaultTimeout);
136 std::string data2 = "hello second";
137 caller()->data_channel()->Send(DataBuffer(data2));
138
139 // Trigger an ICE restart. The signaling channel is not affected by
140 // the network outage.
141 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
142 caller()->CreateAndSetAndSignalOffer();
143 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
144 // Remove the network outage. The connection should reestablish.
145 virtual_socket_server()->set_drop_probability(0.0);
146 EXPECT_EQ_WAIT(data2, callee()->data_observer()->last_message(),
147 kDefaultTimeout);
148}
149
Harald Alvestrand39993842021-02-17 09:05:31 +0000150// This test sets up a call between two parties with audio, video and an SCTP
151// data channel.
152TEST_P(DataChannelIntegrationTest, EndToEndCallWithSctpDataChannel) {
153 ASSERT_TRUE(CreatePeerConnectionWrappers());
154 ConnectFakeSignaling();
155 // Expect that data channel created on caller side will show up for callee as
156 // well.
157 caller()->CreateDataChannel();
158 caller()->AddAudioVideoTracks();
159 callee()->AddAudioVideoTracks();
160 caller()->CreateAndSetAndSignalOffer();
161 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
162 // Ensure the existence of the SCTP data channel didn't impede audio/video.
163 MediaExpectations media_expectations;
164 media_expectations.ExpectBidirectionalAudioAndVideo();
165 ASSERT_TRUE(ExpectNewFrames(media_expectations));
166 // Caller data channel should already exist (it created one). Callee data
167 // channel may not exist yet, since negotiation happens in-band, not in SDP.
168 ASSERT_NE(nullptr, caller()->data_channel());
169 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
170 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
171 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
172
173 // Ensure data can be sent in both directions.
174 std::string data = "hello world";
175 caller()->data_channel()->Send(DataBuffer(data));
176 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
177 kDefaultTimeout);
178 callee()->data_channel()->Send(DataBuffer(data));
179 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
180 kDefaultTimeout);
181}
182
Harald Alvestrand7087b832021-03-11 17:21:13 +0000183// This test sets up a call between two parties with an SCTP
184// data channel only, and sends messages of various sizes.
185TEST_P(DataChannelIntegrationTest,
186 EndToEndCallWithSctpDataChannelVariousSizes) {
187 ASSERT_TRUE(CreatePeerConnectionWrappers());
188 ConnectFakeSignaling();
189 // Expect that data channel created on caller side will show up for callee as
190 // well.
191 caller()->CreateDataChannel();
192 caller()->CreateAndSetAndSignalOffer();
193 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
194 // Caller data channel should already exist (it created one). Callee data
195 // channel may not exist yet, since negotiation happens in-band, not in SDP.
196 ASSERT_NE(nullptr, caller()->data_channel());
197 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
198 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
199 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
200
201 for (int message_size = 1; message_size < 100000; message_size *= 2) {
202 std::string data(message_size, 'a');
203 caller()->data_channel()->Send(DataBuffer(data));
204 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
205 kDefaultTimeout);
206 callee()->data_channel()->Send(DataBuffer(data));
207 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
208 kDefaultTimeout);
209 }
210 // Specifically probe the area around the MTU size.
211 for (int message_size = 1100; message_size < 1300; message_size += 1) {
212 std::string data(message_size, 'a');
213 caller()->data_channel()->Send(DataBuffer(data));
214 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
215 kDefaultTimeout);
216 callee()->data_channel()->Send(DataBuffer(data));
217 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
218 kDefaultTimeout);
219 }
220}
221
Florent Castelli88f4b332021-04-22 13:32:39 +0200222// This test sets up a call between two parties with an SCTP
223// data channel only, and sends empty messages
224TEST_P(DataChannelIntegrationTest,
225 EndToEndCallWithSctpDataChannelEmptyMessages) {
226 ASSERT_TRUE(CreatePeerConnectionWrappers());
227 ConnectFakeSignaling();
228 // Expect that data channel created on caller side will show up for callee as
229 // well.
230 caller()->CreateDataChannel();
231 caller()->CreateAndSetAndSignalOffer();
232 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
233 // Caller data channel should already exist (it created one). Callee data
234 // channel may not exist yet, since negotiation happens in-band, not in SDP.
235 ASSERT_NE(nullptr, caller()->data_channel());
236 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
237 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
238 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
239
240 // Ensure data can be sent in both directions.
241 // Sending empty string data
242 std::string data = "";
243 caller()->data_channel()->Send(DataBuffer(data));
244 EXPECT_EQ_WAIT(1u, callee()->data_observer()->received_message_count(),
245 kDefaultTimeout);
246 EXPECT_TRUE(callee()->data_observer()->last_message().empty());
247 EXPECT_FALSE(callee()->data_observer()->messages().back().binary);
248 callee()->data_channel()->Send(DataBuffer(data));
249 EXPECT_EQ_WAIT(1u, caller()->data_observer()->received_message_count(),
250 kDefaultTimeout);
251 EXPECT_TRUE(caller()->data_observer()->last_message().empty());
252 EXPECT_FALSE(caller()->data_observer()->messages().back().binary);
253
254 // Sending empty binary data
255 rtc::CopyOnWriteBuffer empty_buffer;
256 caller()->data_channel()->Send(DataBuffer(empty_buffer, true));
257 EXPECT_EQ_WAIT(2u, callee()->data_observer()->received_message_count(),
258 kDefaultTimeout);
259 EXPECT_TRUE(callee()->data_observer()->last_message().empty());
260 EXPECT_TRUE(callee()->data_observer()->messages().back().binary);
261 callee()->data_channel()->Send(DataBuffer(empty_buffer, true));
262 EXPECT_EQ_WAIT(2u, caller()->data_observer()->received_message_count(),
263 kDefaultTimeout);
264 EXPECT_TRUE(caller()->data_observer()->last_message().empty());
265 EXPECT_TRUE(caller()->data_observer()->messages().back().binary);
266}
267
Harald Alvestrand7087b832021-03-11 17:21:13 +0000268TEST_P(DataChannelIntegrationTest,
269 EndToEndCallWithSctpDataChannelLowestSafeMtu) {
270 // The lowest payload size limit that's tested and found safe for this
271 // application. Note that this is not the safe limit under all conditions;
272 // in particular, the default is not the largest DTLS signature, and
273 // this test does not use TURN.
274 const size_t kLowestSafePayloadSizeLimit = 1225;
275
276 ASSERT_TRUE(CreatePeerConnectionWrappers());
277 ConnectFakeSignaling();
278 // Expect that data channel created on caller side will show up for callee as
279 // well.
280 caller()->CreateDataChannel();
281 caller()->CreateAndSetAndSignalOffer();
282 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
283 // Caller data channel should already exist (it created one). Callee data
284 // channel may not exist yet, since negotiation happens in-band, not in SDP.
285 ASSERT_NE(nullptr, caller()->data_channel());
286 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
287 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
288 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
289
290 virtual_socket_server()->set_max_udp_payload(kLowestSafePayloadSizeLimit);
291 for (int message_size = 1140; message_size < 1240; message_size += 1) {
292 std::string data(message_size, 'a');
293 caller()->data_channel()->Send(DataBuffer(data));
294 ASSERT_EQ_WAIT(data, callee()->data_observer()->last_message(),
295 kDefaultTimeout);
296 callee()->data_channel()->Send(DataBuffer(data));
297 ASSERT_EQ_WAIT(data, caller()->data_observer()->last_message(),
298 kDefaultTimeout);
299 }
300}
301
302// This test verifies that lowering the MTU of the connection will cause
303// the datachannel to not transmit reliably.
304// The purpose of this test is to ensure that we know how a too-small MTU
305// error manifests itself.
306TEST_P(DataChannelIntegrationTest, EndToEndCallWithSctpDataChannelHarmfulMtu) {
307 // The lowest payload size limit that's tested and found safe for this
308 // application in this configuration (see test above).
309 const size_t kLowestSafePayloadSizeLimit = 1225;
310 // The size of the smallest message that fails to be delivered.
311 const size_t kMessageSizeThatIsNotDelivered = 1157;
312
313 ASSERT_TRUE(CreatePeerConnectionWrappers());
314 ConnectFakeSignaling();
315 caller()->CreateDataChannel();
316 caller()->CreateAndSetAndSignalOffer();
317 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
318 ASSERT_NE(nullptr, caller()->data_channel());
319 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
320 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
321 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
322
323 virtual_socket_server()->set_max_udp_payload(kLowestSafePayloadSizeLimit - 1);
324 // Probe for an undelivered or slowly delivered message. The exact
325 // size limit seems to be dependent on the message history, so make the
326 // code easily able to find the current value.
327 bool failure_seen = false;
328 for (size_t message_size = 1110; message_size < 1400; message_size++) {
329 const size_t message_count =
330 callee()->data_observer()->received_message_count();
331 const std::string data(message_size, 'a');
332 caller()->data_channel()->Send(DataBuffer(data));
333 // Wait a very short time for the message to be delivered.
Harald Alvestrand9d1e0702021-03-16 06:15:01 +0000334 // Note: Waiting only 10 ms is too short for Windows bots; they will
335 // flakily fail at a random frame.
Harald Alvestrand7087b832021-03-11 17:21:13 +0000336 WAIT(callee()->data_observer()->received_message_count() > message_count,
Harald Alvestrand9d1e0702021-03-16 06:15:01 +0000337 100);
Harald Alvestrand7087b832021-03-11 17:21:13 +0000338 if (callee()->data_observer()->received_message_count() == message_count) {
339 ASSERT_EQ(kMessageSizeThatIsNotDelivered, message_size);
340 failure_seen = true;
341 break;
342 }
343 }
344 ASSERT_TRUE(failure_seen);
345}
346
Harald Alvestrand39993842021-02-17 09:05:31 +0000347// Ensure that when the callee closes an SCTP data channel, the closing
348// procedure results in the data channel being closed for the caller as well.
349TEST_P(DataChannelIntegrationTest, CalleeClosesSctpDataChannel) {
350 // Same procedure as above test.
351 ASSERT_TRUE(CreatePeerConnectionWrappers());
352 ConnectFakeSignaling();
353 caller()->CreateDataChannel();
354 caller()->AddAudioVideoTracks();
355 callee()->AddAudioVideoTracks();
356 caller()->CreateAndSetAndSignalOffer();
357 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
358 ASSERT_NE(nullptr, caller()->data_channel());
359 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
360 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
361 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
362
363 // Close the data channel on the callee side, and wait for it to reach the
364 // "closed" state on both sides.
365 callee()->data_channel()->Close();
Florent Castelli141a4de2021-04-29 12:49:25 +0200366
367 DataChannelInterface::DataState expected_states[] = {
368 DataChannelInterface::DataState::kConnecting,
369 DataChannelInterface::DataState::kOpen,
370 DataChannelInterface::DataState::kClosing,
371 DataChannelInterface::DataState::kClosed};
372
373 EXPECT_EQ_WAIT(DataChannelInterface::DataState::kClosed,
374 caller()->data_observer()->state(), kDefaultTimeout);
375 EXPECT_THAT(caller()->data_observer()->states(),
376 ::testing::ElementsAreArray(expected_states));
377
378 EXPECT_EQ_WAIT(DataChannelInterface::DataState::kClosed,
379 callee()->data_observer()->state(), kDefaultTimeout);
380 EXPECT_THAT(callee()->data_observer()->states(),
381 ::testing::ElementsAreArray(expected_states));
Harald Alvestrand39993842021-02-17 09:05:31 +0000382}
383
384TEST_P(DataChannelIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
385 ASSERT_TRUE(CreatePeerConnectionWrappers());
386 ConnectFakeSignaling();
387 webrtc::DataChannelInit init;
388 init.id = 53;
389 init.maxRetransmits = 52;
390 caller()->CreateDataChannel("data-channel", &init);
391 caller()->AddAudioVideoTracks();
392 callee()->AddAudioVideoTracks();
393 caller()->CreateAndSetAndSignalOffer();
394 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
395 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
396 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
397 // Since "negotiated" is false, the "id" parameter should be ignored.
398 EXPECT_NE(init.id, callee()->data_channel()->id());
399 EXPECT_EQ("data-channel", callee()->data_channel()->label());
400 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
401 EXPECT_FALSE(callee()->data_channel()->negotiated());
402}
403
404// Test usrsctp's ability to process unordered data stream, where data actually
405// arrives out of order using simulated delays. Previously there have been some
406// bugs in this area.
407TEST_P(DataChannelIntegrationTest, StressTestUnorderedSctpDataChannel) {
408 // Introduce random network delays.
409 // Otherwise it's not a true "unordered" test.
410 virtual_socket_server()->set_delay_mean(20);
411 virtual_socket_server()->set_delay_stddev(5);
412 virtual_socket_server()->UpdateDelayDistribution();
413 // Normal procedure, but with unordered data channel config.
414 ASSERT_TRUE(CreatePeerConnectionWrappers());
415 ConnectFakeSignaling();
416 webrtc::DataChannelInit init;
417 init.ordered = false;
418 caller()->CreateDataChannel(&init);
419 caller()->CreateAndSetAndSignalOffer();
420 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
421 ASSERT_NE(nullptr, caller()->data_channel());
422 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
423 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
424 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
425
426 static constexpr int kNumMessages = 100;
427 // Deliberately chosen to be larger than the MTU so messages get fragmented.
428 static constexpr size_t kMaxMessageSize = 4096;
429 // Create and send random messages.
430 std::vector<std::string> sent_messages;
431 for (int i = 0; i < kNumMessages; ++i) {
432 size_t length =
433 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
434 std::string message;
435 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
436 caller()->data_channel()->Send(DataBuffer(message));
437 callee()->data_channel()->Send(DataBuffer(message));
438 sent_messages.push_back(message);
439 }
440
441 // Wait for all messages to be received.
442 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
443 caller()->data_observer()->received_message_count(),
444 kDefaultTimeout);
445 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
446 callee()->data_observer()->received_message_count(),
447 kDefaultTimeout);
448
449 // Sort and compare to make sure none of the messages were corrupted.
Florent Castelli88f4b332021-04-22 13:32:39 +0200450 std::vector<std::string> caller_received_messages;
451 absl::c_transform(caller()->data_observer()->messages(),
452 std::back_inserter(caller_received_messages),
453 [](const auto& a) { return a.data; });
454
455 std::vector<std::string> callee_received_messages;
456 absl::c_transform(callee()->data_observer()->messages(),
457 std::back_inserter(callee_received_messages),
458 [](const auto& a) { return a.data; });
459
Harald Alvestrand39993842021-02-17 09:05:31 +0000460 absl::c_sort(sent_messages);
461 absl::c_sort(caller_received_messages);
462 absl::c_sort(callee_received_messages);
463 EXPECT_EQ(sent_messages, caller_received_messages);
464 EXPECT_EQ(sent_messages, callee_received_messages);
465}
466
467// This test sets up a call between two parties with audio, and video. When
468// audio and video are setup and flowing, an SCTP data channel is negotiated.
469TEST_P(DataChannelIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
470 ASSERT_TRUE(CreatePeerConnectionWrappers());
471 ConnectFakeSignaling();
472 // Do initial offer/answer with audio/video.
473 caller()->AddAudioVideoTracks();
474 callee()->AddAudioVideoTracks();
475 caller()->CreateAndSetAndSignalOffer();
476 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
477 // Create data channel and do new offer and answer.
478 caller()->CreateDataChannel();
479 caller()->CreateAndSetAndSignalOffer();
480 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
481 // Caller data channel should already exist (it created one). Callee data
482 // channel may not exist yet, since negotiation happens in-band, not in SDP.
483 ASSERT_NE(nullptr, caller()->data_channel());
484 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
485 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
486 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
487 // Ensure data can be sent in both directions.
488 std::string data = "hello world";
489 caller()->data_channel()->Send(DataBuffer(data));
490 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
491 kDefaultTimeout);
492 callee()->data_channel()->Send(DataBuffer(data));
493 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
494 kDefaultTimeout);
495}
496
497// Set up a connection initially just using SCTP data channels, later upgrading
498// to audio/video, ensuring frames are received end-to-end. Effectively the
499// inverse of the test above.
500// This was broken in M57; see https://crbug.com/711243
501TEST_P(DataChannelIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
502 ASSERT_TRUE(CreatePeerConnectionWrappers());
503 ConnectFakeSignaling();
504 // Do initial offer/answer with just data channel.
505 caller()->CreateDataChannel();
506 caller()->CreateAndSetAndSignalOffer();
507 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
508 // Wait until data can be sent over the data channel.
509 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
510 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
511 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
512
513 // Do subsequent offer/answer with two-way audio and video. Audio and video
514 // should end up bundled on the DTLS/ICE transport already used for data.
515 caller()->AddAudioVideoTracks();
516 callee()->AddAudioVideoTracks();
517 caller()->CreateAndSetAndSignalOffer();
518 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
519 MediaExpectations media_expectations;
520 media_expectations.ExpectBidirectionalAudioAndVideo();
521 ASSERT_TRUE(ExpectNewFrames(media_expectations));
522}
523
524static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
525 cricket::SctpDataContentDescription* dcd_offer =
526 GetFirstSctpDataContentDescription(desc);
527 // See https://crbug.com/webrtc/11211 - this function is a no-op
528 ASSERT_TRUE(dcd_offer);
529 dcd_offer->set_use_sctpmap(false);
530 dcd_offer->set_protocol("UDP/DTLS/SCTP");
531}
532
533// Test that the data channel works when a spec-compliant SCTP m= section is
534// offered (using "a=sctp-port" instead of "a=sctpmap", and using
535// "UDP/DTLS/SCTP" as the protocol).
536TEST_P(DataChannelIntegrationTest,
537 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
538 ASSERT_TRUE(CreatePeerConnectionWrappers());
539 ConnectFakeSignaling();
540 caller()->CreateDataChannel();
541 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
542 caller()->CreateAndSetAndSignalOffer();
543 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
544 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
545 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
546 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
547
548 // Ensure data can be sent in both directions.
549 std::string data = "hello world";
550 caller()->data_channel()->Send(DataBuffer(data));
551 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
552 kDefaultTimeout);
553 callee()->data_channel()->Send(DataBuffer(data));
554 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
555 kDefaultTimeout);
556}
557
Harald Alvestrand39993842021-02-17 09:05:31 +0000558// Test that after closing PeerConnections, they stop sending any packets (ICE,
559// DTLS, RTP...).
560TEST_P(DataChannelIntegrationTest, ClosingConnectionStopsPacketFlow) {
561 // Set up audio/video/data, wait for some frames to be received.
562 ASSERT_TRUE(CreatePeerConnectionWrappers());
563 ConnectFakeSignaling();
564 caller()->AddAudioVideoTracks();
Harald Alvestrand39993842021-02-17 09:05:31 +0000565 caller()->CreateDataChannel();
Harald Alvestrand39993842021-02-17 09:05:31 +0000566 caller()->CreateAndSetAndSignalOffer();
567 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
568 MediaExpectations media_expectations;
569 media_expectations.CalleeExpectsSomeAudioAndVideo();
570 ASSERT_TRUE(ExpectNewFrames(media_expectations));
571 // Close PeerConnections.
572 ClosePeerConnections();
573 // Pump messages for a second, and ensure no new packets end up sent.
574 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
575 WAIT(false, 1000);
576 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
577 EXPECT_EQ(sent_packets_a, sent_packets_b);
578}
579
580// Test that transport stats are generated by the RTCStatsCollector for a
581// connection that only involves data channels. This is a regression test for
582// crbug.com/826972.
Harald Alvestrand39993842021-02-17 09:05:31 +0000583TEST_P(DataChannelIntegrationTest,
584 TransportStatsReportedForDataChannelOnlyConnection) {
585 ASSERT_TRUE(CreatePeerConnectionWrappers());
586 ConnectFakeSignaling();
587 caller()->CreateDataChannel();
588
589 caller()->CreateAndSetAndSignalOffer();
590 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
591 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
592
593 auto caller_report = caller()->NewGetStats();
594 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
595 auto callee_report = callee()->NewGetStats();
596 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
597}
598
Harald Alvestrandfeb6eb92021-04-21 18:52:32 +0000599TEST_P(DataChannelIntegrationTest, QueuedPacketsGetDeliveredInReliableMode) {
600 CreatePeerConnectionWrappers();
601 ConnectFakeSignaling();
602 caller()->CreateDataChannel();
603 caller()->CreateAndSetAndSignalOffer();
604 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
605 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
606
607 caller()->data_channel()->Send(DataBuffer("hello first"));
608 ASSERT_EQ_WAIT(1u, callee()->data_observer()->received_message_count(),
609 kDefaultTimeout);
610 // Cause a temporary network outage
611 virtual_socket_server()->set_drop_probability(1.0);
612 for (int i = 1; i <= 10; i++) {
613 caller()->data_channel()->Send(DataBuffer("Sent while blocked"));
614 }
615 // Nothing should be delivered during outage. Short wait.
616 EXPECT_EQ_WAIT(1u, callee()->data_observer()->received_message_count(), 10);
617 // Reverse outage
618 virtual_socket_server()->set_drop_probability(0.0);
619 // All packets should be delivered.
620 EXPECT_EQ_WAIT(11u, callee()->data_observer()->received_message_count(),
621 kDefaultTimeout);
622}
623
Harald Alvestrand86bd92f2021-05-19 16:17:04 +0000624TEST_P(DataChannelIntegrationTest, QueuedPacketsGetDroppedInUnreliableMode) {
Harald Alvestrandfeb6eb92021-04-21 18:52:32 +0000625 CreatePeerConnectionWrappers();
626 ConnectFakeSignaling();
627 DataChannelInit init;
628 init.maxRetransmits = 0;
629 init.ordered = false;
630 caller()->CreateDataChannel(&init);
631 caller()->CreateAndSetAndSignalOffer();
632 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
633 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
634 caller()->data_channel()->Send(DataBuffer("hello first"));
635 ASSERT_EQ_WAIT(1u, callee()->data_observer()->received_message_count(),
636 kDefaultTimeout);
637 // Cause a temporary network outage
638 virtual_socket_server()->set_drop_probability(1.0);
Harald Alvestrand86bd92f2021-05-19 16:17:04 +0000639 // Send a few packets. Note that all get dropped only when all packets
640 // fit into the receiver receive window/congestion window, so that they
641 // actually get sent.
Harald Alvestrandfeb6eb92021-04-21 18:52:32 +0000642 for (int i = 1; i <= 10; i++) {
643 caller()->data_channel()->Send(DataBuffer("Sent while blocked"));
644 }
645 // Nothing should be delivered during outage.
646 // We do a short wait to verify that delivery count is still 1.
647 WAIT(false, 10);
648 EXPECT_EQ(1u, callee()->data_observer()->received_message_count());
649 // Reverse the network outage.
650 virtual_socket_server()->set_drop_probability(0.0);
651 // Send a new packet, and wait for it to be delivered.
652 caller()->data_channel()->Send(DataBuffer("After block"));
653 EXPECT_EQ_WAIT("After block", callee()->data_observer()->last_message(),
654 kDefaultTimeout);
655 // Some messages should be lost, but first and last message should have
656 // been delivered.
657 // First, check that the protocol guarantee is preserved.
658 EXPECT_GT(11u, callee()->data_observer()->received_message_count());
659 EXPECT_LE(2u, callee()->data_observer()->received_message_count());
660 // Then, check that observed behavior (lose all messages) has not changed
661 EXPECT_EQ(2u, callee()->data_observer()->received_message_count());
662}
663
Harald Alvestrand86bd92f2021-05-19 16:17:04 +0000664TEST_P(DataChannelIntegrationTest,
665 QueuedPacketsGetDroppedInLifetimeLimitedMode) {
666 CreatePeerConnectionWrappers();
667 ConnectFakeSignaling();
668 DataChannelInit init;
669 init.maxRetransmitTime = 1;
670 init.ordered = false;
671 caller()->CreateDataChannel(&init);
672 caller()->CreateAndSetAndSignalOffer();
673 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
674 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
675 caller()->data_channel()->Send(DataBuffer("hello first"));
676 ASSERT_EQ_WAIT(1u, callee()->data_observer()->received_message_count(),
677 kDefaultTimeout);
678 // Cause a temporary network outage
679 virtual_socket_server()->set_drop_probability(1.0);
680 for (int i = 1; i <= 200; i++) {
681 caller()->data_channel()->Send(DataBuffer("Sent while blocked"));
682 }
683 // Nothing should be delivered during outage.
684 // We do a short wait to verify that delivery count is still 1,
685 // and to make sure max packet lifetime (which is in ms) is exceeded.
686 WAIT(false, 10);
687 EXPECT_EQ(1u, callee()->data_observer()->received_message_count());
688 // Reverse the network outage.
689 virtual_socket_server()->set_drop_probability(0.0);
690 // Send a new packet, and wait for it to be delivered.
691 caller()->data_channel()->Send(DataBuffer("After block"));
692 EXPECT_EQ_WAIT("After block", callee()->data_observer()->last_message(),
693 kDefaultTimeout);
694 // Some messages should be lost, but first and last message should have
695 // been delivered.
696 // First, check that the protocol guarantee is preserved.
697 EXPECT_GT(202u, callee()->data_observer()->received_message_count());
698 EXPECT_LE(2u, callee()->data_observer()->received_message_count());
699 // Then, check that observed behavior (lose some messages) has not changed
700 if (webrtc::field_trial::IsEnabled("WebRTC-DataChannel-Dcsctp")) {
701 // DcSctp loses all messages. This is correct.
702 EXPECT_EQ(2u, callee()->data_observer()->received_message_count());
703 } else {
704 // Usrsctp loses some messages, but keeps messages not attempted.
705 // THIS IS THE WRONG BEHAVIOR. According to discussion in
706 // https://github.com/sctplab/usrsctp/issues/584, all these packets
707 // should be discarded.
708 // TODO(bugs.webrtc.org/12731): Fix this.
709 EXPECT_EQ(90u, callee()->data_observer()->received_message_count());
710 }
711}
712
713TEST_P(DataChannelIntegrationTest,
Victor Boiviefe968df2022-02-01 11:26:05 +0100714 DISABLED_ON_ANDROID(SomeQueuedPacketsGetDroppedInMaxRetransmitsMode)) {
Harald Alvestrand86bd92f2021-05-19 16:17:04 +0000715 CreatePeerConnectionWrappers();
716 ConnectFakeSignaling();
717 DataChannelInit init;
718 init.maxRetransmits = 0;
719 init.ordered = false;
720 caller()->CreateDataChannel(&init);
721 caller()->CreateAndSetAndSignalOffer();
722 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
723 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
724 caller()->data_channel()->Send(DataBuffer("hello first"));
725 ASSERT_EQ_WAIT(1u, callee()->data_observer()->received_message_count(),
726 kDefaultTimeout);
727 // Cause a temporary network outage
728 virtual_socket_server()->set_drop_probability(1.0);
729 // Fill the buffer until queued data starts to build
730 size_t packet_counter = 0;
731 while (caller()->data_channel()->buffered_amount() < 1 &&
732 packet_counter < 10000) {
733 packet_counter++;
734 caller()->data_channel()->Send(DataBuffer("Sent while blocked"));
735 }
736 if (caller()->data_channel()->buffered_amount()) {
737 RTC_LOG(LS_INFO) << "Buffered data after " << packet_counter << " packets";
738 } else {
739 RTC_LOG(LS_INFO) << "No buffered data after " << packet_counter
740 << " packets";
741 }
742 // Nothing should be delivered during outage.
743 // We do a short wait to verify that delivery count is still 1.
744 WAIT(false, 10);
745 EXPECT_EQ(1u, callee()->data_observer()->received_message_count());
746 // Reverse the network outage.
747 virtual_socket_server()->set_drop_probability(0.0);
748 // Send a new packet, and wait for it to be delivered.
749 caller()->data_channel()->Send(DataBuffer("After block"));
750 EXPECT_EQ_WAIT("After block", callee()->data_observer()->last_message(),
751 kDefaultTimeout);
752 // Some messages should be lost, but first and last message should have
753 // been delivered.
754 // Due to the fact that retransmissions are only counted when the packet
755 // goes on the wire, NOT when they are stalled in queue due to
756 // congestion, we expect some of the packets to be delivered, because
757 // congestion prevented them from being sent.
758 // Citation: https://tools.ietf.org/html/rfc7496#section-3.1
759
760 // First, check that the protocol guarantee is preserved.
761 EXPECT_GT(packet_counter,
762 callee()->data_observer()->received_message_count());
763 EXPECT_LE(2u, callee()->data_observer()->received_message_count());
764 // Then, check that observed behavior (lose between 100 and 200 messages)
765 // has not changed.
766 // Usrsctp behavior is different on Android (177) and other platforms (122).
767 // Dcsctp loses 432 packets.
768 EXPECT_GT(2 + packet_counter - 100,
769 callee()->data_observer()->received_message_count());
770 EXPECT_LT(2 + packet_counter - 500,
771 callee()->data_observer()->received_message_count());
772}
773
Florent Castellia6983c62021-05-06 10:50:07 +0200774INSTANTIATE_TEST_SUITE_P(
775 DataChannelIntegrationTest,
776 DataChannelIntegrationTest,
777 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
778 Values("WebRTC-DataChannel-Dcsctp/Enabled/",
779 "WebRTC-DataChannel-Dcsctp/Disabled/")));
Harald Alvestrand39993842021-02-17 09:05:31 +0000780
Harald Alvestrand39993842021-02-17 09:05:31 +0000781TEST_F(DataChannelIntegrationTestUnifiedPlan,
782 EndToEndCallWithBundledSctpDataChannel) {
783 ASSERT_TRUE(CreatePeerConnectionWrappers());
784 ConnectFakeSignaling();
785 caller()->CreateDataChannel();
786 caller()->AddAudioVideoTracks();
787 callee()->AddAudioVideoTracks();
788 caller()->CreateAndSetAndSignalOffer();
789 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Harald Alvestrand7af57c62021-04-16 11:12:14 +0000790 ASSERT_TRUE_WAIT(caller()->pc()->GetSctpTransport(), kDefaultTimeout);
791 ASSERT_EQ_WAIT(SctpTransportState::kConnected,
792 caller()->pc()->GetSctpTransport()->Information().state(),
793 kDefaultTimeout);
Harald Alvestrand39993842021-02-17 09:05:31 +0000794 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
795 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
796}
797
798TEST_F(DataChannelIntegrationTestUnifiedPlan,
799 EndToEndCallWithDataChannelOnlyConnects) {
800 ASSERT_TRUE(CreatePeerConnectionWrappers());
801 ConnectFakeSignaling();
802 caller()->CreateDataChannel();
803 caller()->CreateAndSetAndSignalOffer();
804 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
805 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
806 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
807 ASSERT_TRUE(caller()->data_observer()->IsOpen());
808}
809
810TEST_F(DataChannelIntegrationTestUnifiedPlan, DataChannelClosesWhenClosed) {
811 ASSERT_TRUE(CreatePeerConnectionWrappers());
812 ConnectFakeSignaling();
813 caller()->CreateDataChannel();
814 caller()->CreateAndSetAndSignalOffer();
815 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
816 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
817 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
818 caller()->data_channel()->Close();
819 ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
820}
821
822TEST_F(DataChannelIntegrationTestUnifiedPlan,
823 DataChannelClosesWhenClosedReverse) {
824 ASSERT_TRUE(CreatePeerConnectionWrappers());
825 ConnectFakeSignaling();
826 caller()->CreateDataChannel();
827 caller()->CreateAndSetAndSignalOffer();
828 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
829 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
830 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
831 callee()->data_channel()->Close();
832 ASSERT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
833}
834
835TEST_F(DataChannelIntegrationTestUnifiedPlan,
836 DataChannelClosesWhenPeerConnectionClosed) {
837 ASSERT_TRUE(CreatePeerConnectionWrappers());
838 ConnectFakeSignaling();
839 caller()->CreateDataChannel();
840 caller()->CreateAndSetAndSignalOffer();
841 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
842 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
843 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
844 caller()->pc()->Close();
845 ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
846}
847
848#endif // WEBRTC_HAVE_SCTP
849
850} // namespace
851
852} // namespace webrtc