blob: ea66adaf086c35093ab4784fea3843c8dcdd394c [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
Harald Alvestrandc24a2182022-02-23 13:44:59 +000013#include <cstdlib>
14#include <iterator>
Harald Alvestrand39993842021-02-17 09:05:31 +000015#include <string>
Harald Alvestrandc24a2182022-02-23 13:44:59 +000016#include <tuple>
Harald Alvestrand39993842021-02-17 09:05:31 +000017#include <vector>
18
Harald Alvestrandc24a2182022-02-23 13:44:59 +000019#include "absl/algorithm/container.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000020#include "absl/types/optional.h"
21#include "api/data_channel_interface.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000022#include "api/dtls_transport_interface.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000023#include "api/peer_connection_interface.h"
24#include "api/scoped_refptr.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000025#include "api/sctp_transport_interface.h"
26#include "api/stats/rtc_stats_report.h"
27#include "api/stats/rtcstats_objects.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000028#include "api/units/time_delta.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000029#include "p2p/base/transport_description.h"
30#include "p2p/base/transport_info.h"
31#include "pc/media_session.h"
32#include "pc/session_description.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000033#include "pc/test/integration_test_helpers.h"
34#include "pc/test/mock_peer_connection_observers.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000035#include "rtc_base/copy_on_write_buffer.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000036#include "rtc_base/fake_clock.h"
37#include "rtc_base/gunit.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000038#include "rtc_base/helpers.h"
39#include "rtc_base/logging.h"
40#include "rtc_base/numerics/safe_conversions.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000041#include "rtc_base/virtual_socket_server.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000042#include "test/gmock.h"
Florent Castellia6983c62021-05-06 10:50:07 +020043#include "test/gtest.h"
Harald Alvestrand39993842021-02-17 09:05:31 +000044
45namespace webrtc {
46
47namespace {
48
Harald Alvestrand20f94012021-05-21 07:04:27 +000049// All tests in this file require SCTP support.
50#ifdef WEBRTC_HAVE_SCTP
51
Victor Boiviefe968df2022-02-01 11:26:05 +010052#if defined(WEBRTC_ANDROID)
53// Disable heavy tests running on low-end Android devices.
54#define DISABLED_ON_ANDROID(t) DISABLED_##t
55#else
56#define DISABLED_ON_ANDROID(t) t
57#endif
58
Florent Castellif2599a72022-03-31 19:15:10 +020059class DataChannelIntegrationTest
60 : public PeerConnectionIntegrationBaseTest,
61 public ::testing::WithParamInterface<SdpSemantics> {
Harald Alvestrand39993842021-02-17 09:05:31 +000062 protected:
63 DataChannelIntegrationTest()
Florent Castellif2599a72022-03-31 19:15:10 +020064 : PeerConnectionIntegrationBaseTest(GetParam()) {}
Harald Alvestrand39993842021-02-17 09:05:31 +000065};
66
67// Fake clock must be set before threads are started to prevent race on
68// Set/GetClockForTesting().
69// To achieve that, multiple inheritance is used as a mixin pattern
70// where order of construction is finely controlled.
71// This also ensures peerconnection is closed before switching back to non-fake
72// clock, avoiding other races and DCHECK failures such as in rtp_sender.cc.
73class FakeClockForTest : public rtc::ScopedFakeClock {
74 protected:
75 FakeClockForTest() {
76 // Some things use a time of "0" as a special value, so we need to start out
77 // the fake clock at a nonzero time.
78 // TODO(deadbeef): Fix this.
79 AdvanceTime(webrtc::TimeDelta::Seconds(1));
80 }
81
82 // Explicit handle.
83 ScopedFakeClock& FakeClock() { return *this; }
84};
85
Harald Alvestrand39993842021-02-17 09:05:31 +000086class DataChannelIntegrationTestPlanB
87 : public PeerConnectionIntegrationBaseTest {
88 protected:
89 DataChannelIntegrationTestPlanB()
Florent Castelli15a38de2022-04-06 00:38:21 +020090 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB_DEPRECATED) {}
Harald Alvestrand39993842021-02-17 09:05:31 +000091};
92
Harald Alvestrand39993842021-02-17 09:05:31 +000093class DataChannelIntegrationTestUnifiedPlan
94 : public PeerConnectionIntegrationBaseTest {
95 protected:
96 DataChannelIntegrationTestUnifiedPlan()
97 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
98};
99
Harald Alvestrand321ec3b2022-02-10 13:42:18 +0000100void MakeActiveSctpOffer(cricket::SessionDescription* desc) {
101 auto& transport_infos = desc->transport_infos();
102 for (auto& transport_info : transport_infos) {
103 transport_info.description.connection_role = cricket::CONNECTIONROLE_ACTIVE;
104 }
105}
106
Harald Alvestrand39993842021-02-17 09:05:31 +0000107// This test causes a PeerConnection to enter Disconnected state, and
108// sends data on a DataChannel while disconnected.
109// The data should be surfaced when the connection reestablishes.
110TEST_P(DataChannelIntegrationTest, DataChannelWhileDisconnected) {
111 CreatePeerConnectionWrappers();
112 ConnectFakeSignaling();
113 caller()->CreateDataChannel();
114 caller()->CreateAndSetAndSignalOffer();
115 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
116 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
117 std::string data1 = "hello first";
118 caller()->data_channel()->Send(DataBuffer(data1));
119 EXPECT_EQ_WAIT(data1, callee()->data_observer()->last_message(),
120 kDefaultTimeout);
121 // Cause a network outage
122 virtual_socket_server()->set_drop_probability(1.0);
123 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
124 caller()->standardized_ice_connection_state(),
125 kDefaultTimeout);
126 std::string data2 = "hello second";
127 caller()->data_channel()->Send(DataBuffer(data2));
128 // Remove the network outage. The connection should reestablish.
129 virtual_socket_server()->set_drop_probability(0.0);
130 EXPECT_EQ_WAIT(data2, callee()->data_observer()->last_message(),
131 kDefaultTimeout);
132}
133
134// This test causes a PeerConnection to enter Disconnected state,
135// sends data on a DataChannel while disconnected, and then triggers
136// an ICE restart.
137// The data should be surfaced when the connection reestablishes.
138TEST_P(DataChannelIntegrationTest, DataChannelWhileDisconnectedIceRestart) {
139 CreatePeerConnectionWrappers();
140 ConnectFakeSignaling();
141 caller()->CreateDataChannel();
142 caller()->CreateAndSetAndSignalOffer();
143 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
144 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
145 std::string data1 = "hello first";
146 caller()->data_channel()->Send(DataBuffer(data1));
147 EXPECT_EQ_WAIT(data1, callee()->data_observer()->last_message(),
148 kDefaultTimeout);
149 // Cause a network outage
150 virtual_socket_server()->set_drop_probability(1.0);
151 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
152 caller()->standardized_ice_connection_state(),
153 kDefaultTimeout);
154 std::string data2 = "hello second";
155 caller()->data_channel()->Send(DataBuffer(data2));
156
157 // Trigger an ICE restart. The signaling channel is not affected by
158 // the network outage.
159 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
160 caller()->CreateAndSetAndSignalOffer();
161 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
162 // Remove the network outage. The connection should reestablish.
163 virtual_socket_server()->set_drop_probability(0.0);
164 EXPECT_EQ_WAIT(data2, callee()->data_observer()->last_message(),
165 kDefaultTimeout);
166}
167
Harald Alvestrand39993842021-02-17 09:05:31 +0000168// This test sets up a call between two parties with audio, video and an SCTP
169// data channel.
170TEST_P(DataChannelIntegrationTest, EndToEndCallWithSctpDataChannel) {
171 ASSERT_TRUE(CreatePeerConnectionWrappers());
172 ConnectFakeSignaling();
173 // Expect that data channel created on caller side will show up for callee as
174 // well.
175 caller()->CreateDataChannel();
176 caller()->AddAudioVideoTracks();
177 callee()->AddAudioVideoTracks();
178 caller()->CreateAndSetAndSignalOffer();
179 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
180 // Ensure the existence of the SCTP data channel didn't impede audio/video.
181 MediaExpectations media_expectations;
182 media_expectations.ExpectBidirectionalAudioAndVideo();
183 ASSERT_TRUE(ExpectNewFrames(media_expectations));
184 // Caller data channel should already exist (it created one). Callee data
185 // channel may not exist yet, since negotiation happens in-band, not in SDP.
186 ASSERT_NE(nullptr, caller()->data_channel());
187 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
188 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
189 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
190
191 // Ensure data can be sent in both directions.
192 std::string data = "hello world";
193 caller()->data_channel()->Send(DataBuffer(data));
194 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
195 kDefaultTimeout);
196 callee()->data_channel()->Send(DataBuffer(data));
197 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
198 kDefaultTimeout);
199}
200
Harald Alvestrand7087b832021-03-11 17:21:13 +0000201// This test sets up a call between two parties with an SCTP
202// data channel only, and sends messages of various sizes.
203TEST_P(DataChannelIntegrationTest,
204 EndToEndCallWithSctpDataChannelVariousSizes) {
Harald Alvestrand35ba0c52022-05-05 07:37:41 +0000205 ASSERT_TRUE(CreatePeerConnectionWrappersWithoutMediaEngine());
Harald Alvestrand7087b832021-03-11 17:21:13 +0000206 ConnectFakeSignaling();
207 // Expect that data channel created on caller side will show up for callee as
208 // well.
209 caller()->CreateDataChannel();
210 caller()->CreateAndSetAndSignalOffer();
211 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
212 // Caller data channel should already exist (it created one). Callee data
213 // channel may not exist yet, since negotiation happens in-band, not in SDP.
214 ASSERT_NE(nullptr, caller()->data_channel());
215 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
216 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
217 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
218
219 for (int message_size = 1; message_size < 100000; message_size *= 2) {
220 std::string data(message_size, 'a');
221 caller()->data_channel()->Send(DataBuffer(data));
222 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
223 kDefaultTimeout);
224 callee()->data_channel()->Send(DataBuffer(data));
225 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
226 kDefaultTimeout);
227 }
228 // Specifically probe the area around the MTU size.
229 for (int message_size = 1100; message_size < 1300; message_size += 1) {
230 std::string data(message_size, 'a');
231 caller()->data_channel()->Send(DataBuffer(data));
232 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
233 kDefaultTimeout);
234 callee()->data_channel()->Send(DataBuffer(data));
235 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
236 kDefaultTimeout);
237 }
238}
239
Florent Castelli88f4b332021-04-22 13:32:39 +0200240// This test sets up a call between two parties with an SCTP
241// data channel only, and sends empty messages
242TEST_P(DataChannelIntegrationTest,
243 EndToEndCallWithSctpDataChannelEmptyMessages) {
Harald Alvestrand35ba0c52022-05-05 07:37:41 +0000244 ASSERT_TRUE(CreatePeerConnectionWrappersWithoutMediaEngine());
Florent Castelli88f4b332021-04-22 13:32:39 +0200245 ConnectFakeSignaling();
246 // Expect that data channel created on caller side will show up for callee as
247 // well.
248 caller()->CreateDataChannel();
249 caller()->CreateAndSetAndSignalOffer();
250 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
251 // Caller data channel should already exist (it created one). Callee data
252 // channel may not exist yet, since negotiation happens in-band, not in SDP.
253 ASSERT_NE(nullptr, caller()->data_channel());
254 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
255 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
256 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
257
258 // Ensure data can be sent in both directions.
259 // Sending empty string data
260 std::string data = "";
261 caller()->data_channel()->Send(DataBuffer(data));
262 EXPECT_EQ_WAIT(1u, callee()->data_observer()->received_message_count(),
263 kDefaultTimeout);
264 EXPECT_TRUE(callee()->data_observer()->last_message().empty());
265 EXPECT_FALSE(callee()->data_observer()->messages().back().binary);
266 callee()->data_channel()->Send(DataBuffer(data));
267 EXPECT_EQ_WAIT(1u, caller()->data_observer()->received_message_count(),
268 kDefaultTimeout);
269 EXPECT_TRUE(caller()->data_observer()->last_message().empty());
270 EXPECT_FALSE(caller()->data_observer()->messages().back().binary);
271
272 // Sending empty binary data
273 rtc::CopyOnWriteBuffer empty_buffer;
274 caller()->data_channel()->Send(DataBuffer(empty_buffer, true));
275 EXPECT_EQ_WAIT(2u, callee()->data_observer()->received_message_count(),
276 kDefaultTimeout);
277 EXPECT_TRUE(callee()->data_observer()->last_message().empty());
278 EXPECT_TRUE(callee()->data_observer()->messages().back().binary);
279 callee()->data_channel()->Send(DataBuffer(empty_buffer, true));
280 EXPECT_EQ_WAIT(2u, caller()->data_observer()->received_message_count(),
281 kDefaultTimeout);
282 EXPECT_TRUE(caller()->data_observer()->last_message().empty());
283 EXPECT_TRUE(caller()->data_observer()->messages().back().binary);
284}
285
Harald Alvestrand7087b832021-03-11 17:21:13 +0000286TEST_P(DataChannelIntegrationTest,
287 EndToEndCallWithSctpDataChannelLowestSafeMtu) {
288 // The lowest payload size limit that's tested and found safe for this
289 // application. Note that this is not the safe limit under all conditions;
290 // in particular, the default is not the largest DTLS signature, and
291 // this test does not use TURN.
292 const size_t kLowestSafePayloadSizeLimit = 1225;
293
Harald Alvestrand35ba0c52022-05-05 07:37:41 +0000294 ASSERT_TRUE(CreatePeerConnectionWrappersWithoutMediaEngine());
Harald Alvestrand7087b832021-03-11 17:21:13 +0000295 ConnectFakeSignaling();
296 // Expect that data channel created on caller side will show up for callee as
297 // well.
298 caller()->CreateDataChannel();
299 caller()->CreateAndSetAndSignalOffer();
300 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
301 // Caller data channel should already exist (it created one). Callee data
302 // channel may not exist yet, since negotiation happens in-band, not in SDP.
303 ASSERT_NE(nullptr, caller()->data_channel());
304 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
305 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
306 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
307
308 virtual_socket_server()->set_max_udp_payload(kLowestSafePayloadSizeLimit);
309 for (int message_size = 1140; message_size < 1240; message_size += 1) {
310 std::string data(message_size, 'a');
311 caller()->data_channel()->Send(DataBuffer(data));
312 ASSERT_EQ_WAIT(data, callee()->data_observer()->last_message(),
313 kDefaultTimeout);
314 callee()->data_channel()->Send(DataBuffer(data));
315 ASSERT_EQ_WAIT(data, caller()->data_observer()->last_message(),
316 kDefaultTimeout);
317 }
318}
319
320// This test verifies that lowering the MTU of the connection will cause
321// the datachannel to not transmit reliably.
322// The purpose of this test is to ensure that we know how a too-small MTU
323// error manifests itself.
324TEST_P(DataChannelIntegrationTest, EndToEndCallWithSctpDataChannelHarmfulMtu) {
325 // The lowest payload size limit that's tested and found safe for this
326 // application in this configuration (see test above).
327 const size_t kLowestSafePayloadSizeLimit = 1225;
328 // The size of the smallest message that fails to be delivered.
329 const size_t kMessageSizeThatIsNotDelivered = 1157;
330
Harald Alvestrand35ba0c52022-05-05 07:37:41 +0000331 ASSERT_TRUE(CreatePeerConnectionWrappersWithoutMediaEngine());
Harald Alvestrand7087b832021-03-11 17:21:13 +0000332 ConnectFakeSignaling();
333 caller()->CreateDataChannel();
334 caller()->CreateAndSetAndSignalOffer();
335 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
336 ASSERT_NE(nullptr, caller()->data_channel());
337 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
338 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
339 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
340
341 virtual_socket_server()->set_max_udp_payload(kLowestSafePayloadSizeLimit - 1);
342 // Probe for an undelivered or slowly delivered message. The exact
343 // size limit seems to be dependent on the message history, so make the
344 // code easily able to find the current value.
345 bool failure_seen = false;
346 for (size_t message_size = 1110; message_size < 1400; message_size++) {
347 const size_t message_count =
348 callee()->data_observer()->received_message_count();
349 const std::string data(message_size, 'a');
350 caller()->data_channel()->Send(DataBuffer(data));
351 // Wait a very short time for the message to be delivered.
Harald Alvestrand9d1e0702021-03-16 06:15:01 +0000352 // Note: Waiting only 10 ms is too short for Windows bots; they will
353 // flakily fail at a random frame.
Harald Alvestrand7087b832021-03-11 17:21:13 +0000354 WAIT(callee()->data_observer()->received_message_count() > message_count,
Harald Alvestrand9d1e0702021-03-16 06:15:01 +0000355 100);
Harald Alvestrand7087b832021-03-11 17:21:13 +0000356 if (callee()->data_observer()->received_message_count() == message_count) {
357 ASSERT_EQ(kMessageSizeThatIsNotDelivered, message_size);
358 failure_seen = true;
359 break;
360 }
361 }
362 ASSERT_TRUE(failure_seen);
363}
364
Harald Alvestrand39993842021-02-17 09:05:31 +0000365// Ensure that when the callee closes an SCTP data channel, the closing
366// procedure results in the data channel being closed for the caller as well.
367TEST_P(DataChannelIntegrationTest, CalleeClosesSctpDataChannel) {
368 // Same procedure as above test.
369 ASSERT_TRUE(CreatePeerConnectionWrappers());
370 ConnectFakeSignaling();
371 caller()->CreateDataChannel();
372 caller()->AddAudioVideoTracks();
373 callee()->AddAudioVideoTracks();
374 caller()->CreateAndSetAndSignalOffer();
375 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
376 ASSERT_NE(nullptr, caller()->data_channel());
377 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
378 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
379 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
380
381 // Close the data channel on the callee side, and wait for it to reach the
382 // "closed" state on both sides.
383 callee()->data_channel()->Close();
Florent Castelli141a4de2021-04-29 12:49:25 +0200384
385 DataChannelInterface::DataState expected_states[] = {
386 DataChannelInterface::DataState::kConnecting,
387 DataChannelInterface::DataState::kOpen,
388 DataChannelInterface::DataState::kClosing,
389 DataChannelInterface::DataState::kClosed};
390
391 EXPECT_EQ_WAIT(DataChannelInterface::DataState::kClosed,
392 caller()->data_observer()->state(), kDefaultTimeout);
393 EXPECT_THAT(caller()->data_observer()->states(),
394 ::testing::ElementsAreArray(expected_states));
395
396 EXPECT_EQ_WAIT(DataChannelInterface::DataState::kClosed,
397 callee()->data_observer()->state(), kDefaultTimeout);
398 EXPECT_THAT(callee()->data_observer()->states(),
399 ::testing::ElementsAreArray(expected_states));
Harald Alvestrand39993842021-02-17 09:05:31 +0000400}
401
402TEST_P(DataChannelIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
403 ASSERT_TRUE(CreatePeerConnectionWrappers());
404 ConnectFakeSignaling();
405 webrtc::DataChannelInit init;
406 init.id = 53;
407 init.maxRetransmits = 52;
408 caller()->CreateDataChannel("data-channel", &init);
409 caller()->AddAudioVideoTracks();
410 callee()->AddAudioVideoTracks();
411 caller()->CreateAndSetAndSignalOffer();
412 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
413 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
414 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
415 // Since "negotiated" is false, the "id" parameter should be ignored.
416 EXPECT_NE(init.id, callee()->data_channel()->id());
417 EXPECT_EQ("data-channel", callee()->data_channel()->label());
418 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
419 EXPECT_FALSE(callee()->data_channel()->negotiated());
420}
421
Florent Castellif2599a72022-03-31 19:15:10 +0200422// Test sctp's ability to process unordered data stream, where data actually
Harald Alvestrand39993842021-02-17 09:05:31 +0000423// arrives out of order using simulated delays. Previously there have been some
424// bugs in this area.
425TEST_P(DataChannelIntegrationTest, StressTestUnorderedSctpDataChannel) {
426 // Introduce random network delays.
427 // Otherwise it's not a true "unordered" test.
428 virtual_socket_server()->set_delay_mean(20);
429 virtual_socket_server()->set_delay_stddev(5);
430 virtual_socket_server()->UpdateDelayDistribution();
431 // Normal procedure, but with unordered data channel config.
Harald Alvestrand35ba0c52022-05-05 07:37:41 +0000432 ASSERT_TRUE(CreatePeerConnectionWrappersWithoutMediaEngine());
Harald Alvestrand39993842021-02-17 09:05:31 +0000433 ConnectFakeSignaling();
434 webrtc::DataChannelInit init;
435 init.ordered = false;
436 caller()->CreateDataChannel(&init);
437 caller()->CreateAndSetAndSignalOffer();
438 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
439 ASSERT_NE(nullptr, caller()->data_channel());
440 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
441 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
442 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
443
444 static constexpr int kNumMessages = 100;
445 // Deliberately chosen to be larger than the MTU so messages get fragmented.
446 static constexpr size_t kMaxMessageSize = 4096;
447 // Create and send random messages.
448 std::vector<std::string> sent_messages;
449 for (int i = 0; i < kNumMessages; ++i) {
450 size_t length =
451 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
452 std::string message;
453 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
454 caller()->data_channel()->Send(DataBuffer(message));
455 callee()->data_channel()->Send(DataBuffer(message));
456 sent_messages.push_back(message);
457 }
458
459 // Wait for all messages to be received.
460 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
461 caller()->data_observer()->received_message_count(),
462 kDefaultTimeout);
463 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
464 callee()->data_observer()->received_message_count(),
465 kDefaultTimeout);
466
467 // Sort and compare to make sure none of the messages were corrupted.
Florent Castelli88f4b332021-04-22 13:32:39 +0200468 std::vector<std::string> caller_received_messages;
469 absl::c_transform(caller()->data_observer()->messages(),
470 std::back_inserter(caller_received_messages),
471 [](const auto& a) { return a.data; });
472
473 std::vector<std::string> callee_received_messages;
474 absl::c_transform(callee()->data_observer()->messages(),
475 std::back_inserter(callee_received_messages),
476 [](const auto& a) { return a.data; });
477
Harald Alvestrand39993842021-02-17 09:05:31 +0000478 absl::c_sort(sent_messages);
479 absl::c_sort(caller_received_messages);
480 absl::c_sort(callee_received_messages);
481 EXPECT_EQ(sent_messages, caller_received_messages);
482 EXPECT_EQ(sent_messages, callee_received_messages);
483}
484
Florent Castellid4d97eb2022-05-04 00:28:06 +0200485// Repeatedly open and close data channels on a peer connection to check that
486// the channels are properly negotiated and SCTP stream IDs properly recycled.
487TEST_P(DataChannelIntegrationTest, StressTestOpenCloseChannel) {
488 ASSERT_TRUE(CreatePeerConnectionWrappers());
489 ConnectFakeSignaling();
490
491 int channel_id = 0;
492 const size_t kChannelCount = 8;
493 const size_t kIterations = 10;
494 bool has_negotiated = false;
495
496 webrtc::DataChannelInit init;
497 for (size_t repeats = 0; repeats < kIterations; ++repeats) {
498 RTC_LOG(LS_INFO) << "Iteration " << (repeats + 1) << "/" << kIterations;
499
500 for (size_t i = 0; i < kChannelCount; ++i) {
501 rtc::StringBuilder sb;
502 sb << "channel-" << channel_id++;
503 caller()->CreateDataChannel(sb.Release(), &init);
504 }
505 ASSERT_EQ(caller()->data_channels().size(), kChannelCount);
506
507 if (!has_negotiated) {
508 caller()->CreateAndSetAndSignalOffer();
509 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
510 has_negotiated = true;
511 }
512
513 for (size_t i = 0; i < kChannelCount; ++i) {
514 EXPECT_EQ_WAIT(caller()->data_channels()[i]->state(),
515 DataChannelInterface::DataState::kOpen, kDefaultTimeout);
516 RTC_LOG(LS_INFO) << "Caller Channel "
517 << caller()->data_channels()[i]->label() << " with id "
518 << caller()->data_channels()[i]->id() << " is open.";
519 }
520 ASSERT_EQ_WAIT(callee()->data_channels().size(), kChannelCount,
521 kDefaultTimeout);
522 for (size_t i = 0; i < kChannelCount; ++i) {
523 EXPECT_EQ_WAIT(callee()->data_channels()[i]->state(),
524 DataChannelInterface::DataState::kOpen, kDefaultTimeout);
525 RTC_LOG(LS_INFO) << "Callee Channel "
526 << callee()->data_channels()[i]->label() << " with id "
527 << callee()->data_channels()[i]->id() << " is open.";
528 }
529
530 for (size_t i = 0; i < kChannelCount; ++i) {
531 caller()->data_channels()[i]->Close();
532 }
533
534 for (size_t i = 0; i < kChannelCount; ++i) {
535 EXPECT_EQ_WAIT(caller()->data_channels()[i]->state(),
536 DataChannelInterface::DataState::kClosed, kDefaultTimeout);
537 EXPECT_EQ_WAIT(callee()->data_channels()[i]->state(),
538 DataChannelInterface::DataState::kClosed, kDefaultTimeout);
539 }
540
541 caller()->data_channels().clear();
542 caller()->data_observers().clear();
543 callee()->data_channels().clear();
544 callee()->data_observers().clear();
545 }
546}
547
Harald Alvestrand39993842021-02-17 09:05:31 +0000548// This test sets up a call between two parties with audio, and video. When
549// audio and video are setup and flowing, an SCTP data channel is negotiated.
550TEST_P(DataChannelIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
551 ASSERT_TRUE(CreatePeerConnectionWrappers());
552 ConnectFakeSignaling();
553 // Do initial offer/answer with audio/video.
554 caller()->AddAudioVideoTracks();
555 callee()->AddAudioVideoTracks();
556 caller()->CreateAndSetAndSignalOffer();
557 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
558 // Create data channel and do new offer and answer.
559 caller()->CreateDataChannel();
560 caller()->CreateAndSetAndSignalOffer();
561 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
562 // Caller data channel should already exist (it created one). Callee data
563 // channel may not exist yet, since negotiation happens in-band, not in SDP.
564 ASSERT_NE(nullptr, caller()->data_channel());
565 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
566 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
567 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
568 // Ensure data can be sent in both directions.
569 std::string data = "hello world";
570 caller()->data_channel()->Send(DataBuffer(data));
571 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
572 kDefaultTimeout);
573 callee()->data_channel()->Send(DataBuffer(data));
574 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
575 kDefaultTimeout);
576}
577
Florent Castellid4d97eb2022-05-04 00:28:06 +0200578// Set up a connection initially just using SCTP data channels, later
579// upgrading to audio/video, ensuring frames are received end-to-end.
580// Effectively the inverse of the test above. This was broken in M57; see
581// https://crbug.com/711243
Harald Alvestrand39993842021-02-17 09:05:31 +0000582TEST_P(DataChannelIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
583 ASSERT_TRUE(CreatePeerConnectionWrappers());
584 ConnectFakeSignaling();
585 // Do initial offer/answer with just data channel.
586 caller()->CreateDataChannel();
587 caller()->CreateAndSetAndSignalOffer();
588 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
589 // Wait until data can be sent over the data channel.
590 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
591 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
592 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
593
594 // Do subsequent offer/answer with two-way audio and video. Audio and video
595 // should end up bundled on the DTLS/ICE transport already used for data.
596 caller()->AddAudioVideoTracks();
597 callee()->AddAudioVideoTracks();
598 caller()->CreateAndSetAndSignalOffer();
599 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
600 MediaExpectations media_expectations;
601 media_expectations.ExpectBidirectionalAudioAndVideo();
602 ASSERT_TRUE(ExpectNewFrames(media_expectations));
603}
604
605static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
606 cricket::SctpDataContentDescription* dcd_offer =
607 GetFirstSctpDataContentDescription(desc);
608 // See https://crbug.com/webrtc/11211 - this function is a no-op
609 ASSERT_TRUE(dcd_offer);
610 dcd_offer->set_use_sctpmap(false);
611 dcd_offer->set_protocol("UDP/DTLS/SCTP");
612}
613
614// Test that the data channel works when a spec-compliant SCTP m= section is
615// offered (using "a=sctp-port" instead of "a=sctpmap", and using
616// "UDP/DTLS/SCTP" as the protocol).
617TEST_P(DataChannelIntegrationTest,
618 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
619 ASSERT_TRUE(CreatePeerConnectionWrappers());
620 ConnectFakeSignaling();
621 caller()->CreateDataChannel();
622 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
623 caller()->CreateAndSetAndSignalOffer();
624 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
625 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
626 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
627 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
628
629 // Ensure data can be sent in both directions.
630 std::string data = "hello world";
631 caller()->data_channel()->Send(DataBuffer(data));
632 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
633 kDefaultTimeout);
634 callee()->data_channel()->Send(DataBuffer(data));
635 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
636 kDefaultTimeout);
637}
638
Florent Castellid4d97eb2022-05-04 00:28:06 +0200639// Test that after closing PeerConnections, they stop sending any packets
640// (ICE, DTLS, RTP...).
Harald Alvestrand39993842021-02-17 09:05:31 +0000641TEST_P(DataChannelIntegrationTest, ClosingConnectionStopsPacketFlow) {
642 // Set up audio/video/data, wait for some frames to be received.
643 ASSERT_TRUE(CreatePeerConnectionWrappers());
644 ConnectFakeSignaling();
645 caller()->AddAudioVideoTracks();
Harald Alvestrand39993842021-02-17 09:05:31 +0000646 caller()->CreateDataChannel();
Harald Alvestrand39993842021-02-17 09:05:31 +0000647 caller()->CreateAndSetAndSignalOffer();
648 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
649 MediaExpectations media_expectations;
650 media_expectations.CalleeExpectsSomeAudioAndVideo();
651 ASSERT_TRUE(ExpectNewFrames(media_expectations));
652 // Close PeerConnections.
653 ClosePeerConnections();
654 // Pump messages for a second, and ensure no new packets end up sent.
655 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
656 WAIT(false, 1000);
657 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
658 EXPECT_EQ(sent_packets_a, sent_packets_b);
659}
660
Harald Alvestrand321ec3b2022-02-10 13:42:18 +0000661TEST_P(DataChannelIntegrationTest, DtlsRoleIsSetNormally) {
662 ASSERT_TRUE(CreatePeerConnectionWrappers());
663 ConnectFakeSignaling();
664 caller()->CreateDataChannel();
665 ASSERT_FALSE(caller()->pc()->GetSctpTransport());
666 caller()->CreateAndSetAndSignalOffer();
667 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
668 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
669 ASSERT_TRUE(caller()->pc()->GetSctpTransport());
670 ASSERT_TRUE(
671 caller()->pc()->GetSctpTransport()->Information().dtls_transport());
672 EXPECT_TRUE(caller()
673 ->pc()
674 ->GetSctpTransport()
675 ->Information()
676 .dtls_transport()
677 ->Information()
678 .role());
679 EXPECT_EQ(caller()
680 ->pc()
681 ->GetSctpTransport()
682 ->Information()
683 .dtls_transport()
684 ->Information()
685 .role(),
686 DtlsTransportTlsRole::kServer);
687 EXPECT_EQ(callee()
688 ->pc()
689 ->GetSctpTransport()
690 ->Information()
691 .dtls_transport()
692 ->Information()
693 .role(),
694 DtlsTransportTlsRole::kClient);
Florent Castellid4d97eb2022-05-04 00:28:06 +0200695 // ID should be assigned according to the odd/even rule based on role;
696 // client gets even numbers, server gets odd ones. RFC 8832 section 6.
Harald Alvestrand321ec3b2022-02-10 13:42:18 +0000697 // TODO(hta): Test multiple channels.
698 EXPECT_EQ(caller()->data_channel()->id(), 1);
699}
700
701TEST_P(DataChannelIntegrationTest, DtlsRoleIsSetWhenReversed) {
702 ASSERT_TRUE(CreatePeerConnectionWrappers());
703 ConnectFakeSignaling();
704 caller()->CreateDataChannel();
705 callee()->SetReceivedSdpMunger(MakeActiveSctpOffer);
706 caller()->CreateAndSetAndSignalOffer();
707 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
708 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
709 EXPECT_TRUE(caller()
710 ->pc()
711 ->GetSctpTransport()
712 ->Information()
713 .dtls_transport()
714 ->Information()
715 .role());
716 EXPECT_EQ(caller()
717 ->pc()
718 ->GetSctpTransport()
719 ->Information()
720 .dtls_transport()
721 ->Information()
722 .role(),
723 DtlsTransportTlsRole::kClient);
724 EXPECT_EQ(callee()
725 ->pc()
726 ->GetSctpTransport()
727 ->Information()
728 .dtls_transport()
729 ->Information()
730 .role(),
731 DtlsTransportTlsRole::kServer);
Florent Castellid4d97eb2022-05-04 00:28:06 +0200732 // ID should be assigned according to the odd/even rule based on role;
733 // client gets even numbers, server gets odd ones. RFC 8832 section 6.
Harald Alvestrand321ec3b2022-02-10 13:42:18 +0000734 // TODO(hta): Test multiple channels.
735 EXPECT_EQ(caller()->data_channel()->id(), 0);
736}
737
Harald Alvestrand06c87a12022-02-11 13:12:16 +0000738TEST_P(DataChannelIntegrationTest,
739 DtlsRoleIsSetWhenReversedWithChannelCollision) {
740 ASSERT_TRUE(CreatePeerConnectionWrappers());
741 ConnectFakeSignaling();
742 caller()->CreateDataChannel();
743
744 callee()->SetReceivedSdpMunger([this](cricket::SessionDescription* desc) {
745 MakeActiveSctpOffer(desc);
746 callee()->CreateDataChannel();
747 });
748 caller()->CreateAndSetAndSignalOffer();
749 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
750 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
751 ASSERT_EQ_WAIT(callee()->data_channels().size(), 2U, kDefaultTimeout);
752 ASSERT_EQ_WAIT(caller()->data_channels().size(), 2U, kDefaultTimeout);
753 EXPECT_TRUE(caller()
754 ->pc()
755 ->GetSctpTransport()
756 ->Information()
757 .dtls_transport()
758 ->Information()
759 .role());
760 EXPECT_EQ(caller()
761 ->pc()
762 ->GetSctpTransport()
763 ->Information()
764 .dtls_transport()
765 ->Information()
766 .role(),
767 DtlsTransportTlsRole::kClient);
768 EXPECT_EQ(callee()
769 ->pc()
770 ->GetSctpTransport()
771 ->Information()
772 .dtls_transport()
773 ->Information()
774 .role(),
775 DtlsTransportTlsRole::kServer);
Florent Castellid4d97eb2022-05-04 00:28:06 +0200776 // ID should be assigned according to the odd/even rule based on role;
777 // client gets even numbers, server gets odd ones. RFC 8832 section 6.
Harald Alvestrand06c87a12022-02-11 13:12:16 +0000778 ASSERT_EQ(caller()->data_channels().size(), 2U);
779 ASSERT_EQ(callee()->data_channels().size(), 2U);
780 EXPECT_EQ(caller()->data_channels()[0]->id(), 0);
781 EXPECT_EQ(caller()->data_channels()[1]->id(), 1);
782 EXPECT_EQ(callee()->data_channels()[0]->id(), 1);
783 EXPECT_EQ(callee()->data_channels()[1]->id(), 0);
784}
785
Harald Alvestrand39993842021-02-17 09:05:31 +0000786// Test that transport stats are generated by the RTCStatsCollector for a
787// connection that only involves data channels. This is a regression test for
788// crbug.com/826972.
Harald Alvestrand39993842021-02-17 09:05:31 +0000789TEST_P(DataChannelIntegrationTest,
790 TransportStatsReportedForDataChannelOnlyConnection) {
791 ASSERT_TRUE(CreatePeerConnectionWrappers());
792 ConnectFakeSignaling();
793 caller()->CreateDataChannel();
794
795 caller()->CreateAndSetAndSignalOffer();
796 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
797 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
798
799 auto caller_report = caller()->NewGetStats();
800 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
801 auto callee_report = callee()->NewGetStats();
802 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
803}
804
Harald Alvestrandfeb6eb92021-04-21 18:52:32 +0000805TEST_P(DataChannelIntegrationTest, QueuedPacketsGetDeliveredInReliableMode) {
806 CreatePeerConnectionWrappers();
807 ConnectFakeSignaling();
808 caller()->CreateDataChannel();
809 caller()->CreateAndSetAndSignalOffer();
810 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
811 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
812
813 caller()->data_channel()->Send(DataBuffer("hello first"));
814 ASSERT_EQ_WAIT(1u, callee()->data_observer()->received_message_count(),
815 kDefaultTimeout);
816 // Cause a temporary network outage
817 virtual_socket_server()->set_drop_probability(1.0);
818 for (int i = 1; i <= 10; i++) {
819 caller()->data_channel()->Send(DataBuffer("Sent while blocked"));
820 }
821 // Nothing should be delivered during outage. Short wait.
822 EXPECT_EQ_WAIT(1u, callee()->data_observer()->received_message_count(), 10);
823 // Reverse outage
824 virtual_socket_server()->set_drop_probability(0.0);
825 // All packets should be delivered.
826 EXPECT_EQ_WAIT(11u, callee()->data_observer()->received_message_count(),
827 kDefaultTimeout);
828}
829
Harald Alvestrand86bd92f2021-05-19 16:17:04 +0000830TEST_P(DataChannelIntegrationTest, QueuedPacketsGetDroppedInUnreliableMode) {
Harald Alvestrandfeb6eb92021-04-21 18:52:32 +0000831 CreatePeerConnectionWrappers();
832 ConnectFakeSignaling();
833 DataChannelInit init;
834 init.maxRetransmits = 0;
835 init.ordered = false;
836 caller()->CreateDataChannel(&init);
837 caller()->CreateAndSetAndSignalOffer();
838 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
839 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
840 caller()->data_channel()->Send(DataBuffer("hello first"));
841 ASSERT_EQ_WAIT(1u, callee()->data_observer()->received_message_count(),
842 kDefaultTimeout);
843 // Cause a temporary network outage
844 virtual_socket_server()->set_drop_probability(1.0);
Harald Alvestrand86bd92f2021-05-19 16:17:04 +0000845 // Send a few packets. Note that all get dropped only when all packets
846 // fit into the receiver receive window/congestion window, so that they
847 // actually get sent.
Harald Alvestrandfeb6eb92021-04-21 18:52:32 +0000848 for (int i = 1; i <= 10; i++) {
849 caller()->data_channel()->Send(DataBuffer("Sent while blocked"));
850 }
851 // Nothing should be delivered during outage.
852 // We do a short wait to verify that delivery count is still 1.
853 WAIT(false, 10);
854 EXPECT_EQ(1u, callee()->data_observer()->received_message_count());
855 // Reverse the network outage.
856 virtual_socket_server()->set_drop_probability(0.0);
857 // Send a new packet, and wait for it to be delivered.
858 caller()->data_channel()->Send(DataBuffer("After block"));
859 EXPECT_EQ_WAIT("After block", callee()->data_observer()->last_message(),
860 kDefaultTimeout);
861 // Some messages should be lost, but first and last message should have
862 // been delivered.
863 // First, check that the protocol guarantee is preserved.
864 EXPECT_GT(11u, callee()->data_observer()->received_message_count());
865 EXPECT_LE(2u, callee()->data_observer()->received_message_count());
866 // Then, check that observed behavior (lose all messages) has not changed
867 EXPECT_EQ(2u, callee()->data_observer()->received_message_count());
868}
869
Harald Alvestrand86bd92f2021-05-19 16:17:04 +0000870TEST_P(DataChannelIntegrationTest,
871 QueuedPacketsGetDroppedInLifetimeLimitedMode) {
872 CreatePeerConnectionWrappers();
873 ConnectFakeSignaling();
874 DataChannelInit init;
875 init.maxRetransmitTime = 1;
876 init.ordered = false;
877 caller()->CreateDataChannel(&init);
878 caller()->CreateAndSetAndSignalOffer();
879 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
880 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
881 caller()->data_channel()->Send(DataBuffer("hello first"));
882 ASSERT_EQ_WAIT(1u, callee()->data_observer()->received_message_count(),
883 kDefaultTimeout);
884 // Cause a temporary network outage
885 virtual_socket_server()->set_drop_probability(1.0);
886 for (int i = 1; i <= 200; i++) {
887 caller()->data_channel()->Send(DataBuffer("Sent while blocked"));
888 }
889 // Nothing should be delivered during outage.
890 // We do a short wait to verify that delivery count is still 1,
891 // and to make sure max packet lifetime (which is in ms) is exceeded.
892 WAIT(false, 10);
893 EXPECT_EQ(1u, callee()->data_observer()->received_message_count());
894 // Reverse the network outage.
895 virtual_socket_server()->set_drop_probability(0.0);
896 // Send a new packet, and wait for it to be delivered.
897 caller()->data_channel()->Send(DataBuffer("After block"));
898 EXPECT_EQ_WAIT("After block", callee()->data_observer()->last_message(),
899 kDefaultTimeout);
900 // Some messages should be lost, but first and last message should have
901 // been delivered.
902 // First, check that the protocol guarantee is preserved.
903 EXPECT_GT(202u, callee()->data_observer()->received_message_count());
904 EXPECT_LE(2u, callee()->data_observer()->received_message_count());
905 // Then, check that observed behavior (lose some messages) has not changed
Florent Castellif2599a72022-03-31 19:15:10 +0200906 // DcSctp loses all messages. This is correct.
907 EXPECT_EQ(2u, callee()->data_observer()->received_message_count());
Harald Alvestrand86bd92f2021-05-19 16:17:04 +0000908}
909
910TEST_P(DataChannelIntegrationTest,
Victor Boiviefe968df2022-02-01 11:26:05 +0100911 DISABLED_ON_ANDROID(SomeQueuedPacketsGetDroppedInMaxRetransmitsMode)) {
Harald Alvestrand86bd92f2021-05-19 16:17:04 +0000912 CreatePeerConnectionWrappers();
913 ConnectFakeSignaling();
914 DataChannelInit init;
915 init.maxRetransmits = 0;
916 init.ordered = false;
917 caller()->CreateDataChannel(&init);
918 caller()->CreateAndSetAndSignalOffer();
919 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
920 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
921 caller()->data_channel()->Send(DataBuffer("hello first"));
922 ASSERT_EQ_WAIT(1u, callee()->data_observer()->received_message_count(),
923 kDefaultTimeout);
924 // Cause a temporary network outage
925 virtual_socket_server()->set_drop_probability(1.0);
926 // Fill the buffer until queued data starts to build
927 size_t packet_counter = 0;
928 while (caller()->data_channel()->buffered_amount() < 1 &&
929 packet_counter < 10000) {
930 packet_counter++;
931 caller()->data_channel()->Send(DataBuffer("Sent while blocked"));
932 }
933 if (caller()->data_channel()->buffered_amount()) {
934 RTC_LOG(LS_INFO) << "Buffered data after " << packet_counter << " packets";
935 } else {
936 RTC_LOG(LS_INFO) << "No buffered data after " << packet_counter
937 << " packets";
938 }
939 // Nothing should be delivered during outage.
940 // We do a short wait to verify that delivery count is still 1.
941 WAIT(false, 10);
942 EXPECT_EQ(1u, callee()->data_observer()->received_message_count());
943 // Reverse the network outage.
944 virtual_socket_server()->set_drop_probability(0.0);
945 // Send a new packet, and wait for it to be delivered.
946 caller()->data_channel()->Send(DataBuffer("After block"));
947 EXPECT_EQ_WAIT("After block", callee()->data_observer()->last_message(),
948 kDefaultTimeout);
949 // Some messages should be lost, but first and last message should have
950 // been delivered.
951 // Due to the fact that retransmissions are only counted when the packet
952 // goes on the wire, NOT when they are stalled in queue due to
953 // congestion, we expect some of the packets to be delivered, because
954 // congestion prevented them from being sent.
955 // Citation: https://tools.ietf.org/html/rfc7496#section-3.1
956
957 // First, check that the protocol guarantee is preserved.
958 EXPECT_GT(packet_counter,
959 callee()->data_observer()->received_message_count());
960 EXPECT_LE(2u, callee()->data_observer()->received_message_count());
961 // Then, check that observed behavior (lose between 100 and 200 messages)
962 // has not changed.
963 // Usrsctp behavior is different on Android (177) and other platforms (122).
964 // Dcsctp loses 432 packets.
965 EXPECT_GT(2 + packet_counter - 100,
966 callee()->data_observer()->received_message_count());
967 EXPECT_LT(2 + packet_counter - 500,
968 callee()->data_observer()->received_message_count());
969}
970
Florent Castellif2599a72022-03-31 19:15:10 +0200971INSTANTIATE_TEST_SUITE_P(DataChannelIntegrationTest,
972 DataChannelIntegrationTest,
Florent Castelli15a38de2022-04-06 00:38:21 +0200973 Values(SdpSemantics::kPlanB_DEPRECATED,
Florent Castellif2599a72022-03-31 19:15:10 +0200974 SdpSemantics::kUnifiedPlan));
Harald Alvestrand39993842021-02-17 09:05:31 +0000975
Harald Alvestrand39993842021-02-17 09:05:31 +0000976TEST_F(DataChannelIntegrationTestUnifiedPlan,
977 EndToEndCallWithBundledSctpDataChannel) {
978 ASSERT_TRUE(CreatePeerConnectionWrappers());
979 ConnectFakeSignaling();
980 caller()->CreateDataChannel();
981 caller()->AddAudioVideoTracks();
982 callee()->AddAudioVideoTracks();
983 caller()->CreateAndSetAndSignalOffer();
984 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Harald Alvestrand7af57c62021-04-16 11:12:14 +0000985 ASSERT_TRUE_WAIT(caller()->pc()->GetSctpTransport(), kDefaultTimeout);
986 ASSERT_EQ_WAIT(SctpTransportState::kConnected,
987 caller()->pc()->GetSctpTransport()->Information().state(),
988 kDefaultTimeout);
Harald Alvestrand39993842021-02-17 09:05:31 +0000989 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
990 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
991}
992
993TEST_F(DataChannelIntegrationTestUnifiedPlan,
994 EndToEndCallWithDataChannelOnlyConnects) {
995 ASSERT_TRUE(CreatePeerConnectionWrappers());
996 ConnectFakeSignaling();
997 caller()->CreateDataChannel();
998 caller()->CreateAndSetAndSignalOffer();
999 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1000 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
1001 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
1002 ASSERT_TRUE(caller()->data_observer()->IsOpen());
1003}
1004
1005TEST_F(DataChannelIntegrationTestUnifiedPlan, DataChannelClosesWhenClosed) {
1006 ASSERT_TRUE(CreatePeerConnectionWrappers());
1007 ConnectFakeSignaling();
1008 caller()->CreateDataChannel();
1009 caller()->CreateAndSetAndSignalOffer();
1010 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1011 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
1012 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
1013 caller()->data_channel()->Close();
1014 ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
1015}
1016
1017TEST_F(DataChannelIntegrationTestUnifiedPlan,
1018 DataChannelClosesWhenClosedReverse) {
1019 ASSERT_TRUE(CreatePeerConnectionWrappers());
1020 ConnectFakeSignaling();
1021 caller()->CreateDataChannel();
1022 caller()->CreateAndSetAndSignalOffer();
1023 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1024 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
1025 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
1026 callee()->data_channel()->Close();
1027 ASSERT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
1028}
1029
1030TEST_F(DataChannelIntegrationTestUnifiedPlan,
1031 DataChannelClosesWhenPeerConnectionClosed) {
1032 ASSERT_TRUE(CreatePeerConnectionWrappers());
1033 ConnectFakeSignaling();
1034 caller()->CreateDataChannel();
1035 caller()->CreateAndSetAndSignalOffer();
1036 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1037 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
1038 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
1039 caller()->pc()->Close();
1040 ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
1041}
1042
1043#endif // WEBRTC_HAVE_SCTP
1044
1045} // namespace
1046
1047} // namespace webrtc