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