blob: e8e845e648bdea58afa5a8afd4b9fcd1036659eb [file] [log] [blame]
Harald Alvestrand39993842021-02-17 09:05:31 +00001/*
2 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include <stdint.h>
12
13#include <algorithm>
14#include <memory>
15#include <string>
16#include <vector>
17
18#include "absl/types/optional.h"
19#include "api/data_channel_interface.h"
20#include "api/dtmf_sender_interface.h"
21#include "api/peer_connection_interface.h"
22#include "api/scoped_refptr.h"
23#include "api/units/time_delta.h"
24#include "pc/test/integration_test_helpers.h"
25#include "pc/test/mock_peer_connection_observers.h"
26#include "rtc_base/fake_clock.h"
27#include "rtc_base/gunit.h"
28#include "rtc_base/ref_counted_object.h"
29#include "rtc_base/virtual_socket_server.h"
30
31namespace webrtc {
32
33namespace {
34
35class DataChannelIntegrationTest
36 : public PeerConnectionIntegrationBaseTest,
37 public ::testing::WithParamInterface<SdpSemantics> {
38 protected:
39 DataChannelIntegrationTest()
40 : PeerConnectionIntegrationBaseTest(GetParam()) {}
41};
42
Bjorn Terelius3208bf12021-03-04 10:53:08 +010043GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(DataChannelIntegrationTest);
44
Harald Alvestrand39993842021-02-17 09:05:31 +000045// Fake clock must be set before threads are started to prevent race on
46// Set/GetClockForTesting().
47// To achieve that, multiple inheritance is used as a mixin pattern
48// where order of construction is finely controlled.
49// This also ensures peerconnection is closed before switching back to non-fake
50// clock, avoiding other races and DCHECK failures such as in rtp_sender.cc.
51class FakeClockForTest : public rtc::ScopedFakeClock {
52 protected:
53 FakeClockForTest() {
54 // Some things use a time of "0" as a special value, so we need to start out
55 // the fake clock at a nonzero time.
56 // TODO(deadbeef): Fix this.
57 AdvanceTime(webrtc::TimeDelta::Seconds(1));
58 }
59
60 // Explicit handle.
61 ScopedFakeClock& FakeClock() { return *this; }
62};
63
64// Ensure FakeClockForTest is constructed first (see class for rationale).
65class DataChannelIntegrationTestWithFakeClock
66 : public FakeClockForTest,
67 public DataChannelIntegrationTest {};
68
69class DataChannelIntegrationTestPlanB
70 : public PeerConnectionIntegrationBaseTest {
71 protected:
72 DataChannelIntegrationTestPlanB()
73 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
74};
75
Bjorn Terelius3208bf12021-03-04 10:53:08 +010076GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(
77 DataChannelIntegrationTestWithFakeClock);
78
Harald Alvestrand39993842021-02-17 09:05:31 +000079class DataChannelIntegrationTestUnifiedPlan
80 : public PeerConnectionIntegrationBaseTest {
81 protected:
82 DataChannelIntegrationTestUnifiedPlan()
83 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
84};
85
Harald Alvestrand39993842021-02-17 09:05:31 +000086#ifdef WEBRTC_HAVE_SCTP
87
88// This test causes a PeerConnection to enter Disconnected state, and
89// sends data on a DataChannel while disconnected.
90// The data should be surfaced when the connection reestablishes.
91TEST_P(DataChannelIntegrationTest, DataChannelWhileDisconnected) {
92 CreatePeerConnectionWrappers();
93 ConnectFakeSignaling();
94 caller()->CreateDataChannel();
95 caller()->CreateAndSetAndSignalOffer();
96 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
97 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
98 std::string data1 = "hello first";
99 caller()->data_channel()->Send(DataBuffer(data1));
100 EXPECT_EQ_WAIT(data1, callee()->data_observer()->last_message(),
101 kDefaultTimeout);
102 // Cause a network outage
103 virtual_socket_server()->set_drop_probability(1.0);
104 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
105 caller()->standardized_ice_connection_state(),
106 kDefaultTimeout);
107 std::string data2 = "hello second";
108 caller()->data_channel()->Send(DataBuffer(data2));
109 // Remove the network outage. The connection should reestablish.
110 virtual_socket_server()->set_drop_probability(0.0);
111 EXPECT_EQ_WAIT(data2, callee()->data_observer()->last_message(),
112 kDefaultTimeout);
113}
114
115// This test causes a PeerConnection to enter Disconnected state,
116// sends data on a DataChannel while disconnected, and then triggers
117// an ICE restart.
118// The data should be surfaced when the connection reestablishes.
119TEST_P(DataChannelIntegrationTest, DataChannelWhileDisconnectedIceRestart) {
120 CreatePeerConnectionWrappers();
121 ConnectFakeSignaling();
122 caller()->CreateDataChannel();
123 caller()->CreateAndSetAndSignalOffer();
124 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
125 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
126 std::string data1 = "hello first";
127 caller()->data_channel()->Send(DataBuffer(data1));
128 EXPECT_EQ_WAIT(data1, callee()->data_observer()->last_message(),
129 kDefaultTimeout);
130 // Cause a network outage
131 virtual_socket_server()->set_drop_probability(1.0);
132 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
133 caller()->standardized_ice_connection_state(),
134 kDefaultTimeout);
135 std::string data2 = "hello second";
136 caller()->data_channel()->Send(DataBuffer(data2));
137
138 // Trigger an ICE restart. The signaling channel is not affected by
139 // the network outage.
140 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
141 caller()->CreateAndSetAndSignalOffer();
142 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
143 // Remove the network outage. The connection should reestablish.
144 virtual_socket_server()->set_drop_probability(0.0);
145 EXPECT_EQ_WAIT(data2, callee()->data_observer()->last_message(),
146 kDefaultTimeout);
147}
148
Harald Alvestrand39993842021-02-17 09:05:31 +0000149// This test sets up a call between two parties with audio, video and an SCTP
150// data channel.
151TEST_P(DataChannelIntegrationTest, EndToEndCallWithSctpDataChannel) {
152 ASSERT_TRUE(CreatePeerConnectionWrappers());
153 ConnectFakeSignaling();
154 // Expect that data channel created on caller side will show up for callee as
155 // well.
156 caller()->CreateDataChannel();
157 caller()->AddAudioVideoTracks();
158 callee()->AddAudioVideoTracks();
159 caller()->CreateAndSetAndSignalOffer();
160 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
161 // Ensure the existence of the SCTP data channel didn't impede audio/video.
162 MediaExpectations media_expectations;
163 media_expectations.ExpectBidirectionalAudioAndVideo();
164 ASSERT_TRUE(ExpectNewFrames(media_expectations));
165 // Caller data channel should already exist (it created one). Callee data
166 // channel may not exist yet, since negotiation happens in-band, not in SDP.
167 ASSERT_NE(nullptr, caller()->data_channel());
168 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
169 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
170 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
171
172 // Ensure data can be sent in both directions.
173 std::string data = "hello world";
174 caller()->data_channel()->Send(DataBuffer(data));
175 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
176 kDefaultTimeout);
177 callee()->data_channel()->Send(DataBuffer(data));
178 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
179 kDefaultTimeout);
180}
181
Harald Alvestrand7087b832021-03-11 17:21:13 +0000182// This test sets up a call between two parties with an SCTP
183// data channel only, and sends messages of various sizes.
184TEST_P(DataChannelIntegrationTest,
185 EndToEndCallWithSctpDataChannelVariousSizes) {
186 ASSERT_TRUE(CreatePeerConnectionWrappers());
187 ConnectFakeSignaling();
188 // Expect that data channel created on caller side will show up for callee as
189 // well.
190 caller()->CreateDataChannel();
191 caller()->CreateAndSetAndSignalOffer();
192 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
193 // Caller data channel should already exist (it created one). Callee data
194 // channel may not exist yet, since negotiation happens in-band, not in SDP.
195 ASSERT_NE(nullptr, caller()->data_channel());
196 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
197 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
198 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
199
200 for (int message_size = 1; message_size < 100000; message_size *= 2) {
201 std::string data(message_size, 'a');
202 caller()->data_channel()->Send(DataBuffer(data));
203 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
204 kDefaultTimeout);
205 callee()->data_channel()->Send(DataBuffer(data));
206 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
207 kDefaultTimeout);
208 }
209 // Specifically probe the area around the MTU size.
210 for (int message_size = 1100; message_size < 1300; message_size += 1) {
211 std::string data(message_size, 'a');
212 caller()->data_channel()->Send(DataBuffer(data));
213 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
214 kDefaultTimeout);
215 callee()->data_channel()->Send(DataBuffer(data));
216 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
217 kDefaultTimeout);
218 }
219}
220
Florent Castelli88f4b332021-04-22 13:32:39 +0200221// This test sets up a call between two parties with an SCTP
222// data channel only, and sends empty messages
223TEST_P(DataChannelIntegrationTest,
224 EndToEndCallWithSctpDataChannelEmptyMessages) {
225 ASSERT_TRUE(CreatePeerConnectionWrappers());
226 ConnectFakeSignaling();
227 // Expect that data channel created on caller side will show up for callee as
228 // well.
229 caller()->CreateDataChannel();
230 caller()->CreateAndSetAndSignalOffer();
231 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
232 // Caller data channel should already exist (it created one). Callee data
233 // channel may not exist yet, since negotiation happens in-band, not in SDP.
234 ASSERT_NE(nullptr, caller()->data_channel());
235 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
236 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
237 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
238
239 // Ensure data can be sent in both directions.
240 // Sending empty string data
241 std::string data = "";
242 caller()->data_channel()->Send(DataBuffer(data));
243 EXPECT_EQ_WAIT(1u, callee()->data_observer()->received_message_count(),
244 kDefaultTimeout);
245 EXPECT_TRUE(callee()->data_observer()->last_message().empty());
246 EXPECT_FALSE(callee()->data_observer()->messages().back().binary);
247 callee()->data_channel()->Send(DataBuffer(data));
248 EXPECT_EQ_WAIT(1u, caller()->data_observer()->received_message_count(),
249 kDefaultTimeout);
250 EXPECT_TRUE(caller()->data_observer()->last_message().empty());
251 EXPECT_FALSE(caller()->data_observer()->messages().back().binary);
252
253 // Sending empty binary data
254 rtc::CopyOnWriteBuffer empty_buffer;
255 caller()->data_channel()->Send(DataBuffer(empty_buffer, true));
256 EXPECT_EQ_WAIT(2u, callee()->data_observer()->received_message_count(),
257 kDefaultTimeout);
258 EXPECT_TRUE(callee()->data_observer()->last_message().empty());
259 EXPECT_TRUE(callee()->data_observer()->messages().back().binary);
260 callee()->data_channel()->Send(DataBuffer(empty_buffer, true));
261 EXPECT_EQ_WAIT(2u, caller()->data_observer()->received_message_count(),
262 kDefaultTimeout);
263 EXPECT_TRUE(caller()->data_observer()->last_message().empty());
264 EXPECT_TRUE(caller()->data_observer()->messages().back().binary);
265}
266
Harald Alvestrand7087b832021-03-11 17:21:13 +0000267TEST_P(DataChannelIntegrationTest,
268 EndToEndCallWithSctpDataChannelLowestSafeMtu) {
269 // The lowest payload size limit that's tested and found safe for this
270 // application. Note that this is not the safe limit under all conditions;
271 // in particular, the default is not the largest DTLS signature, and
272 // this test does not use TURN.
273 const size_t kLowestSafePayloadSizeLimit = 1225;
274
275 ASSERT_TRUE(CreatePeerConnectionWrappers());
276 ConnectFakeSignaling();
277 // Expect that data channel created on caller side will show up for callee as
278 // well.
279 caller()->CreateDataChannel();
280 caller()->CreateAndSetAndSignalOffer();
281 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
282 // Caller data channel should already exist (it created one). Callee data
283 // channel may not exist yet, since negotiation happens in-band, not in SDP.
284 ASSERT_NE(nullptr, caller()->data_channel());
285 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
286 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
287 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
288
289 virtual_socket_server()->set_max_udp_payload(kLowestSafePayloadSizeLimit);
290 for (int message_size = 1140; message_size < 1240; message_size += 1) {
291 std::string data(message_size, 'a');
292 caller()->data_channel()->Send(DataBuffer(data));
293 ASSERT_EQ_WAIT(data, callee()->data_observer()->last_message(),
294 kDefaultTimeout);
295 callee()->data_channel()->Send(DataBuffer(data));
296 ASSERT_EQ_WAIT(data, caller()->data_observer()->last_message(),
297 kDefaultTimeout);
298 }
299}
300
301// This test verifies that lowering the MTU of the connection will cause
302// the datachannel to not transmit reliably.
303// The purpose of this test is to ensure that we know how a too-small MTU
304// error manifests itself.
305TEST_P(DataChannelIntegrationTest, EndToEndCallWithSctpDataChannelHarmfulMtu) {
306 // The lowest payload size limit that's tested and found safe for this
307 // application in this configuration (see test above).
308 const size_t kLowestSafePayloadSizeLimit = 1225;
309 // The size of the smallest message that fails to be delivered.
310 const size_t kMessageSizeThatIsNotDelivered = 1157;
311
312 ASSERT_TRUE(CreatePeerConnectionWrappers());
313 ConnectFakeSignaling();
314 caller()->CreateDataChannel();
315 caller()->CreateAndSetAndSignalOffer();
316 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
317 ASSERT_NE(nullptr, caller()->data_channel());
318 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
319 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
320 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
321
322 virtual_socket_server()->set_max_udp_payload(kLowestSafePayloadSizeLimit - 1);
323 // Probe for an undelivered or slowly delivered message. The exact
324 // size limit seems to be dependent on the message history, so make the
325 // code easily able to find the current value.
326 bool failure_seen = false;
327 for (size_t message_size = 1110; message_size < 1400; message_size++) {
328 const size_t message_count =
329 callee()->data_observer()->received_message_count();
330 const std::string data(message_size, 'a');
331 caller()->data_channel()->Send(DataBuffer(data));
332 // Wait a very short time for the message to be delivered.
Harald Alvestrand9d1e0702021-03-16 06:15:01 +0000333 // Note: Waiting only 10 ms is too short for Windows bots; they will
334 // flakily fail at a random frame.
Harald Alvestrand7087b832021-03-11 17:21:13 +0000335 WAIT(callee()->data_observer()->received_message_count() > message_count,
Harald Alvestrand9d1e0702021-03-16 06:15:01 +0000336 100);
Harald Alvestrand7087b832021-03-11 17:21:13 +0000337 if (callee()->data_observer()->received_message_count() == message_count) {
338 ASSERT_EQ(kMessageSizeThatIsNotDelivered, message_size);
339 failure_seen = true;
340 break;
341 }
342 }
343 ASSERT_TRUE(failure_seen);
344}
345
Harald Alvestrand39993842021-02-17 09:05:31 +0000346// Ensure that when the callee closes an SCTP data channel, the closing
347// procedure results in the data channel being closed for the caller as well.
348TEST_P(DataChannelIntegrationTest, CalleeClosesSctpDataChannel) {
349 // Same procedure as above test.
350 ASSERT_TRUE(CreatePeerConnectionWrappers());
351 ConnectFakeSignaling();
352 caller()->CreateDataChannel();
353 caller()->AddAudioVideoTracks();
354 callee()->AddAudioVideoTracks();
355 caller()->CreateAndSetAndSignalOffer();
356 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
357 ASSERT_NE(nullptr, caller()->data_channel());
358 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
359 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
360 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
361
362 // Close the data channel on the callee side, and wait for it to reach the
363 // "closed" state on both sides.
364 callee()->data_channel()->Close();
Florent Castelli141a4de2021-04-29 12:49:25 +0200365
366 DataChannelInterface::DataState expected_states[] = {
367 DataChannelInterface::DataState::kConnecting,
368 DataChannelInterface::DataState::kOpen,
369 DataChannelInterface::DataState::kClosing,
370 DataChannelInterface::DataState::kClosed};
371
372 EXPECT_EQ_WAIT(DataChannelInterface::DataState::kClosed,
373 caller()->data_observer()->state(), kDefaultTimeout);
374 EXPECT_THAT(caller()->data_observer()->states(),
375 ::testing::ElementsAreArray(expected_states));
376
377 EXPECT_EQ_WAIT(DataChannelInterface::DataState::kClosed,
378 callee()->data_observer()->state(), kDefaultTimeout);
379 EXPECT_THAT(callee()->data_observer()->states(),
380 ::testing::ElementsAreArray(expected_states));
Harald Alvestrand39993842021-02-17 09:05:31 +0000381}
382
383TEST_P(DataChannelIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
384 ASSERT_TRUE(CreatePeerConnectionWrappers());
385 ConnectFakeSignaling();
386 webrtc::DataChannelInit init;
387 init.id = 53;
388 init.maxRetransmits = 52;
389 caller()->CreateDataChannel("data-channel", &init);
390 caller()->AddAudioVideoTracks();
391 callee()->AddAudioVideoTracks();
392 caller()->CreateAndSetAndSignalOffer();
393 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
394 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
395 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
396 // Since "negotiated" is false, the "id" parameter should be ignored.
397 EXPECT_NE(init.id, callee()->data_channel()->id());
398 EXPECT_EQ("data-channel", callee()->data_channel()->label());
399 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
400 EXPECT_FALSE(callee()->data_channel()->negotiated());
401}
402
403// Test usrsctp's ability to process unordered data stream, where data actually
404// arrives out of order using simulated delays. Previously there have been some
405// bugs in this area.
406TEST_P(DataChannelIntegrationTest, StressTestUnorderedSctpDataChannel) {
407 // Introduce random network delays.
408 // Otherwise it's not a true "unordered" test.
409 virtual_socket_server()->set_delay_mean(20);
410 virtual_socket_server()->set_delay_stddev(5);
411 virtual_socket_server()->UpdateDelayDistribution();
412 // Normal procedure, but with unordered data channel config.
413 ASSERT_TRUE(CreatePeerConnectionWrappers());
414 ConnectFakeSignaling();
415 webrtc::DataChannelInit init;
416 init.ordered = false;
417 caller()->CreateDataChannel(&init);
418 caller()->CreateAndSetAndSignalOffer();
419 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
420 ASSERT_NE(nullptr, caller()->data_channel());
421 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
422 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
423 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
424
425 static constexpr int kNumMessages = 100;
426 // Deliberately chosen to be larger than the MTU so messages get fragmented.
427 static constexpr size_t kMaxMessageSize = 4096;
428 // Create and send random messages.
429 std::vector<std::string> sent_messages;
430 for (int i = 0; i < kNumMessages; ++i) {
431 size_t length =
432 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
433 std::string message;
434 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
435 caller()->data_channel()->Send(DataBuffer(message));
436 callee()->data_channel()->Send(DataBuffer(message));
437 sent_messages.push_back(message);
438 }
439
440 // Wait for all messages to be received.
441 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
442 caller()->data_observer()->received_message_count(),
443 kDefaultTimeout);
444 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
445 callee()->data_observer()->received_message_count(),
446 kDefaultTimeout);
447
448 // Sort and compare to make sure none of the messages were corrupted.
Florent Castelli88f4b332021-04-22 13:32:39 +0200449 std::vector<std::string> caller_received_messages;
450 absl::c_transform(caller()->data_observer()->messages(),
451 std::back_inserter(caller_received_messages),
452 [](const auto& a) { return a.data; });
453
454 std::vector<std::string> callee_received_messages;
455 absl::c_transform(callee()->data_observer()->messages(),
456 std::back_inserter(callee_received_messages),
457 [](const auto& a) { return a.data; });
458
Harald Alvestrand39993842021-02-17 09:05:31 +0000459 absl::c_sort(sent_messages);
460 absl::c_sort(caller_received_messages);
461 absl::c_sort(callee_received_messages);
462 EXPECT_EQ(sent_messages, caller_received_messages);
463 EXPECT_EQ(sent_messages, callee_received_messages);
464}
465
466// This test sets up a call between two parties with audio, and video. When
467// audio and video are setup and flowing, an SCTP data channel is negotiated.
468TEST_P(DataChannelIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
469 ASSERT_TRUE(CreatePeerConnectionWrappers());
470 ConnectFakeSignaling();
471 // Do initial offer/answer with audio/video.
472 caller()->AddAudioVideoTracks();
473 callee()->AddAudioVideoTracks();
474 caller()->CreateAndSetAndSignalOffer();
475 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
476 // Create data channel and do new offer and answer.
477 caller()->CreateDataChannel();
478 caller()->CreateAndSetAndSignalOffer();
479 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
480 // Caller data channel should already exist (it created one). Callee data
481 // channel may not exist yet, since negotiation happens in-band, not in SDP.
482 ASSERT_NE(nullptr, caller()->data_channel());
483 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
484 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
485 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
486 // Ensure data can be sent in both directions.
487 std::string data = "hello world";
488 caller()->data_channel()->Send(DataBuffer(data));
489 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
490 kDefaultTimeout);
491 callee()->data_channel()->Send(DataBuffer(data));
492 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
493 kDefaultTimeout);
494}
495
496// Set up a connection initially just using SCTP data channels, later upgrading
497// to audio/video, ensuring frames are received end-to-end. Effectively the
498// inverse of the test above.
499// This was broken in M57; see https://crbug.com/711243
500TEST_P(DataChannelIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
501 ASSERT_TRUE(CreatePeerConnectionWrappers());
502 ConnectFakeSignaling();
503 // Do initial offer/answer with just data channel.
504 caller()->CreateDataChannel();
505 caller()->CreateAndSetAndSignalOffer();
506 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
507 // Wait until data can be sent over the data channel.
508 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
509 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
510 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
511
512 // Do subsequent offer/answer with two-way audio and video. Audio and video
513 // should end up bundled on the DTLS/ICE transport already used for data.
514 caller()->AddAudioVideoTracks();
515 callee()->AddAudioVideoTracks();
516 caller()->CreateAndSetAndSignalOffer();
517 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
518 MediaExpectations media_expectations;
519 media_expectations.ExpectBidirectionalAudioAndVideo();
520 ASSERT_TRUE(ExpectNewFrames(media_expectations));
521}
522
523static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
524 cricket::SctpDataContentDescription* dcd_offer =
525 GetFirstSctpDataContentDescription(desc);
526 // See https://crbug.com/webrtc/11211 - this function is a no-op
527 ASSERT_TRUE(dcd_offer);
528 dcd_offer->set_use_sctpmap(false);
529 dcd_offer->set_protocol("UDP/DTLS/SCTP");
530}
531
532// Test that the data channel works when a spec-compliant SCTP m= section is
533// offered (using "a=sctp-port" instead of "a=sctpmap", and using
534// "UDP/DTLS/SCTP" as the protocol).
535TEST_P(DataChannelIntegrationTest,
536 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
537 ASSERT_TRUE(CreatePeerConnectionWrappers());
538 ConnectFakeSignaling();
539 caller()->CreateDataChannel();
540 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
541 caller()->CreateAndSetAndSignalOffer();
542 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
543 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
544 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
545 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
546
547 // Ensure data can be sent in both directions.
548 std::string data = "hello world";
549 caller()->data_channel()->Send(DataBuffer(data));
550 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
551 kDefaultTimeout);
552 callee()->data_channel()->Send(DataBuffer(data));
553 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
554 kDefaultTimeout);
555}
556
Harald Alvestrand39993842021-02-17 09:05:31 +0000557// Test that after closing PeerConnections, they stop sending any packets (ICE,
558// DTLS, RTP...).
559TEST_P(DataChannelIntegrationTest, ClosingConnectionStopsPacketFlow) {
560 // Set up audio/video/data, wait for some frames to be received.
561 ASSERT_TRUE(CreatePeerConnectionWrappers());
562 ConnectFakeSignaling();
563 caller()->AddAudioVideoTracks();
Harald Alvestrand39993842021-02-17 09:05:31 +0000564 caller()->CreateDataChannel();
Harald Alvestrand39993842021-02-17 09:05:31 +0000565 caller()->CreateAndSetAndSignalOffer();
566 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
567 MediaExpectations media_expectations;
568 media_expectations.CalleeExpectsSomeAudioAndVideo();
569 ASSERT_TRUE(ExpectNewFrames(media_expectations));
570 // Close PeerConnections.
571 ClosePeerConnections();
572 // Pump messages for a second, and ensure no new packets end up sent.
573 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
574 WAIT(false, 1000);
575 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
576 EXPECT_EQ(sent_packets_a, sent_packets_b);
577}
578
579// Test that transport stats are generated by the RTCStatsCollector for a
580// connection that only involves data channels. This is a regression test for
581// crbug.com/826972.
Harald Alvestrand39993842021-02-17 09:05:31 +0000582TEST_P(DataChannelIntegrationTest,
583 TransportStatsReportedForDataChannelOnlyConnection) {
584 ASSERT_TRUE(CreatePeerConnectionWrappers());
585 ConnectFakeSignaling();
586 caller()->CreateDataChannel();
587
588 caller()->CreateAndSetAndSignalOffer();
589 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
590 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
591
592 auto caller_report = caller()->NewGetStats();
593 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
594 auto callee_report = callee()->NewGetStats();
595 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
596}
597
Harald Alvestrandfeb6eb92021-04-21 18:52:32 +0000598TEST_P(DataChannelIntegrationTest, QueuedPacketsGetDeliveredInReliableMode) {
599 CreatePeerConnectionWrappers();
600 ConnectFakeSignaling();
601 caller()->CreateDataChannel();
602 caller()->CreateAndSetAndSignalOffer();
603 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
604 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
605
606 caller()->data_channel()->Send(DataBuffer("hello first"));
607 ASSERT_EQ_WAIT(1u, callee()->data_observer()->received_message_count(),
608 kDefaultTimeout);
609 // Cause a temporary network outage
610 virtual_socket_server()->set_drop_probability(1.0);
611 for (int i = 1; i <= 10; i++) {
612 caller()->data_channel()->Send(DataBuffer("Sent while blocked"));
613 }
614 // Nothing should be delivered during outage. Short wait.
615 EXPECT_EQ_WAIT(1u, callee()->data_observer()->received_message_count(), 10);
616 // Reverse outage
617 virtual_socket_server()->set_drop_probability(0.0);
618 // All packets should be delivered.
619 EXPECT_EQ_WAIT(11u, callee()->data_observer()->received_message_count(),
620 kDefaultTimeout);
621}
622
623TEST_P(DataChannelIntegrationTest, QueuedPacketsGetDeliveredInUnReliableMode) {
624 CreatePeerConnectionWrappers();
625 ConnectFakeSignaling();
626 DataChannelInit init;
627 init.maxRetransmits = 0;
628 init.ordered = false;
629 caller()->CreateDataChannel(&init);
630 caller()->CreateAndSetAndSignalOffer();
631 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
632 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
633 caller()->data_channel()->Send(DataBuffer("hello first"));
634 ASSERT_EQ_WAIT(1u, callee()->data_observer()->received_message_count(),
635 kDefaultTimeout);
636 // Cause a temporary network outage
637 virtual_socket_server()->set_drop_probability(1.0);
638 for (int i = 1; i <= 10; i++) {
639 caller()->data_channel()->Send(DataBuffer("Sent while blocked"));
640 }
641 // Nothing should be delivered during outage.
642 // We do a short wait to verify that delivery count is still 1.
643 WAIT(false, 10);
644 EXPECT_EQ(1u, callee()->data_observer()->received_message_count());
645 // Reverse the network outage.
646 virtual_socket_server()->set_drop_probability(0.0);
647 // Send a new packet, and wait for it to be delivered.
648 caller()->data_channel()->Send(DataBuffer("After block"));
649 EXPECT_EQ_WAIT("After block", callee()->data_observer()->last_message(),
650 kDefaultTimeout);
651 // Some messages should be lost, but first and last message should have
652 // been delivered.
653 // First, check that the protocol guarantee is preserved.
654 EXPECT_GT(11u, callee()->data_observer()->received_message_count());
655 EXPECT_LE(2u, callee()->data_observer()->received_message_count());
656 // Then, check that observed behavior (lose all messages) has not changed
657 EXPECT_EQ(2u, callee()->data_observer()->received_message_count());
658}
659
Harald Alvestrand39993842021-02-17 09:05:31 +0000660INSTANTIATE_TEST_SUITE_P(DataChannelIntegrationTest,
661 DataChannelIntegrationTest,
662 Values(SdpSemantics::kPlanB,
663 SdpSemantics::kUnifiedPlan));
664
665INSTANTIATE_TEST_SUITE_P(DataChannelIntegrationTest,
666 DataChannelIntegrationTestWithFakeClock,
667 Values(SdpSemantics::kPlanB,
668 SdpSemantics::kUnifiedPlan));
669
670TEST_F(DataChannelIntegrationTestUnifiedPlan,
671 EndToEndCallWithBundledSctpDataChannel) {
672 ASSERT_TRUE(CreatePeerConnectionWrappers());
673 ConnectFakeSignaling();
674 caller()->CreateDataChannel();
675 caller()->AddAudioVideoTracks();
676 callee()->AddAudioVideoTracks();
677 caller()->CreateAndSetAndSignalOffer();
678 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Harald Alvestrand7af57c62021-04-16 11:12:14 +0000679 ASSERT_TRUE_WAIT(caller()->pc()->GetSctpTransport(), kDefaultTimeout);
680 ASSERT_EQ_WAIT(SctpTransportState::kConnected,
681 caller()->pc()->GetSctpTransport()->Information().state(),
682 kDefaultTimeout);
Harald Alvestrand39993842021-02-17 09:05:31 +0000683 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
684 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
685}
686
687TEST_F(DataChannelIntegrationTestUnifiedPlan,
688 EndToEndCallWithDataChannelOnlyConnects) {
689 ASSERT_TRUE(CreatePeerConnectionWrappers());
690 ConnectFakeSignaling();
691 caller()->CreateDataChannel();
692 caller()->CreateAndSetAndSignalOffer();
693 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
694 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
695 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
696 ASSERT_TRUE(caller()->data_observer()->IsOpen());
697}
698
699TEST_F(DataChannelIntegrationTestUnifiedPlan, DataChannelClosesWhenClosed) {
700 ASSERT_TRUE(CreatePeerConnectionWrappers());
701 ConnectFakeSignaling();
702 caller()->CreateDataChannel();
703 caller()->CreateAndSetAndSignalOffer();
704 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
705 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
706 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
707 caller()->data_channel()->Close();
708 ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
709}
710
711TEST_F(DataChannelIntegrationTestUnifiedPlan,
712 DataChannelClosesWhenClosedReverse) {
713 ASSERT_TRUE(CreatePeerConnectionWrappers());
714 ConnectFakeSignaling();
715 caller()->CreateDataChannel();
716 caller()->CreateAndSetAndSignalOffer();
717 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
718 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
719 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
720 callee()->data_channel()->Close();
721 ASSERT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
722}
723
724TEST_F(DataChannelIntegrationTestUnifiedPlan,
725 DataChannelClosesWhenPeerConnectionClosed) {
726 ASSERT_TRUE(CreatePeerConnectionWrappers());
727 ConnectFakeSignaling();
728 caller()->CreateDataChannel();
729 caller()->CreateAndSetAndSignalOffer();
730 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
731 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
732 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
733 caller()->pc()->Close();
734 ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
735}
736
737#endif // WEBRTC_HAVE_SCTP
738
739} // namespace
740
741} // namespace webrtc