wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 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. |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 11 | #include <string.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 12 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 13 | #include <memory> |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 14 | #include <vector> |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 15 | |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 16 | #include "pc/sctp_data_channel.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 17 | #include "pc/sctp_utils.h" |
| 18 | #include "pc/test/fake_data_channel_provider.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "rtc_base/gunit.h" |
Mirko Bonadei | e12c1fe | 2018-07-03 12:53:23 +0200 | [diff] [blame] | 20 | #include "rtc_base/numerics/safe_conversions.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 21 | #include "test/gtest.h" |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 22 | |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 23 | using webrtc::DataChannelInterface; |
| 24 | using webrtc::SctpDataChannel; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 25 | using webrtc::SctpSidAllocator; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 26 | |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 27 | static constexpr int kDefaultTimeout = 10000; |
| 28 | |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 29 | class FakeDataChannelObserver : public webrtc::DataChannelObserver { |
| 30 | public: |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 31 | FakeDataChannelObserver() |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 32 | : messages_received_(0), |
| 33 | on_state_change_count_(0), |
| 34 | on_buffered_amount_change_count_(0) {} |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 35 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 36 | void OnStateChange() { ++on_state_change_count_; } |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 37 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 38 | void OnBufferedAmountChange(uint64_t previous_amount) { |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 39 | ++on_buffered_amount_change_count_; |
| 40 | } |
| 41 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 42 | void OnMessage(const webrtc::DataBuffer& buffer) { ++messages_received_; } |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 43 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 44 | size_t messages_received() const { return messages_received_; } |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 45 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 46 | void ResetOnStateChangeCount() { on_state_change_count_ = 0; } |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 47 | |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 48 | void ResetOnBufferedAmountChangeCount() { |
| 49 | on_buffered_amount_change_count_ = 0; |
| 50 | } |
| 51 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 52 | size_t on_state_change_count() const { return on_state_change_count_; } |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 53 | |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 54 | size_t on_buffered_amount_change_count() const { |
| 55 | return on_buffered_amount_change_count_; |
| 56 | } |
| 57 | |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 58 | private: |
| 59 | size_t messages_received_; |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 60 | size_t on_state_change_count_; |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 61 | size_t on_buffered_amount_change_count_; |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 64 | // TODO(deadbeef): The fact that these tests use a fake provider makes them not |
| 65 | // too valuable. Should rewrite using the |
| 66 | // peerconnection_datachannel_unittest.cc infrastructure. |
Tomas Gunnarsson | 7d3cfbf | 2020-06-15 13:47:42 +0200 | [diff] [blame] | 67 | // TODO(bugs.webrtc.org/11547): Incorporate a dedicated network thread. |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 68 | class SctpDataChannelTest : public ::testing::Test { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 69 | protected: |
| 70 | SctpDataChannelTest() |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 71 | : provider_(new FakeDataChannelProvider()), |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 72 | webrtc_data_channel_(SctpDataChannel::Create(provider_.get(), |
| 73 | "test", |
| 74 | init_, |
| 75 | rtc::Thread::Current(), |
| 76 | rtc::Thread::Current())) {} |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 77 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 78 | void SetChannelReady() { |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 79 | provider_->set_transport_available(true); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 80 | webrtc_data_channel_->OnTransportChannelCreated(); |
| 81 | if (webrtc_data_channel_->id() < 0) { |
| 82 | webrtc_data_channel_->SetSctpSid(0); |
| 83 | } |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 84 | provider_->set_ready_to_send(true); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 85 | } |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 86 | |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 87 | void AddObserver() { |
| 88 | observer_.reset(new FakeDataChannelObserver()); |
| 89 | webrtc_data_channel_->RegisterObserver(observer_.get()); |
| 90 | } |
| 91 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 92 | webrtc::InternalDataChannelInit init_; |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 93 | std::unique_ptr<FakeDataChannelProvider> provider_; |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 94 | std::unique_ptr<FakeDataChannelObserver> observer_; |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 95 | rtc::scoped_refptr<SctpDataChannel> webrtc_data_channel_; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 98 | class StateSignalsListener : public sigslot::has_slots<> { |
| 99 | public: |
| 100 | int opened_count() const { return opened_count_; } |
| 101 | int closed_count() const { return closed_count_; } |
| 102 | |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 103 | void OnSignalOpened(DataChannelInterface* data_channel) { ++opened_count_; } |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 104 | |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 105 | void OnSignalClosed(DataChannelInterface* data_channel) { ++closed_count_; } |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 106 | |
| 107 | private: |
| 108 | int opened_count_ = 0; |
| 109 | int closed_count_ = 0; |
| 110 | }; |
| 111 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 112 | // Verifies that the data channel is connected to the transport after creation. |
| 113 | TEST_F(SctpDataChannelTest, ConnectedToTransportOnCreated) { |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 114 | provider_->set_transport_available(true); |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 115 | rtc::scoped_refptr<SctpDataChannel> dc = |
| 116 | SctpDataChannel::Create(provider_.get(), "test1", init_, |
| 117 | rtc::Thread::Current(), rtc::Thread::Current()); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 118 | |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 119 | EXPECT_TRUE(provider_->IsConnected(dc.get())); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 120 | // The sid is not set yet, so it should not have added the streams. |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 121 | EXPECT_FALSE(provider_->IsSendStreamAdded(dc->id())); |
| 122 | EXPECT_FALSE(provider_->IsRecvStreamAdded(dc->id())); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 123 | |
| 124 | dc->SetSctpSid(0); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 125 | EXPECT_TRUE(provider_->IsSendStreamAdded(dc->id())); |
| 126 | EXPECT_TRUE(provider_->IsRecvStreamAdded(dc->id())); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | // Verifies that the data channel is connected to the transport if the transport |
| 130 | // is not available initially and becomes available later. |
| 131 | TEST_F(SctpDataChannelTest, ConnectedAfterTransportBecomesAvailable) { |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 132 | EXPECT_FALSE(provider_->IsConnected(webrtc_data_channel_.get())); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 133 | |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 134 | provider_->set_transport_available(true); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 135 | webrtc_data_channel_->OnTransportChannelCreated(); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 136 | EXPECT_TRUE(provider_->IsConnected(webrtc_data_channel_.get())); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 137 | } |
| 138 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 139 | // Tests the state of the data channel. |
| 140 | TEST_F(SctpDataChannelTest, StateTransition) { |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 141 | StateSignalsListener state_signals_listener; |
| 142 | webrtc_data_channel_->SignalOpened.connect( |
| 143 | &state_signals_listener, &StateSignalsListener::OnSignalOpened); |
| 144 | webrtc_data_channel_->SignalClosed.connect( |
| 145 | &state_signals_listener, &StateSignalsListener::OnSignalClosed); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 146 | EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, |
| 147 | webrtc_data_channel_->state()); |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 148 | EXPECT_EQ(state_signals_listener.opened_count(), 0); |
| 149 | EXPECT_EQ(state_signals_listener.closed_count(), 0); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 150 | SetChannelReady(); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 151 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 152 | EXPECT_EQ(webrtc::DataChannelInterface::kOpen, webrtc_data_channel_->state()); |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 153 | EXPECT_EQ(state_signals_listener.opened_count(), 1); |
| 154 | EXPECT_EQ(state_signals_listener.closed_count(), 0); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 155 | webrtc_data_channel_->Close(); |
| 156 | EXPECT_EQ(webrtc::DataChannelInterface::kClosed, |
| 157 | webrtc_data_channel_->state()); |
Harald Alvestrand | dfbfb46 | 2019-12-08 05:55:43 +0100 | [diff] [blame] | 158 | EXPECT_TRUE(webrtc_data_channel_->error().ok()); |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 159 | EXPECT_EQ(state_signals_listener.opened_count(), 1); |
| 160 | EXPECT_EQ(state_signals_listener.closed_count(), 1); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 161 | // Verifies that it's disconnected from the transport. |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 162 | EXPECT_FALSE(provider_->IsConnected(webrtc_data_channel_.get())); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 163 | } |
| 164 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 165 | // Tests that DataChannel::buffered_amount() is correct after the channel is |
| 166 | // blocked. |
| 167 | TEST_F(SctpDataChannelTest, BufferedAmountWhenBlocked) { |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 168 | AddObserver(); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 169 | SetChannelReady(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 170 | webrtc::DataBuffer buffer("abcd"); |
| 171 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
Marina Ciocea | e448a3f | 2019-03-04 15:52:21 +0100 | [diff] [blame] | 172 | size_t successful_send_count = 1; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 173 | |
| 174 | EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); |
Marina Ciocea | e448a3f | 2019-03-04 15:52:21 +0100 | [diff] [blame] | 175 | EXPECT_EQ(successful_send_count, |
| 176 | observer_->on_buffered_amount_change_count()); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 177 | |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 178 | provider_->set_send_blocked(true); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 179 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 180 | const int number_of_packets = 3; |
| 181 | for (int i = 0; i < number_of_packets; ++i) { |
| 182 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 183 | } |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 184 | EXPECT_EQ(buffer.data.size() * number_of_packets, |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 185 | webrtc_data_channel_->buffered_amount()); |
Marina Ciocea | e448a3f | 2019-03-04 15:52:21 +0100 | [diff] [blame] | 186 | EXPECT_EQ(successful_send_count, |
| 187 | observer_->on_buffered_amount_change_count()); |
| 188 | |
| 189 | provider_->set_send_blocked(false); |
| 190 | successful_send_count += number_of_packets; |
| 191 | EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); |
| 192 | EXPECT_EQ(successful_send_count, |
Mirko Bonadei | e12c1fe | 2018-07-03 12:53:23 +0200 | [diff] [blame] | 193 | observer_->on_buffered_amount_change_count()); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | // Tests that the queued data are sent when the channel transitions from blocked |
| 197 | // to unblocked. |
| 198 | TEST_F(SctpDataChannelTest, QueuedDataSentWhenUnblocked) { |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 199 | AddObserver(); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 200 | SetChannelReady(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 201 | webrtc::DataBuffer buffer("abcd"); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 202 | provider_->set_send_blocked(true); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 203 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 204 | |
Marina Ciocea | e448a3f | 2019-03-04 15:52:21 +0100 | [diff] [blame] | 205 | EXPECT_EQ(0U, observer_->on_buffered_amount_change_count()); |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 206 | |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 207 | provider_->set_send_blocked(false); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 208 | SetChannelReady(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 209 | EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); |
Marina Ciocea | e448a3f | 2019-03-04 15:52:21 +0100 | [diff] [blame] | 210 | EXPECT_EQ(1U, observer_->on_buffered_amount_change_count()); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 211 | } |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 212 | |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 213 | // Tests that no crash when the channel is blocked right away while trying to |
| 214 | // send queued data. |
| 215 | TEST_F(SctpDataChannelTest, BlockedWhenSendQueuedDataNoCrash) { |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 216 | AddObserver(); |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 217 | SetChannelReady(); |
| 218 | webrtc::DataBuffer buffer("abcd"); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 219 | provider_->set_send_blocked(true); |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 220 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
Marina Ciocea | e448a3f | 2019-03-04 15:52:21 +0100 | [diff] [blame] | 221 | EXPECT_EQ(0U, observer_->on_buffered_amount_change_count()); |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 222 | |
| 223 | // Set channel ready while it is still blocked. |
| 224 | SetChannelReady(); |
| 225 | EXPECT_EQ(buffer.size(), webrtc_data_channel_->buffered_amount()); |
Marina Ciocea | e448a3f | 2019-03-04 15:52:21 +0100 | [diff] [blame] | 226 | EXPECT_EQ(0U, observer_->on_buffered_amount_change_count()); |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 227 | |
| 228 | // Unblock the channel to send queued data again, there should be no crash. |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 229 | provider_->set_send_blocked(false); |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 230 | SetChannelReady(); |
| 231 | EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); |
Marina Ciocea | e448a3f | 2019-03-04 15:52:21 +0100 | [diff] [blame] | 232 | EXPECT_EQ(1U, observer_->on_buffered_amount_change_count()); |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 233 | } |
| 234 | |
hbos | 84ffdee | 2016-10-12 14:14:39 -0700 | [diff] [blame] | 235 | // Tests that DataChannel::messages_sent() and DataChannel::bytes_sent() are |
| 236 | // correct, sending data both while unblocked and while blocked. |
| 237 | TEST_F(SctpDataChannelTest, VerifyMessagesAndBytesSent) { |
| 238 | AddObserver(); |
| 239 | SetChannelReady(); |
| 240 | std::vector<webrtc::DataBuffer> buffers({ |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 241 | webrtc::DataBuffer("message 1"), |
| 242 | webrtc::DataBuffer("msg 2"), |
| 243 | webrtc::DataBuffer("message three"), |
| 244 | webrtc::DataBuffer("quadra message"), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 245 | webrtc::DataBuffer("fifthmsg"), |
| 246 | webrtc::DataBuffer("message of the beast"), |
hbos | 84ffdee | 2016-10-12 14:14:39 -0700 | [diff] [blame] | 247 | }); |
| 248 | |
| 249 | // Default values. |
| 250 | EXPECT_EQ(0U, webrtc_data_channel_->messages_sent()); |
| 251 | EXPECT_EQ(0U, webrtc_data_channel_->bytes_sent()); |
| 252 | |
| 253 | // Send three buffers while not blocked. |
| 254 | provider_->set_send_blocked(false); |
| 255 | EXPECT_TRUE(webrtc_data_channel_->Send(buffers[0])); |
| 256 | EXPECT_TRUE(webrtc_data_channel_->Send(buffers[1])); |
| 257 | EXPECT_TRUE(webrtc_data_channel_->Send(buffers[2])); |
| 258 | size_t bytes_sent = buffers[0].size() + buffers[1].size() + buffers[2].size(); |
| 259 | EXPECT_EQ_WAIT(0U, webrtc_data_channel_->buffered_amount(), kDefaultTimeout); |
| 260 | EXPECT_EQ(3U, webrtc_data_channel_->messages_sent()); |
| 261 | EXPECT_EQ(bytes_sent, webrtc_data_channel_->bytes_sent()); |
| 262 | |
| 263 | // Send three buffers while blocked, queuing the buffers. |
| 264 | provider_->set_send_blocked(true); |
| 265 | EXPECT_TRUE(webrtc_data_channel_->Send(buffers[3])); |
| 266 | EXPECT_TRUE(webrtc_data_channel_->Send(buffers[4])); |
| 267 | EXPECT_TRUE(webrtc_data_channel_->Send(buffers[5])); |
| 268 | size_t bytes_queued = |
| 269 | buffers[3].size() + buffers[4].size() + buffers[5].size(); |
| 270 | EXPECT_EQ(bytes_queued, webrtc_data_channel_->buffered_amount()); |
| 271 | EXPECT_EQ(3U, webrtc_data_channel_->messages_sent()); |
| 272 | EXPECT_EQ(bytes_sent, webrtc_data_channel_->bytes_sent()); |
| 273 | |
| 274 | // Unblock and make sure everything was sent. |
| 275 | provider_->set_send_blocked(false); |
| 276 | EXPECT_EQ_WAIT(0U, webrtc_data_channel_->buffered_amount(), kDefaultTimeout); |
| 277 | bytes_sent += bytes_queued; |
| 278 | EXPECT_EQ(6U, webrtc_data_channel_->messages_sent()); |
| 279 | EXPECT_EQ(bytes_sent, webrtc_data_channel_->bytes_sent()); |
| 280 | } |
| 281 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 282 | // Tests that the queued control message is sent when channel is ready. |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 283 | TEST_F(SctpDataChannelTest, OpenMessageSent) { |
| 284 | // Initially the id is unassigned. |
| 285 | EXPECT_EQ(-1, webrtc_data_channel_->id()); |
| 286 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 287 | SetChannelReady(); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 288 | EXPECT_GE(webrtc_data_channel_->id(), 0); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 289 | EXPECT_EQ(cricket::DMT_CONTROL, provider_->last_send_data_params().type); |
| 290 | EXPECT_EQ(provider_->last_send_data_params().ssrc, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 291 | static_cast<uint32_t>(webrtc_data_channel_->id())); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 292 | } |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 293 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 294 | TEST_F(SctpDataChannelTest, QueuedOpenMessageSent) { |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 295 | provider_->set_send_blocked(true); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 296 | SetChannelReady(); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 297 | provider_->set_send_blocked(false); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 298 | |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 299 | EXPECT_EQ(cricket::DMT_CONTROL, provider_->last_send_data_params().type); |
| 300 | EXPECT_EQ(provider_->last_send_data_params().ssrc, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 301 | static_cast<uint32_t>(webrtc_data_channel_->id())); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 302 | } |
| 303 | |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 304 | // Tests that the DataChannel created after transport gets ready can enter OPEN |
| 305 | // state. |
| 306 | TEST_F(SctpDataChannelTest, LateCreatedChannelTransitionToOpen) { |
| 307 | SetChannelReady(); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 308 | webrtc::InternalDataChannelInit init; |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 309 | init.id = 1; |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 310 | rtc::scoped_refptr<SctpDataChannel> dc = |
| 311 | SctpDataChannel::Create(provider_.get(), "test1", init, |
| 312 | rtc::Thread::Current(), rtc::Thread::Current()); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 313 | EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, dc->state()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 314 | EXPECT_TRUE_WAIT(webrtc::DataChannelInterface::kOpen == dc->state(), 1000); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 315 | } |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 316 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 317 | // Tests that an unordered DataChannel sends data as ordered until the OPEN_ACK |
| 318 | // message is received. |
| 319 | TEST_F(SctpDataChannelTest, SendUnorderedAfterReceivesOpenAck) { |
| 320 | SetChannelReady(); |
| 321 | webrtc::InternalDataChannelInit init; |
| 322 | init.id = 1; |
| 323 | init.ordered = false; |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 324 | rtc::scoped_refptr<SctpDataChannel> dc = |
| 325 | SctpDataChannel::Create(provider_.get(), "test1", init, |
| 326 | rtc::Thread::Current(), rtc::Thread::Current()); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 327 | |
| 328 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); |
| 329 | |
| 330 | // Sends a message and verifies it's ordered. |
| 331 | webrtc::DataBuffer buffer("some data"); |
| 332 | ASSERT_TRUE(dc->Send(buffer)); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 333 | EXPECT_TRUE(provider_->last_send_data_params().ordered); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 334 | |
| 335 | // Emulates receiving an OPEN_ACK message. |
| 336 | cricket::ReceiveDataParams params; |
| 337 | params.ssrc = init.id; |
| 338 | params.type = cricket::DMT_CONTROL; |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 339 | rtc::CopyOnWriteBuffer payload; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 340 | webrtc::WriteDataChannelOpenAckMessage(&payload); |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 341 | dc->OnDataReceived(params, payload); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 342 | |
| 343 | // Sends another message and verifies it's unordered. |
| 344 | ASSERT_TRUE(dc->Send(buffer)); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 345 | EXPECT_FALSE(provider_->last_send_data_params().ordered); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | // Tests that an unordered DataChannel sends unordered data after any DATA |
| 349 | // message is received. |
| 350 | TEST_F(SctpDataChannelTest, SendUnorderedAfterReceiveData) { |
| 351 | SetChannelReady(); |
| 352 | webrtc::InternalDataChannelInit init; |
| 353 | init.id = 1; |
| 354 | init.ordered = false; |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 355 | rtc::scoped_refptr<SctpDataChannel> dc = |
| 356 | SctpDataChannel::Create(provider_.get(), "test1", init, |
| 357 | rtc::Thread::Current(), rtc::Thread::Current()); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 358 | |
| 359 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); |
| 360 | |
| 361 | // Emulates receiving a DATA message. |
| 362 | cricket::ReceiveDataParams params; |
| 363 | params.ssrc = init.id; |
| 364 | params.type = cricket::DMT_TEXT; |
| 365 | webrtc::DataBuffer buffer("data"); |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 366 | dc->OnDataReceived(params, buffer.data); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 367 | |
| 368 | // Sends a message and verifies it's unordered. |
| 369 | ASSERT_TRUE(dc->Send(buffer)); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 370 | EXPECT_FALSE(provider_->last_send_data_params().ordered); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 373 | // Tests that the channel can't open until it's successfully sent the OPEN |
| 374 | // message. |
| 375 | TEST_F(SctpDataChannelTest, OpenWaitsForOpenMesssage) { |
| 376 | webrtc::DataBuffer buffer("foo"); |
| 377 | |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 378 | provider_->set_send_blocked(true); |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 379 | SetChannelReady(); |
| 380 | EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, |
| 381 | webrtc_data_channel_->state()); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 382 | provider_->set_send_blocked(false); |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 383 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, |
| 384 | webrtc_data_channel_->state(), 1000); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 385 | EXPECT_EQ(cricket::DMT_CONTROL, provider_->last_send_data_params().type); |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | // Tests that close first makes sure all queued data gets sent. |
| 389 | TEST_F(SctpDataChannelTest, QueuedCloseFlushes) { |
| 390 | webrtc::DataBuffer buffer("foo"); |
| 391 | |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 392 | provider_->set_send_blocked(true); |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 393 | SetChannelReady(); |
| 394 | EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, |
| 395 | webrtc_data_channel_->state()); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 396 | provider_->set_send_blocked(false); |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 397 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, |
| 398 | webrtc_data_channel_->state(), 1000); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 399 | provider_->set_send_blocked(true); |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 400 | webrtc_data_channel_->Send(buffer); |
| 401 | webrtc_data_channel_->Close(); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 402 | provider_->set_send_blocked(false); |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 403 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, |
| 404 | webrtc_data_channel_->state(), 1000); |
Harald Alvestrand | dfbfb46 | 2019-12-08 05:55:43 +0100 | [diff] [blame] | 405 | EXPECT_TRUE(webrtc_data_channel_->error().ok()); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 406 | EXPECT_EQ(cricket::DMT_TEXT, provider_->last_send_data_params().type); |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 407 | } |
| 408 | |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 409 | // Tests that messages are sent with the right ssrc. |
| 410 | TEST_F(SctpDataChannelTest, SendDataSsrc) { |
| 411 | webrtc_data_channel_->SetSctpSid(1); |
| 412 | SetChannelReady(); |
| 413 | webrtc::DataBuffer buffer("data"); |
| 414 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 415 | EXPECT_EQ(1U, provider_->last_send_data_params().ssrc); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | // Tests that the incoming messages with wrong ssrcs are rejected. |
| 419 | TEST_F(SctpDataChannelTest, ReceiveDataWithInvalidSsrc) { |
| 420 | webrtc_data_channel_->SetSctpSid(1); |
| 421 | SetChannelReady(); |
| 422 | |
| 423 | AddObserver(); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 424 | |
| 425 | cricket::ReceiveDataParams params; |
| 426 | params.ssrc = 0; |
| 427 | webrtc::DataBuffer buffer("abcd"); |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 428 | webrtc_data_channel_->OnDataReceived(params, buffer.data); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 429 | |
| 430 | EXPECT_EQ(0U, observer_->messages_received()); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | // Tests that the incoming messages with right ssrcs are acceted. |
| 434 | TEST_F(SctpDataChannelTest, ReceiveDataWithValidSsrc) { |
| 435 | webrtc_data_channel_->SetSctpSid(1); |
| 436 | SetChannelReady(); |
| 437 | |
| 438 | AddObserver(); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 439 | |
| 440 | cricket::ReceiveDataParams params; |
| 441 | params.ssrc = 1; |
| 442 | webrtc::DataBuffer buffer("abcd"); |
| 443 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 444 | webrtc_data_channel_->OnDataReceived(params, buffer.data); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 445 | EXPECT_EQ(1U, observer_->messages_received()); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 446 | } |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 447 | |
| 448 | // Tests that no CONTROL message is sent if the datachannel is negotiated and |
| 449 | // not created from an OPEN message. |
| 450 | TEST_F(SctpDataChannelTest, NoMsgSentIfNegotiatedAndNotFromOpenMsg) { |
| 451 | webrtc::InternalDataChannelInit config; |
| 452 | config.id = 1; |
| 453 | config.negotiated = true; |
| 454 | config.open_handshake_role = webrtc::InternalDataChannelInit::kNone; |
| 455 | |
| 456 | SetChannelReady(); |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 457 | rtc::scoped_refptr<SctpDataChannel> dc = |
| 458 | SctpDataChannel::Create(provider_.get(), "test1", config, |
| 459 | rtc::Thread::Current(), rtc::Thread::Current()); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 460 | |
| 461 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 462 | EXPECT_EQ(0U, provider_->last_send_data_params().ssrc); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 463 | } |
| 464 | |
hbos | 84ffdee | 2016-10-12 14:14:39 -0700 | [diff] [blame] | 465 | // Tests that DataChannel::messages_received() and DataChannel::bytes_received() |
| 466 | // are correct, receiving data both while not open and while open. |
| 467 | TEST_F(SctpDataChannelTest, VerifyMessagesAndBytesReceived) { |
| 468 | AddObserver(); |
| 469 | std::vector<webrtc::DataBuffer> buffers({ |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 470 | webrtc::DataBuffer("message 1"), |
| 471 | webrtc::DataBuffer("msg 2"), |
| 472 | webrtc::DataBuffer("message three"), |
| 473 | webrtc::DataBuffer("quadra message"), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 474 | webrtc::DataBuffer("fifthmsg"), |
| 475 | webrtc::DataBuffer("message of the beast"), |
hbos | 84ffdee | 2016-10-12 14:14:39 -0700 | [diff] [blame] | 476 | }); |
| 477 | |
| 478 | webrtc_data_channel_->SetSctpSid(1); |
| 479 | cricket::ReceiveDataParams params; |
| 480 | params.ssrc = 1; |
| 481 | |
| 482 | // Default values. |
| 483 | EXPECT_EQ(0U, webrtc_data_channel_->messages_received()); |
| 484 | EXPECT_EQ(0U, webrtc_data_channel_->bytes_received()); |
| 485 | |
| 486 | // Receive three buffers while data channel isn't open. |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 487 | webrtc_data_channel_->OnDataReceived(params, buffers[0].data); |
| 488 | webrtc_data_channel_->OnDataReceived(params, buffers[1].data); |
| 489 | webrtc_data_channel_->OnDataReceived(params, buffers[2].data); |
hbos | 84ffdee | 2016-10-12 14:14:39 -0700 | [diff] [blame] | 490 | EXPECT_EQ(0U, observer_->messages_received()); |
| 491 | EXPECT_EQ(0U, webrtc_data_channel_->messages_received()); |
| 492 | EXPECT_EQ(0U, webrtc_data_channel_->bytes_received()); |
| 493 | |
| 494 | // Open channel and make sure everything was received. |
| 495 | SetChannelReady(); |
| 496 | size_t bytes_received = |
| 497 | buffers[0].size() + buffers[1].size() + buffers[2].size(); |
| 498 | EXPECT_EQ(3U, observer_->messages_received()); |
| 499 | EXPECT_EQ(3U, webrtc_data_channel_->messages_received()); |
| 500 | EXPECT_EQ(bytes_received, webrtc_data_channel_->bytes_received()); |
| 501 | |
| 502 | // Receive three buffers while open. |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 503 | webrtc_data_channel_->OnDataReceived(params, buffers[3].data); |
| 504 | webrtc_data_channel_->OnDataReceived(params, buffers[4].data); |
| 505 | webrtc_data_channel_->OnDataReceived(params, buffers[5].data); |
hbos | 84ffdee | 2016-10-12 14:14:39 -0700 | [diff] [blame] | 506 | bytes_received += buffers[3].size() + buffers[4].size() + buffers[5].size(); |
| 507 | EXPECT_EQ(6U, observer_->messages_received()); |
| 508 | EXPECT_EQ(6U, webrtc_data_channel_->messages_received()); |
| 509 | EXPECT_EQ(bytes_received, webrtc_data_channel_->bytes_received()); |
| 510 | } |
| 511 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 512 | // Tests that OPEN_ACK message is sent if the datachannel is created from an |
| 513 | // OPEN message. |
| 514 | TEST_F(SctpDataChannelTest, OpenAckSentIfCreatedFromOpenMessage) { |
| 515 | webrtc::InternalDataChannelInit config; |
| 516 | config.id = 1; |
| 517 | config.negotiated = true; |
| 518 | config.open_handshake_role = webrtc::InternalDataChannelInit::kAcker; |
| 519 | |
| 520 | SetChannelReady(); |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 521 | rtc::scoped_refptr<SctpDataChannel> dc = |
| 522 | SctpDataChannel::Create(provider_.get(), "test1", config, |
| 523 | rtc::Thread::Current(), rtc::Thread::Current()); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 524 | |
| 525 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); |
| 526 | |
| 527 | EXPECT_EQ(static_cast<unsigned int>(config.id), |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 528 | provider_->last_send_data_params().ssrc); |
| 529 | EXPECT_EQ(cricket::DMT_CONTROL, provider_->last_send_data_params().type); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | // Tests the OPEN_ACK role assigned by InternalDataChannelInit. |
| 533 | TEST_F(SctpDataChannelTest, OpenAckRoleInitialization) { |
| 534 | webrtc::InternalDataChannelInit init; |
| 535 | EXPECT_EQ(webrtc::InternalDataChannelInit::kOpener, init.open_handshake_role); |
| 536 | EXPECT_FALSE(init.negotiated); |
| 537 | |
| 538 | webrtc::DataChannelInit base; |
| 539 | base.negotiated = true; |
| 540 | webrtc::InternalDataChannelInit init2(base); |
| 541 | EXPECT_EQ(webrtc::InternalDataChannelInit::kNone, init2.open_handshake_role); |
| 542 | } |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 543 | |
| 544 | // Tests that the DataChannel is closed if the sending buffer is full. |
| 545 | TEST_F(SctpDataChannelTest, ClosedWhenSendBufferFull) { |
| 546 | SetChannelReady(); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 547 | |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 548 | rtc::CopyOnWriteBuffer buffer(1024); |
Danil Chapovalov | e15dc58 | 2021-01-07 15:24:05 +0100 | [diff] [blame] | 549 | memset(buffer.MutableData(), 0, buffer.size()); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 550 | |
| 551 | webrtc::DataBuffer packet(buffer, true); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 552 | provider_->set_send_blocked(true); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 553 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 554 | for (size_t i = 0; i < 16 * 1024 + 1; ++i) { |
| 555 | EXPECT_TRUE(webrtc_data_channel_->Send(packet)); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 558 | EXPECT_TRUE( |
| 559 | webrtc::DataChannelInterface::kClosed == webrtc_data_channel_->state() || |
| 560 | webrtc::DataChannelInterface::kClosing == webrtc_data_channel_->state()); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | // Tests that the DataChannel is closed on transport errors. |
| 564 | TEST_F(SctpDataChannelTest, ClosedOnTransportError) { |
| 565 | SetChannelReady(); |
| 566 | webrtc::DataBuffer buffer("abcd"); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 567 | provider_->set_transport_error(); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 568 | |
| 569 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 570 | |
| 571 | EXPECT_EQ(webrtc::DataChannelInterface::kClosed, |
| 572 | webrtc_data_channel_->state()); |
Harald Alvestrand | dfbfb46 | 2019-12-08 05:55:43 +0100 | [diff] [blame] | 573 | EXPECT_FALSE(webrtc_data_channel_->error().ok()); |
| 574 | EXPECT_EQ(webrtc::RTCErrorType::NETWORK_ERROR, |
| 575 | webrtc_data_channel_->error().type()); |
| 576 | EXPECT_EQ(webrtc::RTCErrorDetailType::NONE, |
| 577 | webrtc_data_channel_->error().error_detail()); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 578 | } |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 579 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 580 | // Tests that the DataChannel is closed if the received buffer is full. |
| 581 | TEST_F(SctpDataChannelTest, ClosedWhenReceivedBufferFull) { |
| 582 | SetChannelReady(); |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 583 | rtc::CopyOnWriteBuffer buffer(1024); |
Danil Chapovalov | e15dc58 | 2021-01-07 15:24:05 +0100 | [diff] [blame] | 584 | memset(buffer.MutableData(), 0, buffer.size()); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 585 | |
| 586 | cricket::ReceiveDataParams params; |
| 587 | params.ssrc = 0; |
| 588 | |
| 589 | // Receiving data without having an observer will overflow the buffer. |
| 590 | for (size_t i = 0; i < 16 * 1024 + 1; ++i) { |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 591 | webrtc_data_channel_->OnDataReceived(params, buffer); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 592 | } |
| 593 | EXPECT_EQ(webrtc::DataChannelInterface::kClosed, |
| 594 | webrtc_data_channel_->state()); |
Harald Alvestrand | dfbfb46 | 2019-12-08 05:55:43 +0100 | [diff] [blame] | 595 | EXPECT_FALSE(webrtc_data_channel_->error().ok()); |
| 596 | EXPECT_EQ(webrtc::RTCErrorType::RESOURCE_EXHAUSTED, |
| 597 | webrtc_data_channel_->error().type()); |
| 598 | EXPECT_EQ(webrtc::RTCErrorDetailType::NONE, |
| 599 | webrtc_data_channel_->error().error_detail()); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 600 | } |
jiayl@webrtc.org | 3edbaaf | 2014-07-18 23:57:50 +0000 | [diff] [blame] | 601 | |
| 602 | // Tests that sending empty data returns no error and keeps the channel open. |
| 603 | TEST_F(SctpDataChannelTest, SendEmptyData) { |
| 604 | webrtc_data_channel_->SetSctpSid(1); |
| 605 | SetChannelReady(); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 606 | EXPECT_EQ(webrtc::DataChannelInterface::kOpen, webrtc_data_channel_->state()); |
jiayl@webrtc.org | 3edbaaf | 2014-07-18 23:57:50 +0000 | [diff] [blame] | 607 | |
| 608 | webrtc::DataBuffer buffer(""); |
| 609 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 610 | EXPECT_EQ(webrtc::DataChannelInterface::kOpen, webrtc_data_channel_->state()); |
jiayl@webrtc.org | 3edbaaf | 2014-07-18 23:57:50 +0000 | [diff] [blame] | 611 | } |
bemasc@webrtc.org | 9b5467e | 2014-12-04 23:16:52 +0000 | [diff] [blame] | 612 | |
| 613 | // Tests that a channel can be closed without being opened or assigned an sid. |
| 614 | TEST_F(SctpDataChannelTest, NeverOpened) { |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 615 | provider_->set_transport_available(true); |
bemasc@webrtc.org | 9b5467e | 2014-12-04 23:16:52 +0000 | [diff] [blame] | 616 | webrtc_data_channel_->OnTransportChannelCreated(); |
| 617 | webrtc_data_channel_->Close(); |
| 618 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 619 | |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 620 | // Test that the data channel goes to the "closed" state (and doesn't crash) |
| 621 | // when its transport goes away, even while data is buffered. |
| 622 | TEST_F(SctpDataChannelTest, TransportDestroyedWhileDataBuffered) { |
| 623 | SetChannelReady(); |
| 624 | |
| 625 | rtc::CopyOnWriteBuffer buffer(1024); |
Danil Chapovalov | e15dc58 | 2021-01-07 15:24:05 +0100 | [diff] [blame] | 626 | memset(buffer.MutableData(), 0, buffer.size()); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 627 | webrtc::DataBuffer packet(buffer, true); |
| 628 | |
| 629 | // Send a packet while sending is blocked so it ends up buffered. |
| 630 | provider_->set_send_blocked(true); |
| 631 | EXPECT_TRUE(webrtc_data_channel_->Send(packet)); |
| 632 | |
Harald Alvestrand | 408cb4b | 2019-11-16 12:09:08 +0100 | [diff] [blame] | 633 | // Tell the data channel that its transport is being destroyed. |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 634 | // It should then stop using the transport (allowing us to delete it) and |
| 635 | // transition to the "closed" state. |
Harald Alvestrand | 408cb4b | 2019-11-16 12:09:08 +0100 | [diff] [blame] | 636 | webrtc_data_channel_->OnTransportChannelClosed(); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 637 | provider_.reset(nullptr); |
| 638 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, |
| 639 | webrtc_data_channel_->state(), kDefaultTimeout); |
Harald Alvestrand | dfbfb46 | 2019-12-08 05:55:43 +0100 | [diff] [blame] | 640 | EXPECT_FALSE(webrtc_data_channel_->error().ok()); |
Harald Alvestrand | 37e42be | 2020-05-12 12:59:02 +0200 | [diff] [blame] | 641 | EXPECT_EQ(webrtc::RTCErrorType::OPERATION_ERROR_WITH_DATA, |
Harald Alvestrand | dfbfb46 | 2019-12-08 05:55:43 +0100 | [diff] [blame] | 642 | webrtc_data_channel_->error().type()); |
Harald Alvestrand | 37e42be | 2020-05-12 12:59:02 +0200 | [diff] [blame] | 643 | EXPECT_EQ(webrtc::RTCErrorDetailType::SCTP_FAILURE, |
Harald Alvestrand | dfbfb46 | 2019-12-08 05:55:43 +0100 | [diff] [blame] | 644 | webrtc_data_channel_->error().error_detail()); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 647 | class SctpSidAllocatorTest : public ::testing::Test { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 648 | protected: |
| 649 | SctpSidAllocator allocator_; |
| 650 | }; |
| 651 | |
| 652 | // Verifies that an even SCTP id is allocated for SSL_CLIENT and an odd id for |
| 653 | // SSL_SERVER. |
| 654 | TEST_F(SctpSidAllocatorTest, SctpIdAllocationBasedOnRole) { |
| 655 | int id; |
| 656 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &id)); |
| 657 | EXPECT_EQ(1, id); |
| 658 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &id)); |
| 659 | EXPECT_EQ(0, id); |
| 660 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &id)); |
| 661 | EXPECT_EQ(3, id); |
| 662 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &id)); |
| 663 | EXPECT_EQ(2, id); |
| 664 | } |
| 665 | |
| 666 | // Verifies that SCTP ids of existing DataChannels are not reused. |
| 667 | TEST_F(SctpSidAllocatorTest, SctpIdAllocationNoReuse) { |
| 668 | int old_id = 1; |
| 669 | EXPECT_TRUE(allocator_.ReserveSid(old_id)); |
| 670 | |
| 671 | int new_id; |
| 672 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &new_id)); |
| 673 | EXPECT_NE(old_id, new_id); |
| 674 | |
| 675 | old_id = 0; |
| 676 | EXPECT_TRUE(allocator_.ReserveSid(old_id)); |
| 677 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &new_id)); |
| 678 | EXPECT_NE(old_id, new_id); |
| 679 | } |
| 680 | |
| 681 | // Verifies that SCTP ids of removed DataChannels can be reused. |
| 682 | TEST_F(SctpSidAllocatorTest, SctpIdReusedForRemovedDataChannel) { |
| 683 | int odd_id = 1; |
| 684 | int even_id = 0; |
| 685 | EXPECT_TRUE(allocator_.ReserveSid(odd_id)); |
| 686 | EXPECT_TRUE(allocator_.ReserveSid(even_id)); |
| 687 | |
| 688 | int allocated_id = -1; |
| 689 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id)); |
| 690 | EXPECT_EQ(odd_id + 2, allocated_id); |
| 691 | |
| 692 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id)); |
| 693 | EXPECT_EQ(even_id + 2, allocated_id); |
| 694 | |
| 695 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id)); |
| 696 | EXPECT_EQ(odd_id + 4, allocated_id); |
| 697 | |
| 698 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id)); |
| 699 | EXPECT_EQ(even_id + 4, allocated_id); |
| 700 | |
| 701 | allocator_.ReleaseSid(odd_id); |
| 702 | allocator_.ReleaseSid(even_id); |
| 703 | |
| 704 | // Verifies that removed ids are reused. |
| 705 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id)); |
| 706 | EXPECT_EQ(odd_id, allocated_id); |
| 707 | |
| 708 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id)); |
| 709 | EXPECT_EQ(even_id, allocated_id); |
| 710 | |
| 711 | // Verifies that used higher ids are not reused. |
| 712 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id)); |
| 713 | EXPECT_EQ(odd_id + 6, allocated_id); |
| 714 | |
| 715 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id)); |
| 716 | EXPECT_EQ(even_id + 6, allocated_id); |
| 717 | } |