1 /*
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
11 #include <memory>
12 #include <set>
13 #include <string>
14 #include <utility>
15 #include <vector>
16
17 #include "absl/types/optional.h"
18 #include "api/call/call_factory_interface.h"
19 #include "api/jsep.h"
20 #include "api/jsep_session_description.h"
21 #include "api/peer_connection_interface.h"
22 #include "api/peer_connection_proxy.h"
23 #include "api/rtc_error.h"
24 #include "api/scoped_refptr.h"
25 #include "api/task_queue/default_task_queue_factory.h"
26 #include "media/base/fake_media_engine.h"
27 #include "p2p/base/mock_async_resolver.h"
28 #include "p2p/base/port_allocator.h"
29 #include "p2p/client/basic_port_allocator.h"
30 #include "pc/peer_connection.h"
31 #include "pc/peer_connection_factory.h"
32 #include "pc/peer_connection_wrapper.h"
33 #include "pc/sdp_utils.h"
34 #include "pc/test/mock_peer_connection_observers.h"
35 #include "pc/usage_pattern.h"
36 #include "pc/webrtc_sdp.h"
37 #include "rtc_base/arraysize.h"
38 #include "rtc_base/checks.h"
39 #include "rtc_base/fake_mdns_responder.h"
40 #include "rtc_base/fake_network.h"
41 #include "rtc_base/gunit.h"
42 #include "rtc_base/ref_counted_object.h"
43 #include "rtc_base/rtc_certificate_generator.h"
44 #include "rtc_base/socket_address.h"
45 #include "rtc_base/thread.h"
46 #include "rtc_base/virtual_socket_server.h"
47 #include "system_wrappers/include/metrics.h"
48 #include "test/gmock.h"
49
50 namespace webrtc {
51
52 using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
53 using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
54 using ::testing::NiceMock;
55 using ::testing::Values;
56
57 static const char kUsagePatternMetric[] = "WebRTC.PeerConnection.UsagePattern";
58 static constexpr int kDefaultTimeout = 10000;
59 static const rtc::SocketAddress kLocalAddrs[2] = {
60 rtc::SocketAddress("1.1.1.1", 0), rtc::SocketAddress("2.2.2.2", 0)};
61 static const rtc::SocketAddress kPrivateLocalAddress("10.1.1.1", 0);
62 static const rtc::SocketAddress kPrivateIpv6LocalAddress("fd12:3456:789a:1::1",
63 0);
64
MakeUsageFingerprint(std::set<UsageEvent> events)65 int MakeUsageFingerprint(std::set<UsageEvent> events) {
66 int signature = 0;
67 for (const auto it : events) {
68 signature |= static_cast<int>(it);
69 }
70 return signature;
71 }
72
73 class PeerConnectionFactoryForUsageHistogramTest
74 : public rtc::RefCountedObject<PeerConnectionFactory> {
75 public:
PeerConnectionFactoryForUsageHistogramTest()76 PeerConnectionFactoryForUsageHistogramTest()
77 : 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();
82 dependencies.task_queue_factory = CreateDefaultTaskQueueFactory();
83 dependencies.media_engine =
84 std::make_unique<cricket::FakeMediaEngine>();
85 dependencies.call_factory = CreateCallFactory();
86 return dependencies;
87 }()) {}
88 };
89
90 class PeerConnectionWrapperForUsageHistogramTest;
91
92 typedef PeerConnectionWrapperForUsageHistogramTest* RawWrapperPtr;
93
94 class ObserverForUsageHistogramTest : public MockPeerConnectionObserver {
95 public:
96 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override;
97
OnInterestingUsage(int usage_pattern)98 void OnInterestingUsage(int usage_pattern) override {
99 interesting_usage_detected_ = usage_pattern;
100 }
101
PrepareToExchangeCandidates(RawWrapperPtr other)102 void PrepareToExchangeCandidates(RawWrapperPtr other) {
103 candidate_target_ = other;
104 }
105
HaveDataChannel()106 bool HaveDataChannel() { return last_datachannel_; }
107
interesting_usage_detected()108 absl::optional<int> interesting_usage_detected() {
109 return interesting_usage_detected_;
110 }
111
ClearInterestingUsageDetector()112 void ClearInterestingUsageDetector() {
113 interesting_usage_detected_ = absl::optional<int>();
114 }
115
candidate_gathered() const116 bool candidate_gathered() const { return candidate_gathered_; }
117
118 private:
119 absl::optional<int> interesting_usage_detected_;
120 bool candidate_gathered_ = false;
121 RawWrapperPtr candidate_target_; // Note: Not thread-safe against deletions.
122 };
123
124 class PeerConnectionWrapperForUsageHistogramTest
125 : public PeerConnectionWrapper {
126 public:
127 using PeerConnectionWrapper::PeerConnectionWrapper;
128
GetInternalPeerConnection()129 PeerConnection* GetInternalPeerConnection() {
130 auto* pci =
131 static_cast<PeerConnectionProxyWithInternal<PeerConnectionInterface>*>(
132 pc());
133 return static_cast<PeerConnection*>(pci->internal());
134 }
135
136 // Override with different return type
observer()137 ObserverForUsageHistogramTest* observer() {
138 return static_cast<ObserverForUsageHistogramTest*>(
139 PeerConnectionWrapper::observer());
140 }
141
PrepareToExchangeCandidates(PeerConnectionWrapperForUsageHistogramTest * other)142 void PrepareToExchangeCandidates(
143 PeerConnectionWrapperForUsageHistogramTest* other) {
144 observer()->PrepareToExchangeCandidates(other);
145 other->observer()->PrepareToExchangeCandidates(this);
146 }
147
IsConnected()148 bool IsConnected() {
149 return pc()->ice_connection_state() ==
150 PeerConnectionInterface::kIceConnectionConnected ||
151 pc()->ice_connection_state() ==
152 PeerConnectionInterface::kIceConnectionCompleted;
153 }
154
HaveDataChannel()155 bool HaveDataChannel() {
156 return static_cast<ObserverForUsageHistogramTest*>(observer())
157 ->HaveDataChannel();
158 }
BufferIceCandidate(const webrtc::IceCandidateInterface * candidate)159 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));
166 }
167
AddBufferedIceCandidates()168 void AddBufferedIceCandidates() {
169 for (const auto& candidate : buffered_candidates_) {
170 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
171 }
172 buffered_candidates_.clear();
173 }
174
175 // 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.
ConnectTo(PeerConnectionWrapperForUsageHistogramTest * callee)185 bool ConnectTo(PeerConnectionWrapperForUsageHistogramTest* callee) {
186 PrepareToExchangeCandidates(callee);
187 if (!ExchangeOfferAnswerWith(callee)) {
188 return false;
189 }
190 // 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);
193 AddBufferedIceCandidates();
194 callee->AddBufferedIceCandidates();
195 WAIT(IsConnected(), kDefaultTimeout);
196 WAIT(callee->IsConnected(), kDefaultTimeout);
197 return IsConnected() && callee->IsConnected();
198 }
199
GenerateOfferAndCollectCandidates()200 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
ice_gathering_state()215 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
216 return pc()->ice_gathering_state();
217 }
218
219 private:
220 // Candidates that have been sent but not yet configured
221 std::vector<std::unique_ptr<webrtc::IceCandidateInterface>>
222 buffered_candidates_;
223 };
224
225 // Buffers candidates until we add them via AddBufferedIceCandidates.
OnIceCandidate(const webrtc::IceCandidateInterface * candidate)226 void ObserverForUsageHistogramTest::OnIceCandidate(
227 const webrtc::IceCandidateInterface* candidate) {
228 // If target is not set, ignore. This happens in one-ended unit tests.
229 if (candidate_target_) {
230 this->candidate_target_->BufferIceCandidate(candidate);
231 }
232 candidate_gathered_ = true;
233 }
234
235 class PeerConnectionUsageHistogramTest : public ::testing::Test {
236 protected:
237 typedef std::unique_ptr<PeerConnectionWrapperForUsageHistogramTest>
238 WrapperPtr;
239
PeerConnectionUsageHistogramTest()240 PeerConnectionUsageHistogramTest()
241 : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) {
242 webrtc::metrics::Reset();
243 }
244
CreatePeerConnection()245 WrapperPtr CreatePeerConnection() {
246 return CreatePeerConnection(
247 RTCConfiguration(), PeerConnectionFactoryInterface::Options(), nullptr);
248 }
249
CreatePeerConnection(const RTCConfiguration & config)250 WrapperPtr CreatePeerConnection(const RTCConfiguration& config) {
251 return CreatePeerConnection(
252 config, PeerConnectionFactoryInterface::Options(), nullptr);
253 }
254
CreatePeerConnectionWithMdns(const RTCConfiguration & config)255 WrapperPtr CreatePeerConnectionWithMdns(const RTCConfiguration& config) {
256 auto resolver_factory =
257 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
258
259 webrtc::PeerConnectionDependencies deps(nullptr /* observer_in */);
260
261 auto fake_network = NewFakeNetwork();
262 fake_network->set_mdns_responder(
263 std::make_unique<webrtc::FakeMdnsResponder>(rtc::Thread::Current()));
264 fake_network->AddInterface(NextLocalAddress());
265
266 std::unique_ptr<cricket::BasicPortAllocator> port_allocator(
267 new cricket::BasicPortAllocator(fake_network));
268
269 deps.async_resolver_factory = std::move(resolver_factory);
270 deps.allocator = std::move(port_allocator);
271
272 return CreatePeerConnection(
273 config, PeerConnectionFactoryInterface::Options(), std::move(deps));
274 }
275
CreatePeerConnectionWithImmediateReport()276 WrapperPtr CreatePeerConnectionWithImmediateReport() {
277 RTCConfiguration configuration;
278 configuration.report_usage_pattern_delay_ms = 0;
279 return CreatePeerConnection(
280 configuration, PeerConnectionFactoryInterface::Options(), nullptr);
281 }
282
CreatePeerConnectionWithPrivateLocalAddresses()283 WrapperPtr CreatePeerConnectionWithPrivateLocalAddresses() {
284 auto* fake_network = NewFakeNetwork();
285 fake_network->AddInterface(NextLocalAddress());
286 fake_network->AddInterface(kPrivateLocalAddress);
287
288 auto port_allocator =
289 std::make_unique<cricket::BasicPortAllocator>(fake_network);
290 return CreatePeerConnection(RTCConfiguration(),
291 PeerConnectionFactoryInterface::Options(),
292 std::move(port_allocator));
293 }
294
CreatePeerConnectionWithPrivateIpv6LocalAddresses()295 WrapperPtr CreatePeerConnectionWithPrivateIpv6LocalAddresses() {
296 auto* fake_network = NewFakeNetwork();
297 fake_network->AddInterface(NextLocalAddress());
298 fake_network->AddInterface(kPrivateIpv6LocalAddress);
299
300 auto port_allocator =
301 std::make_unique<cricket::BasicPortAllocator>(fake_network);
302
303 return CreatePeerConnection(RTCConfiguration(),
304 PeerConnectionFactoryInterface::Options(),
305 std::move(port_allocator));
306 }
307
CreatePeerConnection(const RTCConfiguration & config,const PeerConnectionFactoryInterface::Options factory_options,std::unique_ptr<cricket::PortAllocator> allocator)308 WrapperPtr CreatePeerConnection(
309 const RTCConfiguration& config,
310 const PeerConnectionFactoryInterface::Options factory_options,
311 std::unique_ptr<cricket::PortAllocator> allocator) {
312 PeerConnectionDependencies deps(nullptr);
313 deps.allocator = std::move(allocator);
314
315 return CreatePeerConnection(config, factory_options, std::move(deps));
316 }
317
CreatePeerConnection(const RTCConfiguration & config,const PeerConnectionFactoryInterface::Options factory_options,PeerConnectionDependencies deps)318 WrapperPtr CreatePeerConnection(
319 const RTCConfiguration& config,
320 const PeerConnectionFactoryInterface::Options factory_options,
321 PeerConnectionDependencies deps) {
322 rtc::scoped_refptr<PeerConnectionFactoryForUsageHistogramTest> pc_factory(
323 new PeerConnectionFactoryForUsageHistogramTest());
324 pc_factory->SetOptions(factory_options);
325
326 // If no allocator is provided, one will be created using a network manager
327 // that uses the host network. This doesn't work on all trybots.
328 if (!deps.allocator) {
329 auto fake_network = NewFakeNetwork();
330 fake_network->AddInterface(NextLocalAddress());
331 deps.allocator =
332 std::make_unique<cricket::BasicPortAllocator>(fake_network);
333 }
334
335 auto observer = std::make_unique<ObserverForUsageHistogramTest>();
336 deps.observer = observer.get();
337
338 auto pc = pc_factory->CreatePeerConnection(config, std::move(deps));
339 if (!pc) {
340 return nullptr;
341 }
342
343 observer->SetPeerConnectionInterface(pc.get());
344 auto wrapper = std::make_unique<PeerConnectionWrapperForUsageHistogramTest>(
345 pc_factory, pc, std::move(observer));
346 return wrapper;
347 }
348
ObservedFingerprint()349 int ObservedFingerprint() {
350 // This works correctly only if there is only one sample value
351 // that has been counted.
352 // Returns -1 for "not found".
353 return webrtc::metrics::MinSample(kUsagePatternMetric);
354 }
355
356 // The PeerConnection's port allocator is tied to the PeerConnection's
357 // lifetime and expects the underlying NetworkManager to outlive it. That
358 // prevents us from having the PeerConnectionWrapper own the fake network.
359 // Therefore, the test fixture will own all the fake networks even though
360 // tests should access the fake network through the PeerConnectionWrapper.
NewFakeNetwork()361 rtc::FakeNetworkManager* NewFakeNetwork() {
362 fake_networks_.emplace_back(std::make_unique<rtc::FakeNetworkManager>());
363 return fake_networks_.back().get();
364 }
365
NextLocalAddress()366 rtc::SocketAddress NextLocalAddress() {
367 RTC_DCHECK(next_local_address_ < (int)arraysize(kLocalAddrs));
368 return kLocalAddrs[next_local_address_++];
369 }
370
371 std::vector<std::unique_ptr<rtc::FakeNetworkManager>> fake_networks_;
372 int next_local_address_ = 0;
373 std::unique_ptr<rtc::VirtualSocketServer> vss_;
374 rtc::AutoSocketServerThread main_;
375 };
376
TEST_F(PeerConnectionUsageHistogramTest,UsageFingerprintHistogramFromTimeout)377 TEST_F(PeerConnectionUsageHistogramTest, UsageFingerprintHistogramFromTimeout) {
378 auto pc = CreatePeerConnectionWithImmediateReport();
379
380 int expected_fingerprint = MakeUsageFingerprint({});
381 EXPECT_METRIC_EQ_WAIT(1, webrtc::metrics::NumSamples(kUsagePatternMetric),
382 kDefaultTimeout);
383 EXPECT_METRIC_EQ(
384 1, webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint));
385 }
386
387 #ifndef WEBRTC_ANDROID
388 // These tests do not work on Android. Why is unclear.
389 // https://bugs.webrtc.org/9461
390
391 // Test getting the usage fingerprint for an audio/video connection.
TEST_F(PeerConnectionUsageHistogramTest,FingerprintAudioVideo)392 TEST_F(PeerConnectionUsageHistogramTest, FingerprintAudioVideo) {
393 auto caller = CreatePeerConnection();
394 auto callee = CreatePeerConnection();
395 caller->AddAudioTrack("audio");
396 caller->AddVideoTrack("video");
397 ASSERT_TRUE(caller->ConnectTo(callee.get()));
398 caller->pc()->Close();
399 callee->pc()->Close();
400 int expected_fingerprint = MakeUsageFingerprint(
401 {UsageEvent::AUDIO_ADDED, UsageEvent::VIDEO_ADDED,
402 UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
403 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
404 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
405 UsageEvent::ICE_STATE_CONNECTED, UsageEvent::REMOTE_CANDIDATE_ADDED,
406 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
407 // In this case, we may or may not have PRIVATE_CANDIDATE_COLLECTED,
408 // depending on the machine configuration.
409 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
410 EXPECT_METRIC_TRUE(
411 webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint) ==
412 2 ||
413 webrtc::metrics::NumEvents(
414 kUsagePatternMetric,
415 expected_fingerprint |
416 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) == 2);
417 }
418
419 // Test getting the usage fingerprint when the caller collects an mDNS
420 // candidate.
TEST_F(PeerConnectionUsageHistogramTest,FingerprintWithMdnsCaller)421 TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithMdnsCaller) {
422 RTCConfiguration config;
423
424 // Enable hostname candidates with mDNS names.
425 auto caller = CreatePeerConnectionWithMdns(config);
426 auto callee = CreatePeerConnection(config);
427
428 caller->AddAudioTrack("audio");
429 caller->AddVideoTrack("video");
430 ASSERT_TRUE(caller->ConnectTo(callee.get()));
431 caller->pc()->Close();
432 callee->pc()->Close();
433
434 int expected_fingerprint_caller = MakeUsageFingerprint(
435 {UsageEvent::AUDIO_ADDED, UsageEvent::VIDEO_ADDED,
436 UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
437 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
438 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::MDNS_CANDIDATE_COLLECTED,
439 UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED, UsageEvent::ICE_STATE_CONNECTED,
440 UsageEvent::REMOTE_CANDIDATE_ADDED,
441 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
442
443 // Without a resolver, the callee cannot resolve the received mDNS candidate
444 // but can still connect with the caller via a prflx candidate. As a result,
445 // the bit for the direct connection should not be logged.
446 int expected_fingerprint_callee = MakeUsageFingerprint(
447 {UsageEvent::AUDIO_ADDED, UsageEvent::VIDEO_ADDED,
448 UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
449 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
450 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
451 UsageEvent::REMOTE_MDNS_CANDIDATE_ADDED, UsageEvent::ICE_STATE_CONNECTED,
452 UsageEvent::REMOTE_CANDIDATE_ADDED, UsageEvent::CLOSE_CALLED});
453 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
454 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
455 expected_fingerprint_caller));
456 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
457 expected_fingerprint_callee));
458 }
459
460 // Test getting the usage fingerprint when the callee collects an mDNS
461 // candidate.
TEST_F(PeerConnectionUsageHistogramTest,FingerprintWithMdnsCallee)462 TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithMdnsCallee) {
463 RTCConfiguration config;
464
465 // Enable hostname candidates with mDNS names.
466 auto caller = CreatePeerConnection(config);
467 auto callee = CreatePeerConnectionWithMdns(config);
468
469 caller->AddAudioTrack("audio");
470 caller->AddVideoTrack("video");
471 ASSERT_TRUE(caller->ConnectTo(callee.get()));
472 caller->pc()->Close();
473 callee->pc()->Close();
474
475 // Similar to the test above, the caller connects with the callee via a prflx
476 // candidate.
477 int expected_fingerprint_caller = MakeUsageFingerprint(
478 {UsageEvent::AUDIO_ADDED, UsageEvent::VIDEO_ADDED,
479 UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
480 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
481 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
482 UsageEvent::REMOTE_MDNS_CANDIDATE_ADDED, UsageEvent::ICE_STATE_CONNECTED,
483 UsageEvent::REMOTE_CANDIDATE_ADDED, UsageEvent::CLOSE_CALLED});
484
485 int expected_fingerprint_callee = MakeUsageFingerprint(
486 {UsageEvent::AUDIO_ADDED, UsageEvent::VIDEO_ADDED,
487 UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
488 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
489 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::MDNS_CANDIDATE_COLLECTED,
490 UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED, UsageEvent::ICE_STATE_CONNECTED,
491 UsageEvent::REMOTE_CANDIDATE_ADDED,
492 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
493 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
494 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
495 expected_fingerprint_caller));
496 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
497 expected_fingerprint_callee));
498 }
499
500 #ifdef WEBRTC_HAVE_SCTP
TEST_F(PeerConnectionUsageHistogramTest,FingerprintDataOnly)501 TEST_F(PeerConnectionUsageHistogramTest, FingerprintDataOnly) {
502 auto caller = CreatePeerConnection();
503 auto callee = CreatePeerConnection();
504 caller->CreateDataChannel("foodata");
505 ASSERT_TRUE(caller->ConnectTo(callee.get()));
506 ASSERT_TRUE_WAIT(callee->HaveDataChannel(), kDefaultTimeout);
507 caller->pc()->Close();
508 callee->pc()->Close();
509 int expected_fingerprint = MakeUsageFingerprint(
510 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
511 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
512 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
513 UsageEvent::ICE_STATE_CONNECTED, UsageEvent::REMOTE_CANDIDATE_ADDED,
514 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
515 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
516 EXPECT_METRIC_TRUE(
517 webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint) ==
518 2 ||
519 webrtc::metrics::NumEvents(
520 kUsagePatternMetric,
521 expected_fingerprint |
522 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) == 2);
523 }
524 #endif // WEBRTC_HAVE_SCTP
525 #endif // WEBRTC_ANDROID
526
TEST_F(PeerConnectionUsageHistogramTest,FingerprintStunTurn)527 TEST_F(PeerConnectionUsageHistogramTest, FingerprintStunTurn) {
528 RTCConfiguration configuration;
529 PeerConnection::IceServer server;
530 server.urls = {"stun:dummy.stun.server"};
531 configuration.servers.push_back(server);
532 server.urls = {"turn:dummy.turn.server"};
533 server.username = "username";
534 server.password = "password";
535 configuration.servers.push_back(server);
536 auto caller = CreatePeerConnection(configuration);
537 ASSERT_TRUE(caller);
538 caller->pc()->Close();
539 int expected_fingerprint = MakeUsageFingerprint(
540 {UsageEvent::STUN_SERVER_ADDED, UsageEvent::TURN_SERVER_ADDED,
541 UsageEvent::CLOSE_CALLED});
542 EXPECT_METRIC_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric));
543 EXPECT_METRIC_EQ(
544 1, webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint));
545 }
546
TEST_F(PeerConnectionUsageHistogramTest,FingerprintStunTurnInReconfiguration)547 TEST_F(PeerConnectionUsageHistogramTest, FingerprintStunTurnInReconfiguration) {
548 RTCConfiguration configuration;
549 PeerConnection::IceServer server;
550 server.urls = {"stun:dummy.stun.server"};
551 configuration.servers.push_back(server);
552 server.urls = {"turn:dummy.turn.server"};
553 server.username = "username";
554 server.password = "password";
555 configuration.servers.push_back(server);
556 auto caller = CreatePeerConnection();
557 ASSERT_TRUE(caller);
558 ASSERT_TRUE(caller->pc()->SetConfiguration(configuration).ok());
559 caller->pc()->Close();
560 int expected_fingerprint = MakeUsageFingerprint(
561 {UsageEvent::STUN_SERVER_ADDED, UsageEvent::TURN_SERVER_ADDED,
562 UsageEvent::CLOSE_CALLED});
563 EXPECT_METRIC_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric));
564 EXPECT_METRIC_EQ(
565 1, webrtc::metrics::NumEvents(kUsagePatternMetric, expected_fingerprint));
566 }
567
TEST_F(PeerConnectionUsageHistogramTest,FingerprintWithPrivateIPCaller)568 TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithPrivateIPCaller) {
569 auto caller = CreatePeerConnectionWithPrivateLocalAddresses();
570 auto callee = CreatePeerConnection();
571 caller->AddAudioTrack("audio");
572 ASSERT_TRUE(caller->ConnectTo(callee.get()));
573 caller->pc()->Close();
574 callee->pc()->Close();
575
576 int expected_fingerprint_caller = MakeUsageFingerprint(
577 {UsageEvent::AUDIO_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
578 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
579 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::PRIVATE_CANDIDATE_COLLECTED,
580 UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED, UsageEvent::ICE_STATE_CONNECTED,
581 UsageEvent::REMOTE_CANDIDATE_ADDED,
582 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
583
584 int expected_fingerprint_callee = MakeUsageFingerprint(
585 {UsageEvent::AUDIO_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
586 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
587 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
588 UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED,
589 UsageEvent::ICE_STATE_CONNECTED, UsageEvent::REMOTE_CANDIDATE_ADDED,
590 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
591 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
592 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
593 expected_fingerprint_caller));
594 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
595 expected_fingerprint_callee));
596 }
597
TEST_F(PeerConnectionUsageHistogramTest,FingerprintWithPrivateIpv6Callee)598 TEST_F(PeerConnectionUsageHistogramTest, FingerprintWithPrivateIpv6Callee) {
599 auto caller = CreatePeerConnection();
600 auto callee = CreatePeerConnectionWithPrivateIpv6LocalAddresses();
601 caller->AddAudioTrack("audio");
602 ASSERT_TRUE(caller->ConnectTo(callee.get()));
603 caller->pc()->Close();
604 callee->pc()->Close();
605
606 int expected_fingerprint_caller = MakeUsageFingerprint(
607 {UsageEvent::AUDIO_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
608 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
609 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
610 UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED,
611 UsageEvent::ICE_STATE_CONNECTED, UsageEvent::REMOTE_CANDIDATE_ADDED,
612 UsageEvent::REMOTE_IPV6_CANDIDATE_ADDED,
613 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
614
615 int expected_fingerprint_callee = MakeUsageFingerprint(
616 {UsageEvent::AUDIO_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
617 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
618 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::PRIVATE_CANDIDATE_COLLECTED,
619 UsageEvent::IPV6_CANDIDATE_COLLECTED,
620 UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED,
621 UsageEvent::REMOTE_CANDIDATE_ADDED, UsageEvent::ICE_STATE_CONNECTED,
622 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
623 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
624 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
625 expected_fingerprint_caller));
626 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
627 expected_fingerprint_callee));
628 }
629
630 #ifndef WEBRTC_ANDROID
631 #ifdef WEBRTC_HAVE_SCTP
632 // Test that the usage pattern bits for adding remote (private IPv6) candidates
633 // are set when the remote candidates are retrieved from the Offer SDP instead
634 // of trickled ICE messages.
TEST_F(PeerConnectionUsageHistogramTest,AddRemoteCandidatesFromRemoteDescription)635 TEST_F(PeerConnectionUsageHistogramTest,
636 AddRemoteCandidatesFromRemoteDescription) {
637 // We construct the following data-channel-only scenario. The caller collects
638 // IPv6 private local candidates and appends them in the Offer as in
639 // non-trickled sessions. The callee collects mDNS candidates that are not
640 // contained in the Answer as in Trickle ICE. Only the Offer and Answer are
641 // signaled and we expect a connection with prflx remote candidates at the
642 // caller side.
643 auto caller = CreatePeerConnectionWithPrivateIpv6LocalAddresses();
644 auto callee = CreatePeerConnectionWithMdns(RTCConfiguration());
645 caller->CreateDataChannel("test_channel");
646 ASSERT_TRUE(caller->SetLocalDescription(caller->CreateOffer()));
647 // Wait until the gathering completes so that the session description would
648 // have contained ICE candidates.
649 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
650 caller->ice_gathering_state(), kDefaultTimeout);
651 EXPECT_TRUE(caller->observer()->candidate_gathered());
652 // Get the current offer that contains candidates and pass it to the callee.
653 //
654 // Note that we cannot use CloneSessionDescription on |cur_offer| to obtain an
655 // SDP with candidates. The method above does not strictly copy everything, in
656 // particular, not copying the ICE candidates.
657 // TODO(qingsi): Technically, this is a bug. Fix it.
658 auto cur_offer = caller->pc()->local_description();
659 ASSERT_TRUE(cur_offer);
660 std::string sdp_with_candidates_str;
661 cur_offer->ToString(&sdp_with_candidates_str);
662 auto offer = std::make_unique<JsepSessionDescription>(SdpType::kOffer);
663 ASSERT_TRUE(SdpDeserialize(sdp_with_candidates_str, offer.get(),
664 nullptr /* error */));
665 ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer)));
666
667 // By default, the Answer created does not contain ICE candidates.
668 auto answer = callee->CreateAnswer();
669 callee->SetLocalDescription(CloneSessionDescription(answer.get()));
670 caller->SetRemoteDescription(std::move(answer));
671 EXPECT_TRUE_WAIT(caller->IsConnected(), kDefaultTimeout);
672 EXPECT_TRUE_WAIT(callee->IsConnected(), kDefaultTimeout);
673 // The callee needs to process the open message to have the data channel open.
674 EXPECT_TRUE_WAIT(callee->observer()->last_datachannel_ != nullptr,
675 kDefaultTimeout);
676 caller->pc()->Close();
677 callee->pc()->Close();
678
679 // The caller should not have added any remote candidate either via
680 // AddIceCandidate or from the remote description. Also, the caller connects
681 // with the callee via a prflx candidate and hence no direct connection bit
682 // should be set.
683 int expected_fingerprint_caller = MakeUsageFingerprint(
684 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
685 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
686 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::PRIVATE_CANDIDATE_COLLECTED,
687 UsageEvent::IPV6_CANDIDATE_COLLECTED, UsageEvent::ICE_STATE_CONNECTED,
688 UsageEvent::CLOSE_CALLED});
689
690 int expected_fingerprint_callee = MakeUsageFingerprint(
691 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
692 UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED,
693 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::MDNS_CANDIDATE_COLLECTED,
694 UsageEvent::REMOTE_CANDIDATE_ADDED,
695 UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED,
696 UsageEvent::REMOTE_IPV6_CANDIDATE_ADDED, UsageEvent::ICE_STATE_CONNECTED,
697 UsageEvent::DIRECT_CONNECTION_SELECTED, UsageEvent::CLOSE_CALLED});
698 EXPECT_METRIC_EQ(2, webrtc::metrics::NumSamples(kUsagePatternMetric));
699 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
700 expected_fingerprint_caller));
701 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(kUsagePatternMetric,
702 expected_fingerprint_callee));
703 }
704
TEST_F(PeerConnectionUsageHistogramTest,NotableUsageNoted)705 TEST_F(PeerConnectionUsageHistogramTest, NotableUsageNoted) {
706 auto caller = CreatePeerConnection();
707 caller->CreateDataChannel("foo");
708 caller->GenerateOfferAndCollectCandidates();
709 caller->pc()->Close();
710 int expected_fingerprint = MakeUsageFingerprint(
711 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
712 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::CLOSE_CALLED});
713 EXPECT_METRIC_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric));
714 EXPECT_METRIC_TRUE(
715 expected_fingerprint == ObservedFingerprint() ||
716 (expected_fingerprint |
717 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) ==
718 ObservedFingerprint());
719 EXPECT_METRIC_EQ(absl::make_optional(ObservedFingerprint()),
720 caller->observer()->interesting_usage_detected());
721 }
722
TEST_F(PeerConnectionUsageHistogramTest,NotableUsageOnEventFiring)723 TEST_F(PeerConnectionUsageHistogramTest, NotableUsageOnEventFiring) {
724 auto caller = CreatePeerConnection();
725 caller->CreateDataChannel("foo");
726 caller->GenerateOfferAndCollectCandidates();
727 int expected_fingerprint = MakeUsageFingerprint(
728 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
729 UsageEvent::CANDIDATE_COLLECTED});
730 EXPECT_METRIC_EQ(0, webrtc::metrics::NumSamples(kUsagePatternMetric));
731 caller->GetInternalPeerConnection()->RequestUsagePatternReportForTesting();
732 EXPECT_METRIC_EQ_WAIT(1, webrtc::metrics::NumSamples(kUsagePatternMetric),
733 kDefaultTimeout);
734 EXPECT_METRIC_TRUE(
735 expected_fingerprint == ObservedFingerprint() ||
736 (expected_fingerprint |
737 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) ==
738 ObservedFingerprint());
739 EXPECT_METRIC_EQ(absl::make_optional(ObservedFingerprint()),
740 caller->observer()->interesting_usage_detected());
741 }
742
TEST_F(PeerConnectionUsageHistogramTest,NoNotableUsageOnEventFiringAfterClose)743 TEST_F(PeerConnectionUsageHistogramTest,
744 NoNotableUsageOnEventFiringAfterClose) {
745 auto caller = CreatePeerConnection();
746 caller->CreateDataChannel("foo");
747 caller->GenerateOfferAndCollectCandidates();
748 int expected_fingerprint = MakeUsageFingerprint(
749 {UsageEvent::DATA_ADDED, UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED,
750 UsageEvent::CANDIDATE_COLLECTED, UsageEvent::CLOSE_CALLED});
751 EXPECT_METRIC_EQ(0, webrtc::metrics::NumSamples(kUsagePatternMetric));
752 caller->pc()->Close();
753 EXPECT_METRIC_EQ(1, webrtc::metrics::NumSamples(kUsagePatternMetric));
754 caller->GetInternalPeerConnection()->RequestUsagePatternReportForTesting();
755 caller->observer()->ClearInterestingUsageDetector();
756 EXPECT_METRIC_EQ_WAIT(2, webrtc::metrics::NumSamples(kUsagePatternMetric),
757 kDefaultTimeout);
758 EXPECT_METRIC_TRUE(
759 expected_fingerprint == ObservedFingerprint() ||
760 (expected_fingerprint |
761 static_cast<int>(UsageEvent::PRIVATE_CANDIDATE_COLLECTED)) ==
762 ObservedFingerprint());
763 // After close, the usage-detection callback should NOT have been called.
764 EXPECT_METRIC_FALSE(caller->observer()->interesting_usage_detected());
765 }
766 #endif
767 #endif
768
769 } // namespace webrtc
770