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