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