OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/api/rtcstatscollector.h" | 11 #include "webrtc/api/rtcstatscollector.h" |
12 | 12 |
13 #include <memory> | 13 #include <memory> |
14 #include <string> | 14 #include <string> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
17 #include "webrtc/api/jsepsessiondescription.h" | 17 #include "webrtc/api/jsepsessiondescription.h" |
18 #include "webrtc/api/stats/rtcstats_objects.h" | 18 #include "webrtc/api/stats/rtcstats_objects.h" |
19 #include "webrtc/api/stats/rtcstatsreport.h" | 19 #include "webrtc/api/stats/rtcstatsreport.h" |
20 #include "webrtc/api/test/mock_datachannel.h" | 20 #include "webrtc/api/test/mock_datachannel.h" |
21 #include "webrtc/api/test/mock_peerconnection.h" | 21 #include "webrtc/api/test/mock_peerconnection.h" |
22 #include "webrtc/api/test/mock_webrtcsession.h" | 22 #include "webrtc/api/test/mock_webrtcsession.h" |
23 #include "webrtc/base/checks.h" | 23 #include "webrtc/base/checks.h" |
24 #include "webrtc/base/fakeclock.h" | 24 #include "webrtc/base/fakeclock.h" |
25 #include "webrtc/base/fakesslidentity.h" | 25 #include "webrtc/base/fakesslidentity.h" |
26 #include "webrtc/base/gunit.h" | 26 #include "webrtc/base/gunit.h" |
27 #include "webrtc/base/logging.h" | 27 #include "webrtc/base/logging.h" |
28 #include "webrtc/base/socketaddress.h" | |
28 #include "webrtc/base/thread_checker.h" | 29 #include "webrtc/base/thread_checker.h" |
29 #include "webrtc/base/timedelta.h" | 30 #include "webrtc/base/timedelta.h" |
30 #include "webrtc/base/timeutils.h" | 31 #include "webrtc/base/timeutils.h" |
31 #include "webrtc/media/base/fakemediaengine.h" | 32 #include "webrtc/media/base/fakemediaengine.h" |
33 #include "webrtc/p2p/base/port.h" | |
32 | 34 |
33 using testing::_; | 35 using testing::_; |
34 using testing::Invoke; | 36 using testing::Invoke; |
35 using testing::Return; | 37 using testing::Return; |
36 using testing::ReturnRef; | 38 using testing::ReturnRef; |
37 | 39 |
38 namespace webrtc { | 40 namespace webrtc { |
39 | 41 |
40 namespace { | 42 namespace { |
41 | 43 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
84 for (size_t i = 0; i < chain->GetSize(); i++) { | 86 for (size_t i = 0; i < chain->GetSize(); i++) { |
85 fp.reset(rtc::SSLFingerprint::Create("sha-1", &chain->Get(i))); | 87 fp.reset(rtc::SSLFingerprint::Create("sha-1", &chain->Get(i))); |
86 EXPECT_TRUE(fp); | 88 EXPECT_TRUE(fp); |
87 info->fingerprints.push_back(fp->GetRfc4572Fingerprint()); | 89 info->fingerprints.push_back(fp->GetRfc4572Fingerprint()); |
88 } | 90 } |
89 } | 91 } |
90 EXPECT_EQ(info->ders.size(), info->fingerprints.size()); | 92 EXPECT_EQ(info->ders.size(), info->fingerprints.size()); |
91 return info; | 93 return info; |
92 } | 94 } |
93 | 95 |
96 std::unique_ptr<cricket::Candidate> CreateFakeCandidate( | |
97 const std::string& hostname, | |
98 int port, | |
99 const std::string& protocol, | |
100 const std::string& candidate_type, | |
101 uint32_t priority) { | |
102 std::unique_ptr<cricket::Candidate> candidate(new cricket::Candidate()); | |
103 candidate->set_address(rtc::SocketAddress(hostname, port)); | |
104 candidate->set_protocol(protocol); | |
105 candidate->set_type(candidate_type); | |
106 candidate->set_priority(priority); | |
107 return candidate; | |
108 } | |
109 | |
94 class RTCStatsCollectorTestHelper : public SetSessionDescriptionObserver { | 110 class RTCStatsCollectorTestHelper : public SetSessionDescriptionObserver { |
95 public: | 111 public: |
96 RTCStatsCollectorTestHelper() | 112 RTCStatsCollectorTestHelper() |
97 : worker_thread_(rtc::Thread::Current()), | 113 : worker_thread_(rtc::Thread::Current()), |
98 network_thread_(rtc::Thread::Current()), | 114 network_thread_(rtc::Thread::Current()), |
99 channel_manager_(new cricket::ChannelManager( | 115 channel_manager_(new cricket::ChannelManager( |
100 new cricket::FakeMediaEngine(), | 116 new cricket::FakeMediaEngine(), |
101 worker_thread_, | 117 worker_thread_, |
102 network_thread_)), | 118 network_thread_)), |
103 media_controller_( | 119 media_controller_( |
104 MediaControllerInterface::Create(cricket::MediaConfig(), | 120 MediaControllerInterface::Create(cricket::MediaConfig(), |
105 worker_thread_, | 121 worker_thread_, |
106 channel_manager_.get())), | 122 channel_manager_.get())), |
107 session_(media_controller_.get()), | 123 session_(media_controller_.get()), |
108 pc_() { | 124 pc_() { |
109 // Default return values for mocks. | 125 // Default return values for mocks. |
110 EXPECT_CALL(pc_, session()).WillRepeatedly(Return(&session_)); | 126 EXPECT_CALL(pc_, session()).WillRepeatedly(Return(&session_)); |
111 EXPECT_CALL(pc_, sctp_data_channels()).WillRepeatedly( | 127 EXPECT_CALL(pc_, sctp_data_channels()).WillRepeatedly( |
112 ReturnRef(data_channels_)); | 128 ReturnRef(data_channels_)); |
113 EXPECT_CALL(session_, GetTransportStats(_)).WillRepeatedly(Return(false)); | 129 EXPECT_CALL(session_, GetTransportStats(_)).WillRepeatedly(Return(false)); |
130 EXPECT_CALL(session_, GetLocalCertificate(_, _)).WillRepeatedly( | |
131 Return(false)); | |
132 EXPECT_CALL(session_, GetRemoteSSLCertificate_ReturnsRawPointer(_)) | |
133 .WillRepeatedly(Return(nullptr)); | |
114 } | 134 } |
115 | 135 |
116 rtc::ScopedFakeClock& fake_clock() { return fake_clock_; } | 136 rtc::ScopedFakeClock& fake_clock() { return fake_clock_; } |
117 MockWebRtcSession& session() { return session_; } | 137 MockWebRtcSession& session() { return session_; } |
118 MockPeerConnection& pc() { return pc_; } | 138 MockPeerConnection& pc() { return pc_; } |
119 std::vector<rtc::scoped_refptr<DataChannel>>& data_channels() { | 139 std::vector<rtc::scoped_refptr<DataChannel>>& data_channels() { |
120 return data_channels_; | 140 return data_channels_; |
121 } | 141 } |
122 | 142 |
123 // SetSessionDescriptionObserver overrides. | 143 // SetSessionDescriptionObserver overrides. |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
301 &test_->pc(), 50 * rtc::kNumMicrosecsPerMillisec)) { | 321 &test_->pc(), 50 * rtc::kNumMicrosecsPerMillisec)) { |
302 } | 322 } |
303 | 323 |
304 rtc::scoped_refptr<const RTCStatsReport> GetStatsReport() { | 324 rtc::scoped_refptr<const RTCStatsReport> GetStatsReport() { |
305 rtc::scoped_refptr<StatsCallback> callback = StatsCallback::Create(); | 325 rtc::scoped_refptr<StatsCallback> callback = StatsCallback::Create(); |
306 collector_->GetStatsReport(callback); | 326 collector_->GetStatsReport(callback); |
307 EXPECT_TRUE_WAIT(callback->report(), kGetStatsReportTimeoutMs); | 327 EXPECT_TRUE_WAIT(callback->report(), kGetStatsReportTimeoutMs); |
308 return callback->report(); | 328 return callback->report(); |
309 } | 329 } |
310 | 330 |
331 void ExpectReportContainsCandidate( | |
332 const rtc::scoped_refptr<const RTCStatsReport>& report, | |
333 const cricket::Candidate& candidate, | |
334 bool is_local) { | |
335 const RTCStats* stats = | |
336 report->Get(is_local ? "RTCLocalIceCandidate_" + candidate.id() | |
337 : "RTCRemoteIceCandidate_" + candidate.id()); | |
338 EXPECT_TRUE(stats); | |
339 const RTCIceCandidateStats* candidate_stats; | |
340 if (is_local) | |
341 candidate_stats = &stats->cast_to<RTCLocalIceCandidateStats>(); | |
342 else | |
343 candidate_stats = &stats->cast_to<RTCRemoteIceCandidateStats>(); | |
344 EXPECT_EQ(*candidate_stats->ip, candidate.address().ipaddr().ToString()); | |
345 EXPECT_EQ(*candidate_stats->port, | |
346 static_cast<int32_t>(candidate.address().port())); | |
347 EXPECT_EQ(*candidate_stats->protocol, candidate.protocol()); | |
348 EXPECT_EQ(*candidate_stats->candidate_type, | |
349 CandidateTypeToRTCIceCandidateType(candidate.type())); | |
350 EXPECT_EQ(*candidate_stats->priority, | |
351 static_cast<int32_t>(candidate.priority())); | |
352 // TODO(hbos): Define candidate_stats->url. crbug.com/632723 | |
353 EXPECT_FALSE(candidate_stats->url.is_defined()); | |
354 } | |
355 | |
311 void ExpectReportContainsCertificateInfo( | 356 void ExpectReportContainsCertificateInfo( |
312 const rtc::scoped_refptr<const RTCStatsReport>& report, | 357 const rtc::scoped_refptr<const RTCStatsReport>& report, |
313 const CertificateInfo& cert_info) { | 358 const CertificateInfo& cert_info) { |
314 for (size_t i = 0; i < cert_info.fingerprints.size(); ++i) { | 359 for (size_t i = 0; i < cert_info.fingerprints.size(); ++i) { |
315 const RTCStats* stats = report->Get( | 360 const RTCStats* stats = report->Get( |
316 "RTCCertificate_" + cert_info.fingerprints[i]); | 361 "RTCCertificate_" + cert_info.fingerprints[i]); |
317 EXPECT_TRUE(stats); | 362 EXPECT_TRUE(stats); |
318 const RTCCertificateStats& cert_stats = | 363 const RTCCertificateStats& cert_stats = |
319 stats->cast_to<const RTCCertificateStats>(); | 364 stats->cast_to<const RTCCertificateStats>(); |
320 EXPECT_EQ(*cert_stats.fingerprint, cert_info.fingerprints[i]); | 365 EXPECT_EQ(*cert_stats.fingerprint, cert_info.fingerprints[i]); |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
527 if (transport_name == "transport") | 572 if (transport_name == "transport") |
528 return remote_certinfo->certificate->ssl_certificate().GetReference(); | 573 return remote_certinfo->certificate->ssl_certificate().GetReference(); |
529 return static_cast<rtc::SSLCertificate*>(nullptr); | 574 return static_cast<rtc::SSLCertificate*>(nullptr); |
530 })); | 575 })); |
531 | 576 |
532 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); | 577 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); |
533 ExpectReportContainsCertificateInfo(report, *local_certinfo.get()); | 578 ExpectReportContainsCertificateInfo(report, *local_certinfo.get()); |
534 ExpectReportContainsCertificateInfo(report, *remote_certinfo.get()); | 579 ExpectReportContainsCertificateInfo(report, *remote_certinfo.get()); |
535 } | 580 } |
536 | 581 |
582 TEST_F(RTCStatsCollectorTest, CollectRTCIceCandidateStats) { | |
583 // Candidates in the first transport stats. | |
584 std::unique_ptr<cricket::Candidate> a_local_host = CreateFakeCandidate( | |
585 "1.2.3.4", 5, | |
586 "a_local_host's protocol", | |
587 cricket::LOCAL_PORT_TYPE, | |
588 0); | |
589 std::unique_ptr<cricket::Candidate> a_remote_srflx = CreateFakeCandidate( | |
590 "6.7.8.9", 10, | |
591 "remote_srflx's protocol", | |
592 cricket::STUN_PORT_TYPE, | |
593 1); | |
594 std::unique_ptr<cricket::Candidate> a_local_prflx = CreateFakeCandidate( | |
595 "11.12.13.14", 15, | |
596 "a_local_prflx's protocol", | |
597 cricket::PRFLX_PORT_TYPE, | |
598 2); | |
599 std::unique_ptr<cricket::Candidate> a_remote_relay = CreateFakeCandidate( | |
600 "16.17.18.19", 20, | |
601 "a_remote_relay's protocol", | |
602 cricket::RELAY_PORT_TYPE, | |
603 3); | |
604 // Candidates in the second transport stats. | |
605 std::unique_ptr<cricket::Candidate> b_local = CreateFakeCandidate( | |
606 "42.42.42.42", 42, | |
607 "b_local's protocol", | |
608 cricket::LOCAL_PORT_TYPE, | |
609 42); | |
610 std::unique_ptr<cricket::Candidate> b_remote = CreateFakeCandidate( | |
611 "42.42.42.42", 42, | |
612 "b_remote's protocol", | |
613 cricket::LOCAL_PORT_TYPE, | |
614 42); | |
615 | |
616 SessionStats session_stats; | |
617 | |
618 cricket::TransportChannelStats a_transport_channel_stats; | |
619 a_transport_channel_stats.connection_infos.push_back( | |
620 cricket::ConnectionInfo()); | |
621 a_transport_channel_stats.connection_infos[0].local_candidate = | |
622 *a_local_host.get(); | |
623 a_transport_channel_stats.connection_infos[0].remote_candidate = | |
624 *a_remote_srflx.get(); | |
625 a_transport_channel_stats.connection_infos.push_back( | |
626 cricket::ConnectionInfo()); | |
627 a_transport_channel_stats.connection_infos[1].local_candidate = | |
628 *a_local_prflx.get(); | |
629 a_transport_channel_stats.connection_infos[1].remote_candidate = | |
630 *a_remote_relay.get(); | |
631 session_stats.transport_stats["a"].channel_stats.push_back( | |
632 a_transport_channel_stats); | |
633 | |
634 cricket::TransportChannelStats b_transport_channel_stats; | |
635 b_transport_channel_stats.connection_infos.push_back( | |
636 cricket::ConnectionInfo()); | |
637 b_transport_channel_stats.connection_infos[0].local_candidate = | |
638 *b_local.get(); | |
639 b_transport_channel_stats.connection_infos[0].remote_candidate = | |
640 *b_remote.get(); | |
641 session_stats.transport_stats["b"].channel_stats.push_back( | |
642 b_transport_channel_stats); | |
643 | |
644 // Mock the session to return the desired candidates. | |
645 EXPECT_CALL(test_->session(), GetTransportStats(_)).WillRepeatedly(Invoke( | |
646 [this, &session_stats](SessionStats* stats) { | |
hta-webrtc
2016/10/05 11:17:14
Does the lambda actually touch "this"?
| |
647 *stats = session_stats; | |
648 return true; | |
649 })); | |
650 | |
651 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); | |
652 ExpectReportContainsCandidate(report, *a_local_host.get(), true); | |
653 ExpectReportContainsCandidate(report, *a_remote_srflx.get(), false); | |
654 ExpectReportContainsCandidate(report, *a_local_prflx.get(), true); | |
655 ExpectReportContainsCandidate(report, *a_remote_relay.get(), false); | |
656 ExpectReportContainsCandidate(report, *b_local.get(), true); | |
657 ExpectReportContainsCandidate(report, *b_remote.get(), false); | |
658 } | |
659 | |
537 TEST_F(RTCStatsCollectorTest, CollectRTCPeerConnectionStats) { | 660 TEST_F(RTCStatsCollectorTest, CollectRTCPeerConnectionStats) { |
538 int64_t before = rtc::TimeUTCMicros(); | 661 int64_t before = rtc::TimeUTCMicros(); |
539 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); | 662 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); |
540 int64_t after = rtc::TimeUTCMicros(); | 663 int64_t after = rtc::TimeUTCMicros(); |
541 EXPECT_EQ(report->GetStatsOfType<RTCPeerConnectionStats>().size(), | 664 EXPECT_EQ(report->GetStatsOfType<RTCPeerConnectionStats>().size(), |
542 static_cast<size_t>(1)) << "Expecting 1 RTCPeerConnectionStats."; | 665 static_cast<size_t>(1)) << "Expecting 1 RTCPeerConnectionStats."; |
543 const RTCStats* stats = report->Get("RTCPeerConnection"); | 666 const RTCStats* stats = report->Get("RTCPeerConnection"); |
544 EXPECT_TRUE(stats); | 667 EXPECT_TRUE(stats); |
545 EXPECT_LE(before, stats->timestamp_us()); | 668 EXPECT_LE(before, stats->timestamp_us()); |
546 EXPECT_LE(stats->timestamp_us(), after); | 669 EXPECT_LE(stats->timestamp_us(), after); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
593 rtc::scoped_refptr<FakeRTCStatsCollector> collector_; | 716 rtc::scoped_refptr<FakeRTCStatsCollector> collector_; |
594 }; | 717 }; |
595 | 718 |
596 TEST_F(RTCStatsCollectorTestWithFakeCollector, ThreadUsageAndResultsMerging) { | 719 TEST_F(RTCStatsCollectorTestWithFakeCollector, ThreadUsageAndResultsMerging) { |
597 collector_->VerifyThreadUsageAndResultsMerging(); | 720 collector_->VerifyThreadUsageAndResultsMerging(); |
598 } | 721 } |
599 | 722 |
600 } // namespace | 723 } // namespace |
601 | 724 |
602 } // namespace webrtc | 725 } // namespace webrtc |
OLD | NEW |