blob: 27bae854d67c260e39defb292b235e4b90bd6f14 [file] [log] [blame]
Harald Alvestrand19793842018-06-25 10:03:501/*
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 15:47:4911#include <memory>
12#include <set>
13#include <string>
14#include <utility>
15#include <vector>
Harald Alvestrand19793842018-06-25 10:03:5016
Yves Gerey3e707812018-11-28 15:47:4917#include "absl/types/optional.h"
Steve Anton10542f22019-01-11 17:11:0018#include "api/call/call_factory_interface.h"
Harald Alvestrand42386282018-07-12 05:56:0519#include "api/jsep.h"
Qingsi Wang1ba5dec2019-08-19 18:57:1720#include "api/jsep_session_description.h"
Steve Anton10542f22019-01-11 17:11:0021#include "api/peer_connection_interface.h"
Steve Anton10542f22019-01-11 17:11:0022#include "api/rtc_error.h"
Mirko Bonadeid9708072019-01-25 19:26:4823#include "api/scoped_refptr.h"
Danil Chapovalov53d45ba2019-07-03 12:56:3324#include "api/task_queue/default_task_queue_factory.h"
Steve Anton10542f22019-01-11 17:11:0025#include "media/base/fake_media_engine.h"
Jeroen de Borstaf242c82019-04-24 20:13:4826#include "p2p/base/mock_async_resolver.h"
Steve Anton10542f22019-01-11 17:11:0027#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 16:56:3031#include "pc/peer_connection_proxy.h"
Steve Anton10542f22019-01-11 17:11:0032#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:5335#include "pc/usage_pattern.h"
Qingsi Wang1ba5dec2019-08-19 18:57:1736#include "pc/webrtc_sdp.h"
Jeroen de Borstaf242c82019-04-24 20:13:4837#include "rtc_base/arraysize.h"
Yves Gerey3e707812018-11-28 15:47:4938#include "rtc_base/checks.h"
Qingsi Wangecd30542019-05-22 21:34:5639#include "rtc_base/fake_mdns_responder.h"
Steve Anton10542f22019-01-11 17:11:0040#include "rtc_base/fake_network.h"
Harald Alvestrand19793842018-06-25 10:03:5041#include "rtc_base/gunit.h"
Steve Anton10542f22019-01-11 17:11:0042#include "rtc_base/ref_counted_object.h"
43#include "rtc_base/rtc_certificate_generator.h"
Steve Anton10542f22019-01-11 17:11:0044#include "rtc_base/socket_address.h"
Yves Gerey3e707812018-11-28 15:47:4945#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 17:11:0046#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei17f48782018-09-28 06:51:1047#include "system_wrappers/include/metrics.h"
Jeroen de Borstaf242c82019-04-24 20:13:4848#include "test/gmock.h"
Harald Alvestrand19793842018-06-25 10:03:5049
50namespace webrtc {
51
52using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
53using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
Jeroen de Borstaf242c82019-04-24 20:13:4854using ::testing::NiceMock;
Harald Alvestrand19793842018-06-25 10:03:5055using ::testing::Values;
56
Harald Alvestrandc0e97252018-07-26 08:39:5557static const char kUsagePatternMetric[] = "WebRTC.PeerConnection.UsagePattern";
Harald Alvestrand19793842018-06-25 10:03:5058static constexpr int kDefaultTimeout = 10000;
Jeroen de Borstaf242c82019-04-24 20:13:4859static const rtc::SocketAddress kLocalAddrs[2] = {
60 rtc::SocketAddress("1.1.1.1", 0), rtc::SocketAddress("2.2.2.2", 0)};
Harald Alvestrand056d8112018-07-16 17:18:5861static const rtc::SocketAddress kPrivateLocalAddress("10.1.1.1", 0);
Qingsi Wang1ba5dec2019-08-19 18:57:1762static const rtc::SocketAddress kPrivateIpv6LocalAddress("fd12:3456:789a:1::1",
63 0);
Harald Alvestrand19793842018-06-25 10:03:5064
Harald Alvestrand44d0dff2020-10-09 05:43:5365int MakeUsageFingerprint(std::set<UsageEvent> events) {
Harald Alvestrand19793842018-06-25 10:03:5066 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 16:45:2477 : 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 12:56:3382 dependencies.task_queue_factory = CreateDefaultTaskQueueFactory();
Danil Chapovalovf5258be2019-03-19 16:45:2483 dependencies.media_engine =
Mirko Bonadei317a1f02019-09-17 15:06:1884 std::make_unique<cricket::FakeMediaEngine>();
Danil Chapovalovf5258be2019-03-19 16:45:2485 dependencies.call_factory = CreateCallFactory();
86 return dependencies;
87 }()) {}
Harald Alvestrand183e09d2018-06-28 10:04:4188};
89
90class PeerConnectionWrapperForUsageHistogramTest;
Yves Gerey3e707812018-11-28 15:47:4991
Harald Alvestrand183e09d2018-06-28 10:04:4192typedef PeerConnectionWrapperForUsageHistogramTest* RawWrapperPtr;
93
94class ObserverForUsageHistogramTest : public MockPeerConnectionObserver {
95 public:
96 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override;
Harald Alvestrandc0e97252018-07-26 08:39:5597
98 void OnInterestingUsage(int usage_pattern) override {
99 interesting_usage_detected_ = usage_pattern;
100 }
101
Harald Alvestrand183e09d2018-06-28 10:04:41102 void PrepareToExchangeCandidates(RawWrapperPtr other) {
103 candidate_target_ = other;
104 }
Harald Alvestrandc0e97252018-07-26 08:39:55105
Harald Alvestrand183e09d2018-06-28 10:04:41106 bool HaveDataChannel() { return last_datachannel_; }
107
Harald Alvestrandc0e97252018-07-26 08:39:55108 absl::optional<int> interesting_usage_detected() {
109 return interesting_usage_detected_;
110 }
111
Harald Alvestrand7a1c7f72018-08-01 08:50:16112 void ClearInterestingUsageDetector() {
113 interesting_usage_detected_ = absl::optional<int>();
114 }
115
Qingsi Wang1ba5dec2019-08-19 18:57:17116 bool candidate_gathered() const { return candidate_gathered_; }
117
Harald Alvestrand183e09d2018-06-28 10:04:41118 private:
Harald Alvestrandc0e97252018-07-26 08:39:55119 absl::optional<int> interesting_usage_detected_;
Qingsi Wang1ba5dec2019-08-19 18:57:17120 bool candidate_gathered_ = false;
Harald Alvestrand183e09d2018-06-28 10:04:41121 RawWrapperPtr candidate_target_; // Note: Not thread-safe against deletions.
Harald Alvestrand19793842018-06-25 10:03:50122};
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 10:04:41135
Harald Alvestrandc0e97252018-07-26 08:39:55136 // Override with different return type
137 ObserverForUsageHistogramTest* observer() {
138 return static_cast<ObserverForUsageHistogramTest*>(
139 PeerConnectionWrapper::observer());
140 }
141
Harald Alvestrand183e09d2018-06-28 10:04:41142 void PrepareToExchangeCandidates(
143 PeerConnectionWrapperForUsageHistogramTest* other) {
Harald Alvestrandc0e97252018-07-26 08:39:55144 observer()->PrepareToExchangeCandidates(other);
145 other->observer()->PrepareToExchangeCandidates(this);
Harald Alvestrand183e09d2018-06-28 10:04:41146 }
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 23:03:46159 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 05:56:05166 }
Harald Alvestrand056d8112018-07-16 17:18:58167
Harald Alvestrand42386282018-07-12 05:56:05168 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 17:18:58174
Qingsi Wang32913c12019-10-30 23:03:46175 // 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 05:56:05185 bool ConnectTo(PeerConnectionWrapperForUsageHistogramTest* callee) {
186 PrepareToExchangeCandidates(callee);
Harald Alvestrand056d8112018-07-16 17:18:58187 if (!ExchangeOfferAnswerWith(callee)) {
188 return false;
189 }
Qingsi Wang32913c12019-10-30 23:03:46190 // 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 05:56:05193 AddBufferedIceCandidates();
194 callee->AddBufferedIceCandidates();
Harald Alvestrand056d8112018-07-16 17:18:58195 WAIT(IsConnected(), kDefaultTimeout);
196 WAIT(callee->IsConnected(), kDefaultTimeout);
Harald Alvestrand42386282018-07-12 05:56:05197 return IsConnected() && callee->IsConnected();
198 }
199
Harald Alvestrand056d8112018-07-16 17:18:58200 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 18:57:17215 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
216 return pc()->ice_gathering_state();
217 }
218
Harald Alvestrand42386282018-07-12 05:56:05219 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 10:03:50223};
224
Qingsi Wang32913c12019-10-30 23:03:46225// Buffers candidates until we add them via AddBufferedIceCandidates.
Harald Alvestrand183e09d2018-06-28 10:04:41226void ObserverForUsageHistogramTest::OnIceCandidate(
227 const webrtc::IceCandidateInterface* candidate) {
Qingsi Wang1ba5dec2019-08-19 18:57:17228 // If target is not set, ignore. This happens in one-ended unit tests.
Harald Alvestrand183e09d2018-06-28 10:04:41229 if (candidate_target_) {
Qingsi Wang32913c12019-10-30 23:03:46230 this->candidate_target_->BufferIceCandidate(candidate);
Harald Alvestrand183e09d2018-06-28 10:04:41231 }
Qingsi Wang1ba5dec2019-08-19 18:57:17232 candidate_gathered_ = true;
Harald Alvestrand183e09d2018-06-28 10:04:41233}
234
Harald Alvestrand19793842018-06-25 10:03:50235class 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 19:54:53242 webrtc::metrics::Reset();
Harald Alvestrand19793842018-06-25 10:03:50243 }
244
245 WrapperPtr CreatePeerConnection() {
Henrik Boström62995db2022-01-03 08:58:10246 RTCConfiguration config;
247 config.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan;
Harald Alvestrand62166932020-10-26 08:30:41248 return CreatePeerConnection(
Henrik Boström62995db2022-01-03 08:58:10249 config, PeerConnectionFactoryInterface::Options(), nullptr);
Harald Alvestrand19793842018-06-25 10:03:50250 }
251
Harald Alvestrandb2a74782018-06-28 11:54:07252 WrapperPtr CreatePeerConnection(const RTCConfiguration& config) {
253 return CreatePeerConnection(
Harald Alvestrand62166932020-10-26 08:30:41254 config, PeerConnectionFactoryInterface::Options(), nullptr);
Harald Alvestrandb2a74782018-06-28 11:54:07255 }
256
Jeroen de Borstaf242c82019-04-24 20:13:48257 WrapperPtr CreatePeerConnectionWithMdns(const RTCConfiguration& config) {
258 auto resolver_factory =
Mirko Bonadei317a1f02019-09-17 15:06:18259 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
Jeroen de Borstaf242c82019-04-24 20:13:48260
261 webrtc::PeerConnectionDependencies deps(nullptr /* observer_in */);
262
263 auto fake_network = NewFakeNetwork();
Qingsi Wangecd30542019-05-22 21:34:56264 fake_network->set_mdns_responder(
Mirko Bonadei317a1f02019-09-17 15:06:18265 std::make_unique<webrtc::FakeMdnsResponder>(rtc::Thread::Current()));
Jeroen de Borstaf242c82019-04-24 20:13:48266 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:41274 return CreatePeerConnection(
275 config, PeerConnectionFactoryInterface::Options(), std::move(deps));
Jeroen de Borstaf242c82019-04-24 20:13:48276 }
277
Harald Alvestrand19793842018-06-25 10:03:50278 WrapperPtr CreatePeerConnectionWithImmediateReport() {
Harald Alvestrand62166932020-10-26 08:30:41279 RTCConfiguration configuration;
Henrik Boström62995db2022-01-03 08:58:10280 configuration.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan;
Harald Alvestrand62166932020-10-26 08:30:41281 configuration.report_usage_pattern_delay_ms = 0;
282 return CreatePeerConnection(
283 configuration, PeerConnectionFactoryInterface::Options(), nullptr);
Harald Alvestrand056d8112018-07-16 17:18:58284 }
285
286 WrapperPtr CreatePeerConnectionWithPrivateLocalAddresses() {
Jeroen de Borstaf242c82019-04-24 20:13:48287 auto* fake_network = NewFakeNetwork();
288 fake_network->AddInterface(NextLocalAddress());
289 fake_network->AddInterface(kPrivateLocalAddress);
290
291 auto port_allocator =
Mirko Bonadei317a1f02019-09-17 15:06:18292 std::make_unique<cricket::BasicPortAllocator>(fake_network);
Henrik Boström62995db2022-01-03 08:58:10293 RTCConfiguration config;
294 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
295 return CreatePeerConnection(config,
Harald Alvestrand056d8112018-07-16 17:18:58296 PeerConnectionFactoryInterface::Options(),
Harald Alvestrand62166932020-10-26 08:30:41297 std::move(port_allocator));
Harald Alvestrand19793842018-06-25 10:03:50298 }
299
Qingsi Wang1ba5dec2019-08-19 18:57:17300 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 15:06:18306 std::make_unique<cricket::BasicPortAllocator>(fake_network);
Qingsi Wang1ba5dec2019-08-19 18:57:17307
Henrik Boström62995db2022-01-03 08:58:10308 RTCConfiguration config;
309 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
310 return CreatePeerConnection(config,
Qingsi Wang1ba5dec2019-08-19 18:57:17311 PeerConnectionFactoryInterface::Options(),
Harald Alvestrand62166932020-10-26 08:30:41312 std::move(port_allocator));
Qingsi Wang1ba5dec2019-08-19 18:57:17313 }
314
Harald Alvestrand19793842018-06-25 10:03:50315 WrapperPtr CreatePeerConnection(
316 const RTCConfiguration& config,
317 const PeerConnectionFactoryInterface::Options factory_options,
Harald Alvestrand62166932020-10-26 08:30:41318 std::unique_ptr<cricket::PortAllocator> allocator) {
Jeroen de Borstaf242c82019-04-24 20:13:48319 PeerConnectionDependencies deps(nullptr);
320 deps.allocator = std::move(allocator);
321
Harald Alvestrand62166932020-10-26 08:30:41322 return CreatePeerConnection(config, factory_options, std::move(deps));
Jeroen de Borstaf242c82019-04-24 20:13:48323 }
324
325 WrapperPtr CreatePeerConnection(
326 const RTCConfiguration& config,
327 const PeerConnectionFactoryInterface::Options factory_options,
Harald Alvestrand62166932020-10-26 08:30:41328 PeerConnectionDependencies deps) {
Harald Alvestrand19793842018-06-25 10:03:50329 rtc::scoped_refptr<PeerConnectionFactoryForUsageHistogramTest> pc_factory(
330 new PeerConnectionFactoryForUsageHistogramTest());
331 pc_factory->SetOptions(factory_options);
Jeroen de Borstaf242c82019-04-24 20:13:48332
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 15:06:18339 std::make_unique<cricket::BasicPortAllocator>(fake_network);
Jeroen de Borstaf242c82019-04-24 20:13:48340 }
341
Mirko Bonadei317a1f02019-09-17 15:06:18342 auto observer = std::make_unique<ObserverForUsageHistogramTest>();
Jeroen de Borstaf242c82019-04-24 20:13:48343 deps.observer = observer.get();
344
345 auto pc = pc_factory->CreatePeerConnection(config, std::move(deps));
Harald Alvestrand19793842018-06-25 10:03:50346 if (!pc) {
347 return nullptr;
348 }
349
Yves Gerey4e933292018-10-31 14:36:05350 observer->SetPeerConnectionInterface(pc.get());
Mirko Bonadei317a1f02019-09-17 15:06:18351 auto wrapper = std::make_unique<PeerConnectionWrapperForUsageHistogramTest>(
352 pc_factory, pc, std::move(observer));
Harald Alvestrand19793842018-06-25 10:03:50353 return wrapper;
354 }
355
Harald Alvestrandc0e97252018-07-26 08:39:55356 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 20:13:48363 // 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 15:06:18369 fake_networks_.emplace_back(std::make_unique<rtc::FakeNetworkManager>());
Jeroen de Borstaf242c82019-04-24 20:13:48370 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 10:03:50380 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 10:03:50387 int expected_fingerprint = MakeUsageFingerprint({});
Ying Wangef3998f2019-12-09 12:06:53388 EXPECT_METRIC_EQ_WAIT(1, webrtc::metrics::NumSamples(kUsagePatternMetric),
389 kDefaultTimeout);
390 EXPECT_METRIC_EQ(
Harald Alvestrandc0e97252018-07-26 08:39:55391 1, webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint));
Harald Alvestrand19793842018-06-25 10:03:50392}
393
Harald Alvestrand183e09d2018-06-28 10:04:41394#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 10:04:41402 caller->AddAudioTrack("audio");
403 caller->AddVideoTrack("video");
Harald Alvestrand42386282018-07-12 05:56:05404 ASSERT_TRUE(caller->ConnectTo(callee.get()));
Harald Alvestrand183e09d2018-06-28 10:04:41405 caller->pc()->Close();
406 callee->pc()->Close();
407 int expected_fingerprint = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53408 {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 17:18:58414 // In this case, we may or may not have PRIVATE_CANDIDATE_COLLECTED,
415 // depending on the machine configuration.
Ying Wangef3998f2019-12-09 12:06:53416 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
417 EXPECT_METRIC_TRUE(
Harald Alvestrandc0e97252018-07-26 08:39:55418 webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint) ==
419 2 ||
Harald Alvestrand056d8112018-07-16 17:18:58420 webrtc::metrics::NumEvents(
Harald Alvestrandc0e97252018-07-26 08:39:55421 kUsagePatternMetric,
Harald Alvestrand056d8112018-07-16 17:18:58422 expected_fingerprint |
Harald Alvestrand44d0dff2020-10-09 05:43:53423 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) == 2);
Harald Alvestrand056d8112018-07-16 17:18:58424}
425
Qingsi Wang1ba5dec2019-08-19 18:57:17426// Test getting the usage fingerprint when the caller collects an mDNS
427// candidate.
Jeroen de Borstaf242c82019-04-24 20:13:48428TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithMdnsCaller) {
429 RTCConfiguration config;
Henrik Boström62995db2022-01-03 08:58:10430 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
Jeroen de Borstaf242c82019-04-24 20:13:48431
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:53443 {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 20:13:48450
Qingsi Wangcc46b10c2019-09-12 18:19:01451 // 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 20:13:48454 int expected_fingerprint_callee = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53455 {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 12:06:53461 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 20:13:48466}
467
Qingsi Wang1ba5dec2019-08-19 18:57:17468// Test getting the usage fingerprint when the callee collects an mDNS
469// candidate.
Jeroen de Borstaf242c82019-04-24 20:13:48470TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithMdnsCallee) {
471 RTCConfiguration config;
Henrik Boström62995db2022-01-03 08:58:10472 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
Jeroen de Borstaf242c82019-04-24 20:13:48473
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 Wangcc46b10c2019-09-12 18:19:01484 // Similar to the test above, the caller connects with the callee via a prflx
485 // candidate.
Jeroen de Borstaf242c82019-04-24 20:13:48486 int expected_fingerprint_caller = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53487 {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 20:13:48493
494 int expected_fingerprint_callee = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53495 {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 12:06:53502 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 20:13:48507}
508
Mirko Bonadei5eb43b42021-01-18 12:24:40509#ifdef WEBRTC_HAVE_SCTP
Harald Alvestrand183e09d2018-06-28 10:04:41510TEST_F(PeerConnectionUsageHistogramTest, FingerprintDataOnly) {
511 auto caller = CreatePeerConnection();
512 auto callee = CreatePeerConnection();
Harald Alvestrand183e09d2018-06-28 10:04:41513 caller->CreateDataChannel("foodata");
Harald Alvestrand42386282018-07-12 05:56:05514 ASSERT_TRUE(caller->ConnectTo(callee.get()));
Harald Alvestrand183e09d2018-06-28 10:04:41515 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:53519 {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 12:06:53524 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
525 EXPECT_METRIC_TRUE(
Harald Alvestrandc0e97252018-07-26 08:39:55526 webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint) ==
527 2 ||
Harald Alvestrand056d8112018-07-16 17:18:58528 webrtc::metrics::NumEvents(
Harald Alvestrandc0e97252018-07-26 08:39:55529 kUsagePatternMetric,
Harald Alvestrand056d8112018-07-16 17:18:58530 expected_fingerprint |
Harald Alvestrand44d0dff2020-10-09 05:43:53531 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) == 2);
Harald Alvestrand183e09d2018-06-28 10:04:41532}
Mirko Bonadei5eb43b42021-01-18 12:24:40533#endif // WEBRTC_HAVE_SCTP
Harald Alvestrand183e09d2018-06-28 10:04:41534#endif // WEBRTC_ANDROID
535
Harald Alvestrandb2a74782018-06-28 11:54:07536TEST_F(PeerConnectionUsageHistogramTest, FingerprintStunTurn) {
537 RTCConfiguration configuration;
Henrik Boström62995db2022-01-03 08:58:10538 configuration.sdp_semantics = SdpSemantics::kUnifiedPlan;
Harald Alvestrandb2a74782018-06-28 11:54:07539 PeerConnection::IceServer server;
Harald Alvestranda3dd7722020-11-27 08:05:42540 server.urls = {"stun:dummy.stun.server"};
Harald Alvestrandb2a74782018-06-28 11:54:07541 configuration.servers.push_back(server);
Harald Alvestranda3dd7722020-11-27 08:05:42542 server.urls = {"turn:dummy.turn.server"};
Harald Alvestrandb2a74782018-06-28 11:54:07543 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 11:54:07548 caller->pc()->Close();
Harald Alvestrand44d0dff2020-10-09 05:43:53549 int expected_fingerprint = MakeUsageFingerprint(
550 {UsageEvent::STUN_SERVER_ADDED, UsageEvent::TURN_SERVER_ADDED,
551 UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 12:06:53552 EXPECT_METRIC_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric));
553 EXPECT_METRIC_EQ(
Harald Alvestrandc0e97252018-07-26 08:39:55554 1, webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint));
Harald Alvestrandb2a74782018-06-28 11:54:07555}
556
557TEST_F(PeerConnectionUsageHistogramTest, FingerprintStunTurnInReconfiguration) {
558 RTCConfiguration configuration;
Henrik Boström62995db2022-01-03 08:58:10559 configuration.sdp_semantics = SdpSemantics::kUnifiedPlan;
Harald Alvestrandb2a74782018-06-28 11:54:07560 PeerConnection::IceServer server;
Harald Alvestranda3dd7722020-11-27 08:05:42561 server.urls = {"stun:dummy.stun.server"};
Harald Alvestrandb2a74782018-06-28 11:54:07562 configuration.servers.push_back(server);
Harald Alvestranda3dd7722020-11-27 08:05:42563 server.urls = {"turn:dummy.turn.server"};
Harald Alvestrandb2a74782018-06-28 11:54:07564 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 07:58:17569 ASSERT_TRUE(caller->pc()->SetConfiguration(configuration).ok());
Harald Alvestrandb2a74782018-06-28 11:54:07570 caller->pc()->Close();
Harald Alvestrand44d0dff2020-10-09 05:43:53571 int expected_fingerprint = MakeUsageFingerprint(
572 {UsageEvent::STUN_SERVER_ADDED, UsageEvent::TURN_SERVER_ADDED,
573 UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 12:06:53574 EXPECT_METRIC_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric));
575 EXPECT_METRIC_EQ(
Harald Alvestrandc0e97252018-07-26 08:39:55576 1, webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint));
Harald Alvestrandb2a74782018-06-28 11:54:07577}
578
Jeroen de Borstaf242c82019-04-24 20:13:48579TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithPrivateIPCaller) {
Harald Alvestrand056d8112018-07-16 17:18:58580 auto caller = CreatePeerConnectionWithPrivateLocalAddresses();
Jeroen de Borstaf242c82019-04-24 20:13:48581 auto callee = CreatePeerConnection();
Harald Alvestrand056d8112018-07-16 17:18:58582 caller->AddAudioTrack("audio");
Jeroen de Borstaf242c82019-04-24 20:13:48583 ASSERT_TRUE(caller->ConnectTo(callee.get()));
Harald Alvestrand056d8112018-07-16 17:18:58584 caller->pc()->Close();
Jeroen de Borstaf242c82019-04-24 20:13:48585 callee->pc()->Close();
586
587 int expected_fingerprint_caller = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53588 {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 20:13:48594
595 int expected_fingerprint_callee = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53596 {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 12:06:53602 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 20:13:48607}
608
Qingsi Wang1ba5dec2019-08-19 18:57:17609TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithPrivateIpv6Callee) {
Jeroen de Borstaf242c82019-04-24 20:13:48610 auto caller = CreatePeerConnection();
Qingsi Wang1ba5dec2019-08-19 18:57:17611 auto callee = CreatePeerConnectionWithPrivateIpv6LocalAddresses();
Jeroen de Borstaf242c82019-04-24 20:13:48612 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:53618 {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 20:13:48625
626 int expected_fingerprint_callee = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53627 {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 12:06:53634 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 17:18:58639}
640
Harald Alvestrandc0e97252018-07-26 08:39:55641#ifndef WEBRTC_ANDROID
Mirko Bonadei5eb43b42021-01-18 12:24:40642#ifdef WEBRTC_HAVE_SCTP
Qingsi Wang1ba5dec2019-08-19 18:57:17643// 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 08:58:10655 RTCConfiguration config;
656 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
657 auto callee = CreatePeerConnectionWithMdns(config);
Qingsi Wang1ba5dec2019-08-19 18:57:17658 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 20:30:23667 // Note that we cannot use CloneSessionDescription on `cur_offer` to obtain an
Qingsi Wang1ba5dec2019-08-19 18:57:17668 // 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 15:06:18675 auto offer = std::make_unique<JsepSessionDescription>(SdpType::kOffer);
Qingsi Wang1ba5dec2019-08-19 18:57:17676 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 Wangcc46b10c2019-09-12 18:19:01693 // 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 18:57:17696 int expected_fingerprint_caller = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53697 {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 18:57:17702
703 int expected_fingerprint_callee = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53704 {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 12:06:53711 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 18:57:17716}
717
Harald Alvestrandc0e97252018-07-26 08:39:55718TEST_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:53724 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
725 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 12:06:53726 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:53730 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) ==
Ying Wangef3998f2019-12-09 12:06:53731 ObservedFingerprint());
732 EXPECT_METRIC_EQ(absl::make_optional(ObservedFingerprint()),
733 caller->observer()->interesting_usage_detected());
Harald Alvestrandc0e97252018-07-26 08:39:55734}
Harald Alvestrand7a1c7f72018-08-01 08:50:16735
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:53741 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
742 UsageEvent::CANDIDATE_COLLECTED});
Ying Wangef3998f2019-12-09 12:06:53743 EXPECT_METRIC_EQ(0, webrtc::metrics::NumSamples(kUsagePatternMetric));
Harald Alvestrand7a1c7f72018-08-01 08:50:16744 caller->GetInternalPeerConnection()->RequestUsagePatternReportForTesting();
Ying Wangef3998f2019-12-09 12:06:53745 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:53750 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) ==
Ying Wangef3998f2019-12-09 12:06:53751 ObservedFingerprint());
752 EXPECT_METRIC_EQ(absl::make_optional(ObservedFingerprint()),
753 caller->observer()->interesting_usage_detected());
Harald Alvestrand7a1c7f72018-08-01 08:50:16754}
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:53762 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
763 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::CLOSE_CALLED});
Ying Wangef3998f2019-12-09 12:06:53764 EXPECT_METRIC_EQ(0, webrtc::metrics::NumSamples(kUsagePatternMetric));
Harald Alvestrand7a1c7f72018-08-01 08:50:16765 caller->pc()->Close();
Ying Wangef3998f2019-12-09 12:06:53766 EXPECT_METRIC_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric));
Harald Alvestrand7a1c7f72018-08-01 08:50:16767 caller->GetInternalPeerConnection()->RequestUsagePatternReportForTesting();
768 caller->observer()->ClearInterestingUsageDetector();
Ying Wangef3998f2019-12-09 12:06:53769 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:53774 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) ==
Ying Wangef3998f2019-12-09 12:06:53775 ObservedFingerprint());
Harald Alvestrand7a1c7f72018-08-01 08:50:16776 // After close, the usage-detection callback should NOT have been called.
Ying Wangef3998f2019-12-09 12:06:53777 EXPECT_METRIC_FALSE(caller->observer()->interesting_usage_detected());
Harald Alvestrand7a1c7f72018-08-01 08:50:16778}
Harald Alvestrandc0e97252018-07-26 08:39:55779#endif
780#endif
781
Harald Alvestrand19793842018-06-25 10:03:50782} // namespace webrtc