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