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