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