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