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