| 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 | #include <string.h> |
| 13 |
| 14 | #include <memory> |
| 15 | #include <string> |
| 16 | #include <vector> |
| 17 |
| 18 | #include "api/data_channel_interface.h" |
| 19 | #include "api/rtc_error.h" |
| 20 | #include "api/scoped_refptr.h" |
| 21 | #include "api/transport/data_channel_transport_interface.h" |
| 22 | #include "media/base/media_channel.h" |
| 23 | #include "media/sctp/sctp_transport_internal.h" |
| 24 | #include "pc/sctp_data_channel.h" |
| 25 | #include "pc/sctp_utils.h" |
| 26 | #include "pc/test/fake_data_channel_controller.h" |
| 27 | #include "rtc_base/copy_on_write_buffer.h" |
| 28 | #include "rtc_base/gunit.h" |
| 29 | #include "rtc_base/null_socket_server.h" |
| 30 | #include "rtc_base/ssl_stream_adapter.h" |
| 31 | #include "rtc_base/thread.h" |
| 32 | #include "test/gtest.h" |
| 33 | #include "test/run_loop.h" |
| 34 |
| 35 | #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
| 36 | #include "test/testsupport/rtc_expect_death.h" |
| 37 | #endif // RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
| 38 |
| 39 | namespace webrtc { |
| 40 |
| 41 | namespace { |
| 42 |
| 43 | static constexpr int kDefaultTimeout = 10000; |
| 44 |
| 45 | class FakeDataChannelObserver : public DataChannelObserver { |
| 46 | public: |
| 47 | FakeDataChannelObserver() { RTC_DCHECK(!IsOkToCallOnTheNetworkThread()); } |
| 48 |
| 49 | void OnStateChange() override { ++on_state_change_count_; } |
| 50 |
| 51 | void OnBufferedAmountChange(uint64_t previous_amount) override { |
| 52 | ++on_buffered_amount_change_count_; |
| 53 | } |
| 54 |
| 55 | void OnMessage(const DataBuffer& buffer) override { ++messages_received_; } |
| 56 |
| 57 | size_t messages_received() const { return messages_received_; } |
| 58 |
| 59 | void ResetOnStateChangeCount() { on_state_change_count_ = 0; } |
| 60 |
| 61 | void ResetOnBufferedAmountChangeCount() { |
| 62 | on_buffered_amount_change_count_ = 0; |
| 63 | } |
| 64 |
| 65 | size_t on_state_change_count() const { return on_state_change_count_; } |
| 66 |
| 67 | size_t on_buffered_amount_change_count() const { |
| 68 | return on_buffered_amount_change_count_; |
| 69 | } |
| 70 |
| 71 | private: |
| 72 | size_t messages_received_ = 0u; |
| 73 | size_t on_state_change_count_ = 0u; |
| 74 | size_t on_buffered_amount_change_count_ = 0u; |
| 75 | }; |
| 76 |
| 77 | class SctpDataChannelTest : public ::testing::Test { |
| 78 | protected: |
| 79 | SctpDataChannelTest() |
| 80 | : network_thread_(std::make_unique<rtc::NullSocketServer>()), |
| 81 | controller_(new FakeDataChannelController(&network_thread_)) { |
| 82 | network_thread_.Start(); |
| 83 | inner_channel_ = controller_->CreateDataChannel("test", init_); |
| 84 | channel_ = SctpDataChannel::CreateProxy(inner_channel_, signaling_safety_); |
| 85 | } |
| 86 | ~SctpDataChannelTest() override { |
| 87 | run_loop_.Flush(); |
| 88 | signaling_safety_->SetNotAlive(); |
| 89 | inner_channel_ = nullptr; |
| 90 | channel_ = nullptr; |
| 91 | controller_.reset(); |
| 92 | observer_.reset(); |
| 93 | network_thread_.Stop(); |
| 94 | } |
| 95 |
| 96 | void SetChannelReady() { |
| 97 | controller_->set_transport_available(true); |
| 98 | StreamId sid(0); |
| 99 | network_thread_.BlockingCall([&]() { |
| 100 | RTC_DCHECK_RUN_ON(&network_thread_); |
| 101 | if (!inner_channel_->sid_n().HasValue()) { |
| 102 | inner_channel_->SetSctpSid_n(sid); |
| 103 | controller_->AddSctpDataStream(sid); |
| 104 | } |
| 105 | inner_channel_->OnTransportChannelCreated(); |
| 106 | }); |
| 107 | controller_->set_ready_to_send(true); |
| 108 | run_loop_.Flush(); |
| 109 | } |
| 110 |
| 111 | // TODO(bugs.webrtc.org/11547): This mirrors what the DataChannelController |
| 112 | // currently does when assigning stream ids to a channel. Right now the sid |
| 113 | // in the SctpDataChannel code is (still) tied to the signaling thread, but |
| 114 | // the `AddSctpDataStream` operation is a bridge to the transport and needs |
| 115 | // to run on the network thread. |
| 116 | void SetChannelSid(const rtc::scoped_refptr<SctpDataChannel>& channel, |
| 117 | StreamId sid) { |
| 118 | RTC_DCHECK(sid.HasValue()); |
| 119 | network_thread_.BlockingCall([&]() { |
| 120 | channel->SetSctpSid_n(sid); |
| 121 | controller_->AddSctpDataStream(sid); |
| 122 | }); |
| 123 | } |
| 124 |
| 125 | void AddObserver() { |
| 126 | observer_.reset(new FakeDataChannelObserver()); |
| 127 | channel_->RegisterObserver(observer_.get()); |
| 128 | } |
| 129 |
| 130 | // Wait for queued up methods to run on the network thread. |
| 131 | void FlushNetworkThread() { |
| 132 | RTC_DCHECK_RUN_ON(run_loop_.task_queue()); |
| 133 | network_thread_.BlockingCall([] {}); |
| 134 | } |
| 135 |
| 136 | // Used to complete pending methods on the network thread |
| 137 | // that might queue up methods on the signaling (main) thread |
| 138 | // that are run too. |
| 139 | void FlushNetworkThreadAndPendingOperations() { |
| 140 | FlushNetworkThread(); |
| 141 | run_loop_.Flush(); |
| 142 | } |
| 143 |
| 144 | test::RunLoop run_loop_; |
| 145 | rtc::Thread network_thread_; |
| 146 | InternalDataChannelInit init_; |
| 147 | rtc::scoped_refptr<PendingTaskSafetyFlag> signaling_safety_ = |
| 148 | PendingTaskSafetyFlag::Create(); |
| 149 | std::unique_ptr<FakeDataChannelController> controller_; |
| 150 | std::unique_ptr<FakeDataChannelObserver> observer_; |
| 151 | rtc::scoped_refptr<SctpDataChannel> inner_channel_; |
| 152 | rtc::scoped_refptr<DataChannelInterface> channel_; |
| 153 | }; |
| 154 |
| 155 | TEST_F(SctpDataChannelTest, VerifyConfigurationGetters) { |
| 156 | EXPECT_EQ(channel_->label(), "test"); |
| 157 | EXPECT_EQ(channel_->protocol(), init_.protocol); |
| 158 |
| 159 | // Note that the `init_.reliable` field is deprecated, so we directly set |
| 160 | // it here to match spec behavior for purposes of checking the `reliable()` |
| 161 | // getter. |
| 162 | init_.reliable = (!init_.maxRetransmits && !init_.maxRetransmitTime); |
| 163 | EXPECT_EQ(channel_->reliable(), init_.reliable); |
| 164 | EXPECT_EQ(channel_->ordered(), init_.ordered); |
| 165 | EXPECT_EQ(channel_->negotiated(), init_.negotiated); |
| 166 | EXPECT_EQ(channel_->priority(), Priority::kLow); |
| 167 | EXPECT_EQ(channel_->maxRetransmitTime(), static_cast<uint16_t>(-1)); |
| 168 | EXPECT_EQ(channel_->maxPacketLifeTime(), init_.maxRetransmitTime); |
| 169 | EXPECT_EQ(channel_->maxRetransmits(), static_cast<uint16_t>(-1)); |
| 170 | EXPECT_EQ(channel_->maxRetransmitsOpt(), init_.maxRetransmits); |
| 171 |
| 172 | // Check the non-const part of the configuration. |
| 173 | EXPECT_EQ(channel_->id(), init_.id); |
| 174 | network_thread_.BlockingCall( |
| 175 | [&]() { EXPECT_EQ(inner_channel_->sid_n(), StreamId()); }); |
| 176 |
| 177 | SetChannelReady(); |
| 178 | EXPECT_EQ(channel_->id(), 0); |
| 179 | network_thread_.BlockingCall( |
| 180 | [&]() { EXPECT_EQ(inner_channel_->sid_n(), StreamId(0)); }); |
| 181 | } |
| 182 |
| 183 | // Verifies that the data channel is connected to the transport after creation. |
| 184 | TEST_F(SctpDataChannelTest, ConnectedToTransportOnCreated) { |
| 185 | controller_->set_transport_available(true); |
| 186 | rtc::scoped_refptr<SctpDataChannel> dc = |
| 187 | controller_->CreateDataChannel("test1", init_); |
| 188 | EXPECT_TRUE(controller_->IsConnected(dc.get())); |
| 189 |
| 190 | // The sid is not set yet, so it should not have added the streams. |
| 191 | StreamId sid = network_thread_.BlockingCall([&]() { return dc->sid_n(); }); |
| 192 | EXPECT_FALSE(controller_->IsStreamAdded(sid)); |
| 193 |
| 194 | SetChannelSid(dc, StreamId(0)); |
| 195 | sid = network_thread_.BlockingCall([&]() { return dc->sid_n(); }); |
| 196 | EXPECT_TRUE(controller_->IsStreamAdded(sid)); |
| 197 | } |
| 198 |
| 199 | // Tests the state of the data channel. |
| 200 | TEST_F(SctpDataChannelTest, StateTransition) { |
| 201 | AddObserver(); |
| 202 |
| 203 | EXPECT_EQ(DataChannelInterface::kConnecting, channel_->state()); |
| 204 | EXPECT_EQ(observer_->on_state_change_count(), 0u); |
| 205 | SetChannelReady(); |
| 206 |
| 207 | EXPECT_EQ(DataChannelInterface::kOpen, channel_->state()); |
| 208 | EXPECT_EQ(observer_->on_state_change_count(), 1u); |
| 209 |
| 210 | // `Close()` should trigger two state changes, first `kClosing`, then |
| 211 | // `kClose`. |
| 212 | channel_->Close(); |
| 213 | // The (simulated) transport close notifications runs on the network thread |
| 214 | // and posts a completion notification to the signaling (current) thread. |
| 215 | // Allow that operation to complete before checking the state. |
| 216 | run_loop_.Flush(); |
| 217 | EXPECT_EQ(DataChannelInterface::kClosed, channel_->state()); |
| 218 | EXPECT_EQ(observer_->on_state_change_count(), 3u); |
| 219 | EXPECT_TRUE(channel_->error().ok()); |
| 220 | // Verifies that it's disconnected from the transport. |
| 221 | EXPECT_FALSE(controller_->IsConnected(inner_channel_.get())); |
| 222 | } |
| 223 |
| 224 | // Tests that DataChannel::buffered_amount() is correct after the channel is |
| 225 | // blocked. |
| 226 | TEST_F(SctpDataChannelTest, BufferedAmountWhenBlocked) { |
| 227 | AddObserver(); |
| 228 | SetChannelReady(); |
| 229 | DataBuffer buffer("abcd"); |
| 230 | size_t successful_sends = 0; |
| 231 | auto send_complete = [&](RTCError err) { |
| 232 | EXPECT_TRUE(err.ok()); |
| 233 | ++successful_sends; |
| 234 | }; |
| 235 | channel_->SendAsync(buffer, send_complete); |
| 236 | FlushNetworkThreadAndPendingOperations(); |
| 237 | EXPECT_EQ(channel_->buffered_amount(), 0u); |
| 238 | size_t successful_send_count = 1; |
| 239 | EXPECT_EQ(successful_send_count, successful_sends); |
| 240 | EXPECT_EQ(successful_send_count, |
| 241 | observer_->on_buffered_amount_change_count()); |
| 242 |
| 243 | controller_->set_send_blocked(true); |
| 244 | const int number_of_packets = 3; |
| 245 | for (int i = 0; i < number_of_packets; ++i) { |
| 246 | channel_->SendAsync(buffer, send_complete); |
| 247 | ++successful_send_count; |
| 248 | } |
| 249 | FlushNetworkThreadAndPendingOperations(); |
| 250 | EXPECT_EQ(buffer.data.size() * number_of_packets, |
| 251 | channel_->buffered_amount()); |
| 252 | EXPECT_EQ(successful_send_count, successful_sends); |
| 253 |
| 254 | // An event should not have been fired for buffered amount. |
| 255 | EXPECT_EQ(1u, observer_->on_buffered_amount_change_count()); |
| 256 |
| 257 | // Now buffered amount events should get fired and the value |
| 258 | // get down to 0u. |
| 259 | controller_->set_send_blocked(false); |
| 260 | run_loop_.Flush(); |
| 261 | EXPECT_EQ(channel_->buffered_amount(), 0u); |
| 262 | EXPECT_EQ(successful_send_count, successful_sends); |
| 263 | EXPECT_EQ(successful_send_count, |
| 264 | observer_->on_buffered_amount_change_count()); |
| 265 | } |
| 266 |
| 267 | // TODO(tommi): This test uses `Send()`. Remove once fully deprecated. |
| 268 | TEST_F(SctpDataChannelTest, DeprecatedBufferedAmountWhenBlocked) { |
| 269 | AddObserver(); |
| 270 | SetChannelReady(); |
| 271 | DataBuffer buffer("abcd"); |
| 272 | EXPECT_TRUE(channel_->Send(buffer)); |
| 273 | size_t successful_send_count = 1; |
| 274 |
| 275 | run_loop_.Flush(); |
| 276 | EXPECT_EQ(0U, channel_->buffered_amount()); |
| 277 | EXPECT_EQ(successful_send_count, |
| 278 | observer_->on_buffered_amount_change_count()); |
| 279 |
| 280 | controller_->set_send_blocked(true); |
| 281 |
| 282 | const int number_of_packets = 3; |
| 283 | for (int i = 0; i < number_of_packets; ++i) { |
| 284 | EXPECT_TRUE(channel_->Send(buffer)); |
| 285 | } |
| 286 | EXPECT_EQ(buffer.data.size() * number_of_packets, |
| 287 | channel_->buffered_amount()); |
| 288 | EXPECT_EQ(successful_send_count, |
| 289 | observer_->on_buffered_amount_change_count()); |
| 290 |
| 291 | controller_->set_send_blocked(false); |
| 292 | run_loop_.Flush(); |
| 293 | successful_send_count += number_of_packets; |
| 294 | EXPECT_EQ(channel_->buffered_amount(), 0u); |
| 295 | EXPECT_EQ(successful_send_count, |
| 296 | observer_->on_buffered_amount_change_count()); |
| 297 | } |
| 298 |
| 299 | // Tests that the queued data are sent when the channel transitions from blocked |
| 300 | // to unblocked. |
| 301 | TEST_F(SctpDataChannelTest, QueuedDataSentWhenUnblocked) { |
| 302 | AddObserver(); |
| 303 | SetChannelReady(); |
| 304 | DataBuffer buffer("abcd"); |
| 305 | controller_->set_send_blocked(true); |
| 306 | size_t successful_send = 0u; |
| 307 | auto send_complete = [&](RTCError err) { |
| 308 | EXPECT_TRUE(err.ok()); |
| 309 | ++successful_send; |
| 310 | }; |
| 311 | channel_->SendAsync(buffer, send_complete); |
| 312 | FlushNetworkThreadAndPendingOperations(); |
| 313 | EXPECT_EQ(1U, successful_send); |
| 314 | EXPECT_EQ(0U, observer_->on_buffered_amount_change_count()); |
| 315 |
| 316 | controller_->set_send_blocked(false); |
| 317 | SetChannelReady(); |
| 318 | EXPECT_EQ(channel_->buffered_amount(), 0u); |
| 319 | EXPECT_EQ(observer_->on_buffered_amount_change_count(), 1u); |
| 320 | } |
| 321 |
| 322 | // TODO(tommi): This test uses `Send()`. Remove once fully deprecated. |
| 323 | TEST_F(SctpDataChannelTest, DeprecatedQueuedDataSentWhenUnblocked) { |
| 324 | AddObserver(); |
| 325 | SetChannelReady(); |
| 326 | DataBuffer buffer("abcd"); |
| 327 | controller_->set_send_blocked(true); |
| 328 | EXPECT_TRUE(channel_->Send(buffer)); |
| 329 |
| 330 | EXPECT_EQ(0U, observer_->on_buffered_amount_change_count()); |
| 331 |
| 332 | controller_->set_send_blocked(false); |
| 333 | SetChannelReady(); |
| 334 | EXPECT_EQ(0U, channel_->buffered_amount()); |
| 335 | EXPECT_EQ(1U, observer_->on_buffered_amount_change_count()); |
| 336 | } |
| 337 |
| 338 | // Tests that no crash when the channel is blocked right away while trying to |
| 339 | // send queued data. |
| 340 | TEST_F(SctpDataChannelTest, BlockedWhenSendQueuedDataNoCrash) { |
| 341 | AddObserver(); |
| 342 | SetChannelReady(); |
| 343 | DataBuffer buffer("abcd"); |
| 344 | controller_->set_send_blocked(true); |
| 345 | size_t successful_send = 0u; |
| 346 | auto send_complete = [&](RTCError err) { |
| 347 | EXPECT_TRUE(err.ok()); |
| 348 | ++successful_send; |
| 349 | }; |
| 350 | channel_->SendAsync(buffer, send_complete); |
| 351 | FlushNetworkThreadAndPendingOperations(); |
| 352 | EXPECT_EQ(1U, successful_send); |
| 353 | EXPECT_EQ(0U, observer_->on_buffered_amount_change_count()); |
| 354 |
| 355 | // Set channel ready while it is still blocked. |
| 356 | SetChannelReady(); |
| 357 | EXPECT_EQ(buffer.size(), channel_->buffered_amount()); |
| 358 | EXPECT_EQ(0U, observer_->on_buffered_amount_change_count()); |
| 359 |
| 360 | // Unblock the channel to send queued data again, there should be no crash. |
| 361 | controller_->set_send_blocked(false); |
| 362 | SetChannelReady(); |
| 363 | EXPECT_EQ(0U, channel_->buffered_amount()); |
| 364 | EXPECT_EQ(1U, observer_->on_buffered_amount_change_count()); |
| 365 | } |
| 366 |
| 367 | // TODO(tommi): This test uses `Send()`. Remove once fully deprecated. |
| 368 | TEST_F(SctpDataChannelTest, DeprecatedBlockedWhenSendQueuedDataNoCrash) { |
| 369 | AddObserver(); |
| 370 | SetChannelReady(); |
| 371 | DataBuffer buffer("abcd"); |
| 372 | controller_->set_send_blocked(true); |
| 373 | EXPECT_TRUE(channel_->Send(buffer)); |
| 374 | EXPECT_EQ(0U, observer_->on_buffered_amount_change_count()); |
| 375 |
| 376 | // Set channel ready while it is still blocked. |
| 377 | SetChannelReady(); |
| 378 | EXPECT_EQ(buffer.size(), channel_->buffered_amount()); |
| 379 | EXPECT_EQ(0U, observer_->on_buffered_amount_change_count()); |
| 380 |
| 381 | // Unblock the channel to send queued data again, there should be no crash. |
| 382 | controller_->set_send_blocked(false); |
| 383 | SetChannelReady(); |
| 384 | EXPECT_EQ(0U, channel_->buffered_amount()); |
| 385 | EXPECT_EQ(1U, observer_->on_buffered_amount_change_count()); |
| 386 | } |
| 387 |
| 388 | // Tests that DataChannel::messages_sent() and DataChannel::bytes_sent() are |
| 389 | // correct, sending data both while unblocked and while blocked. |
| 390 | TEST_F(SctpDataChannelTest, VerifyMessagesAndBytesSent) { |
| 391 | AddObserver(); |
| 392 | SetChannelReady(); |
| 393 | std::vector<DataBuffer> buffers({ |
| 394 | DataBuffer("message 1"), |
| 395 | DataBuffer("msg 2"), |
| 396 | DataBuffer("message three"), |
| 397 | DataBuffer("quadra message"), |
| 398 | DataBuffer("fifthmsg"), |
| 399 | DataBuffer("message of the beast"), |
| 400 | }); |
| 401 |
| 402 | // Default values. |
| 403 | EXPECT_EQ(0U, channel_->messages_sent()); |
| 404 | EXPECT_EQ(0U, channel_->bytes_sent()); |
| 405 |
| 406 | // Send three buffers while not blocked. |
| 407 | controller_->set_send_blocked(false); |
| 408 | for (int i : {0, 1, 2}) { |
| 409 | channel_->SendAsync(buffers[i], nullptr); |
| 410 | } |
| 411 | FlushNetworkThreadAndPendingOperations(); |
| 412 |
| 413 | size_t bytes_sent = buffers[0].size() + buffers[1].size() + buffers[2].size(); |
| 414 | EXPECT_EQ_WAIT(0U, channel_->buffered_amount(), kDefaultTimeout); |
| 415 | EXPECT_EQ(3U, channel_->messages_sent()); |
| 416 | EXPECT_EQ(bytes_sent, channel_->bytes_sent()); |
| 417 |
| 418 | // Send three buffers while blocked, queuing the buffers. |
| 419 | controller_->set_send_blocked(true); |
| 420 | for (int i : {3, 4, 5}) { |
| 421 | channel_->SendAsync(buffers[i], nullptr); |
| 422 | } |
| 423 | FlushNetworkThreadAndPendingOperations(); |
| 424 | size_t bytes_queued = |
| 425 | buffers[3].size() + buffers[4].size() + buffers[5].size(); |
| 426 | EXPECT_EQ(bytes_queued, channel_->buffered_amount()); |
| 427 | EXPECT_EQ(3U, channel_->messages_sent()); |
| 428 | EXPECT_EQ(bytes_sent, channel_->bytes_sent()); |
| 429 |
| 430 | // Unblock and make sure everything was sent. |
| 431 | controller_->set_send_blocked(false); |
| 432 | EXPECT_EQ_WAIT(0U, channel_->buffered_amount(), kDefaultTimeout); |
| 433 | bytes_sent += bytes_queued; |
| 434 | EXPECT_EQ(6U, channel_->messages_sent()); |
| 435 | EXPECT_EQ(bytes_sent, channel_->bytes_sent()); |
| 436 | } |
| 437 |
| 438 | // TODO(tommi): This test uses `Send()`. Remove once fully deprecated. |
| 439 | TEST_F(SctpDataChannelTest, DeprecatedVerifyMessagesAndBytesSent) { |
| 440 | AddObserver(); |
| 441 | SetChannelReady(); |
| 442 | std::vector<DataBuffer> buffers({ |
| 443 | DataBuffer("message 1"), |
| 444 | DataBuffer("msg 2"), |
| 445 | DataBuffer("message three"), |
| 446 | DataBuffer("quadra message"), |
| 447 | DataBuffer("fifthmsg"), |
| 448 | DataBuffer("message of the beast"), |
| 449 | }); |
| 450 |
| 451 | // Default values. |
| 452 | EXPECT_EQ(0U, channel_->messages_sent()); |
| 453 | EXPECT_EQ(0U, channel_->bytes_sent()); |
| 454 |
| 455 | // Send three buffers while not blocked. |
| 456 | controller_->set_send_blocked(false); |
| 457 | EXPECT_TRUE(channel_->Send(buffers[0])); |
| 458 | EXPECT_TRUE(channel_->Send(buffers[1])); |
| 459 | EXPECT_TRUE(channel_->Send(buffers[2])); |
| 460 | size_t bytes_sent = buffers[0].size() + buffers[1].size() + buffers[2].size(); |
| 461 | EXPECT_EQ_WAIT(0U, channel_->buffered_amount(), kDefaultTimeout); |
| 462 | EXPECT_EQ(3U, channel_->messages_sent()); |
| 463 | EXPECT_EQ(bytes_sent, channel_->bytes_sent()); |
| 464 |
| 465 | // Send three buffers while blocked, queuing the buffers. |
| 466 | controller_->set_send_blocked(true); |
| 467 | EXPECT_TRUE(channel_->Send(buffers[3])); |
| 468 | EXPECT_TRUE(channel_->Send(buffers[4])); |
| 469 | EXPECT_TRUE(channel_->Send(buffers[5])); |
| 470 | size_t bytes_queued = |
| 471 | buffers[3].size() + buffers[4].size() + buffers[5].size(); |
| 472 | EXPECT_EQ(bytes_queued, channel_->buffered_amount()); |
| 473 | EXPECT_EQ(3U, channel_->messages_sent()); |
| 474 | EXPECT_EQ(bytes_sent, channel_->bytes_sent()); |
| 475 |
| 476 | // Unblock and make sure everything was sent. |
| 477 | controller_->set_send_blocked(false); |
| 478 | EXPECT_EQ_WAIT(0U, channel_->buffered_amount(), kDefaultTimeout); |
| 479 | bytes_sent += bytes_queued; |
| 480 | EXPECT_EQ(6U, channel_->messages_sent()); |
| 481 | EXPECT_EQ(bytes_sent, channel_->bytes_sent()); |
| 482 | } |
| 483 |
| 484 | // Tests that the queued control message is sent when channel is ready. |
| 485 | TEST_F(SctpDataChannelTest, OpenMessageSent) { |
| 486 | // Initially the id is unassigned. |
| 487 | EXPECT_EQ(-1, channel_->id()); |
| 488 |
| 489 | SetChannelReady(); |
| 490 | EXPECT_GE(channel_->id(), 0); |
| 491 | EXPECT_EQ(DataMessageType::kControl, |
| 492 | controller_->last_send_data_params().type); |
| 493 | EXPECT_EQ(controller_->last_sid(), channel_->id()); |
| 494 | } |
| 495 |
| 496 | TEST_F(SctpDataChannelTest, QueuedOpenMessageSent) { |
| 497 | controller_->set_send_blocked(true); |
| 498 | SetChannelReady(); |
| 499 | controller_->set_send_blocked(false); |
| 500 |
| 501 | EXPECT_EQ(DataMessageType::kControl, |
| 502 | controller_->last_send_data_params().type); |
| 503 | EXPECT_EQ(controller_->last_sid(), channel_->id()); |
| 504 | } |
| 505 |
| 506 | // Tests that the DataChannel created after transport gets ready can enter OPEN |
| 507 | // state. |
| 508 | TEST_F(SctpDataChannelTest, LateCreatedChannelTransitionToOpen) { |
| 509 | SetChannelReady(); |
| 510 | InternalDataChannelInit init; |
| 511 | init.id = 1; |
| 512 | auto dc = SctpDataChannel::CreateProxy( |
| 513 | controller_->CreateDataChannel("test1", init), signaling_safety_); |
| 514 | EXPECT_EQ(DataChannelInterface::kOpen, dc->state()); |
| 515 | } |
| 516 |
| 517 | // Tests that an unordered DataChannel sends data as ordered until the OPEN_ACK |
| 518 | // message is received. |
| 519 | TEST_F(SctpDataChannelTest, SendUnorderedAfterReceivesOpenAck) { |
| 520 | SetChannelReady(); |
| 521 | InternalDataChannelInit init; |
| 522 | init.id = 1; |
| 523 | init.ordered = false; |
| 524 | rtc::scoped_refptr<SctpDataChannel> dc = |
| 525 | controller_->CreateDataChannel("test1", init); |
| 526 | auto proxy = SctpDataChannel::CreateProxy(dc, signaling_safety_); |
| 527 |
| 528 | EXPECT_EQ_WAIT(DataChannelInterface::kOpen, proxy->state(), 1000); |
| 529 |
| 530 | // Sends a message and verifies it's ordered. |
| 531 | DataBuffer buffer("some data"); |
| 532 | proxy->SendAsync(buffer, nullptr); |
| 533 | EXPECT_TRUE(controller_->last_send_data_params().ordered); |
| 534 |
| 535 | // Emulates receiving an OPEN_ACK message. |
| 536 | rtc::CopyOnWriteBuffer payload; |
| 537 | WriteDataChannelOpenAckMessage(&payload); |
| 538 | network_thread_.BlockingCall( |
| 539 | [&] { dc->OnDataReceived(DataMessageType::kControl, payload); }); |
| 540 |
| 541 | // Sends another message and verifies it's unordered. |
| 542 | proxy->SendAsync(buffer, nullptr); |
| 543 | FlushNetworkThreadAndPendingOperations(); |
| 544 | EXPECT_FALSE(controller_->last_send_data_params().ordered); |
| 545 | } |
| 546 |
| 547 | // TODO(tommi): This test uses `Send()`. Remove once fully deprecated. |
| 548 | TEST_F(SctpDataChannelTest, DeprecatedSendUnorderedAfterReceivesOpenAck) { |
| 549 | SetChannelReady(); |
| 550 | InternalDataChannelInit init; |
| 551 | init.id = 1; |
| 552 | init.ordered = false; |
| 553 | rtc::scoped_refptr<SctpDataChannel> dc = |
| 554 | controller_->CreateDataChannel("test1", init); |
| 555 | auto proxy = SctpDataChannel::CreateProxy(dc, signaling_safety_); |
| 556 |
| 557 | EXPECT_EQ_WAIT(DataChannelInterface::kOpen, proxy->state(), 1000); |
| 558 |
| 559 | // Sends a message and verifies it's ordered. |
| 560 | DataBuffer buffer("some data"); |
| 561 | ASSERT_TRUE(proxy->Send(buffer)); |
| 562 | EXPECT_TRUE(controller_->last_send_data_params().ordered); |
| 563 |
| 564 | // Emulates receiving an OPEN_ACK message. |
| 565 | rtc::CopyOnWriteBuffer payload; |
| 566 | WriteDataChannelOpenAckMessage(&payload); |
| 567 | network_thread_.BlockingCall( |
| 568 | [&] { dc->OnDataReceived(DataMessageType::kControl, payload); }); |
| 569 |
| 570 | // Sends another message and verifies it's unordered. |
| 571 | ASSERT_TRUE(proxy->Send(buffer)); |
| 572 | EXPECT_FALSE(controller_->last_send_data_params().ordered); |
| 573 | } |
| 574 |
| 575 | // Tests that an unordered DataChannel sends unordered data after any DATA |
| 576 | // message is received. |
| 577 | TEST_F(SctpDataChannelTest, SendUnorderedAfterReceiveData) { |
| 578 | SetChannelReady(); |
| 579 | InternalDataChannelInit init; |
| 580 | init.id = 1; |
| 581 | init.ordered = false; |
| 582 | rtc::scoped_refptr<SctpDataChannel> dc = |
| 583 | controller_->CreateDataChannel("test1", init); |
| 584 | auto proxy = SctpDataChannel::CreateProxy(dc, signaling_safety_); |
| 585 |
| 586 | EXPECT_EQ_WAIT(DataChannelInterface::kOpen, proxy->state(), 1000); |
| 587 |
| 588 | // Emulates receiving a DATA message. |
| 589 | DataBuffer buffer("data"); |
| 590 | network_thread_.BlockingCall( |
| 591 | [&] { dc->OnDataReceived(DataMessageType::kText, buffer.data); }); |
| 592 |
| 593 | // Sends a message and verifies it's unordered. |
| 594 | proxy->SendAsync(buffer, nullptr); |
| 595 | FlushNetworkThreadAndPendingOperations(); |
| 596 | EXPECT_FALSE(controller_->last_send_data_params().ordered); |
| 597 | } |
| 598 |
| 599 | // TODO(tommi): This test uses `Send()`. Remove once fully deprecated. |
| 600 | TEST_F(SctpDataChannelTest, DeprecatedSendUnorderedAfterReceiveData) { |
| 601 | SetChannelReady(); |
| 602 | InternalDataChannelInit init; |
| 603 | init.id = 1; |
| 604 | init.ordered = false; |
| 605 | rtc::scoped_refptr<SctpDataChannel> dc = |
| 606 | controller_->CreateDataChannel("test1", init); |
| 607 | auto proxy = SctpDataChannel::CreateProxy(dc, signaling_safety_); |
| 608 |
| 609 | EXPECT_EQ_WAIT(DataChannelInterface::kOpen, proxy->state(), 1000); |
| 610 |
| 611 | // Emulates receiving a DATA message. |
| 612 | DataBuffer buffer("data"); |
| 613 | network_thread_.BlockingCall( |
| 614 | [&] { dc->OnDataReceived(DataMessageType::kText, buffer.data); }); |
| 615 |
| 616 | // Sends a message and verifies it's unordered. |
| 617 | ASSERT_TRUE(proxy->Send(buffer)); |
| 618 | EXPECT_FALSE(controller_->last_send_data_params().ordered); |
| 619 | } |
| 620 |
| 621 | // Tests that the channel can't open until it's successfully sent the OPEN |
| 622 | // message. |
| 623 | TEST_F(SctpDataChannelTest, OpenWaitsForOpenMesssage) { |
| 624 | DataBuffer buffer("foo"); |
| 625 |
| 626 | controller_->set_send_blocked(true); |
| 627 | SetChannelReady(); |
| 628 | EXPECT_EQ(DataChannelInterface::kConnecting, channel_->state()); |
| 629 | controller_->set_send_blocked(false); |
| 630 | EXPECT_EQ_WAIT(DataChannelInterface::kOpen, channel_->state(), 1000); |
| 631 | EXPECT_EQ(DataMessageType::kControl, |
| 632 | controller_->last_send_data_params().type); |
| 633 | } |
| 634 |
| 635 | // Tests that close first makes sure all queued data gets sent. |
| 636 | TEST_F(SctpDataChannelTest, QueuedCloseFlushes) { |
| 637 | DataBuffer buffer("foo"); |
| 638 |
| 639 | controller_->set_send_blocked(true); |
| 640 | SetChannelReady(); |
| 641 | EXPECT_EQ(DataChannelInterface::kConnecting, channel_->state()); |
| 642 | controller_->set_send_blocked(false); |
| 643 | EXPECT_EQ_WAIT(DataChannelInterface::kOpen, channel_->state(), 1000); |
| 644 | controller_->set_send_blocked(true); |
| 645 | channel_->SendAsync(buffer, nullptr); |
| 646 | channel_->Close(); |
| 647 | controller_->set_send_blocked(false); |
| 648 | EXPECT_EQ_WAIT(DataChannelInterface::kClosed, channel_->state(), 1000); |
| 649 | EXPECT_TRUE(channel_->error().ok()); |
| 650 | EXPECT_EQ(DataMessageType::kText, controller_->last_send_data_params().type); |
| 651 | } |
| 652 |
| 653 | // TODO(tommi): This test uses `Send()`. Remove once fully deprecated. |
| 654 | TEST_F(SctpDataChannelTest, DeprecatedQueuedCloseFlushes) { |
| 655 | DataBuffer buffer("foo"); |
| 656 |
| 657 | controller_->set_send_blocked(true); |
| 658 | SetChannelReady(); |
| 659 | EXPECT_EQ(DataChannelInterface::kConnecting, channel_->state()); |
| 660 | controller_->set_send_blocked(false); |
| 661 | EXPECT_EQ_WAIT(DataChannelInterface::kOpen, channel_->state(), 1000); |
| 662 | controller_->set_send_blocked(true); |
| 663 | channel_->Send(buffer); |
| 664 | channel_->Close(); |
| 665 | controller_->set_send_blocked(false); |
| 666 | EXPECT_EQ_WAIT(DataChannelInterface::kClosed, channel_->state(), 1000); |
| 667 | EXPECT_TRUE(channel_->error().ok()); |
| 668 | EXPECT_EQ(DataMessageType::kText, controller_->last_send_data_params().type); |
| 669 | } |
| 670 |
| 671 | // Tests that messages are sent with the right id. |
| 672 | TEST_F(SctpDataChannelTest, SendDataId) { |
| 673 | SetChannelSid(inner_channel_, StreamId(1)); |
| 674 | SetChannelReady(); |
| 675 | DataBuffer buffer("data"); |
| 676 | channel_->SendAsync(buffer, nullptr); |
| 677 | FlushNetworkThreadAndPendingOperations(); |
| 678 | EXPECT_EQ(1, controller_->last_sid()); |
| 679 | } |
| 680 |
| 681 | // TODO(tommi): This test uses `Send()`. Remove once fully deprecated. |
| 682 | TEST_F(SctpDataChannelTest, DeprecatedSendDataId) { |
| 683 | SetChannelSid(inner_channel_, StreamId(1)); |
| 684 | SetChannelReady(); |
| 685 | DataBuffer buffer("data"); |
| 686 | EXPECT_TRUE(channel_->Send(buffer)); |
| 687 | EXPECT_EQ(1, controller_->last_sid()); |
| 688 | } |
| 689 |
| 690 | // Tests that the incoming messages with right ids are accepted. |
| 691 | TEST_F(SctpDataChannelTest, ReceiveDataWithValidId) { |
| 692 | SetChannelSid(inner_channel_, StreamId(1)); |
| 693 | SetChannelReady(); |
| 694 |
| 695 | AddObserver(); |
| 696 |
| 697 | DataBuffer buffer("abcd"); |
| 698 | network_thread_.BlockingCall([&] { |
| 699 | inner_channel_->OnDataReceived(DataMessageType::kText, buffer.data); |
| 700 | }); |
| 701 | run_loop_.Flush(); |
| 702 | EXPECT_EQ(1U, observer_->messages_received()); |
| 703 | } |
| 704 |
| 705 | // Tests that no CONTROL message is sent if the datachannel is negotiated and |
| 706 | // not created from an OPEN message. |
| 707 | TEST_F(SctpDataChannelTest, NoMsgSentIfNegotiatedAndNotFromOpenMsg) { |
| 708 | InternalDataChannelInit config; |
| 709 | config.id = 1; |
| 710 | config.negotiated = true; |
| 711 | config.open_handshake_role = InternalDataChannelInit::kNone; |
| 712 |
| 713 | SetChannelReady(); |
| 714 | rtc::scoped_refptr<SctpDataChannel> dc = |
| 715 | controller_->CreateDataChannel("test1", config); |
| 716 | auto proxy = SctpDataChannel::CreateProxy(dc, signaling_safety_); |
| 717 |
| 718 | EXPECT_EQ_WAIT(DataChannelInterface::kOpen, proxy->state(), 1000); |
| 719 | EXPECT_EQ(0, controller_->last_sid()); |
| 720 | } |
| 721 |
| 722 | // Tests that DataChannel::messages_received() and DataChannel::bytes_received() |
| 723 | // are correct, receiving data both while not open and while open. |
| 724 | TEST_F(SctpDataChannelTest, VerifyMessagesAndBytesReceived) { |
| 725 | AddObserver(); |
| 726 | std::vector<DataBuffer> buffers({ |
| 727 | DataBuffer("message 1"), |
| 728 | DataBuffer("msg 2"), |
| 729 | DataBuffer("message three"), |
| 730 | DataBuffer("quadra message"), |
| 731 | DataBuffer("fifthmsg"), |
| 732 | DataBuffer("message of the beast"), |
| 733 | }); |
| 734 |
| 735 | SetChannelSid(inner_channel_, StreamId(1)); |
| 736 |
| 737 | // Default values. |
| 738 | EXPECT_EQ(0U, channel_->messages_received()); |
| 739 | EXPECT_EQ(0U, channel_->bytes_received()); |
| 740 |
| 741 | // Receive three buffers while data channel isn't open. |
| 742 | network_thread_.BlockingCall([&] { |
| 743 | for (int i : {0, 1, 2}) |
| 744 | inner_channel_->OnDataReceived(DataMessageType::kText, buffers[i].data); |
| 745 | }); |
| 746 | EXPECT_EQ(0U, observer_->messages_received()); |
| 747 | EXPECT_EQ(0U, channel_->messages_received()); |
| 748 | EXPECT_EQ(0U, channel_->bytes_received()); |
| 749 |
| 750 | // Open channel and make sure everything was received. |
| 751 | SetChannelReady(); |
| 752 | size_t bytes_received = |
| 753 | buffers[0].size() + buffers[1].size() + buffers[2].size(); |
| 754 | EXPECT_EQ(3U, observer_->messages_received()); |
| 755 | EXPECT_EQ(3U, channel_->messages_received()); |
| 756 | EXPECT_EQ(bytes_received, channel_->bytes_received()); |
| 757 |
| 758 | // Receive three buffers while open. |
| 759 | network_thread_.BlockingCall([&] { |
| 760 | for (int i : {3, 4, 5}) |
| 761 | inner_channel_->OnDataReceived(DataMessageType::kText, buffers[i].data); |
| 762 | }); |
| 763 | run_loop_.Flush(); |
| 764 | bytes_received += buffers[3].size() + buffers[4].size() + buffers[5].size(); |
| 765 | EXPECT_EQ(6U, observer_->messages_received()); |
| 766 | EXPECT_EQ(6U, channel_->messages_received()); |
| 767 | EXPECT_EQ(bytes_received, channel_->bytes_received()); |
| 768 | } |
| 769 |
| 770 | // Tests that OPEN_ACK message is sent if the datachannel is created from an |
| 771 | // OPEN message. |
| 772 | TEST_F(SctpDataChannelTest, OpenAckSentIfCreatedFromOpenMessage) { |
| 773 | InternalDataChannelInit config; |
| 774 | config.id = 1; |
| 775 | config.negotiated = true; |
| 776 | config.open_handshake_role = InternalDataChannelInit::kAcker; |
| 777 |
| 778 | SetChannelReady(); |
| 779 | rtc::scoped_refptr<SctpDataChannel> dc = |
| 780 | controller_->CreateDataChannel("test1", config); |
| 781 | auto proxy = SctpDataChannel::CreateProxy(dc, signaling_safety_); |
| 782 |
| 783 | EXPECT_EQ_WAIT(DataChannelInterface::kOpen, proxy->state(), 1000); |
| 784 |
| 785 | EXPECT_EQ(config.id, controller_->last_sid()); |
| 786 | EXPECT_EQ(DataMessageType::kControl, |
| 787 | controller_->last_send_data_params().type); |
| 788 | } |
| 789 |
| 790 | // Tests the OPEN_ACK role assigned by InternalDataChannelInit. |
| 791 | TEST_F(SctpDataChannelTest, OpenAckRoleInitialization) { |
| 792 | InternalDataChannelInit init; |
| 793 | EXPECT_EQ(InternalDataChannelInit::kOpener, init.open_handshake_role); |
| 794 | EXPECT_FALSE(init.negotiated); |
| 795 |
| 796 | DataChannelInit base; |
| 797 | base.negotiated = true; |
| 798 | InternalDataChannelInit init2(base); |
| 799 | EXPECT_EQ(InternalDataChannelInit::kNone, init2.open_handshake_role); |
| 800 | } |
| 801 |
| 802 | // Tests that that Send() returns false if the sending buffer is full |
| 803 | // and the channel stays open. |
| 804 | TEST_F(SctpDataChannelTest, OpenWhenSendBufferFull) { |
| 805 | AddObserver(); |
| 806 | SetChannelReady(); |
| 807 |
| 808 | const size_t packetSize = 1024; |
| 809 |
| 810 | rtc::CopyOnWriteBuffer buffer(packetSize); |
| 811 | memset(buffer.MutableData(), 0, buffer.size()); |
| 812 |
| 813 | DataBuffer packet(buffer, true); |
| 814 | controller_->set_send_blocked(true); |
| 815 | size_t successful_send = 0u, failed_send = 0u; |
| 816 | auto send_complete = [&](RTCError err) { |
| 817 | err.ok() ? ++successful_send : ++failed_send; |
| 818 | }; |
| 819 |
| 820 | size_t count = DataChannelInterface::MaxSendQueueSize() / packetSize; |
| 821 | for (size_t i = 0; i < count; ++i) { |
| 822 | channel_->SendAsync(packet, send_complete); |
| 823 | } |
| 824 |
| 825 | // The sending buffer should be full, `Send()` returns false. |
| 826 | channel_->SendAsync(packet, std::move(send_complete)); |
| 827 | FlushNetworkThreadAndPendingOperations(); |
| 828 | EXPECT_TRUE(DataChannelInterface::kOpen == channel_->state()); |
| 829 | EXPECT_EQ(successful_send, count); |
| 830 | EXPECT_EQ(failed_send, 1u); |
| 831 | } |
| 832 |
| 833 | // TODO(tommi): This test uses `Send()`. Remove once fully deprecated. |
| 834 | TEST_F(SctpDataChannelTest, DeprecatedOpenWhenSendBufferFull) { |
| 835 | SetChannelReady(); |
| 836 |
| 837 | const size_t packetSize = 1024; |
| 838 |
| 839 | rtc::CopyOnWriteBuffer buffer(packetSize); |
| 840 | memset(buffer.MutableData(), 0, buffer.size()); |
| 841 |
| 842 | DataBuffer packet(buffer, true); |
| 843 | controller_->set_send_blocked(true); |
| 844 |
| 845 | for (size_t i = 0; i < DataChannelInterface::MaxSendQueueSize() / packetSize; |
| 846 | ++i) { |
| 847 | EXPECT_TRUE(channel_->Send(packet)); |
| 848 | } |
| 849 |
| 850 | // The sending buffer should be full, `Send()` returns false. |
| 851 | EXPECT_FALSE(channel_->Send(packet)); |
| 852 | EXPECT_TRUE(DataChannelInterface::kOpen == channel_->state()); |
| 853 | } |
| 854 |
| 855 | // Tests that the DataChannel is closed on transport errors. |
| 856 | TEST_F(SctpDataChannelTest, ClosedOnTransportError) { |
| 857 | SetChannelReady(); |
| 858 | DataBuffer buffer("abcd"); |
| 859 | controller_->set_transport_error(); |
| 860 |
| 861 | channel_->SendAsync(buffer, nullptr); |
| 862 |
| 863 | EXPECT_EQ(DataChannelInterface::kClosed, channel_->state()); |
| 864 | EXPECT_FALSE(channel_->error().ok()); |
| 865 | EXPECT_EQ(RTCErrorType::NETWORK_ERROR, channel_->error().type()); |
| 866 | EXPECT_EQ(RTCErrorDetailType::NONE, channel_->error().error_detail()); |
| 867 | } |
| 868 |
| 869 | // TODO(tommi): This test uses `Send()`. Remove once fully deprecated. |
| 870 | TEST_F(SctpDataChannelTest, DeprecatedClosedOnTransportError) { |
| 871 | SetChannelReady(); |
| 872 | DataBuffer buffer("abcd"); |
| 873 | controller_->set_transport_error(); |
| 874 |
| 875 | EXPECT_TRUE(channel_->Send(buffer)); |
| 876 |
| 877 | EXPECT_EQ(DataChannelInterface::kClosed, channel_->state()); |
| 878 | EXPECT_FALSE(channel_->error().ok()); |
| 879 | EXPECT_EQ(RTCErrorType::NETWORK_ERROR, channel_->error().type()); |
| 880 | EXPECT_EQ(RTCErrorDetailType::NONE, channel_->error().error_detail()); |
| 881 | } |
| 882 |
| 883 | // Tests that the DataChannel is closed if the received buffer is full. |
| 884 | TEST_F(SctpDataChannelTest, ClosedWhenReceivedBufferFull) { |
| 885 | SetChannelReady(); |
| 886 | rtc::CopyOnWriteBuffer buffer(1024); |
| 887 | memset(buffer.MutableData(), 0, buffer.size()); |
| 888 |
| 889 | network_thread_.BlockingCall([&] { |
| 890 | // Receiving data without having an observer will overflow the buffer. |
| 891 | for (size_t i = 0; i < 16 * 1024 + 1; ++i) { |
| 892 | inner_channel_->OnDataReceived(DataMessageType::kText, buffer); |
| 893 | } |
| 894 | }); |
| 895 | EXPECT_EQ(DataChannelInterface::kClosed, channel_->state()); |
| 896 | EXPECT_FALSE(channel_->error().ok()); |
| 897 | EXPECT_EQ(RTCErrorType::RESOURCE_EXHAUSTED, channel_->error().type()); |
| 898 | EXPECT_EQ(RTCErrorDetailType::NONE, channel_->error().error_detail()); |
| 899 | } |
| 900 |
| 901 | // Tests that sending empty data returns no error and keeps the channel open. |
| 902 | TEST_F(SctpDataChannelTest, SendEmptyData) { |
| 903 | SetChannelSid(inner_channel_, StreamId(1)); |
| 904 | SetChannelReady(); |
| 905 | EXPECT_EQ(DataChannelInterface::kOpen, channel_->state()); |
| 906 |
| 907 | DataBuffer buffer(""); |
| 908 | channel_->SendAsync(buffer, nullptr); |
| 909 | EXPECT_EQ(DataChannelInterface::kOpen, channel_->state()); |
| 910 | } |
| 911 |
| 912 | // TODO(tommi): This test uses `Send()`. Remove once fully deprecated. |
| 913 | TEST_F(SctpDataChannelTest, DeprecatedSendEmptyData) { |
| 914 | SetChannelSid(inner_channel_, StreamId(1)); |
| 915 | SetChannelReady(); |
| 916 | EXPECT_EQ(DataChannelInterface::kOpen, channel_->state()); |
| 917 |
| 918 | DataBuffer buffer(""); |
| 919 | EXPECT_TRUE(channel_->Send(buffer)); |
| 920 | EXPECT_EQ(DataChannelInterface::kOpen, channel_->state()); |
| 921 | } |
| 922 |
| 923 | // Tests that a channel can be closed without being opened or assigned an sid. |
| 924 | TEST_F(SctpDataChannelTest, NeverOpened) { |
| 925 | controller_->set_transport_available(true); |
| 926 | network_thread_.BlockingCall( |
| 927 | [&] { inner_channel_->OnTransportChannelCreated(); }); |
| 928 | channel_->Close(); |
| 929 | } |
| 930 |
| 931 | // Tests that a data channel that's not connected to a transport can transition |
| 932 | // directly to the `kClosed` state when closed. |
| 933 | // See also chromium:1421534. |
| 934 | TEST_F(SctpDataChannelTest, UnusedTransitionsDirectlyToClosed) { |
| 935 | channel_->Close(); |
| 936 | EXPECT_EQ(DataChannelInterface::kClosed, channel_->state()); |
| 937 | } |
| 938 |
| 939 | // Test that the data channel goes to the "closed" state (and doesn't crash) |
| 940 | // when its transport goes away, even while data is buffered. |
| 941 | TEST_F(SctpDataChannelTest, TransportDestroyedWhileDataBuffered) { |
| 942 | AddObserver(); |
| 943 | SetChannelReady(); |
| 944 |
| 945 | rtc::CopyOnWriteBuffer buffer(1024); |
| 946 | memset(buffer.MutableData(), 0, buffer.size()); |
| 947 | DataBuffer packet(buffer, true); |
| 948 |
| 949 | // Send a packet while sending is blocked so it ends up buffered. |
| 950 | controller_->set_send_blocked(true); |
| 951 | channel_->SendAsync(packet, nullptr); |
| 952 |
| 953 | // Tell the data channel that its transport is being destroyed. |
| 954 | // It should then stop using the transport (allowing us to delete it) and |
| 955 | // transition to the "closed" state. |
| 956 | RTCError error(RTCErrorType::OPERATION_ERROR_WITH_DATA, ""); |
| 957 | error.set_error_detail(RTCErrorDetailType::SCTP_FAILURE); |
| 958 | network_thread_.BlockingCall( |
| 959 | [&] { inner_channel_->OnTransportChannelClosed(error); }); |
| 960 | controller_.reset(nullptr); |
| 961 | EXPECT_EQ_WAIT(DataChannelInterface::kClosed, channel_->state(), |
| 962 | kDefaultTimeout); |
| 963 | EXPECT_FALSE(channel_->error().ok()); |
| 964 | EXPECT_EQ(RTCErrorType::OPERATION_ERROR_WITH_DATA, channel_->error().type()); |
| 965 | EXPECT_EQ(RTCErrorDetailType::SCTP_FAILURE, channel_->error().error_detail()); |
| 966 | } |
| 967 |
| 968 | // TODO(tommi): This test uses `Send()`. Remove once fully deprecated. |
| 969 | TEST_F(SctpDataChannelTest, DeprecatedTransportDestroyedWhileDataBuffered) { |
| 970 | SetChannelReady(); |
| 971 |
| 972 | rtc::CopyOnWriteBuffer buffer(1024); |
| 973 | memset(buffer.MutableData(), 0, buffer.size()); |
| 974 | DataBuffer packet(buffer, true); |
| 975 |
| 976 | // Send a packet while sending is blocked so it ends up buffered. |
| 977 | controller_->set_send_blocked(true); |
| 978 | EXPECT_TRUE(channel_->Send(packet)); |
| 979 |
| 980 | // Tell the data channel that its transport is being destroyed. |
| 981 | // It should then stop using the transport (allowing us to delete it) and |
| 982 | // transition to the "closed" state. |
| 983 | RTCError error(RTCErrorType::OPERATION_ERROR_WITH_DATA, ""); |
| 984 | error.set_error_detail(RTCErrorDetailType::SCTP_FAILURE); |
| 985 | network_thread_.BlockingCall( |
| 986 | [&] { inner_channel_->OnTransportChannelClosed(error); }); |
| 987 | controller_.reset(nullptr); |
| 988 | EXPECT_EQ_WAIT(DataChannelInterface::kClosed, channel_->state(), |
| 989 | kDefaultTimeout); |
| 990 | EXPECT_FALSE(channel_->error().ok()); |
| 991 | EXPECT_EQ(RTCErrorType::OPERATION_ERROR_WITH_DATA, channel_->error().type()); |
| 992 | EXPECT_EQ(RTCErrorDetailType::SCTP_FAILURE, channel_->error().error_detail()); |
| 993 | } |
| 994 |
| 995 | TEST_F(SctpDataChannelTest, TransportGotErrorCode) { |
| 996 | SetChannelReady(); |
| 997 |
| 998 | // Tell the data channel that its transport is being destroyed with an |
| 999 | // error code. |
| 1000 | // It should then report that error code. |
| 1001 | RTCError error(RTCErrorType::OPERATION_ERROR_WITH_DATA, |
| 1002 | "Transport channel closed"); |
| 1003 | error.set_error_detail(RTCErrorDetailType::SCTP_FAILURE); |
| 1004 | error.set_sctp_cause_code( |
| 1005 | static_cast<uint16_t>(cricket::SctpErrorCauseCode::kProtocolViolation)); |
| 1006 | network_thread_.BlockingCall( |
| 1007 | [&] { inner_channel_->OnTransportChannelClosed(error); }); |
| 1008 | controller_.reset(nullptr); |
| 1009 | EXPECT_EQ_WAIT(DataChannelInterface::kClosed, channel_->state(), |
| 1010 | kDefaultTimeout); |
| 1011 | EXPECT_FALSE(channel_->error().ok()); |
| 1012 | EXPECT_EQ(RTCErrorType::OPERATION_ERROR_WITH_DATA, channel_->error().type()); |
| 1013 | EXPECT_EQ(RTCErrorDetailType::SCTP_FAILURE, channel_->error().error_detail()); |
| 1014 | EXPECT_EQ( |
| 1015 | static_cast<uint16_t>(cricket::SctpErrorCauseCode::kProtocolViolation), |
| 1016 | channel_->error().sctp_cause_code()); |
| 1017 | } |
| 1018 |
| 1019 | class SctpSidAllocatorTest : public ::testing::Test { |
| 1020 | protected: |
| 1021 | SctpSidAllocator allocator_; |
| 1022 | }; |
| 1023 |
| 1024 | // Verifies that an even SCTP id is allocated for SSL_CLIENT and an odd id for |
| 1025 | // SSL_SERVER. |
| 1026 | TEST_F(SctpSidAllocatorTest, SctpIdAllocationBasedOnRole) { |
| 1027 | EXPECT_EQ(allocator_.AllocateSid(rtc::SSL_SERVER), StreamId(1)); |
| 1028 | EXPECT_EQ(allocator_.AllocateSid(rtc::SSL_CLIENT), StreamId(0)); |
| 1029 | EXPECT_EQ(allocator_.AllocateSid(rtc::SSL_SERVER), StreamId(3)); |
| 1030 | EXPECT_EQ(allocator_.AllocateSid(rtc::SSL_CLIENT), StreamId(2)); |
| 1031 | } |
| 1032 |
| 1033 | // Verifies that SCTP ids of existing DataChannels are not reused. |
| 1034 | TEST_F(SctpSidAllocatorTest, SctpIdAllocationNoReuse) { |
| 1035 | StreamId old_id(1); |
| 1036 | EXPECT_TRUE(allocator_.ReserveSid(old_id)); |
| 1037 |
| 1038 | StreamId new_id = allocator_.AllocateSid(rtc::SSL_SERVER); |
| 1039 | EXPECT_TRUE(new_id.HasValue()); |
| 1040 | EXPECT_NE(old_id, new_id); |
| 1041 |
| 1042 | old_id = StreamId(0); |
| 1043 | EXPECT_TRUE(allocator_.ReserveSid(old_id)); |
| 1044 | new_id = allocator_.AllocateSid(rtc::SSL_CLIENT); |
| 1045 | EXPECT_TRUE(new_id.HasValue()); |
| 1046 | EXPECT_NE(old_id, new_id); |
| 1047 | } |
| 1048 |
| 1049 | // Verifies that SCTP ids of removed DataChannels can be reused. |
| 1050 | TEST_F(SctpSidAllocatorTest, SctpIdReusedForRemovedDataChannel) { |
| 1051 | StreamId odd_id(1); |
| 1052 | StreamId even_id(0); |
| 1053 | EXPECT_TRUE(allocator_.ReserveSid(odd_id)); |
| 1054 | EXPECT_TRUE(allocator_.ReserveSid(even_id)); |
| 1055 |
| 1056 | StreamId allocated_id = allocator_.AllocateSid(rtc::SSL_SERVER); |
| 1057 | EXPECT_EQ(odd_id.stream_id_int() + 2, allocated_id.stream_id_int()); |
| 1058 |
| 1059 | allocated_id = allocator_.AllocateSid(rtc::SSL_CLIENT); |
| 1060 | EXPECT_EQ(even_id.stream_id_int() + 2, allocated_id.stream_id_int()); |
| 1061 |
| 1062 | allocated_id = allocator_.AllocateSid(rtc::SSL_SERVER); |
| 1063 | EXPECT_EQ(odd_id.stream_id_int() + 4, allocated_id.stream_id_int()); |
| 1064 |
| 1065 | allocated_id = allocator_.AllocateSid(rtc::SSL_CLIENT); |
| 1066 | EXPECT_EQ(even_id.stream_id_int() + 4, allocated_id.stream_id_int()); |
| 1067 |
| 1068 | allocator_.ReleaseSid(odd_id); |
| 1069 | allocator_.ReleaseSid(even_id); |
| 1070 |
| 1071 | // Verifies that removed ids are reused. |
| 1072 | allocated_id = allocator_.AllocateSid(rtc::SSL_SERVER); |
| 1073 | EXPECT_EQ(odd_id, allocated_id); |
| 1074 |
| 1075 | allocated_id = allocator_.AllocateSid(rtc::SSL_CLIENT); |
| 1076 | EXPECT_EQ(even_id, allocated_id); |
| 1077 |
| 1078 | // Verifies that used higher ids are not reused. |
| 1079 | allocated_id = allocator_.AllocateSid(rtc::SSL_SERVER); |
| 1080 | EXPECT_EQ(odd_id.stream_id_int() + 6, allocated_id.stream_id_int()); |
| 1081 |
| 1082 | allocated_id = allocator_.AllocateSid(rtc::SSL_CLIENT); |
| 1083 | EXPECT_EQ(even_id.stream_id_int() + 6, allocated_id.stream_id_int()); |
| 1084 | } |
| 1085 |
| 1086 | // Code coverage tests for default implementations in data_channel_interface.*. |
| 1087 | namespace { |
| 1088 | class NoImplDataChannel : public DataChannelInterface { |
| 1089 | public: |
| 1090 | NoImplDataChannel() = default; |
| 1091 | // Send and SendAsync implementations are public and implementation |
| 1092 | // is in data_channel_interface.cc. |
| 1093 |
| 1094 | private: |
| 1095 | // Implementation for pure virtual methods, just for compilation sake. |
| 1096 | void RegisterObserver(DataChannelObserver* observer) override {} |
| 1097 | void UnregisterObserver() override {} |
| 1098 | std::string label() const override { return ""; } |
| 1099 | bool reliable() const override { return false; } |
| 1100 | int id() const override { return -1; } |
| 1101 | DataState state() const override { return DataChannelInterface::kClosed; } |
| 1102 | uint32_t messages_sent() const override { return 0u; } |
| 1103 | uint64_t bytes_sent() const override { return 0u; } |
| 1104 | uint32_t messages_received() const override { return 0u; } |
| 1105 | uint64_t bytes_received() const override { return 0u; } |
| 1106 | uint64_t buffered_amount() const override { return 0u; } |
| 1107 | void Close() override {} |
| 1108 | }; |
| 1109 |
| 1110 | class NoImplObserver : public DataChannelObserver { |
| 1111 | public: |
| 1112 | NoImplObserver() = default; |
| 1113 |
| 1114 | private: |
| 1115 | void OnStateChange() override {} |
| 1116 | void OnMessage(const DataBuffer& buffer) override {} |
| 1117 | }; |
| 1118 | } // namespace |
| 1119 |
| 1120 | TEST(DataChannelInterfaceTest, Coverage) { |
| 1121 | auto channel = rtc::make_ref_counted<NoImplDataChannel>(); |
| 1122 | EXPECT_FALSE(channel->ordered()); |
| 1123 | EXPECT_EQ(channel->maxRetransmitTime(), 0u); |
| 1124 | EXPECT_EQ(channel->maxRetransmits(), 0u); |
| 1125 | EXPECT_FALSE(channel->maxRetransmitsOpt()); |
| 1126 | EXPECT_FALSE(channel->maxPacketLifeTime()); |
| 1127 | EXPECT_TRUE(channel->protocol().empty()); |
| 1128 | EXPECT_FALSE(channel->negotiated()); |
| 1129 | EXPECT_EQ(channel->MaxSendQueueSize(), 16u * 1024u * 1024u); |
| 1130 |
| 1131 | NoImplObserver observer; |
| 1132 | observer.OnBufferedAmountChange(0u); |
| 1133 | EXPECT_FALSE(observer.IsOkToCallOnTheNetworkThread()); |
| 1134 | } |
| 1135 |
| 1136 | #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
| 1137 |
| 1138 | TEST(DataChannelInterfaceDeathTest, SendDefaultImplDchecks) { |
| 1139 | auto channel = rtc::make_ref_counted<NoImplDataChannel>(); |
| 1140 | RTC_EXPECT_DEATH(channel->Send(DataBuffer("Foo")), "Check failed: false"); |
| 1141 | } |
| 1142 |
| 1143 | TEST(DataChannelInterfaceDeathTest, SendAsyncDefaultImplDchecks) { |
| 1144 | auto channel = rtc::make_ref_counted<NoImplDataChannel>(); |
| 1145 | RTC_EXPECT_DEATH(channel->SendAsync(DataBuffer("Foo"), nullptr), |
| 1146 | "Check failed: false"); |
| 1147 | } |
| 1148 | #endif // RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
| 1149 |
| 1150 | } // namespace |
| 1151 | } // namespace webrtc |
| 1152 |