blob: ae88b652131b84ca637390e1fe72bab8e1fcb61c [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
78 : public rtc::RefCountedObject<PeerConnectionFactory> {
79 public:
80 PeerConnectionFactoryForUsageHistogramTest()
Danil Chapovalovf5258be2019-03-19 17:45:24 +010081 : rtc::RefCountedObject<PeerConnectionFactory>([] {
82 PeerConnectionFactoryDependencies dependencies;
83 dependencies.network_thread = rtc::Thread::Current();
84 dependencies.worker_thread = rtc::Thread::Current();
85 dependencies.signaling_thread = rtc::Thread::Current();
Danil 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) {
Harald Alvestrand19793842018-06-25 12:03:50 +0200333 rtc::scoped_refptr<PeerConnectionFactoryForUsageHistogramTest> pc_factory(
334 new PeerConnectionFactoryForUsageHistogramTest());
335 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
349 auto pc = pc_factory->CreatePeerConnection(config, std::move(deps));
Harald Alvestrand19793842018-06-25 12:03:50 +0200350 if (!pc) {
351 return nullptr;
352 }
353
Yves Gerey4e933292018-10-31 15:36:05 +0100354 observer->SetPeerConnectionInterface(pc.get());
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200355 auto wrapper = std::make_unique<PeerConnectionWrapperForUsageHistogramTest>(
356 pc_factory, pc, std::move(observer));
Harald Alvestrand19793842018-06-25 12:03:50 +0200357 return wrapper;
358 }
359
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200360 int ObservedFingerprint() {
361 // This works correctly only if there is only one sample value
362 // that has been counted.
363 // Returns -1 for "not found".
364 return webrtc::metrics::MinSample(kUsagePatternMetric);
365 }
366
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700367 // The PeerConnection's port allocator is tied to the PeerConnection's
368 // lifetime and expects the underlying NetworkManager to outlive it. That
369 // prevents us from having the PeerConnectionWrapper own the fake network.
370 // Therefore, the test fixture will own all the fake networks even though
371 // tests should access the fake network through the PeerConnectionWrapper.
372 rtc::FakeNetworkManager* NewFakeNetwork() {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200373 fake_networks_.emplace_back(std::make_unique<rtc::FakeNetworkManager>());
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700374 return fake_networks_.back().get();
375 }
376
377 rtc::SocketAddress NextLocalAddress() {
378 RTC_DCHECK(next_local_address_ < (int)arraysize(kLocalAddrs));
379 return kLocalAddrs[next_local_address_++];
380 }
381
382 std::vector<std::unique_ptr<rtc::FakeNetworkManager>> fake_networks_;
383 int next_local_address_ = 0;
Harald Alvestrand19793842018-06-25 12:03:50 +0200384 std::unique_ptr<rtc::VirtualSocketServer> vss_;
385 rtc::AutoSocketServerThread main_;
386};
387
388TEST_F(PeerConnectionUsageHistogramTest, UsageFingerprintHistogramFromTimeout) {
389 auto pc = CreatePeerConnectionWithImmediateReport();
390
Harald Alvestrand19793842018-06-25 12:03:50 +0200391 int expected_fingerprint = MakeUsageFingerprint({});
Ying Wangef3998f2019-12-09 13:06:53 +0100392 EXPECT_METRIC_EQ_WAIT(1, webrtc::metrics::NumSamples(kUsagePatternMetric),
393 kDefaultTimeout);
394 EXPECT_METRIC_EQ(
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200395 1, webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint));
Harald Alvestrand19793842018-06-25 12:03:50 +0200396}
397
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200398#ifndef WEBRTC_ANDROID
399// These tests do not work on Android. Why is unclear.
400// https://bugs.webrtc.org/9461
401
402// Test getting the usage fingerprint for an audio/video connection.
403TEST_F(PeerConnectionUsageHistogramTest, FingerprintAudioVideo) {
404 auto caller = CreatePeerConnection();
405 auto callee = CreatePeerConnection();
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200406 caller->AddAudioTrack("audio");
407 caller->AddVideoTrack("video");
Harald Alvestrand42386282018-07-12 07:56:05 +0200408 ASSERT_TRUE(caller->ConnectTo(callee.get()));
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200409 caller->pc()->Close();
410 callee->pc()->Close();
411 int expected_fingerprint = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000412 {UsageEvent::AUDIO_ADDED, UsageEvent::VIDEO_ADDED,
413 UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
414 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
415 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
416 UsageEvent::ICE_STATE_CONNECTED, UsageEvent::REMOTE_CANDIDATE_ADDED,
417 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Harald Alvestrand056d8112018-07-16 19:18:58 +0200418 // In this case, we may or may not have PRIVATE_CANDIDATE_COLLECTED,
419 // depending on the machine configuration.
Ying Wangef3998f2019-12-09 13:06:53 +0100420 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
421 EXPECT_METRIC_TRUE(
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200422 webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint) ==
423 2 ||
Harald Alvestrand056d8112018-07-16 19:18:58 +0200424 webrtc::metrics::NumEvents(
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200425 kUsagePatternMetric,
Harald Alvestrand056d8112018-07-16 19:18:58 +0200426 expected_fingerprint |
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000427 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) == 2);
Harald Alvestrand056d8112018-07-16 19:18:58 +0200428}
429
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700430// Test getting the usage fingerprint when the caller collects an mDNS
431// candidate.
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700432TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithMdnsCaller) {
433 RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +0100434 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700435
436 // Enable hostname candidates with mDNS names.
437 auto caller = CreatePeerConnectionWithMdns(config);
438 auto callee = CreatePeerConnection(config);
439
440 caller->AddAudioTrack("audio");
441 caller->AddVideoTrack("video");
442 ASSERT_TRUE(caller->ConnectTo(callee.get()));
443 caller->pc()->Close();
444 callee->pc()->Close();
445
446 int expected_fingerprint_caller = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000447 {UsageEvent::AUDIO_ADDED, UsageEvent::VIDEO_ADDED,
448 UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
449 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
450 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::MDNS_CANDIDATE_COLLECTED,
451 UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED, UsageEvent::ICE_STATE_CONNECTED,
452 UsageEvent::REMOTE_CANDIDATE_ADDED,
453 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700454
Qingsi Wangcc46b102019-09-12 11:19:01 -0700455 // Without a resolver, the callee cannot resolve the received mDNS candidate
456 // but can still connect with the caller via a prflx candidate. As a result,
457 // the bit for the direct connection should not be logged.
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700458 int expected_fingerprint_callee = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000459 {UsageEvent::AUDIO_ADDED, UsageEvent::VIDEO_ADDED,
460 UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
461 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
462 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
463 UsageEvent::REMOTE_MDNS_CANDIDATE_ADDED, UsageEvent::ICE_STATE_CONNECTED,
464 UsageEvent::REMOTE_CANDIDATE_ADDED, UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100465 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
466 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
467 expected_fingerprint_caller));
468 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
469 expected_fingerprint_callee));
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700470}
471
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700472// Test getting the usage fingerprint when the callee collects an mDNS
473// candidate.
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700474TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithMdnsCallee) {
475 RTCConfiguration config;
Henrik Boström62995db2022-01-03 09:58:10 +0100476 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700477
478 // Enable hostname candidates with mDNS names.
479 auto caller = CreatePeerConnection(config);
480 auto callee = CreatePeerConnectionWithMdns(config);
481
482 caller->AddAudioTrack("audio");
483 caller->AddVideoTrack("video");
484 ASSERT_TRUE(caller->ConnectTo(callee.get()));
485 caller->pc()->Close();
486 callee->pc()->Close();
487
Qingsi Wangcc46b102019-09-12 11:19:01 -0700488 // Similar to the test above, the caller connects with the callee via a prflx
489 // candidate.
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700490 int expected_fingerprint_caller = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000491 {UsageEvent::AUDIO_ADDED, UsageEvent::VIDEO_ADDED,
492 UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
493 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
494 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
495 UsageEvent::REMOTE_MDNS_CANDIDATE_ADDED, UsageEvent::ICE_STATE_CONNECTED,
496 UsageEvent::REMOTE_CANDIDATE_ADDED, UsageEvent::CLOSE_CALLED});
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700497
498 int expected_fingerprint_callee = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000499 {UsageEvent::AUDIO_ADDED, UsageEvent::VIDEO_ADDED,
500 UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
501 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
502 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::MDNS_CANDIDATE_COLLECTED,
503 UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED, UsageEvent::ICE_STATE_CONNECTED,
504 UsageEvent::REMOTE_CANDIDATE_ADDED,
505 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100506 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
507 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
508 expected_fingerprint_caller));
509 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
510 expected_fingerprint_callee));
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700511}
512
Mirko Bonadei5eb43b42021-01-18 13:24:40 +0100513#ifdef WEBRTC_HAVE_SCTP
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200514TEST_F(PeerConnectionUsageHistogramTest, FingerprintDataOnly) {
515 auto caller = CreatePeerConnection();
516 auto callee = CreatePeerConnection();
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200517 caller->CreateDataChannel("foodata");
Harald Alvestrand42386282018-07-12 07:56:05 +0200518 ASSERT_TRUE(caller->ConnectTo(callee.get()));
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200519 ASSERT_TRUE_WAIT(callee->HaveDataChannel(), kDefaultTimeout);
520 caller->pc()->Close();
521 callee->pc()->Close();
522 int expected_fingerprint = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000523 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
524 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
525 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
526 UsageEvent::ICE_STATE_CONNECTED, UsageEvent::REMOTE_CANDIDATE_ADDED,
527 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100528 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
529 EXPECT_METRIC_TRUE(
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200530 webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint) ==
531 2 ||
Harald Alvestrand056d8112018-07-16 19:18:58 +0200532 webrtc::metrics::NumEvents(
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200533 kUsagePatternMetric,
Harald Alvestrand056d8112018-07-16 19:18:58 +0200534 expected_fingerprint |
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000535 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) == 2);
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200536}
Mirko Bonadei5eb43b42021-01-18 13:24:40 +0100537#endif // WEBRTC_HAVE_SCTP
Harald Alvestrand183e09d2018-06-28 12:04:41 +0200538#endif // WEBRTC_ANDROID
539
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200540TEST_F(PeerConnectionUsageHistogramTest, FingerprintStunTurn) {
541 RTCConfiguration configuration;
Henrik Boström62995db2022-01-03 09:58:10 +0100542 configuration.sdp_semantics = SdpSemantics::kUnifiedPlan;
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200543 PeerConnection::IceServer server;
Harald Alvestranda3dd7722020-11-27 08:05:42 +0000544 server.urls = {"stun:dummy.stun.server"};
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200545 configuration.servers.push_back(server);
Harald Alvestranda3dd7722020-11-27 08:05:42 +0000546 server.urls = {"turn:dummy.turn.server"};
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200547 server.username = "username";
548 server.password = "password";
549 configuration.servers.push_back(server);
550 auto caller = CreatePeerConnection(configuration);
551 ASSERT_TRUE(caller);
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200552 caller->pc()->Close();
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000553 int expected_fingerprint = MakeUsageFingerprint(
554 {UsageEvent::STUN_SERVER_ADDED, UsageEvent::TURN_SERVER_ADDED,
555 UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100556 EXPECT_METRIC_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric));
557 EXPECT_METRIC_EQ(
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200558 1, webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint));
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200559}
560
561TEST_F(PeerConnectionUsageHistogramTest, FingerprintStunTurnInReconfiguration) {
562 RTCConfiguration configuration;
Henrik Boström62995db2022-01-03 09:58:10 +0100563 configuration.sdp_semantics = SdpSemantics::kUnifiedPlan;
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200564 PeerConnection::IceServer server;
Harald Alvestranda3dd7722020-11-27 08:05:42 +0000565 server.urls = {"stun:dummy.stun.server"};
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200566 configuration.servers.push_back(server);
Harald Alvestranda3dd7722020-11-27 08:05:42 +0000567 server.urls = {"turn:dummy.turn.server"};
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200568 server.username = "username";
569 server.password = "password";
570 configuration.servers.push_back(server);
571 auto caller = CreatePeerConnection();
572 ASSERT_TRUE(caller);
Niels Möller2579f0c2019-08-19 09:58:17 +0200573 ASSERT_TRUE(caller->pc()->SetConfiguration(configuration).ok());
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200574 caller->pc()->Close();
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000575 int expected_fingerprint = MakeUsageFingerprint(
576 {UsageEvent::STUN_SERVER_ADDED, UsageEvent::TURN_SERVER_ADDED,
577 UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100578 EXPECT_METRIC_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric));
579 EXPECT_METRIC_EQ(
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200580 1, webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint));
Harald Alvestrandb2a74782018-06-28 13:54:07 +0200581}
582
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700583TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithPrivateIPCaller) {
Harald Alvestrand056d8112018-07-16 19:18:58 +0200584 auto caller = CreatePeerConnectionWithPrivateLocalAddresses();
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700585 auto callee = CreatePeerConnection();
Harald Alvestrand056d8112018-07-16 19:18:58 +0200586 caller->AddAudioTrack("audio");
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700587 ASSERT_TRUE(caller->ConnectTo(callee.get()));
Harald Alvestrand056d8112018-07-16 19:18:58 +0200588 caller->pc()->Close();
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700589 callee->pc()->Close();
590
591 int expected_fingerprint_caller = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000592 {UsageEvent::AUDIO_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
593 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
594 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::PRIVATE_CANDIDATE_COLLECTED,
595 UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED, UsageEvent::ICE_STATE_CONNECTED,
596 UsageEvent::REMOTE_CANDIDATE_ADDED,
597 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700598
599 int expected_fingerprint_callee = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000600 {UsageEvent::AUDIO_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
601 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
602 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
603 UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED,
604 UsageEvent::ICE_STATE_CONNECTED, UsageEvent::REMOTE_CANDIDATE_ADDED,
605 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100606 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
607 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
608 expected_fingerprint_caller));
609 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
610 expected_fingerprint_callee));
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700611}
612
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700613TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithPrivateIpv6Callee) {
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700614 auto caller = CreatePeerConnection();
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700615 auto callee = CreatePeerConnectionWithPrivateIpv6LocalAddresses();
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700616 caller->AddAudioTrack("audio");
617 ASSERT_TRUE(caller->ConnectTo(callee.get()));
618 caller->pc()->Close();
619 callee->pc()->Close();
620
621 int expected_fingerprint_caller = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000622 {UsageEvent::AUDIO_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
623 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
624 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
625 UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED,
626 UsageEvent::ICE_STATE_CONNECTED, UsageEvent::REMOTE_CANDIDATE_ADDED,
627 UsageEvent::REMOTE_IPV6_CANDIDATE_ADDED,
628 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700629
630 int expected_fingerprint_callee = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000631 {UsageEvent::AUDIO_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
632 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
633 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::PRIVATE_CANDIDATE_COLLECTED,
634 UsageEvent::IPV6_CANDIDATE_COLLECTED,
635 UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
636 UsageEvent::REMOTE_CANDIDATE_ADDED, UsageEvent::ICE_STATE_CONNECTED,
637 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100638 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
639 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
640 expected_fingerprint_caller));
641 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
642 expected_fingerprint_callee));
Harald Alvestrand056d8112018-07-16 19:18:58 +0200643}
644
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200645#ifndef WEBRTC_ANDROID
Mirko Bonadei5eb43b42021-01-18 13:24:40 +0100646#ifdef WEBRTC_HAVE_SCTP
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700647// Test that the usage pattern bits for adding remote (private IPv6) candidates
648// are set when the remote candidates are retrieved from the Offer SDP instead
649// of trickled ICE messages.
650TEST_F(PeerConnectionUsageHistogramTest,
651 AddRemoteCandidatesFromRemoteDescription) {
652 // We construct the following data-channel-only scenario. The caller collects
653 // IPv6 private local candidates and appends them in the Offer as in
654 // non-trickled sessions. The callee collects mDNS candidates that are not
655 // contained in the Answer as in Trickle ICE. Only the Offer and Answer are
656 // signaled and we expect a connection with prflx remote candidates at the
657 // caller side.
658 auto caller = CreatePeerConnectionWithPrivateIpv6LocalAddresses();
Henrik Boström62995db2022-01-03 09:58:10 +0100659 RTCConfiguration config;
660 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
661 auto callee = CreatePeerConnectionWithMdns(config);
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700662 caller->CreateDataChannel("test_channel");
663 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
664 // Wait until the gathering completes so that the session description would
665 // have contained ICE candidates.
666 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
667 caller->ice_gathering_state(), kDefaultTimeout);
668 EXPECT_TRUE(caller->observer()->candidate_gathered());
669 // Get the current offer that contains candidates and pass it to the callee.
670 //
Artem Titov880fa812021-07-30 22:30:23 +0200671 // Note that we cannot use CloneSessionDescription on `cur_offer` to obtain an
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700672 // SDP with candidates. The method above does not strictly copy everything, in
673 // particular, not copying the ICE candidates.
674 // TODO(qingsi): Technically, this is a bug. Fix it.
675 auto cur_offer = caller->pc()->local_description();
676 ASSERT_TRUE(cur_offer);
677 std::string sdp_with_candidates_str;
678 cur_offer->ToString(&sdp_with_candidates_str);
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200679 auto offer = std::make_unique<JsepSessionDescription>(SdpType::kOffer);
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700680 ASSERT_TRUE(SdpDeserialize(sdp_with_candidates_str, offer.get(),
681 nullptr /* error */));
682 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
683
684 // By default, the Answer created does not contain ICE candidates.
685 auto answer = callee->CreateAnswer();
686 callee->SetLocalDescription(CloneSessionDescription(answer.get()));
687 caller->SetRemoteDescription(std::move(answer));
688 EXPECT_TRUE_WAIT(caller->IsConnected(), kDefaultTimeout);
689 EXPECT_TRUE_WAIT(callee->IsConnected(), kDefaultTimeout);
690 // The callee needs to process the open message to have the data channel open.
691 EXPECT_TRUE_WAIT(callee->observer()->last_datachannel_ != nullptr,
692 kDefaultTimeout);
693 caller->pc()->Close();
694 callee->pc()->Close();
695
696 // The caller should not have added any remote candidate either via
Qingsi Wangcc46b102019-09-12 11:19:01 -0700697 // AddIceCandidate or from the remote description. Also, the caller connects
698 // with the callee via a prflx candidate and hence no direct connection bit
699 // should be set.
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700700 int expected_fingerprint_caller = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000701 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
702 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
703 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::PRIVATE_CANDIDATE_COLLECTED,
704 UsageEvent::IPV6_CANDIDATE_COLLECTED, UsageEvent::ICE_STATE_CONNECTED,
705 UsageEvent::CLOSE_CALLED});
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700706
707 int expected_fingerprint_callee = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000708 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
709 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
710 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::MDNS_CANDIDATE_COLLECTED,
711 UsageEvent::REMOTE_CANDIDATE_ADDED,
712 UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED,
713 UsageEvent::REMOTE_IPV6_CANDIDATE_ADDED, UsageEvent::ICE_STATE_CONNECTED,
714 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100715 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
716 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
717 expected_fingerprint_caller));
718 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
719 expected_fingerprint_callee));
Qingsi Wang1ba5dec2019-08-19 11:57:17 -0700720}
721
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200722TEST_F(PeerConnectionUsageHistogramTest, NotableUsageNoted) {
723 auto caller = CreatePeerConnection();
724 caller->CreateDataChannel("foo");
725 caller->GenerateOfferAndCollectCandidates();
726 caller->pc()->Close();
727 int expected_fingerprint = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000728 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
729 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100730 EXPECT_METRIC_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric));
731 EXPECT_METRIC_TRUE(
732 expected_fingerprint == ObservedFingerprint() ||
733 (expected_fingerprint |
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000734 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) ==
Ying Wangef3998f2019-12-09 13:06:53 +0100735 ObservedFingerprint());
736 EXPECT_METRIC_EQ(absl::make_optional(ObservedFingerprint()),
737 caller->observer()->interesting_usage_detected());
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200738}
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200739
740TEST_F(PeerConnectionUsageHistogramTest, NotableUsageOnEventFiring) {
741 auto caller = CreatePeerConnection();
742 caller->CreateDataChannel("foo");
743 caller->GenerateOfferAndCollectCandidates();
744 int expected_fingerprint = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000745 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
746 UsageEvent::CANDIDATE_COLLECTED});
Ying Wangef3998f2019-12-09 13:06:53 +0100747 EXPECT_METRIC_EQ(0, webrtc::metrics::NumSamples(kUsagePatternMetric));
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200748 caller->GetInternalPeerConnection()->RequestUsagePatternReportForTesting();
Ying Wangef3998f2019-12-09 13:06:53 +0100749 EXPECT_METRIC_EQ_WAIT(1, webrtc::metrics::NumSamples(kUsagePatternMetric),
750 kDefaultTimeout);
751 EXPECT_METRIC_TRUE(
752 expected_fingerprint == ObservedFingerprint() ||
753 (expected_fingerprint |
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000754 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) ==
Ying Wangef3998f2019-12-09 13:06:53 +0100755 ObservedFingerprint());
756 EXPECT_METRIC_EQ(absl::make_optional(ObservedFingerprint()),
757 caller->observer()->interesting_usage_detected());
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200758}
759
760TEST_F(PeerConnectionUsageHistogramTest,
761 NoNotableUsageOnEventFiringAfterClose) {
762 auto caller = CreatePeerConnection();
763 caller->CreateDataChannel("foo");
764 caller->GenerateOfferAndCollectCandidates();
765 int expected_fingerprint = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000766 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
767 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 13:06:53 +0100768 EXPECT_METRIC_EQ(0, webrtc::metrics::NumSamples(kUsagePatternMetric));
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200769 caller->pc()->Close();
Ying Wangef3998f2019-12-09 13:06:53 +0100770 EXPECT_METRIC_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric));
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200771 caller->GetInternalPeerConnection()->RequestUsagePatternReportForTesting();
772 caller->observer()->ClearInterestingUsageDetector();
Ying Wangef3998f2019-12-09 13:06:53 +0100773 EXPECT_METRIC_EQ_WAIT(2, webrtc::metrics::NumSamples(kUsagePatternMetric),
774 kDefaultTimeout);
775 EXPECT_METRIC_TRUE(
776 expected_fingerprint == ObservedFingerprint() ||
777 (expected_fingerprint |
Harald Alvestrand44d0dff2020-10-09 05:43:53 +0000778 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) ==
Ying Wangef3998f2019-12-09 13:06:53 +0100779 ObservedFingerprint());
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200780 // After close, the usage-detection callback should NOT have been called.
Ying Wangef3998f2019-12-09 13:06:53 +0100781 EXPECT_METRIC_FALSE(caller->observer()->interesting_usage_detected());
Harald Alvestrand7a1c7f72018-08-01 10:50:16 +0200782}
Harald Alvestrandc0e97252018-07-26 10:39:55 +0200783#endif
784#endif
785
Harald Alvestrand19793842018-06-25 12:03:50 +0200786} // namespace webrtc