blob: 2ec96d2af7882c8c59232a1a55f0e5bb70221e00 [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"
Zhi Huange818b6e2018-02-22 15:26:27 -080040
41namespace cricket {
Qingsi Wang25ec8882019-11-15 12:33:05 -080042namespace {
Zhi Huange818b6e2018-02-22 15:26:27 -080043using webrtc::SdpType;
44
45static const char kIceUfrag1[] = "U001";
46static const char kIcePwd1[] = "TESTICEPWD00000000000001";
47static const char kIceUfrag2[] = "U002";
48static const char kIcePwd2[] = "TESTIEPWD00000000000002";
49static const char kTransportName[] = "Test Transport";
50
Harald Alvestrand0d018412021-11-04 13:52:31 +000051enum class SrtpMode {
52 kSdes,
53 kDtlsSrtp,
54};
55
Zhi Huange818b6e2018-02-22 15:26:27 -080056struct NegotiateRoleParams {
57 ConnectionRole local_role;
58 ConnectionRole remote_role;
59 SdpType local_type;
60 SdpType remote_type;
61};
62
Harald Alvestrandefece422021-08-19 09:12:51 +000063std::ostream& operator<<(std::ostream& os, const ConnectionRole& role) {
64 std::string str = "invalid";
65 ConnectionRoleToString(role, &str);
66 os << str;
67 return os;
68}
69
70std::ostream& operator<<(std::ostream& os, const NegotiateRoleParams& param) {
71 os << "[Local role " << param.local_role << " Remote role "
72 << param.remote_role << " LocalType " << SdpTypeToString(param.local_type)
73 << " RemoteType " << SdpTypeToString(param.remote_type) << "]";
74 return os;
75}
76
Qingsi Wang25ec8882019-11-15 12:33:05 -080077rtc::scoped_refptr<webrtc::IceTransportInterface> CreateIceTransport(
78 std::unique_ptr<FakeIceTransport> internal) {
79 if (!internal) {
80 return nullptr;
81 }
82
Tommi87f70902021-04-27 14:43:08 +020083 return rtc::make_ref_counted<FakeIceTransportWrapper>(std::move(internal));
Qingsi Wang25ec8882019-11-15 12:33:05 -080084}
85
Mirko Bonadei6a489f22019-04-09 15:11:12 +020086class JsepTransport2Test : public ::testing::Test, public sigslot::has_slots<> {
Zhi Huange818b6e2018-02-22 15:26:27 -080087 protected:
88 std::unique_ptr<webrtc::SrtpTransport> CreateSdesTransport(
Zhi Huange818b6e2018-02-22 15:26:27 -080089 rtc::PacketTransportInternal* rtp_packet_transport,
90 rtc::PacketTransportInternal* rtcp_packet_transport) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +020091 auto srtp_transport = std::make_unique<webrtc::SrtpTransport>(
Zhi Huang365381f2018-04-13 16:44:34 -070092 rtcp_packet_transport == nullptr);
Zhi Huange818b6e2018-02-22 15:26:27 -080093
94 srtp_transport->SetRtpPacketTransport(rtp_packet_transport);
95 if (rtcp_packet_transport) {
96 srtp_transport->SetRtcpPacketTransport(rtp_packet_transport);
97 }
98 return srtp_transport;
99 }
100
101 std::unique_ptr<webrtc::DtlsSrtpTransport> CreateDtlsSrtpTransport(
Zhi Huange818b6e2018-02-22 15:26:27 -0800102 cricket::DtlsTransportInternal* rtp_dtls_transport,
103 cricket::DtlsTransportInternal* rtcp_dtls_transport) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200104 auto dtls_srtp_transport = std::make_unique<webrtc::DtlsSrtpTransport>(
Zhi Huang365381f2018-04-13 16:44:34 -0700105 rtcp_dtls_transport == nullptr);
Zhi Huange818b6e2018-02-22 15:26:27 -0800106 dtls_srtp_transport->SetDtlsTransports(rtp_dtls_transport,
107 rtcp_dtls_transport);
108 return dtls_srtp_transport;
109 }
110
Zhi Huang365381f2018-04-13 16:44:34 -0700111 // Create a new JsepTransport with a FakeDtlsTransport and a
Zhi Huange818b6e2018-02-22 15:26:27 -0800112 // FakeIceTransport.
Harald Alvestrand0d018412021-11-04 13:52:31 +0000113 std::unique_ptr<JsepTransport> CreateJsepTransport2(bool rtcp_mux_enabled,
114 SrtpMode srtp_mode) {
Qingsi Wang25ec8882019-11-15 12:33:05 -0800115 auto ice_internal = std::make_unique<FakeIceTransport>(
116 kTransportName, ICE_CANDIDATE_COMPONENT_RTP);
117 auto rtp_dtls_transport =
118 std::make_unique<FakeDtlsTransport>(ice_internal.get());
119 auto ice = CreateIceTransport(std::move(ice_internal));
Zhi Huange818b6e2018-02-22 15:26:27 -0800120
Qingsi Wang25ec8882019-11-15 12:33:05 -0800121 std::unique_ptr<FakeIceTransport> rtcp_ice_internal;
Zhi Huange818b6e2018-02-22 15:26:27 -0800122 std::unique_ptr<FakeDtlsTransport> rtcp_dtls_transport;
123 if (!rtcp_mux_enabled) {
Qingsi Wang25ec8882019-11-15 12:33:05 -0800124 rtcp_ice_internal = std::make_unique<FakeIceTransport>(
Bjorn A Mellem0c1c1b42019-05-29 17:34:13 -0700125 kTransportName, ICE_CANDIDATE_COMPONENT_RTCP);
Qingsi Wang25ec8882019-11-15 12:33:05 -0800126 rtcp_dtls_transport =
127 std::make_unique<FakeDtlsTransport>(rtcp_ice_internal.get());
Zhi Huange818b6e2018-02-22 15:26:27 -0800128 }
Qingsi Wang25ec8882019-11-15 12:33:05 -0800129 auto rtcp_ice = CreateIceTransport(std::move(rtcp_ice_internal));
Zhi Huange818b6e2018-02-22 15:26:27 -0800130
131 std::unique_ptr<webrtc::RtpTransport> unencrypted_rtp_transport;
132 std::unique_ptr<webrtc::SrtpTransport> sdes_transport;
133 std::unique_ptr<webrtc::DtlsSrtpTransport> dtls_srtp_transport;
Harald Alvestrand0d018412021-11-04 13:52:31 +0000134 switch (srtp_mode) {
135 case SrtpMode::kSdes:
136 sdes_transport = CreateSdesTransport(rtp_dtls_transport.get(),
137 rtcp_dtls_transport.get());
138 sdes_transport_ = sdes_transport.get();
139 break;
140 case SrtpMode::kDtlsSrtp:
141 dtls_srtp_transport = CreateDtlsSrtpTransport(
142 rtp_dtls_transport.get(), rtcp_dtls_transport.get());
143 break;
144 default:
Artem Titovd3251962021-11-15 16:57:07 +0100145 RTC_DCHECK_NOTREACHED();
Harald Alvestrand0d018412021-11-04 13:52:31 +0000146 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800147
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200148 auto jsep_transport = std::make_unique<JsepTransport>(
Bjorn A Mellem0c1c1b42019-05-29 17:34:13 -0700149 kTransportName, /*local_certificate=*/nullptr, std::move(ice),
150 std::move(rtcp_ice), std::move(unencrypted_rtp_transport),
151 std::move(sdes_transport), std::move(dtls_srtp_transport),
Niels Möllerab9d6e12021-02-02 16:49:02 +0100152 std::move(rtp_dtls_transport), std::move(rtcp_dtls_transport),
Mirko Bonadei96dca922021-07-10 22:37:40 +0200153 /*sctp_transport=*/nullptr,
154 /*rtcp_mux_active_callback=*/[&]() { OnRtcpMuxActive(); });
Zhi Huange818b6e2018-02-22 15:26:27 -0800155
156 signal_rtcp_mux_active_received_ = false;
Zhi Huange830e682018-03-30 10:48:35 -0700157 return jsep_transport;
Zhi Huange818b6e2018-02-22 15:26:27 -0800158 }
159
160 JsepTransportDescription MakeJsepTransportDescription(
161 bool rtcp_mux_enabled,
162 const char* ufrag,
163 const char* pwd,
164 const rtc::scoped_refptr<rtc::RTCCertificate>& cert,
165 ConnectionRole role = CONNECTIONROLE_NONE) {
166 JsepTransportDescription jsep_description;
167 jsep_description.rtcp_mux_enabled = rtcp_mux_enabled;
168
169 std::unique_ptr<rtc::SSLFingerprint> fingerprint;
170 if (cert) {
Steve Anton4905edb2018-10-15 19:27:44 -0700171 fingerprint = rtc::SSLFingerprint::CreateFromCertificate(*cert);
Zhi Huange818b6e2018-02-22 15:26:27 -0800172 }
173 jsep_description.transport_desc =
174 TransportDescription(std::vector<std::string>(), ufrag, pwd,
175 ICEMODE_FULL, role, fingerprint.get());
176 return jsep_description;
177 }
178
179 Candidate CreateCandidate(int component) {
180 Candidate c;
181 c.set_address(rtc::SocketAddress("192.168.1.1", 8000));
182 c.set_component(component);
183 c.set_protocol(UDP_PROTOCOL_NAME);
184 c.set_priority(1);
185 return c;
186 }
187
188 void OnRtcpMuxActive() { signal_rtcp_mux_active_received_ = true; }
189
Zhi Huang365381f2018-04-13 16:44:34 -0700190 std::unique_ptr<JsepTransport> jsep_transport_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800191 bool signal_rtcp_mux_active_received_ = false;
Artem Titov880fa812021-07-30 22:30:23 +0200192 // The SrtpTransport is owned by `jsep_transport_`. Keep a raw pointer here
Zhi Huange818b6e2018-02-22 15:26:27 -0800193 // for testing.
194 webrtc::SrtpTransport* sdes_transport_ = nullptr;
195};
196
197// The parameterized tests cover both cases when RTCP mux is enable and
198// disabled.
199class JsepTransport2WithRtcpMux : public JsepTransport2Test,
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200200 public ::testing::WithParamInterface<bool> {};
Zhi Huange818b6e2018-02-22 15:26:27 -0800201
202// This test verifies the ICE parameters are properly applied to the transports.
203TEST_P(JsepTransport2WithRtcpMux, SetIceParameters) {
204 bool rtcp_mux_enabled = GetParam();
Harald Alvestrand0d018412021-11-04 13:52:31 +0000205 jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800206
207 JsepTransportDescription jsep_description;
208 jsep_description.transport_desc = TransportDescription(kIceUfrag1, kIcePwd1);
209 jsep_description.rtcp_mux_enabled = rtcp_mux_enabled;
210 ASSERT_TRUE(
211 jsep_transport_
212 ->SetLocalJsepTransportDescription(jsep_description, SdpType::kOffer)
213 .ok());
214 auto fake_ice_transport = static_cast<FakeIceTransport*>(
215 jsep_transport_->rtp_dtls_transport()->ice_transport());
216 EXPECT_EQ(ICEMODE_FULL, fake_ice_transport->remote_ice_mode());
217 EXPECT_EQ(kIceUfrag1, fake_ice_transport->ice_ufrag());
218 EXPECT_EQ(kIcePwd1, fake_ice_transport->ice_pwd());
219 if (!rtcp_mux_enabled) {
220 fake_ice_transport = static_cast<FakeIceTransport*>(
221 jsep_transport_->rtcp_dtls_transport()->ice_transport());
222 ASSERT_TRUE(fake_ice_transport);
223 EXPECT_EQ(ICEMODE_FULL, fake_ice_transport->remote_ice_mode());
224 EXPECT_EQ(kIceUfrag1, fake_ice_transport->ice_ufrag());
225 EXPECT_EQ(kIcePwd1, fake_ice_transport->ice_pwd());
226 }
227
228 jsep_description.transport_desc = TransportDescription(kIceUfrag2, kIcePwd2);
229 ASSERT_TRUE(jsep_transport_
230 ->SetRemoteJsepTransportDescription(jsep_description,
231 SdpType::kAnswer)
232 .ok());
233 fake_ice_transport = static_cast<FakeIceTransport*>(
234 jsep_transport_->rtp_dtls_transport()->ice_transport());
235 EXPECT_EQ(ICEMODE_FULL, fake_ice_transport->remote_ice_mode());
236 EXPECT_EQ(kIceUfrag2, fake_ice_transport->remote_ice_ufrag());
237 EXPECT_EQ(kIcePwd2, fake_ice_transport->remote_ice_pwd());
238 if (!rtcp_mux_enabled) {
239 fake_ice_transport = static_cast<FakeIceTransport*>(
240 jsep_transport_->rtcp_dtls_transport()->ice_transport());
241 ASSERT_TRUE(fake_ice_transport);
242 EXPECT_EQ(ICEMODE_FULL, fake_ice_transport->remote_ice_mode());
243 EXPECT_EQ(kIceUfrag2, fake_ice_transport->remote_ice_ufrag());
244 EXPECT_EQ(kIcePwd2, fake_ice_transport->remote_ice_pwd());
245 }
246}
247
248// Similarly, test DTLS parameters are properly applied to the transports.
249TEST_P(JsepTransport2WithRtcpMux, SetDtlsParameters) {
250 bool rtcp_mux_enabled = GetParam();
Harald Alvestrand0d018412021-11-04 13:52:31 +0000251 jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800252
253 // Create certificates.
254 rtc::scoped_refptr<rtc::RTCCertificate> local_cert =
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100255 rtc::RTCCertificate::Create(
256 rtc::SSLIdentity::Create("local", rtc::KT_DEFAULT));
Zhi Huange818b6e2018-02-22 15:26:27 -0800257 rtc::scoped_refptr<rtc::RTCCertificate> remote_cert =
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100258 rtc::RTCCertificate::Create(
259 rtc::SSLIdentity::Create("remote", rtc::KT_DEFAULT));
Zhi Huange818b6e2018-02-22 15:26:27 -0800260 jsep_transport_->SetLocalCertificate(local_cert);
261
262 // Apply offer.
263 JsepTransportDescription local_description =
264 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1,
265 local_cert, CONNECTIONROLE_ACTPASS);
266 ASSERT_TRUE(
267 jsep_transport_
268 ->SetLocalJsepTransportDescription(local_description, SdpType::kOffer)
269 .ok());
270 // Apply Answer.
271 JsepTransportDescription remote_description =
272 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2,
273 remote_cert, CONNECTIONROLE_ACTIVE);
274 ASSERT_TRUE(jsep_transport_
275 ->SetRemoteJsepTransportDescription(remote_description,
276 SdpType::kAnswer)
277 .ok());
278
279 // Verify that SSL role and remote fingerprint were set correctly based on
280 // transport descriptions.
281 auto role = jsep_transport_->GetDtlsRole();
282 ASSERT_TRUE(role);
283 EXPECT_EQ(rtc::SSL_SERVER, role); // Because remote description was "active".
284 auto fake_dtls =
285 static_cast<FakeDtlsTransport*>(jsep_transport_->rtp_dtls_transport());
286 EXPECT_EQ(remote_description.transport_desc.identity_fingerprint->ToString(),
287 fake_dtls->dtls_fingerprint().ToString());
288
289 if (!rtcp_mux_enabled) {
290 auto fake_rtcp_dtls =
291 static_cast<FakeDtlsTransport*>(jsep_transport_->rtcp_dtls_transport());
292 EXPECT_EQ(
293 remote_description.transport_desc.identity_fingerprint->ToString(),
294 fake_rtcp_dtls->dtls_fingerprint().ToString());
295 }
296}
297
298// Same as above test, but with remote transport description using
299// CONNECTIONROLE_PASSIVE, expecting SSL_CLIENT role.
300TEST_P(JsepTransport2WithRtcpMux, SetDtlsParametersWithPassiveAnswer) {
301 bool rtcp_mux_enabled = GetParam();
Harald Alvestrand0d018412021-11-04 13:52:31 +0000302 jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800303
304 // Create certificates.
305 rtc::scoped_refptr<rtc::RTCCertificate> local_cert =
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100306 rtc::RTCCertificate::Create(
307 rtc::SSLIdentity::Create("local", rtc::KT_DEFAULT));
Zhi Huange818b6e2018-02-22 15:26:27 -0800308 rtc::scoped_refptr<rtc::RTCCertificate> remote_cert =
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100309 rtc::RTCCertificate::Create(
310 rtc::SSLIdentity::Create("remote", rtc::KT_DEFAULT));
Zhi Huange818b6e2018-02-22 15:26:27 -0800311 jsep_transport_->SetLocalCertificate(local_cert);
312
313 // Apply offer.
314 JsepTransportDescription local_description =
315 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1,
316 local_cert, CONNECTIONROLE_ACTPASS);
317 ASSERT_TRUE(
318 jsep_transport_
319 ->SetLocalJsepTransportDescription(local_description, SdpType::kOffer)
320 .ok());
321 // Apply Answer.
322 JsepTransportDescription remote_description =
323 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2,
324 remote_cert, CONNECTIONROLE_PASSIVE);
325 ASSERT_TRUE(jsep_transport_
326 ->SetRemoteJsepTransportDescription(remote_description,
327 SdpType::kAnswer)
328 .ok());
329
330 // Verify that SSL role and remote fingerprint were set correctly based on
331 // transport descriptions.
332 auto role = jsep_transport_->GetDtlsRole();
333 ASSERT_TRUE(role);
334 EXPECT_EQ(rtc::SSL_CLIENT,
335 role); // Because remote description was "passive".
336 auto fake_dtls =
337 static_cast<FakeDtlsTransport*>(jsep_transport_->rtp_dtls_transport());
338 EXPECT_EQ(remote_description.transport_desc.identity_fingerprint->ToString(),
339 fake_dtls->dtls_fingerprint().ToString());
340
341 if (!rtcp_mux_enabled) {
342 auto fake_rtcp_dtls =
343 static_cast<FakeDtlsTransport*>(jsep_transport_->rtcp_dtls_transport());
344 EXPECT_EQ(
345 remote_description.transport_desc.identity_fingerprint->ToString(),
346 fake_rtcp_dtls->dtls_fingerprint().ToString());
347 }
348}
349
350// Tests SetNeedsIceRestartFlag and need_ice_restart, ensuring needs_ice_restart
351// only starts returning "false" once an ICE restart has been initiated.
352TEST_P(JsepTransport2WithRtcpMux, NeedsIceRestart) {
353 bool rtcp_mux_enabled = GetParam();
Harald Alvestrand0d018412021-11-04 13:52:31 +0000354 jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800355
356 // Use the same JsepTransportDescription for both offer and answer.
357 JsepTransportDescription description;
358 description.transport_desc = TransportDescription(kIceUfrag1, kIcePwd1);
359 ASSERT_TRUE(
360 jsep_transport_
361 ->SetLocalJsepTransportDescription(description, SdpType::kOffer)
362 .ok());
363 ASSERT_TRUE(
364 jsep_transport_
365 ->SetRemoteJsepTransportDescription(description, SdpType::kAnswer)
366 .ok());
367 // Flag initially should be false.
368 EXPECT_FALSE(jsep_transport_->needs_ice_restart());
369
370 // After setting flag, it should be true.
371 jsep_transport_->SetNeedsIceRestartFlag();
372 EXPECT_TRUE(jsep_transport_->needs_ice_restart());
373
374 ASSERT_TRUE(
375 jsep_transport_
376 ->SetLocalJsepTransportDescription(description, SdpType::kOffer)
377 .ok());
378 ASSERT_TRUE(
379 jsep_transport_
380 ->SetRemoteJsepTransportDescription(description, SdpType::kAnswer)
381 .ok());
382 EXPECT_TRUE(jsep_transport_->needs_ice_restart());
383
384 // Doing an offer/answer that restarts ICE should clear the flag.
385 description.transport_desc = TransportDescription(kIceUfrag2, kIcePwd2);
386 ASSERT_TRUE(
387 jsep_transport_
388 ->SetLocalJsepTransportDescription(description, SdpType::kOffer)
389 .ok());
390 ASSERT_TRUE(
391 jsep_transport_
392 ->SetRemoteJsepTransportDescription(description, SdpType::kAnswer)
393 .ok());
394 EXPECT_FALSE(jsep_transport_->needs_ice_restart());
395}
396
397TEST_P(JsepTransport2WithRtcpMux, GetStats) {
398 bool rtcp_mux_enabled = GetParam();
Harald Alvestrand0d018412021-11-04 13:52:31 +0000399 jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800400
401 size_t expected_stats_size = rtcp_mux_enabled ? 1u : 2u;
402 TransportStats stats;
403 EXPECT_TRUE(jsep_transport_->GetStats(&stats));
404 EXPECT_EQ(expected_stats_size, stats.channel_stats.size());
405 EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTP, stats.channel_stats[0].component);
406 if (!rtcp_mux_enabled) {
407 EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTCP, stats.channel_stats[1].component);
408 }
409}
410
411// Tests that VerifyCertificateFingerprint only returns true when the
412// certificate matches the fingerprint.
413TEST_P(JsepTransport2WithRtcpMux, VerifyCertificateFingerprint) {
414 bool rtcp_mux_enabled = GetParam();
Harald Alvestrand0d018412021-11-04 13:52:31 +0000415 jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800416
417 EXPECT_FALSE(
418 jsep_transport_->VerifyCertificateFingerprint(nullptr, nullptr).ok());
419 rtc::KeyType key_types[] = {rtc::KT_RSA, rtc::KT_ECDSA};
420
421 for (auto& key_type : key_types) {
422 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100423 rtc::RTCCertificate::Create(
424 rtc::SSLIdentity::Create("testing", key_type));
Zhi Huange818b6e2018-02-22 15:26:27 -0800425 ASSERT_NE(nullptr, certificate);
426
427 std::string digest_algorithm;
Benjamin Wright6c6c9df2018-10-25 01:16:26 -0700428 ASSERT_TRUE(certificate->GetSSLCertificate().GetSignatureDigestAlgorithm(
Zhi Huange818b6e2018-02-22 15:26:27 -0800429 &digest_algorithm));
430 ASSERT_FALSE(digest_algorithm.empty());
Steve Anton4905edb2018-10-15 19:27:44 -0700431 std::unique_ptr<rtc::SSLFingerprint> good_fingerprint =
432 rtc::SSLFingerprint::CreateUnique(digest_algorithm,
433 *certificate->identity());
Zhi Huange818b6e2018-02-22 15:26:27 -0800434 ASSERT_NE(nullptr, good_fingerprint);
435
436 EXPECT_TRUE(jsep_transport_
437 ->VerifyCertificateFingerprint(certificate.get(),
438 good_fingerprint.get())
439 .ok());
440 EXPECT_FALSE(jsep_transport_
441 ->VerifyCertificateFingerprint(certificate.get(), nullptr)
442 .ok());
443 EXPECT_FALSE(
444 jsep_transport_
445 ->VerifyCertificateFingerprint(nullptr, good_fingerprint.get())
446 .ok());
447
448 rtc::SSLFingerprint bad_fingerprint = *good_fingerprint;
449 bad_fingerprint.digest.AppendData("0", 1);
450 EXPECT_FALSE(
451 jsep_transport_
452 ->VerifyCertificateFingerprint(certificate.get(), &bad_fingerprint)
453 .ok());
454 }
455}
456
457// Tests the logic of DTLS role negotiation for an initial offer/answer.
458TEST_P(JsepTransport2WithRtcpMux, ValidDtlsRoleNegotiation) {
459 bool rtcp_mux_enabled = GetParam();
460 // Just use the same certificate for both sides; doesn't really matter in a
461 // non end-to-end test.
462 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100463 rtc::RTCCertificate::Create(
464 rtc::SSLIdentity::Create("testing", rtc::KT_ECDSA));
Zhi Huange818b6e2018-02-22 15:26:27 -0800465
466 JsepTransportDescription local_description = MakeJsepTransportDescription(
467 rtcp_mux_enabled, kIceUfrag1, kIcePwd1, certificate);
468 JsepTransportDescription remote_description = MakeJsepTransportDescription(
469 rtcp_mux_enabled, kIceUfrag2, kIcePwd2, certificate);
470
471 // Parameters which set the SSL role to SSL_CLIENT.
472 NegotiateRoleParams valid_client_params[] = {
473 {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_ACTPASS, SdpType::kAnswer,
474 SdpType::kOffer},
475 {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_ACTPASS, SdpType::kPrAnswer,
476 SdpType::kOffer},
477 {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_PASSIVE, SdpType::kOffer,
478 SdpType::kAnswer},
479 {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_PASSIVE, SdpType::kOffer,
Harald Alvestrandefece422021-08-19 09:12:51 +0000480 SdpType::kPrAnswer},
481 // Combinations permitted by RFC 8842 section 5.3
482 {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_PASSIVE, SdpType::kAnswer,
483 SdpType::kOffer},
484 {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_PASSIVE, SdpType::kPrAnswer,
485 SdpType::kOffer},
486 };
Zhi Huange818b6e2018-02-22 15:26:27 -0800487
488 for (auto& param : valid_client_params) {
Harald Alvestrand0d018412021-11-04 13:52:31 +0000489 jsep_transport_ =
490 CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800491 jsep_transport_->SetLocalCertificate(certificate);
492
493 local_description.transport_desc.connection_role = param.local_role;
494 remote_description.transport_desc.connection_role = param.remote_role;
495
496 // Set the offer first.
497 if (param.local_type == SdpType::kOffer) {
498 EXPECT_TRUE(jsep_transport_
499 ->SetLocalJsepTransportDescription(local_description,
500 param.local_type)
501 .ok());
502 EXPECT_TRUE(jsep_transport_
503 ->SetRemoteJsepTransportDescription(remote_description,
504 param.remote_type)
505 .ok());
506 } else {
507 EXPECT_TRUE(jsep_transport_
508 ->SetRemoteJsepTransportDescription(remote_description,
509 param.remote_type)
510 .ok());
511 EXPECT_TRUE(jsep_transport_
512 ->SetLocalJsepTransportDescription(local_description,
513 param.local_type)
514 .ok());
515 }
516 EXPECT_EQ(rtc::SSL_CLIENT, *jsep_transport_->GetDtlsRole());
517 }
518
519 // Parameters which set the SSL role to SSL_SERVER.
520 NegotiateRoleParams valid_server_params[] = {
521 {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_ACTPASS, SdpType::kAnswer,
522 SdpType::kOffer},
523 {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_ACTPASS, SdpType::kPrAnswer,
524 SdpType::kOffer},
525 {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_ACTIVE, SdpType::kOffer,
526 SdpType::kAnswer},
527 {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_ACTIVE, SdpType::kOffer,
Harald Alvestrandefece422021-08-19 09:12:51 +0000528 SdpType::kPrAnswer},
529 // Combinations permitted by RFC 8842 section 5.3
530 {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_ACTIVE, SdpType::kPrAnswer,
531 SdpType::kOffer},
532 };
Zhi Huange818b6e2018-02-22 15:26:27 -0800533
534 for (auto& param : valid_server_params) {
Harald Alvestrand0d018412021-11-04 13:52:31 +0000535 jsep_transport_ =
536 CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800537 jsep_transport_->SetLocalCertificate(certificate);
538
539 local_description.transport_desc.connection_role = param.local_role;
540 remote_description.transport_desc.connection_role = param.remote_role;
541
542 // Set the offer first.
543 if (param.local_type == SdpType::kOffer) {
544 EXPECT_TRUE(jsep_transport_
545 ->SetLocalJsepTransportDescription(local_description,
546 param.local_type)
547 .ok());
548 EXPECT_TRUE(jsep_transport_
549 ->SetRemoteJsepTransportDescription(remote_description,
550 param.remote_type)
551 .ok());
552 } else {
553 EXPECT_TRUE(jsep_transport_
554 ->SetRemoteJsepTransportDescription(remote_description,
555 param.remote_type)
556 .ok());
557 EXPECT_TRUE(jsep_transport_
558 ->SetLocalJsepTransportDescription(local_description,
559 param.local_type)
560 .ok());
561 }
562 EXPECT_EQ(rtc::SSL_SERVER, *jsep_transport_->GetDtlsRole());
563 }
564}
565
566// Tests the logic of DTLS role negotiation for an initial offer/answer.
567TEST_P(JsepTransport2WithRtcpMux, InvalidDtlsRoleNegotiation) {
568 bool rtcp_mux_enabled = GetParam();
569 // Just use the same certificate for both sides; doesn't really matter in a
570 // non end-to-end test.
571 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100572 rtc::RTCCertificate::Create(
573 rtc::SSLIdentity::Create("testing", rtc::KT_ECDSA));
Zhi Huange818b6e2018-02-22 15:26:27 -0800574
575 JsepTransportDescription local_description = MakeJsepTransportDescription(
576 rtcp_mux_enabled, kIceUfrag1, kIcePwd1, certificate);
577 JsepTransportDescription remote_description = MakeJsepTransportDescription(
578 rtcp_mux_enabled, kIceUfrag2, kIcePwd2, certificate);
579
580 NegotiateRoleParams duplicate_params[] = {
581 {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_ACTIVE, SdpType::kAnswer,
582 SdpType::kOffer},
583 {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_ACTPASS, SdpType::kAnswer,
584 SdpType::kOffer},
585 {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_PASSIVE, SdpType::kAnswer,
586 SdpType::kOffer},
587 {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_ACTIVE, SdpType::kPrAnswer,
588 SdpType::kOffer},
589 {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_ACTPASS, SdpType::kPrAnswer,
590 SdpType::kOffer},
591 {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_PASSIVE, SdpType::kPrAnswer,
592 SdpType::kOffer},
593 {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_ACTIVE, SdpType::kOffer,
594 SdpType::kAnswer},
595 {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_ACTPASS, SdpType::kOffer,
596 SdpType::kAnswer},
597 {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_PASSIVE, SdpType::kOffer,
598 SdpType::kAnswer},
599 {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_ACTIVE, SdpType::kOffer,
600 SdpType::kPrAnswer},
601 {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_ACTPASS, SdpType::kOffer,
602 SdpType::kPrAnswer},
603 {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_PASSIVE, SdpType::kOffer,
604 SdpType::kPrAnswer}};
605
606 for (auto& param : duplicate_params) {
Harald Alvestrand0d018412021-11-04 13:52:31 +0000607 jsep_transport_ =
608 CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800609 jsep_transport_->SetLocalCertificate(certificate);
610
611 local_description.transport_desc.connection_role = param.local_role;
612 remote_description.transport_desc.connection_role = param.remote_role;
613
614 if (param.local_type == SdpType::kOffer) {
615 EXPECT_TRUE(jsep_transport_
616 ->SetLocalJsepTransportDescription(local_description,
617 param.local_type)
618 .ok());
619 EXPECT_FALSE(jsep_transport_
620 ->SetRemoteJsepTransportDescription(remote_description,
621 param.remote_type)
622 .ok());
623 } else {
624 EXPECT_TRUE(jsep_transport_
625 ->SetRemoteJsepTransportDescription(remote_description,
626 param.remote_type)
627 .ok());
628 EXPECT_FALSE(jsep_transport_
629 ->SetLocalJsepTransportDescription(local_description,
630 param.local_type)
631 .ok());
632 }
633 }
634
Harald Alvestrandefece422021-08-19 09:12:51 +0000635 // Invalid parameters due to the offerer not using a role consistent with the
636 // state
Zhi Huange818b6e2018-02-22 15:26:27 -0800637 NegotiateRoleParams offerer_without_actpass_params[] = {
Harald Alvestrandefece422021-08-19 09:12:51 +0000638 // Cannot use ACTPASS in an answer
Zhi Huange818b6e2018-02-22 15:26:27 -0800639 {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_PASSIVE, SdpType::kAnswer,
640 SdpType::kOffer},
Zhi Huange818b6e2018-02-22 15:26:27 -0800641 {CONNECTIONROLE_ACTPASS, CONNECTIONROLE_PASSIVE, SdpType::kPrAnswer,
642 SdpType::kOffer},
Harald Alvestrandefece422021-08-19 09:12:51 +0000643 // Cannot send ACTIVE or PASSIVE in an offer (must handle, must not send)
Zhi Huange818b6e2018-02-22 15:26:27 -0800644 {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_PASSIVE, SdpType::kOffer,
645 SdpType::kAnswer},
646 {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_ACTIVE, SdpType::kOffer,
647 SdpType::kAnswer},
648 {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_ACTPASS, SdpType::kOffer,
649 SdpType::kAnswer},
650 {CONNECTIONROLE_ACTIVE, CONNECTIONROLE_PASSIVE, SdpType::kOffer,
651 SdpType::kPrAnswer},
652 {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_ACTIVE, SdpType::kOffer,
653 SdpType::kPrAnswer},
654 {CONNECTIONROLE_PASSIVE, CONNECTIONROLE_ACTPASS, SdpType::kOffer,
655 SdpType::kPrAnswer}};
656
657 for (auto& param : offerer_without_actpass_params) {
Harald Alvestrand0d018412021-11-04 13:52:31 +0000658 jsep_transport_ =
659 CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800660 jsep_transport_->SetLocalCertificate(certificate);
661
662 local_description.transport_desc.connection_role = param.local_role;
663 remote_description.transport_desc.connection_role = param.remote_role;
664
665 if (param.local_type == SdpType::kOffer) {
666 EXPECT_TRUE(jsep_transport_
667 ->SetLocalJsepTransportDescription(local_description,
668 param.local_type)
Harald Alvestrandefece422021-08-19 09:12:51 +0000669 .ok())
670 << param;
Zhi Huange818b6e2018-02-22 15:26:27 -0800671 EXPECT_FALSE(jsep_transport_
672 ->SetRemoteJsepTransportDescription(remote_description,
673 param.remote_type)
Harald Alvestrandefece422021-08-19 09:12:51 +0000674 .ok())
675 << param;
Zhi Huange818b6e2018-02-22 15:26:27 -0800676 } else {
677 EXPECT_TRUE(jsep_transport_
678 ->SetRemoteJsepTransportDescription(remote_description,
679 param.remote_type)
Harald Alvestrandefece422021-08-19 09:12:51 +0000680 .ok())
681 << param;
Zhi Huange818b6e2018-02-22 15:26:27 -0800682 EXPECT_FALSE(jsep_transport_
683 ->SetLocalJsepTransportDescription(local_description,
684 param.local_type)
Harald Alvestrandefece422021-08-19 09:12:51 +0000685 .ok())
686 << param;
Zhi Huange818b6e2018-02-22 15:26:27 -0800687 }
688 }
689}
690
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100691INSTANTIATE_TEST_SUITE_P(JsepTransport2Test,
692 JsepTransport2WithRtcpMux,
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200693 ::testing::Bool());
Zhi Huange818b6e2018-02-22 15:26:27 -0800694
695// Test that a reoffer in the opposite direction is successful as long as the
696// role isn't changing. Doesn't test every possible combination like the test
697// above.
698TEST_F(JsepTransport2Test, ValidDtlsReofferFromAnswerer) {
699 // Just use the same certificate for both sides; doesn't really matter in a
700 // non end-to-end test.
701 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100702 rtc::RTCCertificate::Create(
703 rtc::SSLIdentity::Create("testing", rtc::KT_ECDSA));
Zhi Huange818b6e2018-02-22 15:26:27 -0800704 bool rtcp_mux_enabled = true;
Harald Alvestrand0d018412021-11-04 13:52:31 +0000705 jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800706 jsep_transport_->SetLocalCertificate(certificate);
707
708 JsepTransportDescription local_offer =
709 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1,
710 certificate, CONNECTIONROLE_ACTPASS);
711 JsepTransportDescription remote_answer =
712 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2,
713 certificate, CONNECTIONROLE_ACTIVE);
714
715 EXPECT_TRUE(
716 jsep_transport_
717 ->SetLocalJsepTransportDescription(local_offer, SdpType::kOffer)
718 .ok());
719 EXPECT_TRUE(
720 jsep_transport_
721 ->SetRemoteJsepTransportDescription(remote_answer, SdpType::kAnswer)
722 .ok());
723
724 // We were actpass->active previously, now in the other direction it's
725 // actpass->passive.
726 JsepTransportDescription remote_offer =
727 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2,
728 certificate, CONNECTIONROLE_ACTPASS);
729 JsepTransportDescription local_answer =
730 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1,
731 certificate, CONNECTIONROLE_PASSIVE);
732
733 EXPECT_TRUE(
734 jsep_transport_
735 ->SetRemoteJsepTransportDescription(remote_offer, SdpType::kOffer)
736 .ok());
737 EXPECT_TRUE(
738 jsep_transport_
739 ->SetLocalJsepTransportDescription(local_answer, SdpType::kAnswer)
740 .ok());
741}
742
743// Test that a reoffer in the opposite direction fails if the role changes.
744// Inverse of test above.
745TEST_F(JsepTransport2Test, InvalidDtlsReofferFromAnswerer) {
746 // Just use the same certificate for both sides; doesn't really matter in a
747 // non end-to-end test.
748 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100749 rtc::RTCCertificate::Create(
750 rtc::SSLIdentity::Create("testing", rtc::KT_ECDSA));
Zhi Huange818b6e2018-02-22 15:26:27 -0800751 bool rtcp_mux_enabled = true;
Harald Alvestrand0d018412021-11-04 13:52:31 +0000752 jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800753 jsep_transport_->SetLocalCertificate(certificate);
754
755 JsepTransportDescription local_offer =
756 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1,
757 certificate, CONNECTIONROLE_ACTPASS);
758 JsepTransportDescription remote_answer =
759 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2,
760 certificate, CONNECTIONROLE_ACTIVE);
761
762 EXPECT_TRUE(
763 jsep_transport_
764 ->SetLocalJsepTransportDescription(local_offer, SdpType::kOffer)
765 .ok());
766 EXPECT_TRUE(
767 jsep_transport_
768 ->SetRemoteJsepTransportDescription(remote_answer, SdpType::kAnswer)
769 .ok());
770
771 // Changing role to passive here isn't allowed. Though for some reason this
772 // only fails in SetLocalTransportDescription.
773 JsepTransportDescription remote_offer =
774 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2,
775 certificate, CONNECTIONROLE_PASSIVE);
776 JsepTransportDescription local_answer =
777 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1,
778 certificate, CONNECTIONROLE_ACTIVE);
779
780 EXPECT_TRUE(
781 jsep_transport_
782 ->SetRemoteJsepTransportDescription(remote_offer, SdpType::kOffer)
783 .ok());
784 EXPECT_FALSE(
785 jsep_transport_
786 ->SetLocalJsepTransportDescription(local_answer, SdpType::kAnswer)
787 .ok());
788}
789
790// Test that a remote offer with the current negotiated role can be accepted.
791// This is allowed by dtls-sdp, though we'll never generate such an offer,
792// since JSEP requires generating "actpass".
793TEST_F(JsepTransport2Test, RemoteOfferWithCurrentNegotiatedDtlsRole) {
794 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100795 rtc::RTCCertificate::Create(
796 rtc::SSLIdentity::Create("testing", rtc::KT_ECDSA));
Zhi Huange818b6e2018-02-22 15:26:27 -0800797 bool rtcp_mux_enabled = true;
Harald Alvestrand0d018412021-11-04 13:52:31 +0000798 jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800799 jsep_transport_->SetLocalCertificate(certificate);
800
801 JsepTransportDescription remote_desc =
802 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1,
803 certificate, CONNECTIONROLE_ACTPASS);
804 JsepTransportDescription local_desc =
805 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2,
806 certificate, CONNECTIONROLE_ACTIVE);
807
808 // Normal initial offer/answer with "actpass" in the offer and "active" in
809 // the answer.
810 ASSERT_TRUE(
811 jsep_transport_
812 ->SetRemoteJsepTransportDescription(remote_desc, SdpType::kOffer)
813 .ok());
814 ASSERT_TRUE(
815 jsep_transport_
816 ->SetLocalJsepTransportDescription(local_desc, SdpType::kAnswer)
817 .ok());
818
819 // Sanity check that role was actually negotiated.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200820 absl::optional<rtc::SSLRole> role = jsep_transport_->GetDtlsRole();
Zhi Huange818b6e2018-02-22 15:26:27 -0800821 ASSERT_TRUE(role);
822 EXPECT_EQ(rtc::SSL_CLIENT, *role);
823
824 // Subsequent offer with current negotiated role of "passive".
825 remote_desc.transport_desc.connection_role = CONNECTIONROLE_PASSIVE;
826 EXPECT_TRUE(
827 jsep_transport_
828 ->SetRemoteJsepTransportDescription(remote_desc, SdpType::kOffer)
829 .ok());
830 EXPECT_TRUE(
831 jsep_transport_
832 ->SetLocalJsepTransportDescription(local_desc, SdpType::kAnswer)
833 .ok());
834}
835
836// Test that a remote offer with the inverse of the current negotiated DTLS
837// role is rejected.
838TEST_F(JsepTransport2Test, RemoteOfferThatChangesNegotiatedDtlsRole) {
839 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100840 rtc::RTCCertificate::Create(
841 rtc::SSLIdentity::Create("testing", rtc::KT_ECDSA));
Zhi Huange818b6e2018-02-22 15:26:27 -0800842 bool rtcp_mux_enabled = true;
Harald Alvestrand0d018412021-11-04 13:52:31 +0000843 jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800844 jsep_transport_->SetLocalCertificate(certificate);
845
846 JsepTransportDescription remote_desc =
847 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1,
848 certificate, CONNECTIONROLE_ACTPASS);
849 JsepTransportDescription local_desc =
850 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2,
851 certificate, CONNECTIONROLE_ACTIVE);
852
853 // Normal initial offer/answer with "actpass" in the offer and "active" in
854 // the answer.
855 ASSERT_TRUE(
856 jsep_transport_
857 ->SetRemoteJsepTransportDescription(remote_desc, SdpType::kOffer)
858 .ok());
859 ASSERT_TRUE(
860 jsep_transport_
861 ->SetLocalJsepTransportDescription(local_desc, SdpType::kAnswer)
862 .ok());
863
864 // Sanity check that role was actually negotiated.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200865 absl::optional<rtc::SSLRole> role = jsep_transport_->GetDtlsRole();
Zhi Huange818b6e2018-02-22 15:26:27 -0800866 ASSERT_TRUE(role);
867 EXPECT_EQ(rtc::SSL_CLIENT, *role);
868
869 // Subsequent offer with current negotiated role of "passive".
870 remote_desc.transport_desc.connection_role = CONNECTIONROLE_ACTIVE;
871 EXPECT_TRUE(
872 jsep_transport_
873 ->SetRemoteJsepTransportDescription(remote_desc, SdpType::kOffer)
874 .ok());
875 EXPECT_FALSE(
876 jsep_transport_
877 ->SetLocalJsepTransportDescription(local_desc, SdpType::kAnswer)
878 .ok());
879}
880
881// Testing that a legacy client that doesn't use the setup attribute will be
882// interpreted as having an active role.
883TEST_F(JsepTransport2Test, DtlsSetupWithLegacyAsAnswerer) {
884 rtc::scoped_refptr<rtc::RTCCertificate> certificate =
Harald Alvestrand8515d5a2020-03-20 22:51:32 +0100885 rtc::RTCCertificate::Create(
886 rtc::SSLIdentity::Create("testing", rtc::KT_ECDSA));
Zhi Huange818b6e2018-02-22 15:26:27 -0800887 bool rtcp_mux_enabled = true;
Harald Alvestrand0d018412021-11-04 13:52:31 +0000888 jsep_transport_ = CreateJsepTransport2(rtcp_mux_enabled, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800889 jsep_transport_->SetLocalCertificate(certificate);
890
891 JsepTransportDescription remote_desc =
892 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag1, kIcePwd1,
893 certificate, CONNECTIONROLE_ACTPASS);
894 JsepTransportDescription local_desc =
895 MakeJsepTransportDescription(rtcp_mux_enabled, kIceUfrag2, kIcePwd2,
896 certificate, CONNECTIONROLE_ACTIVE);
897
898 local_desc.transport_desc.connection_role = CONNECTIONROLE_ACTPASS;
899 ASSERT_TRUE(
900 jsep_transport_
901 ->SetLocalJsepTransportDescription(local_desc, SdpType::kOffer)
902 .ok());
903 // Use CONNECTIONROLE_NONE to simulate legacy endpoint.
904 remote_desc.transport_desc.connection_role = CONNECTIONROLE_NONE;
905 ASSERT_TRUE(
906 jsep_transport_
907 ->SetRemoteJsepTransportDescription(remote_desc, SdpType::kAnswer)
908 .ok());
909
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200910 absl::optional<rtc::SSLRole> role = jsep_transport_->GetDtlsRole();
Zhi Huange818b6e2018-02-22 15:26:27 -0800911 ASSERT_TRUE(role);
912 // Since legacy answer ommitted setup atribute, and we offered actpass, we
913 // should act as passive (server).
914 EXPECT_EQ(rtc::SSL_SERVER, *role);
915}
916
917// Tests that when the RTCP mux is successfully negotiated, the RTCP transport
918// will be destroyed and the SignalRtpMuxActive will be fired.
919TEST_F(JsepTransport2Test, RtcpMuxNegotiation) {
Harald Alvestrand0d018412021-11-04 13:52:31 +0000920 jsep_transport_ =
921 CreateJsepTransport2(/*rtcp_mux_enabled=*/false, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800922 JsepTransportDescription local_desc;
923 local_desc.rtcp_mux_enabled = true;
Harald Alvestrandad88c882018-11-28 16:47:46 +0100924 ASSERT_NE(nullptr, jsep_transport_->rtcp_dtls_transport());
Zhi Huange818b6e2018-02-22 15:26:27 -0800925 EXPECT_FALSE(signal_rtcp_mux_active_received_);
926
927 // The remote side supports RTCP-mux.
928 JsepTransportDescription remote_desc;
929 remote_desc.rtcp_mux_enabled = true;
930 ASSERT_TRUE(
931 jsep_transport_
932 ->SetLocalJsepTransportDescription(local_desc, SdpType::kOffer)
933 .ok());
934 ASSERT_TRUE(
935 jsep_transport_
936 ->SetRemoteJsepTransportDescription(remote_desc, SdpType::kAnswer)
937 .ok());
938
939 EXPECT_EQ(nullptr, jsep_transport_->rtcp_dtls_transport());
940 EXPECT_TRUE(signal_rtcp_mux_active_received_);
941
942 // The remote side doesn't support RTCP-mux.
Harald Alvestrand0d018412021-11-04 13:52:31 +0000943 jsep_transport_ =
944 CreateJsepTransport2(/*rtcp_mux_enabled=*/false, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800945 signal_rtcp_mux_active_received_ = false;
946 remote_desc.rtcp_mux_enabled = false;
947 ASSERT_TRUE(
948 jsep_transport_
949 ->SetLocalJsepTransportDescription(local_desc, SdpType::kOffer)
950 .ok());
951 ASSERT_TRUE(
952 jsep_transport_
953 ->SetRemoteJsepTransportDescription(remote_desc, SdpType::kAnswer)
954 .ok());
955
956 EXPECT_NE(nullptr, jsep_transport_->rtcp_dtls_transport());
957 EXPECT_FALSE(signal_rtcp_mux_active_received_);
958}
959
Harald Alvestrand0d018412021-11-04 13:52:31 +0000960TEST_F(JsepTransport2Test, SdesNegotiation) {
961 jsep_transport_ =
962 CreateJsepTransport2(/*rtcp_mux_enabled=*/true, SrtpMode::kSdes);
963 ASSERT_TRUE(sdes_transport_);
964 EXPECT_FALSE(sdes_transport_->IsSrtpActive());
965
966 JsepTransportDescription offer_desc;
967 offer_desc.cryptos.push_back(cricket::CryptoParams(
968 1, rtc::kCsAesCm128HmacSha1_32, "inline:" + rtc::CreateRandomString(40),
969 std::string()));
970 ASSERT_TRUE(
971 jsep_transport_
972 ->SetLocalJsepTransportDescription(offer_desc, SdpType::kOffer)
973 .ok());
974
975 JsepTransportDescription answer_desc;
976 answer_desc.cryptos.push_back(cricket::CryptoParams(
977 1, rtc::kCsAesCm128HmacSha1_32, "inline:" + rtc::CreateRandomString(40),
978 std::string()));
979 ASSERT_TRUE(
980 jsep_transport_
981 ->SetRemoteJsepTransportDescription(answer_desc, SdpType::kAnswer)
982 .ok());
983 EXPECT_TRUE(sdes_transport_->IsSrtpActive());
984}
985
986TEST_F(JsepTransport2Test, SdesNegotiationWithEmptyCryptosInAnswer) {
987 jsep_transport_ =
988 CreateJsepTransport2(/*rtcp_mux_enabled=*/true, SrtpMode::kSdes);
989 ASSERT_TRUE(sdes_transport_);
990 EXPECT_FALSE(sdes_transport_->IsSrtpActive());
991
992 JsepTransportDescription offer_desc;
993 offer_desc.cryptos.push_back(cricket::CryptoParams(
994 1, rtc::kCsAesCm128HmacSha1_32, "inline:" + rtc::CreateRandomString(40),
995 std::string()));
996 ASSERT_TRUE(
997 jsep_transport_
998 ->SetLocalJsepTransportDescription(offer_desc, SdpType::kOffer)
999 .ok());
1000
1001 JsepTransportDescription answer_desc;
1002 ASSERT_TRUE(
1003 jsep_transport_
1004 ->SetRemoteJsepTransportDescription(answer_desc, SdpType::kAnswer)
1005 .ok());
1006 // SRTP is not active because the crypto parameter is answer is empty.
1007 EXPECT_FALSE(sdes_transport_->IsSrtpActive());
1008}
1009
1010TEST_F(JsepTransport2Test, SdesNegotiationWithMismatchedCryptos) {
1011 jsep_transport_ =
1012 CreateJsepTransport2(/*rtcp_mux_enabled=*/true, SrtpMode::kSdes);
1013 ASSERT_TRUE(sdes_transport_);
1014 EXPECT_FALSE(sdes_transport_->IsSrtpActive());
1015
1016 JsepTransportDescription offer_desc;
1017 offer_desc.cryptos.push_back(cricket::CryptoParams(
1018 1, rtc::kCsAesCm128HmacSha1_32, "inline:" + rtc::CreateRandomString(40),
1019 std::string()));
1020 ASSERT_TRUE(
1021 jsep_transport_
1022 ->SetLocalJsepTransportDescription(offer_desc, SdpType::kOffer)
1023 .ok());
1024
1025 JsepTransportDescription answer_desc;
1026 answer_desc.cryptos.push_back(cricket::CryptoParams(
1027 1, rtc::kCsAesCm128HmacSha1_80, "inline:" + rtc::CreateRandomString(40),
1028 std::string()));
1029 // Expected to fail because the crypto parameters don't match.
1030 ASSERT_FALSE(
1031 jsep_transport_
1032 ->SetRemoteJsepTransportDescription(answer_desc, SdpType::kAnswer)
1033 .ok());
1034}
1035
Zhi Huange818b6e2018-02-22 15:26:27 -08001036// Tests that the remote candidates can be added to the transports after both
1037// local and remote descriptions are set.
1038TEST_F(JsepTransport2Test, AddRemoteCandidates) {
Harald Alvestrand0d018412021-11-04 13:52:31 +00001039 jsep_transport_ =
1040 CreateJsepTransport2(/*rtcp_mux_enabled=*/true, SrtpMode::kDtlsSrtp);
Zhi Huange818b6e2018-02-22 15:26:27 -08001041 auto fake_ice_transport = static_cast<FakeIceTransport*>(
1042 jsep_transport_->rtp_dtls_transport()->ice_transport());
1043
1044 Candidates candidates;
1045 candidates.push_back(CreateCandidate(/*COMPONENT_RTP*/ 1));
1046 candidates.push_back(CreateCandidate(/*COMPONENT_RTP*/ 1));
1047
1048 JsepTransportDescription desc;
1049 ASSERT_TRUE(
1050 jsep_transport_->SetLocalJsepTransportDescription(desc, SdpType::kOffer)
1051 .ok());
1052 // Expected to fail because the remote description is unset.
1053 EXPECT_FALSE(jsep_transport_->AddRemoteCandidates(candidates).ok());
1054
1055 ASSERT_TRUE(
1056 jsep_transport_->SetRemoteJsepTransportDescription(desc, SdpType::kAnswer)
1057 .ok());
1058 EXPECT_EQ(0u, fake_ice_transport->remote_candidates().size());
1059 EXPECT_TRUE(jsep_transport_->AddRemoteCandidates(candidates).ok());
1060 EXPECT_EQ(candidates.size(), fake_ice_transport->remote_candidates().size());
1061}
1062
Zhi Huange830e682018-03-30 10:48:35 -07001063enum class Scenario {
Harald Alvestrand0d018412021-11-04 13:52:31 +00001064 kSdes,
Zhi Huange830e682018-03-30 10:48:35 -07001065 kDtlsBeforeCallerSendOffer,
1066 kDtlsBeforeCallerSetAnswer,
1067 kDtlsAfterCallerSetAnswer,
1068};
1069
1070class JsepTransport2HeaderExtensionTest
1071 : public JsepTransport2Test,
1072 public ::testing::WithParamInterface<std::tuple<Scenario, bool>> {
1073 protected:
1074 JsepTransport2HeaderExtensionTest() {}
1075
Harald Alvestrand0d018412021-11-04 13:52:31 +00001076 void CreateJsepTransportPair(SrtpMode mode) {
1077 jsep_transport1_ = CreateJsepTransport2(/*rtcp_mux_enabled=*/true, mode);
1078 jsep_transport2_ = CreateJsepTransport2(/*rtcp_mux_enabled=*/true, mode);
Zhi Huange830e682018-03-30 10:48:35 -07001079
1080 auto fake_dtls1 =
1081 static_cast<FakeDtlsTransport*>(jsep_transport1_->rtp_dtls_transport());
1082 auto fake_dtls2 =
1083 static_cast<FakeDtlsTransport*>(jsep_transport2_->rtp_dtls_transport());
1084
1085 fake_dtls1->fake_ice_transport()->SignalReadPacket.connect(
1086 this, &JsepTransport2HeaderExtensionTest::OnReadPacket1);
1087 fake_dtls2->fake_ice_transport()->SignalReadPacket.connect(
1088 this, &JsepTransport2HeaderExtensionTest::OnReadPacket2);
1089
Harald Alvestrand0d018412021-11-04 13:52:31 +00001090 if (mode == SrtpMode::kDtlsSrtp) {
1091 auto cert1 = rtc::RTCCertificate::Create(
1092 rtc::SSLIdentity::Create("session1", rtc::KT_DEFAULT));
1093 jsep_transport1_->rtp_dtls_transport()->SetLocalCertificate(cert1);
1094 auto cert2 = rtc::RTCCertificate::Create(
1095 rtc::SSLIdentity::Create("session1", rtc::KT_DEFAULT));
1096 jsep_transport2_->rtp_dtls_transport()->SetLocalCertificate(cert2);
1097 }
Zhi Huange830e682018-03-30 10:48:35 -07001098 }
1099
1100 void OnReadPacket1(rtc::PacketTransportInternal* transport,
1101 const char* data,
1102 size_t size,
Niels Möllere6933812018-11-05 13:01:41 +01001103 const int64_t& /* packet_time_us */,
Zhi Huange830e682018-03-30 10:48:35 -07001104 int flags) {
1105 RTC_LOG(LS_INFO) << "JsepTransport 1 Received a packet.";
1106 CompareHeaderExtensions(
1107 reinterpret_cast<const char*>(kPcmuFrameWithExtensions),
1108 sizeof(kPcmuFrameWithExtensions), data, size, recv_encrypted_headers1_,
1109 false);
1110 received_packet_count_++;
1111 }
1112
1113 void OnReadPacket2(rtc::PacketTransportInternal* transport,
1114 const char* data,
1115 size_t size,
Niels Möllere6933812018-11-05 13:01:41 +01001116 const int64_t& /* packet_time_us */,
Zhi Huange830e682018-03-30 10:48:35 -07001117 int flags) {
1118 RTC_LOG(LS_INFO) << "JsepTransport 2 Received a packet.";
1119 CompareHeaderExtensions(
1120 reinterpret_cast<const char*>(kPcmuFrameWithExtensions),
1121 sizeof(kPcmuFrameWithExtensions), data, size, recv_encrypted_headers2_,
1122 false);
1123 received_packet_count_++;
1124 }
1125
1126 void ConnectTransport() {
1127 auto rtp_dtls_transport1 =
1128 static_cast<FakeDtlsTransport*>(jsep_transport1_->rtp_dtls_transport());
1129 auto rtp_dtls_transport2 =
1130 static_cast<FakeDtlsTransport*>(jsep_transport2_->rtp_dtls_transport());
1131 rtp_dtls_transport1->SetDestination(rtp_dtls_transport2);
1132 }
1133
1134 int GetRtpAuthLen() {
1135 bool use_gcm = std::get<1>(GetParam());
1136 if (use_gcm) {
1137 return 16;
1138 }
1139 return 10;
1140 }
1141
1142 void TestSendRecvPacketWithEncryptedHeaderExtension() {
1143 TestOneWaySendRecvPacketWithEncryptedHeaderExtension(
1144 jsep_transport1_.get());
1145 TestOneWaySendRecvPacketWithEncryptedHeaderExtension(
1146 jsep_transport2_.get());
1147 }
1148
1149 void TestOneWaySendRecvPacketWithEncryptedHeaderExtension(
Zhi Huang365381f2018-04-13 16:44:34 -07001150 JsepTransport* sender_transport) {
Zhi Huange830e682018-03-30 10:48:35 -07001151 size_t rtp_len = sizeof(kPcmuFrameWithExtensions);
1152 size_t packet_size = rtp_len + GetRtpAuthLen();
1153 rtc::Buffer rtp_packet_buffer(packet_size);
1154 char* rtp_packet_data = rtp_packet_buffer.data<char>();
1155 memcpy(rtp_packet_data, kPcmuFrameWithExtensions, rtp_len);
1156 // In order to be able to run this test function multiple times we can not
1157 // use the same sequence number twice. Increase the sequence number by one.
1158 rtc::SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_data) + 2,
1159 ++sequence_number_);
1160 rtc::CopyOnWriteBuffer rtp_packet(rtp_packet_data, rtp_len, packet_size);
1161
1162 int packet_count_before = received_packet_count_;
1163 rtc::PacketOptions options;
1164 // Send a packet and verify that the packet can be successfully received and
1165 // decrypted.
1166 ASSERT_TRUE(sender_transport->rtp_transport()->SendRtpPacket(
1167 &rtp_packet, options, cricket::PF_SRTP_BYPASS));
1168 EXPECT_EQ(packet_count_before + 1, received_packet_count_);
1169 }
1170
1171 int sequence_number_ = 0;
1172 int received_packet_count_ = 0;
Zhi Huang365381f2018-04-13 16:44:34 -07001173 std::unique_ptr<JsepTransport> jsep_transport1_;
1174 std::unique_ptr<JsepTransport> jsep_transport2_;
Zhi Huange830e682018-03-30 10:48:35 -07001175 std::vector<int> recv_encrypted_headers1_;
1176 std::vector<int> recv_encrypted_headers2_;
1177};
1178
1179// Test that the encrypted header extension works and can be changed in
1180// different scenarios.
1181TEST_P(JsepTransport2HeaderExtensionTest, EncryptedHeaderExtensionNegotiation) {
1182 Scenario scenario = std::get<0>(GetParam());
1183 bool use_gcm = std::get<1>(GetParam());
Harald Alvestrand0d018412021-11-04 13:52:31 +00001184 SrtpMode mode = SrtpMode ::kDtlsSrtp;
1185 if (scenario == Scenario::kSdes) {
1186 mode = SrtpMode::kSdes;
1187 }
1188 CreateJsepTransportPair(mode);
Zhi Huange830e682018-03-30 10:48:35 -07001189 recv_encrypted_headers1_.push_back(kHeaderExtensionIDs[0]);
1190 recv_encrypted_headers2_.push_back(kHeaderExtensionIDs[1]);
1191
Harald Alvestrand0d018412021-11-04 13:52:31 +00001192 cricket::CryptoParams sdes_param(1, rtc::kCsAesCm128HmacSha1_80,
1193 "inline:" + rtc::CreateRandomString(40),
1194 std::string());
Zhi Huange830e682018-03-30 10:48:35 -07001195 if (use_gcm) {
1196 auto fake_dtls1 =
1197 static_cast<FakeDtlsTransport*>(jsep_transport1_->rtp_dtls_transport());
1198 auto fake_dtls2 =
1199 static_cast<FakeDtlsTransport*>(jsep_transport2_->rtp_dtls_transport());
1200
Mirko Bonadei7750d802021-07-26 17:27:42 +02001201 fake_dtls1->SetSrtpCryptoSuite(rtc::kSrtpAeadAes256Gcm);
1202 fake_dtls2->SetSrtpCryptoSuite(rtc::kSrtpAeadAes256Gcm);
Zhi Huange830e682018-03-30 10:48:35 -07001203 }
1204
1205 if (scenario == Scenario::kDtlsBeforeCallerSendOffer) {
1206 ConnectTransport();
1207 }
1208
1209 JsepTransportDescription offer_desc;
1210 offer_desc.encrypted_header_extension_ids = recv_encrypted_headers1_;
Harald Alvestrand0d018412021-11-04 13:52:31 +00001211 if (scenario == Scenario::kSdes) {
1212 offer_desc.cryptos.push_back(sdes_param);
1213 }
Zhi Huange830e682018-03-30 10:48:35 -07001214 ASSERT_TRUE(
1215 jsep_transport1_
1216 ->SetLocalJsepTransportDescription(offer_desc, SdpType::kOffer)
1217 .ok());
1218 ASSERT_TRUE(
1219 jsep_transport2_
1220 ->SetRemoteJsepTransportDescription(offer_desc, SdpType::kOffer)
1221 .ok());
1222
1223 JsepTransportDescription answer_desc;
1224 answer_desc.encrypted_header_extension_ids = recv_encrypted_headers2_;
Harald Alvestrand0d018412021-11-04 13:52:31 +00001225 if (scenario == Scenario::kSdes) {
1226 answer_desc.cryptos.push_back(sdes_param);
1227 }
Zhi Huange830e682018-03-30 10:48:35 -07001228 ASSERT_TRUE(
1229 jsep_transport2_
1230 ->SetLocalJsepTransportDescription(answer_desc, SdpType::kAnswer)
1231 .ok());
1232
1233 if (scenario == Scenario::kDtlsBeforeCallerSetAnswer) {
1234 ConnectTransport();
1235 // Sending packet from transport2 to transport1 should work when they are
1236 // partially configured.
1237 TestOneWaySendRecvPacketWithEncryptedHeaderExtension(
1238 /*sender_transport=*/jsep_transport2_.get());
1239 }
1240
1241 ASSERT_TRUE(
1242 jsep_transport1_
1243 ->SetRemoteJsepTransportDescription(answer_desc, SdpType::kAnswer)
1244 .ok());
1245
Harald Alvestrand0d018412021-11-04 13:52:31 +00001246 if (scenario == Scenario::kDtlsAfterCallerSetAnswer ||
1247 scenario == Scenario::kSdes) {
Zhi Huange830e682018-03-30 10:48:35 -07001248 ConnectTransport();
1249 }
1250 EXPECT_TRUE(jsep_transport1_->rtp_transport()->IsSrtpActive());
1251 EXPECT_TRUE(jsep_transport2_->rtp_transport()->IsSrtpActive());
1252 TestSendRecvPacketWithEncryptedHeaderExtension();
1253
1254 // Change the encrypted header extension in a new offer/answer exchange.
1255 recv_encrypted_headers1_.clear();
1256 recv_encrypted_headers2_.clear();
1257 recv_encrypted_headers1_.push_back(kHeaderExtensionIDs[1]);
1258 recv_encrypted_headers2_.push_back(kHeaderExtensionIDs[0]);
1259 offer_desc.encrypted_header_extension_ids = recv_encrypted_headers1_;
1260 answer_desc.encrypted_header_extension_ids = recv_encrypted_headers2_;
1261 ASSERT_TRUE(
1262 jsep_transport1_
1263 ->SetLocalJsepTransportDescription(offer_desc, SdpType::kOffer)
1264 .ok());
1265 ASSERT_TRUE(
1266 jsep_transport2_
1267 ->SetRemoteJsepTransportDescription(offer_desc, SdpType::kOffer)
1268 .ok());
1269 ASSERT_TRUE(
1270 jsep_transport2_
1271 ->SetLocalJsepTransportDescription(answer_desc, SdpType::kAnswer)
1272 .ok());
1273 ASSERT_TRUE(
1274 jsep_transport1_
1275 ->SetRemoteJsepTransportDescription(answer_desc, SdpType::kAnswer)
1276 .ok());
1277 EXPECT_TRUE(jsep_transport1_->rtp_transport()->IsSrtpActive());
1278 EXPECT_TRUE(jsep_transport2_->rtp_transport()->IsSrtpActive());
1279 TestSendRecvPacketWithEncryptedHeaderExtension();
1280}
1281
Mirko Bonadeic84f6612019-01-31 12:20:57 +01001282INSTANTIATE_TEST_SUITE_P(
Zhi Huange830e682018-03-30 10:48:35 -07001283 JsepTransport2Test,
1284 JsepTransport2HeaderExtensionTest,
1285 ::testing::Values(
Harald Alvestrand0d018412021-11-04 13:52:31 +00001286 std::make_tuple(Scenario::kSdes, false),
Zhi Huange830e682018-03-30 10:48:35 -07001287 std::make_tuple(Scenario::kDtlsBeforeCallerSendOffer, true),
1288 std::make_tuple(Scenario::kDtlsBeforeCallerSetAnswer, true),
1289 std::make_tuple(Scenario::kDtlsAfterCallerSetAnswer, true),
1290 std::make_tuple(Scenario::kDtlsBeforeCallerSendOffer, false),
1291 std::make_tuple(Scenario::kDtlsBeforeCallerSetAnswer, false),
1292 std::make_tuple(Scenario::kDtlsAfterCallerSetAnswer, false)));
Jonas Oreland52aea5d2020-03-03 13:21:30 +01001293
1294// This test verifies the ICE parameters are properly applied to the transports.
1295TEST_F(JsepTransport2Test, SetIceParametersWithRenomination) {
Harald Alvestrand0d018412021-11-04 13:52:31 +00001296 jsep_transport_ =
1297 CreateJsepTransport2(/* rtcp_mux_enabled= */ true, SrtpMode::kDtlsSrtp);
Jonas Oreland52aea5d2020-03-03 13:21:30 +01001298
1299 JsepTransportDescription jsep_description;
1300 jsep_description.transport_desc = TransportDescription(kIceUfrag1, kIcePwd1);
1301 jsep_description.transport_desc.AddOption(ICE_OPTION_RENOMINATION);
1302 ASSERT_TRUE(
1303 jsep_transport_
1304 ->SetLocalJsepTransportDescription(jsep_description, SdpType::kOffer)
1305 .ok());
1306 auto fake_ice_transport = static_cast<FakeIceTransport*>(
1307 jsep_transport_->rtp_dtls_transport()->ice_transport());
1308 EXPECT_EQ(ICEMODE_FULL, fake_ice_transport->remote_ice_mode());
1309 EXPECT_EQ(kIceUfrag1, fake_ice_transport->ice_ufrag());
1310 EXPECT_EQ(kIcePwd1, fake_ice_transport->ice_pwd());
1311 EXPECT_TRUE(fake_ice_transport->ice_parameters().renomination);
1312
1313 jsep_description.transport_desc = TransportDescription(kIceUfrag2, kIcePwd2);
1314 jsep_description.transport_desc.AddOption(ICE_OPTION_RENOMINATION);
1315 ASSERT_TRUE(jsep_transport_
1316 ->SetRemoteJsepTransportDescription(jsep_description,
1317 SdpType::kAnswer)
1318 .ok());
1319 fake_ice_transport = static_cast<FakeIceTransport*>(
1320 jsep_transport_->rtp_dtls_transport()->ice_transport());
1321 EXPECT_EQ(ICEMODE_FULL, fake_ice_transport->remote_ice_mode());
1322 EXPECT_EQ(kIceUfrag2, fake_ice_transport->remote_ice_ufrag());
1323 EXPECT_EQ(kIcePwd2, fake_ice_transport->remote_ice_pwd());
1324 EXPECT_TRUE(fake_ice_transport->remote_ice_parameters().renomination);
1325}
1326
Qingsi Wang25ec8882019-11-15 12:33:05 -08001327} // namespace
Zhi Huange818b6e2018-02-22 15:26:27 -08001328} // namespace cricket