Index: webrtc/api/rtcstatscollector_unittest.cc |
diff --git a/webrtc/api/rtcstatscollector_unittest.cc b/webrtc/api/rtcstatscollector_unittest.cc |
index 446990c574129238a24798453f872d8f234005e3..020a759a68ec5a470a66ede1a91555dc4b5f1466 100644 |
--- a/webrtc/api/rtcstatscollector_unittest.cc |
+++ b/webrtc/api/rtcstatscollector_unittest.cc |
@@ -22,6 +22,7 @@ |
#include "webrtc/api/test/mock_webrtcsession.h" |
#include "webrtc/base/checks.h" |
#include "webrtc/base/fakeclock.h" |
+#include "webrtc/base/fakesslidentity.h" |
#include "webrtc/base/gunit.h" |
#include "webrtc/base/logging.h" |
#include "webrtc/base/thread_checker.h" |
@@ -29,6 +30,8 @@ |
#include "webrtc/base/timeutils.h" |
#include "webrtc/media/base/fakemediaengine.h" |
+using testing::_; |
+using testing::Invoke; |
using testing::Return; |
using testing::ReturnRef; |
@@ -38,6 +41,56 @@ namespace { |
const int64_t kGetStatsReportTimeoutMs = 1000; |
+struct CertificateInfo { |
+ rtc::scoped_refptr<rtc::RTCCertificate> certificate; |
+ std::vector<std::string> ders; |
+ std::vector<std::string> pems; |
+ std::vector<std::string> fingerprints; |
+}; |
+ |
+std::unique_ptr<CertificateInfo> CreateFakeCertificateAndInfoFromDers( |
+ const std::vector<std::string>& ders) { |
+ RTC_CHECK(!ders.empty()); |
+ std::unique_ptr<CertificateInfo> info(new CertificateInfo()); |
+ info->ders = ders; |
+ for (const std::string& der : ders) { |
+ info->pems.push_back(rtc::SSLIdentity::DerToPem( |
+ "CERTIFICATE", |
+ reinterpret_cast<const unsigned char*>(der.c_str()), |
+ der.length())); |
+ } |
+ info->certificate = |
+ rtc::RTCCertificate::Create(std::unique_ptr<rtc::FakeSSLIdentity>( |
+ new rtc::FakeSSLIdentity(rtc::FakeSSLCertificate(info->pems)))); |
+ // Strip header/footer and newline characters of PEM strings. |
+ for (size_t i = 0; i < info->pems.size(); ++i) { |
+ rtc::replace_substrs("-----BEGIN CERTIFICATE-----", 27, |
+ "", 0, &info->pems[i]); |
+ rtc::replace_substrs("-----END CERTIFICATE-----", 25, |
+ "", 0, &info->pems[i]); |
+ rtc::replace_substrs("\n", 1, |
+ "", 0, &info->pems[i]); |
+ } |
+ // Fingerprint of leaf certificate. |
+ std::unique_ptr<rtc::SSLFingerprint> fp( |
+ rtc::SSLFingerprint::Create("sha-1", |
+ &info->certificate->ssl_certificate())); |
+ EXPECT_TRUE(fp); |
+ info->fingerprints.push_back(fp->GetRfc4572Fingerprint()); |
+ // Fingerprints of the rest of the chain. |
+ std::unique_ptr<rtc::SSLCertChain> chain = |
+ info->certificate->ssl_certificate().GetChain(); |
+ if (chain) { |
+ for (size_t i = 0; i < chain->GetSize(); i++) { |
+ fp.reset(rtc::SSLFingerprint::Create("sha-1", &chain->Get(i))); |
+ EXPECT_TRUE(fp); |
+ info->fingerprints.push_back(fp->GetRfc4572Fingerprint()); |
+ } |
+ } |
+ EXPECT_EQ(info->ders.size(), info->fingerprints.size()); |
+ return info; |
+} |
+ |
class RTCStatsCollectorTestHelper : public SetSessionDescriptionObserver { |
public: |
RTCStatsCollectorTestHelper() |
@@ -53,9 +106,11 @@ class RTCStatsCollectorTestHelper : public SetSessionDescriptionObserver { |
channel_manager_.get())), |
session_(media_controller_.get()), |
pc_() { |
+ // Default return values for mocks. |
EXPECT_CALL(pc_, session()).WillRepeatedly(Return(&session_)); |
EXPECT_CALL(pc_, sctp_data_channels()).WillRepeatedly( |
ReturnRef(data_channels_)); |
+ EXPECT_CALL(session_, GetTransportStats(_)).WillRepeatedly(Return(false)); |
} |
rtc::ScopedFakeClock& fake_clock() { return fake_clock_; } |
@@ -253,6 +308,27 @@ class RTCStatsCollectorTest : public testing::Test { |
return callback->report(); |
} |
+ void ExpectReportContainsCertificateInfo( |
+ const rtc::scoped_refptr<const RTCStatsReport>& report, |
+ const CertificateInfo& cert_info) { |
+ for (size_t i = 0; i < cert_info.fingerprints.size(); ++i) { |
+ const RTCStats* stats = report->Get( |
+ "RTCCertificate_" + cert_info.fingerprints[i]); |
+ EXPECT_TRUE(stats); |
+ const RTCCertificateStats& cert_stats = |
+ stats->cast_to<const RTCCertificateStats>(); |
+ EXPECT_EQ(*cert_stats.fingerprint, cert_info.fingerprints[i]); |
+ EXPECT_EQ(*cert_stats.fingerprint_algorithm, "sha-1"); |
+ EXPECT_EQ(*cert_stats.base64_certificate, cert_info.pems[i]); |
+ if (i + 1 < cert_info.fingerprints.size()) { |
+ EXPECT_EQ(*cert_stats.issuer_certificate_id, |
+ "RTCCertificate_" + cert_info.fingerprints[i + 1]); |
+ } else { |
+ EXPECT_FALSE(cert_stats.issuer_certificate_id.is_defined()); |
+ } |
+ } |
+ } |
+ |
protected: |
rtc::scoped_refptr<RTCStatsCollectorTestHelper> test_; |
rtc::scoped_refptr<RTCStatsCollector> collector_; |
@@ -312,6 +388,152 @@ TEST_F(RTCStatsCollectorTest, MultipleCallbacksWithInvalidatedCacheInBetween) { |
EXPECT_NE(c.get(), b.get()); |
} |
+TEST_F(RTCStatsCollectorTest, CollectRTCCertificateStatsSingle) { |
+ std::unique_ptr<CertificateInfo> local_certinfo = |
+ CreateFakeCertificateAndInfoFromDers( |
+ std::vector<std::string>({ "(local) single certificate" })); |
+ std::unique_ptr<CertificateInfo> remote_certinfo = |
+ CreateFakeCertificateAndInfoFromDers( |
+ std::vector<std::string>({ "(remote) single certificate" })); |
+ |
+ // Mock the session to return the local and remote certificates. |
+ EXPECT_CALL(test_->session(), GetTransportStats(_)).WillRepeatedly(Invoke( |
+ [this](SessionStats* stats) { |
+ stats->transport_stats["transport"].transport_name = "transport"; |
+ return true; |
+ })); |
+ EXPECT_CALL(test_->session(), GetLocalCertificate(_, _)).WillRepeatedly( |
+ Invoke([this, &local_certinfo](const std::string& transport_name, |
+ rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
+ if (transport_name == "transport") { |
+ *certificate = local_certinfo->certificate; |
+ return true; |
+ } |
+ return false; |
+ })); |
+ EXPECT_CALL(test_->session(), |
+ GetRemoteSSLCertificate_ReturnsRawPointer(_)).WillRepeatedly(Invoke( |
+ [this, &remote_certinfo](const std::string& transport_name) { |
+ if (transport_name == "transport") |
+ return remote_certinfo->certificate->ssl_certificate().GetReference(); |
+ return static_cast<rtc::SSLCertificate*>(nullptr); |
+ })); |
+ |
+ rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); |
+ ExpectReportContainsCertificateInfo(report, *local_certinfo.get()); |
+ ExpectReportContainsCertificateInfo(report, *remote_certinfo.get()); |
+} |
+ |
+TEST_F(RTCStatsCollectorTest, CollectRTCCertificateStatsMultiple) { |
+ std::unique_ptr<CertificateInfo> audio_local_certinfo = |
+ CreateFakeCertificateAndInfoFromDers( |
+ std::vector<std::string>({ "(local) audio" })); |
+ audio_local_certinfo = CreateFakeCertificateAndInfoFromDers( |
+ audio_local_certinfo->ders); |
+ std::unique_ptr<CertificateInfo> audio_remote_certinfo = |
+ CreateFakeCertificateAndInfoFromDers( |
+ std::vector<std::string>({ "(remote) audio" })); |
+ audio_remote_certinfo = CreateFakeCertificateAndInfoFromDers( |
+ audio_remote_certinfo->ders); |
+ |
+ std::unique_ptr<CertificateInfo> video_local_certinfo = |
+ CreateFakeCertificateAndInfoFromDers( |
+ std::vector<std::string>({ "(local) video" })); |
+ video_local_certinfo = CreateFakeCertificateAndInfoFromDers( |
+ video_local_certinfo->ders); |
+ std::unique_ptr<CertificateInfo> video_remote_certinfo = |
+ CreateFakeCertificateAndInfoFromDers( |
+ std::vector<std::string>({ "(remote) video" })); |
+ video_remote_certinfo = CreateFakeCertificateAndInfoFromDers( |
+ video_remote_certinfo->ders); |
+ |
+ // Mock the session to return the local and remote certificates. |
+ EXPECT_CALL(test_->session(), GetTransportStats(_)).WillRepeatedly(Invoke( |
+ [this](SessionStats* stats) { |
+ stats->transport_stats["audio"].transport_name = "audio"; |
+ stats->transport_stats["video"].transport_name = "video"; |
+ return true; |
+ })); |
+ EXPECT_CALL(test_->session(), GetLocalCertificate(_, _)).WillRepeatedly( |
+ Invoke([this, &audio_local_certinfo, &video_local_certinfo]( |
+ const std::string& transport_name, |
+ rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
+ if (transport_name == "audio") { |
+ *certificate = audio_local_certinfo->certificate; |
+ return true; |
+ } |
+ if (transport_name == "video") { |
+ *certificate = video_local_certinfo->certificate; |
+ return true; |
+ } |
+ return false; |
+ })); |
+ EXPECT_CALL(test_->session(), |
+ GetRemoteSSLCertificate_ReturnsRawPointer(_)).WillRepeatedly(Invoke( |
+ [this, &audio_remote_certinfo, &video_remote_certinfo]( |
+ const std::string& transport_name) { |
+ if (transport_name == "audio") { |
+ return audio_remote_certinfo->certificate->ssl_certificate() |
+ .GetReference(); |
+ } |
+ if (transport_name == "video") { |
+ return video_remote_certinfo->certificate->ssl_certificate() |
+ .GetReference(); |
+ } |
+ return static_cast<rtc::SSLCertificate*>(nullptr); |
+ })); |
+ |
+ rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); |
+ ExpectReportContainsCertificateInfo(report, *audio_local_certinfo.get()); |
+ ExpectReportContainsCertificateInfo(report, *audio_remote_certinfo.get()); |
+ ExpectReportContainsCertificateInfo(report, *video_local_certinfo.get()); |
+ ExpectReportContainsCertificateInfo(report, *video_remote_certinfo.get()); |
+} |
+ |
+TEST_F(RTCStatsCollectorTest, CollectRTCCertificateStatsChain) { |
+ std::vector<std::string> local_ders; |
+ local_ders.push_back("(local) this"); |
+ local_ders.push_back("(local) is"); |
+ local_ders.push_back("(local) a"); |
+ local_ders.push_back("(local) chain"); |
+ std::unique_ptr<CertificateInfo> local_certinfo = |
+ CreateFakeCertificateAndInfoFromDers(local_ders); |
+ std::vector<std::string> remote_ders; |
+ remote_ders.push_back("(remote) this"); |
+ remote_ders.push_back("(remote) is"); |
+ remote_ders.push_back("(remote) another"); |
+ remote_ders.push_back("(remote) chain"); |
+ std::unique_ptr<CertificateInfo> remote_certinfo = |
+ CreateFakeCertificateAndInfoFromDers(remote_ders); |
+ |
+ // Mock the session to return the local and remote certificates. |
+ EXPECT_CALL(test_->session(), GetTransportStats(_)).WillRepeatedly(Invoke( |
+ [this](SessionStats* stats) { |
+ stats->transport_stats["transport"].transport_name = "transport"; |
+ return true; |
+ })); |
+ EXPECT_CALL(test_->session(), GetLocalCertificate(_, _)).WillRepeatedly( |
+ Invoke([this, &local_certinfo](const std::string& transport_name, |
+ rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
+ if (transport_name == "transport") { |
+ *certificate = local_certinfo->certificate; |
+ return true; |
+ } |
+ return false; |
+ })); |
+ EXPECT_CALL(test_->session(), |
+ GetRemoteSSLCertificate_ReturnsRawPointer(_)).WillRepeatedly(Invoke( |
+ [this, &remote_certinfo](const std::string& transport_name) { |
+ if (transport_name == "transport") |
+ return remote_certinfo->certificate->ssl_certificate().GetReference(); |
+ return static_cast<rtc::SSLCertificate*>(nullptr); |
+ })); |
+ |
+ rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); |
+ ExpectReportContainsCertificateInfo(report, *local_certinfo.get()); |
+ ExpectReportContainsCertificateInfo(report, *remote_certinfo.get()); |
+} |
+ |
TEST_F(RTCStatsCollectorTest, CollectRTCPeerConnectionStats) { |
int64_t before = rtc::TimeUTCMicros(); |
rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); |