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