blob: 8837ab0ecead328bbb0cd95f76e2d8f0ae44af42 [file] [log] [blame]
Harald Alvestrand19793842018-06-25 12:03:50 +02001/*
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 Gerey3e707812018-11-28 16:47:49 +010011#include <memory>
12#include <set>
13#include <string>
14#include <utility>
15#include <vector>
Harald Alvestrand19793842018-06-25 12:03:50 +020016
Yves Gerey3e707812018-11-28 16:47:49 +010017#include "absl/types/optional.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000018#include "api/async_resolver_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "api/call/call_factory_interface.h"
Harald Alvestrand42386282018-07-12 07:56:05 +020020#include "api/jsep.h"
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070021#include "api/jsep_session_description.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "api/peer_connection_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "api/rtc_error.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010024#include "api/scoped_refptr.h"
Danil Chapovalov53d45ba2019-07-03 14:56:33 +020025#include "api/task_queue/default_task_queue_factory.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000026#include "api/task_queue/task_queue_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "media/base/fake_media_engine.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000028#include "media/base/media_engine.h"
Jeroen de Borstaf242c82019-04-24 13:13:48 -070029#include "p2p/base/mock_async_resolver.h"
Steve Anton10542f22019-01-11 09:11:00 -080030#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 Handella1b82012021-05-26 18:56:30 +020034#include "pc/peer_connection_proxy.h"
Steve Anton10542f22019-01-11 09:11:00 -080035#include "pc/peer_connection_wrapper.h"
36#include "pc/sdp_utils.h"
37#include "pc/test/mock_peer_connection_observers.h"
Harald Alvestrand44d0dff2020-10-09 05:43:53 +000038#include "pc/usage_pattern.h"
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070039#include "pc/webrtc_sdp.h"
Jeroen de Borstaf242c82019-04-24 13:13:48 -070040#include "rtc_base/arraysize.h"
Yves Gerey3e707812018-11-28 16:47:49 +010041#include "rtc_base/checks.h"
Qingsi Wangecd30542019-05-22 14:34:56 -070042#include "rtc_base/fake_mdns_responder.h"
Steve Anton10542f22019-01-11 09:11:00 -080043#include "rtc_base/fake_network.h"
Harald Alvestrand19793842018-06-25 12:03:50 +020044#include "rtc_base/gunit.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000045#include "rtc_base/mdns_responder_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080046#include "rtc_base/ref_counted_object.h"
Steve Anton10542f22019-01-11 09:11:00 -080047#include "rtc_base/socket_address.h"
Yves Gerey3e707812018-11-28 16:47:49 +010048#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080049#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020050#include "system_wrappers/include/metrics.h"
Jeroen de Borstaf242c82019-04-24 13:13:48 -070051#include "test/gmock.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000052#include "test/gtest.h"
Harald Alvestrand19793842018-06-25 12:03:50 +020053
54namespace webrtc {
55
56using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
57using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
Jeroen de Borstaf242c82019-04-24 13:13:48 -070058using ::testing::NiceMock;
Harald Alvestrand19793842018-06-25 12:03:50 +020059using ::testing::Values;
60
Harald Alvestrandc0e97252018-07-26 10:39:55 +020061static const char kUsagePatternMetric[] = "WebRTC.PeerConnection.UsagePattern";
Harald Alvestrand19793842018-06-25 12:03:50 +020062static constexpr int kDefaultTimeout = 10000;
Jeroen de Borstaf242c82019-04-24 13:13:48 -070063static const rtc::SocketAddress kLocalAddrs[2] = {
64 rtc::SocketAddress("1.1.1.1", 0), rtc::SocketAddress("2.2.2.2", 0)};
Harald Alvestrand056d8112018-07-16 19:18:58 +020065static const rtc::SocketAddress kPrivateLocalAddress("10.1.1.1", 0);
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070066static const rtc::SocketAddress kPrivateIpv6LocalAddress("fd12:3456:789a:1::1",
67 0);
Harald Alvestrand19793842018-06-25 12:03:50 +020068
Harald Alvestrand44d0dff2020-10-09 05:43:53 +000069int MakeUsageFingerprint(std::set<UsageEvent> events) {
Harald Alvestrand19793842018-06-25 12:03:50 +020070 int signature = 0;
71 for (const auto it : events) {
72 signature |= static_cast<int>(it);
73 }
74 return signature;
75}
76
77class PeerConnectionFactoryForUsageHistogramTest
Niels Möllerdf411872022-03-24 16:13:25 +010078 : public PeerConnectionFactory {
Harald Alvestrand19793842018-06-25 12:03:50 +020079 public:
80 PeerConnectionFactoryForUsageHistogramTest()
Niels Möllerdf411872022-03-24 16:13:25 +010081 : PeerConnectionFactory([] {
Danil Chapovalovf5258be2019-03-19 17:45:24 +010082 PeerConnectionFactoryDependencies dependencies;
83 dependencies.network_thread = rtc::Thread::Current();
84 dependencies.worker_thread = rtc::Thread::Current();
85 dependencies.signaling_thread = rtc::Thread::Current();
Danil Chapovalov53d45ba2019-07-03 14:56:33 +020086 dependencies.task_queue_factory = CreateDefaultTaskQueueFactory();
Danil Chapovalovf5258be2019-03-19 17:45:24 +010087 dependencies.media_engine =
Mirko Bonadei317a1f02019-09-17 17:06:18 +020088 std::make_unique<cricket::FakeMediaEngine>();
Danil Chapovalovf5258be2019-03-19 17:45:24 +010089 dependencies.call_factory = CreateCallFactory();
90 return dependencies;
91 }()) {}
Harald Alvestrand183e09d2018-06-28 12:04:41 +020092};
93
94class PeerConnectionWrapperForUsageHistogramTest;
Yves Gerey3e707812018-11-28 16:47:49 +010095
Harald Alvestrand183e09d2018-06-28 12:04:41 +020096typedef PeerConnectionWrapperForUsageHistogramTest* RawWrapperPtr;
97
98class ObserverForUsageHistogramTest : public MockPeerConnectionObserver {
99 public:
100 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override;
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200101
102 void OnInterestingUsage(int usage_pattern) override {
103 interesting_usage_detected_ = usage_pattern;
104 }
105
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200106 void PrepareToExchangeCandidates(RawWrapperPtr other) {
107 candidate_target_ = other;
108 }
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200109
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200110 bool HaveDataChannel() { return last_datachannel_; }
111
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200112 absl::optional<int> interesting_usage_detected() {
113 return interesting_usage_detected_;
114 }
115
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200116 void ClearInterestingUsageDetector() {
117 interesting_usage_detected_ = absl::optional<int>();
118 }
119
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700120 bool candidate_gathered() const { return candidate_gathered_; }
121
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200122 private:
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200123 absl::optional<int> interesting_usage_detected_;
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700124 bool candidate_gathered_ = false;
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200125 RawWrapperPtr candidate_target_; // Note: Not thread-safe against deletions.
Harald Alvestrand19793842018-06-25 12:03:50 +0200126};
127
128class 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 Alvestrand183e09d2018-06-28 12:04:41 +0200139
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200140 // Override with different return type
141 ObserverForUsageHistogramTest* observer() {
142 return static_cast<ObserverForUsageHistogramTest*>(
143 PeerConnectionWrapper::observer());
144 }
145
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200146 void PrepareToExchangeCandidates(
147 PeerConnectionWrapperForUsageHistogramTest* other) {
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200148 observer()->PrepareToExchangeCandidates(other);
149 other->observer()->PrepareToExchangeCandidates(this);
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200150 }
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 Wang32913c12019-10-30 16:03:46 -0700163 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 Alvestrand42386282018-07-12 07:56:05 +0200170 }
Harald Alvestrand056d8112018-07-16 19:18:58 +0200171
Harald Alvestrand42386282018-07-12 07:56:05 +0200172 void AddBufferedIceCandidates() {
173 for (const auto& candidate : buffered_candidates_) {
174 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
175 }
176 buffered_candidates_.clear();
177 }
Harald Alvestrand056d8112018-07-16 19:18:58 +0200178
Qingsi Wang32913c12019-10-30 16:03:46 -0700179 // 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 Alvestrand42386282018-07-12 07:56:05 +0200189 bool ConnectTo(PeerConnectionWrapperForUsageHistogramTest* callee) {
190 PrepareToExchangeCandidates(callee);
Harald Alvestrand056d8112018-07-16 19:18:58 +0200191 if (!ExchangeOfferAnswerWith(callee)) {
192 return false;
193 }
Qingsi Wang32913c12019-10-30 16:03:46 -0700194 // 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 Alvestrand42386282018-07-12 07:56:05 +0200197 AddBufferedIceCandidates();
198 callee->AddBufferedIceCandidates();
Harald Alvestrand056d8112018-07-16 19:18:58 +0200199 WAIT(IsConnected(), kDefaultTimeout);
200 WAIT(callee->IsConnected(), kDefaultTimeout);
Harald Alvestrand42386282018-07-12 07:56:05 +0200201 return IsConnected() && callee->IsConnected();
202 }
203
Harald Alvestrand056d8112018-07-16 19:18:58 +0200204 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 Wang1ba5dec2019-08-19 11:57:17 -0700219 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
220 return pc()->ice_gathering_state();
221 }
222
Harald Alvestrand42386282018-07-12 07:56:05 +0200223 private:
224 // Candidates that have been sent but not yet configured
225 std::vector<std::unique_ptr<webrtc::IceCandidateInterface>>
226 buffered_candidates_;
Harald Alvestrand19793842018-06-25 12:03:50 +0200227};
228
Qingsi Wang32913c12019-10-30 16:03:46 -0700229// Buffers candidates until we add them via AddBufferedIceCandidates.
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200230void ObserverForUsageHistogramTest::OnIceCandidate(
231 const webrtc::IceCandidateInterface* candidate) {
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700232 // If target is not set, ignore. This happens in one-ended unit tests.
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200233 if (candidate_target_) {
Qingsi Wang32913c12019-10-30 16:03:46 -0700234 this->candidate_target_->BufferIceCandidate(candidate);
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200235 }
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700236 candidate_gathered_ = true;
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200237}
238
Harald Alvestrand19793842018-06-25 12:03:50 +0200239class 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 Wang7fc821d2018-07-12 12:54:53 -0700246 webrtc::metrics::Reset();
Harald Alvestrand19793842018-06-25 12:03:50 +0200247 }
248
249 WrapperPtr CreatePeerConnection() {
Henrik Boström62995db2022-01-03 09:58:10 +0100250 RTCConfiguration config;
251 config.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan;
Harald Alvestrand62166932020-10-26 08:30:41 +0000252 return CreatePeerConnection(
Henrik Boström62995db2022-01-03 09:58:10 +0100253 config, PeerConnectionFactoryInterface::Options(), nullptr);
Harald Alvestrand19793842018-06-25 12:03:50 +0200254 }
255
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200256 WrapperPtr CreatePeerConnection(const RTCConfiguration& config) {
257 return CreatePeerConnection(
Harald Alvestrand62166932020-10-26 08:30:41 +0000258 config, PeerConnectionFactoryInterface::Options(), nullptr);
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200259 }
260
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700261 WrapperPtr CreatePeerConnectionWithMdns(const RTCConfiguration& config) {
262 auto resolver_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200263 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700264
265 webrtc::PeerConnectionDependencies deps(nullptr /* observer_in */);
266
267 auto fake_network = NewFakeNetwork();
Qingsi Wangecd30542019-05-22 14:34:56 -0700268 fake_network->set_mdns_responder(
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200269 std::make_unique<webrtc::FakeMdnsResponder>(rtc::Thread::Current()));
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700270 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 Alvestrand62166932020-10-26 08:30:41 +0000278 return CreatePeerConnection(
279 config, PeerConnectionFactoryInterface::Options(), std::move(deps));
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700280 }
281
Harald Alvestrand19793842018-06-25 12:03:50 +0200282 WrapperPtr CreatePeerConnectionWithImmediateReport() {
Harald Alvestrand62166932020-10-26 08:30:41 +0000283 RTCConfiguration configuration;
Henrik Boström62995db2022-01-03 09:58:10 +0100284 configuration.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan;
Harald Alvestrand62166932020-10-26 08:30:41 +0000285 configuration.report_usage_pattern_delay_ms = 0;
286 return CreatePeerConnection(
287 configuration, PeerConnectionFactoryInterface::Options(), nullptr);
Harald Alvestrand056d8112018-07-16 19:18:58 +0200288 }
289
290 WrapperPtr CreatePeerConnectionWithPrivateLocalAddresses() {
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700291 auto* fake_network = NewFakeNetwork();
292 fake_network->AddInterface(NextLocalAddress());
293 fake_network->AddInterface(kPrivateLocalAddress);
294
295 auto port_allocator =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200296 std::make_unique<cricket::BasicPortAllocator>(fake_network);
Henrik Boström62995db2022-01-03 09:58:10 +0100297 RTCConfiguration config;
298 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
299 return CreatePeerConnection(config,
Harald Alvestrand056d8112018-07-16 19:18:58 +0200300 PeerConnectionFactoryInterface::Options(),
Harald Alvestrand62166932020-10-26 08:30:41 +0000301 std::move(port_allocator));
Harald Alvestrand19793842018-06-25 12:03:50 +0200302 }
303
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700304 WrapperPtr CreatePeerConnectionWithPrivateIpv6LocalAddresses() {
305 auto* fake_network = NewFakeNetwork();
306 fake_network->AddInterface(NextLocalAddress());
307 fake_network->AddInterface(kPrivateIpv6LocalAddress);
308
309 auto port_allocator =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200310 std::make_unique<cricket::BasicPortAllocator>(fake_network);
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700311
Henrik Boström62995db2022-01-03 09:58:10 +0100312 RTCConfiguration config;
313 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
314 return CreatePeerConnection(config,
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700315 PeerConnectionFactoryInterface::Options(),
Harald Alvestrand62166932020-10-26 08:30:41 +0000316 std::move(port_allocator));
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700317 }
318
Harald Alvestrand19793842018-06-25 12:03:50 +0200319 WrapperPtr CreatePeerConnection(
320 const RTCConfiguration& config,
321 const PeerConnectionFactoryInterface::Options factory_options,
Harald Alvestrand62166932020-10-26 08:30:41 +0000322 std::unique_ptr<cricket::PortAllocator> allocator) {
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700323 PeerConnectionDependencies deps(nullptr);
324 deps.allocator = std::move(allocator);
325
Harald Alvestrand62166932020-10-26 08:30:41 +0000326 return CreatePeerConnection(config, factory_options, std::move(deps));
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700327 }
328
329 WrapperPtr CreatePeerConnection(
330 const RTCConfiguration& config,
331 const PeerConnectionFactoryInterface::Options factory_options,
Harald Alvestrand62166932020-10-26 08:30:41 +0000332 PeerConnectionDependencies deps) {
Niels Möllerdf411872022-03-24 16:13:25 +0100333 auto pc_factory =
334 rtc::make_ref_counted<PeerConnectionFactoryForUsageHistogramTest>();
Harald Alvestrand19793842018-06-25 12:03:50 +0200335 pc_factory->SetOptions(factory_options);
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700336
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 Bonadei317a1f02019-09-17 17:06:18 +0200343 std::make_unique<cricket::BasicPortAllocator>(fake_network);
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700344 }
345
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200346 auto observer = std::make_unique<ObserverForUsageHistogramTest>();
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700347 deps.observer = observer.get();
348
Florent Castelli72424402022-04-06 03:45:10 +0200349 auto result =
350 pc_factory->CreatePeerConnectionOrError(config, std::move(deps));
351 if (!result.ok()) {
Harald Alvestrand19793842018-06-25 12:03:50 +0200352 return nullptr;
353 }
354
Florent Castelli72424402022-04-06 03:45:10 +0200355 observer->SetPeerConnectionInterface(result.value());
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200356 auto wrapper = std::make_unique<PeerConnectionWrapperForUsageHistogramTest>(
Florent Castelli72424402022-04-06 03:45:10 +0200357 pc_factory, result.MoveValue(), std::move(observer));
Harald Alvestrand19793842018-06-25 12:03:50 +0200358 return wrapper;
359 }
360
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200361 int ObservedFingerprint() {
362 // This works correctly only if there is only one sample value
363 // that has been counted.
364 // Returns -1 for "not found".
365 return webrtc::metrics::MinSample(kUsagePatternMetric);
366 }
367
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700368 // The PeerConnection's port allocator is tied to the PeerConnection's
369 // lifetime and expects the underlying NetworkManager to outlive it. That
370 // prevents us from having the PeerConnectionWrapper own the fake network.
371 // Therefore, the test fixture will own all the fake networks even though
372 // tests should access the fake network through the PeerConnectionWrapper.
373 rtc::FakeNetworkManager* NewFakeNetwork() {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200374 fake_networks_.emplace_back(std::make_unique<rtc::FakeNetworkManager>());
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700375 return fake_networks_.back().get();
376 }
377
378 rtc::SocketAddress NextLocalAddress() {
379 RTC_DCHECK(next_local_address_ < (int)arraysize(kLocalAddrs));
380 return kLocalAddrs[next_local_address_++];
381 }
382
383 std::vector<std::unique_ptr<rtc::FakeNetworkManager>> fake_networks_;
384 int next_local_address_ = 0;
Harald Alvestrand19793842018-06-25 12:03:50 +0200385 std::unique_ptr<rtc::VirtualSocketServer> vss_;
386 rtc::AutoSocketServerThread main_;
387};
388
389TEST_F(PeerConnectionUsageHistogramTest, UsageFingerprintHistogramFromTimeout) {
390 auto pc = CreatePeerConnectionWithImmediateReport();
391
Harald Alvestrand19793842018-06-25 12:03:50 +0200392 int expected_fingerprint = MakeUsageFingerprint({});
Ying Wangef3998f2019-12-09 13:06:53 +0100393 EXPECT_METRIC_EQ_WAIT(1, webrtc::metrics::NumSamples(kUsagePatternMetric),
394 kDefaultTimeout);
395 EXPECT_METRIC_EQ(
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200396 1, webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint));
Harald Alvestrand19793842018-06-25 12:03:50 +0200397}
398
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200399#ifndef WEBRTC_ANDROID
400// These tests do not work on Android. Why is unclear.
401// https://bugs.webrtc.org/9461
402
403// Test getting the usage fingerprint for an audio/video connection.
404TEST_F(PeerConnectionUsageHistogramTest, FingerprintAudioVideo) {
405 auto caller = CreatePeerConnection();
406 auto callee = CreatePeerConnection();
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200407 caller->AddAudioTrack("audio");
408 caller->AddVideoTrack("video");
Harald Alvestrand42386282018-07-12 07:56:05 +0200409 ASSERT_TRUE(caller->ConnectTo(callee.get()));
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200410 caller->pc()->Close();
411 callee->pc()->Close();
412 int expected_fingerprint = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000413 {UsageEvent::AUDIO_ADDED, UsageEvent::VIDEO_ADDED,
414 UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
415 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
416 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
417 UsageEvent::ICE_STATE_CONNECTED, UsageEvent::REMOTE_CANDIDATE_ADDED,
418 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Harald Alvestrand056d8112018-07-16 19:18:58 +0200419 // In this case, we may or may not have PRIVATE_CANDIDATE_COLLECTED,
420 // depending on the machine configuration.
Ying Wangef3998f2019-12-09 13:06:53 +0100421 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
422 EXPECT_METRIC_TRUE(
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200423 webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint) ==
424 2 ||
Harald Alvestrand056d8112018-07-16 19:18:58 +0200425 webrtc::metrics::NumEvents(
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200426 kUsagePatternMetric,
Harald Alvestrand056d8112018-07-16 19:18:58 +0200427 expected_fingerprint |
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000428 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) == 2);
Harald Alvestrand056d8112018-07-16 19:18:58 +0200429}
430
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700431// Test getting the usage fingerprint when the caller collects an mDNS
432// candidate.
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700433TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithMdnsCaller) {
434 RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +0100435 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700436
437 // Enable hostname candidates with mDNS names.
438 auto caller = CreatePeerConnectionWithMdns(config);
439 auto callee = CreatePeerConnection(config);
440
441 caller->AddAudioTrack("audio");
442 caller->AddVideoTrack("video");
443 ASSERT_TRUE(caller->ConnectTo(callee.get()));
444 caller->pc()->Close();
445 callee->pc()->Close();
446
447 int expected_fingerprint_caller = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000448 {UsageEvent::AUDIO_ADDED, UsageEvent::VIDEO_ADDED,
449 UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
450 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
451 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::MDNS_CANDIDATE_COLLECTED,
452 UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED, UsageEvent::ICE_STATE_CONNECTED,
453 UsageEvent::REMOTE_CANDIDATE_ADDED,
454 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700455
Qingsi Wangcc46b102019-09-12 11:19:01 -0700456 // Without a resolver, the callee cannot resolve the received mDNS candidate
457 // but can still connect with the caller via a prflx candidate. As a result,
458 // the bit for the direct connection should not be logged.
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700459 int expected_fingerprint_callee = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000460 {UsageEvent::AUDIO_ADDED, UsageEvent::VIDEO_ADDED,
461 UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
462 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
463 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
464 UsageEvent::REMOTE_MDNS_CANDIDATE_ADDED, UsageEvent::ICE_STATE_CONNECTED,
465 UsageEvent::REMOTE_CANDIDATE_ADDED, UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100466 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
467 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
468 expected_fingerprint_caller));
469 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
470 expected_fingerprint_callee));
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700471}
472
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700473// Test getting the usage fingerprint when the callee collects an mDNS
474// candidate.
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700475TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithMdnsCallee) {
476 RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +0100477 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700478
479 // Enable hostname candidates with mDNS names.
480 auto caller = CreatePeerConnection(config);
481 auto callee = CreatePeerConnectionWithMdns(config);
482
483 caller->AddAudioTrack("audio");
484 caller->AddVideoTrack("video");
485 ASSERT_TRUE(caller->ConnectTo(callee.get()));
486 caller->pc()->Close();
487 callee->pc()->Close();
488
Qingsi Wangcc46b102019-09-12 11:19:01 -0700489 // Similar to the test above, the caller connects with the callee via a prflx
490 // candidate.
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700491 int expected_fingerprint_caller = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000492 {UsageEvent::AUDIO_ADDED, UsageEvent::VIDEO_ADDED,
493 UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
494 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
495 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
496 UsageEvent::REMOTE_MDNS_CANDIDATE_ADDED, UsageEvent::ICE_STATE_CONNECTED,
497 UsageEvent::REMOTE_CANDIDATE_ADDED, UsageEvent::CLOSE_CALLED});
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700498
499 int expected_fingerprint_callee = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000500 {UsageEvent::AUDIO_ADDED, UsageEvent::VIDEO_ADDED,
501 UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
502 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
503 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::MDNS_CANDIDATE_COLLECTED,
504 UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED, UsageEvent::ICE_STATE_CONNECTED,
505 UsageEvent::REMOTE_CANDIDATE_ADDED,
506 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100507 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
508 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
509 expected_fingerprint_caller));
510 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
511 expected_fingerprint_callee));
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700512}
513
Mirko Bonadei5eb43b42021-01-18 13:24:40 +0100514#ifdef WEBRTC_HAVE_SCTP
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200515TEST_F(PeerConnectionUsageHistogramTest, FingerprintDataOnly) {
516 auto caller = CreatePeerConnection();
517 auto callee = CreatePeerConnection();
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200518 caller->CreateDataChannel("foodata");
Harald Alvestrand42386282018-07-12 07:56:05 +0200519 ASSERT_TRUE(caller->ConnectTo(callee.get()));
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200520 ASSERT_TRUE_WAIT(callee->HaveDataChannel(), kDefaultTimeout);
521 caller->pc()->Close();
522 callee->pc()->Close();
523 int expected_fingerprint = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000524 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
525 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
526 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
527 UsageEvent::ICE_STATE_CONNECTED, UsageEvent::REMOTE_CANDIDATE_ADDED,
528 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100529 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
530 EXPECT_METRIC_TRUE(
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200531 webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint) ==
532 2 ||
Harald Alvestrand056d8112018-07-16 19:18:58 +0200533 webrtc::metrics::NumEvents(
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200534 kUsagePatternMetric,
Harald Alvestrand056d8112018-07-16 19:18:58 +0200535 expected_fingerprint |
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000536 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) == 2);
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200537}
Mirko Bonadei5eb43b42021-01-18 13:24:40 +0100538#endif // WEBRTC_HAVE_SCTP
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200539#endif // WEBRTC_ANDROID
540
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200541TEST_F(PeerConnectionUsageHistogramTest, FingerprintStunTurn) {
542 RTCConfiguration configuration;
Henrik Boström62995db2022-01-03 09:58:10 +0100543 configuration.sdp_semantics = SdpSemantics::kUnifiedPlan;
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200544 PeerConnection::IceServer server;
Harald Alvestranda3dd7722020-11-27 08:05:42 +0000545 server.urls = {"stun:dummy.stun.server"};
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200546 configuration.servers.push_back(server);
Harald Alvestranda3dd7722020-11-27 08:05:42 +0000547 server.urls = {"turn:dummy.turn.server"};
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200548 server.username = "username";
549 server.password = "password";
550 configuration.servers.push_back(server);
551 auto caller = CreatePeerConnection(configuration);
552 ASSERT_TRUE(caller);
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200553 caller->pc()->Close();
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000554 int expected_fingerprint = MakeUsageFingerprint(
555 {UsageEvent::STUN_SERVER_ADDED, UsageEvent::TURN_SERVER_ADDED,
556 UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100557 EXPECT_METRIC_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric));
558 EXPECT_METRIC_EQ(
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200559 1, webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint));
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200560}
561
562TEST_F(PeerConnectionUsageHistogramTest, FingerprintStunTurnInReconfiguration) {
563 RTCConfiguration configuration;
Henrik Boström62995db2022-01-03 09:58:10 +0100564 configuration.sdp_semantics = SdpSemantics::kUnifiedPlan;
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200565 PeerConnection::IceServer server;
Harald Alvestranda3dd7722020-11-27 08:05:42 +0000566 server.urls = {"stun:dummy.stun.server"};
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200567 configuration.servers.push_back(server);
Harald Alvestranda3dd7722020-11-27 08:05:42 +0000568 server.urls = {"turn:dummy.turn.server"};
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200569 server.username = "username";
570 server.password = "password";
571 configuration.servers.push_back(server);
572 auto caller = CreatePeerConnection();
573 ASSERT_TRUE(caller);
Niels Möller2579f0c2019-08-19 09:58:17 +0200574 ASSERT_TRUE(caller->pc()->SetConfiguration(configuration).ok());
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200575 caller->pc()->Close();
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000576 int expected_fingerprint = MakeUsageFingerprint(
577 {UsageEvent::STUN_SERVER_ADDED, UsageEvent::TURN_SERVER_ADDED,
578 UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100579 EXPECT_METRIC_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric));
580 EXPECT_METRIC_EQ(
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200581 1, webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint));
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200582}
583
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700584TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithPrivateIPCaller) {
Harald Alvestrand056d8112018-07-16 19:18:58 +0200585 auto caller = CreatePeerConnectionWithPrivateLocalAddresses();
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700586 auto callee = CreatePeerConnection();
Harald Alvestrand056d8112018-07-16 19:18:58 +0200587 caller->AddAudioTrack("audio");
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700588 ASSERT_TRUE(caller->ConnectTo(callee.get()));
Harald Alvestrand056d8112018-07-16 19:18:58 +0200589 caller->pc()->Close();
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700590 callee->pc()->Close();
591
592 int expected_fingerprint_caller = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000593 {UsageEvent::AUDIO_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
594 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
595 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::PRIVATE_CANDIDATE_COLLECTED,
596 UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED, UsageEvent::ICE_STATE_CONNECTED,
597 UsageEvent::REMOTE_CANDIDATE_ADDED,
598 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700599
600 int expected_fingerprint_callee = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000601 {UsageEvent::AUDIO_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
602 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
603 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
604 UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED,
605 UsageEvent::ICE_STATE_CONNECTED, UsageEvent::REMOTE_CANDIDATE_ADDED,
606 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100607 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
608 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
609 expected_fingerprint_caller));
610 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
611 expected_fingerprint_callee));
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700612}
613
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700614TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithPrivateIpv6Callee) {
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700615 auto caller = CreatePeerConnection();
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700616 auto callee = CreatePeerConnectionWithPrivateIpv6LocalAddresses();
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700617 caller->AddAudioTrack("audio");
618 ASSERT_TRUE(caller->ConnectTo(callee.get()));
619 caller->pc()->Close();
620 callee->pc()->Close();
621
622 int expected_fingerprint_caller = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000623 {UsageEvent::AUDIO_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
624 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
625 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
626 UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED,
627 UsageEvent::ICE_STATE_CONNECTED, UsageEvent::REMOTE_CANDIDATE_ADDED,
628 UsageEvent::REMOTE_IPV6_CANDIDATE_ADDED,
629 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700630
631 int expected_fingerprint_callee = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000632 {UsageEvent::AUDIO_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
633 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
634 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::PRIVATE_CANDIDATE_COLLECTED,
635 UsageEvent::IPV6_CANDIDATE_COLLECTED,
636 UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
637 UsageEvent::REMOTE_CANDIDATE_ADDED, UsageEvent::ICE_STATE_CONNECTED,
638 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100639 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
640 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
641 expected_fingerprint_caller));
642 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
643 expected_fingerprint_callee));
Harald Alvestrand056d8112018-07-16 19:18:58 +0200644}
645
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200646#ifndef WEBRTC_ANDROID
Mirko Bonadei5eb43b42021-01-18 13:24:40 +0100647#ifdef WEBRTC_HAVE_SCTP
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700648// Test that the usage pattern bits for adding remote (private IPv6) candidates
649// are set when the remote candidates are retrieved from the Offer SDP instead
650// of trickled ICE messages.
651TEST_F(PeerConnectionUsageHistogramTest,
652 AddRemoteCandidatesFromRemoteDescription) {
653 // We construct the following data-channel-only scenario. The caller collects
654 // IPv6 private local candidates and appends them in the Offer as in
655 // non-trickled sessions. The callee collects mDNS candidates that are not
656 // contained in the Answer as in Trickle ICE. Only the Offer and Answer are
657 // signaled and we expect a connection with prflx remote candidates at the
658 // caller side.
659 auto caller = CreatePeerConnectionWithPrivateIpv6LocalAddresses();
Henrik Boström62995db2022-01-03 09:58:10 +0100660 RTCConfiguration config;
661 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
662 auto callee = CreatePeerConnectionWithMdns(config);
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700663 caller->CreateDataChannel("test_channel");
664 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
665 // Wait until the gathering completes so that the session description would
666 // have contained ICE candidates.
667 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
668 caller->ice_gathering_state(), kDefaultTimeout);
669 EXPECT_TRUE(caller->observer()->candidate_gathered());
670 // Get the current offer that contains candidates and pass it to the callee.
671 //
Artem Titov880fa812021-07-30 22:30:23 +0200672 // Note that we cannot use CloneSessionDescription on `cur_offer` to obtain an
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700673 // SDP with candidates. The method above does not strictly copy everything, in
674 // particular, not copying the ICE candidates.
675 // TODO(qingsi): Technically, this is a bug. Fix it.
676 auto cur_offer = caller->pc()->local_description();
677 ASSERT_TRUE(cur_offer);
678 std::string sdp_with_candidates_str;
679 cur_offer->ToString(&sdp_with_candidates_str);
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200680 auto offer = std::make_unique<JsepSessionDescription>(SdpType::kOffer);
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700681 ASSERT_TRUE(SdpDeserialize(sdp_with_candidates_str, offer.get(),
682 nullptr /* error */));
683 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
684
685 // By default, the Answer created does not contain ICE candidates.
686 auto answer = callee->CreateAnswer();
687 callee->SetLocalDescription(CloneSessionDescription(answer.get()));
688 caller->SetRemoteDescription(std::move(answer));
689 EXPECT_TRUE_WAIT(caller->IsConnected(), kDefaultTimeout);
690 EXPECT_TRUE_WAIT(callee->IsConnected(), kDefaultTimeout);
691 // The callee needs to process the open message to have the data channel open.
692 EXPECT_TRUE_WAIT(callee->observer()->last_datachannel_ != nullptr,
693 kDefaultTimeout);
694 caller->pc()->Close();
695 callee->pc()->Close();
696
697 // The caller should not have added any remote candidate either via
Qingsi Wangcc46b102019-09-12 11:19:01 -0700698 // AddIceCandidate or from the remote description. Also, the caller connects
699 // with the callee via a prflx candidate and hence no direct connection bit
700 // should be set.
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700701 int expected_fingerprint_caller = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000702 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
703 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
704 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::PRIVATE_CANDIDATE_COLLECTED,
705 UsageEvent::IPV6_CANDIDATE_COLLECTED, UsageEvent::ICE_STATE_CONNECTED,
706 UsageEvent::CLOSE_CALLED});
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700707
708 int expected_fingerprint_callee = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000709 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
710 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
711 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::MDNS_CANDIDATE_COLLECTED,
712 UsageEvent::REMOTE_CANDIDATE_ADDED,
713 UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED,
714 UsageEvent::REMOTE_IPV6_CANDIDATE_ADDED, UsageEvent::ICE_STATE_CONNECTED,
715 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100716 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
717 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
718 expected_fingerprint_caller));
719 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
720 expected_fingerprint_callee));
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700721}
722
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200723TEST_F(PeerConnectionUsageHistogramTest, NotableUsageNoted) {
724 auto caller = CreatePeerConnection();
725 caller->CreateDataChannel("foo");
726 caller->GenerateOfferAndCollectCandidates();
727 caller->pc()->Close();
728 int expected_fingerprint = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000729 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
730 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100731 EXPECT_METRIC_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric));
732 EXPECT_METRIC_TRUE(
733 expected_fingerprint == ObservedFingerprint() ||
734 (expected_fingerprint |
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000735 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) ==
Ying Wangef3998f2019-12-09 13:06:53 +0100736 ObservedFingerprint());
737 EXPECT_METRIC_EQ(absl::make_optional(ObservedFingerprint()),
738 caller->observer()->interesting_usage_detected());
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200739}
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200740
741TEST_F(PeerConnectionUsageHistogramTest, NotableUsageOnEventFiring) {
742 auto caller = CreatePeerConnection();
743 caller->CreateDataChannel("foo");
744 caller->GenerateOfferAndCollectCandidates();
745 int expected_fingerprint = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000746 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
747 UsageEvent::CANDIDATE_COLLECTED});
Ying Wangef3998f2019-12-09 13:06:53 +0100748 EXPECT_METRIC_EQ(0, webrtc::metrics::NumSamples(kUsagePatternMetric));
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200749 caller->GetInternalPeerConnection()->RequestUsagePatternReportForTesting();
Ying Wangef3998f2019-12-09 13:06:53 +0100750 EXPECT_METRIC_EQ_WAIT(1, webrtc::metrics::NumSamples(kUsagePatternMetric),
751 kDefaultTimeout);
752 EXPECT_METRIC_TRUE(
753 expected_fingerprint == ObservedFingerprint() ||
754 (expected_fingerprint |
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000755 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) ==
Ying Wangef3998f2019-12-09 13:06:53 +0100756 ObservedFingerprint());
757 EXPECT_METRIC_EQ(absl::make_optional(ObservedFingerprint()),
758 caller->observer()->interesting_usage_detected());
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200759}
760
761TEST_F(PeerConnectionUsageHistogramTest,
762 NoNotableUsageOnEventFiringAfterClose) {
763 auto caller = CreatePeerConnection();
764 caller->CreateDataChannel("foo");
765 caller->GenerateOfferAndCollectCandidates();
766 int expected_fingerprint = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000767 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
768 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100769 EXPECT_METRIC_EQ(0, webrtc::metrics::NumSamples(kUsagePatternMetric));
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200770 caller->pc()->Close();
Ying Wangef3998f2019-12-09 13:06:53 +0100771 EXPECT_METRIC_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric));
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200772 caller->GetInternalPeerConnection()->RequestUsagePatternReportForTesting();
773 caller->observer()->ClearInterestingUsageDetector();
Ying Wangef3998f2019-12-09 13:06:53 +0100774 EXPECT_METRIC_EQ_WAIT(2, webrtc::metrics::NumSamples(kUsagePatternMetric),
775 kDefaultTimeout);
776 EXPECT_METRIC_TRUE(
777 expected_fingerprint == ObservedFingerprint() ||
778 (expected_fingerprint |
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000779 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) ==
Ying Wangef3998f2019-12-09 13:06:53 +0100780 ObservedFingerprint());
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200781 // After close, the usage-detection callback should NOT have been called.
Ying Wangef3998f2019-12-09 13:06:53 +0100782 EXPECT_METRIC_FALSE(caller->observer()->interesting_usage_detected());
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200783}
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200784#endif
785#endif
786
Harald Alvestrand19793842018-06-25 12:03:50 +0200787} // namespace webrtc