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