blob: 868605662988ced0a9c6c16f7297dfc0c3182560 [file] [log] [blame]
hbosd565b732016-08-30 14:04:35 -07001/*
2 * Copyright 2016 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
hbos74e1a4f2016-09-15 23:33:01 -070011#include "webrtc/api/rtcstatscollector.h"
hbosd565b732016-08-30 14:04:35 -070012
13#include <memory>
14#include <string>
15#include <vector>
16
17#include "webrtc/api/jsepsessiondescription.h"
hbos74e1a4f2016-09-15 23:33:01 -070018#include "webrtc/api/stats/rtcstats_objects.h"
19#include "webrtc/api/stats/rtcstatsreport.h"
hbosd565b732016-08-30 14:04:35 -070020#include "webrtc/api/test/mock_datachannel.h"
21#include "webrtc/api/test/mock_peerconnection.h"
22#include "webrtc/api/test/mock_webrtcsession.h"
23#include "webrtc/base/checks.h"
hbos0e6758d2016-08-31 07:57:36 -070024#include "webrtc/base/fakeclock.h"
hbos6ab97ce2016-10-03 14:16:56 -070025#include "webrtc/base/fakesslidentity.h"
hbosd565b732016-08-30 14:04:35 -070026#include "webrtc/base/gunit.h"
27#include "webrtc/base/logging.h"
hbosab9f6e42016-10-07 02:18:47 -070028#include "webrtc/base/socketaddress.h"
hbosc82f2e12016-09-05 01:36:50 -070029#include "webrtc/base/thread_checker.h"
hbos0e6758d2016-08-31 07:57:36 -070030#include "webrtc/base/timedelta.h"
31#include "webrtc/base/timeutils.h"
skvlad11a9cbf2016-10-07 11:53:05 -070032#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
hbosd565b732016-08-30 14:04:35 -070033#include "webrtc/media/base/fakemediaengine.h"
hbos2fa7c672016-10-24 04:00:05 -070034#include "webrtc/p2p/base/p2pconstants.h"
hbosab9f6e42016-10-07 02:18:47 -070035#include "webrtc/p2p/base/port.h"
hbosd565b732016-08-30 14:04:35 -070036
hbos6ab97ce2016-10-03 14:16:56 -070037using testing::_;
38using testing::Invoke;
hbosd565b732016-08-30 14:04:35 -070039using testing::Return;
40using testing::ReturnRef;
41
42namespace webrtc {
43
hbosc82f2e12016-09-05 01:36:50 -070044namespace {
45
46const int64_t kGetStatsReportTimeoutMs = 1000;
47
hbos6ab97ce2016-10-03 14:16:56 -070048struct CertificateInfo {
49 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
50 std::vector<std::string> ders;
51 std::vector<std::string> pems;
52 std::vector<std::string> fingerprints;
53};
54
55std::unique_ptr<CertificateInfo> CreateFakeCertificateAndInfoFromDers(
56 const std::vector<std::string>& ders) {
57 RTC_CHECK(!ders.empty());
58 std::unique_ptr<CertificateInfo> info(new CertificateInfo());
59 info->ders = ders;
60 for (const std::string& der : ders) {
61 info->pems.push_back(rtc::SSLIdentity::DerToPem(
62 "CERTIFICATE",
63 reinterpret_cast<const unsigned char*>(der.c_str()),
64 der.length()));
65 }
66 info->certificate =
67 rtc::RTCCertificate::Create(std::unique_ptr<rtc::FakeSSLIdentity>(
68 new rtc::FakeSSLIdentity(rtc::FakeSSLCertificate(info->pems))));
69 // Strip header/footer and newline characters of PEM strings.
70 for (size_t i = 0; i < info->pems.size(); ++i) {
71 rtc::replace_substrs("-----BEGIN CERTIFICATE-----", 27,
72 "", 0, &info->pems[i]);
73 rtc::replace_substrs("-----END CERTIFICATE-----", 25,
74 "", 0, &info->pems[i]);
75 rtc::replace_substrs("\n", 1,
76 "", 0, &info->pems[i]);
77 }
78 // Fingerprint of leaf certificate.
79 std::unique_ptr<rtc::SSLFingerprint> fp(
80 rtc::SSLFingerprint::Create("sha-1",
81 &info->certificate->ssl_certificate()));
82 EXPECT_TRUE(fp);
83 info->fingerprints.push_back(fp->GetRfc4572Fingerprint());
84 // Fingerprints of the rest of the chain.
85 std::unique_ptr<rtc::SSLCertChain> chain =
86 info->certificate->ssl_certificate().GetChain();
87 if (chain) {
88 for (size_t i = 0; i < chain->GetSize(); i++) {
89 fp.reset(rtc::SSLFingerprint::Create("sha-1", &chain->Get(i)));
90 EXPECT_TRUE(fp);
91 info->fingerprints.push_back(fp->GetRfc4572Fingerprint());
92 }
93 }
94 EXPECT_EQ(info->ders.size(), info->fingerprints.size());
95 return info;
96}
97
hbosab9f6e42016-10-07 02:18:47 -070098std::unique_ptr<cricket::Candidate> CreateFakeCandidate(
99 const std::string& hostname,
100 int port,
101 const std::string& protocol,
102 const std::string& candidate_type,
103 uint32_t priority) {
104 std::unique_ptr<cricket::Candidate> candidate(new cricket::Candidate());
105 candidate->set_address(rtc::SocketAddress(hostname, port));
106 candidate->set_protocol(protocol);
107 candidate->set_type(candidate_type);
108 candidate->set_priority(priority);
109 return candidate;
110}
111
hbosc82f2e12016-09-05 01:36:50 -0700112class RTCStatsCollectorTestHelper : public SetSessionDescriptionObserver {
hbosd565b732016-08-30 14:04:35 -0700113 public:
hbosc82f2e12016-09-05 01:36:50 -0700114 RTCStatsCollectorTestHelper()
hbosd565b732016-08-30 14:04:35 -0700115 : worker_thread_(rtc::Thread::Current()),
116 network_thread_(rtc::Thread::Current()),
skvlad11a9cbf2016-10-07 11:53:05 -0700117 channel_manager_(
118 new cricket::ChannelManager(new cricket::FakeMediaEngine(),
119 worker_thread_,
120 network_thread_)),
hbosd565b732016-08-30 14:04:35 -0700121 media_controller_(
122 MediaControllerInterface::Create(cricket::MediaConfig(),
123 worker_thread_,
skvlad11a9cbf2016-10-07 11:53:05 -0700124 channel_manager_.get(),
125 &event_log_)),
hbosd565b732016-08-30 14:04:35 -0700126 session_(media_controller_.get()),
127 pc_() {
hbos6ab97ce2016-10-03 14:16:56 -0700128 // Default return values for mocks.
hbosd565b732016-08-30 14:04:35 -0700129 EXPECT_CALL(pc_, session()).WillRepeatedly(Return(&session_));
130 EXPECT_CALL(pc_, sctp_data_channels()).WillRepeatedly(
131 ReturnRef(data_channels_));
hbos6ab97ce2016-10-03 14:16:56 -0700132 EXPECT_CALL(session_, GetTransportStats(_)).WillRepeatedly(Return(false));
hbosab9f6e42016-10-07 02:18:47 -0700133 EXPECT_CALL(session_, GetLocalCertificate(_, _)).WillRepeatedly(
134 Return(false));
135 EXPECT_CALL(session_, GetRemoteSSLCertificate_ReturnsRawPointer(_))
136 .WillRepeatedly(Return(nullptr));
hbosd565b732016-08-30 14:04:35 -0700137 }
138
hbosfdafab82016-09-14 06:02:13 -0700139 rtc::ScopedFakeClock& fake_clock() { return fake_clock_; }
hbosd565b732016-08-30 14:04:35 -0700140 MockWebRtcSession& session() { return session_; }
141 MockPeerConnection& pc() { return pc_; }
142 std::vector<rtc::scoped_refptr<DataChannel>>& data_channels() {
143 return data_channels_;
144 }
145
146 // SetSessionDescriptionObserver overrides.
147 void OnSuccess() override {}
148 void OnFailure(const std::string& error) override {
149 RTC_NOTREACHED() << error;
150 }
151
152 private:
hbosfdafab82016-09-14 06:02:13 -0700153 rtc::ScopedFakeClock fake_clock_;
skvlad11a9cbf2016-10-07 11:53:05 -0700154 webrtc::RtcEventLogNullImpl event_log_;
hbosd565b732016-08-30 14:04:35 -0700155 rtc::Thread* const worker_thread_;
156 rtc::Thread* const network_thread_;
157 std::unique_ptr<cricket::ChannelManager> channel_manager_;
158 std::unique_ptr<webrtc::MediaControllerInterface> media_controller_;
159 MockWebRtcSession session_;
160 MockPeerConnection pc_;
161
162 std::vector<rtc::scoped_refptr<DataChannel>> data_channels_;
163};
164
hbosc82f2e12016-09-05 01:36:50 -0700165class RTCTestStats : public RTCStats {
hbosd565b732016-08-30 14:04:35 -0700166 public:
hbosfc5e0502016-10-06 02:06:10 -0700167 WEBRTC_RTCSTATS_DECL();
168
hbosc82f2e12016-09-05 01:36:50 -0700169 RTCTestStats(const std::string& id, int64_t timestamp_us)
170 : RTCStats(id, timestamp_us),
171 dummy_stat("dummyStat") {}
172
hbosc82f2e12016-09-05 01:36:50 -0700173 RTCStatsMember<int32_t> dummy_stat;
174};
175
hbosfc5e0502016-10-06 02:06:10 -0700176WEBRTC_RTCSTATS_IMPL(RTCTestStats, RTCStats, "test-stats",
177 &dummy_stat);
hbosc82f2e12016-09-05 01:36:50 -0700178
179// Overrides the stats collection to verify thread usage and that the resulting
180// partial reports are merged.
181class FakeRTCStatsCollector : public RTCStatsCollector,
182 public RTCStatsCollectorCallback {
183 public:
184 static rtc::scoped_refptr<FakeRTCStatsCollector> Create(
185 PeerConnection* pc,
186 int64_t cache_lifetime_us) {
187 return rtc::scoped_refptr<FakeRTCStatsCollector>(
188 new rtc::RefCountedObject<FakeRTCStatsCollector>(
189 pc, cache_lifetime_us));
190 }
191
192 // RTCStatsCollectorCallback implementation.
193 void OnStatsDelivered(
194 const rtc::scoped_refptr<const RTCStatsReport>& report) override {
195 EXPECT_TRUE(signaling_thread_->IsCurrent());
196 rtc::CritScope cs(&lock_);
197 delivered_report_ = report;
198 }
199
200 void VerifyThreadUsageAndResultsMerging() {
201 GetStatsReport(rtc::scoped_refptr<RTCStatsCollectorCallback>(this));
202 EXPECT_TRUE_WAIT(HasVerifiedResults(), kGetStatsReportTimeoutMs);
203 }
204
205 bool HasVerifiedResults() {
206 EXPECT_TRUE(signaling_thread_->IsCurrent());
207 rtc::CritScope cs(&lock_);
208 if (!delivered_report_)
209 return false;
210 EXPECT_EQ(produced_on_signaling_thread_, 1);
211 EXPECT_EQ(produced_on_worker_thread_, 1);
212 EXPECT_EQ(produced_on_network_thread_, 1);
213
214 EXPECT_TRUE(delivered_report_->Get("SignalingThreadStats"));
215 EXPECT_TRUE(delivered_report_->Get("WorkerThreadStats"));
216 EXPECT_TRUE(delivered_report_->Get("NetworkThreadStats"));
217
218 produced_on_signaling_thread_ = 0;
219 produced_on_worker_thread_ = 0;
220 produced_on_network_thread_ = 0;
221 delivered_report_ = nullptr;
222 return true;
hbosd565b732016-08-30 14:04:35 -0700223 }
224
225 protected:
hbosc82f2e12016-09-05 01:36:50 -0700226 FakeRTCStatsCollector(
227 PeerConnection* pc,
228 int64_t cache_lifetime)
229 : RTCStatsCollector(pc, cache_lifetime),
230 signaling_thread_(pc->session()->signaling_thread()),
231 worker_thread_(pc->session()->worker_thread()),
232 network_thread_(pc->session()->network_thread()) {
233 }
234
235 void ProducePartialResultsOnSignalingThread(int64_t timestamp_us) override {
236 EXPECT_TRUE(signaling_thread_->IsCurrent());
237 {
238 rtc::CritScope cs(&lock_);
239 EXPECT_FALSE(delivered_report_);
240 ++produced_on_signaling_thread_;
241 }
242
243 rtc::scoped_refptr<RTCStatsReport> signaling_report =
244 RTCStatsReport::Create();
245 signaling_report->AddStats(std::unique_ptr<const RTCStats>(
246 new RTCTestStats("SignalingThreadStats", timestamp_us)));
247 AddPartialResults(signaling_report);
248 }
249 void ProducePartialResultsOnWorkerThread(int64_t timestamp_us) override {
250 EXPECT_TRUE(worker_thread_->IsCurrent());
251 {
252 rtc::CritScope cs(&lock_);
253 EXPECT_FALSE(delivered_report_);
254 ++produced_on_worker_thread_;
255 }
256
257 rtc::scoped_refptr<RTCStatsReport> worker_report = RTCStatsReport::Create();
258 worker_report->AddStats(std::unique_ptr<const RTCStats>(
259 new RTCTestStats("WorkerThreadStats", timestamp_us)));
260 AddPartialResults(worker_report);
261 }
262 void ProducePartialResultsOnNetworkThread(int64_t timestamp_us) override {
263 EXPECT_TRUE(network_thread_->IsCurrent());
264 {
265 rtc::CritScope cs(&lock_);
266 EXPECT_FALSE(delivered_report_);
267 ++produced_on_network_thread_;
268 }
269
270 rtc::scoped_refptr<RTCStatsReport> network_report =
271 RTCStatsReport::Create();
272 network_report->AddStats(std::unique_ptr<const RTCStats>(
273 new RTCTestStats("NetworkThreadStats", timestamp_us)));
274 AddPartialResults(network_report);
275 }
276
277 private:
278 rtc::Thread* const signaling_thread_;
279 rtc::Thread* const worker_thread_;
280 rtc::Thread* const network_thread_;
281
282 rtc::CriticalSection lock_;
283 rtc::scoped_refptr<const RTCStatsReport> delivered_report_;
284 int produced_on_signaling_thread_ = 0;
285 int produced_on_worker_thread_ = 0;
286 int produced_on_network_thread_ = 0;
hbosd565b732016-08-30 14:04:35 -0700287};
288
hbosc82f2e12016-09-05 01:36:50 -0700289class StatsCallback : public RTCStatsCollectorCallback {
290 public:
291 static rtc::scoped_refptr<StatsCallback> Create(
292 rtc::scoped_refptr<const RTCStatsReport>* report_ptr = nullptr) {
293 return rtc::scoped_refptr<StatsCallback>(
294 new rtc::RefCountedObject<StatsCallback>(report_ptr));
295 }
296
297 void OnStatsDelivered(
298 const rtc::scoped_refptr<const RTCStatsReport>& report) override {
299 EXPECT_TRUE(thread_checker_.CalledOnValidThread());
300 report_ = report;
301 if (report_ptr_)
302 *report_ptr_ = report_;
303 }
304
305 rtc::scoped_refptr<const RTCStatsReport> report() const {
306 EXPECT_TRUE(thread_checker_.CalledOnValidThread());
307 return report_;
308 }
309
310 protected:
311 explicit StatsCallback(rtc::scoped_refptr<const RTCStatsReport>* report_ptr)
312 : report_ptr_(report_ptr) {}
313
314 private:
315 rtc::ThreadChecker thread_checker_;
316 rtc::scoped_refptr<const RTCStatsReport> report_;
317 rtc::scoped_refptr<const RTCStatsReport>* report_ptr_;
318};
319
320class RTCStatsCollectorTest : public testing::Test {
321 public:
322 RTCStatsCollectorTest()
323 : test_(new rtc::RefCountedObject<RTCStatsCollectorTestHelper>()),
324 collector_(RTCStatsCollector::Create(
325 &test_->pc(), 50 * rtc::kNumMicrosecsPerMillisec)) {
326 }
327
328 rtc::scoped_refptr<const RTCStatsReport> GetStatsReport() {
329 rtc::scoped_refptr<StatsCallback> callback = StatsCallback::Create();
330 collector_->GetStatsReport(callback);
331 EXPECT_TRUE_WAIT(callback->report(), kGetStatsReportTimeoutMs);
hboscc555c52016-10-18 12:48:31 -0700332 int64_t after = rtc::TimeUTCMicros();
333 for (const RTCStats& stats : *callback->report()) {
334 EXPECT_LE(stats.timestamp_us(), after);
335 }
hbosc82f2e12016-09-05 01:36:50 -0700336 return callback->report();
337 }
338
hbosc47a0c32016-10-11 14:54:49 -0700339 const RTCIceCandidateStats* ExpectReportContainsCandidate(
hbosab9f6e42016-10-07 02:18:47 -0700340 const rtc::scoped_refptr<const RTCStatsReport>& report,
341 const cricket::Candidate& candidate,
342 bool is_local) {
hbosc47a0c32016-10-11 14:54:49 -0700343 const RTCStats* stats = report->Get("RTCIceCandidate_" + candidate.id());
hbosab9f6e42016-10-07 02:18:47 -0700344 EXPECT_TRUE(stats);
345 const RTCIceCandidateStats* candidate_stats;
346 if (is_local)
347 candidate_stats = &stats->cast_to<RTCLocalIceCandidateStats>();
348 else
349 candidate_stats = &stats->cast_to<RTCRemoteIceCandidateStats>();
350 EXPECT_EQ(*candidate_stats->ip, candidate.address().ipaddr().ToString());
351 EXPECT_EQ(*candidate_stats->port,
352 static_cast<int32_t>(candidate.address().port()));
353 EXPECT_EQ(*candidate_stats->protocol, candidate.protocol());
354 EXPECT_EQ(*candidate_stats->candidate_type,
hboscc555c52016-10-18 12:48:31 -0700355 CandidateTypeToRTCIceCandidateTypeForTesting(candidate.type()));
hbosab9f6e42016-10-07 02:18:47 -0700356 EXPECT_EQ(*candidate_stats->priority,
357 static_cast<int32_t>(candidate.priority()));
358 // TODO(hbos): Define candidate_stats->url. crbug.com/632723
359 EXPECT_FALSE(candidate_stats->url.is_defined());
hbosc47a0c32016-10-11 14:54:49 -0700360 return candidate_stats;
361 }
362
363 void ExpectReportContainsCandidatePair(
364 const rtc::scoped_refptr<const RTCStatsReport>& report,
365 const cricket::TransportStats& transport_stats) {
366 for (const auto& channel_stats : transport_stats.channel_stats) {
367 for (const cricket::ConnectionInfo& info :
368 channel_stats.connection_infos) {
369 const std::string& id = "RTCIceCandidatePair_" +
370 info.local_candidate.id() + "_" + info.remote_candidate.id();
371 const RTCStats* stats = report->Get(id);
hbos2fa7c672016-10-24 04:00:05 -0700372 ASSERT_TRUE(stats);
hbosc47a0c32016-10-11 14:54:49 -0700373 const RTCIceCandidatePairStats& candidate_pair_stats =
374 stats->cast_to<RTCIceCandidatePairStats>();
375
376 // TODO(hbos): Define all the undefined |candidate_pair_stats| stats.
377 // The EXPECT_FALSE are for the undefined stats, see also todos listed
hbos5d79a7c2016-10-24 09:27:10 -0700378 // in rtcstats_objects.h. crbug.com/633550
hbosc47a0c32016-10-11 14:54:49 -0700379 EXPECT_FALSE(candidate_pair_stats.transport_id.is_defined());
380 const RTCIceCandidateStats* local_candidate =
381 ExpectReportContainsCandidate(report, info.local_candidate, true);
382 EXPECT_EQ(*candidate_pair_stats.local_candidate_id,
383 local_candidate->id());
384 const RTCIceCandidateStats* remote_candidate =
385 ExpectReportContainsCandidate(report, info.remote_candidate, false);
386 EXPECT_EQ(*candidate_pair_stats.remote_candidate_id,
387 remote_candidate->id());
388
389 EXPECT_FALSE(candidate_pair_stats.state.is_defined());
390 EXPECT_FALSE(candidate_pair_stats.priority.is_defined());
391 EXPECT_FALSE(candidate_pair_stats.nominated.is_defined());
392 EXPECT_EQ(*candidate_pair_stats.writable, info.writable);
393 EXPECT_FALSE(candidate_pair_stats.readable.is_defined());
394 EXPECT_EQ(*candidate_pair_stats.bytes_sent,
395 static_cast<uint64_t>(info.sent_total_bytes));
396 EXPECT_EQ(*candidate_pair_stats.bytes_received,
397 static_cast<uint64_t>(info.recv_total_bytes));
398 EXPECT_FALSE(candidate_pair_stats.total_rtt.is_defined());
399 EXPECT_EQ(*candidate_pair_stats.current_rtt,
400 static_cast<double>(info.rtt) / 1000.0);
401 EXPECT_FALSE(
402 candidate_pair_stats.available_outgoing_bitrate.is_defined());
403 EXPECT_FALSE(
404 candidate_pair_stats.available_incoming_bitrate.is_defined());
405 EXPECT_FALSE(candidate_pair_stats.requests_received.is_defined());
406 EXPECT_EQ(*candidate_pair_stats.requests_sent,
407 static_cast<uint64_t>(info.sent_ping_requests_total));
408 EXPECT_EQ(*candidate_pair_stats.responses_received,
409 static_cast<uint64_t>(info.recv_ping_responses));
410 EXPECT_EQ(*candidate_pair_stats.responses_sent,
411 static_cast<uint64_t>(info.sent_ping_responses));
412 EXPECT_FALSE(
413 candidate_pair_stats.retransmissions_received.is_defined());
414 EXPECT_FALSE(candidate_pair_stats.retransmissions_sent.is_defined());
415 EXPECT_FALSE(
416 candidate_pair_stats.consent_requests_received.is_defined());
417 EXPECT_FALSE(candidate_pair_stats.consent_requests_sent.is_defined());
418 EXPECT_FALSE(
419 candidate_pair_stats.consent_responses_received.is_defined());
420 EXPECT_FALSE(candidate_pair_stats.consent_responses_sent.is_defined());
421 }
422 }
hbosab9f6e42016-10-07 02:18:47 -0700423 }
424
hbos6ab97ce2016-10-03 14:16:56 -0700425 void ExpectReportContainsCertificateInfo(
426 const rtc::scoped_refptr<const RTCStatsReport>& report,
427 const CertificateInfo& cert_info) {
428 for (size_t i = 0; i < cert_info.fingerprints.size(); ++i) {
429 const RTCStats* stats = report->Get(
430 "RTCCertificate_" + cert_info.fingerprints[i]);
hbos2fa7c672016-10-24 04:00:05 -0700431 ASSERT_TRUE(stats);
hbos6ab97ce2016-10-03 14:16:56 -0700432 const RTCCertificateStats& cert_stats =
433 stats->cast_to<const RTCCertificateStats>();
434 EXPECT_EQ(*cert_stats.fingerprint, cert_info.fingerprints[i]);
435 EXPECT_EQ(*cert_stats.fingerprint_algorithm, "sha-1");
436 EXPECT_EQ(*cert_stats.base64_certificate, cert_info.pems[i]);
437 if (i + 1 < cert_info.fingerprints.size()) {
438 EXPECT_EQ(*cert_stats.issuer_certificate_id,
439 "RTCCertificate_" + cert_info.fingerprints[i + 1]);
440 } else {
441 EXPECT_FALSE(cert_stats.issuer_certificate_id.is_defined());
442 }
443 }
444 }
445
hbos2fa7c672016-10-24 04:00:05 -0700446 void ExpectReportContainsTransportStats(
447 const rtc::scoped_refptr<const RTCStatsReport>& report,
448 const cricket::TransportStats& transport,
449 const CertificateInfo* local_certinfo,
450 const CertificateInfo* remote_certinfo) {
451 std::string rtcp_transport_stats_id;
452 for (const auto& channel_stats : transport.channel_stats) {
453 if (channel_stats.component == cricket::ICE_CANDIDATE_COMPONENT_RTCP) {
454 rtcp_transport_stats_id = "RTCTransport_" + transport.transport_name +
455 "_" + rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTCP);
456 }
457 }
458 for (const auto& channel_stats : transport.channel_stats) {
459 const cricket::ConnectionInfo* best_connection_info = nullptr;
460 const RTCStats* stats = report->Get(
461 "RTCTransport_" + transport.transport_name + "_" +
462 rtc::ToString<>(channel_stats.component));
463 ASSERT_TRUE(stats);
464 const RTCTransportStats& transport_stats =
465 stats->cast_to<const RTCTransportStats>();
466 uint64_t bytes_sent = 0;
467 uint64_t bytes_received = 0;
468 for (const cricket::ConnectionInfo& info :
469 channel_stats.connection_infos) {
470 bytes_sent += info.sent_total_bytes;
471 bytes_received += info.recv_total_bytes;
472 if (info.best_connection)
473 best_connection_info = &info;
474 }
475 EXPECT_EQ(*transport_stats.bytes_sent, bytes_sent);
476 EXPECT_EQ(*transport_stats.bytes_received, bytes_received);
477 if (best_connection_info) {
478 EXPECT_EQ(*transport_stats.active_connection, true);
479 // TODO(hbos): Instead of testing how the ID looks, test that the
480 // corresponding pair's IP addresses are equal to the IP addresses of
481 // the |best_connection_info| data. crbug.com/653873
482 EXPECT_EQ(*transport_stats.selected_candidate_pair_id,
483 "RTCIceCandidatePair_" +
484 best_connection_info->local_candidate.id() + "_" +
485 best_connection_info->remote_candidate.id());
486 EXPECT_TRUE(report->Get(*transport_stats.selected_candidate_pair_id));
487 } else {
488 EXPECT_EQ(*transport_stats.active_connection, false);
489 EXPECT_FALSE(transport_stats.selected_candidate_pair_id.is_defined());
490 }
491 if (channel_stats.component != cricket::ICE_CANDIDATE_COMPONENT_RTCP &&
492 !rtcp_transport_stats_id.empty()) {
493 EXPECT_EQ(*transport_stats.rtcp_transport_stats_id,
494 rtcp_transport_stats_id);
495 } else {
496 EXPECT_FALSE(transport_stats.rtcp_transport_stats_id.is_defined());
497 }
498 if (local_certinfo && remote_certinfo) {
499 EXPECT_EQ(*transport_stats.local_certificate_id,
500 "RTCCertificate_" + local_certinfo->fingerprints[0]);
501 EXPECT_EQ(*transport_stats.remote_certificate_id,
502 "RTCCertificate_" + remote_certinfo->fingerprints[0]);
503 EXPECT_TRUE(report->Get(*transport_stats.local_certificate_id));
504 EXPECT_TRUE(report->Get(*transport_stats.remote_certificate_id));
505 } else {
506 EXPECT_FALSE(transport_stats.local_certificate_id.is_defined());
507 EXPECT_FALSE(transport_stats.remote_certificate_id.is_defined());
508 }
509 }
510 }
511
hboscc555c52016-10-18 12:48:31 -0700512 void ExpectReportContainsDataChannel(
513 const rtc::scoped_refptr<const RTCStatsReport>& report,
514 const DataChannel& data_channel) {
515 const RTCStats* stats = report->Get("RTCDataChannel_" +
516 rtc::ToString<>(data_channel.id()));
517 EXPECT_TRUE(stats);
518 const RTCDataChannelStats& data_channel_stats =
519 stats->cast_to<const RTCDataChannelStats>();
520 EXPECT_EQ(*data_channel_stats.label, data_channel.label());
521 EXPECT_EQ(*data_channel_stats.protocol, data_channel.protocol());
522 EXPECT_EQ(*data_channel_stats.datachannelid, data_channel.id());
523 EXPECT_EQ(*data_channel_stats.state,
524 DataStateToRTCDataChannelStateForTesting(data_channel.state()));
525 EXPECT_EQ(*data_channel_stats.messages_sent, data_channel.messages_sent());
526 EXPECT_EQ(*data_channel_stats.bytes_sent, data_channel.bytes_sent());
527 EXPECT_EQ(*data_channel_stats.messages_received,
528 data_channel.messages_received());
529 EXPECT_EQ(*data_channel_stats.bytes_received,
530 data_channel.bytes_received());
531 }
532
hbosc82f2e12016-09-05 01:36:50 -0700533 protected:
534 rtc::scoped_refptr<RTCStatsCollectorTestHelper> test_;
535 rtc::scoped_refptr<RTCStatsCollector> collector_;
536};
537
538TEST_F(RTCStatsCollectorTest, SingleCallback) {
539 rtc::scoped_refptr<const RTCStatsReport> result;
540 collector_->GetStatsReport(StatsCallback::Create(&result));
541 EXPECT_TRUE_WAIT(result, kGetStatsReportTimeoutMs);
542}
543
544TEST_F(RTCStatsCollectorTest, MultipleCallbacks) {
545 rtc::scoped_refptr<const RTCStatsReport> a;
546 rtc::scoped_refptr<const RTCStatsReport> b;
547 rtc::scoped_refptr<const RTCStatsReport> c;
548 collector_->GetStatsReport(StatsCallback::Create(&a));
549 collector_->GetStatsReport(StatsCallback::Create(&b));
550 collector_->GetStatsReport(StatsCallback::Create(&c));
551 EXPECT_TRUE_WAIT(a, kGetStatsReportTimeoutMs);
552 EXPECT_TRUE_WAIT(b, kGetStatsReportTimeoutMs);
553 EXPECT_TRUE_WAIT(c, kGetStatsReportTimeoutMs);
554 EXPECT_EQ(a.get(), b.get());
555 EXPECT_EQ(b.get(), c.get());
556}
557
558TEST_F(RTCStatsCollectorTest, CachedStatsReports) {
hbosd565b732016-08-30 14:04:35 -0700559 // Caching should ensure |a| and |b| are the same report.
hbosc82f2e12016-09-05 01:36:50 -0700560 rtc::scoped_refptr<const RTCStatsReport> a = GetStatsReport();
561 rtc::scoped_refptr<const RTCStatsReport> b = GetStatsReport();
hbosd565b732016-08-30 14:04:35 -0700562 EXPECT_EQ(a.get(), b.get());
563 // Invalidate cache by clearing it.
hbosc82f2e12016-09-05 01:36:50 -0700564 collector_->ClearCachedStatsReport();
565 rtc::scoped_refptr<const RTCStatsReport> c = GetStatsReport();
hbosd565b732016-08-30 14:04:35 -0700566 EXPECT_NE(b.get(), c.get());
567 // Invalidate cache by advancing time.
hbosfdafab82016-09-14 06:02:13 -0700568 test_->fake_clock().AdvanceTime(rtc::TimeDelta::FromMilliseconds(51));
hbosc82f2e12016-09-05 01:36:50 -0700569 rtc::scoped_refptr<const RTCStatsReport> d = GetStatsReport();
hbosd565b732016-08-30 14:04:35 -0700570 EXPECT_TRUE(d);
571 EXPECT_NE(c.get(), d.get());
572}
573
hbosc82f2e12016-09-05 01:36:50 -0700574TEST_F(RTCStatsCollectorTest, MultipleCallbacksWithInvalidatedCacheInBetween) {
hbosc82f2e12016-09-05 01:36:50 -0700575 rtc::scoped_refptr<const RTCStatsReport> a;
576 rtc::scoped_refptr<const RTCStatsReport> b;
577 rtc::scoped_refptr<const RTCStatsReport> c;
578 collector_->GetStatsReport(StatsCallback::Create(&a));
579 collector_->GetStatsReport(StatsCallback::Create(&b));
580 // Cache is invalidated after 50 ms.
hbosfdafab82016-09-14 06:02:13 -0700581 test_->fake_clock().AdvanceTime(rtc::TimeDelta::FromMilliseconds(51));
hbosc82f2e12016-09-05 01:36:50 -0700582 collector_->GetStatsReport(StatsCallback::Create(&c));
583 EXPECT_TRUE_WAIT(a, kGetStatsReportTimeoutMs);
584 EXPECT_TRUE_WAIT(b, kGetStatsReportTimeoutMs);
585 EXPECT_TRUE_WAIT(c, kGetStatsReportTimeoutMs);
586 EXPECT_EQ(a.get(), b.get());
587 // The act of doing |AdvanceTime| processes all messages. If this was not the
588 // case we might not require |c| to be fresher than |b|.
589 EXPECT_NE(c.get(), b.get());
590}
591
hbos6ab97ce2016-10-03 14:16:56 -0700592TEST_F(RTCStatsCollectorTest, CollectRTCCertificateStatsSingle) {
593 std::unique_ptr<CertificateInfo> local_certinfo =
594 CreateFakeCertificateAndInfoFromDers(
595 std::vector<std::string>({ "(local) single certificate" }));
596 std::unique_ptr<CertificateInfo> remote_certinfo =
597 CreateFakeCertificateAndInfoFromDers(
598 std::vector<std::string>({ "(remote) single certificate" }));
599
600 // Mock the session to return the local and remote certificates.
601 EXPECT_CALL(test_->session(), GetTransportStats(_)).WillRepeatedly(Invoke(
602 [this](SessionStats* stats) {
603 stats->transport_stats["transport"].transport_name = "transport";
604 return true;
605 }));
606 EXPECT_CALL(test_->session(), GetLocalCertificate(_, _)).WillRepeatedly(
607 Invoke([this, &local_certinfo](const std::string& transport_name,
608 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
609 if (transport_name == "transport") {
610 *certificate = local_certinfo->certificate;
611 return true;
612 }
613 return false;
614 }));
615 EXPECT_CALL(test_->session(),
616 GetRemoteSSLCertificate_ReturnsRawPointer(_)).WillRepeatedly(Invoke(
617 [this, &remote_certinfo](const std::string& transport_name) {
618 if (transport_name == "transport")
619 return remote_certinfo->certificate->ssl_certificate().GetReference();
620 return static_cast<rtc::SSLCertificate*>(nullptr);
621 }));
622
623 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
624 ExpectReportContainsCertificateInfo(report, *local_certinfo.get());
625 ExpectReportContainsCertificateInfo(report, *remote_certinfo.get());
626}
627
628TEST_F(RTCStatsCollectorTest, CollectRTCCertificateStatsMultiple) {
629 std::unique_ptr<CertificateInfo> audio_local_certinfo =
630 CreateFakeCertificateAndInfoFromDers(
631 std::vector<std::string>({ "(local) audio" }));
632 audio_local_certinfo = CreateFakeCertificateAndInfoFromDers(
633 audio_local_certinfo->ders);
634 std::unique_ptr<CertificateInfo> audio_remote_certinfo =
635 CreateFakeCertificateAndInfoFromDers(
636 std::vector<std::string>({ "(remote) audio" }));
637 audio_remote_certinfo = CreateFakeCertificateAndInfoFromDers(
638 audio_remote_certinfo->ders);
639
640 std::unique_ptr<CertificateInfo> video_local_certinfo =
641 CreateFakeCertificateAndInfoFromDers(
642 std::vector<std::string>({ "(local) video" }));
643 video_local_certinfo = CreateFakeCertificateAndInfoFromDers(
644 video_local_certinfo->ders);
645 std::unique_ptr<CertificateInfo> video_remote_certinfo =
646 CreateFakeCertificateAndInfoFromDers(
647 std::vector<std::string>({ "(remote) video" }));
648 video_remote_certinfo = CreateFakeCertificateAndInfoFromDers(
649 video_remote_certinfo->ders);
650
651 // Mock the session to return the local and remote certificates.
652 EXPECT_CALL(test_->session(), GetTransportStats(_)).WillRepeatedly(Invoke(
653 [this](SessionStats* stats) {
654 stats->transport_stats["audio"].transport_name = "audio";
655 stats->transport_stats["video"].transport_name = "video";
656 return true;
657 }));
658 EXPECT_CALL(test_->session(), GetLocalCertificate(_, _)).WillRepeatedly(
659 Invoke([this, &audio_local_certinfo, &video_local_certinfo](
660 const std::string& transport_name,
661 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
662 if (transport_name == "audio") {
663 *certificate = audio_local_certinfo->certificate;
664 return true;
665 }
666 if (transport_name == "video") {
667 *certificate = video_local_certinfo->certificate;
668 return true;
669 }
670 return false;
671 }));
672 EXPECT_CALL(test_->session(),
673 GetRemoteSSLCertificate_ReturnsRawPointer(_)).WillRepeatedly(Invoke(
674 [this, &audio_remote_certinfo, &video_remote_certinfo](
675 const std::string& transport_name) {
676 if (transport_name == "audio") {
677 return audio_remote_certinfo->certificate->ssl_certificate()
678 .GetReference();
679 }
680 if (transport_name == "video") {
681 return video_remote_certinfo->certificate->ssl_certificate()
682 .GetReference();
683 }
684 return static_cast<rtc::SSLCertificate*>(nullptr);
685 }));
686
687 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
688 ExpectReportContainsCertificateInfo(report, *audio_local_certinfo.get());
689 ExpectReportContainsCertificateInfo(report, *audio_remote_certinfo.get());
690 ExpectReportContainsCertificateInfo(report, *video_local_certinfo.get());
691 ExpectReportContainsCertificateInfo(report, *video_remote_certinfo.get());
692}
693
694TEST_F(RTCStatsCollectorTest, CollectRTCCertificateStatsChain) {
695 std::vector<std::string> local_ders;
696 local_ders.push_back("(local) this");
697 local_ders.push_back("(local) is");
698 local_ders.push_back("(local) a");
699 local_ders.push_back("(local) chain");
700 std::unique_ptr<CertificateInfo> local_certinfo =
701 CreateFakeCertificateAndInfoFromDers(local_ders);
702 std::vector<std::string> remote_ders;
703 remote_ders.push_back("(remote) this");
704 remote_ders.push_back("(remote) is");
705 remote_ders.push_back("(remote) another");
706 remote_ders.push_back("(remote) chain");
707 std::unique_ptr<CertificateInfo> remote_certinfo =
708 CreateFakeCertificateAndInfoFromDers(remote_ders);
709
710 // Mock the session to return the local and remote certificates.
711 EXPECT_CALL(test_->session(), GetTransportStats(_)).WillRepeatedly(Invoke(
712 [this](SessionStats* stats) {
713 stats->transport_stats["transport"].transport_name = "transport";
714 return true;
715 }));
716 EXPECT_CALL(test_->session(), GetLocalCertificate(_, _)).WillRepeatedly(
717 Invoke([this, &local_certinfo](const std::string& transport_name,
718 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
719 if (transport_name == "transport") {
720 *certificate = local_certinfo->certificate;
721 return true;
722 }
723 return false;
724 }));
725 EXPECT_CALL(test_->session(),
726 GetRemoteSSLCertificate_ReturnsRawPointer(_)).WillRepeatedly(Invoke(
727 [this, &remote_certinfo](const std::string& transport_name) {
728 if (transport_name == "transport")
729 return remote_certinfo->certificate->ssl_certificate().GetReference();
730 return static_cast<rtc::SSLCertificate*>(nullptr);
731 }));
732
733 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
734 ExpectReportContainsCertificateInfo(report, *local_certinfo.get());
735 ExpectReportContainsCertificateInfo(report, *remote_certinfo.get());
736}
737
hboscc555c52016-10-18 12:48:31 -0700738TEST_F(RTCStatsCollectorTest, CollectRTCDataChannelStats) {
739 test_->data_channels().push_back(
740 new MockDataChannel(0, DataChannelInterface::kConnecting));
741 test_->data_channels().push_back(
742 new MockDataChannel(1, DataChannelInterface::kOpen));
743 test_->data_channels().push_back(
744 new MockDataChannel(2, DataChannelInterface::kClosing));
745 test_->data_channels().push_back(
746 new MockDataChannel(3, DataChannelInterface::kClosed));
747
748 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
749 ExpectReportContainsDataChannel(report, *test_->data_channels()[0]);
750 ExpectReportContainsDataChannel(report, *test_->data_channels()[1]);
751 ExpectReportContainsDataChannel(report, *test_->data_channels()[2]);
752 ExpectReportContainsDataChannel(report, *test_->data_channels()[3]);
753
754 test_->data_channels().clear();
755 test_->data_channels().push_back(
756 new MockDataChannel(0, DataChannelInterface::kConnecting,
757 1, 2, 3, 4));
758 test_->data_channels().push_back(
759 new MockDataChannel(1, DataChannelInterface::kOpen,
760 5, 6, 7, 8));
761 test_->data_channels().push_back(
762 new MockDataChannel(2, DataChannelInterface::kClosing,
763 9, 10, 11, 12));
764 test_->data_channels().push_back(
765 new MockDataChannel(3, DataChannelInterface::kClosed,
766 13, 14, 15, 16));
767
768 collector_->ClearCachedStatsReport();
769 report = GetStatsReport();
770 ExpectReportContainsDataChannel(report, *test_->data_channels()[0]);
771 ExpectReportContainsDataChannel(report, *test_->data_channels()[1]);
772 ExpectReportContainsDataChannel(report, *test_->data_channels()[2]);
773 ExpectReportContainsDataChannel(report, *test_->data_channels()[3]);
774}
775
hbosab9f6e42016-10-07 02:18:47 -0700776TEST_F(RTCStatsCollectorTest, CollectRTCIceCandidateStats) {
777 // Candidates in the first transport stats.
778 std::unique_ptr<cricket::Candidate> a_local_host = CreateFakeCandidate(
779 "1.2.3.4", 5,
780 "a_local_host's protocol",
781 cricket::LOCAL_PORT_TYPE,
782 0);
783 std::unique_ptr<cricket::Candidate> a_remote_srflx = CreateFakeCandidate(
784 "6.7.8.9", 10,
785 "remote_srflx's protocol",
786 cricket::STUN_PORT_TYPE,
787 1);
788 std::unique_ptr<cricket::Candidate> a_local_prflx = CreateFakeCandidate(
789 "11.12.13.14", 15,
790 "a_local_prflx's protocol",
791 cricket::PRFLX_PORT_TYPE,
792 2);
793 std::unique_ptr<cricket::Candidate> a_remote_relay = CreateFakeCandidate(
794 "16.17.18.19", 20,
795 "a_remote_relay's protocol",
796 cricket::RELAY_PORT_TYPE,
797 3);
798 // Candidates in the second transport stats.
799 std::unique_ptr<cricket::Candidate> b_local = CreateFakeCandidate(
800 "42.42.42.42", 42,
801 "b_local's protocol",
802 cricket::LOCAL_PORT_TYPE,
803 42);
804 std::unique_ptr<cricket::Candidate> b_remote = CreateFakeCandidate(
805 "42.42.42.42", 42,
806 "b_remote's protocol",
807 cricket::LOCAL_PORT_TYPE,
808 42);
809
810 SessionStats session_stats;
811
812 cricket::TransportChannelStats a_transport_channel_stats;
813 a_transport_channel_stats.connection_infos.push_back(
814 cricket::ConnectionInfo());
815 a_transport_channel_stats.connection_infos[0].local_candidate =
816 *a_local_host.get();
817 a_transport_channel_stats.connection_infos[0].remote_candidate =
818 *a_remote_srflx.get();
819 a_transport_channel_stats.connection_infos.push_back(
820 cricket::ConnectionInfo());
821 a_transport_channel_stats.connection_infos[1].local_candidate =
822 *a_local_prflx.get();
823 a_transport_channel_stats.connection_infos[1].remote_candidate =
824 *a_remote_relay.get();
825 session_stats.transport_stats["a"].channel_stats.push_back(
826 a_transport_channel_stats);
827
828 cricket::TransportChannelStats b_transport_channel_stats;
829 b_transport_channel_stats.connection_infos.push_back(
830 cricket::ConnectionInfo());
831 b_transport_channel_stats.connection_infos[0].local_candidate =
832 *b_local.get();
833 b_transport_channel_stats.connection_infos[0].remote_candidate =
834 *b_remote.get();
835 session_stats.transport_stats["b"].channel_stats.push_back(
836 b_transport_channel_stats);
837
838 // Mock the session to return the desired candidates.
839 EXPECT_CALL(test_->session(), GetTransportStats(_)).WillRepeatedly(Invoke(
840 [this, &session_stats](SessionStats* stats) {
841 *stats = session_stats;
842 return true;
843 }));
844
845 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
846 ExpectReportContainsCandidate(report, *a_local_host.get(), true);
847 ExpectReportContainsCandidate(report, *a_remote_srflx.get(), false);
848 ExpectReportContainsCandidate(report, *a_local_prflx.get(), true);
849 ExpectReportContainsCandidate(report, *a_remote_relay.get(), false);
850 ExpectReportContainsCandidate(report, *b_local.get(), true);
851 ExpectReportContainsCandidate(report, *b_remote.get(), false);
852}
853
hbosc47a0c32016-10-11 14:54:49 -0700854TEST_F(RTCStatsCollectorTest, CollectRTCIceCandidatePairStats) {
855 std::unique_ptr<cricket::Candidate> local_candidate = CreateFakeCandidate(
856 "42.42.42.42", 42, "protocol", cricket::LOCAL_PORT_TYPE, 42);
857 std::unique_ptr<cricket::Candidate> remote_candidate = CreateFakeCandidate(
858 "42.42.42.42", 42, "protocol", cricket::LOCAL_PORT_TYPE, 42);
859
860 SessionStats session_stats;
861
862 cricket::ConnectionInfo connection_info;
863 connection_info.local_candidate = *local_candidate.get();
864 connection_info.remote_candidate = *remote_candidate.get();
865 connection_info.writable = true;
866 connection_info.sent_total_bytes = 42;
867 connection_info.recv_total_bytes = 1234;
868 connection_info.rtt = 1337;
869 connection_info.sent_ping_requests_total = 1010;
870 connection_info.recv_ping_responses = 4321;
871 connection_info.sent_ping_responses = 1000;
872
873 cricket::TransportChannelStats transport_channel_stats;
874 transport_channel_stats.connection_infos.push_back(connection_info);
875 session_stats.transport_stats["transport"].transport_name = "transport";
876 session_stats.transport_stats["transport"].channel_stats.push_back(
877 transport_channel_stats);
878
879 // Mock the session to return the desired candidates.
880 EXPECT_CALL(test_->session(), GetTransportStats(_)).WillRepeatedly(Invoke(
881 [this, &session_stats](SessionStats* stats) {
882 *stats = session_stats;
883 return true;
884 }));
885
886 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
887 ExpectReportContainsCandidatePair(
888 report, session_stats.transport_stats["transport"]);
889}
890
hbosd565b732016-08-30 14:04:35 -0700891TEST_F(RTCStatsCollectorTest, CollectRTCPeerConnectionStats) {
hbosc82f2e12016-09-05 01:36:50 -0700892 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
hbosd565b732016-08-30 14:04:35 -0700893 EXPECT_EQ(report->GetStatsOfType<RTCPeerConnectionStats>().size(),
894 static_cast<size_t>(1)) << "Expecting 1 RTCPeerConnectionStats.";
895 const RTCStats* stats = report->Get("RTCPeerConnection");
896 EXPECT_TRUE(stats);
hbosd565b732016-08-30 14:04:35 -0700897 {
898 // Expected stats with no data channels
899 const RTCPeerConnectionStats& pcstats =
900 stats->cast_to<RTCPeerConnectionStats>();
901 EXPECT_EQ(*pcstats.data_channels_opened, static_cast<uint32_t>(0));
902 EXPECT_EQ(*pcstats.data_channels_closed, static_cast<uint32_t>(0));
903 }
904
905 test_->data_channels().push_back(
hboscc555c52016-10-18 12:48:31 -0700906 new MockDataChannel(0, DataChannelInterface::kConnecting));
hbosd565b732016-08-30 14:04:35 -0700907 test_->data_channels().push_back(
hboscc555c52016-10-18 12:48:31 -0700908 new MockDataChannel(1, DataChannelInterface::kOpen));
hbosd565b732016-08-30 14:04:35 -0700909 test_->data_channels().push_back(
hboscc555c52016-10-18 12:48:31 -0700910 new MockDataChannel(2, DataChannelInterface::kClosing));
hbosd565b732016-08-30 14:04:35 -0700911 test_->data_channels().push_back(
hboscc555c52016-10-18 12:48:31 -0700912 new MockDataChannel(3, DataChannelInterface::kClosed));
hbosd565b732016-08-30 14:04:35 -0700913
hbosc82f2e12016-09-05 01:36:50 -0700914 collector_->ClearCachedStatsReport();
915 report = GetStatsReport();
hbosd565b732016-08-30 14:04:35 -0700916 EXPECT_EQ(report->GetStatsOfType<RTCPeerConnectionStats>().size(),
917 static_cast<size_t>(1)) << "Expecting 1 RTCPeerConnectionStats.";
918 stats = report->Get("RTCPeerConnection");
hbos2fa7c672016-10-24 04:00:05 -0700919 ASSERT_TRUE(stats);
hbosd565b732016-08-30 14:04:35 -0700920 {
921 // Expected stats with the above four data channels
922 // TODO(hbos): When the |RTCPeerConnectionStats| is the number of data
923 // channels that have been opened and closed, not the numbers currently
924 // open/closed, we would expect opened >= closed and (opened - closed) to be
925 // the number currently open. crbug.com/636818.
926 const RTCPeerConnectionStats& pcstats =
927 stats->cast_to<RTCPeerConnectionStats>();
928 EXPECT_EQ(*pcstats.data_channels_opened, static_cast<uint32_t>(1));
929 EXPECT_EQ(*pcstats.data_channels_closed, static_cast<uint32_t>(3));
930 }
931}
932
hbos2fa7c672016-10-24 04:00:05 -0700933TEST_F(RTCStatsCollectorTest, CollectRTCTransportStats) {
934 std::unique_ptr<cricket::Candidate> rtp_local_candidate = CreateFakeCandidate(
935 "42.42.42.42", 42, "protocol", cricket::LOCAL_PORT_TYPE, 42);
936 std::unique_ptr<cricket::Candidate> rtp_remote_candidate =
937 CreateFakeCandidate("42.42.42.42", 42, "protocol",
938 cricket::LOCAL_PORT_TYPE, 42);
939 std::unique_ptr<cricket::Candidate> rtcp_local_candidate =
940 CreateFakeCandidate("42.42.42.42", 42, "protocol",
941 cricket::LOCAL_PORT_TYPE, 42);
942 std::unique_ptr<cricket::Candidate> rtcp_remote_candidate =
943 CreateFakeCandidate("42.42.42.42", 42, "protocol",
944 cricket::LOCAL_PORT_TYPE, 42);
945
946 SessionStats session_stats;
947 session_stats.transport_stats["transport"].transport_name = "transport";
948
949 cricket::ConnectionInfo rtp_connection_info;
950 rtp_connection_info.best_connection = false;
951 rtp_connection_info.local_candidate = *rtp_local_candidate.get();
952 rtp_connection_info.remote_candidate = *rtp_remote_candidate.get();
953 rtp_connection_info.sent_total_bytes = 42;
954 rtp_connection_info.recv_total_bytes = 1337;
955 cricket::TransportChannelStats rtp_transport_channel_stats;
956 rtp_transport_channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP;
957 rtp_transport_channel_stats.connection_infos.push_back(rtp_connection_info);
958 session_stats.transport_stats["transport"].channel_stats.push_back(
959 rtp_transport_channel_stats);
960
961
962 // Mock the session to return the desired candidates.
963 EXPECT_CALL(test_->session(), GetTransportStats(_)).WillRepeatedly(Invoke(
964 [this, &session_stats](SessionStats* stats) {
965 *stats = session_stats;
966 return true;
967 }));
968
969 // Get stats without RTCP, an active connection or certificates.
970 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
971 ExpectReportContainsTransportStats(
972 report, session_stats.transport_stats["transport"], nullptr, nullptr);
973
974 cricket::ConnectionInfo rtcp_connection_info;
975 rtcp_connection_info.best_connection = false;
976 rtcp_connection_info.local_candidate = *rtcp_local_candidate.get();
977 rtcp_connection_info.remote_candidate = *rtcp_remote_candidate.get();
978 rtcp_connection_info.sent_total_bytes = 1337;
979 rtcp_connection_info.recv_total_bytes = 42;
980 cricket::TransportChannelStats rtcp_transport_channel_stats;
981 rtcp_transport_channel_stats.component =
982 cricket::ICE_CANDIDATE_COMPONENT_RTCP;
983 rtcp_transport_channel_stats.connection_infos.push_back(rtcp_connection_info);
984 session_stats.transport_stats["transport"].channel_stats.push_back(
985 rtcp_transport_channel_stats);
986
987 collector_->ClearCachedStatsReport();
988 // Get stats with RTCP and without an active connection or certificates.
989 report = GetStatsReport();
990 ExpectReportContainsTransportStats(
991 report, session_stats.transport_stats["transport"], nullptr, nullptr);
992
993 // Get stats with an active connection.
994 rtcp_connection_info.best_connection = true;
995
996 collector_->ClearCachedStatsReport();
997 report = GetStatsReport();
998 ExpectReportContainsTransportStats(
999 report, session_stats.transport_stats["transport"], nullptr, nullptr);
1000
1001 // Get stats with certificates.
1002 std::unique_ptr<CertificateInfo> local_certinfo =
1003 CreateFakeCertificateAndInfoFromDers(
1004 std::vector<std::string>({ "(local) local", "(local) chain" }));
1005 std::unique_ptr<CertificateInfo> remote_certinfo =
1006 CreateFakeCertificateAndInfoFromDers(
1007 std::vector<std::string>({ "(remote) local", "(remote) chain" }));
1008 EXPECT_CALL(test_->session(), GetLocalCertificate(_, _)).WillRepeatedly(
1009 Invoke([this, &local_certinfo](const std::string& transport_name,
1010 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) {
1011 if (transport_name == "transport") {
1012 *certificate = local_certinfo->certificate;
1013 return true;
1014 }
1015 return false;
1016 }));
1017 EXPECT_CALL(test_->session(),
1018 GetRemoteSSLCertificate_ReturnsRawPointer(_)).WillRepeatedly(Invoke(
1019 [this, &remote_certinfo](const std::string& transport_name) {
1020 if (transport_name == "transport")
1021 return remote_certinfo->certificate->ssl_certificate().GetReference();
1022 return static_cast<rtc::SSLCertificate*>(nullptr);
1023 }));
1024
1025 collector_->ClearCachedStatsReport();
1026 report = GetStatsReport();
1027 ExpectReportContainsTransportStats(
1028 report, session_stats.transport_stats["transport"],
1029 local_certinfo.get(), remote_certinfo.get());
1030}
1031
hbosc82f2e12016-09-05 01:36:50 -07001032class RTCStatsCollectorTestWithFakeCollector : public testing::Test {
1033 public:
1034 RTCStatsCollectorTestWithFakeCollector()
1035 : test_(new rtc::RefCountedObject<RTCStatsCollectorTestHelper>()),
1036 collector_(FakeRTCStatsCollector::Create(
1037 &test_->pc(), 50 * rtc::kNumMicrosecsPerMillisec)) {
1038 }
1039
1040 protected:
1041 rtc::scoped_refptr<RTCStatsCollectorTestHelper> test_;
1042 rtc::scoped_refptr<FakeRTCStatsCollector> collector_;
1043};
1044
1045TEST_F(RTCStatsCollectorTestWithFakeCollector, ThreadUsageAndResultsMerging) {
1046 collector_->VerifyThreadUsageAndResultsMerging();
1047}
1048
1049} // namespace
1050
hbosd565b732016-08-30 14:04:35 -07001051} // namespace webrtc