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