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