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