Harald Alvestrand | 3999384 | 2021-02-17 09:05:31 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 31 | namespace webrtc { |
| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | class DataChannelIntegrationTest |
| 36 | : public PeerConnectionIntegrationBaseTest, |
| 37 | public ::testing::WithParamInterface<SdpSemantics> { |
| 38 | protected: |
| 39 | DataChannelIntegrationTest() |
| 40 | : PeerConnectionIntegrationBaseTest(GetParam()) {} |
| 41 | }; |
| 42 | |
Bjorn Terelius | 3208bf1 | 2021-03-04 10:53:08 +0100 | [diff] [blame] | 43 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(DataChannelIntegrationTest); |
| 44 | |
Harald Alvestrand | 3999384 | 2021-02-17 09:05:31 +0000 | [diff] [blame] | 45 | // 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. |
| 51 | class 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). |
| 65 | class DataChannelIntegrationTestWithFakeClock |
| 66 | : public FakeClockForTest, |
| 67 | public DataChannelIntegrationTest {}; |
| 68 | |
| 69 | class DataChannelIntegrationTestPlanB |
| 70 | : public PeerConnectionIntegrationBaseTest { |
| 71 | protected: |
| 72 | DataChannelIntegrationTestPlanB() |
| 73 | : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {} |
| 74 | }; |
| 75 | |
Bjorn Terelius | 3208bf1 | 2021-03-04 10:53:08 +0100 | [diff] [blame] | 76 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST( |
| 77 | DataChannelIntegrationTestWithFakeClock); |
| 78 | |
Harald Alvestrand | 3999384 | 2021-02-17 09:05:31 +0000 | [diff] [blame] | 79 | class DataChannelIntegrationTestUnifiedPlan |
| 80 | : public PeerConnectionIntegrationBaseTest { |
| 81 | protected: |
| 82 | DataChannelIntegrationTestUnifiedPlan() |
| 83 | : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {} |
| 84 | }; |
| 85 | |
Harald Alvestrand | 3999384 | 2021-02-17 09:05:31 +0000 | [diff] [blame] | 86 | #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. |
| 91 | TEST_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. |
| 119 | TEST_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 Alvestrand | 3999384 | 2021-02-17 09:05:31 +0000 | [diff] [blame] | 149 | // This test sets up a call between two parties with audio, video and an SCTP |
| 150 | // data channel. |
| 151 | TEST_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 Alvestrand | 7087b83 | 2021-03-11 17:21:13 +0000 | [diff] [blame] | 182 | // This test sets up a call between two parties with an SCTP |
| 183 | // data channel only, and sends messages of various sizes. |
| 184 | TEST_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 Castelli | 88f4b33 | 2021-04-22 13:32:39 +0200 | [diff] [blame] | 221 | // This test sets up a call between two parties with an SCTP |
| 222 | // data channel only, and sends empty messages |
| 223 | TEST_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 Alvestrand | 7087b83 | 2021-03-11 17:21:13 +0000 | [diff] [blame] | 267 | TEST_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. |
| 305 | TEST_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 Alvestrand | 9d1e070 | 2021-03-16 06:15:01 +0000 | [diff] [blame] | 333 | // Note: Waiting only 10 ms is too short for Windows bots; they will |
| 334 | // flakily fail at a random frame. |
Harald Alvestrand | 7087b83 | 2021-03-11 17:21:13 +0000 | [diff] [blame] | 335 | WAIT(callee()->data_observer()->received_message_count() > message_count, |
Harald Alvestrand | 9d1e070 | 2021-03-16 06:15:01 +0000 | [diff] [blame] | 336 | 100); |
Harald Alvestrand | 7087b83 | 2021-03-11 17:21:13 +0000 | [diff] [blame] | 337 | 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 Alvestrand | 3999384 | 2021-02-17 09:05:31 +0000 | [diff] [blame] | 346 | // 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. |
| 348 | TEST_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(); |
| 365 | EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout); |
| 366 | EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout); |
| 367 | } |
| 368 | |
| 369 | TEST_P(DataChannelIntegrationTest, SctpDataChannelConfigSentToOtherSide) { |
| 370 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 371 | ConnectFakeSignaling(); |
| 372 | webrtc::DataChannelInit init; |
| 373 | init.id = 53; |
| 374 | init.maxRetransmits = 52; |
| 375 | caller()->CreateDataChannel("data-channel", &init); |
| 376 | caller()->AddAudioVideoTracks(); |
| 377 | callee()->AddAudioVideoTracks(); |
| 378 | caller()->CreateAndSetAndSignalOffer(); |
| 379 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 380 | ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout); |
| 381 | ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout); |
| 382 | // Since "negotiated" is false, the "id" parameter should be ignored. |
| 383 | EXPECT_NE(init.id, callee()->data_channel()->id()); |
| 384 | EXPECT_EQ("data-channel", callee()->data_channel()->label()); |
| 385 | EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits()); |
| 386 | EXPECT_FALSE(callee()->data_channel()->negotiated()); |
| 387 | } |
| 388 | |
| 389 | // Test usrsctp's ability to process unordered data stream, where data actually |
| 390 | // arrives out of order using simulated delays. Previously there have been some |
| 391 | // bugs in this area. |
| 392 | TEST_P(DataChannelIntegrationTest, StressTestUnorderedSctpDataChannel) { |
| 393 | // Introduce random network delays. |
| 394 | // Otherwise it's not a true "unordered" test. |
| 395 | virtual_socket_server()->set_delay_mean(20); |
| 396 | virtual_socket_server()->set_delay_stddev(5); |
| 397 | virtual_socket_server()->UpdateDelayDistribution(); |
| 398 | // Normal procedure, but with unordered data channel config. |
| 399 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 400 | ConnectFakeSignaling(); |
| 401 | webrtc::DataChannelInit init; |
| 402 | init.ordered = false; |
| 403 | caller()->CreateDataChannel(&init); |
| 404 | caller()->CreateAndSetAndSignalOffer(); |
| 405 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 406 | ASSERT_NE(nullptr, caller()->data_channel()); |
| 407 | ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout); |
| 408 | ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout); |
| 409 | ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout); |
| 410 | |
| 411 | static constexpr int kNumMessages = 100; |
| 412 | // Deliberately chosen to be larger than the MTU so messages get fragmented. |
| 413 | static constexpr size_t kMaxMessageSize = 4096; |
| 414 | // Create and send random messages. |
| 415 | std::vector<std::string> sent_messages; |
| 416 | for (int i = 0; i < kNumMessages; ++i) { |
| 417 | size_t length = |
| 418 | (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand) |
| 419 | std::string message; |
| 420 | ASSERT_TRUE(rtc::CreateRandomString(length, &message)); |
| 421 | caller()->data_channel()->Send(DataBuffer(message)); |
| 422 | callee()->data_channel()->Send(DataBuffer(message)); |
| 423 | sent_messages.push_back(message); |
| 424 | } |
| 425 | |
| 426 | // Wait for all messages to be received. |
| 427 | EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages), |
| 428 | caller()->data_observer()->received_message_count(), |
| 429 | kDefaultTimeout); |
| 430 | EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages), |
| 431 | callee()->data_observer()->received_message_count(), |
| 432 | kDefaultTimeout); |
| 433 | |
| 434 | // Sort and compare to make sure none of the messages were corrupted. |
Florent Castelli | 88f4b33 | 2021-04-22 13:32:39 +0200 | [diff] [blame] | 435 | std::vector<std::string> caller_received_messages; |
| 436 | absl::c_transform(caller()->data_observer()->messages(), |
| 437 | std::back_inserter(caller_received_messages), |
| 438 | [](const auto& a) { return a.data; }); |
| 439 | |
| 440 | std::vector<std::string> callee_received_messages; |
| 441 | absl::c_transform(callee()->data_observer()->messages(), |
| 442 | std::back_inserter(callee_received_messages), |
| 443 | [](const auto& a) { return a.data; }); |
| 444 | |
Harald Alvestrand | 3999384 | 2021-02-17 09:05:31 +0000 | [diff] [blame] | 445 | absl::c_sort(sent_messages); |
| 446 | absl::c_sort(caller_received_messages); |
| 447 | absl::c_sort(callee_received_messages); |
| 448 | EXPECT_EQ(sent_messages, caller_received_messages); |
| 449 | EXPECT_EQ(sent_messages, callee_received_messages); |
| 450 | } |
| 451 | |
| 452 | // This test sets up a call between two parties with audio, and video. When |
| 453 | // audio and video are setup and flowing, an SCTP data channel is negotiated. |
| 454 | TEST_P(DataChannelIntegrationTest, AddSctpDataChannelInSubsequentOffer) { |
| 455 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 456 | ConnectFakeSignaling(); |
| 457 | // Do initial offer/answer with audio/video. |
| 458 | caller()->AddAudioVideoTracks(); |
| 459 | callee()->AddAudioVideoTracks(); |
| 460 | caller()->CreateAndSetAndSignalOffer(); |
| 461 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 462 | // Create data channel and do new offer and answer. |
| 463 | caller()->CreateDataChannel(); |
| 464 | caller()->CreateAndSetAndSignalOffer(); |
| 465 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 466 | // Caller data channel should already exist (it created one). Callee data |
| 467 | // channel may not exist yet, since negotiation happens in-band, not in SDP. |
| 468 | ASSERT_NE(nullptr, caller()->data_channel()); |
| 469 | ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout); |
| 470 | EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout); |
| 471 | EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout); |
| 472 | // Ensure data can be sent in both directions. |
| 473 | std::string data = "hello world"; |
| 474 | caller()->data_channel()->Send(DataBuffer(data)); |
| 475 | EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(), |
| 476 | kDefaultTimeout); |
| 477 | callee()->data_channel()->Send(DataBuffer(data)); |
| 478 | EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(), |
| 479 | kDefaultTimeout); |
| 480 | } |
| 481 | |
| 482 | // Set up a connection initially just using SCTP data channels, later upgrading |
| 483 | // to audio/video, ensuring frames are received end-to-end. Effectively the |
| 484 | // inverse of the test above. |
| 485 | // This was broken in M57; see https://crbug.com/711243 |
| 486 | TEST_P(DataChannelIntegrationTest, SctpDataChannelToAudioVideoUpgrade) { |
| 487 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 488 | ConnectFakeSignaling(); |
| 489 | // Do initial offer/answer with just data channel. |
| 490 | caller()->CreateDataChannel(); |
| 491 | caller()->CreateAndSetAndSignalOffer(); |
| 492 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 493 | // Wait until data can be sent over the data channel. |
| 494 | ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout); |
| 495 | ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout); |
| 496 | ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout); |
| 497 | |
| 498 | // Do subsequent offer/answer with two-way audio and video. Audio and video |
| 499 | // should end up bundled on the DTLS/ICE transport already used for data. |
| 500 | caller()->AddAudioVideoTracks(); |
| 501 | callee()->AddAudioVideoTracks(); |
| 502 | caller()->CreateAndSetAndSignalOffer(); |
| 503 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 504 | MediaExpectations media_expectations; |
| 505 | media_expectations.ExpectBidirectionalAudioAndVideo(); |
| 506 | ASSERT_TRUE(ExpectNewFrames(media_expectations)); |
| 507 | } |
| 508 | |
| 509 | static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) { |
| 510 | cricket::SctpDataContentDescription* dcd_offer = |
| 511 | GetFirstSctpDataContentDescription(desc); |
| 512 | // See https://crbug.com/webrtc/11211 - this function is a no-op |
| 513 | ASSERT_TRUE(dcd_offer); |
| 514 | dcd_offer->set_use_sctpmap(false); |
| 515 | dcd_offer->set_protocol("UDP/DTLS/SCTP"); |
| 516 | } |
| 517 | |
| 518 | // Test that the data channel works when a spec-compliant SCTP m= section is |
| 519 | // offered (using "a=sctp-port" instead of "a=sctpmap", and using |
| 520 | // "UDP/DTLS/SCTP" as the protocol). |
| 521 | TEST_P(DataChannelIntegrationTest, |
| 522 | DataChannelWorksWhenSpecCompliantSctpOfferReceived) { |
| 523 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 524 | ConnectFakeSignaling(); |
| 525 | caller()->CreateDataChannel(); |
| 526 | caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer); |
| 527 | caller()->CreateAndSetAndSignalOffer(); |
| 528 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 529 | ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout); |
| 530 | EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout); |
| 531 | EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout); |
| 532 | |
| 533 | // Ensure data can be sent in both directions. |
| 534 | std::string data = "hello world"; |
| 535 | caller()->data_channel()->Send(DataBuffer(data)); |
| 536 | EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(), |
| 537 | kDefaultTimeout); |
| 538 | callee()->data_channel()->Send(DataBuffer(data)); |
| 539 | EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(), |
| 540 | kDefaultTimeout); |
| 541 | } |
| 542 | |
Harald Alvestrand | 3999384 | 2021-02-17 09:05:31 +0000 | [diff] [blame] | 543 | // Test that after closing PeerConnections, they stop sending any packets (ICE, |
| 544 | // DTLS, RTP...). |
| 545 | TEST_P(DataChannelIntegrationTest, ClosingConnectionStopsPacketFlow) { |
| 546 | // Set up audio/video/data, wait for some frames to be received. |
| 547 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 548 | ConnectFakeSignaling(); |
| 549 | caller()->AddAudioVideoTracks(); |
Harald Alvestrand | 3999384 | 2021-02-17 09:05:31 +0000 | [diff] [blame] | 550 | caller()->CreateDataChannel(); |
Harald Alvestrand | 3999384 | 2021-02-17 09:05:31 +0000 | [diff] [blame] | 551 | caller()->CreateAndSetAndSignalOffer(); |
| 552 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 553 | MediaExpectations media_expectations; |
| 554 | media_expectations.CalleeExpectsSomeAudioAndVideo(); |
| 555 | ASSERT_TRUE(ExpectNewFrames(media_expectations)); |
| 556 | // Close PeerConnections. |
| 557 | ClosePeerConnections(); |
| 558 | // Pump messages for a second, and ensure no new packets end up sent. |
| 559 | uint32_t sent_packets_a = virtual_socket_server()->sent_packets(); |
| 560 | WAIT(false, 1000); |
| 561 | uint32_t sent_packets_b = virtual_socket_server()->sent_packets(); |
| 562 | EXPECT_EQ(sent_packets_a, sent_packets_b); |
| 563 | } |
| 564 | |
| 565 | // Test that transport stats are generated by the RTCStatsCollector for a |
| 566 | // connection that only involves data channels. This is a regression test for |
| 567 | // crbug.com/826972. |
Harald Alvestrand | 3999384 | 2021-02-17 09:05:31 +0000 | [diff] [blame] | 568 | TEST_P(DataChannelIntegrationTest, |
| 569 | TransportStatsReportedForDataChannelOnlyConnection) { |
| 570 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 571 | ConnectFakeSignaling(); |
| 572 | caller()->CreateDataChannel(); |
| 573 | |
| 574 | caller()->CreateAndSetAndSignalOffer(); |
| 575 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 576 | ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout); |
| 577 | |
| 578 | auto caller_report = caller()->NewGetStats(); |
| 579 | EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size()); |
| 580 | auto callee_report = callee()->NewGetStats(); |
| 581 | EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size()); |
| 582 | } |
| 583 | |
Harald Alvestrand | feb6eb9 | 2021-04-21 18:52:32 +0000 | [diff] [blame] | 584 | TEST_P(DataChannelIntegrationTest, QueuedPacketsGetDeliveredInReliableMode) { |
| 585 | CreatePeerConnectionWrappers(); |
| 586 | ConnectFakeSignaling(); |
| 587 | caller()->CreateDataChannel(); |
| 588 | caller()->CreateAndSetAndSignalOffer(); |
| 589 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 590 | ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout); |
| 591 | |
| 592 | caller()->data_channel()->Send(DataBuffer("hello first")); |
| 593 | ASSERT_EQ_WAIT(1u, callee()->data_observer()->received_message_count(), |
| 594 | kDefaultTimeout); |
| 595 | // Cause a temporary network outage |
| 596 | virtual_socket_server()->set_drop_probability(1.0); |
| 597 | for (int i = 1; i <= 10; i++) { |
| 598 | caller()->data_channel()->Send(DataBuffer("Sent while blocked")); |
| 599 | } |
| 600 | // Nothing should be delivered during outage. Short wait. |
| 601 | EXPECT_EQ_WAIT(1u, callee()->data_observer()->received_message_count(), 10); |
| 602 | // Reverse outage |
| 603 | virtual_socket_server()->set_drop_probability(0.0); |
| 604 | // All packets should be delivered. |
| 605 | EXPECT_EQ_WAIT(11u, callee()->data_observer()->received_message_count(), |
| 606 | kDefaultTimeout); |
| 607 | } |
| 608 | |
| 609 | TEST_P(DataChannelIntegrationTest, QueuedPacketsGetDeliveredInUnReliableMode) { |
| 610 | CreatePeerConnectionWrappers(); |
| 611 | ConnectFakeSignaling(); |
| 612 | DataChannelInit init; |
| 613 | init.maxRetransmits = 0; |
| 614 | init.ordered = false; |
| 615 | caller()->CreateDataChannel(&init); |
| 616 | caller()->CreateAndSetAndSignalOffer(); |
| 617 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 618 | ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout); |
| 619 | caller()->data_channel()->Send(DataBuffer("hello first")); |
| 620 | ASSERT_EQ_WAIT(1u, callee()->data_observer()->received_message_count(), |
| 621 | kDefaultTimeout); |
| 622 | // Cause a temporary network outage |
| 623 | virtual_socket_server()->set_drop_probability(1.0); |
| 624 | for (int i = 1; i <= 10; i++) { |
| 625 | caller()->data_channel()->Send(DataBuffer("Sent while blocked")); |
| 626 | } |
| 627 | // Nothing should be delivered during outage. |
| 628 | // We do a short wait to verify that delivery count is still 1. |
| 629 | WAIT(false, 10); |
| 630 | EXPECT_EQ(1u, callee()->data_observer()->received_message_count()); |
| 631 | // Reverse the network outage. |
| 632 | virtual_socket_server()->set_drop_probability(0.0); |
| 633 | // Send a new packet, and wait for it to be delivered. |
| 634 | caller()->data_channel()->Send(DataBuffer("After block")); |
| 635 | EXPECT_EQ_WAIT("After block", callee()->data_observer()->last_message(), |
| 636 | kDefaultTimeout); |
| 637 | // Some messages should be lost, but first and last message should have |
| 638 | // been delivered. |
| 639 | // First, check that the protocol guarantee is preserved. |
| 640 | EXPECT_GT(11u, callee()->data_observer()->received_message_count()); |
| 641 | EXPECT_LE(2u, callee()->data_observer()->received_message_count()); |
| 642 | // Then, check that observed behavior (lose all messages) has not changed |
| 643 | EXPECT_EQ(2u, callee()->data_observer()->received_message_count()); |
| 644 | } |
| 645 | |
Harald Alvestrand | 3999384 | 2021-02-17 09:05:31 +0000 | [diff] [blame] | 646 | INSTANTIATE_TEST_SUITE_P(DataChannelIntegrationTest, |
| 647 | DataChannelIntegrationTest, |
| 648 | Values(SdpSemantics::kPlanB, |
| 649 | SdpSemantics::kUnifiedPlan)); |
| 650 | |
| 651 | INSTANTIATE_TEST_SUITE_P(DataChannelIntegrationTest, |
| 652 | DataChannelIntegrationTestWithFakeClock, |
| 653 | Values(SdpSemantics::kPlanB, |
| 654 | SdpSemantics::kUnifiedPlan)); |
| 655 | |
| 656 | TEST_F(DataChannelIntegrationTestUnifiedPlan, |
| 657 | EndToEndCallWithBundledSctpDataChannel) { |
| 658 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 659 | ConnectFakeSignaling(); |
| 660 | caller()->CreateDataChannel(); |
| 661 | caller()->AddAudioVideoTracks(); |
| 662 | callee()->AddAudioVideoTracks(); |
| 663 | caller()->CreateAndSetAndSignalOffer(); |
| 664 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
Harald Alvestrand | 7af57c6 | 2021-04-16 11:12:14 +0000 | [diff] [blame] | 665 | ASSERT_TRUE_WAIT(caller()->pc()->GetSctpTransport(), kDefaultTimeout); |
| 666 | ASSERT_EQ_WAIT(SctpTransportState::kConnected, |
| 667 | caller()->pc()->GetSctpTransport()->Information().state(), |
| 668 | kDefaultTimeout); |
Harald Alvestrand | 3999384 | 2021-02-17 09:05:31 +0000 | [diff] [blame] | 669 | ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout); |
| 670 | ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout); |
| 671 | } |
| 672 | |
| 673 | TEST_F(DataChannelIntegrationTestUnifiedPlan, |
| 674 | EndToEndCallWithDataChannelOnlyConnects) { |
| 675 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 676 | ConnectFakeSignaling(); |
| 677 | caller()->CreateDataChannel(); |
| 678 | caller()->CreateAndSetAndSignalOffer(); |
| 679 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 680 | ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout); |
| 681 | ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout); |
| 682 | ASSERT_TRUE(caller()->data_observer()->IsOpen()); |
| 683 | } |
| 684 | |
| 685 | TEST_F(DataChannelIntegrationTestUnifiedPlan, DataChannelClosesWhenClosed) { |
| 686 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 687 | ConnectFakeSignaling(); |
| 688 | caller()->CreateDataChannel(); |
| 689 | caller()->CreateAndSetAndSignalOffer(); |
| 690 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 691 | ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout); |
| 692 | ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout); |
| 693 | caller()->data_channel()->Close(); |
| 694 | ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout); |
| 695 | } |
| 696 | |
| 697 | TEST_F(DataChannelIntegrationTestUnifiedPlan, |
| 698 | DataChannelClosesWhenClosedReverse) { |
| 699 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 700 | ConnectFakeSignaling(); |
| 701 | caller()->CreateDataChannel(); |
| 702 | caller()->CreateAndSetAndSignalOffer(); |
| 703 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 704 | ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout); |
| 705 | ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout); |
| 706 | callee()->data_channel()->Close(); |
| 707 | ASSERT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout); |
| 708 | } |
| 709 | |
| 710 | TEST_F(DataChannelIntegrationTestUnifiedPlan, |
| 711 | DataChannelClosesWhenPeerConnectionClosed) { |
| 712 | ASSERT_TRUE(CreatePeerConnectionWrappers()); |
| 713 | ConnectFakeSignaling(); |
| 714 | caller()->CreateDataChannel(); |
| 715 | caller()->CreateAndSetAndSignalOffer(); |
| 716 | ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout); |
| 717 | ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout); |
| 718 | ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout); |
| 719 | caller()->pc()->Close(); |
| 720 | ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout); |
| 721 | } |
| 722 | |
| 723 | #endif // WEBRTC_HAVE_SCTP |
| 724 | |
| 725 | } // namespace |
| 726 | |
| 727 | } // namespace webrtc |