/* * Copyright 2013 The WebRTC project authors. All Rights Reserved. * * 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.
*/
// 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) {
network_thread_.BlockingCall([&]() {
channel->SetSctpSid_n(sid);
controller_->AddSctpDataStream(sid, channel->priority());
});
}
// Wait for queued up methods to run on the network thread. void FlushNetworkThread() {
RTC_DCHECK_RUN_ON(run_loop_.task_queue());
network_thread_.BlockingCall([] {});
}
// Used to complete pending methods on the network thread // that might queue up methods on the signaling (main) thread // that are run too. void FlushNetworkThreadAndPendingOperations() {
FlushNetworkThread();
run_loop_.Flush();
}
// 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);
EXPECT_EQ(channel_->reliable(), init_.reliable);
EXPECT_EQ(channel_->ordered(), init_.ordered);
EXPECT_EQ(channel_->negotiated(), init_.negotiated);
EXPECT_EQ(channel_->priority(), PriorityValue(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);
// Check the non-const part of the configuration.
EXPECT_EQ(channel_->id(), init_.id);
network_thread_.BlockingCall(
[&]() { EXPECT_EQ(inner_channel_->sid_n(), std::nullopt); });
// Verifies that the data channel is connected to the transport after creation.
TEST_F(SctpDataChannelTest, ConnectedToTransportOnCreated) {
controller_->set_transport_available(true);
rtc::scoped_refptr<SctpDataChannel> dc =
controller_->CreateDataChannel("test1", init_);
EXPECT_TRUE(controller_->IsConnected(dc.get()));
// The sid is not set yet, so it should not have added the streams.
std::optional<StreamId> sid =
network_thread_.BlockingCall([&]() { return dc->sid_n(); });
EXPECT_FALSE(sid.has_value());
// `Close()` should trigger two state changes, first `kClosing`, then // `kClose`.
channel_->Close(); // The (simulated) transport close notifications runs on the network thread // and posts a completion notification to the signaling (current) thread. // Allow that operation to complete before checking the state.
run_loop_.Flush();
EXPECT_EQ(DataChannelInterface::kClosed, channel_->state());
EXPECT_EQ(observer_->on_state_change_count(), 3u);
EXPECT_TRUE(channel_->error().ok()); // Verifies that it's disconnected from the transport.
EXPECT_FALSE(controller_->IsConnected(inner_channel_.get()));
}
// Tests that the queued control message is sent when channel is ready.
TEST_F(SctpDataChannelTest, OpenMessageSent) { // Initially the id is unassigned.
EXPECT_EQ(-1, channel_->id());
// Tests that the DataChannel created after transport gets ready can enter OPEN // state.
TEST_F(SctpDataChannelTest, LateCreatedChannelTransitionToOpen) {
SetChannelReady();
InternalDataChannelInit init;
init.id = 1; auto dc = SctpDataChannel::CreateProxy(
controller_->CreateDataChannel("test1", init), signaling_safety_);
EXPECT_EQ(DataChannelInterface::kOpen, dc->state());
}
// Tests that an unordered DataChannel sends data as ordered until the OPEN_ACK // message is received.
TEST_F(SctpDataChannelTest, SendUnorderedAfterReceivesOpenAck) {
SetChannelReady();
InternalDataChannelInit init;
init.id = 1;
init.ordered = false;
rtc::scoped_refptr<SctpDataChannel> dc =
controller_->CreateDataChannel("test1", init); auto proxy = SctpDataChannel::CreateProxy(dc, signaling_safety_);
// Sends another message and verifies it's unordered.
ASSERT_TRUE(proxy->Send(buffer));
EXPECT_FALSE(controller_->last_send_data_params().ordered);
}
// Tests that an unordered DataChannel sends unordered data after any DATA // message is received.
TEST_F(SctpDataChannelTest, SendUnorderedAfterReceiveData) {
SetChannelReady();
InternalDataChannelInit init;
init.id = 1;
init.ordered = false;
rtc::scoped_refptr<SctpDataChannel> dc =
controller_->CreateDataChannel("test1", init); auto proxy = SctpDataChannel::CreateProxy(dc, signaling_safety_);
// Emulates receiving a DATA message.
DataBuffer buffer("data");
network_thread_.BlockingCall(
[&] { dc->OnDataReceived(DataMessageType::kText, buffer.data); });
// Sends a message and verifies it's unordered.
ASSERT_TRUE(proxy->Send(buffer));
EXPECT_FALSE(controller_->last_send_data_params().ordered);
}
// Tests that messages are sent with the right id.
TEST_F(SctpDataChannelTest, SendDataId) {
SetChannelSid(inner_channel_, StreamId(1));
SetChannelReady();
DataBuffer buffer("data");
channel_->SendAsync(buffer, nullptr);
FlushNetworkThreadAndPendingOperations();
EXPECT_EQ(1, controller_->last_sid());
}
// TODO(tommi): This test uses `Send()`. Remove once fully deprecated.
TEST_F(SctpDataChannelTest, DeprecatedSendDataId) {
SetChannelSid(inner_channel_, StreamId(1));
SetChannelReady();
DataBuffer buffer("data");
EXPECT_TRUE(channel_->Send(buffer));
EXPECT_EQ(1, controller_->last_sid());
}
// Tests that the incoming messages with right ids are accepted.
TEST_F(SctpDataChannelTest, ReceiveDataWithValidId) {
SetChannelSid(inner_channel_, StreamId(1));
SetChannelReady();
// Tests that no CONTROL message is sent if the datachannel is negotiated and // not created from an OPEN message.
TEST_F(SctpDataChannelTest, NoMsgSentIfNegotiatedAndNotFromOpenMsg) {
InternalDataChannelInit config;
config.id = 1;
config.negotiated = true;
config.open_handshake_role = InternalDataChannelInit::kNone;
SetChannelReady();
rtc::scoped_refptr<SctpDataChannel> dc =
controller_->CreateDataChannel("test1", config); auto proxy = SctpDataChannel::CreateProxy(dc, signaling_safety_);
// 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();
std::vector<DataBuffer> buffers({
DataBuffer("message 1"),
DataBuffer("msg 2"),
DataBuffer("message three"),
DataBuffer("quadra message"),
DataBuffer("fifthmsg"),
DataBuffer("message of the beast"),
});
// Receive three buffers while data channel isn't open.
network_thread_.BlockingCall([&] { for (int i : {0, 1, 2})
inner_channel_->OnDataReceived(DataMessageType::kText, buffers[i].data);
});
EXPECT_EQ(0U, observer_->messages_received());
EXPECT_EQ(0U, channel_->messages_received());
EXPECT_EQ(0U, channel_->bytes_received());
// 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());
EXPECT_EQ(3U, channel_->messages_received());
EXPECT_EQ(bytes_received, channel_->bytes_received());
// Receive three buffers while open.
network_thread_.BlockingCall([&] { for (int i : {3, 4, 5})
inner_channel_->OnDataReceived(DataMessageType::kText, buffers[i].data);
});
run_loop_.Flush();
bytes_received += buffers[3].size() + buffers[4].size() + buffers[5].size();
EXPECT_EQ(6U, observer_->messages_received());
EXPECT_EQ(6U, channel_->messages_received());
EXPECT_EQ(bytes_received, channel_->bytes_received());
}
// Tests that OPEN_ACK message is sent if the datachannel is created from an // OPEN message.
TEST_F(SctpDataChannelTest, OpenAckSentIfCreatedFromOpenMessage) {
InternalDataChannelInit config;
config.id = 1;
config.negotiated = true;
config.open_handshake_role = InternalDataChannelInit::kAcker;
SetChannelReady();
rtc::scoped_refptr<SctpDataChannel> dc =
controller_->CreateDataChannel("test1", config); auto proxy = SctpDataChannel::CreateProxy(dc, signaling_safety_);
// Tests that the DataChannel is closed on transport errors.
TEST_F(SctpDataChannelTest, ClosedOnTransportError) {
SetChannelReady();
DataBuffer buffer("abcd");
controller_->set_transport_error();
// Tests that the DataChannel is closed if the received buffer is full.
TEST_F(SctpDataChannelTest, ClosedWhenReceivedBufferFull) {
SetChannelReady();
rtc::CopyOnWriteBuffer buffer(1024);
memset(buffer.MutableData(), 0, buffer.size());
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);
}
});
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());
}
// Tests that sending empty data returns no error and keeps the channel open.
TEST_F(SctpDataChannelTest, SendEmptyData) {
SetChannelSid(inner_channel_, StreamId(1));
SetChannelReady();
EXPECT_EQ(DataChannelInterface::kOpen, channel_->state());
// Tests that a channel can be closed without being opened or assigned an sid.
TEST_F(SctpDataChannelTest, NeverOpened) {
controller_->set_transport_available(true);
network_thread_.BlockingCall(
[&] { inner_channel_->OnTransportChannelCreated(); });
channel_->Close();
}
// 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) {
channel_->Close();
EXPECT_EQ(DataChannelInterface::kClosed, channel_->state());
}
// 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) {
AddObserver();
SetChannelReady();
// Send a very large packet, forcing the message to become buffered.
channel_->SendAsync(packet, nullptr);
// Tell the data channel that its transport is being destroyed. // It should then stop using the transport (allowing us to delete it) and // transition to the "closed" state.
RTCError error(RTCErrorType::OPERATION_ERROR_WITH_DATA, "");
error.set_error_detail(RTCErrorDetailType::SCTP_FAILURE);
network_thread_.BlockingCall(
[&] { inner_channel_->OnTransportChannelClosed(error); });
controller_.reset(nullptr);
EXPECT_EQ_WAIT(DataChannelInterface::kClosed, channel_->state(),
kDefaultTimeout);
EXPECT_FALSE(channel_->error().ok());
EXPECT_EQ(RTCErrorType::OPERATION_ERROR_WITH_DATA, channel_->error().type());
EXPECT_EQ(RTCErrorDetailType::SCTP_FAILURE, channel_->error().error_detail());
}
// Tell the data channel that its transport is being destroyed with an // error code. // It should then report that error code.
RTCError error(RTCErrorType::OPERATION_ERROR_WITH_DATA, "Transport channel closed");
error.set_error_detail(RTCErrorDetailType::SCTP_FAILURE);
error.set_sctp_cause_code( static_cast<uint16_t>(cricket::SctpErrorCauseCode::kProtocolViolation));
network_thread_.BlockingCall(
[&] { inner_channel_->OnTransportChannelClosed(error); });
controller_.reset(nullptr);
EXPECT_EQ_WAIT(DataChannelInterface::kClosed, channel_->state(),
kDefaultTimeout);
EXPECT_FALSE(channel_->error().ok());
EXPECT_EQ(RTCErrorType::OPERATION_ERROR_WITH_DATA, channel_->error().type());
EXPECT_EQ(RTCErrorDetailType::SCTP_FAILURE, channel_->error().error_detail());
EXPECT_EQ( static_cast<uint16_t>(cricket::SctpErrorCauseCode::kProtocolViolation),
channel_->error().sctp_cause_code());
}
class SctpSidAllocatorTest : public ::testing::Test { 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) {
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));
}
// Verifies that SCTP ids of existing DataChannels are not reused.
TEST_F(SctpSidAllocatorTest, SctpIdAllocationNoReuse) {
StreamId old_id(1);
EXPECT_TRUE(allocator_.ReserveSid(old_id));
// Verifies that SCTP ids of removed DataChannels can be reused.
TEST_F(SctpSidAllocatorTest, SctpIdReusedForRemovedDataChannel) {
StreamId odd_id(1);
StreamId even_id(0);
EXPECT_TRUE(allocator_.ReserveSid(odd_id));
EXPECT_TRUE(allocator_.ReserveSid(even_id));
// Verifies that used higher ids are not reused.
allocated_id = allocator_.AllocateSid(rtc::SSL_SERVER);
EXPECT_EQ(odd_id.stream_id_int() + 6, allocated_id->stream_id_int());
// Code coverage tests for default implementations in data_channel_interface.*. namespace { class NoImplDataChannel : public DataChannelInterface { public:
NoImplDataChannel() = default; // Send and SendAsync implementations are public and implementation // is in data_channel_interface.cc.
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.