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