blob: bb669304f4d7498a28c1dca77561812827c82bb8 [file] [log] [blame]
Steve Anton6b63cd52017-10-06 11:20:31 -07001/*
2 * Copyright 2017 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
Mirko Bonadei317a1f02019-09-17 17:06:18 +020011#include <memory>
12
Karl Wiberg1b0eae32017-10-17 14:48:54 +020013#include "api/audio_codecs/builtin_audio_decoder_factory.h"
14#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Mirko Bonadei2ff3f492018-11-22 09:00:13 +010015#include "api/create_peerconnection_factory.h"
Anders Carlsson67537952018-05-03 11:28:29 +020016#include "api/video_codecs/builtin_video_decoder_factory.h"
17#include "api/video_codecs/builtin_video_encoder_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "p2p/base/fake_port_allocator.h"
19#include "pc/media_session.h"
20#include "pc/peer_connection_wrapper.h"
21#include "pc/sdp_utils.h"
Steve Anton6b63cd52017-10-06 11:20:31 -070022#ifdef WEBRTC_ANDROID
Steve Anton10542f22019-01-11 09:11:00 -080023#include "pc/test/android_test_initializer.h"
Steve Anton6b63cd52017-10-06 11:20:31 -070024#endif
Steve Anton10542f22019-01-11 09:11:00 -080025#include "pc/test/fake_audio_capture_module.h"
26#include "pc/test/fake_rtc_certificate_generator.h"
Steve Anton6b63cd52017-10-06 11:20:31 -070027#include "rtc_base/gunit.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "rtc_base/virtual_socket_server.h"
Steve Anton6b63cd52017-10-06 11:20:31 -070029
30namespace webrtc {
31
32using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
Steve Anton8a63f782017-10-23 13:08:53 -070033using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
Steve Anton6b63cd52017-10-06 11:20:31 -070034using ::testing::Combine;
Jonas Olssona4d87372019-07-05 19:08:33 +020035using ::testing::Values;
Steve Anton6b63cd52017-10-06 11:20:31 -070036
37constexpr int kGenerateCertTimeout = 1000;
38
Steve Anton71182f42018-01-19 14:59:54 -080039class PeerConnectionCryptoBaseTest : public ::testing::Test {
Steve Anton6b63cd52017-10-06 11:20:31 -070040 protected:
41 typedef std::unique_ptr<PeerConnectionWrapper> WrapperPtr;
42
Steve Anton71182f42018-01-19 14:59:54 -080043 explicit PeerConnectionCryptoBaseTest(SdpSemantics sdp_semantics)
44 : vss_(new rtc::VirtualSocketServer()),
45 main_(vss_.get()),
46 sdp_semantics_(sdp_semantics) {
Steve Anton6b63cd52017-10-06 11:20:31 -070047#ifdef WEBRTC_ANDROID
48 InitializeAndroidObjects();
49#endif
50 pc_factory_ = CreatePeerConnectionFactory(
51 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +020052 FakeAudioCaptureModule::Create(), CreateBuiltinAudioEncoderFactory(),
Anders Carlsson67537952018-05-03 11:28:29 +020053 CreateBuiltinAudioDecoderFactory(), CreateBuiltinVideoEncoderFactory(),
54 CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
55 nullptr /* audio_processing */);
Steve Anton6b63cd52017-10-06 11:20:31 -070056 }
57
Steve Anton8a63f782017-10-23 13:08:53 -070058 WrapperPtr CreatePeerConnection() {
59 return CreatePeerConnection(RTCConfiguration());
60 }
61
Steve Anton6b63cd52017-10-06 11:20:31 -070062 WrapperPtr CreatePeerConnection(const RTCConfiguration& config) {
63 return CreatePeerConnection(config, nullptr);
64 }
65
66 WrapperPtr CreatePeerConnection(
67 const RTCConfiguration& config,
68 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_gen) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +020069 auto fake_port_allocator = std::make_unique<cricket::FakePortAllocator>(
Steve Anton6b63cd52017-10-06 11:20:31 -070070 rtc::Thread::Current(), nullptr);
Mirko Bonadei317a1f02019-09-17 17:06:18 +020071 auto observer = std::make_unique<MockPeerConnectionObserver>();
Steve Anton71182f42018-01-19 14:59:54 -080072 RTCConfiguration modified_config = config;
73 modified_config.sdp_semantics = sdp_semantics_;
Steve Anton6b63cd52017-10-06 11:20:31 -070074 auto pc = pc_factory_->CreatePeerConnection(
Steve Anton71182f42018-01-19 14:59:54 -080075 modified_config, std::move(fake_port_allocator), std::move(cert_gen),
Steve Anton6b63cd52017-10-06 11:20:31 -070076 observer.get());
77 if (!pc) {
78 return nullptr;
79 }
80
Yves Gerey4e933292018-10-31 15:36:05 +010081 observer->SetPeerConnectionInterface(pc.get());
Mirko Bonadei317a1f02019-09-17 17:06:18 +020082 return std::make_unique<PeerConnectionWrapper>(pc_factory_, pc,
83 std::move(observer));
Steve Anton6b63cd52017-10-06 11:20:31 -070084 }
85
86 // Accepts the same arguments as CreatePeerConnection and adds default audio
87 // and video tracks.
88 template <typename... Args>
89 WrapperPtr CreatePeerConnectionWithAudioVideo(Args&&... args) {
90 auto wrapper = CreatePeerConnection(std::forward<Args>(args)...);
91 if (!wrapper) {
92 return nullptr;
93 }
Steve Anton8d3444d2017-10-20 15:30:51 -070094 wrapper->AddAudioTrack("a");
95 wrapper->AddVideoTrack("v");
Steve Anton6b63cd52017-10-06 11:20:31 -070096 return wrapper;
97 }
98
Steve Anton8a63f782017-10-23 13:08:53 -070099 cricket::ConnectionRole& AudioConnectionRole(
100 cricket::SessionDescription* desc) {
101 return ConnectionRoleFromContent(desc, cricket::GetFirstAudioContent(desc));
102 }
103
104 cricket::ConnectionRole& VideoConnectionRole(
105 cricket::SessionDescription* desc) {
106 return ConnectionRoleFromContent(desc, cricket::GetFirstVideoContent(desc));
107 }
108
109 cricket::ConnectionRole& ConnectionRoleFromContent(
110 cricket::SessionDescription* desc,
111 cricket::ContentInfo* content) {
112 RTC_DCHECK(content);
113 auto* transport_info = desc->GetTransportInfoByName(content->name);
114 RTC_DCHECK(transport_info);
115 return transport_info->description.connection_role;
116 }
117
Steve Anton6b63cd52017-10-06 11:20:31 -0700118 std::unique_ptr<rtc::VirtualSocketServer> vss_;
119 rtc::AutoSocketServerThread main_;
120 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_;
Steve Anton71182f42018-01-19 14:59:54 -0800121 const SdpSemantics sdp_semantics_;
Steve Anton6b63cd52017-10-06 11:20:31 -0700122};
123
124SdpContentPredicate HaveDtlsFingerprint() {
125 return [](const cricket::ContentInfo* content,
126 const cricket::TransportInfo* transport) {
127 return transport->description.identity_fingerprint != nullptr;
128 };
129}
130
Steve Anton6b63cd52017-10-06 11:20:31 -0700131SdpContentPredicate HaveProtocol(const std::string& protocol) {
132 return [protocol](const cricket::ContentInfo* content,
133 const cricket::TransportInfo* transport) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800134 return content->media_description()->protocol() == protocol;
Steve Anton6b63cd52017-10-06 11:20:31 -0700135 };
136}
137
Steve Anton71182f42018-01-19 14:59:54 -0800138class PeerConnectionCryptoTest
139 : public PeerConnectionCryptoBaseTest,
140 public ::testing::WithParamInterface<SdpSemantics> {
141 protected:
142 PeerConnectionCryptoTest() : PeerConnectionCryptoBaseTest(GetParam()) {}
143};
144
Steve Anton6b63cd52017-10-06 11:20:31 -0700145SdpContentMutator RemoveDtlsFingerprint() {
146 return [](cricket::ContentInfo* content, cricket::TransportInfo* transport) {
147 transport->description.identity_fingerprint.reset();
148 };
149}
150
Harald Alvestrandee212a72021-11-02 12:06:45 +0000151// When DTLS is enabled, the SDP offer/answer should have a DTLS fingerprint.
Steve Anton71182f42018-01-19 14:59:54 -0800152TEST_P(PeerConnectionCryptoTest, CorrectCryptoInOfferWhenDtlsEnabled) {
Steve Anton6b63cd52017-10-06 11:20:31 -0700153 RTCConfiguration config;
Steve Anton6b63cd52017-10-06 11:20:31 -0700154 auto caller = CreatePeerConnectionWithAudioVideo(config);
155
156 auto offer = caller->CreateOffer();
157 ASSERT_TRUE(offer);
158
159 ASSERT_FALSE(offer->description()->contents().empty());
160 EXPECT_TRUE(SdpContentsAll(HaveDtlsFingerprint(), offer->description()));
Steve Anton6b63cd52017-10-06 11:20:31 -0700161 EXPECT_TRUE(SdpContentsAll(HaveProtocol(cricket::kMediaProtocolDtlsSavpf),
162 offer->description()));
163}
Steve Anton71182f42018-01-19 14:59:54 -0800164TEST_P(PeerConnectionCryptoTest, CorrectCryptoInAnswerWhenDtlsEnabled) {
Steve Anton6b63cd52017-10-06 11:20:31 -0700165 RTCConfiguration config;
Steve Anton6b63cd52017-10-06 11:20:31 -0700166 auto caller = CreatePeerConnectionWithAudioVideo(config);
167 auto callee = CreatePeerConnectionWithAudioVideo(config);
168
169 callee->SetRemoteDescription(caller->CreateOffer());
170 auto answer = callee->CreateAnswer();
171 ASSERT_TRUE(answer);
172
173 ASSERT_FALSE(answer->description()->contents().empty());
174 EXPECT_TRUE(SdpContentsAll(HaveDtlsFingerprint(), answer->description()));
Steve Anton6b63cd52017-10-06 11:20:31 -0700175 EXPECT_TRUE(SdpContentsAll(HaveProtocol(cricket::kMediaProtocolDtlsSavpf),
176 answer->description()));
177}
178
Steve Anton6b63cd52017-10-06 11:20:31 -0700179// When encryption is disabled, the SDP offer/answer should have neither a DTLS
180// fingerprint nor any SDES crypto options.
Steve Anton71182f42018-01-19 14:59:54 -0800181TEST_P(PeerConnectionCryptoTest, CorrectCryptoInOfferWhenEncryptionDisabled) {
Steve Anton6b63cd52017-10-06 11:20:31 -0700182 PeerConnectionFactoryInterface::Options options;
183 options.disable_encryption = true;
184 pc_factory_->SetOptions(options);
185
186 RTCConfiguration config;
Steve Anton6b63cd52017-10-06 11:20:31 -0700187 auto caller = CreatePeerConnectionWithAudioVideo(config);
188
189 auto offer = caller->CreateOffer();
190 ASSERT_TRUE(offer);
191
192 ASSERT_FALSE(offer->description()->contents().empty());
Steve Anton6b63cd52017-10-06 11:20:31 -0700193 EXPECT_TRUE(SdpContentsNone(HaveDtlsFingerprint(), offer->description()));
194 EXPECT_TRUE(SdpContentsAll(HaveProtocol(cricket::kMediaProtocolAvpf),
195 offer->description()));
196}
Steve Anton71182f42018-01-19 14:59:54 -0800197TEST_P(PeerConnectionCryptoTest, CorrectCryptoInAnswerWhenEncryptionDisabled) {
Steve Anton6b63cd52017-10-06 11:20:31 -0700198 PeerConnectionFactoryInterface::Options options;
199 options.disable_encryption = true;
200 pc_factory_->SetOptions(options);
201
202 RTCConfiguration config;
Steve Anton6b63cd52017-10-06 11:20:31 -0700203 auto caller = CreatePeerConnectionWithAudioVideo(config);
204 auto callee = CreatePeerConnectionWithAudioVideo(config);
205
206 callee->SetRemoteDescription(caller->CreateOffer());
207 auto answer = callee->CreateAnswer();
208 ASSERT_TRUE(answer);
209
210 ASSERT_FALSE(answer->description()->contents().empty());
Steve Anton6b63cd52017-10-06 11:20:31 -0700211 EXPECT_TRUE(SdpContentsNone(HaveDtlsFingerprint(), answer->description()));
212 EXPECT_TRUE(SdpContentsAll(HaveProtocol(cricket::kMediaProtocolAvpf),
213 answer->description()));
214}
215
Steve Anton71182f42018-01-19 14:59:54 -0800216TEST_P(PeerConnectionCryptoTest, CanSetSdesGcmRemoteOfferAndLocalAnswer) {
Steve Anton6b63cd52017-10-06 11:20:31 -0700217 PeerConnectionFactoryInterface::Options options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700218 options.crypto_options.srtp.enable_gcm_crypto_suites = true;
Steve Anton6b63cd52017-10-06 11:20:31 -0700219 pc_factory_->SetOptions(options);
220
221 RTCConfiguration config;
Steve Anton6b63cd52017-10-06 11:20:31 -0700222 auto caller = CreatePeerConnectionWithAudioVideo(config);
223 auto callee = CreatePeerConnectionWithAudioVideo(config);
224
225 auto offer = caller->CreateOffer();
226 ASSERT_TRUE(offer);
227 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
228
229 auto answer = callee->CreateAnswer();
230 ASSERT_TRUE(answer);
231 ASSERT_TRUE(callee->SetLocalDescription(std::move(answer)));
Harald Alvestrandf9e502d2021-10-13 15:26:26 +0000232 // Note - this test doesn't verify that Gcm is present, just that it
233 // does not caue a failure.
Steve Anton6b63cd52017-10-06 11:20:31 -0700234}
235
236// The following group tests that two PeerConnections can successfully exchange
237// an offer/answer when DTLS is on and that they will refuse any offer/answer
238// applied locally/remotely if it does not include a DTLS fingerprint.
Steve Anton71182f42018-01-19 14:59:54 -0800239TEST_P(PeerConnectionCryptoTest, ExchangeOfferAnswerWhenDtlsOn) {
Steve Anton6b63cd52017-10-06 11:20:31 -0700240 RTCConfiguration config;
Steve Anton6b63cd52017-10-06 11:20:31 -0700241 auto caller = CreatePeerConnectionWithAudioVideo(config);
242 auto callee = CreatePeerConnectionWithAudioVideo(config);
243
244 auto offer = caller->CreateOfferAndSetAsLocal();
245 ASSERT_TRUE(offer);
246 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
247
248 auto answer = callee->CreateAnswerAndSetAsLocal();
249 ASSERT_TRUE(answer);
250 ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer)));
251}
Steve Anton71182f42018-01-19 14:59:54 -0800252TEST_P(PeerConnectionCryptoTest,
Steve Anton6b63cd52017-10-06 11:20:31 -0700253 FailToSetLocalOfferWithNoFingerprintWhenDtlsOn) {
254 RTCConfiguration config;
Steve Anton6b63cd52017-10-06 11:20:31 -0700255 auto caller = CreatePeerConnectionWithAudioVideo(config);
256
257 auto offer = caller->CreateOffer();
258 SdpContentsForEach(RemoveDtlsFingerprint(), offer->description());
259
260 EXPECT_FALSE(caller->SetLocalDescription(std::move(offer)));
261}
Steve Anton71182f42018-01-19 14:59:54 -0800262TEST_P(PeerConnectionCryptoTest,
Steve Anton6b63cd52017-10-06 11:20:31 -0700263 FailToSetRemoteOfferWithNoFingerprintWhenDtlsOn) {
264 RTCConfiguration config;
Steve Anton6b63cd52017-10-06 11:20:31 -0700265 auto caller = CreatePeerConnectionWithAudioVideo(config);
266 auto callee = CreatePeerConnectionWithAudioVideo(config);
267
268 auto offer = caller->CreateOffer();
269 SdpContentsForEach(RemoveDtlsFingerprint(), offer->description());
270
271 EXPECT_FALSE(callee->SetRemoteDescription(std::move(offer)));
272}
Steve Anton71182f42018-01-19 14:59:54 -0800273TEST_P(PeerConnectionCryptoTest,
Steve Anton6b63cd52017-10-06 11:20:31 -0700274 FailToSetLocalAnswerWithNoFingerprintWhenDtlsOn) {
275 RTCConfiguration config;
Steve Anton6b63cd52017-10-06 11:20:31 -0700276 auto caller = CreatePeerConnectionWithAudioVideo(config);
277 auto callee = CreatePeerConnectionWithAudioVideo(config);
278
279 callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal());
280 auto answer = callee->CreateAnswer();
281 SdpContentsForEach(RemoveDtlsFingerprint(), answer->description());
282}
Steve Anton71182f42018-01-19 14:59:54 -0800283TEST_P(PeerConnectionCryptoTest,
Steve Anton6b63cd52017-10-06 11:20:31 -0700284 FailToSetRemoteAnswerWithNoFingerprintWhenDtlsOn) {
285 RTCConfiguration config;
Steve Anton6b63cd52017-10-06 11:20:31 -0700286 auto caller = CreatePeerConnectionWithAudioVideo(config);
287 auto callee = CreatePeerConnectionWithAudioVideo(config);
288
289 callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal());
290 auto answer = callee->CreateAnswerAndSetAsLocal();
291 SdpContentsForEach(RemoveDtlsFingerprint(), answer->description());
292
293 EXPECT_FALSE(caller->SetRemoteDescription(std::move(answer)));
294}
295
296// Test that an offer/answer can be exchanged when encryption is disabled.
Steve Anton71182f42018-01-19 14:59:54 -0800297TEST_P(PeerConnectionCryptoTest, ExchangeOfferAnswerWhenNoEncryption) {
Steve Anton6b63cd52017-10-06 11:20:31 -0700298 PeerConnectionFactoryInterface::Options options;
299 options.disable_encryption = true;
300 pc_factory_->SetOptions(options);
301
302 RTCConfiguration config;
Steve Anton6b63cd52017-10-06 11:20:31 -0700303 auto caller = CreatePeerConnectionWithAudioVideo(config);
304 auto callee = CreatePeerConnectionWithAudioVideo(config);
305
306 auto offer = caller->CreateOfferAndSetAsLocal();
307 ASSERT_TRUE(offer);
308 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
309
310 auto answer = callee->CreateAnswerAndSetAsLocal();
311 ASSERT_TRUE(answer);
312 ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer)));
313}
314
315// Tests that a DTLS call can be established when the certificate is specified
316// in the PeerConnection config and no certificate generator is specified.
Steve Anton71182f42018-01-19 14:59:54 -0800317TEST_P(PeerConnectionCryptoTest,
Steve Anton6b63cd52017-10-06 11:20:31 -0700318 ExchangeOfferAnswerWhenDtlsCertificateInConfig) {
319 RTCConfiguration caller_config;
Steve Anton6b63cd52017-10-06 11:20:31 -0700320 caller_config.certificates.push_back(
321 FakeRTCCertificateGenerator::GenerateCertificate());
322 auto caller = CreatePeerConnectionWithAudioVideo(caller_config);
323
324 RTCConfiguration callee_config;
Steve Anton6b63cd52017-10-06 11:20:31 -0700325 callee_config.certificates.push_back(
326 FakeRTCCertificateGenerator::GenerateCertificate());
327 auto callee = CreatePeerConnectionWithAudioVideo(callee_config);
328
329 auto offer = caller->CreateOfferAndSetAsLocal();
330 ASSERT_TRUE(offer);
331 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
332
333 auto answer = callee->CreateAnswerAndSetAsLocal();
334 ASSERT_TRUE(answer);
335 ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer)));
336}
337
338// The following parameterized test verifies that CreateOffer/CreateAnswer
339// returns successfully (or with failure if the underlying certificate generator
340// fails) no matter when the DTLS certificate is generated. If multiple
341// CreateOffer/CreateAnswer calls are made while waiting for the certificate,
342// they all finish after the certificate is generated.
343
Steve Anton6b63cd52017-10-06 11:20:31 -0700344// Whether the certificate will be generated before calling CreateOffer or
345// while CreateOffer is executing.
346enum class CertGenTime { kBefore, kDuring };
347std::ostream& operator<<(std::ostream& out, CertGenTime value) {
348 switch (value) {
349 case CertGenTime::kBefore:
350 return out << "before";
351 case CertGenTime::kDuring:
352 return out << "during";
353 default:
354 return out << "unknown";
355 }
356}
357
358// Whether the fake certificate generator will produce a certificate or fail.
359enum class CertGenResult { kSucceed, kFail };
360std::ostream& operator<<(std::ostream& out, CertGenResult value) {
361 switch (value) {
362 case CertGenResult::kSucceed:
363 return out << "succeed";
364 case CertGenResult::kFail:
365 return out << "fail";
366 default:
367 return out << "unknown";
368 }
369}
370
Steve Anton71182f42018-01-19 14:59:54 -0800371class PeerConnectionCryptoDtlsCertGenTest
372 : public PeerConnectionCryptoBaseTest,
373 public ::testing::WithParamInterface<std::tuple<SdpSemantics,
374 SdpType,
375 CertGenTime,
376 CertGenResult,
377 size_t>> {
Steve Anton6b63cd52017-10-06 11:20:31 -0700378 protected:
Steve Anton71182f42018-01-19 14:59:54 -0800379 PeerConnectionCryptoDtlsCertGenTest()
380 : PeerConnectionCryptoBaseTest(std::get<0>(GetParam())) {
381 sdp_type_ = std::get<1>(GetParam());
382 cert_gen_time_ = std::get<2>(GetParam());
383 cert_gen_result_ = std::get<3>(GetParam());
384 concurrent_calls_ = std::get<4>(GetParam());
Steve Anton6b63cd52017-10-06 11:20:31 -0700385 }
386
387 SdpType sdp_type_;
388 CertGenTime cert_gen_time_;
389 CertGenResult cert_gen_result_;
390 size_t concurrent_calls_;
391};
392
Steve Anton71182f42018-01-19 14:59:54 -0800393TEST_P(PeerConnectionCryptoDtlsCertGenTest, TestCertificateGeneration) {
Steve Anton6b63cd52017-10-06 11:20:31 -0700394 RTCConfiguration config;
Steve Anton6b63cd52017-10-06 11:20:31 -0700395 auto owned_fake_certificate_generator =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200396 std::make_unique<FakeRTCCertificateGenerator>();
Steve Anton6b63cd52017-10-06 11:20:31 -0700397 auto* fake_certificate_generator = owned_fake_certificate_generator.get();
398 fake_certificate_generator->set_should_fail(cert_gen_result_ ==
399 CertGenResult::kFail);
400 fake_certificate_generator->set_should_wait(cert_gen_time_ ==
401 CertGenTime::kDuring);
402 WrapperPtr pc;
403 if (sdp_type_ == SdpType::kOffer) {
404 pc = CreatePeerConnectionWithAudioVideo(
405 config, std::move(owned_fake_certificate_generator));
406 } else {
407 auto caller = CreatePeerConnectionWithAudioVideo(config);
408 pc = CreatePeerConnectionWithAudioVideo(
409 config, std::move(owned_fake_certificate_generator));
410 pc->SetRemoteDescription(caller->CreateOfferAndSetAsLocal());
411 }
412 if (cert_gen_time_ == CertGenTime::kBefore) {
413 ASSERT_TRUE_WAIT(fake_certificate_generator->generated_certificates() +
414 fake_certificate_generator->generated_failures() >
415 0,
416 kGenerateCertTimeout);
417 } else {
418 ASSERT_EQ(fake_certificate_generator->generated_certificates(), 0);
419 fake_certificate_generator->set_should_wait(false);
420 }
421 std::vector<rtc::scoped_refptr<MockCreateSessionDescriptionObserver>>
422 observers;
423 for (size_t i = 0; i < concurrent_calls_; i++) {
424 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer =
Tommi87f70902021-04-27 14:43:08 +0200425 rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
Steve Anton6b63cd52017-10-06 11:20:31 -0700426 observers.push_back(observer);
427 if (sdp_type_ == SdpType::kOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +0200428 pc->pc()->CreateOffer(observer,
429 PeerConnectionInterface::RTCOfferAnswerOptions());
Steve Anton6b63cd52017-10-06 11:20:31 -0700430 } else {
Niels Möllerf06f9232018-08-07 12:32:18 +0200431 pc->pc()->CreateAnswer(observer,
432 PeerConnectionInterface::RTCOfferAnswerOptions());
Steve Anton6b63cd52017-10-06 11:20:31 -0700433 }
434 }
435 for (auto& observer : observers) {
436 EXPECT_TRUE_WAIT(observer->called(), 1000);
437 if (cert_gen_result_ == CertGenResult::kSucceed) {
438 EXPECT_TRUE(observer->result());
439 } else {
440 EXPECT_FALSE(observer->result());
441 }
442 }
443}
444
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100445INSTANTIATE_TEST_SUITE_P(
Steve Anton71182f42018-01-19 14:59:54 -0800446 PeerConnectionCryptoTest,
447 PeerConnectionCryptoDtlsCertGenTest,
448 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
449 Values(SdpType::kOffer, SdpType::kAnswer),
Steve Anton6b63cd52017-10-06 11:20:31 -0700450 Values(CertGenTime::kBefore, CertGenTime::kDuring),
451 Values(CertGenResult::kSucceed, CertGenResult::kFail),
452 Values(1, 3)));
453
Steve Anton8a63f782017-10-23 13:08:53 -0700454// Test that we can create and set an answer correctly when different
455// SSL roles have been negotiated for different transports.
456// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=4525
Steve Anton71182f42018-01-19 14:59:54 -0800457TEST_P(PeerConnectionCryptoTest, CreateAnswerWithDifferentSslRoles) {
Steve Anton8a63f782017-10-23 13:08:53 -0700458 auto caller = CreatePeerConnectionWithAudioVideo();
459 auto callee = CreatePeerConnectionWithAudioVideo();
460
461 RTCOfferAnswerOptions options_no_bundle;
462 options_no_bundle.use_rtp_mux = false;
463
464 // First, negotiate different SSL roles for audio and video.
465 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
466 auto answer = callee->CreateAnswer(options_no_bundle);
467
468 AudioConnectionRole(answer->description()) = cricket::CONNECTIONROLE_ACTIVE;
469 VideoConnectionRole(answer->description()) = cricket::CONNECTIONROLE_PASSIVE;
470
471 ASSERT_TRUE(
472 callee->SetLocalDescription(CloneSessionDescription(answer.get())));
473 ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer)));
474
475 // Now create an offer in the reverse direction, and ensure the initial
476 // offerer responds with an answer with the correct SSL roles.
477 ASSERT_TRUE(caller->SetRemoteDescription(callee->CreateOfferAndSetAsLocal()));
478 answer = caller->CreateAnswer(options_no_bundle);
479
480 EXPECT_EQ(cricket::CONNECTIONROLE_PASSIVE,
481 AudioConnectionRole(answer->description()));
482 EXPECT_EQ(cricket::CONNECTIONROLE_ACTIVE,
483 VideoConnectionRole(answer->description()));
484
485 ASSERT_TRUE(
486 caller->SetLocalDescription(CloneSessionDescription(answer.get())));
487 ASSERT_TRUE(callee->SetRemoteDescription(std::move(answer)));
488
489 // Lastly, start BUNDLE-ing on "audio", expecting that the "passive" role of
490 // audio is transferred over to video in the answer that completes the BUNDLE
491 // negotiation.
492 RTCOfferAnswerOptions options_bundle;
493 options_bundle.use_rtp_mux = true;
494
495 ASSERT_TRUE(caller->SetRemoteDescription(callee->CreateOfferAndSetAsLocal()));
496 answer = caller->CreateAnswer(options_bundle);
497
498 EXPECT_EQ(cricket::CONNECTIONROLE_PASSIVE,
499 AudioConnectionRole(answer->description()));
500 EXPECT_EQ(cricket::CONNECTIONROLE_PASSIVE,
501 VideoConnectionRole(answer->description()));
502
503 ASSERT_TRUE(
504 caller->SetLocalDescription(CloneSessionDescription(answer.get())));
505 ASSERT_TRUE(callee->SetRemoteDescription(std::move(answer)));
506}
507
Steve Anton80dd7b52018-02-16 17:08:42 -0800508// Tests that if the DTLS fingerprint is invalid then all future calls to
509// SetLocalDescription and SetRemoteDescription will fail due to a session
510// error.
511// This is a regression test for crbug.com/800775
512TEST_P(PeerConnectionCryptoTest, SessionErrorIfFingerprintInvalid) {
513 auto callee_certificate = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
514 auto other_certificate = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
515
516 auto caller = CreatePeerConnectionWithAudioVideo();
517 RTCConfiguration callee_config;
Steve Anton80dd7b52018-02-16 17:08:42 -0800518 callee_config.certificates.push_back(callee_certificate);
519 auto callee = CreatePeerConnectionWithAudioVideo(callee_config);
520
521 ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
522
523 // Create an invalid answer with the other certificate's fingerprint.
Steve Anton25ca0ac2019-06-25 10:40:48 -0700524 auto valid_answer = callee->CreateAnswer();
525 auto invalid_answer = CloneSessionDescription(valid_answer.get());
Steve Anton80dd7b52018-02-16 17:08:42 -0800526 auto* audio_content =
527 cricket::GetFirstAudioContent(invalid_answer->description());
528 ASSERT_TRUE(audio_content);
529 auto* audio_transport_info =
530 invalid_answer->description()->GetTransportInfoByName(
531 audio_content->name);
532 ASSERT_TRUE(audio_transport_info);
Steve Anton4905edb2018-10-15 19:27:44 -0700533 audio_transport_info->description.identity_fingerprint =
534 rtc::SSLFingerprint::CreateFromCertificate(*other_certificate);
Steve Anton80dd7b52018-02-16 17:08:42 -0800535
536 // Set the invalid answer and expect a fingerprint error.
537 std::string error;
538 ASSERT_FALSE(callee->SetLocalDescription(std::move(invalid_answer), &error));
539 EXPECT_PRED_FORMAT2(AssertStringContains, error,
540 "Local fingerprint does not match identity.");
541
542 // Make sure that setting a valid remote offer or local answer also fails now.
543 ASSERT_FALSE(callee->SetRemoteDescription(caller->CreateOffer(), &error));
544 EXPECT_PRED_FORMAT2(AssertStringContains, error,
545 "Session error code: ERROR_CONTENT.");
Steve Anton25ca0ac2019-06-25 10:40:48 -0700546 ASSERT_FALSE(callee->SetLocalDescription(std::move(valid_answer), &error));
Steve Anton80dd7b52018-02-16 17:08:42 -0800547 EXPECT_PRED_FORMAT2(AssertStringContains, error,
548 "Session error code: ERROR_CONTENT.");
549}
550
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100551INSTANTIATE_TEST_SUITE_P(PeerConnectionCryptoTest,
552 PeerConnectionCryptoTest,
553 Values(SdpSemantics::kPlanB,
554 SdpSemantics::kUnifiedPlan));
Steve Anton71182f42018-01-19 14:59:54 -0800555
Steve Anton6b63cd52017-10-06 11:20:31 -0700556} // namespace webrtc