2019-02-28 07:51:00 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright 2019 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "pc/sctp_transport.h"
|
|
|
|
|
|
2024-08-29 13:00:40 +00:00
|
|
|
#include <optional>
|
2019-02-28 07:51:00 +01:00
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#include "absl/memory/memory.h"
|
2021-05-21 20:46:09 +02:00
|
|
|
#include "api/dtls_transport_interface.h"
|
2024-07-26 16:16:41 +00:00
|
|
|
#include "api/priority.h"
|
2022-02-23 13:44:59 +00:00
|
|
|
#include "api/transport/data_channel_transport_interface.h"
|
|
|
|
|
#include "media/base/media_channel.h"
|
2019-02-28 07:51:00 +01:00
|
|
|
#include "p2p/base/fake_dtls_transport.h"
|
2022-02-23 13:44:59 +00:00
|
|
|
#include "p2p/base/p2p_constants.h"
|
|
|
|
|
#include "p2p/base/packet_transport_internal.h"
|
2019-02-28 07:51:00 +01:00
|
|
|
#include "pc/dtls_transport.h"
|
2022-02-23 13:44:59 +00:00
|
|
|
#include "rtc_base/copy_on_write_buffer.h"
|
2019-02-28 07:51:00 +01:00
|
|
|
#include "rtc_base/gunit.h"
|
|
|
|
|
#include "test/gmock.h"
|
|
|
|
|
#include "test/gtest.h"
|
|
|
|
|
|
|
|
|
|
constexpr int kDefaultTimeout = 1000; // milliseconds
|
2019-05-21 10:52:59 +02:00
|
|
|
constexpr int kTestMaxSctpStreams = 1234;
|
2019-02-28 07:51:00 +01:00
|
|
|
|
|
|
|
|
using cricket::FakeDtlsTransport;
|
|
|
|
|
using ::testing::ElementsAre;
|
|
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
class FakeCricketSctpTransport : public cricket::SctpTransportInternal {
|
|
|
|
|
public:
|
2022-08-22 11:34:29 +02:00
|
|
|
void SetOnConnectedCallback(std::function<void()> callback) override {
|
|
|
|
|
on_connected_callback_ = std::move(callback);
|
|
|
|
|
}
|
|
|
|
|
void SetDataChannelSink(DataChannelSink* sink) override {}
|
2019-02-28 07:51:00 +01:00
|
|
|
void SetDtlsTransport(rtc::PacketTransportInternal* transport) override {}
|
2019-05-15 08:07:47 +02:00
|
|
|
bool Start(int local_port, int remote_port, int max_message_size) override {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-07-26 16:16:41 +00:00
|
|
|
bool OpenStream(int sid, PriorityValue priority) override { return true; }
|
2019-02-28 07:51:00 +01:00
|
|
|
bool ResetStream(int sid) override { return true; }
|
2023-03-21 14:48:51 +01:00
|
|
|
RTCError SendData(int sid,
|
|
|
|
|
const SendDataParams& params,
|
|
|
|
|
const rtc::CopyOnWriteBuffer& payload) override {
|
|
|
|
|
return RTCError::OK();
|
2019-02-28 07:51:00 +01:00
|
|
|
}
|
|
|
|
|
bool ReadyToSendData() override { return true; }
|
|
|
|
|
void set_debug_name_for_testing(const char* debug_name) override {}
|
2019-05-15 08:07:47 +02:00
|
|
|
int max_message_size() const override { return 0; }
|
2024-08-29 13:00:40 +00:00
|
|
|
std::optional<int> max_outbound_streams() const override {
|
2019-05-21 10:52:59 +02:00
|
|
|
return max_outbound_streams_;
|
|
|
|
|
}
|
2024-08-29 13:00:40 +00:00
|
|
|
std::optional<int> max_inbound_streams() const override {
|
2019-05-21 10:52:59 +02:00
|
|
|
return max_inbound_streams_;
|
|
|
|
|
}
|
2024-03-11 16:43:31 +01:00
|
|
|
size_t buffered_amount(int sid) const override { return 0; }
|
2024-03-18 13:47:34 +01:00
|
|
|
size_t buffered_amount_low_threshold(int sid) const override { return 0; }
|
|
|
|
|
void SetBufferedAmountLowThreshold(int sid, size_t bytes) override {}
|
2019-02-28 07:51:00 +01:00
|
|
|
|
2019-05-21 10:52:59 +02:00
|
|
|
void SendSignalAssociationChangeCommunicationUp() {
|
2022-08-22 11:34:29 +02:00
|
|
|
ASSERT_TRUE(on_connected_callback_);
|
|
|
|
|
on_connected_callback_();
|
2019-02-28 07:51:00 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-21 10:52:59 +02:00
|
|
|
void set_max_outbound_streams(int streams) {
|
|
|
|
|
max_outbound_streams_ = streams;
|
|
|
|
|
}
|
|
|
|
|
void set_max_inbound_streams(int streams) { max_inbound_streams_ = streams; }
|
|
|
|
|
|
|
|
|
|
private:
|
2024-08-29 13:00:40 +00:00
|
|
|
std::optional<int> max_outbound_streams_;
|
|
|
|
|
std::optional<int> max_inbound_streams_;
|
2022-08-22 11:34:29 +02:00
|
|
|
std::function<void()> on_connected_callback_;
|
2019-02-28 07:51:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
class TestSctpTransportObserver : public SctpTransportObserverInterface {
|
|
|
|
|
public:
|
2019-05-21 10:52:59 +02:00
|
|
|
TestSctpTransportObserver() : info_(SctpTransportState::kNew) {}
|
|
|
|
|
|
2019-02-28 07:51:00 +01:00
|
|
|
void OnStateChange(SctpTransportInformation info) override {
|
2019-05-21 10:52:59 +02:00
|
|
|
info_ = info;
|
2019-02-28 07:51:00 +01:00
|
|
|
states_.push_back(info.state());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SctpTransportState State() {
|
|
|
|
|
if (states_.size() > 0) {
|
|
|
|
|
return states_[states_.size() - 1];
|
|
|
|
|
} else {
|
|
|
|
|
return SctpTransportState::kNew;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const std::vector<SctpTransportState>& States() { return states_; }
|
|
|
|
|
|
2019-05-21 10:52:59 +02:00
|
|
|
const SctpTransportInformation LastReceivedInformation() { return info_; }
|
|
|
|
|
|
2019-02-28 07:51:00 +01:00
|
|
|
private:
|
|
|
|
|
std::vector<SctpTransportState> states_;
|
2019-05-21 10:52:59 +02:00
|
|
|
SctpTransportInformation info_;
|
2019-02-28 07:51:00 +01:00
|
|
|
};
|
|
|
|
|
|
2019-04-09 15:11:12 +02:00
|
|
|
class SctpTransportTest : public ::testing::Test {
|
2019-02-28 07:51:00 +01:00
|
|
|
public:
|
|
|
|
|
SctpTransport* transport() { return transport_.get(); }
|
|
|
|
|
SctpTransportObserverInterface* observer() { return &observer_; }
|
|
|
|
|
|
|
|
|
|
void CreateTransport() {
|
|
|
|
|
std::unique_ptr<cricket::DtlsTransportInternal> cricket_transport =
|
2019-09-17 17:06:18 +02:00
|
|
|
std::make_unique<FakeDtlsTransport>(
|
2019-02-28 07:51:00 +01:00
|
|
|
"audio", cricket::ICE_CANDIDATE_COMPONENT_RTP);
|
|
|
|
|
dtls_transport_ =
|
2021-04-27 14:43:08 +02:00
|
|
|
rtc::make_ref_counted<DtlsTransport>(std::move(cricket_transport));
|
pc: Provide DtlsTransport to SctpTransport constr
This code looked a bit weird before this CL - probably because of old
refactorings.
In JsepTransport constructor, there is a DCHECK assuring that the RTP
DTLS transport is always present, so it can be passed directly to the
SctpTransport constructor, which avoids having the SetDtlsTransport
method in it.
Also, in the SctpTransport constructor, there was code that would set
the SCTP transport state to `kConnecting` if the DTLS transport was
present, but that was dead code, as it was always `nullptr` inside the
constructor before this CL. With this CL, it's always present, and the
SCTP Transport's state will initially always be `kConnecting` now. Which
is a step to deprecating the `kNew` state that doesn't exist in
https://w3c.github.io/webrtc-pc/#dom-rtcsctptransportstate.
One test case was modified, as it didn't test the reality. The test
created a SctpTransport, registered an observer, and added the DTLS
transport, and expected to receive a "statechange" from `kNew` (which is
not a state that exists in the spec) to `kConnecting`. If the test had
tested the opposite ordering - adding the DTLS transport first, and then
adding an observer, it wouldn't have experienced this. And since in
reality (with the implementation of JsepTransport before and
after this CL), it always adds the DTLS transport before any observer is
registered. So it wouldn't ever be fired, outside of tests.
Bug: webrtc:15897
Change-Id: I6ac24e0a331b686eb400fcf388ece50f2ad46a32
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/345420
Commit-Queue: Victor Boivie <boivie@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#41987}
2024-04-02 10:42:43 +02:00
|
|
|
|
|
|
|
|
auto cricket_sctp_transport =
|
|
|
|
|
absl::WrapUnique(new FakeCricketSctpTransport());
|
|
|
|
|
transport_ = rtc::make_ref_counted<SctpTransport>(
|
|
|
|
|
std::move(cricket_sctp_transport), dtls_transport_);
|
2019-02-28 07:51:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CompleteSctpHandshake() {
|
2019-05-21 10:52:59 +02:00
|
|
|
// The computed MaxChannels shall be the minimum of the outgoing
|
|
|
|
|
// and incoming # of streams.
|
|
|
|
|
CricketSctpTransport()->set_max_outbound_streams(kTestMaxSctpStreams);
|
|
|
|
|
CricketSctpTransport()->set_max_inbound_streams(kTestMaxSctpStreams + 1);
|
|
|
|
|
CricketSctpTransport()->SendSignalAssociationChangeCommunicationUp();
|
2019-02-28 07:51:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FakeCricketSctpTransport* CricketSctpTransport() {
|
|
|
|
|
return static_cast<FakeCricketSctpTransport*>(transport_->internal());
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 09:12:57 +02:00
|
|
|
rtc::AutoThread main_thread_;
|
2019-02-28 07:51:00 +01:00
|
|
|
rtc::scoped_refptr<SctpTransport> transport_;
|
|
|
|
|
rtc::scoped_refptr<DtlsTransport> dtls_transport_;
|
|
|
|
|
TestSctpTransportObserver observer_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TEST(SctpTransportSimpleTest, CreateClearDelete) {
|
2022-05-20 09:12:57 +02:00
|
|
|
rtc::AutoThread main_thread;
|
pc: Provide DtlsTransport to SctpTransport constr
This code looked a bit weird before this CL - probably because of old
refactorings.
In JsepTransport constructor, there is a DCHECK assuring that the RTP
DTLS transport is always present, so it can be passed directly to the
SctpTransport constructor, which avoids having the SetDtlsTransport
method in it.
Also, in the SctpTransport constructor, there was code that would set
the SCTP transport state to `kConnecting` if the DTLS transport was
present, but that was dead code, as it was always `nullptr` inside the
constructor before this CL. With this CL, it's always present, and the
SCTP Transport's state will initially always be `kConnecting` now. Which
is a step to deprecating the `kNew` state that doesn't exist in
https://w3c.github.io/webrtc-pc/#dom-rtcsctptransportstate.
One test case was modified, as it didn't test the reality. The test
created a SctpTransport, registered an observer, and added the DTLS
transport, and expected to receive a "statechange" from `kNew` (which is
not a state that exists in the spec) to `kConnecting`. If the test had
tested the opposite ordering - adding the DTLS transport first, and then
adding an observer, it wouldn't have experienced this. And since in
reality (with the implementation of JsepTransport before and
after this CL), it always adds the DTLS transport before any observer is
registered. So it wouldn't ever be fired, outside of tests.
Bug: webrtc:15897
Change-Id: I6ac24e0a331b686eb400fcf388ece50f2ad46a32
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/345420
Commit-Queue: Victor Boivie <boivie@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#41987}
2024-04-02 10:42:43 +02:00
|
|
|
std::unique_ptr<cricket::DtlsTransportInternal> cricket_transport =
|
|
|
|
|
std::make_unique<FakeDtlsTransport>("audio",
|
|
|
|
|
cricket::ICE_CANDIDATE_COMPONENT_RTP);
|
|
|
|
|
rtc::scoped_refptr<DtlsTransport> dtls_transport =
|
|
|
|
|
rtc::make_ref_counted<DtlsTransport>(std::move(cricket_transport));
|
|
|
|
|
|
2019-02-28 07:51:00 +01:00
|
|
|
std::unique_ptr<cricket::SctpTransportInternal> fake_cricket_sctp_transport =
|
|
|
|
|
absl::WrapUnique(new FakeCricketSctpTransport());
|
|
|
|
|
rtc::scoped_refptr<SctpTransport> sctp_transport =
|
2021-04-27 14:43:08 +02:00
|
|
|
rtc::make_ref_counted<SctpTransport>(
|
pc: Provide DtlsTransport to SctpTransport constr
This code looked a bit weird before this CL - probably because of old
refactorings.
In JsepTransport constructor, there is a DCHECK assuring that the RTP
DTLS transport is always present, so it can be passed directly to the
SctpTransport constructor, which avoids having the SetDtlsTransport
method in it.
Also, in the SctpTransport constructor, there was code that would set
the SCTP transport state to `kConnecting` if the DTLS transport was
present, but that was dead code, as it was always `nullptr` inside the
constructor before this CL. With this CL, it's always present, and the
SCTP Transport's state will initially always be `kConnecting` now. Which
is a step to deprecating the `kNew` state that doesn't exist in
https://w3c.github.io/webrtc-pc/#dom-rtcsctptransportstate.
One test case was modified, as it didn't test the reality. The test
created a SctpTransport, registered an observer, and added the DTLS
transport, and expected to receive a "statechange" from `kNew` (which is
not a state that exists in the spec) to `kConnecting`. If the test had
tested the opposite ordering - adding the DTLS transport first, and then
adding an observer, it wouldn't have experienced this. And since in
reality (with the implementation of JsepTransport before and
after this CL), it always adds the DTLS transport before any observer is
registered. So it wouldn't ever be fired, outside of tests.
Bug: webrtc:15897
Change-Id: I6ac24e0a331b686eb400fcf388ece50f2ad46a32
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/345420
Commit-Queue: Victor Boivie <boivie@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#41987}
2024-04-02 10:42:43 +02:00
|
|
|
std::move(fake_cricket_sctp_transport), dtls_transport);
|
2019-02-28 07:51:00 +01:00
|
|
|
ASSERT_TRUE(sctp_transport->internal());
|
pc: Provide DtlsTransport to SctpTransport constr
This code looked a bit weird before this CL - probably because of old
refactorings.
In JsepTransport constructor, there is a DCHECK assuring that the RTP
DTLS transport is always present, so it can be passed directly to the
SctpTransport constructor, which avoids having the SetDtlsTransport
method in it.
Also, in the SctpTransport constructor, there was code that would set
the SCTP transport state to `kConnecting` if the DTLS transport was
present, but that was dead code, as it was always `nullptr` inside the
constructor before this CL. With this CL, it's always present, and the
SCTP Transport's state will initially always be `kConnecting` now. Which
is a step to deprecating the `kNew` state that doesn't exist in
https://w3c.github.io/webrtc-pc/#dom-rtcsctptransportstate.
One test case was modified, as it didn't test the reality. The test
created a SctpTransport, registered an observer, and added the DTLS
transport, and expected to receive a "statechange" from `kNew` (which is
not a state that exists in the spec) to `kConnecting`. If the test had
tested the opposite ordering - adding the DTLS transport first, and then
adding an observer, it wouldn't have experienced this. And since in
reality (with the implementation of JsepTransport before and
after this CL), it always adds the DTLS transport before any observer is
registered. So it wouldn't ever be fired, outside of tests.
Bug: webrtc:15897
Change-Id: I6ac24e0a331b686eb400fcf388ece50f2ad46a32
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/345420
Commit-Queue: Victor Boivie <boivie@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#41987}
2024-04-02 10:42:43 +02:00
|
|
|
ASSERT_EQ(SctpTransportState::kConnecting,
|
|
|
|
|
sctp_transport->Information().state());
|
2019-02-28 07:51:00 +01:00
|
|
|
sctp_transport->Clear();
|
|
|
|
|
ASSERT_FALSE(sctp_transport->internal());
|
|
|
|
|
ASSERT_EQ(SctpTransportState::kClosed, sctp_transport->Information().state());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SctpTransportTest, EventsObservedWhenConnecting) {
|
|
|
|
|
CreateTransport();
|
|
|
|
|
transport()->RegisterObserver(observer());
|
|
|
|
|
CompleteSctpHandshake();
|
|
|
|
|
ASSERT_EQ_WAIT(SctpTransportState::kConnected, observer_.State(),
|
|
|
|
|
kDefaultTimeout);
|
pc: Provide DtlsTransport to SctpTransport constr
This code looked a bit weird before this CL - probably because of old
refactorings.
In JsepTransport constructor, there is a DCHECK assuring that the RTP
DTLS transport is always present, so it can be passed directly to the
SctpTransport constructor, which avoids having the SetDtlsTransport
method in it.
Also, in the SctpTransport constructor, there was code that would set
the SCTP transport state to `kConnecting` if the DTLS transport was
present, but that was dead code, as it was always `nullptr` inside the
constructor before this CL. With this CL, it's always present, and the
SCTP Transport's state will initially always be `kConnecting` now. Which
is a step to deprecating the `kNew` state that doesn't exist in
https://w3c.github.io/webrtc-pc/#dom-rtcsctptransportstate.
One test case was modified, as it didn't test the reality. The test
created a SctpTransport, registered an observer, and added the DTLS
transport, and expected to receive a "statechange" from `kNew` (which is
not a state that exists in the spec) to `kConnecting`. If the test had
tested the opposite ordering - adding the DTLS transport first, and then
adding an observer, it wouldn't have experienced this. And since in
reality (with the implementation of JsepTransport before and
after this CL), it always adds the DTLS transport before any observer is
registered. So it wouldn't ever be fired, outside of tests.
Bug: webrtc:15897
Change-Id: I6ac24e0a331b686eb400fcf388ece50f2ad46a32
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/345420
Commit-Queue: Victor Boivie <boivie@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#41987}
2024-04-02 10:42:43 +02:00
|
|
|
EXPECT_THAT(observer_.States(), ElementsAre(SctpTransportState::kConnected));
|
2019-02-28 07:51:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SctpTransportTest, CloseWhenClearing) {
|
|
|
|
|
CreateTransport();
|
|
|
|
|
transport()->RegisterObserver(observer());
|
|
|
|
|
CompleteSctpHandshake();
|
|
|
|
|
ASSERT_EQ_WAIT(SctpTransportState::kConnected, observer_.State(),
|
|
|
|
|
kDefaultTimeout);
|
|
|
|
|
transport()->Clear();
|
|
|
|
|
ASSERT_EQ_WAIT(SctpTransportState::kClosed, observer_.State(),
|
|
|
|
|
kDefaultTimeout);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-21 10:52:59 +02:00
|
|
|
TEST_F(SctpTransportTest, MaxChannelsSignalled) {
|
|
|
|
|
CreateTransport();
|
|
|
|
|
transport()->RegisterObserver(observer());
|
|
|
|
|
EXPECT_FALSE(transport()->Information().MaxChannels());
|
|
|
|
|
EXPECT_FALSE(observer_.LastReceivedInformation().MaxChannels());
|
|
|
|
|
CompleteSctpHandshake();
|
|
|
|
|
ASSERT_EQ_WAIT(SctpTransportState::kConnected, observer_.State(),
|
|
|
|
|
kDefaultTimeout);
|
|
|
|
|
EXPECT_TRUE(transport()->Information().MaxChannels());
|
|
|
|
|
EXPECT_EQ(kTestMaxSctpStreams, *(transport()->Information().MaxChannels()));
|
|
|
|
|
EXPECT_TRUE(observer_.LastReceivedInformation().MaxChannels());
|
|
|
|
|
EXPECT_EQ(kTestMaxSctpStreams,
|
|
|
|
|
*(observer_.LastReceivedInformation().MaxChannels()));
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-16 12:09:08 +01:00
|
|
|
TEST_F(SctpTransportTest, CloseWhenTransportCloses) {
|
|
|
|
|
CreateTransport();
|
|
|
|
|
transport()->RegisterObserver(observer());
|
|
|
|
|
CompleteSctpHandshake();
|
|
|
|
|
ASSERT_EQ_WAIT(SctpTransportState::kConnected, observer_.State(),
|
|
|
|
|
kDefaultTimeout);
|
|
|
|
|
static_cast<cricket::FakeDtlsTransport*>(dtls_transport_->internal())
|
2021-05-21 20:46:09 +02:00
|
|
|
->SetDtlsState(DtlsTransportState::kClosed);
|
2019-11-16 12:09:08 +01:00
|
|
|
ASSERT_EQ_WAIT(SctpTransportState::kClosed, observer_.State(),
|
|
|
|
|
kDefaultTimeout);
|
|
|
|
|
}
|
2019-02-28 07:51:00 +01:00
|
|
|
} // namespace webrtc
|