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