wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * Copyright 2013 Google Inc. |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "talk/app/webrtc/datachannel.h" |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 29 | #include "talk/app/webrtc/sctputils.h" |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 30 | #include "talk/app/webrtc/test/fakedatachannelprovider.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 31 | #include "webrtc/base/gunit.h" |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 32 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 33 | using webrtc::DataChannel; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 34 | |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 35 | class FakeDataChannelObserver : public webrtc::DataChannelObserver { |
| 36 | public: |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 37 | FakeDataChannelObserver() |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 38 | : messages_received_(0), |
| 39 | on_state_change_count_(0), |
| 40 | on_buffered_amount_change_count_(0) {} |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 41 | |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 42 | void OnStateChange() { |
| 43 | ++on_state_change_count_; |
| 44 | } |
| 45 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame^] | 46 | void OnBufferedAmountChange(uint64_t previous_amount) { |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 47 | ++on_buffered_amount_change_count_; |
| 48 | } |
| 49 | |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 50 | void OnMessage(const webrtc::DataBuffer& buffer) { |
| 51 | ++messages_received_; |
| 52 | } |
| 53 | |
| 54 | size_t messages_received() const { |
| 55 | return messages_received_; |
| 56 | } |
| 57 | |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 58 | void ResetOnStateChangeCount() { |
| 59 | on_state_change_count_ = 0; |
| 60 | } |
| 61 | |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 62 | void ResetOnBufferedAmountChangeCount() { |
| 63 | on_buffered_amount_change_count_ = 0; |
| 64 | } |
| 65 | |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 66 | size_t on_state_change_count() const { |
| 67 | return on_state_change_count_; |
| 68 | } |
| 69 | |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 70 | size_t on_buffered_amount_change_count() const { |
| 71 | return on_buffered_amount_change_count_; |
| 72 | } |
| 73 | |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 74 | private: |
| 75 | size_t messages_received_; |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 76 | size_t on_state_change_count_; |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 77 | size_t on_buffered_amount_change_count_; |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 80 | class SctpDataChannelTest : public testing::Test { |
| 81 | protected: |
| 82 | SctpDataChannelTest() |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 83 | : webrtc_data_channel_( |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 84 | DataChannel::Create( |
| 85 | &provider_, cricket::DCT_SCTP, "test", init_)) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 86 | } |
| 87 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 88 | void SetChannelReady() { |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 89 | provider_.set_transport_available(true); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 90 | webrtc_data_channel_->OnTransportChannelCreated(); |
| 91 | if (webrtc_data_channel_->id() < 0) { |
| 92 | webrtc_data_channel_->SetSctpSid(0); |
| 93 | } |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 94 | provider_.set_ready_to_send(true); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 95 | } |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 96 | |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 97 | void AddObserver() { |
| 98 | observer_.reset(new FakeDataChannelObserver()); |
| 99 | webrtc_data_channel_->RegisterObserver(observer_.get()); |
| 100 | } |
| 101 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 102 | webrtc::InternalDataChannelInit init_; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 103 | FakeDataChannelProvider provider_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 104 | rtc::scoped_ptr<FakeDataChannelObserver> observer_; |
| 105 | rtc::scoped_refptr<DataChannel> webrtc_data_channel_; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 108 | // Verifies that the data channel is connected to the transport after creation. |
| 109 | TEST_F(SctpDataChannelTest, ConnectedToTransportOnCreated) { |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 110 | provider_.set_transport_available(true); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 111 | rtc::scoped_refptr<DataChannel> dc = DataChannel::Create( |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 112 | &provider_, cricket::DCT_SCTP, "test1", init_); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 113 | |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 114 | EXPECT_TRUE(provider_.IsConnected(dc.get())); |
| 115 | // The sid is not set yet, so it should not have added the streams. |
| 116 | EXPECT_FALSE(provider_.IsSendStreamAdded(dc->id())); |
| 117 | EXPECT_FALSE(provider_.IsRecvStreamAdded(dc->id())); |
| 118 | |
| 119 | dc->SetSctpSid(0); |
| 120 | EXPECT_TRUE(provider_.IsSendStreamAdded(dc->id())); |
| 121 | EXPECT_TRUE(provider_.IsRecvStreamAdded(dc->id())); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | // Verifies that the data channel is connected to the transport if the transport |
| 125 | // is not available initially and becomes available later. |
| 126 | TEST_F(SctpDataChannelTest, ConnectedAfterTransportBecomesAvailable) { |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 127 | EXPECT_FALSE(provider_.IsConnected(webrtc_data_channel_.get())); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 128 | |
| 129 | provider_.set_transport_available(true); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 130 | webrtc_data_channel_->OnTransportChannelCreated(); |
| 131 | EXPECT_TRUE(provider_.IsConnected(webrtc_data_channel_.get())); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 132 | } |
| 133 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 134 | // Tests the state of the data channel. |
| 135 | TEST_F(SctpDataChannelTest, StateTransition) { |
| 136 | EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, |
| 137 | webrtc_data_channel_->state()); |
| 138 | SetChannelReady(); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 139 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 140 | EXPECT_EQ(webrtc::DataChannelInterface::kOpen, webrtc_data_channel_->state()); |
| 141 | webrtc_data_channel_->Close(); |
| 142 | EXPECT_EQ(webrtc::DataChannelInterface::kClosed, |
| 143 | webrtc_data_channel_->state()); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 144 | // Verifies that it's disconnected from the transport. |
| 145 | EXPECT_FALSE(provider_.IsConnected(webrtc_data_channel_.get())); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 146 | } |
| 147 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 148 | // Tests that DataChannel::buffered_amount() is correct after the channel is |
| 149 | // blocked. |
| 150 | TEST_F(SctpDataChannelTest, BufferedAmountWhenBlocked) { |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 151 | AddObserver(); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 152 | SetChannelReady(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 153 | webrtc::DataBuffer buffer("abcd"); |
| 154 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 155 | |
| 156 | EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 157 | EXPECT_EQ(0U, observer_->on_buffered_amount_change_count()); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 158 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 159 | provider_.set_send_blocked(true); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 160 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 161 | const int number_of_packets = 3; |
| 162 | for (int i = 0; i < number_of_packets; ++i) { |
| 163 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 164 | } |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 165 | EXPECT_EQ(buffer.data.size() * number_of_packets, |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 166 | webrtc_data_channel_->buffered_amount()); |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 167 | EXPECT_EQ(number_of_packets, observer_->on_buffered_amount_change_count()); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // Tests that the queued data are sent when the channel transitions from blocked |
| 171 | // to unblocked. |
| 172 | TEST_F(SctpDataChannelTest, QueuedDataSentWhenUnblocked) { |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 173 | AddObserver(); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 174 | SetChannelReady(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 175 | webrtc::DataBuffer buffer("abcd"); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 176 | provider_.set_send_blocked(true); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 177 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 178 | |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 179 | EXPECT_EQ(1U, observer_->on_buffered_amount_change_count()); |
| 180 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 181 | provider_.set_send_blocked(false); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 182 | SetChannelReady(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 183 | EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 184 | EXPECT_EQ(2U, observer_->on_buffered_amount_change_count()); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 185 | } |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 186 | |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 187 | // Tests that no crash when the channel is blocked right away while trying to |
| 188 | // send queued data. |
| 189 | TEST_F(SctpDataChannelTest, BlockedWhenSendQueuedDataNoCrash) { |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 190 | AddObserver(); |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 191 | SetChannelReady(); |
| 192 | webrtc::DataBuffer buffer("abcd"); |
| 193 | provider_.set_send_blocked(true); |
| 194 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 195 | EXPECT_EQ(1U, observer_->on_buffered_amount_change_count()); |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 196 | |
| 197 | // Set channel ready while it is still blocked. |
| 198 | SetChannelReady(); |
| 199 | EXPECT_EQ(buffer.size(), webrtc_data_channel_->buffered_amount()); |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 200 | EXPECT_EQ(1U, observer_->on_buffered_amount_change_count()); |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 201 | |
| 202 | // Unblock the channel to send queued data again, there should be no crash. |
| 203 | provider_.set_send_blocked(false); |
| 204 | SetChannelReady(); |
| 205 | EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 206 | EXPECT_EQ(2U, observer_->on_buffered_amount_change_count()); |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 207 | } |
| 208 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 209 | // 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] | 210 | TEST_F(SctpDataChannelTest, OpenMessageSent) { |
| 211 | // Initially the id is unassigned. |
| 212 | EXPECT_EQ(-1, webrtc_data_channel_->id()); |
| 213 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 214 | SetChannelReady(); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 215 | EXPECT_GE(webrtc_data_channel_->id(), 0); |
| 216 | EXPECT_EQ(cricket::DMT_CONTROL, provider_.last_send_data_params().type); |
| 217 | EXPECT_EQ(provider_.last_send_data_params().ssrc, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame^] | 218 | static_cast<uint32_t>(webrtc_data_channel_->id())); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 219 | } |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 220 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 221 | TEST_F(SctpDataChannelTest, QueuedOpenMessageSent) { |
| 222 | provider_.set_send_blocked(true); |
| 223 | SetChannelReady(); |
| 224 | provider_.set_send_blocked(false); |
| 225 | |
| 226 | EXPECT_EQ(cricket::DMT_CONTROL, provider_.last_send_data_params().type); |
| 227 | EXPECT_EQ(provider_.last_send_data_params().ssrc, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame^] | 228 | static_cast<uint32_t>(webrtc_data_channel_->id())); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 229 | } |
| 230 | |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 231 | // Tests that the DataChannel created after transport gets ready can enter OPEN |
| 232 | // state. |
| 233 | TEST_F(SctpDataChannelTest, LateCreatedChannelTransitionToOpen) { |
| 234 | SetChannelReady(); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 235 | webrtc::InternalDataChannelInit init; |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 236 | init.id = 1; |
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); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 239 | EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, dc->state()); |
| 240 | EXPECT_TRUE_WAIT(webrtc::DataChannelInterface::kOpen == dc->state(), |
| 241 | 1000); |
| 242 | } |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 243 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 244 | // Tests that an unordered DataChannel sends data as ordered until the OPEN_ACK |
| 245 | // message is received. |
| 246 | TEST_F(SctpDataChannelTest, SendUnorderedAfterReceivesOpenAck) { |
| 247 | SetChannelReady(); |
| 248 | webrtc::InternalDataChannelInit init; |
| 249 | init.id = 1; |
| 250 | init.ordered = false; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 251 | rtc::scoped_refptr<DataChannel> dc = DataChannel::Create( |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 252 | &provider_, cricket::DCT_SCTP, "test1", init); |
| 253 | |
| 254 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); |
| 255 | |
| 256 | // Sends a message and verifies it's ordered. |
| 257 | webrtc::DataBuffer buffer("some data"); |
| 258 | ASSERT_TRUE(dc->Send(buffer)); |
| 259 | EXPECT_TRUE(provider_.last_send_data_params().ordered); |
| 260 | |
| 261 | // Emulates receiving an OPEN_ACK message. |
| 262 | cricket::ReceiveDataParams params; |
| 263 | params.ssrc = init.id; |
| 264 | params.type = cricket::DMT_CONTROL; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 265 | rtc::Buffer payload; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 266 | webrtc::WriteDataChannelOpenAckMessage(&payload); |
| 267 | dc->OnDataReceived(NULL, params, payload); |
| 268 | |
| 269 | // Sends another message and verifies it's unordered. |
| 270 | ASSERT_TRUE(dc->Send(buffer)); |
| 271 | EXPECT_FALSE(provider_.last_send_data_params().ordered); |
| 272 | } |
| 273 | |
| 274 | // Tests that an unordered DataChannel sends unordered data after any DATA |
| 275 | // message is received. |
| 276 | TEST_F(SctpDataChannelTest, SendUnorderedAfterReceiveData) { |
| 277 | SetChannelReady(); |
| 278 | webrtc::InternalDataChannelInit init; |
| 279 | init.id = 1; |
| 280 | init.ordered = false; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 281 | rtc::scoped_refptr<DataChannel> dc = DataChannel::Create( |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 282 | &provider_, cricket::DCT_SCTP, "test1", init); |
| 283 | |
| 284 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); |
| 285 | |
| 286 | // Emulates receiving a DATA message. |
| 287 | cricket::ReceiveDataParams params; |
| 288 | params.ssrc = init.id; |
| 289 | params.type = cricket::DMT_TEXT; |
| 290 | webrtc::DataBuffer buffer("data"); |
| 291 | dc->OnDataReceived(NULL, params, buffer.data); |
| 292 | |
| 293 | // Sends a message and verifies it's unordered. |
| 294 | ASSERT_TRUE(dc->Send(buffer)); |
| 295 | EXPECT_FALSE(provider_.last_send_data_params().ordered); |
| 296 | } |
| 297 | |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 298 | // Tests that the channel can't open until it's successfully sent the OPEN |
| 299 | // message. |
| 300 | TEST_F(SctpDataChannelTest, OpenWaitsForOpenMesssage) { |
| 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 | EXPECT_EQ(cricket::DMT_CONTROL, provider_.last_send_data_params().type); |
| 311 | } |
| 312 | |
| 313 | // Tests that close first makes sure all queued data gets sent. |
| 314 | TEST_F(SctpDataChannelTest, QueuedCloseFlushes) { |
| 315 | webrtc::DataBuffer buffer("foo"); |
| 316 | |
| 317 | provider_.set_send_blocked(true); |
| 318 | SetChannelReady(); |
| 319 | EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, |
| 320 | webrtc_data_channel_->state()); |
| 321 | provider_.set_send_blocked(false); |
| 322 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, |
| 323 | webrtc_data_channel_->state(), 1000); |
| 324 | provider_.set_send_blocked(true); |
| 325 | webrtc_data_channel_->Send(buffer); |
| 326 | webrtc_data_channel_->Close(); |
| 327 | provider_.set_send_blocked(false); |
| 328 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, |
| 329 | webrtc_data_channel_->state(), 1000); |
| 330 | EXPECT_EQ(cricket::DMT_TEXT, provider_.last_send_data_params().type); |
| 331 | } |
| 332 | |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 333 | // Tests that messages are sent with the right ssrc. |
| 334 | TEST_F(SctpDataChannelTest, SendDataSsrc) { |
| 335 | webrtc_data_channel_->SetSctpSid(1); |
| 336 | SetChannelReady(); |
| 337 | webrtc::DataBuffer buffer("data"); |
| 338 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 339 | EXPECT_EQ(1U, provider_.last_send_data_params().ssrc); |
| 340 | } |
| 341 | |
| 342 | // Tests that the incoming messages with wrong ssrcs are rejected. |
| 343 | TEST_F(SctpDataChannelTest, ReceiveDataWithInvalidSsrc) { |
| 344 | webrtc_data_channel_->SetSctpSid(1); |
| 345 | SetChannelReady(); |
| 346 | |
| 347 | AddObserver(); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 348 | |
| 349 | cricket::ReceiveDataParams params; |
| 350 | params.ssrc = 0; |
| 351 | webrtc::DataBuffer buffer("abcd"); |
| 352 | webrtc_data_channel_->OnDataReceived(NULL, params, buffer.data); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 353 | |
| 354 | EXPECT_EQ(0U, observer_->messages_received()); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | // Tests that the incoming messages with right ssrcs are acceted. |
| 358 | TEST_F(SctpDataChannelTest, ReceiveDataWithValidSsrc) { |
| 359 | webrtc_data_channel_->SetSctpSid(1); |
| 360 | SetChannelReady(); |
| 361 | |
| 362 | AddObserver(); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 363 | |
| 364 | cricket::ReceiveDataParams params; |
| 365 | params.ssrc = 1; |
| 366 | webrtc::DataBuffer buffer("abcd"); |
| 367 | |
| 368 | webrtc_data_channel_->OnDataReceived(NULL, params, buffer.data); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 369 | EXPECT_EQ(1U, observer_->messages_received()); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 370 | } |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 371 | |
| 372 | // Tests that no CONTROL message is sent if the datachannel is negotiated and |
| 373 | // not created from an OPEN message. |
| 374 | TEST_F(SctpDataChannelTest, NoMsgSentIfNegotiatedAndNotFromOpenMsg) { |
| 375 | webrtc::InternalDataChannelInit config; |
| 376 | config.id = 1; |
| 377 | config.negotiated = true; |
| 378 | config.open_handshake_role = webrtc::InternalDataChannelInit::kNone; |
| 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 | EXPECT_EQ(0U, provider_.last_send_data_params().ssrc); |
| 386 | } |
| 387 | |
| 388 | // Tests that OPEN_ACK message is sent if the datachannel is created from an |
| 389 | // OPEN message. |
| 390 | TEST_F(SctpDataChannelTest, OpenAckSentIfCreatedFromOpenMessage) { |
| 391 | webrtc::InternalDataChannelInit config; |
| 392 | config.id = 1; |
| 393 | config.negotiated = true; |
| 394 | config.open_handshake_role = webrtc::InternalDataChannelInit::kAcker; |
| 395 | |
| 396 | SetChannelReady(); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 397 | rtc::scoped_refptr<DataChannel> dc = DataChannel::Create( |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 398 | &provider_, cricket::DCT_SCTP, "test1", config); |
| 399 | |
| 400 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); |
| 401 | |
| 402 | EXPECT_EQ(static_cast<unsigned int>(config.id), |
| 403 | provider_.last_send_data_params().ssrc); |
| 404 | EXPECT_EQ(cricket::DMT_CONTROL, provider_.last_send_data_params().type); |
| 405 | } |
| 406 | |
| 407 | // Tests the OPEN_ACK role assigned by InternalDataChannelInit. |
| 408 | TEST_F(SctpDataChannelTest, OpenAckRoleInitialization) { |
| 409 | webrtc::InternalDataChannelInit init; |
| 410 | EXPECT_EQ(webrtc::InternalDataChannelInit::kOpener, init.open_handshake_role); |
| 411 | EXPECT_FALSE(init.negotiated); |
| 412 | |
| 413 | webrtc::DataChannelInit base; |
| 414 | base.negotiated = true; |
| 415 | webrtc::InternalDataChannelInit init2(base); |
| 416 | EXPECT_EQ(webrtc::InternalDataChannelInit::kNone, init2.open_handshake_role); |
| 417 | } |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 418 | |
| 419 | // Tests that the DataChannel is closed if the sending buffer is full. |
| 420 | TEST_F(SctpDataChannelTest, ClosedWhenSendBufferFull) { |
| 421 | SetChannelReady(); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 422 | |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 423 | rtc::Buffer buffer(1024); |
| 424 | memset(buffer.data(), 0, buffer.size()); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 425 | |
| 426 | webrtc::DataBuffer packet(buffer, true); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 427 | provider_.set_send_blocked(true); |
| 428 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 429 | for (size_t i = 0; i < 16 * 1024 + 1; ++i) { |
| 430 | EXPECT_TRUE(webrtc_data_channel_->Send(packet)); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 433 | EXPECT_TRUE( |
| 434 | webrtc::DataChannelInterface::kClosed == webrtc_data_channel_->state() || |
| 435 | webrtc::DataChannelInterface::kClosing == webrtc_data_channel_->state()); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | // Tests that the DataChannel is closed on transport errors. |
| 439 | TEST_F(SctpDataChannelTest, ClosedOnTransportError) { |
| 440 | SetChannelReady(); |
| 441 | webrtc::DataBuffer buffer("abcd"); |
| 442 | provider_.set_transport_error(); |
| 443 | |
| 444 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 445 | |
| 446 | EXPECT_EQ(webrtc::DataChannelInterface::kClosed, |
| 447 | webrtc_data_channel_->state()); |
| 448 | } |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 449 | |
| 450 | // Tests that a already closed DataChannel does not fire onStateChange again. |
| 451 | TEST_F(SctpDataChannelTest, ClosedDataChannelDoesNotFireOnStateChange) { |
| 452 | AddObserver(); |
| 453 | webrtc_data_channel_->Close(); |
| 454 | // OnStateChange called for kClosing and kClosed. |
| 455 | EXPECT_EQ(2U, observer_->on_state_change_count()); |
| 456 | |
| 457 | observer_->ResetOnStateChangeCount(); |
| 458 | webrtc_data_channel_->RemotePeerRequestClose(); |
| 459 | EXPECT_EQ(0U, observer_->on_state_change_count()); |
| 460 | } |
| 461 | |
| 462 | // Tests that RemotePeerRequestClose closes the local DataChannel. |
| 463 | TEST_F(SctpDataChannelTest, RemotePeerRequestClose) { |
| 464 | AddObserver(); |
| 465 | webrtc_data_channel_->RemotePeerRequestClose(); |
| 466 | |
| 467 | // OnStateChange called for kClosing and kClosed. |
| 468 | EXPECT_EQ(2U, observer_->on_state_change_count()); |
| 469 | EXPECT_EQ(webrtc::DataChannelInterface::kClosed, |
| 470 | webrtc_data_channel_->state()); |
| 471 | } |
| 472 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 473 | // Tests that the DataChannel is closed if the received buffer is full. |
| 474 | TEST_F(SctpDataChannelTest, ClosedWhenReceivedBufferFull) { |
| 475 | SetChannelReady(); |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 476 | rtc::Buffer buffer(1024); |
| 477 | memset(buffer.data(), 0, buffer.size()); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 478 | |
| 479 | cricket::ReceiveDataParams params; |
| 480 | params.ssrc = 0; |
| 481 | |
| 482 | // Receiving data without having an observer will overflow the buffer. |
| 483 | for (size_t i = 0; i < 16 * 1024 + 1; ++i) { |
| 484 | webrtc_data_channel_->OnDataReceived(NULL, params, buffer); |
| 485 | } |
| 486 | EXPECT_EQ(webrtc::DataChannelInterface::kClosed, |
| 487 | webrtc_data_channel_->state()); |
| 488 | } |
jiayl@webrtc.org | 3edbaaf | 2014-07-18 23:57:50 +0000 | [diff] [blame] | 489 | |
| 490 | // Tests that sending empty data returns no error and keeps the channel open. |
| 491 | TEST_F(SctpDataChannelTest, SendEmptyData) { |
| 492 | webrtc_data_channel_->SetSctpSid(1); |
| 493 | SetChannelReady(); |
| 494 | EXPECT_EQ(webrtc::DataChannelInterface::kOpen, |
| 495 | webrtc_data_channel_->state()); |
| 496 | |
| 497 | webrtc::DataBuffer buffer(""); |
| 498 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 499 | EXPECT_EQ(webrtc::DataChannelInterface::kOpen, |
| 500 | webrtc_data_channel_->state()); |
| 501 | } |
bemasc@webrtc.org | 9b5467e | 2014-12-04 23:16:52 +0000 | [diff] [blame] | 502 | |
| 503 | // Tests that a channel can be closed without being opened or assigned an sid. |
| 504 | TEST_F(SctpDataChannelTest, NeverOpened) { |
| 505 | provider_.set_transport_available(true); |
| 506 | webrtc_data_channel_->OnTransportChannelCreated(); |
| 507 | webrtc_data_channel_->Close(); |
| 508 | } |