blob: eb5deb93cec85548aced8b1c4f8160613db3cd1c [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>
Steve Anton36b29d12017-10-30 09:57:42 -070012#include <vector>
kwibergd1fe2812016-04-27 06:47:29 -070013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "pc/datachannel.h"
15#include "pc/sctputils.h"
16#include "pc/test/fakedatachannelprovider.h"
17#include "rtc_base/gunit.h"
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +020018#include "rtc_base/numerics/safe_conversions.h"
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000019
wu@webrtc.org78187522013-10-07 23:32:02 +000020using webrtc::DataChannel;
deadbeefab9b2d12015-10-14 11:33:11 -070021using webrtc::SctpSidAllocator;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000022
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -070023static constexpr int kDefaultTimeout = 10000;
24
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +000025class FakeDataChannelObserver : public webrtc::DataChannelObserver {
26 public:
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +000027 FakeDataChannelObserver()
bemasc0edd50c2015-07-01 13:34:33 -070028 : messages_received_(0),
29 on_state_change_count_(0),
30 on_buffered_amount_change_count_(0) {}
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +000031
Yves Gerey665174f2018-06-19 15:03:05 +020032 void OnStateChange() { ++on_state_change_count_; }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +000033
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
Yves Gerey665174f2018-06-19 15:03:05 +020038 void OnMessage(const webrtc::DataBuffer& buffer) { ++messages_received_; }
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +000039
Yves Gerey665174f2018-06-19 15:03:05 +020040 size_t messages_received() const { return messages_received_; }
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +000041
Yves Gerey665174f2018-06-19 15:03:05 +020042 void ResetOnStateChangeCount() { on_state_change_count_ = 0; }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +000043
bemasc0edd50c2015-07-01 13:34:33 -070044 void ResetOnBufferedAmountChangeCount() {
45 on_buffered_amount_change_count_ = 0;
46 }
47
Yves Gerey665174f2018-06-19 15:03:05 +020048 size_t on_state_change_count() const { return on_state_change_count_; }
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +000049
bemasc0edd50c2015-07-01 13:34:33 -070050 size_t on_buffered_amount_change_count() const {
51 return on_buffered_amount_change_count_;
52 }
53
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +000054 private:
55 size_t messages_received_;
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +000056 size_t on_state_change_count_;
bemasc0edd50c2015-07-01 13:34:33 -070057 size_t on_buffered_amount_change_count_;
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +000058};
59
Taylor Brandstettercdd05f02018-05-31 13:23:32 -070060// TODO(deadbeef): The fact that these tests use a fake provider makes them not
61// too valuable. Should rewrite using the
62// peerconnection_datachannel_unittest.cc infrastructure.
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000063class SctpDataChannelTest : public testing::Test {
64 protected:
65 SctpDataChannelTest()
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -070066 : provider_(new FakeDataChannelProvider()),
67 webrtc_data_channel_(DataChannel::Create(provider_.get(),
68 cricket::DCT_SCTP,
69 "test",
70 init_)) {}
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000071
wu@webrtc.org78187522013-10-07 23:32:02 +000072 void SetChannelReady() {
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -070073 provider_->set_transport_available(true);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000074 webrtc_data_channel_->OnTransportChannelCreated();
75 if (webrtc_data_channel_->id() < 0) {
76 webrtc_data_channel_->SetSctpSid(0);
77 }
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -070078 provider_->set_ready_to_send(true);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000079 }
wu@webrtc.org78187522013-10-07 23:32:02 +000080
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +000081 void AddObserver() {
82 observer_.reset(new FakeDataChannelObserver());
83 webrtc_data_channel_->RegisterObserver(observer_.get());
84 }
85
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000086 webrtc::InternalDataChannelInit init_;
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -070087 std::unique_ptr<FakeDataChannelProvider> provider_;
kwibergd1fe2812016-04-27 06:47:29 -070088 std::unique_ptr<FakeDataChannelObserver> observer_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000089 rtc::scoped_refptr<DataChannel> webrtc_data_channel_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000090};
91
hbos82ebe022016-11-14 01:41:09 -080092class StateSignalsListener : public sigslot::has_slots<> {
93 public:
94 int opened_count() const { return opened_count_; }
95 int closed_count() const { return closed_count_; }
96
Yves Gerey665174f2018-06-19 15:03:05 +020097 void OnSignalOpened(DataChannel* data_channel) { ++opened_count_; }
hbos82ebe022016-11-14 01:41:09 -080098
Yves Gerey665174f2018-06-19 15:03:05 +020099 void OnSignalClosed(DataChannel* data_channel) { ++closed_count_; }
hbos82ebe022016-11-14 01:41:09 -0800100
101 private:
102 int opened_count_ = 0;
103 int closed_count_ = 0;
104};
105
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000106// Verifies that the data channel is connected to the transport after creation.
107TEST_F(SctpDataChannelTest, ConnectedToTransportOnCreated) {
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700108 provider_->set_transport_available(true);
109 rtc::scoped_refptr<DataChannel> dc =
110 DataChannel::Create(provider_.get(), cricket::DCT_SCTP, "test1", init_);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000111
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700112 EXPECT_TRUE(provider_->IsConnected(dc.get()));
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000113 // The sid is not set yet, so it should not have added the streams.
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700114 EXPECT_FALSE(provider_->IsSendStreamAdded(dc->id()));
115 EXPECT_FALSE(provider_->IsRecvStreamAdded(dc->id()));
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000116
117 dc->SetSctpSid(0);
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700118 EXPECT_TRUE(provider_->IsSendStreamAdded(dc->id()));
119 EXPECT_TRUE(provider_->IsRecvStreamAdded(dc->id()));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000120}
121
122// Verifies that the data channel is connected to the transport if the transport
123// is not available initially and becomes available later.
124TEST_F(SctpDataChannelTest, ConnectedAfterTransportBecomesAvailable) {
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700125 EXPECT_FALSE(provider_->IsConnected(webrtc_data_channel_.get()));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000126
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700127 provider_->set_transport_available(true);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000128 webrtc_data_channel_->OnTransportChannelCreated();
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700129 EXPECT_TRUE(provider_->IsConnected(webrtc_data_channel_.get()));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000130}
131
wu@webrtc.org78187522013-10-07 23:32:02 +0000132// Tests the state of the data channel.
133TEST_F(SctpDataChannelTest, StateTransition) {
hbos82ebe022016-11-14 01:41:09 -0800134 StateSignalsListener state_signals_listener;
135 webrtc_data_channel_->SignalOpened.connect(
136 &state_signals_listener, &StateSignalsListener::OnSignalOpened);
137 webrtc_data_channel_->SignalClosed.connect(
138 &state_signals_listener, &StateSignalsListener::OnSignalClosed);
wu@webrtc.org78187522013-10-07 23:32:02 +0000139 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting,
140 webrtc_data_channel_->state());
hbos82ebe022016-11-14 01:41:09 -0800141 EXPECT_EQ(state_signals_listener.opened_count(), 0);
142 EXPECT_EQ(state_signals_listener.closed_count(), 0);
wu@webrtc.org78187522013-10-07 23:32:02 +0000143 SetChannelReady();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000144
wu@webrtc.org78187522013-10-07 23:32:02 +0000145 EXPECT_EQ(webrtc::DataChannelInterface::kOpen, webrtc_data_channel_->state());
hbos82ebe022016-11-14 01:41:09 -0800146 EXPECT_EQ(state_signals_listener.opened_count(), 1);
147 EXPECT_EQ(state_signals_listener.closed_count(), 0);
wu@webrtc.org78187522013-10-07 23:32:02 +0000148 webrtc_data_channel_->Close();
149 EXPECT_EQ(webrtc::DataChannelInterface::kClosed,
150 webrtc_data_channel_->state());
hbos82ebe022016-11-14 01:41:09 -0800151 EXPECT_EQ(state_signals_listener.opened_count(), 1);
152 EXPECT_EQ(state_signals_listener.closed_count(), 1);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000153 // Verifies that it's disconnected from the transport.
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700154 EXPECT_FALSE(provider_->IsConnected(webrtc_data_channel_.get()));
wu@webrtc.org78187522013-10-07 23:32:02 +0000155}
156
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000157// Tests that DataChannel::buffered_amount() is correct after the channel is
158// blocked.
159TEST_F(SctpDataChannelTest, BufferedAmountWhenBlocked) {
bemasc0edd50c2015-07-01 13:34:33 -0700160 AddObserver();
wu@webrtc.org78187522013-10-07 23:32:02 +0000161 SetChannelReady();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000162 webrtc::DataBuffer buffer("abcd");
163 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
164
165 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount());
bemasc0edd50c2015-07-01 13:34:33 -0700166 EXPECT_EQ(0U, observer_->on_buffered_amount_change_count());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000167
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700168 provider_->set_send_blocked(true);
wu@webrtc.org78187522013-10-07 23:32:02 +0000169
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000170 const int number_of_packets = 3;
171 for (int i = 0; i < number_of_packets; ++i) {
172 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
173 }
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000174 EXPECT_EQ(buffer.data.size() * number_of_packets,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000175 webrtc_data_channel_->buffered_amount());
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +0200176 EXPECT_EQ(rtc::checked_cast<size_t>(number_of_packets),
177 observer_->on_buffered_amount_change_count());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000178}
179
180// Tests that the queued data are sent when the channel transitions from blocked
181// to unblocked.
182TEST_F(SctpDataChannelTest, QueuedDataSentWhenUnblocked) {
bemasc0edd50c2015-07-01 13:34:33 -0700183 AddObserver();
wu@webrtc.org78187522013-10-07 23:32:02 +0000184 SetChannelReady();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000185 webrtc::DataBuffer buffer("abcd");
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700186 provider_->set_send_blocked(true);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000187 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
188
bemasc0edd50c2015-07-01 13:34:33 -0700189 EXPECT_EQ(1U, observer_->on_buffered_amount_change_count());
190
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700191 provider_->set_send_blocked(false);
wu@webrtc.org78187522013-10-07 23:32:02 +0000192 SetChannelReady();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000193 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount());
bemasc0edd50c2015-07-01 13:34:33 -0700194 EXPECT_EQ(2U, observer_->on_buffered_amount_change_count());
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000195}
wu@webrtc.org78187522013-10-07 23:32:02 +0000196
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000197// Tests that no crash when the channel is blocked right away while trying to
198// send queued data.
199TEST_F(SctpDataChannelTest, BlockedWhenSendQueuedDataNoCrash) {
bemasc0edd50c2015-07-01 13:34:33 -0700200 AddObserver();
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000201 SetChannelReady();
202 webrtc::DataBuffer buffer("abcd");
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700203 provider_->set_send_blocked(true);
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000204 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
bemasc0edd50c2015-07-01 13:34:33 -0700205 EXPECT_EQ(1U, observer_->on_buffered_amount_change_count());
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000206
207 // Set channel ready while it is still blocked.
208 SetChannelReady();
209 EXPECT_EQ(buffer.size(), webrtc_data_channel_->buffered_amount());
bemasc0edd50c2015-07-01 13:34:33 -0700210 EXPECT_EQ(1U, observer_->on_buffered_amount_change_count());
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000211
212 // Unblock the channel to send queued data again, there should be no crash.
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700213 provider_->set_send_blocked(false);
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000214 SetChannelReady();
215 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount());
bemasc0edd50c2015-07-01 13:34:33 -0700216 EXPECT_EQ(2U, observer_->on_buffered_amount_change_count());
jiayl@webrtc.orgcceb1662015-01-22 00:55:10 +0000217}
218
hbos84ffdee2016-10-12 14:14:39 -0700219// Tests that DataChannel::messages_sent() and DataChannel::bytes_sent() are
220// correct, sending data both while unblocked and while blocked.
221TEST_F(SctpDataChannelTest, VerifyMessagesAndBytesSent) {
222 AddObserver();
223 SetChannelReady();
224 std::vector<webrtc::DataBuffer> buffers({
Yves Gerey665174f2018-06-19 15:03:05 +0200225 webrtc::DataBuffer("message 1"), webrtc::DataBuffer("msg 2"),
226 webrtc::DataBuffer("message three"), webrtc::DataBuffer("quadra message"),
227 webrtc::DataBuffer("fifthmsg"),
228 webrtc::DataBuffer("message of the beast"),
hbos84ffdee2016-10-12 14:14:39 -0700229 });
230
231 // Default values.
232 EXPECT_EQ(0U, webrtc_data_channel_->messages_sent());
233 EXPECT_EQ(0U, webrtc_data_channel_->bytes_sent());
234
235 // Send three buffers while not blocked.
236 provider_->set_send_blocked(false);
237 EXPECT_TRUE(webrtc_data_channel_->Send(buffers[0]));
238 EXPECT_TRUE(webrtc_data_channel_->Send(buffers[1]));
239 EXPECT_TRUE(webrtc_data_channel_->Send(buffers[2]));
240 size_t bytes_sent = buffers[0].size() + buffers[1].size() + buffers[2].size();
241 EXPECT_EQ_WAIT(0U, webrtc_data_channel_->buffered_amount(), kDefaultTimeout);
242 EXPECT_EQ(3U, webrtc_data_channel_->messages_sent());
243 EXPECT_EQ(bytes_sent, webrtc_data_channel_->bytes_sent());
244
245 // Send three buffers while blocked, queuing the buffers.
246 provider_->set_send_blocked(true);
247 EXPECT_TRUE(webrtc_data_channel_->Send(buffers[3]));
248 EXPECT_TRUE(webrtc_data_channel_->Send(buffers[4]));
249 EXPECT_TRUE(webrtc_data_channel_->Send(buffers[5]));
250 size_t bytes_queued =
251 buffers[3].size() + buffers[4].size() + buffers[5].size();
252 EXPECT_EQ(bytes_queued, webrtc_data_channel_->buffered_amount());
253 EXPECT_EQ(3U, webrtc_data_channel_->messages_sent());
254 EXPECT_EQ(bytes_sent, webrtc_data_channel_->bytes_sent());
255
256 // Unblock and make sure everything was sent.
257 provider_->set_send_blocked(false);
258 EXPECT_EQ_WAIT(0U, webrtc_data_channel_->buffered_amount(), kDefaultTimeout);
259 bytes_sent += bytes_queued;
260 EXPECT_EQ(6U, webrtc_data_channel_->messages_sent());
261 EXPECT_EQ(bytes_sent, webrtc_data_channel_->bytes_sent());
262}
263
wu@webrtc.org78187522013-10-07 23:32:02 +0000264// Tests that the queued control message is sent when channel is ready.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000265TEST_F(SctpDataChannelTest, OpenMessageSent) {
266 // Initially the id is unassigned.
267 EXPECT_EQ(-1, webrtc_data_channel_->id());
268
wu@webrtc.org78187522013-10-07 23:32:02 +0000269 SetChannelReady();
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000270 EXPECT_GE(webrtc_data_channel_->id(), 0);
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700271 EXPECT_EQ(cricket::DMT_CONTROL, provider_->last_send_data_params().type);
272 EXPECT_EQ(provider_->last_send_data_params().ssrc,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200273 static_cast<uint32_t>(webrtc_data_channel_->id()));
wu@webrtc.org78187522013-10-07 23:32:02 +0000274}
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000275
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000276TEST_F(SctpDataChannelTest, QueuedOpenMessageSent) {
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700277 provider_->set_send_blocked(true);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000278 SetChannelReady();
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700279 provider_->set_send_blocked(false);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000280
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700281 EXPECT_EQ(cricket::DMT_CONTROL, provider_->last_send_data_params().type);
282 EXPECT_EQ(provider_->last_send_data_params().ssrc,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200283 static_cast<uint32_t>(webrtc_data_channel_->id()));
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000284}
285
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000286// Tests that the DataChannel created after transport gets ready can enter OPEN
287// state.
288TEST_F(SctpDataChannelTest, LateCreatedChannelTransitionToOpen) {
289 SetChannelReady();
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000290 webrtc::InternalDataChannelInit init;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000291 init.id = 1;
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700292 rtc::scoped_refptr<DataChannel> dc =
293 DataChannel::Create(provider_.get(), cricket::DCT_SCTP, "test1", init);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000294 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, dc->state());
Yves Gerey665174f2018-06-19 15:03:05 +0200295 EXPECT_TRUE_WAIT(webrtc::DataChannelInterface::kOpen == dc->state(), 1000);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000296}
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000297
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000298// Tests that an unordered DataChannel sends data as ordered until the OPEN_ACK
299// message is received.
300TEST_F(SctpDataChannelTest, SendUnorderedAfterReceivesOpenAck) {
301 SetChannelReady();
302 webrtc::InternalDataChannelInit init;
303 init.id = 1;
304 init.ordered = false;
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700305 rtc::scoped_refptr<DataChannel> dc =
306 DataChannel::Create(provider_.get(), cricket::DCT_SCTP, "test1", init);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000307
308 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000);
309
310 // Sends a message and verifies it's ordered.
311 webrtc::DataBuffer buffer("some data");
312 ASSERT_TRUE(dc->Send(buffer));
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700313 EXPECT_TRUE(provider_->last_send_data_params().ordered);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000314
315 // Emulates receiving an OPEN_ACK message.
316 cricket::ReceiveDataParams params;
317 params.ssrc = init.id;
318 params.type = cricket::DMT_CONTROL;
jbaucheec21bd2016-03-20 06:15:43 -0700319 rtc::CopyOnWriteBuffer payload;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000320 webrtc::WriteDataChannelOpenAckMessage(&payload);
deadbeef953c2ce2017-01-09 14:53:41 -0800321 dc->OnDataReceived(params, payload);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000322
323 // Sends another message and verifies it's unordered.
324 ASSERT_TRUE(dc->Send(buffer));
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700325 EXPECT_FALSE(provider_->last_send_data_params().ordered);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000326}
327
328// Tests that an unordered DataChannel sends unordered data after any DATA
329// message is received.
330TEST_F(SctpDataChannelTest, SendUnorderedAfterReceiveData) {
331 SetChannelReady();
332 webrtc::InternalDataChannelInit init;
333 init.id = 1;
334 init.ordered = false;
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700335 rtc::scoped_refptr<DataChannel> dc =
336 DataChannel::Create(provider_.get(), cricket::DCT_SCTP, "test1", init);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000337
338 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000);
339
340 // Emulates receiving a DATA message.
341 cricket::ReceiveDataParams params;
342 params.ssrc = init.id;
343 params.type = cricket::DMT_TEXT;
344 webrtc::DataBuffer buffer("data");
deadbeef953c2ce2017-01-09 14:53:41 -0800345 dc->OnDataReceived(params, buffer.data);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000346
347 // Sends a message and verifies it's unordered.
348 ASSERT_TRUE(dc->Send(buffer));
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700349 EXPECT_FALSE(provider_->last_send_data_params().ordered);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000350}
351
Lally Singh5c6c6e02015-05-29 11:52:39 -0400352// Tests that the channel can't open until it's successfully sent the OPEN
353// message.
354TEST_F(SctpDataChannelTest, OpenWaitsForOpenMesssage) {
355 webrtc::DataBuffer buffer("foo");
356
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700357 provider_->set_send_blocked(true);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400358 SetChannelReady();
359 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting,
360 webrtc_data_channel_->state());
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700361 provider_->set_send_blocked(false);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400362 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen,
363 webrtc_data_channel_->state(), 1000);
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700364 EXPECT_EQ(cricket::DMT_CONTROL, provider_->last_send_data_params().type);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400365}
366
367// Tests that close first makes sure all queued data gets sent.
368TEST_F(SctpDataChannelTest, QueuedCloseFlushes) {
369 webrtc::DataBuffer buffer("foo");
370
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700371 provider_->set_send_blocked(true);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400372 SetChannelReady();
373 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting,
374 webrtc_data_channel_->state());
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700375 provider_->set_send_blocked(false);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400376 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen,
377 webrtc_data_channel_->state(), 1000);
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700378 provider_->set_send_blocked(true);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400379 webrtc_data_channel_->Send(buffer);
380 webrtc_data_channel_->Close();
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700381 provider_->set_send_blocked(false);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400382 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed,
383 webrtc_data_channel_->state(), 1000);
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700384 EXPECT_EQ(cricket::DMT_TEXT, provider_->last_send_data_params().type);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400385}
386
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000387// Tests that messages are sent with the right ssrc.
388TEST_F(SctpDataChannelTest, SendDataSsrc) {
389 webrtc_data_channel_->SetSctpSid(1);
390 SetChannelReady();
391 webrtc::DataBuffer buffer("data");
392 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700393 EXPECT_EQ(1U, provider_->last_send_data_params().ssrc);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000394}
395
396// Tests that the incoming messages with wrong ssrcs are rejected.
397TEST_F(SctpDataChannelTest, ReceiveDataWithInvalidSsrc) {
398 webrtc_data_channel_->SetSctpSid(1);
399 SetChannelReady();
400
401 AddObserver();
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000402
403 cricket::ReceiveDataParams params;
404 params.ssrc = 0;
405 webrtc::DataBuffer buffer("abcd");
deadbeef953c2ce2017-01-09 14:53:41 -0800406 webrtc_data_channel_->OnDataReceived(params, buffer.data);
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000407
408 EXPECT_EQ(0U, observer_->messages_received());
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000409}
410
411// Tests that the incoming messages with right ssrcs are acceted.
412TEST_F(SctpDataChannelTest, ReceiveDataWithValidSsrc) {
413 webrtc_data_channel_->SetSctpSid(1);
414 SetChannelReady();
415
416 AddObserver();
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000417
418 cricket::ReceiveDataParams params;
419 params.ssrc = 1;
420 webrtc::DataBuffer buffer("abcd");
421
deadbeef953c2ce2017-01-09 14:53:41 -0800422 webrtc_data_channel_->OnDataReceived(params, buffer.data);
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000423 EXPECT_EQ(1U, observer_->messages_received());
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000424}
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000425
426// Tests that no CONTROL message is sent if the datachannel is negotiated and
427// not created from an OPEN message.
428TEST_F(SctpDataChannelTest, NoMsgSentIfNegotiatedAndNotFromOpenMsg) {
429 webrtc::InternalDataChannelInit config;
430 config.id = 1;
431 config.negotiated = true;
432 config.open_handshake_role = webrtc::InternalDataChannelInit::kNone;
433
434 SetChannelReady();
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700435 rtc::scoped_refptr<DataChannel> dc =
436 DataChannel::Create(provider_.get(), cricket::DCT_SCTP, "test1", config);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000437
438 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000);
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700439 EXPECT_EQ(0U, provider_->last_send_data_params().ssrc);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000440}
441
hbos84ffdee2016-10-12 14:14:39 -0700442// Tests that DataChannel::messages_received() and DataChannel::bytes_received()
443// are correct, receiving data both while not open and while open.
444TEST_F(SctpDataChannelTest, VerifyMessagesAndBytesReceived) {
445 AddObserver();
446 std::vector<webrtc::DataBuffer> buffers({
Yves Gerey665174f2018-06-19 15:03:05 +0200447 webrtc::DataBuffer("message 1"), webrtc::DataBuffer("msg 2"),
448 webrtc::DataBuffer("message three"), webrtc::DataBuffer("quadra message"),
449 webrtc::DataBuffer("fifthmsg"),
450 webrtc::DataBuffer("message of the beast"),
hbos84ffdee2016-10-12 14:14:39 -0700451 });
452
453 webrtc_data_channel_->SetSctpSid(1);
454 cricket::ReceiveDataParams params;
455 params.ssrc = 1;
456
457 // Default values.
458 EXPECT_EQ(0U, webrtc_data_channel_->messages_received());
459 EXPECT_EQ(0U, webrtc_data_channel_->bytes_received());
460
461 // Receive three buffers while data channel isn't open.
deadbeef953c2ce2017-01-09 14:53:41 -0800462 webrtc_data_channel_->OnDataReceived(params, buffers[0].data);
463 webrtc_data_channel_->OnDataReceived(params, buffers[1].data);
464 webrtc_data_channel_->OnDataReceived(params, buffers[2].data);
hbos84ffdee2016-10-12 14:14:39 -0700465 EXPECT_EQ(0U, observer_->messages_received());
466 EXPECT_EQ(0U, webrtc_data_channel_->messages_received());
467 EXPECT_EQ(0U, webrtc_data_channel_->bytes_received());
468
469 // Open channel and make sure everything was received.
470 SetChannelReady();
471 size_t bytes_received =
472 buffers[0].size() + buffers[1].size() + buffers[2].size();
473 EXPECT_EQ(3U, observer_->messages_received());
474 EXPECT_EQ(3U, webrtc_data_channel_->messages_received());
475 EXPECT_EQ(bytes_received, webrtc_data_channel_->bytes_received());
476
477 // Receive three buffers while open.
deadbeef953c2ce2017-01-09 14:53:41 -0800478 webrtc_data_channel_->OnDataReceived(params, buffers[3].data);
479 webrtc_data_channel_->OnDataReceived(params, buffers[4].data);
480 webrtc_data_channel_->OnDataReceived(params, buffers[5].data);
hbos84ffdee2016-10-12 14:14:39 -0700481 bytes_received += buffers[3].size() + buffers[4].size() + buffers[5].size();
482 EXPECT_EQ(6U, observer_->messages_received());
483 EXPECT_EQ(6U, webrtc_data_channel_->messages_received());
484 EXPECT_EQ(bytes_received, webrtc_data_channel_->bytes_received());
485}
486
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000487// Tests that OPEN_ACK message is sent if the datachannel is created from an
488// OPEN message.
489TEST_F(SctpDataChannelTest, OpenAckSentIfCreatedFromOpenMessage) {
490 webrtc::InternalDataChannelInit config;
491 config.id = 1;
492 config.negotiated = true;
493 config.open_handshake_role = webrtc::InternalDataChannelInit::kAcker;
494
495 SetChannelReady();
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700496 rtc::scoped_refptr<DataChannel> dc =
497 DataChannel::Create(provider_.get(), cricket::DCT_SCTP, "test1", config);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000498
499 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000);
500
501 EXPECT_EQ(static_cast<unsigned int>(config.id),
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700502 provider_->last_send_data_params().ssrc);
503 EXPECT_EQ(cricket::DMT_CONTROL, provider_->last_send_data_params().type);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000504}
505
506// Tests the OPEN_ACK role assigned by InternalDataChannelInit.
507TEST_F(SctpDataChannelTest, OpenAckRoleInitialization) {
508 webrtc::InternalDataChannelInit init;
509 EXPECT_EQ(webrtc::InternalDataChannelInit::kOpener, init.open_handshake_role);
510 EXPECT_FALSE(init.negotiated);
511
512 webrtc::DataChannelInit base;
513 base.negotiated = true;
514 webrtc::InternalDataChannelInit init2(base);
515 EXPECT_EQ(webrtc::InternalDataChannelInit::kNone, init2.open_handshake_role);
516}
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000517
518// Tests that the DataChannel is closed if the sending buffer is full.
519TEST_F(SctpDataChannelTest, ClosedWhenSendBufferFull) {
520 SetChannelReady();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000521
jbaucheec21bd2016-03-20 06:15:43 -0700522 rtc::CopyOnWriteBuffer buffer(1024);
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000523 memset(buffer.data(), 0, buffer.size());
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000524
525 webrtc::DataBuffer packet(buffer, true);
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700526 provider_->set_send_blocked(true);
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000527
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000528 for (size_t i = 0; i < 16 * 1024 + 1; ++i) {
529 EXPECT_TRUE(webrtc_data_channel_->Send(packet));
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000530 }
531
Lally Singh5c6c6e02015-05-29 11:52:39 -0400532 EXPECT_TRUE(
533 webrtc::DataChannelInterface::kClosed == webrtc_data_channel_->state() ||
534 webrtc::DataChannelInterface::kClosing == webrtc_data_channel_->state());
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000535}
536
537// Tests that the DataChannel is closed on transport errors.
538TEST_F(SctpDataChannelTest, ClosedOnTransportError) {
539 SetChannelReady();
540 webrtc::DataBuffer buffer("abcd");
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700541 provider_->set_transport_error();
jiayl@webrtc.org5dc51fb2014-05-29 15:33:54 +0000542
543 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
544
545 EXPECT_EQ(webrtc::DataChannelInterface::kClosed,
546 webrtc_data_channel_->state());
547}
jiayl@webrtc.org9f8164c2014-05-30 21:53:17 +0000548
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000549// Tests that the DataChannel is closed if the received buffer is full.
550TEST_F(SctpDataChannelTest, ClosedWhenReceivedBufferFull) {
551 SetChannelReady();
jbaucheec21bd2016-03-20 06:15:43 -0700552 rtc::CopyOnWriteBuffer buffer(1024);
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000553 memset(buffer.data(), 0, buffer.size());
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000554
555 cricket::ReceiveDataParams params;
556 params.ssrc = 0;
557
558 // Receiving data without having an observer will overflow the buffer.
559 for (size_t i = 0; i < 16 * 1024 + 1; ++i) {
deadbeef953c2ce2017-01-09 14:53:41 -0800560 webrtc_data_channel_->OnDataReceived(params, buffer);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000561 }
562 EXPECT_EQ(webrtc::DataChannelInterface::kClosed,
563 webrtc_data_channel_->state());
564}
jiayl@webrtc.org3edbaaf2014-07-18 23:57:50 +0000565
566// Tests that sending empty data returns no error and keeps the channel open.
567TEST_F(SctpDataChannelTest, SendEmptyData) {
568 webrtc_data_channel_->SetSctpSid(1);
569 SetChannelReady();
Yves Gerey665174f2018-06-19 15:03:05 +0200570 EXPECT_EQ(webrtc::DataChannelInterface::kOpen, webrtc_data_channel_->state());
jiayl@webrtc.org3edbaaf2014-07-18 23:57:50 +0000571
572 webrtc::DataBuffer buffer("");
573 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
Yves Gerey665174f2018-06-19 15:03:05 +0200574 EXPECT_EQ(webrtc::DataChannelInterface::kOpen, webrtc_data_channel_->state());
jiayl@webrtc.org3edbaaf2014-07-18 23:57:50 +0000575}
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +0000576
577// Tests that a channel can be closed without being opened or assigned an sid.
578TEST_F(SctpDataChannelTest, NeverOpened) {
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700579 provider_->set_transport_available(true);
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +0000580 webrtc_data_channel_->OnTransportChannelCreated();
581 webrtc_data_channel_->Close();
582}
deadbeefab9b2d12015-10-14 11:33:11 -0700583
Taylor Brandstetter4cb5b642016-08-12 10:10:31 -0700584// Test that the data channel goes to the "closed" state (and doesn't crash)
585// when its transport goes away, even while data is buffered.
586TEST_F(SctpDataChannelTest, TransportDestroyedWhileDataBuffered) {
587 SetChannelReady();
588
589 rtc::CopyOnWriteBuffer buffer(1024);
590 memset(buffer.data(), 0, buffer.size());
591 webrtc::DataBuffer packet(buffer, true);
592
593 // Send a packet while sending is blocked so it ends up buffered.
594 provider_->set_send_blocked(true);
595 EXPECT_TRUE(webrtc_data_channel_->Send(packet));
596
597 // Tell the data channel that its tranpsort is being destroyed.
598 // It should then stop using the transport (allowing us to delete it) and
599 // transition to the "closed" state.
600 webrtc_data_channel_->OnTransportChannelDestroyed();
601 provider_.reset(nullptr);
602 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed,
603 webrtc_data_channel_->state(), kDefaultTimeout);
604}
605
deadbeefab9b2d12015-10-14 11:33:11 -0700606class SctpSidAllocatorTest : public testing::Test {
607 protected:
608 SctpSidAllocator allocator_;
609};
610
611// Verifies that an even SCTP id is allocated for SSL_CLIENT and an odd id for
612// SSL_SERVER.
613TEST_F(SctpSidAllocatorTest, SctpIdAllocationBasedOnRole) {
614 int id;
615 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &id));
616 EXPECT_EQ(1, id);
617 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &id));
618 EXPECT_EQ(0, id);
619 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &id));
620 EXPECT_EQ(3, id);
621 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &id));
622 EXPECT_EQ(2, id);
623}
624
625// Verifies that SCTP ids of existing DataChannels are not reused.
626TEST_F(SctpSidAllocatorTest, SctpIdAllocationNoReuse) {
627 int old_id = 1;
628 EXPECT_TRUE(allocator_.ReserveSid(old_id));
629
630 int new_id;
631 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &new_id));
632 EXPECT_NE(old_id, new_id);
633
634 old_id = 0;
635 EXPECT_TRUE(allocator_.ReserveSid(old_id));
636 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &new_id));
637 EXPECT_NE(old_id, new_id);
638}
639
640// Verifies that SCTP ids of removed DataChannels can be reused.
641TEST_F(SctpSidAllocatorTest, SctpIdReusedForRemovedDataChannel) {
642 int odd_id = 1;
643 int even_id = 0;
644 EXPECT_TRUE(allocator_.ReserveSid(odd_id));
645 EXPECT_TRUE(allocator_.ReserveSid(even_id));
646
647 int allocated_id = -1;
648 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id));
649 EXPECT_EQ(odd_id + 2, allocated_id);
650
651 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id));
652 EXPECT_EQ(even_id + 2, allocated_id);
653
654 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id));
655 EXPECT_EQ(odd_id + 4, allocated_id);
656
657 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id));
658 EXPECT_EQ(even_id + 4, allocated_id);
659
660 allocator_.ReleaseSid(odd_id);
661 allocator_.ReleaseSid(even_id);
662
663 // Verifies that removed ids are reused.
664 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id));
665 EXPECT_EQ(odd_id, allocated_id);
666
667 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id));
668 EXPECT_EQ(even_id, allocated_id);
669
670 // Verifies that used higher ids are not reused.
671 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id));
672 EXPECT_EQ(odd_id + 6, allocated_id);
673
674 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id));
675 EXPECT_EQ(even_id + 6, allocated_id);
676}