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