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