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 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 11 | #include <string.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 12 | |
kwiberg | d1fe281 | 2016-04-27 13:47:29 | [diff] [blame] | 13 | #include <memory> |
Steve Anton | 36b29d1 | 2017-10-30 16:57:42 | [diff] [blame] | 14 | #include <vector> |
kwiberg | d1fe281 | 2016-04-27 13:47:29 | [diff] [blame] | 15 | |
Florent Castelli | dcb9ffc | 2021-06-29 12:58:23 | [diff] [blame] | 16 | #include "media/sctp/sctp_transport_internal.h" |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 17 | #include "pc/sctp_data_channel.h" |
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 | |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 24 | using webrtc::DataChannelInterface; |
| 25 | using webrtc::SctpDataChannel; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 26 | using webrtc::SctpSidAllocator; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 | [diff] [blame] | 27 | |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 17:10:31 | [diff] [blame] | 28 | static constexpr int kDefaultTimeout = 10000; |
| 29 | |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 | [diff] [blame] | 30 | class FakeDataChannelObserver : public webrtc::DataChannelObserver { |
| 31 | public: |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 | [diff] [blame] | 32 | FakeDataChannelObserver() |
bemasc | 0edd50c | 2015-07-01 20:34:33 | [diff] [blame] | 33 | : messages_received_(0), |
| 34 | on_state_change_count_(0), |
| 35 | on_buffered_amount_change_count_(0) {} |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 | [diff] [blame] | 36 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 37 | void OnStateChange() { ++on_state_change_count_; } |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 | [diff] [blame] | 38 | |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 39 | void OnBufferedAmountChange(uint64_t previous_amount) { |
bemasc | 0edd50c | 2015-07-01 20:34:33 | [diff] [blame] | 40 | ++on_buffered_amount_change_count_; |
| 41 | } |
| 42 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 43 | void OnMessage(const webrtc::DataBuffer& buffer) { ++messages_received_; } |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 | [diff] [blame] | 44 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 45 | size_t messages_received() const { return messages_received_; } |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 | [diff] [blame] | 46 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 47 | void ResetOnStateChangeCount() { on_state_change_count_ = 0; } |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 | [diff] [blame] | 48 | |
bemasc | 0edd50c | 2015-07-01 20:34:33 | [diff] [blame] | 49 | void ResetOnBufferedAmountChangeCount() { |
| 50 | on_buffered_amount_change_count_ = 0; |
| 51 | } |
| 52 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 53 | size_t on_state_change_count() const { return on_state_change_count_; } |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 | [diff] [blame] | 54 | |
bemasc | 0edd50c | 2015-07-01 20:34:33 | [diff] [blame] | 55 | size_t on_buffered_amount_change_count() const { |
| 56 | return on_buffered_amount_change_count_; |
| 57 | } |
| 58 | |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 | [diff] [blame] | 59 | private: |
| 60 | size_t messages_received_; |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 | [diff] [blame] | 61 | size_t on_state_change_count_; |
bemasc | 0edd50c | 2015-07-01 20:34:33 | [diff] [blame] | 62 | size_t on_buffered_amount_change_count_; |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 | [diff] [blame] | 63 | }; |
| 64 | |
Taylor Brandstetter | cdd05f0 | 2018-05-31 20:23:32 | [diff] [blame] | 65 | // TODO(deadbeef): The fact that these tests use a fake provider makes them not |
| 66 | // too valuable. Should rewrite using the |
| 67 | // peerconnection_datachannel_unittest.cc infrastructure. |
Tomas Gunnarsson | 7d3cfbf | 2020-06-15 11:47:42 | [diff] [blame] | 68 | // TODO(bugs.webrtc.org/11547): Incorporate a dedicated network thread. |
Mirko Bonadei | 6a489f2 | 2019-04-09 13:11:12 | [diff] [blame] | 69 | class SctpDataChannelTest : public ::testing::Test { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 | [diff] [blame] | 70 | protected: |
| 71 | SctpDataChannelTest() |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 17:10:31 | [diff] [blame] | 72 | : provider_(new FakeDataChannelProvider()), |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 73 | webrtc_data_channel_(SctpDataChannel::Create(provider_.get(), |
| 74 | "test", |
| 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_; |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 96 | rtc::scoped_refptr<SctpDataChannel> 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 | |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 104 | void OnSignalOpened(DataChannelInterface* data_channel) { ++opened_count_; } |
hbos | 82ebe02 | 2016-11-14 09:41:09 | [diff] [blame] | 105 | |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 106 | void OnSignalClosed(DataChannelInterface* 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); |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 116 | rtc::scoped_refptr<SctpDataChannel> dc = |
| 117 | SctpDataChannel::Create(provider_.get(), "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); |
Florent Castelli | d95b149 | 2021-05-10 09:29:56 | [diff] [blame] | 290 | EXPECT_EQ(webrtc::DataMessageType::kControl, |
| 291 | provider_->last_send_data_params().type); |
| 292 | EXPECT_EQ(provider_->last_sid(), 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 | |
Florent Castelli | d95b149 | 2021-05-10 09:29:56 | [diff] [blame] | 300 | EXPECT_EQ(webrtc::DataMessageType::kControl, |
| 301 | provider_->last_send_data_params().type); |
| 302 | EXPECT_EQ(provider_->last_sid(), 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 | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 311 | rtc::scoped_refptr<SctpDataChannel> dc = |
| 312 | SctpDataChannel::Create(provider_.get(), "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 | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 325 | rtc::scoped_refptr<SctpDataChannel> dc = |
| 326 | SctpDataChannel::Create(provider_.get(), "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; |
Harald Alvestrand | 7af57c6 | 2021-04-16 11:12:14 | [diff] [blame] | 338 | params.sid = init.id; |
Florent Castelli | d95b149 | 2021-05-10 09:29:56 | [diff] [blame] | 339 | params.type = webrtc::DataMessageType::kControl; |
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 | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 356 | rtc::scoped_refptr<SctpDataChannel> dc = |
| 357 | SctpDataChannel::Create(provider_.get(), "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; |
Harald Alvestrand | 7af57c6 | 2021-04-16 11:12:14 | [diff] [blame] | 364 | params.sid = init.id; |
Florent Castelli | d95b149 | 2021-05-10 09:29:56 | [diff] [blame] | 365 | params.type = webrtc::DataMessageType::kText; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 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); |
Florent Castelli | d95b149 | 2021-05-10 09:29:56 | [diff] [blame] | 386 | EXPECT_EQ(webrtc::DataMessageType::kControl, |
| 387 | provider_->last_send_data_params().type); |
Lally Singh | 5c6c6e0 | 2015-05-29 15:52:39 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | // Tests that close first makes sure all queued data gets sent. |
| 391 | TEST_F(SctpDataChannelTest, QueuedCloseFlushes) { |
| 392 | webrtc::DataBuffer buffer("foo"); |
| 393 | |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 17:10:31 | [diff] [blame] | 394 | provider_->set_send_blocked(true); |
Lally Singh | 5c6c6e0 | 2015-05-29 15:52:39 | [diff] [blame] | 395 | SetChannelReady(); |
| 396 | EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, |
| 397 | webrtc_data_channel_->state()); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 17:10:31 | [diff] [blame] | 398 | provider_->set_send_blocked(false); |
Lally Singh | 5c6c6e0 | 2015-05-29 15:52:39 | [diff] [blame] | 399 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, |
| 400 | webrtc_data_channel_->state(), 1000); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 17:10:31 | [diff] [blame] | 401 | provider_->set_send_blocked(true); |
Lally Singh | 5c6c6e0 | 2015-05-29 15:52:39 | [diff] [blame] | 402 | webrtc_data_channel_->Send(buffer); |
| 403 | webrtc_data_channel_->Close(); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 17:10:31 | [diff] [blame] | 404 | provider_->set_send_blocked(false); |
Lally Singh | 5c6c6e0 | 2015-05-29 15:52:39 | [diff] [blame] | 405 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, |
| 406 | webrtc_data_channel_->state(), 1000); |
Harald Alvestrand | dfbfb46 | 2019-12-08 04:55:43 | [diff] [blame] | 407 | EXPECT_TRUE(webrtc_data_channel_->error().ok()); |
Florent Castelli | d95b149 | 2021-05-10 09:29:56 | [diff] [blame] | 408 | EXPECT_EQ(webrtc::DataMessageType::kText, |
| 409 | provider_->last_send_data_params().type); |
Lally Singh | 5c6c6e0 | 2015-05-29 15:52:39 | [diff] [blame] | 410 | } |
| 411 | |
Harald Alvestrand | 7af57c6 | 2021-04-16 11:12:14 | [diff] [blame] | 412 | // Tests that messages are sent with the right id. |
| 413 | TEST_F(SctpDataChannelTest, SendDataId) { |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 | [diff] [blame] | 414 | webrtc_data_channel_->SetSctpSid(1); |
| 415 | SetChannelReady(); |
| 416 | webrtc::DataBuffer buffer("data"); |
| 417 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
Florent Castelli | d95b149 | 2021-05-10 09:29:56 | [diff] [blame] | 418 | EXPECT_EQ(1, provider_->last_sid()); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 | [diff] [blame] | 419 | } |
| 420 | |
Harald Alvestrand | 7af57c6 | 2021-04-16 11:12:14 | [diff] [blame] | 421 | // Tests that the incoming messages with wrong ids are rejected. |
| 422 | TEST_F(SctpDataChannelTest, ReceiveDataWithInvalidId) { |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 | [diff] [blame] | 423 | webrtc_data_channel_->SetSctpSid(1); |
| 424 | SetChannelReady(); |
| 425 | |
| 426 | AddObserver(); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 | [diff] [blame] | 427 | |
| 428 | cricket::ReceiveDataParams params; |
Harald Alvestrand | 7af57c6 | 2021-04-16 11:12:14 | [diff] [blame] | 429 | params.sid = 0; |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 | [diff] [blame] | 430 | webrtc::DataBuffer buffer("abcd"); |
deadbeef | 953c2ce | 2017-01-09 22:53:41 | [diff] [blame] | 431 | webrtc_data_channel_->OnDataReceived(params, buffer.data); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 | [diff] [blame] | 432 | |
| 433 | EXPECT_EQ(0U, observer_->messages_received()); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 | [diff] [blame] | 434 | } |
| 435 | |
Harald Alvestrand | 7af57c6 | 2021-04-16 11:12:14 | [diff] [blame] | 436 | // Tests that the incoming messages with right ids are accepted. |
| 437 | TEST_F(SctpDataChannelTest, ReceiveDataWithValidId) { |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 | [diff] [blame] | 438 | webrtc_data_channel_->SetSctpSid(1); |
| 439 | SetChannelReady(); |
| 440 | |
| 441 | AddObserver(); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 | [diff] [blame] | 442 | |
| 443 | cricket::ReceiveDataParams params; |
Harald Alvestrand | 7af57c6 | 2021-04-16 11:12:14 | [diff] [blame] | 444 | params.sid = 1; |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 | [diff] [blame] | 445 | webrtc::DataBuffer buffer("abcd"); |
| 446 | |
deadbeef | 953c2ce | 2017-01-09 22:53:41 | [diff] [blame] | 447 | webrtc_data_channel_->OnDataReceived(params, buffer.data); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 | [diff] [blame] | 448 | EXPECT_EQ(1U, observer_->messages_received()); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 | [diff] [blame] | 449 | } |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 450 | |
| 451 | // Tests that no CONTROL message is sent if the datachannel is negotiated and |
| 452 | // not created from an OPEN message. |
| 453 | TEST_F(SctpDataChannelTest, NoMsgSentIfNegotiatedAndNotFromOpenMsg) { |
| 454 | webrtc::InternalDataChannelInit config; |
| 455 | config.id = 1; |
| 456 | config.negotiated = true; |
| 457 | config.open_handshake_role = webrtc::InternalDataChannelInit::kNone; |
| 458 | |
| 459 | SetChannelReady(); |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 460 | rtc::scoped_refptr<SctpDataChannel> dc = |
| 461 | SctpDataChannel::Create(provider_.get(), "test1", config, |
| 462 | rtc::Thread::Current(), rtc::Thread::Current()); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 463 | |
| 464 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); |
Florent Castelli | d95b149 | 2021-05-10 09:29:56 | [diff] [blame] | 465 | EXPECT_EQ(0, provider_->last_sid()); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 466 | } |
| 467 | |
hbos | 84ffdee | 2016-10-12 21:14:39 | [diff] [blame] | 468 | // Tests that DataChannel::messages_received() and DataChannel::bytes_received() |
| 469 | // are correct, receiving data both while not open and while open. |
| 470 | TEST_F(SctpDataChannelTest, VerifyMessagesAndBytesReceived) { |
| 471 | AddObserver(); |
| 472 | std::vector<webrtc::DataBuffer> buffers({ |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 473 | webrtc::DataBuffer("message 1"), |
| 474 | webrtc::DataBuffer("msg 2"), |
| 475 | webrtc::DataBuffer("message three"), |
| 476 | webrtc::DataBuffer("quadra message"), |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 477 | webrtc::DataBuffer("fifthmsg"), |
| 478 | webrtc::DataBuffer("message of the beast"), |
hbos | 84ffdee | 2016-10-12 21:14:39 | [diff] [blame] | 479 | }); |
| 480 | |
| 481 | webrtc_data_channel_->SetSctpSid(1); |
| 482 | cricket::ReceiveDataParams params; |
Harald Alvestrand | 7af57c6 | 2021-04-16 11:12:14 | [diff] [blame] | 483 | params.sid = 1; |
hbos | 84ffdee | 2016-10-12 21:14:39 | [diff] [blame] | 484 | |
| 485 | // Default values. |
| 486 | EXPECT_EQ(0U, webrtc_data_channel_->messages_received()); |
| 487 | EXPECT_EQ(0U, webrtc_data_channel_->bytes_received()); |
| 488 | |
| 489 | // Receive three buffers while data channel isn't open. |
deadbeef | 953c2ce | 2017-01-09 22:53:41 | [diff] [blame] | 490 | webrtc_data_channel_->OnDataReceived(params, buffers[0].data); |
| 491 | webrtc_data_channel_->OnDataReceived(params, buffers[1].data); |
| 492 | webrtc_data_channel_->OnDataReceived(params, buffers[2].data); |
hbos | 84ffdee | 2016-10-12 21:14:39 | [diff] [blame] | 493 | EXPECT_EQ(0U, observer_->messages_received()); |
| 494 | EXPECT_EQ(0U, webrtc_data_channel_->messages_received()); |
| 495 | EXPECT_EQ(0U, webrtc_data_channel_->bytes_received()); |
| 496 | |
| 497 | // Open channel and make sure everything was received. |
| 498 | SetChannelReady(); |
| 499 | size_t bytes_received = |
| 500 | buffers[0].size() + buffers[1].size() + buffers[2].size(); |
| 501 | EXPECT_EQ(3U, observer_->messages_received()); |
| 502 | EXPECT_EQ(3U, webrtc_data_channel_->messages_received()); |
| 503 | EXPECT_EQ(bytes_received, webrtc_data_channel_->bytes_received()); |
| 504 | |
| 505 | // Receive three buffers while open. |
deadbeef | 953c2ce | 2017-01-09 22:53:41 | [diff] [blame] | 506 | webrtc_data_channel_->OnDataReceived(params, buffers[3].data); |
| 507 | webrtc_data_channel_->OnDataReceived(params, buffers[4].data); |
| 508 | webrtc_data_channel_->OnDataReceived(params, buffers[5].data); |
hbos | 84ffdee | 2016-10-12 21:14:39 | [diff] [blame] | 509 | bytes_received += buffers[3].size() + buffers[4].size() + buffers[5].size(); |
| 510 | EXPECT_EQ(6U, observer_->messages_received()); |
| 511 | EXPECT_EQ(6U, webrtc_data_channel_->messages_received()); |
| 512 | EXPECT_EQ(bytes_received, webrtc_data_channel_->bytes_received()); |
| 513 | } |
| 514 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 515 | // Tests that OPEN_ACK message is sent if the datachannel is created from an |
| 516 | // OPEN message. |
| 517 | TEST_F(SctpDataChannelTest, OpenAckSentIfCreatedFromOpenMessage) { |
| 518 | webrtc::InternalDataChannelInit config; |
| 519 | config.id = 1; |
| 520 | config.negotiated = true; |
| 521 | config.open_handshake_role = webrtc::InternalDataChannelInit::kAcker; |
| 522 | |
| 523 | SetChannelReady(); |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 524 | rtc::scoped_refptr<SctpDataChannel> dc = |
| 525 | SctpDataChannel::Create(provider_.get(), "test1", config, |
| 526 | rtc::Thread::Current(), rtc::Thread::Current()); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 527 | |
| 528 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); |
| 529 | |
Florent Castelli | d95b149 | 2021-05-10 09:29:56 | [diff] [blame] | 530 | EXPECT_EQ(config.id, provider_->last_sid()); |
| 531 | EXPECT_EQ(webrtc::DataMessageType::kControl, |
| 532 | provider_->last_send_data_params().type); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | // Tests the OPEN_ACK role assigned by InternalDataChannelInit. |
| 536 | TEST_F(SctpDataChannelTest, OpenAckRoleInitialization) { |
| 537 | webrtc::InternalDataChannelInit init; |
| 538 | EXPECT_EQ(webrtc::InternalDataChannelInit::kOpener, init.open_handshake_role); |
| 539 | EXPECT_FALSE(init.negotiated); |
| 540 | |
| 541 | webrtc::DataChannelInit base; |
| 542 | base.negotiated = true; |
| 543 | webrtc::InternalDataChannelInit init2(base); |
| 544 | EXPECT_EQ(webrtc::InternalDataChannelInit::kNone, init2.open_handshake_role); |
| 545 | } |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 | [diff] [blame] | 546 | |
| 547 | // Tests that the DataChannel is closed if the sending buffer is full. |
| 548 | TEST_F(SctpDataChannelTest, ClosedWhenSendBufferFull) { |
| 549 | SetChannelReady(); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 | [diff] [blame] | 550 | |
jbauch | eec21bd | 2016-03-20 13:15:43 | [diff] [blame] | 551 | rtc::CopyOnWriteBuffer buffer(1024); |
Danil Chapovalov | e15dc58 | 2021-01-07 14:24:05 | [diff] [blame] | 552 | memset(buffer.MutableData(), 0, buffer.size()); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 | [diff] [blame] | 553 | |
| 554 | webrtc::DataBuffer packet(buffer, true); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 17:10:31 | [diff] [blame] | 555 | provider_->set_send_blocked(true); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 | [diff] [blame] | 556 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 | [diff] [blame] | 557 | for (size_t i = 0; i < 16 * 1024 + 1; ++i) { |
| 558 | EXPECT_TRUE(webrtc_data_channel_->Send(packet)); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 | [diff] [blame] | 559 | } |
| 560 | |
Lally Singh | 5c6c6e0 | 2015-05-29 15:52:39 | [diff] [blame] | 561 | EXPECT_TRUE( |
| 562 | webrtc::DataChannelInterface::kClosed == webrtc_data_channel_->state() || |
| 563 | webrtc::DataChannelInterface::kClosing == webrtc_data_channel_->state()); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | // Tests that the DataChannel is closed on transport errors. |
| 567 | TEST_F(SctpDataChannelTest, ClosedOnTransportError) { |
| 568 | SetChannelReady(); |
| 569 | webrtc::DataBuffer buffer("abcd"); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 17:10:31 | [diff] [blame] | 570 | provider_->set_transport_error(); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 | [diff] [blame] | 571 | |
| 572 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 573 | |
| 574 | EXPECT_EQ(webrtc::DataChannelInterface::kClosed, |
| 575 | webrtc_data_channel_->state()); |
Harald Alvestrand | dfbfb46 | 2019-12-08 04:55:43 | [diff] [blame] | 576 | EXPECT_FALSE(webrtc_data_channel_->error().ok()); |
| 577 | EXPECT_EQ(webrtc::RTCErrorType::NETWORK_ERROR, |
| 578 | webrtc_data_channel_->error().type()); |
| 579 | EXPECT_EQ(webrtc::RTCErrorDetailType::NONE, |
| 580 | webrtc_data_channel_->error().error_detail()); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 | [diff] [blame] | 581 | } |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 | [diff] [blame] | 582 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 | [diff] [blame] | 583 | // Tests that the DataChannel is closed if the received buffer is full. |
| 584 | TEST_F(SctpDataChannelTest, ClosedWhenReceivedBufferFull) { |
| 585 | SetChannelReady(); |
jbauch | eec21bd | 2016-03-20 13:15:43 | [diff] [blame] | 586 | rtc::CopyOnWriteBuffer buffer(1024); |
Danil Chapovalov | e15dc58 | 2021-01-07 14:24:05 | [diff] [blame] | 587 | memset(buffer.MutableData(), 0, buffer.size()); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 | [diff] [blame] | 588 | |
| 589 | cricket::ReceiveDataParams params; |
Harald Alvestrand | 7af57c6 | 2021-04-16 11:12:14 | [diff] [blame] | 590 | params.sid = 0; |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 | [diff] [blame] | 591 | |
| 592 | // Receiving data without having an observer will overflow the buffer. |
| 593 | for (size_t i = 0; i < 16 * 1024 + 1; ++i) { |
deadbeef | 953c2ce | 2017-01-09 22:53:41 | [diff] [blame] | 594 | webrtc_data_channel_->OnDataReceived(params, buffer); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 | [diff] [blame] | 595 | } |
| 596 | EXPECT_EQ(webrtc::DataChannelInterface::kClosed, |
| 597 | webrtc_data_channel_->state()); |
Harald Alvestrand | dfbfb46 | 2019-12-08 04:55:43 | [diff] [blame] | 598 | EXPECT_FALSE(webrtc_data_channel_->error().ok()); |
| 599 | EXPECT_EQ(webrtc::RTCErrorType::RESOURCE_EXHAUSTED, |
| 600 | webrtc_data_channel_->error().type()); |
| 601 | EXPECT_EQ(webrtc::RTCErrorDetailType::NONE, |
| 602 | webrtc_data_channel_->error().error_detail()); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 | [diff] [blame] | 603 | } |
jiayl@webrtc.org | 3edbaaf | 2014-07-18 23:57:50 | [diff] [blame] | 604 | |
| 605 | // Tests that sending empty data returns no error and keeps the channel open. |
| 606 | TEST_F(SctpDataChannelTest, SendEmptyData) { |
| 607 | webrtc_data_channel_->SetSctpSid(1); |
| 608 | SetChannelReady(); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 609 | EXPECT_EQ(webrtc::DataChannelInterface::kOpen, webrtc_data_channel_->state()); |
jiayl@webrtc.org | 3edbaaf | 2014-07-18 23:57:50 | [diff] [blame] | 610 | |
| 611 | webrtc::DataBuffer buffer(""); |
| 612 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 613 | EXPECT_EQ(webrtc::DataChannelInterface::kOpen, webrtc_data_channel_->state()); |
jiayl@webrtc.org | 3edbaaf | 2014-07-18 23:57:50 | [diff] [blame] | 614 | } |
bemasc@webrtc.org | 9b5467e | 2014-12-04 23:16:52 | [diff] [blame] | 615 | |
| 616 | // Tests that a channel can be closed without being opened or assigned an sid. |
| 617 | TEST_F(SctpDataChannelTest, NeverOpened) { |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 17:10:31 | [diff] [blame] | 618 | provider_->set_transport_available(true); |
bemasc@webrtc.org | 9b5467e | 2014-12-04 23:16:52 | [diff] [blame] | 619 | webrtc_data_channel_->OnTransportChannelCreated(); |
| 620 | webrtc_data_channel_->Close(); |
| 621 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 622 | |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 17:10:31 | [diff] [blame] | 623 | // Test that the data channel goes to the "closed" state (and doesn't crash) |
| 624 | // when its transport goes away, even while data is buffered. |
| 625 | TEST_F(SctpDataChannelTest, TransportDestroyedWhileDataBuffered) { |
| 626 | SetChannelReady(); |
| 627 | |
| 628 | rtc::CopyOnWriteBuffer buffer(1024); |
Danil Chapovalov | e15dc58 | 2021-01-07 14:24:05 | [diff] [blame] | 629 | memset(buffer.MutableData(), 0, buffer.size()); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 17:10:31 | [diff] [blame] | 630 | webrtc::DataBuffer packet(buffer, true); |
| 631 | |
| 632 | // Send a packet while sending is blocked so it ends up buffered. |
| 633 | provider_->set_send_blocked(true); |
| 634 | EXPECT_TRUE(webrtc_data_channel_->Send(packet)); |
| 635 | |
Harald Alvestrand | 408cb4b | 2019-11-16 11:09:08 | [diff] [blame] | 636 | // Tell the data channel that its transport is being destroyed. |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 17:10:31 | [diff] [blame] | 637 | // It should then stop using the transport (allowing us to delete it) and |
| 638 | // transition to the "closed" state. |
Florent Castelli | dcb9ffc | 2021-06-29 12:58:23 | [diff] [blame] | 639 | webrtc::RTCError error(webrtc::RTCErrorType::OPERATION_ERROR_WITH_DATA, ""); |
| 640 | error.set_error_detail(webrtc::RTCErrorDetailType::SCTP_FAILURE); |
| 641 | webrtc_data_channel_->OnTransportChannelClosed(error); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 17:10:31 | [diff] [blame] | 642 | provider_.reset(nullptr); |
| 643 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, |
| 644 | webrtc_data_channel_->state(), kDefaultTimeout); |
Harald Alvestrand | dfbfb46 | 2019-12-08 04:55:43 | [diff] [blame] | 645 | EXPECT_FALSE(webrtc_data_channel_->error().ok()); |
Harald Alvestrand | 37e42be | 2020-05-12 10:59:02 | [diff] [blame] | 646 | EXPECT_EQ(webrtc::RTCErrorType::OPERATION_ERROR_WITH_DATA, |
Harald Alvestrand | dfbfb46 | 2019-12-08 04:55:43 | [diff] [blame] | 647 | webrtc_data_channel_->error().type()); |
Harald Alvestrand | 37e42be | 2020-05-12 10:59:02 | [diff] [blame] | 648 | EXPECT_EQ(webrtc::RTCErrorDetailType::SCTP_FAILURE, |
Harald Alvestrand | dfbfb46 | 2019-12-08 04:55:43 | [diff] [blame] | 649 | webrtc_data_channel_->error().error_detail()); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 17:10:31 | [diff] [blame] | 650 | } |
| 651 | |
Florent Castelli | dcb9ffc | 2021-06-29 12:58:23 | [diff] [blame] | 652 | TEST_F(SctpDataChannelTest, TransportGotErrorCode) { |
| 653 | SetChannelReady(); |
| 654 | |
| 655 | // Tell the data channel that its transport is being destroyed with an |
| 656 | // error code. |
| 657 | // It should then report that error code. |
| 658 | webrtc::RTCError error(webrtc::RTCErrorType::OPERATION_ERROR_WITH_DATA, |
| 659 | "Transport channel closed"); |
| 660 | error.set_error_detail(webrtc::RTCErrorDetailType::SCTP_FAILURE); |
| 661 | error.set_sctp_cause_code( |
| 662 | static_cast<uint16_t>(cricket::SctpErrorCauseCode::kProtocolViolation)); |
| 663 | webrtc_data_channel_->OnTransportChannelClosed(error); |
| 664 | provider_.reset(nullptr); |
| 665 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, |
| 666 | webrtc_data_channel_->state(), kDefaultTimeout); |
| 667 | EXPECT_FALSE(webrtc_data_channel_->error().ok()); |
| 668 | EXPECT_EQ(webrtc::RTCErrorType::OPERATION_ERROR_WITH_DATA, |
| 669 | webrtc_data_channel_->error().type()); |
| 670 | EXPECT_EQ(webrtc::RTCErrorDetailType::SCTP_FAILURE, |
| 671 | webrtc_data_channel_->error().error_detail()); |
| 672 | EXPECT_EQ( |
| 673 | static_cast<uint16_t>(cricket::SctpErrorCauseCode::kProtocolViolation), |
| 674 | webrtc_data_channel_->error().sctp_cause_code()); |
| 675 | } |
| 676 | |
Mirko Bonadei | 6a489f2 | 2019-04-09 13:11:12 | [diff] [blame] | 677 | class SctpSidAllocatorTest : public ::testing::Test { |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 678 | protected: |
| 679 | SctpSidAllocator allocator_; |
| 680 | }; |
| 681 | |
| 682 | // Verifies that an even SCTP id is allocated for SSL_CLIENT and an odd id for |
| 683 | // SSL_SERVER. |
| 684 | TEST_F(SctpSidAllocatorTest, SctpIdAllocationBasedOnRole) { |
| 685 | int id; |
| 686 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &id)); |
| 687 | EXPECT_EQ(1, id); |
| 688 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &id)); |
| 689 | EXPECT_EQ(0, id); |
| 690 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &id)); |
| 691 | EXPECT_EQ(3, id); |
| 692 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &id)); |
| 693 | EXPECT_EQ(2, id); |
| 694 | } |
| 695 | |
| 696 | // Verifies that SCTP ids of existing DataChannels are not reused. |
| 697 | TEST_F(SctpSidAllocatorTest, SctpIdAllocationNoReuse) { |
| 698 | int old_id = 1; |
| 699 | EXPECT_TRUE(allocator_.ReserveSid(old_id)); |
| 700 | |
| 701 | int new_id; |
| 702 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &new_id)); |
| 703 | EXPECT_NE(old_id, new_id); |
| 704 | |
| 705 | old_id = 0; |
| 706 | EXPECT_TRUE(allocator_.ReserveSid(old_id)); |
| 707 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &new_id)); |
| 708 | EXPECT_NE(old_id, new_id); |
| 709 | } |
| 710 | |
| 711 | // Verifies that SCTP ids of removed DataChannels can be reused. |
| 712 | TEST_F(SctpSidAllocatorTest, SctpIdReusedForRemovedDataChannel) { |
| 713 | int odd_id = 1; |
| 714 | int even_id = 0; |
| 715 | EXPECT_TRUE(allocator_.ReserveSid(odd_id)); |
| 716 | EXPECT_TRUE(allocator_.ReserveSid(even_id)); |
| 717 | |
| 718 | int allocated_id = -1; |
| 719 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id)); |
| 720 | EXPECT_EQ(odd_id + 2, allocated_id); |
| 721 | |
| 722 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id)); |
| 723 | EXPECT_EQ(even_id + 2, allocated_id); |
| 724 | |
| 725 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id)); |
| 726 | EXPECT_EQ(odd_id + 4, allocated_id); |
| 727 | |
| 728 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id)); |
| 729 | EXPECT_EQ(even_id + 4, allocated_id); |
| 730 | |
| 731 | allocator_.ReleaseSid(odd_id); |
| 732 | allocator_.ReleaseSid(even_id); |
| 733 | |
| 734 | // Verifies that removed ids are reused. |
| 735 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id)); |
| 736 | EXPECT_EQ(odd_id, allocated_id); |
| 737 | |
| 738 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id)); |
| 739 | EXPECT_EQ(even_id, allocated_id); |
| 740 | |
| 741 | // Verifies that used higher ids are not reused. |
| 742 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id)); |
| 743 | EXPECT_EQ(odd_id + 6, allocated_id); |
| 744 | |
| 745 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id)); |
| 746 | EXPECT_EQ(even_id + 6, allocated_id); |
| 747 | } |