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