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