Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 11 | #include "pc/jsep_transport.h" |
| 12 | |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 13 | #include <memory> |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 14 | #include <tuple> |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 15 | #include <utility> |
| 16 | |
Qingsi Wang | 25ec888 | 2019-11-15 12:33:05 -0800 | [diff] [blame] | 17 | #include "api/ice_transport_factory.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 18 | #include "media/base/fake_rtp.h" |
| 19 | #include "p2p/base/fake_dtls_transport.h" |
| 20 | #include "p2p/base/fake_ice_transport.h" |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 21 | #include "rtc_base/gunit.h" |
| 22 | |
| 23 | namespace cricket { |
Qingsi Wang | 25ec888 | 2019-11-15 12:33:05 -0800 | [diff] [blame] | 24 | namespace { |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 25 | using webrtc::SdpType; |
| 26 | |
| 27 | static const char kIceUfrag1[] = "U001"; |
| 28 | static const char kIcePwd1[] = "TESTICEPWD00000000000001"; |
| 29 | static const char kIceUfrag2[] = "U002"; |
| 30 | static const char kIcePwd2[] = "TESTIEPWD00000000000002"; |
| 31 | static const char kTransportName[] = "Test Transport"; |
| 32 | |
| 33 | enum class SrtpMode { |
| 34 | kSdes, |
| 35 | kDtlsSrtp, |
| 36 | }; |
| 37 | |
| 38 | struct NegotiateRoleParams { |
| 39 | ConnectionRole local_role; |
| 40 | ConnectionRole remote_role; |
| 41 | SdpType local_type; |
| 42 | SdpType remote_type; |
| 43 | }; |
| 44 | |
Qingsi Wang | 25ec888 | 2019-11-15 12:33:05 -0800 | [diff] [blame] | 45 | rtc::scoped_refptr<webrtc::IceTransportInterface> CreateIceTransport( |
| 46 | std::unique_ptr<FakeIceTransport> internal) { |
| 47 | if (!internal) { |
| 48 | return nullptr; |
| 49 | } |
| 50 | |
Tommi | 87f7090 | 2021-04-27 14:43:08 +0200 | [diff] [blame] | 51 | return rtc::make_ref_counted<FakeIceTransportWrapper>(std::move(internal)); |
Qingsi Wang | 25ec888 | 2019-11-15 12:33:05 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 54 | class JsepTransport2Test : public ::testing::Test, public sigslot::has_slots<> { |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 55 | protected: |
| 56 | std::unique_ptr<webrtc::SrtpTransport> CreateSdesTransport( |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 57 | rtc::PacketTransportInternal* rtp_packet_transport, |
| 58 | rtc::PacketTransportInternal* rtcp_packet_transport) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 59 | auto srtp_transport = std::make_unique<webrtc::SrtpTransport>( |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 60 | rtcp_packet_transport == nullptr); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 61 | |
| 62 | srtp_transport->SetRtpPacketTransport(rtp_packet_transport); |
| 63 | if (rtcp_packet_transport) { |
| 64 | srtp_transport->SetRtcpPacketTransport(rtp_packet_transport); |
| 65 | } |
| 66 | return srtp_transport; |
| 67 | } |
| 68 | |
| 69 | std::unique_ptr<webrtc::DtlsSrtpTransport> CreateDtlsSrtpTransport( |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 70 | cricket::DtlsTransportInternal* rtp_dtls_transport, |
| 71 | cricket::DtlsTransportInternal* rtcp_dtls_transport) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 72 | auto dtls_srtp_transport = std::make_unique<webrtc::DtlsSrtpTransport>( |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 73 | rtcp_dtls_transport == nullptr); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 74 | dtls_srtp_transport->SetDtlsTransports(rtp_dtls_transport, |
| 75 | rtcp_dtls_transport); |
| 76 | return dtls_srtp_transport; |
| 77 | } |
| 78 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 79 | // Create a new JsepTransport with a FakeDtlsTransport and a |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 80 | // FakeIceTransport. |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 81 | std::unique_ptr<JsepTransport> CreateJsepTransport2(bool rtcp_mux_enabled, |
| 82 | SrtpMode srtp_mode) { |
Qingsi Wang | 25ec888 | 2019-11-15 12:33:05 -0800 | [diff] [blame] | 83 | auto ice_internal = std::make_unique<FakeIceTransport>( |
| 84 | kTransportName, ICE_CANDIDATE_COMPONENT_RTP); |
| 85 | auto rtp_dtls_transport = |
| 86 | std::make_unique<FakeDtlsTransport>(ice_internal.get()); |
| 87 | auto ice = CreateIceTransport(std::move(ice_internal)); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 88 | |
Qingsi Wang | 25ec888 | 2019-11-15 12:33:05 -0800 | [diff] [blame] | 89 | std::unique_ptr<FakeIceTransport> rtcp_ice_internal; |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 90 | std::unique_ptr<FakeDtlsTransport> rtcp_dtls_transport; |
| 91 | if (!rtcp_mux_enabled) { |
Qingsi Wang | 25ec888 | 2019-11-15 12:33:05 -0800 | [diff] [blame] | 92 | rtcp_ice_internal = std::make_unique<FakeIceTransport>( |
Bjorn A Mellem | 0c1c1b4 | 2019-05-29 17:34:13 -0700 | [diff] [blame] | 93 | kTransportName, ICE_CANDIDATE_COMPONENT_RTCP); |
Qingsi Wang | 25ec888 | 2019-11-15 12:33:05 -0800 | [diff] [blame] | 94 | rtcp_dtls_transport = |
| 95 | std::make_unique<FakeDtlsTransport>(rtcp_ice_internal.get()); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 96 | } |
Qingsi Wang | 25ec888 | 2019-11-15 12:33:05 -0800 | [diff] [blame] | 97 | auto rtcp_ice = CreateIceTransport(std::move(rtcp_ice_internal)); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 98 | |
| 99 | std::unique_ptr<webrtc::RtpTransport> unencrypted_rtp_transport; |
| 100 | std::unique_ptr<webrtc::SrtpTransport> sdes_transport; |
| 101 | std::unique_ptr<webrtc::DtlsSrtpTransport> dtls_srtp_transport; |
| 102 | switch (srtp_mode) { |
| 103 | case SrtpMode::kSdes: |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 104 | sdes_transport = CreateSdesTransport(rtp_dtls_transport.get(), |
| 105 | rtcp_dtls_transport.get()); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 106 | sdes_transport_ = sdes_transport.get(); |
| 107 | break; |
| 108 | case SrtpMode::kDtlsSrtp: |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 109 | dtls_srtp_transport = CreateDtlsSrtpTransport( |
| 110 | rtp_dtls_transport.get(), rtcp_dtls_transport.get()); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 111 | break; |
| 112 | default: |
| 113 | RTC_NOTREACHED(); |
| 114 | } |
| 115 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 116 | auto jsep_transport = std::make_unique<JsepTransport>( |
Bjorn A Mellem | 0c1c1b4 | 2019-05-29 17:34:13 -0700 | [diff] [blame] | 117 | kTransportName, /*local_certificate=*/nullptr, std::move(ice), |
| 118 | std::move(rtcp_ice), std::move(unencrypted_rtp_transport), |
| 119 | std::move(sdes_transport), std::move(dtls_srtp_transport), |
Niels Möller | ab9d6e1 | 2021-02-02 16:49:02 +0100 | [diff] [blame] | 120 | std::move(rtp_dtls_transport), std::move(rtcp_dtls_transport), |
Mirko Bonadei | 96dca92 | 2021-07-10 22:37:40 +0200 | [diff] [blame] | 121 | /*sctp_transport=*/nullptr, |
| 122 | /*rtcp_mux_active_callback=*/[&]() { OnRtcpMuxActive(); }); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 123 | |
| 124 | signal_rtcp_mux_active_received_ = false; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 125 | return jsep_transport; |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | JsepTransportDescription MakeJsepTransportDescription( |
| 129 | bool rtcp_mux_enabled, |
| 130 | const char* ufrag, |
| 131 | const char* pwd, |
| 132 | const rtc::scoped_refptr<rtc::RTCCertificate>& cert, |
| 133 | ConnectionRole role = CONNECTIONROLE_NONE) { |
| 134 | JsepTransportDescription jsep_description; |
| 135 | jsep_description.rtcp_mux_enabled = rtcp_mux_enabled; |
| 136 | |
| 137 | std::unique_ptr<rtc::SSLFingerprint> fingerprint; |
| 138 | if (cert) { |
Steve Anton | 4905edb | 2018-10-15 19:27:44 -0700 | [diff] [blame] | 139 | fingerprint = rtc::SSLFingerprint::CreateFromCertificate(*cert); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 140 | } |
| 141 | jsep_description.transport_desc = |
| 142 | TransportDescription(std::vector<std::string>(), ufrag, pwd, |
| 143 | ICEMODE_FULL, role, fingerprint.get()); |
| 144 | return jsep_description; |
| 145 | } |
| 146 | |
| 147 | Candidate CreateCandidate(int component) { |
| 148 | Candidate c; |
| 149 | c.set_address(rtc::SocketAddress("192.168.1.1", 8000)); |
| 150 | c.set_component(component); |
| 151 | c.set_protocol(UDP_PROTOCOL_NAME); |
| 152 | c.set_priority(1); |
| 153 | return c; |
| 154 | } |
| 155 | |
| 156 | void OnRtcpMuxActive() { signal_rtcp_mux_active_received_ = true; } |
| 157 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 158 | std::unique_ptr<JsepTransport> jsep_transport_; |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 159 | bool signal_rtcp_mux_active_received_ = false; |
| 160 | // The SrtpTransport is owned by |jsep_transport_|. Keep a raw pointer here |
| 161 | // for testing. |
| 162 | webrtc::SrtpTransport* sdes_transport_ = nullptr; |
| 163 | }; |
| 164 | |
| 165 | // The parameterized tests cover both cases when RTCP mux is enable and |
| 166 | // disabled. |
| 167 | class JsepTransport2WithRtcpMux : public JsepTransport2Test, |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 168 | public ::testing::WithParamInterface<bool> {}; |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 169 | |
| 170 | // This test verifies the ICE parameters are properly applied to the transports. |
| 171 | TEST_P(JsepTransport2WithRtcpMux, SetIceParameters) { |
| 172 | bool rtcp_mux_enabled = GetParam(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 173 | jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 174 | |
| 175 | JsepTransportDescription jsep_description; |
| 176 | jsep_description.transport_desc = TransportDescription(kIceUfrag1, kIcePwd1); |
| 177 | jsep_description.rtcp_mux_enabled = rtcp_mux_enabled; |
| 178 | ASSERT_TRUE( |
| 179 | jsep_transport_ |
| 180 | ->SetLocalJsepTransportDescription(jsep_description, SdpType::kOffer) |
| 181 | .ok()); |
| 182 | auto fake_ice_transport = static_cast<FakeIceTransport*>( |
| 183 | jsep_transport_->rtp_dtls_transport()->ice_transport()); |
| 184 | EXPECT_EQ(ICEMODE_FULL, fake_ice_transport->remote_ice_mode()); |
| 185 | EXPECT_EQ(kIceUfrag1, fake_ice_transport->ice_ufrag()); |
| 186 | EXPECT_EQ(kIcePwd1, fake_ice_transport->ice_pwd()); |
| 187 | if (!rtcp_mux_enabled) { |
| 188 | fake_ice_transport = static_cast<FakeIceTransport*>( |
| 189 | jsep_transport_->rtcp_dtls_transport()->ice_transport()); |
| 190 | ASSERT_TRUE(fake_ice_transport); |
| 191 | EXPECT_EQ(ICEMODE_FULL, fake_ice_transport->remote_ice_mode()); |
| 192 | EXPECT_EQ(kIceUfrag1, fake_ice_transport->ice_ufrag()); |
| 193 | EXPECT_EQ(kIcePwd1, fake_ice_transport->ice_pwd()); |
| 194 | } |
| 195 | |
| 196 | jsep_description.transport_desc = TransportDescription(kIceUfrag2, kIcePwd2); |
| 197 | ASSERT_TRUE(jsep_transport_ |
| 198 | ->SetRemoteJsepTransportDescription(jsep_description, |
| 199 | SdpType::kAnswer) |
| 200 | .ok()); |
| 201 | fake_ice_transport = static_cast<FakeIceTransport*>( |
| 202 | jsep_transport_->rtp_dtls_transport()->ice_transport()); |
| 203 | EXPECT_EQ(ICEMODE_FULL, fake_ice_transport->remote_ice_mode()); |
| 204 | EXPECT_EQ(kIceUfrag2, fake_ice_transport->remote_ice_ufrag()); |
| 205 | EXPECT_EQ(kIcePwd2, fake_ice_transport->remote_ice_pwd()); |
| 206 | if (!rtcp_mux_enabled) { |
| 207 | fake_ice_transport = static_cast<FakeIceTransport*>( |
| 208 | jsep_transport_->rtcp_dtls_transport()->ice_transport()); |
| 209 | ASSERT_TRUE(fake_ice_transport); |
| 210 | EXPECT_EQ(ICEMODE_FULL, fake_ice_transport->remote_ice_mode()); |
| 211 | EXPECT_EQ(kIceUfrag2, fake_ice_transport->remote_ice_ufrag()); |
| 212 | EXPECT_EQ(kIcePwd2, fake_ice_transport->remote_ice_pwd()); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // Similarly, test DTLS parameters are properly applied to the transports. |
| 217 | TEST_P(JsepTransport2WithRtcpMux, SetDtlsParameters) { |
| 218 | bool rtcp_mux_enabled = GetParam(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 219 | jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 220 | |
| 221 | // Create certificates. |
| 222 | rtc::scoped_refptr<rtc::RTCCertificate> local_cert = |
Harald Alvestrand | 8515d5a | 2020-03-20 22:51:32 +0100 | [diff] [blame] | 223 | rtc::RTCCertificate::Create( |
| 224 | rtc::SSLIdentity::Create("local", rtc::KT_DEFAULT)); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 225 | rtc::scoped_refptr<rtc::RTCCertificate> remote_cert = |
Harald Alvestrand | 8515d5a | 2020-03-20 22:51:32 +0100 | [diff] [blame] | 226 | rtc::RTCCertificate::Create( |
| 227 | rtc::SSLIdentity::Create("remote", rtc::KT_DEFAULT)); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 228 | jsep_transport_->SetLocalCertificate(local_cert); |
| 229 | |
| 230 | // Apply offer. |
| 231 | JsepTransportDescription local_description = |
| 232 | MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1, |
| 233 | local_cert, CONNECTIONROLE_ACTPASS); |
| 234 | ASSERT_TRUE( |
| 235 | jsep_transport_ |
| 236 | ->SetLocalJsepTransportDescription(local_description, SdpType::kOffer) |
| 237 | .ok()); |
| 238 | // Apply Answer. |
| 239 | JsepTransportDescription remote_description = |
| 240 | MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2, |
| 241 | remote_cert, CONNECTIONROLE_ACTIVE); |
| 242 | ASSERT_TRUE(jsep_transport_ |
| 243 | ->SetRemoteJsepTransportDescription(remote_description, |
| 244 | SdpType::kAnswer) |
| 245 | .ok()); |
| 246 | |
| 247 | // Verify that SSL role and remote fingerprint were set correctly based on |
| 248 | // transport descriptions. |
| 249 | auto role = jsep_transport_->GetDtlsRole(); |
| 250 | ASSERT_TRUE(role); |
| 251 | EXPECT_EQ(rtc::SSL_SERVER, role); // Because remote description was "active". |
| 252 | auto fake_dtls = |
| 253 | static_cast<FakeDtlsTransport*>(jsep_transport_->rtp_dtls_transport()); |
| 254 | EXPECT_EQ(remote_description.transport_desc.identity_fingerprint->ToString(), |
| 255 | fake_dtls->dtls_fingerprint().ToString()); |
| 256 | |
| 257 | if (!rtcp_mux_enabled) { |
| 258 | auto fake_rtcp_dtls = |
| 259 | static_cast<FakeDtlsTransport*>(jsep_transport_->rtcp_dtls_transport()); |
| 260 | EXPECT_EQ( |
| 261 | remote_description.transport_desc.identity_fingerprint->ToString(), |
| 262 | fake_rtcp_dtls->dtls_fingerprint().ToString()); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | // Same as above test, but with remote transport description using |
| 267 | // CONNECTIONROLE_PASSIVE, expecting SSL_CLIENT role. |
| 268 | TEST_P(JsepTransport2WithRtcpMux, SetDtlsParametersWithPassiveAnswer) { |
| 269 | bool rtcp_mux_enabled = GetParam(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 270 | jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 271 | |
| 272 | // Create certificates. |
| 273 | rtc::scoped_refptr<rtc::RTCCertificate> local_cert = |
Harald Alvestrand | 8515d5a | 2020-03-20 22:51:32 +0100 | [diff] [blame] | 274 | rtc::RTCCertificate::Create( |
| 275 | rtc::SSLIdentity::Create("local", rtc::KT_DEFAULT)); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 276 | rtc::scoped_refptr<rtc::RTCCertificate> remote_cert = |
Harald Alvestrand | 8515d5a | 2020-03-20 22:51:32 +0100 | [diff] [blame] | 277 | rtc::RTCCertificate::Create( |
| 278 | rtc::SSLIdentity::Create("remote", rtc::KT_DEFAULT)); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 279 | jsep_transport_->SetLocalCertificate(local_cert); |
| 280 | |
| 281 | // Apply offer. |
| 282 | JsepTransportDescription local_description = |
| 283 | MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1, |
| 284 | local_cert, CONNECTIONROLE_ACTPASS); |
| 285 | ASSERT_TRUE( |
| 286 | jsep_transport_ |
| 287 | ->SetLocalJsepTransportDescription(local_description, SdpType::kOffer) |
| 288 | .ok()); |
| 289 | // Apply Answer. |
| 290 | JsepTransportDescription remote_description = |
| 291 | MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2, |
| 292 | remote_cert, CONNECTIONROLE_PASSIVE); |
| 293 | ASSERT_TRUE(jsep_transport_ |
| 294 | ->SetRemoteJsepTransportDescription(remote_description, |
| 295 | SdpType::kAnswer) |
| 296 | .ok()); |
| 297 | |
| 298 | // Verify that SSL role and remote fingerprint were set correctly based on |
| 299 | // transport descriptions. |
| 300 | auto role = jsep_transport_->GetDtlsRole(); |
| 301 | ASSERT_TRUE(role); |
| 302 | EXPECT_EQ(rtc::SSL_CLIENT, |
| 303 | role); // Because remote description was "passive". |
| 304 | auto fake_dtls = |
| 305 | static_cast<FakeDtlsTransport*>(jsep_transport_->rtp_dtls_transport()); |
| 306 | EXPECT_EQ(remote_description.transport_desc.identity_fingerprint->ToString(), |
| 307 | fake_dtls->dtls_fingerprint().ToString()); |
| 308 | |
| 309 | if (!rtcp_mux_enabled) { |
| 310 | auto fake_rtcp_dtls = |
| 311 | static_cast<FakeDtlsTransport*>(jsep_transport_->rtcp_dtls_transport()); |
| 312 | EXPECT_EQ( |
| 313 | remote_description.transport_desc.identity_fingerprint->ToString(), |
| 314 | fake_rtcp_dtls->dtls_fingerprint().ToString()); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | // Tests SetNeedsIceRestartFlag and need_ice_restart, ensuring needs_ice_restart |
| 319 | // only starts returning "false" once an ICE restart has been initiated. |
| 320 | TEST_P(JsepTransport2WithRtcpMux, NeedsIceRestart) { |
| 321 | bool rtcp_mux_enabled = GetParam(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 322 | jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 323 | |
| 324 | // Use the same JsepTransportDescription for both offer and answer. |
| 325 | JsepTransportDescription description; |
| 326 | description.transport_desc = TransportDescription(kIceUfrag1, kIcePwd1); |
| 327 | ASSERT_TRUE( |
| 328 | jsep_transport_ |
| 329 | ->SetLocalJsepTransportDescription(description, SdpType::kOffer) |
| 330 | .ok()); |
| 331 | ASSERT_TRUE( |
| 332 | jsep_transport_ |
| 333 | ->SetRemoteJsepTransportDescription(description, SdpType::kAnswer) |
| 334 | .ok()); |
| 335 | // Flag initially should be false. |
| 336 | EXPECT_FALSE(jsep_transport_->needs_ice_restart()); |
| 337 | |
| 338 | // After setting flag, it should be true. |
| 339 | jsep_transport_->SetNeedsIceRestartFlag(); |
| 340 | EXPECT_TRUE(jsep_transport_->needs_ice_restart()); |
| 341 | |
| 342 | ASSERT_TRUE( |
| 343 | jsep_transport_ |
| 344 | ->SetLocalJsepTransportDescription(description, SdpType::kOffer) |
| 345 | .ok()); |
| 346 | ASSERT_TRUE( |
| 347 | jsep_transport_ |
| 348 | ->SetRemoteJsepTransportDescription(description, SdpType::kAnswer) |
| 349 | .ok()); |
| 350 | EXPECT_TRUE(jsep_transport_->needs_ice_restart()); |
| 351 | |
| 352 | // Doing an offer/answer that restarts ICE should clear the flag. |
| 353 | description.transport_desc = TransportDescription(kIceUfrag2, kIcePwd2); |
| 354 | ASSERT_TRUE( |
| 355 | jsep_transport_ |
| 356 | ->SetLocalJsepTransportDescription(description, SdpType::kOffer) |
| 357 | .ok()); |
| 358 | ASSERT_TRUE( |
| 359 | jsep_transport_ |
| 360 | ->SetRemoteJsepTransportDescription(description, SdpType::kAnswer) |
| 361 | .ok()); |
| 362 | EXPECT_FALSE(jsep_transport_->needs_ice_restart()); |
| 363 | } |
| 364 | |
| 365 | TEST_P(JsepTransport2WithRtcpMux, GetStats) { |
| 366 | bool rtcp_mux_enabled = GetParam(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 367 | jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 368 | |
| 369 | size_t expected_stats_size = rtcp_mux_enabled ? 1u : 2u; |
| 370 | TransportStats stats; |
| 371 | EXPECT_TRUE(jsep_transport_->GetStats(&stats)); |
| 372 | EXPECT_EQ(expected_stats_size, stats.channel_stats.size()); |
| 373 | EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTP, stats.channel_stats[0].component); |
| 374 | if (!rtcp_mux_enabled) { |
| 375 | EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTCP, stats.channel_stats[1].component); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | // Tests that VerifyCertificateFingerprint only returns true when the |
| 380 | // certificate matches the fingerprint. |
| 381 | TEST_P(JsepTransport2WithRtcpMux, VerifyCertificateFingerprint) { |
| 382 | bool rtcp_mux_enabled = GetParam(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 383 | jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 384 | |
| 385 | EXPECT_FALSE( |
| 386 | jsep_transport_->VerifyCertificateFingerprint(nullptr, nullptr).ok()); |
| 387 | rtc::KeyType key_types[] = {rtc::KT_RSA, rtc::KT_ECDSA}; |
| 388 | |
| 389 | for (auto& key_type : key_types) { |
| 390 | rtc::scoped_refptr<rtc::RTCCertificate> certificate = |
Harald Alvestrand | 8515d5a | 2020-03-20 22:51:32 +0100 | [diff] [blame] | 391 | rtc::RTCCertificate::Create( |
| 392 | rtc::SSLIdentity::Create("testing", key_type)); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 393 | ASSERT_NE(nullptr, certificate); |
| 394 | |
| 395 | std::string digest_algorithm; |
Benjamin Wright | 6c6c9df | 2018-10-25 01:16:26 -0700 | [diff] [blame] | 396 | ASSERT_TRUE(certificate->GetSSLCertificate().GetSignatureDigestAlgorithm( |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 397 | &digest_algorithm)); |
| 398 | ASSERT_FALSE(digest_algorithm.empty()); |
Steve Anton | 4905edb | 2018-10-15 19:27:44 -0700 | [diff] [blame] | 399 | std::unique_ptr<rtc::SSLFingerprint> good_fingerprint = |
| 400 | rtc::SSLFingerprint::CreateUnique(digest_algorithm, |
| 401 | *certificate->identity()); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 402 | ASSERT_NE(nullptr, good_fingerprint); |
| 403 | |
| 404 | EXPECT_TRUE(jsep_transport_ |
| 405 | ->VerifyCertificateFingerprint(certificate.get(), |
| 406 | good_fingerprint.get()) |
| 407 | .ok()); |
| 408 | EXPECT_FALSE(jsep_transport_ |
| 409 | ->VerifyCertificateFingerprint(certificate.get(), nullptr) |
| 410 | .ok()); |
| 411 | EXPECT_FALSE( |
| 412 | jsep_transport_ |
| 413 | ->VerifyCertificateFingerprint(nullptr, good_fingerprint.get()) |
| 414 | .ok()); |
| 415 | |
| 416 | rtc::SSLFingerprint bad_fingerprint = *good_fingerprint; |
| 417 | bad_fingerprint.digest.AppendData("0", 1); |
| 418 | EXPECT_FALSE( |
| 419 | jsep_transport_ |
| 420 | ->VerifyCertificateFingerprint(certificate.get(), &bad_fingerprint) |
| 421 | .ok()); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | // Tests the logic of DTLS role negotiation for an initial offer/answer. |
| 426 | TEST_P(JsepTransport2WithRtcpMux, ValidDtlsRoleNegotiation) { |
| 427 | bool rtcp_mux_enabled = GetParam(); |
| 428 | // Just use the same certificate for both sides; doesn't really matter in a |
| 429 | // non end-to-end test. |
| 430 | rtc::scoped_refptr<rtc::RTCCertificate> certificate = |
Harald Alvestrand | 8515d5a | 2020-03-20 22:51:32 +0100 | [diff] [blame] | 431 | rtc::RTCCertificate::Create( |
| 432 | rtc::SSLIdentity::Create("testing", rtc::KT_ECDSA)); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 433 | |
| 434 | JsepTransportDescription local_description = MakeJsepTransportDescription( |
| 435 | rtcp_mux_enabled, kIceUfrag1, kIcePwd1, certificate); |
| 436 | JsepTransportDescription remote_description = MakeJsepTransportDescription( |
| 437 | rtcp_mux_enabled, kIceUfrag2, kIcePwd2, certificate); |
| 438 | |
| 439 | // Parameters which set the SSL role to SSL_CLIENT. |
| 440 | NegotiateRoleParams valid_client_params[] = { |
| 441 | {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_ACTPASS, SdpType::kAnswer, |
| 442 | SdpType::kOffer}, |
| 443 | {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_ACTPASS, SdpType::kPrAnswer, |
| 444 | SdpType::kOffer}, |
| 445 | {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_PASSIVE, SdpType::kOffer, |
| 446 | SdpType::kAnswer}, |
| 447 | {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_PASSIVE, SdpType::kOffer, |
| 448 | SdpType::kPrAnswer}}; |
| 449 | |
| 450 | for (auto& param : valid_client_params) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 451 | jsep_transport_ = |
| 452 | CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 453 | jsep_transport_->SetLocalCertificate(certificate); |
| 454 | |
| 455 | local_description.transport_desc.connection_role = param.local_role; |
| 456 | remote_description.transport_desc.connection_role = param.remote_role; |
| 457 | |
| 458 | // Set the offer first. |
| 459 | if (param.local_type == SdpType::kOffer) { |
| 460 | EXPECT_TRUE(jsep_transport_ |
| 461 | ->SetLocalJsepTransportDescription(local_description, |
| 462 | param.local_type) |
| 463 | .ok()); |
| 464 | EXPECT_TRUE(jsep_transport_ |
| 465 | ->SetRemoteJsepTransportDescription(remote_description, |
| 466 | param.remote_type) |
| 467 | .ok()); |
| 468 | } else { |
| 469 | EXPECT_TRUE(jsep_transport_ |
| 470 | ->SetRemoteJsepTransportDescription(remote_description, |
| 471 | param.remote_type) |
| 472 | .ok()); |
| 473 | EXPECT_TRUE(jsep_transport_ |
| 474 | ->SetLocalJsepTransportDescription(local_description, |
| 475 | param.local_type) |
| 476 | .ok()); |
| 477 | } |
| 478 | EXPECT_EQ(rtc::SSL_CLIENT, *jsep_transport_->GetDtlsRole()); |
| 479 | } |
| 480 | |
| 481 | // Parameters which set the SSL role to SSL_SERVER. |
| 482 | NegotiateRoleParams valid_server_params[] = { |
| 483 | {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_ACTPASS, SdpType::kAnswer, |
| 484 | SdpType::kOffer}, |
| 485 | {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_ACTPASS, SdpType::kPrAnswer, |
| 486 | SdpType::kOffer}, |
| 487 | {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_ACTIVE, SdpType::kOffer, |
| 488 | SdpType::kAnswer}, |
| 489 | {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_ACTIVE, SdpType::kOffer, |
| 490 | SdpType::kPrAnswer}}; |
| 491 | |
| 492 | for (auto& param : valid_server_params) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 493 | jsep_transport_ = |
| 494 | CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 495 | jsep_transport_->SetLocalCertificate(certificate); |
| 496 | |
| 497 | local_description.transport_desc.connection_role = param.local_role; |
| 498 | remote_description.transport_desc.connection_role = param.remote_role; |
| 499 | |
| 500 | // Set the offer first. |
| 501 | if (param.local_type == SdpType::kOffer) { |
| 502 | EXPECT_TRUE(jsep_transport_ |
| 503 | ->SetLocalJsepTransportDescription(local_description, |
| 504 | param.local_type) |
| 505 | .ok()); |
| 506 | EXPECT_TRUE(jsep_transport_ |
| 507 | ->SetRemoteJsepTransportDescription(remote_description, |
| 508 | param.remote_type) |
| 509 | .ok()); |
| 510 | } else { |
| 511 | EXPECT_TRUE(jsep_transport_ |
| 512 | ->SetRemoteJsepTransportDescription(remote_description, |
| 513 | param.remote_type) |
| 514 | .ok()); |
| 515 | EXPECT_TRUE(jsep_transport_ |
| 516 | ->SetLocalJsepTransportDescription(local_description, |
| 517 | param.local_type) |
| 518 | .ok()); |
| 519 | } |
| 520 | EXPECT_EQ(rtc::SSL_SERVER, *jsep_transport_->GetDtlsRole()); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | // Tests the logic of DTLS role negotiation for an initial offer/answer. |
| 525 | TEST_P(JsepTransport2WithRtcpMux, InvalidDtlsRoleNegotiation) { |
| 526 | bool rtcp_mux_enabled = GetParam(); |
| 527 | // Just use the same certificate for both sides; doesn't really matter in a |
| 528 | // non end-to-end test. |
| 529 | rtc::scoped_refptr<rtc::RTCCertificate> certificate = |
Harald Alvestrand | 8515d5a | 2020-03-20 22:51:32 +0100 | [diff] [blame] | 530 | rtc::RTCCertificate::Create( |
| 531 | rtc::SSLIdentity::Create("testing", rtc::KT_ECDSA)); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 532 | |
| 533 | JsepTransportDescription local_description = MakeJsepTransportDescription( |
| 534 | rtcp_mux_enabled, kIceUfrag1, kIcePwd1, certificate); |
| 535 | JsepTransportDescription remote_description = MakeJsepTransportDescription( |
| 536 | rtcp_mux_enabled, kIceUfrag2, kIcePwd2, certificate); |
| 537 | |
| 538 | NegotiateRoleParams duplicate_params[] = { |
| 539 | {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_ACTIVE, SdpType::kAnswer, |
| 540 | SdpType::kOffer}, |
| 541 | {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_ACTPASS, SdpType::kAnswer, |
| 542 | SdpType::kOffer}, |
| 543 | {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_PASSIVE, SdpType::kAnswer, |
| 544 | SdpType::kOffer}, |
| 545 | {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_ACTIVE, SdpType::kPrAnswer, |
| 546 | SdpType::kOffer}, |
| 547 | {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_ACTPASS, SdpType::kPrAnswer, |
| 548 | SdpType::kOffer}, |
| 549 | {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_PASSIVE, SdpType::kPrAnswer, |
| 550 | SdpType::kOffer}, |
| 551 | {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_ACTIVE, SdpType::kOffer, |
| 552 | SdpType::kAnswer}, |
| 553 | {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_ACTPASS, SdpType::kOffer, |
| 554 | SdpType::kAnswer}, |
| 555 | {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_PASSIVE, SdpType::kOffer, |
| 556 | SdpType::kAnswer}, |
| 557 | {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_ACTIVE, SdpType::kOffer, |
| 558 | SdpType::kPrAnswer}, |
| 559 | {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_ACTPASS, SdpType::kOffer, |
| 560 | SdpType::kPrAnswer}, |
| 561 | {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_PASSIVE, SdpType::kOffer, |
| 562 | SdpType::kPrAnswer}}; |
| 563 | |
| 564 | for (auto& param : duplicate_params) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 565 | jsep_transport_ = |
| 566 | CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 567 | jsep_transport_->SetLocalCertificate(certificate); |
| 568 | |
| 569 | local_description.transport_desc.connection_role = param.local_role; |
| 570 | remote_description.transport_desc.connection_role = param.remote_role; |
| 571 | |
| 572 | if (param.local_type == SdpType::kOffer) { |
| 573 | EXPECT_TRUE(jsep_transport_ |
| 574 | ->SetLocalJsepTransportDescription(local_description, |
| 575 | param.local_type) |
| 576 | .ok()); |
| 577 | EXPECT_FALSE(jsep_transport_ |
| 578 | ->SetRemoteJsepTransportDescription(remote_description, |
| 579 | param.remote_type) |
| 580 | .ok()); |
| 581 | } else { |
| 582 | EXPECT_TRUE(jsep_transport_ |
| 583 | ->SetRemoteJsepTransportDescription(remote_description, |
| 584 | param.remote_type) |
| 585 | .ok()); |
| 586 | EXPECT_FALSE(jsep_transport_ |
| 587 | ->SetLocalJsepTransportDescription(local_description, |
| 588 | param.local_type) |
| 589 | .ok()); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | // Invalid parameters due to the offerer not using ACTPASS. |
| 594 | NegotiateRoleParams offerer_without_actpass_params[] = { |
| 595 | {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_PASSIVE, SdpType::kAnswer, |
| 596 | SdpType::kOffer}, |
| 597 | {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_ACTIVE, SdpType::kAnswer, |
| 598 | SdpType::kOffer}, |
| 599 | {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_PASSIVE, SdpType::kAnswer, |
| 600 | SdpType::kOffer}, |
| 601 | {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_PASSIVE, SdpType::kPrAnswer, |
| 602 | SdpType::kOffer}, |
| 603 | {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_ACTIVE, SdpType::kPrAnswer, |
| 604 | SdpType::kOffer}, |
| 605 | {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_PASSIVE, SdpType::kPrAnswer, |
| 606 | SdpType::kOffer}, |
| 607 | {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_PASSIVE, SdpType::kOffer, |
| 608 | SdpType::kAnswer}, |
| 609 | {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_ACTIVE, SdpType::kOffer, |
| 610 | SdpType::kAnswer}, |
| 611 | {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_ACTPASS, SdpType::kOffer, |
| 612 | SdpType::kAnswer}, |
| 613 | {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_PASSIVE, SdpType::kOffer, |
| 614 | SdpType::kPrAnswer}, |
| 615 | {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_ACTIVE, SdpType::kOffer, |
| 616 | SdpType::kPrAnswer}, |
| 617 | {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_ACTPASS, SdpType::kOffer, |
| 618 | SdpType::kPrAnswer}}; |
| 619 | |
| 620 | for (auto& param : offerer_without_actpass_params) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 621 | jsep_transport_ = |
| 622 | CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 623 | jsep_transport_->SetLocalCertificate(certificate); |
| 624 | |
| 625 | local_description.transport_desc.connection_role = param.local_role; |
| 626 | remote_description.transport_desc.connection_role = param.remote_role; |
| 627 | |
| 628 | if (param.local_type == SdpType::kOffer) { |
| 629 | EXPECT_TRUE(jsep_transport_ |
| 630 | ->SetLocalJsepTransportDescription(local_description, |
| 631 | param.local_type) |
| 632 | .ok()); |
| 633 | EXPECT_FALSE(jsep_transport_ |
| 634 | ->SetRemoteJsepTransportDescription(remote_description, |
| 635 | param.remote_type) |
| 636 | .ok()); |
| 637 | } else { |
| 638 | EXPECT_TRUE(jsep_transport_ |
| 639 | ->SetRemoteJsepTransportDescription(remote_description, |
| 640 | param.remote_type) |
| 641 | .ok()); |
| 642 | EXPECT_FALSE(jsep_transport_ |
| 643 | ->SetLocalJsepTransportDescription(local_description, |
| 644 | param.local_type) |
| 645 | .ok()); |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | |
Mirko Bonadei | c84f661 | 2019-01-31 12:20:57 +0100 | [diff] [blame] | 650 | INSTANTIATE_TEST_SUITE_P(JsepTransport2Test, |
| 651 | JsepTransport2WithRtcpMux, |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 652 | ::testing::Bool()); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 653 | |
| 654 | // Test that a reoffer in the opposite direction is successful as long as the |
| 655 | // role isn't changing. Doesn't test every possible combination like the test |
| 656 | // above. |
| 657 | TEST_F(JsepTransport2Test, ValidDtlsReofferFromAnswerer) { |
| 658 | // Just use the same certificate for both sides; doesn't really matter in a |
| 659 | // non end-to-end test. |
| 660 | rtc::scoped_refptr<rtc::RTCCertificate> certificate = |
Harald Alvestrand | 8515d5a | 2020-03-20 22:51:32 +0100 | [diff] [blame] | 661 | rtc::RTCCertificate::Create( |
| 662 | rtc::SSLIdentity::Create("testing", rtc::KT_ECDSA)); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 663 | bool rtcp_mux_enabled = true; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 664 | jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 665 | jsep_transport_->SetLocalCertificate(certificate); |
| 666 | |
| 667 | JsepTransportDescription local_offer = |
| 668 | MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1, |
| 669 | certificate, CONNECTIONROLE_ACTPASS); |
| 670 | JsepTransportDescription remote_answer = |
| 671 | MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2, |
| 672 | certificate, CONNECTIONROLE_ACTIVE); |
| 673 | |
| 674 | EXPECT_TRUE( |
| 675 | jsep_transport_ |
| 676 | ->SetLocalJsepTransportDescription(local_offer, SdpType::kOffer) |
| 677 | .ok()); |
| 678 | EXPECT_TRUE( |
| 679 | jsep_transport_ |
| 680 | ->SetRemoteJsepTransportDescription(remote_answer, SdpType::kAnswer) |
| 681 | .ok()); |
| 682 | |
| 683 | // We were actpass->active previously, now in the other direction it's |
| 684 | // actpass->passive. |
| 685 | JsepTransportDescription remote_offer = |
| 686 | MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2, |
| 687 | certificate, CONNECTIONROLE_ACTPASS); |
| 688 | JsepTransportDescription local_answer = |
| 689 | MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1, |
| 690 | certificate, CONNECTIONROLE_PASSIVE); |
| 691 | |
| 692 | EXPECT_TRUE( |
| 693 | jsep_transport_ |
| 694 | ->SetRemoteJsepTransportDescription(remote_offer, SdpType::kOffer) |
| 695 | .ok()); |
| 696 | EXPECT_TRUE( |
| 697 | jsep_transport_ |
| 698 | ->SetLocalJsepTransportDescription(local_answer, SdpType::kAnswer) |
| 699 | .ok()); |
| 700 | } |
| 701 | |
| 702 | // Test that a reoffer in the opposite direction fails if the role changes. |
| 703 | // Inverse of test above. |
| 704 | TEST_F(JsepTransport2Test, InvalidDtlsReofferFromAnswerer) { |
| 705 | // Just use the same certificate for both sides; doesn't really matter in a |
| 706 | // non end-to-end test. |
| 707 | rtc::scoped_refptr<rtc::RTCCertificate> certificate = |
Harald Alvestrand | 8515d5a | 2020-03-20 22:51:32 +0100 | [diff] [blame] | 708 | rtc::RTCCertificate::Create( |
| 709 | rtc::SSLIdentity::Create("testing", rtc::KT_ECDSA)); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 710 | bool rtcp_mux_enabled = true; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 711 | jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 712 | jsep_transport_->SetLocalCertificate(certificate); |
| 713 | |
| 714 | JsepTransportDescription local_offer = |
| 715 | MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1, |
| 716 | certificate, CONNECTIONROLE_ACTPASS); |
| 717 | JsepTransportDescription remote_answer = |
| 718 | MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2, |
| 719 | certificate, CONNECTIONROLE_ACTIVE); |
| 720 | |
| 721 | EXPECT_TRUE( |
| 722 | jsep_transport_ |
| 723 | ->SetLocalJsepTransportDescription(local_offer, SdpType::kOffer) |
| 724 | .ok()); |
| 725 | EXPECT_TRUE( |
| 726 | jsep_transport_ |
| 727 | ->SetRemoteJsepTransportDescription(remote_answer, SdpType::kAnswer) |
| 728 | .ok()); |
| 729 | |
| 730 | // Changing role to passive here isn't allowed. Though for some reason this |
| 731 | // only fails in SetLocalTransportDescription. |
| 732 | JsepTransportDescription remote_offer = |
| 733 | MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2, |
| 734 | certificate, CONNECTIONROLE_PASSIVE); |
| 735 | JsepTransportDescription local_answer = |
| 736 | MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1, |
| 737 | certificate, CONNECTIONROLE_ACTIVE); |
| 738 | |
| 739 | EXPECT_TRUE( |
| 740 | jsep_transport_ |
| 741 | ->SetRemoteJsepTransportDescription(remote_offer, SdpType::kOffer) |
| 742 | .ok()); |
| 743 | EXPECT_FALSE( |
| 744 | jsep_transport_ |
| 745 | ->SetLocalJsepTransportDescription(local_answer, SdpType::kAnswer) |
| 746 | .ok()); |
| 747 | } |
| 748 | |
| 749 | // Test that a remote offer with the current negotiated role can be accepted. |
| 750 | // This is allowed by dtls-sdp, though we'll never generate such an offer, |
| 751 | // since JSEP requires generating "actpass". |
| 752 | TEST_F(JsepTransport2Test, RemoteOfferWithCurrentNegotiatedDtlsRole) { |
| 753 | rtc::scoped_refptr<rtc::RTCCertificate> certificate = |
Harald Alvestrand | 8515d5a | 2020-03-20 22:51:32 +0100 | [diff] [blame] | 754 | rtc::RTCCertificate::Create( |
| 755 | rtc::SSLIdentity::Create("testing", rtc::KT_ECDSA)); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 756 | bool rtcp_mux_enabled = true; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 757 | jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 758 | jsep_transport_->SetLocalCertificate(certificate); |
| 759 | |
| 760 | JsepTransportDescription remote_desc = |
| 761 | MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1, |
| 762 | certificate, CONNECTIONROLE_ACTPASS); |
| 763 | JsepTransportDescription local_desc = |
| 764 | MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2, |
| 765 | certificate, CONNECTIONROLE_ACTIVE); |
| 766 | |
| 767 | // Normal initial offer/answer with "actpass" in the offer and "active" in |
| 768 | // the answer. |
| 769 | ASSERT_TRUE( |
| 770 | jsep_transport_ |
| 771 | ->SetRemoteJsepTransportDescription(remote_desc, SdpType::kOffer) |
| 772 | .ok()); |
| 773 | ASSERT_TRUE( |
| 774 | jsep_transport_ |
| 775 | ->SetLocalJsepTransportDescription(local_desc, SdpType::kAnswer) |
| 776 | .ok()); |
| 777 | |
| 778 | // Sanity check that role was actually negotiated. |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 779 | absl::optional<rtc::SSLRole> role = jsep_transport_->GetDtlsRole(); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 780 | ASSERT_TRUE(role); |
| 781 | EXPECT_EQ(rtc::SSL_CLIENT, *role); |
| 782 | |
| 783 | // Subsequent offer with current negotiated role of "passive". |
| 784 | remote_desc.transport_desc.connection_role = CONNECTIONROLE_PASSIVE; |
| 785 | EXPECT_TRUE( |
| 786 | jsep_transport_ |
| 787 | ->SetRemoteJsepTransportDescription(remote_desc, SdpType::kOffer) |
| 788 | .ok()); |
| 789 | EXPECT_TRUE( |
| 790 | jsep_transport_ |
| 791 | ->SetLocalJsepTransportDescription(local_desc, SdpType::kAnswer) |
| 792 | .ok()); |
| 793 | } |
| 794 | |
| 795 | // Test that a remote offer with the inverse of the current negotiated DTLS |
| 796 | // role is rejected. |
| 797 | TEST_F(JsepTransport2Test, RemoteOfferThatChangesNegotiatedDtlsRole) { |
| 798 | rtc::scoped_refptr<rtc::RTCCertificate> certificate = |
Harald Alvestrand | 8515d5a | 2020-03-20 22:51:32 +0100 | [diff] [blame] | 799 | rtc::RTCCertificate::Create( |
| 800 | rtc::SSLIdentity::Create("testing", rtc::KT_ECDSA)); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 801 | bool rtcp_mux_enabled = true; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 802 | jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 803 | jsep_transport_->SetLocalCertificate(certificate); |
| 804 | |
| 805 | JsepTransportDescription remote_desc = |
| 806 | MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1, |
| 807 | certificate, CONNECTIONROLE_ACTPASS); |
| 808 | JsepTransportDescription local_desc = |
| 809 | MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2, |
| 810 | certificate, CONNECTIONROLE_ACTIVE); |
| 811 | |
| 812 | // Normal initial offer/answer with "actpass" in the offer and "active" in |
| 813 | // the answer. |
| 814 | ASSERT_TRUE( |
| 815 | jsep_transport_ |
| 816 | ->SetRemoteJsepTransportDescription(remote_desc, SdpType::kOffer) |
| 817 | .ok()); |
| 818 | ASSERT_TRUE( |
| 819 | jsep_transport_ |
| 820 | ->SetLocalJsepTransportDescription(local_desc, SdpType::kAnswer) |
| 821 | .ok()); |
| 822 | |
| 823 | // Sanity check that role was actually negotiated. |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 824 | absl::optional<rtc::SSLRole> role = jsep_transport_->GetDtlsRole(); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 825 | ASSERT_TRUE(role); |
| 826 | EXPECT_EQ(rtc::SSL_CLIENT, *role); |
| 827 | |
| 828 | // Subsequent offer with current negotiated role of "passive". |
| 829 | remote_desc.transport_desc.connection_role = CONNECTIONROLE_ACTIVE; |
| 830 | EXPECT_TRUE( |
| 831 | jsep_transport_ |
| 832 | ->SetRemoteJsepTransportDescription(remote_desc, SdpType::kOffer) |
| 833 | .ok()); |
| 834 | EXPECT_FALSE( |
| 835 | jsep_transport_ |
| 836 | ->SetLocalJsepTransportDescription(local_desc, SdpType::kAnswer) |
| 837 | .ok()); |
| 838 | } |
| 839 | |
| 840 | // Testing that a legacy client that doesn't use the setup attribute will be |
| 841 | // interpreted as having an active role. |
| 842 | TEST_F(JsepTransport2Test, DtlsSetupWithLegacyAsAnswerer) { |
| 843 | rtc::scoped_refptr<rtc::RTCCertificate> certificate = |
Harald Alvestrand | 8515d5a | 2020-03-20 22:51:32 +0100 | [diff] [blame] | 844 | rtc::RTCCertificate::Create( |
| 845 | rtc::SSLIdentity::Create("testing", rtc::KT_ECDSA)); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 846 | bool rtcp_mux_enabled = true; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 847 | jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 848 | jsep_transport_->SetLocalCertificate(certificate); |
| 849 | |
| 850 | JsepTransportDescription remote_desc = |
| 851 | MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1, |
| 852 | certificate, CONNECTIONROLE_ACTPASS); |
| 853 | JsepTransportDescription local_desc = |
| 854 | MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2, |
| 855 | certificate, CONNECTIONROLE_ACTIVE); |
| 856 | |
| 857 | local_desc.transport_desc.connection_role = CONNECTIONROLE_ACTPASS; |
| 858 | ASSERT_TRUE( |
| 859 | jsep_transport_ |
| 860 | ->SetLocalJsepTransportDescription(local_desc, SdpType::kOffer) |
| 861 | .ok()); |
| 862 | // Use CONNECTIONROLE_NONE to simulate legacy endpoint. |
| 863 | remote_desc.transport_desc.connection_role = CONNECTIONROLE_NONE; |
| 864 | ASSERT_TRUE( |
| 865 | jsep_transport_ |
| 866 | ->SetRemoteJsepTransportDescription(remote_desc, SdpType::kAnswer) |
| 867 | .ok()); |
| 868 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 869 | absl::optional<rtc::SSLRole> role = jsep_transport_->GetDtlsRole(); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 870 | ASSERT_TRUE(role); |
| 871 | // Since legacy answer ommitted setup atribute, and we offered actpass, we |
| 872 | // should act as passive (server). |
| 873 | EXPECT_EQ(rtc::SSL_SERVER, *role); |
| 874 | } |
| 875 | |
| 876 | // Tests that when the RTCP mux is successfully negotiated, the RTCP transport |
| 877 | // will be destroyed and the SignalRtpMuxActive will be fired. |
| 878 | TEST_F(JsepTransport2Test, RtcpMuxNegotiation) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 879 | jsep_transport_ = |
| 880 | CreateJsepTransport2(/*rtcp_mux_enabled=*/false, SrtpMode::kDtlsSrtp); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 881 | JsepTransportDescription local_desc; |
| 882 | local_desc.rtcp_mux_enabled = true; |
Harald Alvestrand | ad88c88 | 2018-11-28 16:47:46 +0100 | [diff] [blame] | 883 | ASSERT_NE(nullptr, jsep_transport_->rtcp_dtls_transport()); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 884 | EXPECT_FALSE(signal_rtcp_mux_active_received_); |
| 885 | |
| 886 | // The remote side supports RTCP-mux. |
| 887 | JsepTransportDescription remote_desc; |
| 888 | remote_desc.rtcp_mux_enabled = true; |
| 889 | ASSERT_TRUE( |
| 890 | jsep_transport_ |
| 891 | ->SetLocalJsepTransportDescription(local_desc, SdpType::kOffer) |
| 892 | .ok()); |
| 893 | ASSERT_TRUE( |
| 894 | jsep_transport_ |
| 895 | ->SetRemoteJsepTransportDescription(remote_desc, SdpType::kAnswer) |
| 896 | .ok()); |
| 897 | |
| 898 | EXPECT_EQ(nullptr, jsep_transport_->rtcp_dtls_transport()); |
| 899 | EXPECT_TRUE(signal_rtcp_mux_active_received_); |
| 900 | |
| 901 | // The remote side doesn't support RTCP-mux. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 902 | jsep_transport_ = |
| 903 | CreateJsepTransport2(/*rtcp_mux_enabled=*/false, SrtpMode::kDtlsSrtp); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 904 | signal_rtcp_mux_active_received_ = false; |
| 905 | remote_desc.rtcp_mux_enabled = false; |
| 906 | ASSERT_TRUE( |
| 907 | jsep_transport_ |
| 908 | ->SetLocalJsepTransportDescription(local_desc, SdpType::kOffer) |
| 909 | .ok()); |
| 910 | ASSERT_TRUE( |
| 911 | jsep_transport_ |
| 912 | ->SetRemoteJsepTransportDescription(remote_desc, SdpType::kAnswer) |
| 913 | .ok()); |
| 914 | |
| 915 | EXPECT_NE(nullptr, jsep_transport_->rtcp_dtls_transport()); |
| 916 | EXPECT_FALSE(signal_rtcp_mux_active_received_); |
| 917 | } |
| 918 | |
| 919 | TEST_F(JsepTransport2Test, SdesNegotiation) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 920 | jsep_transport_ = |
| 921 | CreateJsepTransport2(/*rtcp_mux_enabled=*/true, SrtpMode::kSdes); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 922 | ASSERT_TRUE(sdes_transport_); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 923 | EXPECT_FALSE(sdes_transport_->IsSrtpActive()); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 924 | |
| 925 | JsepTransportDescription offer_desc; |
| 926 | offer_desc.cryptos.push_back(cricket::CryptoParams( |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame^] | 927 | 1, rtc::kCsAesCm128HmacSha1_32, "inline:" + rtc::CreateRandomString(40), |
| 928 | std::string())); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 929 | ASSERT_TRUE( |
| 930 | jsep_transport_ |
| 931 | ->SetLocalJsepTransportDescription(offer_desc, SdpType::kOffer) |
| 932 | .ok()); |
| 933 | |
| 934 | JsepTransportDescription answer_desc; |
| 935 | answer_desc.cryptos.push_back(cricket::CryptoParams( |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame^] | 936 | 1, rtc::kCsAesCm128HmacSha1_32, "inline:" + rtc::CreateRandomString(40), |
| 937 | std::string())); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 938 | ASSERT_TRUE( |
| 939 | jsep_transport_ |
| 940 | ->SetRemoteJsepTransportDescription(answer_desc, SdpType::kAnswer) |
| 941 | .ok()); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 942 | EXPECT_TRUE(sdes_transport_->IsSrtpActive()); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | TEST_F(JsepTransport2Test, SdesNegotiationWithEmptyCryptosInAnswer) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 946 | jsep_transport_ = |
| 947 | CreateJsepTransport2(/*rtcp_mux_enabled=*/true, SrtpMode::kSdes); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 948 | ASSERT_TRUE(sdes_transport_); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 949 | EXPECT_FALSE(sdes_transport_->IsSrtpActive()); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 950 | |
| 951 | JsepTransportDescription offer_desc; |
| 952 | offer_desc.cryptos.push_back(cricket::CryptoParams( |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame^] | 953 | 1, rtc::kCsAesCm128HmacSha1_32, "inline:" + rtc::CreateRandomString(40), |
| 954 | std::string())); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 955 | ASSERT_TRUE( |
| 956 | jsep_transport_ |
| 957 | ->SetLocalJsepTransportDescription(offer_desc, SdpType::kOffer) |
| 958 | .ok()); |
| 959 | |
| 960 | JsepTransportDescription answer_desc; |
| 961 | ASSERT_TRUE( |
| 962 | jsep_transport_ |
| 963 | ->SetRemoteJsepTransportDescription(answer_desc, SdpType::kAnswer) |
| 964 | .ok()); |
| 965 | // SRTP is not active because the crypto parameter is answer is empty. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 966 | EXPECT_FALSE(sdes_transport_->IsSrtpActive()); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | TEST_F(JsepTransport2Test, SdesNegotiationWithMismatchedCryptos) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 970 | jsep_transport_ = |
| 971 | CreateJsepTransport2(/*rtcp_mux_enabled=*/true, SrtpMode::kSdes); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 972 | ASSERT_TRUE(sdes_transport_); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 973 | EXPECT_FALSE(sdes_transport_->IsSrtpActive()); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 974 | |
| 975 | JsepTransportDescription offer_desc; |
| 976 | offer_desc.cryptos.push_back(cricket::CryptoParams( |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame^] | 977 | 1, rtc::kCsAesCm128HmacSha1_32, "inline:" + rtc::CreateRandomString(40), |
| 978 | std::string())); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 979 | ASSERT_TRUE( |
| 980 | jsep_transport_ |
| 981 | ->SetLocalJsepTransportDescription(offer_desc, SdpType::kOffer) |
| 982 | .ok()); |
| 983 | |
| 984 | JsepTransportDescription answer_desc; |
| 985 | answer_desc.cryptos.push_back(cricket::CryptoParams( |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame^] | 986 | 1, rtc::kCsAesCm128HmacSha1_80, "inline:" + rtc::CreateRandomString(40), |
| 987 | std::string())); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 988 | // Expected to fail because the crypto parameters don't match. |
| 989 | ASSERT_FALSE( |
| 990 | jsep_transport_ |
| 991 | ->SetRemoteJsepTransportDescription(answer_desc, SdpType::kAnswer) |
| 992 | .ok()); |
| 993 | } |
| 994 | |
| 995 | // Tests that the remote candidates can be added to the transports after both |
| 996 | // local and remote descriptions are set. |
| 997 | TEST_F(JsepTransport2Test, AddRemoteCandidates) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 998 | jsep_transport_ = |
| 999 | CreateJsepTransport2(/*rtcp_mux_enabled=*/true, SrtpMode::kDtlsSrtp); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 1000 | auto fake_ice_transport = static_cast<FakeIceTransport*>( |
| 1001 | jsep_transport_->rtp_dtls_transport()->ice_transport()); |
| 1002 | |
| 1003 | Candidates candidates; |
| 1004 | candidates.push_back(CreateCandidate(/*COMPONENT_RTP*/ 1)); |
| 1005 | candidates.push_back(CreateCandidate(/*COMPONENT_RTP*/ 1)); |
| 1006 | |
| 1007 | JsepTransportDescription desc; |
| 1008 | ASSERT_TRUE( |
| 1009 | jsep_transport_->SetLocalJsepTransportDescription(desc, SdpType::kOffer) |
| 1010 | .ok()); |
| 1011 | // Expected to fail because the remote description is unset. |
| 1012 | EXPECT_FALSE(jsep_transport_->AddRemoteCandidates(candidates).ok()); |
| 1013 | |
| 1014 | ASSERT_TRUE( |
| 1015 | jsep_transport_->SetRemoteJsepTransportDescription(desc, SdpType::kAnswer) |
| 1016 | .ok()); |
| 1017 | EXPECT_EQ(0u, fake_ice_transport->remote_candidates().size()); |
| 1018 | EXPECT_TRUE(jsep_transport_->AddRemoteCandidates(candidates).ok()); |
| 1019 | EXPECT_EQ(candidates.size(), fake_ice_transport->remote_candidates().size()); |
| 1020 | } |
| 1021 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1022 | enum class Scenario { |
| 1023 | kSdes, |
| 1024 | kDtlsBeforeCallerSendOffer, |
| 1025 | kDtlsBeforeCallerSetAnswer, |
| 1026 | kDtlsAfterCallerSetAnswer, |
| 1027 | }; |
| 1028 | |
| 1029 | class JsepTransport2HeaderExtensionTest |
| 1030 | : public JsepTransport2Test, |
| 1031 | public ::testing::WithParamInterface<std::tuple<Scenario, bool>> { |
| 1032 | protected: |
| 1033 | JsepTransport2HeaderExtensionTest() {} |
| 1034 | |
| 1035 | void CreateJsepTransportPair(SrtpMode mode) { |
| 1036 | jsep_transport1_ = CreateJsepTransport2(/*rtcp_mux_enabled=*/true, mode); |
| 1037 | jsep_transport2_ = CreateJsepTransport2(/*rtcp_mux_enabled=*/true, mode); |
| 1038 | |
| 1039 | auto fake_dtls1 = |
| 1040 | static_cast<FakeDtlsTransport*>(jsep_transport1_->rtp_dtls_transport()); |
| 1041 | auto fake_dtls2 = |
| 1042 | static_cast<FakeDtlsTransport*>(jsep_transport2_->rtp_dtls_transport()); |
| 1043 | |
| 1044 | fake_dtls1->fake_ice_transport()->SignalReadPacket.connect( |
| 1045 | this, &JsepTransport2HeaderExtensionTest::OnReadPacket1); |
| 1046 | fake_dtls2->fake_ice_transport()->SignalReadPacket.connect( |
| 1047 | this, &JsepTransport2HeaderExtensionTest::OnReadPacket2); |
| 1048 | |
| 1049 | if (mode == SrtpMode::kDtlsSrtp) { |
Harald Alvestrand | 8515d5a | 2020-03-20 22:51:32 +0100 | [diff] [blame] | 1050 | auto cert1 = rtc::RTCCertificate::Create( |
| 1051 | rtc::SSLIdentity::Create("session1", rtc::KT_DEFAULT)); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1052 | jsep_transport1_->rtp_dtls_transport()->SetLocalCertificate(cert1); |
Harald Alvestrand | 8515d5a | 2020-03-20 22:51:32 +0100 | [diff] [blame] | 1053 | auto cert2 = rtc::RTCCertificate::Create( |
| 1054 | rtc::SSLIdentity::Create("session1", rtc::KT_DEFAULT)); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1055 | jsep_transport2_->rtp_dtls_transport()->SetLocalCertificate(cert2); |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | void OnReadPacket1(rtc::PacketTransportInternal* transport, |
| 1060 | const char* data, |
| 1061 | size_t size, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 1062 | const int64_t& /* packet_time_us */, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1063 | int flags) { |
| 1064 | RTC_LOG(LS_INFO) << "JsepTransport 1 Received a packet."; |
| 1065 | CompareHeaderExtensions( |
| 1066 | reinterpret_cast<const char*>(kPcmuFrameWithExtensions), |
| 1067 | sizeof(kPcmuFrameWithExtensions), data, size, recv_encrypted_headers1_, |
| 1068 | false); |
| 1069 | received_packet_count_++; |
| 1070 | } |
| 1071 | |
| 1072 | void OnReadPacket2(rtc::PacketTransportInternal* transport, |
| 1073 | const char* data, |
| 1074 | size_t size, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 1075 | const int64_t& /* packet_time_us */, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1076 | int flags) { |
| 1077 | RTC_LOG(LS_INFO) << "JsepTransport 2 Received a packet."; |
| 1078 | CompareHeaderExtensions( |
| 1079 | reinterpret_cast<const char*>(kPcmuFrameWithExtensions), |
| 1080 | sizeof(kPcmuFrameWithExtensions), data, size, recv_encrypted_headers2_, |
| 1081 | false); |
| 1082 | received_packet_count_++; |
| 1083 | } |
| 1084 | |
| 1085 | void ConnectTransport() { |
| 1086 | auto rtp_dtls_transport1 = |
| 1087 | static_cast<FakeDtlsTransport*>(jsep_transport1_->rtp_dtls_transport()); |
| 1088 | auto rtp_dtls_transport2 = |
| 1089 | static_cast<FakeDtlsTransport*>(jsep_transport2_->rtp_dtls_transport()); |
| 1090 | rtp_dtls_transport1->SetDestination(rtp_dtls_transport2); |
| 1091 | } |
| 1092 | |
| 1093 | int GetRtpAuthLen() { |
| 1094 | bool use_gcm = std::get<1>(GetParam()); |
| 1095 | if (use_gcm) { |
| 1096 | return 16; |
| 1097 | } |
| 1098 | return 10; |
| 1099 | } |
| 1100 | |
| 1101 | void TestSendRecvPacketWithEncryptedHeaderExtension() { |
| 1102 | TestOneWaySendRecvPacketWithEncryptedHeaderExtension( |
| 1103 | jsep_transport1_.get()); |
| 1104 | TestOneWaySendRecvPacketWithEncryptedHeaderExtension( |
| 1105 | jsep_transport2_.get()); |
| 1106 | } |
| 1107 | |
| 1108 | void TestOneWaySendRecvPacketWithEncryptedHeaderExtension( |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 1109 | JsepTransport* sender_transport) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1110 | size_t rtp_len = sizeof(kPcmuFrameWithExtensions); |
| 1111 | size_t packet_size = rtp_len + GetRtpAuthLen(); |
| 1112 | rtc::Buffer rtp_packet_buffer(packet_size); |
| 1113 | char* rtp_packet_data = rtp_packet_buffer.data<char>(); |
| 1114 | memcpy(rtp_packet_data, kPcmuFrameWithExtensions, rtp_len); |
| 1115 | // In order to be able to run this test function multiple times we can not |
| 1116 | // use the same sequence number twice. Increase the sequence number by one. |
| 1117 | rtc::SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_data) + 2, |
| 1118 | ++sequence_number_); |
| 1119 | rtc::CopyOnWriteBuffer rtp_packet(rtp_packet_data, rtp_len, packet_size); |
| 1120 | |
| 1121 | int packet_count_before = received_packet_count_; |
| 1122 | rtc::PacketOptions options; |
| 1123 | // Send a packet and verify that the packet can be successfully received and |
| 1124 | // decrypted. |
| 1125 | ASSERT_TRUE(sender_transport->rtp_transport()->SendRtpPacket( |
| 1126 | &rtp_packet, options, cricket::PF_SRTP_BYPASS)); |
| 1127 | EXPECT_EQ(packet_count_before + 1, received_packet_count_); |
| 1128 | } |
| 1129 | |
| 1130 | int sequence_number_ = 0; |
| 1131 | int received_packet_count_ = 0; |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 1132 | std::unique_ptr<JsepTransport> jsep_transport1_; |
| 1133 | std::unique_ptr<JsepTransport> jsep_transport2_; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1134 | std::vector<int> recv_encrypted_headers1_; |
| 1135 | std::vector<int> recv_encrypted_headers2_; |
| 1136 | }; |
| 1137 | |
| 1138 | // Test that the encrypted header extension works and can be changed in |
| 1139 | // different scenarios. |
| 1140 | TEST_P(JsepTransport2HeaderExtensionTest, EncryptedHeaderExtensionNegotiation) { |
| 1141 | Scenario scenario = std::get<0>(GetParam()); |
| 1142 | bool use_gcm = std::get<1>(GetParam()); |
| 1143 | SrtpMode mode = SrtpMode ::kDtlsSrtp; |
| 1144 | if (scenario == Scenario::kSdes) { |
| 1145 | mode = SrtpMode::kSdes; |
| 1146 | } |
| 1147 | CreateJsepTransportPair(mode); |
| 1148 | recv_encrypted_headers1_.push_back(kHeaderExtensionIDs[0]); |
| 1149 | recv_encrypted_headers2_.push_back(kHeaderExtensionIDs[1]); |
| 1150 | |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame^] | 1151 | cricket::CryptoParams sdes_param(1, rtc::kCsAesCm128HmacSha1_80, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1152 | "inline:" + rtc::CreateRandomString(40), |
| 1153 | std::string()); |
| 1154 | if (use_gcm) { |
| 1155 | auto fake_dtls1 = |
| 1156 | static_cast<FakeDtlsTransport*>(jsep_transport1_->rtp_dtls_transport()); |
| 1157 | auto fake_dtls2 = |
| 1158 | static_cast<FakeDtlsTransport*>(jsep_transport2_->rtp_dtls_transport()); |
| 1159 | |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame^] | 1160 | fake_dtls1->SetSrtpCryptoSuite(rtc::kSrtpAeadAes256Gcm); |
| 1161 | fake_dtls2->SetSrtpCryptoSuite(rtc::kSrtpAeadAes256Gcm); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | if (scenario == Scenario::kDtlsBeforeCallerSendOffer) { |
| 1165 | ConnectTransport(); |
| 1166 | } |
| 1167 | |
| 1168 | JsepTransportDescription offer_desc; |
| 1169 | offer_desc.encrypted_header_extension_ids = recv_encrypted_headers1_; |
| 1170 | if (scenario == Scenario::kSdes) { |
| 1171 | offer_desc.cryptos.push_back(sdes_param); |
| 1172 | } |
| 1173 | ASSERT_TRUE( |
| 1174 | jsep_transport1_ |
| 1175 | ->SetLocalJsepTransportDescription(offer_desc, SdpType::kOffer) |
| 1176 | .ok()); |
| 1177 | ASSERT_TRUE( |
| 1178 | jsep_transport2_ |
| 1179 | ->SetRemoteJsepTransportDescription(offer_desc, SdpType::kOffer) |
| 1180 | .ok()); |
| 1181 | |
| 1182 | JsepTransportDescription answer_desc; |
| 1183 | answer_desc.encrypted_header_extension_ids = recv_encrypted_headers2_; |
| 1184 | if (scenario == Scenario::kSdes) { |
| 1185 | answer_desc.cryptos.push_back(sdes_param); |
| 1186 | } |
| 1187 | ASSERT_TRUE( |
| 1188 | jsep_transport2_ |
| 1189 | ->SetLocalJsepTransportDescription(answer_desc, SdpType::kAnswer) |
| 1190 | .ok()); |
| 1191 | |
| 1192 | if (scenario == Scenario::kDtlsBeforeCallerSetAnswer) { |
| 1193 | ConnectTransport(); |
| 1194 | // Sending packet from transport2 to transport1 should work when they are |
| 1195 | // partially configured. |
| 1196 | TestOneWaySendRecvPacketWithEncryptedHeaderExtension( |
| 1197 | /*sender_transport=*/jsep_transport2_.get()); |
| 1198 | } |
| 1199 | |
| 1200 | ASSERT_TRUE( |
| 1201 | jsep_transport1_ |
| 1202 | ->SetRemoteJsepTransportDescription(answer_desc, SdpType::kAnswer) |
| 1203 | .ok()); |
| 1204 | |
| 1205 | if (scenario == Scenario::kDtlsAfterCallerSetAnswer || |
| 1206 | scenario == Scenario::kSdes) { |
| 1207 | ConnectTransport(); |
| 1208 | } |
| 1209 | EXPECT_TRUE(jsep_transport1_->rtp_transport()->IsSrtpActive()); |
| 1210 | EXPECT_TRUE(jsep_transport2_->rtp_transport()->IsSrtpActive()); |
| 1211 | TestSendRecvPacketWithEncryptedHeaderExtension(); |
| 1212 | |
| 1213 | // Change the encrypted header extension in a new offer/answer exchange. |
| 1214 | recv_encrypted_headers1_.clear(); |
| 1215 | recv_encrypted_headers2_.clear(); |
| 1216 | recv_encrypted_headers1_.push_back(kHeaderExtensionIDs[1]); |
| 1217 | recv_encrypted_headers2_.push_back(kHeaderExtensionIDs[0]); |
| 1218 | offer_desc.encrypted_header_extension_ids = recv_encrypted_headers1_; |
| 1219 | answer_desc.encrypted_header_extension_ids = recv_encrypted_headers2_; |
| 1220 | ASSERT_TRUE( |
| 1221 | jsep_transport1_ |
| 1222 | ->SetLocalJsepTransportDescription(offer_desc, SdpType::kOffer) |
| 1223 | .ok()); |
| 1224 | ASSERT_TRUE( |
| 1225 | jsep_transport2_ |
| 1226 | ->SetRemoteJsepTransportDescription(offer_desc, SdpType::kOffer) |
| 1227 | .ok()); |
| 1228 | ASSERT_TRUE( |
| 1229 | jsep_transport2_ |
| 1230 | ->SetLocalJsepTransportDescription(answer_desc, SdpType::kAnswer) |
| 1231 | .ok()); |
| 1232 | ASSERT_TRUE( |
| 1233 | jsep_transport1_ |
| 1234 | ->SetRemoteJsepTransportDescription(answer_desc, SdpType::kAnswer) |
| 1235 | .ok()); |
| 1236 | EXPECT_TRUE(jsep_transport1_->rtp_transport()->IsSrtpActive()); |
| 1237 | EXPECT_TRUE(jsep_transport2_->rtp_transport()->IsSrtpActive()); |
| 1238 | TestSendRecvPacketWithEncryptedHeaderExtension(); |
| 1239 | } |
| 1240 | |
Mirko Bonadei | c84f661 | 2019-01-31 12:20:57 +0100 | [diff] [blame] | 1241 | INSTANTIATE_TEST_SUITE_P( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1242 | JsepTransport2Test, |
| 1243 | JsepTransport2HeaderExtensionTest, |
| 1244 | ::testing::Values( |
| 1245 | std::make_tuple(Scenario::kSdes, false), |
| 1246 | std::make_tuple(Scenario::kDtlsBeforeCallerSendOffer, true), |
| 1247 | std::make_tuple(Scenario::kDtlsBeforeCallerSetAnswer, true), |
| 1248 | std::make_tuple(Scenario::kDtlsAfterCallerSetAnswer, true), |
| 1249 | std::make_tuple(Scenario::kDtlsBeforeCallerSendOffer, false), |
| 1250 | std::make_tuple(Scenario::kDtlsBeforeCallerSetAnswer, false), |
| 1251 | std::make_tuple(Scenario::kDtlsAfterCallerSetAnswer, false))); |
Jonas Oreland | 52aea5d | 2020-03-03 13:21:30 +0100 | [diff] [blame] | 1252 | |
| 1253 | // This test verifies the ICE parameters are properly applied to the transports. |
| 1254 | TEST_F(JsepTransport2Test, SetIceParametersWithRenomination) { |
| 1255 | jsep_transport_ = |
| 1256 | CreateJsepTransport2(/* rtcp_mux_enabled= */ true, SrtpMode::kDtlsSrtp); |
| 1257 | |
| 1258 | JsepTransportDescription jsep_description; |
| 1259 | jsep_description.transport_desc = TransportDescription(kIceUfrag1, kIcePwd1); |
| 1260 | jsep_description.transport_desc.AddOption(ICE_OPTION_RENOMINATION); |
| 1261 | ASSERT_TRUE( |
| 1262 | jsep_transport_ |
| 1263 | ->SetLocalJsepTransportDescription(jsep_description, SdpType::kOffer) |
| 1264 | .ok()); |
| 1265 | auto fake_ice_transport = static_cast<FakeIceTransport*>( |
| 1266 | jsep_transport_->rtp_dtls_transport()->ice_transport()); |
| 1267 | EXPECT_EQ(ICEMODE_FULL, fake_ice_transport->remote_ice_mode()); |
| 1268 | EXPECT_EQ(kIceUfrag1, fake_ice_transport->ice_ufrag()); |
| 1269 | EXPECT_EQ(kIcePwd1, fake_ice_transport->ice_pwd()); |
| 1270 | EXPECT_TRUE(fake_ice_transport->ice_parameters().renomination); |
| 1271 | |
| 1272 | jsep_description.transport_desc = TransportDescription(kIceUfrag2, kIcePwd2); |
| 1273 | jsep_description.transport_desc.AddOption(ICE_OPTION_RENOMINATION); |
| 1274 | ASSERT_TRUE(jsep_transport_ |
| 1275 | ->SetRemoteJsepTransportDescription(jsep_description, |
| 1276 | SdpType::kAnswer) |
| 1277 | .ok()); |
| 1278 | fake_ice_transport = static_cast<FakeIceTransport*>( |
| 1279 | jsep_transport_->rtp_dtls_transport()->ice_transport()); |
| 1280 | EXPECT_EQ(ICEMODE_FULL, fake_ice_transport->remote_ice_mode()); |
| 1281 | EXPECT_EQ(kIceUfrag2, fake_ice_transport->remote_ice_ufrag()); |
| 1282 | EXPECT_EQ(kIcePwd2, fake_ice_transport->remote_ice_pwd()); |
| 1283 | EXPECT_TRUE(fake_ice_transport->remote_ice_parameters().renomination); |
| 1284 | } |
| 1285 | |
Qingsi Wang | 25ec888 | 2019-11-15 12:33:05 -0800 | [diff] [blame] | 1286 | } // namespace |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 1287 | } // namespace cricket |