blob: 27bae854d67c260e39defb292b235e4b90bd6f14 [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"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "api/call/call_factory_interface.h"
Harald Alvestrand42386282018-07-12 07:56:05 +020019#include "api/jsep.h"
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070020#include "api/jsep_session_description.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "api/peer_connection_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "api/rtc_error.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010023#include "api/scoped_refptr.h"
Danil Chapovalov53d45ba2019-07-03 14:56:33 +020024#include "api/task_queue/default_task_queue_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "media/base/fake_media_engine.h"
Jeroen de Borstaf242c82019-04-24 13:13:48 -070026#include "p2p/base/mock_async_resolver.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#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 Handella1b82012021-05-26 18:56:30 +020031#include "pc/peer_connection_proxy.h"
Steve Anton10542f22019-01-11 09:11:00 -080032#include "pc/peer_connection_wrapper.h"
33#include "pc/sdp_utils.h"
34#include "pc/test/mock_peer_connection_observers.h"
Harald Alvestrand44d0dff2020-10-09 05:43:53 +000035#include "pc/usage_pattern.h"
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070036#include "pc/webrtc_sdp.h"
Jeroen de Borstaf242c82019-04-24 13:13:48 -070037#include "rtc_base/arraysize.h"
Yves Gerey3e707812018-11-28 16:47:49 +010038#include "rtc_base/checks.h"
Qingsi Wangecd30542019-05-22 14:34:56 -070039#include "rtc_base/fake_mdns_responder.h"
Steve Anton10542f22019-01-11 09:11:00 -080040#include "rtc_base/fake_network.h"
Harald Alvestrand19793842018-06-25 12:03:50 +020041#include "rtc_base/gunit.h"
Steve Anton10542f22019-01-11 09:11:00 -080042#include "rtc_base/ref_counted_object.h"
43#include "rtc_base/rtc_certificate_generator.h"
Steve Anton10542f22019-01-11 09:11:00 -080044#include "rtc_base/socket_address.h"
Yves Gerey3e707812018-11-28 16:47:49 +010045#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080046#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020047#include "system_wrappers/include/metrics.h"
Jeroen de Borstaf242c82019-04-24 13:13:48 -070048#include "test/gmock.h"
Harald Alvestrand19793842018-06-25 12:03:50 +020049
50namespace webrtc {
51
52using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
53using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
Jeroen de Borstaf242c82019-04-24 13:13:48 -070054using ::testing::NiceMock;
Harald Alvestrand19793842018-06-25 12:03:50 +020055using ::testing::Values;
56
Harald Alvestrandc0e97252018-07-26 10:39:55 +020057static const char kUsagePatternMetric[] = "WebRTC.PeerConnection.UsagePattern";
Harald Alvestrand19793842018-06-25 12:03:50 +020058static constexpr int kDefaultTimeout = 10000;
Jeroen de Borstaf242c82019-04-24 13:13:48 -070059static const rtc::SocketAddress kLocalAddrs[2] = {
60 rtc::SocketAddress("1.1.1.1", 0), rtc::SocketAddress("2.2.2.2", 0)};
Harald Alvestrand056d8112018-07-16 19:18:58 +020061static const rtc::SocketAddress kPrivateLocalAddress("10.1.1.1", 0);
Qingsi Wang1ba5dec2019-08-19 11:57:17 -070062static const rtc::SocketAddress kPrivateIpv6LocalAddress("fd12:3456:789a:1::1",
63 0);
Harald Alvestrand19793842018-06-25 12:03:50 +020064
Harald Alvestrand44d0dff2020-10-09 05:43:53 +000065int MakeUsageFingerprint(std::set<UsageEvent> events) {
Harald Alvestrand19793842018-06-25 12:03:50 +020066 int signature = 0;
67 for (const auto it : events) {
68 signature |= static_cast<int>(it);
69 }
70 return signature;
71}
72
73class PeerConnectionFactoryForUsageHistogramTest
74 : public rtc::RefCountedObject<PeerConnectionFactory> {
75 public:
76 PeerConnectionFactoryForUsageHistogramTest()
Danil Chapovalovf5258be2019-03-19 17:45:24 +010077 : rtc::RefCountedObject<PeerConnectionFactory>([] {
78 PeerConnectionFactoryDependencies dependencies;
79 dependencies.network_thread = rtc::Thread::Current();
80 dependencies.worker_thread = rtc::Thread::Current();
81 dependencies.signaling_thread = rtc::Thread::Current();
Danil Chapovalov53d45ba2019-07-03 14:56:33 +020082 dependencies.task_queue_factory = CreateDefaultTaskQueueFactory();
Danil Chapovalovf5258be2019-03-19 17:45:24 +010083 dependencies.media_engine =
Mirko Bonadei317a1f02019-09-17 17:06:18 +020084 std::make_unique<cricket::FakeMediaEngine>();
Danil Chapovalovf5258be2019-03-19 17:45:24 +010085 dependencies.call_factory = CreateCallFactory();
86 return dependencies;
87 }()) {}
Harald Alvestrand183e09d2018-06-28 12:04:41 +020088};
89
90class PeerConnectionWrapperForUsageHistogramTest;
Yves Gerey3e707812018-11-28 16:47:49 +010091
Harald Alvestrand183e09d2018-06-28 12:04:41 +020092typedef PeerConnectionWrapperForUsageHistogramTest* RawWrapperPtr;
93
94class ObserverForUsageHistogramTest : public MockPeerConnectionObserver {
95 public:
96 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override;
Harald Alvestrandc0e97252018-07-26 10:39:55 +020097
98 void OnInterestingUsage(int usage_pattern) override {
99 interesting_usage_detected_ = usage_pattern;
100 }
101
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200102 void PrepareToExchangeCandidates(RawWrapperPtr other) {
103 candidate_target_ = other;
104 }
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200105
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200106 bool HaveDataChannel() { return last_datachannel_; }
107
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200108 absl::optional<int> interesting_usage_detected() {
109 return interesting_usage_detected_;
110 }
111
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200112 void ClearInterestingUsageDetector() {
113 interesting_usage_detected_ = absl::optional<int>();
114 }
115
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700116 bool candidate_gathered() const { return candidate_gathered_; }
117
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200118 private:
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200119 absl::optional<int> interesting_usage_detected_;
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700120 bool candidate_gathered_ = false;
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200121 RawWrapperPtr candidate_target_; // Note: Not thread-safe against deletions.
Harald Alvestrand19793842018-06-25 12:03:50 +0200122};
123
124class PeerConnectionWrapperForUsageHistogramTest
125 : public PeerConnectionWrapper {
126 public:
127 using PeerConnectionWrapper::PeerConnectionWrapper;
128
129 PeerConnection* GetInternalPeerConnection() {
130 auto* pci =
131 static_cast<PeerConnectionProxyWithInternal<PeerConnectionInterface>*>(
132 pc());
133 return static_cast<PeerConnection*>(pci->internal());
134 }
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200135
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200136 // Override with different return type
137 ObserverForUsageHistogramTest* observer() {
138 return static_cast<ObserverForUsageHistogramTest*>(
139 PeerConnectionWrapper::observer());
140 }
141
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200142 void PrepareToExchangeCandidates(
143 PeerConnectionWrapperForUsageHistogramTest* other) {
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200144 observer()->PrepareToExchangeCandidates(other);
145 other->observer()->PrepareToExchangeCandidates(this);
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200146 }
147
148 bool IsConnected() {
149 return pc()->ice_connection_state() ==
150 PeerConnectionInterface::kIceConnectionConnected ||
151 pc()->ice_connection_state() ==
152 PeerConnectionInterface::kIceConnectionCompleted;
153 }
154
155 bool HaveDataChannel() {
156 return static_cast<ObserverForUsageHistogramTest*>(observer())
157 ->HaveDataChannel();
158 }
Qingsi Wang32913c12019-10-30 16:03:46 -0700159 void BufferIceCandidate(const webrtc::IceCandidateInterface* candidate) {
160 std::string sdp;
161 EXPECT_TRUE(candidate->ToString(&sdp));
162 std::unique_ptr<webrtc::IceCandidateInterface> candidate_copy(
163 CreateIceCandidate(candidate->sdp_mid(), candidate->sdp_mline_index(),
164 sdp, nullptr));
165 buffered_candidates_.push_back(std::move(candidate_copy));
Harald Alvestrand42386282018-07-12 07:56:05 +0200166 }
Harald Alvestrand056d8112018-07-16 19:18:58 +0200167
Harald Alvestrand42386282018-07-12 07:56:05 +0200168 void AddBufferedIceCandidates() {
169 for (const auto& candidate : buffered_candidates_) {
170 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
171 }
172 buffered_candidates_.clear();
173 }
Harald Alvestrand056d8112018-07-16 19:18:58 +0200174
Qingsi Wang32913c12019-10-30 16:03:46 -0700175 // This method performs the following actions in sequence:
176 // 1. Exchange Offer and Answer.
177 // 2. Exchange ICE candidates after both caller and callee complete
178 // gathering.
179 // 3. Wait for ICE to connect.
180 //
181 // This guarantees a deterministic sequence of events and also rules out the
182 // occurrence of prflx candidates if the offer/answer signaling and the
183 // candidate trickling race in order. In case prflx candidates need to be
184 // simulated, see the approach used by tests below for that.
Harald Alvestrand42386282018-07-12 07:56:05 +0200185 bool ConnectTo(PeerConnectionWrapperForUsageHistogramTest* callee) {
186 PrepareToExchangeCandidates(callee);
Harald Alvestrand056d8112018-07-16 19:18:58 +0200187 if (!ExchangeOfferAnswerWith(callee)) {
188 return false;
189 }
Qingsi Wang32913c12019-10-30 16:03:46 -0700190 // Wait until the gathering completes before we signal the candidate.
191 WAIT(observer()->ice_gathering_complete_, kDefaultTimeout);
192 WAIT(callee->observer()->ice_gathering_complete_, kDefaultTimeout);
Harald Alvestrand42386282018-07-12 07:56:05 +0200193 AddBufferedIceCandidates();
194 callee->AddBufferedIceCandidates();
Harald Alvestrand056d8112018-07-16 19:18:58 +0200195 WAIT(IsConnected(), kDefaultTimeout);
196 WAIT(callee->IsConnected(), kDefaultTimeout);
Harald Alvestrand42386282018-07-12 07:56:05 +0200197 return IsConnected() && callee->IsConnected();
198 }
199
Harald Alvestrand056d8112018-07-16 19:18:58 +0200200 bool GenerateOfferAndCollectCandidates() {
201 auto offer = CreateOffer(RTCOfferAnswerOptions());
202 if (!offer) {
203 return false;
204 }
205 bool set_local_offer =
206 SetLocalDescription(CloneSessionDescription(offer.get()));
207 EXPECT_TRUE(set_local_offer);
208 if (!set_local_offer) {
209 return false;
210 }
211 EXPECT_TRUE_WAIT(observer()->ice_gathering_complete_, kDefaultTimeout);
212 return true;
213 }
214
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700215 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
216 return pc()->ice_gathering_state();
217 }
218
Harald Alvestrand42386282018-07-12 07:56:05 +0200219 private:
220 // Candidates that have been sent but not yet configured
221 std::vector<std::unique_ptr<webrtc::IceCandidateInterface>>
222 buffered_candidates_;
Harald Alvestrand19793842018-06-25 12:03:50 +0200223};
224
Qingsi Wang32913c12019-10-30 16:03:46 -0700225// Buffers candidates until we add them via AddBufferedIceCandidates.
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200226void ObserverForUsageHistogramTest::OnIceCandidate(
227 const webrtc::IceCandidateInterface* candidate) {
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700228 // If target is not set, ignore. This happens in one-ended unit tests.
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200229 if (candidate_target_) {
Qingsi Wang32913c12019-10-30 16:03:46 -0700230 this->candidate_target_->BufferIceCandidate(candidate);
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200231 }
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700232 candidate_gathered_ = true;
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200233}
234
Harald Alvestrand19793842018-06-25 12:03:50 +0200235class PeerConnectionUsageHistogramTest : public ::testing::Test {
236 protected:
237 typedef std::unique_ptr<PeerConnectionWrapperForUsageHistogramTest>
238 WrapperPtr;
239
240 PeerConnectionUsageHistogramTest()
241 : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) {
Qingsi Wang7fc821d2018-07-12 12:54:53 -0700242 webrtc::metrics::Reset();
Harald Alvestrand19793842018-06-25 12:03:50 +0200243 }
244
245 WrapperPtr CreatePeerConnection() {
Henrik Boström62995db2022-01-03 09:58:10 +0100246 RTCConfiguration config;
247 config.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan;
Harald Alvestrand62166932020-10-26 08:30:41 +0000248 return CreatePeerConnection(
Henrik Boström62995db2022-01-03 09:58:10 +0100249 config, PeerConnectionFactoryInterface::Options(), nullptr);
Harald Alvestrand19793842018-06-25 12:03:50 +0200250 }
251
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200252 WrapperPtr CreatePeerConnection(const RTCConfiguration& config) {
253 return CreatePeerConnection(
Harald Alvestrand62166932020-10-26 08:30:41 +0000254 config, PeerConnectionFactoryInterface::Options(), nullptr);
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200255 }
256
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700257 WrapperPtr CreatePeerConnectionWithMdns(const RTCConfiguration& config) {
258 auto resolver_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200259 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700260
261 webrtc::PeerConnectionDependencies deps(nullptr /* observer_in */);
262
263 auto fake_network = NewFakeNetwork();
Qingsi Wangecd30542019-05-22 14:34:56 -0700264 fake_network->set_mdns_responder(
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200265 std::make_unique<webrtc::FakeMdnsResponder>(rtc::Thread::Current()));
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700266 fake_network->AddInterface(NextLocalAddress());
267
268 std::unique_ptr<cricket::BasicPortAllocator> port_allocator(
269 new cricket::BasicPortAllocator(fake_network));
270
271 deps.async_resolver_factory = std::move(resolver_factory);
272 deps.allocator = std::move(port_allocator);
273
Harald Alvestrand62166932020-10-26 08:30:41 +0000274 return CreatePeerConnection(
275 config, PeerConnectionFactoryInterface::Options(), std::move(deps));
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700276 }
277
Harald Alvestrand19793842018-06-25 12:03:50 +0200278 WrapperPtr CreatePeerConnectionWithImmediateReport() {
Harald Alvestrand62166932020-10-26 08:30:41 +0000279 RTCConfiguration configuration;
Henrik Boström62995db2022-01-03 09:58:10 +0100280 configuration.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan;
Harald Alvestrand62166932020-10-26 08:30:41 +0000281 configuration.report_usage_pattern_delay_ms = 0;
282 return CreatePeerConnection(
283 configuration, PeerConnectionFactoryInterface::Options(), nullptr);
Harald Alvestrand056d8112018-07-16 19:18:58 +0200284 }
285
286 WrapperPtr CreatePeerConnectionWithPrivateLocalAddresses() {
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700287 auto* fake_network = NewFakeNetwork();
288 fake_network->AddInterface(NextLocalAddress());
289 fake_network->AddInterface(kPrivateLocalAddress);
290
291 auto port_allocator =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200292 std::make_unique<cricket::BasicPortAllocator>(fake_network);
Henrik Boström62995db2022-01-03 09:58:10 +0100293 RTCConfiguration config;
294 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
295 return CreatePeerConnection(config,
Harald Alvestrand056d8112018-07-16 19:18:58 +0200296 PeerConnectionFactoryInterface::Options(),
Harald Alvestrand62166932020-10-26 08:30:41 +0000297 std::move(port_allocator));
Harald Alvestrand19793842018-06-25 12:03:50 +0200298 }
299
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700300 WrapperPtr CreatePeerConnectionWithPrivateIpv6LocalAddresses() {
301 auto* fake_network = NewFakeNetwork();
302 fake_network->AddInterface(NextLocalAddress());
303 fake_network->AddInterface(kPrivateIpv6LocalAddress);
304
305 auto port_allocator =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200306 std::make_unique<cricket::BasicPortAllocator>(fake_network);
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700307
Henrik Boström62995db2022-01-03 09:58:10 +0100308 RTCConfiguration config;
309 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
310 return CreatePeerConnection(config,
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700311 PeerConnectionFactoryInterface::Options(),
Harald Alvestrand62166932020-10-26 08:30:41 +0000312 std::move(port_allocator));
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700313 }
314
Harald Alvestrand19793842018-06-25 12:03:50 +0200315 WrapperPtr CreatePeerConnection(
316 const RTCConfiguration& config,
317 const PeerConnectionFactoryInterface::Options factory_options,
Harald Alvestrand62166932020-10-26 08:30:41 +0000318 std::unique_ptr<cricket::PortAllocator> allocator) {
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700319 PeerConnectionDependencies deps(nullptr);
320 deps.allocator = std::move(allocator);
321
Harald Alvestrand62166932020-10-26 08:30:41 +0000322 return CreatePeerConnection(config, factory_options, std::move(deps));
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700323 }
324
325 WrapperPtr CreatePeerConnection(
326 const RTCConfiguration& config,
327 const PeerConnectionFactoryInterface::Options factory_options,
Harald Alvestrand62166932020-10-26 08:30:41 +0000328 PeerConnectionDependencies deps) {
Harald Alvestrand19793842018-06-25 12:03:50 +0200329 rtc::scoped_refptr<PeerConnectionFactoryForUsageHistogramTest> pc_factory(
330 new PeerConnectionFactoryForUsageHistogramTest());
331 pc_factory->SetOptions(factory_options);
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700332
333 // If no allocator is provided, one will be created using a network manager
334 // that uses the host network. This doesn't work on all trybots.
335 if (!deps.allocator) {
336 auto fake_network = NewFakeNetwork();
337 fake_network->AddInterface(NextLocalAddress());
338 deps.allocator =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200339 std::make_unique<cricket::BasicPortAllocator>(fake_network);
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700340 }
341
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200342 auto observer = std::make_unique<ObserverForUsageHistogramTest>();
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700343 deps.observer = observer.get();
344
345 auto pc = pc_factory->CreatePeerConnection(config, std::move(deps));
Harald Alvestrand19793842018-06-25 12:03:50 +0200346 if (!pc) {
347 return nullptr;
348 }
349
Yves Gerey4e933292018-10-31 15:36:05 +0100350 observer->SetPeerConnectionInterface(pc.get());
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200351 auto wrapper = std::make_unique<PeerConnectionWrapperForUsageHistogramTest>(
352 pc_factory, pc, std::move(observer));
Harald Alvestrand19793842018-06-25 12:03:50 +0200353 return wrapper;
354 }
355
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200356 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".
360 return webrtc::metrics::MinSample(kUsagePatternMetric);
361 }
362
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700363 // 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 Bonadei317a1f02019-09-17 17:06:18 +0200369 fake_networks_.emplace_back(std::make_unique<rtc::FakeNetworkManager>());
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700370 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 Alvestrand19793842018-06-25 12:03:50 +0200380 std::unique_ptr<rtc::VirtualSocketServer> vss_;
381 rtc::AutoSocketServerThread main_;
382};
383
384TEST_F(PeerConnectionUsageHistogramTest, UsageFingerprintHistogramFromTimeout) {
385 auto pc = CreatePeerConnectionWithImmediateReport();
386
Harald Alvestrand19793842018-06-25 12:03:50 +0200387 int expected_fingerprint = MakeUsageFingerprint({});
Ying Wangef3998f2019-12-09 13:06:53 +0100388 EXPECT_METRIC_EQ_WAIT(1, webrtc::metrics::NumSamples(kUsagePatternMetric),
389 kDefaultTimeout);
390 EXPECT_METRIC_EQ(
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200391 1, webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint));
Harald Alvestrand19793842018-06-25 12:03:50 +0200392}
393
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200394#ifndef WEBRTC_ANDROID
395// These tests do not work on Android. Why is unclear.
396// https://bugs.webrtc.org/9461
397
398// Test getting the usage fingerprint for an audio/video connection.
399TEST_F(PeerConnectionUsageHistogramTest, FingerprintAudioVideo) {
400 auto caller = CreatePeerConnection();
401 auto callee = CreatePeerConnection();
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200402 caller->AddAudioTrack("audio");
403 caller->AddVideoTrack("video");
Harald Alvestrand42386282018-07-12 07:56:05 +0200404 ASSERT_TRUE(caller->ConnectTo(callee.get()));
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200405 caller->pc()->Close();
406 callee->pc()->Close();
407 int expected_fingerprint = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000408 {UsageEvent::AUDIO_ADDED, UsageEvent::VIDEO_ADDED,
409 UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
410 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
411 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
412 UsageEvent::ICE_STATE_CONNECTED, UsageEvent::REMOTE_CANDIDATE_ADDED,
413 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Harald Alvestrand056d8112018-07-16 19:18:58 +0200414 // In this case, we may or may not have PRIVATE_CANDIDATE_COLLECTED,
415 // depending on the machine configuration.
Ying Wangef3998f2019-12-09 13:06:53 +0100416 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
417 EXPECT_METRIC_TRUE(
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200418 webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint) ==
419 2 ||
Harald Alvestrand056d8112018-07-16 19:18:58 +0200420 webrtc::metrics::NumEvents(
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200421 kUsagePatternMetric,
Harald Alvestrand056d8112018-07-16 19:18:58 +0200422 expected_fingerprint |
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000423 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) == 2);
Harald Alvestrand056d8112018-07-16 19:18:58 +0200424}
425
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700426// Test getting the usage fingerprint when the caller collects an mDNS
427// candidate.
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700428TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithMdnsCaller) {
429 RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +0100430 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700431
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 Alvestrand44d0dff2020-10-09 05:43:53 +0000443 {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 Borstaf242c82019-04-24 13:13:48 -0700450
Qingsi Wangcc46b102019-09-12 11:19:01 -0700451 // 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 Borstaf242c82019-04-24 13:13:48 -0700454 int expected_fingerprint_callee = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000455 {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});
Ying Wangef3998f2019-12-09 13:06:53 +0100461 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
462 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
463 expected_fingerprint_caller));
464 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
465 expected_fingerprint_callee));
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700466}
467
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700468// Test getting the usage fingerprint when the callee collects an mDNS
469// candidate.
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700470TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithMdnsCallee) {
471 RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +0100472 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700473
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 Wangcc46b102019-09-12 11:19:01 -0700484 // Similar to the test above, the caller connects with the callee via a prflx
485 // candidate.
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700486 int expected_fingerprint_caller = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000487 {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 Borstaf242c82019-04-24 13:13:48 -0700493
494 int expected_fingerprint_callee = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000495 {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});
Ying Wangef3998f2019-12-09 13:06:53 +0100502 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
503 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
504 expected_fingerprint_caller));
505 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
506 expected_fingerprint_callee));
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700507}
508
Mirko Bonadei5eb43b42021-01-18 13:24:40 +0100509#ifdef WEBRTC_HAVE_SCTP
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200510TEST_F(PeerConnectionUsageHistogramTest, FingerprintDataOnly) {
511 auto caller = CreatePeerConnection();
512 auto callee = CreatePeerConnection();
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200513 caller->CreateDataChannel("foodata");
Harald Alvestrand42386282018-07-12 07:56:05 +0200514 ASSERT_TRUE(caller->ConnectTo(callee.get()));
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200515 ASSERT_TRUE_WAIT(callee->HaveDataChannel(), kDefaultTimeout);
516 caller->pc()->Close();
517 callee->pc()->Close();
518 int expected_fingerprint = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000519 {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});
Ying Wangef3998f2019-12-09 13:06:53 +0100524 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
525 EXPECT_METRIC_TRUE(
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200526 webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint) ==
527 2 ||
Harald Alvestrand056d8112018-07-16 19:18:58 +0200528 webrtc::metrics::NumEvents(
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200529 kUsagePatternMetric,
Harald Alvestrand056d8112018-07-16 19:18:58 +0200530 expected_fingerprint |
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000531 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) == 2);
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200532}
Mirko Bonadei5eb43b42021-01-18 13:24:40 +0100533#endif // WEBRTC_HAVE_SCTP
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200534#endif // WEBRTC_ANDROID
535
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200536TEST_F(PeerConnectionUsageHistogramTest, FingerprintStunTurn) {
537 RTCConfiguration configuration;
Henrik Boström62995db2022-01-03 09:58:10 +0100538 configuration.sdp_semantics = SdpSemantics::kUnifiedPlan;
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200539 PeerConnection::IceServer server;
Harald Alvestranda3dd7722020-11-27 08:05:42 +0000540 server.urls = {"stun:dummy.stun.server"};
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200541 configuration.servers.push_back(server);
Harald Alvestranda3dd7722020-11-27 08:05:42 +0000542 server.urls = {"turn:dummy.turn.server"};
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200543 server.username = "username";
544 server.password = "password";
545 configuration.servers.push_back(server);
546 auto caller = CreatePeerConnection(configuration);
547 ASSERT_TRUE(caller);
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200548 caller->pc()->Close();
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000549 int expected_fingerprint = MakeUsageFingerprint(
550 {UsageEvent::STUN_SERVER_ADDED, UsageEvent::TURN_SERVER_ADDED,
551 UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100552 EXPECT_METRIC_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric));
553 EXPECT_METRIC_EQ(
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200554 1, webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint));
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200555}
556
557TEST_F(PeerConnectionUsageHistogramTest, FingerprintStunTurnInReconfiguration) {
558 RTCConfiguration configuration;
Henrik Boström62995db2022-01-03 09:58:10 +0100559 configuration.sdp_semantics = SdpSemantics::kUnifiedPlan;
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200560 PeerConnection::IceServer server;
Harald Alvestranda3dd7722020-11-27 08:05:42 +0000561 server.urls = {"stun:dummy.stun.server"};
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200562 configuration.servers.push_back(server);
Harald Alvestranda3dd7722020-11-27 08:05:42 +0000563 server.urls = {"turn:dummy.turn.server"};
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200564 server.username = "username";
565 server.password = "password";
566 configuration.servers.push_back(server);
567 auto caller = CreatePeerConnection();
568 ASSERT_TRUE(caller);
Niels Möller2579f0c2019-08-19 09:58:17 +0200569 ASSERT_TRUE(caller->pc()->SetConfiguration(configuration).ok());
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200570 caller->pc()->Close();
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000571 int expected_fingerprint = MakeUsageFingerprint(
572 {UsageEvent::STUN_SERVER_ADDED, UsageEvent::TURN_SERVER_ADDED,
573 UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100574 EXPECT_METRIC_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric));
575 EXPECT_METRIC_EQ(
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200576 1, webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint));
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200577}
578
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700579TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithPrivateIPCaller) {
Harald Alvestrand056d8112018-07-16 19:18:58 +0200580 auto caller = CreatePeerConnectionWithPrivateLocalAddresses();
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700581 auto callee = CreatePeerConnection();
Harald Alvestrand056d8112018-07-16 19:18:58 +0200582 caller->AddAudioTrack("audio");
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700583 ASSERT_TRUE(caller->ConnectTo(callee.get()));
Harald Alvestrand056d8112018-07-16 19:18:58 +0200584 caller->pc()->Close();
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700585 callee->pc()->Close();
586
587 int expected_fingerprint_caller = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000588 {UsageEvent::AUDIO_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
589 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
590 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::PRIVATE_CANDIDATE_COLLECTED,
591 UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED, UsageEvent::ICE_STATE_CONNECTED,
592 UsageEvent::REMOTE_CANDIDATE_ADDED,
593 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700594
595 int expected_fingerprint_callee = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000596 {UsageEvent::AUDIO_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
597 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
598 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
599 UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED,
600 UsageEvent::ICE_STATE_CONNECTED, UsageEvent::REMOTE_CANDIDATE_ADDED,
601 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100602 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
603 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
604 expected_fingerprint_caller));
605 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
606 expected_fingerprint_callee));
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700607}
608
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700609TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithPrivateIpv6Callee) {
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700610 auto caller = CreatePeerConnection();
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700611 auto callee = CreatePeerConnectionWithPrivateIpv6LocalAddresses();
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700612 caller->AddAudioTrack("audio");
613 ASSERT_TRUE(caller->ConnectTo(callee.get()));
614 caller->pc()->Close();
615 callee->pc()->Close();
616
617 int expected_fingerprint_caller = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000618 {UsageEvent::AUDIO_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
619 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
620 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
621 UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED,
622 UsageEvent::ICE_STATE_CONNECTED, UsageEvent::REMOTE_CANDIDATE_ADDED,
623 UsageEvent::REMOTE_IPV6_CANDIDATE_ADDED,
624 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700625
626 int expected_fingerprint_callee = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000627 {UsageEvent::AUDIO_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
628 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
629 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::PRIVATE_CANDIDATE_COLLECTED,
630 UsageEvent::IPV6_CANDIDATE_COLLECTED,
631 UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
632 UsageEvent::REMOTE_CANDIDATE_ADDED, UsageEvent::ICE_STATE_CONNECTED,
633 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100634 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
635 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
636 expected_fingerprint_caller));
637 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
638 expected_fingerprint_callee));
Harald Alvestrand056d8112018-07-16 19:18:58 +0200639}
640
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200641#ifndef WEBRTC_ANDROID
Mirko Bonadei5eb43b42021-01-18 13:24:40 +0100642#ifdef WEBRTC_HAVE_SCTP
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700643// Test that the usage pattern bits for adding remote (private IPv6) candidates
644// are set when the remote candidates are retrieved from the Offer SDP instead
645// of trickled ICE messages.
646TEST_F(PeerConnectionUsageHistogramTest,
647 AddRemoteCandidatesFromRemoteDescription) {
648 // We construct the following data-channel-only scenario. The caller collects
649 // IPv6 private local candidates and appends them in the Offer as in
650 // non-trickled sessions. The callee collects mDNS candidates that are not
651 // contained in the Answer as in Trickle ICE. Only the Offer and Answer are
652 // signaled and we expect a connection with prflx remote candidates at the
653 // caller side.
654 auto caller = CreatePeerConnectionWithPrivateIpv6LocalAddresses();
Henrik Boström62995db2022-01-03 09:58:10 +0100655 RTCConfiguration config;
656 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
657 auto callee = CreatePeerConnectionWithMdns(config);
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700658 caller->CreateDataChannel("test_channel");
659 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
660 // Wait until the gathering completes so that the session description would
661 // have contained ICE candidates.
662 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
663 caller->ice_gathering_state(), kDefaultTimeout);
664 EXPECT_TRUE(caller->observer()->candidate_gathered());
665 // Get the current offer that contains candidates and pass it to the callee.
666 //
Artem Titov880fa812021-07-30 22:30:23 +0200667 // Note that we cannot use CloneSessionDescription on `cur_offer` to obtain an
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700668 // SDP with candidates. The method above does not strictly copy everything, in
669 // particular, not copying the ICE candidates.
670 // TODO(qingsi): Technically, this is a bug. Fix it.
671 auto cur_offer = caller->pc()->local_description();
672 ASSERT_TRUE(cur_offer);
673 std::string sdp_with_candidates_str;
674 cur_offer->ToString(&sdp_with_candidates_str);
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200675 auto offer = std::make_unique<JsepSessionDescription>(SdpType::kOffer);
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700676 ASSERT_TRUE(SdpDeserialize(sdp_with_candidates_str, offer.get(),
677 nullptr /* error */));
678 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
679
680 // By default, the Answer created does not contain ICE candidates.
681 auto answer = callee->CreateAnswer();
682 callee->SetLocalDescription(CloneSessionDescription(answer.get()));
683 caller->SetRemoteDescription(std::move(answer));
684 EXPECT_TRUE_WAIT(caller->IsConnected(), kDefaultTimeout);
685 EXPECT_TRUE_WAIT(callee->IsConnected(), kDefaultTimeout);
686 // The callee needs to process the open message to have the data channel open.
687 EXPECT_TRUE_WAIT(callee->observer()->last_datachannel_ != nullptr,
688 kDefaultTimeout);
689 caller->pc()->Close();
690 callee->pc()->Close();
691
692 // The caller should not have added any remote candidate either via
Qingsi Wangcc46b102019-09-12 11:19:01 -0700693 // AddIceCandidate or from the remote description. Also, the caller connects
694 // with the callee via a prflx candidate and hence no direct connection bit
695 // should be set.
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700696 int expected_fingerprint_caller = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000697 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
698 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
699 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::PRIVATE_CANDIDATE_COLLECTED,
700 UsageEvent::IPV6_CANDIDATE_COLLECTED, UsageEvent::ICE_STATE_CONNECTED,
701 UsageEvent::CLOSE_CALLED});
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700702
703 int expected_fingerprint_callee = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000704 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
705 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
706 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::MDNS_CANDIDATE_COLLECTED,
707 UsageEvent::REMOTE_CANDIDATE_ADDED,
708 UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED,
709 UsageEvent::REMOTE_IPV6_CANDIDATE_ADDED, UsageEvent::ICE_STATE_CONNECTED,
710 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100711 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
712 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
713 expected_fingerprint_caller));
714 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
715 expected_fingerprint_callee));
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700716}
717
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200718TEST_F(PeerConnectionUsageHistogramTest, NotableUsageNoted) {
719 auto caller = CreatePeerConnection();
720 caller->CreateDataChannel("foo");
721 caller->GenerateOfferAndCollectCandidates();
722 caller->pc()->Close();
723 int expected_fingerprint = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000724 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
725 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100726 EXPECT_METRIC_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric));
727 EXPECT_METRIC_TRUE(
728 expected_fingerprint == ObservedFingerprint() ||
729 (expected_fingerprint |
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000730 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) ==
Ying Wangef3998f2019-12-09 13:06:53 +0100731 ObservedFingerprint());
732 EXPECT_METRIC_EQ(absl::make_optional(ObservedFingerprint()),
733 caller->observer()->interesting_usage_detected());
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200734}
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200735
736TEST_F(PeerConnectionUsageHistogramTest, NotableUsageOnEventFiring) {
737 auto caller = CreatePeerConnection();
738 caller->CreateDataChannel("foo");
739 caller->GenerateOfferAndCollectCandidates();
740 int expected_fingerprint = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000741 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
742 UsageEvent::CANDIDATE_COLLECTED});
Ying Wangef3998f2019-12-09 13:06:53 +0100743 EXPECT_METRIC_EQ(0, webrtc::metrics::NumSamples(kUsagePatternMetric));
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200744 caller->GetInternalPeerConnection()->RequestUsagePatternReportForTesting();
Ying Wangef3998f2019-12-09 13:06:53 +0100745 EXPECT_METRIC_EQ_WAIT(1, webrtc::metrics::NumSamples(kUsagePatternMetric),
746 kDefaultTimeout);
747 EXPECT_METRIC_TRUE(
748 expected_fingerprint == ObservedFingerprint() ||
749 (expected_fingerprint |
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000750 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) ==
Ying Wangef3998f2019-12-09 13:06:53 +0100751 ObservedFingerprint());
752 EXPECT_METRIC_EQ(absl::make_optional(ObservedFingerprint()),
753 caller->observer()->interesting_usage_detected());
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200754}
755
756TEST_F(PeerConnectionUsageHistogramTest,
757 NoNotableUsageOnEventFiringAfterClose) {
758 auto caller = CreatePeerConnection();
759 caller->CreateDataChannel("foo");
760 caller->GenerateOfferAndCollectCandidates();
761 int expected_fingerprint = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000762 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
763 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100764 EXPECT_METRIC_EQ(0, webrtc::metrics::NumSamples(kUsagePatternMetric));
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200765 caller->pc()->Close();
Ying Wangef3998f2019-12-09 13:06:53 +0100766 EXPECT_METRIC_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric));
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200767 caller->GetInternalPeerConnection()->RequestUsagePatternReportForTesting();
768 caller->observer()->ClearInterestingUsageDetector();
Ying Wangef3998f2019-12-09 13:06:53 +0100769 EXPECT_METRIC_EQ_WAIT(2, webrtc::metrics::NumSamples(kUsagePatternMetric),
770 kDefaultTimeout);
771 EXPECT_METRIC_TRUE(
772 expected_fingerprint == ObservedFingerprint() ||
773 (expected_fingerprint |
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000774 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) ==
Ying Wangef3998f2019-12-09 13:06:53 +0100775 ObservedFingerprint());
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200776 // After close, the usage-detection callback should NOT have been called.
Ying Wangef3998f2019-12-09 13:06:53 +0100777 EXPECT_METRIC_FALSE(caller->observer()->interesting_usage_detected());
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200778}
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200779#endif
780#endif
781
Harald Alvestrand19793842018-06-25 12:03:50 +0200782} // namespace webrtc