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