blob: c63dc496dbc23085176211803c576cf7fd009df5 [file] [log] [blame]
Zhi Huange818b6e2018-02-22 15:26:27 -08001/*
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 Olssona4d87372019-07-05 19:08:33 +020011#include "pc/jsep_transport.h"
12
Harald Alvestrandc24a2182022-02-23 13:44:59 +000013#include <stdint.h>
14#include <string.h>
15
16#include <ostream>
17#include <string>
Zhi Huange830e682018-03-30 10:48:35 -070018#include <tuple>
Zhi Huange818b6e2018-02-22 15:26:27 -080019#include <utility>
20
Harald Alvestrandc24a2182022-02-23 13:44:59 +000021#include "api/candidate.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "media/base/fake_rtp.h"
23#include "p2p/base/fake_dtls_transport.h"
24#include "p2p/base/fake_ice_transport.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000025#include "p2p/base/p2p_constants.h"
26#include "p2p/base/packet_transport_internal.h"
27#include "rtc_base/async_packet_socket.h"
28#include "rtc_base/buffer.h"
29#include "rtc_base/byte_order.h"
30#include "rtc_base/copy_on_write_buffer.h"
31#include "rtc_base/helpers.h"
32#include "rtc_base/logging.h"
33#include "rtc_base/net_helper.h"
34#include "rtc_base/ref_counted_object.h"
35#include "rtc_base/socket_address.h"
36#include "rtc_base/ssl_certificate.h"
37#include "rtc_base/ssl_identity.h"
38#include "rtc_base/third_party/sigslot/sigslot.h"
39#include "test/gtest.h"
Jonas Orelanded99dae2022-03-09 09:28:10 +010040#include "test/scoped_key_value_config.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080041
42namespace cricket {
Qingsi Wang25ec8882019-11-15 12:33:05 -080043namespace {
Zhi Huange818b6e2018-02-22 15:26:27 -080044using webrtc::SdpType;
45
46static const char kIceUfrag1[] = "U001";
47static const char kIcePwd1[] = "TESTICEPWD00000000000001";
48static const char kIceUfrag2[] = "U002";
49static const char kIcePwd2[] = "TESTIEPWD00000000000002";
50static const char kTransportName[] = "Test Transport";
51
Harald Alvestrand0d018412021-11-04 13:52:31 +000052enum class SrtpMode {
53 kSdes,
54 kDtlsSrtp,
55};
56
Zhi Huange818b6e2018-02-22 15:26:27 -080057struct NegotiateRoleParams {
58 ConnectionRole local_role;
59 ConnectionRole remote_role;
60 SdpType local_type;
61 SdpType remote_type;
62};
63
Harald Alvestrandefece422021-08-19 09:12:51 +000064std::ostream& operator<<(std::ostream& os, const ConnectionRole& role) {
65 std::string str = "invalid";
66 ConnectionRoleToString(role, &str);
67 os << str;
68 return os;
69}
70
71std::ostream& operator<<(std::ostream& os, const NegotiateRoleParams& param) {
72 os << "[Local role " << param.local_role << " Remote role "
73 << param.remote_role << " LocalType " << SdpTypeToString(param.local_type)
74 << " RemoteType " << SdpTypeToString(param.remote_type) << "]";
75 return os;
76}
77
Qingsi Wang25ec8882019-11-15 12:33:05 -080078rtc::scoped_refptr<webrtc::IceTransportInterface> CreateIceTransport(
79 std::unique_ptr<FakeIceTransport> internal) {
80 if (!internal) {
81 return nullptr;
82 }
83
Tommi87f70902021-04-27 14:43:08 +020084 return rtc::make_ref_counted<FakeIceTransportWrapper>(std::move(internal));
Qingsi Wang25ec8882019-11-15 12:33:05 -080085}
86
Mirko Bonadei6a489f22019-04-09 15:11:12 +020087class JsepTransport2Test : public ::testing::Test, public sigslot::has_slots<> {
Zhi Huange818b6e2018-02-22 15:26:27 -080088 protected:
89 std::unique_ptr<webrtc::SrtpTransport> CreateSdesTransport(
Zhi Huange818b6e2018-02-22 15:26:27 -080090 rtc::PacketTransportInternal* rtp_packet_transport,
91 rtc::PacketTransportInternal* rtcp_packet_transport) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +020092 auto srtp_transport = std::make_unique<webrtc::SrtpTransport>(
Jonas Orelanded99dae2022-03-09 09:28:10 +010093 rtcp_packet_transport == nullptr, field_trials_);
Zhi Huange818b6e2018-02-22 15:26:27 -080094
95 srtp_transport->SetRtpPacketTransport(rtp_packet_transport);
96 if (rtcp_packet_transport) {
97 srtp_transport->SetRtcpPacketTransport(rtp_packet_transport);
98 }
99 return srtp_transport;
100 }
101
102 std::unique_ptr<webrtc::DtlsSrtpTransport> CreateDtlsSrtpTransport(
Zhi Huange818b6e2018-02-22 15:26:27 -0800103 cricket::DtlsTransportInternal* rtp_dtls_transport,
104 cricket::DtlsTransportInternal* rtcp_dtls_transport) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200105 auto dtls_srtp_transport = std::make_unique<webrtc::DtlsSrtpTransport>(
Jonas Orelanded99dae2022-03-09 09:28:10 +0100106 rtcp_dtls_transport == nullptr, field_trials_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800107 dtls_srtp_transport->SetDtlsTransports(rtp_dtls_transport,
108 rtcp_dtls_transport);
109 return dtls_srtp_transport;
110 }
111
Zhi Huang365381f2018-04-13 16:44:34 -0700112 // Create a new JsepTransport with a FakeDtlsTransport and a
Zhi Huange818b6e2018-02-22 15:26:27 -0800113 // FakeIceTransport.
Harald Alvestrand0d018412021-11-04 13:52:31 +0000114 std::unique_ptr<JsepTransport> CreateJsepTransport2(bool rtcp_mux_enabled,
115 SrtpMode srtp_mode) {
Qingsi Wang25ec8882019-11-15 12:33:05 -0800116 auto ice_internal = std::make_unique<FakeIceTransport>(
117 kTransportName, ICE_CANDIDATE_COMPONENT_RTP);
118 auto rtp_dtls_transport =
119 std::make_unique<FakeDtlsTransport>(ice_internal.get());
120 auto ice = CreateIceTransport(std::move(ice_internal));
Zhi Huange818b6e2018-02-22 15:26:27 -0800121
Qingsi Wang25ec8882019-11-15 12:33:05 -0800122 std::unique_ptr<FakeIceTransport> rtcp_ice_internal;
Zhi Huange818b6e2018-02-22 15:26:27 -0800123 std::unique_ptr<FakeDtlsTransport> rtcp_dtls_transport;
124 if (!rtcp_mux_enabled) {
Qingsi Wang25ec8882019-11-15 12:33:05 -0800125 rtcp_ice_internal = std::make_unique<FakeIceTransport>(
Bjorn A Mellem0c1c1b42019-05-29 17:34:13 -0700126 kTransportName, ICE_CANDIDATE_COMPONENT_RTCP);
Qingsi Wang25ec8882019-11-15 12:33:05 -0800127 rtcp_dtls_transport =
128 std::make_unique<FakeDtlsTransport>(rtcp_ice_internal.get());
Zhi Huange818b6e2018-02-22 15:26:27 -0800129 }
Qingsi Wang25ec8882019-11-15 12:33:05 -0800130 auto rtcp_ice = CreateIceTransport(std::move(rtcp_ice_internal));
Zhi Huange818b6e2018-02-22 15:26:27 -0800131
132 std::unique_ptr<webrtc::RtpTransport> unencrypted_rtp_transport;
133 std::unique_ptr<webrtc::SrtpTransport> sdes_transport;
134 std::unique_ptr<webrtc::DtlsSrtpTransport> dtls_srtp_transport;
Harald Alvestrand0d018412021-11-04 13:52:31 +0000135 switch (srtp_mode) {
136 case SrtpMode::kSdes:
137 sdes_transport = CreateSdesTransport(rtp_dtls_transport.get(),
138 rtcp_dtls_transport.get());
139 sdes_transport_ = sdes_transport.get();
140 break;
141 case SrtpMode::kDtlsSrtp:
142 dtls_srtp_transport = CreateDtlsSrtpTransport(
143 rtp_dtls_transport.get(), rtcp_dtls_transport.get());
144 break;
145 default:
Artem Titovd3251962021-11-15 16:57:07 +0100146 RTC_DCHECK_NOTREACHED();
Harald Alvestrand0d018412021-11-04 13:52:31 +0000147 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800148
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200149 auto jsep_transport = std::make_unique<JsepTransport>(
Bjorn A Mellem0c1c1b42019-05-29 17:34:13 -0700150 kTransportName, /*local_certificate=*/nullptr, std::move(ice),
151 std::move(rtcp_ice), std::move(unencrypted_rtp_transport),
152 std::move(sdes_transport), std::move(dtls_srtp_transport),
Niels Möllerab9d6e12021-02-02 16:49:02 +0100153 std::move(rtp_dtls_transport), std::move(rtcp_dtls_transport),
Mirko Bonadei96dca922021-07-10 22:37:40 +0200154 /*sctp_transport=*/nullptr,
155 /*rtcp_mux_active_callback=*/[&]() { OnRtcpMuxActive(); });
Zhi Huange818b6e2018-02-22 15:26:27 -0800156
157 signal_rtcp_mux_active_received_ = false;
Zhi Huange830e682018-03-30 10:48:35 -0700158 return jsep_transport;
Zhi Huange818b6e2018-02-22 15:26:27 -0800159 }
160
161 JsepTransportDescription MakeJsepTransportDescription(
162 bool rtcp_mux_enabled,
163 const char* ufrag,
164 const char* pwd,
165 const rtc::scoped_refptr<rtc::RTCCertificate>& cert,
166 ConnectionRole role = CONNECTIONROLE_NONE) {
167 JsepTransportDescription jsep_description;
168 jsep_description.rtcp_mux_enabled = rtcp_mux_enabled;
169
170 std::unique_ptr<rtc::SSLFingerprint> fingerprint;
171 if (cert) {
Steve Anton4905edb2018-10-15 19:27:44 -0700172 fingerprint = rtc::SSLFingerprint::CreateFromCertificate(*cert);
Zhi Huange818b6e2018-02-22 15:26:27 -0800173 }
174 jsep_description.transport_desc =
175 TransportDescription(std::vector<std::string>(), ufrag, pwd,
176 ICEMODE_FULL, role, fingerprint.get());
177 return jsep_description;
178 }
179
180 Candidate CreateCandidate(int component) {
181 Candidate c;
182 c.set_address(rtc::SocketAddress("192.168.1.1", 8000));
183 c.set_component(component);
184 c.set_protocol(UDP_PROTOCOL_NAME);
185 c.set_priority(1);
186 return c;
187 }
188
189 void OnRtcpMuxActive() { signal_rtcp_mux_active_received_ = true; }
190
Zhi Huang365381f2018-04-13 16:44:34 -0700191 std::unique_ptr<JsepTransport> jsep_transport_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800192 bool signal_rtcp_mux_active_received_ = false;
Artem Titov880fa812021-07-30 22:30:23 +0200193 // The SrtpTransport is owned by `jsep_transport_`. Keep a raw pointer here
Zhi Huange818b6e2018-02-22 15:26:27 -0800194 // for testing.
195 webrtc::SrtpTransport* sdes_transport_ = nullptr;
Jonas Orelanded99dae2022-03-09 09:28:10 +0100196
197 webrtc::test::ScopedKeyValueConfig field_trials_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800198};
199
200// The parameterized tests cover both cases when RTCP mux is enable and
201// disabled.
202class JsepTransport2WithRtcpMux : public JsepTransport2Test,
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200203 public ::testing::WithParamInterface<bool> {};
Zhi Huange818b6e2018-02-22 15:26:27 -0800204
205// This test verifies the ICE parameters are properly applied to the transports.
206TEST_P(JsepTransport2WithRtcpMux, SetIceParameters) {
207 bool rtcp_mux_enabled = GetParam();
Harald Alvestrand0d018412021-11-04 13:52:31 +0000208 jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800209
210 JsepTransportDescription jsep_description;
211 jsep_description.transport_desc = TransportDescription(kIceUfrag1, kIcePwd1);
212 jsep_description.rtcp_mux_enabled = rtcp_mux_enabled;
213 ASSERT_TRUE(
214 jsep_transport_
215 ->SetLocalJsepTransportDescription(jsep_description, SdpType::kOffer)
216 .ok());
217 auto fake_ice_transport = static_cast<FakeIceTransport*>(
218 jsep_transport_->rtp_dtls_transport()->ice_transport());
219 EXPECT_EQ(ICEMODE_FULL, fake_ice_transport->remote_ice_mode());
220 EXPECT_EQ(kIceUfrag1, fake_ice_transport->ice_ufrag());
221 EXPECT_EQ(kIcePwd1, fake_ice_transport->ice_pwd());
222 if (!rtcp_mux_enabled) {
223 fake_ice_transport = static_cast<FakeIceTransport*>(
224 jsep_transport_->rtcp_dtls_transport()->ice_transport());
225 ASSERT_TRUE(fake_ice_transport);
226 EXPECT_EQ(ICEMODE_FULL, fake_ice_transport->remote_ice_mode());
227 EXPECT_EQ(kIceUfrag1, fake_ice_transport->ice_ufrag());
228 EXPECT_EQ(kIcePwd1, fake_ice_transport->ice_pwd());
229 }
230
231 jsep_description.transport_desc = TransportDescription(kIceUfrag2, kIcePwd2);
232 ASSERT_TRUE(jsep_transport_
233 ->SetRemoteJsepTransportDescription(jsep_description,
234 SdpType::kAnswer)
235 .ok());
236 fake_ice_transport = static_cast<FakeIceTransport*>(
237 jsep_transport_->rtp_dtls_transport()->ice_transport());
238 EXPECT_EQ(ICEMODE_FULL, fake_ice_transport->remote_ice_mode());
239 EXPECT_EQ(kIceUfrag2, fake_ice_transport->remote_ice_ufrag());
240 EXPECT_EQ(kIcePwd2, fake_ice_transport->remote_ice_pwd());
241 if (!rtcp_mux_enabled) {
242 fake_ice_transport = static_cast<FakeIceTransport*>(
243 jsep_transport_->rtcp_dtls_transport()->ice_transport());
244 ASSERT_TRUE(fake_ice_transport);
245 EXPECT_EQ(ICEMODE_FULL, fake_ice_transport->remote_ice_mode());
246 EXPECT_EQ(kIceUfrag2, fake_ice_transport->remote_ice_ufrag());
247 EXPECT_EQ(kIcePwd2, fake_ice_transport->remote_ice_pwd());
248 }
249}
250
251// Similarly, test DTLS parameters are properly applied to the transports.
252TEST_P(JsepTransport2WithRtcpMux, SetDtlsParameters) {
253 bool rtcp_mux_enabled = GetParam();
Harald Alvestrand0d018412021-11-04 13:52:31 +0000254 jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800255
256 // Create certificates.
257 rtc::scoped_refptr<rtc::RTCCertificate> local_cert =
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100258 rtc::RTCCertificate::Create(
259 rtc::SSLIdentity::Create("local", rtc::KT_DEFAULT));
Zhi Huange818b6e2018-02-22 15:26:27 -0800260 rtc::scoped_refptr<rtc::RTCCertificate> remote_cert =
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100261 rtc::RTCCertificate::Create(
262 rtc::SSLIdentity::Create("remote", rtc::KT_DEFAULT));
Zhi Huange818b6e2018-02-22 15:26:27 -0800263 jsep_transport_->SetLocalCertificate(local_cert);
264
265 // Apply offer.
266 JsepTransportDescription local_description =
267 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1,
268 local_cert, CONNECTIONROLE_ACTPASS);
269 ASSERT_TRUE(
270 jsep_transport_
271 ->SetLocalJsepTransportDescription(local_description, SdpType::kOffer)
272 .ok());
273 // Apply Answer.
274 JsepTransportDescription remote_description =
275 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2,
276 remote_cert, CONNECTIONROLE_ACTIVE);
277 ASSERT_TRUE(jsep_transport_
278 ->SetRemoteJsepTransportDescription(remote_description,
279 SdpType::kAnswer)
280 .ok());
281
282 // Verify that SSL role and remote fingerprint were set correctly based on
283 // transport descriptions.
284 auto role = jsep_transport_->GetDtlsRole();
285 ASSERT_TRUE(role);
286 EXPECT_EQ(rtc::SSL_SERVER, role); // Because remote description was "active".
287 auto fake_dtls =
288 static_cast<FakeDtlsTransport*>(jsep_transport_->rtp_dtls_transport());
289 EXPECT_EQ(remote_description.transport_desc.identity_fingerprint->ToString(),
290 fake_dtls->dtls_fingerprint().ToString());
291
292 if (!rtcp_mux_enabled) {
293 auto fake_rtcp_dtls =
294 static_cast<FakeDtlsTransport*>(jsep_transport_->rtcp_dtls_transport());
295 EXPECT_EQ(
296 remote_description.transport_desc.identity_fingerprint->ToString(),
297 fake_rtcp_dtls->dtls_fingerprint().ToString());
298 }
299}
300
301// Same as above test, but with remote transport description using
302// CONNECTIONROLE_PASSIVE, expecting SSL_CLIENT role.
303TEST_P(JsepTransport2WithRtcpMux, SetDtlsParametersWithPassiveAnswer) {
304 bool rtcp_mux_enabled = GetParam();
Harald Alvestrand0d018412021-11-04 13:52:31 +0000305 jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800306
307 // Create certificates.
308 rtc::scoped_refptr<rtc::RTCCertificate> local_cert =
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100309 rtc::RTCCertificate::Create(
310 rtc::SSLIdentity::Create("local", rtc::KT_DEFAULT));
Zhi Huange818b6e2018-02-22 15:26:27 -0800311 rtc::scoped_refptr<rtc::RTCCertificate> remote_cert =
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100312 rtc::RTCCertificate::Create(
313 rtc::SSLIdentity::Create("remote", rtc::KT_DEFAULT));
Zhi Huange818b6e2018-02-22 15:26:27 -0800314 jsep_transport_->SetLocalCertificate(local_cert);
315
316 // Apply offer.
317 JsepTransportDescription local_description =
318 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1,
319 local_cert, CONNECTIONROLE_ACTPASS);
320 ASSERT_TRUE(
321 jsep_transport_
322 ->SetLocalJsepTransportDescription(local_description, SdpType::kOffer)
323 .ok());
324 // Apply Answer.
325 JsepTransportDescription remote_description =
326 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2,
327 remote_cert, CONNECTIONROLE_PASSIVE);
328 ASSERT_TRUE(jsep_transport_
329 ->SetRemoteJsepTransportDescription(remote_description,
330 SdpType::kAnswer)
331 .ok());
332
333 // Verify that SSL role and remote fingerprint were set correctly based on
334 // transport descriptions.
335 auto role = jsep_transport_->GetDtlsRole();
336 ASSERT_TRUE(role);
337 EXPECT_EQ(rtc::SSL_CLIENT,
338 role); // Because remote description was "passive".
339 auto fake_dtls =
340 static_cast<FakeDtlsTransport*>(jsep_transport_->rtp_dtls_transport());
341 EXPECT_EQ(remote_description.transport_desc.identity_fingerprint->ToString(),
342 fake_dtls->dtls_fingerprint().ToString());
343
344 if (!rtcp_mux_enabled) {
345 auto fake_rtcp_dtls =
346 static_cast<FakeDtlsTransport*>(jsep_transport_->rtcp_dtls_transport());
347 EXPECT_EQ(
348 remote_description.transport_desc.identity_fingerprint->ToString(),
349 fake_rtcp_dtls->dtls_fingerprint().ToString());
350 }
351}
352
353// Tests SetNeedsIceRestartFlag and need_ice_restart, ensuring needs_ice_restart
354// only starts returning "false" once an ICE restart has been initiated.
355TEST_P(JsepTransport2WithRtcpMux, NeedsIceRestart) {
356 bool rtcp_mux_enabled = GetParam();
Harald Alvestrand0d018412021-11-04 13:52:31 +0000357 jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800358
359 // Use the same JsepTransportDescription for both offer and answer.
360 JsepTransportDescription description;
361 description.transport_desc = TransportDescription(kIceUfrag1, kIcePwd1);
362 ASSERT_TRUE(
363 jsep_transport_
364 ->SetLocalJsepTransportDescription(description, SdpType::kOffer)
365 .ok());
366 ASSERT_TRUE(
367 jsep_transport_
368 ->SetRemoteJsepTransportDescription(description, SdpType::kAnswer)
369 .ok());
370 // Flag initially should be false.
371 EXPECT_FALSE(jsep_transport_->needs_ice_restart());
372
373 // After setting flag, it should be true.
374 jsep_transport_->SetNeedsIceRestartFlag();
375 EXPECT_TRUE(jsep_transport_->needs_ice_restart());
376
377 ASSERT_TRUE(
378 jsep_transport_
379 ->SetLocalJsepTransportDescription(description, SdpType::kOffer)
380 .ok());
381 ASSERT_TRUE(
382 jsep_transport_
383 ->SetRemoteJsepTransportDescription(description, SdpType::kAnswer)
384 .ok());
385 EXPECT_TRUE(jsep_transport_->needs_ice_restart());
386
387 // Doing an offer/answer that restarts ICE should clear the flag.
388 description.transport_desc = TransportDescription(kIceUfrag2, kIcePwd2);
389 ASSERT_TRUE(
390 jsep_transport_
391 ->SetLocalJsepTransportDescription(description, SdpType::kOffer)
392 .ok());
393 ASSERT_TRUE(
394 jsep_transport_
395 ->SetRemoteJsepTransportDescription(description, SdpType::kAnswer)
396 .ok());
397 EXPECT_FALSE(jsep_transport_->needs_ice_restart());
398}
399
400TEST_P(JsepTransport2WithRtcpMux, GetStats) {
401 bool rtcp_mux_enabled = GetParam();
Harald Alvestrand0d018412021-11-04 13:52:31 +0000402 jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800403
404 size_t expected_stats_size = rtcp_mux_enabled ? 1u : 2u;
405 TransportStats stats;
406 EXPECT_TRUE(jsep_transport_->GetStats(&stats));
407 EXPECT_EQ(expected_stats_size, stats.channel_stats.size());
408 EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTP, stats.channel_stats[0].component);
409 if (!rtcp_mux_enabled) {
410 EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTCP, stats.channel_stats[1].component);
411 }
412}
413
414// Tests that VerifyCertificateFingerprint only returns true when the
415// certificate matches the fingerprint.
416TEST_P(JsepTransport2WithRtcpMux, VerifyCertificateFingerprint) {
417 bool rtcp_mux_enabled = GetParam();
Harald Alvestrand0d018412021-11-04 13:52:31 +0000418 jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800419
420 EXPECT_FALSE(
421 jsep_transport_->VerifyCertificateFingerprint(nullptr, nullptr).ok());
422 rtc::KeyType key_types[] = {rtc::KT_RSA, rtc::KT_ECDSA};
423
424 for (auto& key_type : key_types) {
425 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100426 rtc::RTCCertificate::Create(
427 rtc::SSLIdentity::Create("testing", key_type));
Zhi Huange818b6e2018-02-22 15:26:27 -0800428 ASSERT_NE(nullptr, certificate);
429
430 std::string digest_algorithm;
Benjamin Wright6c6c9df2018-10-25 01:16:26 -0700431 ASSERT_TRUE(certificate->GetSSLCertificate().GetSignatureDigestAlgorithm(
Zhi Huange818b6e2018-02-22 15:26:27 -0800432 &digest_algorithm));
433 ASSERT_FALSE(digest_algorithm.empty());
Steve Anton4905edb2018-10-15 19:27:44 -0700434 std::unique_ptr<rtc::SSLFingerprint> good_fingerprint =
435 rtc::SSLFingerprint::CreateUnique(digest_algorithm,
436 *certificate->identity());
Zhi Huange818b6e2018-02-22 15:26:27 -0800437 ASSERT_NE(nullptr, good_fingerprint);
438
439 EXPECT_TRUE(jsep_transport_
440 ->VerifyCertificateFingerprint(certificate.get(),
441 good_fingerprint.get())
442 .ok());
443 EXPECT_FALSE(jsep_transport_
444 ->VerifyCertificateFingerprint(certificate.get(), nullptr)
445 .ok());
446 EXPECT_FALSE(
447 jsep_transport_
448 ->VerifyCertificateFingerprint(nullptr, good_fingerprint.get())
449 .ok());
450
451 rtc::SSLFingerprint bad_fingerprint = *good_fingerprint;
452 bad_fingerprint.digest.AppendData("0", 1);
453 EXPECT_FALSE(
454 jsep_transport_
455 ->VerifyCertificateFingerprint(certificate.get(), &bad_fingerprint)
456 .ok());
457 }
458}
459
460// Tests the logic of DTLS role negotiation for an initial offer/answer.
461TEST_P(JsepTransport2WithRtcpMux, ValidDtlsRoleNegotiation) {
462 bool rtcp_mux_enabled = GetParam();
463 // Just use the same certificate for both sides; doesn't really matter in a
464 // non end-to-end test.
465 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100466 rtc::RTCCertificate::Create(
467 rtc::SSLIdentity::Create("testing", rtc::KT_ECDSA));
Zhi Huange818b6e2018-02-22 15:26:27 -0800468
469 JsepTransportDescription local_description = MakeJsepTransportDescription(
470 rtcp_mux_enabled, kIceUfrag1, kIcePwd1, certificate);
471 JsepTransportDescription remote_description = MakeJsepTransportDescription(
472 rtcp_mux_enabled, kIceUfrag2, kIcePwd2, certificate);
473
474 // Parameters which set the SSL role to SSL_CLIENT.
475 NegotiateRoleParams valid_client_params[] = {
476 {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_ACTPASS, SdpType::kAnswer,
477 SdpType::kOffer},
478 {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_ACTPASS, SdpType::kPrAnswer,
479 SdpType::kOffer},
480 {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_PASSIVE, SdpType::kOffer,
481 SdpType::kAnswer},
482 {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_PASSIVE, SdpType::kOffer,
Harald Alvestrandefece422021-08-19 09:12:51 +0000483 SdpType::kPrAnswer},
484 // Combinations permitted by RFC 8842 section 5.3
485 {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_PASSIVE, SdpType::kAnswer,
486 SdpType::kOffer},
487 {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_PASSIVE, SdpType::kPrAnswer,
488 SdpType::kOffer},
489 };
Zhi Huange818b6e2018-02-22 15:26:27 -0800490
491 for (auto& param : valid_client_params) {
Harald Alvestrand0d018412021-11-04 13:52:31 +0000492 jsep_transport_ =
493 CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800494 jsep_transport_->SetLocalCertificate(certificate);
495
496 local_description.transport_desc.connection_role = param.local_role;
497 remote_description.transport_desc.connection_role = param.remote_role;
498
499 // Set the offer first.
500 if (param.local_type == SdpType::kOffer) {
501 EXPECT_TRUE(jsep_transport_
502 ->SetLocalJsepTransportDescription(local_description,
503 param.local_type)
504 .ok());
505 EXPECT_TRUE(jsep_transport_
506 ->SetRemoteJsepTransportDescription(remote_description,
507 param.remote_type)
508 .ok());
509 } else {
510 EXPECT_TRUE(jsep_transport_
511 ->SetRemoteJsepTransportDescription(remote_description,
512 param.remote_type)
513 .ok());
514 EXPECT_TRUE(jsep_transport_
515 ->SetLocalJsepTransportDescription(local_description,
516 param.local_type)
517 .ok());
518 }
519 EXPECT_EQ(rtc::SSL_CLIENT, *jsep_transport_->GetDtlsRole());
520 }
521
522 // Parameters which set the SSL role to SSL_SERVER.
523 NegotiateRoleParams valid_server_params[] = {
524 {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_ACTPASS, SdpType::kAnswer,
525 SdpType::kOffer},
526 {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_ACTPASS, SdpType::kPrAnswer,
527 SdpType::kOffer},
528 {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_ACTIVE, SdpType::kOffer,
529 SdpType::kAnswer},
530 {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_ACTIVE, SdpType::kOffer,
Harald Alvestrandefece422021-08-19 09:12:51 +0000531 SdpType::kPrAnswer},
532 // Combinations permitted by RFC 8842 section 5.3
533 {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_ACTIVE, SdpType::kPrAnswer,
534 SdpType::kOffer},
535 };
Zhi Huange818b6e2018-02-22 15:26:27 -0800536
537 for (auto& param : valid_server_params) {
Harald Alvestrand0d018412021-11-04 13:52:31 +0000538 jsep_transport_ =
539 CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800540 jsep_transport_->SetLocalCertificate(certificate);
541
542 local_description.transport_desc.connection_role = param.local_role;
543 remote_description.transport_desc.connection_role = param.remote_role;
544
545 // Set the offer first.
546 if (param.local_type == SdpType::kOffer) {
547 EXPECT_TRUE(jsep_transport_
548 ->SetLocalJsepTransportDescription(local_description,
549 param.local_type)
550 .ok());
551 EXPECT_TRUE(jsep_transport_
552 ->SetRemoteJsepTransportDescription(remote_description,
553 param.remote_type)
554 .ok());
555 } else {
556 EXPECT_TRUE(jsep_transport_
557 ->SetRemoteJsepTransportDescription(remote_description,
558 param.remote_type)
559 .ok());
560 EXPECT_TRUE(jsep_transport_
561 ->SetLocalJsepTransportDescription(local_description,
562 param.local_type)
563 .ok());
564 }
565 EXPECT_EQ(rtc::SSL_SERVER, *jsep_transport_->GetDtlsRole());
566 }
567}
568
569// Tests the logic of DTLS role negotiation for an initial offer/answer.
570TEST_P(JsepTransport2WithRtcpMux, InvalidDtlsRoleNegotiation) {
571 bool rtcp_mux_enabled = GetParam();
572 // Just use the same certificate for both sides; doesn't really matter in a
573 // non end-to-end test.
574 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100575 rtc::RTCCertificate::Create(
576 rtc::SSLIdentity::Create("testing", rtc::KT_ECDSA));
Zhi Huange818b6e2018-02-22 15:26:27 -0800577
578 JsepTransportDescription local_description = MakeJsepTransportDescription(
579 rtcp_mux_enabled, kIceUfrag1, kIcePwd1, certificate);
580 JsepTransportDescription remote_description = MakeJsepTransportDescription(
581 rtcp_mux_enabled, kIceUfrag2, kIcePwd2, certificate);
582
583 NegotiateRoleParams duplicate_params[] = {
584 {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_ACTIVE, SdpType::kAnswer,
585 SdpType::kOffer},
586 {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_ACTPASS, SdpType::kAnswer,
587 SdpType::kOffer},
588 {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_PASSIVE, SdpType::kAnswer,
589 SdpType::kOffer},
590 {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_ACTIVE, SdpType::kPrAnswer,
591 SdpType::kOffer},
592 {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_ACTPASS, SdpType::kPrAnswer,
593 SdpType::kOffer},
594 {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_PASSIVE, SdpType::kPrAnswer,
595 SdpType::kOffer},
596 {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_ACTIVE, SdpType::kOffer,
597 SdpType::kAnswer},
598 {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_ACTPASS, SdpType::kOffer,
599 SdpType::kAnswer},
600 {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_PASSIVE, SdpType::kOffer,
601 SdpType::kAnswer},
602 {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_ACTIVE, SdpType::kOffer,
603 SdpType::kPrAnswer},
604 {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_ACTPASS, SdpType::kOffer,
605 SdpType::kPrAnswer},
606 {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_PASSIVE, SdpType::kOffer,
607 SdpType::kPrAnswer}};
608
609 for (auto& param : duplicate_params) {
Harald Alvestrand0d018412021-11-04 13:52:31 +0000610 jsep_transport_ =
611 CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800612 jsep_transport_->SetLocalCertificate(certificate);
613
614 local_description.transport_desc.connection_role = param.local_role;
615 remote_description.transport_desc.connection_role = param.remote_role;
616
617 if (param.local_type == SdpType::kOffer) {
618 EXPECT_TRUE(jsep_transport_
619 ->SetLocalJsepTransportDescription(local_description,
620 param.local_type)
621 .ok());
622 EXPECT_FALSE(jsep_transport_
623 ->SetRemoteJsepTransportDescription(remote_description,
624 param.remote_type)
625 .ok());
626 } else {
627 EXPECT_TRUE(jsep_transport_
628 ->SetRemoteJsepTransportDescription(remote_description,
629 param.remote_type)
630 .ok());
631 EXPECT_FALSE(jsep_transport_
632 ->SetLocalJsepTransportDescription(local_description,
633 param.local_type)
634 .ok());
635 }
636 }
637
Harald Alvestrandefece422021-08-19 09:12:51 +0000638 // Invalid parameters due to the offerer not using a role consistent with the
639 // state
Zhi Huange818b6e2018-02-22 15:26:27 -0800640 NegotiateRoleParams offerer_without_actpass_params[] = {
Harald Alvestrandefece422021-08-19 09:12:51 +0000641 // Cannot use ACTPASS in an answer
Zhi Huange818b6e2018-02-22 15:26:27 -0800642 {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_PASSIVE, SdpType::kAnswer,
643 SdpType::kOffer},
Zhi Huange818b6e2018-02-22 15:26:27 -0800644 {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_PASSIVE, SdpType::kPrAnswer,
645 SdpType::kOffer},
Harald Alvestrandefece422021-08-19 09:12:51 +0000646 // Cannot send ACTIVE or PASSIVE in an offer (must handle, must not send)
Zhi Huange818b6e2018-02-22 15:26:27 -0800647 {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_PASSIVE, SdpType::kOffer,
648 SdpType::kAnswer},
649 {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_ACTIVE, SdpType::kOffer,
650 SdpType::kAnswer},
651 {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_ACTPASS, SdpType::kOffer,
652 SdpType::kAnswer},
653 {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_PASSIVE, SdpType::kOffer,
654 SdpType::kPrAnswer},
655 {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_ACTIVE, SdpType::kOffer,
656 SdpType::kPrAnswer},
657 {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_ACTPASS, SdpType::kOffer,
658 SdpType::kPrAnswer}};
659
660 for (auto& param : offerer_without_actpass_params) {
Harald Alvestrand0d018412021-11-04 13:52:31 +0000661 jsep_transport_ =
662 CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800663 jsep_transport_->SetLocalCertificate(certificate);
664
665 local_description.transport_desc.connection_role = param.local_role;
666 remote_description.transport_desc.connection_role = param.remote_role;
667
668 if (param.local_type == SdpType::kOffer) {
669 EXPECT_TRUE(jsep_transport_
670 ->SetLocalJsepTransportDescription(local_description,
671 param.local_type)
Harald Alvestrandefece422021-08-19 09:12:51 +0000672 .ok())
673 << param;
Zhi Huange818b6e2018-02-22 15:26:27 -0800674 EXPECT_FALSE(jsep_transport_
675 ->SetRemoteJsepTransportDescription(remote_description,
676 param.remote_type)
Harald Alvestrandefece422021-08-19 09:12:51 +0000677 .ok())
678 << param;
Zhi Huange818b6e2018-02-22 15:26:27 -0800679 } else {
680 EXPECT_TRUE(jsep_transport_
681 ->SetRemoteJsepTransportDescription(remote_description,
682 param.remote_type)
Harald Alvestrandefece422021-08-19 09:12:51 +0000683 .ok())
684 << param;
Zhi Huange818b6e2018-02-22 15:26:27 -0800685 EXPECT_FALSE(jsep_transport_
686 ->SetLocalJsepTransportDescription(local_description,
687 param.local_type)
Harald Alvestrandefece422021-08-19 09:12:51 +0000688 .ok())
689 << param;
Zhi Huange818b6e2018-02-22 15:26:27 -0800690 }
691 }
692}
693
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100694INSTANTIATE_TEST_SUITE_P(JsepTransport2Test,
695 JsepTransport2WithRtcpMux,
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200696 ::testing::Bool());
Zhi Huange818b6e2018-02-22 15:26:27 -0800697
698// Test that a reoffer in the opposite direction is successful as long as the
699// role isn't changing. Doesn't test every possible combination like the test
700// above.
701TEST_F(JsepTransport2Test, ValidDtlsReofferFromAnswerer) {
702 // Just use the same certificate for both sides; doesn't really matter in a
703 // non end-to-end test.
704 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100705 rtc::RTCCertificate::Create(
706 rtc::SSLIdentity::Create("testing", rtc::KT_ECDSA));
Zhi Huange818b6e2018-02-22 15:26:27 -0800707 bool rtcp_mux_enabled = true;
Harald Alvestrand0d018412021-11-04 13:52:31 +0000708 jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800709 jsep_transport_->SetLocalCertificate(certificate);
710
711 JsepTransportDescription local_offer =
712 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1,
713 certificate, CONNECTIONROLE_ACTPASS);
714 JsepTransportDescription remote_answer =
715 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2,
716 certificate, CONNECTIONROLE_ACTIVE);
717
718 EXPECT_TRUE(
719 jsep_transport_
720 ->SetLocalJsepTransportDescription(local_offer, SdpType::kOffer)
721 .ok());
722 EXPECT_TRUE(
723 jsep_transport_
724 ->SetRemoteJsepTransportDescription(remote_answer, SdpType::kAnswer)
725 .ok());
726
727 // We were actpass->active previously, now in the other direction it's
728 // actpass->passive.
729 JsepTransportDescription remote_offer =
730 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2,
731 certificate, CONNECTIONROLE_ACTPASS);
732 JsepTransportDescription local_answer =
733 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1,
734 certificate, CONNECTIONROLE_PASSIVE);
735
736 EXPECT_TRUE(
737 jsep_transport_
738 ->SetRemoteJsepTransportDescription(remote_offer, SdpType::kOffer)
739 .ok());
740 EXPECT_TRUE(
741 jsep_transport_
742 ->SetLocalJsepTransportDescription(local_answer, SdpType::kAnswer)
743 .ok());
744}
745
746// Test that a reoffer in the opposite direction fails if the role changes.
747// Inverse of test above.
748TEST_F(JsepTransport2Test, InvalidDtlsReofferFromAnswerer) {
749 // Just use the same certificate for both sides; doesn't really matter in a
750 // non end-to-end test.
751 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100752 rtc::RTCCertificate::Create(
753 rtc::SSLIdentity::Create("testing", rtc::KT_ECDSA));
Zhi Huange818b6e2018-02-22 15:26:27 -0800754 bool rtcp_mux_enabled = true;
Harald Alvestrand0d018412021-11-04 13:52:31 +0000755 jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800756 jsep_transport_->SetLocalCertificate(certificate);
757
758 JsepTransportDescription local_offer =
759 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1,
760 certificate, CONNECTIONROLE_ACTPASS);
761 JsepTransportDescription remote_answer =
762 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2,
763 certificate, CONNECTIONROLE_ACTIVE);
764
765 EXPECT_TRUE(
766 jsep_transport_
767 ->SetLocalJsepTransportDescription(local_offer, SdpType::kOffer)
768 .ok());
769 EXPECT_TRUE(
770 jsep_transport_
771 ->SetRemoteJsepTransportDescription(remote_answer, SdpType::kAnswer)
772 .ok());
773
774 // Changing role to passive here isn't allowed. Though for some reason this
775 // only fails in SetLocalTransportDescription.
776 JsepTransportDescription remote_offer =
777 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2,
778 certificate, CONNECTIONROLE_PASSIVE);
779 JsepTransportDescription local_answer =
780 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1,
781 certificate, CONNECTIONROLE_ACTIVE);
782
783 EXPECT_TRUE(
784 jsep_transport_
785 ->SetRemoteJsepTransportDescription(remote_offer, SdpType::kOffer)
786 .ok());
787 EXPECT_FALSE(
788 jsep_transport_
789 ->SetLocalJsepTransportDescription(local_answer, SdpType::kAnswer)
790 .ok());
791}
792
793// Test that a remote offer with the current negotiated role can be accepted.
794// This is allowed by dtls-sdp, though we'll never generate such an offer,
795// since JSEP requires generating "actpass".
796TEST_F(JsepTransport2Test, RemoteOfferWithCurrentNegotiatedDtlsRole) {
797 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100798 rtc::RTCCertificate::Create(
799 rtc::SSLIdentity::Create("testing", rtc::KT_ECDSA));
Zhi Huange818b6e2018-02-22 15:26:27 -0800800 bool rtcp_mux_enabled = true;
Harald Alvestrand0d018412021-11-04 13:52:31 +0000801 jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800802 jsep_transport_->SetLocalCertificate(certificate);
803
804 JsepTransportDescription remote_desc =
805 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1,
806 certificate, CONNECTIONROLE_ACTPASS);
807 JsepTransportDescription local_desc =
808 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2,
809 certificate, CONNECTIONROLE_ACTIVE);
810
811 // Normal initial offer/answer with "actpass" in the offer and "active" in
812 // the answer.
813 ASSERT_TRUE(
814 jsep_transport_
815 ->SetRemoteJsepTransportDescription(remote_desc, SdpType::kOffer)
816 .ok());
817 ASSERT_TRUE(
818 jsep_transport_
819 ->SetLocalJsepTransportDescription(local_desc, SdpType::kAnswer)
820 .ok());
821
822 // Sanity check that role was actually negotiated.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200823 absl::optional<rtc::SSLRole> role = jsep_transport_->GetDtlsRole();
Zhi Huange818b6e2018-02-22 15:26:27 -0800824 ASSERT_TRUE(role);
825 EXPECT_EQ(rtc::SSL_CLIENT, *role);
826
827 // Subsequent offer with current negotiated role of "passive".
828 remote_desc.transport_desc.connection_role = CONNECTIONROLE_PASSIVE;
829 EXPECT_TRUE(
830 jsep_transport_
831 ->SetRemoteJsepTransportDescription(remote_desc, SdpType::kOffer)
832 .ok());
833 EXPECT_TRUE(
834 jsep_transport_
835 ->SetLocalJsepTransportDescription(local_desc, SdpType::kAnswer)
836 .ok());
837}
838
839// Test that a remote offer with the inverse of the current negotiated DTLS
840// role is rejected.
841TEST_F(JsepTransport2Test, RemoteOfferThatChangesNegotiatedDtlsRole) {
842 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100843 rtc::RTCCertificate::Create(
844 rtc::SSLIdentity::Create("testing", rtc::KT_ECDSA));
Zhi Huange818b6e2018-02-22 15:26:27 -0800845 bool rtcp_mux_enabled = true;
Harald Alvestrand0d018412021-11-04 13:52:31 +0000846 jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800847 jsep_transport_->SetLocalCertificate(certificate);
848
849 JsepTransportDescription remote_desc =
850 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1,
851 certificate, CONNECTIONROLE_ACTPASS);
852 JsepTransportDescription local_desc =
853 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2,
854 certificate, CONNECTIONROLE_ACTIVE);
855
856 // Normal initial offer/answer with "actpass" in the offer and "active" in
857 // the answer.
858 ASSERT_TRUE(
859 jsep_transport_
860 ->SetRemoteJsepTransportDescription(remote_desc, SdpType::kOffer)
861 .ok());
862 ASSERT_TRUE(
863 jsep_transport_
864 ->SetLocalJsepTransportDescription(local_desc, SdpType::kAnswer)
865 .ok());
866
867 // Sanity check that role was actually negotiated.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200868 absl::optional<rtc::SSLRole> role = jsep_transport_->GetDtlsRole();
Zhi Huange818b6e2018-02-22 15:26:27 -0800869 ASSERT_TRUE(role);
870 EXPECT_EQ(rtc::SSL_CLIENT, *role);
871
872 // Subsequent offer with current negotiated role of "passive".
873 remote_desc.transport_desc.connection_role = CONNECTIONROLE_ACTIVE;
874 EXPECT_TRUE(
875 jsep_transport_
876 ->SetRemoteJsepTransportDescription(remote_desc, SdpType::kOffer)
877 .ok());
878 EXPECT_FALSE(
879 jsep_transport_
880 ->SetLocalJsepTransportDescription(local_desc, SdpType::kAnswer)
881 .ok());
882}
883
884// Testing that a legacy client that doesn't use the setup attribute will be
885// interpreted as having an active role.
886TEST_F(JsepTransport2Test, DtlsSetupWithLegacyAsAnswerer) {
887 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100888 rtc::RTCCertificate::Create(
889 rtc::SSLIdentity::Create("testing", rtc::KT_ECDSA));
Zhi Huange818b6e2018-02-22 15:26:27 -0800890 bool rtcp_mux_enabled = true;
Harald Alvestrand0d018412021-11-04 13:52:31 +0000891 jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800892 jsep_transport_->SetLocalCertificate(certificate);
893
894 JsepTransportDescription remote_desc =
895 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1,
896 certificate, CONNECTIONROLE_ACTPASS);
897 JsepTransportDescription local_desc =
898 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2,
899 certificate, CONNECTIONROLE_ACTIVE);
900
901 local_desc.transport_desc.connection_role = CONNECTIONROLE_ACTPASS;
902 ASSERT_TRUE(
903 jsep_transport_
904 ->SetLocalJsepTransportDescription(local_desc, SdpType::kOffer)
905 .ok());
906 // Use CONNECTIONROLE_NONE to simulate legacy endpoint.
907 remote_desc.transport_desc.connection_role = CONNECTIONROLE_NONE;
908 ASSERT_TRUE(
909 jsep_transport_
910 ->SetRemoteJsepTransportDescription(remote_desc, SdpType::kAnswer)
911 .ok());
912
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200913 absl::optional<rtc::SSLRole> role = jsep_transport_->GetDtlsRole();
Zhi Huange818b6e2018-02-22 15:26:27 -0800914 ASSERT_TRUE(role);
915 // Since legacy answer ommitted setup atribute, and we offered actpass, we
916 // should act as passive (server).
917 EXPECT_EQ(rtc::SSL_SERVER, *role);
918}
919
920// Tests that when the RTCP mux is successfully negotiated, the RTCP transport
921// will be destroyed and the SignalRtpMuxActive will be fired.
922TEST_F(JsepTransport2Test, RtcpMuxNegotiation) {
Harald Alvestrand0d018412021-11-04 13:52:31 +0000923 jsep_transport_ =
924 CreateJsepTransport2(/*rtcp_mux_enabled=*/false, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800925 JsepTransportDescription local_desc;
926 local_desc.rtcp_mux_enabled = true;
Harald Alvestrandad88c882018-11-28 16:47:46 +0100927 ASSERT_NE(nullptr, jsep_transport_->rtcp_dtls_transport());
Zhi Huange818b6e2018-02-22 15:26:27 -0800928 EXPECT_FALSE(signal_rtcp_mux_active_received_);
929
930 // The remote side supports RTCP-mux.
931 JsepTransportDescription remote_desc;
932 remote_desc.rtcp_mux_enabled = true;
933 ASSERT_TRUE(
934 jsep_transport_
935 ->SetLocalJsepTransportDescription(local_desc, SdpType::kOffer)
936 .ok());
937 ASSERT_TRUE(
938 jsep_transport_
939 ->SetRemoteJsepTransportDescription(remote_desc, SdpType::kAnswer)
940 .ok());
941
942 EXPECT_EQ(nullptr, jsep_transport_->rtcp_dtls_transport());
943 EXPECT_TRUE(signal_rtcp_mux_active_received_);
944
945 // The remote side doesn't support RTCP-mux.
Harald Alvestrand0d018412021-11-04 13:52:31 +0000946 jsep_transport_ =
947 CreateJsepTransport2(/*rtcp_mux_enabled=*/false, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800948 signal_rtcp_mux_active_received_ = false;
949 remote_desc.rtcp_mux_enabled = false;
950 ASSERT_TRUE(
951 jsep_transport_
952 ->SetLocalJsepTransportDescription(local_desc, SdpType::kOffer)
953 .ok());
954 ASSERT_TRUE(
955 jsep_transport_
956 ->SetRemoteJsepTransportDescription(remote_desc, SdpType::kAnswer)
957 .ok());
958
959 EXPECT_NE(nullptr, jsep_transport_->rtcp_dtls_transport());
960 EXPECT_FALSE(signal_rtcp_mux_active_received_);
961}
962
Harald Alvestrand0d018412021-11-04 13:52:31 +0000963TEST_F(JsepTransport2Test, SdesNegotiation) {
964 jsep_transport_ =
965 CreateJsepTransport2(/*rtcp_mux_enabled=*/true, SrtpMode::kSdes);
966 ASSERT_TRUE(sdes_transport_);
967 EXPECT_FALSE(sdes_transport_->IsSrtpActive());
968
969 JsepTransportDescription offer_desc;
970 offer_desc.cryptos.push_back(cricket::CryptoParams(
971 1, rtc::kCsAesCm128HmacSha1_32, "inline:" + rtc::CreateRandomString(40),
972 std::string()));
973 ASSERT_TRUE(
974 jsep_transport_
975 ->SetLocalJsepTransportDescription(offer_desc, SdpType::kOffer)
976 .ok());
977
978 JsepTransportDescription answer_desc;
979 answer_desc.cryptos.push_back(cricket::CryptoParams(
980 1, rtc::kCsAesCm128HmacSha1_32, "inline:" + rtc::CreateRandomString(40),
981 std::string()));
982 ASSERT_TRUE(
983 jsep_transport_
984 ->SetRemoteJsepTransportDescription(answer_desc, SdpType::kAnswer)
985 .ok());
986 EXPECT_TRUE(sdes_transport_->IsSrtpActive());
987}
988
989TEST_F(JsepTransport2Test, SdesNegotiationWithEmptyCryptosInAnswer) {
990 jsep_transport_ =
991 CreateJsepTransport2(/*rtcp_mux_enabled=*/true, SrtpMode::kSdes);
992 ASSERT_TRUE(sdes_transport_);
993 EXPECT_FALSE(sdes_transport_->IsSrtpActive());
994
995 JsepTransportDescription offer_desc;
996 offer_desc.cryptos.push_back(cricket::CryptoParams(
997 1, rtc::kCsAesCm128HmacSha1_32, "inline:" + rtc::CreateRandomString(40),
998 std::string()));
999 ASSERT_TRUE(
1000 jsep_transport_
1001 ->SetLocalJsepTransportDescription(offer_desc, SdpType::kOffer)
1002 .ok());
1003
1004 JsepTransportDescription answer_desc;
1005 ASSERT_TRUE(
1006 jsep_transport_
1007 ->SetRemoteJsepTransportDescription(answer_desc, SdpType::kAnswer)
1008 .ok());
1009 // SRTP is not active because the crypto parameter is answer is empty.
1010 EXPECT_FALSE(sdes_transport_->IsSrtpActive());
1011}
1012
1013TEST_F(JsepTransport2Test, SdesNegotiationWithMismatchedCryptos) {
1014 jsep_transport_ =
1015 CreateJsepTransport2(/*rtcp_mux_enabled=*/true, SrtpMode::kSdes);
1016 ASSERT_TRUE(sdes_transport_);
1017 EXPECT_FALSE(sdes_transport_->IsSrtpActive());
1018
1019 JsepTransportDescription offer_desc;
1020 offer_desc.cryptos.push_back(cricket::CryptoParams(
1021 1, rtc::kCsAesCm128HmacSha1_32, "inline:" + rtc::CreateRandomString(40),
1022 std::string()));
1023 ASSERT_TRUE(
1024 jsep_transport_
1025 ->SetLocalJsepTransportDescription(offer_desc, SdpType::kOffer)
1026 .ok());
1027
1028 JsepTransportDescription answer_desc;
1029 answer_desc.cryptos.push_back(cricket::CryptoParams(
1030 1, rtc::kCsAesCm128HmacSha1_80, "inline:" + rtc::CreateRandomString(40),
1031 std::string()));
1032 // Expected to fail because the crypto parameters don't match.
1033 ASSERT_FALSE(
1034 jsep_transport_
1035 ->SetRemoteJsepTransportDescription(answer_desc, SdpType::kAnswer)
1036 .ok());
1037}
1038
Zhi Huange818b6e2018-02-22 15:26:27 -08001039// Tests that the remote candidates can be added to the transports after both
1040// local and remote descriptions are set.
1041TEST_F(JsepTransport2Test, AddRemoteCandidates) {
Harald Alvestrand0d018412021-11-04 13:52:31 +00001042 jsep_transport_ =
1043 CreateJsepTransport2(/*rtcp_mux_enabled=*/true, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -08001044 auto fake_ice_transport = static_cast<FakeIceTransport*>(
1045 jsep_transport_->rtp_dtls_transport()->ice_transport());
1046
1047 Candidates candidates;
1048 candidates.push_back(CreateCandidate(/*COMPONENT_RTP*/ 1));
1049 candidates.push_back(CreateCandidate(/*COMPONENT_RTP*/ 1));
1050
1051 JsepTransportDescription desc;
1052 ASSERT_TRUE(
1053 jsep_transport_->SetLocalJsepTransportDescription(desc, SdpType::kOffer)
1054 .ok());
1055 // Expected to fail because the remote description is unset.
1056 EXPECT_FALSE(jsep_transport_->AddRemoteCandidates(candidates).ok());
1057
1058 ASSERT_TRUE(
1059 jsep_transport_->SetRemoteJsepTransportDescription(desc, SdpType::kAnswer)
1060 .ok());
1061 EXPECT_EQ(0u, fake_ice_transport->remote_candidates().size());
1062 EXPECT_TRUE(jsep_transport_->AddRemoteCandidates(candidates).ok());
1063 EXPECT_EQ(candidates.size(), fake_ice_transport->remote_candidates().size());
1064}
1065
Zhi Huange830e682018-03-30 10:48:35 -07001066enum class Scenario {
Harald Alvestrand0d018412021-11-04 13:52:31 +00001067 kSdes,
Zhi Huange830e682018-03-30 10:48:35 -07001068 kDtlsBeforeCallerSendOffer,
1069 kDtlsBeforeCallerSetAnswer,
1070 kDtlsAfterCallerSetAnswer,
1071};
1072
1073class JsepTransport2HeaderExtensionTest
1074 : public JsepTransport2Test,
1075 public ::testing::WithParamInterface<std::tuple<Scenario, bool>> {
1076 protected:
1077 JsepTransport2HeaderExtensionTest() {}
1078
Harald Alvestrand0d018412021-11-04 13:52:31 +00001079 void CreateJsepTransportPair(SrtpMode mode) {
1080 jsep_transport1_ = CreateJsepTransport2(/*rtcp_mux_enabled=*/true, mode);
1081 jsep_transport2_ = CreateJsepTransport2(/*rtcp_mux_enabled=*/true, mode);
Zhi Huange830e682018-03-30 10:48:35 -07001082
1083 auto fake_dtls1 =
1084 static_cast<FakeDtlsTransport*>(jsep_transport1_->rtp_dtls_transport());
1085 auto fake_dtls2 =
1086 static_cast<FakeDtlsTransport*>(jsep_transport2_->rtp_dtls_transport());
1087
1088 fake_dtls1->fake_ice_transport()->SignalReadPacket.connect(
1089 this, &JsepTransport2HeaderExtensionTest::OnReadPacket1);
1090 fake_dtls2->fake_ice_transport()->SignalReadPacket.connect(
1091 this, &JsepTransport2HeaderExtensionTest::OnReadPacket2);
1092
Harald Alvestrand0d018412021-11-04 13:52:31 +00001093 if (mode == SrtpMode::kDtlsSrtp) {
1094 auto cert1 = rtc::RTCCertificate::Create(
1095 rtc::SSLIdentity::Create("session1", rtc::KT_DEFAULT));
1096 jsep_transport1_->rtp_dtls_transport()->SetLocalCertificate(cert1);
1097 auto cert2 = rtc::RTCCertificate::Create(
1098 rtc::SSLIdentity::Create("session1", rtc::KT_DEFAULT));
1099 jsep_transport2_->rtp_dtls_transport()->SetLocalCertificate(cert2);
1100 }
Zhi Huange830e682018-03-30 10:48:35 -07001101 }
1102
1103 void OnReadPacket1(rtc::PacketTransportInternal* transport,
1104 const char* data,
1105 size_t size,
Niels Möllere6933812018-11-05 13:01:41 +01001106 const int64_t& /* packet_time_us */,
Zhi Huange830e682018-03-30 10:48:35 -07001107 int flags) {
1108 RTC_LOG(LS_INFO) << "JsepTransport 1 Received a packet.";
1109 CompareHeaderExtensions(
1110 reinterpret_cast<const char*>(kPcmuFrameWithExtensions),
1111 sizeof(kPcmuFrameWithExtensions), data, size, recv_encrypted_headers1_,
1112 false);
1113 received_packet_count_++;
1114 }
1115
1116 void OnReadPacket2(rtc::PacketTransportInternal* transport,
1117 const char* data,
1118 size_t size,
Niels Möllere6933812018-11-05 13:01:41 +01001119 const int64_t& /* packet_time_us */,
Zhi Huange830e682018-03-30 10:48:35 -07001120 int flags) {
1121 RTC_LOG(LS_INFO) << "JsepTransport 2 Received a packet.";
1122 CompareHeaderExtensions(
1123 reinterpret_cast<const char*>(kPcmuFrameWithExtensions),
1124 sizeof(kPcmuFrameWithExtensions), data, size, recv_encrypted_headers2_,
1125 false);
1126 received_packet_count_++;
1127 }
1128
1129 void ConnectTransport() {
1130 auto rtp_dtls_transport1 =
1131 static_cast<FakeDtlsTransport*>(jsep_transport1_->rtp_dtls_transport());
1132 auto rtp_dtls_transport2 =
1133 static_cast<FakeDtlsTransport*>(jsep_transport2_->rtp_dtls_transport());
1134 rtp_dtls_transport1->SetDestination(rtp_dtls_transport2);
1135 }
1136
1137 int GetRtpAuthLen() {
1138 bool use_gcm = std::get<1>(GetParam());
1139 if (use_gcm) {
1140 return 16;
1141 }
1142 return 10;
1143 }
1144
1145 void TestSendRecvPacketWithEncryptedHeaderExtension() {
1146 TestOneWaySendRecvPacketWithEncryptedHeaderExtension(
1147 jsep_transport1_.get());
1148 TestOneWaySendRecvPacketWithEncryptedHeaderExtension(
1149 jsep_transport2_.get());
1150 }
1151
1152 void TestOneWaySendRecvPacketWithEncryptedHeaderExtension(
Zhi Huang365381f2018-04-13 16:44:34 -07001153 JsepTransport* sender_transport) {
Zhi Huange830e682018-03-30 10:48:35 -07001154 size_t rtp_len = sizeof(kPcmuFrameWithExtensions);
1155 size_t packet_size = rtp_len + GetRtpAuthLen();
1156 rtc::Buffer rtp_packet_buffer(packet_size);
1157 char* rtp_packet_data = rtp_packet_buffer.data<char>();
1158 memcpy(rtp_packet_data, kPcmuFrameWithExtensions, rtp_len);
1159 // In order to be able to run this test function multiple times we can not
1160 // use the same sequence number twice. Increase the sequence number by one.
1161 rtc::SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_data) + 2,
1162 ++sequence_number_);
1163 rtc::CopyOnWriteBuffer rtp_packet(rtp_packet_data, rtp_len, packet_size);
1164
1165 int packet_count_before = received_packet_count_;
1166 rtc::PacketOptions options;
1167 // Send a packet and verify that the packet can be successfully received and
1168 // decrypted.
1169 ASSERT_TRUE(sender_transport->rtp_transport()->SendRtpPacket(
1170 &rtp_packet, options, cricket::PF_SRTP_BYPASS));
1171 EXPECT_EQ(packet_count_before + 1, received_packet_count_);
1172 }
1173
1174 int sequence_number_ = 0;
1175 int received_packet_count_ = 0;
Zhi Huang365381f2018-04-13 16:44:34 -07001176 std::unique_ptr<JsepTransport> jsep_transport1_;
1177 std::unique_ptr<JsepTransport> jsep_transport2_;
Zhi Huange830e682018-03-30 10:48:35 -07001178 std::vector<int> recv_encrypted_headers1_;
1179 std::vector<int> recv_encrypted_headers2_;
1180};
1181
1182// Test that the encrypted header extension works and can be changed in
1183// different scenarios.
1184TEST_P(JsepTransport2HeaderExtensionTest, EncryptedHeaderExtensionNegotiation) {
1185 Scenario scenario = std::get<0>(GetParam());
1186 bool use_gcm = std::get<1>(GetParam());
Harald Alvestrand0d018412021-11-04 13:52:31 +00001187 SrtpMode mode = SrtpMode ::kDtlsSrtp;
1188 if (scenario == Scenario::kSdes) {
1189 mode = SrtpMode::kSdes;
1190 }
1191 CreateJsepTransportPair(mode);
Zhi Huange830e682018-03-30 10:48:35 -07001192 recv_encrypted_headers1_.push_back(kHeaderExtensionIDs[0]);
1193 recv_encrypted_headers2_.push_back(kHeaderExtensionIDs[1]);
1194
Harald Alvestrand0d018412021-11-04 13:52:31 +00001195 cricket::CryptoParams sdes_param(1, rtc::kCsAesCm128HmacSha1_80,
1196 "inline:" + rtc::CreateRandomString(40),
1197 std::string());
Zhi Huange830e682018-03-30 10:48:35 -07001198 if (use_gcm) {
1199 auto fake_dtls1 =
1200 static_cast<FakeDtlsTransport*>(jsep_transport1_->rtp_dtls_transport());
1201 auto fake_dtls2 =
1202 static_cast<FakeDtlsTransport*>(jsep_transport2_->rtp_dtls_transport());
1203
Mirko Bonadei7750d802021-07-26 17:27:42 +02001204 fake_dtls1->SetSrtpCryptoSuite(rtc::kSrtpAeadAes256Gcm);
1205 fake_dtls2->SetSrtpCryptoSuite(rtc::kSrtpAeadAes256Gcm);
Zhi Huange830e682018-03-30 10:48:35 -07001206 }
1207
1208 if (scenario == Scenario::kDtlsBeforeCallerSendOffer) {
1209 ConnectTransport();
1210 }
1211
1212 JsepTransportDescription offer_desc;
1213 offer_desc.encrypted_header_extension_ids = recv_encrypted_headers1_;
Harald Alvestrand0d018412021-11-04 13:52:31 +00001214 if (scenario == Scenario::kSdes) {
1215 offer_desc.cryptos.push_back(sdes_param);
1216 }
Zhi Huange830e682018-03-30 10:48:35 -07001217 ASSERT_TRUE(
1218 jsep_transport1_
1219 ->SetLocalJsepTransportDescription(offer_desc, SdpType::kOffer)
1220 .ok());
1221 ASSERT_TRUE(
1222 jsep_transport2_
1223 ->SetRemoteJsepTransportDescription(offer_desc, SdpType::kOffer)
1224 .ok());
1225
1226 JsepTransportDescription answer_desc;
1227 answer_desc.encrypted_header_extension_ids = recv_encrypted_headers2_;
Harald Alvestrand0d018412021-11-04 13:52:31 +00001228 if (scenario == Scenario::kSdes) {
1229 answer_desc.cryptos.push_back(sdes_param);
1230 }
Zhi Huange830e682018-03-30 10:48:35 -07001231 ASSERT_TRUE(
1232 jsep_transport2_
1233 ->SetLocalJsepTransportDescription(answer_desc, SdpType::kAnswer)
1234 .ok());
1235
1236 if (scenario == Scenario::kDtlsBeforeCallerSetAnswer) {
1237 ConnectTransport();
1238 // Sending packet from transport2 to transport1 should work when they are
1239 // partially configured.
1240 TestOneWaySendRecvPacketWithEncryptedHeaderExtension(
1241 /*sender_transport=*/jsep_transport2_.get());
1242 }
1243
1244 ASSERT_TRUE(
1245 jsep_transport1_
1246 ->SetRemoteJsepTransportDescription(answer_desc, SdpType::kAnswer)
1247 .ok());
1248
Harald Alvestrand0d018412021-11-04 13:52:31 +00001249 if (scenario == Scenario::kDtlsAfterCallerSetAnswer ||
1250 scenario == Scenario::kSdes) {
Zhi Huange830e682018-03-30 10:48:35 -07001251 ConnectTransport();
1252 }
1253 EXPECT_TRUE(jsep_transport1_->rtp_transport()->IsSrtpActive());
1254 EXPECT_TRUE(jsep_transport2_->rtp_transport()->IsSrtpActive());
1255 TestSendRecvPacketWithEncryptedHeaderExtension();
1256
1257 // Change the encrypted header extension in a new offer/answer exchange.
1258 recv_encrypted_headers1_.clear();
1259 recv_encrypted_headers2_.clear();
1260 recv_encrypted_headers1_.push_back(kHeaderExtensionIDs[1]);
1261 recv_encrypted_headers2_.push_back(kHeaderExtensionIDs[0]);
1262 offer_desc.encrypted_header_extension_ids = recv_encrypted_headers1_;
1263 answer_desc.encrypted_header_extension_ids = recv_encrypted_headers2_;
1264 ASSERT_TRUE(
1265 jsep_transport1_
1266 ->SetLocalJsepTransportDescription(offer_desc, SdpType::kOffer)
1267 .ok());
1268 ASSERT_TRUE(
1269 jsep_transport2_
1270 ->SetRemoteJsepTransportDescription(offer_desc, SdpType::kOffer)
1271 .ok());
1272 ASSERT_TRUE(
1273 jsep_transport2_
1274 ->SetLocalJsepTransportDescription(answer_desc, SdpType::kAnswer)
1275 .ok());
1276 ASSERT_TRUE(
1277 jsep_transport1_
1278 ->SetRemoteJsepTransportDescription(answer_desc, SdpType::kAnswer)
1279 .ok());
1280 EXPECT_TRUE(jsep_transport1_->rtp_transport()->IsSrtpActive());
1281 EXPECT_TRUE(jsep_transport2_->rtp_transport()->IsSrtpActive());
1282 TestSendRecvPacketWithEncryptedHeaderExtension();
1283}
1284
Mirko Bonadeic84f6612019-01-31 12:20:57 +01001285INSTANTIATE_TEST_SUITE_P(
Zhi Huange830e682018-03-30 10:48:35 -07001286 JsepTransport2Test,
1287 JsepTransport2HeaderExtensionTest,
1288 ::testing::Values(
Harald Alvestrand0d018412021-11-04 13:52:31 +00001289 std::make_tuple(Scenario::kSdes, false),
Zhi Huange830e682018-03-30 10:48:35 -07001290 std::make_tuple(Scenario::kDtlsBeforeCallerSendOffer, true),
1291 std::make_tuple(Scenario::kDtlsBeforeCallerSetAnswer, true),
1292 std::make_tuple(Scenario::kDtlsAfterCallerSetAnswer, true),
1293 std::make_tuple(Scenario::kDtlsBeforeCallerSendOffer, false),
1294 std::make_tuple(Scenario::kDtlsBeforeCallerSetAnswer, false),
1295 std::make_tuple(Scenario::kDtlsAfterCallerSetAnswer, false)));
Jonas Oreland52aea5d2020-03-03 13:21:30 +01001296
1297// This test verifies the ICE parameters are properly applied to the transports.
1298TEST_F(JsepTransport2Test, SetIceParametersWithRenomination) {
Harald Alvestrand0d018412021-11-04 13:52:31 +00001299 jsep_transport_ =
1300 CreateJsepTransport2(/* rtcp_mux_enabled= */ true, SrtpMode::kDtlsSrtp);
Jonas Oreland52aea5d2020-03-03 13:21:30 +01001301
1302 JsepTransportDescription jsep_description;
1303 jsep_description.transport_desc = TransportDescription(kIceUfrag1, kIcePwd1);
1304 jsep_description.transport_desc.AddOption(ICE_OPTION_RENOMINATION);
1305 ASSERT_TRUE(
1306 jsep_transport_
1307 ->SetLocalJsepTransportDescription(jsep_description, SdpType::kOffer)
1308 .ok());
1309 auto fake_ice_transport = static_cast<FakeIceTransport*>(
1310 jsep_transport_->rtp_dtls_transport()->ice_transport());
1311 EXPECT_EQ(ICEMODE_FULL, fake_ice_transport->remote_ice_mode());
1312 EXPECT_EQ(kIceUfrag1, fake_ice_transport->ice_ufrag());
1313 EXPECT_EQ(kIcePwd1, fake_ice_transport->ice_pwd());
1314 EXPECT_TRUE(fake_ice_transport->ice_parameters().renomination);
1315
1316 jsep_description.transport_desc = TransportDescription(kIceUfrag2, kIcePwd2);
1317 jsep_description.transport_desc.AddOption(ICE_OPTION_RENOMINATION);
1318 ASSERT_TRUE(jsep_transport_
1319 ->SetRemoteJsepTransportDescription(jsep_description,
1320 SdpType::kAnswer)
1321 .ok());
1322 fake_ice_transport = static_cast<FakeIceTransport*>(
1323 jsep_transport_->rtp_dtls_transport()->ice_transport());
1324 EXPECT_EQ(ICEMODE_FULL, fake_ice_transport->remote_ice_mode());
1325 EXPECT_EQ(kIceUfrag2, fake_ice_transport->remote_ice_ufrag());
1326 EXPECT_EQ(kIcePwd2, fake_ice_transport->remote_ice_pwd());
1327 EXPECT_TRUE(fake_ice_transport->remote_ice_parameters().renomination);
1328}
1329
Qingsi Wang25ec8882019-11-15 12:33:05 -08001330} // namespace
Zhi Huange818b6e2018-02-22 15:26:27 -08001331} // namespace cricket