blob: d5711e8ecd5103c127c9f4d46074fd8ec87ed710 [file] [log] [blame]
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2013 Google Inc.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00004 *
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
Henrik Kjellander15583c12016-02-10 10:53:12 +010028#include "webrtc/api/datachannel.h"
29#include "webrtc/api/sctputils.h"
30#include "webrtc/api/test/fakedatachannelprovider.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000031#include "webrtc/base/gunit.h"
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000032
wu@webrtc.org78187522013-10-07 23:32:02 +000033using webrtc::DataChannel;
deadbeefab9b2d12015-10-14 11:33:11 -070034using webrtc::SctpSidAllocator;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000035
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +000036class FakeDataChannelObserver : public webrtc::DataChannelObserver {
37 public:
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +000038 FakeDataChannelObserver()
bemasc0edd50c2015-07-01 13:34:33 -070039 : messages_received_(0),
40 on_state_change_count_(0),
41 on_buffered_amount_change_count_(0) {}
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +000042
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +000043 void OnStateChange() {
44 ++on_state_change_count_;
45 }
46
Peter Boström0c4e06b2015-10-07 12:23:21 +020047 void OnBufferedAmountChange(uint64_t previous_amount) {
bemasc0edd50c2015-07-01 13:34:33 -070048 ++on_buffered_amount_change_count_;
49 }
50
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +000051 void OnMessage(const webrtc::DataBuffer& buffer) {
52 ++messages_received_;
53 }
54
55 size_t messages_received() const {
56 return messages_received_;
57 }
58
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +000059 void ResetOnStateChangeCount() {
60 on_state_change_count_ = 0;
61 }
62
bemasc0edd50c2015-07-01 13:34:33 -070063 void ResetOnBufferedAmountChangeCount() {
64 on_buffered_amount_change_count_ = 0;
65 }
66
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +000067 size_t on_state_change_count() const {
68 return on_state_change_count_;
69 }
70
bemasc0edd50c2015-07-01 13:34:33 -070071 size_t on_buffered_amount_change_count() const {
72 return on_buffered_amount_change_count_;
73 }
74
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +000075 private:
76 size_t messages_received_;
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +000077 size_t on_state_change_count_;
bemasc0edd50c2015-07-01 13:34:33 -070078 size_t on_buffered_amount_change_count_;
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +000079};
80
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000081class SctpDataChannelTest : public testing::Test {
82 protected:
83 SctpDataChannelTest()
wu@webrtc.org78187522013-10-07 23:32:02 +000084 : webrtc_data_channel_(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000085 DataChannel::Create(
86 &provider_, cricket::DCT_SCTP, "test", init_)) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000087 }
88
wu@webrtc.org78187522013-10-07 23:32:02 +000089 void SetChannelReady() {
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000090 provider_.set_transport_available(true);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000091 webrtc_data_channel_->OnTransportChannelCreated();
92 if (webrtc_data_channel_->id() < 0) {
93 webrtc_data_channel_->SetSctpSid(0);
94 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000095 provider_.set_ready_to_send(true);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000096 }
wu@webrtc.org78187522013-10-07 23:32:02 +000097
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +000098 void AddObserver() {
99 observer_.reset(new FakeDataChannelObserver());
100 webrtc_data_channel_->RegisterObserver(observer_.get());
101 }
102
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000103 webrtc::InternalDataChannelInit init_;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000104 FakeDataChannelProvider provider_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000105 rtc::scoped_ptr<FakeDataChannelObserver> observer_;
106 rtc::scoped_refptr<DataChannel> webrtc_data_channel_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000107};
108
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000109// Verifies that the data channel is connected to the transport after creation.
110TEST_F(SctpDataChannelTest, ConnectedToTransportOnCreated) {
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000111 provider_.set_transport_available(true);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000112 rtc::scoped_refptr<DataChannel> dc = DataChannel::Create(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000113 &provider_, cricket::DCT_SCTP, "test1", init_);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000114
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000115 EXPECT_TRUE(provider_.IsConnected(dc.get()));
116 // The sid is not set yet, so it should not have added the streams.
117 EXPECT_FALSE(provider_.IsSendStreamAdded(dc->id()));
118 EXPECT_FALSE(provider_.IsRecvStreamAdded(dc->id()));
119
120 dc->SetSctpSid(0);
121 EXPECT_TRUE(provider_.IsSendStreamAdded(dc->id()));
122 EXPECT_TRUE(provider_.IsRecvStreamAdded(dc->id()));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000123}
124
125// Verifies that the data channel is connected to the transport if the transport
126// is not available initially and becomes available later.
127TEST_F(SctpDataChannelTest, ConnectedAfterTransportBecomesAvailable) {
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000128 EXPECT_FALSE(provider_.IsConnected(webrtc_data_channel_.get()));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000129
130 provider_.set_transport_available(true);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000131 webrtc_data_channel_->OnTransportChannelCreated();
132 EXPECT_TRUE(provider_.IsConnected(webrtc_data_channel_.get()));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000133}
134
wu@webrtc.org78187522013-10-07 23:32:02 +0000135// Tests the state of the data channel.
136TEST_F(SctpDataChannelTest, StateTransition) {
137 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting,
138 webrtc_data_channel_->state());
139 SetChannelReady();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000140
wu@webrtc.org78187522013-10-07 23:32:02 +0000141 EXPECT_EQ(webrtc::DataChannelInterface::kOpen, webrtc_data_channel_->state());
142 webrtc_data_channel_->Close();
143 EXPECT_EQ(webrtc::DataChannelInterface::kClosed,
144 webrtc_data_channel_->state());
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000145 // Verifies that it's disconnected from the transport.
146 EXPECT_FALSE(provider_.IsConnected(webrtc_data_channel_.get()));
wu@webrtc.org78187522013-10-07 23:32:02 +0000147}
148
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000149// Tests that DataChannel::buffered_amount() is correct after the channel is
150// blocked.
151TEST_F(SctpDataChannelTest, BufferedAmountWhenBlocked) {
bemasc0edd50c2015-07-01 13:34:33 -0700152 AddObserver();
wu@webrtc.org78187522013-10-07 23:32:02 +0000153 SetChannelReady();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000154 webrtc::DataBuffer buffer("abcd");
155 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
156
157 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount());
bemasc0edd50c2015-07-01 13:34:33 -0700158 EXPECT_EQ(0U, observer_->on_buffered_amount_change_count());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000159
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000160 provider_.set_send_blocked(true);
wu@webrtc.org78187522013-10-07 23:32:02 +0000161
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000162 const int number_of_packets = 3;
163 for (int i = 0; i < number_of_packets; ++i) {
164 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
165 }
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000166 EXPECT_EQ(buffer.data.size() * number_of_packets,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000167 webrtc_data_channel_->buffered_amount());
bemasc0edd50c2015-07-01 13:34:33 -0700168 EXPECT_EQ(number_of_packets, observer_->on_buffered_amount_change_count());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000169}
170
171// Tests that the queued data are sent when the channel transitions from blocked
172// to unblocked.
173TEST_F(SctpDataChannelTest, QueuedDataSentWhenUnblocked) {
bemasc0edd50c2015-07-01 13:34:33 -0700174 AddObserver();
wu@webrtc.org78187522013-10-07 23:32:02 +0000175 SetChannelReady();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000176 webrtc::DataBuffer buffer("abcd");
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000177 provider_.set_send_blocked(true);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000178 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
179
bemasc0edd50c2015-07-01 13:34:33 -0700180 EXPECT_EQ(1U, observer_->on_buffered_amount_change_count());
181
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000182 provider_.set_send_blocked(false);
wu@webrtc.org78187522013-10-07 23:32:02 +0000183 SetChannelReady();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000184 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount());
bemasc0edd50c2015-07-01 13:34:33 -0700185 EXPECT_EQ(2U, observer_->on_buffered_amount_change_count());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000186}
wu@webrtc.org78187522013-10-07 23:32:02 +0000187
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000188// Tests that no crash when the channel is blocked right away while trying to
189// send queued data.
190TEST_F(SctpDataChannelTest, BlockedWhenSendQueuedDataNoCrash) {
bemasc0edd50c2015-07-01 13:34:33 -0700191 AddObserver();
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000192 SetChannelReady();
193 webrtc::DataBuffer buffer("abcd");
194 provider_.set_send_blocked(true);
195 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
bemasc0edd50c2015-07-01 13:34:33 -0700196 EXPECT_EQ(1U, observer_->on_buffered_amount_change_count());
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000197
198 // Set channel ready while it is still blocked.
199 SetChannelReady();
200 EXPECT_EQ(buffer.size(), webrtc_data_channel_->buffered_amount());
bemasc0edd50c2015-07-01 13:34:33 -0700201 EXPECT_EQ(1U, observer_->on_buffered_amount_change_count());
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000202
203 // Unblock the channel to send queued data again, there should be no crash.
204 provider_.set_send_blocked(false);
205 SetChannelReady();
206 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount());
bemasc0edd50c2015-07-01 13:34:33 -0700207 EXPECT_EQ(2U, observer_->on_buffered_amount_change_count());
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000208}
209
wu@webrtc.org78187522013-10-07 23:32:02 +0000210// Tests that the queued control message is sent when channel is ready.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000211TEST_F(SctpDataChannelTest, OpenMessageSent) {
212 // Initially the id is unassigned.
213 EXPECT_EQ(-1, webrtc_data_channel_->id());
214
wu@webrtc.org78187522013-10-07 23:32:02 +0000215 SetChannelReady();
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000216 EXPECT_GE(webrtc_data_channel_->id(), 0);
217 EXPECT_EQ(cricket::DMT_CONTROL, provider_.last_send_data_params().type);
218 EXPECT_EQ(provider_.last_send_data_params().ssrc,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200219 static_cast<uint32_t>(webrtc_data_channel_->id()));
wu@webrtc.org78187522013-10-07 23:32:02 +0000220}
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000221
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000222TEST_F(SctpDataChannelTest, QueuedOpenMessageSent) {
223 provider_.set_send_blocked(true);
224 SetChannelReady();
225 provider_.set_send_blocked(false);
226
227 EXPECT_EQ(cricket::DMT_CONTROL, provider_.last_send_data_params().type);
228 EXPECT_EQ(provider_.last_send_data_params().ssrc,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200229 static_cast<uint32_t>(webrtc_data_channel_->id()));
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000230}
231
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000232// Tests that the DataChannel created after transport gets ready can enter OPEN
233// state.
234TEST_F(SctpDataChannelTest, LateCreatedChannelTransitionToOpen) {
235 SetChannelReady();
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000236 webrtc::InternalDataChannelInit init;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000237 init.id = 1;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000238 rtc::scoped_refptr<DataChannel> dc = DataChannel::Create(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000239 &provider_, cricket::DCT_SCTP, "test1", init);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000240 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, dc->state());
241 EXPECT_TRUE_WAIT(webrtc::DataChannelInterface::kOpen == dc->state(),
242 1000);
243}
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000244
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000245// Tests that an unordered DataChannel sends data as ordered until the OPEN_ACK
246// message is received.
247TEST_F(SctpDataChannelTest, SendUnorderedAfterReceivesOpenAck) {
248 SetChannelReady();
249 webrtc::InternalDataChannelInit init;
250 init.id = 1;
251 init.ordered = false;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000252 rtc::scoped_refptr<DataChannel> dc = DataChannel::Create(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000253 &provider_, cricket::DCT_SCTP, "test1", init);
254
255 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000);
256
257 // Sends a message and verifies it's ordered.
258 webrtc::DataBuffer buffer("some data");
259 ASSERT_TRUE(dc->Send(buffer));
260 EXPECT_TRUE(provider_.last_send_data_params().ordered);
261
262 // Emulates receiving an OPEN_ACK message.
263 cricket::ReceiveDataParams params;
264 params.ssrc = init.id;
265 params.type = cricket::DMT_CONTROL;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000266 rtc::Buffer payload;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000267 webrtc::WriteDataChannelOpenAckMessage(&payload);
268 dc->OnDataReceived(NULL, params, payload);
269
270 // Sends another message and verifies it's unordered.
271 ASSERT_TRUE(dc->Send(buffer));
272 EXPECT_FALSE(provider_.last_send_data_params().ordered);
273}
274
275// Tests that an unordered DataChannel sends unordered data after any DATA
276// message is received.
277TEST_F(SctpDataChannelTest, SendUnorderedAfterReceiveData) {
278 SetChannelReady();
279 webrtc::InternalDataChannelInit init;
280 init.id = 1;
281 init.ordered = false;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000282 rtc::scoped_refptr<DataChannel> dc = DataChannel::Create(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000283 &provider_, cricket::DCT_SCTP, "test1", init);
284
285 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000);
286
287 // Emulates receiving a DATA message.
288 cricket::ReceiveDataParams params;
289 params.ssrc = init.id;
290 params.type = cricket::DMT_TEXT;
291 webrtc::DataBuffer buffer("data");
292 dc->OnDataReceived(NULL, params, buffer.data);
293
294 // Sends a message and verifies it's unordered.
295 ASSERT_TRUE(dc->Send(buffer));
296 EXPECT_FALSE(provider_.last_send_data_params().ordered);
297}
298
Lally Singh5c6c6e02015-05-29 11:52:39 -0400299// Tests that the channel can't open until it's successfully sent the OPEN
300// message.
301TEST_F(SctpDataChannelTest, OpenWaitsForOpenMesssage) {
302 webrtc::DataBuffer buffer("foo");
303
304 provider_.set_send_blocked(true);
305 SetChannelReady();
306 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting,
307 webrtc_data_channel_->state());
308 provider_.set_send_blocked(false);
309 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen,
310 webrtc_data_channel_->state(), 1000);
311 EXPECT_EQ(cricket::DMT_CONTROL, provider_.last_send_data_params().type);
312}
313
314// Tests that close first makes sure all queued data gets sent.
315TEST_F(SctpDataChannelTest, QueuedCloseFlushes) {
316 webrtc::DataBuffer buffer("foo");
317
318 provider_.set_send_blocked(true);
319 SetChannelReady();
320 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting,
321 webrtc_data_channel_->state());
322 provider_.set_send_blocked(false);
323 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen,
324 webrtc_data_channel_->state(), 1000);
325 provider_.set_send_blocked(true);
326 webrtc_data_channel_->Send(buffer);
327 webrtc_data_channel_->Close();
328 provider_.set_send_blocked(false);
329 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed,
330 webrtc_data_channel_->state(), 1000);
331 EXPECT_EQ(cricket::DMT_TEXT, provider_.last_send_data_params().type);
332}
333
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000334// Tests that messages are sent with the right ssrc.
335TEST_F(SctpDataChannelTest, SendDataSsrc) {
336 webrtc_data_channel_->SetSctpSid(1);
337 SetChannelReady();
338 webrtc::DataBuffer buffer("data");
339 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
340 EXPECT_EQ(1U, provider_.last_send_data_params().ssrc);
341}
342
343// Tests that the incoming messages with wrong ssrcs are rejected.
344TEST_F(SctpDataChannelTest, ReceiveDataWithInvalidSsrc) {
345 webrtc_data_channel_->SetSctpSid(1);
346 SetChannelReady();
347
348 AddObserver();
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000349
350 cricket::ReceiveDataParams params;
351 params.ssrc = 0;
352 webrtc::DataBuffer buffer("abcd");
353 webrtc_data_channel_->OnDataReceived(NULL, params, buffer.data);
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000354
355 EXPECT_EQ(0U, observer_->messages_received());
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000356}
357
358// Tests that the incoming messages with right ssrcs are acceted.
359TEST_F(SctpDataChannelTest, ReceiveDataWithValidSsrc) {
360 webrtc_data_channel_->SetSctpSid(1);
361 SetChannelReady();
362
363 AddObserver();
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000364
365 cricket::ReceiveDataParams params;
366 params.ssrc = 1;
367 webrtc::DataBuffer buffer("abcd");
368
369 webrtc_data_channel_->OnDataReceived(NULL, params, buffer.data);
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000370 EXPECT_EQ(1U, observer_->messages_received());
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000371}
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000372
373// Tests that no CONTROL message is sent if the datachannel is negotiated and
374// not created from an OPEN message.
375TEST_F(SctpDataChannelTest, NoMsgSentIfNegotiatedAndNotFromOpenMsg) {
376 webrtc::InternalDataChannelInit config;
377 config.id = 1;
378 config.negotiated = true;
379 config.open_handshake_role = webrtc::InternalDataChannelInit::kNone;
380
381 SetChannelReady();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000382 rtc::scoped_refptr<DataChannel> dc = DataChannel::Create(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000383 &provider_, cricket::DCT_SCTP, "test1", config);
384
385 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000);
386 EXPECT_EQ(0U, provider_.last_send_data_params().ssrc);
387}
388
389// Tests that OPEN_ACK message is sent if the datachannel is created from an
390// OPEN message.
391TEST_F(SctpDataChannelTest, OpenAckSentIfCreatedFromOpenMessage) {
392 webrtc::InternalDataChannelInit config;
393 config.id = 1;
394 config.negotiated = true;
395 config.open_handshake_role = webrtc::InternalDataChannelInit::kAcker;
396
397 SetChannelReady();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000398 rtc::scoped_refptr<DataChannel> dc = DataChannel::Create(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000399 &provider_, cricket::DCT_SCTP, "test1", config);
400
401 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000);
402
403 EXPECT_EQ(static_cast<unsigned int>(config.id),
404 provider_.last_send_data_params().ssrc);
405 EXPECT_EQ(cricket::DMT_CONTROL, provider_.last_send_data_params().type);
406}
407
408// Tests the OPEN_ACK role assigned by InternalDataChannelInit.
409TEST_F(SctpDataChannelTest, OpenAckRoleInitialization) {
410 webrtc::InternalDataChannelInit init;
411 EXPECT_EQ(webrtc::InternalDataChannelInit::kOpener, init.open_handshake_role);
412 EXPECT_FALSE(init.negotiated);
413
414 webrtc::DataChannelInit base;
415 base.negotiated = true;
416 webrtc::InternalDataChannelInit init2(base);
417 EXPECT_EQ(webrtc::InternalDataChannelInit::kNone, init2.open_handshake_role);
418}
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000419
420// Tests that the DataChannel is closed if the sending buffer is full.
421TEST_F(SctpDataChannelTest, ClosedWhenSendBufferFull) {
422 SetChannelReady();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000423
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000424 rtc::Buffer buffer(1024);
425 memset(buffer.data(), 0, buffer.size());
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000426
427 webrtc::DataBuffer packet(buffer, true);
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000428 provider_.set_send_blocked(true);
429
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000430 for (size_t i = 0; i < 16 * 1024 + 1; ++i) {
431 EXPECT_TRUE(webrtc_data_channel_->Send(packet));
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000432 }
433
Lally Singh5c6c6e02015-05-29 11:52:39 -0400434 EXPECT_TRUE(
435 webrtc::DataChannelInterface::kClosed == webrtc_data_channel_->state() ||
436 webrtc::DataChannelInterface::kClosing == webrtc_data_channel_->state());
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000437}
438
439// Tests that the DataChannel is closed on transport errors.
440TEST_F(SctpDataChannelTest, ClosedOnTransportError) {
441 SetChannelReady();
442 webrtc::DataBuffer buffer("abcd");
443 provider_.set_transport_error();
444
445 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
446
447 EXPECT_EQ(webrtc::DataChannelInterface::kClosed,
448 webrtc_data_channel_->state());
449}
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000450
451// Tests that a already closed DataChannel does not fire onStateChange again.
452TEST_F(SctpDataChannelTest, ClosedDataChannelDoesNotFireOnStateChange) {
453 AddObserver();
454 webrtc_data_channel_->Close();
455 // OnStateChange called for kClosing and kClosed.
456 EXPECT_EQ(2U, observer_->on_state_change_count());
457
458 observer_->ResetOnStateChangeCount();
459 webrtc_data_channel_->RemotePeerRequestClose();
460 EXPECT_EQ(0U, observer_->on_state_change_count());
461}
462
463// Tests that RemotePeerRequestClose closes the local DataChannel.
464TEST_F(SctpDataChannelTest, RemotePeerRequestClose) {
465 AddObserver();
466 webrtc_data_channel_->RemotePeerRequestClose();
467
468 // OnStateChange called for kClosing and kClosed.
469 EXPECT_EQ(2U, observer_->on_state_change_count());
470 EXPECT_EQ(webrtc::DataChannelInterface::kClosed,
471 webrtc_data_channel_->state());
472}
473
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000474// Tests that the DataChannel is closed if the received buffer is full.
475TEST_F(SctpDataChannelTest, ClosedWhenReceivedBufferFull) {
476 SetChannelReady();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000477 rtc::Buffer buffer(1024);
478 memset(buffer.data(), 0, buffer.size());
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000479
480 cricket::ReceiveDataParams params;
481 params.ssrc = 0;
482
483 // Receiving data without having an observer will overflow the buffer.
484 for (size_t i = 0; i < 16 * 1024 + 1; ++i) {
485 webrtc_data_channel_->OnDataReceived(NULL, params, buffer);
486 }
487 EXPECT_EQ(webrtc::DataChannelInterface::kClosed,
488 webrtc_data_channel_->state());
489}
jiayl@webrtc.org3edbaaf2014-07-18 23:57:50 +0000490
491// Tests that sending empty data returns no error and keeps the channel open.
492TEST_F(SctpDataChannelTest, SendEmptyData) {
493 webrtc_data_channel_->SetSctpSid(1);
494 SetChannelReady();
495 EXPECT_EQ(webrtc::DataChannelInterface::kOpen,
496 webrtc_data_channel_->state());
497
498 webrtc::DataBuffer buffer("");
499 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
500 EXPECT_EQ(webrtc::DataChannelInterface::kOpen,
501 webrtc_data_channel_->state());
502}
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +0000503
504// Tests that a channel can be closed without being opened or assigned an sid.
505TEST_F(SctpDataChannelTest, NeverOpened) {
506 provider_.set_transport_available(true);
507 webrtc_data_channel_->OnTransportChannelCreated();
508 webrtc_data_channel_->Close();
509}
deadbeefab9b2d12015-10-14 11:33:11 -0700510
511class SctpSidAllocatorTest : public testing::Test {
512 protected:
513 SctpSidAllocator allocator_;
514};
515
516// Verifies that an even SCTP id is allocated for SSL_CLIENT and an odd id for
517// SSL_SERVER.
518TEST_F(SctpSidAllocatorTest, SctpIdAllocationBasedOnRole) {
519 int id;
520 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &id));
521 EXPECT_EQ(1, id);
522 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &id));
523 EXPECT_EQ(0, id);
524 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &id));
525 EXPECT_EQ(3, id);
526 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &id));
527 EXPECT_EQ(2, id);
528}
529
530// Verifies that SCTP ids of existing DataChannels are not reused.
531TEST_F(SctpSidAllocatorTest, SctpIdAllocationNoReuse) {
532 int old_id = 1;
533 EXPECT_TRUE(allocator_.ReserveSid(old_id));
534
535 int new_id;
536 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &new_id));
537 EXPECT_NE(old_id, new_id);
538
539 old_id = 0;
540 EXPECT_TRUE(allocator_.ReserveSid(old_id));
541 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &new_id));
542 EXPECT_NE(old_id, new_id);
543}
544
545// Verifies that SCTP ids of removed DataChannels can be reused.
546TEST_F(SctpSidAllocatorTest, SctpIdReusedForRemovedDataChannel) {
547 int odd_id = 1;
548 int even_id = 0;
549 EXPECT_TRUE(allocator_.ReserveSid(odd_id));
550 EXPECT_TRUE(allocator_.ReserveSid(even_id));
551
552 int allocated_id = -1;
553 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id));
554 EXPECT_EQ(odd_id + 2, allocated_id);
555
556 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id));
557 EXPECT_EQ(even_id + 2, allocated_id);
558
559 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id));
560 EXPECT_EQ(odd_id + 4, allocated_id);
561
562 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id));
563 EXPECT_EQ(even_id + 4, allocated_id);
564
565 allocator_.ReleaseSid(odd_id);
566 allocator_.ReleaseSid(even_id);
567
568 // Verifies that removed ids are reused.
569 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id));
570 EXPECT_EQ(odd_id, allocated_id);
571
572 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id));
573 EXPECT_EQ(even_id, allocated_id);
574
575 // Verifies that used higher ids are not reused.
576 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id));
577 EXPECT_EQ(odd_id + 6, allocated_id);
578
579 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id));
580 EXPECT_EQ(even_id + 6, allocated_id);
581}