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("RTCIceCandidate_" + candidate.id()); | |
337 EXPECT_TRUE(stats); | |
338 const RTCIceCandidateStats* candidate_stats; | |
339 if (is_local) | |
340 candidate_stats = &stats->cast_to<RTCLocalIceCandidateStats>(); | |
341 else | |
342 candidate_stats = &stats->cast_to<RTCRemoteIceCandidateStats>(); | |
343 EXPECT_EQ(*candidate_stats->ip, candidate.address().ipaddr().ToString()); | |
344 EXPECT_EQ(*candidate_stats->port, | |
345 static_cast<int32_t>(candidate.address().port())); | |
346 EXPECT_EQ(*candidate_stats->protocol, candidate.protocol()); | |
347 EXPECT_EQ(*candidate_stats->candidate_type, | |
348 CandidateTypeToRTCIceCandidateType(candidate.type())); | |
349 EXPECT_EQ(*candidate_stats->priority, | |
350 static_cast<int32_t>(candidate.priority())); | |
351 // TODO(hbos): Define candidate_stats->url. crbug.com/632723 | |
352 EXPECT_FALSE(candidate_stats->url.is_defined()); | |
353 } | |
354 | |
311 void ExpectReportContainsCertificateInfo( | 355 void ExpectReportContainsCertificateInfo( |
312 const rtc::scoped_refptr<const RTCStatsReport>& report, | 356 const rtc::scoped_refptr<const RTCStatsReport>& report, |
313 const CertificateInfo& cert_info) { | 357 const CertificateInfo& cert_info) { |
314 for (size_t i = 0; i < cert_info.fingerprints.size(); ++i) { | 358 for (size_t i = 0; i < cert_info.fingerprints.size(); ++i) { |
315 const RTCStats* stats = report->Get( | 359 const RTCStats* stats = report->Get( |
316 "RTCCertificate_" + cert_info.fingerprints[i]); | 360 "RTCCertificate_" + cert_info.fingerprints[i]); |
317 EXPECT_TRUE(stats); | 361 EXPECT_TRUE(stats); |
318 const RTCCertificateStats& cert_stats = | 362 const RTCCertificateStats& cert_stats = |
319 stats->cast_to<const RTCCertificateStats>(); | 363 stats->cast_to<const RTCCertificateStats>(); |
320 EXPECT_EQ(*cert_stats.fingerprint, cert_info.fingerprints[i]); | 364 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") | 571 if (transport_name == "transport") |
528 return remote_certinfo->certificate->ssl_certificate().GetReference(); | 572 return remote_certinfo->certificate->ssl_certificate().GetReference(); |
529 return static_cast<rtc::SSLCertificate*>(nullptr); | 573 return static_cast<rtc::SSLCertificate*>(nullptr); |
530 })); | 574 })); |
531 | 575 |
532 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); | 576 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); |
533 ExpectReportContainsCertificateInfo(report, *local_certinfo.get()); | 577 ExpectReportContainsCertificateInfo(report, *local_certinfo.get()); |
534 ExpectReportContainsCertificateInfo(report, *remote_certinfo.get()); | 578 ExpectReportContainsCertificateInfo(report, *remote_certinfo.get()); |
535 } | 579 } |
536 | 580 |
581 TEST_F(RTCStatsCollectorTest, CollectRTCIceCandidateStats) { | |
582 // Candidates in the first transport stats. | |
583 std::unique_ptr<cricket::Candidate> a_local_host = CreateFakeCandidate( | |
584 "1.2.3.4", 5, | |
585 "a_local_host's protocol", | |
586 cricket::LOCAL_PORT_TYPE, | |
587 0); | |
588 std::unique_ptr<cricket::Candidate> a_remote_srflx = CreateFakeCandidate( | |
589 "6.7.8.9", 10, | |
590 "remote_stflx's protocol", | |
hta-webrtc
2016/10/04 14:12:46
nit: remote_srflx (server reflexive).
hbos
2016/10/05 10:16:30
Done.
| |
591 cricket::STUN_PORT_TYPE, | |
592 1); | |
593 std::unique_ptr<cricket::Candidate> a_local_prflx = CreateFakeCandidate( | |
594 "11.12.13.14", 15, | |
595 "a_local_prflx's protocol", | |
596 cricket::PRFLX_PORT_TYPE, | |
597 2); | |
598 std::unique_ptr<cricket::Candidate> a_remote_relay = CreateFakeCandidate( | |
599 "16.17.18.19", 20, | |
600 "a_remote_relay's protocol", | |
601 cricket::RELAY_PORT_TYPE, | |
602 3); | |
603 // Candidates in the second transport stats. | |
604 std::unique_ptr<cricket::Candidate> b_local = CreateFakeCandidate( | |
605 "42.42.42.42", 42, | |
606 "b_local's protocol", | |
607 cricket::LOCAL_PORT_TYPE, | |
608 42); | |
609 std::unique_ptr<cricket::Candidate> b_remote = CreateFakeCandidate( | |
610 "42.42.42.42", 42, | |
611 "b_remote's protocol", | |
612 cricket::LOCAL_PORT_TYPE, | |
613 42); | |
614 | |
615 // Mock the session to return the desired candidates. | |
616 EXPECT_CALL(test_->session(), GetTransportStats(_)).WillRepeatedly(Invoke( | |
617 [this, &a_local_host, &a_remote_srflx, &a_local_prflx, &a_remote_relay, | |
618 &b_local, &b_remote](SessionStats* stats) { | |
hta-webrtc
2016/10/04 14:12:46
Isn't it simpler to construct the SessionStats bef
hbos
2016/10/05 10:16:30
Done.
| |
619 cricket::TransportChannelStats a_transport_channel_stats; | |
620 a_transport_channel_stats.connection_infos.push_back( | |
621 cricket::ConnectionInfo()); | |
622 a_transport_channel_stats.connection_infos[0].local_candidate = | |
623 *a_local_host.get(); | |
624 a_transport_channel_stats.connection_infos[0].remote_candidate = | |
625 *a_remote_srflx.get(); | |
626 a_transport_channel_stats.connection_infos.push_back( | |
627 cricket::ConnectionInfo()); | |
628 a_transport_channel_stats.connection_infos[1].local_candidate = | |
629 *a_local_prflx.get(); | |
630 a_transport_channel_stats.connection_infos[1].remote_candidate = | |
631 *a_remote_relay.get(); | |
632 stats->transport_stats["a"].channel_stats.push_back( | |
633 a_transport_channel_stats); | |
634 | |
635 cricket::TransportChannelStats b_transport_channel_stats; | |
636 b_transport_channel_stats.connection_infos.push_back( | |
637 cricket::ConnectionInfo()); | |
638 b_transport_channel_stats.connection_infos[0].local_candidate = | |
639 *b_local.get(); | |
640 b_transport_channel_stats.connection_infos[0].remote_candidate = | |
641 *b_remote.get(); | |
642 stats->transport_stats["b"].channel_stats.push_back( | |
643 b_transport_channel_stats); | |
644 | |
645 return true; | |
646 })); | |
647 | |
648 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); | |
649 ExpectReportContainsCandidate(report, *a_local_host.get(), true); | |
650 ExpectReportContainsCandidate(report, *a_remote_srflx.get(), false); | |
651 ExpectReportContainsCandidate(report, *a_local_prflx.get(), true); | |
652 ExpectReportContainsCandidate(report, *a_remote_relay.get(), false); | |
653 ExpectReportContainsCandidate(report, *b_local.get(), true); | |
654 ExpectReportContainsCandidate(report, *b_remote.get(), false); | |
655 } | |
656 | |
537 TEST_F(RTCStatsCollectorTest, CollectRTCPeerConnectionStats) { | 657 TEST_F(RTCStatsCollectorTest, CollectRTCPeerConnectionStats) { |
538 int64_t before = rtc::TimeUTCMicros(); | 658 int64_t before = rtc::TimeUTCMicros(); |
539 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); | 659 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); |
540 int64_t after = rtc::TimeUTCMicros(); | 660 int64_t after = rtc::TimeUTCMicros(); |
541 EXPECT_EQ(report->GetStatsOfType<RTCPeerConnectionStats>().size(), | 661 EXPECT_EQ(report->GetStatsOfType<RTCPeerConnectionStats>().size(), |
542 static_cast<size_t>(1)) << "Expecting 1 RTCPeerConnectionStats."; | 662 static_cast<size_t>(1)) << "Expecting 1 RTCPeerConnectionStats."; |
543 const RTCStats* stats = report->Get("RTCPeerConnection"); | 663 const RTCStats* stats = report->Get("RTCPeerConnection"); |
544 EXPECT_TRUE(stats); | 664 EXPECT_TRUE(stats); |
545 EXPECT_LE(before, stats->timestamp_us()); | 665 EXPECT_LE(before, stats->timestamp_us()); |
546 EXPECT_LE(stats->timestamp_us(), after); | 666 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_; | 713 rtc::scoped_refptr<FakeRTCStatsCollector> collector_; |
594 }; | 714 }; |
595 | 715 |
596 TEST_F(RTCStatsCollectorTestWithFakeCollector, ThreadUsageAndResultsMerging) { | 716 TEST_F(RTCStatsCollectorTestWithFakeCollector, ThreadUsageAndResultsMerging) { |
597 collector_->VerifyThreadUsageAndResultsMerging(); | 717 collector_->VerifyThreadUsageAndResultsMerging(); |
598 } | 718 } |
599 | 719 |
600 } // namespace | 720 } // namespace |
601 | 721 |
602 } // namespace webrtc | 722 } // namespace webrtc |
OLD | NEW |