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