Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [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 | |
| 11 | #include <tuple> |
| 12 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 13 | #include "absl/memory/memory.h" |
Harald Alvestrand | 4238628 | 2018-07-12 07:56:05 +0200 | [diff] [blame] | 14 | #include "api/jsep.h" |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 15 | #include "api/peerconnectionproxy.h" |
| 16 | #include "media/base/fakemediaengine.h" |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 17 | #include "p2p/client/basicportallocator.h" |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 18 | #include "pc/mediasession.h" |
| 19 | #include "pc/peerconnection.h" |
| 20 | #include "pc/peerconnectionfactory.h" |
| 21 | #include "pc/peerconnectionwrapper.h" |
| 22 | #include "pc/sdputils.h" |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 23 | #include "pc/test/fakesctptransport.h" |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 24 | #include "rtc_base/fakenetwork.h" |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 25 | #include "rtc_base/gunit.h" |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 26 | #include "rtc_base/virtualsocketserver.h" |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 27 | #include "system_wrappers/include/metrics_default.h" |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 28 | |
| 29 | namespace webrtc { |
| 30 | |
| 31 | using RTCConfiguration = PeerConnectionInterface::RTCConfiguration; |
| 32 | using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions; |
| 33 | using ::testing::Values; |
| 34 | |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame^] | 35 | static const char kUsagePatternMetric[] = "WebRTC.PeerConnection.UsagePattern"; |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 36 | static constexpr int kDefaultTimeout = 10000; |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 37 | static const rtc::SocketAddress kDefaultLocalAddress("1.1.1.1", 0); |
| 38 | static const rtc::SocketAddress kPrivateLocalAddress("10.1.1.1", 0); |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 39 | |
| 40 | int MakeUsageFingerprint(std::set<PeerConnection::UsageEvent> events) { |
| 41 | int signature = 0; |
| 42 | for (const auto it : events) { |
| 43 | signature |= static_cast<int>(it); |
| 44 | } |
| 45 | return signature; |
| 46 | } |
| 47 | |
| 48 | class PeerConnectionFactoryForUsageHistogramTest |
| 49 | : public rtc::RefCountedObject<PeerConnectionFactory> { |
| 50 | public: |
| 51 | PeerConnectionFactoryForUsageHistogramTest() |
| 52 | : rtc::RefCountedObject<PeerConnectionFactory>( |
| 53 | rtc::Thread::Current(), |
| 54 | rtc::Thread::Current(), |
| 55 | rtc::Thread::Current(), |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 56 | absl::make_unique<cricket::FakeMediaEngine>(), |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 57 | CreateCallFactory(), |
| 58 | nullptr) {} |
| 59 | |
| 60 | void ActionsBeforeInitializeForTesting(PeerConnectionInterface* pc) override { |
| 61 | PeerConnection* internal_pc = static_cast<PeerConnection*>(pc); |
| 62 | if (return_histogram_very_quickly_) { |
| 63 | internal_pc->ReturnHistogramVeryQuicklyForTesting(); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void ReturnHistogramVeryQuickly() { return_histogram_very_quickly_ = true; } |
| 68 | |
| 69 | private: |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 70 | bool return_histogram_very_quickly_ = false; |
| 71 | }; |
| 72 | |
| 73 | class PeerConnectionWrapperForUsageHistogramTest; |
| 74 | typedef PeerConnectionWrapperForUsageHistogramTest* RawWrapperPtr; |
| 75 | |
| 76 | class ObserverForUsageHistogramTest : public MockPeerConnectionObserver { |
| 77 | public: |
| 78 | void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override; |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame^] | 79 | |
| 80 | void OnInterestingUsage(int usage_pattern) override { |
| 81 | interesting_usage_detected_ = usage_pattern; |
| 82 | } |
| 83 | |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 84 | void PrepareToExchangeCandidates(RawWrapperPtr other) { |
| 85 | candidate_target_ = other; |
| 86 | } |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame^] | 87 | |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 88 | bool HaveDataChannel() { return last_datachannel_; } |
| 89 | |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame^] | 90 | absl::optional<int> interesting_usage_detected() { |
| 91 | return interesting_usage_detected_; |
| 92 | } |
| 93 | |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 94 | private: |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame^] | 95 | absl::optional<int> interesting_usage_detected_; |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 96 | RawWrapperPtr candidate_target_; // Note: Not thread-safe against deletions. |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | class PeerConnectionWrapperForUsageHistogramTest |
| 100 | : public PeerConnectionWrapper { |
| 101 | public: |
| 102 | using PeerConnectionWrapper::PeerConnectionWrapper; |
| 103 | |
| 104 | PeerConnection* GetInternalPeerConnection() { |
| 105 | auto* pci = |
| 106 | static_cast<PeerConnectionProxyWithInternal<PeerConnectionInterface>*>( |
| 107 | pc()); |
| 108 | return static_cast<PeerConnection*>(pci->internal()); |
| 109 | } |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 110 | |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame^] | 111 | // Override with different return type |
| 112 | ObserverForUsageHistogramTest* observer() { |
| 113 | return static_cast<ObserverForUsageHistogramTest*>( |
| 114 | PeerConnectionWrapper::observer()); |
| 115 | } |
| 116 | |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 117 | void PrepareToExchangeCandidates( |
| 118 | PeerConnectionWrapperForUsageHistogramTest* other) { |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame^] | 119 | observer()->PrepareToExchangeCandidates(other); |
| 120 | other->observer()->PrepareToExchangeCandidates(this); |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | bool IsConnected() { |
| 124 | return pc()->ice_connection_state() == |
| 125 | PeerConnectionInterface::kIceConnectionConnected || |
| 126 | pc()->ice_connection_state() == |
| 127 | PeerConnectionInterface::kIceConnectionCompleted; |
| 128 | } |
| 129 | |
| 130 | bool HaveDataChannel() { |
| 131 | return static_cast<ObserverForUsageHistogramTest*>(observer()) |
| 132 | ->HaveDataChannel(); |
| 133 | } |
Harald Alvestrand | 4238628 | 2018-07-12 07:56:05 +0200 | [diff] [blame] | 134 | void AddOrBufferIceCandidate(const webrtc::IceCandidateInterface* candidate) { |
| 135 | if (!pc()->AddIceCandidate(candidate)) { |
| 136 | std::string sdp; |
| 137 | EXPECT_TRUE(candidate->ToString(&sdp)); |
| 138 | std::unique_ptr<webrtc::IceCandidateInterface> candidate_copy( |
| 139 | CreateIceCandidate(candidate->sdp_mid(), candidate->sdp_mline_index(), |
| 140 | sdp, nullptr)); |
| 141 | buffered_candidates_.push_back(std::move(candidate_copy)); |
| 142 | } |
| 143 | } |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 144 | |
Harald Alvestrand | 4238628 | 2018-07-12 07:56:05 +0200 | [diff] [blame] | 145 | void AddBufferedIceCandidates() { |
| 146 | for (const auto& candidate : buffered_candidates_) { |
| 147 | EXPECT_TRUE(pc()->AddIceCandidate(candidate.get())); |
| 148 | } |
| 149 | buffered_candidates_.clear(); |
| 150 | } |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 151 | |
Harald Alvestrand | 4238628 | 2018-07-12 07:56:05 +0200 | [diff] [blame] | 152 | bool ConnectTo(PeerConnectionWrapperForUsageHistogramTest* callee) { |
| 153 | PrepareToExchangeCandidates(callee); |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 154 | if (!ExchangeOfferAnswerWith(callee)) { |
| 155 | return false; |
| 156 | } |
Harald Alvestrand | 4238628 | 2018-07-12 07:56:05 +0200 | [diff] [blame] | 157 | AddBufferedIceCandidates(); |
| 158 | callee->AddBufferedIceCandidates(); |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 159 | WAIT(IsConnected(), kDefaultTimeout); |
| 160 | WAIT(callee->IsConnected(), kDefaultTimeout); |
Harald Alvestrand | 4238628 | 2018-07-12 07:56:05 +0200 | [diff] [blame] | 161 | return IsConnected() && callee->IsConnected(); |
| 162 | } |
| 163 | |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 164 | bool GenerateOfferAndCollectCandidates() { |
| 165 | auto offer = CreateOffer(RTCOfferAnswerOptions()); |
| 166 | if (!offer) { |
| 167 | return false; |
| 168 | } |
| 169 | bool set_local_offer = |
| 170 | SetLocalDescription(CloneSessionDescription(offer.get())); |
| 171 | EXPECT_TRUE(set_local_offer); |
| 172 | if (!set_local_offer) { |
| 173 | return false; |
| 174 | } |
| 175 | EXPECT_TRUE_WAIT(observer()->ice_gathering_complete_, kDefaultTimeout); |
| 176 | return true; |
| 177 | } |
| 178 | |
Harald Alvestrand | 4238628 | 2018-07-12 07:56:05 +0200 | [diff] [blame] | 179 | private: |
| 180 | // Candidates that have been sent but not yet configured |
| 181 | std::vector<std::unique_ptr<webrtc::IceCandidateInterface>> |
| 182 | buffered_candidates_; |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 183 | }; |
| 184 | |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 185 | void ObserverForUsageHistogramTest::OnIceCandidate( |
| 186 | const webrtc::IceCandidateInterface* candidate) { |
| 187 | if (candidate_target_) { |
Harald Alvestrand | 4238628 | 2018-07-12 07:56:05 +0200 | [diff] [blame] | 188 | this->candidate_target_->AddOrBufferIceCandidate(candidate); |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 189 | } |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 190 | // If target is not set, ignore. This happens in one-ended unit tests. |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 191 | } |
| 192 | |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 193 | class PeerConnectionUsageHistogramTest : public ::testing::Test { |
| 194 | protected: |
| 195 | typedef std::unique_ptr<PeerConnectionWrapperForUsageHistogramTest> |
| 196 | WrapperPtr; |
| 197 | |
| 198 | PeerConnectionUsageHistogramTest() |
| 199 | : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 200 | webrtc::metrics::Reset(); |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | WrapperPtr CreatePeerConnection() { |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 204 | return CreatePeerConnection(RTCConfiguration(), |
| 205 | PeerConnectionFactoryInterface::Options(), |
| 206 | nullptr, false); |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 209 | WrapperPtr CreatePeerConnection(const RTCConfiguration& config) { |
| 210 | return CreatePeerConnection( |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 211 | config, PeerConnectionFactoryInterface::Options(), nullptr, false); |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 212 | } |
| 213 | |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 214 | WrapperPtr CreatePeerConnectionWithImmediateReport() { |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 215 | return CreatePeerConnection(RTCConfiguration(), |
| 216 | PeerConnectionFactoryInterface::Options(), |
| 217 | nullptr, true); |
| 218 | } |
| 219 | |
| 220 | WrapperPtr CreatePeerConnectionWithPrivateLocalAddresses() { |
| 221 | fake_network_manager_.reset(new rtc::FakeNetworkManager()); |
| 222 | fake_network_manager_->AddInterface(kDefaultLocalAddress); |
| 223 | fake_network_manager_->AddInterface(kPrivateLocalAddress); |
| 224 | std::unique_ptr<cricket::BasicPortAllocator> port_allocator( |
| 225 | new cricket::BasicPortAllocator(fake_network_manager_.get())); |
| 226 | return CreatePeerConnection(RTCConfiguration(), |
| 227 | PeerConnectionFactoryInterface::Options(), |
| 228 | std::move(port_allocator), false); |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | WrapperPtr CreatePeerConnection( |
| 232 | const RTCConfiguration& config, |
| 233 | const PeerConnectionFactoryInterface::Options factory_options, |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 234 | std::unique_ptr<cricket::PortAllocator> allocator, |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 235 | bool immediate_report) { |
| 236 | rtc::scoped_refptr<PeerConnectionFactoryForUsageHistogramTest> pc_factory( |
| 237 | new PeerConnectionFactoryForUsageHistogramTest()); |
| 238 | pc_factory->SetOptions(factory_options); |
| 239 | RTC_CHECK(pc_factory->Initialize()); |
| 240 | if (immediate_report) { |
| 241 | pc_factory->ReturnHistogramVeryQuickly(); |
| 242 | } |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 243 | auto observer = absl::make_unique<ObserverForUsageHistogramTest>(); |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 244 | auto pc = pc_factory->CreatePeerConnection(config, std::move(allocator), |
| 245 | nullptr, observer.get()); |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 246 | if (!pc) { |
| 247 | return nullptr; |
| 248 | } |
| 249 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 250 | auto wrapper = |
| 251 | absl::make_unique<PeerConnectionWrapperForUsageHistogramTest>( |
| 252 | pc_factory, pc, std::move(observer)); |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 253 | return wrapper; |
| 254 | } |
| 255 | |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame^] | 256 | int ObservedFingerprint() { |
| 257 | // This works correctly only if there is only one sample value |
| 258 | // that has been counted. |
| 259 | // Returns -1 for "not found". |
| 260 | return webrtc::metrics::MinSample(kUsagePatternMetric); |
| 261 | } |
| 262 | |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 263 | std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_; |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 264 | std::unique_ptr<rtc::VirtualSocketServer> vss_; |
| 265 | rtc::AutoSocketServerThread main_; |
| 266 | }; |
| 267 | |
| 268 | TEST_F(PeerConnectionUsageHistogramTest, UsageFingerprintHistogramFromTimeout) { |
| 269 | auto pc = CreatePeerConnectionWithImmediateReport(); |
| 270 | |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 271 | int expected_fingerprint = MakeUsageFingerprint({}); |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame^] | 272 | ASSERT_EQ_WAIT(1, webrtc::metrics::NumSamples(kUsagePatternMetric), |
| 273 | kDefaultTimeout); |
| 274 | EXPECT_EQ( |
| 275 | 1, webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint)); |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 276 | } |
| 277 | |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 278 | #ifndef WEBRTC_ANDROID |
| 279 | // These tests do not work on Android. Why is unclear. |
| 280 | // https://bugs.webrtc.org/9461 |
| 281 | |
| 282 | // Test getting the usage fingerprint for an audio/video connection. |
| 283 | TEST_F(PeerConnectionUsageHistogramTest, FingerprintAudioVideo) { |
| 284 | auto caller = CreatePeerConnection(); |
| 285 | auto callee = CreatePeerConnection(); |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 286 | caller->AddAudioTrack("audio"); |
| 287 | caller->AddVideoTrack("video"); |
Harald Alvestrand | 4238628 | 2018-07-12 07:56:05 +0200 | [diff] [blame] | 288 | ASSERT_TRUE(caller->ConnectTo(callee.get())); |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 289 | caller->pc()->Close(); |
| 290 | callee->pc()->Close(); |
| 291 | int expected_fingerprint = MakeUsageFingerprint( |
| 292 | {PeerConnection::UsageEvent::AUDIO_ADDED, |
| 293 | PeerConnection::UsageEvent::VIDEO_ADDED, |
| 294 | PeerConnection::UsageEvent::SET_LOCAL_DESCRIPTION_CALLED, |
| 295 | PeerConnection::UsageEvent::SET_REMOTE_DESCRIPTION_CALLED, |
| 296 | PeerConnection::UsageEvent::CANDIDATE_COLLECTED, |
| 297 | PeerConnection::UsageEvent::REMOTE_CANDIDATE_ADDED, |
| 298 | PeerConnection::UsageEvent::ICE_STATE_CONNECTED, |
| 299 | PeerConnection::UsageEvent::CLOSE_CALLED}); |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 300 | // In this case, we may or may not have PRIVATE_CANDIDATE_COLLECTED, |
| 301 | // depending on the machine configuration. |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame^] | 302 | EXPECT_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric)); |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 303 | EXPECT_TRUE( |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame^] | 304 | webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint) == |
| 305 | 2 || |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 306 | webrtc::metrics::NumEvents( |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame^] | 307 | kUsagePatternMetric, |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 308 | expected_fingerprint | |
| 309 | static_cast<int>( |
| 310 | PeerConnection::UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) == |
| 311 | 2); |
| 312 | } |
| 313 | |
| 314 | // Test getting the usage fingerprint when there are no host candidates. |
| 315 | TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithNoHostCandidates) { |
| 316 | RTCConfiguration config; |
| 317 | config.type = PeerConnectionInterface::kNoHost; |
| 318 | auto caller = CreatePeerConnection(config); |
| 319 | auto callee = CreatePeerConnection(config); |
| 320 | caller->AddAudioTrack("audio"); |
| 321 | caller->AddVideoTrack("video"); |
| 322 | // Under some bot configurations, this will fail - presumably bots where |
| 323 | // no working non-host addresses exist. |
| 324 | if (!caller->ConnectTo(callee.get())) { |
| 325 | return; |
| 326 | } |
| 327 | // If we manage to connect, we should get this precise fingerprint. |
| 328 | caller->pc()->Close(); |
| 329 | callee->pc()->Close(); |
| 330 | int expected_fingerprint = MakeUsageFingerprint( |
| 331 | {PeerConnection::UsageEvent::AUDIO_ADDED, |
| 332 | PeerConnection::UsageEvent::VIDEO_ADDED, |
| 333 | PeerConnection::UsageEvent::SET_LOCAL_DESCRIPTION_CALLED, |
| 334 | PeerConnection::UsageEvent::SET_REMOTE_DESCRIPTION_CALLED, |
| 335 | PeerConnection::UsageEvent::CANDIDATE_COLLECTED, |
| 336 | PeerConnection::UsageEvent::REMOTE_CANDIDATE_ADDED, |
| 337 | PeerConnection::UsageEvent::ICE_STATE_CONNECTED, |
| 338 | PeerConnection::UsageEvent::CLOSE_CALLED}); |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame^] | 339 | EXPECT_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric)); |
| 340 | EXPECT_EQ( |
| 341 | 2, webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint)); |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | #ifdef HAVE_SCTP |
| 345 | TEST_F(PeerConnectionUsageHistogramTest, FingerprintDataOnly) { |
| 346 | auto caller = CreatePeerConnection(); |
| 347 | auto callee = CreatePeerConnection(); |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 348 | caller->CreateDataChannel("foodata"); |
Harald Alvestrand | 4238628 | 2018-07-12 07:56:05 +0200 | [diff] [blame] | 349 | ASSERT_TRUE(caller->ConnectTo(callee.get())); |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 350 | ASSERT_TRUE_WAIT(callee->HaveDataChannel(), kDefaultTimeout); |
| 351 | caller->pc()->Close(); |
| 352 | callee->pc()->Close(); |
| 353 | int expected_fingerprint = MakeUsageFingerprint( |
| 354 | {PeerConnection::UsageEvent::DATA_ADDED, |
| 355 | PeerConnection::UsageEvent::SET_LOCAL_DESCRIPTION_CALLED, |
| 356 | PeerConnection::UsageEvent::SET_REMOTE_DESCRIPTION_CALLED, |
| 357 | PeerConnection::UsageEvent::CANDIDATE_COLLECTED, |
| 358 | PeerConnection::UsageEvent::REMOTE_CANDIDATE_ADDED, |
| 359 | PeerConnection::UsageEvent::ICE_STATE_CONNECTED, |
| 360 | PeerConnection::UsageEvent::CLOSE_CALLED}); |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame^] | 361 | EXPECT_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric)); |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 362 | EXPECT_TRUE( |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame^] | 363 | webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint) == |
| 364 | 2 || |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 365 | webrtc::metrics::NumEvents( |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame^] | 366 | kUsagePatternMetric, |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 367 | expected_fingerprint | |
| 368 | static_cast<int>( |
| 369 | PeerConnection::UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) == |
| 370 | 2); |
Harald Alvestrand | 183e09d | 2018-06-28 12:04:41 +0200 | [diff] [blame] | 371 | } |
| 372 | #endif // HAVE_SCTP |
| 373 | #endif // WEBRTC_ANDROID |
| 374 | |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 375 | TEST_F(PeerConnectionUsageHistogramTest, FingerprintStunTurn) { |
| 376 | RTCConfiguration configuration; |
| 377 | PeerConnection::IceServer server; |
| 378 | server.urls = {"stun:dummy.stun.server/"}; |
| 379 | configuration.servers.push_back(server); |
| 380 | server.urls = {"turn:dummy.turn.server/"}; |
| 381 | server.username = "username"; |
| 382 | server.password = "password"; |
| 383 | configuration.servers.push_back(server); |
| 384 | auto caller = CreatePeerConnection(configuration); |
| 385 | ASSERT_TRUE(caller); |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 386 | caller->pc()->Close(); |
| 387 | int expected_fingerprint = |
| 388 | MakeUsageFingerprint({PeerConnection::UsageEvent::STUN_SERVER_ADDED, |
| 389 | PeerConnection::UsageEvent::TURN_SERVER_ADDED, |
| 390 | PeerConnection::UsageEvent::CLOSE_CALLED}); |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame^] | 391 | EXPECT_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric)); |
| 392 | EXPECT_EQ( |
| 393 | 1, webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint)); |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | TEST_F(PeerConnectionUsageHistogramTest, FingerprintStunTurnInReconfiguration) { |
| 397 | RTCConfiguration configuration; |
| 398 | PeerConnection::IceServer server; |
| 399 | server.urls = {"stun:dummy.stun.server/"}; |
| 400 | configuration.servers.push_back(server); |
| 401 | server.urls = {"turn:dummy.turn.server/"}; |
| 402 | server.username = "username"; |
| 403 | server.password = "password"; |
| 404 | configuration.servers.push_back(server); |
| 405 | auto caller = CreatePeerConnection(); |
| 406 | ASSERT_TRUE(caller); |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 407 | RTCError error; |
| 408 | caller->pc()->SetConfiguration(configuration, &error); |
| 409 | ASSERT_TRUE(error.ok()); |
| 410 | caller->pc()->Close(); |
| 411 | int expected_fingerprint = |
| 412 | MakeUsageFingerprint({PeerConnection::UsageEvent::STUN_SERVER_ADDED, |
| 413 | PeerConnection::UsageEvent::TURN_SERVER_ADDED, |
| 414 | PeerConnection::UsageEvent::CLOSE_CALLED}); |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame^] | 415 | EXPECT_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric)); |
| 416 | EXPECT_EQ( |
| 417 | 1, webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint)); |
Harald Alvestrand | b2a7478 | 2018-06-28 13:54:07 +0200 | [diff] [blame] | 418 | } |
| 419 | |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 420 | TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithPrivateIP) { |
| 421 | auto caller = CreatePeerConnectionWithPrivateLocalAddresses(); |
| 422 | caller->AddAudioTrack("audio"); |
| 423 | ASSERT_TRUE(caller->GenerateOfferAndCollectCandidates()); |
| 424 | caller->pc()->Close(); |
| 425 | int expected_fingerprint = MakeUsageFingerprint( |
| 426 | {PeerConnection::UsageEvent::AUDIO_ADDED, |
| 427 | PeerConnection::UsageEvent::SET_LOCAL_DESCRIPTION_CALLED, |
| 428 | PeerConnection::UsageEvent::CANDIDATE_COLLECTED, |
| 429 | PeerConnection::UsageEvent::CLOSE_CALLED, |
| 430 | PeerConnection::UsageEvent::PRIVATE_CANDIDATE_COLLECTED}); |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame^] | 431 | EXPECT_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric)); |
| 432 | EXPECT_EQ( |
| 433 | 1, webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint)); |
Harald Alvestrand | 056d811 | 2018-07-16 19:18:58 +0200 | [diff] [blame] | 434 | } |
| 435 | |
Harald Alvestrand | c0e9725 | 2018-07-26 10:39:55 +0200 | [diff] [blame^] | 436 | #ifndef WEBRTC_ANDROID |
| 437 | #ifdef HAVE_SCTP |
| 438 | TEST_F(PeerConnectionUsageHistogramTest, NotableUsageNoted) { |
| 439 | auto caller = CreatePeerConnection(); |
| 440 | caller->CreateDataChannel("foo"); |
| 441 | caller->GenerateOfferAndCollectCandidates(); |
| 442 | caller->pc()->Close(); |
| 443 | int expected_fingerprint = MakeUsageFingerprint( |
| 444 | {PeerConnection::UsageEvent::DATA_ADDED, |
| 445 | PeerConnection::UsageEvent::SET_LOCAL_DESCRIPTION_CALLED, |
| 446 | PeerConnection::UsageEvent::CANDIDATE_COLLECTED, |
| 447 | PeerConnection::UsageEvent::CLOSE_CALLED}); |
| 448 | EXPECT_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric)); |
| 449 | EXPECT_TRUE(expected_fingerprint == ObservedFingerprint() || |
| 450 | (expected_fingerprint | |
| 451 | static_cast<int>( |
| 452 | PeerConnection::UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) == |
| 453 | ObservedFingerprint()); |
| 454 | EXPECT_EQ(absl::make_optional(ObservedFingerprint()), |
| 455 | caller->observer()->interesting_usage_detected()); |
| 456 | } |
| 457 | #endif |
| 458 | #endif |
| 459 | |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 460 | } // namespace webrtc |