Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 1 | /* |
| 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 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 11 | #include <memory> |
| 12 | #include <set> |
| 13 | #include <string> |
| 14 | #include <utility> |
| 15 | #include <vector> |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 16 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 17 | #include "absl/types/optional.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 18 | #include "api/call/call_factory_interface.h" |
Harald Alvestrand | 4238628 | 2018-07-12 05:56:05 | [diff] [blame] | 19 | #include "api/jsep.h" |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 20 | #include "api/jsep_session_description.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 21 | #include "api/peer_connection_interface.h" |
| 22 | #include "api/peer_connection_proxy.h" |
| 23 | #include "api/rtc_error.h" |
Mirko Bonadei | d970807 | 2019-01-25 19:26:48 | [diff] [blame] | 24 | #include "api/scoped_refptr.h" |
Danil Chapovalov | 53d45ba | 2019-07-03 12:56:33 | [diff] [blame] | 25 | #include "api/task_queue/default_task_queue_factory.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 26 | #include "media/base/fake_media_engine.h" |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 27 | #include "p2p/base/mock_async_resolver.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 28 | #include "p2p/base/port_allocator.h" |
| 29 | #include "p2p/client/basic_port_allocator.h" |
| 30 | #include "pc/peer_connection.h" |
| 31 | #include "pc/peer_connection_factory.h" |
| 32 | #include "pc/peer_connection_wrapper.h" |
| 33 | #include "pc/sdp_utils.h" |
| 34 | #include "pc/test/mock_peer_connection_observers.h" |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 35 | #include "pc/webrtc_sdp.h" |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 36 | #include "rtc_base/arraysize.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 37 | #include "rtc_base/checks.h" |
Qingsi Wang | ecd3054 | 2019-05-22 21:34:56 | [diff] [blame] | 38 | #include "rtc_base/fake_mdns_responder.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 39 | #include "rtc_base/fake_network.h" |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 40 | #include "rtc_base/gunit.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 41 | #include "rtc_base/ref_counted_object.h" |
| 42 | #include "rtc_base/rtc_certificate_generator.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 43 | #include "rtc_base/socket_address.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 44 | #include "rtc_base/thread.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 45 | #include "rtc_base/virtual_socket_server.h" |
Mirko Bonadei | 17f4878 | 2018-09-28 06:51:10 | [diff] [blame] | 46 | #include "system_wrappers/include/metrics.h" |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 47 | #include "test/gmock.h" |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 48 | |
| 49 | namespace webrtc { |
| 50 | |
| 51 | using RTCConfiguration = PeerConnectionInterface::RTCConfiguration; |
| 52 | using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions; |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 53 | using ::testing::NiceMock; |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 54 | using ::testing::Values; |
| 55 | |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 56 | static const char kUsagePatternMetric[] = "WebRTC.PeerConnection.UsagePattern"; |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 57 | static constexpr int kDefaultTimeout = 10000; |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 58 | static const rtc::SocketAddress kLocalAddrs[2] = { |
| 59 | rtc::SocketAddress("1.1.1.1", 0), rtc::SocketAddress("2.2.2.2", 0)}; |
Harald Alvestrand | 056d811 | 2018-07-16 17:18:58 | [diff] [blame] | 60 | static const rtc::SocketAddress kPrivateLocalAddress("10.1.1.1", 0); |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 61 | static const rtc::SocketAddress kPrivateIpv6LocalAddress("fd12:3456:789a:1::1", |
| 62 | 0); |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 63 | |
| 64 | int MakeUsageFingerprint(std::set<PeerConnection::UsageEvent> events) { |
| 65 | int signature = 0; |
| 66 | for (const auto it : events) { |
| 67 | signature |= static_cast<int>(it); |
| 68 | } |
| 69 | return signature; |
| 70 | } |
| 71 | |
| 72 | class PeerConnectionFactoryForUsageHistogramTest |
| 73 | : public rtc::RefCountedObject<PeerConnectionFactory> { |
| 74 | public: |
| 75 | PeerConnectionFactoryForUsageHistogramTest() |
Danil Chapovalov | f5258be | 2019-03-19 16:45:24 | [diff] [blame] | 76 | : rtc::RefCountedObject<PeerConnectionFactory>([] { |
| 77 | PeerConnectionFactoryDependencies dependencies; |
| 78 | dependencies.network_thread = rtc::Thread::Current(); |
| 79 | dependencies.worker_thread = rtc::Thread::Current(); |
| 80 | dependencies.signaling_thread = rtc::Thread::Current(); |
Danil Chapovalov | 53d45ba | 2019-07-03 12:56:33 | [diff] [blame] | 81 | dependencies.task_queue_factory = CreateDefaultTaskQueueFactory(); |
Danil Chapovalov | f5258be | 2019-03-19 16:45:24 | [diff] [blame] | 82 | dependencies.media_engine = |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 83 | std::make_unique<cricket::FakeMediaEngine>(); |
Danil Chapovalov | f5258be | 2019-03-19 16:45:24 | [diff] [blame] | 84 | dependencies.call_factory = CreateCallFactory(); |
| 85 | return dependencies; |
| 86 | }()) {} |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 87 | |
| 88 | void ActionsBeforeInitializeForTesting(PeerConnectionInterface* pc) override { |
| 89 | PeerConnection* internal_pc = static_cast<PeerConnection*>(pc); |
| 90 | if (return_histogram_very_quickly_) { |
| 91 | internal_pc->ReturnHistogramVeryQuicklyForTesting(); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void ReturnHistogramVeryQuickly() { return_histogram_very_quickly_ = true; } |
| 96 | |
| 97 | private: |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 98 | bool return_histogram_very_quickly_ = false; |
| 99 | }; |
| 100 | |
| 101 | class PeerConnectionWrapperForUsageHistogramTest; |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 102 | |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 103 | typedef PeerConnectionWrapperForUsageHistogramTest* RawWrapperPtr; |
| 104 | |
| 105 | class ObserverForUsageHistogramTest : public MockPeerConnectionObserver { |
| 106 | public: |
| 107 | void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override; |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 108 | |
| 109 | void OnInterestingUsage(int usage_pattern) override { |
| 110 | interesting_usage_detected_ = usage_pattern; |
| 111 | } |
| 112 | |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 113 | void PrepareToExchangeCandidates(RawWrapperPtr other) { |
| 114 | candidate_target_ = other; |
| 115 | } |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 116 | |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 117 | bool HaveDataChannel() { return last_datachannel_; } |
| 118 | |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 119 | absl::optional<int> interesting_usage_detected() { |
| 120 | return interesting_usage_detected_; |
| 121 | } |
| 122 | |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 123 | void ClearInterestingUsageDetector() { |
| 124 | interesting_usage_detected_ = absl::optional<int>(); |
| 125 | } |
| 126 | |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 127 | bool candidate_gathered() const { return candidate_gathered_; } |
| 128 | |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 129 | private: |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 130 | absl::optional<int> interesting_usage_detected_; |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 131 | bool candidate_gathered_ = false; |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 132 | RawWrapperPtr candidate_target_; // Note: Not thread-safe against deletions. |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | class PeerConnectionWrapperForUsageHistogramTest |
| 136 | : public PeerConnectionWrapper { |
| 137 | public: |
| 138 | using PeerConnectionWrapper::PeerConnectionWrapper; |
| 139 | |
| 140 | PeerConnection* GetInternalPeerConnection() { |
| 141 | auto* pci = |
| 142 | static_cast<PeerConnectionProxyWithInternal<PeerConnectionInterface>*>( |
| 143 | pc()); |
| 144 | return static_cast<PeerConnection*>(pci->internal()); |
| 145 | } |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 146 | |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 147 | // Override with different return type |
| 148 | ObserverForUsageHistogramTest* observer() { |
| 149 | return static_cast<ObserverForUsageHistogramTest*>( |
| 150 | PeerConnectionWrapper::observer()); |
| 151 | } |
| 152 | |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 153 | void PrepareToExchangeCandidates( |
| 154 | PeerConnectionWrapperForUsageHistogramTest* other) { |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 155 | observer()->PrepareToExchangeCandidates(other); |
| 156 | other->observer()->PrepareToExchangeCandidates(this); |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | bool IsConnected() { |
| 160 | return pc()->ice_connection_state() == |
| 161 | PeerConnectionInterface::kIceConnectionConnected || |
| 162 | pc()->ice_connection_state() == |
| 163 | PeerConnectionInterface::kIceConnectionCompleted; |
| 164 | } |
| 165 | |
| 166 | bool HaveDataChannel() { |
| 167 | return static_cast<ObserverForUsageHistogramTest*>(observer()) |
| 168 | ->HaveDataChannel(); |
| 169 | } |
Qingsi Wang | 32913c1 | 2019-10-30 23:03:46 | [diff] [blame] | 170 | void BufferIceCandidate(const webrtc::IceCandidateInterface* candidate) { |
| 171 | std::string sdp; |
| 172 | EXPECT_TRUE(candidate->ToString(&sdp)); |
| 173 | std::unique_ptr<webrtc::IceCandidateInterface> candidate_copy( |
| 174 | CreateIceCandidate(candidate->sdp_mid(), candidate->sdp_mline_index(), |
| 175 | sdp, nullptr)); |
| 176 | buffered_candidates_.push_back(std::move(candidate_copy)); |
Harald Alvestrand | 4238628 | 2018-07-12 05:56:05 | [diff] [blame] | 177 | } |
Harald Alvestrand | 056d811 | 2018-07-16 17:18:58 | [diff] [blame] | 178 | |
Harald Alvestrand | 4238628 | 2018-07-12 05:56:05 | [diff] [blame] | 179 | void AddBufferedIceCandidates() { |
| 180 | for (const auto& candidate : buffered_candidates_) { |
| 181 | EXPECT_TRUE(pc()->AddIceCandidate(candidate.get())); |
| 182 | } |
| 183 | buffered_candidates_.clear(); |
| 184 | } |
Harald Alvestrand | 056d811 | 2018-07-16 17:18:58 | [diff] [blame] | 185 | |
Qingsi Wang | 32913c1 | 2019-10-30 23:03:46 | [diff] [blame] | 186 | // This method performs the following actions in sequence: |
| 187 | // 1. Exchange Offer and Answer. |
| 188 | // 2. Exchange ICE candidates after both caller and callee complete |
| 189 | // gathering. |
| 190 | // 3. Wait for ICE to connect. |
| 191 | // |
| 192 | // This guarantees a deterministic sequence of events and also rules out the |
| 193 | // occurrence of prflx candidates if the offer/answer signaling and the |
| 194 | // candidate trickling race in order. In case prflx candidates need to be |
| 195 | // simulated, see the approach used by tests below for that. |
Harald Alvestrand | 4238628 | 2018-07-12 05:56:05 | [diff] [blame] | 196 | bool ConnectTo(PeerConnectionWrapperForUsageHistogramTest* callee) { |
| 197 | PrepareToExchangeCandidates(callee); |
Harald Alvestrand | 056d811 | 2018-07-16 17:18:58 | [diff] [blame] | 198 | if (!ExchangeOfferAnswerWith(callee)) { |
| 199 | return false; |
| 200 | } |
Qingsi Wang | 32913c1 | 2019-10-30 23:03:46 | [diff] [blame] | 201 | // Wait until the gathering completes before we signal the candidate. |
| 202 | WAIT(observer()->ice_gathering_complete_, kDefaultTimeout); |
| 203 | WAIT(callee->observer()->ice_gathering_complete_, kDefaultTimeout); |
Harald Alvestrand | 4238628 | 2018-07-12 05:56:05 | [diff] [blame] | 204 | AddBufferedIceCandidates(); |
| 205 | callee->AddBufferedIceCandidates(); |
Harald Alvestrand | 056d811 | 2018-07-16 17:18:58 | [diff] [blame] | 206 | WAIT(IsConnected(), kDefaultTimeout); |
| 207 | WAIT(callee->IsConnected(), kDefaultTimeout); |
Harald Alvestrand | 4238628 | 2018-07-12 05:56:05 | [diff] [blame] | 208 | return IsConnected() && callee->IsConnected(); |
| 209 | } |
| 210 | |
Harald Alvestrand | 056d811 | 2018-07-16 17:18:58 | [diff] [blame] | 211 | bool GenerateOfferAndCollectCandidates() { |
| 212 | auto offer = CreateOffer(RTCOfferAnswerOptions()); |
| 213 | if (!offer) { |
| 214 | return false; |
| 215 | } |
| 216 | bool set_local_offer = |
| 217 | SetLocalDescription(CloneSessionDescription(offer.get())); |
| 218 | EXPECT_TRUE(set_local_offer); |
| 219 | if (!set_local_offer) { |
| 220 | return false; |
| 221 | } |
| 222 | EXPECT_TRUE_WAIT(observer()->ice_gathering_complete_, kDefaultTimeout); |
| 223 | return true; |
| 224 | } |
| 225 | |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 226 | webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() { |
| 227 | return pc()->ice_gathering_state(); |
| 228 | } |
| 229 | |
Harald Alvestrand | 4238628 | 2018-07-12 05:56:05 | [diff] [blame] | 230 | private: |
| 231 | // Candidates that have been sent but not yet configured |
| 232 | std::vector<std::unique_ptr<webrtc::IceCandidateInterface>> |
| 233 | buffered_candidates_; |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 234 | }; |
| 235 | |
Qingsi Wang | 32913c1 | 2019-10-30 23:03:46 | [diff] [blame] | 236 | // Buffers candidates until we add them via AddBufferedIceCandidates. |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 237 | void ObserverForUsageHistogramTest::OnIceCandidate( |
| 238 | const webrtc::IceCandidateInterface* candidate) { |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 239 | // If target is not set, ignore. This happens in one-ended unit tests. |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 240 | if (candidate_target_) { |
Qingsi Wang | 32913c1 | 2019-10-30 23:03:46 | [diff] [blame] | 241 | this->candidate_target_->BufferIceCandidate(candidate); |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 242 | } |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 243 | candidate_gathered_ = true; |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 244 | } |
| 245 | |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 246 | class PeerConnectionUsageHistogramTest : public ::testing::Test { |
| 247 | protected: |
| 248 | typedef std::unique_ptr<PeerConnectionWrapperForUsageHistogramTest> |
| 249 | WrapperPtr; |
| 250 | |
| 251 | PeerConnectionUsageHistogramTest() |
| 252 | : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 253 | webrtc::metrics::Reset(); |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | WrapperPtr CreatePeerConnection() { |
Harald Alvestrand | 056d811 | 2018-07-16 17:18:58 | [diff] [blame] | 257 | return CreatePeerConnection(RTCConfiguration(), |
| 258 | PeerConnectionFactoryInterface::Options(), |
| 259 | nullptr, false); |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 260 | } |
| 261 | |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 262 | WrapperPtr CreatePeerConnection(const RTCConfiguration& config) { |
| 263 | return CreatePeerConnection( |
Harald Alvestrand | 056d811 | 2018-07-16 17:18:58 | [diff] [blame] | 264 | config, PeerConnectionFactoryInterface::Options(), nullptr, false); |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 265 | } |
| 266 | |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 267 | WrapperPtr CreatePeerConnectionWithMdns(const RTCConfiguration& config) { |
| 268 | auto resolver_factory = |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 269 | std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>(); |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 270 | |
| 271 | webrtc::PeerConnectionDependencies deps(nullptr /* observer_in */); |
| 272 | |
| 273 | auto fake_network = NewFakeNetwork(); |
Qingsi Wang | ecd3054 | 2019-05-22 21:34:56 | [diff] [blame] | 274 | fake_network->set_mdns_responder( |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 275 | std::make_unique<webrtc::FakeMdnsResponder>(rtc::Thread::Current())); |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 276 | fake_network->AddInterface(NextLocalAddress()); |
| 277 | |
| 278 | std::unique_ptr<cricket::BasicPortAllocator> port_allocator( |
| 279 | new cricket::BasicPortAllocator(fake_network)); |
| 280 | |
| 281 | deps.async_resolver_factory = std::move(resolver_factory); |
| 282 | deps.allocator = std::move(port_allocator); |
| 283 | |
| 284 | return CreatePeerConnection(config, |
| 285 | PeerConnectionFactoryInterface::Options(), |
| 286 | std::move(deps), false); |
| 287 | } |
| 288 | |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 289 | WrapperPtr CreatePeerConnectionWithImmediateReport() { |
Harald Alvestrand | 056d811 | 2018-07-16 17:18:58 | [diff] [blame] | 290 | return CreatePeerConnection(RTCConfiguration(), |
| 291 | PeerConnectionFactoryInterface::Options(), |
| 292 | nullptr, true); |
| 293 | } |
| 294 | |
| 295 | WrapperPtr CreatePeerConnectionWithPrivateLocalAddresses() { |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 296 | auto* fake_network = NewFakeNetwork(); |
| 297 | fake_network->AddInterface(NextLocalAddress()); |
| 298 | fake_network->AddInterface(kPrivateLocalAddress); |
| 299 | |
| 300 | auto port_allocator = |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 301 | std::make_unique<cricket::BasicPortAllocator>(fake_network); |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 302 | |
Harald Alvestrand | 056d811 | 2018-07-16 17:18:58 | [diff] [blame] | 303 | return CreatePeerConnection(RTCConfiguration(), |
| 304 | PeerConnectionFactoryInterface::Options(), |
| 305 | std::move(port_allocator), false); |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 306 | } |
| 307 | |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 308 | WrapperPtr CreatePeerConnectionWithPrivateIpv6LocalAddresses() { |
| 309 | auto* fake_network = NewFakeNetwork(); |
| 310 | fake_network->AddInterface(NextLocalAddress()); |
| 311 | fake_network->AddInterface(kPrivateIpv6LocalAddress); |
| 312 | |
| 313 | auto port_allocator = |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 314 | std::make_unique<cricket::BasicPortAllocator>(fake_network); |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 315 | |
| 316 | return CreatePeerConnection(RTCConfiguration(), |
| 317 | PeerConnectionFactoryInterface::Options(), |
| 318 | std::move(port_allocator), false); |
| 319 | } |
| 320 | |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 321 | WrapperPtr CreatePeerConnection( |
| 322 | const RTCConfiguration& config, |
| 323 | const PeerConnectionFactoryInterface::Options factory_options, |
Harald Alvestrand | 056d811 | 2018-07-16 17:18:58 | [diff] [blame] | 324 | std::unique_ptr<cricket::PortAllocator> allocator, |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 325 | bool immediate_report) { |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 326 | PeerConnectionDependencies deps(nullptr); |
| 327 | deps.allocator = std::move(allocator); |
| 328 | |
| 329 | return CreatePeerConnection(config, factory_options, std::move(deps), |
| 330 | immediate_report); |
| 331 | } |
| 332 | |
| 333 | WrapperPtr CreatePeerConnection( |
| 334 | const RTCConfiguration& config, |
| 335 | const PeerConnectionFactoryInterface::Options factory_options, |
| 336 | PeerConnectionDependencies deps, |
| 337 | bool immediate_report) { |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 338 | rtc::scoped_refptr<PeerConnectionFactoryForUsageHistogramTest> pc_factory( |
| 339 | new PeerConnectionFactoryForUsageHistogramTest()); |
| 340 | pc_factory->SetOptions(factory_options); |
| 341 | RTC_CHECK(pc_factory->Initialize()); |
| 342 | if (immediate_report) { |
| 343 | pc_factory->ReturnHistogramVeryQuickly(); |
| 344 | } |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 345 | |
| 346 | // If no allocator is provided, one will be created using a network manager |
| 347 | // that uses the host network. This doesn't work on all trybots. |
| 348 | if (!deps.allocator) { |
| 349 | auto fake_network = NewFakeNetwork(); |
| 350 | fake_network->AddInterface(NextLocalAddress()); |
| 351 | deps.allocator = |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 352 | std::make_unique<cricket::BasicPortAllocator>(fake_network); |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 353 | } |
| 354 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 355 | auto observer = std::make_unique<ObserverForUsageHistogramTest>(); |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 356 | deps.observer = observer.get(); |
| 357 | |
| 358 | auto pc = pc_factory->CreatePeerConnection(config, std::move(deps)); |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 359 | if (!pc) { |
| 360 | return nullptr; |
| 361 | } |
| 362 | |
Yves Gerey | 4e93329 | 2018-10-31 14:36:05 | [diff] [blame] | 363 | observer->SetPeerConnectionInterface(pc.get()); |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 364 | auto wrapper = std::make_unique<PeerConnectionWrapperForUsageHistogramTest>( |
| 365 | pc_factory, pc, std::move(observer)); |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 366 | return wrapper; |
| 367 | } |
| 368 | |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 369 | int ObservedFingerprint() { |
| 370 | // This works correctly only if there is only one sample value |
| 371 | // that has been counted. |
| 372 | // Returns -1 for "not found". |
| 373 | return webrtc::metrics::MinSample(kUsagePatternMetric); |
| 374 | } |
| 375 | |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 376 | // The PeerConnection's port allocator is tied to the PeerConnection's |
| 377 | // lifetime and expects the underlying NetworkManager to outlive it. That |
| 378 | // prevents us from having the PeerConnectionWrapper own the fake network. |
| 379 | // Therefore, the test fixture will own all the fake networks even though |
| 380 | // tests should access the fake network through the PeerConnectionWrapper. |
| 381 | rtc::FakeNetworkManager* NewFakeNetwork() { |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 382 | fake_networks_.emplace_back(std::make_unique<rtc::FakeNetworkManager>()); |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 383 | return fake_networks_.back().get(); |
| 384 | } |
| 385 | |
| 386 | rtc::SocketAddress NextLocalAddress() { |
| 387 | RTC_DCHECK(next_local_address_ < (int)arraysize(kLocalAddrs)); |
| 388 | return kLocalAddrs[next_local_address_++]; |
| 389 | } |
| 390 | |
| 391 | std::vector<std::unique_ptr<rtc::FakeNetworkManager>> fake_networks_; |
| 392 | int next_local_address_ = 0; |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 393 | std::unique_ptr<rtc::VirtualSocketServer> vss_; |
| 394 | rtc::AutoSocketServerThread main_; |
| 395 | }; |
| 396 | |
| 397 | TEST_F(PeerConnectionUsageHistogramTest, UsageFingerprintHistogramFromTimeout) { |
| 398 | auto pc = CreatePeerConnectionWithImmediateReport(); |
| 399 | |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 400 | int expected_fingerprint = MakeUsageFingerprint({}); |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 401 | EXPECT_METRIC_EQ_WAIT(1, webrtc::metrics::NumSamples(kUsagePatternMetric), |
| 402 | kDefaultTimeout); |
| 403 | EXPECT_METRIC_EQ( |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 404 | 1, webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint)); |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 405 | } |
| 406 | |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 407 | #ifndef WEBRTC_ANDROID |
| 408 | // These tests do not work on Android. Why is unclear. |
| 409 | // https://bugs.webrtc.org/9461 |
| 410 | |
| 411 | // Test getting the usage fingerprint for an audio/video connection. |
| 412 | TEST_F(PeerConnectionUsageHistogramTest, FingerprintAudioVideo) { |
| 413 | auto caller = CreatePeerConnection(); |
| 414 | auto callee = CreatePeerConnection(); |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 415 | caller->AddAudioTrack("audio"); |
| 416 | caller->AddVideoTrack("video"); |
Harald Alvestrand | 4238628 | 2018-07-12 05:56:05 | [diff] [blame] | 417 | ASSERT_TRUE(caller->ConnectTo(callee.get())); |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 418 | caller->pc()->Close(); |
| 419 | callee->pc()->Close(); |
| 420 | int expected_fingerprint = MakeUsageFingerprint( |
| 421 | {PeerConnection::UsageEvent::AUDIO_ADDED, |
| 422 | PeerConnection::UsageEvent::VIDEO_ADDED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 423 | PeerConnection::UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED, |
| 424 | PeerConnection::UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED, |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 425 | PeerConnection::UsageEvent::CANDIDATE_COLLECTED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 426 | PeerConnection::UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED, |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 427 | PeerConnection::UsageEvent::ICE_STATE_CONNECTED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 428 | PeerConnection::UsageEvent::REMOTE_CANDIDATE_ADDED, |
Qingsi Wang | cc46b10c | 2019-09-12 18:19:01 | [diff] [blame] | 429 | PeerConnection::UsageEvent::DIRECT_CONNECTION_SELECTED, |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 430 | PeerConnection::UsageEvent::CLOSE_CALLED}); |
Harald Alvestrand | 056d811 | 2018-07-16 17:18:58 | [diff] [blame] | 431 | // In this case, we may or may not have PRIVATE_CANDIDATE_COLLECTED, |
| 432 | // depending on the machine configuration. |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 433 | EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric)); |
| 434 | EXPECT_METRIC_TRUE( |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 435 | webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint) == |
| 436 | 2 || |
Harald Alvestrand | 056d811 | 2018-07-16 17:18:58 | [diff] [blame] | 437 | webrtc::metrics::NumEvents( |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 438 | kUsagePatternMetric, |
Harald Alvestrand | 056d811 | 2018-07-16 17:18:58 | [diff] [blame] | 439 | expected_fingerprint | |
| 440 | static_cast<int>( |
| 441 | PeerConnection::UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) == |
| 442 | 2); |
| 443 | } |
| 444 | |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 445 | // Test getting the usage fingerprint when the caller collects an mDNS |
| 446 | // candidate. |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 447 | TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithMdnsCaller) { |
| 448 | RTCConfiguration config; |
| 449 | |
| 450 | // Enable hostname candidates with mDNS names. |
| 451 | auto caller = CreatePeerConnectionWithMdns(config); |
| 452 | auto callee = CreatePeerConnection(config); |
| 453 | |
| 454 | caller->AddAudioTrack("audio"); |
| 455 | caller->AddVideoTrack("video"); |
| 456 | ASSERT_TRUE(caller->ConnectTo(callee.get())); |
| 457 | caller->pc()->Close(); |
| 458 | callee->pc()->Close(); |
| 459 | |
| 460 | int expected_fingerprint_caller = MakeUsageFingerprint( |
| 461 | {PeerConnection::UsageEvent::AUDIO_ADDED, |
| 462 | PeerConnection::UsageEvent::VIDEO_ADDED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 463 | PeerConnection::UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED, |
| 464 | PeerConnection::UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED, |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 465 | PeerConnection::UsageEvent::CANDIDATE_COLLECTED, |
| 466 | PeerConnection::UsageEvent::MDNS_CANDIDATE_COLLECTED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 467 | PeerConnection::UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED, |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 468 | PeerConnection::UsageEvent::ICE_STATE_CONNECTED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 469 | PeerConnection::UsageEvent::REMOTE_CANDIDATE_ADDED, |
Qingsi Wang | cc46b10c | 2019-09-12 18:19:01 | [diff] [blame] | 470 | PeerConnection::UsageEvent::DIRECT_CONNECTION_SELECTED, |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 471 | PeerConnection::UsageEvent::CLOSE_CALLED}); |
| 472 | |
Qingsi Wang | cc46b10c | 2019-09-12 18:19:01 | [diff] [blame] | 473 | // Without a resolver, the callee cannot resolve the received mDNS candidate |
| 474 | // but can still connect with the caller via a prflx candidate. As a result, |
| 475 | // the bit for the direct connection should not be logged. |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 476 | int expected_fingerprint_callee = MakeUsageFingerprint( |
| 477 | {PeerConnection::UsageEvent::AUDIO_ADDED, |
| 478 | PeerConnection::UsageEvent::VIDEO_ADDED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 479 | PeerConnection::UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED, |
| 480 | PeerConnection::UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED, |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 481 | PeerConnection::UsageEvent::CANDIDATE_COLLECTED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 482 | PeerConnection::UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED, |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 483 | PeerConnection::UsageEvent::REMOTE_MDNS_CANDIDATE_ADDED, |
| 484 | PeerConnection::UsageEvent::ICE_STATE_CONNECTED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 485 | PeerConnection::UsageEvent::REMOTE_CANDIDATE_ADDED, |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 486 | PeerConnection::UsageEvent::CLOSE_CALLED}); |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 487 | EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric)); |
| 488 | EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric, |
| 489 | expected_fingerprint_caller)); |
| 490 | EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric, |
| 491 | expected_fingerprint_callee)); |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 492 | } |
| 493 | |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 494 | // Test getting the usage fingerprint when the callee collects an mDNS |
| 495 | // candidate. |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 496 | TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithMdnsCallee) { |
| 497 | RTCConfiguration config; |
| 498 | |
| 499 | // Enable hostname candidates with mDNS names. |
| 500 | auto caller = CreatePeerConnection(config); |
| 501 | auto callee = CreatePeerConnectionWithMdns(config); |
| 502 | |
| 503 | caller->AddAudioTrack("audio"); |
| 504 | caller->AddVideoTrack("video"); |
| 505 | ASSERT_TRUE(caller->ConnectTo(callee.get())); |
| 506 | caller->pc()->Close(); |
| 507 | callee->pc()->Close(); |
| 508 | |
Qingsi Wang | cc46b10c | 2019-09-12 18:19:01 | [diff] [blame] | 509 | // Similar to the test above, the caller connects with the callee via a prflx |
| 510 | // candidate. |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 511 | int expected_fingerprint_caller = MakeUsageFingerprint( |
| 512 | {PeerConnection::UsageEvent::AUDIO_ADDED, |
| 513 | PeerConnection::UsageEvent::VIDEO_ADDED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 514 | PeerConnection::UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED, |
| 515 | PeerConnection::UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED, |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 516 | PeerConnection::UsageEvent::CANDIDATE_COLLECTED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 517 | PeerConnection::UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED, |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 518 | PeerConnection::UsageEvent::REMOTE_MDNS_CANDIDATE_ADDED, |
| 519 | PeerConnection::UsageEvent::ICE_STATE_CONNECTED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 520 | PeerConnection::UsageEvent::REMOTE_CANDIDATE_ADDED, |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 521 | PeerConnection::UsageEvent::CLOSE_CALLED}); |
| 522 | |
| 523 | int expected_fingerprint_callee = MakeUsageFingerprint( |
| 524 | {PeerConnection::UsageEvent::AUDIO_ADDED, |
| 525 | PeerConnection::UsageEvent::VIDEO_ADDED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 526 | PeerConnection::UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED, |
| 527 | PeerConnection::UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED, |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 528 | PeerConnection::UsageEvent::CANDIDATE_COLLECTED, |
| 529 | PeerConnection::UsageEvent::MDNS_CANDIDATE_COLLECTED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 530 | PeerConnection::UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED, |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 531 | PeerConnection::UsageEvent::ICE_STATE_CONNECTED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 532 | PeerConnection::UsageEvent::REMOTE_CANDIDATE_ADDED, |
Qingsi Wang | cc46b10c | 2019-09-12 18:19:01 | [diff] [blame] | 533 | PeerConnection::UsageEvent::DIRECT_CONNECTION_SELECTED, |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 534 | PeerConnection::UsageEvent::CLOSE_CALLED}); |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 535 | EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric)); |
| 536 | EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric, |
| 537 | expected_fingerprint_caller)); |
| 538 | EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric, |
| 539 | expected_fingerprint_callee)); |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 540 | } |
| 541 | |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 542 | #ifdef HAVE_SCTP |
| 543 | TEST_F(PeerConnectionUsageHistogramTest, FingerprintDataOnly) { |
| 544 | auto caller = CreatePeerConnection(); |
| 545 | auto callee = CreatePeerConnection(); |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 546 | caller->CreateDataChannel("foodata"); |
Harald Alvestrand | 4238628 | 2018-07-12 05:56:05 | [diff] [blame] | 547 | ASSERT_TRUE(caller->ConnectTo(callee.get())); |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 548 | ASSERT_TRUE_WAIT(callee->HaveDataChannel(), kDefaultTimeout); |
| 549 | caller->pc()->Close(); |
| 550 | callee->pc()->Close(); |
| 551 | int expected_fingerprint = MakeUsageFingerprint( |
| 552 | {PeerConnection::UsageEvent::DATA_ADDED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 553 | PeerConnection::UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED, |
| 554 | PeerConnection::UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED, |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 555 | PeerConnection::UsageEvent::CANDIDATE_COLLECTED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 556 | PeerConnection::UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED, |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 557 | PeerConnection::UsageEvent::ICE_STATE_CONNECTED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 558 | PeerConnection::UsageEvent::REMOTE_CANDIDATE_ADDED, |
Qingsi Wang | cc46b10c | 2019-09-12 18:19:01 | [diff] [blame] | 559 | PeerConnection::UsageEvent::DIRECT_CONNECTION_SELECTED, |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 560 | PeerConnection::UsageEvent::CLOSE_CALLED}); |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 561 | EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric)); |
| 562 | EXPECT_METRIC_TRUE( |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 563 | webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint) == |
| 564 | 2 || |
Harald Alvestrand | 056d811 | 2018-07-16 17:18:58 | [diff] [blame] | 565 | webrtc::metrics::NumEvents( |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 566 | kUsagePatternMetric, |
Harald Alvestrand | 056d811 | 2018-07-16 17:18:58 | [diff] [blame] | 567 | expected_fingerprint | |
| 568 | static_cast<int>( |
| 569 | PeerConnection::UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) == |
| 570 | 2); |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 571 | } |
| 572 | #endif // HAVE_SCTP |
| 573 | #endif // WEBRTC_ANDROID |
| 574 | |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 575 | TEST_F(PeerConnectionUsageHistogramTest, FingerprintStunTurn) { |
| 576 | RTCConfiguration configuration; |
| 577 | PeerConnection::IceServer server; |
| 578 | server.urls = {"stun:dummy.stun.server/"}; |
| 579 | configuration.servers.push_back(server); |
| 580 | server.urls = {"turn:dummy.turn.server/"}; |
| 581 | server.username = "username"; |
| 582 | server.password = "password"; |
| 583 | configuration.servers.push_back(server); |
| 584 | auto caller = CreatePeerConnection(configuration); |
| 585 | ASSERT_TRUE(caller); |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 586 | caller->pc()->Close(); |
| 587 | int expected_fingerprint = |
| 588 | MakeUsageFingerprint({PeerConnection::UsageEvent::STUN_SERVER_ADDED, |
| 589 | PeerConnection::UsageEvent::TURN_SERVER_ADDED, |
| 590 | PeerConnection::UsageEvent::CLOSE_CALLED}); |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 591 | EXPECT_METRIC_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric)); |
| 592 | EXPECT_METRIC_EQ( |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 593 | 1, webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint)); |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | TEST_F(PeerConnectionUsageHistogramTest, FingerprintStunTurnInReconfiguration) { |
| 597 | RTCConfiguration configuration; |
| 598 | PeerConnection::IceServer server; |
| 599 | server.urls = {"stun:dummy.stun.server/"}; |
| 600 | configuration.servers.push_back(server); |
| 601 | server.urls = {"turn:dummy.turn.server/"}; |
| 602 | server.username = "username"; |
| 603 | server.password = "password"; |
| 604 | configuration.servers.push_back(server); |
| 605 | auto caller = CreatePeerConnection(); |
| 606 | ASSERT_TRUE(caller); |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 607 | ASSERT_TRUE(caller->pc()->SetConfiguration(configuration).ok()); |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 608 | caller->pc()->Close(); |
| 609 | int expected_fingerprint = |
| 610 | MakeUsageFingerprint({PeerConnection::UsageEvent::STUN_SERVER_ADDED, |
| 611 | PeerConnection::UsageEvent::TURN_SERVER_ADDED, |
| 612 | PeerConnection::UsageEvent::CLOSE_CALLED}); |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 613 | EXPECT_METRIC_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric)); |
| 614 | EXPECT_METRIC_EQ( |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 615 | 1, webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint)); |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 616 | } |
| 617 | |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 618 | TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithPrivateIPCaller) { |
Harald Alvestrand | 056d811 | 2018-07-16 17:18:58 | [diff] [blame] | 619 | auto caller = CreatePeerConnectionWithPrivateLocalAddresses(); |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 620 | auto callee = CreatePeerConnection(); |
Harald Alvestrand | 056d811 | 2018-07-16 17:18:58 | [diff] [blame] | 621 | caller->AddAudioTrack("audio"); |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 622 | ASSERT_TRUE(caller->ConnectTo(callee.get())); |
Harald Alvestrand | 056d811 | 2018-07-16 17:18:58 | [diff] [blame] | 623 | caller->pc()->Close(); |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 624 | callee->pc()->Close(); |
| 625 | |
| 626 | int expected_fingerprint_caller = MakeUsageFingerprint( |
Harald Alvestrand | 056d811 | 2018-07-16 17:18:58 | [diff] [blame] | 627 | {PeerConnection::UsageEvent::AUDIO_ADDED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 628 | PeerConnection::UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED, |
| 629 | PeerConnection::UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED, |
Harald Alvestrand | 056d811 | 2018-07-16 17:18:58 | [diff] [blame] | 630 | PeerConnection::UsageEvent::CANDIDATE_COLLECTED, |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 631 | PeerConnection::UsageEvent::PRIVATE_CANDIDATE_COLLECTED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 632 | PeerConnection::UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED, |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 633 | PeerConnection::UsageEvent::ICE_STATE_CONNECTED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 634 | PeerConnection::UsageEvent::REMOTE_CANDIDATE_ADDED, |
Qingsi Wang | cc46b10c | 2019-09-12 18:19:01 | [diff] [blame] | 635 | PeerConnection::UsageEvent::DIRECT_CONNECTION_SELECTED, |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 636 | PeerConnection::UsageEvent::CLOSE_CALLED}); |
| 637 | |
| 638 | int expected_fingerprint_callee = MakeUsageFingerprint( |
| 639 | {PeerConnection::UsageEvent::AUDIO_ADDED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 640 | PeerConnection::UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED, |
| 641 | PeerConnection::UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED, |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 642 | PeerConnection::UsageEvent::CANDIDATE_COLLECTED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 643 | PeerConnection::UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED, |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 644 | PeerConnection::UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED, |
| 645 | PeerConnection::UsageEvent::ICE_STATE_CONNECTED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 646 | PeerConnection::UsageEvent::REMOTE_CANDIDATE_ADDED, |
Qingsi Wang | cc46b10c | 2019-09-12 18:19:01 | [diff] [blame] | 647 | PeerConnection::UsageEvent::DIRECT_CONNECTION_SELECTED, |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 648 | PeerConnection::UsageEvent::CLOSE_CALLED}); |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 649 | EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric)); |
| 650 | EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric, |
| 651 | expected_fingerprint_caller)); |
| 652 | EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric, |
| 653 | expected_fingerprint_callee)); |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 654 | } |
| 655 | |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 656 | TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithPrivateIpv6Callee) { |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 657 | auto caller = CreatePeerConnection(); |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 658 | auto callee = CreatePeerConnectionWithPrivateIpv6LocalAddresses(); |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 659 | caller->AddAudioTrack("audio"); |
| 660 | ASSERT_TRUE(caller->ConnectTo(callee.get())); |
| 661 | caller->pc()->Close(); |
| 662 | callee->pc()->Close(); |
| 663 | |
| 664 | int expected_fingerprint_caller = MakeUsageFingerprint( |
| 665 | {PeerConnection::UsageEvent::AUDIO_ADDED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 666 | PeerConnection::UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED, |
| 667 | PeerConnection::UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED, |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 668 | PeerConnection::UsageEvent::CANDIDATE_COLLECTED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 669 | PeerConnection::UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED, |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 670 | PeerConnection::UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED, |
| 671 | PeerConnection::UsageEvent::ICE_STATE_CONNECTED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 672 | PeerConnection::UsageEvent::REMOTE_CANDIDATE_ADDED, |
| 673 | PeerConnection::UsageEvent::REMOTE_IPV6_CANDIDATE_ADDED, |
Qingsi Wang | cc46b10c | 2019-09-12 18:19:01 | [diff] [blame] | 674 | PeerConnection::UsageEvent::DIRECT_CONNECTION_SELECTED, |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 675 | PeerConnection::UsageEvent::CLOSE_CALLED}); |
| 676 | |
| 677 | int expected_fingerprint_callee = MakeUsageFingerprint( |
| 678 | {PeerConnection::UsageEvent::AUDIO_ADDED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 679 | PeerConnection::UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED, |
| 680 | PeerConnection::UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED, |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 681 | PeerConnection::UsageEvent::CANDIDATE_COLLECTED, |
| 682 | PeerConnection::UsageEvent::PRIVATE_CANDIDATE_COLLECTED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 683 | PeerConnection::UsageEvent::IPV6_CANDIDATE_COLLECTED, |
| 684 | PeerConnection::UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED, |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 685 | PeerConnection::UsageEvent::REMOTE_CANDIDATE_ADDED, |
| 686 | PeerConnection::UsageEvent::ICE_STATE_CONNECTED, |
Qingsi Wang | cc46b10c | 2019-09-12 18:19:01 | [diff] [blame] | 687 | PeerConnection::UsageEvent::DIRECT_CONNECTION_SELECTED, |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 688 | PeerConnection::UsageEvent::CLOSE_CALLED}); |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 689 | EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric)); |
| 690 | EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric, |
| 691 | expected_fingerprint_caller)); |
| 692 | EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric, |
| 693 | expected_fingerprint_callee)); |
Harald Alvestrand | 056d811 | 2018-07-16 17:18:58 | [diff] [blame] | 694 | } |
| 695 | |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 696 | #ifndef WEBRTC_ANDROID |
| 697 | #ifdef HAVE_SCTP |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 698 | // Test that the usage pattern bits for adding remote (private IPv6) candidates |
| 699 | // are set when the remote candidates are retrieved from the Offer SDP instead |
| 700 | // of trickled ICE messages. |
| 701 | TEST_F(PeerConnectionUsageHistogramTest, |
| 702 | AddRemoteCandidatesFromRemoteDescription) { |
| 703 | // We construct the following data-channel-only scenario. The caller collects |
| 704 | // IPv6 private local candidates and appends them in the Offer as in |
| 705 | // non-trickled sessions. The callee collects mDNS candidates that are not |
| 706 | // contained in the Answer as in Trickle ICE. Only the Offer and Answer are |
| 707 | // signaled and we expect a connection with prflx remote candidates at the |
| 708 | // caller side. |
| 709 | auto caller = CreatePeerConnectionWithPrivateIpv6LocalAddresses(); |
| 710 | auto callee = CreatePeerConnectionWithMdns(RTCConfiguration()); |
| 711 | caller->CreateDataChannel("test_channel"); |
| 712 | ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer())); |
| 713 | // Wait until the gathering completes so that the session description would |
| 714 | // have contained ICE candidates. |
| 715 | EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete, |
| 716 | caller->ice_gathering_state(), kDefaultTimeout); |
| 717 | EXPECT_TRUE(caller->observer()->candidate_gathered()); |
| 718 | // Get the current offer that contains candidates and pass it to the callee. |
| 719 | // |
| 720 | // Note that we cannot use CloneSessionDescription on |cur_offer| to obtain an |
| 721 | // SDP with candidates. The method above does not strictly copy everything, in |
| 722 | // particular, not copying the ICE candidates. |
| 723 | // TODO(qingsi): Technically, this is a bug. Fix it. |
| 724 | auto cur_offer = caller->pc()->local_description(); |
| 725 | ASSERT_TRUE(cur_offer); |
| 726 | std::string sdp_with_candidates_str; |
| 727 | cur_offer->ToString(&sdp_with_candidates_str); |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 728 | auto offer = std::make_unique<JsepSessionDescription>(SdpType::kOffer); |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 729 | ASSERT_TRUE(SdpDeserialize(sdp_with_candidates_str, offer.get(), |
| 730 | nullptr /* error */)); |
| 731 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 732 | |
| 733 | // By default, the Answer created does not contain ICE candidates. |
| 734 | auto answer = callee->CreateAnswer(); |
| 735 | callee->SetLocalDescription(CloneSessionDescription(answer.get())); |
| 736 | caller->SetRemoteDescription(std::move(answer)); |
| 737 | EXPECT_TRUE_WAIT(caller->IsConnected(), kDefaultTimeout); |
| 738 | EXPECT_TRUE_WAIT(callee->IsConnected(), kDefaultTimeout); |
| 739 | // The callee needs to process the open message to have the data channel open. |
| 740 | EXPECT_TRUE_WAIT(callee->observer()->last_datachannel_ != nullptr, |
| 741 | kDefaultTimeout); |
| 742 | caller->pc()->Close(); |
| 743 | callee->pc()->Close(); |
| 744 | |
| 745 | // The caller should not have added any remote candidate either via |
Qingsi Wang | cc46b10c | 2019-09-12 18:19:01 | [diff] [blame] | 746 | // AddIceCandidate or from the remote description. Also, the caller connects |
| 747 | // with the callee via a prflx candidate and hence no direct connection bit |
| 748 | // should be set. |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 749 | int expected_fingerprint_caller = MakeUsageFingerprint( |
| 750 | {PeerConnection::UsageEvent::DATA_ADDED, |
| 751 | PeerConnection::UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED, |
| 752 | PeerConnection::UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED, |
| 753 | PeerConnection::UsageEvent::CANDIDATE_COLLECTED, |
| 754 | PeerConnection::UsageEvent::PRIVATE_CANDIDATE_COLLECTED, |
| 755 | PeerConnection::UsageEvent::IPV6_CANDIDATE_COLLECTED, |
| 756 | PeerConnection::UsageEvent::ICE_STATE_CONNECTED, |
| 757 | PeerConnection::UsageEvent::CLOSE_CALLED}); |
| 758 | |
| 759 | int expected_fingerprint_callee = MakeUsageFingerprint( |
| 760 | {PeerConnection::UsageEvent::DATA_ADDED, |
| 761 | PeerConnection::UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED, |
| 762 | PeerConnection::UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED, |
| 763 | PeerConnection::UsageEvent::CANDIDATE_COLLECTED, |
| 764 | PeerConnection::UsageEvent::MDNS_CANDIDATE_COLLECTED, |
| 765 | PeerConnection::UsageEvent::REMOTE_CANDIDATE_ADDED, |
| 766 | PeerConnection::UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED, |
| 767 | PeerConnection::UsageEvent::REMOTE_IPV6_CANDIDATE_ADDED, |
| 768 | PeerConnection::UsageEvent::ICE_STATE_CONNECTED, |
Qingsi Wang | cc46b10c | 2019-09-12 18:19:01 | [diff] [blame] | 769 | PeerConnection::UsageEvent::DIRECT_CONNECTION_SELECTED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 770 | PeerConnection::UsageEvent::CLOSE_CALLED}); |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 771 | EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric)); |
| 772 | EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric, |
| 773 | expected_fingerprint_caller)); |
| 774 | EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric, |
| 775 | expected_fingerprint_callee)); |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 776 | } |
| 777 | |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 778 | TEST_F(PeerConnectionUsageHistogramTest, NotableUsageNoted) { |
| 779 | auto caller = CreatePeerConnection(); |
| 780 | caller->CreateDataChannel("foo"); |
| 781 | caller->GenerateOfferAndCollectCandidates(); |
| 782 | caller->pc()->Close(); |
| 783 | int expected_fingerprint = MakeUsageFingerprint( |
| 784 | {PeerConnection::UsageEvent::DATA_ADDED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 785 | PeerConnection::UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED, |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 786 | PeerConnection::UsageEvent::CANDIDATE_COLLECTED, |
| 787 | PeerConnection::UsageEvent::CLOSE_CALLED}); |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 788 | EXPECT_METRIC_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric)); |
| 789 | EXPECT_METRIC_TRUE( |
| 790 | expected_fingerprint == ObservedFingerprint() || |
| 791 | (expected_fingerprint | |
| 792 | static_cast<int>( |
| 793 | PeerConnection::UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) == |
| 794 | ObservedFingerprint()); |
| 795 | EXPECT_METRIC_EQ(absl::make_optional(ObservedFingerprint()), |
| 796 | caller->observer()->interesting_usage_detected()); |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 797 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 798 | |
| 799 | TEST_F(PeerConnectionUsageHistogramTest, NotableUsageOnEventFiring) { |
| 800 | auto caller = CreatePeerConnection(); |
| 801 | caller->CreateDataChannel("foo"); |
| 802 | caller->GenerateOfferAndCollectCandidates(); |
| 803 | int expected_fingerprint = MakeUsageFingerprint( |
| 804 | {PeerConnection::UsageEvent::DATA_ADDED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 805 | PeerConnection::UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED, |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 806 | PeerConnection::UsageEvent::CANDIDATE_COLLECTED}); |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 807 | EXPECT_METRIC_EQ(0, webrtc::metrics::NumSamples(kUsagePatternMetric)); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 808 | caller->GetInternalPeerConnection()->RequestUsagePatternReportForTesting(); |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 809 | EXPECT_METRIC_EQ_WAIT(1, webrtc::metrics::NumSamples(kUsagePatternMetric), |
| 810 | kDefaultTimeout); |
| 811 | EXPECT_METRIC_TRUE( |
| 812 | expected_fingerprint == ObservedFingerprint() || |
| 813 | (expected_fingerprint | |
| 814 | static_cast<int>( |
| 815 | PeerConnection::UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) == |
| 816 | ObservedFingerprint()); |
| 817 | EXPECT_METRIC_EQ(absl::make_optional(ObservedFingerprint()), |
| 818 | caller->observer()->interesting_usage_detected()); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | TEST_F(PeerConnectionUsageHistogramTest, |
| 822 | NoNotableUsageOnEventFiringAfterClose) { |
| 823 | auto caller = CreatePeerConnection(); |
| 824 | caller->CreateDataChannel("foo"); |
| 825 | caller->GenerateOfferAndCollectCandidates(); |
| 826 | int expected_fingerprint = MakeUsageFingerprint( |
| 827 | {PeerConnection::UsageEvent::DATA_ADDED, |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 828 | PeerConnection::UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED, |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 829 | PeerConnection::UsageEvent::CANDIDATE_COLLECTED, |
| 830 | PeerConnection::UsageEvent::CLOSE_CALLED}); |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 831 | EXPECT_METRIC_EQ(0, webrtc::metrics::NumSamples(kUsagePatternMetric)); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 832 | caller->pc()->Close(); |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 833 | EXPECT_METRIC_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric)); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 834 | caller->GetInternalPeerConnection()->RequestUsagePatternReportForTesting(); |
| 835 | caller->observer()->ClearInterestingUsageDetector(); |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 836 | EXPECT_METRIC_EQ_WAIT(2, webrtc::metrics::NumSamples(kUsagePatternMetric), |
| 837 | kDefaultTimeout); |
| 838 | EXPECT_METRIC_TRUE( |
| 839 | expected_fingerprint == ObservedFingerprint() || |
| 840 | (expected_fingerprint | |
| 841 | static_cast<int>( |
| 842 | PeerConnection::UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) == |
| 843 | ObservedFingerprint()); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 844 | // After close, the usage-detection callback should NOT have been called. |
Ying Wang | ef3998f | 2019-12-09 12:06:53 | [diff] [blame] | 845 | EXPECT_METRIC_FALSE(caller->observer()->interesting_usage_detected()); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 846 | } |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 847 | #endif |
| 848 | #endif |
| 849 | |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 850 | } // namespace webrtc |