2013-08-01 00:00:07 +00:00
|
|
|
/*
|
2016-02-10 07:54:43 -08:00
|
|
|
* Copyright 2013 The WebRTC project authors. All Rights Reserved.
|
2013-08-01 00:00:07 +00:00
|
|
|
*
|
2016-02-10 07:54:43 -08:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2013-08-01 00:00:07 +00:00
|
|
|
*/
|
|
|
|
|
|
2022-02-23 13:44:59 +00:00
|
|
|
#include <stdint.h>
|
2018-11-28 16:47:49 +01:00
|
|
|
#include <string.h>
|
2019-07-05 19:08:33 +02:00
|
|
|
|
2016-04-27 06:47:29 -07:00
|
|
|
#include <memory>
|
2022-02-23 13:44:59 +00:00
|
|
|
#include <string>
|
2017-10-30 09:57:42 -07:00
|
|
|
#include <vector>
|
2016-04-27 06:47:29 -07:00
|
|
|
|
2022-02-23 13:44:59 +00:00
|
|
|
#include "api/data_channel_interface.h"
|
|
|
|
|
#include "api/rtc_error.h"
|
|
|
|
|
#include "api/scoped_refptr.h"
|
|
|
|
|
#include "api/transport/data_channel_transport_interface.h"
|
|
|
|
|
#include "media/base/media_channel.h"
|
2021-06-29 14:58:23 +02:00
|
|
|
#include "media/sctp/sctp_transport_internal.h"
|
2020-07-09 15:32:34 -07:00
|
|
|
#include "pc/sctp_data_channel.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "pc/sctp_utils.h"
|
2022-05-11 09:35:36 +00:00
|
|
|
#include "pc/test/fake_data_channel_controller.h"
|
2022-02-23 13:44:59 +00:00
|
|
|
#include "rtc_base/copy_on_write_buffer.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/gunit.h"
|
2023-03-27 12:39:33 +02:00
|
|
|
#include "rtc_base/null_socket_server.h"
|
2022-02-23 13:44:59 +00:00
|
|
|
#include "rtc_base/ssl_stream_adapter.h"
|
|
|
|
|
#include "rtc_base/thread.h"
|
2018-11-28 16:47:49 +01:00
|
|
|
#include "test/gtest.h"
|
2023-03-27 12:39:33 +02:00
|
|
|
#include "test/run_loop.h"
|
2013-08-01 00:00:07 +00:00
|
|
|
|
2023-03-12 16:59:25 +01:00
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
|
|
namespace {
|
2013-08-01 00:00:07 +00:00
|
|
|
|
2016-08-12 10:10:31 -07:00
|
|
|
static constexpr int kDefaultTimeout = 10000;
|
|
|
|
|
|
2023-03-12 16:59:25 +01:00
|
|
|
class FakeDataChannelObserver : public DataChannelObserver {
|
2013-11-13 22:48:52 +00:00
|
|
|
public:
|
2023-04-04 13:48:34 +02:00
|
|
|
FakeDataChannelObserver() {
|
|
|
|
|
// This implementation relies on the SctpDataChannel::ObserverAdapter
|
|
|
|
|
// implementation to post events to the signaling thread.
|
|
|
|
|
RTC_DCHECK(!IsOkToCallOnTheNetworkThread());
|
|
|
|
|
}
|
2014-05-30 21:53:17 +00:00
|
|
|
|
2023-04-04 13:48:34 +02:00
|
|
|
void OnStateChange() override { ++on_state_change_count_; }
|
2014-05-29 15:33:54 +00:00
|
|
|
|
2023-04-04 13:48:34 +02:00
|
|
|
void OnBufferedAmountChange(uint64_t previous_amount) override {
|
2015-07-01 13:34:33 -07:00
|
|
|
++on_buffered_amount_change_count_;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-04 13:48:34 +02:00
|
|
|
void OnMessage(const DataBuffer& buffer) override { ++messages_received_; }
|
2014-05-29 15:33:54 +00:00
|
|
|
|
|
|
|
|
size_t messages_received() const { return messages_received_; }
|
|
|
|
|
|
2014-05-30 21:53:17 +00:00
|
|
|
void ResetOnStateChangeCount() { on_state_change_count_ = 0; }
|
|
|
|
|
|
2015-07-01 13:34:33 -07:00
|
|
|
void ResetOnBufferedAmountChangeCount() {
|
|
|
|
|
on_buffered_amount_change_count_ = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-30 21:53:17 +00:00
|
|
|
size_t on_state_change_count() const { return on_state_change_count_; }
|
|
|
|
|
|
2015-07-01 13:34:33 -07:00
|
|
|
size_t on_buffered_amount_change_count() const {
|
|
|
|
|
return on_buffered_amount_change_count_;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-29 15:33:54 +00:00
|
|
|
private:
|
2023-04-04 13:48:34 +02:00
|
|
|
size_t messages_received_ = 0u;
|
|
|
|
|
size_t on_state_change_count_ = 0u;
|
|
|
|
|
size_t on_buffered_amount_change_count_ = 0u;
|
2013-11-13 22:48:52 +00:00
|
|
|
};
|
|
|
|
|
|
2019-04-09 15:11:12 +02:00
|
|
|
class SctpDataChannelTest : public ::testing::Test {
|
2013-08-01 00:00:07 +00:00
|
|
|
protected:
|
|
|
|
|
SctpDataChannelTest()
|
2023-03-27 12:39:33 +02:00
|
|
|
: network_thread_(std::make_unique<rtc::NullSocketServer>()),
|
2023-03-29 20:46:59 +02:00
|
|
|
controller_(new FakeDataChannelController(&network_thread_)) {
|
2023-03-27 12:39:33 +02:00
|
|
|
network_thread_.Start();
|
2023-03-31 11:07:31 +02:00
|
|
|
inner_channel_ = controller_->CreateDataChannel("test", init_);
|
|
|
|
|
channel_ = webrtc::SctpDataChannel::CreateProxy(inner_channel_);
|
2023-03-27 12:39:33 +02:00
|
|
|
}
|
|
|
|
|
~SctpDataChannelTest() override {
|
|
|
|
|
run_loop_.Flush();
|
2023-03-31 11:07:31 +02:00
|
|
|
inner_channel_ = nullptr;
|
|
|
|
|
channel_ = nullptr;
|
|
|
|
|
controller_.reset();
|
|
|
|
|
observer_.reset();
|
2023-03-27 12:39:33 +02:00
|
|
|
network_thread_.Stop();
|
|
|
|
|
}
|
2013-08-01 00:00:07 +00:00
|
|
|
|
2013-10-07 23:32:02 +00:00
|
|
|
void SetChannelReady() {
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->set_transport_available(true);
|
2023-04-04 13:48:34 +02:00
|
|
|
StreamId sid(0);
|
|
|
|
|
network_thread_.BlockingCall([&]() {
|
|
|
|
|
RTC_DCHECK_RUN_ON(&network_thread_);
|
|
|
|
|
if (!inner_channel_->sid_n().HasValue()) {
|
|
|
|
|
inner_channel_->SetSctpSid_n(sid);
|
|
|
|
|
controller_->AddSctpDataStream(sid);
|
|
|
|
|
}
|
|
|
|
|
inner_channel_->OnTransportChannelCreated();
|
|
|
|
|
});
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->set_ready_to_send(true);
|
2023-04-04 13:48:34 +02:00
|
|
|
run_loop_.Flush();
|
2013-08-01 00:00:07 +00:00
|
|
|
}
|
2013-10-07 23:32:02 +00:00
|
|
|
|
2023-03-29 20:46:59 +02:00
|
|
|
// TODO(bugs.webrtc.org/11547): This mirrors what the DataChannelController
|
|
|
|
|
// currently does when assigning stream ids to a channel. Right now the sid
|
|
|
|
|
// in the SctpDataChannel code is (still) tied to the signaling thread, but
|
|
|
|
|
// the `AddSctpDataStream` operation is a bridge to the transport and needs
|
|
|
|
|
// to run on the network thread.
|
|
|
|
|
void SetChannelSid(const rtc::scoped_refptr<SctpDataChannel>& channel,
|
|
|
|
|
StreamId sid) {
|
|
|
|
|
RTC_DCHECK(sid.HasValue());
|
2023-04-04 13:48:34 +02:00
|
|
|
network_thread_.BlockingCall([&]() {
|
|
|
|
|
channel->SetSctpSid_n(sid);
|
|
|
|
|
controller_->AddSctpDataStream(sid);
|
|
|
|
|
});
|
2023-03-29 20:46:59 +02:00
|
|
|
}
|
|
|
|
|
|
2013-11-13 22:48:52 +00:00
|
|
|
void AddObserver() {
|
|
|
|
|
observer_.reset(new FakeDataChannelObserver());
|
2023-03-31 11:07:31 +02:00
|
|
|
channel_->RegisterObserver(observer_.get());
|
2013-11-13 22:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
2023-03-27 12:39:33 +02:00
|
|
|
test::RunLoop run_loop_;
|
|
|
|
|
rtc::Thread network_thread_;
|
2023-03-12 16:59:25 +01:00
|
|
|
InternalDataChannelInit init_;
|
2022-05-11 09:35:36 +00:00
|
|
|
std::unique_ptr<FakeDataChannelController> controller_;
|
2016-04-27 06:47:29 -07:00
|
|
|
std::unique_ptr<FakeDataChannelObserver> observer_;
|
2023-03-31 11:07:31 +02:00
|
|
|
rtc::scoped_refptr<SctpDataChannel> inner_channel_;
|
|
|
|
|
rtc::scoped_refptr<DataChannelInterface> channel_;
|
2013-08-01 00:00:07 +00:00
|
|
|
};
|
|
|
|
|
|
2023-03-12 16:59:25 +01:00
|
|
|
TEST_F(SctpDataChannelTest, VerifyConfigurationGetters) {
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(channel_->label(), "test");
|
|
|
|
|
EXPECT_EQ(channel_->protocol(), init_.protocol);
|
2023-03-12 16:59:25 +01:00
|
|
|
|
|
|
|
|
// Note that the `init_.reliable` field is deprecated, so we directly set
|
|
|
|
|
// it here to match spec behavior for purposes of checking the `reliable()`
|
|
|
|
|
// getter.
|
|
|
|
|
init_.reliable = (!init_.maxRetransmits && !init_.maxRetransmitTime);
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(channel_->reliable(), init_.reliable);
|
|
|
|
|
EXPECT_EQ(channel_->ordered(), init_.ordered);
|
|
|
|
|
EXPECT_EQ(channel_->negotiated(), init_.negotiated);
|
|
|
|
|
EXPECT_EQ(channel_->priority(), Priority::kLow);
|
|
|
|
|
EXPECT_EQ(channel_->maxRetransmitTime(), static_cast<uint16_t>(-1));
|
|
|
|
|
EXPECT_EQ(channel_->maxPacketLifeTime(), init_.maxRetransmitTime);
|
|
|
|
|
EXPECT_EQ(channel_->maxRetransmits(), static_cast<uint16_t>(-1));
|
|
|
|
|
EXPECT_EQ(channel_->maxRetransmitsOpt(), init_.maxRetransmits);
|
2023-03-12 16:59:25 +01:00
|
|
|
|
|
|
|
|
// Check the non-const part of the configuration.
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(channel_->id(), init_.id);
|
2023-04-04 13:48:34 +02:00
|
|
|
network_thread_.BlockingCall(
|
|
|
|
|
[&]() { EXPECT_EQ(inner_channel_->sid_n(), StreamId()); });
|
2023-03-12 16:59:25 +01:00
|
|
|
|
|
|
|
|
SetChannelReady();
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(channel_->id(), 0);
|
2023-04-04 13:48:34 +02:00
|
|
|
network_thread_.BlockingCall(
|
|
|
|
|
[&]() { EXPECT_EQ(inner_channel_->sid_n(), StreamId(0)); });
|
2023-03-12 16:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
2013-10-30 05:18:12 +00:00
|
|
|
// Verifies that the data channel is connected to the transport after creation.
|
|
|
|
|
TEST_F(SctpDataChannelTest, ConnectedToTransportOnCreated) {
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->set_transport_available(true);
|
2020-07-09 15:32:34 -07:00
|
|
|
rtc::scoped_refptr<SctpDataChannel> dc =
|
2023-03-20 14:43:09 +01:00
|
|
|
controller_->CreateDataChannel("test1", init_);
|
2022-05-11 09:35:36 +00:00
|
|
|
EXPECT_TRUE(controller_->IsConnected(dc.get()));
|
2023-04-04 13:48:34 +02:00
|
|
|
|
2013-10-30 05:18:12 +00:00
|
|
|
// The sid is not set yet, so it should not have added the streams.
|
2023-04-04 13:48:34 +02:00
|
|
|
StreamId sid = network_thread_.BlockingCall([&]() { return dc->sid_n(); });
|
|
|
|
|
EXPECT_FALSE(controller_->IsStreamAdded(sid));
|
2013-10-30 05:18:12 +00:00
|
|
|
|
2023-03-29 20:46:59 +02:00
|
|
|
SetChannelSid(dc, StreamId(0));
|
2023-04-04 13:48:34 +02:00
|
|
|
sid = network_thread_.BlockingCall([&]() { return dc->sid_n(); });
|
|
|
|
|
EXPECT_TRUE(controller_->IsStreamAdded(sid));
|
2013-10-30 05:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-07 23:32:02 +00:00
|
|
|
// Tests the state of the data channel.
|
|
|
|
|
TEST_F(SctpDataChannelTest, StateTransition) {
|
2023-03-14 09:23:51 +01:00
|
|
|
AddObserver();
|
|
|
|
|
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(DataChannelInterface::kConnecting, channel_->state());
|
2023-03-14 09:23:51 +01:00
|
|
|
EXPECT_EQ(observer_->on_state_change_count(), 0u);
|
2013-10-07 23:32:02 +00:00
|
|
|
SetChannelReady();
|
2013-11-04 18:41:34 +00:00
|
|
|
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(DataChannelInterface::kOpen, channel_->state());
|
2023-03-14 09:23:51 +01:00
|
|
|
EXPECT_EQ(observer_->on_state_change_count(), 1u);
|
2023-03-02 10:51:16 +01:00
|
|
|
|
2023-03-14 09:23:51 +01:00
|
|
|
// `Close()` should trigger two state changes, first `kClosing`, then
|
|
|
|
|
// `kClose`.
|
2023-03-31 11:07:31 +02:00
|
|
|
channel_->Close();
|
2023-03-27 12:39:33 +02:00
|
|
|
// The (simulated) transport close notifications runs on the network thread
|
|
|
|
|
// and posts a completion notification to the signaling (current) thread.
|
2023-04-04 13:48:34 +02:00
|
|
|
// Allow that operation to complete before checking the state.
|
2023-03-27 12:39:33 +02:00
|
|
|
run_loop_.Flush();
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(DataChannelInterface::kClosed, channel_->state());
|
2023-03-14 09:23:51 +01:00
|
|
|
EXPECT_EQ(observer_->on_state_change_count(), 3u);
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_TRUE(channel_->error().ok());
|
2013-10-30 05:18:12 +00:00
|
|
|
// Verifies that it's disconnected from the transport.
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_FALSE(controller_->IsConnected(inner_channel_.get()));
|
2013-10-07 23:32:02 +00:00
|
|
|
}
|
|
|
|
|
|
2013-08-01 00:00:07 +00:00
|
|
|
// Tests that DataChannel::buffered_amount() is correct after the channel is
|
|
|
|
|
// blocked.
|
|
|
|
|
TEST_F(SctpDataChannelTest, BufferedAmountWhenBlocked) {
|
2015-07-01 13:34:33 -07:00
|
|
|
AddObserver();
|
2013-10-07 23:32:02 +00:00
|
|
|
SetChannelReady();
|
2023-03-12 16:59:25 +01:00
|
|
|
DataBuffer buffer("abcd");
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_TRUE(channel_->Send(buffer));
|
2019-03-04 15:52:21 +01:00
|
|
|
size_t successful_send_count = 1;
|
2013-08-01 00:00:07 +00:00
|
|
|
|
2023-04-04 13:48:34 +02:00
|
|
|
run_loop_.Flush();
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(0U, channel_->buffered_amount());
|
2019-03-04 15:52:21 +01:00
|
|
|
EXPECT_EQ(successful_send_count,
|
|
|
|
|
observer_->on_buffered_amount_change_count());
|
2013-08-01 00:00:07 +00:00
|
|
|
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->set_send_blocked(true);
|
2013-10-07 23:32:02 +00:00
|
|
|
|
2013-08-01 00:00:07 +00:00
|
|
|
const int number_of_packets = 3;
|
|
|
|
|
for (int i = 0; i < number_of_packets; ++i) {
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_TRUE(channel_->Send(buffer));
|
2013-08-01 00:00:07 +00:00
|
|
|
}
|
2015-03-24 09:19:06 +00:00
|
|
|
EXPECT_EQ(buffer.data.size() * number_of_packets,
|
2023-03-31 11:07:31 +02:00
|
|
|
channel_->buffered_amount());
|
2019-03-04 15:52:21 +01:00
|
|
|
EXPECT_EQ(successful_send_count,
|
|
|
|
|
observer_->on_buffered_amount_change_count());
|
|
|
|
|
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->set_send_blocked(false);
|
2023-04-04 13:48:34 +02:00
|
|
|
run_loop_.Flush();
|
2019-03-04 15:52:21 +01:00
|
|
|
successful_send_count += number_of_packets;
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(0U, channel_->buffered_amount());
|
2019-03-04 15:52:21 +01:00
|
|
|
EXPECT_EQ(successful_send_count,
|
2018-07-03 12:53:23 +02:00
|
|
|
observer_->on_buffered_amount_change_count());
|
2013-08-01 00:00:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tests that the queued data are sent when the channel transitions from blocked
|
|
|
|
|
// to unblocked.
|
|
|
|
|
TEST_F(SctpDataChannelTest, QueuedDataSentWhenUnblocked) {
|
2015-07-01 13:34:33 -07:00
|
|
|
AddObserver();
|
2013-10-07 23:32:02 +00:00
|
|
|
SetChannelReady();
|
2023-03-12 16:59:25 +01:00
|
|
|
DataBuffer buffer("abcd");
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->set_send_blocked(true);
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_TRUE(channel_->Send(buffer));
|
2013-08-01 00:00:07 +00:00
|
|
|
|
2019-03-04 15:52:21 +01:00
|
|
|
EXPECT_EQ(0U, observer_->on_buffered_amount_change_count());
|
2015-07-01 13:34:33 -07:00
|
|
|
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->set_send_blocked(false);
|
2013-10-07 23:32:02 +00:00
|
|
|
SetChannelReady();
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(0U, channel_->buffered_amount());
|
2019-03-04 15:52:21 +01:00
|
|
|
EXPECT_EQ(1U, observer_->on_buffered_amount_change_count());
|
2013-08-01 00:00:07 +00:00
|
|
|
}
|
2013-10-07 23:32:02 +00:00
|
|
|
|
2015-01-22 00:55:10 +00:00
|
|
|
// Tests that no crash when the channel is blocked right away while trying to
|
|
|
|
|
// send queued data.
|
|
|
|
|
TEST_F(SctpDataChannelTest, BlockedWhenSendQueuedDataNoCrash) {
|
2015-07-01 13:34:33 -07:00
|
|
|
AddObserver();
|
2015-01-22 00:55:10 +00:00
|
|
|
SetChannelReady();
|
2023-03-12 16:59:25 +01:00
|
|
|
DataBuffer buffer("abcd");
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->set_send_blocked(true);
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_TRUE(channel_->Send(buffer));
|
2019-03-04 15:52:21 +01:00
|
|
|
EXPECT_EQ(0U, observer_->on_buffered_amount_change_count());
|
2015-01-22 00:55:10 +00:00
|
|
|
|
|
|
|
|
// Set channel ready while it is still blocked.
|
|
|
|
|
SetChannelReady();
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(buffer.size(), channel_->buffered_amount());
|
2019-03-04 15:52:21 +01:00
|
|
|
EXPECT_EQ(0U, observer_->on_buffered_amount_change_count());
|
2015-01-22 00:55:10 +00:00
|
|
|
|
|
|
|
|
// Unblock the channel to send queued data again, there should be no crash.
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->set_send_blocked(false);
|
2015-01-22 00:55:10 +00:00
|
|
|
SetChannelReady();
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(0U, channel_->buffered_amount());
|
2019-03-04 15:52:21 +01:00
|
|
|
EXPECT_EQ(1U, observer_->on_buffered_amount_change_count());
|
2015-01-22 00:55:10 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-12 14:14:39 -07:00
|
|
|
// Tests that DataChannel::messages_sent() and DataChannel::bytes_sent() are
|
|
|
|
|
// correct, sending data both while unblocked and while blocked.
|
|
|
|
|
TEST_F(SctpDataChannelTest, VerifyMessagesAndBytesSent) {
|
|
|
|
|
AddObserver();
|
|
|
|
|
SetChannelReady();
|
2023-03-12 16:59:25 +01:00
|
|
|
std::vector<DataBuffer> buffers({
|
|
|
|
|
DataBuffer("message 1"),
|
|
|
|
|
DataBuffer("msg 2"),
|
|
|
|
|
DataBuffer("message three"),
|
|
|
|
|
DataBuffer("quadra message"),
|
|
|
|
|
DataBuffer("fifthmsg"),
|
|
|
|
|
DataBuffer("message of the beast"),
|
2016-10-12 14:14:39 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Default values.
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(0U, channel_->messages_sent());
|
|
|
|
|
EXPECT_EQ(0U, channel_->bytes_sent());
|
2016-10-12 14:14:39 -07:00
|
|
|
|
|
|
|
|
// Send three buffers while not blocked.
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->set_send_blocked(false);
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_TRUE(channel_->Send(buffers[0]));
|
|
|
|
|
EXPECT_TRUE(channel_->Send(buffers[1]));
|
|
|
|
|
EXPECT_TRUE(channel_->Send(buffers[2]));
|
2016-10-12 14:14:39 -07:00
|
|
|
size_t bytes_sent = buffers[0].size() + buffers[1].size() + buffers[2].size();
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ_WAIT(0U, channel_->buffered_amount(), kDefaultTimeout);
|
|
|
|
|
EXPECT_EQ(3U, channel_->messages_sent());
|
|
|
|
|
EXPECT_EQ(bytes_sent, channel_->bytes_sent());
|
2016-10-12 14:14:39 -07:00
|
|
|
|
|
|
|
|
// Send three buffers while blocked, queuing the buffers.
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->set_send_blocked(true);
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_TRUE(channel_->Send(buffers[3]));
|
|
|
|
|
EXPECT_TRUE(channel_->Send(buffers[4]));
|
|
|
|
|
EXPECT_TRUE(channel_->Send(buffers[5]));
|
2016-10-12 14:14:39 -07:00
|
|
|
size_t bytes_queued =
|
|
|
|
|
buffers[3].size() + buffers[4].size() + buffers[5].size();
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(bytes_queued, channel_->buffered_amount());
|
|
|
|
|
EXPECT_EQ(3U, channel_->messages_sent());
|
|
|
|
|
EXPECT_EQ(bytes_sent, channel_->bytes_sent());
|
2016-10-12 14:14:39 -07:00
|
|
|
|
|
|
|
|
// Unblock and make sure everything was sent.
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->set_send_blocked(false);
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ_WAIT(0U, channel_->buffered_amount(), kDefaultTimeout);
|
2016-10-12 14:14:39 -07:00
|
|
|
bytes_sent += bytes_queued;
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(6U, channel_->messages_sent());
|
|
|
|
|
EXPECT_EQ(bytes_sent, channel_->bytes_sent());
|
2016-10-12 14:14:39 -07:00
|
|
|
}
|
|
|
|
|
|
2013-10-07 23:32:02 +00:00
|
|
|
// Tests that the queued control message is sent when channel is ready.
|
2013-10-30 05:18:12 +00:00
|
|
|
TEST_F(SctpDataChannelTest, OpenMessageSent) {
|
|
|
|
|
// Initially the id is unassigned.
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(-1, channel_->id());
|
2013-10-30 05:18:12 +00:00
|
|
|
|
2013-10-07 23:32:02 +00:00
|
|
|
SetChannelReady();
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_GE(channel_->id(), 0);
|
2023-03-12 16:59:25 +01:00
|
|
|
EXPECT_EQ(DataMessageType::kControl,
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->last_send_data_params().type);
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(controller_->last_sid(), channel_->id());
|
2013-10-07 23:32:02 +00:00
|
|
|
}
|
2013-11-04 18:41:34 +00:00
|
|
|
|
2014-06-20 17:11:14 +00:00
|
|
|
TEST_F(SctpDataChannelTest, QueuedOpenMessageSent) {
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->set_send_blocked(true);
|
2014-06-20 17:11:14 +00:00
|
|
|
SetChannelReady();
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->set_send_blocked(false);
|
2014-06-20 17:11:14 +00:00
|
|
|
|
2023-03-12 16:59:25 +01:00
|
|
|
EXPECT_EQ(DataMessageType::kControl,
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->last_send_data_params().type);
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(controller_->last_sid(), channel_->id());
|
2014-06-20 17:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
2013-11-04 18:41:34 +00:00
|
|
|
// Tests that the DataChannel created after transport gets ready can enter OPEN
|
|
|
|
|
// state.
|
|
|
|
|
TEST_F(SctpDataChannelTest, LateCreatedChannelTransitionToOpen) {
|
|
|
|
|
SetChannelReady();
|
2023-03-12 16:59:25 +01:00
|
|
|
InternalDataChannelInit init;
|
2013-11-04 18:41:34 +00:00
|
|
|
init.id = 1;
|
2023-04-04 13:48:34 +02:00
|
|
|
auto dc = webrtc::SctpDataChannel::CreateProxy(
|
|
|
|
|
controller_->CreateDataChannel("test1", init));
|
|
|
|
|
EXPECT_EQ(DataChannelInterface::kOpen, dc->state());
|
2013-11-04 18:41:34 +00:00
|
|
|
}
|
2013-11-13 22:48:52 +00:00
|
|
|
|
2014-01-14 10:00:58 +00:00
|
|
|
// Tests that an unordered DataChannel sends data as ordered until the OPEN_ACK
|
|
|
|
|
// message is received.
|
|
|
|
|
TEST_F(SctpDataChannelTest, SendUnorderedAfterReceivesOpenAck) {
|
|
|
|
|
SetChannelReady();
|
2023-03-12 16:59:25 +01:00
|
|
|
InternalDataChannelInit init;
|
2014-01-14 10:00:58 +00:00
|
|
|
init.id = 1;
|
|
|
|
|
init.ordered = false;
|
2020-07-09 15:32:34 -07:00
|
|
|
rtc::scoped_refptr<SctpDataChannel> dc =
|
2023-03-20 14:43:09 +01:00
|
|
|
controller_->CreateDataChannel("test1", init);
|
2023-04-04 13:48:34 +02:00
|
|
|
auto proxy = webrtc::SctpDataChannel::CreateProxy(dc);
|
2014-01-14 10:00:58 +00:00
|
|
|
|
2023-04-04 13:48:34 +02:00
|
|
|
EXPECT_EQ_WAIT(DataChannelInterface::kOpen, proxy->state(), 1000);
|
2014-01-14 10:00:58 +00:00
|
|
|
|
|
|
|
|
// Sends a message and verifies it's ordered.
|
2023-03-12 16:59:25 +01:00
|
|
|
DataBuffer buffer("some data");
|
2023-04-04 13:48:34 +02:00
|
|
|
ASSERT_TRUE(proxy->Send(buffer));
|
2022-05-11 09:35:36 +00:00
|
|
|
EXPECT_TRUE(controller_->last_send_data_params().ordered);
|
2014-01-14 10:00:58 +00:00
|
|
|
|
|
|
|
|
// Emulates receiving an OPEN_ACK message.
|
2016-03-20 06:15:43 -07:00
|
|
|
rtc::CopyOnWriteBuffer payload;
|
2023-03-12 16:59:25 +01:00
|
|
|
WriteDataChannelOpenAckMessage(&payload);
|
2023-04-04 13:48:34 +02:00
|
|
|
network_thread_.BlockingCall(
|
|
|
|
|
[&] { dc->OnDataReceived(DataMessageType::kControl, payload); });
|
2014-01-14 10:00:58 +00:00
|
|
|
|
|
|
|
|
// Sends another message and verifies it's unordered.
|
2023-04-04 13:48:34 +02:00
|
|
|
ASSERT_TRUE(proxy->Send(buffer));
|
2022-05-11 09:35:36 +00:00
|
|
|
EXPECT_FALSE(controller_->last_send_data_params().ordered);
|
2014-01-14 10:00:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tests that an unordered DataChannel sends unordered data after any DATA
|
|
|
|
|
// message is received.
|
|
|
|
|
TEST_F(SctpDataChannelTest, SendUnorderedAfterReceiveData) {
|
|
|
|
|
SetChannelReady();
|
2023-03-12 16:59:25 +01:00
|
|
|
InternalDataChannelInit init;
|
2014-01-14 10:00:58 +00:00
|
|
|
init.id = 1;
|
|
|
|
|
init.ordered = false;
|
2020-07-09 15:32:34 -07:00
|
|
|
rtc::scoped_refptr<SctpDataChannel> dc =
|
2023-03-20 14:43:09 +01:00
|
|
|
controller_->CreateDataChannel("test1", init);
|
2023-04-04 13:48:34 +02:00
|
|
|
auto proxy = webrtc::SctpDataChannel::CreateProxy(dc);
|
2014-01-14 10:00:58 +00:00
|
|
|
|
2023-04-04 13:48:34 +02:00
|
|
|
EXPECT_EQ_WAIT(DataChannelInterface::kOpen, proxy->state(), 1000);
|
2014-01-14 10:00:58 +00:00
|
|
|
|
|
|
|
|
// Emulates receiving a DATA message.
|
2023-03-12 16:59:25 +01:00
|
|
|
DataBuffer buffer("data");
|
2023-04-04 13:48:34 +02:00
|
|
|
network_thread_.BlockingCall(
|
|
|
|
|
[&] { dc->OnDataReceived(DataMessageType::kText, buffer.data); });
|
2014-01-14 10:00:58 +00:00
|
|
|
|
|
|
|
|
// Sends a message and verifies it's unordered.
|
2023-04-04 13:48:34 +02:00
|
|
|
ASSERT_TRUE(proxy->Send(buffer));
|
2022-05-11 09:35:36 +00:00
|
|
|
EXPECT_FALSE(controller_->last_send_data_params().ordered);
|
2014-01-14 10:00:58 +00:00
|
|
|
}
|
|
|
|
|
|
2015-05-29 11:52:39 -04:00
|
|
|
// Tests that the channel can't open until it's successfully sent the OPEN
|
|
|
|
|
// message.
|
|
|
|
|
TEST_F(SctpDataChannelTest, OpenWaitsForOpenMesssage) {
|
2023-03-12 16:59:25 +01:00
|
|
|
DataBuffer buffer("foo");
|
2015-05-29 11:52:39 -04:00
|
|
|
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->set_send_blocked(true);
|
2015-05-29 11:52:39 -04:00
|
|
|
SetChannelReady();
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(DataChannelInterface::kConnecting, channel_->state());
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->set_send_blocked(false);
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ_WAIT(DataChannelInterface::kOpen, channel_->state(), 1000);
|
2023-03-12 16:59:25 +01:00
|
|
|
EXPECT_EQ(DataMessageType::kControl,
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->last_send_data_params().type);
|
2015-05-29 11:52:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tests that close first makes sure all queued data gets sent.
|
|
|
|
|
TEST_F(SctpDataChannelTest, QueuedCloseFlushes) {
|
2023-03-12 16:59:25 +01:00
|
|
|
DataBuffer buffer("foo");
|
2015-05-29 11:52:39 -04:00
|
|
|
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->set_send_blocked(true);
|
2015-05-29 11:52:39 -04:00
|
|
|
SetChannelReady();
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(DataChannelInterface::kConnecting, channel_->state());
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->set_send_blocked(false);
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ_WAIT(DataChannelInterface::kOpen, channel_->state(), 1000);
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->set_send_blocked(true);
|
2023-03-31 11:07:31 +02:00
|
|
|
channel_->Send(buffer);
|
|
|
|
|
channel_->Close();
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->set_send_blocked(false);
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ_WAIT(DataChannelInterface::kClosed, channel_->state(), 1000);
|
|
|
|
|
EXPECT_TRUE(channel_->error().ok());
|
2023-03-12 16:59:25 +01:00
|
|
|
EXPECT_EQ(DataMessageType::kText, controller_->last_send_data_params().type);
|
2015-05-29 11:52:39 -04:00
|
|
|
}
|
|
|
|
|
|
2021-04-16 11:12:14 +00:00
|
|
|
// Tests that messages are sent with the right id.
|
|
|
|
|
TEST_F(SctpDataChannelTest, SendDataId) {
|
2023-03-31 11:07:31 +02:00
|
|
|
SetChannelSid(inner_channel_, StreamId(1));
|
2013-11-13 22:48:52 +00:00
|
|
|
SetChannelReady();
|
2023-03-12 16:59:25 +01:00
|
|
|
DataBuffer buffer("data");
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_TRUE(channel_->Send(buffer));
|
2022-05-11 09:35:36 +00:00
|
|
|
EXPECT_EQ(1, controller_->last_sid());
|
2013-11-13 22:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-16 11:12:14 +00:00
|
|
|
// Tests that the incoming messages with right ids are accepted.
|
|
|
|
|
TEST_F(SctpDataChannelTest, ReceiveDataWithValidId) {
|
2023-03-31 11:07:31 +02:00
|
|
|
SetChannelSid(inner_channel_, StreamId(1));
|
2013-11-13 22:48:52 +00:00
|
|
|
SetChannelReady();
|
|
|
|
|
|
|
|
|
|
AddObserver();
|
|
|
|
|
|
2023-03-12 16:59:25 +01:00
|
|
|
DataBuffer buffer("abcd");
|
2023-04-04 13:48:34 +02:00
|
|
|
network_thread_.BlockingCall([&] {
|
|
|
|
|
inner_channel_->OnDataReceived(DataMessageType::kText, buffer.data);
|
|
|
|
|
});
|
|
|
|
|
run_loop_.Flush();
|
2014-05-29 15:33:54 +00:00
|
|
|
EXPECT_EQ(1U, observer_->messages_received());
|
2013-11-13 22:48:52 +00:00
|
|
|
}
|
2014-01-14 10:00:58 +00:00
|
|
|
|
|
|
|
|
// Tests that no CONTROL message is sent if the datachannel is negotiated and
|
|
|
|
|
// not created from an OPEN message.
|
|
|
|
|
TEST_F(SctpDataChannelTest, NoMsgSentIfNegotiatedAndNotFromOpenMsg) {
|
2023-03-12 16:59:25 +01:00
|
|
|
InternalDataChannelInit config;
|
2014-01-14 10:00:58 +00:00
|
|
|
config.id = 1;
|
|
|
|
|
config.negotiated = true;
|
2023-03-12 16:59:25 +01:00
|
|
|
config.open_handshake_role = InternalDataChannelInit::kNone;
|
2014-01-14 10:00:58 +00:00
|
|
|
|
|
|
|
|
SetChannelReady();
|
2020-07-09 15:32:34 -07:00
|
|
|
rtc::scoped_refptr<SctpDataChannel> dc =
|
2023-03-20 14:43:09 +01:00
|
|
|
controller_->CreateDataChannel("test1", config);
|
2023-04-04 13:48:34 +02:00
|
|
|
auto proxy = webrtc::SctpDataChannel::CreateProxy(dc);
|
2014-01-14 10:00:58 +00:00
|
|
|
|
2023-04-04 13:48:34 +02:00
|
|
|
EXPECT_EQ_WAIT(DataChannelInterface::kOpen, proxy->state(), 1000);
|
2022-05-11 09:35:36 +00:00
|
|
|
EXPECT_EQ(0, controller_->last_sid());
|
2014-01-14 10:00:58 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-12 14:14:39 -07:00
|
|
|
// Tests that DataChannel::messages_received() and DataChannel::bytes_received()
|
|
|
|
|
// are correct, receiving data both while not open and while open.
|
|
|
|
|
TEST_F(SctpDataChannelTest, VerifyMessagesAndBytesReceived) {
|
|
|
|
|
AddObserver();
|
2023-03-12 16:59:25 +01:00
|
|
|
std::vector<DataBuffer> buffers({
|
|
|
|
|
DataBuffer("message 1"),
|
|
|
|
|
DataBuffer("msg 2"),
|
|
|
|
|
DataBuffer("message three"),
|
|
|
|
|
DataBuffer("quadra message"),
|
|
|
|
|
DataBuffer("fifthmsg"),
|
|
|
|
|
DataBuffer("message of the beast"),
|
2016-10-12 14:14:39 -07:00
|
|
|
});
|
|
|
|
|
|
2023-03-31 11:07:31 +02:00
|
|
|
SetChannelSid(inner_channel_, StreamId(1));
|
2016-10-12 14:14:39 -07:00
|
|
|
|
|
|
|
|
// Default values.
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(0U, channel_->messages_received());
|
|
|
|
|
EXPECT_EQ(0U, channel_->bytes_received());
|
2016-10-12 14:14:39 -07:00
|
|
|
|
|
|
|
|
// Receive three buffers while data channel isn't open.
|
2023-04-04 13:48:34 +02:00
|
|
|
network_thread_.BlockingCall([&] {
|
|
|
|
|
for (int i : {0, 1, 2})
|
|
|
|
|
inner_channel_->OnDataReceived(DataMessageType::kText, buffers[i].data);
|
|
|
|
|
});
|
2016-10-12 14:14:39 -07:00
|
|
|
EXPECT_EQ(0U, observer_->messages_received());
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(0U, channel_->messages_received());
|
|
|
|
|
EXPECT_EQ(0U, channel_->bytes_received());
|
2016-10-12 14:14:39 -07:00
|
|
|
|
|
|
|
|
// Open channel and make sure everything was received.
|
|
|
|
|
SetChannelReady();
|
|
|
|
|
size_t bytes_received =
|
|
|
|
|
buffers[0].size() + buffers[1].size() + buffers[2].size();
|
|
|
|
|
EXPECT_EQ(3U, observer_->messages_received());
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(3U, channel_->messages_received());
|
|
|
|
|
EXPECT_EQ(bytes_received, channel_->bytes_received());
|
2016-10-12 14:14:39 -07:00
|
|
|
|
|
|
|
|
// Receive three buffers while open.
|
2023-04-04 13:48:34 +02:00
|
|
|
network_thread_.BlockingCall([&] {
|
|
|
|
|
for (int i : {3, 4, 5})
|
|
|
|
|
inner_channel_->OnDataReceived(DataMessageType::kText, buffers[i].data);
|
|
|
|
|
});
|
|
|
|
|
run_loop_.Flush();
|
2016-10-12 14:14:39 -07:00
|
|
|
bytes_received += buffers[3].size() + buffers[4].size() + buffers[5].size();
|
|
|
|
|
EXPECT_EQ(6U, observer_->messages_received());
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(6U, channel_->messages_received());
|
|
|
|
|
EXPECT_EQ(bytes_received, channel_->bytes_received());
|
2016-10-12 14:14:39 -07:00
|
|
|
}
|
|
|
|
|
|
2014-01-14 10:00:58 +00:00
|
|
|
// Tests that OPEN_ACK message is sent if the datachannel is created from an
|
|
|
|
|
// OPEN message.
|
|
|
|
|
TEST_F(SctpDataChannelTest, OpenAckSentIfCreatedFromOpenMessage) {
|
2023-03-12 16:59:25 +01:00
|
|
|
InternalDataChannelInit config;
|
2014-01-14 10:00:58 +00:00
|
|
|
config.id = 1;
|
|
|
|
|
config.negotiated = true;
|
2023-03-12 16:59:25 +01:00
|
|
|
config.open_handshake_role = InternalDataChannelInit::kAcker;
|
2014-01-14 10:00:58 +00:00
|
|
|
|
|
|
|
|
SetChannelReady();
|
2020-07-09 15:32:34 -07:00
|
|
|
rtc::scoped_refptr<SctpDataChannel> dc =
|
2023-03-20 14:43:09 +01:00
|
|
|
controller_->CreateDataChannel("test1", config);
|
2023-04-04 13:48:34 +02:00
|
|
|
auto proxy = webrtc::SctpDataChannel::CreateProxy(dc);
|
2014-01-14 10:00:58 +00:00
|
|
|
|
2023-04-04 13:48:34 +02:00
|
|
|
EXPECT_EQ_WAIT(DataChannelInterface::kOpen, proxy->state(), 1000);
|
2014-01-14 10:00:58 +00:00
|
|
|
|
2022-05-11 09:35:36 +00:00
|
|
|
EXPECT_EQ(config.id, controller_->last_sid());
|
2023-03-12 16:59:25 +01:00
|
|
|
EXPECT_EQ(DataMessageType::kControl,
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->last_send_data_params().type);
|
2014-01-14 10:00:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tests the OPEN_ACK role assigned by InternalDataChannelInit.
|
|
|
|
|
TEST_F(SctpDataChannelTest, OpenAckRoleInitialization) {
|
2023-03-12 16:59:25 +01:00
|
|
|
InternalDataChannelInit init;
|
|
|
|
|
EXPECT_EQ(InternalDataChannelInit::kOpener, init.open_handshake_role);
|
2014-01-14 10:00:58 +00:00
|
|
|
EXPECT_FALSE(init.negotiated);
|
|
|
|
|
|
2023-03-12 16:59:25 +01:00
|
|
|
DataChannelInit base;
|
2014-01-14 10:00:58 +00:00
|
|
|
base.negotiated = true;
|
2023-03-12 16:59:25 +01:00
|
|
|
InternalDataChannelInit init2(base);
|
|
|
|
|
EXPECT_EQ(InternalDataChannelInit::kNone, init2.open_handshake_role);
|
2014-01-14 10:00:58 +00:00
|
|
|
}
|
2014-05-29 15:33:54 +00:00
|
|
|
|
2021-11-03 16:09:46 +01:00
|
|
|
// Tests that that Send() returns false if the sending buffer is full
|
|
|
|
|
// and the channel stays open.
|
|
|
|
|
TEST_F(SctpDataChannelTest, OpenWhenSendBufferFull) {
|
2014-05-29 15:33:54 +00:00
|
|
|
SetChannelReady();
|
2014-06-20 17:11:14 +00:00
|
|
|
|
2021-11-03 16:09:46 +01:00
|
|
|
const size_t packetSize = 1024;
|
|
|
|
|
|
|
|
|
|
rtc::CopyOnWriteBuffer buffer(packetSize);
|
2021-01-07 15:24:05 +01:00
|
|
|
memset(buffer.MutableData(), 0, buffer.size());
|
2014-06-20 17:11:14 +00:00
|
|
|
|
2023-03-12 16:59:25 +01:00
|
|
|
DataBuffer packet(buffer, true);
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->set_send_blocked(true);
|
2014-05-29 15:33:54 +00:00
|
|
|
|
2023-03-12 16:59:25 +01:00
|
|
|
for (size_t i = 0; i < DataChannelInterface::MaxSendQueueSize() / packetSize;
|
|
|
|
|
++i) {
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_TRUE(channel_->Send(packet));
|
2014-05-29 15:33:54 +00:00
|
|
|
}
|
|
|
|
|
|
2023-04-04 13:48:34 +02:00
|
|
|
// The sending buffer should be full, `Send()` returns false.
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_FALSE(channel_->Send(packet));
|
|
|
|
|
EXPECT_TRUE(DataChannelInterface::kOpen == channel_->state());
|
2014-05-29 15:33:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tests that the DataChannel is closed on transport errors.
|
|
|
|
|
TEST_F(SctpDataChannelTest, ClosedOnTransportError) {
|
|
|
|
|
SetChannelReady();
|
2023-03-12 16:59:25 +01:00
|
|
|
DataBuffer buffer("abcd");
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->set_transport_error();
|
2014-05-29 15:33:54 +00:00
|
|
|
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_TRUE(channel_->Send(buffer));
|
2014-05-29 15:33:54 +00:00
|
|
|
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(DataChannelInterface::kClosed, channel_->state());
|
|
|
|
|
EXPECT_FALSE(channel_->error().ok());
|
|
|
|
|
EXPECT_EQ(RTCErrorType::NETWORK_ERROR, channel_->error().type());
|
|
|
|
|
EXPECT_EQ(RTCErrorDetailType::NONE, channel_->error().error_detail());
|
2014-05-29 15:33:54 +00:00
|
|
|
}
|
2014-05-30 21:53:17 +00:00
|
|
|
|
2014-06-20 17:11:14 +00:00
|
|
|
// Tests that the DataChannel is closed if the received buffer is full.
|
|
|
|
|
TEST_F(SctpDataChannelTest, ClosedWhenReceivedBufferFull) {
|
|
|
|
|
SetChannelReady();
|
2016-03-20 06:15:43 -07:00
|
|
|
rtc::CopyOnWriteBuffer buffer(1024);
|
2021-01-07 15:24:05 +01:00
|
|
|
memset(buffer.MutableData(), 0, buffer.size());
|
2014-06-20 17:11:14 +00:00
|
|
|
|
2023-04-04 13:48:34 +02:00
|
|
|
network_thread_.BlockingCall([&] {
|
|
|
|
|
// Receiving data without having an observer will overflow the buffer.
|
|
|
|
|
for (size_t i = 0; i < 16 * 1024 + 1; ++i) {
|
|
|
|
|
inner_channel_->OnDataReceived(DataMessageType::kText, buffer);
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(DataChannelInterface::kClosed, channel_->state());
|
|
|
|
|
EXPECT_FALSE(channel_->error().ok());
|
|
|
|
|
EXPECT_EQ(RTCErrorType::RESOURCE_EXHAUSTED, channel_->error().type());
|
|
|
|
|
EXPECT_EQ(RTCErrorDetailType::NONE, channel_->error().error_detail());
|
2014-06-20 17:11:14 +00:00
|
|
|
}
|
2014-07-18 23:57:50 +00:00
|
|
|
|
|
|
|
|
// Tests that sending empty data returns no error and keeps the channel open.
|
|
|
|
|
TEST_F(SctpDataChannelTest, SendEmptyData) {
|
2023-03-31 11:07:31 +02:00
|
|
|
SetChannelSid(inner_channel_, StreamId(1));
|
2014-07-18 23:57:50 +00:00
|
|
|
SetChannelReady();
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ(DataChannelInterface::kOpen, channel_->state());
|
2014-07-18 23:57:50 +00:00
|
|
|
|
2023-03-12 16:59:25 +01:00
|
|
|
DataBuffer buffer("");
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_TRUE(channel_->Send(buffer));
|
|
|
|
|
EXPECT_EQ(DataChannelInterface::kOpen, channel_->state());
|
2014-07-18 23:57:50 +00:00
|
|
|
}
|
2014-12-04 23:16:52 +00:00
|
|
|
|
|
|
|
|
// Tests that a channel can be closed without being opened or assigned an sid.
|
|
|
|
|
TEST_F(SctpDataChannelTest, NeverOpened) {
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->set_transport_available(true);
|
2023-04-04 13:48:34 +02:00
|
|
|
network_thread_.BlockingCall(
|
|
|
|
|
[&] { inner_channel_->OnTransportChannelCreated(); });
|
2023-03-31 11:07:31 +02:00
|
|
|
channel_->Close();
|
2014-12-04 23:16:52 +00:00
|
|
|
}
|
2015-10-14 11:33:11 -07:00
|
|
|
|
2023-03-07 08:43:24 +01:00
|
|
|
// Tests that a data channel that's not connected to a transport can transition
|
|
|
|
|
// directly to the `kClosed` state when closed.
|
|
|
|
|
// See also chromium:1421534.
|
|
|
|
|
TEST_F(SctpDataChannelTest, UnusedTransitionsDirectlyToClosed) {
|
2023-03-31 11:07:31 +02:00
|
|
|
channel_->Close();
|
|
|
|
|
EXPECT_EQ(DataChannelInterface::kClosed, channel_->state());
|
2023-03-07 08:43:24 +01:00
|
|
|
}
|
|
|
|
|
|
2016-08-12 10:10:31 -07:00
|
|
|
// Test that the data channel goes to the "closed" state (and doesn't crash)
|
|
|
|
|
// when its transport goes away, even while data is buffered.
|
|
|
|
|
TEST_F(SctpDataChannelTest, TransportDestroyedWhileDataBuffered) {
|
|
|
|
|
SetChannelReady();
|
|
|
|
|
|
|
|
|
|
rtc::CopyOnWriteBuffer buffer(1024);
|
2021-01-07 15:24:05 +01:00
|
|
|
memset(buffer.MutableData(), 0, buffer.size());
|
2023-03-12 16:59:25 +01:00
|
|
|
DataBuffer packet(buffer, true);
|
2016-08-12 10:10:31 -07:00
|
|
|
|
|
|
|
|
// Send a packet while sending is blocked so it ends up buffered.
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_->set_send_blocked(true);
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_TRUE(channel_->Send(packet));
|
2016-08-12 10:10:31 -07:00
|
|
|
|
2019-11-16 12:09:08 +01:00
|
|
|
// Tell the data channel that its transport is being destroyed.
|
2016-08-12 10:10:31 -07:00
|
|
|
// It should then stop using the transport (allowing us to delete it) and
|
|
|
|
|
// transition to the "closed" state.
|
2023-03-12 16:59:25 +01:00
|
|
|
RTCError error(RTCErrorType::OPERATION_ERROR_WITH_DATA, "");
|
|
|
|
|
error.set_error_detail(RTCErrorDetailType::SCTP_FAILURE);
|
2023-04-04 13:48:34 +02:00
|
|
|
network_thread_.BlockingCall(
|
|
|
|
|
[&] { inner_channel_->OnTransportChannelClosed(error); });
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_.reset(nullptr);
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ_WAIT(DataChannelInterface::kClosed, channel_->state(),
|
2023-03-12 16:59:25 +01:00
|
|
|
kDefaultTimeout);
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_FALSE(channel_->error().ok());
|
|
|
|
|
EXPECT_EQ(RTCErrorType::OPERATION_ERROR_WITH_DATA, channel_->error().type());
|
|
|
|
|
EXPECT_EQ(RTCErrorDetailType::SCTP_FAILURE, channel_->error().error_detail());
|
2016-08-12 10:10:31 -07:00
|
|
|
}
|
|
|
|
|
|
2021-06-29 14:58:23 +02:00
|
|
|
TEST_F(SctpDataChannelTest, TransportGotErrorCode) {
|
|
|
|
|
SetChannelReady();
|
|
|
|
|
|
|
|
|
|
// Tell the data channel that its transport is being destroyed with an
|
|
|
|
|
// error code.
|
|
|
|
|
// It should then report that error code.
|
2023-03-12 16:59:25 +01:00
|
|
|
RTCError error(RTCErrorType::OPERATION_ERROR_WITH_DATA,
|
|
|
|
|
"Transport channel closed");
|
|
|
|
|
error.set_error_detail(RTCErrorDetailType::SCTP_FAILURE);
|
2021-06-29 14:58:23 +02:00
|
|
|
error.set_sctp_cause_code(
|
|
|
|
|
static_cast<uint16_t>(cricket::SctpErrorCauseCode::kProtocolViolation));
|
2023-04-04 13:48:34 +02:00
|
|
|
network_thread_.BlockingCall(
|
|
|
|
|
[&] { inner_channel_->OnTransportChannelClosed(error); });
|
2022-05-11 09:35:36 +00:00
|
|
|
controller_.reset(nullptr);
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_EQ_WAIT(DataChannelInterface::kClosed, channel_->state(),
|
2023-03-12 16:59:25 +01:00
|
|
|
kDefaultTimeout);
|
2023-03-31 11:07:31 +02:00
|
|
|
EXPECT_FALSE(channel_->error().ok());
|
|
|
|
|
EXPECT_EQ(RTCErrorType::OPERATION_ERROR_WITH_DATA, channel_->error().type());
|
|
|
|
|
EXPECT_EQ(RTCErrorDetailType::SCTP_FAILURE, channel_->error().error_detail());
|
2021-06-29 14:58:23 +02:00
|
|
|
EXPECT_EQ(
|
|
|
|
|
static_cast<uint16_t>(cricket::SctpErrorCauseCode::kProtocolViolation),
|
2023-03-31 11:07:31 +02:00
|
|
|
channel_->error().sctp_cause_code());
|
2021-06-29 14:58:23 +02:00
|
|
|
}
|
|
|
|
|
|
2019-04-09 15:11:12 +02:00
|
|
|
class SctpSidAllocatorTest : public ::testing::Test {
|
2015-10-14 11:33:11 -07:00
|
|
|
protected:
|
|
|
|
|
SctpSidAllocator allocator_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Verifies that an even SCTP id is allocated for SSL_CLIENT and an odd id for
|
|
|
|
|
// SSL_SERVER.
|
|
|
|
|
TEST_F(SctpSidAllocatorTest, SctpIdAllocationBasedOnRole) {
|
2023-03-21 18:45:24 +01:00
|
|
|
EXPECT_EQ(allocator_.AllocateSid(rtc::SSL_SERVER), StreamId(1));
|
|
|
|
|
EXPECT_EQ(allocator_.AllocateSid(rtc::SSL_CLIENT), StreamId(0));
|
|
|
|
|
EXPECT_EQ(allocator_.AllocateSid(rtc::SSL_SERVER), StreamId(3));
|
|
|
|
|
EXPECT_EQ(allocator_.AllocateSid(rtc::SSL_CLIENT), StreamId(2));
|
2015-10-14 11:33:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Verifies that SCTP ids of existing DataChannels are not reused.
|
|
|
|
|
TEST_F(SctpSidAllocatorTest, SctpIdAllocationNoReuse) {
|
2023-03-12 16:59:25 +01:00
|
|
|
StreamId old_id(1);
|
2015-10-14 11:33:11 -07:00
|
|
|
EXPECT_TRUE(allocator_.ReserveSid(old_id));
|
|
|
|
|
|
2023-03-21 18:45:24 +01:00
|
|
|
StreamId new_id = allocator_.AllocateSid(rtc::SSL_SERVER);
|
|
|
|
|
EXPECT_TRUE(new_id.HasValue());
|
2015-10-14 11:33:11 -07:00
|
|
|
EXPECT_NE(old_id, new_id);
|
|
|
|
|
|
2023-03-12 16:59:25 +01:00
|
|
|
old_id = StreamId(0);
|
2015-10-14 11:33:11 -07:00
|
|
|
EXPECT_TRUE(allocator_.ReserveSid(old_id));
|
2023-03-21 18:45:24 +01:00
|
|
|
new_id = allocator_.AllocateSid(rtc::SSL_CLIENT);
|
|
|
|
|
EXPECT_TRUE(new_id.HasValue());
|
2015-10-14 11:33:11 -07:00
|
|
|
EXPECT_NE(old_id, new_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Verifies that SCTP ids of removed DataChannels can be reused.
|
|
|
|
|
TEST_F(SctpSidAllocatorTest, SctpIdReusedForRemovedDataChannel) {
|
2023-03-12 16:59:25 +01:00
|
|
|
StreamId odd_id(1);
|
|
|
|
|
StreamId even_id(0);
|
2015-10-14 11:33:11 -07:00
|
|
|
EXPECT_TRUE(allocator_.ReserveSid(odd_id));
|
|
|
|
|
EXPECT_TRUE(allocator_.ReserveSid(even_id));
|
|
|
|
|
|
2023-03-21 18:45:24 +01:00
|
|
|
StreamId allocated_id = allocator_.AllocateSid(rtc::SSL_SERVER);
|
2023-03-12 16:59:25 +01:00
|
|
|
EXPECT_EQ(odd_id.stream_id_int() + 2, allocated_id.stream_id_int());
|
2015-10-14 11:33:11 -07:00
|
|
|
|
2023-03-21 18:45:24 +01:00
|
|
|
allocated_id = allocator_.AllocateSid(rtc::SSL_CLIENT);
|
2023-03-12 16:59:25 +01:00
|
|
|
EXPECT_EQ(even_id.stream_id_int() + 2, allocated_id.stream_id_int());
|
2015-10-14 11:33:11 -07:00
|
|
|
|
2023-03-21 18:45:24 +01:00
|
|
|
allocated_id = allocator_.AllocateSid(rtc::SSL_SERVER);
|
2023-03-12 16:59:25 +01:00
|
|
|
EXPECT_EQ(odd_id.stream_id_int() + 4, allocated_id.stream_id_int());
|
2015-10-14 11:33:11 -07:00
|
|
|
|
2023-03-21 18:45:24 +01:00
|
|
|
allocated_id = allocator_.AllocateSid(rtc::SSL_CLIENT);
|
2023-03-12 16:59:25 +01:00
|
|
|
EXPECT_EQ(even_id.stream_id_int() + 4, allocated_id.stream_id_int());
|
2015-10-14 11:33:11 -07:00
|
|
|
|
|
|
|
|
allocator_.ReleaseSid(odd_id);
|
|
|
|
|
allocator_.ReleaseSid(even_id);
|
|
|
|
|
|
|
|
|
|
// Verifies that removed ids are reused.
|
2023-03-21 18:45:24 +01:00
|
|
|
allocated_id = allocator_.AllocateSid(rtc::SSL_SERVER);
|
2015-10-14 11:33:11 -07:00
|
|
|
EXPECT_EQ(odd_id, allocated_id);
|
|
|
|
|
|
2023-03-21 18:45:24 +01:00
|
|
|
allocated_id = allocator_.AllocateSid(rtc::SSL_CLIENT);
|
2015-10-14 11:33:11 -07:00
|
|
|
EXPECT_EQ(even_id, allocated_id);
|
|
|
|
|
|
|
|
|
|
// Verifies that used higher ids are not reused.
|
2023-03-21 18:45:24 +01:00
|
|
|
allocated_id = allocator_.AllocateSid(rtc::SSL_SERVER);
|
2023-03-12 16:59:25 +01:00
|
|
|
EXPECT_EQ(odd_id.stream_id_int() + 6, allocated_id.stream_id_int());
|
2015-10-14 11:33:11 -07:00
|
|
|
|
2023-03-21 18:45:24 +01:00
|
|
|
allocated_id = allocator_.AllocateSid(rtc::SSL_CLIENT);
|
2023-03-12 16:59:25 +01:00
|
|
|
EXPECT_EQ(even_id.stream_id_int() + 6, allocated_id.stream_id_int());
|
2015-10-14 11:33:11 -07:00
|
|
|
}
|
2023-03-12 16:59:25 +01:00
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
} // namespace webrtc
|