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