blob: 0697c672ee75ef1611f31ecc8524c32b0c58bb34 [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>
Florent Castelli8037fc62024-08-29 13:00:4012#include <optional>
Yves Gerey3e707812018-11-28 15:47:4913#include <set>
14#include <string>
15#include <utility>
16#include <vector>
Harald Alvestrand19793842018-06-25 10:03:5017
Harald Alvestrand42386282018-07-12 05:56:0518#include "api/jsep.h"
Qingsi Wang1ba5dec2019-08-19 18:57:1719#include "api/jsep_session_description.h"
Steve Anton10542f22019-01-11 17:11:0020#include "api/peer_connection_interface.h"
Steve Anton10542f22019-01-11 17:11:0021#include "api/rtc_error.h"
Mirko Bonadeid9708072019-01-25 19:26:4822#include "api/scoped_refptr.h"
Danil Chapovalov53d45ba2019-07-03 12:56:3323#include "api/task_queue/default_task_queue_factory.h"
Harald Alvestrandc24a2182022-02-23 13:44:5924#include "api/task_queue/task_queue_factory.h"
Harald Alvestrand4d25a772023-08-25 11:07:2825#include "api/test/mock_async_dns_resolver.h"
Harald Alvestrandc24a2182022-02-23 13:44:5926#include "media/base/media_engine.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"
Danil Chapovalovc63120a2023-11-03 10:32:2434#include "pc/test/enable_fake_media.h"
Steve Anton10542f22019-01-11 17:11:0035#include "pc/test/mock_peer_connection_observers.h"
Harald Alvestrand44d0dff2020-10-09 05:43:5336#include "pc/usage_pattern.h"
Qingsi Wang1ba5dec2019-08-19 18:57:1737#include "pc/webrtc_sdp.h"
Jeroen de Borstaf242c82019-04-24 20:13:4838#include "rtc_base/arraysize.h"
Yves Gerey3e707812018-11-28 15:47:4939#include "rtc_base/checks.h"
Qingsi Wangecd30542019-05-22 21:34:5640#include "rtc_base/fake_mdns_responder.h"
Steve Anton10542f22019-01-11 17:11:0041#include "rtc_base/fake_network.h"
Harald Alvestrand19793842018-06-25 10:03:5042#include "rtc_base/gunit.h"
Harald Alvestrandc24a2182022-02-23 13:44:5943#include "rtc_base/mdns_responder_interface.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 Alvestrandc24a2182022-02-23 13:44:5949#include "test/gtest.h"
Harald Alvestrand19793842018-06-25 10:03:5050
51namespace webrtc {
52
53using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
54using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
Jeroen de Borstaf242c82019-04-24 20:13:4855using ::testing::NiceMock;
Harald Alvestrand19793842018-06-25 10:03:5056using ::testing::Values;
57
Harald Alvestrandc0e97252018-07-26 08:39:5558static const char kUsagePatternMetric[] = "WebRTC.PeerConnection.UsagePattern";
Harald Alvestrand19793842018-06-25 10:03:5059static constexpr int kDefaultTimeout = 10000;
Jeroen de Borstaf242c82019-04-24 20:13:4860static const rtc::SocketAddress kLocalAddrs[2] = {
61 rtc::SocketAddress("1.1.1.1", 0), rtc::SocketAddress("2.2.2.2", 0)};
Harald Alvestrand056d8112018-07-16 17:18:5862static const rtc::SocketAddress kPrivateLocalAddress("10.1.1.1", 0);
Qingsi Wang1ba5dec2019-08-19 18:57:1763static const rtc::SocketAddress kPrivateIpv6LocalAddress("fd12:3456:789a:1::1",
64 0);
Harald Alvestrand19793842018-06-25 10:03:5065
Harald Alvestrand44d0dff2020-10-09 05:43:5366int MakeUsageFingerprint(std::set<UsageEvent> events) {
Harald Alvestrand19793842018-06-25 10:03:5067 int signature = 0;
68 for (const auto it : events) {
69 signature |= static_cast<int>(it);
70 }
71 return signature;
72}
73
74class PeerConnectionFactoryForUsageHistogramTest
Niels Möllerdf411872022-03-24 15:13:2575 : public PeerConnectionFactory {
Harald Alvestrand19793842018-06-25 10:03:5076 public:
77 PeerConnectionFactoryForUsageHistogramTest()
Niels Möllerdf411872022-03-24 15:13:2578 : PeerConnectionFactory([] {
Danil Chapovalovf5258be2019-03-19 16:45:2479 PeerConnectionFactoryDependencies dependencies;
80 dependencies.network_thread = rtc::Thread::Current();
81 dependencies.worker_thread = rtc::Thread::Current();
82 dependencies.signaling_thread = rtc::Thread::Current();
Danil Chapovalov53d45ba2019-07-03 12:56:3383 dependencies.task_queue_factory = CreateDefaultTaskQueueFactory();
Danil Chapovalovc63120a2023-11-03 10:32:2484 EnableFakeMedia(dependencies);
Danil Chapovalovf5258be2019-03-19 16:45:2485 return dependencies;
86 }()) {}
Harald Alvestrand183e09d2018-06-28 10:04:4187};
88
89class PeerConnectionWrapperForUsageHistogramTest;
Yves Gerey3e707812018-11-28 15:47:4990
Harald Alvestrand183e09d2018-06-28 10:04:4191typedef PeerConnectionWrapperForUsageHistogramTest* RawWrapperPtr;
92
93class ObserverForUsageHistogramTest : public MockPeerConnectionObserver {
94 public:
Harald Alvestranda6544372023-11-13 09:33:5695 void OnIceCandidate(const IceCandidateInterface* candidate) override;
Harald Alvestrandc0e97252018-07-26 08:39:5596
97 void OnInterestingUsage(int usage_pattern) override {
98 interesting_usage_detected_ = usage_pattern;
99 }
100
Harald Alvestrand183e09d2018-06-28 10:04:41101 void PrepareToExchangeCandidates(RawWrapperPtr other) {
102 candidate_target_ = other;
103 }
Harald Alvestrandc0e97252018-07-26 08:39:55104
Niels Möller94327682022-04-28 11:20:29105 bool HaveDataChannel() { return last_datachannel_ != nullptr; }
Harald Alvestrand183e09d2018-06-28 10:04:41106
Florent Castelli8037fc62024-08-29 13:00:40107 std::optional<int> interesting_usage_detected() {
Harald Alvestrandc0e97252018-07-26 08:39:55108 return interesting_usage_detected_;
109 }
110
Harald Alvestrand7a1c7f72018-08-01 08:50:16111 void ClearInterestingUsageDetector() {
Florent Castelli8037fc62024-08-29 13:00:40112 interesting_usage_detected_ = std::optional<int>();
Harald Alvestrand7a1c7f72018-08-01 08:50:16113 }
114
Qingsi Wang1ba5dec2019-08-19 18:57:17115 bool candidate_gathered() const { return candidate_gathered_; }
116
Harald Alvestrand183e09d2018-06-28 10:04:41117 private:
Florent Castelli8037fc62024-08-29 13:00:40118 std::optional<int> interesting_usage_detected_;
Qingsi Wang1ba5dec2019-08-19 18:57:17119 bool candidate_gathered_ = false;
Harald Alvestrand183e09d2018-06-28 10:04:41120 RawWrapperPtr candidate_target_; // Note: Not thread-safe against deletions.
Harald Alvestrand19793842018-06-25 10:03:50121};
122
123class PeerConnectionWrapperForUsageHistogramTest
124 : public PeerConnectionWrapper {
125 public:
126 using PeerConnectionWrapper::PeerConnectionWrapper;
127
128 PeerConnection* GetInternalPeerConnection() {
129 auto* pci =
130 static_cast<PeerConnectionProxyWithInternal<PeerConnectionInterface>*>(
131 pc());
132 return static_cast<PeerConnection*>(pci->internal());
133 }
Harald Alvestrand183e09d2018-06-28 10:04:41134
Harald Alvestrandc0e97252018-07-26 08:39:55135 // Override with different return type
136 ObserverForUsageHistogramTest* observer() {
137 return static_cast<ObserverForUsageHistogramTest*>(
138 PeerConnectionWrapper::observer());
139 }
140
Harald Alvestrand183e09d2018-06-28 10:04:41141 void PrepareToExchangeCandidates(
142 PeerConnectionWrapperForUsageHistogramTest* other) {
Harald Alvestrandc0e97252018-07-26 08:39:55143 observer()->PrepareToExchangeCandidates(other);
144 other->observer()->PrepareToExchangeCandidates(this);
Harald Alvestrand183e09d2018-06-28 10:04:41145 }
146
147 bool IsConnected() {
148 return pc()->ice_connection_state() ==
149 PeerConnectionInterface::kIceConnectionConnected ||
150 pc()->ice_connection_state() ==
151 PeerConnectionInterface::kIceConnectionCompleted;
152 }
153
154 bool HaveDataChannel() {
155 return static_cast<ObserverForUsageHistogramTest*>(observer())
156 ->HaveDataChannel();
157 }
Harald Alvestranda6544372023-11-13 09:33:56158 void BufferIceCandidate(const IceCandidateInterface* candidate) {
Qingsi Wang32913c12019-10-30 23:03:46159 std::string sdp;
160 EXPECT_TRUE(candidate->ToString(&sdp));
Harald Alvestranda6544372023-11-13 09:33:56161 std::unique_ptr<IceCandidateInterface> candidate_copy(CreateIceCandidate(
162 candidate->sdp_mid(), candidate->sdp_mline_index(), sdp, nullptr));
Qingsi Wang32913c12019-10-30 23:03:46163 buffered_candidates_.push_back(std::move(candidate_copy));
Harald Alvestrand42386282018-07-12 05:56:05164 }
Harald Alvestrand056d8112018-07-16 17:18:58165
Harald Alvestrand42386282018-07-12 05:56:05166 void AddBufferedIceCandidates() {
167 for (const auto& candidate : buffered_candidates_) {
168 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
169 }
170 buffered_candidates_.clear();
171 }
Harald Alvestrand056d8112018-07-16 17:18:58172
Qingsi Wang32913c12019-10-30 23:03:46173 // This method performs the following actions in sequence:
174 // 1. Exchange Offer and Answer.
175 // 2. Exchange ICE candidates after both caller and callee complete
176 // gathering.
177 // 3. Wait for ICE to connect.
178 //
179 // This guarantees a deterministic sequence of events and also rules out the
180 // occurrence of prflx candidates if the offer/answer signaling and the
181 // candidate trickling race in order. In case prflx candidates need to be
182 // simulated, see the approach used by tests below for that.
Harald Alvestrand42386282018-07-12 05:56:05183 bool ConnectTo(PeerConnectionWrapperForUsageHistogramTest* callee) {
184 PrepareToExchangeCandidates(callee);
Harald Alvestrand056d8112018-07-16 17:18:58185 if (!ExchangeOfferAnswerWith(callee)) {
186 return false;
187 }
Qingsi Wang32913c12019-10-30 23:03:46188 // Wait until the gathering completes before we signal the candidate.
189 WAIT(observer()->ice_gathering_complete_, kDefaultTimeout);
190 WAIT(callee->observer()->ice_gathering_complete_, kDefaultTimeout);
Harald Alvestrand42386282018-07-12 05:56:05191 AddBufferedIceCandidates();
192 callee->AddBufferedIceCandidates();
Harald Alvestrand056d8112018-07-16 17:18:58193 WAIT(IsConnected(), kDefaultTimeout);
194 WAIT(callee->IsConnected(), kDefaultTimeout);
Harald Alvestrand42386282018-07-12 05:56:05195 return IsConnected() && callee->IsConnected();
196 }
197
Harald Alvestrand056d8112018-07-16 17:18:58198 bool GenerateOfferAndCollectCandidates() {
199 auto offer = CreateOffer(RTCOfferAnswerOptions());
200 if (!offer) {
201 return false;
202 }
203 bool set_local_offer =
204 SetLocalDescription(CloneSessionDescription(offer.get()));
205 EXPECT_TRUE(set_local_offer);
206 if (!set_local_offer) {
207 return false;
208 }
209 EXPECT_TRUE_WAIT(observer()->ice_gathering_complete_, kDefaultTimeout);
210 return true;
211 }
212
Harald Alvestranda6544372023-11-13 09:33:56213 PeerConnectionInterface::IceGatheringState ice_gathering_state() {
Qingsi Wang1ba5dec2019-08-19 18:57:17214 return pc()->ice_gathering_state();
215 }
216
Harald Alvestrand42386282018-07-12 05:56:05217 private:
218 // Candidates that have been sent but not yet configured
Harald Alvestranda6544372023-11-13 09:33:56219 std::vector<std::unique_ptr<IceCandidateInterface>> buffered_candidates_;
Harald Alvestrand19793842018-06-25 10:03:50220};
221
Qingsi Wang32913c12019-10-30 23:03:46222// Buffers candidates until we add them via AddBufferedIceCandidates.
Harald Alvestrand183e09d2018-06-28 10:04:41223void ObserverForUsageHistogramTest::OnIceCandidate(
Harald Alvestranda6544372023-11-13 09:33:56224 const IceCandidateInterface* candidate) {
Qingsi Wang1ba5dec2019-08-19 18:57:17225 // If target is not set, ignore. This happens in one-ended unit tests.
Harald Alvestrand183e09d2018-06-28 10:04:41226 if (candidate_target_) {
Qingsi Wang32913c12019-10-30 23:03:46227 this->candidate_target_->BufferIceCandidate(candidate);
Harald Alvestrand183e09d2018-06-28 10:04:41228 }
Qingsi Wang1ba5dec2019-08-19 18:57:17229 candidate_gathered_ = true;
Harald Alvestrand183e09d2018-06-28 10:04:41230}
231
Harald Alvestrand19793842018-06-25 10:03:50232class PeerConnectionUsageHistogramTest : public ::testing::Test {
233 protected:
234 typedef std::unique_ptr<PeerConnectionWrapperForUsageHistogramTest>
235 WrapperPtr;
236
237 PeerConnectionUsageHistogramTest()
Tommi3756e292023-09-01 10:45:06238 : vss_(new rtc::VirtualSocketServer()),
239 socket_factory_(new rtc::BasicPacketSocketFactory(vss_.get())),
240 main_(vss_.get()) {
Harald Alvestranda6544372023-11-13 09:33:56241 metrics::Reset();
Harald Alvestrand19793842018-06-25 10:03:50242 }
243
244 WrapperPtr CreatePeerConnection() {
Henrik Boström62995db2022-01-03 08:58:10245 RTCConfiguration config;
Harald Alvestranda6544372023-11-13 09:33:56246 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
Harald Alvestrand62166932020-10-26 08:30:41247 return CreatePeerConnection(
Henrik Boström62995db2022-01-03 08:58:10248 config, PeerConnectionFactoryInterface::Options(), nullptr);
Harald Alvestrand19793842018-06-25 10:03:50249 }
250
Harald Alvestrandb2a74782018-06-28 11:54:07251 WrapperPtr CreatePeerConnection(const RTCConfiguration& config) {
252 return CreatePeerConnection(
Harald Alvestrand62166932020-10-26 08:30:41253 config, PeerConnectionFactoryInterface::Options(), nullptr);
Harald Alvestrandb2a74782018-06-28 11:54:07254 }
255
Jeroen de Borstaf242c82019-04-24 20:13:48256 WrapperPtr CreatePeerConnectionWithMdns(const RTCConfiguration& config) {
257 auto resolver_factory =
Harald Alvestranda6544372023-11-13 09:33:56258 std::make_unique<NiceMock<MockAsyncDnsResolverFactory>>();
Jeroen de Borstaf242c82019-04-24 20:13:48259
Harald Alvestranda6544372023-11-13 09:33:56260 PeerConnectionDependencies deps(nullptr /* observer_in */);
Jeroen de Borstaf242c82019-04-24 20:13:48261
262 auto fake_network = NewFakeNetwork();
Qingsi Wangecd30542019-05-22 21:34:56263 fake_network->set_mdns_responder(
Harald Alvestranda6544372023-11-13 09:33:56264 std::make_unique<FakeMdnsResponder>(rtc::Thread::Current()));
Jeroen de Borstaf242c82019-04-24 20:13:48265 fake_network->AddInterface(NextLocalAddress());
266
267 std::unique_ptr<cricket::BasicPortAllocator> port_allocator(
Tommi3756e292023-09-01 10:45:06268 new cricket::BasicPortAllocator(fake_network, socket_factory_.get()));
Jeroen de Borstaf242c82019-04-24 20:13:48269
Harald Alvestrand4d25a772023-08-25 11:07:28270 deps.async_dns_resolver_factory = std::move(resolver_factory);
Jeroen de Borstaf242c82019-04-24 20:13:48271 deps.allocator = std::move(port_allocator);
272
Harald Alvestrand62166932020-10-26 08:30:41273 return CreatePeerConnection(
274 config, PeerConnectionFactoryInterface::Options(), std::move(deps));
Jeroen de Borstaf242c82019-04-24 20:13:48275 }
276
Harald Alvestrand19793842018-06-25 10:03:50277 WrapperPtr CreatePeerConnectionWithImmediateReport() {
Harald Alvestrand62166932020-10-26 08:30:41278 RTCConfiguration configuration;
Harald Alvestranda6544372023-11-13 09:33:56279 configuration.sdp_semantics = SdpSemantics::kUnifiedPlan;
Harald Alvestrand62166932020-10-26 08:30:41280 configuration.report_usage_pattern_delay_ms = 0;
281 return CreatePeerConnection(
282 configuration, PeerConnectionFactoryInterface::Options(), nullptr);
Harald Alvestrand056d8112018-07-16 17:18:58283 }
284
285 WrapperPtr CreatePeerConnectionWithPrivateLocalAddresses() {
Jeroen de Borstaf242c82019-04-24 20:13:48286 auto* fake_network = NewFakeNetwork();
287 fake_network->AddInterface(NextLocalAddress());
288 fake_network->AddInterface(kPrivateLocalAddress);
289
Byoungchan Leed197e0b2022-05-30 14:59:55290 auto port_allocator = std::make_unique<cricket::BasicPortAllocator>(
Tommi3756e292023-09-01 10:45:06291 fake_network, socket_factory_.get());
Henrik Boström62995db2022-01-03 08:58:10292 RTCConfiguration config;
293 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
294 return CreatePeerConnection(config,
Harald Alvestrand056d8112018-07-16 17:18:58295 PeerConnectionFactoryInterface::Options(),
Harald Alvestrand62166932020-10-26 08:30:41296 std::move(port_allocator));
Harald Alvestrand19793842018-06-25 10:03:50297 }
298
Qingsi Wang1ba5dec2019-08-19 18:57:17299 WrapperPtr CreatePeerConnectionWithPrivateIpv6LocalAddresses() {
300 auto* fake_network = NewFakeNetwork();
301 fake_network->AddInterface(NextLocalAddress());
302 fake_network->AddInterface(kPrivateIpv6LocalAddress);
303
Byoungchan Leed197e0b2022-05-30 14:59:55304 auto port_allocator = std::make_unique<cricket::BasicPortAllocator>(
Tommi3756e292023-09-01 10:45:06305 fake_network, socket_factory_.get());
Qingsi Wang1ba5dec2019-08-19 18:57:17306
Henrik Boström62995db2022-01-03 08:58:10307 RTCConfiguration config;
308 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
309 return CreatePeerConnection(config,
Qingsi Wang1ba5dec2019-08-19 18:57:17310 PeerConnectionFactoryInterface::Options(),
Harald Alvestrand62166932020-10-26 08:30:41311 std::move(port_allocator));
Qingsi Wang1ba5dec2019-08-19 18:57:17312 }
313
Harald Alvestrand19793842018-06-25 10:03:50314 WrapperPtr CreatePeerConnection(
315 const RTCConfiguration& config,
316 const PeerConnectionFactoryInterface::Options factory_options,
Harald Alvestrand62166932020-10-26 08:30:41317 std::unique_ptr<cricket::PortAllocator> allocator) {
Jeroen de Borstaf242c82019-04-24 20:13:48318 PeerConnectionDependencies deps(nullptr);
319 deps.allocator = std::move(allocator);
320
Harald Alvestrand62166932020-10-26 08:30:41321 return CreatePeerConnection(config, factory_options, std::move(deps));
Jeroen de Borstaf242c82019-04-24 20:13:48322 }
323
324 WrapperPtr CreatePeerConnection(
325 const RTCConfiguration& config,
326 const PeerConnectionFactoryInterface::Options factory_options,
Harald Alvestrand62166932020-10-26 08:30:41327 PeerConnectionDependencies deps) {
Niels Möllerdf411872022-03-24 15:13:25328 auto pc_factory =
329 rtc::make_ref_counted<PeerConnectionFactoryForUsageHistogramTest>();
Harald Alvestrand19793842018-06-25 10:03:50330 pc_factory->SetOptions(factory_options);
Jeroen de Borstaf242c82019-04-24 20:13:48331
332 // If no allocator is provided, one will be created using a network manager
333 // that uses the host network. This doesn't work on all trybots.
334 if (!deps.allocator) {
335 auto fake_network = NewFakeNetwork();
336 fake_network->AddInterface(NextLocalAddress());
Byoungchan Leed197e0b2022-05-30 14:59:55337 deps.allocator = std::make_unique<cricket::BasicPortAllocator>(
Tommi3756e292023-09-01 10:45:06338 fake_network, socket_factory_.get());
Jeroen de Borstaf242c82019-04-24 20:13:48339 }
340
Mirko Bonadei317a1f02019-09-17 15:06:18341 auto observer = std::make_unique<ObserverForUsageHistogramTest>();
Jeroen de Borstaf242c82019-04-24 20:13:48342 deps.observer = observer.get();
343
Florent Castelli72424402022-04-06 01:45:10344 auto result =
345 pc_factory->CreatePeerConnectionOrError(config, std::move(deps));
346 if (!result.ok()) {
Harald Alvestrand19793842018-06-25 10:03:50347 return nullptr;
348 }
349
Niels Möllerafb246b2022-04-20 12:26:50350 observer->SetPeerConnectionInterface(result.value().get());
Mirko Bonadei317a1f02019-09-17 15:06:18351 auto wrapper = std::make_unique<PeerConnectionWrapperForUsageHistogramTest>(
Florent Castelli72424402022-04-06 01:45:10352 pc_factory, result.MoveValue(), 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".
Harald Alvestranda6544372023-11-13 09:33:56360 return metrics::MinSample(kUsagePatternMetric);
Harald Alvestrandc0e97252018-07-26 08:39:55361 }
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_;
Tommi3756e292023-09-01 10:45:06381 std::unique_ptr<rtc::BasicPacketSocketFactory> socket_factory_;
Harald Alvestrand19793842018-06-25 10:03:50382 rtc::AutoSocketServerThread main_;
383};
384
385TEST_F(PeerConnectionUsageHistogramTest, UsageFingerprintHistogramFromTimeout) {
386 auto pc = CreatePeerConnectionWithImmediateReport();
387
Harald Alvestrand19793842018-06-25 10:03:50388 int expected_fingerprint = MakeUsageFingerprint({});
Harald Alvestranda6544372023-11-13 09:33:56389 EXPECT_METRIC_EQ_WAIT(1, metrics::NumSamples(kUsagePatternMetric),
Ying Wangef3998f2019-12-09 12:06:53390 kDefaultTimeout);
391 EXPECT_METRIC_EQ(
Harald Alvestranda6544372023-11-13 09:33:56392 1, metrics::NumEvents(kUsagePatternMetric, expected_fingerprint));
Harald Alvestrand19793842018-06-25 10:03:50393}
394
Harald Alvestrand183e09d2018-06-28 10:04:41395#ifndef WEBRTC_ANDROID
396// These tests do not work on Android. Why is unclear.
397// https://bugs.webrtc.org/9461
398
399// Test getting the usage fingerprint for an audio/video connection.
400TEST_F(PeerConnectionUsageHistogramTest, FingerprintAudioVideo) {
401 auto caller = CreatePeerConnection();
402 auto callee = CreatePeerConnection();
Harald Alvestrand183e09d2018-06-28 10:04:41403 caller->AddAudioTrack("audio");
404 caller->AddVideoTrack("video");
Harald Alvestrand42386282018-07-12 05:56:05405 ASSERT_TRUE(caller->ConnectTo(callee.get()));
Harald Alvestrand183e09d2018-06-28 10:04:41406 caller->pc()->Close();
407 callee->pc()->Close();
408 int expected_fingerprint = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53409 {UsageEvent::AUDIO_ADDED, UsageEvent::VIDEO_ADDED,
410 UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
411 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
412 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
413 UsageEvent::ICE_STATE_CONNECTED, UsageEvent::REMOTE_CANDIDATE_ADDED,
414 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Harald Alvestrand056d8112018-07-16 17:18:58415 // In this case, we may or may not have PRIVATE_CANDIDATE_COLLECTED,
416 // depending on the machine configuration.
Harald Alvestranda6544372023-11-13 09:33:56417 EXPECT_METRIC_EQ(2, metrics::NumSamples(kUsagePatternMetric));
Ying Wangef3998f2019-12-09 12:06:53418 EXPECT_METRIC_TRUE(
Harald Alvestranda6544372023-11-13 09:33:56419 metrics::NumEvents(kUsagePatternMetric, expected_fingerprint) == 2 ||
420 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});
Harald Alvestranda6544372023-11-13 09:33:56461 EXPECT_METRIC_EQ(2, metrics::NumSamples(kUsagePatternMetric));
462 EXPECT_METRIC_EQ(
463 1, metrics::NumEvents(kUsagePatternMetric, expected_fingerprint_caller));
464 EXPECT_METRIC_EQ(
465 1, metrics::NumEvents(kUsagePatternMetric, expected_fingerprint_callee));
Jeroen de 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});
Harald Alvestranda6544372023-11-13 09:33:56502 EXPECT_METRIC_EQ(2, metrics::NumSamples(kUsagePatternMetric));
503 EXPECT_METRIC_EQ(
504 1, metrics::NumEvents(kUsagePatternMetric, expected_fingerprint_caller));
505 EXPECT_METRIC_EQ(
506 1, metrics::NumEvents(kUsagePatternMetric, expected_fingerprint_callee));
Jeroen de 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});
Harald Alvestranda6544372023-11-13 09:33:56524 EXPECT_METRIC_EQ(2, metrics::NumSamples(kUsagePatternMetric));
Ying Wangef3998f2019-12-09 12:06:53525 EXPECT_METRIC_TRUE(
Harald Alvestranda6544372023-11-13 09:33:56526 metrics::NumEvents(kUsagePatternMetric, expected_fingerprint) == 2 ||
527 metrics::NumEvents(
Harald Alvestrandc0e97252018-07-26 08:39:55528 kUsagePatternMetric,
Harald Alvestrand056d8112018-07-16 17:18:58529 expected_fingerprint |
Harald Alvestrand44d0dff2020-10-09 05:43:53530 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) == 2);
Harald Alvestrand183e09d2018-06-28 10:04:41531}
Mirko Bonadei5eb43b42021-01-18 12:24:40532#endif // WEBRTC_HAVE_SCTP
Harald Alvestrand183e09d2018-06-28 10:04:41533#endif // WEBRTC_ANDROID
534
Harald Alvestrandb2a74782018-06-28 11:54:07535TEST_F(PeerConnectionUsageHistogramTest, FingerprintStunTurn) {
536 RTCConfiguration configuration;
Henrik Boström62995db2022-01-03 08:58:10537 configuration.sdp_semantics = SdpSemantics::kUnifiedPlan;
Harald Alvestrandb2a74782018-06-28 11:54:07538 PeerConnection::IceServer server;
Harald Alvestranda3dd7722020-11-27 08:05:42539 server.urls = {"stun:dummy.stun.server"};
Harald Alvestrandb2a74782018-06-28 11:54:07540 configuration.servers.push_back(server);
Harald Alvestranda3dd7722020-11-27 08:05:42541 server.urls = {"turn:dummy.turn.server"};
Harald Alvestrandb2a74782018-06-28 11:54:07542 server.username = "username";
543 server.password = "password";
544 configuration.servers.push_back(server);
545 auto caller = CreatePeerConnection(configuration);
546 ASSERT_TRUE(caller);
Harald Alvestrandb2a74782018-06-28 11:54:07547 caller->pc()->Close();
Harald Alvestrand44d0dff2020-10-09 05:43:53548 int expected_fingerprint = MakeUsageFingerprint(
549 {UsageEvent::STUN_SERVER_ADDED, UsageEvent::TURN_SERVER_ADDED,
550 UsageEvent::CLOSE_CALLED});
Harald Alvestranda6544372023-11-13 09:33:56551 EXPECT_METRIC_EQ(1, metrics::NumSamples(kUsagePatternMetric));
Ying Wangef3998f2019-12-09 12:06:53552 EXPECT_METRIC_EQ(
Harald Alvestranda6544372023-11-13 09:33:56553 1, metrics::NumEvents(kUsagePatternMetric, expected_fingerprint));
Harald Alvestrandb2a74782018-06-28 11:54:07554}
555
556TEST_F(PeerConnectionUsageHistogramTest, FingerprintStunTurnInReconfiguration) {
557 RTCConfiguration configuration;
Henrik Boström62995db2022-01-03 08:58:10558 configuration.sdp_semantics = SdpSemantics::kUnifiedPlan;
Harald Alvestrandb2a74782018-06-28 11:54:07559 PeerConnection::IceServer server;
Harald Alvestranda3dd7722020-11-27 08:05:42560 server.urls = {"stun:dummy.stun.server"};
Harald Alvestrandb2a74782018-06-28 11:54:07561 configuration.servers.push_back(server);
Harald Alvestranda3dd7722020-11-27 08:05:42562 server.urls = {"turn:dummy.turn.server"};
Harald Alvestrandb2a74782018-06-28 11:54:07563 server.username = "username";
564 server.password = "password";
565 configuration.servers.push_back(server);
566 auto caller = CreatePeerConnection();
567 ASSERT_TRUE(caller);
Niels Möller2579f0c2019-08-19 07:58:17568 ASSERT_TRUE(caller->pc()->SetConfiguration(configuration).ok());
Harald Alvestrandb2a74782018-06-28 11:54:07569 caller->pc()->Close();
Harald Alvestrand44d0dff2020-10-09 05:43:53570 int expected_fingerprint = MakeUsageFingerprint(
571 {UsageEvent::STUN_SERVER_ADDED, UsageEvent::TURN_SERVER_ADDED,
572 UsageEvent::CLOSE_CALLED});
Harald Alvestranda6544372023-11-13 09:33:56573 EXPECT_METRIC_EQ(1, metrics::NumSamples(kUsagePatternMetric));
Ying Wangef3998f2019-12-09 12:06:53574 EXPECT_METRIC_EQ(
Harald Alvestranda6544372023-11-13 09:33:56575 1, metrics::NumEvents(kUsagePatternMetric, expected_fingerprint));
Harald Alvestrandb2a74782018-06-28 11:54:07576}
577
Jeroen de Borstaf242c82019-04-24 20:13:48578TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithPrivateIPCaller) {
Harald Alvestrand056d8112018-07-16 17:18:58579 auto caller = CreatePeerConnectionWithPrivateLocalAddresses();
Jeroen de Borstaf242c82019-04-24 20:13:48580 auto callee = CreatePeerConnection();
Harald Alvestrand056d8112018-07-16 17:18:58581 caller->AddAudioTrack("audio");
Jeroen de Borstaf242c82019-04-24 20:13:48582 ASSERT_TRUE(caller->ConnectTo(callee.get()));
Harald Alvestrand056d8112018-07-16 17:18:58583 caller->pc()->Close();
Jeroen de Borstaf242c82019-04-24 20:13:48584 callee->pc()->Close();
585
586 int expected_fingerprint_caller = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53587 {UsageEvent::AUDIO_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
588 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
589 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::PRIVATE_CANDIDATE_COLLECTED,
590 UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED, UsageEvent::ICE_STATE_CONNECTED,
591 UsageEvent::REMOTE_CANDIDATE_ADDED,
592 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Jeroen de Borstaf242c82019-04-24 20:13:48593
594 int expected_fingerprint_callee = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53595 {UsageEvent::AUDIO_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
596 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
597 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
598 UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED,
599 UsageEvent::ICE_STATE_CONNECTED, UsageEvent::REMOTE_CANDIDATE_ADDED,
600 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Harald Alvestranda6544372023-11-13 09:33:56601 EXPECT_METRIC_EQ(2, metrics::NumSamples(kUsagePatternMetric));
602 EXPECT_METRIC_EQ(
603 1, metrics::NumEvents(kUsagePatternMetric, expected_fingerprint_caller));
604 EXPECT_METRIC_EQ(
605 1, metrics::NumEvents(kUsagePatternMetric, expected_fingerprint_callee));
Jeroen de Borstaf242c82019-04-24 20:13:48606}
607
Qingsi Wang1ba5dec2019-08-19 18:57:17608TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithPrivateIpv6Callee) {
Jeroen de Borstaf242c82019-04-24 20:13:48609 auto caller = CreatePeerConnection();
Qingsi Wang1ba5dec2019-08-19 18:57:17610 auto callee = CreatePeerConnectionWithPrivateIpv6LocalAddresses();
Jeroen de Borstaf242c82019-04-24 20:13:48611 caller->AddAudioTrack("audio");
612 ASSERT_TRUE(caller->ConnectTo(callee.get()));
613 caller->pc()->Close();
614 callee->pc()->Close();
615
616 int expected_fingerprint_caller = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53617 {UsageEvent::AUDIO_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
618 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
619 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
620 UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED,
621 UsageEvent::ICE_STATE_CONNECTED, UsageEvent::REMOTE_CANDIDATE_ADDED,
622 UsageEvent::REMOTE_IPV6_CANDIDATE_ADDED,
623 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Jeroen de Borstaf242c82019-04-24 20:13:48624
625 int expected_fingerprint_callee = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53626 {UsageEvent::AUDIO_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
627 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
628 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::PRIVATE_CANDIDATE_COLLECTED,
629 UsageEvent::IPV6_CANDIDATE_COLLECTED,
630 UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
631 UsageEvent::REMOTE_CANDIDATE_ADDED, UsageEvent::ICE_STATE_CONNECTED,
632 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Harald Alvestranda6544372023-11-13 09:33:56633 EXPECT_METRIC_EQ(2, metrics::NumSamples(kUsagePatternMetric));
634 EXPECT_METRIC_EQ(
635 1, metrics::NumEvents(kUsagePatternMetric, expected_fingerprint_caller));
636 EXPECT_METRIC_EQ(
637 1, metrics::NumEvents(kUsagePatternMetric, expected_fingerprint_callee));
Harald Alvestrand056d8112018-07-16 17:18:58638}
639
Harald Alvestrandc0e97252018-07-26 08:39:55640#ifndef WEBRTC_ANDROID
Mirko Bonadei5eb43b42021-01-18 12:24:40641#ifdef WEBRTC_HAVE_SCTP
Qingsi Wang1ba5dec2019-08-19 18:57:17642// Test that the usage pattern bits for adding remote (private IPv6) candidates
643// are set when the remote candidates are retrieved from the Offer SDP instead
644// of trickled ICE messages.
645TEST_F(PeerConnectionUsageHistogramTest,
646 AddRemoteCandidatesFromRemoteDescription) {
647 // We construct the following data-channel-only scenario. The caller collects
648 // IPv6 private local candidates and appends them in the Offer as in
649 // non-trickled sessions. The callee collects mDNS candidates that are not
650 // contained in the Answer as in Trickle ICE. Only the Offer and Answer are
651 // signaled and we expect a connection with prflx remote candidates at the
652 // caller side.
653 auto caller = CreatePeerConnectionWithPrivateIpv6LocalAddresses();
Henrik Boström62995db2022-01-03 08:58:10654 RTCConfiguration config;
655 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
656 auto callee = CreatePeerConnectionWithMdns(config);
Qingsi Wang1ba5dec2019-08-19 18:57:17657 caller->CreateDataChannel("test_channel");
658 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
659 // Wait until the gathering completes so that the session description would
660 // have contained ICE candidates.
Harald Alvestranda6544372023-11-13 09:33:56661 EXPECT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
Qingsi Wang1ba5dec2019-08-19 18:57:17662 caller->ice_gathering_state(), kDefaultTimeout);
663 EXPECT_TRUE(caller->observer()->candidate_gathered());
664 // Get the current offer that contains candidates and pass it to the callee.
665 //
Artem Titov880fa812021-07-30 20:30:23666 // Note that we cannot use CloneSessionDescription on `cur_offer` to obtain an
Qingsi Wang1ba5dec2019-08-19 18:57:17667 // SDP with candidates. The method above does not strictly copy everything, in
668 // particular, not copying the ICE candidates.
669 // TODO(qingsi): Technically, this is a bug. Fix it.
670 auto cur_offer = caller->pc()->local_description();
671 ASSERT_TRUE(cur_offer);
672 std::string sdp_with_candidates_str;
673 cur_offer->ToString(&sdp_with_candidates_str);
Mirko Bonadei317a1f02019-09-17 15:06:18674 auto offer = std::make_unique<JsepSessionDescription>(SdpType::kOffer);
Qingsi Wang1ba5dec2019-08-19 18:57:17675 ASSERT_TRUE(SdpDeserialize(sdp_with_candidates_str, offer.get(),
676 nullptr /* error */));
677 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
678
679 // By default, the Answer created does not contain ICE candidates.
680 auto answer = callee->CreateAnswer();
681 callee->SetLocalDescription(CloneSessionDescription(answer.get()));
682 caller->SetRemoteDescription(std::move(answer));
683 EXPECT_TRUE_WAIT(caller->IsConnected(), kDefaultTimeout);
684 EXPECT_TRUE_WAIT(callee->IsConnected(), kDefaultTimeout);
685 // The callee needs to process the open message to have the data channel open.
686 EXPECT_TRUE_WAIT(callee->observer()->last_datachannel_ != nullptr,
687 kDefaultTimeout);
688 caller->pc()->Close();
689 callee->pc()->Close();
690
691 // The caller should not have added any remote candidate either via
Qingsi Wangcc46b10c2019-09-12 18:19:01692 // AddIceCandidate or from the remote description. Also, the caller connects
693 // with the callee via a prflx candidate and hence no direct connection bit
694 // should be set.
Qingsi Wang1ba5dec2019-08-19 18:57:17695 int expected_fingerprint_caller = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53696 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
697 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
698 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::PRIVATE_CANDIDATE_COLLECTED,
699 UsageEvent::IPV6_CANDIDATE_COLLECTED, UsageEvent::ICE_STATE_CONNECTED,
700 UsageEvent::CLOSE_CALLED});
Qingsi Wang1ba5dec2019-08-19 18:57:17701
702 int expected_fingerprint_callee = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53703 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
704 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
705 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::MDNS_CANDIDATE_COLLECTED,
706 UsageEvent::REMOTE_CANDIDATE_ADDED,
707 UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED,
708 UsageEvent::REMOTE_IPV6_CANDIDATE_ADDED, UsageEvent::ICE_STATE_CONNECTED,
709 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
Harald Alvestranda6544372023-11-13 09:33:56710 EXPECT_METRIC_EQ(2, metrics::NumSamples(kUsagePatternMetric));
711 EXPECT_METRIC_EQ(
712 1, metrics::NumEvents(kUsagePatternMetric, expected_fingerprint_caller));
713 EXPECT_METRIC_EQ(
714 1, metrics::NumEvents(kUsagePatternMetric, expected_fingerprint_callee));
Qingsi Wang1ba5dec2019-08-19 18:57:17715}
716
Harald Alvestrandc0e97252018-07-26 08:39:55717TEST_F(PeerConnectionUsageHistogramTest, NotableUsageNoted) {
718 auto caller = CreatePeerConnection();
719 caller->CreateDataChannel("foo");
720 caller->GenerateOfferAndCollectCandidates();
721 caller->pc()->Close();
722 int expected_fingerprint = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53723 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
724 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::CLOSE_CALLED});
Harald Alvestranda6544372023-11-13 09:33:56725 EXPECT_METRIC_EQ(1, metrics::NumSamples(kUsagePatternMetric));
Ying Wangef3998f2019-12-09 12:06:53726 EXPECT_METRIC_TRUE(
727 expected_fingerprint == ObservedFingerprint() ||
728 (expected_fingerprint |
Harald Alvestrand44d0dff2020-10-09 05:43:53729 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) ==
Ying Wangef3998f2019-12-09 12:06:53730 ObservedFingerprint());
Florent Castelli8037fc62024-08-29 13:00:40731 EXPECT_METRIC_EQ(std::make_optional(ObservedFingerprint()),
Ying Wangef3998f2019-12-09 12:06:53732 caller->observer()->interesting_usage_detected());
Harald Alvestrandc0e97252018-07-26 08:39:55733}
Harald Alvestrand7a1c7f72018-08-01 08:50:16734
735TEST_F(PeerConnectionUsageHistogramTest, NotableUsageOnEventFiring) {
736 auto caller = CreatePeerConnection();
737 caller->CreateDataChannel("foo");
738 caller->GenerateOfferAndCollectCandidates();
739 int expected_fingerprint = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53740 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
741 UsageEvent::CANDIDATE_COLLECTED});
Harald Alvestranda6544372023-11-13 09:33:56742 EXPECT_METRIC_EQ(0, metrics::NumSamples(kUsagePatternMetric));
Harald Alvestrand7a1c7f72018-08-01 08:50:16743 caller->GetInternalPeerConnection()->RequestUsagePatternReportForTesting();
Harald Alvestranda6544372023-11-13 09:33:56744 EXPECT_METRIC_EQ_WAIT(1, metrics::NumSamples(kUsagePatternMetric),
Ying Wangef3998f2019-12-09 12:06:53745 kDefaultTimeout);
746 EXPECT_METRIC_TRUE(
747 expected_fingerprint == ObservedFingerprint() ||
748 (expected_fingerprint |
Harald Alvestrand44d0dff2020-10-09 05:43:53749 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) ==
Ying Wangef3998f2019-12-09 12:06:53750 ObservedFingerprint());
Florent Castelli8037fc62024-08-29 13:00:40751 EXPECT_METRIC_EQ(std::make_optional(ObservedFingerprint()),
Ying Wangef3998f2019-12-09 12:06:53752 caller->observer()->interesting_usage_detected());
Harald Alvestrand7a1c7f72018-08-01 08:50:16753}
754
755TEST_F(PeerConnectionUsageHistogramTest,
756 NoNotableUsageOnEventFiringAfterClose) {
757 auto caller = CreatePeerConnection();
758 caller->CreateDataChannel("foo");
759 caller->GenerateOfferAndCollectCandidates();
760 int expected_fingerprint = MakeUsageFingerprint(
Harald Alvestrand44d0dff2020-10-09 05:43:53761 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
762 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::CLOSE_CALLED});
Harald Alvestranda6544372023-11-13 09:33:56763 EXPECT_METRIC_EQ(0, metrics::NumSamples(kUsagePatternMetric));
Harald Alvestrand7a1c7f72018-08-01 08:50:16764 caller->pc()->Close();
Harald Alvestranda6544372023-11-13 09:33:56765 EXPECT_METRIC_EQ(1, metrics::NumSamples(kUsagePatternMetric));
Harald Alvestrand7a1c7f72018-08-01 08:50:16766 caller->GetInternalPeerConnection()->RequestUsagePatternReportForTesting();
767 caller->observer()->ClearInterestingUsageDetector();
Harald Alvestranda6544372023-11-13 09:33:56768 EXPECT_METRIC_EQ_WAIT(2, metrics::NumSamples(kUsagePatternMetric),
Ying Wangef3998f2019-12-09 12:06:53769 kDefaultTimeout);
770 EXPECT_METRIC_TRUE(
771 expected_fingerprint == ObservedFingerprint() ||
772 (expected_fingerprint |
Harald Alvestrand44d0dff2020-10-09 05:43:53773 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) ==
Ying Wangef3998f2019-12-09 12:06:53774 ObservedFingerprint());
Harald Alvestrand7a1c7f72018-08-01 08:50:16775 // After close, the usage-detection callback should NOT have been called.
Ying Wangef3998f2019-12-09 12:06:53776 EXPECT_METRIC_FALSE(caller->observer()->interesting_usage_detected());
Harald Alvestrand7a1c7f72018-08-01 08:50:16777}
Harald Alvestrandc0e97252018-07-26 08:39:55778#endif
779#endif
780
Harald Alvestrand19793842018-06-25 10:03:50781} // namespace webrtc