blob: 773e7523e6c7e8be85c6abd81f9c3a95df052a7b [file] [log] [blame]
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.orgd64719d2013-08-01 00:00:07 +00009 */
10
kwibergd1fe2812016-04-27 06:47:29 -070011#include <memory>
12
Henrik Kjellander15583c12016-02-10 10:53:12 +010013#include "webrtc/api/datachannel.h"
14#include "webrtc/api/sctputils.h"
15#include "webrtc/api/test/fakedatachannelprovider.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000016#include "webrtc/base/gunit.h"
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000017
wu@webrtc.org78187522013-10-07 23:32:02 +000018using webrtc::DataChannel;
deadbeefab9b2d12015-10-14 11:33:11 -070019using webrtc::SctpSidAllocator;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000020
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -070021static constexpr int kDefaultTimeout = 10000;
22
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +000023class FakeDataChannelObserver : public webrtc::DataChannelObserver {
24 public:
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +000025 FakeDataChannelObserver()
bemasc0edd50c2015-07-01 13:34:33 -070026 : messages_received_(0),
27 on_state_change_count_(0),
28 on_buffered_amount_change_count_(0) {}
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +000029
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +000030 void OnStateChange() {
31 ++on_state_change_count_;
32 }
33
Peter Boström0c4e06b2015-10-07 12:23:21 +020034 void OnBufferedAmountChange(uint64_t previous_amount) {
bemasc0edd50c2015-07-01 13:34:33 -070035 ++on_buffered_amount_change_count_;
36 }
37
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +000038 void OnMessage(const webrtc::DataBuffer& buffer) {
39 ++messages_received_;
40 }
41
42 size_t messages_received() const {
43 return messages_received_;
44 }
45
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +000046 void ResetOnStateChangeCount() {
47 on_state_change_count_ = 0;
48 }
49
bemasc0edd50c2015-07-01 13:34:33 -070050 void ResetOnBufferedAmountChangeCount() {
51 on_buffered_amount_change_count_ = 0;
52 }
53
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +000054 size_t on_state_change_count() const {
55 return on_state_change_count_;
56 }
57
bemasc0edd50c2015-07-01 13:34:33 -070058 size_t on_buffered_amount_change_count() const {
59 return on_buffered_amount_change_count_;
60 }
61
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +000062 private:
63 size_t messages_received_;
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +000064 size_t on_state_change_count_;
bemasc0edd50c2015-07-01 13:34:33 -070065 size_t on_buffered_amount_change_count_;
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +000066};
67
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000068class SctpDataChannelTest : public testing::Test {
69 protected:
70 SctpDataChannelTest()
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -070071 : provider_(new FakeDataChannelProvider()),
72 webrtc_data_channel_(DataChannel::Create(provider_.get(),
73 cricket::DCT_SCTP,
74 "test",
75 init_)) {}
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000076
wu@webrtc.org78187522013-10-07 23:32:02 +000077 void SetChannelReady() {
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -070078 provider_->set_transport_available(true);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000079 webrtc_data_channel_->OnTransportChannelCreated();
80 if (webrtc_data_channel_->id() < 0) {
81 webrtc_data_channel_->SetSctpSid(0);
82 }
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -070083 provider_->set_ready_to_send(true);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000084 }
wu@webrtc.org78187522013-10-07 23:32:02 +000085
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +000086 void AddObserver() {
87 observer_.reset(new FakeDataChannelObserver());
88 webrtc_data_channel_->RegisterObserver(observer_.get());
89 }
90
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000091 webrtc::InternalDataChannelInit init_;
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -070092 std::unique_ptr<FakeDataChannelProvider> provider_;
kwibergd1fe2812016-04-27 06:47:29 -070093 std::unique_ptr<FakeDataChannelObserver> observer_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000094 rtc::scoped_refptr<DataChannel> webrtc_data_channel_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000095};
96
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000097// Verifies that the data channel is connected to the transport after creation.
98TEST_F(SctpDataChannelTest, ConnectedToTransportOnCreated) {
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -070099 provider_->set_transport_available(true);
100 rtc::scoped_refptr<DataChannel> dc =
101 DataChannel::Create(provider_.get(), cricket::DCT_SCTP, "test1", init_);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000102
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700103 EXPECT_TRUE(provider_->IsConnected(dc.get()));
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000104 // The sid is not set yet, so it should not have added the streams.
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700105 EXPECT_FALSE(provider_->IsSendStreamAdded(dc->id()));
106 EXPECT_FALSE(provider_->IsRecvStreamAdded(dc->id()));
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000107
108 dc->SetSctpSid(0);
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700109 EXPECT_TRUE(provider_->IsSendStreamAdded(dc->id()));
110 EXPECT_TRUE(provider_->IsRecvStreamAdded(dc->id()));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000111}
112
113// Verifies that the data channel is connected to the transport if the transport
114// is not available initially and becomes available later.
115TEST_F(SctpDataChannelTest, ConnectedAfterTransportBecomesAvailable) {
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700116 EXPECT_FALSE(provider_->IsConnected(webrtc_data_channel_.get()));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000117
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700118 provider_->set_transport_available(true);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000119 webrtc_data_channel_->OnTransportChannelCreated();
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700120 EXPECT_TRUE(provider_->IsConnected(webrtc_data_channel_.get()));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000121}
122
wu@webrtc.org78187522013-10-07 23:32:02 +0000123// Tests the state of the data channel.
124TEST_F(SctpDataChannelTest, StateTransition) {
125 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting,
126 webrtc_data_channel_->state());
127 SetChannelReady();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000128
wu@webrtc.org78187522013-10-07 23:32:02 +0000129 EXPECT_EQ(webrtc::DataChannelInterface::kOpen, webrtc_data_channel_->state());
130 webrtc_data_channel_->Close();
131 EXPECT_EQ(webrtc::DataChannelInterface::kClosed,
132 webrtc_data_channel_->state());
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000133 // Verifies that it's disconnected from the transport.
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700134 EXPECT_FALSE(provider_->IsConnected(webrtc_data_channel_.get()));
wu@webrtc.org78187522013-10-07 23:32:02 +0000135}
136
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000137// Tests that DataChannel::buffered_amount() is correct after the channel is
138// blocked.
139TEST_F(SctpDataChannelTest, BufferedAmountWhenBlocked) {
bemasc0edd50c2015-07-01 13:34:33 -0700140 AddObserver();
wu@webrtc.org78187522013-10-07 23:32:02 +0000141 SetChannelReady();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000142 webrtc::DataBuffer buffer("abcd");
143 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
144
145 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount());
bemasc0edd50c2015-07-01 13:34:33 -0700146 EXPECT_EQ(0U, observer_->on_buffered_amount_change_count());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000147
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700148 provider_->set_send_blocked(true);
wu@webrtc.org78187522013-10-07 23:32:02 +0000149
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000150 const int number_of_packets = 3;
151 for (int i = 0; i < number_of_packets; ++i) {
152 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
153 }
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000154 EXPECT_EQ(buffer.data.size() * number_of_packets,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000155 webrtc_data_channel_->buffered_amount());
bemasc0edd50c2015-07-01 13:34:33 -0700156 EXPECT_EQ(number_of_packets, observer_->on_buffered_amount_change_count());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000157}
158
159// Tests that the queued data are sent when the channel transitions from blocked
160// to unblocked.
161TEST_F(SctpDataChannelTest, QueuedDataSentWhenUnblocked) {
bemasc0edd50c2015-07-01 13:34:33 -0700162 AddObserver();
wu@webrtc.org78187522013-10-07 23:32:02 +0000163 SetChannelReady();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000164 webrtc::DataBuffer buffer("abcd");
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700165 provider_->set_send_blocked(true);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000166 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
167
bemasc0edd50c2015-07-01 13:34:33 -0700168 EXPECT_EQ(1U, observer_->on_buffered_amount_change_count());
169
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700170 provider_->set_send_blocked(false);
wu@webrtc.org78187522013-10-07 23:32:02 +0000171 SetChannelReady();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000172 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount());
bemasc0edd50c2015-07-01 13:34:33 -0700173 EXPECT_EQ(2U, observer_->on_buffered_amount_change_count());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000174}
wu@webrtc.org78187522013-10-07 23:32:02 +0000175
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000176// Tests that no crash when the channel is blocked right away while trying to
177// send queued data.
178TEST_F(SctpDataChannelTest, BlockedWhenSendQueuedDataNoCrash) {
bemasc0edd50c2015-07-01 13:34:33 -0700179 AddObserver();
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000180 SetChannelReady();
181 webrtc::DataBuffer buffer("abcd");
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700182 provider_->set_send_blocked(true);
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000183 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
bemasc0edd50c2015-07-01 13:34:33 -0700184 EXPECT_EQ(1U, observer_->on_buffered_amount_change_count());
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000185
186 // Set channel ready while it is still blocked.
187 SetChannelReady();
188 EXPECT_EQ(buffer.size(), webrtc_data_channel_->buffered_amount());
bemasc0edd50c2015-07-01 13:34:33 -0700189 EXPECT_EQ(1U, observer_->on_buffered_amount_change_count());
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000190
191 // Unblock the channel to send queued data again, there should be no crash.
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700192 provider_->set_send_blocked(false);
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000193 SetChannelReady();
194 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount());
bemasc0edd50c2015-07-01 13:34:33 -0700195 EXPECT_EQ(2U, observer_->on_buffered_amount_change_count());
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000196}
197
hbos84ffdee2016-10-12 14:14:39 -0700198// Tests that DataChannel::messages_sent() and DataChannel::bytes_sent() are
199// correct, sending data both while unblocked and while blocked.
200TEST_F(SctpDataChannelTest, VerifyMessagesAndBytesSent) {
201 AddObserver();
202 SetChannelReady();
203 std::vector<webrtc::DataBuffer> buffers({
204 webrtc::DataBuffer("message 1"),
205 webrtc::DataBuffer("msg 2"),
206 webrtc::DataBuffer("message three"),
207 webrtc::DataBuffer("quadra message"),
208 webrtc::DataBuffer("fifthmsg"),
209 webrtc::DataBuffer("message of the beast"),
210 });
211
212 // Default values.
213 EXPECT_EQ(0U, webrtc_data_channel_->messages_sent());
214 EXPECT_EQ(0U, webrtc_data_channel_->bytes_sent());
215
216 // Send three buffers while not blocked.
217 provider_->set_send_blocked(false);
218 EXPECT_TRUE(webrtc_data_channel_->Send(buffers[0]));
219 EXPECT_TRUE(webrtc_data_channel_->Send(buffers[1]));
220 EXPECT_TRUE(webrtc_data_channel_->Send(buffers[2]));
221 size_t bytes_sent = buffers[0].size() + buffers[1].size() + buffers[2].size();
222 EXPECT_EQ_WAIT(0U, webrtc_data_channel_->buffered_amount(), kDefaultTimeout);
223 EXPECT_EQ(3U, webrtc_data_channel_->messages_sent());
224 EXPECT_EQ(bytes_sent, webrtc_data_channel_->bytes_sent());
225
226 // Send three buffers while blocked, queuing the buffers.
227 provider_->set_send_blocked(true);
228 EXPECT_TRUE(webrtc_data_channel_->Send(buffers[3]));
229 EXPECT_TRUE(webrtc_data_channel_->Send(buffers[4]));
230 EXPECT_TRUE(webrtc_data_channel_->Send(buffers[5]));
231 size_t bytes_queued =
232 buffers[3].size() + buffers[4].size() + buffers[5].size();
233 EXPECT_EQ(bytes_queued, webrtc_data_channel_->buffered_amount());
234 EXPECT_EQ(3U, webrtc_data_channel_->messages_sent());
235 EXPECT_EQ(bytes_sent, webrtc_data_channel_->bytes_sent());
236
237 // Unblock and make sure everything was sent.
238 provider_->set_send_blocked(false);
239 EXPECT_EQ_WAIT(0U, webrtc_data_channel_->buffered_amount(), kDefaultTimeout);
240 bytes_sent += bytes_queued;
241 EXPECT_EQ(6U, webrtc_data_channel_->messages_sent());
242 EXPECT_EQ(bytes_sent, webrtc_data_channel_->bytes_sent());
243}
244
wu@webrtc.org78187522013-10-07 23:32:02 +0000245// Tests that the queued control message is sent when channel is ready.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000246TEST_F(SctpDataChannelTest, OpenMessageSent) {
247 // Initially the id is unassigned.
248 EXPECT_EQ(-1, webrtc_data_channel_->id());
249
wu@webrtc.org78187522013-10-07 23:32:02 +0000250 SetChannelReady();
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000251 EXPECT_GE(webrtc_data_channel_->id(), 0);
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700252 EXPECT_EQ(cricket::DMT_CONTROL, provider_->last_send_data_params().type);
253 EXPECT_EQ(provider_->last_send_data_params().ssrc,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200254 static_cast<uint32_t>(webrtc_data_channel_->id()));
wu@webrtc.org78187522013-10-07 23:32:02 +0000255}
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000256
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000257TEST_F(SctpDataChannelTest, QueuedOpenMessageSent) {
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700258 provider_->set_send_blocked(true);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000259 SetChannelReady();
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700260 provider_->set_send_blocked(false);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000261
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700262 EXPECT_EQ(cricket::DMT_CONTROL, provider_->last_send_data_params().type);
263 EXPECT_EQ(provider_->last_send_data_params().ssrc,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200264 static_cast<uint32_t>(webrtc_data_channel_->id()));
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000265}
266
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000267// Tests that the DataChannel created after transport gets ready can enter OPEN
268// state.
269TEST_F(SctpDataChannelTest, LateCreatedChannelTransitionToOpen) {
270 SetChannelReady();
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000271 webrtc::InternalDataChannelInit init;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000272 init.id = 1;
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700273 rtc::scoped_refptr<DataChannel> dc =
274 DataChannel::Create(provider_.get(), cricket::DCT_SCTP, "test1", init);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000275 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, dc->state());
276 EXPECT_TRUE_WAIT(webrtc::DataChannelInterface::kOpen == dc->state(),
277 1000);
278}
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000279
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000280// Tests that an unordered DataChannel sends data as ordered until the OPEN_ACK
281// message is received.
282TEST_F(SctpDataChannelTest, SendUnorderedAfterReceivesOpenAck) {
283 SetChannelReady();
284 webrtc::InternalDataChannelInit init;
285 init.id = 1;
286 init.ordered = false;
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700287 rtc::scoped_refptr<DataChannel> dc =
288 DataChannel::Create(provider_.get(), cricket::DCT_SCTP, "test1", init);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000289
290 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000);
291
292 // Sends a message and verifies it's ordered.
293 webrtc::DataBuffer buffer("some data");
294 ASSERT_TRUE(dc->Send(buffer));
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700295 EXPECT_TRUE(provider_->last_send_data_params().ordered);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000296
297 // Emulates receiving an OPEN_ACK message.
298 cricket::ReceiveDataParams params;
299 params.ssrc = init.id;
300 params.type = cricket::DMT_CONTROL;
jbaucheec21bd2016-03-20 06:15:43 -0700301 rtc::CopyOnWriteBuffer payload;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000302 webrtc::WriteDataChannelOpenAckMessage(&payload);
303 dc->OnDataReceived(NULL, params, payload);
304
305 // Sends another message and verifies it's unordered.
306 ASSERT_TRUE(dc->Send(buffer));
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700307 EXPECT_FALSE(provider_->last_send_data_params().ordered);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000308}
309
310// Tests that an unordered DataChannel sends unordered data after any DATA
311// message is received.
312TEST_F(SctpDataChannelTest, SendUnorderedAfterReceiveData) {
313 SetChannelReady();
314 webrtc::InternalDataChannelInit init;
315 init.id = 1;
316 init.ordered = false;
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700317 rtc::scoped_refptr<DataChannel> dc =
318 DataChannel::Create(provider_.get(), cricket::DCT_SCTP, "test1", init);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000319
320 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000);
321
322 // Emulates receiving a DATA message.
323 cricket::ReceiveDataParams params;
324 params.ssrc = init.id;
325 params.type = cricket::DMT_TEXT;
326 webrtc::DataBuffer buffer("data");
327 dc->OnDataReceived(NULL, params, buffer.data);
328
329 // Sends a message and verifies it's unordered.
330 ASSERT_TRUE(dc->Send(buffer));
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700331 EXPECT_FALSE(provider_->last_send_data_params().ordered);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000332}
333
Lally Singh5c6c6e02015-05-29 11:52:39 -0400334// Tests that the channel can't open until it's successfully sent the OPEN
335// message.
336TEST_F(SctpDataChannelTest, OpenWaitsForOpenMesssage) {
337 webrtc::DataBuffer buffer("foo");
338
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700339 provider_->set_send_blocked(true);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400340 SetChannelReady();
341 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting,
342 webrtc_data_channel_->state());
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700343 provider_->set_send_blocked(false);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400344 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen,
345 webrtc_data_channel_->state(), 1000);
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700346 EXPECT_EQ(cricket::DMT_CONTROL, provider_->last_send_data_params().type);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400347}
348
349// Tests that close first makes sure all queued data gets sent.
350TEST_F(SctpDataChannelTest, QueuedCloseFlushes) {
351 webrtc::DataBuffer buffer("foo");
352
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700353 provider_->set_send_blocked(true);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400354 SetChannelReady();
355 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting,
356 webrtc_data_channel_->state());
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700357 provider_->set_send_blocked(false);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400358 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen,
359 webrtc_data_channel_->state(), 1000);
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700360 provider_->set_send_blocked(true);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400361 webrtc_data_channel_->Send(buffer);
362 webrtc_data_channel_->Close();
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700363 provider_->set_send_blocked(false);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400364 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed,
365 webrtc_data_channel_->state(), 1000);
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700366 EXPECT_EQ(cricket::DMT_TEXT, provider_->last_send_data_params().type);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400367}
368
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000369// Tests that messages are sent with the right ssrc.
370TEST_F(SctpDataChannelTest, SendDataSsrc) {
371 webrtc_data_channel_->SetSctpSid(1);
372 SetChannelReady();
373 webrtc::DataBuffer buffer("data");
374 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700375 EXPECT_EQ(1U, provider_->last_send_data_params().ssrc);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000376}
377
378// Tests that the incoming messages with wrong ssrcs are rejected.
379TEST_F(SctpDataChannelTest, ReceiveDataWithInvalidSsrc) {
380 webrtc_data_channel_->SetSctpSid(1);
381 SetChannelReady();
382
383 AddObserver();
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000384
385 cricket::ReceiveDataParams params;
386 params.ssrc = 0;
387 webrtc::DataBuffer buffer("abcd");
388 webrtc_data_channel_->OnDataReceived(NULL, params, buffer.data);
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000389
390 EXPECT_EQ(0U, observer_->messages_received());
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000391}
392
393// Tests that the incoming messages with right ssrcs are acceted.
394TEST_F(SctpDataChannelTest, ReceiveDataWithValidSsrc) {
395 webrtc_data_channel_->SetSctpSid(1);
396 SetChannelReady();
397
398 AddObserver();
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000399
400 cricket::ReceiveDataParams params;
401 params.ssrc = 1;
402 webrtc::DataBuffer buffer("abcd");
403
404 webrtc_data_channel_->OnDataReceived(NULL, params, buffer.data);
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000405 EXPECT_EQ(1U, observer_->messages_received());
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000406}
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000407
408// Tests that no CONTROL message is sent if the datachannel is negotiated and
409// not created from an OPEN message.
410TEST_F(SctpDataChannelTest, NoMsgSentIfNegotiatedAndNotFromOpenMsg) {
411 webrtc::InternalDataChannelInit config;
412 config.id = 1;
413 config.negotiated = true;
414 config.open_handshake_role = webrtc::InternalDataChannelInit::kNone;
415
416 SetChannelReady();
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700417 rtc::scoped_refptr<DataChannel> dc =
418 DataChannel::Create(provider_.get(), cricket::DCT_SCTP, "test1", config);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000419
420 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000);
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700421 EXPECT_EQ(0U, provider_->last_send_data_params().ssrc);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000422}
423
hbos84ffdee2016-10-12 14:14:39 -0700424// Tests that DataChannel::messages_received() and DataChannel::bytes_received()
425// are correct, receiving data both while not open and while open.
426TEST_F(SctpDataChannelTest, VerifyMessagesAndBytesReceived) {
427 AddObserver();
428 std::vector<webrtc::DataBuffer> buffers({
429 webrtc::DataBuffer("message 1"),
430 webrtc::DataBuffer("msg 2"),
431 webrtc::DataBuffer("message three"),
432 webrtc::DataBuffer("quadra message"),
433 webrtc::DataBuffer("fifthmsg"),
434 webrtc::DataBuffer("message of the beast"),
435 });
436
437 webrtc_data_channel_->SetSctpSid(1);
438 cricket::ReceiveDataParams params;
439 params.ssrc = 1;
440
441 // Default values.
442 EXPECT_EQ(0U, webrtc_data_channel_->messages_received());
443 EXPECT_EQ(0U, webrtc_data_channel_->bytes_received());
444
445 // Receive three buffers while data channel isn't open.
446 webrtc_data_channel_->OnDataReceived(nullptr, params, buffers[0].data);
447 webrtc_data_channel_->OnDataReceived(nullptr, params, buffers[1].data);
448 webrtc_data_channel_->OnDataReceived(nullptr, params, buffers[2].data);
449 EXPECT_EQ(0U, observer_->messages_received());
450 EXPECT_EQ(0U, webrtc_data_channel_->messages_received());
451 EXPECT_EQ(0U, webrtc_data_channel_->bytes_received());
452
453 // Open channel and make sure everything was received.
454 SetChannelReady();
455 size_t bytes_received =
456 buffers[0].size() + buffers[1].size() + buffers[2].size();
457 EXPECT_EQ(3U, observer_->messages_received());
458 EXPECT_EQ(3U, webrtc_data_channel_->messages_received());
459 EXPECT_EQ(bytes_received, webrtc_data_channel_->bytes_received());
460
461 // Receive three buffers while open.
462 webrtc_data_channel_->OnDataReceived(nullptr, params, buffers[3].data);
463 webrtc_data_channel_->OnDataReceived(nullptr, params, buffers[4].data);
464 webrtc_data_channel_->OnDataReceived(nullptr, params, buffers[5].data);
465 bytes_received += buffers[3].size() + buffers[4].size() + buffers[5].size();
466 EXPECT_EQ(6U, observer_->messages_received());
467 EXPECT_EQ(6U, webrtc_data_channel_->messages_received());
468 EXPECT_EQ(bytes_received, webrtc_data_channel_->bytes_received());
469}
470
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000471// Tests that OPEN_ACK message is sent if the datachannel is created from an
472// OPEN message.
473TEST_F(SctpDataChannelTest, OpenAckSentIfCreatedFromOpenMessage) {
474 webrtc::InternalDataChannelInit config;
475 config.id = 1;
476 config.negotiated = true;
477 config.open_handshake_role = webrtc::InternalDataChannelInit::kAcker;
478
479 SetChannelReady();
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700480 rtc::scoped_refptr<DataChannel> dc =
481 DataChannel::Create(provider_.get(), cricket::DCT_SCTP, "test1", config);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000482
483 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000);
484
485 EXPECT_EQ(static_cast<unsigned int>(config.id),
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700486 provider_->last_send_data_params().ssrc);
487 EXPECT_EQ(cricket::DMT_CONTROL, provider_->last_send_data_params().type);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000488}
489
490// Tests the OPEN_ACK role assigned by InternalDataChannelInit.
491TEST_F(SctpDataChannelTest, OpenAckRoleInitialization) {
492 webrtc::InternalDataChannelInit init;
493 EXPECT_EQ(webrtc::InternalDataChannelInit::kOpener, init.open_handshake_role);
494 EXPECT_FALSE(init.negotiated);
495
496 webrtc::DataChannelInit base;
497 base.negotiated = true;
498 webrtc::InternalDataChannelInit init2(base);
499 EXPECT_EQ(webrtc::InternalDataChannelInit::kNone, init2.open_handshake_role);
500}
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000501
502// Tests that the DataChannel is closed if the sending buffer is full.
503TEST_F(SctpDataChannelTest, ClosedWhenSendBufferFull) {
504 SetChannelReady();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000505
jbaucheec21bd2016-03-20 06:15:43 -0700506 rtc::CopyOnWriteBuffer buffer(1024);
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000507 memset(buffer.data(), 0, buffer.size());
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000508
509 webrtc::DataBuffer packet(buffer, true);
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700510 provider_->set_send_blocked(true);
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000511
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000512 for (size_t i = 0; i < 16 * 1024 + 1; ++i) {
513 EXPECT_TRUE(webrtc_data_channel_->Send(packet));
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000514 }
515
Lally Singh5c6c6e02015-05-29 11:52:39 -0400516 EXPECT_TRUE(
517 webrtc::DataChannelInterface::kClosed == webrtc_data_channel_->state() ||
518 webrtc::DataChannelInterface::kClosing == webrtc_data_channel_->state());
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000519}
520
521// Tests that the DataChannel is closed on transport errors.
522TEST_F(SctpDataChannelTest, ClosedOnTransportError) {
523 SetChannelReady();
524 webrtc::DataBuffer buffer("abcd");
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700525 provider_->set_transport_error();
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000526
527 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
528
529 EXPECT_EQ(webrtc::DataChannelInterface::kClosed,
530 webrtc_data_channel_->state());
531}
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000532
533// Tests that a already closed DataChannel does not fire onStateChange again.
534TEST_F(SctpDataChannelTest, ClosedDataChannelDoesNotFireOnStateChange) {
535 AddObserver();
536 webrtc_data_channel_->Close();
537 // OnStateChange called for kClosing and kClosed.
538 EXPECT_EQ(2U, observer_->on_state_change_count());
539
540 observer_->ResetOnStateChangeCount();
541 webrtc_data_channel_->RemotePeerRequestClose();
542 EXPECT_EQ(0U, observer_->on_state_change_count());
543}
544
545// Tests that RemotePeerRequestClose closes the local DataChannel.
546TEST_F(SctpDataChannelTest, RemotePeerRequestClose) {
547 AddObserver();
548 webrtc_data_channel_->RemotePeerRequestClose();
549
550 // OnStateChange called for kClosing and kClosed.
551 EXPECT_EQ(2U, observer_->on_state_change_count());
552 EXPECT_EQ(webrtc::DataChannelInterface::kClosed,
553 webrtc_data_channel_->state());
554}
555
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000556// Tests that the DataChannel is closed if the received buffer is full.
557TEST_F(SctpDataChannelTest, ClosedWhenReceivedBufferFull) {
558 SetChannelReady();
jbaucheec21bd2016-03-20 06:15:43 -0700559 rtc::CopyOnWriteBuffer buffer(1024);
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000560 memset(buffer.data(), 0, buffer.size());
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000561
562 cricket::ReceiveDataParams params;
563 params.ssrc = 0;
564
565 // Receiving data without having an observer will overflow the buffer.
566 for (size_t i = 0; i < 16 * 1024 + 1; ++i) {
567 webrtc_data_channel_->OnDataReceived(NULL, params, buffer);
568 }
569 EXPECT_EQ(webrtc::DataChannelInterface::kClosed,
570 webrtc_data_channel_->state());
571}
jiayl@webrtc.org3edbaaf2014-07-18 23:57:50 +0000572
573// Tests that sending empty data returns no error and keeps the channel open.
574TEST_F(SctpDataChannelTest, SendEmptyData) {
575 webrtc_data_channel_->SetSctpSid(1);
576 SetChannelReady();
577 EXPECT_EQ(webrtc::DataChannelInterface::kOpen,
578 webrtc_data_channel_->state());
579
580 webrtc::DataBuffer buffer("");
581 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
582 EXPECT_EQ(webrtc::DataChannelInterface::kOpen,
583 webrtc_data_channel_->state());
584}
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +0000585
586// Tests that a channel can be closed without being opened or assigned an sid.
587TEST_F(SctpDataChannelTest, NeverOpened) {
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700588 provider_->set_transport_available(true);
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +0000589 webrtc_data_channel_->OnTransportChannelCreated();
590 webrtc_data_channel_->Close();
591}
deadbeefab9b2d12015-10-14 11:33:11 -0700592
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700593// Test that the data channel goes to the "closed" state (and doesn't crash)
594// when its transport goes away, even while data is buffered.
595TEST_F(SctpDataChannelTest, TransportDestroyedWhileDataBuffered) {
596 SetChannelReady();
597
598 rtc::CopyOnWriteBuffer buffer(1024);
599 memset(buffer.data(), 0, buffer.size());
600 webrtc::DataBuffer packet(buffer, true);
601
602 // Send a packet while sending is blocked so it ends up buffered.
603 provider_->set_send_blocked(true);
604 EXPECT_TRUE(webrtc_data_channel_->Send(packet));
605
606 // Tell the data channel that its tranpsort is being destroyed.
607 // It should then stop using the transport (allowing us to delete it) and
608 // transition to the "closed" state.
609 webrtc_data_channel_->OnTransportChannelDestroyed();
610 provider_.reset(nullptr);
611 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed,
612 webrtc_data_channel_->state(), kDefaultTimeout);
613}
614
deadbeefab9b2d12015-10-14 11:33:11 -0700615class SctpSidAllocatorTest : public testing::Test {
616 protected:
617 SctpSidAllocator allocator_;
618};
619
620// Verifies that an even SCTP id is allocated for SSL_CLIENT and an odd id for
621// SSL_SERVER.
622TEST_F(SctpSidAllocatorTest, SctpIdAllocationBasedOnRole) {
623 int id;
624 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &id));
625 EXPECT_EQ(1, id);
626 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &id));
627 EXPECT_EQ(0, id);
628 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &id));
629 EXPECT_EQ(3, id);
630 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &id));
631 EXPECT_EQ(2, id);
632}
633
634// Verifies that SCTP ids of existing DataChannels are not reused.
635TEST_F(SctpSidAllocatorTest, SctpIdAllocationNoReuse) {
636 int old_id = 1;
637 EXPECT_TRUE(allocator_.ReserveSid(old_id));
638
639 int new_id;
640 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &new_id));
641 EXPECT_NE(old_id, new_id);
642
643 old_id = 0;
644 EXPECT_TRUE(allocator_.ReserveSid(old_id));
645 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &new_id));
646 EXPECT_NE(old_id, new_id);
647}
648
649// Verifies that SCTP ids of removed DataChannels can be reused.
650TEST_F(SctpSidAllocatorTest, SctpIdReusedForRemovedDataChannel) {
651 int odd_id = 1;
652 int even_id = 0;
653 EXPECT_TRUE(allocator_.ReserveSid(odd_id));
654 EXPECT_TRUE(allocator_.ReserveSid(even_id));
655
656 int allocated_id = -1;
657 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id));
658 EXPECT_EQ(odd_id + 2, allocated_id);
659
660 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id));
661 EXPECT_EQ(even_id + 2, allocated_id);
662
663 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id));
664 EXPECT_EQ(odd_id + 4, allocated_id);
665
666 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id));
667 EXPECT_EQ(even_id + 4, allocated_id);
668
669 allocator_.ReleaseSid(odd_id);
670 allocator_.ReleaseSid(even_id);
671
672 // Verifies that removed ids are reused.
673 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id));
674 EXPECT_EQ(odd_id, allocated_id);
675
676 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id));
677 EXPECT_EQ(even_id, allocated_id);
678
679 // Verifies that used higher ids are not reused.
680 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id));
681 EXPECT_EQ(odd_id + 6, allocated_id);
682
683 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id));
684 EXPECT_EQ(even_id + 6, allocated_id);
685}