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