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() |
| 38 | : messages_received_(0), on_state_change_count_(0) {} |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 39 | |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 40 | void OnStateChange() { |
| 41 | ++on_state_change_count_; |
| 42 | } |
| 43 | |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 44 | void OnMessage(const webrtc::DataBuffer& buffer) { |
| 45 | ++messages_received_; |
| 46 | } |
| 47 | |
| 48 | size_t messages_received() const { |
| 49 | return messages_received_; |
| 50 | } |
| 51 | |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 52 | void ResetOnStateChangeCount() { |
| 53 | on_state_change_count_ = 0; |
| 54 | } |
| 55 | |
| 56 | size_t on_state_change_count() const { |
| 57 | return on_state_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_; |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 65 | class SctpDataChannelTest : public testing::Test { |
| 66 | protected: |
| 67 | SctpDataChannelTest() |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 68 | : webrtc_data_channel_( |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 69 | DataChannel::Create( |
| 70 | &provider_, cricket::DCT_SCTP, "test", init_)) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 71 | } |
| 72 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 73 | void SetChannelReady() { |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 74 | provider_.set_transport_available(true); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 75 | webrtc_data_channel_->OnTransportChannelCreated(); |
| 76 | if (webrtc_data_channel_->id() < 0) { |
| 77 | webrtc_data_channel_->SetSctpSid(0); |
| 78 | } |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 79 | provider_.set_ready_to_send(true); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 80 | } |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 81 | |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 82 | void AddObserver() { |
| 83 | observer_.reset(new FakeDataChannelObserver()); |
| 84 | webrtc_data_channel_->RegisterObserver(observer_.get()); |
| 85 | } |
| 86 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 87 | webrtc::InternalDataChannelInit init_; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 88 | FakeDataChannelProvider provider_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 89 | rtc::scoped_ptr<FakeDataChannelObserver> observer_; |
| 90 | rtc::scoped_refptr<DataChannel> webrtc_data_channel_; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 93 | // Verifies that the data channel is connected to the transport after creation. |
| 94 | TEST_F(SctpDataChannelTest, ConnectedToTransportOnCreated) { |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 95 | provider_.set_transport_available(true); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 96 | rtc::scoped_refptr<DataChannel> dc = DataChannel::Create( |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 97 | &provider_, cricket::DCT_SCTP, "test1", init_); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 98 | |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 99 | EXPECT_TRUE(provider_.IsConnected(dc.get())); |
| 100 | // The sid is not set yet, so it should not have added the streams. |
| 101 | EXPECT_FALSE(provider_.IsSendStreamAdded(dc->id())); |
| 102 | EXPECT_FALSE(provider_.IsRecvStreamAdded(dc->id())); |
| 103 | |
| 104 | dc->SetSctpSid(0); |
| 105 | EXPECT_TRUE(provider_.IsSendStreamAdded(dc->id())); |
| 106 | EXPECT_TRUE(provider_.IsRecvStreamAdded(dc->id())); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // Verifies that the data channel is connected to the transport if the transport |
| 110 | // is not available initially and becomes available later. |
| 111 | TEST_F(SctpDataChannelTest, ConnectedAfterTransportBecomesAvailable) { |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 112 | EXPECT_FALSE(provider_.IsConnected(webrtc_data_channel_.get())); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 113 | |
| 114 | provider_.set_transport_available(true); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 115 | webrtc_data_channel_->OnTransportChannelCreated(); |
| 116 | EXPECT_TRUE(provider_.IsConnected(webrtc_data_channel_.get())); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 117 | } |
| 118 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 119 | // Tests the state of the data channel. |
| 120 | TEST_F(SctpDataChannelTest, StateTransition) { |
| 121 | EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, |
| 122 | webrtc_data_channel_->state()); |
| 123 | SetChannelReady(); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 124 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 125 | EXPECT_EQ(webrtc::DataChannelInterface::kOpen, webrtc_data_channel_->state()); |
| 126 | webrtc_data_channel_->Close(); |
| 127 | EXPECT_EQ(webrtc::DataChannelInterface::kClosed, |
| 128 | webrtc_data_channel_->state()); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 129 | // Verifies that it's disconnected from the transport. |
| 130 | EXPECT_FALSE(provider_.IsConnected(webrtc_data_channel_.get())); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 131 | } |
| 132 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 133 | // Tests that DataChannel::buffered_amount() is correct after the channel is |
| 134 | // blocked. |
| 135 | TEST_F(SctpDataChannelTest, BufferedAmountWhenBlocked) { |
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()); |
| 141 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 142 | provider_.set_send_blocked(true); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 143 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 144 | const int number_of_packets = 3; |
| 145 | for (int i = 0; i < number_of_packets; ++i) { |
| 146 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 147 | } |
| 148 | EXPECT_EQ(buffer.data.length() * number_of_packets, |
| 149 | webrtc_data_channel_->buffered_amount()); |
| 150 | } |
| 151 | |
| 152 | // Tests that the queued data are sent when the channel transitions from blocked |
| 153 | // to unblocked. |
| 154 | TEST_F(SctpDataChannelTest, QueuedDataSentWhenUnblocked) { |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 155 | SetChannelReady(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 156 | webrtc::DataBuffer buffer("abcd"); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 157 | provider_.set_send_blocked(true); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 158 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 159 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 160 | provider_.set_send_blocked(false); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 161 | SetChannelReady(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 162 | EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); |
| 163 | } |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 164 | |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame^] | 165 | // Tests that no crash when the channel is blocked right away while trying to |
| 166 | // send queued data. |
| 167 | TEST_F(SctpDataChannelTest, BlockedWhenSendQueuedDataNoCrash) { |
| 168 | SetChannelReady(); |
| 169 | webrtc::DataBuffer buffer("abcd"); |
| 170 | provider_.set_send_blocked(true); |
| 171 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 172 | |
| 173 | // Set channel ready while it is still blocked. |
| 174 | SetChannelReady(); |
| 175 | EXPECT_EQ(buffer.size(), webrtc_data_channel_->buffered_amount()); |
| 176 | |
| 177 | // Unblock the channel to send queued data again, there should be no crash. |
| 178 | provider_.set_send_blocked(false); |
| 179 | SetChannelReady(); |
| 180 | EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); |
| 181 | } |
| 182 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 183 | // 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] | 184 | TEST_F(SctpDataChannelTest, OpenMessageSent) { |
| 185 | // Initially the id is unassigned. |
| 186 | EXPECT_EQ(-1, webrtc_data_channel_->id()); |
| 187 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 188 | SetChannelReady(); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 189 | EXPECT_GE(webrtc_data_channel_->id(), 0); |
| 190 | EXPECT_EQ(cricket::DMT_CONTROL, provider_.last_send_data_params().type); |
| 191 | EXPECT_EQ(provider_.last_send_data_params().ssrc, |
| 192 | static_cast<uint32>(webrtc_data_channel_->id())); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 193 | } |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 194 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 195 | TEST_F(SctpDataChannelTest, QueuedOpenMessageSent) { |
| 196 | provider_.set_send_blocked(true); |
| 197 | SetChannelReady(); |
| 198 | provider_.set_send_blocked(false); |
| 199 | |
| 200 | EXPECT_EQ(cricket::DMT_CONTROL, provider_.last_send_data_params().type); |
| 201 | EXPECT_EQ(provider_.last_send_data_params().ssrc, |
| 202 | static_cast<uint32>(webrtc_data_channel_->id())); |
| 203 | } |
| 204 | |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 205 | // Tests that the DataChannel created after transport gets ready can enter OPEN |
| 206 | // state. |
| 207 | TEST_F(SctpDataChannelTest, LateCreatedChannelTransitionToOpen) { |
| 208 | SetChannelReady(); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 209 | webrtc::InternalDataChannelInit init; |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 210 | init.id = 1; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 211 | rtc::scoped_refptr<DataChannel> dc = DataChannel::Create( |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 212 | &provider_, cricket::DCT_SCTP, "test1", init); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 213 | EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, dc->state()); |
| 214 | EXPECT_TRUE_WAIT(webrtc::DataChannelInterface::kOpen == dc->state(), |
| 215 | 1000); |
| 216 | } |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 217 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 218 | // Tests that an unordered DataChannel sends data as ordered until the OPEN_ACK |
| 219 | // message is received. |
| 220 | TEST_F(SctpDataChannelTest, SendUnorderedAfterReceivesOpenAck) { |
| 221 | SetChannelReady(); |
| 222 | webrtc::InternalDataChannelInit init; |
| 223 | init.id = 1; |
| 224 | init.ordered = false; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 225 | rtc::scoped_refptr<DataChannel> dc = DataChannel::Create( |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 226 | &provider_, cricket::DCT_SCTP, "test1", init); |
| 227 | |
| 228 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); |
| 229 | |
| 230 | // Sends a message and verifies it's ordered. |
| 231 | webrtc::DataBuffer buffer("some data"); |
| 232 | ASSERT_TRUE(dc->Send(buffer)); |
| 233 | EXPECT_TRUE(provider_.last_send_data_params().ordered); |
| 234 | |
| 235 | // Emulates receiving an OPEN_ACK message. |
| 236 | cricket::ReceiveDataParams params; |
| 237 | params.ssrc = init.id; |
| 238 | params.type = cricket::DMT_CONTROL; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 239 | rtc::Buffer payload; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 240 | webrtc::WriteDataChannelOpenAckMessage(&payload); |
| 241 | dc->OnDataReceived(NULL, params, payload); |
| 242 | |
| 243 | // Sends another message and verifies it's unordered. |
| 244 | ASSERT_TRUE(dc->Send(buffer)); |
| 245 | EXPECT_FALSE(provider_.last_send_data_params().ordered); |
| 246 | } |
| 247 | |
| 248 | // Tests that an unordered DataChannel sends unordered data after any DATA |
| 249 | // message is received. |
| 250 | TEST_F(SctpDataChannelTest, SendUnorderedAfterReceiveData) { |
| 251 | SetChannelReady(); |
| 252 | webrtc::InternalDataChannelInit init; |
| 253 | init.id = 1; |
| 254 | init.ordered = false; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 255 | rtc::scoped_refptr<DataChannel> dc = DataChannel::Create( |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 256 | &provider_, cricket::DCT_SCTP, "test1", init); |
| 257 | |
| 258 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); |
| 259 | |
| 260 | // Emulates receiving a DATA message. |
| 261 | cricket::ReceiveDataParams params; |
| 262 | params.ssrc = init.id; |
| 263 | params.type = cricket::DMT_TEXT; |
| 264 | webrtc::DataBuffer buffer("data"); |
| 265 | dc->OnDataReceived(NULL, params, buffer.data); |
| 266 | |
| 267 | // Sends a message and verifies it's unordered. |
| 268 | ASSERT_TRUE(dc->Send(buffer)); |
| 269 | EXPECT_FALSE(provider_.last_send_data_params().ordered); |
| 270 | } |
| 271 | |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 272 | // Tests that messages are sent with the right ssrc. |
| 273 | TEST_F(SctpDataChannelTest, SendDataSsrc) { |
| 274 | webrtc_data_channel_->SetSctpSid(1); |
| 275 | SetChannelReady(); |
| 276 | webrtc::DataBuffer buffer("data"); |
| 277 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 278 | EXPECT_EQ(1U, provider_.last_send_data_params().ssrc); |
| 279 | } |
| 280 | |
| 281 | // Tests that the incoming messages with wrong ssrcs are rejected. |
| 282 | TEST_F(SctpDataChannelTest, ReceiveDataWithInvalidSsrc) { |
| 283 | webrtc_data_channel_->SetSctpSid(1); |
| 284 | SetChannelReady(); |
| 285 | |
| 286 | AddObserver(); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 287 | |
| 288 | cricket::ReceiveDataParams params; |
| 289 | params.ssrc = 0; |
| 290 | webrtc::DataBuffer buffer("abcd"); |
| 291 | webrtc_data_channel_->OnDataReceived(NULL, params, buffer.data); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 292 | |
| 293 | EXPECT_EQ(0U, observer_->messages_received()); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | // Tests that the incoming messages with right ssrcs are acceted. |
| 297 | TEST_F(SctpDataChannelTest, ReceiveDataWithValidSsrc) { |
| 298 | webrtc_data_channel_->SetSctpSid(1); |
| 299 | SetChannelReady(); |
| 300 | |
| 301 | AddObserver(); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 302 | |
| 303 | cricket::ReceiveDataParams params; |
| 304 | params.ssrc = 1; |
| 305 | webrtc::DataBuffer buffer("abcd"); |
| 306 | |
| 307 | webrtc_data_channel_->OnDataReceived(NULL, params, buffer.data); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 308 | EXPECT_EQ(1U, observer_->messages_received()); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 309 | } |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 310 | |
| 311 | // Tests that no CONTROL message is sent if the datachannel is negotiated and |
| 312 | // not created from an OPEN message. |
| 313 | TEST_F(SctpDataChannelTest, NoMsgSentIfNegotiatedAndNotFromOpenMsg) { |
| 314 | webrtc::InternalDataChannelInit config; |
| 315 | config.id = 1; |
| 316 | config.negotiated = true; |
| 317 | config.open_handshake_role = webrtc::InternalDataChannelInit::kNone; |
| 318 | |
| 319 | SetChannelReady(); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 320 | rtc::scoped_refptr<DataChannel> dc = DataChannel::Create( |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 321 | &provider_, cricket::DCT_SCTP, "test1", config); |
| 322 | |
| 323 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); |
| 324 | EXPECT_EQ(0U, provider_.last_send_data_params().ssrc); |
| 325 | } |
| 326 | |
| 327 | // Tests that OPEN_ACK message is sent if the datachannel is created from an |
| 328 | // OPEN message. |
| 329 | TEST_F(SctpDataChannelTest, OpenAckSentIfCreatedFromOpenMessage) { |
| 330 | webrtc::InternalDataChannelInit config; |
| 331 | config.id = 1; |
| 332 | config.negotiated = true; |
| 333 | config.open_handshake_role = webrtc::InternalDataChannelInit::kAcker; |
| 334 | |
| 335 | SetChannelReady(); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 336 | rtc::scoped_refptr<DataChannel> dc = DataChannel::Create( |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 337 | &provider_, cricket::DCT_SCTP, "test1", config); |
| 338 | |
| 339 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); |
| 340 | |
| 341 | EXPECT_EQ(static_cast<unsigned int>(config.id), |
| 342 | provider_.last_send_data_params().ssrc); |
| 343 | EXPECT_EQ(cricket::DMT_CONTROL, provider_.last_send_data_params().type); |
| 344 | } |
| 345 | |
| 346 | // Tests the OPEN_ACK role assigned by InternalDataChannelInit. |
| 347 | TEST_F(SctpDataChannelTest, OpenAckRoleInitialization) { |
| 348 | webrtc::InternalDataChannelInit init; |
| 349 | EXPECT_EQ(webrtc::InternalDataChannelInit::kOpener, init.open_handshake_role); |
| 350 | EXPECT_FALSE(init.negotiated); |
| 351 | |
| 352 | webrtc::DataChannelInit base; |
| 353 | base.negotiated = true; |
| 354 | webrtc::InternalDataChannelInit init2(base); |
| 355 | EXPECT_EQ(webrtc::InternalDataChannelInit::kNone, init2.open_handshake_role); |
| 356 | } |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 357 | |
| 358 | // Tests that the DataChannel is closed if the sending buffer is full. |
| 359 | TEST_F(SctpDataChannelTest, ClosedWhenSendBufferFull) { |
| 360 | SetChannelReady(); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 361 | |
| 362 | const size_t buffer_size = 1024; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 363 | rtc::Buffer buffer; |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 364 | buffer.SetLength(buffer_size); |
| 365 | memset(buffer.data(), 0, buffer_size); |
| 366 | |
| 367 | webrtc::DataBuffer packet(buffer, true); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 368 | provider_.set_send_blocked(true); |
| 369 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 370 | for (size_t i = 0; i < 16 * 1024 + 1; ++i) { |
| 371 | EXPECT_TRUE(webrtc_data_channel_->Send(packet)); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | EXPECT_EQ(webrtc::DataChannelInterface::kClosed, |
| 375 | webrtc_data_channel_->state()); |
| 376 | } |
| 377 | |
| 378 | // Tests that the DataChannel is closed on transport errors. |
| 379 | TEST_F(SctpDataChannelTest, ClosedOnTransportError) { |
| 380 | SetChannelReady(); |
| 381 | webrtc::DataBuffer buffer("abcd"); |
| 382 | provider_.set_transport_error(); |
| 383 | |
| 384 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 385 | |
| 386 | EXPECT_EQ(webrtc::DataChannelInterface::kClosed, |
| 387 | webrtc_data_channel_->state()); |
| 388 | } |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 389 | |
| 390 | // Tests that a already closed DataChannel does not fire onStateChange again. |
| 391 | TEST_F(SctpDataChannelTest, ClosedDataChannelDoesNotFireOnStateChange) { |
| 392 | AddObserver(); |
| 393 | webrtc_data_channel_->Close(); |
| 394 | // OnStateChange called for kClosing and kClosed. |
| 395 | EXPECT_EQ(2U, observer_->on_state_change_count()); |
| 396 | |
| 397 | observer_->ResetOnStateChangeCount(); |
| 398 | webrtc_data_channel_->RemotePeerRequestClose(); |
| 399 | EXPECT_EQ(0U, observer_->on_state_change_count()); |
| 400 | } |
| 401 | |
| 402 | // Tests that RemotePeerRequestClose closes the local DataChannel. |
| 403 | TEST_F(SctpDataChannelTest, RemotePeerRequestClose) { |
| 404 | AddObserver(); |
| 405 | webrtc_data_channel_->RemotePeerRequestClose(); |
| 406 | |
| 407 | // OnStateChange called for kClosing and kClosed. |
| 408 | EXPECT_EQ(2U, observer_->on_state_change_count()); |
| 409 | EXPECT_EQ(webrtc::DataChannelInterface::kClosed, |
| 410 | webrtc_data_channel_->state()); |
| 411 | } |
| 412 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 413 | // Tests that the DataChannel is closed if the received buffer is full. |
| 414 | TEST_F(SctpDataChannelTest, ClosedWhenReceivedBufferFull) { |
| 415 | SetChannelReady(); |
| 416 | const size_t buffer_size = 1024; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 417 | rtc::Buffer buffer; |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 418 | buffer.SetLength(buffer_size); |
| 419 | memset(buffer.data(), 0, buffer_size); |
| 420 | |
| 421 | cricket::ReceiveDataParams params; |
| 422 | params.ssrc = 0; |
| 423 | |
| 424 | // Receiving data without having an observer will overflow the buffer. |
| 425 | for (size_t i = 0; i < 16 * 1024 + 1; ++i) { |
| 426 | webrtc_data_channel_->OnDataReceived(NULL, params, buffer); |
| 427 | } |
| 428 | EXPECT_EQ(webrtc::DataChannelInterface::kClosed, |
| 429 | webrtc_data_channel_->state()); |
| 430 | } |
jiayl@webrtc.org | 3edbaaf | 2014-07-18 23:57:50 +0000 | [diff] [blame] | 431 | |
| 432 | // Tests that sending empty data returns no error and keeps the channel open. |
| 433 | TEST_F(SctpDataChannelTest, SendEmptyData) { |
| 434 | webrtc_data_channel_->SetSctpSid(1); |
| 435 | SetChannelReady(); |
| 436 | EXPECT_EQ(webrtc::DataChannelInterface::kOpen, |
| 437 | webrtc_data_channel_->state()); |
| 438 | |
| 439 | webrtc::DataBuffer buffer(""); |
| 440 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 441 | EXPECT_EQ(webrtc::DataChannelInterface::kOpen, |
| 442 | webrtc_data_channel_->state()); |
| 443 | } |
bemasc@webrtc.org | 9b5467e | 2014-12-04 23:16:52 +0000 | [diff] [blame] | 444 | |
| 445 | // Tests that a channel can be closed without being opened or assigned an sid. |
| 446 | TEST_F(SctpDataChannelTest, NeverOpened) { |
| 447 | provider_.set_transport_available(true); |
| 448 | webrtc_data_channel_->OnTransportChannelCreated(); |
| 449 | webrtc_data_channel_->Close(); |
| 450 | } |