wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * 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.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 +0000 | [diff] [blame] | 11 | #include <stdint.h> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 12 | #include <string.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 13 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 14 | #include <memory> |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 +0000 | [diff] [blame] | 15 | #include <string> |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 16 | #include <vector> |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 17 | |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 +0000 | [diff] [blame] | 18 | #include "api/data_channel_interface.h" |
| 19 | #include "api/rtc_error.h" |
| 20 | #include "api/scoped_refptr.h" |
| 21 | #include "api/transport/data_channel_transport_interface.h" |
| 22 | #include "media/base/media_channel.h" |
Florent Castelli | dcb9ffc | 2021-06-29 14:58:23 +0200 | [diff] [blame] | 23 | #include "media/sctp/sctp_transport_internal.h" |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 24 | #include "pc/sctp_data_channel.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 25 | #include "pc/sctp_utils.h" |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 26 | #include "pc/test/fake_data_channel_controller.h" |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 +0000 | [diff] [blame] | 27 | #include "rtc_base/copy_on_write_buffer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 28 | #include "rtc_base/gunit.h" |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 +0000 | [diff] [blame] | 29 | #include "rtc_base/ssl_stream_adapter.h" |
| 30 | #include "rtc_base/third_party/sigslot/sigslot.h" |
| 31 | #include "rtc_base/thread.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 32 | #include "test/gtest.h" |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 33 | |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 34 | using webrtc::DataChannelInterface; |
| 35 | using webrtc::SctpDataChannel; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 36 | using webrtc::SctpSidAllocator; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 37 | |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 38 | static constexpr int kDefaultTimeout = 10000; |
| 39 | |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 40 | class FakeDataChannelObserver : public webrtc::DataChannelObserver { |
| 41 | public: |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 42 | FakeDataChannelObserver() |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 43 | : messages_received_(0), |
| 44 | on_state_change_count_(0), |
| 45 | on_buffered_amount_change_count_(0) {} |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 46 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 47 | void OnStateChange() { ++on_state_change_count_; } |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 48 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 49 | void OnBufferedAmountChange(uint64_t previous_amount) { |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 50 | ++on_buffered_amount_change_count_; |
| 51 | } |
| 52 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 53 | void OnMessage(const webrtc::DataBuffer& buffer) { ++messages_received_; } |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 54 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 55 | size_t messages_received() const { return messages_received_; } |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 56 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 57 | void ResetOnStateChangeCount() { on_state_change_count_ = 0; } |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 58 | |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 59 | void ResetOnBufferedAmountChangeCount() { |
| 60 | on_buffered_amount_change_count_ = 0; |
| 61 | } |
| 62 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 63 | size_t on_state_change_count() const { return on_state_change_count_; } |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 64 | |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 65 | size_t on_buffered_amount_change_count() const { |
| 66 | return on_buffered_amount_change_count_; |
| 67 | } |
| 68 | |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 69 | private: |
| 70 | size_t messages_received_; |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 71 | size_t on_state_change_count_; |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 72 | size_t on_buffered_amount_change_count_; |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 75 | // TODO(deadbeef): The fact that these tests use a fake controller makes them |
| 76 | // not too valuable. Should rewrite using the |
Taylor Brandstetter | cdd05f0 | 2018-05-31 13:23:32 -0700 | [diff] [blame] | 77 | // peerconnection_datachannel_unittest.cc infrastructure. |
Tomas Gunnarsson | 7d3cfbf | 2020-06-15 13:47:42 +0200 | [diff] [blame] | 78 | // TODO(bugs.webrtc.org/11547): Incorporate a dedicated network thread. |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 79 | class SctpDataChannelTest : public ::testing::Test { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 80 | protected: |
| 81 | SctpDataChannelTest() |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 82 | : controller_(new FakeDataChannelController()), |
| 83 | webrtc_data_channel_(SctpDataChannel::Create(controller_.get(), |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 84 | "test", |
| 85 | init_, |
| 86 | rtc::Thread::Current(), |
| 87 | rtc::Thread::Current())) {} |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 88 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 89 | void SetChannelReady() { |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 90 | controller_->set_transport_available(true); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 91 | webrtc_data_channel_->OnTransportChannelCreated(); |
| 92 | if (webrtc_data_channel_->id() < 0) { |
| 93 | webrtc_data_channel_->SetSctpSid(0); |
| 94 | } |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 95 | controller_->set_ready_to_send(true); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 96 | } |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 97 | |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 98 | void AddObserver() { |
| 99 | observer_.reset(new FakeDataChannelObserver()); |
| 100 | webrtc_data_channel_->RegisterObserver(observer_.get()); |
| 101 | } |
| 102 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 103 | webrtc::InternalDataChannelInit init_; |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 104 | std::unique_ptr<FakeDataChannelController> controller_; |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 105 | std::unique_ptr<FakeDataChannelObserver> observer_; |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 106 | rtc::scoped_refptr<SctpDataChannel> webrtc_data_channel_; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 109 | class StateSignalsListener : public sigslot::has_slots<> { |
| 110 | public: |
| 111 | int opened_count() const { return opened_count_; } |
| 112 | int closed_count() const { return closed_count_; } |
| 113 | |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 114 | void OnSignalOpened(DataChannelInterface* data_channel) { ++opened_count_; } |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 115 | |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 116 | void OnSignalClosed(DataChannelInterface* data_channel) { ++closed_count_; } |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 117 | |
| 118 | private: |
| 119 | int opened_count_ = 0; |
| 120 | int closed_count_ = 0; |
| 121 | }; |
| 122 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 123 | // Verifies that the data channel is connected to the transport after creation. |
| 124 | TEST_F(SctpDataChannelTest, ConnectedToTransportOnCreated) { |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 125 | controller_->set_transport_available(true); |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 126 | rtc::scoped_refptr<SctpDataChannel> dc = |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 127 | SctpDataChannel::Create(controller_.get(), "test1", init_, |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 128 | rtc::Thread::Current(), rtc::Thread::Current()); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 129 | |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 130 | EXPECT_TRUE(controller_->IsConnected(dc.get())); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 131 | // The sid is not set yet, so it should not have added the streams. |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 132 | EXPECT_FALSE(controller_->IsSendStreamAdded(dc->id())); |
| 133 | EXPECT_FALSE(controller_->IsRecvStreamAdded(dc->id())); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 134 | |
| 135 | dc->SetSctpSid(0); |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 136 | EXPECT_TRUE(controller_->IsSendStreamAdded(dc->id())); |
| 137 | EXPECT_TRUE(controller_->IsRecvStreamAdded(dc->id())); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // Verifies that the data channel is connected to the transport if the transport |
| 141 | // is not available initially and becomes available later. |
| 142 | TEST_F(SctpDataChannelTest, ConnectedAfterTransportBecomesAvailable) { |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 143 | EXPECT_FALSE(controller_->IsConnected(webrtc_data_channel_.get())); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 144 | |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 145 | controller_->set_transport_available(true); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 146 | webrtc_data_channel_->OnTransportChannelCreated(); |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 147 | EXPECT_TRUE(controller_->IsConnected(webrtc_data_channel_.get())); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 148 | } |
| 149 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 150 | // Tests the state of the data channel. |
| 151 | TEST_F(SctpDataChannelTest, StateTransition) { |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 152 | StateSignalsListener state_signals_listener; |
| 153 | webrtc_data_channel_->SignalOpened.connect( |
| 154 | &state_signals_listener, &StateSignalsListener::OnSignalOpened); |
| 155 | webrtc_data_channel_->SignalClosed.connect( |
| 156 | &state_signals_listener, &StateSignalsListener::OnSignalClosed); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 157 | EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, |
| 158 | webrtc_data_channel_->state()); |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 159 | EXPECT_EQ(state_signals_listener.opened_count(), 0); |
| 160 | EXPECT_EQ(state_signals_listener.closed_count(), 0); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 161 | SetChannelReady(); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 162 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 163 | EXPECT_EQ(webrtc::DataChannelInterface::kOpen, webrtc_data_channel_->state()); |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 164 | EXPECT_EQ(state_signals_listener.opened_count(), 1); |
| 165 | EXPECT_EQ(state_signals_listener.closed_count(), 0); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 166 | webrtc_data_channel_->Close(); |
| 167 | EXPECT_EQ(webrtc::DataChannelInterface::kClosed, |
| 168 | webrtc_data_channel_->state()); |
Harald Alvestrand | dfbfb46 | 2019-12-08 05:55:43 +0100 | [diff] [blame] | 169 | EXPECT_TRUE(webrtc_data_channel_->error().ok()); |
hbos | 82ebe02 | 2016-11-14 01:41:09 -0800 | [diff] [blame] | 170 | EXPECT_EQ(state_signals_listener.opened_count(), 1); |
| 171 | EXPECT_EQ(state_signals_listener.closed_count(), 1); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 172 | // Verifies that it's disconnected from the transport. |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 173 | EXPECT_FALSE(controller_->IsConnected(webrtc_data_channel_.get())); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 174 | } |
| 175 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 176 | // Tests that DataChannel::buffered_amount() is correct after the channel is |
| 177 | // blocked. |
| 178 | TEST_F(SctpDataChannelTest, BufferedAmountWhenBlocked) { |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 179 | AddObserver(); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 180 | SetChannelReady(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 181 | webrtc::DataBuffer buffer("abcd"); |
| 182 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
Marina Ciocea | e448a3f | 2019-03-04 15:52:21 +0100 | [diff] [blame] | 183 | size_t successful_send_count = 1; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 184 | |
| 185 | EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); |
Marina Ciocea | e448a3f | 2019-03-04 15:52:21 +0100 | [diff] [blame] | 186 | EXPECT_EQ(successful_send_count, |
| 187 | observer_->on_buffered_amount_change_count()); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 188 | |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 189 | controller_->set_send_blocked(true); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 190 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 191 | const int number_of_packets = 3; |
| 192 | for (int i = 0; i < number_of_packets; ++i) { |
| 193 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 194 | } |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 195 | EXPECT_EQ(buffer.data.size() * number_of_packets, |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 196 | webrtc_data_channel_->buffered_amount()); |
Marina Ciocea | e448a3f | 2019-03-04 15:52:21 +0100 | [diff] [blame] | 197 | EXPECT_EQ(successful_send_count, |
| 198 | observer_->on_buffered_amount_change_count()); |
| 199 | |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 200 | controller_->set_send_blocked(false); |
Marina Ciocea | e448a3f | 2019-03-04 15:52:21 +0100 | [diff] [blame] | 201 | successful_send_count += number_of_packets; |
| 202 | EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); |
| 203 | EXPECT_EQ(successful_send_count, |
Mirko Bonadei | e12c1fe | 2018-07-03 12:53:23 +0200 | [diff] [blame] | 204 | observer_->on_buffered_amount_change_count()); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // Tests that the queued data are sent when the channel transitions from blocked |
| 208 | // to unblocked. |
| 209 | TEST_F(SctpDataChannelTest, QueuedDataSentWhenUnblocked) { |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 210 | AddObserver(); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 211 | SetChannelReady(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 212 | webrtc::DataBuffer buffer("abcd"); |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 213 | controller_->set_send_blocked(true); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 214 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 215 | |
Marina Ciocea | e448a3f | 2019-03-04 15:52:21 +0100 | [diff] [blame] | 216 | EXPECT_EQ(0U, observer_->on_buffered_amount_change_count()); |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 217 | |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 218 | controller_->set_send_blocked(false); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 219 | SetChannelReady(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 220 | EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); |
Marina Ciocea | e448a3f | 2019-03-04 15:52:21 +0100 | [diff] [blame] | 221 | EXPECT_EQ(1U, observer_->on_buffered_amount_change_count()); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 222 | } |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 223 | |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 224 | // Tests that no crash when the channel is blocked right away while trying to |
| 225 | // send queued data. |
| 226 | TEST_F(SctpDataChannelTest, BlockedWhenSendQueuedDataNoCrash) { |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 227 | AddObserver(); |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 228 | SetChannelReady(); |
| 229 | webrtc::DataBuffer buffer("abcd"); |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 230 | controller_->set_send_blocked(true); |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 231 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
Marina Ciocea | e448a3f | 2019-03-04 15:52:21 +0100 | [diff] [blame] | 232 | EXPECT_EQ(0U, observer_->on_buffered_amount_change_count()); |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 233 | |
| 234 | // Set channel ready while it is still blocked. |
| 235 | SetChannelReady(); |
| 236 | EXPECT_EQ(buffer.size(), webrtc_data_channel_->buffered_amount()); |
Marina Ciocea | e448a3f | 2019-03-04 15:52:21 +0100 | [diff] [blame] | 237 | EXPECT_EQ(0U, observer_->on_buffered_amount_change_count()); |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 238 | |
| 239 | // Unblock the channel to send queued data again, there should be no crash. |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 240 | controller_->set_send_blocked(false); |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 241 | SetChannelReady(); |
| 242 | EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); |
Marina Ciocea | e448a3f | 2019-03-04 15:52:21 +0100 | [diff] [blame] | 243 | EXPECT_EQ(1U, observer_->on_buffered_amount_change_count()); |
jiayl@webrtc.org | cceb166 | 2015-01-22 00:55:10 +0000 | [diff] [blame] | 244 | } |
| 245 | |
hbos | 84ffdee | 2016-10-12 14:14:39 -0700 | [diff] [blame] | 246 | // Tests that DataChannel::messages_sent() and DataChannel::bytes_sent() are |
| 247 | // correct, sending data both while unblocked and while blocked. |
| 248 | TEST_F(SctpDataChannelTest, VerifyMessagesAndBytesSent) { |
| 249 | AddObserver(); |
| 250 | SetChannelReady(); |
| 251 | std::vector<webrtc::DataBuffer> buffers({ |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 252 | webrtc::DataBuffer("message 1"), |
| 253 | webrtc::DataBuffer("msg 2"), |
| 254 | webrtc::DataBuffer("message three"), |
| 255 | webrtc::DataBuffer("quadra message"), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 256 | webrtc::DataBuffer("fifthmsg"), |
| 257 | webrtc::DataBuffer("message of the beast"), |
hbos | 84ffdee | 2016-10-12 14:14:39 -0700 | [diff] [blame] | 258 | }); |
| 259 | |
| 260 | // Default values. |
| 261 | EXPECT_EQ(0U, webrtc_data_channel_->messages_sent()); |
| 262 | EXPECT_EQ(0U, webrtc_data_channel_->bytes_sent()); |
| 263 | |
| 264 | // Send three buffers while not blocked. |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 265 | controller_->set_send_blocked(false); |
hbos | 84ffdee | 2016-10-12 14:14:39 -0700 | [diff] [blame] | 266 | EXPECT_TRUE(webrtc_data_channel_->Send(buffers[0])); |
| 267 | EXPECT_TRUE(webrtc_data_channel_->Send(buffers[1])); |
| 268 | EXPECT_TRUE(webrtc_data_channel_->Send(buffers[2])); |
| 269 | size_t bytes_sent = buffers[0].size() + buffers[1].size() + buffers[2].size(); |
| 270 | EXPECT_EQ_WAIT(0U, webrtc_data_channel_->buffered_amount(), kDefaultTimeout); |
| 271 | EXPECT_EQ(3U, webrtc_data_channel_->messages_sent()); |
| 272 | EXPECT_EQ(bytes_sent, webrtc_data_channel_->bytes_sent()); |
| 273 | |
| 274 | // Send three buffers while blocked, queuing the buffers. |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 275 | controller_->set_send_blocked(true); |
hbos | 84ffdee | 2016-10-12 14:14:39 -0700 | [diff] [blame] | 276 | EXPECT_TRUE(webrtc_data_channel_->Send(buffers[3])); |
| 277 | EXPECT_TRUE(webrtc_data_channel_->Send(buffers[4])); |
| 278 | EXPECT_TRUE(webrtc_data_channel_->Send(buffers[5])); |
| 279 | size_t bytes_queued = |
| 280 | buffers[3].size() + buffers[4].size() + buffers[5].size(); |
| 281 | EXPECT_EQ(bytes_queued, webrtc_data_channel_->buffered_amount()); |
| 282 | EXPECT_EQ(3U, webrtc_data_channel_->messages_sent()); |
| 283 | EXPECT_EQ(bytes_sent, webrtc_data_channel_->bytes_sent()); |
| 284 | |
| 285 | // Unblock and make sure everything was sent. |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 286 | controller_->set_send_blocked(false); |
hbos | 84ffdee | 2016-10-12 14:14:39 -0700 | [diff] [blame] | 287 | EXPECT_EQ_WAIT(0U, webrtc_data_channel_->buffered_amount(), kDefaultTimeout); |
| 288 | bytes_sent += bytes_queued; |
| 289 | EXPECT_EQ(6U, webrtc_data_channel_->messages_sent()); |
| 290 | EXPECT_EQ(bytes_sent, webrtc_data_channel_->bytes_sent()); |
| 291 | } |
| 292 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 293 | // Tests that the queued control message is sent when channel is ready. |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 294 | TEST_F(SctpDataChannelTest, OpenMessageSent) { |
| 295 | // Initially the id is unassigned. |
| 296 | EXPECT_EQ(-1, webrtc_data_channel_->id()); |
| 297 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 298 | SetChannelReady(); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 299 | EXPECT_GE(webrtc_data_channel_->id(), 0); |
Florent Castelli | d95b149 | 2021-05-10 11:29:56 +0200 | [diff] [blame] | 300 | EXPECT_EQ(webrtc::DataMessageType::kControl, |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 301 | controller_->last_send_data_params().type); |
| 302 | EXPECT_EQ(controller_->last_sid(), webrtc_data_channel_->id()); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 303 | } |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 304 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 305 | TEST_F(SctpDataChannelTest, QueuedOpenMessageSent) { |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 306 | controller_->set_send_blocked(true); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 307 | SetChannelReady(); |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 308 | controller_->set_send_blocked(false); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 309 | |
Florent Castelli | d95b149 | 2021-05-10 11:29:56 +0200 | [diff] [blame] | 310 | EXPECT_EQ(webrtc::DataMessageType::kControl, |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 311 | controller_->last_send_data_params().type); |
| 312 | EXPECT_EQ(controller_->last_sid(), webrtc_data_channel_->id()); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 313 | } |
| 314 | |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 315 | // Tests that the DataChannel created after transport gets ready can enter OPEN |
| 316 | // state. |
| 317 | TEST_F(SctpDataChannelTest, LateCreatedChannelTransitionToOpen) { |
| 318 | SetChannelReady(); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 319 | webrtc::InternalDataChannelInit init; |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 320 | init.id = 1; |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 321 | rtc::scoped_refptr<SctpDataChannel> dc = |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 322 | SctpDataChannel::Create(controller_.get(), "test1", init, |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 323 | rtc::Thread::Current(), rtc::Thread::Current()); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 324 | EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, dc->state()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 325 | EXPECT_TRUE_WAIT(webrtc::DataChannelInterface::kOpen == dc->state(), 1000); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 326 | } |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 327 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 328 | // Tests that an unordered DataChannel sends data as ordered until the OPEN_ACK |
| 329 | // message is received. |
| 330 | TEST_F(SctpDataChannelTest, SendUnorderedAfterReceivesOpenAck) { |
| 331 | SetChannelReady(); |
| 332 | webrtc::InternalDataChannelInit init; |
| 333 | init.id = 1; |
| 334 | init.ordered = false; |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 335 | rtc::scoped_refptr<SctpDataChannel> dc = |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 336 | SctpDataChannel::Create(controller_.get(), "test1", init, |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 337 | rtc::Thread::Current(), rtc::Thread::Current()); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 338 | |
| 339 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); |
| 340 | |
| 341 | // Sends a message and verifies it's ordered. |
| 342 | webrtc::DataBuffer buffer("some data"); |
| 343 | ASSERT_TRUE(dc->Send(buffer)); |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 344 | EXPECT_TRUE(controller_->last_send_data_params().ordered); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 345 | |
| 346 | // Emulates receiving an OPEN_ACK message. |
| 347 | cricket::ReceiveDataParams params; |
Harald Alvestrand | 7af57c6 | 2021-04-16 11:12:14 +0000 | [diff] [blame] | 348 | params.sid = init.id; |
Florent Castelli | d95b149 | 2021-05-10 11:29:56 +0200 | [diff] [blame] | 349 | params.type = webrtc::DataMessageType::kControl; |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 350 | rtc::CopyOnWriteBuffer payload; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 351 | webrtc::WriteDataChannelOpenAckMessage(&payload); |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 352 | dc->OnDataReceived(params, payload); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 353 | |
| 354 | // Sends another message and verifies it's unordered. |
| 355 | ASSERT_TRUE(dc->Send(buffer)); |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 356 | EXPECT_FALSE(controller_->last_send_data_params().ordered); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | // Tests that an unordered DataChannel sends unordered data after any DATA |
| 360 | // message is received. |
| 361 | TEST_F(SctpDataChannelTest, SendUnorderedAfterReceiveData) { |
| 362 | SetChannelReady(); |
| 363 | webrtc::InternalDataChannelInit init; |
| 364 | init.id = 1; |
| 365 | init.ordered = false; |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 366 | rtc::scoped_refptr<SctpDataChannel> dc = |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 367 | SctpDataChannel::Create(controller_.get(), "test1", init, |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 368 | rtc::Thread::Current(), rtc::Thread::Current()); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 369 | |
| 370 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); |
| 371 | |
| 372 | // Emulates receiving a DATA message. |
| 373 | cricket::ReceiveDataParams params; |
Harald Alvestrand | 7af57c6 | 2021-04-16 11:12:14 +0000 | [diff] [blame] | 374 | params.sid = init.id; |
Florent Castelli | d95b149 | 2021-05-10 11:29:56 +0200 | [diff] [blame] | 375 | params.type = webrtc::DataMessageType::kText; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 376 | webrtc::DataBuffer buffer("data"); |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 377 | dc->OnDataReceived(params, buffer.data); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 378 | |
| 379 | // Sends a message and verifies it's unordered. |
| 380 | ASSERT_TRUE(dc->Send(buffer)); |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 381 | EXPECT_FALSE(controller_->last_send_data_params().ordered); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 384 | // Tests that the channel can't open until it's successfully sent the OPEN |
| 385 | // message. |
| 386 | TEST_F(SctpDataChannelTest, OpenWaitsForOpenMesssage) { |
| 387 | webrtc::DataBuffer buffer("foo"); |
| 388 | |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 389 | controller_->set_send_blocked(true); |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 390 | SetChannelReady(); |
| 391 | EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, |
| 392 | webrtc_data_channel_->state()); |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 393 | controller_->set_send_blocked(false); |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 394 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, |
| 395 | webrtc_data_channel_->state(), 1000); |
Florent Castelli | d95b149 | 2021-05-10 11:29:56 +0200 | [diff] [blame] | 396 | EXPECT_EQ(webrtc::DataMessageType::kControl, |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 397 | controller_->last_send_data_params().type); |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | // Tests that close first makes sure all queued data gets sent. |
| 401 | TEST_F(SctpDataChannelTest, QueuedCloseFlushes) { |
| 402 | webrtc::DataBuffer buffer("foo"); |
| 403 | |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 404 | controller_->set_send_blocked(true); |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 405 | SetChannelReady(); |
| 406 | EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, |
| 407 | webrtc_data_channel_->state()); |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 408 | controller_->set_send_blocked(false); |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 409 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, |
| 410 | webrtc_data_channel_->state(), 1000); |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 411 | controller_->set_send_blocked(true); |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 412 | webrtc_data_channel_->Send(buffer); |
| 413 | webrtc_data_channel_->Close(); |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 414 | controller_->set_send_blocked(false); |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 415 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, |
| 416 | webrtc_data_channel_->state(), 1000); |
Harald Alvestrand | dfbfb46 | 2019-12-08 05:55:43 +0100 | [diff] [blame] | 417 | EXPECT_TRUE(webrtc_data_channel_->error().ok()); |
Florent Castelli | d95b149 | 2021-05-10 11:29:56 +0200 | [diff] [blame] | 418 | EXPECT_EQ(webrtc::DataMessageType::kText, |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 419 | controller_->last_send_data_params().type); |
Lally Singh | 5c6c6e0 | 2015-05-29 11:52:39 -0400 | [diff] [blame] | 420 | } |
| 421 | |
Harald Alvestrand | 7af57c6 | 2021-04-16 11:12:14 +0000 | [diff] [blame] | 422 | // Tests that messages are sent with the right id. |
| 423 | TEST_F(SctpDataChannelTest, SendDataId) { |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 424 | webrtc_data_channel_->SetSctpSid(1); |
| 425 | SetChannelReady(); |
| 426 | webrtc::DataBuffer buffer("data"); |
| 427 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 428 | EXPECT_EQ(1, controller_->last_sid()); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Harald Alvestrand | 7af57c6 | 2021-04-16 11:12:14 +0000 | [diff] [blame] | 431 | // Tests that the incoming messages with wrong ids are rejected. |
| 432 | TEST_F(SctpDataChannelTest, ReceiveDataWithInvalidId) { |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 433 | webrtc_data_channel_->SetSctpSid(1); |
| 434 | SetChannelReady(); |
| 435 | |
| 436 | AddObserver(); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 437 | |
| 438 | cricket::ReceiveDataParams params; |
Harald Alvestrand | 7af57c6 | 2021-04-16 11:12:14 +0000 | [diff] [blame] | 439 | params.sid = 0; |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 440 | webrtc::DataBuffer buffer("abcd"); |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 441 | webrtc_data_channel_->OnDataReceived(params, buffer.data); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 442 | |
| 443 | EXPECT_EQ(0U, observer_->messages_received()); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Harald Alvestrand | 7af57c6 | 2021-04-16 11:12:14 +0000 | [diff] [blame] | 446 | // Tests that the incoming messages with right ids are accepted. |
| 447 | TEST_F(SctpDataChannelTest, ReceiveDataWithValidId) { |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 448 | webrtc_data_channel_->SetSctpSid(1); |
| 449 | SetChannelReady(); |
| 450 | |
| 451 | AddObserver(); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 452 | |
| 453 | cricket::ReceiveDataParams params; |
Harald Alvestrand | 7af57c6 | 2021-04-16 11:12:14 +0000 | [diff] [blame] | 454 | params.sid = 1; |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 455 | webrtc::DataBuffer buffer("abcd"); |
| 456 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 457 | webrtc_data_channel_->OnDataReceived(params, buffer.data); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 458 | EXPECT_EQ(1U, observer_->messages_received()); |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 459 | } |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 460 | |
| 461 | // Tests that no CONTROL message is sent if the datachannel is negotiated and |
| 462 | // not created from an OPEN message. |
| 463 | TEST_F(SctpDataChannelTest, NoMsgSentIfNegotiatedAndNotFromOpenMsg) { |
| 464 | webrtc::InternalDataChannelInit config; |
| 465 | config.id = 1; |
| 466 | config.negotiated = true; |
| 467 | config.open_handshake_role = webrtc::InternalDataChannelInit::kNone; |
| 468 | |
| 469 | SetChannelReady(); |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 470 | rtc::scoped_refptr<SctpDataChannel> dc = |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 471 | SctpDataChannel::Create(controller_.get(), "test1", config, |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 472 | rtc::Thread::Current(), rtc::Thread::Current()); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 473 | |
| 474 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 475 | EXPECT_EQ(0, controller_->last_sid()); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 476 | } |
| 477 | |
hbos | 84ffdee | 2016-10-12 14:14:39 -0700 | [diff] [blame] | 478 | // Tests that DataChannel::messages_received() and DataChannel::bytes_received() |
| 479 | // are correct, receiving data both while not open and while open. |
| 480 | TEST_F(SctpDataChannelTest, VerifyMessagesAndBytesReceived) { |
| 481 | AddObserver(); |
| 482 | std::vector<webrtc::DataBuffer> buffers({ |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 483 | webrtc::DataBuffer("message 1"), |
| 484 | webrtc::DataBuffer("msg 2"), |
| 485 | webrtc::DataBuffer("message three"), |
| 486 | webrtc::DataBuffer("quadra message"), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 487 | webrtc::DataBuffer("fifthmsg"), |
| 488 | webrtc::DataBuffer("message of the beast"), |
hbos | 84ffdee | 2016-10-12 14:14:39 -0700 | [diff] [blame] | 489 | }); |
| 490 | |
| 491 | webrtc_data_channel_->SetSctpSid(1); |
| 492 | cricket::ReceiveDataParams params; |
Harald Alvestrand | 7af57c6 | 2021-04-16 11:12:14 +0000 | [diff] [blame] | 493 | params.sid = 1; |
hbos | 84ffdee | 2016-10-12 14:14:39 -0700 | [diff] [blame] | 494 | |
| 495 | // Default values. |
| 496 | EXPECT_EQ(0U, webrtc_data_channel_->messages_received()); |
| 497 | EXPECT_EQ(0U, webrtc_data_channel_->bytes_received()); |
| 498 | |
| 499 | // Receive three buffers while data channel isn't open. |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 500 | webrtc_data_channel_->OnDataReceived(params, buffers[0].data); |
| 501 | webrtc_data_channel_->OnDataReceived(params, buffers[1].data); |
| 502 | webrtc_data_channel_->OnDataReceived(params, buffers[2].data); |
hbos | 84ffdee | 2016-10-12 14:14:39 -0700 | [diff] [blame] | 503 | EXPECT_EQ(0U, observer_->messages_received()); |
| 504 | EXPECT_EQ(0U, webrtc_data_channel_->messages_received()); |
| 505 | EXPECT_EQ(0U, webrtc_data_channel_->bytes_received()); |
| 506 | |
| 507 | // Open channel and make sure everything was received. |
| 508 | SetChannelReady(); |
| 509 | size_t bytes_received = |
| 510 | buffers[0].size() + buffers[1].size() + buffers[2].size(); |
| 511 | EXPECT_EQ(3U, observer_->messages_received()); |
| 512 | EXPECT_EQ(3U, webrtc_data_channel_->messages_received()); |
| 513 | EXPECT_EQ(bytes_received, webrtc_data_channel_->bytes_received()); |
| 514 | |
| 515 | // Receive three buffers while open. |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 516 | webrtc_data_channel_->OnDataReceived(params, buffers[3].data); |
| 517 | webrtc_data_channel_->OnDataReceived(params, buffers[4].data); |
| 518 | webrtc_data_channel_->OnDataReceived(params, buffers[5].data); |
hbos | 84ffdee | 2016-10-12 14:14:39 -0700 | [diff] [blame] | 519 | bytes_received += buffers[3].size() + buffers[4].size() + buffers[5].size(); |
| 520 | EXPECT_EQ(6U, observer_->messages_received()); |
| 521 | EXPECT_EQ(6U, webrtc_data_channel_->messages_received()); |
| 522 | EXPECT_EQ(bytes_received, webrtc_data_channel_->bytes_received()); |
| 523 | } |
| 524 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 525 | // Tests that OPEN_ACK message is sent if the datachannel is created from an |
| 526 | // OPEN message. |
| 527 | TEST_F(SctpDataChannelTest, OpenAckSentIfCreatedFromOpenMessage) { |
| 528 | webrtc::InternalDataChannelInit config; |
| 529 | config.id = 1; |
| 530 | config.negotiated = true; |
| 531 | config.open_handshake_role = webrtc::InternalDataChannelInit::kAcker; |
| 532 | |
| 533 | SetChannelReady(); |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 534 | rtc::scoped_refptr<SctpDataChannel> dc = |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 535 | SctpDataChannel::Create(controller_.get(), "test1", config, |
Taylor Brandstetter | 3a034e1 | 2020-07-09 15:32:34 -0700 | [diff] [blame] | 536 | rtc::Thread::Current(), rtc::Thread::Current()); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 537 | |
| 538 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); |
| 539 | |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 540 | EXPECT_EQ(config.id, controller_->last_sid()); |
Florent Castelli | d95b149 | 2021-05-10 11:29:56 +0200 | [diff] [blame] | 541 | EXPECT_EQ(webrtc::DataMessageType::kControl, |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 542 | controller_->last_send_data_params().type); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | // Tests the OPEN_ACK role assigned by InternalDataChannelInit. |
| 546 | TEST_F(SctpDataChannelTest, OpenAckRoleInitialization) { |
| 547 | webrtc::InternalDataChannelInit init; |
| 548 | EXPECT_EQ(webrtc::InternalDataChannelInit::kOpener, init.open_handshake_role); |
| 549 | EXPECT_FALSE(init.negotiated); |
| 550 | |
| 551 | webrtc::DataChannelInit base; |
| 552 | base.negotiated = true; |
| 553 | webrtc::InternalDataChannelInit init2(base); |
| 554 | EXPECT_EQ(webrtc::InternalDataChannelInit::kNone, init2.open_handshake_role); |
| 555 | } |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 556 | |
Florent Castelli | 0134303 | 2021-11-03 16:09:46 +0100 | [diff] [blame] | 557 | // Tests that that Send() returns false if the sending buffer is full |
| 558 | // and the channel stays open. |
| 559 | TEST_F(SctpDataChannelTest, OpenWhenSendBufferFull) { |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 560 | SetChannelReady(); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 561 | |
Florent Castelli | 0134303 | 2021-11-03 16:09:46 +0100 | [diff] [blame] | 562 | const size_t packetSize = 1024; |
| 563 | |
| 564 | rtc::CopyOnWriteBuffer buffer(packetSize); |
Danil Chapovalov | e15dc58 | 2021-01-07 15:24:05 +0100 | [diff] [blame] | 565 | memset(buffer.MutableData(), 0, buffer.size()); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 566 | |
| 567 | webrtc::DataBuffer packet(buffer, true); |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 568 | controller_->set_send_blocked(true); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 569 | |
Florent Castelli | 0134303 | 2021-11-03 16:09:46 +0100 | [diff] [blame] | 570 | for (size_t i = 0; |
| 571 | i < webrtc::DataChannelInterface::MaxSendQueueSize() / packetSize; ++i) { |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 572 | EXPECT_TRUE(webrtc_data_channel_->Send(packet)); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Florent Castelli | 0134303 | 2021-11-03 16:09:46 +0100 | [diff] [blame] | 575 | // The sending buffer shoul be full, send returns false. |
| 576 | EXPECT_FALSE(webrtc_data_channel_->Send(packet)); |
| 577 | |
| 578 | EXPECT_TRUE(webrtc::DataChannelInterface::kOpen == |
| 579 | webrtc_data_channel_->state()); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | // Tests that the DataChannel is closed on transport errors. |
| 583 | TEST_F(SctpDataChannelTest, ClosedOnTransportError) { |
| 584 | SetChannelReady(); |
| 585 | webrtc::DataBuffer buffer("abcd"); |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 586 | controller_->set_transport_error(); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 587 | |
| 588 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 589 | |
| 590 | EXPECT_EQ(webrtc::DataChannelInterface::kClosed, |
| 591 | webrtc_data_channel_->state()); |
Harald Alvestrand | dfbfb46 | 2019-12-08 05:55:43 +0100 | [diff] [blame] | 592 | EXPECT_FALSE(webrtc_data_channel_->error().ok()); |
| 593 | EXPECT_EQ(webrtc::RTCErrorType::NETWORK_ERROR, |
| 594 | webrtc_data_channel_->error().type()); |
| 595 | EXPECT_EQ(webrtc::RTCErrorDetailType::NONE, |
| 596 | webrtc_data_channel_->error().error_detail()); |
jiayl@webrtc.org | 5dc51fb | 2014-05-29 15:33:54 +0000 | [diff] [blame] | 597 | } |
jiayl@webrtc.org | 9f8164c | 2014-05-30 21:53:17 +0000 | [diff] [blame] | 598 | |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 599 | // Tests that the DataChannel is closed if the received buffer is full. |
| 600 | TEST_F(SctpDataChannelTest, ClosedWhenReceivedBufferFull) { |
| 601 | SetChannelReady(); |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 602 | rtc::CopyOnWriteBuffer buffer(1024); |
Danil Chapovalov | e15dc58 | 2021-01-07 15:24:05 +0100 | [diff] [blame] | 603 | memset(buffer.MutableData(), 0, buffer.size()); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 604 | |
| 605 | cricket::ReceiveDataParams params; |
Harald Alvestrand | 7af57c6 | 2021-04-16 11:12:14 +0000 | [diff] [blame] | 606 | params.sid = 0; |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 607 | |
| 608 | // Receiving data without having an observer will overflow the buffer. |
| 609 | for (size_t i = 0; i < 16 * 1024 + 1; ++i) { |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 610 | webrtc_data_channel_->OnDataReceived(params, buffer); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 611 | } |
| 612 | EXPECT_EQ(webrtc::DataChannelInterface::kClosed, |
| 613 | webrtc_data_channel_->state()); |
Harald Alvestrand | dfbfb46 | 2019-12-08 05:55:43 +0100 | [diff] [blame] | 614 | EXPECT_FALSE(webrtc_data_channel_->error().ok()); |
| 615 | EXPECT_EQ(webrtc::RTCErrorType::RESOURCE_EXHAUSTED, |
| 616 | webrtc_data_channel_->error().type()); |
| 617 | EXPECT_EQ(webrtc::RTCErrorDetailType::NONE, |
| 618 | webrtc_data_channel_->error().error_detail()); |
jiayl@webrtc.org | b43c99d | 2014-06-20 17:11:14 +0000 | [diff] [blame] | 619 | } |
jiayl@webrtc.org | 3edbaaf | 2014-07-18 23:57:50 +0000 | [diff] [blame] | 620 | |
| 621 | // Tests that sending empty data returns no error and keeps the channel open. |
| 622 | TEST_F(SctpDataChannelTest, SendEmptyData) { |
| 623 | webrtc_data_channel_->SetSctpSid(1); |
| 624 | SetChannelReady(); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 625 | EXPECT_EQ(webrtc::DataChannelInterface::kOpen, webrtc_data_channel_->state()); |
jiayl@webrtc.org | 3edbaaf | 2014-07-18 23:57:50 +0000 | [diff] [blame] | 626 | |
| 627 | webrtc::DataBuffer buffer(""); |
| 628 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 629 | EXPECT_EQ(webrtc::DataChannelInterface::kOpen, webrtc_data_channel_->state()); |
jiayl@webrtc.org | 3edbaaf | 2014-07-18 23:57:50 +0000 | [diff] [blame] | 630 | } |
bemasc@webrtc.org | 9b5467e | 2014-12-04 23:16:52 +0000 | [diff] [blame] | 631 | |
| 632 | // Tests that a channel can be closed without being opened or assigned an sid. |
| 633 | TEST_F(SctpDataChannelTest, NeverOpened) { |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 634 | controller_->set_transport_available(true); |
bemasc@webrtc.org | 9b5467e | 2014-12-04 23:16:52 +0000 | [diff] [blame] | 635 | webrtc_data_channel_->OnTransportChannelCreated(); |
| 636 | webrtc_data_channel_->Close(); |
| 637 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 638 | |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 639 | // Test that the data channel goes to the "closed" state (and doesn't crash) |
| 640 | // when its transport goes away, even while data is buffered. |
| 641 | TEST_F(SctpDataChannelTest, TransportDestroyedWhileDataBuffered) { |
| 642 | SetChannelReady(); |
| 643 | |
| 644 | rtc::CopyOnWriteBuffer buffer(1024); |
Danil Chapovalov | e15dc58 | 2021-01-07 15:24:05 +0100 | [diff] [blame] | 645 | memset(buffer.MutableData(), 0, buffer.size()); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 646 | webrtc::DataBuffer packet(buffer, true); |
| 647 | |
| 648 | // Send a packet while sending is blocked so it ends up buffered. |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 649 | controller_->set_send_blocked(true); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 650 | EXPECT_TRUE(webrtc_data_channel_->Send(packet)); |
| 651 | |
Harald Alvestrand | 408cb4b | 2019-11-16 12:09:08 +0100 | [diff] [blame] | 652 | // Tell the data channel that its transport is being destroyed. |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 653 | // It should then stop using the transport (allowing us to delete it) and |
| 654 | // transition to the "closed" state. |
Florent Castelli | dcb9ffc | 2021-06-29 14:58:23 +0200 | [diff] [blame] | 655 | webrtc::RTCError error(webrtc::RTCErrorType::OPERATION_ERROR_WITH_DATA, ""); |
| 656 | error.set_error_detail(webrtc::RTCErrorDetailType::SCTP_FAILURE); |
| 657 | webrtc_data_channel_->OnTransportChannelClosed(error); |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 658 | controller_.reset(nullptr); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 659 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, |
| 660 | webrtc_data_channel_->state(), kDefaultTimeout); |
Harald Alvestrand | dfbfb46 | 2019-12-08 05:55:43 +0100 | [diff] [blame] | 661 | EXPECT_FALSE(webrtc_data_channel_->error().ok()); |
Harald Alvestrand | 37e42be | 2020-05-12 12:59:02 +0200 | [diff] [blame] | 662 | EXPECT_EQ(webrtc::RTCErrorType::OPERATION_ERROR_WITH_DATA, |
Harald Alvestrand | dfbfb46 | 2019-12-08 05:55:43 +0100 | [diff] [blame] | 663 | webrtc_data_channel_->error().type()); |
Harald Alvestrand | 37e42be | 2020-05-12 12:59:02 +0200 | [diff] [blame] | 664 | EXPECT_EQ(webrtc::RTCErrorDetailType::SCTP_FAILURE, |
Harald Alvestrand | dfbfb46 | 2019-12-08 05:55:43 +0100 | [diff] [blame] | 665 | webrtc_data_channel_->error().error_detail()); |
Taylor Brandstetter | 4cb5b64 | 2016-08-12 10:10:31 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Florent Castelli | dcb9ffc | 2021-06-29 14:58:23 +0200 | [diff] [blame] | 668 | TEST_F(SctpDataChannelTest, TransportGotErrorCode) { |
| 669 | SetChannelReady(); |
| 670 | |
| 671 | // Tell the data channel that its transport is being destroyed with an |
| 672 | // error code. |
| 673 | // It should then report that error code. |
| 674 | webrtc::RTCError error(webrtc::RTCErrorType::OPERATION_ERROR_WITH_DATA, |
| 675 | "Transport channel closed"); |
| 676 | error.set_error_detail(webrtc::RTCErrorDetailType::SCTP_FAILURE); |
| 677 | error.set_sctp_cause_code( |
| 678 | static_cast<uint16_t>(cricket::SctpErrorCauseCode::kProtocolViolation)); |
| 679 | webrtc_data_channel_->OnTransportChannelClosed(error); |
Harald Alvestrand | 9e5aeb9 | 2022-05-11 09:35:36 +0000 | [diff] [blame^] | 680 | controller_.reset(nullptr); |
Florent Castelli | dcb9ffc | 2021-06-29 14:58:23 +0200 | [diff] [blame] | 681 | EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, |
| 682 | webrtc_data_channel_->state(), kDefaultTimeout); |
| 683 | EXPECT_FALSE(webrtc_data_channel_->error().ok()); |
| 684 | EXPECT_EQ(webrtc::RTCErrorType::OPERATION_ERROR_WITH_DATA, |
| 685 | webrtc_data_channel_->error().type()); |
| 686 | EXPECT_EQ(webrtc::RTCErrorDetailType::SCTP_FAILURE, |
| 687 | webrtc_data_channel_->error().error_detail()); |
| 688 | EXPECT_EQ( |
| 689 | static_cast<uint16_t>(cricket::SctpErrorCauseCode::kProtocolViolation), |
| 690 | webrtc_data_channel_->error().sctp_cause_code()); |
| 691 | } |
| 692 | |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 693 | class SctpSidAllocatorTest : public ::testing::Test { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 694 | protected: |
| 695 | SctpSidAllocator allocator_; |
| 696 | }; |
| 697 | |
| 698 | // Verifies that an even SCTP id is allocated for SSL_CLIENT and an odd id for |
| 699 | // SSL_SERVER. |
| 700 | TEST_F(SctpSidAllocatorTest, SctpIdAllocationBasedOnRole) { |
| 701 | int id; |
| 702 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &id)); |
| 703 | EXPECT_EQ(1, id); |
| 704 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &id)); |
| 705 | EXPECT_EQ(0, id); |
| 706 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &id)); |
| 707 | EXPECT_EQ(3, id); |
| 708 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &id)); |
| 709 | EXPECT_EQ(2, id); |
| 710 | } |
| 711 | |
| 712 | // Verifies that SCTP ids of existing DataChannels are not reused. |
| 713 | TEST_F(SctpSidAllocatorTest, SctpIdAllocationNoReuse) { |
| 714 | int old_id = 1; |
| 715 | EXPECT_TRUE(allocator_.ReserveSid(old_id)); |
| 716 | |
| 717 | int new_id; |
| 718 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &new_id)); |
| 719 | EXPECT_NE(old_id, new_id); |
| 720 | |
| 721 | old_id = 0; |
| 722 | EXPECT_TRUE(allocator_.ReserveSid(old_id)); |
| 723 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &new_id)); |
| 724 | EXPECT_NE(old_id, new_id); |
| 725 | } |
| 726 | |
| 727 | // Verifies that SCTP ids of removed DataChannels can be reused. |
| 728 | TEST_F(SctpSidAllocatorTest, SctpIdReusedForRemovedDataChannel) { |
| 729 | int odd_id = 1; |
| 730 | int even_id = 0; |
| 731 | EXPECT_TRUE(allocator_.ReserveSid(odd_id)); |
| 732 | EXPECT_TRUE(allocator_.ReserveSid(even_id)); |
| 733 | |
| 734 | int allocated_id = -1; |
| 735 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id)); |
| 736 | EXPECT_EQ(odd_id + 2, allocated_id); |
| 737 | |
| 738 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id)); |
| 739 | EXPECT_EQ(even_id + 2, allocated_id); |
| 740 | |
| 741 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id)); |
| 742 | EXPECT_EQ(odd_id + 4, allocated_id); |
| 743 | |
| 744 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id)); |
| 745 | EXPECT_EQ(even_id + 4, allocated_id); |
| 746 | |
| 747 | allocator_.ReleaseSid(odd_id); |
| 748 | allocator_.ReleaseSid(even_id); |
| 749 | |
| 750 | // Verifies that removed ids are reused. |
| 751 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id)); |
| 752 | EXPECT_EQ(odd_id, allocated_id); |
| 753 | |
| 754 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id)); |
| 755 | EXPECT_EQ(even_id, allocated_id); |
| 756 | |
| 757 | // Verifies that used higher ids are not reused. |
| 758 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id)); |
| 759 | EXPECT_EQ(odd_id + 6, allocated_id); |
| 760 | |
| 761 | EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id)); |
| 762 | EXPECT_EQ(even_id + 6, allocated_id); |
| 763 | } |