Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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. |
| 9 | */ |
| 10 | |
| 11 | #include "pc/sctp_transport.h" |
| 12 | |
| 13 | #include <utility> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include "absl/memory/memory.h" |
Mirko Bonadei | 9f6808b | 2021-05-21 20:46:09 +0200 | [diff] [blame^] | 17 | #include "api/dtls_transport_interface.h" |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 18 | #include "p2p/base/fake_dtls_transport.h" |
| 19 | #include "pc/dtls_transport.h" |
| 20 | #include "rtc_base/gunit.h" |
| 21 | #include "test/gmock.h" |
| 22 | #include "test/gtest.h" |
| 23 | |
| 24 | constexpr int kDefaultTimeout = 1000; // milliseconds |
Harald Alvestrand | 97716c0 | 2019-05-21 10:52:59 +0200 | [diff] [blame] | 25 | constexpr int kTestMaxSctpStreams = 1234; |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 26 | |
| 27 | using cricket::FakeDtlsTransport; |
| 28 | using ::testing::ElementsAre; |
| 29 | |
| 30 | namespace webrtc { |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | class FakeCricketSctpTransport : public cricket::SctpTransportInternal { |
| 35 | public: |
| 36 | void SetDtlsTransport(rtc::PacketTransportInternal* transport) override {} |
Harald Alvestrand | fbb45bd | 2019-05-15 08:07:47 +0200 | [diff] [blame] | 37 | bool Start(int local_port, int remote_port, int max_message_size) override { |
| 38 | return true; |
| 39 | } |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 40 | bool OpenStream(int sid) override { return true; } |
| 41 | bool ResetStream(int sid) override { return true; } |
Florent Castelli | d95b149 | 2021-05-10 11:29:56 +0200 | [diff] [blame] | 42 | bool SendData(int sid, |
| 43 | const SendDataParams& params, |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 44 | const rtc::CopyOnWriteBuffer& payload, |
| 45 | cricket::SendDataResult* result = nullptr) override { |
| 46 | return true; |
| 47 | } |
| 48 | bool ReadyToSendData() override { return true; } |
| 49 | void set_debug_name_for_testing(const char* debug_name) override {} |
Harald Alvestrand | fbb45bd | 2019-05-15 08:07:47 +0200 | [diff] [blame] | 50 | int max_message_size() const override { return 0; } |
Harald Alvestrand | 97716c0 | 2019-05-21 10:52:59 +0200 | [diff] [blame] | 51 | absl::optional<int> max_outbound_streams() const override { |
| 52 | return max_outbound_streams_; |
| 53 | } |
| 54 | absl::optional<int> max_inbound_streams() const override { |
| 55 | return max_inbound_streams_; |
| 56 | } |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 57 | // Methods exposed for testing |
| 58 | void SendSignalReadyToSendData() { SignalReadyToSendData(); } |
| 59 | |
Harald Alvestrand | 97716c0 | 2019-05-21 10:52:59 +0200 | [diff] [blame] | 60 | void SendSignalAssociationChangeCommunicationUp() { |
| 61 | SignalAssociationChangeCommunicationUp(); |
| 62 | } |
| 63 | |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 64 | void SendSignalClosingProcedureStartedRemotely() { |
| 65 | SignalClosingProcedureStartedRemotely(1); |
| 66 | } |
| 67 | |
| 68 | void SendSignalClosingProcedureComplete() { |
| 69 | SignalClosingProcedureComplete(1); |
| 70 | } |
Harald Alvestrand | 97716c0 | 2019-05-21 10:52:59 +0200 | [diff] [blame] | 71 | void set_max_outbound_streams(int streams) { |
| 72 | max_outbound_streams_ = streams; |
| 73 | } |
| 74 | void set_max_inbound_streams(int streams) { max_inbound_streams_ = streams; } |
| 75 | |
| 76 | private: |
| 77 | absl::optional<int> max_outbound_streams_; |
| 78 | absl::optional<int> max_inbound_streams_; |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | } // namespace |
| 82 | |
| 83 | class TestSctpTransportObserver : public SctpTransportObserverInterface { |
| 84 | public: |
Harald Alvestrand | 97716c0 | 2019-05-21 10:52:59 +0200 | [diff] [blame] | 85 | TestSctpTransportObserver() : info_(SctpTransportState::kNew) {} |
| 86 | |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 87 | void OnStateChange(SctpTransportInformation info) override { |
Harald Alvestrand | 97716c0 | 2019-05-21 10:52:59 +0200 | [diff] [blame] | 88 | info_ = info; |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 89 | states_.push_back(info.state()); |
| 90 | } |
| 91 | |
| 92 | SctpTransportState State() { |
| 93 | if (states_.size() > 0) { |
| 94 | return states_[states_.size() - 1]; |
| 95 | } else { |
| 96 | return SctpTransportState::kNew; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | const std::vector<SctpTransportState>& States() { return states_; } |
| 101 | |
Harald Alvestrand | 97716c0 | 2019-05-21 10:52:59 +0200 | [diff] [blame] | 102 | const SctpTransportInformation LastReceivedInformation() { return info_; } |
| 103 | |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 104 | private: |
| 105 | std::vector<SctpTransportState> states_; |
Harald Alvestrand | 97716c0 | 2019-05-21 10:52:59 +0200 | [diff] [blame] | 106 | SctpTransportInformation info_; |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 107 | }; |
| 108 | |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 109 | class SctpTransportTest : public ::testing::Test { |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 110 | public: |
| 111 | SctpTransport* transport() { return transport_.get(); } |
| 112 | SctpTransportObserverInterface* observer() { return &observer_; } |
| 113 | |
| 114 | void CreateTransport() { |
| 115 | auto cricket_sctp_transport = |
| 116 | absl::WrapUnique(new FakeCricketSctpTransport()); |
Tommi | 87f7090 | 2021-04-27 14:43:08 +0200 | [diff] [blame] | 117 | transport_ = |
| 118 | rtc::make_ref_counted<SctpTransport>(std::move(cricket_sctp_transport)); |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void AddDtlsTransport() { |
| 122 | std::unique_ptr<cricket::DtlsTransportInternal> cricket_transport = |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 123 | std::make_unique<FakeDtlsTransport>( |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 124 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 125 | dtls_transport_ = |
Tommi | 87f7090 | 2021-04-27 14:43:08 +0200 | [diff] [blame] | 126 | rtc::make_ref_counted<DtlsTransport>(std::move(cricket_transport)); |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 127 | transport_->SetDtlsTransport(dtls_transport_); |
| 128 | } |
| 129 | |
| 130 | void CompleteSctpHandshake() { |
| 131 | CricketSctpTransport()->SendSignalReadyToSendData(); |
Harald Alvestrand | 97716c0 | 2019-05-21 10:52:59 +0200 | [diff] [blame] | 132 | // The computed MaxChannels shall be the minimum of the outgoing |
| 133 | // and incoming # of streams. |
| 134 | CricketSctpTransport()->set_max_outbound_streams(kTestMaxSctpStreams); |
| 135 | CricketSctpTransport()->set_max_inbound_streams(kTestMaxSctpStreams + 1); |
| 136 | CricketSctpTransport()->SendSignalAssociationChangeCommunicationUp(); |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | FakeCricketSctpTransport* CricketSctpTransport() { |
| 140 | return static_cast<FakeCricketSctpTransport*>(transport_->internal()); |
| 141 | } |
| 142 | |
| 143 | rtc::scoped_refptr<SctpTransport> transport_; |
| 144 | rtc::scoped_refptr<DtlsTransport> dtls_transport_; |
| 145 | TestSctpTransportObserver observer_; |
| 146 | }; |
| 147 | |
| 148 | TEST(SctpTransportSimpleTest, CreateClearDelete) { |
| 149 | std::unique_ptr<cricket::SctpTransportInternal> fake_cricket_sctp_transport = |
| 150 | absl::WrapUnique(new FakeCricketSctpTransport()); |
| 151 | rtc::scoped_refptr<SctpTransport> sctp_transport = |
Tommi | 87f7090 | 2021-04-27 14:43:08 +0200 | [diff] [blame] | 152 | rtc::make_ref_counted<SctpTransport>( |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 153 | std::move(fake_cricket_sctp_transport)); |
| 154 | ASSERT_TRUE(sctp_transport->internal()); |
| 155 | ASSERT_EQ(SctpTransportState::kNew, sctp_transport->Information().state()); |
| 156 | sctp_transport->Clear(); |
| 157 | ASSERT_FALSE(sctp_transport->internal()); |
| 158 | ASSERT_EQ(SctpTransportState::kClosed, sctp_transport->Information().state()); |
| 159 | } |
| 160 | |
| 161 | TEST_F(SctpTransportTest, EventsObservedWhenConnecting) { |
| 162 | CreateTransport(); |
| 163 | transport()->RegisterObserver(observer()); |
| 164 | AddDtlsTransport(); |
| 165 | CompleteSctpHandshake(); |
| 166 | ASSERT_EQ_WAIT(SctpTransportState::kConnected, observer_.State(), |
| 167 | kDefaultTimeout); |
| 168 | EXPECT_THAT(observer_.States(), ElementsAre(SctpTransportState::kConnecting, |
| 169 | SctpTransportState::kConnected)); |
| 170 | } |
| 171 | |
| 172 | TEST_F(SctpTransportTest, CloseWhenClearing) { |
| 173 | CreateTransport(); |
| 174 | transport()->RegisterObserver(observer()); |
| 175 | AddDtlsTransport(); |
| 176 | CompleteSctpHandshake(); |
| 177 | ASSERT_EQ_WAIT(SctpTransportState::kConnected, observer_.State(), |
| 178 | kDefaultTimeout); |
| 179 | transport()->Clear(); |
| 180 | ASSERT_EQ_WAIT(SctpTransportState::kClosed, observer_.State(), |
| 181 | kDefaultTimeout); |
| 182 | } |
| 183 | |
Harald Alvestrand | 97716c0 | 2019-05-21 10:52:59 +0200 | [diff] [blame] | 184 | TEST_F(SctpTransportTest, MaxChannelsSignalled) { |
| 185 | CreateTransport(); |
| 186 | transport()->RegisterObserver(observer()); |
| 187 | AddDtlsTransport(); |
| 188 | EXPECT_FALSE(transport()->Information().MaxChannels()); |
| 189 | EXPECT_FALSE(observer_.LastReceivedInformation().MaxChannels()); |
| 190 | CompleteSctpHandshake(); |
| 191 | ASSERT_EQ_WAIT(SctpTransportState::kConnected, observer_.State(), |
| 192 | kDefaultTimeout); |
| 193 | EXPECT_TRUE(transport()->Information().MaxChannels()); |
| 194 | EXPECT_EQ(kTestMaxSctpStreams, *(transport()->Information().MaxChannels())); |
| 195 | EXPECT_TRUE(observer_.LastReceivedInformation().MaxChannels()); |
| 196 | EXPECT_EQ(kTestMaxSctpStreams, |
| 197 | *(observer_.LastReceivedInformation().MaxChannels())); |
| 198 | } |
| 199 | |
Harald Alvestrand | 408cb4b | 2019-11-16 12:09:08 +0100 | [diff] [blame] | 200 | TEST_F(SctpTransportTest, CloseWhenTransportCloses) { |
| 201 | CreateTransport(); |
| 202 | transport()->RegisterObserver(observer()); |
| 203 | AddDtlsTransport(); |
| 204 | CompleteSctpHandshake(); |
| 205 | ASSERT_EQ_WAIT(SctpTransportState::kConnected, observer_.State(), |
| 206 | kDefaultTimeout); |
| 207 | static_cast<cricket::FakeDtlsTransport*>(dtls_transport_->internal()) |
Mirko Bonadei | 9f6808b | 2021-05-21 20:46:09 +0200 | [diff] [blame^] | 208 | ->SetDtlsState(DtlsTransportState::kClosed); |
Harald Alvestrand | 408cb4b | 2019-11-16 12:09:08 +0100 | [diff] [blame] | 209 | ASSERT_EQ_WAIT(SctpTransportState::kClosed, observer_.State(), |
| 210 | kDefaultTimeout); |
| 211 | } |
| 212 | |
Harald Alvestrand | c85328f | 2019-02-28 07:51:00 +0100 | [diff] [blame] | 213 | } // namespace webrtc |